Revision da24c5e3 libavformat/utils.c

View differences:

libavformat/utils.c
274 274
/************************************************************/
275 275
/* input media file */
276 276

  
277
/**
278
 * open a media file from an IO stream. 'fmt' must be specified.
279
 */
280
int av_open_input_stream(AVFormatContext **ic_ptr, 
281
                         ByteIOContext *pb, const char *filename, 
282
                         AVInputFormat *fmt, AVFormatParameters *ap)
283
{
284
    int err;
285
    AVFormatContext *ic;
286

  
287
    ic = av_mallocz(sizeof(AVFormatContext));
288
    if (!ic) {
289
        err = AVERROR_NOMEM;
290
        goto fail;
291
    }
292
    ic->iformat = fmt;
293
    if (pb)
294
        ic->pb = *pb;
295
    ic->duration = AV_NOPTS_VALUE;
296
    ic->start_time = AV_NOPTS_VALUE;
297
    pstrcpy(ic->filename, sizeof(ic->filename), filename);
298

  
299
    /* allocate private data */
300
    if (fmt->priv_data_size > 0) {
301
        ic->priv_data = av_mallocz(fmt->priv_data_size);
302
        if (!ic->priv_data) {
303
            err = AVERROR_NOMEM;
304
            goto fail;
305
        }
306
    } else {
307
        ic->priv_data = NULL;
308
    }
309

  
310
    /* default pts settings is MPEG like */
311
    av_set_pts_info(ic, 33, 1, 90000);
312

  
313
    err = ic->iformat->read_header(ic, ap);
314
    if (err < 0)
315
        goto fail;
316
    *ic_ptr = ic;
317
    return 0;
318
 fail:
319
    if (ic) {
320
        av_freep(&ic->priv_data);
321
    }
322
    av_free(ic);
323
    *ic_ptr = NULL;
324
    return err;
325
}
326

  
277 327
#define PROBE_BUF_SIZE 2048
278 328

  
279 329
/**
......
292 342
                       int buf_size,
293 343
                       AVFormatParameters *ap)
294 344
{
295
    AVFormatContext *ic = NULL;
296
    int err, must_open_file;
297
    unsigned char buf[PROBE_BUF_SIZE];
345
    int err, must_open_file, file_opened;
346
    uint8_t buf[PROBE_BUF_SIZE];
298 347
    AVProbeData probe_data, *pd = &probe_data;
299

  
300
    ic = av_mallocz(sizeof(AVFormatContext));
301
    if (!ic) {
302
        err = AVERROR_NOMEM;
303
        goto fail;
304
    }
305
    ic->duration = AV_NOPTS_VALUE;
306
    ic->start_time = AV_NOPTS_VALUE;
307
    pstrcpy(ic->filename, sizeof(ic->filename), filename);
308
    pd->filename = ic->filename;
348
    ByteIOContext pb1, *pb = &pb1;
349
    
350
    file_opened = 0;
351
    pd->filename = "";
352
    if (filename)
353
        pd->filename = filename;
309 354
    pd->buf = buf;
310 355
    pd->buf_size = 0;
311 356

  
......
317 362
    /* do not open file if the format does not need it. XXX: specific
318 363
       hack needed to handle RTSP/TCP */
319 364
    must_open_file = 1;
320
    if ((fmt && (fmt->flags & AVFMT_NOFILE)) 
321
#ifdef CONFIG_NETWORK
322
        || (fmt == &rtp_demux && !strcmp(filename, "null"))
323
#endif
324
        ) {
365
    if (fmt && (fmt->flags & AVFMT_NOFILE)) {
325 366
        must_open_file = 0;
326 367
    }
327 368

  
328 369
    if (!fmt || must_open_file) {
329 370
        /* if no file needed do not try to open one */
330
        if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
371
        if (url_fopen(pb, filename, URL_RDONLY) < 0) {
331 372
            err = AVERROR_IO;
332 373
            goto fail;
333 374
        }
375
        file_opened = 1;
334 376
        if (buf_size > 0) {
335
            url_setbufsize(&ic->pb, buf_size);
377
            url_setbufsize(pb, buf_size);
336 378
        }
337 379
        if (!fmt) {
338 380
            /* read probe data */
339
            pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE);
340
            url_fseek(&ic->pb, 0, SEEK_SET);
381
            pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE);
382
            url_fseek(pb, 0, SEEK_SET);
341 383
        }
342 384
    }
343 385
    
......
349 391
    /* if still no format found, error */
350 392
    if (!fmt) {
351 393
        err = AVERROR_NOFMT;
352
        goto fail1;
394
        goto fail;
353 395
    }
354 396
        
355 397
    /* XXX: suppress this hack for redirectors */
356 398
#ifdef CONFIG_NETWORK
357 399
    if (fmt == &redir_demux) {
358
        err = redir_open(ic_ptr, &ic->pb);
359
        url_fclose(&ic->pb);
360
        av_free(ic);
400
        err = redir_open(ic_ptr, pb);
401
        url_fclose(pb);
361 402
        return err;
362 403
    }
363 404
#endif
364 405

  
365
    ic->iformat = fmt;
366

  
367 406
    /* check filename in case of an image number is expected */
368
    if (ic->iformat->flags & AVFMT_NEEDNUMBER) {
369
        if (filename_number_test(ic->filename) < 0) { 
407
    if (fmt->flags & AVFMT_NEEDNUMBER) {
408
        if (filename_number_test(filename) < 0) { 
370 409
            err = AVERROR_NUMEXPECTED;
371
            goto fail1;
410
            goto fail;
372 411
        }
373 412
    }
374
    
375
    /* allocate private data */
376
    if (fmt->priv_data_size > 0) {
377
        ic->priv_data = av_mallocz(fmt->priv_data_size);
378
        if (!ic->priv_data) {
379
            err = AVERROR_NOMEM;
380
        goto fail1;
381
        }
382
    } else
383
        ic->priv_data = NULL;
384

  
385
    /* default pts settings is MPEG like */
386
    av_set_pts_info(ic, 33, 1, 90000);
387

  
388
    err = ic->iformat->read_header(ic, ap);
389
    if (err < 0)
390
        goto fail1;
391
    *ic_ptr = ic;
413
    err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
414
    if (err)
415
        goto fail;
392 416
    return 0;
393
 fail1:
394
    if (!fmt || must_open_file) {
395
        url_fclose(&ic->pb);
396
    }
397 417
 fail:
398
    if (ic) {
399
        av_freep(&ic->priv_data);
400
    }
401
    av_free(ic);
418
    if (file_opened)
419
        url_fclose(pb);
402 420
    *ic_ptr = NULL;
403 421
    return err;
422
    
404 423
}
405 424

  
425
/*******************************************************/
426

  
406 427
/**
407
 * Read a packet from a media file
428
 * Read a packet from a media file. Use it only for low level file
429
 * reading. It is almost always better to use av_read_frame().
430
 * 
408 431
 * @param s media file handle
409 432
 * @param pkt is filled 
410
 * @return 0 if OK. AVERROR_xxx if error.
433
 * @return 0 if OK. AVERROR_xxx if error.  
411 434
 */
412 435
int av_read_packet(AVFormatContext *s, AVPacket *pkt)
413 436
{
......
425 448
    }
426 449
}
427 450

  
451
/*******************************************************/
428 452

  
429 453
/* return TRUE if the stream has accurate timings for at least one component */
430 454
static int av_has_timings(AVFormatContext *ic)
......
796 820
            /* NOTE: if the format has no header, then we need to read
797 821
               some packets to get most of the streams, so we cannot
798 822
               stop here */
799
            if (!(ic->iformat->flags & AVFMT_NOHEADER) ||
823
            if (!(ic->ctx_flags & AVFMTCTX_NOHEADER) ||
800 824
                read_size >= min_read_size) {
801 825
                /* if we found the info for all the codecs, we can stop */
802 826
                ret = count;
......
821 845
        ppktl = &pktl->next;
822 846

  
823 847
        /* NOTE: a new stream can be added there if no header in file
824
           (AVFMT_NOHEADER) */
848
           (AVFMTCTX_NOHEADER) */
825 849
        pkt = &pktl->pkt;
826 850
        if (ic->iformat->read_packet(ic, pkt) < 0) {
827 851
            /* EOF or error */
828 852
            ret = -1; /* we could not have all the codec parameters before EOF */
829
            if ((ic->iformat->flags & AVFMT_NOHEADER) &&
853
            if ((ic->ctx_flags & AVFMTCTX_NOHEADER) &&
830 854
                i == ic->nb_streams)
831 855
                ret = 0;
832 856
            break;
......
950 974
void av_close_input_file(AVFormatContext *s)
951 975
{
952 976
    int i, must_open_file;
977
    AVStream *st;
953 978

  
954 979
    if (s->iformat->read_close)
955 980
        s->iformat->read_close(s);
956 981
    for(i=0;i<s->nb_streams;i++) {
957
        av_free(s->streams[i]);
982
        /* free all data in a stream component */
983
        st = s->streams[i];
984
        av_free(st);
958 985
    }
959 986
    if (s->packet_buffer) {
960 987
        AVPacketList *p, *p1;
......
968 995
        s->packet_buffer = NULL;
969 996
    }
970 997
    must_open_file = 1;
971
    if ((s->iformat->flags & AVFMT_NOFILE)
972
#ifdef CONFIG_NETWORK
973
        || (s->iformat == &rtp_demux && !strcmp(s->filename, "null"))
974
#endif
975
        ) {
998
    if (s->iformat->flags & AVFMT_NOFILE) {
976 999
        must_open_file = 0;
977 1000
    }
978 1001
    if (must_open_file) {
......
984 1007

  
985 1008
/**
986 1009
 * Add a new stream to a media file. Can only be called in the
987
 * read_header function. If the flag AVFMT_NOHEADER is in the format
988
 * description, then new streams can be added in read_packet too.
1010
 * read_header function. If the flag AVFMTCTX_NOHEADER is in the
1011
 * format context, then new streams can be added in read_packet too.
989 1012
 *
990 1013
 *
991 1014
 * @param s media file handle
992
 * @param id file format dependent stream id
1015
 * @param id file format dependent stream id 
993 1016
 */
994 1017
AVStream *av_new_stream(AVFormatContext *s, int id)
995 1018
{

Also available in: Unified diff