]> git.sesse.net Git - x264/blobdiff - x264.c
Backport various speed tweak ideas from ffmpeg
[x264] / x264.c
diff --git a/x264.c b/x264.c
index 4834961ebd75aa5ae56fedd7fc223ea419cd4907..959626adc327042d6f5d05135ccee62a64596adc 100644 (file)
--- a/x264.c
+++ b/x264.c
@@ -223,8 +223,8 @@ static void Help( x264_param_t *defaults, int longhelp )
     H0( "\n" );
     H0( "Presets:\n" );
     H0( "\n" );
-    H0( "      --profile               Force H.264 profile [high]\n" );
-    H0( "                                  Overrides all settings\n" );
+    H0( "      --profile               Force the limits of an H.264 profile [high]\n"
+        "                                  Overrides all settings.\n" );
     H2( "                                  - baseline:\n"
         "                                    --no-8x8dct --bframes 0 --no-cabac\n"
         "                                    --cqm flat --weightp 0 No interlaced\n"
@@ -234,8 +234,8 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                  - high:\n"
         "                                    No lossless\n" );
         else H0( "                                  - baseline,main,high\n" );
-    H0( "      --preset                Use a preset to select encoding settings [medium]\n" );
-    H0( "                                  Overridden by user settings\n" );
+    H0( "      --preset                Use a preset to select encoding settings [medium]\n"
+        "                                  Overridden by user settings.\n" );
     H2( "                                  - ultrafast:\n"
         "                                    --no-8x8dct --aq-mode 0 --b-adapt 0\n"
         "                                    --bframes 0 --no-cabac --no-deblock\n"
@@ -250,23 +250,23 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                  - faster:\n"
         "                                    --no-mbtree --no-mixed-refs --ref 2\n"
         "                                    --subme 4 --weightp 1\n"
-        "                                  - fast\n"
+        "                                  - fast:\n"
         "                                    --rc-lookahead 30 --ref 2 --subme 6\n"
-        "                                  - medium\n"
+        "                                  - medium:\n"
         "                                    Default settings apply.\n"
-        "                                  - slow\n"
+        "                                  - slow:\n"
         "                                    --b-adapt 2 --direct auto --me umh\n"
         "                                    --rc-lookahead 50 --ref 5 --subme 8\n"
-        "                                  - slower\n"
+        "                                  - slower:\n"
         "                                    --b-adapt 2 --direct auto --me umh\n"
         "                                    --partitions all --rc-lookahead 60\n"
         "                                    --ref 8 --subme 9 --trellis 2\n"
-        "                                  - veryslow\n"
+        "                                  - veryslow:\n"
         "                                    --b-adapt 2 --bframes 8 --direct auto\n"
         "                                    --me umh --merange 24 --partitions all\n"
         "                                    --ref 16 --subme 10 --trellis 2\n"
         "                                    --rc-lookahead 60\n"
-        "                                  - placebo\n"
+        "                                  - placebo:\n"
         "                                    --bframes 16 --b-adapt 2 --direct auto\n"
         "                                    --slow-firstpass --no-fast-pskip\n"
         "                                    --me tesa --merange 24 --partitions all\n"
@@ -274,37 +274,35 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                    --trellis 2\n" );
     else H0( "                                  - ultrafast,veryfast,faster,fast,medium\n"
              "                                  - slow,slower,veryslow,placebo\n" );
-    H0( "      --tune                  Tune the settings for a particular type of source\n" );
-    H0( "                                  Overridden by user settings\n" );
-    H2( "                                  - film:\n"
+    H0( "      --tune                  Tune the settings for a particular type of source\n"
+        "                              or situation\n"
+        "                                  Overridden by user settings.\n"
+        "                                  Multiple tunings are separated by commas.\n"
+        "                                  Only one psy tuning can be used at a time.\n" );
+    H2( "                                  - film (psy tuning):\n"
         "                                    --deblock -1:-1 --psy-rd <unset>:0.15\n"
-        "                                  - animation:\n"
+        "                                  - animation (psy tuning):\n"
         "                                    --bframes {+2} --deblock 1:1\n"
         "                                    --psy-rd 0.4:<unset> --aq-strength 0.6\n"
         "                                    --ref {Double if >1 else 1}\n"
-        "                                  - grain:\n"
+        "                                  - grain (psy tuning):\n"
         "                                    --aq-strength 0.5 --no-dct-decimate\n"
         "                                    --deadzone-inter 6 --deadzone-intra 6\n"
         "                                    --deblock -2:-2 --ipratio 1.1 \n"
         "                                    --pbratio 1.1 --psy-rd <unset>:0.25\n"
         "                                    --qcomp 0.8\n"
-        "                                  - psnr:\n"
+        "                                  - psnr (psy tuning):\n"
         "                                    --aq-mode 0 --no-psy\n"
-        "                                  - ssim:\n"
+        "                                  - ssim (psy tuning):\n"
         "                                    --aq-mode 2 --no-psy\n"
         "                                  - fastdecode:\n"
         "                                    --no-cabac --no-deblock --no-weightb\n"
         "                                    --weightp 0\n"
         "                                  - zerolatency:\n"
         "                                    --bframes 0 --rc-lookahead 0\n"
-        "                                    --sync-lookahead 0 --sliced-threads\n"
-        "                                  - touhou:\n"
-        "                                    --aq-strength 1.3 --deblock -1:-1\n"
-        "                                    --partitions {p4x4 if p8x8 set}\n"
-        "                                    --psy-rd <unset>:0.2\n"
-        "                                    --ref {Double if >1 else 1}\n" );
-    else H0( "                                  - film,animation,grain,psnr,ssim\n"
-             "                                  - fastdecode,zerolatency\n" );
+        "                                    --sync-lookahead 0 --sliced-threads\n" );
+    else H0( "                                  - psy tunings: film,animation,grain,psnr,ssim\n"
+             "                                  - other tunings: fastdecode,zerolatency\n" );
     H1( "      --slow-firstpass        Don't use faster settings with --pass 1\n" );
     H0( "\n" );
     H0( "Frame-type options:\n" );
@@ -680,6 +678,7 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
         output = mp4_output;
         param->b_annexb = 0;
         param->b_aud = 0;
+        param->b_dts_compress = 0;
         param->b_repeat_headers = 0;
 #else
         fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
@@ -691,6 +690,7 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
         output = mkv_output;
         param->b_annexb = 0;
         param->b_aud = 0;
+        param->b_dts_compress = 0;
         param->b_repeat_headers = 0;
     }
     else if( !strcasecmp( ext, "flv" ) )
@@ -698,6 +698,7 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
         output = flv_output;
         param->b_annexb = 0;
         param->b_aud = 0;
+        param->b_dts_compress = 1;
         param->b_repeat_headers = 0;
     }
     else
@@ -716,13 +717,11 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
     if( b_regular )
     {
         FILE *f = fopen( filename, "r" );
-        if( !f )
+        if( f )
         {
-            fprintf( stderr, "x264 [error]: could not open input file `%s'\n", filename );
-            return -1;
+            b_regular = x264_is_regular_file( f );
+            fclose( f );
         }
-        b_regular = x264_is_regular_file( f );
-        fclose( f );
     }
     const char *module = b_auto ? ext : demuxer;
 
@@ -753,7 +752,7 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
 #endif
 #ifdef LAVF_INPUT
         if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
-            (!b_regular || !lavf_input.open_file( filename, p_handle, info, opt )) )
+            !lavf_input.open_file( filename, p_handle, info, opt ) )
         {
             module = "lavf";
             b_auto = 0;
@@ -935,72 +934,95 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
 
         if( c == OPT_TUNE )
         {
-            if( !strcasecmp( optarg, "film" ) )
-            {
-                param->i_deblocking_filter_alphac0 = -1;
-                param->i_deblocking_filter_beta = -1;
-                param->analyse.f_psy_trellis = 0.15;
-            }
-            else if( !strcasecmp( optarg, "animation" ) )
-            {
-                param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
-                param->i_deblocking_filter_alphac0 = 1;
-                param->i_deblocking_filter_beta = 1;
-                param->analyse.f_psy_rd = 0.4;
-                param->rc.f_aq_strength = 0.6;
-                param->i_bframe += 2;
-            }
-            else if( !strcasecmp( optarg, "grain" ) )
-            {
-                param->i_deblocking_filter_alphac0 = -2;
-                param->i_deblocking_filter_beta = -2;
-                param->analyse.f_psy_trellis = 0.25;
-                param->analyse.b_dct_decimate = 0;
-                param->rc.f_pb_factor = 1.1;
-                param->rc.f_ip_factor = 1.1;
-                param->rc.f_aq_strength = 0.5;
-                param->analyse.i_luma_deadzone[0] = 6;
-                param->analyse.i_luma_deadzone[1] = 6;
-                param->rc.f_qcompress = 0.8;
-            }
-            else if( !strcasecmp( optarg, "psnr" ) )
-            {
-                param->rc.i_aq_mode = X264_AQ_NONE;
-                param->analyse.b_psy = 0;
-            }
-            else if( !strcasecmp( optarg, "ssim" ) )
+            char *s = strtok( optarg, ",./-+" );
+            int psy_tuning_used = 0;
+            while( s )
             {
-                param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
-                param->analyse.b_psy = 0;
-            }
-            else if( !strcasecmp( optarg, "fastdecode" ) )
-            {
-                param->b_deblocking_filter = 0;
-                param->b_cabac = 0;
-                param->analyse.b_weighted_bipred = 0;
-                param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
-            }
-            else if( !strcasecmp( optarg, "zerolatency" ) )
-            {
-                param->rc.i_lookahead = 0;
-                param->i_sync_lookahead = 0;
-                param->i_bframe = 0;
-                param->b_sliced_threads = 1;
-            }
-            else if( !strcasecmp( optarg, "touhou" ) )
-            {
-                param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
-                param->i_deblocking_filter_alphac0 = -1;
-                param->i_deblocking_filter_beta = -1;
-                param->analyse.f_psy_trellis = 0.2;
-                param->rc.f_aq_strength = 1.3;
-                if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
-                    param->analyse.inter |= X264_ANALYSE_PSUB8x8;
-            }
-            else
-            {
-                fprintf( stderr, "x264 [error]: invalid tune '%s'\n", optarg );
-                return -1;
+                if( !strncasecmp( s, "film", 4 ) )
+                {
+                    if( psy_tuning_used ) goto psy_failure;
+                    param->i_deblocking_filter_alphac0 = -1;
+                    param->i_deblocking_filter_beta = -1;
+                    param->analyse.f_psy_trellis = 0.15;
+                    psy_tuning_used = 1;
+                }
+                else if( !strncasecmp( s, "animation", 9 ) )
+                {
+                    if( psy_tuning_used ) goto psy_failure;
+                    param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
+                    param->i_deblocking_filter_alphac0 = 1;
+                    param->i_deblocking_filter_beta = 1;
+                    param->analyse.f_psy_rd = 0.4;
+                    param->rc.f_aq_strength = 0.6;
+                    param->i_bframe += 2;
+                    psy_tuning_used = 1;
+                }
+                else if( !strncasecmp( s, "grain", 5 ) )
+                {
+                    if( psy_tuning_used ) goto psy_failure;
+                    param->i_deblocking_filter_alphac0 = -2;
+                    param->i_deblocking_filter_beta = -2;
+                    param->analyse.f_psy_trellis = 0.25;
+                    param->analyse.b_dct_decimate = 0;
+                    param->rc.f_pb_factor = 1.1;
+                    param->rc.f_ip_factor = 1.1;
+                    param->rc.f_aq_strength = 0.5;
+                    param->analyse.i_luma_deadzone[0] = 6;
+                    param->analyse.i_luma_deadzone[1] = 6;
+                    param->rc.f_qcompress = 0.8;
+                    psy_tuning_used = 1;
+                }
+                else if( !strncasecmp( s, "psnr", 4 ) )
+                {
+                    if( psy_tuning_used ) goto psy_failure;
+                    param->rc.i_aq_mode = X264_AQ_NONE;
+                    param->analyse.b_psy = 0;
+                    psy_tuning_used = 1;
+                }
+                else if( !strncasecmp( s, "ssim", 4 ) )
+                {
+                    if( psy_tuning_used ) goto psy_failure;
+                    param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
+                    param->analyse.b_psy = 0;
+                    psy_tuning_used = 1;
+                }
+                else if( !strncasecmp( s, "fastdecode", 10 ) )
+                {
+                    param->b_deblocking_filter = 0;
+                    param->b_cabac = 0;
+                    param->analyse.b_weighted_bipred = 0;
+                    param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
+                }
+                else if( !strncasecmp( s, "zerolatency", 11 ) )
+                {
+                    param->rc.i_lookahead = 0;
+                    param->i_sync_lookahead = 0;
+                    param->i_bframe = 0;
+                    param->b_sliced_threads = 1;
+                }
+                else if( !strncasecmp( s, "touhou", 6 ) )
+                {
+                    if( psy_tuning_used ) goto psy_failure;
+                    param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
+                    param->i_deblocking_filter_alphac0 = -1;
+                    param->i_deblocking_filter_beta = -1;
+                    param->analyse.f_psy_trellis = 0.2;
+                    param->rc.f_aq_strength = 1.3;
+                    if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
+                        param->analyse.inter |= X264_ANALYSE_PSUB8x8;
+                    psy_tuning_used = 1;
+                }
+                else
+                {
+                    fprintf( stderr, "x264 [error]: invalid tune '%s'\n", s );
+                    return -1;
+                }
+                if( 0 )
+                {
+psy_failure:
+                    fprintf( stderr, "x264 [warning]: only 1 psy tuning can be used: ignoring tune %s\n", s );
+                }
+                s = strtok( NULL, ",./-+" );
             }
         }
         else if( c == '?' )
@@ -1127,6 +1149,7 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
                 goto generic_option;
             case OPT_FPS:
                 b_user_fps = 1;
+                param->b_vfr_input = 0;
                 goto generic_option;
             case OPT_INTERLACED:
                 b_user_interlaced = 1;
@@ -1430,6 +1453,8 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     int64_t second_largest_pts = -1;
     int64_t ticks_per_frame;
     double  duration;
+    int     prev_timebase_den = param->i_timebase_den / gcd( param->i_timebase_num, param->i_timebase_den );
+    int     dts_compress_multiplier;
 
     opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
     i_frame_total = input.get_frame_total( opt->hin );
@@ -1447,6 +1472,10 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
         return -1;
     }
 
+    x264_encoder_parameters( h, param );
+
+    dts_compress_multiplier = param->i_timebase_den / prev_timebase_den;
+
     if( output.set_param( opt->hout, param ) )
     {
         fprintf( stderr, "x264 [error]: can't set outfile param\n" );
@@ -1464,14 +1493,14 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
 
     i_start = x264_mdate();
     /* ticks/frame = ticks/second / frames/second */
-    ticks_per_frame = (int64_t)h->param.i_timebase_den * h->param.i_fps_den / h->param.i_timebase_num / h->param.i_fps_num;
+    ticks_per_frame = (int64_t)param->i_timebase_den * param->i_fps_den / param->i_timebase_num / param->i_fps_num;
     if( ticks_per_frame < 1 )
     {
         fprintf( stderr, "x264 [error]: ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame );
         return -1;
     }
 
-    if( !h->param.b_repeat_headers )
+    if( !param->b_repeat_headers )
     {
         // Write SPS/PPS/SEI
         x264_nal_t *headers;
@@ -1497,11 +1526,11 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
             pic.i_pts = i_frame;
         if( pic.i_pts <= largest_pts )
         {
-            if( h->param.i_log_level >= X264_LOG_WARNING )
+            if( param->i_log_level >= X264_LOG_WARNING )
             {
-                if( h->param.i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
+                if( param->i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
                     fprintf( stderr, "x264 [warning]: non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
-                             i_frame, pic.i_pts, largest_pts );
+                             i_frame, pic.i_pts * dts_compress_multiplier, largest_pts * dts_compress_multiplier );
                 else if( pts_warning_cnt == MAX_PTS_WARNING )
                     fprintf( stderr, "x264 [warning]: too many nonmonotonic pts warnings, suppressing further ones\n" );
                 pts_warning_cnt++;
@@ -1548,7 +1577,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
         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 );
     }
-    if( pts_warning_cnt >= MAX_PTS_WARNING && h->param.i_log_level < X264_LOG_DEBUG )
+    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 );
 
     /* duration algorithm fails when only 1 frame is output */
@@ -1556,6 +1585,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
         duration = (double)param->i_fps_den / param->i_fps_num;
     else
         duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
+    duration *= dts_compress_multiplier;
 
     i_end = x264_mdate();
     input.picture_clean( &pic );