From 9740beffc65d08ba454ce4c1d904b6d9388a6544 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Wed, 25 Feb 2004 17:35:52 +0000
Subject: [PATCH] use AVFrame.pts=AV_NOPTS_VALUE instead of AVFrame.pts=0 move
 AV_NOPTS_VALUE & AV_TIME_BASE from avformat.h -> avcodec.h related fixes

Originally committed as revision 2814 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 ffmpeg.c               |  9 +++++----
 libavcodec/avcodec.h   |  9 ++++++---
 libavcodec/h263.c      | 17 ++++++++---------
 libavcodec/mpegvideo.c |  8 ++++----
 libavcodec/utils.c     | 16 ++++++++++++++--
 libavformat/avformat.h |  3 ---
 6 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 138d531215..622e03aa0b 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -650,7 +650,7 @@ static void do_video_out(AVFormatContext *s,
         } else {
             AVFrame big_picture;
             
-            memset(&big_picture, 0, sizeof(AVFrame));
+            avcodec_get_frame_defaults(&big_picture);
             *(AVPicture*)&big_picture= *final_picture;
             /* better than nothing: use input picture interlaced
                settings */
@@ -851,7 +851,7 @@ static int output_packet(AVInputStream *ist, int ist_index,
     AVFrame picture;
     short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
     void *buffer_to_free;
-        
+    
     if (pkt && pkt->pts != AV_NOPTS_VALUE) {
         ist->pts = pkt->pts;
     } else {
@@ -896,7 +896,8 @@ static int output_packet(AVInputStream *ist, int ist_index,
             case CODEC_TYPE_VIDEO:
                     data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
                     /* XXX: allocate picture correctly */
-                    memset(&picture, 0, sizeof(picture));
+                    avcodec_get_frame_defaults(&picture);
+
                     ret = avcodec_decode_video(&ist->st->codec, 
                                                &picture, &got_picture, ptr, len);
                     ist->st->quality= picture.quality;
@@ -1009,7 +1010,7 @@ static int output_packet(AVInputStream *ist, int ist_index,
                             /* no reencoding needed : output the packet directly */
                             /* force the input stream PTS */
                         
-                            memset(&avframe, 0, sizeof(AVFrame));
+                            avcodec_get_frame_defaults(&avframe);
                             ost->st->codec.coded_frame= &avframe;
                             avframe.key_frame = pkt->flags & PKT_FLAG_KEY; 
                         
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 0fc295bf12..3d66f56726 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -17,7 +17,7 @@ extern "C" {
 
 #define FFMPEG_VERSION_INT     0x000408
 #define FFMPEG_VERSION         "0.4.8"
-#define LIBAVCODEC_BUILD       4703
+#define LIBAVCODEC_BUILD       4704
 
 #define LIBAVCODEC_VERSION_INT FFMPEG_VERSION_INT
 #define LIBAVCODEC_VERSION     FFMPEG_VERSION
@@ -26,6 +26,9 @@ extern "C" {
 #define AV_TOSTRING(s) #s
 #define LIBAVCODEC_IDENT	"FFmpeg" LIBAVCODEC_VERSION "b" AV_STRINGIFY(LIBAVCODEC_BUILD)
 
+#define AV_NOPTS_VALUE INT64_MIN
+#define AV_TIME_BASE 1000000
+
 enum CodecID {
     CODEC_ID_NONE, 
     CODEC_ID_MPEG1VIDEO,
@@ -372,8 +375,8 @@ typedef struct AVPanScan{
     int pict_type;\
 \
     /**\
-     * presentation timestamp in micro seconds (time when frame should be shown to user)\
-     * if 0 then the frame_rate will be used as reference\
+     * presentation timestamp in AV_TIME_BASE (=micro seconds currently) (time when frame should be shown to user)\
+     * if AV_NOPTS_VALUE then the frame_rate will be used as reference\
      * - encoding: MUST be set by user\
      * - decoding: set by lavc\
      */\
diff --git a/libavcodec/h263.c b/libavcodec/h263.c
index 2318c07774..3df42aabe9 100644
--- a/libavcodec/h263.c
+++ b/libavcodec/h263.c
@@ -2156,8 +2156,8 @@ void ff_mpeg4_stuffing(PutBitContext * pbc)
 void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
     int time_div, time_mod;
 
-    assert(s->current_picture_ptr->pts);
-    s->time= (s->current_picture_ptr->pts*s->time_increment_resolution + 500*1000)/(1000*1000);
+    assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE);
+    s->time= (s->current_picture_ptr->pts*s->time_increment_resolution + AV_TIME_BASE/2)/AV_TIME_BASE;
 
     time_div= s->time/s->time_increment_resolution;
     time_mod= s->time%s->time_increment_resolution;
@@ -2179,11 +2179,10 @@ static void mpeg4_encode_gop_header(MpegEncContext * s){
     put_bits(&s->pb, 16, 0);
     put_bits(&s->pb, 16, GOP_STARTCODE);
     
-    if(s->current_picture_ptr->pts && s->reordered_input_picture[1]){
-        time= FFMIN(s->reordered_input_picture[1]->pts, s->current_picture_ptr->pts);
-        time= (time*s->time_increment_resolution + 500*1000)/(1000*1000);
-    }else
-        time= av_rescale(s->current_picture_ptr->coded_picture_number*(int64_t)s->avctx->frame_rate_base, s->time_increment_resolution, s->avctx->frame_rate);
+    time= s->current_picture_ptr->pts;
+    if(s->reordered_input_picture[1])
+        time= FFMIN(time, s->reordered_input_picture[1]->pts);
+    time= (time*s->time_increment_resolution + AV_TIME_BASE/2)/AV_TIME_BASE;
 
     seconds= time/s->time_increment_resolution;
     minutes= seconds/60; seconds %= 60;
@@ -5709,9 +5708,9 @@ static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
                            - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
     }
     
-    s->current_picture_ptr->pts= s->time*1000LL*1000LL / s->time_increment_resolution;
+    s->current_picture_ptr->pts= s->time*(int64_t)AV_TIME_BASE / s->time_increment_resolution;
     if(s->avctx->debug&FF_DEBUG_PTS)
-        av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %f\n", s->current_picture_ptr->pts/(1000.0*1000.0));
+        av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %f\n", s->current_picture_ptr->pts/(float)AV_TIME_BASE);
     
     check_marker(gb, "before vop_coded");
     
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index a5048e3968..3ce01c43aa 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -1850,14 +1850,14 @@ 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){ 
+    if(pic->pts != AV_NOPTS_VALUE){ 
         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);
+            pic->pts= s->user_specified_pts + AV_TIME_BASE*(int64_t)s->avctx->frame_rate_base / s->avctx->frame_rate;
+            av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? 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);
+            pic->pts= av_rescale(pic->display_picture_number*(int64_t)s->avctx->frame_rate_base, AV_TIME_BASE, s->avctx->frame_rate);
         }
     }
   }
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index eb4b19eedc..7227e4f507 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -339,6 +339,8 @@ enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum Pixel
 }
 
 void avcodec_get_context_defaults(AVCodecContext *s){
+    memset(s, 0, sizeof(AVCodecContext));
+
     s->bit_rate= 800*1000;
     s->bit_rate_tolerance= s->bit_rate*10;
     s->qmin= 2;
@@ -381,7 +383,7 @@ void avcodec_get_context_defaults(AVCodecContext *s){
  * this can be deallocated by simply calling free() 
  */
 AVCodecContext *avcodec_alloc_context(void){
-    AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
+    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
     
     if(avctx==NULL) return NULL;
     
@@ -390,12 +392,22 @@ AVCodecContext *avcodec_alloc_context(void){
     return avctx;
 }
 
+void avcodec_get_frame_defaults(AVFrame *pic){
+    memset(pic, 0, sizeof(AVFrame));
+
+    pic->pts= AV_NOPTS_VALUE;
+}
+
 /**
  * allocates a AVPFrame and set it to defaults.
  * this can be deallocated by simply calling free() 
  */
 AVFrame *avcodec_alloc_frame(void){
-    AVFrame *pic= av_mallocz(sizeof(AVFrame));
+    AVFrame *pic= av_malloc(sizeof(AVFrame));
+    
+    if(pic==NULL) return NULL;
+    
+    avcodec_get_frame_defaults(pic);
     
     return pic;
 }
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index a44fe8ffbd..432d2e57bc 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -27,9 +27,6 @@ extern "C" {
 #define MININT64 int64_t_C(0x8000000000000000)
 #endif
 
-#define AV_NOPTS_VALUE MININT64
-#define AV_TIME_BASE 1000000
-
 typedef struct AVPacket {
     int64_t pts; /* presentation time stamp in AV_TIME_BASE units (or
                     pts_den units in muxers or demuxers) */