move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
[ffmpeg.git] / ffmpeg.c
index 53354c6..e5e2d0a 100644 (file)
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -17,6 +17,7 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 #define HAVE_AV_CONFIG_H
+#include <limits.h>
 #include "avformat.h"
 #include "framehook.h"
 
@@ -594,10 +595,10 @@ static void do_video_out(AVFormatContext *s,
         double adelta, vdelta, av_delay;
 
         adelta = audio_sync->sync_ipts - ((double)audio_sync->sync_opts * 
-            s->pts_num / s->pts_den);
+            ost->st->time_base.num / ost->st->time_base.den);
 
         vdelta = ost->sync_ipts - ((double)ost->sync_opts *
-            s->pts_num / s->pts_den);
+            ost->st->time_base.num / ost->st->time_base.den);
 
         av_delay = adelta - vdelta;
         if (av_delay < -AV_DELAY_MAX)
@@ -608,7 +609,7 @@ static void do_video_out(AVFormatContext *s,
     } else {
         double vdelta;
 
-        vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
+        vdelta = (double)(ost->st->pts.val) * ost->st->time_base.num / ost->st->time_base.den - (ost->sync_ipts - ost->sync_ipts_offset);
         if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
             if (vdelta < -AV_DELAY_MAX)
                 nb_frames = 2;
@@ -629,10 +630,10 @@ static void do_video_out(AVFormatContext *s,
         if (audio_sync && verbose >=0) {
             fprintf(stderr, "Input APTS %12.6f, output APTS %12.6f, ",
                     (double) audio_sync->sync_ipts, 
-                    (double) audio_sync->st->pts.val * s->pts_num / s->pts_den);
+                    (double) audio_sync->st->pts.val * st->time_base.num / st->time_base.den);
             fprintf(stderr, "Input VPTS %12.6f, output VPTS %12.6f: %s\n",
                     (double) ost->sync_ipts, 
-                    (double) ost->st->pts.val * s->pts_num / s->pts_den,
+                    (double) ost->st->pts.val * st->time_base.num / st->time_base.den,
                     action[nb_frames]);
         }
     }
@@ -871,7 +872,7 @@ static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
         
         fprintf(fvstats,"f_size= %6d ", frame_size);
         /* compute pts value */
-        ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
+        ti1 = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
         if (ti1 < 0.01)
             ti1 = 0.01;
     
@@ -955,7 +956,7 @@ static void print_report(AVFormatContext **output_files,
             vid = 1;
         }
         /* compute min output value */
-        pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
+        pts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
         if ((pts < ti1) && (pts > 0))
             ti1 = pts;
     }
@@ -1003,7 +1004,7 @@ static int output_packet(AVInputStream *ist, int ist_index,
     void *buffer_to_free;
     
     if (pkt && pkt->pts != AV_NOPTS_VALUE) {
-        ist->pts = pkt->pts;
+        ist->next_pts = ist->pts = pkt->pts;
     } else {
         ist->pts = ist->next_pts;
     }
@@ -1117,7 +1118,7 @@ static int output_packet(AVInputStream *ist, int ist_index,
                         printf("%d: got pts=%0.3f %0.3f\n", i, 
                                (double)pkt->pts / AV_TIME_BASE, 
                                ((double)ist->pts / AV_TIME_BASE) - 
-                               ((double)ost->st->pts.val * os->pts_num / os->pts_den));
+                               ((double)ost->st->pts.val * ost->time_base.num / ost->time_base.den));
 #endif
                         /* set the input output pts pairs */
                         ost->sync_ipts = (double)ist->pts / AV_TIME_BASE;
@@ -1164,7 +1165,12 @@ static int output_packet(AVInputStream *ist, int ist_index,
                             avcodec_get_frame_defaults(&avframe);
                             ost->st->codec.coded_frame= &avframe;
                             avframe.key_frame = pkt->flags & PKT_FLAG_KEY; 
-                            ost->st->pts.val= av_rescale(ist->pts, os->pts_den, os->pts_num*AV_TIME_BASE);
+                            ost->st->pts.val= av_rescale(ist->pts, ost->st->time_base.den, ost->st->time_base.num*AV_TIME_BASE);
+
+                            if(ost->st->codec.codec_type == CODEC_TYPE_AUDIO)
+                                audio_size += data_size;
+                            else if (ost->st->codec.codec_type == CODEC_TYPE_VIDEO)
+                                video_size += data_size;
 
                             av_write_frame(os, ost->index, data_buf, data_size);
                             ost->st->codec.frame_number++;
@@ -1631,7 +1637,7 @@ static int av_encode(AVFormatContext **output_files,
             ost = ost_table[i];
             os = output_files[ost->file_index];
             ist = ist_table[ost->source_index];
-            pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
+            pts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
             if (!file_table[ist->file_index].eof_reached && 
                 pts < pts_min) {
                 pts_min = pts;
@@ -2723,12 +2729,11 @@ static void opt_output_file(const char *filename)
         if (use_video) {
             AVCodecContext *video_enc;
             
-            st = av_mallocz(sizeof(AVStream));
+            st = av_new_stream(oc, nb_streams++);
             if (!st) {
                 fprintf(stderr, "Could not alloc stream\n");
                 exit(1);
             }
-            avcodec_get_context_defaults(&st->codec);
 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
             if(thread_count>1)
                 avcodec_thread_init(&st->codec, thread_count);
@@ -2744,22 +2749,50 @@ static void opt_output_file(const char *filename)
             } else {
                 char *p;
                 int i;
+                AVCodec *codec;
             
                 codec_id = file_oformat->video_codec;
                 if (video_codec_id != CODEC_ID_NONE)
                     codec_id = video_codec_id;
                 
                 video_enc->codec_id = codec_id;
+                codec = avcodec_find_encoder(codec_id);
                 
                 video_enc->bit_rate = video_bit_rate;
                 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
                 video_enc->frame_rate = frame_rate; 
                 video_enc->frame_rate_base = frame_rate_base; 
+                if(codec && codec->supported_framerates){
+                    const AVRational *p= codec->supported_framerates;
+                    AVRational req= (AVRational){frame_rate, frame_rate_base};
+                    const AVRational *best=NULL;
+                    AVRational best_error= (AVRational){INT_MAX, 1};
+                    for(; p->den!=0; p++){
+                        AVRational error= av_sub_q(req, *p);
+                        if(error.num <0) error.num *= -1;
+                        if(av_cmp_q(error, best_error) < 0){
+                            best_error= error;
+                            best= p;
+                        }
+                    }
+                    video_enc->frame_rate     = best->num;
+                    video_enc->frame_rate_base= best->den;
+                }
                 
                 video_enc->width = frame_width + frame_padright + frame_padleft;
                 video_enc->height = frame_height + frame_padtop + frame_padbottom;
                video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
-               video_enc->pix_fmt = frame_pix_fmt;
+                video_enc->pix_fmt = frame_pix_fmt;
+
+                if(codec && codec->pix_fmts){
+                    const enum PixelFormat *p= codec->pix_fmts;
+                    for(; *p!=-1; p++){
+                        if(*p == video_enc->pix_fmt)
+                            break;
+                    }
+                    if(*p == -1)
+                        video_enc->pix_fmt = codec->pix_fmts[0];
+                }
 
                 if (!intra_only)
                     video_enc->gop_size = gop_size;
@@ -2926,19 +2959,16 @@ static void opt_output_file(const char *filename)
                     }
                 }
             }
-            oc->streams[nb_streams] = st;
-            nb_streams++;
         }
     
         if (use_audio) {
             AVCodecContext *audio_enc;
 
-            st = av_mallocz(sizeof(AVStream));
+            st = av_new_stream(oc, nb_streams++);
             if (!st) {
                 fprintf(stderr, "Could not alloc stream\n");
                 exit(1);
             }
-            avcodec_get_context_defaults(&st->codec);
 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
             if(thread_count>1)
                 avcodec_thread_init(&st->codec, thread_count);
@@ -2968,8 +2998,6 @@ static void opt_output_file(const char *filename)
                 } else
                     audio_enc->channels = audio_channels;
             }
-            oc->streams[nb_streams] = st;
-            nb_streams++;
         }
 
         oc->nb_streams = nb_streams;