]> git.sesse.net Git - x264/blobdiff - x264.c
Overhaul CABAC: faster, less cache usage
[x264] / x264.c
diff --git a/x264.c b/x264.c
index 22fd2ce339f5570d88f0502e4d75b10e739cc375..862aabb264411629c3e533d9e5ed6449b976001d 100644 (file)
--- a/x264.c
+++ b/x264.c
@@ -120,7 +120,7 @@ enum pulldown_type_e
 
 static const cli_pulldown_t pulldown_values[] =
 {
-    [X264_PULLDOWN_22]     = {1,  {TB},                                   2.0},
+    [X264_PULLDOWN_22]     = {1,  {TB},                                   1.0},
     [X264_PULLDOWN_32]     = {4,  {TBT, BT, BTB, TB},                     1.25},
     [X264_PULLDOWN_64]     = {2,  {PIC_STRUCT_DOUBLE, PIC_STRUCT_TRIPLE}, 1.0},
     [X264_PULLDOWN_DOUBLE] = {1,  {PIC_STRUCT_DOUBLE},                    2.0},
@@ -288,13 +288,16 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                    --partitions none --ref 1 --scenecut 0\n"
         "                                    --subme 0 --trellis 0 --no-weightb\n"
         "                                    --weightp 0\n"
-        "                                  - veryfast:\n"
+        "                                  - superfast:\n"
         "                                    --no-mbtree --me dia --no-mixed-refs\n"
         "                                    --partitions i8x8,i4x4 --ref 1\n"
         "                                    --subme 1 --trellis 0 --weightp 0\n"
+        "                                  - veryfast:\n"
+        "                                    --no-mbtree --no-mixed-refs --ref 1\n"
+        "                                    --subme 2 --trellis 0 --weightp 0\n"
         "                                  - faster:\n"
-        "                                    --no-mbtree --no-mixed-refs --ref 2\n"
-        "                                    --subme 4 --weightp 1\n"
+        "                                    --no-mixed-refs --rc-lookahead 20\n"
+        "                                    --ref 2 --subme 4 --weightp 1\n"
         "                                  - fast:\n"
         "                                    --rc-lookahead 30 --ref 2 --subme 6\n"
         "                                  - medium:\n"
@@ -317,8 +320,8 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                    --me tesa --merange 24 --partitions all\n"
         "                                    --rc-lookahead 60 --ref 16 --subme 10\n"
         "                                    --trellis 2\n" );
-    else H0( "                                  - ultrafast,veryfast,faster,fast,medium\n"
-             "                                  - slow,slower,veryslow,placebo\n" );
+    else H0( "                                  - ultrafast,superfast,veryfast,faster,fast\n"
+             "                                  - medium,slow,slower,veryslow,placebo\n" );
     H0( "      --tune                  Tune the settings for a particular type of source\n"
         "                              or situation\n"
         "                                  Overridden by user settings.\n"
@@ -353,14 +356,15 @@ static void Help( x264_param_t *defaults, int longhelp )
              "                                                 stillimage,psnr,ssim\n"
              "                                  - other tunings: fastdecode,zerolatency\n" );
     H2( "      --slow-firstpass        Don't force these faster settings with --pass 1:\n"
-        "                                  --no-8x8dct --me dia --partitions none --ref 1\n"
-        "                                  --subme {2 if >2 else unchanged} --trellis 0\n" );
+        "                                  --no-8x8dct --me dia --partitions none\n"
+        "                                  --ref 1 --subme {2 if >2 else unchanged}\n"
+        "                                  --trellis 0 --fast-pskip\n" );
     else H1( "      --slow-firstpass        Don't force faster settings with --pass 1\n" );
     H0( "\n" );
     H0( "Frame-type options:\n" );
     H0( "\n" );
     H0( "  -I, --keyint <integer>      Maximum GOP size [%d]\n", defaults->i_keyint_max );
-    H2( "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n", defaults->i_keyint_min );
+    H2( "  -i, --min-keyint <integer>  Minimum GOP size [auto]\n" );
     H2( "      --no-scenecut           Disable adaptive I-frame decision\n" );
     H2( "      --scenecut <integer>    How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
     H2( "      --intra-refresh         Use Periodic Intra Refresh instead of IDR frames\n" );
@@ -868,6 +872,28 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
     return 0;
 }
 
+static int parse_enum_name( const char *arg, const char * const *names, const char **dst )
+{
+    for( int i = 0; names[i]; i++ )
+        if( !strcasecmp( arg, names[i] ) )
+        {
+            *dst = names[i];
+            return 0;
+        }
+    return -1;
+}
+
+static int parse_enum_value( const char *arg, const char * const *names, int *dst )
+{
+    for( int i = 0; names[i]; i++ )
+        if( !strcasecmp( arg, names[i] ) )
+        {
+            *dst = i;
+            return 0;
+        }
+    return -1;
+}
+
 /*****************************************************************************
  * Parse:
  *****************************************************************************/
@@ -885,7 +911,6 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
     int b_user_ref = 0;
     int b_user_fps = 0;
     int b_user_interlaced = 0;
-    int i;
     cli_input_opt_t input_opt;
     char *preset = NULL;
     char *tune = NULL;
@@ -963,24 +988,12 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
                 output_filename = optarg;
                 break;
             case OPT_MUXER:
-                for( i = 0; muxer_names[i] && strcasecmp( muxer_names[i], optarg ); )
-                    i++;
-                if( !muxer_names[i] )
-                {
-                    fprintf( stderr, "x264 [error]: invalid muxer '%s'\n", optarg );
+                if( parse_enum_name( optarg, muxer_names, &muxer ) < 0 )
                     return -1;
-                }
-                muxer = optarg;
                 break;
             case OPT_DEMUXER:
-                for( i = 0; demuxer_names[i] && strcasecmp( demuxer_names[i], optarg ); )
-                    i++;
-                if( !demuxer_names[i] )
-                {
-                    fprintf( stderr, "x264 [error]: invalid demuxer '%s'\n", optarg );
+                if( parse_enum_name( optarg, demuxer_names, &demuxer ) < 0 )
                     return -1;
-                }
-                demuxer = optarg;
                 break;
             case OPT_INDEX:
                 input_opt.index = optarg;
@@ -1012,7 +1025,7 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
                 opt->b_progress = 0;
                 break;
             case OPT_VISUALIZE:
-#ifdef VISUALIZE
+#ifdef HAVE_VISUALIZE
                 param->b_visualize = 1;
                 b_exit_on_ctrl_c = 1;
 #else
@@ -1053,22 +1066,15 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
                 input_opt.timebase = optarg;
                 break;
             case OPT_PULLDOWN:
-                for( i = 0; pulldown_names[i] && strcasecmp( pulldown_names[i], optarg ); )
-                    i++;
-                if( !pulldown_names[i] )
-                {
-                    fprintf( stderr, "x264 [error]: invalid pulldown '%s'\n", optarg );
+                if( parse_enum_value( optarg, pulldown_names, &opt->i_pulldown ) < 0 )
                     return -1;
-                }
-                opt->i_pulldown = i;
                 break;
             default:
 generic_option:
             {
-                int i;
                 if( long_options_index < 0 )
                 {
-                    for( i = 0; long_options[i].name; i++ )
+                    for( int i = 0; long_options[i].name; i++ )
                         if( long_options[i].val == c )
                         {
                             long_options_index = i;
@@ -1129,6 +1135,7 @@ generic_option:
     info.interlaced = param->b_interlaced;
     info.sar_width  = param->vui.i_sar_width;
     info.sar_height = param->vui.i_sar_height;
+    info.tff        = param->b_tff;
     info.vfr        = param->b_vfr_input;
 
     if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
@@ -1175,9 +1182,11 @@ generic_option:
     param->i_width     = info.width;
     if( !b_user_interlaced && info.interlaced )
     {
-        fprintf( stderr, "x264 [warning]: input appears to be interlaced, enabling interlaced mode.\n"
-                         "                If you want otherwise, use --no-interlaced\n" );
+        fprintf( stderr, "x264 [warning]: input appears to be interlaced, enabling %cff interlaced mode.\n"
+                         "                If you want otherwise, use --no-interlaced or --%cff\n",
+                 info.tff ? 't' : 'b', info.tff ? 'b' : 't' );
         param->b_interlaced = 1;
+        param->b_tff = !!info.tff;
     }
     if( !b_user_fps )
     {
@@ -1196,9 +1205,9 @@ generic_option:
     }
     if( !tcfile_name && input_opt.timebase )
     {
-        int i_user_timebase_num;
-        int i_user_timebase_den;
-        int ret = sscanf( input_opt.timebase, "%d/%d", &i_user_timebase_num, &i_user_timebase_den );
+        uint64_t i_user_timebase_num;
+        uint64_t i_user_timebase_den;
+        int ret = sscanf( input_opt.timebase, "%"SCNu64"/%"SCNu64, &i_user_timebase_num, &i_user_timebase_den );
         if( !ret )
         {
             fprintf( stderr, "x264 [error]: invalid argument: timebase = %s\n", input_opt.timebase );
@@ -1207,16 +1216,15 @@ generic_option:
         else if( ret == 1 )
         {
             i_user_timebase_num = param->i_timebase_num;
-            i_user_timebase_den = atoi( input_opt.timebase );
+            i_user_timebase_den = strtoul( input_opt.timebase, NULL, 10 );
         }
-        opt->timebase_convert_multiplier = ((double)i_user_timebase_den / param->i_timebase_den)
-                                         * ((double)param->i_timebase_num / i_user_timebase_num);
-        if( opt->timebase_convert_multiplier < 1 )
+        if( i_user_timebase_num > UINT32_MAX || i_user_timebase_den > UINT32_MAX )
         {
-            fprintf( stderr, "x264 [error]: timebase you specified will generate nonmonotonic pts: %d/%d\n",
-                     i_user_timebase_num, i_user_timebase_den );
+            fprintf( stderr, "x264 [error]: timebase you specified exceeds H.264 maximum\n" );
             return -1;
         }
+        opt->timebase_convert_multiplier = ((double)i_user_timebase_den / param->i_timebase_den)
+                                         * ((double)param->i_timebase_num / i_user_timebase_num);
         param->i_timebase_num = i_user_timebase_num;
         param->i_timebase_den = i_user_timebase_den;
         param->b_vfr_input = 1;
@@ -1247,12 +1255,11 @@ generic_option:
     if( !b_user_ref )
     {
         int mbs = (((param->i_width)+15)>>4) * (((param->i_height)+15)>>4);
-        int i;
-        for( i = 0; x264_levels[i].level_idc != 0; i++ )
+        for( int i = 0; x264_levels[i].level_idc != 0; i++ )
             if( param->i_level_idc == x264_levels[i].level_idc )
             {
-                while( mbs * 384 * param->i_frame_reference > x264_levels[i].dpb
-                       && param->i_frame_reference > 1 )
+                while( mbs * 384 * param->i_frame_reference > x264_levels[i].dpb &&
+                       param->i_frame_reference > 1 )
                 {
                     param->i_frame_reference--;
                 }
@@ -1305,7 +1312,7 @@ static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
  * Encode:
  *****************************************************************************/
 
-static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_pts )
+static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_dts )
 {
     x264_picture_t pic_out;
     x264_nal_t *nal;
@@ -1323,18 +1330,22 @@ static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *l
     if( i_frame_size )
     {
         i_frame_size = output.write_frame( hout, nal[0].p_payload, i_frame_size, &pic_out );
-        *last_pts = pic_out.i_pts;
+        *last_dts = pic_out.i_dts;
     }
 
     return i_frame_size;
 }
 
-static void Print_status( int64_t i_start, int i_frame, int i_frame_total, int64_t i_file, x264_param_t *param, int64_t last_pts )
+static void Print_status( int64_t i_start, int i_frame, int i_frame_total, int64_t i_file, x264_param_t *param, int64_t last_ts )
 {
     char    buf[200];
     int64_t i_elapsed = x264_mdate() - i_start;
     double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
-    double bitrate = (double) i_file * 8 / ( (double) last_pts * 1000 * param->i_timebase_num / param->i_timebase_den );
+    double bitrate;
+    if( last_ts )
+        bitrate = (double) i_file * 8 / ( (double) last_ts * 1000 * param->i_timebase_num / param->i_timebase_den );
+    else
+        bitrate = (double) i_file * 8 / ( (double) 1000 * param->i_fps_den / param->i_fps_num );
     if( i_frame_total )
     {
         int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
@@ -1362,7 +1373,9 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     int64_t i_file = 0;
     int     i_frame_size;
     int     i_update_interval;
-    int64_t last_pts = 0;
+    int64_t last_dts = 0;
+    int64_t prev_dts = 0;
+    int64_t first_dts = 0;
 #   define  MAX_PTS_WARNING 3 /* arbitrary */
     int     pts_warning_cnt = 0;
     int64_t largest_pts = -1;
@@ -1499,12 +1512,17 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
             pic.i_qpplus1 = 0;
         }
 
-        i_frame_size = Encode_frame( h, opt->hout, &pic, &last_pts );
+        prev_dts = last_dts;
+        i_frame_size = Encode_frame( h, opt->hout, &pic, &last_dts );
         if( i_frame_size < 0 )
             return -1;
         i_file += i_frame_size;
         if( i_frame_size )
+        {
             i_frame_output++;
+            if( i_frame_output == 1 )
+                first_dts = prev_dts = last_dts;
+        }
 
         i_frame++;
 
@@ -1513,19 +1531,24 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
 
         /* update status line (up to 1000 times per input file) */
         if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
-            Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
+            Print_status( i_start, i_frame_output, i_frame_total, i_file, param, 2 * last_dts - prev_dts - first_dts );
     }
     /* Flush delayed frames */
     while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
     {
-        i_frame_size = Encode_frame( h, opt->hout, NULL, &last_pts );
+        prev_dts = last_dts;
+        i_frame_size = Encode_frame( h, opt->hout, NULL, &last_dts );
         if( i_frame_size < 0 )
             return -1;
         i_file += i_frame_size;
         if( i_frame_size )
+        {
             i_frame_output++;
+            if( i_frame_output == 1 )
+                first_dts = prev_dts = last_dts;
+        }
         if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
-            Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
+            Print_status( i_start, i_frame_output, i_frame_total, i_file, param, 2 * last_dts - prev_dts - first_dts );
     }
     if( pts_warning_cnt >= MAX_PTS_WARNING && param->i_log_level < X264_LOG_DEBUG )
         fprintf( stderr, "x264 [warning]: %d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );