1 /*****************************************************************************
2 * common.c: misc common functions
3 *****************************************************************************
4 * Copyright (C) 2003-2016 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
38 const int x264_bit_depth = BIT_DEPTH;
40 const int x264_chroma_format = X264_CHROMA_FORMAT;
42 static void x264_log_default( void *, int, const char *, va_list );
44 /****************************************************************************
46 ****************************************************************************/
47 void x264_param_default( x264_param_t *param )
50 memset( param, 0, sizeof( x264_param_t ) );
53 param->cpu = x264_cpu_detect();
54 param->i_threads = X264_THREADS_AUTO;
55 param->i_lookahead_threads = X264_THREADS_AUTO;
56 param->b_deterministic = 1;
57 param->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
59 /* Video properties */
60 param->i_csp = X264_CHROMA_FORMAT ? X264_CHROMA_FORMAT : X264_CSP_I420;
63 param->vui.i_sar_width = 0;
64 param->vui.i_sar_height= 0;
65 param->vui.i_overscan = 0; /* undef */
66 param->vui.i_vidformat = 5; /* undef */
67 param->vui.b_fullrange = -1; /* default depends on input */
68 param->vui.i_colorprim = 2; /* undef */
69 param->vui.i_transfer = 2; /* undef */
70 param->vui.i_colmatrix = -1; /* default depends on input */
71 param->vui.i_chroma_loc= 0; /* left center */
72 param->i_fps_num = 25;
74 param->i_level_idc = -1;
75 param->i_slice_max_size = 0;
76 param->i_slice_max_mbs = 0;
77 param->i_slice_count = 0;
79 /* Encoder parameters */
80 param->i_frame_reference = 3;
81 param->i_keyint_max = 250;
82 param->i_keyint_min = X264_KEYINT_MIN_AUTO;
84 param->i_scenecut_threshold = 40;
85 param->i_bframe_adaptive = X264_B_ADAPT_FAST;
86 param->i_bframe_bias = 0;
87 param->i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
88 param->b_interlaced = 0;
89 param->b_constrained_intra = 0;
91 param->b_deblocking_filter = 1;
92 param->i_deblocking_filter_alphac0 = 0;
93 param->i_deblocking_filter_beta = 0;
96 param->i_cabac_init_idc = 0;
98 param->rc.i_rc_method = X264_RC_CRF;
99 param->rc.i_bitrate = 0;
100 param->rc.f_rate_tolerance = 1.0;
101 param->rc.i_vbv_max_bitrate = 0;
102 param->rc.i_vbv_buffer_size = 0;
103 param->rc.f_vbv_buffer_init = 0.9;
104 param->rc.i_qp_constant = 23 + QP_BD_OFFSET;
105 param->rc.f_rf_constant = 23;
106 param->rc.i_qp_min = 0;
107 param->rc.i_qp_max = QP_MAX;
108 param->rc.i_qp_step = 4;
109 param->rc.f_ip_factor = 1.4;
110 param->rc.f_pb_factor = 1.3;
111 param->rc.i_aq_mode = X264_AQ_VARIANCE;
112 param->rc.f_aq_strength = 1.0;
113 param->rc.i_lookahead = 40;
115 param->rc.b_stat_write = 0;
116 param->rc.psz_stat_out = "x264_2pass.log";
117 param->rc.b_stat_read = 0;
118 param->rc.psz_stat_in = "x264_2pass.log";
119 param->rc.f_qcompress = 0.6;
120 param->rc.f_qblur = 0.5;
121 param->rc.f_complexity_blur = 20;
122 param->rc.i_zones = 0;
123 param->rc.b_mb_tree = 1;
126 param->pf_log = x264_log_default;
127 param->p_log_private = NULL;
128 param->i_log_level = X264_LOG_INFO;
131 param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
132 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
133 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
134 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
135 param->analyse.i_me_method = X264_ME_HEX;
136 param->analyse.f_psy_rd = 1.0;
137 param->analyse.b_psy = 1;
138 param->analyse.f_psy_trellis = 0;
139 param->analyse.i_me_range = 16;
140 param->analyse.i_subpel_refine = 7;
141 param->analyse.b_mixed_references = 1;
142 param->analyse.b_chroma_me = 1;
143 param->analyse.i_mv_range_thread = -1;
144 param->analyse.i_mv_range = -1; // set from level_idc
145 param->analyse.i_chroma_qp_offset = 0;
146 param->analyse.b_fast_pskip = 1;
147 param->analyse.b_weighted_bipred = 1;
148 param->analyse.i_weighted_pred = X264_WEIGHTP_SMART;
149 param->analyse.b_dct_decimate = 1;
150 param->analyse.b_transform_8x8 = 1;
151 param->analyse.i_trellis = 1;
152 param->analyse.i_luma_deadzone[0] = 21;
153 param->analyse.i_luma_deadzone[1] = 11;
154 param->analyse.b_psnr = 0;
155 param->analyse.b_ssim = 0;
157 param->i_cqm_preset = X264_CQM_FLAT;
158 memset( param->cqm_4iy, 16, sizeof( param->cqm_4iy ) );
159 memset( param->cqm_4py, 16, sizeof( param->cqm_4py ) );
160 memset( param->cqm_4ic, 16, sizeof( param->cqm_4ic ) );
161 memset( param->cqm_4pc, 16, sizeof( param->cqm_4pc ) );
162 memset( param->cqm_8iy, 16, sizeof( param->cqm_8iy ) );
163 memset( param->cqm_8py, 16, sizeof( param->cqm_8py ) );
164 memset( param->cqm_8ic, 16, sizeof( param->cqm_8ic ) );
165 memset( param->cqm_8pc, 16, sizeof( param->cqm_8pc ) );
167 param->b_repeat_headers = 1;
170 param->b_vfr_input = 1;
171 param->i_nal_hrd = X264_NAL_HRD_NONE;
173 param->b_pic_struct = 0;
174 param->b_fake_interlaced = 0;
175 param->i_frame_packing = -1;
177 param->i_opencl_device = 0;
178 param->opencl_device_id = NULL;
179 param->psz_clbin_file = NULL;
182 static int x264_param_apply_preset( x264_param_t *param, const char *preset )
185 int i = strtol( preset, &end, 10 );
186 if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
187 preset = x264_preset_names[i];
189 if( !strcasecmp( preset, "ultrafast" ) )
191 param->i_frame_reference = 1;
192 param->i_scenecut_threshold = 0;
193 param->b_deblocking_filter = 0;
196 param->analyse.intra = 0;
197 param->analyse.inter = 0;
198 param->analyse.b_transform_8x8 = 0;
199 param->analyse.i_me_method = X264_ME_DIA;
200 param->analyse.i_subpel_refine = 0;
201 param->rc.i_aq_mode = 0;
202 param->analyse.b_mixed_references = 0;
203 param->analyse.i_trellis = 0;
204 param->i_bframe_adaptive = X264_B_ADAPT_NONE;
205 param->rc.b_mb_tree = 0;
206 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
207 param->analyse.b_weighted_bipred = 0;
208 param->rc.i_lookahead = 0;
210 else if( !strcasecmp( preset, "superfast" ) )
212 param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
213 param->analyse.i_me_method = X264_ME_DIA;
214 param->analyse.i_subpel_refine = 1;
215 param->i_frame_reference = 1;
216 param->analyse.b_mixed_references = 0;
217 param->analyse.i_trellis = 0;
218 param->rc.b_mb_tree = 0;
219 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
220 param->rc.i_lookahead = 0;
222 else if( !strcasecmp( preset, "veryfast" ) )
224 param->analyse.i_me_method = X264_ME_HEX;
225 param->analyse.i_subpel_refine = 2;
226 param->i_frame_reference = 1;
227 param->analyse.b_mixed_references = 0;
228 param->analyse.i_trellis = 0;
229 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
230 param->rc.i_lookahead = 10;
232 else if( !strcasecmp( preset, "faster" ) )
234 param->analyse.b_mixed_references = 0;
235 param->i_frame_reference = 2;
236 param->analyse.i_subpel_refine = 4;
237 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
238 param->rc.i_lookahead = 20;
240 else if( !strcasecmp( preset, "fast" ) )
242 param->i_frame_reference = 2;
243 param->analyse.i_subpel_refine = 6;
244 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
245 param->rc.i_lookahead = 30;
247 else if( !strcasecmp( preset, "medium" ) )
249 /* Default is medium */
251 else if( !strcasecmp( preset, "slow" ) )
253 param->analyse.i_me_method = X264_ME_UMH;
254 param->analyse.i_subpel_refine = 8;
255 param->i_frame_reference = 5;
256 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
257 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
258 param->rc.i_lookahead = 50;
260 else if( !strcasecmp( preset, "slower" ) )
262 param->analyse.i_me_method = X264_ME_UMH;
263 param->analyse.i_subpel_refine = 9;
264 param->i_frame_reference = 8;
265 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
266 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
267 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
268 param->analyse.i_trellis = 2;
269 param->rc.i_lookahead = 60;
271 else if( !strcasecmp( preset, "veryslow" ) )
273 param->analyse.i_me_method = X264_ME_UMH;
274 param->analyse.i_subpel_refine = 10;
275 param->analyse.i_me_range = 24;
276 param->i_frame_reference = 16;
277 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
278 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
279 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
280 param->analyse.i_trellis = 2;
282 param->rc.i_lookahead = 60;
284 else if( !strcasecmp( preset, "placebo" ) )
286 param->analyse.i_me_method = X264_ME_TESA;
287 param->analyse.i_subpel_refine = 11;
288 param->analyse.i_me_range = 24;
289 param->i_frame_reference = 16;
290 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
291 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
292 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
293 param->analyse.b_fast_pskip = 0;
294 param->analyse.i_trellis = 2;
295 param->i_bframe = 16;
296 param->rc.i_lookahead = 60;
300 x264_log( NULL, X264_LOG_ERROR, "invalid preset '%s'\n", preset );
306 static int x264_param_apply_tune( x264_param_t *param, const char *tune )
308 char *tmp = x264_malloc( strlen( tune ) + 1 );
311 tmp = strcpy( tmp, tune );
312 char *s = strtok( tmp, ",./-+" );
313 int psy_tuning_used = 0;
316 if( !strncasecmp( s, "film", 4 ) )
318 if( psy_tuning_used++ ) goto psy_failure;
319 param->i_deblocking_filter_alphac0 = -1;
320 param->i_deblocking_filter_beta = -1;
321 param->analyse.f_psy_trellis = 0.15;
323 else if( !strncasecmp( s, "animation", 9 ) )
325 if( psy_tuning_used++ ) goto psy_failure;
326 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
327 param->i_deblocking_filter_alphac0 = 1;
328 param->i_deblocking_filter_beta = 1;
329 param->analyse.f_psy_rd = 0.4;
330 param->rc.f_aq_strength = 0.6;
331 param->i_bframe += 2;
333 else if( !strncasecmp( s, "grain", 5 ) )
335 if( psy_tuning_used++ ) goto psy_failure;
336 param->i_deblocking_filter_alphac0 = -2;
337 param->i_deblocking_filter_beta = -2;
338 param->analyse.f_psy_trellis = 0.25;
339 param->analyse.b_dct_decimate = 0;
340 param->rc.f_pb_factor = 1.1;
341 param->rc.f_ip_factor = 1.1;
342 param->rc.f_aq_strength = 0.5;
343 param->analyse.i_luma_deadzone[0] = 6;
344 param->analyse.i_luma_deadzone[1] = 6;
345 param->rc.f_qcompress = 0.8;
347 else if( !strncasecmp( s, "stillimage", 10 ) )
349 if( psy_tuning_used++ ) goto psy_failure;
350 param->i_deblocking_filter_alphac0 = -3;
351 param->i_deblocking_filter_beta = -3;
352 param->analyse.f_psy_rd = 2.0;
353 param->analyse.f_psy_trellis = 0.7;
354 param->rc.f_aq_strength = 1.2;
356 else if( !strncasecmp( s, "psnr", 4 ) )
358 if( psy_tuning_used++ ) goto psy_failure;
359 param->rc.i_aq_mode = X264_AQ_NONE;
360 param->analyse.b_psy = 0;
362 else if( !strncasecmp( s, "ssim", 4 ) )
364 if( psy_tuning_used++ ) goto psy_failure;
365 param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
366 param->analyse.b_psy = 0;
368 else if( !strncasecmp( s, "fastdecode", 10 ) )
370 param->b_deblocking_filter = 0;
372 param->analyse.b_weighted_bipred = 0;
373 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
375 else if( !strncasecmp( s, "zerolatency", 11 ) )
377 param->rc.i_lookahead = 0;
378 param->i_sync_lookahead = 0;
380 param->b_sliced_threads = 1;
381 param->b_vfr_input = 0;
382 param->rc.b_mb_tree = 0;
384 else if( !strncasecmp( s, "touhou", 6 ) )
386 if( psy_tuning_used++ ) goto psy_failure;
387 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
388 param->i_deblocking_filter_alphac0 = -1;
389 param->i_deblocking_filter_beta = -1;
390 param->analyse.f_psy_trellis = 0.2;
391 param->rc.f_aq_strength = 1.3;
392 if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
393 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
397 x264_log( NULL, X264_LOG_ERROR, "invalid tune '%s'\n", s );
404 x264_log( NULL, X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %s\n", s );
406 s = strtok( NULL, ",./-+" );
412 int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
414 x264_param_default( param );
416 if( preset && x264_param_apply_preset( param, preset ) < 0 )
418 if( tune && x264_param_apply_tune( param, tune ) < 0 )
423 void x264_param_apply_fastfirstpass( x264_param_t *param )
425 /* Set faster options in case of turbo firstpass. */
426 if( param->rc.b_stat_write && !param->rc.b_stat_read )
428 param->i_frame_reference = 1;
429 param->analyse.b_transform_8x8 = 0;
430 param->analyse.inter = 0;
431 param->analyse.i_me_method = X264_ME_DIA;
432 param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
433 param->analyse.i_trellis = 0;
434 param->analyse.b_fast_pskip = 1;
438 static int profile_string_to_int( const char *str )
440 if( !strcasecmp( str, "baseline" ) )
441 return PROFILE_BASELINE;
442 if( !strcasecmp( str, "main" ) )
444 if( !strcasecmp( str, "high" ) )
446 if( !strcasecmp( str, "high10" ) )
447 return PROFILE_HIGH10;
448 if( !strcasecmp( str, "high422" ) )
449 return PROFILE_HIGH422;
450 if( !strcasecmp( str, "high444" ) )
451 return PROFILE_HIGH444_PREDICTIVE;
455 int x264_param_apply_profile( x264_param_t *param, const char *profile )
460 int p = profile_string_to_int( profile );
463 x264_log( NULL, X264_LOG_ERROR, "invalid profile: %s\n", profile );
466 if( p < PROFILE_HIGH444_PREDICTIVE && ((param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
467 (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0)) )
469 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
472 if( p < PROFILE_HIGH444_PREDICTIVE && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I444 )
474 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:4:4\n", profile );
477 if( p < PROFILE_HIGH422 && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I422 )
479 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:2:2\n", profile );
482 if( p < PROFILE_HIGH10 && BIT_DEPTH > 8 )
484 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d\n", profile, BIT_DEPTH );
488 if( p == PROFILE_BASELINE )
490 param->analyse.b_transform_8x8 = 0;
492 param->i_cqm_preset = X264_CQM_FLAT;
493 param->psz_cqm_file = NULL;
495 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
496 if( param->b_interlaced )
498 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
501 if( param->b_fake_interlaced )
503 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
507 else if( p == PROFILE_MAIN )
509 param->analyse.b_transform_8x8 = 0;
510 param->i_cqm_preset = X264_CQM_FLAT;
511 param->psz_cqm_file = NULL;
516 static int parse_enum( const char *arg, const char * const *names, int *dst )
518 for( int i = 0; names[i]; i++ )
519 if( !strcasecmp( arg, names[i] ) )
527 static int parse_cqm( const char *str, uint8_t *cqm, int length )
532 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
535 } while( i < length && (str = strchr( str, ',' )) && str++ );
536 return (i == length) ? 0 : -1;
539 static int x264_atobool( const char *str, int *b_error )
541 if( !strcmp(str, "1") ||
542 !strcasecmp(str, "true") ||
543 !strcasecmp(str, "yes") )
545 if( !strcmp(str, "0") ||
546 !strcasecmp(str, "false") ||
547 !strcasecmp(str, "no") )
553 static int x264_atoi( const char *str, int *b_error )
556 int v = strtol( str, &end, 0 );
557 if( end == str || *end != '\0' )
562 static double x264_atof( const char *str, int *b_error )
565 double v = strtod( str, &end );
566 if( end == str || *end != '\0' )
571 #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
574 #define atoi(str) x264_atoi( str, &b_error )
575 #define atof(str) x264_atof( str, &b_error )
577 int x264_param_parse( x264_param_t *p, const char *name, const char *value )
579 char *name_buf = NULL;
581 int errortype = X264_PARAM_BAD_VALUE;
583 int value_was_null = !value;
586 return X264_PARAM_BAD_NAME;
590 if( value[0] == '=' )
593 if( strchr( name, '_' ) ) // s/_/-/g
596 name_buf = strdup(name);
598 return X264_PARAM_BAD_NAME;
599 while( (c = strchr( name_buf, '_' )) )
604 if( !strncmp( name, "no", 2 ) )
609 value = atobool(value) ? "false" : "true";
613 #define OPT(STR) else if( !strcmp( name, STR ) )
614 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
618 p->cpu = isdigit(value[0]) ? atoi(value) :
619 !strcasecmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
622 char *buf = strdup( value );
625 char *tok, UNUSED *saveptr=NULL, *init;
628 for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
631 while( x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name) )
633 p->cpu |= x264_cpu_names[i].flags;
634 if( !x264_cpu_names[i].flags )
638 if( (p->cpu&X264_CPU_SSSE3) && !(p->cpu&X264_CPU_SSE2_IS_SLOW) )
639 p->cpu |= X264_CPU_SSE2_IS_FAST;
645 if( !strcasecmp(value, "auto") )
646 p->i_threads = X264_THREADS_AUTO;
648 p->i_threads = atoi(value);
650 OPT("lookahead-threads")
652 if( !strcasecmp(value, "auto") )
653 p->i_lookahead_threads = X264_THREADS_AUTO;
655 p->i_lookahead_threads = atoi(value);
657 OPT("sliced-threads")
658 p->b_sliced_threads = atobool(value);
659 OPT("sync-lookahead")
661 if( !strcasecmp(value, "auto") )
662 p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
664 p->i_sync_lookahead = atoi(value);
666 OPT2("deterministic", "n-deterministic")
667 p->b_deterministic = atobool(value);
668 OPT("cpu-independent")
669 p->b_cpu_independent = atobool(value);
670 OPT2("level", "level-idc")
672 if( !strcmp(value, "1b") )
674 else if( atof(value) < 6 )
675 p->i_level_idc = (int)(10*atof(value)+.5);
677 p->i_level_idc = atoi(value);
680 p->b_bluray_compat = atobool(value);
681 OPT("avcintra-class")
682 p->i_avcintra_class = atoi(value);
685 b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
686 2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
689 b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
691 b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
693 b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
695 b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
697 b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
699 b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
702 p->vui.i_chroma_loc = atoi(value);
703 b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
707 if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) != 2 )
709 double fps = atof(value);
710 if( fps > 0.0 && fps <= INT_MAX/1000.0 )
712 p->i_fps_num = (int)(fps * 1000.0 + .5);
717 p->i_fps_num = atoi(value);
722 OPT2("ref", "frameref")
723 p->i_frame_reference = atoi(value);
725 p->i_dpb_size = atoi(value);
728 if( strstr( value, "infinite" ) )
729 p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
731 p->i_keyint_max = atoi(value);
733 OPT2("min-keyint", "keyint-min")
735 p->i_keyint_min = atoi(value);
736 if( p->i_keyint_max < p->i_keyint_min )
737 p->i_keyint_max = p->i_keyint_min;
741 p->i_scenecut_threshold = atobool(value);
742 if( b_error || p->i_scenecut_threshold )
745 p->i_scenecut_threshold = atoi(value);
749 p->b_intra_refresh = atobool(value);
751 p->i_bframe = atoi(value);
754 p->i_bframe_adaptive = atobool(value);
758 p->i_bframe_adaptive = atoi(value);
762 p->i_bframe_bias = atoi(value);
765 b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
769 p->i_bframe_pyramid = atoi(value);
773 p->b_open_gop = atobool(value);
775 p->b_deblocking_filter = !atobool(value);
776 OPT2("filter", "deblock")
778 if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
779 2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
781 p->b_deblocking_filter = 1;
783 else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
785 p->b_deblocking_filter = 1;
786 p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
789 p->b_deblocking_filter = atobool(value);
791 OPT("slice-max-size")
792 p->i_slice_max_size = atoi(value);
794 p->i_slice_max_mbs = atoi(value);
796 p->i_slice_min_mbs = atoi(value);
798 p->i_slice_count = atoi(value);
800 p->i_slice_count_max = atoi(value);
802 p->b_cabac = atobool(value);
804 p->i_cabac_init_idc = atoi(value);
806 p->b_interlaced = atobool(value);
808 p->b_interlaced = p->b_tff = atobool(value);
811 p->b_interlaced = atobool(value);
812 p->b_tff = !p->b_interlaced;
814 OPT("constrained-intra")
815 p->b_constrained_intra = atobool(value);
818 if( strstr( value, "flat" ) )
819 p->i_cqm_preset = X264_CQM_FLAT;
820 else if( strstr( value, "jvt" ) )
821 p->i_cqm_preset = X264_CQM_JVT;
823 p->psz_cqm_file = strdup(value);
826 p->psz_cqm_file = strdup(value);
829 p->i_cqm_preset = X264_CQM_CUSTOM;
830 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
831 b_error |= parse_cqm( value, p->cqm_4py, 16 );
832 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
833 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
837 p->i_cqm_preset = X264_CQM_CUSTOM;
838 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
839 b_error |= parse_cqm( value, p->cqm_8py, 64 );
840 b_error |= parse_cqm( value, p->cqm_8ic, 64 );
841 b_error |= parse_cqm( value, p->cqm_8pc, 64 );
845 p->i_cqm_preset = X264_CQM_CUSTOM;
846 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
847 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
851 p->i_cqm_preset = X264_CQM_CUSTOM;
852 b_error |= parse_cqm( value, p->cqm_4py, 16 );
853 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
857 p->i_cqm_preset = X264_CQM_CUSTOM;
858 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
862 p->i_cqm_preset = X264_CQM_CUSTOM;
863 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
867 p->i_cqm_preset = X264_CQM_CUSTOM;
868 b_error |= parse_cqm( value, p->cqm_4py, 16 );
872 p->i_cqm_preset = X264_CQM_CUSTOM;
873 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
877 p->i_cqm_preset = X264_CQM_CUSTOM;
878 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
879 b_error |= parse_cqm( value, p->cqm_8ic, 64 );
883 p->i_cqm_preset = X264_CQM_CUSTOM;
884 b_error |= parse_cqm( value, p->cqm_8py, 64 );
885 b_error |= parse_cqm( value, p->cqm_8pc, 64 );
888 p->i_log_level = atoi(value);
890 p->psz_dump_yuv = strdup(value);
891 OPT2("analyse", "partitions")
893 p->analyse.inter = 0;
894 if( strstr( value, "none" ) ) p->analyse.inter = 0;
895 if( strstr( value, "all" ) ) p->analyse.inter = ~0;
897 if( strstr( value, "i4x4" ) ) p->analyse.inter |= X264_ANALYSE_I4x4;
898 if( strstr( value, "i8x8" ) ) p->analyse.inter |= X264_ANALYSE_I8x8;
899 if( strstr( value, "p8x8" ) ) p->analyse.inter |= X264_ANALYSE_PSUB16x16;
900 if( strstr( value, "p4x4" ) ) p->analyse.inter |= X264_ANALYSE_PSUB8x8;
901 if( strstr( value, "b8x8" ) ) p->analyse.inter |= X264_ANALYSE_BSUB16x16;
904 p->analyse.b_transform_8x8 = atobool(value);
905 OPT2("weightb", "weight-b")
906 p->analyse.b_weighted_bipred = atobool(value);
908 p->analyse.i_weighted_pred = atoi(value);
909 OPT2("direct", "direct-pred")
910 b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
911 OPT("chroma-qp-offset")
912 p->analyse.i_chroma_qp_offset = atoi(value);
914 b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
915 OPT2("merange", "me-range")
916 p->analyse.i_me_range = atoi(value);
917 OPT2("mvrange", "mv-range")
918 p->analyse.i_mv_range = atoi(value);
919 OPT2("mvrange-thread", "mv-range-thread")
920 p->analyse.i_mv_range_thread = atoi(value);
921 OPT2("subme", "subq")
922 p->analyse.i_subpel_refine = atoi(value);
925 if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
926 2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
927 2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
929 else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
931 p->analyse.f_psy_trellis = 0;
935 p->analyse.f_psy_rd = 0;
936 p->analyse.f_psy_trellis = 0;
940 p->analyse.b_psy = atobool(value);
942 p->analyse.b_chroma_me = atobool(value);
944 p->analyse.b_mixed_references = atobool(value);
946 p->analyse.i_trellis = atoi(value);
948 p->analyse.b_fast_pskip = atobool(value);
950 p->analyse.b_dct_decimate = atobool(value);
951 OPT("deadzone-inter")
952 p->analyse.i_luma_deadzone[0] = atoi(value);
953 OPT("deadzone-intra")
954 p->analyse.i_luma_deadzone[1] = atoi(value);
956 p->analyse.i_noise_reduction = atoi(value);
959 p->rc.i_bitrate = atoi(value);
960 p->rc.i_rc_method = X264_RC_ABR;
962 OPT2("qp", "qp_constant")
964 p->rc.i_qp_constant = atoi(value);
965 p->rc.i_rc_method = X264_RC_CQP;
969 p->rc.f_rf_constant = atof(value);
970 p->rc.i_rc_method = X264_RC_CRF;
973 p->rc.f_rf_constant_max = atof(value);
975 p->rc.i_lookahead = atoi(value);
976 OPT2("qpmin", "qp-min")
977 p->rc.i_qp_min = atoi(value);
978 OPT2("qpmax", "qp-max")
979 p->rc.i_qp_max = atoi(value);
980 OPT2("qpstep", "qp-step")
981 p->rc.i_qp_step = atoi(value);
983 p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
985 p->rc.i_vbv_max_bitrate = atoi(value);
987 p->rc.i_vbv_buffer_size = atoi(value);
989 p->rc.f_vbv_buffer_init = atof(value);
990 OPT2("ipratio", "ip-factor")
991 p->rc.f_ip_factor = atof(value);
992 OPT2("pbratio", "pb-factor")
993 p->rc.f_pb_factor = atof(value);
995 p->rc.i_aq_mode = atoi(value);
997 p->rc.f_aq_strength = atof(value);
1000 int pass = x264_clip3( atoi(value), 0, 3 );
1001 p->rc.b_stat_write = pass & 1;
1002 p->rc.b_stat_read = pass & 2;
1006 p->rc.psz_stat_in = strdup(value);
1007 p->rc.psz_stat_out = strdup(value);
1010 p->rc.f_qcompress = atof(value);
1012 p->rc.b_mb_tree = atobool(value);
1014 p->rc.f_qblur = atof(value);
1015 OPT2("cplxblur", "cplx-blur")
1016 p->rc.f_complexity_blur = atof(value);
1018 p->rc.psz_zones = strdup(value);
1020 b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
1021 &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
1023 p->analyse.b_psnr = atobool(value);
1025 p->analyse.b_ssim = atobool(value);
1027 p->b_aud = atobool(value);
1029 p->i_sps_id = atoi(value);
1030 OPT("global-header")
1031 p->b_repeat_headers = !atobool(value);
1032 OPT("repeat-headers")
1033 p->b_repeat_headers = atobool(value);
1035 p->b_annexb = atobool(value);
1037 p->b_vfr_input = !atobool(value);
1039 b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
1041 p->rc.b_filler = atobool(value);
1043 p->b_pic_struct = atobool(value);
1044 OPT("fake-interlaced")
1045 p->b_fake_interlaced = atobool(value);
1046 OPT("frame-packing")
1047 p->i_frame_packing = atoi(value);
1049 p->b_stitchable = atobool(value);
1051 p->b_opencl = atobool( value );
1053 p->psz_clbin_file = strdup( value );
1054 OPT("opencl-device")
1055 p->i_opencl_device = atoi( value );
1059 errortype = X264_PARAM_BAD_NAME;
1070 b_error |= value_was_null && !name_was_bool;
1071 return b_error ? errortype : 0;
1074 /****************************************************************************
1076 ****************************************************************************/
1077 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
1079 if( !h || i_level <= h->param.i_log_level )
1082 va_start( arg, psz_fmt );
1084 x264_log_default( NULL, i_level, psz_fmt, arg );
1086 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
1091 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
1096 case X264_LOG_ERROR:
1097 psz_prefix = "error";
1099 case X264_LOG_WARNING:
1100 psz_prefix = "warning";
1103 psz_prefix = "info";
1105 case X264_LOG_DEBUG:
1106 psz_prefix = "debug";
1109 psz_prefix = "unknown";
1112 fprintf( stderr, "x264 [%s]: ", psz_prefix );
1113 x264_vfprintf( stderr, psz_fmt, arg );
1116 /****************************************************************************
1117 * x264_picture_init:
1118 ****************************************************************************/
1119 void x264_picture_init( x264_picture_t *pic )
1121 memset( pic, 0, sizeof( x264_picture_t ) );
1122 pic->i_type = X264_TYPE_AUTO;
1123 pic->i_qpplus1 = X264_QP_AUTO;
1124 pic->i_pic_struct = PIC_STRUCT_AUTO;
1127 /****************************************************************************
1128 * x264_picture_alloc:
1129 ****************************************************************************/
1130 int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
1139 static const x264_csp_tab_t x264_csp_tab[] =
1141 [X264_CSP_I420] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1142 [X264_CSP_YV12] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1143 [X264_CSP_NV12] = { 2, { 256*1, 256*1 }, { 256*1, 256/2 }, },
1144 [X264_CSP_NV21] = { 2, { 256*1, 256*1 }, { 256*1, 256/2 }, },
1145 [X264_CSP_I422] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1146 [X264_CSP_YV16] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1147 [X264_CSP_NV16] = { 2, { 256*1, 256*1 }, { 256*1, 256*1 }, },
1148 [X264_CSP_I444] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1149 [X264_CSP_YV24] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1150 [X264_CSP_BGR] = { 1, { 256*3 }, { 256*1 }, },
1151 [X264_CSP_BGRA] = { 1, { 256*4 }, { 256*1 }, },
1152 [X264_CSP_RGB] = { 1, { 256*3 }, { 256*1 }, },
1155 int csp = i_csp & X264_CSP_MASK;
1156 if( csp <= X264_CSP_NONE || csp >= X264_CSP_MAX || csp == X264_CSP_V210 )
1158 x264_picture_init( pic );
1159 pic->img.i_csp = i_csp;
1160 pic->img.i_plane = x264_csp_tab[csp].planes;
1161 int depth_factor = i_csp & X264_CSP_HIGH_DEPTH ? 2 : 1;
1162 int plane_offset[3] = {0};
1164 for( int i = 0; i < pic->img.i_plane; i++ )
1166 int stride = (((int64_t)i_width * x264_csp_tab[csp].width_fix8[i]) >> 8) * depth_factor;
1167 int plane_size = (((int64_t)i_height * x264_csp_tab[csp].height_fix8[i]) >> 8) * stride;
1168 pic->img.i_stride[i] = stride;
1169 plane_offset[i] = frame_size;
1170 frame_size += plane_size;
1172 pic->img.plane[0] = x264_malloc( frame_size );
1173 if( !pic->img.plane[0] )
1175 for( int i = 1; i < pic->img.i_plane; i++ )
1176 pic->img.plane[i] = pic->img.plane[0] + plane_offset[i];
1180 /****************************************************************************
1181 * x264_picture_clean:
1182 ****************************************************************************/
1183 void x264_picture_clean( x264_picture_t *pic )
1185 x264_free( pic->img.plane[0] );
1187 /* just to be safe */
1188 memset( pic, 0, sizeof( x264_picture_t ) );
1191 /****************************************************************************
1193 ****************************************************************************/
1194 void *x264_malloc( int i_size )
1196 uint8_t *align_buf = NULL;
1199 #define HUGE_PAGE_SIZE 2*1024*1024
1200 #define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
1201 /* Attempt to allocate huge pages to reduce TLB misses. */
1202 if( i_size >= HUGE_PAGE_THRESHOLD )
1204 align_buf = memalign( HUGE_PAGE_SIZE, i_size );
1207 /* Round up to the next huge page boundary if we are close enough. */
1208 size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
1209 madvise( align_buf, madv_size, MADV_HUGEPAGE );
1213 #undef HUGE_PAGE_SIZE
1214 #undef HUGE_PAGE_THRESHOLD
1216 align_buf = memalign( NATIVE_ALIGN, i_size );
1218 uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
1221 align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
1222 align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
1223 *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
1227 x264_log( NULL, X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
1231 /****************************************************************************
1233 ****************************************************************************/
1234 void x264_free( void *p )
1241 free( *( ( ( void **) p ) - 1 ) );
1246 /****************************************************************************
1247 * x264_reduce_fraction:
1248 ****************************************************************************/
1249 #define REDUCE_FRACTION( name, type )\
1250 void name( type *n, type *d )\
1268 REDUCE_FRACTION( x264_reduce_fraction , uint32_t )
1269 REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
1271 /****************************************************************************
1273 ****************************************************************************/
1274 char *x264_slurp_file( const char *filename )
1279 FILE *fh = x264_fopen( filename, "rb" );
1283 b_error |= fseek( fh, 0, SEEK_END ) < 0;
1284 b_error |= ( i_size = ftell( fh ) ) <= 0;
1285 if( WORD_SIZE == 4 )
1286 b_error |= i_size > INT32_MAX;
1287 b_error |= fseek( fh, 0, SEEK_SET ) < 0;
1291 buf = x264_malloc( i_size+2 );
1295 b_error |= fread( buf, 1, i_size, fh ) != i_size;
1303 if( buf[i_size-1] != '\n' )
1304 buf[i_size++] = '\n';
1313 /****************************************************************************
1314 * x264_param2string:
1315 ****************************************************************************/
1316 char *x264_param2string( x264_param_t *p, int b_res )
1320 if( p->rc.psz_zones )
1321 len += strlen(p->rc.psz_zones);
1322 buf = s = x264_malloc( len );
1328 s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
1329 s += sprintf( s, "fps=%u/%u ", p->i_fps_num, p->i_fps_den );
1330 s += sprintf( s, "timebase=%u/%u ", p->i_timebase_num, p->i_timebase_den );
1331 s += sprintf( s, "bitdepth=%d ", BIT_DEPTH );
1335 s += sprintf( s, "opencl=%d ", p->b_opencl );
1336 s += sprintf( s, "cabac=%d", p->b_cabac );
1337 s += sprintf( s, " ref=%d", p->i_frame_reference );
1338 s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
1339 p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
1340 s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
1341 s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
1342 s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
1343 s += sprintf( s, " psy=%d", p->analyse.b_psy );
1344 if( p->analyse.b_psy )
1345 s += sprintf( s, " psy_rd=%.2f:%.2f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis );
1346 s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
1347 s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
1348 s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
1349 s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
1350 s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
1351 s += sprintf( s, " cqm=%d", p->i_cqm_preset );
1352 s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
1353 s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
1354 s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
1355 s += sprintf( s, " threads=%d", p->i_threads );
1356 s += sprintf( s, " lookahead_threads=%d", p->i_lookahead_threads );
1357 s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
1358 if( p->i_slice_count )
1359 s += sprintf( s, " slices=%d", p->i_slice_count );
1360 if( p->i_slice_count_max )
1361 s += sprintf( s, " slices_max=%d", p->i_slice_count_max );
1362 if( p->i_slice_max_size )
1363 s += sprintf( s, " slice_max_size=%d", p->i_slice_max_size );
1364 if( p->i_slice_max_mbs )
1365 s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
1366 if( p->i_slice_min_mbs )
1367 s += sprintf( s, " slice_min_mbs=%d", p->i_slice_min_mbs );
1368 s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
1369 s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
1370 s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : p->b_fake_interlaced ? "fake" : "0" );
1371 s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
1372 if( p->b_stitchable )
1373 s += sprintf( s, " stitchable=%d", p->b_stitchable );
1375 s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1377 s += sprintf( s, " bframes=%d", p->i_bframe );
1380 s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1381 p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1382 p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
1384 s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1386 if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1387 s += sprintf( s, " keyint=infinite" );
1389 s += sprintf( s, " keyint=%d", p->i_keyint_max );
1390 s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1391 p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1393 if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1394 s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1396 s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1397 ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1398 : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1399 if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1401 if( p->rc.i_rc_method == X264_RC_CRF )
1402 s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
1404 s += sprintf( s, " bitrate=%d ratetol=%.1f",
1405 p->rc.i_bitrate, p->rc.f_rate_tolerance );
1406 s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1407 p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
1408 if( p->rc.b_stat_read )
1409 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1410 p->rc.f_complexity_blur, p->rc.f_qblur );
1411 if( p->rc.i_vbv_buffer_size )
1413 s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1414 p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1415 if( p->rc.i_rc_method == X264_RC_CRF )
1416 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1419 else if( p->rc.i_rc_method == X264_RC_CQP )
1420 s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1422 if( p->rc.i_vbv_buffer_size )
1423 s += sprintf( s, " nal_hrd=%s filler=%d", x264_nal_hrd_names[p->i_nal_hrd], p->rc.b_filler );
1424 if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
1425 s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
1426 p->crop_rect.i_right, p->crop_rect.i_bottom );
1427 if( p->i_frame_packing >= 0 )
1428 s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
1430 if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1432 s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1433 if( p->i_bframe && !p->rc.b_mb_tree )
1434 s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1435 s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1436 if( p->rc.i_aq_mode )
1437 s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1438 if( p->rc.psz_zones )
1439 s += sprintf( s, " zones=%s", p->rc.psz_zones );
1440 else if( p->rc.i_zones )
1441 s += sprintf( s, " zones" );