1 /*****************************************************************************
2 * common.c: misc common functions
3 *****************************************************************************
4 * Copyright (C) 2003-2013 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 *****************************************************************************/
39 const int x264_bit_depth = BIT_DEPTH;
41 const int x264_chroma_format = X264_CHROMA_FORMAT;
43 static void x264_log_default( void *, int, const char *, va_list );
45 /****************************************************************************
47 ****************************************************************************/
48 void x264_param_default( x264_param_t *param )
51 memset( param, 0, sizeof( x264_param_t ) );
54 param->cpu = x264_cpu_detect();
55 param->i_threads = X264_THREADS_AUTO;
56 param->i_lookahead_threads = X264_THREADS_AUTO;
57 param->b_deterministic = 1;
58 param->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
60 /* Video properties */
61 param->i_csp = X264_CHROMA_FORMAT ? X264_CHROMA_FORMAT : X264_CSP_I420;
64 param->vui.i_sar_width = 0;
65 param->vui.i_sar_height= 0;
66 param->vui.i_overscan = 0; /* undef */
67 param->vui.i_vidformat = 5; /* undef */
68 param->vui.b_fullrange = -1; /* default depends on input */
69 param->vui.i_colorprim = 2; /* undef */
70 param->vui.i_transfer = 2; /* undef */
71 param->vui.i_colmatrix = -1; /* default depends on input */
72 param->vui.i_chroma_loc= 0; /* left center */
73 param->i_fps_num = 25;
75 param->i_level_idc = -1;
76 param->i_slice_max_size = 0;
77 param->i_slice_max_mbs = 0;
78 param->i_slice_count = 0;
80 /* Encoder parameters */
81 param->i_frame_reference = 3;
82 param->i_keyint_max = 250;
83 param->i_keyint_min = X264_KEYINT_MIN_AUTO;
85 param->i_scenecut_threshold = 40;
86 param->i_bframe_adaptive = X264_B_ADAPT_FAST;
87 param->i_bframe_bias = 0;
88 param->i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
89 param->b_interlaced = 0;
90 param->b_constrained_intra = 0;
92 param->b_deblocking_filter = 1;
93 param->i_deblocking_filter_alphac0 = 0;
94 param->i_deblocking_filter_beta = 0;
97 param->i_cabac_init_idc = 0;
99 param->rc.i_rc_method = X264_RC_CRF;
100 param->rc.i_bitrate = 0;
101 param->rc.f_rate_tolerance = 1.0;
102 param->rc.i_vbv_max_bitrate = 0;
103 param->rc.i_vbv_buffer_size = 0;
104 param->rc.f_vbv_buffer_init = 0.9;
105 param->rc.i_qp_constant = 23 + QP_BD_OFFSET;
106 param->rc.f_rf_constant = 23;
107 param->rc.i_qp_min = 0;
108 param->rc.i_qp_max = QP_MAX;
109 param->rc.i_qp_step = 4;
110 param->rc.f_ip_factor = 1.4;
111 param->rc.f_pb_factor = 1.3;
112 param->rc.i_aq_mode = X264_AQ_VARIANCE;
113 param->rc.f_aq_strength = 1.0;
114 param->rc.i_lookahead = 40;
116 param->rc.b_stat_write = 0;
117 param->rc.psz_stat_out = "x264_2pass.log";
118 param->rc.b_stat_read = 0;
119 param->rc.psz_stat_in = "x264_2pass.log";
120 param->rc.f_qcompress = 0.6;
121 param->rc.f_qblur = 0.5;
122 param->rc.f_complexity_blur = 20;
123 param->rc.i_zones = 0;
124 param->rc.b_mb_tree = 1;
127 param->pf_log = x264_log_default;
128 param->p_log_private = NULL;
129 param->i_log_level = X264_LOG_INFO;
132 param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
133 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
134 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
135 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
136 param->analyse.i_me_method = X264_ME_HEX;
137 param->analyse.f_psy_rd = 1.0;
138 param->analyse.b_psy = 1;
139 param->analyse.f_psy_trellis = 0;
140 param->analyse.i_me_range = 16;
141 param->analyse.i_subpel_refine = 7;
142 param->analyse.b_mixed_references = 1;
143 param->analyse.b_chroma_me = 1;
144 param->analyse.i_mv_range_thread = -1;
145 param->analyse.i_mv_range = -1; // set from level_idc
146 param->analyse.i_chroma_qp_offset = 0;
147 param->analyse.b_fast_pskip = 1;
148 param->analyse.b_weighted_bipred = 1;
149 param->analyse.i_weighted_pred = X264_WEIGHTP_SMART;
150 param->analyse.b_dct_decimate = 1;
151 param->analyse.b_transform_8x8 = 1;
152 param->analyse.i_trellis = 1;
153 param->analyse.i_luma_deadzone[0] = 21;
154 param->analyse.i_luma_deadzone[1] = 11;
155 param->analyse.b_psnr = 0;
156 param->analyse.b_ssim = 0;
158 param->i_cqm_preset = X264_CQM_FLAT;
159 memset( param->cqm_4iy, 16, sizeof( param->cqm_4iy ) );
160 memset( param->cqm_4py, 16, sizeof( param->cqm_4py ) );
161 memset( param->cqm_4ic, 16, sizeof( param->cqm_4ic ) );
162 memset( param->cqm_4pc, 16, sizeof( param->cqm_4pc ) );
163 memset( param->cqm_8iy, 16, sizeof( param->cqm_8iy ) );
164 memset( param->cqm_8py, 16, sizeof( param->cqm_8py ) );
165 memset( param->cqm_8ic, 16, sizeof( param->cqm_8ic ) );
166 memset( param->cqm_8pc, 16, sizeof( param->cqm_8pc ) );
168 param->b_repeat_headers = 1;
171 param->b_vfr_input = 1;
172 param->i_nal_hrd = X264_NAL_HRD_NONE;
174 param->b_pic_struct = 0;
175 param->b_fake_interlaced = 0;
176 param->i_frame_packing = -1;
178 param->i_opencl_device = 0;
179 param->opencl_device_id = NULL;
180 param->psz_clbin_file = NULL;
183 static int x264_param_apply_preset( x264_param_t *param, const char *preset )
186 int i = strtol( preset, &end, 10 );
187 if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
188 preset = x264_preset_names[i];
190 if( !strcasecmp( preset, "ultrafast" ) )
192 param->i_frame_reference = 1;
193 param->i_scenecut_threshold = 0;
194 param->b_deblocking_filter = 0;
197 param->analyse.intra = 0;
198 param->analyse.inter = 0;
199 param->analyse.b_transform_8x8 = 0;
200 param->analyse.i_me_method = X264_ME_DIA;
201 param->analyse.i_subpel_refine = 0;
202 param->rc.i_aq_mode = 0;
203 param->analyse.b_mixed_references = 0;
204 param->analyse.i_trellis = 0;
205 param->i_bframe_adaptive = X264_B_ADAPT_NONE;
206 param->rc.b_mb_tree = 0;
207 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
208 param->analyse.b_weighted_bipred = 0;
209 param->rc.i_lookahead = 0;
211 else if( !strcasecmp( preset, "superfast" ) )
213 param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
214 param->analyse.i_me_method = X264_ME_DIA;
215 param->analyse.i_subpel_refine = 1;
216 param->i_frame_reference = 1;
217 param->analyse.b_mixed_references = 0;
218 param->analyse.i_trellis = 0;
219 param->rc.b_mb_tree = 0;
220 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
221 param->rc.i_lookahead = 0;
223 else if( !strcasecmp( preset, "veryfast" ) )
225 param->analyse.i_me_method = X264_ME_HEX;
226 param->analyse.i_subpel_refine = 2;
227 param->i_frame_reference = 1;
228 param->analyse.b_mixed_references = 0;
229 param->analyse.i_trellis = 0;
230 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
231 param->rc.i_lookahead = 10;
233 else if( !strcasecmp( preset, "faster" ) )
235 param->analyse.b_mixed_references = 0;
236 param->i_frame_reference = 2;
237 param->analyse.i_subpel_refine = 4;
238 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
239 param->rc.i_lookahead = 20;
241 else if( !strcasecmp( preset, "fast" ) )
243 param->i_frame_reference = 2;
244 param->analyse.i_subpel_refine = 6;
245 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
246 param->rc.i_lookahead = 30;
248 else if( !strcasecmp( preset, "medium" ) )
250 /* Default is medium */
252 else if( !strcasecmp( preset, "slow" ) )
254 param->analyse.i_me_method = X264_ME_UMH;
255 param->analyse.i_subpel_refine = 8;
256 param->i_frame_reference = 5;
257 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
258 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
259 param->rc.i_lookahead = 50;
261 else if( !strcasecmp( preset, "slower" ) )
263 param->analyse.i_me_method = X264_ME_UMH;
264 param->analyse.i_subpel_refine = 9;
265 param->i_frame_reference = 8;
266 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
267 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
268 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
269 param->analyse.i_trellis = 2;
270 param->rc.i_lookahead = 60;
272 else if( !strcasecmp( preset, "veryslow" ) )
274 param->analyse.i_me_method = X264_ME_UMH;
275 param->analyse.i_subpel_refine = 10;
276 param->analyse.i_me_range = 24;
277 param->i_frame_reference = 16;
278 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
279 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
280 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
281 param->analyse.i_trellis = 2;
283 param->rc.i_lookahead = 60;
285 else if( !strcasecmp( preset, "placebo" ) )
287 param->analyse.i_me_method = X264_ME_TESA;
288 param->analyse.i_subpel_refine = 11;
289 param->analyse.i_me_range = 24;
290 param->i_frame_reference = 16;
291 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
292 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
293 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
294 param->analyse.b_fast_pskip = 0;
295 param->analyse.i_trellis = 2;
296 param->i_bframe = 16;
297 param->rc.i_lookahead = 60;
301 x264_log( NULL, X264_LOG_ERROR, "invalid preset '%s'\n", preset );
307 static int x264_param_apply_tune( x264_param_t *param, const char *tune )
309 char *tmp = x264_malloc( strlen( tune ) + 1 );
312 tmp = strcpy( tmp, tune );
313 char *s = strtok( tmp, ",./-+" );
314 int psy_tuning_used = 0;
317 if( !strncasecmp( s, "film", 4 ) )
319 if( psy_tuning_used++ ) goto psy_failure;
320 param->i_deblocking_filter_alphac0 = -1;
321 param->i_deblocking_filter_beta = -1;
322 param->analyse.f_psy_trellis = 0.15;
324 else if( !strncasecmp( s, "animation", 9 ) )
326 if( psy_tuning_used++ ) goto psy_failure;
327 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
328 param->i_deblocking_filter_alphac0 = 1;
329 param->i_deblocking_filter_beta = 1;
330 param->analyse.f_psy_rd = 0.4;
331 param->rc.f_aq_strength = 0.6;
332 param->i_bframe += 2;
334 else if( !strncasecmp( s, "grain", 5 ) )
336 if( psy_tuning_used++ ) goto psy_failure;
337 param->i_deblocking_filter_alphac0 = -2;
338 param->i_deblocking_filter_beta = -2;
339 param->analyse.f_psy_trellis = 0.25;
340 param->analyse.b_dct_decimate = 0;
341 param->rc.f_pb_factor = 1.1;
342 param->rc.f_ip_factor = 1.1;
343 param->rc.f_aq_strength = 0.5;
344 param->analyse.i_luma_deadzone[0] = 6;
345 param->analyse.i_luma_deadzone[1] = 6;
346 param->rc.f_qcompress = 0.8;
348 else if( !strncasecmp( s, "stillimage", 10 ) )
350 if( psy_tuning_used++ ) goto psy_failure;
351 param->i_deblocking_filter_alphac0 = -3;
352 param->i_deblocking_filter_beta = -3;
353 param->analyse.f_psy_rd = 2.0;
354 param->analyse.f_psy_trellis = 0.7;
355 param->rc.f_aq_strength = 1.2;
357 else if( !strncasecmp( s, "psnr", 4 ) )
359 if( psy_tuning_used++ ) goto psy_failure;
360 param->rc.i_aq_mode = X264_AQ_NONE;
361 param->analyse.b_psy = 0;
363 else if( !strncasecmp( s, "ssim", 4 ) )
365 if( psy_tuning_used++ ) goto psy_failure;
366 param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
367 param->analyse.b_psy = 0;
369 else if( !strncasecmp( s, "fastdecode", 10 ) )
371 param->b_deblocking_filter = 0;
373 param->analyse.b_weighted_bipred = 0;
374 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
376 else if( !strncasecmp( s, "zerolatency", 11 ) )
378 param->rc.i_lookahead = 0;
379 param->i_sync_lookahead = 0;
381 param->b_sliced_threads = 1;
382 param->b_vfr_input = 0;
383 param->rc.b_mb_tree = 0;
385 else if( !strncasecmp( s, "touhou", 6 ) )
387 if( psy_tuning_used++ ) goto psy_failure;
388 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
389 param->i_deblocking_filter_alphac0 = -1;
390 param->i_deblocking_filter_beta = -1;
391 param->analyse.f_psy_trellis = 0.2;
392 param->rc.f_aq_strength = 1.3;
393 if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
394 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
398 x264_log( NULL, X264_LOG_ERROR, "invalid tune '%s'\n", s );
405 x264_log( NULL, X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %s\n", s );
407 s = strtok( NULL, ",./-+" );
413 int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
415 x264_param_default( param );
417 if( preset && x264_param_apply_preset( param, preset ) < 0 )
419 if( tune && x264_param_apply_tune( param, tune ) < 0 )
424 void x264_param_apply_fastfirstpass( x264_param_t *param )
426 /* Set faster options in case of turbo firstpass. */
427 if( param->rc.b_stat_write && !param->rc.b_stat_read )
429 param->i_frame_reference = 1;
430 param->analyse.b_transform_8x8 = 0;
431 param->analyse.inter = 0;
432 param->analyse.i_me_method = X264_ME_DIA;
433 param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
434 param->analyse.i_trellis = 0;
435 param->analyse.b_fast_pskip = 1;
439 static int profile_string_to_int( const char *str )
441 if( !strcasecmp( str, "baseline" ) )
442 return PROFILE_BASELINE;
443 if( !strcasecmp( str, "main" ) )
445 if( !strcasecmp( str, "high" ) )
447 if( !strcasecmp( str, "high10" ) )
448 return PROFILE_HIGH10;
449 if( !strcasecmp( str, "high422" ) )
450 return PROFILE_HIGH422;
451 if( !strcasecmp( str, "high444" ) )
452 return PROFILE_HIGH444_PREDICTIVE;
456 int x264_param_apply_profile( x264_param_t *param, const char *profile )
461 int p = profile_string_to_int( profile );
464 x264_log( NULL, X264_LOG_ERROR, "invalid profile: %s\n", profile );
467 if( p < PROFILE_HIGH444_PREDICTIVE && ((param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
468 (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0)) )
470 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
473 if( p < PROFILE_HIGH444_PREDICTIVE && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I444 )
475 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:4:4\n", profile );
478 if( p < PROFILE_HIGH422 && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I422 )
480 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:2:2\n", profile );
483 if( p < PROFILE_HIGH10 && BIT_DEPTH > 8 )
485 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d\n", profile, BIT_DEPTH );
489 if( p == PROFILE_BASELINE )
491 param->analyse.b_transform_8x8 = 0;
493 param->i_cqm_preset = X264_CQM_FLAT;
494 param->psz_cqm_file = NULL;
496 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
497 if( param->b_interlaced )
499 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
502 if( param->b_fake_interlaced )
504 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
508 else if( p == PROFILE_MAIN )
510 param->analyse.b_transform_8x8 = 0;
511 param->i_cqm_preset = X264_CQM_FLAT;
512 param->psz_cqm_file = NULL;
517 static int parse_enum( const char *arg, const char * const *names, int *dst )
519 for( int i = 0; names[i]; i++ )
520 if( !strcmp( arg, names[i] ) )
528 static int parse_cqm( const char *str, uint8_t *cqm, int length )
533 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
536 } while( i < length && (str = strchr( str, ',' )) && str++ );
537 return (i == length) ? 0 : -1;
540 static int x264_atobool( const char *str, int *b_error )
542 if( !strcmp(str, "1") ||
543 !strcmp(str, "true") ||
544 !strcmp(str, "yes") )
546 if( !strcmp(str, "0") ||
547 !strcmp(str, "false") ||
554 static int x264_atoi( const char *str, int *b_error )
557 int v = strtol( str, &end, 0 );
558 if( end == str || *end != '\0' )
563 static double x264_atof( const char *str, int *b_error )
566 double v = strtod( str, &end );
567 if( end == str || *end != '\0' )
572 #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
575 #define atoi(str) x264_atoi( str, &b_error )
576 #define atof(str) x264_atof( str, &b_error )
578 int x264_param_parse( x264_param_t *p, const char *name, const char *value )
580 char *name_buf = NULL;
583 int value_was_null = !value;
587 return X264_PARAM_BAD_NAME;
591 if( value[0] == '=' )
594 if( strchr( name, '_' ) ) // s/_/-/g
597 name_buf = strdup(name);
598 while( (c = strchr( name_buf, '_' )) )
603 if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
604 (!strncmp( name, "no", 2 ) && (i = 2)) )
607 value = atobool(value) ? "false" : "true";
611 #define OPT(STR) else if( !strcmp( name, STR ) )
612 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
616 p->cpu = isdigit(value[0]) ? atoi(value) :
617 !strcmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
620 char *buf = strdup(value);
621 char *tok, UNUSED *saveptr=NULL, *init;
624 for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
626 for( i=0; x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name); i++ );
627 p->cpu |= x264_cpu_names[i].flags;
628 if( !x264_cpu_names[i].flags )
632 if( (p->cpu&X264_CPU_SSSE3) && !(p->cpu&X264_CPU_SSE2_IS_SLOW) )
633 p->cpu |= X264_CPU_SSE2_IS_FAST;
638 if( !strcmp(value, "auto") )
639 p->i_threads = X264_THREADS_AUTO;
641 p->i_threads = atoi(value);
643 OPT("lookahead-threads")
645 if( !strcmp(value, "auto") )
646 p->i_lookahead_threads = X264_THREADS_AUTO;
648 p->i_lookahead_threads = atoi(value);
650 OPT("sliced-threads")
651 p->b_sliced_threads = atobool(value);
652 OPT("sync-lookahead")
654 if( !strcmp(value, "auto") )
655 p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
657 p->i_sync_lookahead = atoi(value);
659 OPT2("deterministic", "n-deterministic")
660 p->b_deterministic = atobool(value);
661 OPT("cpu-independent")
662 p->b_cpu_independent = atobool(value);
663 OPT2("level", "level-idc")
665 if( !strcmp(value, "1b") )
667 else if( atof(value) < 6 )
668 p->i_level_idc = (int)(10*atof(value)+.5);
670 p->i_level_idc = atoi(value);
673 p->b_bluray_compat = atobool(value);
674 OPT("avcintra-compat")
675 p->b_avcintra_compat = atobool(value);
678 b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
679 2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
682 b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
684 b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
686 b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
688 b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
690 b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
692 b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
695 p->vui.i_chroma_loc = atoi(value);
696 b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
700 if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) == 2 )
704 float fps = atof(value);
705 if( fps > 0 && fps <= INT_MAX/1000 )
707 p->i_fps_num = (int)(fps * 1000 + .5);
712 p->i_fps_num = atoi(value);
717 OPT2("ref", "frameref")
718 p->i_frame_reference = atoi(value);
720 p->i_dpb_size = atoi(value);
723 if( strstr( value, "infinite" ) )
724 p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
726 p->i_keyint_max = atoi(value);
728 OPT2("min-keyint", "keyint-min")
730 p->i_keyint_min = atoi(value);
731 if( p->i_keyint_max < p->i_keyint_min )
732 p->i_keyint_max = p->i_keyint_min;
736 p->i_scenecut_threshold = atobool(value);
737 if( b_error || p->i_scenecut_threshold )
740 p->i_scenecut_threshold = atoi(value);
744 p->b_intra_refresh = atobool(value);
746 p->i_bframe = atoi(value);
749 p->i_bframe_adaptive = atobool(value);
753 p->i_bframe_adaptive = atoi(value);
757 p->i_bframe_bias = atoi(value);
760 b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
764 p->i_bframe_pyramid = atoi(value);
768 p->b_open_gop = atobool(value);
770 p->b_deblocking_filter = !atobool(value);
771 OPT2("filter", "deblock")
773 if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
774 2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
776 p->b_deblocking_filter = 1;
778 else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
780 p->b_deblocking_filter = 1;
781 p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
784 p->b_deblocking_filter = atobool(value);
786 OPT("slice-max-size")
787 p->i_slice_max_size = atoi(value);
789 p->i_slice_max_mbs = atoi(value);
791 p->i_slice_min_mbs = atoi(value);
793 p->i_slice_count = atoi(value);
795 p->i_slice_count_max = atoi(value);
797 p->b_cabac = atobool(value);
799 p->i_cabac_init_idc = atoi(value);
801 p->b_interlaced = atobool(value);
803 p->b_interlaced = p->b_tff = atobool(value);
806 p->b_interlaced = atobool(value);
807 p->b_tff = !p->b_interlaced;
809 OPT("constrained-intra")
810 p->b_constrained_intra = atobool(value);
813 if( strstr( value, "flat" ) )
814 p->i_cqm_preset = X264_CQM_FLAT;
815 else if( strstr( value, "jvt" ) )
816 p->i_cqm_preset = X264_CQM_JVT;
818 p->psz_cqm_file = strdup(value);
821 p->psz_cqm_file = strdup(value);
824 p->i_cqm_preset = X264_CQM_CUSTOM;
825 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
826 b_error |= parse_cqm( value, p->cqm_4py, 16 );
827 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
828 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
832 p->i_cqm_preset = X264_CQM_CUSTOM;
833 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
834 b_error |= parse_cqm( value, p->cqm_8py, 64 );
835 b_error |= parse_cqm( value, p->cqm_8ic, 64 );
836 b_error |= parse_cqm( value, p->cqm_8pc, 64 );
840 p->i_cqm_preset = X264_CQM_CUSTOM;
841 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
842 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
846 p->i_cqm_preset = X264_CQM_CUSTOM;
847 b_error |= parse_cqm( value, p->cqm_4py, 16 );
848 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
852 p->i_cqm_preset = X264_CQM_CUSTOM;
853 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
857 p->i_cqm_preset = X264_CQM_CUSTOM;
858 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
862 p->i_cqm_preset = X264_CQM_CUSTOM;
863 b_error |= parse_cqm( value, p->cqm_4py, 16 );
867 p->i_cqm_preset = X264_CQM_CUSTOM;
868 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
872 p->i_cqm_preset = X264_CQM_CUSTOM;
873 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
874 b_error |= parse_cqm( value, p->cqm_8ic, 64 );
878 p->i_cqm_preset = X264_CQM_CUSTOM;
879 b_error |= parse_cqm( value, p->cqm_8py, 64 );
880 b_error |= parse_cqm( value, p->cqm_8pc, 64 );
883 p->i_log_level = atoi(value);
885 p->psz_dump_yuv = strdup(value);
886 OPT2("analyse", "partitions")
888 p->analyse.inter = 0;
889 if( strstr( value, "none" ) ) p->analyse.inter = 0;
890 if( strstr( value, "all" ) ) p->analyse.inter = ~0;
892 if( strstr( value, "i4x4" ) ) p->analyse.inter |= X264_ANALYSE_I4x4;
893 if( strstr( value, "i8x8" ) ) p->analyse.inter |= X264_ANALYSE_I8x8;
894 if( strstr( value, "p8x8" ) ) p->analyse.inter |= X264_ANALYSE_PSUB16x16;
895 if( strstr( value, "p4x4" ) ) p->analyse.inter |= X264_ANALYSE_PSUB8x8;
896 if( strstr( value, "b8x8" ) ) p->analyse.inter |= X264_ANALYSE_BSUB16x16;
899 p->analyse.b_transform_8x8 = atobool(value);
900 OPT2("weightb", "weight-b")
901 p->analyse.b_weighted_bipred = atobool(value);
903 p->analyse.i_weighted_pred = atoi(value);
904 OPT2("direct", "direct-pred")
905 b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
906 OPT("chroma-qp-offset")
907 p->analyse.i_chroma_qp_offset = atoi(value);
909 b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
910 OPT2("merange", "me-range")
911 p->analyse.i_me_range = atoi(value);
912 OPT2("mvrange", "mv-range")
913 p->analyse.i_mv_range = atoi(value);
914 OPT2("mvrange-thread", "mv-range-thread")
915 p->analyse.i_mv_range_thread = atoi(value);
916 OPT2("subme", "subq")
917 p->analyse.i_subpel_refine = atoi(value);
920 if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
921 2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
922 2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
924 else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
926 p->analyse.f_psy_trellis = 0;
930 p->analyse.f_psy_rd = 0;
931 p->analyse.f_psy_trellis = 0;
935 p->analyse.b_psy = atobool(value);
937 p->analyse.b_chroma_me = atobool(value);
939 p->analyse.b_mixed_references = atobool(value);
941 p->analyse.i_trellis = atoi(value);
943 p->analyse.b_fast_pskip = atobool(value);
945 p->analyse.b_dct_decimate = atobool(value);
946 OPT("deadzone-inter")
947 p->analyse.i_luma_deadzone[0] = atoi(value);
948 OPT("deadzone-intra")
949 p->analyse.i_luma_deadzone[1] = atoi(value);
951 p->analyse.i_noise_reduction = atoi(value);
954 p->rc.i_bitrate = atoi(value);
955 p->rc.i_rc_method = X264_RC_ABR;
957 OPT2("qp", "qp_constant")
959 p->rc.i_qp_constant = atoi(value);
960 p->rc.i_rc_method = X264_RC_CQP;
964 p->rc.f_rf_constant = atof(value);
965 p->rc.i_rc_method = X264_RC_CRF;
968 p->rc.f_rf_constant_max = atof(value);
970 p->rc.i_lookahead = atoi(value);
971 OPT2("qpmin", "qp-min")
972 p->rc.i_qp_min = atoi(value);
973 OPT2("qpmax", "qp-max")
974 p->rc.i_qp_max = atoi(value);
975 OPT2("qpstep", "qp-step")
976 p->rc.i_qp_step = atoi(value);
978 p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
980 p->rc.i_vbv_max_bitrate = atoi(value);
982 p->rc.i_vbv_buffer_size = atoi(value);
984 p->rc.f_vbv_buffer_init = atof(value);
985 OPT2("ipratio", "ip-factor")
986 p->rc.f_ip_factor = atof(value);
987 OPT2("pbratio", "pb-factor")
988 p->rc.f_pb_factor = atof(value);
990 p->rc.i_aq_mode = atoi(value);
992 p->rc.f_aq_strength = atof(value);
995 int pass = x264_clip3( atoi(value), 0, 3 );
996 p->rc.b_stat_write = pass & 1;
997 p->rc.b_stat_read = pass & 2;
1001 p->rc.psz_stat_in = strdup(value);
1002 p->rc.psz_stat_out = strdup(value);
1005 p->rc.f_qcompress = atof(value);
1007 p->rc.b_mb_tree = atobool(value);
1009 p->rc.f_qblur = atof(value);
1010 OPT2("cplxblur", "cplx-blur")
1011 p->rc.f_complexity_blur = atof(value);
1013 p->rc.psz_zones = strdup(value);
1015 b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
1016 &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
1018 p->analyse.b_psnr = atobool(value);
1020 p->analyse.b_ssim = atobool(value);
1022 p->b_aud = atobool(value);
1024 p->i_sps_id = atoi(value);
1025 OPT("global-header")
1026 p->b_repeat_headers = !atobool(value);
1027 OPT("repeat-headers")
1028 p->b_repeat_headers = atobool(value);
1030 p->b_annexb = atobool(value);
1032 p->b_vfr_input = !atobool(value);
1034 b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
1036 p->rc.b_filler = atobool(value);
1038 p->b_pic_struct = atobool(value);
1039 OPT("fake-interlaced")
1040 p->b_fake_interlaced = atobool(value);
1041 OPT("frame-packing")
1042 p->i_frame_packing = atoi(value);
1044 p->b_stitchable = atobool(value);
1046 p->b_opencl = atobool( value );
1048 p->psz_clbin_file = strdup( value );
1049 OPT("opencl-device")
1050 p->i_opencl_device = atoi( value );
1052 return X264_PARAM_BAD_NAME;
1062 b_error |= value_was_null && !name_was_bool;
1063 return b_error ? X264_PARAM_BAD_VALUE : 0;
1066 /****************************************************************************
1068 ****************************************************************************/
1069 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
1071 if( !h || i_level <= h->param.i_log_level )
1074 va_start( arg, psz_fmt );
1076 x264_log_default( NULL, i_level, psz_fmt, arg );
1078 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
1083 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
1088 case X264_LOG_ERROR:
1089 psz_prefix = "error";
1091 case X264_LOG_WARNING:
1092 psz_prefix = "warning";
1095 psz_prefix = "info";
1097 case X264_LOG_DEBUG:
1098 psz_prefix = "debug";
1101 psz_prefix = "unknown";
1104 fprintf( stderr, "x264 [%s]: ", psz_prefix );
1105 x264_vfprintf( stderr, psz_fmt, arg );
1108 /****************************************************************************
1109 * x264_picture_init:
1110 ****************************************************************************/
1111 void x264_picture_init( x264_picture_t *pic )
1113 memset( pic, 0, sizeof( x264_picture_t ) );
1114 pic->i_type = X264_TYPE_AUTO;
1115 pic->i_qpplus1 = X264_QP_AUTO;
1116 pic->i_pic_struct = PIC_STRUCT_AUTO;
1119 /****************************************************************************
1120 * x264_picture_alloc:
1121 ****************************************************************************/
1122 int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
1131 static const x264_csp_tab_t x264_csp_tab[] =
1133 [X264_CSP_I420] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1134 [X264_CSP_YV12] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1135 [X264_CSP_NV12] = { 2, { 256*1, 256*1 }, { 256*1, 256/2 }, },
1136 [X264_CSP_I422] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1137 [X264_CSP_YV16] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1138 [X264_CSP_NV16] = { 2, { 256*1, 256*1 }, { 256*1, 256*1 }, },
1139 [X264_CSP_I444] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1140 [X264_CSP_YV24] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1141 [X264_CSP_BGR] = { 1, { 256*3 }, { 256*1 }, },
1142 [X264_CSP_BGRA] = { 1, { 256*4 }, { 256*1 }, },
1143 [X264_CSP_RGB] = { 1, { 256*3 }, { 256*1 }, },
1146 int csp = i_csp & X264_CSP_MASK;
1147 if( csp <= X264_CSP_NONE || csp >= X264_CSP_MAX )
1149 x264_picture_init( pic );
1150 pic->img.i_csp = i_csp;
1151 pic->img.i_plane = x264_csp_tab[csp].planes;
1152 int depth_factor = i_csp & X264_CSP_HIGH_DEPTH ? 2 : 1;
1153 int plane_offset[3] = {0};
1155 for( int i = 0; i < pic->img.i_plane; i++ )
1157 int stride = (((int64_t)i_width * x264_csp_tab[csp].width_fix8[i]) >> 8) * depth_factor;
1158 int plane_size = (((int64_t)i_height * x264_csp_tab[csp].height_fix8[i]) >> 8) * stride;
1159 pic->img.i_stride[i] = stride;
1160 plane_offset[i] = frame_size;
1161 frame_size += plane_size;
1163 pic->img.plane[0] = x264_malloc( frame_size );
1164 if( !pic->img.plane[0] )
1166 for( int i = 1; i < pic->img.i_plane; i++ )
1167 pic->img.plane[i] = pic->img.plane[0] + plane_offset[i];
1171 /****************************************************************************
1172 * x264_picture_clean:
1173 ****************************************************************************/
1174 void x264_picture_clean( x264_picture_t *pic )
1176 x264_free( pic->img.plane[0] );
1178 /* just to be safe */
1179 memset( pic, 0, sizeof( x264_picture_t ) );
1182 /****************************************************************************
1184 ****************************************************************************/
1185 void *x264_malloc( int i_size )
1187 uint8_t *align_buf = NULL;
1190 #define HUGE_PAGE_SIZE 2*1024*1024
1191 #define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
1192 /* Attempt to allocate huge pages to reduce TLB misses. */
1193 if( i_size >= HUGE_PAGE_THRESHOLD )
1195 align_buf = memalign( HUGE_PAGE_SIZE, i_size );
1198 /* Round up to the next huge page boundary if we are close enough. */
1199 size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
1200 madvise( align_buf, madv_size, MADV_HUGEPAGE );
1204 #undef HUGE_PAGE_SIZE
1205 #undef HUGE_PAGE_THRESHOLD
1207 align_buf = memalign( NATIVE_ALIGN, i_size );
1209 uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
1212 align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
1213 align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
1214 *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
1218 x264_log( NULL, X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
1222 /****************************************************************************
1224 ****************************************************************************/
1225 void x264_free( void *p )
1232 free( *( ( ( void **) p ) - 1 ) );
1237 /****************************************************************************
1238 * x264_reduce_fraction:
1239 ****************************************************************************/
1240 #define REDUCE_FRACTION( name, type )\
1241 void name( type *n, type *d )\
1259 REDUCE_FRACTION( x264_reduce_fraction , uint32_t )
1260 REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
1262 /****************************************************************************
1264 ****************************************************************************/
1265 char *x264_slurp_file( const char *filename )
1270 FILE *fh = x264_fopen( filename, "rb" );
1273 b_error |= fseek( fh, 0, SEEK_END ) < 0;
1274 b_error |= ( i_size = ftell( fh ) ) <= 0;
1275 b_error |= fseek( fh, 0, SEEK_SET ) < 0;
1278 buf = x264_malloc( i_size+2 );
1281 b_error |= fread( buf, 1, i_size, fh ) != i_size;
1282 if( buf[i_size-1] != '\n' )
1283 buf[i_size++] = '\n';
1297 /****************************************************************************
1298 * x264_param2string:
1299 ****************************************************************************/
1300 char *x264_param2string( x264_param_t *p, int b_res )
1304 if( p->rc.psz_zones )
1305 len += strlen(p->rc.psz_zones);
1306 buf = s = x264_malloc( len );
1312 s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
1313 s += sprintf( s, "fps=%u/%u ", p->i_fps_num, p->i_fps_den );
1314 s += sprintf( s, "timebase=%u/%u ", p->i_timebase_num, p->i_timebase_den );
1315 s += sprintf( s, "bitdepth=%d ", BIT_DEPTH );
1319 s += sprintf( s, "opencl=%d ", p->b_opencl );
1320 s += sprintf( s, "cabac=%d", p->b_cabac );
1321 s += sprintf( s, " ref=%d", p->i_frame_reference );
1322 s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
1323 p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
1324 s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
1325 s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
1326 s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
1327 s += sprintf( s, " psy=%d", p->analyse.b_psy );
1328 if( p->analyse.b_psy )
1329 s += sprintf( s, " psy_rd=%.2f:%.2f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis );
1330 s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
1331 s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
1332 s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
1333 s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
1334 s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
1335 s += sprintf( s, " cqm=%d", p->i_cqm_preset );
1336 s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
1337 s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
1338 s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
1339 s += sprintf( s, " threads=%d", p->i_threads );
1340 s += sprintf( s, " lookahead_threads=%d", p->i_lookahead_threads );
1341 s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
1342 if( p->i_slice_count )
1343 s += sprintf( s, " slices=%d", p->i_slice_count );
1344 if( p->i_slice_count_max )
1345 s += sprintf( s, " slices_max=%d", p->i_slice_count_max );
1346 if( p->i_slice_max_size )
1347 s += sprintf( s, " slice_max_size=%d", p->i_slice_max_size );
1348 if( p->i_slice_max_mbs )
1349 s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
1350 if( p->i_slice_min_mbs )
1351 s += sprintf( s, " slice_min_mbs=%d", p->i_slice_min_mbs );
1352 s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
1353 s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
1354 s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : p->b_fake_interlaced ? "fake" : "0" );
1355 s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
1356 if( p->b_stitchable )
1357 s += sprintf( s, " stitchable=%d", p->b_stitchable );
1359 s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1361 s += sprintf( s, " bframes=%d", p->i_bframe );
1364 s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1365 p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1366 p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
1368 s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1370 if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1371 s += sprintf( s, " keyint=infinite" );
1373 s += sprintf( s, " keyint=%d", p->i_keyint_max );
1374 s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1375 p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1377 if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1378 s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1380 s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1381 ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1382 : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1383 if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1385 if( p->rc.i_rc_method == X264_RC_CRF )
1386 s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
1388 s += sprintf( s, " bitrate=%d ratetol=%.1f",
1389 p->rc.i_bitrate, p->rc.f_rate_tolerance );
1390 s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1391 p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
1392 if( p->rc.b_stat_read )
1393 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1394 p->rc.f_complexity_blur, p->rc.f_qblur );
1395 if( p->rc.i_vbv_buffer_size )
1397 s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1398 p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1399 if( p->rc.i_rc_method == X264_RC_CRF )
1400 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1403 else if( p->rc.i_rc_method == X264_RC_CQP )
1404 s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1406 if( p->rc.i_vbv_buffer_size )
1407 s += sprintf( s, " nal_hrd=%s filler=%d", x264_nal_hrd_names[p->i_nal_hrd], p->rc.b_filler );
1408 if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
1409 s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
1410 p->crop_rect.i_right, p->crop_rect.i_bottom );
1411 if( p->i_frame_packing >= 0 )
1412 s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
1414 if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1416 s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1417 if( p->i_bframe && !p->rc.b_mb_tree )
1418 s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1419 s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1420 if( p->rc.i_aq_mode )
1421 s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1422 if( p->rc.psz_zones )
1423 s += sprintf( s, " zones=%s", p->rc.psz_zones );
1424 else if( p->rc.i_zones )
1425 s += sprintf( s, " zones" );