1 /*****************************************************************************
2 * common.c: misc common functions
3 *****************************************************************************
4 * Copyright (C) 2003-2010 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;
170 static int x264_param_apply_preset( x264_param_t *param, const char *preset )
173 int i = strtol( preset, &end, 10 );
174 if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
175 preset = x264_preset_names[i];
177 if( !strcasecmp( preset, "ultrafast" ) )
179 param->i_frame_reference = 1;
180 param->i_scenecut_threshold = 0;
181 param->b_deblocking_filter = 0;
184 param->analyse.intra = 0;
185 param->analyse.inter = 0;
186 param->analyse.b_transform_8x8 = 0;
187 param->analyse.i_me_method = X264_ME_DIA;
188 param->analyse.i_subpel_refine = 0;
189 param->rc.i_aq_mode = 0;
190 param->analyse.b_mixed_references = 0;
191 param->analyse.i_trellis = 0;
192 param->i_bframe_adaptive = X264_B_ADAPT_NONE;
193 param->rc.b_mb_tree = 0;
194 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
195 param->analyse.b_weighted_bipred = 0;
196 param->rc.i_lookahead = 0;
198 else if( !strcasecmp( preset, "superfast" ) )
200 param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
201 param->analyse.i_me_method = X264_ME_DIA;
202 param->analyse.i_subpel_refine = 1;
203 param->i_frame_reference = 1;
204 param->analyse.b_mixed_references = 0;
205 param->analyse.i_trellis = 0;
206 param->rc.b_mb_tree = 0;
207 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
208 param->rc.i_lookahead = 0;
210 else if( !strcasecmp( preset, "veryfast" ) )
212 param->analyse.i_me_method = X264_ME_HEX;
213 param->analyse.i_subpel_refine = 2;
214 param->i_frame_reference = 1;
215 param->analyse.b_mixed_references = 0;
216 param->analyse.i_trellis = 0;
217 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
218 param->rc.i_lookahead = 10;
220 else if( !strcasecmp( preset, "faster" ) )
222 param->analyse.b_mixed_references = 0;
223 param->i_frame_reference = 2;
224 param->analyse.i_subpel_refine = 4;
225 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
226 param->rc.i_lookahead = 20;
228 else if( !strcasecmp( preset, "fast" ) )
230 param->i_frame_reference = 2;
231 param->analyse.i_subpel_refine = 6;
232 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
233 param->rc.i_lookahead = 30;
235 else if( !strcasecmp( preset, "medium" ) )
237 /* Default is medium */
239 else if( !strcasecmp( preset, "slow" ) )
241 param->analyse.i_me_method = X264_ME_UMH;
242 param->analyse.i_subpel_refine = 8;
243 param->i_frame_reference = 5;
244 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
245 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
246 param->rc.i_lookahead = 50;
248 else if( !strcasecmp( preset, "slower" ) )
250 param->analyse.i_me_method = X264_ME_UMH;
251 param->analyse.i_subpel_refine = 9;
252 param->i_frame_reference = 8;
253 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
254 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
255 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
256 param->analyse.i_trellis = 2;
257 param->rc.i_lookahead = 60;
259 else if( !strcasecmp( preset, "veryslow" ) )
261 param->analyse.i_me_method = X264_ME_UMH;
262 param->analyse.i_subpel_refine = 10;
263 param->analyse.i_me_range = 24;
264 param->i_frame_reference = 16;
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;
270 param->rc.i_lookahead = 60;
272 else if( !strcasecmp( preset, "placebo" ) )
274 param->analyse.i_me_method = X264_ME_TESA;
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.b_fast_pskip = 0;
282 param->analyse.i_trellis = 2;
283 param->i_bframe = 16;
284 param->rc.i_lookahead = 60;
288 x264_log( NULL, X264_LOG_ERROR, "invalid preset '%s'\n", preset );
294 static int x264_param_apply_tune( x264_param_t *param, const char *tune )
296 char *tmp = x264_malloc( strlen( tune ) + 1 );
299 tmp = strcpy( tmp, tune );
300 char *s = strtok( tmp, ",./-+" );
301 int psy_tuning_used = 0;
304 if( !strncasecmp( s, "film", 4 ) )
306 if( psy_tuning_used++ ) goto psy_failure;
307 param->i_deblocking_filter_alphac0 = -1;
308 param->i_deblocking_filter_beta = -1;
309 param->analyse.f_psy_trellis = 0.15;
311 else if( !strncasecmp( s, "animation", 9 ) )
313 if( psy_tuning_used++ ) goto psy_failure;
314 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
315 param->i_deblocking_filter_alphac0 = 1;
316 param->i_deblocking_filter_beta = 1;
317 param->analyse.f_psy_rd = 0.4;
318 param->rc.f_aq_strength = 0.6;
319 param->i_bframe += 2;
321 else if( !strncasecmp( s, "grain", 5 ) )
323 if( psy_tuning_used++ ) goto psy_failure;
324 param->i_deblocking_filter_alphac0 = -2;
325 param->i_deblocking_filter_beta = -2;
326 param->analyse.f_psy_trellis = 0.25;
327 param->analyse.b_dct_decimate = 0;
328 param->rc.f_pb_factor = 1.1;
329 param->rc.f_ip_factor = 1.1;
330 param->rc.f_aq_strength = 0.5;
331 param->analyse.i_luma_deadzone[0] = 6;
332 param->analyse.i_luma_deadzone[1] = 6;
333 param->rc.f_qcompress = 0.8;
335 else if( !strncasecmp( s, "stillimage", 5 ) )
337 if( psy_tuning_used++ ) goto psy_failure;
338 param->i_deblocking_filter_alphac0 = -3;
339 param->i_deblocking_filter_beta = -3;
340 param->analyse.f_psy_rd = 2.0;
341 param->analyse.f_psy_trellis = 0.7;
342 param->rc.f_aq_strength = 1.2;
344 else if( !strncasecmp( s, "psnr", 4 ) )
346 if( psy_tuning_used++ ) goto psy_failure;
347 param->rc.i_aq_mode = X264_AQ_NONE;
348 param->analyse.b_psy = 0;
350 else if( !strncasecmp( s, "ssim", 4 ) )
352 if( psy_tuning_used++ ) goto psy_failure;
353 param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
354 param->analyse.b_psy = 0;
356 else if( !strncasecmp( s, "fastdecode", 10 ) )
358 param->b_deblocking_filter = 0;
360 param->analyse.b_weighted_bipred = 0;
361 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
363 else if( !strncasecmp( s, "zerolatency", 11 ) )
365 param->rc.i_lookahead = 0;
366 param->i_sync_lookahead = 0;
368 param->b_sliced_threads = 1;
369 param->b_vfr_input = 0;
370 param->rc.b_mb_tree = 0;
372 else if( !strncasecmp( s, "touhou", 6 ) )
374 if( psy_tuning_used++ ) goto psy_failure;
375 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
376 param->i_deblocking_filter_alphac0 = -1;
377 param->i_deblocking_filter_beta = -1;
378 param->analyse.f_psy_trellis = 0.2;
379 param->rc.f_aq_strength = 1.3;
380 if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
381 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
385 x264_log( NULL, X264_LOG_ERROR, "invalid tune '%s'\n", s );
392 x264_log( NULL, X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %s\n", s );
394 s = strtok( NULL, ",./-+" );
400 int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
402 x264_param_default( param );
404 if( preset && x264_param_apply_preset( param, preset ) < 0 )
406 if( tune && x264_param_apply_tune( param, tune ) < 0 )
411 void x264_param_apply_fastfirstpass( x264_param_t *param )
413 /* Set faster options in case of turbo firstpass. */
414 if( param->rc.b_stat_write && !param->rc.b_stat_read )
416 param->i_frame_reference = 1;
417 param->analyse.b_transform_8x8 = 0;
418 param->analyse.inter = 0;
419 param->analyse.i_me_method = X264_ME_DIA;
420 param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
421 param->analyse.i_trellis = 0;
422 param->analyse.b_fast_pskip = 1;
426 int x264_param_apply_profile( x264_param_t *param, const char *profile )
432 if( !strcasecmp( profile, "baseline" ) || !strcasecmp( profile, "main" ) ||
433 !strcasecmp( profile, "high" ) )
435 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d.\n", profile, BIT_DEPTH );
440 if( !strcasecmp( profile, "baseline" ) )
442 param->analyse.b_transform_8x8 = 0;
444 param->i_cqm_preset = X264_CQM_FLAT;
446 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
447 if( param->b_interlaced )
449 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
452 if( param->b_fake_interlaced )
454 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
458 else if( !strcasecmp( profile, "main" ) )
460 param->analyse.b_transform_8x8 = 0;
461 param->i_cqm_preset = X264_CQM_FLAT;
463 else if( !strcasecmp( profile, "high" ) || !strcasecmp( profile, "high10" ) )
469 x264_log( NULL, X264_LOG_ERROR, "invalid profile: %s\n", profile );
472 if( (param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
473 (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0) )
475 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
481 static int parse_enum( const char *arg, const char * const *names, int *dst )
483 for( int i = 0; names[i]; i++ )
484 if( !strcmp( arg, names[i] ) )
492 static int parse_cqm( const char *str, uint8_t *cqm, int length )
497 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
500 } while( i < length && (str = strchr( str, ',' )) && str++ );
501 return (i == length) ? 0 : -1;
504 static int x264_atobool( const char *str, int *b_error )
506 if( !strcmp(str, "1") ||
507 !strcmp(str, "true") ||
508 !strcmp(str, "yes") )
510 if( !strcmp(str, "0") ||
511 !strcmp(str, "false") ||
518 static int x264_atoi( const char *str, int *b_error )
521 int v = strtol( str, &end, 0 );
522 if( end == str || *end != '\0' )
527 static double x264_atof( const char *str, int *b_error )
530 double v = strtod( str, &end );
531 if( end == str || *end != '\0' )
536 #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
537 #define atoi(str) x264_atoi( str, &b_error )
538 #define atof(str) x264_atof( str, &b_error )
540 int x264_param_parse( x264_param_t *p, const char *name, const char *value )
542 char *name_buf = NULL;
545 int value_was_null = !value;
549 return X264_PARAM_BAD_NAME;
553 if( value[0] == '=' )
556 if( strchr( name, '_' ) ) // s/_/-/g
559 name_buf = strdup(name);
560 while( (c = strchr( name_buf, '_' )) )
565 if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
566 (!strncmp( name, "no", 2 ) && (i = 2)) )
569 value = atobool(value) ? "false" : "true";
573 #define OPT(STR) else if( !strcmp( name, STR ) )
574 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
578 p->cpu = isdigit(value[0]) ? atoi(value) :
579 !strcmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
582 char *buf = strdup(value);
583 char *tok, UNUSED *saveptr=NULL, *init;
586 for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
588 for( i=0; x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name); i++ );
589 p->cpu |= x264_cpu_names[i].flags;
590 if( !x264_cpu_names[i].flags )
598 if( !strcmp(value, "auto") )
599 p->i_threads = X264_THREADS_AUTO;
601 p->i_threads = atoi(value);
603 OPT("sliced-threads")
604 p->b_sliced_threads = atobool(value);
605 OPT("sync-lookahead")
607 if( !strcmp(value, "auto") )
608 p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
610 p->i_sync_lookahead = atoi(value);
612 OPT2("deterministic", "n-deterministic")
613 p->b_deterministic = atobool(value);
614 OPT2("level", "level-idc")
616 if( !strcmp(value, "1b") )
618 else if( atof(value) < 6 )
619 p->i_level_idc = (int)(10*atof(value)+.5);
621 p->i_level_idc = atoi(value);
625 b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
626 2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
629 b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
631 b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
633 b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
635 b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
637 b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
639 b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
642 p->vui.i_chroma_loc = atoi(value);
643 b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
647 if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) == 2 )
651 float fps = atof(value);
652 p->i_fps_num = (int)(fps * 1000 + .5);
656 OPT2("ref", "frameref")
657 p->i_frame_reference = atoi(value);
659 p->i_dpb_size = atoi(value);
662 if( strstr( value, "infinite" ) )
663 p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
665 p->i_keyint_max = atoi(value);
667 OPT2("min-keyint", "keyint-min")
669 p->i_keyint_min = atoi(value);
670 if( p->i_keyint_max < p->i_keyint_min )
671 p->i_keyint_max = p->i_keyint_min;
675 p->i_scenecut_threshold = atobool(value);
676 if( b_error || p->i_scenecut_threshold )
679 p->i_scenecut_threshold = atoi(value);
683 p->b_intra_refresh = atobool(value);
685 p->i_bframe = atoi(value);
688 p->i_bframe_adaptive = atobool(value);
692 p->i_bframe_adaptive = atoi(value);
696 p->i_bframe_bias = atoi(value);
699 b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
703 p->i_bframe_pyramid = atoi(value);
708 b_error |= parse_enum( value, x264_open_gop_names, &p->i_open_gop );
712 p->i_open_gop = atoi(value);
716 p->b_deblocking_filter = !atobool(value);
717 OPT2("filter", "deblock")
719 if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
720 2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
722 p->b_deblocking_filter = 1;
724 else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
726 p->b_deblocking_filter = 1;
727 p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
730 p->b_deblocking_filter = atobool(value);
732 OPT("slice-max-size")
733 p->i_slice_max_size = atoi(value);
735 p->i_slice_max_mbs = atoi(value);
737 p->i_slice_count = atoi(value);
739 p->b_cabac = atobool(value);
741 p->i_cabac_init_idc = atoi(value);
743 p->b_interlaced = atobool(value);
745 p->b_interlaced = p->b_tff = atobool(value);
748 p->b_interlaced = atobool(value);
749 p->b_tff = !p->b_interlaced;
751 OPT("constrained-intra")
752 p->b_constrained_intra = atobool(value);
755 if( strstr( value, "flat" ) )
756 p->i_cqm_preset = X264_CQM_FLAT;
757 else if( strstr( value, "jvt" ) )
758 p->i_cqm_preset = X264_CQM_JVT;
760 p->psz_cqm_file = strdup(value);
763 p->psz_cqm_file = strdup(value);
766 p->i_cqm_preset = X264_CQM_CUSTOM;
767 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
768 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
769 b_error |= parse_cqm( value, p->cqm_4py, 16 );
770 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
774 p->i_cqm_preset = X264_CQM_CUSTOM;
775 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
776 b_error |= parse_cqm( value, p->cqm_8py, 64 );
780 p->i_cqm_preset = X264_CQM_CUSTOM;
781 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
782 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
786 p->i_cqm_preset = X264_CQM_CUSTOM;
787 b_error |= parse_cqm( value, p->cqm_4py, 16 );
788 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
792 p->i_cqm_preset = X264_CQM_CUSTOM;
793 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
797 p->i_cqm_preset = X264_CQM_CUSTOM;
798 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
802 p->i_cqm_preset = X264_CQM_CUSTOM;
803 b_error |= parse_cqm( value, p->cqm_4py, 16 );
807 p->i_cqm_preset = X264_CQM_CUSTOM;
808 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
812 p->i_cqm_preset = X264_CQM_CUSTOM;
813 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
817 p->i_cqm_preset = X264_CQM_CUSTOM;
818 b_error |= parse_cqm( value, p->cqm_8py, 64 );
821 p->i_log_level = atoi(value);
824 p->b_visualize = atobool(value);
827 p->psz_dump_yuv = strdup(value);
828 OPT2("analyse", "partitions")
830 p->analyse.inter = 0;
831 if( strstr( value, "none" ) ) p->analyse.inter = 0;
832 if( strstr( value, "all" ) ) p->analyse.inter = ~0;
834 if( strstr( value, "i4x4" ) ) p->analyse.inter |= X264_ANALYSE_I4x4;
835 if( strstr( value, "i8x8" ) ) p->analyse.inter |= X264_ANALYSE_I8x8;
836 if( strstr( value, "p8x8" ) ) p->analyse.inter |= X264_ANALYSE_PSUB16x16;
837 if( strstr( value, "p4x4" ) ) p->analyse.inter |= X264_ANALYSE_PSUB8x8;
838 if( strstr( value, "b8x8" ) ) p->analyse.inter |= X264_ANALYSE_BSUB16x16;
841 p->analyse.b_transform_8x8 = atobool(value);
842 OPT2("weightb", "weight-b")
843 p->analyse.b_weighted_bipred = atobool(value);
845 p->analyse.i_weighted_pred = atoi(value);
846 OPT2("direct", "direct-pred")
847 b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
848 OPT("chroma-qp-offset")
849 p->analyse.i_chroma_qp_offset = atoi(value);
851 b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
852 OPT2("merange", "me-range")
853 p->analyse.i_me_range = atoi(value);
854 OPT2("mvrange", "mv-range")
855 p->analyse.i_mv_range = atoi(value);
856 OPT2("mvrange-thread", "mv-range-thread")
857 p->analyse.i_mv_range_thread = atoi(value);
858 OPT2("subme", "subq")
859 p->analyse.i_subpel_refine = atoi(value);
862 if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
863 2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
864 2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
866 else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
868 p->analyse.f_psy_trellis = 0;
872 p->analyse.f_psy_rd = 0;
873 p->analyse.f_psy_trellis = 0;
877 p->analyse.b_psy = atobool(value);
879 p->analyse.b_chroma_me = atobool(value);
881 p->analyse.b_mixed_references = atobool(value);
883 p->analyse.i_trellis = atoi(value);
885 p->analyse.b_fast_pskip = atobool(value);
887 p->analyse.b_dct_decimate = atobool(value);
888 OPT("deadzone-inter")
889 p->analyse.i_luma_deadzone[0] = atoi(value);
890 OPT("deadzone-intra")
891 p->analyse.i_luma_deadzone[1] = atoi(value);
893 p->analyse.i_noise_reduction = atoi(value);
896 p->rc.i_bitrate = atoi(value);
897 p->rc.i_rc_method = X264_RC_ABR;
899 OPT2("qp", "qp_constant")
901 p->rc.i_qp_constant = atoi(value);
902 p->rc.i_rc_method = X264_RC_CQP;
906 p->rc.f_rf_constant = atof(value);
907 p->rc.i_rc_method = X264_RC_CRF;
910 p->rc.f_rf_constant_max = atof(value);
912 p->rc.i_lookahead = atoi(value);
913 OPT2("qpmin", "qp-min")
914 p->rc.i_qp_min = atoi(value);
915 OPT2("qpmax", "qp-max")
916 p->rc.i_qp_max = atoi(value);
917 OPT2("qpstep", "qp-step")
918 p->rc.i_qp_step = atoi(value);
920 p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
922 p->rc.i_vbv_max_bitrate = atoi(value);
924 p->rc.i_vbv_buffer_size = atoi(value);
926 p->rc.f_vbv_buffer_init = atof(value);
927 OPT2("ipratio", "ip-factor")
928 p->rc.f_ip_factor = atof(value);
929 OPT2("pbratio", "pb-factor")
930 p->rc.f_pb_factor = atof(value);
932 p->rc.i_aq_mode = atoi(value);
934 p->rc.f_aq_strength = atof(value);
937 int pass = x264_clip3( atoi(value), 0, 3 );
938 p->rc.b_stat_write = pass & 1;
939 p->rc.b_stat_read = pass & 2;
943 p->rc.psz_stat_in = strdup(value);
944 p->rc.psz_stat_out = strdup(value);
947 p->rc.f_qcompress = atof(value);
949 p->rc.b_mb_tree = atobool(value);
951 p->rc.f_qblur = atof(value);
952 OPT2("cplxblur", "cplx-blur")
953 p->rc.f_complexity_blur = atof(value);
955 p->rc.psz_zones = strdup(value);
957 b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
958 &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
960 p->analyse.b_psnr = atobool(value);
962 p->analyse.b_ssim = atobool(value);
964 p->b_aud = atobool(value);
966 p->i_sps_id = atoi(value);
968 p->b_repeat_headers = !atobool(value);
969 OPT("repeat-headers")
970 p->b_repeat_headers = atobool(value);
972 p->b_annexb = atobool(value);
974 p->b_vfr_input = !atobool(value);
976 b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
978 p->b_pic_struct = atobool(value);
979 OPT("fake-interlaced")
980 p->b_fake_interlaced = atobool(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_MINGW && 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_MINGW && 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" );
1241 s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1243 s += sprintf( s, " bframes=%d", p->i_bframe );
1246 s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1247 p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1248 p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->i_open_gop );
1250 s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1252 if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1253 s += sprintf( s, " keyint=infinite" );
1255 s += sprintf( s, " keyint=%d", p->i_keyint_max );
1256 s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1257 p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1259 if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1260 s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1262 s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1263 ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1264 : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1265 if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1267 if( p->rc.i_rc_method == X264_RC_CRF )
1268 s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
1270 s += sprintf( s, " bitrate=%d ratetol=%.1f",
1271 p->rc.i_bitrate, p->rc.f_rate_tolerance );
1272 s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1273 p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
1274 if( p->rc.b_stat_read )
1275 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1276 p->rc.f_complexity_blur, p->rc.f_qblur );
1277 if( p->rc.i_vbv_buffer_size )
1279 s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1280 p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1281 if( p->rc.i_rc_method == X264_RC_CRF )
1282 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1285 else if( p->rc.i_rc_method == X264_RC_CQP )
1286 s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1287 if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1289 s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1290 if( p->i_bframe && !p->rc.b_mb_tree )
1291 s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1292 s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1293 if( p->rc.i_aq_mode )
1294 s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1295 if( p->rc.psz_zones )
1296 s += sprintf( s, " zones=%s", p->rc.psz_zones );
1297 else if( p->rc.i_zones )
1298 s += sprintf( s, " zones" );
1301 if( p->rc.i_vbv_buffer_size )
1302 s += sprintf( s, " nal_hrd=%s", x264_nal_hrd_names[p->i_nal_hrd] );