1 /*****************************************************************************
2 * common.c: misc common functions
3 *****************************************************************************
4 * Copyright (C) 2003-2011 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 *****************************************************************************/
36 const int x264_bit_depth = BIT_DEPTH;
38 static void x264_log_default( void *, int, const char *, va_list );
40 /****************************************************************************
42 ****************************************************************************/
43 void x264_param_default( x264_param_t *param )
46 memset( param, 0, sizeof( x264_param_t ) );
49 param->cpu = x264_cpu_detect();
50 param->i_threads = X264_THREADS_AUTO;
51 param->b_deterministic = 1;
52 param->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
54 /* Video properties */
55 param->i_csp = X264_CSP_I420;
58 param->vui.i_sar_width = 0;
59 param->vui.i_sar_height= 0;
60 param->vui.i_overscan = 0; /* undef */
61 param->vui.i_vidformat = 5; /* undef */
62 param->vui.b_fullrange = 0; /* off */
63 param->vui.i_colorprim = 2; /* undef */
64 param->vui.i_transfer = 2; /* undef */
65 param->vui.i_colmatrix = 2; /* undef */
66 param->vui.i_chroma_loc= 0; /* left center */
67 param->i_fps_num = 25;
69 param->i_level_idc = -1;
70 param->i_slice_max_size = 0;
71 param->i_slice_max_mbs = 0;
72 param->i_slice_count = 0;
74 /* Encoder parameters */
75 param->i_frame_reference = 3;
76 param->i_keyint_max = 250;
77 param->i_keyint_min = X264_KEYINT_MIN_AUTO;
79 param->i_scenecut_threshold = 40;
80 param->i_bframe_adaptive = X264_B_ADAPT_FAST;
81 param->i_bframe_bias = 0;
82 param->i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
83 param->b_interlaced = 0;
84 param->b_constrained_intra = 0;
86 param->b_deblocking_filter = 1;
87 param->i_deblocking_filter_alphac0 = 0;
88 param->i_deblocking_filter_beta = 0;
91 param->i_cabac_init_idc = 0;
93 param->rc.i_rc_method = X264_RC_CRF;
94 param->rc.i_bitrate = 0;
95 param->rc.f_rate_tolerance = 1.0;
96 param->rc.i_vbv_max_bitrate = 0;
97 param->rc.i_vbv_buffer_size = 0;
98 param->rc.f_vbv_buffer_init = 0.9;
99 param->rc.i_qp_constant = 23 + QP_BD_OFFSET;
100 param->rc.f_rf_constant = 23;
101 param->rc.i_qp_min = 0;
102 param->rc.i_qp_max = QP_MAX;
103 param->rc.i_qp_step = 4;
104 param->rc.f_ip_factor = 1.4;
105 param->rc.f_pb_factor = 1.3;
106 param->rc.i_aq_mode = X264_AQ_VARIANCE;
107 param->rc.f_aq_strength = 1.0;
108 param->rc.i_lookahead = 40;
110 param->rc.b_stat_write = 0;
111 param->rc.psz_stat_out = "x264_2pass.log";
112 param->rc.b_stat_read = 0;
113 param->rc.psz_stat_in = "x264_2pass.log";
114 param->rc.f_qcompress = 0.6;
115 param->rc.f_qblur = 0.5;
116 param->rc.f_complexity_blur = 20;
117 param->rc.i_zones = 0;
118 param->rc.b_mb_tree = 1;
121 param->pf_log = x264_log_default;
122 param->p_log_private = NULL;
123 param->i_log_level = X264_LOG_INFO;
126 param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
127 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
128 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
129 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
130 param->analyse.i_me_method = X264_ME_HEX;
131 param->analyse.f_psy_rd = 1.0;
132 param->analyse.b_psy = 1;
133 param->analyse.f_psy_trellis = 0;
134 param->analyse.i_me_range = 16;
135 param->analyse.i_subpel_refine = 7;
136 param->analyse.b_mixed_references = 1;
137 param->analyse.b_chroma_me = 1;
138 param->analyse.i_mv_range_thread = -1;
139 param->analyse.i_mv_range = -1; // set from level_idc
140 param->analyse.i_chroma_qp_offset = 0;
141 param->analyse.b_fast_pskip = 1;
142 param->analyse.b_weighted_bipred = 1;
143 param->analyse.i_weighted_pred = X264_WEIGHTP_SMART;
144 param->analyse.b_dct_decimate = 1;
145 param->analyse.b_transform_8x8 = 1;
146 param->analyse.i_trellis = 1;
147 param->analyse.i_luma_deadzone[0] = 21;
148 param->analyse.i_luma_deadzone[1] = 11;
149 param->analyse.b_psnr = 0;
150 param->analyse.b_ssim = 0;
152 param->i_cqm_preset = X264_CQM_FLAT;
153 memset( param->cqm_4iy, 16, sizeof( param->cqm_4iy ) );
154 memset( param->cqm_4ic, 16, sizeof( param->cqm_4ic ) );
155 memset( param->cqm_4py, 16, sizeof( param->cqm_4py ) );
156 memset( param->cqm_4pc, 16, sizeof( param->cqm_4pc ) );
157 memset( param->cqm_8iy, 16, sizeof( param->cqm_8iy ) );
158 memset( param->cqm_8py, 16, sizeof( param->cqm_8py ) );
160 param->b_repeat_headers = 1;
163 param->b_vfr_input = 1;
164 param->i_nal_hrd = X264_NAL_HRD_NONE;
166 param->b_pic_struct = 0;
167 param->b_fake_interlaced = 0;
168 param->i_frame_packing = -1;
171 static int x264_param_apply_preset( x264_param_t *param, const char *preset )
174 int i = strtol( preset, &end, 10 );
175 if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
176 preset = x264_preset_names[i];
178 if( !strcasecmp( preset, "ultrafast" ) )
180 param->i_frame_reference = 1;
181 param->i_scenecut_threshold = 0;
182 param->b_deblocking_filter = 0;
185 param->analyse.intra = 0;
186 param->analyse.inter = 0;
187 param->analyse.b_transform_8x8 = 0;
188 param->analyse.i_me_method = X264_ME_DIA;
189 param->analyse.i_subpel_refine = 0;
190 param->rc.i_aq_mode = 0;
191 param->analyse.b_mixed_references = 0;
192 param->analyse.i_trellis = 0;
193 param->i_bframe_adaptive = X264_B_ADAPT_NONE;
194 param->rc.b_mb_tree = 0;
195 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
196 param->analyse.b_weighted_bipred = 0;
197 param->rc.i_lookahead = 0;
199 else if( !strcasecmp( preset, "superfast" ) )
201 param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
202 param->analyse.i_me_method = X264_ME_DIA;
203 param->analyse.i_subpel_refine = 1;
204 param->i_frame_reference = 1;
205 param->analyse.b_mixed_references = 0;
206 param->analyse.i_trellis = 0;
207 param->rc.b_mb_tree = 0;
208 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
209 param->rc.i_lookahead = 0;
211 else if( !strcasecmp( preset, "veryfast" ) )
213 param->analyse.i_me_method = X264_ME_HEX;
214 param->analyse.i_subpel_refine = 2;
215 param->i_frame_reference = 1;
216 param->analyse.b_mixed_references = 0;
217 param->analyse.i_trellis = 0;
218 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
219 param->rc.i_lookahead = 10;
221 else if( !strcasecmp( preset, "faster" ) )
223 param->analyse.b_mixed_references = 0;
224 param->i_frame_reference = 2;
225 param->analyse.i_subpel_refine = 4;
226 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
227 param->rc.i_lookahead = 20;
229 else if( !strcasecmp( preset, "fast" ) )
231 param->i_frame_reference = 2;
232 param->analyse.i_subpel_refine = 6;
233 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
234 param->rc.i_lookahead = 30;
236 else if( !strcasecmp( preset, "medium" ) )
238 /* Default is medium */
240 else if( !strcasecmp( preset, "slow" ) )
242 param->analyse.i_me_method = X264_ME_UMH;
243 param->analyse.i_subpel_refine = 8;
244 param->i_frame_reference = 5;
245 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
246 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
247 param->rc.i_lookahead = 50;
249 else if( !strcasecmp( preset, "slower" ) )
251 param->analyse.i_me_method = X264_ME_UMH;
252 param->analyse.i_subpel_refine = 9;
253 param->i_frame_reference = 8;
254 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
255 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
256 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
257 param->analyse.i_trellis = 2;
258 param->rc.i_lookahead = 60;
260 else if( !strcasecmp( preset, "veryslow" ) )
262 param->analyse.i_me_method = X264_ME_UMH;
263 param->analyse.i_subpel_refine = 10;
264 param->analyse.i_me_range = 24;
265 param->i_frame_reference = 16;
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;
271 param->rc.i_lookahead = 60;
273 else if( !strcasecmp( preset, "placebo" ) )
275 param->analyse.i_me_method = X264_ME_TESA;
276 param->analyse.i_subpel_refine = 10;
277 param->analyse.i_me_range = 24;
278 param->i_frame_reference = 16;
279 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
280 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
281 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
282 param->analyse.b_fast_pskip = 0;
283 param->analyse.i_trellis = 2;
284 param->i_bframe = 16;
285 param->rc.i_lookahead = 60;
289 x264_log( NULL, X264_LOG_ERROR, "invalid preset '%s'\n", preset );
295 static int x264_param_apply_tune( x264_param_t *param, const char *tune )
297 char *tmp = x264_malloc( strlen( tune ) + 1 );
300 tmp = strcpy( tmp, tune );
301 char *s = strtok( tmp, ",./-+" );
302 int psy_tuning_used = 0;
305 if( !strncasecmp( s, "film", 4 ) )
307 if( psy_tuning_used++ ) goto psy_failure;
308 param->i_deblocking_filter_alphac0 = -1;
309 param->i_deblocking_filter_beta = -1;
310 param->analyse.f_psy_trellis = 0.15;
312 else if( !strncasecmp( s, "animation", 9 ) )
314 if( psy_tuning_used++ ) goto psy_failure;
315 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
316 param->i_deblocking_filter_alphac0 = 1;
317 param->i_deblocking_filter_beta = 1;
318 param->analyse.f_psy_rd = 0.4;
319 param->rc.f_aq_strength = 0.6;
320 param->i_bframe += 2;
322 else if( !strncasecmp( s, "grain", 5 ) )
324 if( psy_tuning_used++ ) goto psy_failure;
325 param->i_deblocking_filter_alphac0 = -2;
326 param->i_deblocking_filter_beta = -2;
327 param->analyse.f_psy_trellis = 0.25;
328 param->analyse.b_dct_decimate = 0;
329 param->rc.f_pb_factor = 1.1;
330 param->rc.f_ip_factor = 1.1;
331 param->rc.f_aq_strength = 0.5;
332 param->analyse.i_luma_deadzone[0] = 6;
333 param->analyse.i_luma_deadzone[1] = 6;
334 param->rc.f_qcompress = 0.8;
336 else if( !strncasecmp( s, "stillimage", 5 ) )
338 if( psy_tuning_used++ ) goto psy_failure;
339 param->i_deblocking_filter_alphac0 = -3;
340 param->i_deblocking_filter_beta = -3;
341 param->analyse.f_psy_rd = 2.0;
342 param->analyse.f_psy_trellis = 0.7;
343 param->rc.f_aq_strength = 1.2;
345 else if( !strncasecmp( s, "psnr", 4 ) )
347 if( psy_tuning_used++ ) goto psy_failure;
348 param->rc.i_aq_mode = X264_AQ_NONE;
349 param->analyse.b_psy = 0;
351 else if( !strncasecmp( s, "ssim", 4 ) )
353 if( psy_tuning_used++ ) goto psy_failure;
354 param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
355 param->analyse.b_psy = 0;
357 else if( !strncasecmp( s, "fastdecode", 10 ) )
359 param->b_deblocking_filter = 0;
361 param->analyse.b_weighted_bipred = 0;
362 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
364 else if( !strncasecmp( s, "zerolatency", 11 ) )
366 param->rc.i_lookahead = 0;
367 param->i_sync_lookahead = 0;
369 param->b_sliced_threads = 1;
370 param->b_vfr_input = 0;
371 param->rc.b_mb_tree = 0;
373 else if( !strncasecmp( s, "touhou", 6 ) )
375 if( psy_tuning_used++ ) goto psy_failure;
376 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
377 param->i_deblocking_filter_alphac0 = -1;
378 param->i_deblocking_filter_beta = -1;
379 param->analyse.f_psy_trellis = 0.2;
380 param->rc.f_aq_strength = 1.3;
381 if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
382 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
386 x264_log( NULL, X264_LOG_ERROR, "invalid tune '%s'\n", s );
393 x264_log( NULL, X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %s\n", s );
395 s = strtok( NULL, ",./-+" );
401 int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
403 x264_param_default( param );
405 if( preset && x264_param_apply_preset( param, preset ) < 0 )
407 if( tune && x264_param_apply_tune( param, tune ) < 0 )
412 void x264_param_apply_fastfirstpass( x264_param_t *param )
414 /* Set faster options in case of turbo firstpass. */
415 if( param->rc.b_stat_write && !param->rc.b_stat_read )
417 param->i_frame_reference = 1;
418 param->analyse.b_transform_8x8 = 0;
419 param->analyse.inter = 0;
420 param->analyse.i_me_method = X264_ME_DIA;
421 param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
422 param->analyse.i_trellis = 0;
423 param->analyse.b_fast_pskip = 1;
427 int x264_param_apply_profile( x264_param_t *param, const char *profile )
433 if( !strcasecmp( profile, "baseline" ) || !strcasecmp( profile, "main" ) ||
434 !strcasecmp( profile, "high" ) )
436 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d.\n", profile, BIT_DEPTH );
441 if( !strcasecmp( profile, "baseline" ) )
443 param->analyse.b_transform_8x8 = 0;
445 param->i_cqm_preset = X264_CQM_FLAT;
446 param->psz_cqm_file = NULL;
448 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
449 if( param->b_interlaced )
451 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
454 if( param->b_fake_interlaced )
456 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
460 else if( !strcasecmp( profile, "main" ) )
462 param->analyse.b_transform_8x8 = 0;
463 param->i_cqm_preset = X264_CQM_FLAT;
464 param->psz_cqm_file = NULL;
466 else if( !strcasecmp( profile, "high" ) || !strcasecmp( profile, "high10" ) )
472 x264_log( NULL, X264_LOG_ERROR, "invalid profile: %s\n", profile );
475 if( (param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
476 (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0) )
478 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
484 static int parse_enum( const char *arg, const char * const *names, int *dst )
486 for( int i = 0; names[i]; i++ )
487 if( !strcmp( arg, names[i] ) )
495 static int parse_cqm( const char *str, uint8_t *cqm, int length )
500 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
503 } while( i < length && (str = strchr( str, ',' )) && str++ );
504 return (i == length) ? 0 : -1;
507 static int x264_atobool( const char *str, int *b_error )
509 if( !strcmp(str, "1") ||
510 !strcmp(str, "true") ||
511 !strcmp(str, "yes") )
513 if( !strcmp(str, "0") ||
514 !strcmp(str, "false") ||
521 static int x264_atoi( const char *str, int *b_error )
524 int v = strtol( str, &end, 0 );
525 if( end == str || *end != '\0' )
530 static double x264_atof( const char *str, int *b_error )
533 double v = strtod( str, &end );
534 if( end == str || *end != '\0' )
539 #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
540 #define atoi(str) x264_atoi( str, &b_error )
541 #define atof(str) x264_atof( str, &b_error )
543 int x264_param_parse( x264_param_t *p, const char *name, const char *value )
545 char *name_buf = NULL;
548 int value_was_null = !value;
552 return X264_PARAM_BAD_NAME;
556 if( value[0] == '=' )
559 if( strchr( name, '_' ) ) // s/_/-/g
562 name_buf = strdup(name);
563 while( (c = strchr( name_buf, '_' )) )
568 if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
569 (!strncmp( name, "no", 2 ) && (i = 2)) )
572 value = atobool(value) ? "false" : "true";
576 #define OPT(STR) else if( !strcmp( name, STR ) )
577 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
581 p->cpu = isdigit(value[0]) ? atoi(value) :
582 !strcmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
585 char *buf = strdup(value);
586 char *tok, UNUSED *saveptr=NULL, *init;
589 for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
591 for( i=0; x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name); i++ );
592 p->cpu |= x264_cpu_names[i].flags;
593 if( !x264_cpu_names[i].flags )
601 if( !strcmp(value, "auto") )
602 p->i_threads = X264_THREADS_AUTO;
604 p->i_threads = atoi(value);
606 OPT("sliced-threads")
607 p->b_sliced_threads = atobool(value);
608 OPT("sync-lookahead")
610 if( !strcmp(value, "auto") )
611 p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
613 p->i_sync_lookahead = atoi(value);
615 OPT2("deterministic", "n-deterministic")
616 p->b_deterministic = atobool(value);
617 OPT2("level", "level-idc")
619 if( !strcmp(value, "1b") )
621 else if( atof(value) < 6 )
622 p->i_level_idc = (int)(10*atof(value)+.5);
624 p->i_level_idc = atoi(value);
627 p->b_bluray_compat = atobool(value);
630 b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
631 2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
634 b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
636 b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
638 b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
640 b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
642 b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
644 b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
647 p->vui.i_chroma_loc = atoi(value);
648 b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
652 if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) == 2 )
656 float fps = atof(value);
657 p->i_fps_num = (int)(fps * 1000 + .5);
661 OPT2("ref", "frameref")
662 p->i_frame_reference = atoi(value);
664 p->i_dpb_size = atoi(value);
667 if( strstr( value, "infinite" ) )
668 p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
670 p->i_keyint_max = atoi(value);
672 OPT2("min-keyint", "keyint-min")
674 p->i_keyint_min = atoi(value);
675 if( p->i_keyint_max < p->i_keyint_min )
676 p->i_keyint_max = p->i_keyint_min;
680 p->i_scenecut_threshold = atobool(value);
681 if( b_error || p->i_scenecut_threshold )
684 p->i_scenecut_threshold = atoi(value);
688 p->b_intra_refresh = atobool(value);
690 p->i_bframe = atoi(value);
693 p->i_bframe_adaptive = atobool(value);
697 p->i_bframe_adaptive = atoi(value);
701 p->i_bframe_bias = atoi(value);
704 b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
708 p->i_bframe_pyramid = atoi(value);
712 p->b_open_gop = atobool(value);
714 p->b_deblocking_filter = !atobool(value);
715 OPT2("filter", "deblock")
717 if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
718 2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
720 p->b_deblocking_filter = 1;
722 else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
724 p->b_deblocking_filter = 1;
725 p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
728 p->b_deblocking_filter = atobool(value);
730 OPT("slice-max-size")
731 p->i_slice_max_size = atoi(value);
733 p->i_slice_max_mbs = atoi(value);
735 p->i_slice_count = atoi(value);
737 p->b_cabac = atobool(value);
739 p->i_cabac_init_idc = atoi(value);
741 p->b_interlaced = atobool(value);
743 p->b_interlaced = p->b_tff = atobool(value);
746 p->b_interlaced = atobool(value);
747 p->b_tff = !p->b_interlaced;
749 OPT("constrained-intra")
750 p->b_constrained_intra = atobool(value);
753 if( strstr( value, "flat" ) )
754 p->i_cqm_preset = X264_CQM_FLAT;
755 else if( strstr( value, "jvt" ) )
756 p->i_cqm_preset = X264_CQM_JVT;
758 p->psz_cqm_file = strdup(value);
761 p->psz_cqm_file = strdup(value);
764 p->i_cqm_preset = X264_CQM_CUSTOM;
765 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
766 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
767 b_error |= parse_cqm( value, p->cqm_4py, 16 );
768 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
772 p->i_cqm_preset = X264_CQM_CUSTOM;
773 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
774 b_error |= parse_cqm( value, p->cqm_8py, 64 );
778 p->i_cqm_preset = X264_CQM_CUSTOM;
779 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
780 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
784 p->i_cqm_preset = X264_CQM_CUSTOM;
785 b_error |= parse_cqm( value, p->cqm_4py, 16 );
786 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
790 p->i_cqm_preset = X264_CQM_CUSTOM;
791 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
795 p->i_cqm_preset = X264_CQM_CUSTOM;
796 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
800 p->i_cqm_preset = X264_CQM_CUSTOM;
801 b_error |= parse_cqm( value, p->cqm_4py, 16 );
805 p->i_cqm_preset = X264_CQM_CUSTOM;
806 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
810 p->i_cqm_preset = X264_CQM_CUSTOM;
811 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
815 p->i_cqm_preset = X264_CQM_CUSTOM;
816 b_error |= parse_cqm( value, p->cqm_8py, 64 );
819 p->i_log_level = atoi(value);
822 p->b_visualize = atobool(value);
825 p->psz_dump_yuv = strdup(value);
826 OPT2("analyse", "partitions")
828 p->analyse.inter = 0;
829 if( strstr( value, "none" ) ) p->analyse.inter = 0;
830 if( strstr( value, "all" ) ) p->analyse.inter = ~0;
832 if( strstr( value, "i4x4" ) ) p->analyse.inter |= X264_ANALYSE_I4x4;
833 if( strstr( value, "i8x8" ) ) p->analyse.inter |= X264_ANALYSE_I8x8;
834 if( strstr( value, "p8x8" ) ) p->analyse.inter |= X264_ANALYSE_PSUB16x16;
835 if( strstr( value, "p4x4" ) ) p->analyse.inter |= X264_ANALYSE_PSUB8x8;
836 if( strstr( value, "b8x8" ) ) p->analyse.inter |= X264_ANALYSE_BSUB16x16;
839 p->analyse.b_transform_8x8 = atobool(value);
840 OPT2("weightb", "weight-b")
841 p->analyse.b_weighted_bipred = atobool(value);
843 p->analyse.i_weighted_pred = atoi(value);
844 OPT2("direct", "direct-pred")
845 b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
846 OPT("chroma-qp-offset")
847 p->analyse.i_chroma_qp_offset = atoi(value);
849 b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
850 OPT2("merange", "me-range")
851 p->analyse.i_me_range = atoi(value);
852 OPT2("mvrange", "mv-range")
853 p->analyse.i_mv_range = atoi(value);
854 OPT2("mvrange-thread", "mv-range-thread")
855 p->analyse.i_mv_range_thread = atoi(value);
856 OPT2("subme", "subq")
857 p->analyse.i_subpel_refine = atoi(value);
860 if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
861 2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
862 2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
864 else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
866 p->analyse.f_psy_trellis = 0;
870 p->analyse.f_psy_rd = 0;
871 p->analyse.f_psy_trellis = 0;
875 p->analyse.b_psy = atobool(value);
877 p->analyse.b_chroma_me = atobool(value);
879 p->analyse.b_mixed_references = atobool(value);
881 p->analyse.i_trellis = atoi(value);
883 p->analyse.b_fast_pskip = atobool(value);
885 p->analyse.b_dct_decimate = atobool(value);
886 OPT("deadzone-inter")
887 p->analyse.i_luma_deadzone[0] = atoi(value);
888 OPT("deadzone-intra")
889 p->analyse.i_luma_deadzone[1] = atoi(value);
891 p->analyse.i_noise_reduction = atoi(value);
894 p->rc.i_bitrate = atoi(value);
895 p->rc.i_rc_method = X264_RC_ABR;
897 OPT2("qp", "qp_constant")
899 p->rc.i_qp_constant = atoi(value);
900 p->rc.i_rc_method = X264_RC_CQP;
904 p->rc.f_rf_constant = atof(value);
905 p->rc.i_rc_method = X264_RC_CRF;
908 p->rc.f_rf_constant_max = atof(value);
910 p->rc.i_lookahead = atoi(value);
911 OPT2("qpmin", "qp-min")
912 p->rc.i_qp_min = atoi(value);
913 OPT2("qpmax", "qp-max")
914 p->rc.i_qp_max = atoi(value);
915 OPT2("qpstep", "qp-step")
916 p->rc.i_qp_step = atoi(value);
918 p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
920 p->rc.i_vbv_max_bitrate = atoi(value);
922 p->rc.i_vbv_buffer_size = atoi(value);
924 p->rc.f_vbv_buffer_init = atof(value);
925 OPT2("ipratio", "ip-factor")
926 p->rc.f_ip_factor = atof(value);
927 OPT2("pbratio", "pb-factor")
928 p->rc.f_pb_factor = atof(value);
930 p->rc.i_aq_mode = atoi(value);
932 p->rc.f_aq_strength = atof(value);
935 int pass = x264_clip3( atoi(value), 0, 3 );
936 p->rc.b_stat_write = pass & 1;
937 p->rc.b_stat_read = pass & 2;
941 p->rc.psz_stat_in = strdup(value);
942 p->rc.psz_stat_out = strdup(value);
945 p->rc.f_qcompress = atof(value);
947 p->rc.b_mb_tree = atobool(value);
949 p->rc.f_qblur = atof(value);
950 OPT2("cplxblur", "cplx-blur")
951 p->rc.f_complexity_blur = atof(value);
953 p->rc.psz_zones = strdup(value);
955 b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
956 &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
958 p->analyse.b_psnr = atobool(value);
960 p->analyse.b_ssim = atobool(value);
962 p->b_aud = atobool(value);
964 p->i_sps_id = atoi(value);
966 p->b_repeat_headers = !atobool(value);
967 OPT("repeat-headers")
968 p->b_repeat_headers = atobool(value);
970 p->b_annexb = atobool(value);
972 p->b_vfr_input = !atobool(value);
974 b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
976 p->b_pic_struct = atobool(value);
977 OPT("fake-interlaced")
978 p->b_fake_interlaced = atobool(value);
980 p->i_frame_packing = atoi(value);
982 return X264_PARAM_BAD_NAME;
992 b_error |= value_was_null && !name_was_bool;
993 return b_error ? X264_PARAM_BAD_VALUE : 0;
996 /****************************************************************************
998 ****************************************************************************/
999 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
1001 if( !h || i_level <= h->param.i_log_level )
1004 va_start( arg, psz_fmt );
1006 x264_log_default( NULL, i_level, psz_fmt, arg );
1008 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
1013 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
1018 case X264_LOG_ERROR:
1019 psz_prefix = "error";
1021 case X264_LOG_WARNING:
1022 psz_prefix = "warning";
1025 psz_prefix = "info";
1027 case X264_LOG_DEBUG:
1028 psz_prefix = "debug";
1031 psz_prefix = "unknown";
1034 fprintf( stderr, "x264 [%s]: ", psz_prefix );
1035 vfprintf( stderr, psz_fmt, arg );
1038 /****************************************************************************
1039 * x264_picture_init:
1040 ****************************************************************************/
1041 void x264_picture_init( x264_picture_t *pic )
1043 memset( pic, 0, sizeof( x264_picture_t ) );
1044 pic->i_type = X264_TYPE_AUTO;
1045 pic->i_qpplus1 = X264_QP_AUTO;
1046 pic->i_pic_struct = PIC_STRUCT_AUTO;
1049 /****************************************************************************
1050 * x264_picture_alloc:
1051 ****************************************************************************/
1052 int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
1054 int csp = i_csp & X264_CSP_MASK;
1055 if( csp <= X264_CSP_NONE || csp >= X264_CSP_MAX )
1057 x264_picture_init( pic );
1058 pic->img.i_csp = i_csp;
1059 pic->img.i_plane = csp == X264_CSP_NV12 ? 2 : 3;
1060 int depth_factor = i_csp & X264_CSP_HIGH_DEPTH ? 2 : 1;
1061 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 * depth_factor );
1062 if( !pic->img.plane[0] )
1064 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height * depth_factor;
1065 if( csp != X264_CSP_NV12 )
1066 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 4 * depth_factor;
1067 pic->img.i_stride[0] = i_width * depth_factor;
1068 if( csp == X264_CSP_NV12 )
1069 pic->img.i_stride[1] = i_width * depth_factor;
1072 pic->img.i_stride[1] = i_width / 2 * depth_factor;
1073 pic->img.i_stride[2] = i_width / 2 * depth_factor;
1078 /****************************************************************************
1079 * x264_picture_clean:
1080 ****************************************************************************/
1081 void x264_picture_clean( x264_picture_t *pic )
1083 x264_free( pic->img.plane[0] );
1085 /* just to be safe */
1086 memset( pic, 0, sizeof( x264_picture_t ) );
1089 /****************************************************************************
1091 ****************************************************************************/
1092 void *x264_malloc( int i_size )
1094 uint8_t *align_buf = NULL;
1095 #if SYS_MACOSX || (SYS_WINDOWS && ARCH_X86_64)
1096 /* Mac OS X and Win x64 always returns 16 byte aligned memory */
1097 align_buf = malloc( i_size );
1099 align_buf = memalign( 16, i_size );
1101 uint8_t *buf = malloc( i_size + 15 + sizeof(void **) );
1104 align_buf = buf + 15 + sizeof(void **);
1105 align_buf -= (intptr_t) align_buf & 15;
1106 *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
1110 x264_log( NULL, X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
1114 /****************************************************************************
1116 ****************************************************************************/
1117 void x264_free( void *p )
1121 #if HAVE_MALLOC_H || SYS_MACOSX || (SYS_WINDOWS && ARCH_X86_64)
1124 free( *( ( ( void **) p ) - 1 ) );
1129 /****************************************************************************
1130 * x264_reduce_fraction:
1131 ****************************************************************************/
1132 #define REDUCE_FRACTION( name, type )\
1133 void name( type *n, type *d )\
1151 REDUCE_FRACTION( x264_reduce_fraction , uint32_t )
1152 REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
1154 /****************************************************************************
1156 ****************************************************************************/
1157 char *x264_slurp_file( const char *filename )
1162 FILE *fh = fopen( filename, "rb" );
1165 b_error |= fseek( fh, 0, SEEK_END ) < 0;
1166 b_error |= ( i_size = ftell( fh ) ) <= 0;
1167 b_error |= fseek( fh, 0, SEEK_SET ) < 0;
1170 buf = x264_malloc( i_size+2 );
1173 b_error |= fread( buf, 1, i_size, fh ) != i_size;
1174 if( buf[i_size-1] != '\n' )
1175 buf[i_size++] = '\n';
1189 /****************************************************************************
1190 * x264_param2string:
1191 ****************************************************************************/
1192 char *x264_param2string( x264_param_t *p, int b_res )
1196 if( p->rc.psz_zones )
1197 len += strlen(p->rc.psz_zones);
1198 buf = s = x264_malloc( len );
1204 s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
1205 s += sprintf( s, "fps=%u/%u ", p->i_fps_num, p->i_fps_den );
1206 s += sprintf( s, "timebase=%u/%u ", p->i_timebase_num, p->i_timebase_den );
1207 s += sprintf( s, "bitdepth=%d ", BIT_DEPTH );
1210 s += sprintf( s, "cabac=%d", p->b_cabac );
1211 s += sprintf( s, " ref=%d", p->i_frame_reference );
1212 s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
1213 p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
1214 s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
1215 s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
1216 s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
1217 s += sprintf( s, " psy=%d", p->analyse.b_psy );
1218 if( p->analyse.b_psy )
1219 s += sprintf( s, " psy_rd=%.2f:%.2f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis );
1220 s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
1221 s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
1222 s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
1223 s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
1224 s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
1225 s += sprintf( s, " cqm=%d", p->i_cqm_preset );
1226 s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
1227 s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
1228 s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
1229 s += sprintf( s, " threads=%d", p->i_threads );
1230 s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
1231 if( p->i_slice_count )
1232 s += sprintf( s, " slices=%d", p->i_slice_count );
1233 if( p->i_slice_max_size )
1234 s += sprintf( s, " slice_max_size=%d", p->i_slice_max_size );
1235 if( p->i_slice_max_mbs )
1236 s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
1237 s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
1238 s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
1239 s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : p->b_fake_interlaced ? "fake" : "0" );
1240 s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
1242 s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1244 s += sprintf( s, " bframes=%d", p->i_bframe );
1247 s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1248 p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1249 p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
1251 s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1253 if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1254 s += sprintf( s, " keyint=infinite" );
1256 s += sprintf( s, " keyint=%d", p->i_keyint_max );
1257 s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1258 p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1260 if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1261 s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1263 s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1264 ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1265 : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1266 if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1268 if( p->rc.i_rc_method == X264_RC_CRF )
1269 s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
1271 s += sprintf( s, " bitrate=%d ratetol=%.1f",
1272 p->rc.i_bitrate, p->rc.f_rate_tolerance );
1273 s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1274 p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
1275 if( p->rc.b_stat_read )
1276 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1277 p->rc.f_complexity_blur, p->rc.f_qblur );
1278 if( p->rc.i_vbv_buffer_size )
1280 s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1281 p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1282 if( p->rc.i_rc_method == X264_RC_CRF )
1283 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1286 else if( p->rc.i_rc_method == X264_RC_CQP )
1287 s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1289 if( p->rc.i_vbv_buffer_size )
1290 s += sprintf( s, " nal_hrd=%s", x264_nal_hrd_names[p->i_nal_hrd] );
1291 if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
1292 s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
1293 p->crop_rect.i_right, p->crop_rect.i_bottom );
1294 if( p->i_frame_packing >= 0 )
1295 s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
1297 if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1299 s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1300 if( p->i_bframe && !p->rc.b_mb_tree )
1301 s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1302 s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1303 if( p->rc.i_aq_mode )
1304 s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1305 if( p->rc.psz_zones )
1306 s += sprintf( s, " zones=%s", p->rc.psz_zones );
1307 else if( p->rc.i_zones )
1308 s += sprintf( s, " zones" );