]> git.sesse.net Git - ffmpeg/blobdiff - ffmpeg.c
fixing qmin==qmax && qsquish==1
[ffmpeg] / ffmpeg.c
index 103dc0dea1e8b2f9f376fe62882c7cc1b72eb14f..7816da9391a7847ce7173e1bb0955335f5343c1e 100644 (file)
--- a/ffmpeg.c
+++ b/ffmpeg.c
 #include <termios.h>
 #include <sys/resource.h>
 #endif
+#ifdef __BEOS__
+/* for snooze() */
+#include <OS.h>
+#endif
 #include <time.h>
 #include <ctype.h>
 
@@ -76,14 +80,28 @@ static AVOutputFormat *file_oformat;
 static int frame_width  = 160;
 static int frame_height = 128;
 static int frame_rate = 25 * FRAME_RATE_BASE;
-static int video_bit_rate = 200000;
-static int video_bit_rate_tolerance = 200000;
+static int video_bit_rate = 200*1000;
+static int video_bit_rate_tolerance = 4000*1000;
 static int video_qscale = 0;
-static int video_qmin = 3;
-static int video_qmax = 15;
+static int video_qmin = 2;
+static int video_qmax = 31;
 static int video_qdiff = 3;
 static float video_qblur = 0.5;
 static float video_qcomp = 0.5;
+static float video_rc_qsquish=1.0;
+static float video_rc_qmod_amp=0;
+static int video_rc_qmod_freq=0;
+static char *video_rc_override_string=NULL;
+static char *video_rc_eq="tex^qComp";
+static int video_rc_buffer_size=0;
+static float video_rc_buffer_aggressivity=1.0;
+static int video_rc_max_rate=0;
+static int video_rc_min_rate=0;
+static float video_rc_initial_cplx=0;
+static float video_b_qfactor = 1.25;
+static float video_b_qoffset = 1.25;
+static float video_i_qfactor = 0.8;
+static float video_i_qoffset = 0.0;
 static int me_method = 0;
 static int video_disable = 0;
 static int video_codec_id = CODEC_ID_NONE;
@@ -92,6 +110,7 @@ static int b_frames = 0;
 static int use_hq = 0;
 static int use_4mv = 0;
 static int do_deinterlace = 0;
+static int workaround_bugs = 0;
 
 static int gop_size = 12;
 static int intra_only = 0;
@@ -207,8 +226,11 @@ static int read_key(void)
     tv.tv_usec = 0;
     n = select(1, &rfds, NULL, NULL, &tv);
     if (n > 0) {
-        if (read(0, &ch, 1) == 1)
+        n = read(0, &ch, 1);
+        if (n == 1)
             return ch;
+
+        return n;
     }
     return -1;
 }
@@ -226,7 +248,7 @@ static void term_init(void)
 
 static int read_key(void)
 {
-    return -1;
+    return 0;
 }
 
 #endif
@@ -417,10 +439,12 @@ static void do_video_out(AVFormatContext *s,
     int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
     AVPicture *picture, *picture2, *pict;
     AVPicture picture_tmp1, picture_tmp2;
-    UINT8 video_buffer[1024*1024];
+    static UINT8 *video_buffer;
     UINT8 *buf = NULL, *buf1 = NULL;
     AVCodecContext *enc, *dec;
 
+#define VIDEO_BUFFER_SIZE (1024*1024)
+
     enc = &ost->st->codec;
     dec = &ist->st->codec;
 
@@ -434,6 +458,10 @@ static void do_video_out(AVFormatContext *s,
     if (nb <= 0) 
         return;
 
+    if (!video_buffer)
+        video_buffer= av_malloc(VIDEO_BUFFER_SIZE);
+    if(!video_buffer) return;
+
     /* deinterlace : must be done before any resize */
     if (do_deinterlace) {
         int size;
@@ -501,7 +529,7 @@ static void do_video_out(AVFormatContext *s,
             }
             
             ret = avcodec_encode_video(enc, 
-                                       video_buffer, sizeof(video_buffer), 
+                                       video_buffer, VIDEO_BUFFER_SIZE,
                                        picture);
             //enc->frame_number = enc->real_pict_num;
             s->oformat->write_packet(s, ost->index, video_buffer, ret, 0);
@@ -591,6 +619,7 @@ static int av_encode(AVFormatContext **output_files,
     INT64 min_pts, start_time;
     AVInputFile *file_table;
     AVFormatContext *stream_no_data;
+    int key;
 
     file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
     if (!file_table)
@@ -800,7 +829,7 @@ static int av_encode(AVFormatContext **output_files,
             }
             break;
         default:
-            abort();
+            av_abort();
         }
     }
 
@@ -875,9 +904,10 @@ static int av_encode(AVFormatContext **output_files,
 #endif
     term_init();
 
-    start_time = gettime();
+    start_time = av_gettime();
     min_pts = 0;
     stream_no_data = 0;
+    key = -1;
 
     for(;;) {
         int file_index, ist_index;
@@ -891,8 +921,12 @@ static int av_encode(AVFormatContext **output_files,
 
     redo:
         /* if 'q' pressed, exits */
-        if (read_key() == 'q')
-            break;
+        if (key) {
+            /* read_key() returns 0 on EOF */
+            key = read_key();
+            if (key == 'q')
+                break;
+        }
 
         /* select the input file with the smallest pts */
         file_index = -1;
@@ -910,11 +944,15 @@ static int av_encode(AVFormatContext **output_files,
         if (file_index < 0) {
             if (stream_no_data) {
 #ifndef CONFIG_WIN32
+#ifndef __BEOS__
                 struct timespec ts;
 
                 ts.tv_sec = 0;
                 ts.tv_nsec = 1000 * 1000 * 10;
                 nanosleep(&ts, 0);
+#else
+               snooze(10 * 1000); /* mmu_man */ /* in microsec */
+#endif
 #endif
                 stream_no_data = 0;
                 continue;
@@ -1033,14 +1071,14 @@ static int av_encode(AVFormatContext **output_files,
                     ist->ticker_inited = 1;
                     break;
                 default:
-                    abort();
+                    av_abort();
                 }
             }
             /* update pts */
             switch(ist->st->codec.codec_type) {
             case CODEC_TYPE_AUDIO:
                 //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
-                ist->pts = ticker_tick(&ist->pts_ticker, ist->sample_index);
+                ist->pts = ticker_abs(&ist->pts_ticker, ist->sample_index);
                 ist->sample_index += data_size / (2 * ist->st->codec.channels);
                 ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
                 break;
@@ -1048,12 +1086,12 @@ static int av_encode(AVFormatContext **output_files,
                 ist->frame_number++;
                 //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) / 
                 //    ist->st->codec.frame_rate;
-                ist->pts = ticker_tick(&ist->pts_ticker, ist->frame_number);
+                ist->pts = ticker_abs(&ist->pts_ticker, ist->frame_number);
                 ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) / 
                     ist->st->codec.frame_rate;
                 break;
             default:
-                abort();
+                av_abort();
             }
             ptr += ret;
             len -= ret;
@@ -1077,7 +1115,7 @@ static int av_encode(AVFormatContext **output_files,
                                 do_video_stats(ost, ist, frame_size);
                             break;
                         default:
-                            abort();
+                            av_abort();
                         }
                     } else {
                         /* no reencoding needed : output the packet directly */
@@ -1310,6 +1348,27 @@ void opt_video_bitrate_tolerance(const char *arg)
     video_bit_rate_tolerance = atoi(arg) * 1000;
 }
 
+void opt_video_bitrate_max(const char *arg)
+{
+    video_rc_max_rate = atoi(arg) * 1000;
+}
+
+void opt_video_bitrate_min(const char *arg)
+{
+    video_rc_min_rate = atoi(arg) * 1000;
+}
+
+void opt_video_rc_eq(char *arg)
+{
+    video_rc_eq = arg;
+}
+
+
+void opt_workaround_bugs(const char *arg)
+{
+    workaround_bugs = atoi(arg);
+}
+
 void opt_frame_rate(const char *arg)
 {
     frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
@@ -1566,6 +1625,7 @@ void opt_input_file(const char *filename)
             frame_height = enc->height;
             frame_width = enc->width;
             rfps = ic->streams[i]->r_frame_rate;
+            enc->workaround_bugs = workaround_bugs;
             if (enc->frame_rate != rfps) {
                 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
                     i, (float)enc->frame_rate / FRAME_RATE_BASE,
@@ -1575,7 +1635,7 @@ void opt_input_file(const char *filename)
             frame_rate = rfps;
             break;
         default:
-            abort();
+            av_abort();
         }
     }
     
@@ -1606,7 +1666,7 @@ void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
                 has_video = 1;
                 break;
             default:
-                abort();
+                av_abort();
             }
         }
     }
@@ -1724,6 +1784,14 @@ void opt_output_file(const char *filename)
             video_enc->max_qdiff = video_qdiff;
             video_enc->qblur = video_qblur;
             video_enc->qcompress = video_qcomp;
+            video_enc->rc_eq = video_rc_eq;
+            video_enc->rc_max_rate = video_rc_max_rate;
+            video_enc->rc_min_rate = video_rc_min_rate;
+            video_enc->rc_buffer_size = video_rc_buffer_size;
+            video_enc->i_quant_factor = video_i_qfactor;
+            video_enc->b_quant_factor = video_b_qfactor;
+            video_enc->i_quant_offset = video_i_qoffset;
+            video_enc->b_quant_offset = video_b_qoffset;
             
             if (do_psnr)
                 video_enc->get_psnr = 1;
@@ -1868,7 +1936,7 @@ void prepare_grab(void)
                 has_video = 1;
                 break;
             default:
-                abort();
+                av_abort();
             }
         }
     }
@@ -1929,7 +1997,7 @@ INT64 getutime(void)
 #else
 INT64 getutime(void)
 {
-  return gettime();
+  return av_gettime();
 }
 #endif
 
@@ -2067,7 +2135,10 @@ const OptionDef options[] = {
     { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
     { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
     { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
+    { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
     { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
+    { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
+    { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
     { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
     { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
     { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method", 
@@ -2075,6 +2146,7 @@ const OptionDef options[] = {
     { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
     { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
     { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
+    { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
     { "sameq", OPT_BOOL, {(void*)&same_quality}, 
       "use same video quality as source (implies VBR)" },
     /* audio options */
@@ -2103,7 +2175,7 @@ int main(int argc, char **argv)
     const OptionDef *po;
     INT64 ti;
     
-    register_all();
+    av_register_all();
 
     /* detect if invoked as player */
     i = strlen(argv[0]);