Revision a85736f2

View differences:

libavformat/utils.c
1 1
/*
2
 * Various utilities for ffmpeg system
2
 * various utility functions for use within FFmpeg
3 3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 4
 *
5 5
 * This file is part of FFmpeg.
......
30 30

  
31 31
/**
32 32
 * @file libavformat/utils.c
33
 * Various utility functions for using ffmpeg library.
33
 * various utility functions for use within FFmpeg
34 34
 */
35 35

  
36 36
static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den);
37 37
static void av_frac_add(AVFrac *f, int64_t incr);
38 38

  
39
/** head of registered input format linked list. */
39
/** head of registered input format linked list */
40 40
AVInputFormat *first_iformat = NULL;
41
/** head of registered output format linked list. */
41
/** head of registered output format linked list */
42 42
AVOutputFormat *first_oformat = NULL;
43 43

  
44 44
AVInputFormat  *av_iformat_next(AVInputFormat  *f)
......
112 112
        return guess_format("image2", NULL, NULL);
113 113
    }
114 114
#endif
115
    /* find the proper file type */
115
    /* Find the proper file type. */
116 116
    fmt_found = NULL;
117 117
    score_max = 0;
118 118
    fmt = first_oformat;
......
241 241
{
242 242
    if (pkt->destruct != av_destruct_packet) {
243 243
        uint8_t *data;
244
        /* we duplicate the packet and don't forget to put the padding
245
           again */
244
        /* We duplicate the packet and don't forget to add the padding again. */
246 245
        if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
247 246
            return AVERROR(ENOMEM);
248 247
        data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
......
409 408
    return err;
410 409
}
411 410

  
412
/** Size of probe buffer, for guessing file type from file contents. */
411
/** size of probe buffer, for guessing file type from file contents */
413 412
#define PROBE_BUF_MIN 2048
414 413
#define PROBE_BUF_MAX (1<<20)
415 414

  
......
429 428
    pd->buf_size = 0;
430 429

  
431 430
    if (!fmt) {
432
        /* guess format if no file can be opened  */
431
        /* guess format if no file can be opened */
433 432
        fmt = av_probe_input_format(pd, 0);
434 433
    }
435 434

  
436
    /* do not open file if the format does not need it. XXX: specific
435
    /* Do not open file if the format does not need it. XXX: specific
437 436
       hack needed to handle RTSP/TCP */
438 437
    if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
439 438
        /* if no file needed do not try to open one */
......
470 469
        goto fail;
471 470
    }
472 471

  
473
    /* check filename in case of an image number is expected */
472
    /* check filename in case an image number is expected */
474 473
    if (fmt->flags & AVFMT_NEEDNUMBER) {
475 474
        if (!av_filename_number_test(filename)) {
476 475
            err = AVERROR_NUMEXPECTED;
......
520 519
/**********************************************************/
521 520

  
522 521
/**
523
 * Get the number of samples of an audio frame. Return (-1) if error.
522
 * Get the number of samples of an audio frame. Return -1 on error.
524 523
 */
525 524
static int get_audio_frame_size(AVCodecContext *enc, int size)
526 525
{
......
547 546

  
548 547

  
549 548
/**
550
 * Return the frame duration in seconds, return 0 if not available.
549
 * Return the frame duration in seconds. Return 0 if not available.
551 550
 */
552 551
static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
553 552
                                   AVCodecParserContext *pc, AVPacket *pkt)
......
651 650
        }
652 651
    }
653 652

  
654
    /* correct timestamps with byte offset if demuxers only have timestamps on packet boundaries */
653
    /* correct timestamps with byte offset if demuxers only have timestamps
654
       on packet boundaries */
655 655
    if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
656 656
        /* this will estimate bitrate based on this frame's duration and size */
657 657
        offset = av_rescale(pc->offset, pkt->duration, pkt->size);
......
661 661
            pkt->dts += offset;
662 662
    }
663 663

  
664
    /* do we have a video B frame ? */
664
    /* do we have a video B-frame ? */
665 665
    delay= st->codec->has_b_frames;
666 666
    presentation_delayed = 0;
667 667
    /* XXX: need has_b_frame, but cannot get it if the codec is
......
681 681
    /* interpolate PTS and DTS if they are not present */
682 682
    if(delay <=1){
683 683
        if (presentation_delayed) {
684
            /* DTS = decompression time stamp */
685
            /* PTS = presentation time stamp */
684
            /* DTS = decompression timestamp */
685
            /* PTS = presentation timestamp */
686 686
            if (pkt->dts == AV_NOPTS_VALUE)
687 687
                pkt->dts = st->last_IP_pts;
688 688
            update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
......
690 690
                pkt->dts = st->cur_dts;
691 691

  
692 692
            /* this is tricky: the dts must be incremented by the duration
693
            of the frame we are displaying, i.e. the last I or P frame */
693
            of the frame we are displaying, i.e. the last I- or P-frame */
694 694
            if (st->last_IP_duration == 0)
695 695
                st->last_IP_duration = pkt->duration;
696 696
            st->cur_dts = pkt->dts + st->last_IP_duration;
697 697
            st->last_IP_duration  = pkt->duration;
698 698
            st->last_IP_pts= pkt->pts;
699 699
            /* cannot compute PTS if not present (we can compute it only
700
            by knowing the futur */
700
            by knowing the future */
701 701
        } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){
702 702
            if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){
703 703
                int64_t old_diff= FFABS(st->cur_dts - pkt->duration - pkt->pts);
......
741 741
        pkt->flags |= PKT_FLAG_KEY;
742 742
    else if (pc) {
743 743
        pkt->flags = 0;
744
        /* key frame computation */
744
        /* keyframe computation */
745 745
            if (pc->pict_type == FF_I_TYPE)
746 746
                pkt->flags |= PKT_FLAG_KEY;
747 747
    }
......
822 822
                            goto got_packet;
823 823
                    }
824 824
                }
825
                /* no more packets: really terminates parsing */
825
                /* no more packets: really terminate parsing */
826 826
                return ret;
827 827
            }
828 828

  
......
840 840
            if (st->need_parsing && !st->parser) {
841 841
                st->parser = av_parser_init(st->codec->codec_id);
842 842
                if (!st->parser) {
843
                    /* no parser available : just output the raw packets */
843
                    /* no parser available: just output the raw packets */
844 844
                    st->need_parsing = AVSTREAM_PARSE_NONE;
845 845
                }else if(st->need_parsing == AVSTREAM_PARSE_HEADERS){
846 846
                    st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
......
1123 1123
    if(st->index_entries){
1124 1124
        AVIndexEntry *e;
1125 1125

  
1126
        index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD); //FIXME whole func must be checked for non keyframe entries in index case, especially read_timestamp()
1126
        index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD); //FIXME whole func must be checked for non-keyframe entries in index case, especially read_timestamp()
1127 1127
        index= FFMAX(index, 0);
1128 1128
        e= &st->index_entries[index];
1129 1129

  
......
1230 1230
            // bisection, if interpolation failed to change min or max pos last time
1231 1231
            pos = (pos_min + pos_limit)>>1;
1232 1232
        }else{
1233
            // linear search if bisection failed, can only happen if there are very few or no keframes between min/max
1233
            /* linear search if bisection failed, can only happen if there
1234
               are very few or no keyframes between min/max */
1234 1235
            pos=pos_min;
1235 1236
        }
1236 1237
        if(pos <= pos_min)
......
1452 1453
    if (duration != INT64_MIN) {
1453 1454
        ic->duration = duration;
1454 1455
        if (ic->file_size > 0) {
1455
            /* compute the bit rate */
1456
            /* compute the bitrate */
1456 1457
            ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE /
1457 1458
                (double)ic->duration;
1458 1459
        }
......
1624 1625
        /* get accurate estimate from the PTSes */
1625 1626
        av_estimate_timings_from_pts(ic, old_offset);
1626 1627
    } else if (av_has_duration(ic)) {
1627
        /* at least one components has timings - we use them for all
1628
        /* at least one component has timings - we use them for all
1628 1629
           the components */
1629 1630
        fill_all_stream_timings(ic);
1630 1631
    } else {
1631
        /* less precise: use bit rate info */
1632
        /* less precise: use bitrate info */
1632 1633
        av_estimate_timings_from_bit_rate(ic);
1633 1634
    }
1634 1635
    av_update_stream_timings(ic);
......
1781 1782
 * Is the time base unreliable.
1782 1783
 * This is a heuristic to balance between quick acceptance of the values in
1783 1784
 * the headers vs. some extra checks.
1784
 * Old divx and xvid often have nonsense timebases like 1fps or 2fps.
1785
 * Mpeg2 commonly misuses field repeat flags to store different framerates.
1785
 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
1786
 * MPEG-2 commonly misuses field repeat flags to store different framerates.
1786 1787
 * And there are "variable" fps files this needs to detect as well.
1787 1788
 */
1788 1789
static int tb_unreliable(AVCodecContext *c){
......
1941 1942

  
1942 1943
        /* if still no information, we try to open the codec and to
1943 1944
           decompress the frame. We try to avoid that in most cases as
1944
           it takes longer and uses more memory. For MPEG4, we need to
1945
           decompress for Quicktime. */
1945
           it takes longer and uses more memory. For MPEG-4, we need to
1946
           decompress for QuickTime. */
1946 1947
        if (!has_codec_parameters(st->codec) /*&&
1947 1948
            (st->codec->codec_id == CODEC_ID_FLV1 ||
1948 1949
             st->codec->codec_id == CODEC_ID_H264 ||
......
1980 1981

  
1981 1982
            if(duration_count[i]
1982 1983
               && tb_unreliable(st->codec) /*&&
1983
               //FIXME we should not special case mpeg2, but this needs testing with non mpeg2 ...
1984
               //FIXME we should not special-case MPEG-2, but this needs testing with non-MPEG-2 ...
1984 1985
               st->time_base.num*duration_sum[i]/duration_count[i]*101LL > st->time_base.den*/){
1985 1986
                double best_error= 2*av_q2d(st->time_base);
1986 1987
                best_error= best_error*best_error*duration_count[i]*1000*12*30;
......
2039 2040
    }
2040 2041

  
2041 2042
#if 0
2042
    /* correct DTS for b frame streams with no timestamps */
2043
    /* correct DTS for B-frame streams with no timestamps */
2043 2044
    for(i=0;i<ic->nb_streams;i++) {
2044 2045
        st = ic->streams[i];
2045 2046
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
......
2156 2157
    st->cur_dts = AV_NOPTS_VALUE;
2157 2158
    st->first_dts = AV_NOPTS_VALUE;
2158 2159

  
2159
    /* default pts settings is MPEG like */
2160
    /* default pts setting is MPEG-like */
2160 2161
    av_set_pts_info(st, 33, 1, 90000);
2161 2162
    st->last_IP_pts = AV_NOPTS_VALUE;
2162 2163
    for(i=0; i<MAX_REORDER_DELAY+1; i++)
......
2257 2258
            if(st->codec->codec_tag){
2258 2259
                //FIXME
2259 2260
                //check that tag + id is in the table
2260
                //if neither is in the table -> ok
2261
                //if neither is in the table -> OK
2261 2262
                //if tag is in the table with another id -> FAIL
2262 2263
                //if id is in the table with another tag -> FAIL unless strict < ?
2263 2264
            }else
......
2355 2356
    case CODEC_TYPE_AUDIO:
2356 2357
        frame_size = get_audio_frame_size(st->codec, pkt->size);
2357 2358

  
2358
        /* HACK/FIXME, we skip the initial 0-size packets as they are most likely equal to the encoder delay,
2359
           but it would be better if we had the real timestamps from the encoder */
2359
        /* HACK/FIXME, we skip the initial 0 size packets as they are most
2360
           likely equal to the encoder delay, but it would be better if we
2361
           had the real timestamps from the encoder */
2360 2362
        if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
2361 2363
            av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
2362 2364
        }
......
2374 2376
    int64_t pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1;
2375 2377

  
2376 2378
//    if(pkt->dts < 0)
2377
//        pkt->dts= 0;  //this happens for low_delay=0 and b frames, FIXME, needs further invstigation about what we should do here
2379
//        pkt->dts= 0;  //this happens for low_delay=0 and B-frames, FIXME, needs further investigation about what we should do here
2378 2380

  
2379 2381
    if (pkt->pts != AV_NOPTS_VALUE)
2380 2382
        pkt->pts &= pts_mask;
......
2411 2413
        this_pktl = av_mallocz(sizeof(AVPacketList));
2412 2414
        this_pktl->pkt= *pkt;
2413 2415
        if(pkt->destruct == av_destruct_packet)
2414
            pkt->destruct= NULL; // non shared -> must keep original from being freed
2416
            pkt->destruct= NULL; // not shared -> must keep original from being freed
2415 2417
        else
2416 2418
            av_dup_packet(&this_pktl->pkt);  //shared -> must dup
2417 2419

  
......
2452 2454
}
2453 2455

  
2454 2456
/**
2455
 * Interleaves a AVPacket correctly so it can be muxed.
2457
 * Interleaves an AVPacket correctly so it can be muxed.
2456 2458
 * @param out the interleaved packet will be output here
2457 2459
 * @param in the input packet
2458 2460
 * @param flush 1 if no further packets are available as input and all
......
2656 2658
}
2657 2659

  
2658 2660
/**
2659
 * gets the current time in micro seconds.
2661
 * Gets the current time in microseconds.
2660 2662
 */
2661 2663
int64_t av_gettime(void)
2662 2664
{
......
2926 2928
        PRINT("N/A");
2927 2929
    else
2928 2930
        PRINT("%0.3f", (double)pkt->dts / AV_TIME_BASE);
2929
    /* PTS may be not known if B frames are present */
2931
    /* PTS may not be known if B-frames are present. */
2930 2932
    PRINT("  pts=");
2931 2933
    if (pkt->pts == AV_NOPTS_VALUE)
2932 2934
        PRINT("N/A");
......
3043 3045
}
3044 3046

  
3045 3047
/**
3046
 * Fractionnal addition to f: f = f + (incr / f->den).
3048
 * Fractional addition to f: f = f + (incr / f->den).
3047 3049
 *
3048 3050
 * @param f fractional number
3049 3051
 * @param incr increment, can be positive or negative

Also available in: Unified diff