move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
[ffmpeg.git] / ffmpeg.c
index a30a9d0..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"
 
@@ -88,6 +89,8 @@ static int video_bit_rate_tolerance = 4000*1000;
 static float video_qscale = 0;
 static int video_qmin = 2;
 static int video_qmax = 31;
+static int video_lmin = 2*FF_QP2LAMBDA;
+static int video_lmax = 31*FF_QP2LAMBDA;
 static int video_mb_qmin = 2;
 static int video_mb_qmax = 31;
 static int video_qdiff = 3;
@@ -140,6 +143,7 @@ static int use_ss = 0;
 static int use_alt_scan = 0;
 static int use_trell = 0;
 static int use_scan_offset = 0;
+static int use_qpel = 0;
 static int qns = 0;
 static int closed_gop = 0;
 static int do_deinterlace = 0;
@@ -160,6 +164,11 @@ static int sc_threshold = 0;
 static int debug = 0;
 static int debug_mv = 0;
 static int me_threshold = 0;
+static int mb_threshold = 0;
+static int intra_dc_precision = 0;
+static int coder = 0;
+static int context = 0;
+static int predictor = 0;
 extern int loop_input; /* currently a hack */
 
 static int gop_size = 12;
@@ -205,6 +214,7 @@ static int using_vhook = 0;
 static int verbose = 1;
 static int thread_count= 1;
 static int q_pressed = 0;
+static int me_range = 0;
 static int64_t video_size = 0;
 static int64_t audio_size = 0;
 static int64_t extra_size = 0;
@@ -585,21 +595,21 @@ 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)
             nb_frames = 2;
         else if (av_delay > AV_DELAY_MAX)
             nb_frames = 0;
-//        printf("delay=%f nb=%d (A)\n", av_delay, nb_frames);
+//        printf("adelta=%f vdelta=%f delay=%f nb=%d (A)\n", adelta, vdelta, av_delay, nb_frames);
     } 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;
@@ -620,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]);
         }
     }
@@ -862,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;
     
@@ -946,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;
     }
@@ -981,7 +991,7 @@ static void print_report(AVFormatContext **output_files,
 /* pkt = NULL means EOF (needed to flush decoder buffers) */
 static int output_packet(AVInputStream *ist, int ist_index,
                          AVOutputStream **ost_table, int nb_ostreams,
-                         AVPacket *pkt)
+                         const AVPacket *pkt)
 {
     AVFormatContext *os;
     AVOutputStream *ost;
@@ -994,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;
     }
@@ -1031,8 +1041,8 @@ static int output_packet(AVInputStream *ist, int ist_index,
                     continue;
                 }
                 data_buf = (uint8_t *)samples;
-                ist->next_pts += ((int64_t)AV_TIME_BASE * data_size) / 
-                    (2 * ist->st->codec.channels);
+                ist->next_pts += ((int64_t)AV_TIME_BASE/2 * data_size) / 
+                    (ist->st->codec.sample_rate * ist->st->codec.channels);
                 break;
             case CODEC_TYPE_VIDEO:
                     data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
@@ -1108,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;
@@ -1155,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++;
@@ -1622,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;
@@ -1864,6 +1879,10 @@ static void opt_me_threshold(const char *arg)
     me_threshold = atoi(arg);
 }
 
+static void opt_mb_threshold(const char *arg)
+{
+    mb_threshold = atoi(arg);
+}
 
 static void opt_error_resilience(const char *arg)
 {
@@ -2190,6 +2209,16 @@ static void opt_qscale(const char *arg)
     }
 }
 
+static void opt_lmax(const char *arg)
+{
+    video_lmax = atof(arg)*FF_QP2LAMBDA;
+}
+
+static void opt_lmin(const char *arg)
+{
+    video_lmin = atof(arg)*FF_QP2LAMBDA;
+}
+
 static void opt_qmin(const char *arg)
 {
     video_qmin = atoi(arg);
@@ -2315,6 +2344,11 @@ static void opt_sc_threshold(const char *arg)
     sc_threshold= atoi(arg);
 }
 
+static void opt_me_range(const char *arg)
+{
+    me_range = atoi(arg);
+}
+
 static void opt_thread_count(const char *arg)
 {
     thread_count= atoi(arg);
@@ -2695,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);
@@ -2716,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;
@@ -2801,6 +2862,9 @@ static void opt_output_file(const char *filename)
                if (closed_gop) {
                     video_enc->flags |= CODEC_FLAG_CLOSED_GOP;
                 }
+               if (use_qpel) {
+                    video_enc->flags |= CODEC_FLAG_QPEL;
+                }
                 if (b_frames) {
                     video_enc->max_b_frames = b_frames;
                     video_enc->b_frame_strategy = 0;
@@ -2814,6 +2878,8 @@ static void opt_output_file(const char *filename)
                 }
                 video_enc->qmin = video_qmin;
                 video_enc->qmax = video_qmax;
+                video_enc->lmin = video_lmin;
+                video_enc->lmax = video_lmax;
                 video_enc->mb_qmin = video_mb_qmin;
                 video_enc->mb_qmax = video_mb_qmax;
                 video_enc->max_qdiff = video_qdiff;
@@ -2863,10 +2929,17 @@ static void opt_output_file(const char *filename)
                 video_enc->dct_algo = dct_algo;
                 video_enc->idct_algo = idct_algo;
                 video_enc->me_threshold= me_threshold;
+                video_enc->mb_threshold= mb_threshold;
+                video_enc->intra_dc_precision= intra_dc_precision;
                 video_enc->strict_std_compliance = strict;
                 video_enc->error_rate = error_rate;
                 video_enc->noise_reduction= noise_reduction;
                 video_enc->scenechange_threshold= sc_threshold;
+                video_enc->me_range = me_range;
+                video_enc->coder_type= coder;
+                video_enc->context_model= context;
+                video_enc->prediction_method= predictor;
+
                 if(packet_size){
                     video_enc->rtp_mode= 1;
                     video_enc->rtp_payload_size= packet_size;
@@ -2886,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);
@@ -2928,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;
@@ -3403,10 +3471,20 @@ static void opt_target(const char *arg)
     }
 }
 
+static void show_version(void)
+{
+    printf("ffmpeg      " FFMPEG_VERSION "\n"
+           "libavcodec  %d\n"
+           "libavformat %d\n", 
+           avcodec_build(), LIBAVFORMAT_BUILD);
+    exit(1);
+}
+
 const OptionDef options[] = {
     /* main options */
     { "L", 0, {(void*)show_license}, "show license" },
     { "h", 0, {(void*)show_help}, "show help" },
+    { "version", 0, {(void*)show_version}, "show version" },
     { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
     { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
     { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
@@ -3457,6 +3535,8 @@ const OptionDef options[] = {
     { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
     { "qmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
     { "qmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
+    { "lmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_lmin}, "min video lagrange factor (VBR)", "lambda" },
+    { "lmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_lmax}, "max video lagrange factor (VBR)", "lambda" },
     { "mbqmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
     { "mbqmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
     { "qdiff", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
@@ -3482,6 +3562,7 @@ const OptionDef options[] = {
     { "dct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_dct_algo}, "set dct algo",  "algo" },
     { "idct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_idct_algo}, "set idct algo",  "algo" },
     { "me_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_me_threshold}, "motion estimaton threshold",  "" },
+    { "mb_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_threshold}, "macroblock threshold",  "" },
     { "er", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_resilience}, "set error resilience",  "n" },
     { "ec", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_concealment}, "set error concealment",  "bit_mask" },
     { "bf", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_frames}, "use 'frames' B frames", "frames" },
@@ -3527,12 +3608,18 @@ const OptionDef options[] = {
     { "trell", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_trell}, "enable trellis quantization" },
     { "cgop", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&closed_gop}, "closed gop" },
     { "scan_offset", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_scan_offset}, "enable SVCD Scan Offset placeholder" },
+    { "qpel", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_qpel}, "enable 1/4-pel" },
     { "intra_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_intra_matrix}, "specify intra matrix coeffs", "matrix" },
     { "inter_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_inter_matrix}, "specify inter matrix coeffs", "matrix" },
     { "top", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_top_field_first}, "top=1/bottom=0/auto=-1 field first", "" },
     { "nr", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_noise_reduction}, "noise reduction", "" },
     { "qns", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qns}, "quantization noise shaping", "" },
     { "sc_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_sc_threshold}, "scene change threshold", "threshold" },
+    { "me_range", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_me_range}, "limit motion vectors range (1023 for DivX player)", "range" },
+    { "dc", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_dc_precision}, "intra_dc_precision", "precision" },
+    { "coder", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&coder}, "coder type", "" },
+    { "context", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&context}, "context model", "" },
+    { "pred", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&predictor}, "prediction method", "" },
 
     /* audio options */
     { "ab", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },