[FFmpeg-devel] [RFC][PATCH] ticks_per_frame / timebase

Michael Niedermayer michaelni
Thu Feb 26 15:15:09 CET 2009


Hi

Attached is a patch that adds ticks_per_frame, which is a convertion factor
between the timebase and the "framerate" as stored in the header.
the patch also adapts several uses of timebase that where using it in the
meaning of the framerate
this patch is based on the one from ivan

i will commit this in a few hours if i receive no objections or better
suggestions. (short delay because of the release ...)

comments welcome, especially from baptiste & ivan


-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The worst form of inequality is to try to make unequal things equal.
-- Aristotle
-------------- next part --------------
Index: ffmpeg.c
===================================================================
--- ffmpeg.c	(revision 17613)
+++ ffmpeg.c	(working copy)
@@ -1261,7 +1261,7 @@
                         goto discard_packet;
                     }
                     if (ist->st->codec->time_base.num != 0) {
-                        int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : 1;
+                        int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
                         ist->next_pts += ((int64_t)AV_TIME_BASE *
                                           ist->st->codec->time_base.num * ticks) /
                             ist->st->codec->time_base.den;
@@ -1290,7 +1290,7 @@
                 break;
             case CODEC_TYPE_VIDEO:
                 if (ist->st->codec->time_base.num != 0) {
-                    int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : 1;
+                    int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
                     ist->next_pts += ((int64_t)AV_TIME_BASE *
                                       ist->st->codec->time_base.num * ticks) /
                         ist->st->codec->time_base.den;
@@ -2878,11 +2878,11 @@
             if(me_threshold)
                 enc->debug |= FF_DEBUG_MV;
 
-            if (enc->time_base.den != rfps || enc->time_base.num != rfps_base) {
+            if (enc->time_base.den != rfps || enc->time_base.num * enc->ticks_per_frame != rfps_base) {
 
                 if (verbose >= 0)
                     fprintf(stderr,"\nSeems stream %d codec frame rate differs from container frame rate: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
-                            i, (float)enc->time_base.den / enc->time_base.num, enc->time_base.den, enc->time_base.num,
+                            i, (float)enc->time_base.den / (enc->time_base.num * enc->ticks_per_frame), enc->time_base.den, enc->time_base.num * enc->ticks_per_frame,
 
                     (float)rfps / rfps_base, rfps, rfps_base);
             }
Index: libavcodec/options.c
===================================================================
--- libavcodec/options.c	(revision 17596)
+++ libavcodec/options.c	(working copy)
@@ -393,6 +393,7 @@
 {"request_channel_layout", NULL, OFFSET(request_channel_layout), FF_OPT_TYPE_INT64, DEFAULT, 0, INT64_MAX, A|D, "request_channel_layout"},
 {"rc_max_vbv_use", NULL, OFFSET(rc_max_available_vbv_use), FF_OPT_TYPE_FLOAT, 1.0/3, 0.0, FLT_MAX, V|E},
 {"rc_min_vbv_use", NULL, OFFSET(rc_min_vbv_overflow_use),  FF_OPT_TYPE_FLOAT, 3,     0.0, FLT_MAX, V|E},
+{"ticks_per_frame", NULL, OFFSET(ticks_per_frame), FF_OPT_TYPE_INT, 1, 1, INT_MAX, A|V|E|D},
 {NULL},
 };
 
Index: libavcodec/h264.c
===================================================================
--- libavcodec/h264.c	(revision 17596)
+++ libavcodec/h264.c	(working copy)
@@ -2201,6 +2201,7 @@
     h->sei_dpb_output_delay = 0;
     h->sei_cpb_removal_delay = -1;
     h->sei_buffering_period_present = 0;
+    avctx->ticks_per_frame = 2;
     return 0;
 }
 
Index: libavcodec/mpeg12.c
===================================================================
--- libavcodec/mpeg12.c	(revision 17596)
+++ libavcodec/mpeg12.c	(working copy)
@@ -1269,7 +1269,7 @@
             //MPEG-1 aspect
             avctx->sample_aspect_ratio= av_d2q(
                     1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
-
+            avctx->ticks_per_frame=1;
         }else{//MPEG-2
         //MPEG-2 fps
             av_reduce(
@@ -1278,6 +1278,7 @@
                 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
                 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
                 1<<30);
+            avctx->ticks_per_frame=2;
         //MPEG-2 aspect
             if(s->aspect_ratio_info > 1){
                 //we ignore the spec here as reality does not match the spec, see for example
Index: libavcodec/avcodec.h
===================================================================
--- libavcodec/avcodec.h	(revision 17596)
+++ libavcodec/avcodec.h	(working copy)
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVCODEC_VERSION_MAJOR 52
-#define LIBAVCODEC_VERSION_MINOR 19
+#define LIBAVCODEC_VERSION_MINOR 20
 #define LIBAVCODEC_VERSION_MICRO  0
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
@@ -2322,6 +2322,15 @@
      * - decoding: Set by libavcodec
      */
     struct AVHWAccel *hwaccel;
+
+    /**
+     * For some codecs, the time base is closer to the field rate than the frame rate.
+     * Most notably, H.264 and MPEG-2 specify time_base as half of frame duration
+     * if no telecine is used ...
+     *
+     * Set to time_base ticks per frame. Default 1, e.g., H.264/MPEG-2 set it to 2.
+     */
+    int ticks_per_frame;
 } AVCodecContext;
 
 /**
Index: libavformat/avformat.h
===================================================================
--- libavformat/avformat.h	(revision 17596)
+++ libavformat/avformat.h	(working copy)
@@ -23,7 +23,7 @@
 
 #define LIBAVFORMAT_VERSION_MAJOR 52
 #define LIBAVFORMAT_VERSION_MINOR 30
-#define LIBAVFORMAT_VERSION_MICRO  0
+#define LIBAVFORMAT_VERSION_MICRO  1
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
Index: libavformat/utils.c
===================================================================
--- libavformat/utils.c	(revision 17596)
+++ libavformat/utils.c	(working copy)
@@ -2201,9 +2201,9 @@
 
             if (!st->r_frame_rate.num){
                 if(    st->codec->time_base.den * (int64_t)st->time_base.num
-                    <= st->codec->time_base.num * (int64_t)st->time_base.den){
+                    <= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t)st->time_base.den){
                     st->r_frame_rate.num = st->codec->time_base.den;
-                    st->r_frame_rate.den = st->codec->time_base.num;
+                    st->r_frame_rate.den = st->codec->time_base.num * st->codec->ticks_per_frame;
                 }else{
                     st->r_frame_rate.num = st->time_base.den;
                     st->r_frame_rate.den = st->time_base.num;
@@ -2537,7 +2537,7 @@
     if (pkt->duration == 0) {
         compute_frame_duration(&num, &den, st, NULL, pkt);
         if (den && num) {
-            pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num);
+            pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
         }
     }
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090226/80e5fe7c/attachment.pgp>



More information about the ffmpeg-devel mailing list