~ubuntu-branches/debian/sid/gstreamer0.10-ffmpeg/sid

« back to all changes in this revision

Viewing changes to gst-libs/ext/ffmpeg/libavformat/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2009-02-22 12:24:07 UTC
  • mfrom: (1.1.24 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090222122407-nubojphrd84klmee
Tags: 0.10.6-3
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 */
21
21
#include "avformat.h"
22
 
#include "opt.h"
23
 
#include "avstring.h"
 
22
#include "internal.h"
 
23
#include "libavcodec/opt.h"
 
24
#include "libavutil/avstring.h"
24
25
#include "riff.h"
25
26
#include <sys/time.h>
26
27
#include <time.h>
 
28
#include <strings.h>
27
29
 
28
30
#undef NDEBUG
29
31
#include <assert.h>
33
35
 * various utility functions for use within FFmpeg
34
36
 */
35
37
 
36
 
static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den);
37
 
static void av_frac_add(AVFrac *f, int64_t incr);
 
38
unsigned avformat_version(void)
 
39
{
 
40
    return LIBAVFORMAT_VERSION_INT;
 
41
}
 
42
 
 
43
/* fraction handling */
 
44
 
 
45
/**
 
46
 * f = val + (num / den) + 0.5.
 
47
 *
 
48
 * 'num' is normalized so that it is such as 0 <= num < den.
 
49
 *
 
50
 * @param f fractional number
 
51
 * @param val integer value
 
52
 * @param num must be >= 0
 
53
 * @param den must be >= 1
 
54
 */
 
55
static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
 
56
{
 
57
    num += (den >> 1);
 
58
    if (num >= den) {
 
59
        val += num / den;
 
60
        num = num % den;
 
61
    }
 
62
    f->val = val;
 
63
    f->num = num;
 
64
    f->den = den;
 
65
}
 
66
 
 
67
/**
 
68
 * Fractional addition to f: f = f + (incr / f->den).
 
69
 *
 
70
 * @param f fractional number
 
71
 * @param incr increment, can be positive or negative
 
72
 */
 
73
static void av_frac_add(AVFrac *f, int64_t incr)
 
74
{
 
75
    int64_t num, den;
 
76
 
 
77
    num = f->num + incr;
 
78
    den = f->den;
 
79
    if (num < 0) {
 
80
        f->val += num / den;
 
81
        num = num % den;
 
82
        if (num < 0) {
 
83
            num += den;
 
84
            f->val--;
 
85
        }
 
86
    } else if (num >= den) {
 
87
        f->val += num / den;
 
88
        num = num % den;
 
89
    }
 
90
    f->num = num;
 
91
}
38
92
 
39
93
/** head of registered input format linked list */
40
94
AVInputFormat *first_iformat = NULL;
197
251
    pkt->dts   = AV_NOPTS_VALUE;
198
252
    pkt->pos   = -1;
199
253
    pkt->duration = 0;
 
254
    pkt->convergence_duration = 0;
200
255
    pkt->flags = 0;
201
256
    pkt->stream_index = 0;
202
257
    pkt->destruct= av_destruct_packet_nofree;
293
348
    return av_probe_input_format2(pd, is_opened, &score);
294
349
}
295
350
 
 
351
static int set_codec_from_probe_data(AVStream *st, AVProbeData *pd, int score)
 
352
{
 
353
    AVInputFormat *fmt;
 
354
    fmt = av_probe_input_format2(pd, 1, &score);
 
355
 
 
356
    if (fmt) {
 
357
        if (!strcmp(fmt->name, "mp3")) {
 
358
            st->codec->codec_id = CODEC_ID_MP3;
 
359
            st->codec->codec_type = CODEC_TYPE_AUDIO;
 
360
        } else if (!strcmp(fmt->name, "ac3")) {
 
361
            st->codec->codec_id = CODEC_ID_AC3;
 
362
            st->codec->codec_type = CODEC_TYPE_AUDIO;
 
363
        } else if (!strcmp(fmt->name, "mpegvideo")) {
 
364
            st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
 
365
            st->codec->codec_type = CODEC_TYPE_VIDEO;
 
366
        } else if (!strcmp(fmt->name, "m4v")) {
 
367
            st->codec->codec_id = CODEC_ID_MPEG4;
 
368
            st->codec->codec_type = CODEC_TYPE_VIDEO;
 
369
        } else if (!strcmp(fmt->name, "h264")) {
 
370
            st->codec->codec_id = CODEC_ID_H264;
 
371
            st->codec->codec_type = CODEC_TYPE_VIDEO;
 
372
        }
 
373
    }
 
374
    return !!fmt;
 
375
}
 
376
 
296
377
/************************************************************/
297
378
/* input media file */
298
379
 
326
407
{"cryptokey", "decryption key", OFFSET(key), FF_OPT_TYPE_BINARY, 0, 0, 0, D},
327
408
{"indexmem", "max memory used for timestamp index (per stream)", OFFSET(max_index_size), FF_OPT_TYPE_INT, 1<<20, 0, INT_MAX, D},
328
409
{"rtbufsize", "max memory used for buffering real-time frames", OFFSET(max_picture_buffer), FF_OPT_TYPE_INT, 3041280, 0, INT_MAX, D}, /* defaults to 1s of 15fps 352x288 YUYV422 video */
 
410
{"fdebug", "print specific debug info", OFFSET(debug), FF_OPT_TYPE_FLAGS, DEFAULT, 0, INT_MAX, E|D, "fdebug"},
 
411
{"ts", NULL, 0, FF_OPT_TYPE_CONST, FF_FDEBUG_TS, INT_MIN, INT_MAX, E|D, "fdebug"},
329
412
{NULL},
330
413
};
331
414
 
392
475
        ic->priv_data = NULL;
393
476
    }
394
477
 
395
 
    err = ic->iformat->read_header(ic, ap);
396
 
    if (err < 0)
397
 
        goto fail;
 
478
    if (ic->iformat->read_header) {
 
479
        err = ic->iformat->read_header(ic, ap);
 
480
        if (err < 0)
 
481
            goto fail;
 
482
    }
398
483
 
399
484
    if (pb && !ic->data_offset)
400
485
        ic->data_offset = url_ftell(ic->pb);
403
488
    return 0;
404
489
 fail:
405
490
    if (ic) {
 
491
        int i;
406
492
        av_freep(&ic->priv_data);
 
493
        for(i=0;i<ic->nb_streams;i++) {
 
494
            AVStream *st = ic->streams[i];
 
495
            if (st) {
 
496
                av_free(st->priv_data);
 
497
                av_free(st->codec->extradata);
 
498
            }
 
499
            av_free(st);
 
500
        }
407
501
    }
408
502
    av_free(ic);
409
503
    *ic_ptr = NULL;
493
587
 
494
588
/*******************************************************/
495
589
 
 
590
static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
 
591
                               AVPacketList **plast_pktl){
 
592
    AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
 
593
    if (!pktl)
 
594
        return NULL;
 
595
 
 
596
    if (*packet_buffer)
 
597
        (*plast_pktl)->next = pktl;
 
598
    else
 
599
        *packet_buffer = pktl;
 
600
 
 
601
    /* add the packet in the buffered packet list */
 
602
    *plast_pktl = pktl;
 
603
    pktl->pkt= *pkt;
 
604
    return &pktl->pkt;
 
605
}
 
606
 
496
607
int av_read_packet(AVFormatContext *s, AVPacket *pkt)
497
608
{
498
609
    int ret;
499
610
    AVStream *st;
500
 
    av_init_packet(pkt);
501
 
    ret= s->iformat->read_packet(s, pkt);
502
 
    if (ret < 0)
503
 
        return ret;
504
 
    st= s->streams[pkt->stream_index];
505
 
 
506
 
    switch(st->codec->codec_type){
507
 
    case CODEC_TYPE_VIDEO:
508
 
        if(s->video_codec_id)   st->codec->codec_id= s->video_codec_id;
509
 
        break;
510
 
    case CODEC_TYPE_AUDIO:
511
 
        if(s->audio_codec_id)   st->codec->codec_id= s->audio_codec_id;
512
 
        break;
513
 
    case CODEC_TYPE_SUBTITLE:
514
 
        if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
515
 
        break;
 
611
 
 
612
    for(;;){
 
613
        AVPacketList *pktl = s->raw_packet_buffer;
 
614
 
 
615
        if (pktl) {
 
616
            *pkt = pktl->pkt;
 
617
            if(s->streams[pkt->stream_index]->codec->codec_id != CODEC_ID_PROBE){
 
618
                s->raw_packet_buffer = pktl->next;
 
619
                av_free(pktl);
 
620
                return 0;
 
621
            }
 
622
        }
 
623
 
 
624
        av_init_packet(pkt);
 
625
        ret= s->iformat->read_packet(s, pkt);
 
626
        if (ret < 0)
 
627
            return ret;
 
628
        st= s->streams[pkt->stream_index];
 
629
 
 
630
        switch(st->codec->codec_type){
 
631
        case CODEC_TYPE_VIDEO:
 
632
            if(s->video_codec_id)   st->codec->codec_id= s->video_codec_id;
 
633
            break;
 
634
        case CODEC_TYPE_AUDIO:
 
635
            if(s->audio_codec_id)   st->codec->codec_id= s->audio_codec_id;
 
636
            break;
 
637
        case CODEC_TYPE_SUBTITLE:
 
638
            if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
 
639
            break;
 
640
        }
 
641
 
 
642
        if(!pktl && st->codec->codec_id!=CODEC_ID_PROBE)
 
643
            return ret;
 
644
 
 
645
        add_to_pktbuf(&s->raw_packet_buffer, pkt, &s->raw_packet_buffer_end);
 
646
 
 
647
        if(st->codec->codec_id == CODEC_ID_PROBE){
 
648
            AVProbeData *pd = &st->probe_data;
 
649
 
 
650
            pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
 
651
            memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
 
652
            pd->buf_size += pkt->size;
 
653
            memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
 
654
 
 
655
            if(av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
 
656
                set_codec_from_probe_data(st, pd, 1);
 
657
                if(st->codec->codec_id != CODEC_ID_PROBE){
 
658
                    pd->buf_size=0;
 
659
                    av_freep(&pd->buf);
 
660
                }
 
661
            }
 
662
        }
516
663
    }
517
 
 
518
 
    return ret;
519
664
}
520
665
 
521
666
/**********************************************************/
527
672
{
528
673
    int frame_size;
529
674
 
 
675
    if(enc->codec_id == CODEC_ID_VORBIS)
 
676
        return -1;
 
677
 
530
678
    if (enc->frame_size <= 1) {
531
679
        int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
532
680
 
598
746
        case CODEC_ID_ASV1:
599
747
        case CODEC_ID_ASV2:
600
748
        case CODEC_ID_VCR1:
 
749
        case CODEC_ID_DNXHD:
601
750
            return 1;
602
751
        default: break;
603
752
        }
611
760
    AVStream *st= s->streams[stream_index];
612
761
    AVPacketList *pktl= s->packet_buffer;
613
762
 
614
 
    if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)
 
763
    if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE)
615
764
        return;
616
765
 
617
766
    st->first_dts= dts - st->cur_dts;
637
786
static void update_initial_durations(AVFormatContext *s, AVStream *st, AVPacket *pkt)
638
787
{
639
788
    AVPacketList *pktl= s->packet_buffer;
 
789
    int64_t cur_dts= 0;
640
790
 
641
 
    assert(pkt->duration && !st->cur_dts);
 
791
    if(st->first_dts != AV_NOPTS_VALUE){
 
792
        cur_dts= st->first_dts;
 
793
        for(; pktl; pktl= pktl->next){
 
794
            if(pktl->pkt.stream_index == pkt->stream_index){
 
795
                if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
 
796
                    break;
 
797
                cur_dts -= pkt->duration;
 
798
            }
 
799
        }
 
800
        pktl= s->packet_buffer;
 
801
        st->first_dts = cur_dts;
 
802
    }else if(st->cur_dts)
 
803
        return;
642
804
 
643
805
    for(; pktl; pktl= pktl->next){
644
806
        if(pktl->pkt.stream_index != pkt->stream_index)
645
807
            continue;
646
808
        if(pktl->pkt.pts == pktl->pkt.dts && pktl->pkt.dts == AV_NOPTS_VALUE
647
809
           && !pktl->pkt.duration){
648
 
            pktl->pkt.pts= pktl->pkt.dts= st->cur_dts;
649
 
            st->cur_dts += pkt->duration;
 
810
            pktl->pkt.dts= cur_dts;
 
811
            if(!st->codec->has_b_frames)
 
812
                pktl->pkt.pts= cur_dts;
 
813
            cur_dts += pkt->duration;
650
814
            pktl->pkt.duration= pkt->duration;
651
815
        }else
652
816
            break;
653
817
    }
 
818
    if(st->first_dts == AV_NOPTS_VALUE)
 
819
        st->cur_dts= cur_dts;
654
820
}
655
821
 
656
822
static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
659
825
    int num, den, presentation_delayed, delay, i;
660
826
    int64_t offset;
661
827
 
 
828
    /* do we have a video B-frame ? */
 
829
    delay= st->codec->has_b_frames;
 
830
    presentation_delayed = 0;
 
831
    /* XXX: need has_b_frame, but cannot get it if the codec is
 
832
        not initialized */
 
833
    if (delay &&
 
834
        pc && pc->pict_type != FF_B_TYPE)
 
835
        presentation_delayed = 1;
 
836
 
662
837
    if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
663
838
       /*&& pkt->dts-(1LL<<st->pts_wrap_bits) < pkt->pts*/){
664
839
        pkt->dts -= 1LL<<st->pts_wrap_bits;
665
840
    }
666
841
 
 
842
    // some mpeg2 in mpeg-ps lack dts (issue171 / input_file.mpg)
 
843
    // we take the conservative approach and discard both
 
844
    // Note, if this is misbehaving for a H.264 file then possibly presentation_delayed is not set correctly.
 
845
    if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){
 
846
        av_log(s, AV_LOG_ERROR, "invalid dts/pts combination\n");
 
847
        pkt->dts= pkt->pts= AV_NOPTS_VALUE;
 
848
    }
 
849
 
667
850
    if (pkt->duration == 0) {
668
851
        compute_frame_duration(&num, &den, st, pc, pkt);
669
852
        if (den && num) {
670
853
            pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num);
671
854
 
672
 
            if(st->cur_dts == 0 && pkt->duration != 0)
 
855
            if(pkt->duration != 0 && s->packet_buffer)
673
856
                update_initial_durations(s, st, pkt);
674
857
        }
675
858
    }
685
868
            pkt->dts += offset;
686
869
    }
687
870
 
688
 
    /* do we have a video B-frame ? */
689
 
    delay= st->codec->has_b_frames;
690
 
    presentation_delayed = 0;
691
 
    /* XXX: need has_b_frame, but cannot get it if the codec is
692
 
        not initialized */
693
 
    if (delay &&
694
 
        pc && pc->pict_type != FF_B_TYPE)
695
 
        presentation_delayed = 1;
696
871
    /* This may be redundant, but it should not hurt. */
697
872
    if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
698
873
        presentation_delayed = 1;
699
874
 
700
 
    if(st->cur_dts == AV_NOPTS_VALUE){
701
 
        st->cur_dts = 0; //FIXME maybe set it to 0 during init
702
 
    }
703
 
 
704
875
//    av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc);
705
876
    /* interpolate PTS and DTS if they are not present */
706
 
    if(delay <=1){
 
877
    if(delay==0 || (delay==1 && pc)){
707
878
        if (presentation_delayed) {
708
879
            /* DTS = decompression timestamp */
709
880
            /* PTS = presentation timestamp */
717
888
            of the frame we are displaying, i.e. the last I- or P-frame */
718
889
            if (st->last_IP_duration == 0)
719
890
                st->last_IP_duration = pkt->duration;
720
 
            st->cur_dts = pkt->dts + st->last_IP_duration;
 
891
            if(pkt->dts != AV_NOPTS_VALUE)
 
892
                st->cur_dts = pkt->dts + st->last_IP_duration;
721
893
            st->last_IP_duration  = pkt->duration;
722
894
            st->last_IP_pts= pkt->pts;
723
895
            /* cannot compute PTS if not present (we can compute it only
739
911
            if(pkt->pts == AV_NOPTS_VALUE)
740
912
                pkt->pts = st->cur_dts;
741
913
            pkt->dts = pkt->pts;
742
 
            st->cur_dts = pkt->pts + pkt->duration;
 
914
            if(pkt->pts != AV_NOPTS_VALUE)
 
915
                st->cur_dts = pkt->pts + pkt->duration;
743
916
        }
744
917
    }
745
918
 
746
 
    if(pkt->pts != AV_NOPTS_VALUE){
 
919
    if(pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
747
920
        st->pts_buffer[0]= pkt->pts;
748
 
        for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
749
 
            st->pts_buffer[i]= (i-delay-1) * pkt->duration;
750
921
        for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
751
922
            FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
752
923
        if(pkt->dts == AV_NOPTS_VALUE)
850
1021
                return ret;
851
1022
            }
852
1023
 
 
1024
            if(s->cur_pkt.pts != AV_NOPTS_VALUE &&
 
1025
               s->cur_pkt.dts != AV_NOPTS_VALUE &&
 
1026
               s->cur_pkt.pts < s->cur_pkt.dts){
 
1027
                av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
 
1028
                    s->cur_pkt.stream_index,
 
1029
                    s->cur_pkt.pts,
 
1030
                    s->cur_pkt.dts,
 
1031
                    s->cur_pkt.size);
 
1032
//                av_free_packet(&s->cur_pkt);
 
1033
//                return -1;
 
1034
            }
 
1035
 
853
1036
            st = s->streams[s->cur_pkt.stream_index];
854
 
            if(st->codec->debug & FF_DEBUG_PTS)
855
 
                av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
 
1037
            if(s->debug & FF_FDEBUG_TS)
 
1038
                av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d,  flags=%d\n",
856
1039
                    s->cur_pkt.stream_index,
857
1040
                    s->cur_pkt.pts,
858
1041
                    s->cur_pkt.dts,
859
 
                    s->cur_pkt.size);
 
1042
                    s->cur_pkt.size,
 
1043
                    s->cur_pkt.flags);
860
1044
 
861
1045
            s->cur_st = st;
862
1046
            s->cur_ptr = s->cur_pkt.data;
870
1054
                    st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
871
1055
                }
872
1056
                if(st->parser && (s->iformat->flags & AVFMT_GENERIC_INDEX)){
873
 
                    st->parser->last_frame_offset=
 
1057
                    st->parser->next_frame_offset=
874
1058
                    st->parser->cur_offset= s->cur_pkt.pos;
875
1059
                }
876
1060
            }
877
1061
        }
878
1062
    }
879
 
    if(st->codec->debug & FF_DEBUG_PTS)
880
 
        av_log(s, AV_LOG_DEBUG, "av_read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
 
1063
    if(s->debug & FF_FDEBUG_TS)
 
1064
        av_log(s, AV_LOG_DEBUG, "av_read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, flags=%d\n",
881
1065
            pkt->stream_index,
882
1066
            pkt->pts,
883
1067
            pkt->dts,
884
 
            pkt->size);
 
1068
            pkt->size,
 
1069
            pkt->flags);
885
1070
 
886
1071
    return 0;
887
1072
}
888
1073
 
889
 
static AVPacket *add_to_pktbuf(AVFormatContext *s, AVPacket *pkt){
890
 
    AVPacketList *pktl= s->packet_buffer;
891
 
    AVPacketList **plast_pktl= &s->packet_buffer;
892
 
 
893
 
    while(*plast_pktl) plast_pktl= &(*plast_pktl)->next; //FIXME maybe maintain pointer to the last?
894
 
 
895
 
    pktl = av_mallocz(sizeof(AVPacketList));
896
 
    if (!pktl)
897
 
        return NULL;
898
 
 
899
 
    /* add the packet in the buffered packet list */
900
 
    *plast_pktl = pktl;
901
 
    pktl->pkt= *pkt;
902
 
    return &pktl->pkt;
903
 
}
904
 
 
905
1074
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
906
1075
{
907
1076
    AVPacketList *pktl;
946
1115
                    return ret;
947
1116
            }
948
1117
 
949
 
            if(av_dup_packet(add_to_pktbuf(s, pkt)) < 0)
 
1118
            if(av_dup_packet(add_to_pktbuf(&s->packet_buffer, pkt,
 
1119
                                           &s->packet_buffer_end)) < 0)
950
1120
                return AVERROR(ENOMEM);
951
1121
        }else{
952
1122
            assert(!s->packet_buffer);
975
1145
 
976
1146
int av_find_default_stream_index(AVFormatContext *s)
977
1147
{
 
1148
    int first_audio_index = -1;
978
1149
    int i;
979
1150
    AVStream *st;
980
1151
 
985
1156
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
986
1157
            return i;
987
1158
        }
 
1159
        if (first_audio_index < 0 && st->codec->codec_type == CODEC_TYPE_AUDIO)
 
1160
            first_audio_index = i;
988
1161
    }
989
 
    return 0;
 
1162
    return first_audio_index >= 0 ? first_audio_index : 0;
990
1163
}
991
1164
 
992
1165
/**
1330
1503
static int av_seek_frame_generic(AVFormatContext *s,
1331
1504
                                 int stream_index, int64_t timestamp, int flags)
1332
1505
{
1333
 
    int index;
 
1506
    int index, ret;
1334
1507
    AVStream *st;
1335
1508
    AVIndexEntry *ie;
1336
1509
 
1342
1515
        int i;
1343
1516
        AVPacket pkt;
1344
1517
 
1345
 
        if(st->index_entries && st->nb_index_entries){
 
1518
        if(st->nb_index_entries){
 
1519
            assert(st->index_entries);
1346
1520
            ie= &st->index_entries[st->nb_index_entries-1];
1347
 
            url_fseek(s->pb, ie->pos, SEEK_SET);
 
1521
            if ((ret = url_fseek(s->pb, ie->pos, SEEK_SET)) < 0)
 
1522
                return ret;
1348
1523
            av_update_cur_dts(s, st, ie->timestamp);
1349
 
        }else
1350
 
            url_fseek(s->pb, 0, SEEK_SET);
1351
 
 
 
1524
        }else{
 
1525
            if ((ret = url_fseek(s->pb, 0, SEEK_SET)) < 0)
 
1526
                return ret;
 
1527
        }
1352
1528
        for(i=0;; i++) {
1353
1529
            int ret = av_read_frame(s, &pkt);
1354
1530
            if(ret<0)
1370
1546
            return 0;
1371
1547
    }
1372
1548
    ie = &st->index_entries[index];
1373
 
    url_fseek(s->pb, ie->pos, SEEK_SET);
1374
 
 
 
1549
    if ((ret = url_fseek(s->pb, ie->pos, SEEK_SET)) < 0)
 
1550
        return ret;
1375
1551
    av_update_cur_dts(s, st, ie->timestamp);
1376
1552
 
1377
1553
    return 0;
1396
1572
       /* timestamp for default must be expressed in AV_TIME_BASE units */
1397
1573
        timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
1398
1574
    }
1399
 
    st= s->streams[stream_index];
1400
1575
 
1401
1576
    /* first, we try the format specific seek */
1402
1577
    if (s->iformat->read_seek)
1536
1711
#define DURATION_MAX_READ_SIZE 250000
1537
1712
 
1538
1713
/* only usable for MPEG-PS streams */
1539
 
static void av_estimate_timings_from_pts(AVFormatContext *ic, offset_t old_offset)
 
1714
static void av_estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1540
1715
{
1541
1716
    AVPacket pkt1, *pkt = &pkt1;
1542
1717
    AVStream *st;
1629
1804
    }
1630
1805
}
1631
1806
 
1632
 
static void av_estimate_timings(AVFormatContext *ic, offset_t old_offset)
 
1807
static void av_estimate_timings(AVFormatContext *ic, int64_t old_offset)
1633
1808
{
1634
1809
    int64_t file_size;
1635
1810
 
1681
1856
    int val;
1682
1857
    switch(enc->codec_type) {
1683
1858
    case CODEC_TYPE_AUDIO:
1684
 
        val = enc->sample_rate;
 
1859
        val = enc->sample_rate && enc->channels && enc->sample_fmt != SAMPLE_FMT_NONE;
 
1860
        if(!enc->frame_size &&
 
1861
           (enc->codec_id == CODEC_ID_VORBIS ||
 
1862
            enc->codec_id == CODEC_ID_AAC))
 
1863
            return 0;
1685
1864
        break;
1686
1865
    case CODEC_TYPE_VIDEO:
1687
1866
        val = enc->width && enc->pix_fmt != PIX_FMT_NONE;
1732
1911
    return ret;
1733
1912
}
1734
1913
 
1735
 
static int set_codec_from_probe_data(AVStream *st, AVProbeData *pd, int score)
1736
 
{
1737
 
    AVInputFormat *fmt;
1738
 
    fmt = av_probe_input_format2(pd, 1, &score);
1739
 
 
1740
 
    if (fmt) {
1741
 
        if (strncmp(fmt->name, "mp3", 3) == 0)
1742
 
            st->codec->codec_id = CODEC_ID_MP3;
1743
 
        else if (strncmp(fmt->name, "ac3", 3) == 0)
1744
 
            st->codec->codec_id = CODEC_ID_AC3;
1745
 
    }
1746
 
    return !!fmt;
1747
 
}
1748
 
 
1749
1914
unsigned int codec_get_tag(const AVCodecTag *tags, int id)
1750
1915
{
1751
1916
    while (tags->id != CODEC_ID_NONE) {
1793
1958
    return CODEC_ID_NONE;
1794
1959
}
1795
1960
 
 
1961
static void compute_chapters_end(AVFormatContext *s)
 
1962
{
 
1963
    unsigned int i;
 
1964
 
 
1965
    for (i=0; i+1<s->nb_chapters; i++)
 
1966
        if (s->chapters[i]->end == AV_NOPTS_VALUE) {
 
1967
            assert(s->chapters[i]->start <= s->chapters[i+1]->start);
 
1968
            assert(!av_cmp_q(s->chapters[i]->time_base, s->chapters[i+1]->time_base));
 
1969
            s->chapters[i]->end = s->chapters[i+1]->start;
 
1970
        }
 
1971
 
 
1972
    if (s->nb_chapters && s->chapters[i]->end == AV_NOPTS_VALUE) {
 
1973
        assert(s->start_time != AV_NOPTS_VALUE);
 
1974
        assert(s->duration > 0);
 
1975
        s->chapters[i]->end = av_rescale_q(s->start_time + s->duration,
 
1976
                                           AV_TIME_BASE_Q,
 
1977
                                           s->chapters[i]->time_base);
 
1978
    }
 
1979
}
 
1980
 
1796
1981
/* absolute maximum size we read until we abort */
1797
1982
#define MAX_READ_SIZE        5000000
1798
1983
 
1799
1984
#define MAX_STD_TIMEBASES (60*12+5)
1800
1985
static int get_std_framerate(int i){
1801
1986
    if(i<60*12) return i*1001;
1802
 
    else        return ((int[]){24,30,60,12,15})[i-60*12]*1000*12;
 
1987
    else        return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
1803
1988
}
1804
1989
 
1805
1990
/*
1828
2013
    int64_t last_dts[MAX_STREAMS];
1829
2014
    int duration_count[MAX_STREAMS]={0};
1830
2015
    double (*duration_error)[MAX_STD_TIMEBASES];
1831
 
    offset_t old_offset = url_ftell(ic->pb);
 
2016
    int64_t old_offset = url_ftell(ic->pb);
1832
2017
    int64_t codec_info_duration[MAX_STREAMS]={0};
1833
2018
    int codec_info_nb_frames[MAX_STREAMS]={0};
1834
 
    AVProbeData probe_data[MAX_STREAMS];
1835
 
    int codec_identified[MAX_STREAMS]={0};
1836
2019
 
1837
2020
    duration_error = av_mallocz(MAX_STREAMS * sizeof(*duration_error));
1838
2021
    if (!duration_error) return AVERROR(ENOMEM);
1858
2041
        last_dts[i]= AV_NOPTS_VALUE;
1859
2042
    }
1860
2043
 
1861
 
    memset(probe_data, 0, sizeof(probe_data));
1862
2044
    count = 0;
1863
2045
    read_size = 0;
1864
2046
    for(;;) {
1911
2093
            break;
1912
2094
        }
1913
2095
 
1914
 
        pkt= add_to_pktbuf(ic, &pkt1);
1915
 
        if(av_dup_packet(pkt) < 0)
 
2096
        pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
 
2097
        if(av_dup_packet(pkt) < 0) {
 
2098
            av_free(duration_error);
1916
2099
            return AVERROR(ENOMEM);
 
2100
        }
1917
2101
 
1918
2102
        read_size += pkt->size;
1919
2103
 
1945
2129
            }
1946
2130
            if(last == AV_NOPTS_VALUE || duration_count[index]<=1)
1947
2131
                last_dts[pkt->stream_index]= pkt->dts;
1948
 
 
1949
 
            if (st->codec->codec_id == CODEC_ID_NONE) {
1950
 
                AVProbeData *pd = &(probe_data[st->index]);
1951
 
                pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
1952
 
                memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
1953
 
                pd->buf_size += pkt->size;
1954
 
                memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
1955
 
            }
1956
2132
        }
1957
2133
        if(st->parser && st->parser->parser->split && !st->codec->extradata){
1958
2134
            int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
2000
2176
    for(i=0;i<ic->nb_streams;i++) {
2001
2177
        st = ic->streams[i];
2002
2178
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
2003
 
            if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_sample)
 
2179
            if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample)
2004
2180
                st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
2005
2181
 
2006
2182
            if(duration_count[i]
2032
2208
                }
2033
2209
            }
2034
2210
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO) {
2035
 
            if (st->codec->codec_id == CODEC_ID_NONE && probe_data[st->index].buf_size > 0) {
2036
 
                codec_identified[st->index] = set_codec_from_probe_data(st, &(probe_data[st->index]), 1);
2037
 
                if (codec_identified[st->index]) {
2038
 
                    st->need_parsing = AVSTREAM_PARSE_FULL;
2039
 
                }
2040
 
            }
2041
 
            if(!st->codec->bits_per_sample)
2042
 
                st->codec->bits_per_sample= av_get_bits_per_sample(st->codec->codec_id);
 
2211
            if(!st->codec->bits_per_coded_sample)
 
2212
                st->codec->bits_per_coded_sample= av_get_bits_per_sample(st->codec->codec_id);
2043
2213
        }
2044
2214
    }
2045
2215
 
2046
2216
    av_estimate_timings(ic, old_offset);
2047
2217
 
2048
 
    for(i=0;i<ic->nb_streams;i++) {
2049
 
        st = ic->streams[i];
2050
 
        if (codec_identified[st->index])
2051
 
            break;
2052
 
    }
2053
 
    //FIXME this is a mess
2054
 
    if(i!=ic->nb_streams){
2055
 
        av_read_frame_flush(ic);
2056
 
        for(i=0;i<ic->nb_streams;i++) {
2057
 
            st = ic->streams[i];
2058
 
            if (codec_identified[st->index]) {
2059
 
                av_seek_frame(ic, st->index, 0.0, 0);
2060
 
            }
2061
 
            st->cur_dts= st->first_dts;
2062
 
        }
2063
 
        url_fseek(ic->pb, ic->data_offset, SEEK_SET);
2064
 
    }
 
2218
    compute_chapters_end(ic);
2065
2219
 
2066
2220
#if 0
2067
2221
    /* correct DTS for B-frame streams with no timestamps */
2089
2243
#endif
2090
2244
 
2091
2245
    av_free(duration_error);
2092
 
    for(i=0;i<MAX_STREAMS;i++){
2093
 
        av_freep(&(probe_data[i].buf));
2094
 
    }
2095
2246
 
2096
2247
    return ret;
2097
2248
}
2137
2288
        av_free(st->codec->extradata);
2138
2289
        av_free(st->codec);
2139
2290
        av_free(st->filename);
 
2291
        av_free(st->priv_data);
2140
2292
        av_free(st);
2141
2293
    }
2142
2294
    for(i=s->nb_programs-1; i>=0; i--) {
2148
2300
    av_freep(&s->programs);
2149
2301
    flush_packet_queue(s);
2150
2302
    av_freep(&s->priv_data);
 
2303
    while(s->nb_chapters--) {
 
2304
        av_free(s->chapters[s->nb_chapters]->title);
 
2305
        av_free(s->chapters[s->nb_chapters]);
 
2306
    }
 
2307
    av_freep(&s->chapters);
2151
2308
    av_free(s);
2152
2309
}
2153
2310
 
2180
2337
    st->id = id;
2181
2338
    st->start_time = AV_NOPTS_VALUE;
2182
2339
    st->duration = AV_NOPTS_VALUE;
2183
 
    st->cur_dts = AV_NOPTS_VALUE;
 
2340
        /* we set the current DTS to 0 so that formats without any timestamps
 
2341
           but durations get some timestamps, formats with some unknown
 
2342
           timestamps have their first few packets buffered and the
 
2343
           timestamps corrected before they are returned to the user */
 
2344
    st->cur_dts = 0;
2184
2345
    st->first_dts = AV_NOPTS_VALUE;
2185
2346
 
2186
2347
    /* default pts setting is MPEG-like */
2189
2350
    for(i=0; i<MAX_REORDER_DELAY+1; i++)
2190
2351
        st->pts_buffer[i]= AV_NOPTS_VALUE;
2191
2352
 
 
2353
    st->sample_aspect_ratio = (AVRational){0,1};
 
2354
 
2192
2355
    s->streams[s->nb_streams++] = st;
2193
2356
    return st;
2194
2357
}
2229
2392
    }
2230
2393
}
2231
2394
 
 
2395
AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
 
2396
{
 
2397
    AVChapter *chapter = NULL;
 
2398
    int i;
 
2399
 
 
2400
    for(i=0; i<s->nb_chapters; i++)
 
2401
        if(s->chapters[i]->id == id)
 
2402
            chapter = s->chapters[i];
 
2403
 
 
2404
    if(!chapter){
 
2405
        chapter= av_mallocz(sizeof(AVChapter));
 
2406
        if(!chapter)
 
2407
            return NULL;
 
2408
        dynarray_add(&s->chapters, &s->nb_chapters, chapter);
 
2409
    }
 
2410
    av_free(chapter->title);
 
2411
    chapter->title = av_strdup(title);
 
2412
    chapter->id    = id;
 
2413
    chapter->time_base= time_base;
 
2414
    chapter->start = start;
 
2415
    chapter->end   = end;
 
2416
 
 
2417
    return chapter;
 
2418
}
2232
2419
 
2233
2420
/************************************************************/
2234
2421
/* output media file */
2267
2454
                av_log(s, AV_LOG_ERROR, "sample rate not set\n");
2268
2455
                return -1;
2269
2456
            }
 
2457
            if(!st->codec->block_align)
 
2458
                st->codec->block_align = st->codec->channels *
 
2459
                    av_get_bits_per_sample(st->codec->codec_id) >> 3;
2270
2460
            break;
2271
2461
        case CODEC_TYPE_VIDEO:
2272
2462
            if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too?
2277
2467
                av_log(s, AV_LOG_ERROR, "dimensions not set\n");
2278
2468
                return -1;
2279
2469
            }
 
2470
            if(av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)){
 
2471
                av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between encoder and muxer layer\n");
 
2472
                return -1;
 
2473
            }
2280
2474
            break;
2281
2475
        }
2282
2476
 
2346
2540
        }
2347
2541
    }
2348
2542
 
 
2543
    if(pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay==0)
 
2544
        pkt->pts= pkt->dts;
 
2545
 
2349
2546
    //XXX/FIXME this is a temporary hack until all encoders output pts
2350
2547
    if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay){
2351
2548
        pkt->dts=
2354
2551
    }
2355
2552
 
2356
2553
    //calculate dts from pts
2357
 
    if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE){
 
2554
    if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
2358
2555
        st->pts_buffer[0]= pkt->pts;
2359
2556
        for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
2360
2557
            st->pts_buffer[i]= (i-delay-1) * pkt->duration;
2365
2562
    }
2366
2563
 
2367
2564
    if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
2368
 
        av_log(NULL, AV_LOG_ERROR, "error, non monotone timestamps %"PRId64" >= %"PRId64"\n", st->cur_dts, pkt->dts);
 
2565
        av_log(st->codec, AV_LOG_ERROR, "error, non monotone timestamps %"PRId64" >= %"PRId64"\n", st->cur_dts, pkt->dts);
2369
2566
        return -1;
2370
2567
    }
2371
2568
    if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
2372
 
        av_log(NULL, AV_LOG_ERROR, "error, pts < dts\n");
 
2569
        av_log(st->codec, AV_LOG_ERROR, "error, pts < dts\n");
2373
2570
        return -1;
2374
2571
    }
2375
2572
 
2398
2595
    return 0;
2399
2596
}
2400
2597
 
2401
 
static void truncate_ts(AVStream *st, AVPacket *pkt){
2402
 
    int64_t pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1;
2403
 
 
2404
 
//    if(pkt->dts < 0)
2405
 
//        pkt->dts= 0;  //this happens for low_delay=0 and B-frames, FIXME, needs further investigation about what we should do here
2406
 
 
2407
 
    if (pkt->pts != AV_NOPTS_VALUE)
2408
 
        pkt->pts &= pts_mask;
2409
 
    if (pkt->dts != AV_NOPTS_VALUE)
2410
 
        pkt->dts &= pts_mask;
2411
 
}
2412
 
 
2413
2598
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
2414
2599
{
2415
 
    int ret;
 
2600
    int ret = compute_pkt_fields2(s->streams[pkt->stream_index], pkt);
2416
2601
 
2417
 
    ret=compute_pkt_fields2(s->streams[pkt->stream_index], pkt);
2418
2602
    if(ret<0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
2419
2603
        return ret;
2420
2604
 
2421
 
    truncate_ts(s->streams[pkt->stream_index], pkt);
2422
 
 
2423
2605
    ret= s->oformat->write_packet(s, pkt);
2424
2606
    if(!ret)
2425
2607
        ret= url_ferror(s->pb);
2466
2648
        pktl= pktl->next;
2467
2649
    }
2468
2650
 
2469
 
    if(s->nb_streams == stream_count || (flush && stream_count)){
 
2651
    if(stream_count && (s->nb_streams == stream_count || flush)){
2470
2652
        pktl= s->packet_buffer;
2471
2653
        *out= pktl->pkt;
2472
2654
 
2515
2697
        if(ret<=0) //FIXME cleanup needed for ret<0 ?
2516
2698
            return ret;
2517
2699
 
2518
 
        truncate_ts(s->streams[opkt.stream_index], &opkt);
2519
2700
        ret= s->oformat->write_packet(s, &opkt);
2520
2701
 
2521
2702
        av_free_packet(&opkt);
2540
2721
        if(!ret)
2541
2722
            break;
2542
2723
 
2543
 
        truncate_ts(s->streams[pkt.stream_index], &pkt);
2544
2724
        ret= s->oformat->write_packet(s, &pkt);
2545
2725
 
2546
2726
        av_free_packet(&pkt);
2683
2863
    return ret;
2684
2864
}
2685
2865
 
2686
 
/**
2687
 
 * Gets the current time in microseconds.
2688
 
 */
2689
2866
int64_t av_gettime(void)
2690
2867
{
2691
2868
    struct timeval tv;
2699
2876
    int64_t t;
2700
2877
    struct tm dt;
2701
2878
    int i;
2702
 
    static const char *date_fmt[] = {
 
2879
    static const char * const date_fmt[] = {
2703
2880
        "%Y-%m-%d",
2704
2881
        "%Y%m%d",
2705
2882
    };
2706
 
    static const char *time_fmt[] = {
 
2883
    static const char * const time_fmt[] = {
2707
2884
        "%H:%M:%S",
2708
2885
        "%H%M%S",
2709
2886
    };
2728
2905
    q = NULL;
2729
2906
    if (!duration) {
2730
2907
        /* parse the year-month-day part */
2731
 
        for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) {
 
2908
        for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
2732
2909
            q = small_strptime(p, date_fmt[i], &dt);
2733
2910
            if (q) {
2734
2911
                break;
2752
2929
            p++;
2753
2930
 
2754
2931
        /* parse the hour-minute-second part */
2755
 
        for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) {
 
2932
        for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
2756
2933
            q = small_strptime(p, time_fmt[i], &dt);
2757
2934
            if (q) {
2758
2935
                break;
3038
3215
    }
3039
3216
}
3040
3217
 
 
3218
char *ff_data_to_hex(char *buff, const uint8_t *src, int s)
 
3219
{
 
3220
    int i;
 
3221
    static const char hex_table[16] = { '0', '1', '2', '3',
 
3222
                                        '4', '5', '6', '7',
 
3223
                                        '8', '9', 'A', 'B',
 
3224
                                        'C', 'D', 'E', 'F' };
 
3225
 
 
3226
    for(i = 0; i < s; i++) {
 
3227
        buff[i * 2]     = hex_table[src[i] >> 4];
 
3228
        buff[i * 2 + 1] = hex_table[src[i] & 0xF];
 
3229
    }
 
3230
 
 
3231
    return buff;
 
3232
}
 
3233
 
3041
3234
void av_set_pts_info(AVStream *s, int pts_wrap_bits,
3042
3235
                     int pts_num, int pts_den)
3043
3236
{
 
3237
    unsigned int gcd= ff_gcd(pts_num, pts_den);
3044
3238
    s->pts_wrap_bits = pts_wrap_bits;
3045
 
    s->time_base.num = pts_num;
3046
 
    s->time_base.den = pts_den;
3047
 
}
3048
 
 
3049
 
/* fraction handling */
3050
 
 
3051
 
/**
3052
 
 * f = val + (num / den) + 0.5.
3053
 
 *
3054
 
 * 'num' is normalized so that it is such as 0 <= num < den.
3055
 
 *
3056
 
 * @param f fractional number
3057
 
 * @param val integer value
3058
 
 * @param num must be >= 0
3059
 
 * @param den must be >= 1
3060
 
 */
3061
 
static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
3062
 
{
3063
 
    num += (den >> 1);
3064
 
    if (num >= den) {
3065
 
        val += num / den;
3066
 
        num = num % den;
3067
 
    }
3068
 
    f->val = val;
3069
 
    f->num = num;
3070
 
    f->den = den;
3071
 
}
3072
 
 
3073
 
/**
3074
 
 * Fractional addition to f: f = f + (incr / f->den).
3075
 
 *
3076
 
 * @param f fractional number
3077
 
 * @param incr increment, can be positive or negative
3078
 
 */
3079
 
static void av_frac_add(AVFrac *f, int64_t incr)
3080
 
{
3081
 
    int64_t num, den;
3082
 
 
3083
 
    num = f->num + incr;
3084
 
    den = f->den;
3085
 
    if (num < 0) {
3086
 
        f->val += num / den;
3087
 
        num = num % den;
3088
 
        if (num < 0) {
3089
 
            num += den;
3090
 
            f->val--;
3091
 
        }
3092
 
    } else if (num >= den) {
3093
 
        f->val += num / den;
3094
 
        num = num % den;
3095
 
    }
3096
 
    f->num = num;
 
3239
    s->time_base.num = pts_num/gcd;
 
3240
    s->time_base.den = pts_den/gcd;
 
3241
 
 
3242
    if(gcd>1)
 
3243
        av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, gcd);
3097
3244
}