From e51f49487e13e544e4540d60ed4e4c28f1b5ef62 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Wed, 25 Feb 2004 02:05:50 +0000
Subject: [PATCH] set AVFrame.pts based upon framerate if its 0 detect & warn
 if the user prvides pts only for some frames, and try to guess the missing
 ones

Originally committed as revision 2812 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavcodec/h263.c      |  7 +++----
 libavcodec/mpegvideo.c | 12 +++++++++++-
 libavcodec/mpegvideo.h |  1 +
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/libavcodec/h263.c b/libavcodec/h263.c
index 313214cd45f..2318c077742 100644
--- a/libavcodec/h263.c
+++ b/libavcodec/h263.c
@@ -2156,10 +2156,9 @@ void ff_mpeg4_stuffing(PutBitContext * pbc)
 void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
     int time_div, time_mod;
 
-    if(s->current_picture_ptr->pts)
-        s->time= (s->current_picture_ptr->pts*s->time_increment_resolution + 500*1000)/(1000*1000);
-    else
-        s->time= av_rescale(picture_number*(int64_t)s->avctx->frame_rate_base, s->time_increment_resolution, s->avctx->frame_rate);
+    assert(s->current_picture_ptr->pts);
+    s->time= (s->current_picture_ptr->pts*s->time_increment_resolution + 500*1000)/(1000*1000);
+
     time_div= s->time/s->time_increment_resolution;
     time_mod= s->time%s->time_increment_resolution;
 
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 3b36f972445..a5048e3968a 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -1850,8 +1850,18 @@ static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
     copy_picture_attributes(pic, pic_arg);
     
     pic->display_picture_number= s->input_picture_number++;
+    if(pic->pts){ 
+        s->user_specified_pts= pic->pts;
+    }else{
+        if(s->user_specified_pts){
+            pic->pts= s->user_specified_pts + 1000ULL*1000ULL*s->avctx->frame_rate_base / s->avctx->frame_rate;
+            av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=0 trying to guess (%Ld)\n", pic->pts);
+        }else{
+            pic->pts= av_rescale(pic->display_picture_number*(int64_t)s->avctx->frame_rate_base, 1000*1000, s->avctx->frame_rate);
+        }
+    }
   }
-
+  
     /* shift buffer entries */
     for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
         s->input_picture[i-1]= s->input_picture[i];
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index e6e242323e4..51cc5e1001d 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -275,6 +275,7 @@ typedef struct MpegEncContext {
     int picture_number;       //FIXME remove, unclear definition
     int picture_in_gop_number; ///< 0-> first pic in gop, ... 
     int b_frames_since_non_b;  ///< used for encoding, relative to not yet reordered input 
+    int64_t user_specified_pts;///< last non zero pts from AVFrame which was passed into avcodec_encode_video()
     int mb_width, mb_height;   ///< number of MBs horizontally & vertically 
     int mb_stride;             ///< mb_width+1 used for some arrays to allow simple addressng of left & top MBs withoutt sig11
     int b8_stride;             ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressng
-- 
GitLab