K-means weightp
[x264-7mod.git] / common / common.c
blob56e536614fc6ecd6a1c0ea93f63bce2b49be99b1
1 /*****************************************************************************
2 * common.c: misc common functions
3 *****************************************************************************
4 * Copyright (C) 2003-2017 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 *****************************************************************************/
27 #include "common.h"
29 #include <ctype.h>
31 #if HAVE_MALLOC_H
32 #include <malloc.h>
33 #endif
34 #if HAVE_THP
35 #include <sys/mman.h>
36 #endif
38 const int x264_bit_depth = BIT_DEPTH;
40 const int x264_chroma_format = X264_CHROMA_FORMAT;
42 static void x264_log_default( void *, int, const char *, va_list );
43 static void x264_log_file( char *, int, const char *, va_list );
45 /****************************************************************************
46 * x264_param_default:
47 ****************************************************************************/
48 void x264_param_default( x264_param_t *param )
50 /* */
51 memset( param, 0, sizeof( x264_param_t ) );
53 /* CPU autodetect */
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;
62 param->i_width = 0;
63 param->i_height = 0;
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;
74 param->i_fps_den = 1;
75 param->i_level_idc = -1;
76 param->b_level_force = 0;
77 param->i_profile = 0;
78 param->b_profile_force = 0;
79 param->i_slice_max_size = 0;
80 param->i_slice_max_mbs = 0;
81 param->i_slice_count = 0;
83 /* Encoder parameters */
84 param->i_frame_reference = 3;
85 param->i_keyint_max = 250;
86 param->i_keyint_min = X264_KEYINT_MIN_AUTO;
87 param->i_bframe = 3;
88 param->i_scenecut_threshold = 40;
89 param->i_bframe_adaptive = X264_B_ADAPT_FAST;
90 param->i_bframe_bias = 0;
91 param->i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
92 param->b_interlaced = 0;
93 param->b_constrained_intra = 0;
95 param->b_deblocking_filter = 1;
96 param->i_deblocking_filter_alphac0 = 0;
97 param->i_deblocking_filter_beta = 0;
99 param->b_cabac = 1;
100 param->i_cabac_init_idc = 0;
102 param->rc.i_rc_method = X264_RC_CRF;
103 param->rc.i_bitrate = 0;
104 param->rc.f_rate_tolerance = 1.0;
105 param->rc.i_vbv_max_bitrate = 0;
106 param->rc.i_vbv_buffer_size = 0;
107 param->rc.f_vbv_buffer_init = 0.9;
108 param->rc.i_qp_constant = 23 + QP_BD_OFFSET;
109 param->rc.f_rf_constant = 23;
110 param->rc.i_qp_min_min =
111 param->rc.i_qp_min[SLICE_TYPE_I] =
112 param->rc.i_qp_min[SLICE_TYPE_P] =
113 param->rc.i_qp_min[SLICE_TYPE_B] = 0;
114 param->rc.i_qp_max_max =
115 param->rc.i_qp_max[SLICE_TYPE_I] =
116 param->rc.i_qp_max[SLICE_TYPE_P] =
117 param->rc.i_qp_max[SLICE_TYPE_B] = QP_MAX;
118 param->rc.i_qp_step = 4;
119 param->rc.f_ip_factor = 1.4;
120 param->rc.f_pb_factor = 1.3;
121 param->rc.i_aq_mode = X264_AQ_MIX;
122 param->rc.f_aq_strength = 1.0;
123 param->rc.f_aq_sensitivity = 10;
124 param->rc.f_aq_ifactor = 1.0;
125 param->rc.f_aq_pfactor = 1.0;
126 param->rc.f_aq_bfactor = 1.0;
127 param->rc.b_aq2 = 0;
128 param->rc.f_aq2_strength = 0.0;
129 param->rc.f_aq2_sensitivity = 15.0;
130 param->rc.f_aq2_ifactor = 1.0;
131 param->rc.f_aq2_pfactor = 1.0;
132 param->rc.f_aq2_bfactor = 1.0;
133 param->rc.i_aq3_mode = X264_AQ_NONE;
134 param->rc.f_aq3_strength = 0.5;
135 param->rc.f_aq3_strengths[0][0] = 0;
136 param->rc.f_aq3_strengths[0][1] = 0;
137 param->rc.f_aq3_strengths[0][2] = 0;
138 param->rc.f_aq3_strengths[0][3] = 0;
139 param->rc.f_aq3_strengths[1][0] = 0;
140 param->rc.f_aq3_strengths[1][1] = 0;
141 param->rc.f_aq3_strengths[1][2] = 0;
142 param->rc.f_aq3_strengths[1][3] = 0;
143 param->rc.f_aq3_sensitivity = 10;
144 param->rc.f_aq3_ifactor[0] = 1.0;
145 param->rc.f_aq3_ifactor[1] = 1.0;
146 param->rc.f_aq3_pfactor[0] = 1.0;
147 param->rc.f_aq3_pfactor[1] = 1.0;
148 param->rc.f_aq3_bfactor[0] = 1.0;
149 param->rc.f_aq3_bfactor[1] = 1.0;
150 param->rc.b_aq3_boundary = 0;
151 param->rc.i_aq3_boundary[0] = 192;
152 param->rc.i_aq3_boundary[1] = 64;
153 param->rc.i_aq3_boundary[2] = 24;
154 param->rc.i_lookahead = 40;
156 param->rc.b_stat_write = 0;
157 param->rc.psz_stat_out = "x264_2pass.log";
158 param->rc.b_stat_read = 0;
159 param->rc.psz_stat_in = "x264_2pass.log";
160 param->rc.f_qcompress = 0.6;
161 param->rc.f_qblur = 0.5;
162 param->rc.f_complexity_blur = 20;
163 param->rc.i_zones = 0;
164 param->rc.b_mb_tree = 1;
166 /* Log */
167 param->pf_log = x264_log_default;
168 param->p_log_private = NULL;
169 param->i_log_level = X264_LOG_INFO;
170 param->i_log_file_level = X264_LOG_INFO;
171 param->b_stylish = 0;
173 /* */
174 param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
175 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
176 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
177 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
178 param->analyse.i_me_method = X264_ME_HEX;
179 param->analyse.f_psy_rd = 1.0;
180 param->analyse.b_psy = 1;
181 param->analyse.f_psy_trellis = 0;
182 param->analyse.i_fgo = 0;
183 param->analyse.i_me_range = 16;
184 param->analyse.i_subpel_refine = 7;
185 param->analyse.b_mixed_references = 1;
186 param->analyse.b_chroma_me = 1;
187 param->analyse.i_mv_range_thread = -1;
188 param->analyse.i_mv_range = -1; // set from level_idc
189 param->analyse.i_chroma_qp_offset = 0;
190 param->analyse.b_fast_pskip = 1;
191 param->analyse.b_weighted_bipred = 1;
192 param->analyse.i_weighted_pred = X264_WEIGHTP_SMART;
193 param->analyse.b_dct_decimate = 1;
194 param->analyse.b_transform_8x8 = 1;
195 param->analyse.i_trellis = 1;
196 param->analyse.i_luma_deadzone[0] = 21;
197 param->analyse.i_luma_deadzone[1] = 11;
198 param->analyse.b_psnr = 0;
199 param->analyse.b_ssim = 0;
201 param->i_cqm_preset = X264_CQM_FLAT;
202 memset( param->cqm_4iy, 16, sizeof( param->cqm_4iy ) );
203 memset( param->cqm_4py, 16, sizeof( param->cqm_4py ) );
204 memset( param->cqm_4ic, 16, sizeof( param->cqm_4ic ) );
205 memset( param->cqm_4pc, 16, sizeof( param->cqm_4pc ) );
206 memset( param->cqm_8iy, 16, sizeof( param->cqm_8iy ) );
207 memset( param->cqm_8py, 16, sizeof( param->cqm_8py ) );
208 memset( param->cqm_8ic, 16, sizeof( param->cqm_8ic ) );
209 memset( param->cqm_8pc, 16, sizeof( param->cqm_8pc ) );
211 param->b_repeat_headers = 1;
212 param->b_annexb = 1;
213 param->b_aud = 0;
214 param->b_vfr_input = 1;
215 param->i_nal_hrd = X264_NAL_HRD_NONE;
216 param->b_tff = 1;
217 param->b_pic_struct = 0;
218 param->b_fake_interlaced = 0;
219 param->i_frame_packing = -1;
220 param->b_opencl = 0;
221 param->i_opencl_device = 0;
222 param->opencl_device_id = NULL;
223 param->psz_clbin_file = NULL;
225 param->filters.b_sub = 0;
227 param->i_opts_write = X264_OPTS_FULL;
228 for( int i = 0; i < X264_OPTS_MAX; i++ )
229 param->psz_opts[i] = NULL;
232 static int x264_param_apply_preset( x264_param_t *param, const char *preset )
234 char *end;
235 int i = strtol( preset, &end, 10 );
236 if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
237 preset = x264_preset_names[i];
239 if( !strcasecmp( preset, "ultrafast" ) )
241 param->i_frame_reference = 1;
242 param->i_scenecut_threshold = 0;
243 param->b_deblocking_filter = 0;
244 param->b_cabac = 0;
245 param->i_bframe = 0;
246 param->analyse.intra = 0;
247 param->analyse.inter = 0;
248 param->analyse.b_transform_8x8 = 0;
249 param->analyse.i_me_method = X264_ME_DIA;
250 param->analyse.i_subpel_refine = 0;
251 param->rc.i_aq_mode = 0;
252 param->analyse.b_mixed_references = 0;
253 param->analyse.i_trellis = 0;
254 param->i_bframe_adaptive = X264_B_ADAPT_NONE;
255 param->rc.b_mb_tree = 0;
256 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
257 param->analyse.b_weighted_bipred = 0;
258 param->rc.i_lookahead = 0;
260 else if( !strcasecmp( preset, "superfast" ) )
262 param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
263 param->analyse.i_me_method = X264_ME_DIA;
264 param->analyse.i_subpel_refine = 1;
265 param->i_frame_reference = 1;
266 param->analyse.b_mixed_references = 0;
267 param->analyse.i_trellis = 0;
268 param->rc.b_mb_tree = 0;
269 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
270 param->rc.i_lookahead = 0;
272 else if( !strcasecmp( preset, "veryfast" ) )
274 param->analyse.i_subpel_refine = 2;
275 param->i_frame_reference = 1;
276 param->analyse.b_mixed_references = 0;
277 param->analyse.i_trellis = 0;
278 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
279 param->rc.i_lookahead = 10;
281 else if( !strcasecmp( preset, "faster" ) )
283 param->analyse.b_mixed_references = 0;
284 param->i_frame_reference = 2;
285 param->analyse.i_subpel_refine = 4;
286 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
287 param->rc.i_lookahead = 20;
289 else if( !strcasecmp( preset, "fast" ) )
291 param->i_frame_reference = 2;
292 param->analyse.i_subpel_refine = 6;
293 param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
294 param->rc.i_lookahead = 30;
296 else if( !strcasecmp( preset, "medium" ) )
298 /* Default is medium */
300 else if( !strcasecmp( preset, "slow" ) )
302 param->analyse.i_subpel_refine = 8;
303 param->i_frame_reference = 5;
304 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
305 param->analyse.i_trellis = 2;
306 param->rc.i_lookahead = 50;
308 else if( !strcasecmp( preset, "slower" ) )
310 param->analyse.i_me_method = X264_ME_UMH;
311 param->analyse.i_subpel_refine = 9;
312 param->i_frame_reference = 8;
313 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
314 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
315 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
316 param->analyse.i_trellis = 2;
317 param->rc.i_lookahead = 60;
319 else if( !strcasecmp( preset, "veryslow" ) )
321 param->analyse.i_me_method = X264_ME_UMH;
322 param->analyse.i_subpel_refine = 10;
323 param->analyse.i_me_range = 24;
324 param->i_frame_reference = 16;
325 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
326 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
327 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
328 param->analyse.i_trellis = 2;
329 param->i_bframe = 8;
330 param->rc.i_lookahead = 60;
332 else if( !strcasecmp( preset, "placebo" ) )
334 param->analyse.i_me_method = X264_ME_TESA;
335 param->analyse.i_subpel_refine = 11;
336 param->analyse.i_me_range = 24;
337 param->i_frame_reference = 16;
338 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
339 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
340 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
341 param->analyse.b_fast_pskip = 0;
342 param->analyse.i_trellis = 2;
343 param->i_bframe = 16;
344 param->rc.i_lookahead = 60;
346 else
348 x264_log( NULL, X264_LOG_ERROR, "invalid preset '%s'\n", preset );
349 return -1;
351 return 0;
354 static int x264_param_apply_tune( x264_param_t *param, const char *tune )
356 char *tmp = x264_malloc( strlen( tune ) + 1 );
357 if( !tmp )
358 return -1;
359 tmp = strcpy( tmp, tune );
360 char *s = strtok( tmp, ",./-+" );
361 int psy_tuning_used = 0;
362 while( s )
364 if( !strncasecmp( s, "film", 4 ) )
366 if( psy_tuning_used++ ) goto psy_failure;
367 param->i_deblocking_filter_alphac0 = -1;
368 param->i_deblocking_filter_beta = -1;
369 param->analyse.f_psy_trellis = 0.15;
371 else if( !strncasecmp( s, "animation", 9 ) )
373 if( psy_tuning_used++ ) goto psy_failure;
374 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
375 param->i_deblocking_filter_alphac0 = 1;
376 param->i_deblocking_filter_beta = 1;
377 param->analyse.f_psy_rd = 0.4;
378 param->rc.f_aq_strength = 0.6;
379 param->i_bframe += 2;
381 else if( !strncasecmp( s, "grain", 5 ) )
383 if( psy_tuning_used++ ) goto psy_failure;
384 param->i_deblocking_filter_alphac0 = -2;
385 param->i_deblocking_filter_beta = -2;
386 param->analyse.f_psy_trellis = 0.25;
387 param->analyse.b_dct_decimate = 0;
388 param->rc.f_pb_factor = 1.1;
389 param->rc.f_ip_factor = 1.1;
390 param->rc.f_aq_strength = 0.5;
391 param->analyse.i_luma_deadzone[0] = 6;
392 param->analyse.i_luma_deadzone[1] = 6;
393 param->rc.f_qcompress = 0.8;
395 else if( !strncasecmp( s, "stillimage", 10 ) )
397 if( psy_tuning_used++ ) goto psy_failure;
398 param->i_deblocking_filter_alphac0 = -3;
399 param->i_deblocking_filter_beta = -3;
400 param->analyse.f_psy_rd = 2.0;
401 param->analyse.f_psy_trellis = 0.7;
402 param->rc.f_aq_strength = 1.2;
404 else if( !strncasecmp( s, "psnr", 4 ) )
406 if( psy_tuning_used++ ) goto psy_failure;
407 param->rc.i_aq_mode = X264_AQ_NONE;
408 param->analyse.b_psy = 0;
410 else if( !strncasecmp( s, "ssim", 4 ) )
412 if( psy_tuning_used++ ) goto psy_failure;
413 param->rc.i_aq_mode = X264_AQ_AUTOMIX;
414 param->analyse.b_psy = 0;
416 else if( !strncasecmp( s, "fastdecode", 10 ) )
418 param->b_deblocking_filter = 0;
419 param->b_cabac = 0;
420 param->analyse.b_weighted_bipred = 0;
421 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
423 else if( !strncasecmp( s, "zerolatency", 11 ) )
425 param->rc.i_lookahead = 0;
426 param->i_sync_lookahead = 0;
427 param->i_bframe = 0;
428 param->b_sliced_threads = 1;
429 param->b_vfr_input = 0;
430 param->rc.b_mb_tree = 0;
432 else if( !strncasecmp( s, "touhou", 6 ) )
434 if( psy_tuning_used++ ) goto psy_failure;
435 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
436 param->i_deblocking_filter_alphac0 = -1;
437 param->i_deblocking_filter_beta = -1;
438 param->analyse.f_psy_trellis = 0.2;
439 param->rc.f_aq_strength = 1.3;
440 if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
441 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
443 else
445 x264_log( NULL, X264_LOG_ERROR, "invalid tune '%s'\n", s );
446 x264_free( tmp );
447 return -1;
449 if( 0 )
451 psy_failure:
452 x264_log( NULL, X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %s\n", s );
454 s = strtok( NULL, ",./-+" );
456 x264_free( tmp );
457 return 0;
460 int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
462 x264_param_default( param );
464 if( preset && x264_param_apply_preset( param, preset ) < 0 )
465 return -1;
466 if( tune && x264_param_apply_tune( param, tune ) < 0 )
467 return -1;
468 return 0;
471 void x264_param_apply_fastfirstpass( x264_param_t *param )
473 /* Set faster options in case of turbo firstpass. */
474 if( param->rc.b_stat_write && !param->rc.b_stat_read )
476 param->i_frame_reference = 1;
477 param->analyse.b_transform_8x8 = 0;
478 param->analyse.inter = 0;
479 param->analyse.i_me_method = X264_ME_DIA;
480 param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
481 param->analyse.i_trellis = 0;
482 param->analyse.b_fast_pskip = 1;
483 param->analyse.i_fgo = 0;
487 static int profile_string_to_int( const char *str )
489 if( !strcasecmp( str, "baseline" ) )
490 return PROFILE_BASELINE;
491 if( !strcasecmp( str, "main" ) )
492 return PROFILE_MAIN;
493 if( !strcasecmp( str, "high" ) )
494 return PROFILE_HIGH;
495 if( !strcasecmp( str, "high10" ) )
496 return PROFILE_HIGH10;
497 if( !strcasecmp( str, "high422" ) )
498 return PROFILE_HIGH422;
499 if( !strcasecmp( str, "high444" ) )
500 return PROFILE_HIGH444_PREDICTIVE;
501 return -1;
504 static int x264_generic_device_check( x264_param_t *param, const char *device, int device_mask )
506 if( (device_mask & X264_DEVICE_LEVEL_FREE) == 0 )
507 param->b_level_force = 1;
509 if( (device_mask & X264_DEVICE_LOSSLESS) == 0 && (
510 (param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
511 (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0)
515 x264_log( NULL, X264_LOG_ERROR, "%s doesn't support lossless\n", device );
516 return -1;
519 if( (device_mask & X264_DEVICE_444) == 0 && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I444 )
521 x264_log( NULL, X264_LOG_ERROR, "%s doesn't support 4:4:4\n", device );
522 return -1;
525 if( (device_mask & X264_DEVICE_422) == 0 && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I422 )
527 x264_log( NULL, X264_LOG_ERROR, "%s doesn't support 4:2:2\n", device );
528 return -1;
531 if( (device_mask & X264_DEVICE_HIGH_DEPTH) == 0 && BIT_DEPTH > 8 )
533 x264_log( NULL, X264_LOG_ERROR, "%s doesn't support a bit depth of %d\n", device, BIT_DEPTH );
534 return -1;
537 return 0;
540 static int x264_param_restrict_device( x264_param_t *param, int i_profile, const char *device )
542 char *tmp = x264_malloc( strlen( device ) + 1 );
543 if( !tmp )
544 return -1;
545 tmp = strcpy( tmp, device );
546 char *s = strtok( tmp, ",./-+" );
547 while( s )
549 if( !strcasecmp( s, "dxva" ) )
551 if( x264_generic_device_check( param, s, X264_DEVICE_DXVA ) < 0 )
553 x264_free( tmp );
554 return -1;
556 param->i_level_idc = ( param->i_level_idc < 0 ) ?
557 X264_LEVEL_IDC_DXVA : X264_MIN( param->i_level_idc, X264_LEVEL_IDC_DXVA );
559 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_HIGH ) : PROFILE_HIGH;
561 else if( !strcasecmp( s, "bluray" ) || !strcasecmp( s, "blu-ray" ) )
563 if( x264_generic_device_check( param, s, X264_DEVICE_BLURAY ) < 0 )
565 x264_free( tmp );
566 return -1;
568 param->i_level_idc = ( param->i_level_idc < 0 ) ?
569 X264_LEVEL_IDC_BLURAY : X264_MIN( param->i_level_idc, X264_LEVEL_IDC_BLURAY );
570 param->b_bluray_compat = 1;
572 param->rc.i_vbv_max_bitrate = ( param->rc.i_vbv_max_bitrate <= 0 ) ?
573 X264_VBV_MAXRATE_BLURAY : X264_MIN( param->rc.i_vbv_max_bitrate, X264_VBV_MAXRATE_BLURAY );
575 param->rc.i_vbv_buffer_size = ( param->rc.i_vbv_buffer_size <= 0 ) ?
576 X264_VBV_BUFSIZE_BLURAY : X264_MIN( param->rc.i_vbv_buffer_size, X264_VBV_BUFSIZE_BLURAY );
578 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_HIGH ) : PROFILE_HIGH;
580 else if( !strcasecmp( s, "psp" ) )
582 if( x264_generic_device_check( param, s, X264_DEVICE_PSP ) < 0 )
584 x264_free( tmp );
585 return -1;
587 param->i_frame_reference = X264_MIN( param->i_frame_reference, 3 );
588 param->i_bframe_pyramid = X264_B_PYRAMID_NONE;
589 param->analyse.i_weighted_pred = X264_MIN( param->analyse.i_weighted_pred, X264_WEIGHTP_SIMPLE );
591 param->rc.i_vbv_max_bitrate = ( param->rc.i_vbv_max_bitrate <= 0 ) ?
592 X264_VBV_MAXRATE_PSP : X264_MIN( param->rc.i_vbv_max_bitrate, X264_VBV_MAXRATE_PSP );
594 param->rc.i_vbv_buffer_size = ( param->rc.i_vbv_buffer_size <= 0 ) ?
595 X264_VBV_BUFSIZE_PSP : X264_MIN( param->rc.i_vbv_buffer_size, X264_VBV_BUFSIZE_PSP );
597 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_MAIN ) : PROFILE_MAIN;
599 else if( !strcasecmp( s, "psv" ) )
601 if( x264_generic_device_check( param, s, X264_DEVICE_PSV ) < 0 )
603 x264_free( tmp );
604 return -1;
607 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_HIGH ) : PROFILE_HIGH;
609 else if( !strcasecmp( s, "ps3" ) )
611 if( x264_generic_device_check( param, s, X264_DEVICE_PS3 ) < 0 )
613 x264_free( tmp );
614 return -1;
616 param->i_level_idc = ( param->i_level_idc < 0 ) ?
617 X264_LEVEL_IDC_PS3 : X264_MIN( param->i_level_idc, X264_LEVEL_IDC_PS3 );
619 param->rc.i_vbv_max_bitrate = ( param->rc.i_vbv_max_bitrate <= 0 ) ?
620 X264_VBV_MAXRATE_PS3 : X264_MIN( param->rc.i_vbv_max_bitrate, X264_VBV_MAXRATE_PS3 );
622 param->rc.i_vbv_buffer_size = ( param->rc.i_vbv_buffer_size <= 0 ) ?
623 X264_VBV_BUFSIZE_PS3 : X264_MIN( param->rc.i_vbv_buffer_size, X264_VBV_BUFSIZE_PS3 );
625 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_HIGH ) : PROFILE_HIGH;
627 else if( !strcasecmp( s, "xbox" ) || !strcasecmp( s, "xbox360" ) )
629 if( x264_generic_device_check( param, s, X264_DEVICE_XBOX ) < 0 )
631 x264_free( tmp );
632 return -1;
634 param->i_level_idc = ( param->i_level_idc < 0 ) ?
635 X264_LEVEL_IDC_XBOX : X264_MIN( param->i_level_idc, X264_LEVEL_IDC_XBOX );
637 param->rc.i_vbv_max_bitrate = ( param->rc.i_vbv_max_bitrate <= 0 ) ?
638 X264_VBV_MAXRATE_XBOX : X264_MIN( param->rc.i_vbv_max_bitrate, X264_VBV_MAXRATE_XBOX );
640 param->rc.i_vbv_buffer_size = ( param->rc.i_vbv_buffer_size <= 0 ) ?
641 X264_VBV_BUFSIZE_XBOX : X264_MIN( param->rc.i_vbv_buffer_size, X264_VBV_BUFSIZE_XBOX );
643 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_HIGH ) : PROFILE_HIGH;
645 else if( !strcasecmp( s, "iphone" ) || !strcasecmp( s, "ipad" ) )
647 if( x264_generic_device_check( param, s, X264_DEVICE_IPHONE ) < 0 )
649 x264_free( tmp );
650 return -1;
652 param->i_level_idc = ( param->i_level_idc < 0 ) ?
653 X264_LEVEL_IDC_IPHONE : X264_MIN( param->i_level_idc, X264_LEVEL_IDC_IPHONE );
654 /* Fix me: I have no data about iPhone/iPad's vbv restriction */
656 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_HIGH ) : PROFILE_HIGH;
658 else if( !strcasecmp( s, "generic" ) )
660 if( x264_generic_device_check( param, s, X264_DEVICE_GENERIC ) < 0 )
662 x264_free( tmp );
663 return -1;
665 param->i_frame_reference = X264_MIN( param->i_frame_reference, 3 );
666 param->i_bframe = X264_MIN( param->i_bframe, 3 );
667 param->i_bframe_pyramid = X264_B_PYRAMID_NONE;
668 param->analyse.i_weighted_pred = X264_MIN( param->analyse.i_weighted_pred, X264_WEIGHTP_SIMPLE );
669 param->i_level_idc = ( param->i_level_idc < 0 ) ?
670 X264_LEVEL_IDC_GENERIC : X264_MIN( param->i_level_idc, X264_LEVEL_IDC_GENERIC );
672 param->rc.i_vbv_max_bitrate = ( param->rc.i_vbv_max_bitrate <= 0 ) ?
673 X264_VBV_MAXRATE_GENERIC : X264_MIN( param->rc.i_vbv_max_bitrate, X264_VBV_MAXRATE_GENERIC );
675 param->rc.i_vbv_buffer_size = ( param->rc.i_vbv_buffer_size <= 0 ) ?
676 X264_VBV_BUFSIZE_GENERIC : X264_MIN( param->rc.i_vbv_buffer_size, X264_VBV_BUFSIZE_GENERIC );
678 i_profile = i_profile ? X264_MIN( i_profile, PROFILE_MAIN ) : PROFILE_MAIN;
680 else
682 x264_log( NULL, X264_LOG_ERROR, "invalid device: %s\n", s );
683 x264_free( tmp );
684 return -1;
686 s = strtok( NULL, ",./-+" );
688 x264_free( tmp );
689 return i_profile;
692 int x264_param_apply_profile( x264_param_t *param, const char *profile, const char *device )
694 int p = param->i_profile;
696 if( profile )
697 p = profile_string_to_int( profile );
698 if( p < 0 )
700 x264_log( NULL, X264_LOG_ERROR, "invalid profile: %s\n", profile );
701 return -1;
704 if( device )
705 p = x264_param_restrict_device( param, p, device );
706 if( !p ) // auto profile
707 return 0;
708 if( p < 0 )
709 return -1;
711 if( p < PROFILE_HIGH444_PREDICTIVE && ((param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
712 (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0)) )
714 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
715 return -1;
717 if( p < PROFILE_HIGH444_PREDICTIVE && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I444 )
719 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:4:4\n", profile );
720 return -1;
722 if( p < PROFILE_HIGH422 && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I422 )
724 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:2:2\n", profile );
725 return -1;
727 if( p < PROFILE_HIGH10 && BIT_DEPTH > 8 )
729 x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d\n", profile, BIT_DEPTH );
730 return -1;
733 if( p == PROFILE_BASELINE )
735 param->analyse.b_transform_8x8 = 0;
736 param->b_cabac = 0;
737 param->i_cqm_preset = X264_CQM_FLAT;
738 param->psz_cqm_file = NULL;
739 param->i_bframe = 0;
740 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
741 if( param->b_interlaced )
743 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
744 return -1;
746 if( param->b_fake_interlaced )
748 x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
749 return -1;
752 else if( p == PROFILE_MAIN )
754 param->analyse.b_transform_8x8 = 0;
755 param->i_cqm_preset = X264_CQM_FLAT;
756 param->psz_cqm_file = NULL;
758 param->i_profile = p;
759 return 0;
762 static int parse_enum( const char *arg, const char * const *names, int *dst )
764 for( int i = 0; names[i]; i++ )
765 if( !strcasecmp( arg, names[i] ) )
767 *dst = i;
768 return 0;
770 return -1;
773 static int parse_cqm( const char *str, uint8_t *cqm, int length )
775 int i = 0;
776 do {
777 int coef;
778 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
779 return -1;
780 cqm[i++] = coef;
781 } while( i < length && (str = strchr( str, ',' )) && str++ );
782 return (i == length) ? 0 : -1;
785 static int x264_atobool( const char *str, int *b_error )
787 if( !strcmp(str, "1") ||
788 !strcasecmp(str, "true") ||
789 !strcasecmp(str, "yes") )
790 return 1;
791 if( !strcmp(str, "0") ||
792 !strcasecmp(str, "false") ||
793 !strcasecmp(str, "no") )
794 return 0;
795 *b_error = 1;
796 return 0;
799 static int x264_atoi( const char *str, int *b_error )
801 char *end;
802 int v = strtol( str, &end, 0 );
803 if( end == str || *end != '\0' )
804 *b_error = 1;
805 return v;
808 static double x264_atof( const char *str, int *b_error )
810 char *end;
811 double v = strtod( str, &end );
812 if( end == str || *end != '\0' )
813 *b_error = 1;
814 return v;
817 #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
818 #undef atoi
819 #undef atof
820 #define atoi(str) x264_atoi( str, &b_error )
821 #define atof(str) x264_atof( str, &b_error )
823 int x264_param_parse( x264_param_t *p, const char *name, const char *value )
825 char *name_buf = NULL;
826 int b_error = 0;
827 int errortype = X264_PARAM_BAD_VALUE;
828 int name_was_bool;
829 int value_was_null = !value;
830 int i;
832 if( !name )
833 return X264_PARAM_BAD_NAME;
834 if( !value )
835 value = "true";
837 if( value[0] == '=' )
838 value++;
840 if( strchr( name, '_' ) ) // s/_/-/g
842 char *c;
843 name_buf = strdup(name);
844 if( !name_buf )
845 return X264_PARAM_BAD_NAME;
846 while( (c = strchr( name_buf, '_' )) )
847 *c = '-';
848 name = name_buf;
851 if( !strncmp( name, "no", 2 ) )
853 name += 2;
854 if( name[0] == '-' )
855 name++;
856 value = atobool(value) ? "false" : "true";
858 name_was_bool = 0;
860 #define OPT(STR) else if( !strcmp( name, STR ) )
861 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
862 if( 0 );
863 OPT("asm")
865 p->cpu = isdigit(value[0]) ? atoi(value) :
866 !strcasecmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
867 if( b_error )
869 char *buf = strdup( value );
870 if( buf )
872 char *tok, UNUSED *saveptr=NULL, *init;
873 b_error = 0;
874 p->cpu = 0;
875 for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
877 i = 0;
878 while( x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name) )
879 i++;
880 p->cpu |= x264_cpu_names[i].flags;
881 if( !x264_cpu_names[i].flags )
882 b_error = 1;
884 free( buf );
885 if( (p->cpu&X264_CPU_SSSE3) && !(p->cpu&X264_CPU_SSE2_IS_SLOW) )
886 p->cpu |= X264_CPU_SSE2_IS_FAST;
890 OPT("threads")
892 if( !strcasecmp(value, "auto") )
893 p->i_threads = X264_THREADS_AUTO;
894 else
895 p->i_threads = atoi(value);
897 OPT("lookahead-threads")
899 if( !strcasecmp(value, "auto") )
900 p->i_lookahead_threads = X264_THREADS_AUTO;
901 else
902 p->i_lookahead_threads = atoi(value);
904 OPT("sliced-threads")
905 p->b_sliced_threads = atobool(value);
906 OPT("sync-lookahead")
908 if( !strcasecmp(value, "auto") )
909 p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
910 else
911 p->i_sync_lookahead = atoi(value);
913 OPT2("deterministic", "n-deterministic")
914 p->b_deterministic = atobool(value);
915 OPT("cpu-independent")
916 p->b_cpu_independent = atobool(value);
917 OPT2("level", "level-idc")
919 if( !strcmp(value, "1b") )
920 p->i_level_idc = 9;
921 else if( atof(value) < 7 )
922 p->i_level_idc = (int)(10*atof(value)+.5);
923 else
924 p->i_level_idc = atoi(value);
926 OPT("level-force")
927 p->b_level_force = atobool(value);
928 OPT("profile-force")
929 p->b_profile_force = atobool(value);
930 OPT("bluray-compat")
931 p->b_bluray_compat = atobool(value);
932 OPT("avcintra-class")
933 p->i_avcintra_class = atoi(value);
934 OPT("sar")
936 b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
937 2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
939 OPT("overscan")
940 b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
941 OPT("videoformat")
942 b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
943 OPT("fullrange")
944 b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
945 OPT("colorprim")
946 b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
947 OPT("transfer")
948 b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
949 OPT("colormatrix")
950 b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
951 OPT("chromaloc")
953 p->vui.i_chroma_loc = atoi(value);
954 b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
956 OPT("fps")
958 if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) != 2 )
960 double fps = atof(value);
961 if( fps > 0.0 && fps <= INT_MAX/1000.0 )
963 p->i_fps_num = (int)(fps * 1000.0 + .5);
964 p->i_fps_den = 1000;
966 else
968 p->i_fps_num = atoi(value);
969 p->i_fps_den = 1;
973 OPT2("ref", "frameref")
974 p->i_frame_reference = atoi(value);
975 OPT("dpb-size")
976 p->i_dpb_size = atoi(value);
977 OPT("keyint")
979 if( strstr( value, "infinite" ) )
980 p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
981 else if( !strcmp( value, "auto" ) || atoi(value) < 0 )
982 p->i_keyint_max = X264_KEYINT_MAX_AUTO;
983 else
984 p->i_keyint_max = atoi(value);
986 OPT2("min-keyint", "keyint-min")
988 p->i_keyint_min = atoi(value);
989 if( p->i_keyint_max !=X264_KEYINT_MAX_AUTO && p->i_keyint_max < p->i_keyint_min )
990 p->i_keyint_max = p->i_keyint_min;
992 OPT("scenecut")
994 p->i_scenecut_threshold = atobool(value);
995 if( b_error || p->i_scenecut_threshold )
997 b_error = 0;
998 p->i_scenecut_threshold = atoi(value);
1001 OPT("intra-refresh")
1002 p->b_intra_refresh = atobool(value);
1003 OPT("bframes")
1004 p->i_bframe = atoi(value);
1005 OPT("b-adapt")
1007 p->i_bframe_adaptive = atobool(value);
1008 if( b_error )
1010 b_error = 0;
1011 p->i_bframe_adaptive = atoi(value);
1014 OPT("b-bias")
1015 p->i_bframe_bias = atoi(value);
1016 OPT("b-pyramid")
1018 b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
1019 if( b_error )
1021 b_error = 0;
1022 p->i_bframe_pyramid = atoi(value);
1025 OPT("open-gop")
1026 p->b_open_gop = atobool(value);
1027 OPT("nf")
1028 p->b_deblocking_filter = !atobool(value);
1029 OPT2("filter", "deblock")
1031 if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
1032 2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
1034 p->b_deblocking_filter = 1;
1036 else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
1038 p->b_deblocking_filter = 1;
1039 p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
1041 else
1042 p->b_deblocking_filter = atobool(value);
1044 OPT("slice-max-size")
1045 p->i_slice_max_size = atoi(value);
1046 OPT("slice-max-mbs")
1047 p->i_slice_max_mbs = atoi(value);
1048 OPT("slice-min-mbs")
1049 p->i_slice_min_mbs = atoi(value);
1050 OPT("slices")
1051 p->i_slice_count = atoi(value);
1052 OPT("slices-max")
1053 p->i_slice_count_max = atoi(value);
1054 OPT("cabac")
1055 p->b_cabac = atobool(value);
1056 OPT("cabac-idc")
1057 p->i_cabac_init_idc = atoi(value);
1058 OPT("interlaced")
1059 p->b_interlaced = atobool(value);
1060 OPT("tff")
1061 p->b_interlaced = p->b_tff = atobool(value);
1062 OPT("bff")
1064 p->b_interlaced = atobool(value);
1065 p->b_tff = !p->b_interlaced;
1067 OPT("constrained-intra")
1068 p->b_constrained_intra = atobool(value);
1069 OPT("cqm")
1071 if( strstr( value, "flat" ) )
1072 p->i_cqm_preset = X264_CQM_FLAT;
1073 else if( strstr( value, "jvt" ) )
1074 p->i_cqm_preset = X264_CQM_JVT;
1075 else
1076 p->psz_cqm_file = strdup(value);
1078 OPT("cqmfile")
1079 p->psz_cqm_file = strdup(value);
1080 OPT("cqm4")
1082 p->i_cqm_preset = X264_CQM_CUSTOM;
1083 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
1084 b_error |= parse_cqm( value, p->cqm_4py, 16 );
1085 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
1086 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
1088 OPT("cqm8")
1090 p->i_cqm_preset = X264_CQM_CUSTOM;
1091 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
1092 b_error |= parse_cqm( value, p->cqm_8py, 64 );
1093 b_error |= parse_cqm( value, p->cqm_8ic, 64 );
1094 b_error |= parse_cqm( value, p->cqm_8pc, 64 );
1096 OPT("cqm4i")
1098 p->i_cqm_preset = X264_CQM_CUSTOM;
1099 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
1100 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
1102 OPT("cqm4p")
1104 p->i_cqm_preset = X264_CQM_CUSTOM;
1105 b_error |= parse_cqm( value, p->cqm_4py, 16 );
1106 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
1108 OPT("cqm4iy")
1110 p->i_cqm_preset = X264_CQM_CUSTOM;
1111 b_error |= parse_cqm( value, p->cqm_4iy, 16 );
1113 OPT("cqm4ic")
1115 p->i_cqm_preset = X264_CQM_CUSTOM;
1116 b_error |= parse_cqm( value, p->cqm_4ic, 16 );
1118 OPT("cqm4py")
1120 p->i_cqm_preset = X264_CQM_CUSTOM;
1121 b_error |= parse_cqm( value, p->cqm_4py, 16 );
1123 OPT("cqm4pc")
1125 p->i_cqm_preset = X264_CQM_CUSTOM;
1126 b_error |= parse_cqm( value, p->cqm_4pc, 16 );
1128 OPT("cqm8i")
1130 p->i_cqm_preset = X264_CQM_CUSTOM;
1131 b_error |= parse_cqm( value, p->cqm_8iy, 64 );
1132 b_error |= parse_cqm( value, p->cqm_8ic, 64 );
1134 OPT("cqm8p")
1136 p->i_cqm_preset = X264_CQM_CUSTOM;
1137 b_error |= parse_cqm( value, p->cqm_8py, 64 );
1138 b_error |= parse_cqm( value, p->cqm_8pc, 64 );
1140 OPT("log")
1141 p->i_log_level = atoi(value);
1142 OPT("log-file")
1143 p->psz_log_file = strdup(value);
1144 OPT("log-file-level")
1145 if( !parse_enum( value, x264_log_level_names, &p->i_log_file_level ) )
1146 p->i_log_file_level += X264_LOG_NONE;
1147 else
1148 p->i_log_file_level = atoi(value);
1149 OPT("dump-yuv")
1150 p->psz_dump_yuv = strdup(value);
1151 OPT2("analyse", "partitions")
1153 p->analyse.inter = 0;
1154 if( strstr( value, "none" ) ) p->analyse.inter = 0;
1155 if( strstr( value, "all" ) ) p->analyse.inter = ~0;
1157 if( strstr( value, "i4x4" ) ) p->analyse.inter |= X264_ANALYSE_I4x4;
1158 if( strstr( value, "i8x8" ) ) p->analyse.inter |= X264_ANALYSE_I8x8;
1159 if( strstr( value, "p8x8" ) ) p->analyse.inter |= X264_ANALYSE_PSUB16x16;
1160 if( strstr( value, "p4x4" ) ) p->analyse.inter |= X264_ANALYSE_PSUB8x8;
1161 if( strstr( value, "b8x8" ) ) p->analyse.inter |= X264_ANALYSE_BSUB16x16;
1163 OPT("8x8dct")
1164 p->analyse.b_transform_8x8 = atobool(value);
1165 OPT2("weightb", "weight-b")
1166 p->analyse.b_weighted_bipred = atobool(value);
1167 OPT("weightp")
1168 p->analyse.i_weighted_pred = atoi(value);
1169 OPT2("direct", "direct-pred")
1170 b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
1171 OPT("chroma-qp-offset")
1172 p->analyse.i_chroma_qp_offset = atoi(value);
1173 OPT("me")
1174 b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
1175 OPT2("merange", "me-range")
1176 p->analyse.i_me_range = atoi(value);
1177 OPT2("mvrange", "mv-range")
1178 p->analyse.i_mv_range = atoi(value);
1179 OPT2("mvrange-thread", "mv-range-thread")
1180 p->analyse.i_mv_range_thread = atoi(value);
1181 OPT2("subme", "subq")
1182 p->analyse.i_subpel_refine = atoi(value);
1183 OPT("psy-rd")
1185 if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
1186 2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
1187 2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
1189 else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
1191 p->analyse.f_psy_trellis = 0;
1193 else
1195 p->analyse.f_psy_rd = 0;
1196 p->analyse.f_psy_trellis = 0;
1199 OPT("psy")
1200 p->analyse.b_psy = atobool(value);
1201 OPT("chroma-me")
1202 p->analyse.b_chroma_me = atobool(value);
1203 OPT("mixed-refs")
1204 p->analyse.b_mixed_references = atobool(value);
1205 OPT("trellis")
1206 p->analyse.i_trellis = atoi(value);
1207 OPT("fast-pskip")
1208 p->analyse.b_fast_pskip = atobool(value);
1209 OPT("dct-decimate")
1210 p->analyse.b_dct_decimate = atobool(value);
1211 OPT("deadzone-inter")
1212 p->analyse.i_luma_deadzone[0] = atoi(value);
1213 OPT("deadzone-intra")
1214 p->analyse.i_luma_deadzone[1] = atoi(value);
1215 OPT("nr")
1216 p->analyse.i_noise_reduction = atoi(value);
1217 OPT("bitrate")
1219 p->rc.i_bitrate = atoi(value);
1220 p->rc.i_rc_method = X264_RC_ABR;
1222 OPT2("qp", "qp_constant")
1224 p->rc.i_qp_constant = atoi(value);
1225 p->rc.i_rc_method = X264_RC_CQP;
1227 OPT("crf")
1229 p->rc.f_rf_constant = atof(value);
1230 p->rc.i_rc_method = X264_RC_CRF;
1232 OPT("crf-max")
1233 p->rc.f_rf_constant_max = atof(value);
1234 OPT("rc-lookahead")
1235 p->rc.i_lookahead = atoi(value);
1236 OPT2("qpmin", "qp-min")
1238 if( 3 == sscanf( value, "%d:%d:%d", &p->rc.i_qp_min[SLICE_TYPE_I], &p->rc.i_qp_min[SLICE_TYPE_P], &p->rc.i_qp_min[SLICE_TYPE_B] ) ||
1239 3 == sscanf( value, "%d,%d,%d", &p->rc.i_qp_min[SLICE_TYPE_I], &p->rc.i_qp_min[SLICE_TYPE_P], &p->rc.i_qp_min[SLICE_TYPE_B] ) )
1240 p->rc.i_qp_min_min = X264_MIN3( p->rc.i_qp_min[SLICE_TYPE_I], p->rc.i_qp_min[SLICE_TYPE_P], p->rc.i_qp_min[SLICE_TYPE_B] );
1241 else if( sscanf( value, "%d", &p->rc.i_qp_min_min ) )
1242 p->rc.i_qp_min[SLICE_TYPE_I] = p->rc.i_qp_min[SLICE_TYPE_P] = p->rc.i_qp_min[SLICE_TYPE_B] = p->rc.i_qp_min_min;
1244 OPT2("qpmax", "qp-max")
1246 if( 3 == sscanf( value, "%d:%d:%d", &p->rc.i_qp_max[SLICE_TYPE_I], &p->rc.i_qp_max[SLICE_TYPE_P], &p->rc.i_qp_max[SLICE_TYPE_B] ) ||
1247 3 == sscanf( value, "%d,%d,%d", &p->rc.i_qp_max[SLICE_TYPE_I], &p->rc.i_qp_max[SLICE_TYPE_P], &p->rc.i_qp_max[SLICE_TYPE_B] ) )
1248 p->rc.i_qp_max_max = X264_MAX3( p->rc.i_qp_max[SLICE_TYPE_I], p->rc.i_qp_max[SLICE_TYPE_P], p->rc.i_qp_max[SLICE_TYPE_B] );
1249 else if( sscanf( value, "%d", &p->rc.i_qp_max_max ) )
1250 p->rc.i_qp_max[SLICE_TYPE_I] = p->rc.i_qp_max[SLICE_TYPE_P] = p->rc.i_qp_max[SLICE_TYPE_B] = p->rc.i_qp_max_max;
1252 OPT2("qpstep", "qp-step")
1253 p->rc.i_qp_step = atoi(value);
1254 OPT("ratetol")
1255 p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
1256 OPT("vbv-maxrate")
1257 if( !strcmp(value, "auto_high444") )
1258 p->rc.i_vbv_max_bitrate = X264_VBV_MAXRATE_HIGH444;
1259 else if( !strcmp(value, "auto_high422") )
1260 p->rc.i_vbv_max_bitrate = X264_VBV_MAXRATE_HIGH422;
1261 else if( !strcmp(value, "auto_high10") )
1262 p->rc.i_vbv_max_bitrate = X264_VBV_MAXRATE_HIGH10;
1263 else if( !strcmp(value, "auto_high") )
1264 p->rc.i_vbv_max_bitrate = X264_VBV_MAXRATE_HIGH;
1265 else if( !strcmp(value, "auto_main") )
1266 p->rc.i_vbv_max_bitrate = X264_VBV_MAXRATE_MAIN;
1267 else
1268 p->rc.i_vbv_max_bitrate = atoi(value);
1269 OPT("vbv-bufsize")
1270 if( !strcmp(value, "auto_high444") )
1271 p->rc.i_vbv_buffer_size = X264_VBV_BUFSIZE_HIGH444;
1272 else if( !strcmp(value, "auto_high422") )
1273 p->rc.i_vbv_buffer_size = X264_VBV_BUFSIZE_HIGH422;
1274 else if( !strcmp(value, "auto_high10") )
1275 p->rc.i_vbv_buffer_size = X264_VBV_BUFSIZE_HIGH10;
1276 else if( !strcmp(value, "auto_high") )
1277 p->rc.i_vbv_buffer_size = X264_VBV_BUFSIZE_HIGH;
1278 else if( !strcmp(value, "auto_main") )
1279 p->rc.i_vbv_buffer_size = X264_VBV_BUFSIZE_MAIN;
1280 else
1281 p->rc.i_vbv_buffer_size = atoi(value);
1282 OPT("vbv-init")
1283 p->rc.f_vbv_buffer_init = atof(value);
1284 OPT2("ipratio", "ip-factor")
1285 p->rc.f_ip_factor = atof(value);
1286 OPT2("pbratio", "pb-factor")
1287 p->rc.f_pb_factor = atof(value);
1288 OPT("aq-mode")
1289 p->rc.i_aq_mode = atoi(value);
1290 OPT("aq-strength")
1291 p->rc.f_aq_strength = atof(value);
1292 OPT("aq-sensitivity")
1293 p->rc.f_aq_sensitivity = atof(value);
1294 OPT("aq-ifactor")
1295 p->rc.f_aq_ifactor = atof(value);
1296 OPT("aq-pfactor")
1297 p->rc.f_aq_pfactor = atof(value);
1298 OPT("aq-bfactor")
1299 p->rc.f_aq_bfactor = atof(value);
1300 OPT("aq2-strength")
1302 p->rc.f_aq2_strength = atof(value);
1303 p->rc.b_aq2 = 1;
1305 OPT("aq2-sensitivity")
1306 p->rc.f_aq2_sensitivity = atof(value);
1307 OPT("aq2-ifactor")
1308 p->rc.f_aq2_ifactor = atof(value);
1309 OPT("aq2-pfactor")
1310 p->rc.f_aq2_pfactor = atof(value);
1311 OPT("aq2-bfactor")
1312 p->rc.f_aq2_bfactor = atof(value);
1313 OPT("aq3-mode")
1314 p->rc.i_aq3_mode = atoi(value);
1315 OPT("aq3-strength")
1317 if( 8 == sscanf( value, "%f:%f:%f:%f:%f:%f:%f:%f",
1318 &p->rc.f_aq3_strengths[0][0], &p->rc.f_aq3_strengths[1][0], &p->rc.f_aq3_strengths[0][1], &p->rc.f_aq3_strengths[1][1],
1319 &p->rc.f_aq3_strengths[0][2], &p->rc.f_aq3_strengths[1][2], &p->rc.f_aq3_strengths[0][3], &p->rc.f_aq3_strengths[1][3] ) ||
1320 8 == sscanf( value, "%f,%f,%f,%f,%f,%f,%f,%f",
1321 &p->rc.f_aq3_strengths[0][0], &p->rc.f_aq3_strengths[1][0], &p->rc.f_aq3_strengths[0][1], &p->rc.f_aq3_strengths[1][1],
1322 &p->rc.f_aq3_strengths[0][2], &p->rc.f_aq3_strengths[1][2], &p->rc.f_aq3_strengths[0][3], &p->rc.f_aq3_strengths[1][3] ) )
1323 p->rc.f_aq3_strength = 0.0;
1324 else if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq3_strengths[0][0], &p->rc.f_aq3_strengths[1][0] ) ||
1325 2 == sscanf( value, "%f,%f", &p->rc.f_aq3_strengths[0][0], &p->rc.f_aq3_strengths[1][0] ) )
1327 p->rc.f_aq3_strength = 0.0;
1328 for( i = 0; i < 2; i++ )
1329 for( int j = 1; j < 4; j++ )
1330 p->rc.f_aq3_strengths[i][j] = p->rc.f_aq3_strengths[i][0];
1332 else if( sscanf( value, "%f", &p->rc.f_aq3_strength ) )
1333 for( i = 0; i < 2; i++ )
1334 for( int j = 0; j < 4; j++ )
1335 p->rc.f_aq3_strengths[i][j] = p->rc.f_aq3_strength;
1337 OPT("aq3-sensitivity")
1338 p->rc.f_aq3_sensitivity = atof(value);
1339 OPT("aq3-ifactor")
1340 if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq3_ifactor[0], &p->rc.f_aq3_ifactor[1] ) ||
1341 2 == sscanf( value, "%f,%f", &p->rc.f_aq3_ifactor[0], &p->rc.f_aq3_ifactor[1] ) )
1343 else if( sscanf( value, "%f", &p->rc.f_aq3_ifactor[0] ) )
1344 p->rc.f_aq3_ifactor[1] = p->rc.f_aq3_ifactor[0];
1345 else
1346 p->rc.f_aq3_ifactor[1] = p->rc.f_aq3_ifactor[0] = 1.0;
1347 OPT("aq3-pfactor")
1348 if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq3_pfactor[0], &p->rc.f_aq3_pfactor[1] ) ||
1349 2 == sscanf( value, "%f,%f", &p->rc.f_aq3_pfactor[0], &p->rc.f_aq3_pfactor[1] ) )
1351 else if( sscanf( value, "%f", &p->rc.f_aq3_pfactor[0] ) )
1352 p->rc.f_aq3_pfactor[1] = p->rc.f_aq3_pfactor[0];
1353 else
1354 p->rc.f_aq3_pfactor[1] = p->rc.f_aq3_pfactor[0] = 1.0;
1355 OPT("aq3-bfactor")
1356 if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq3_bfactor[0], &p->rc.f_aq3_bfactor[1] ) ||
1357 2 == sscanf( value, "%f,%f", &p->rc.f_aq3_bfactor[0], &p->rc.f_aq3_bfactor[1] ) )
1359 else if( sscanf( value, "%f", &p->rc.f_aq3_bfactor[0] ) )
1360 p->rc.f_aq3_bfactor[1] = p->rc.f_aq3_bfactor[0];
1361 else
1362 p->rc.f_aq3_bfactor[1] = p->rc.f_aq3_bfactor[0] = 1.0;
1363 OPT("aq3-boundary")
1365 if( 3 == sscanf( value, "%d:%d:%d", &p->rc.i_aq3_boundary[0], &p->rc.i_aq3_boundary[1], &p->rc.i_aq3_boundary[2] ) ||
1366 3 == sscanf( value, "%d,%d,%d", &p->rc.i_aq3_boundary[0], &p->rc.i_aq3_boundary[1], &p->rc.i_aq3_boundary[2] ) )
1367 p->rc.b_aq3_boundary = 1;
1368 else
1369 p->rc.i_aq3_boundary[0] = p->rc.i_aq3_boundary[1] = p->rc.i_aq3_boundary[2] = 0;
1371 OPT("fgo")
1372 p->analyse.i_fgo = atoi(value);
1373 OPT("fade-compensate")
1374 p->rc.f_fade_compensate = atof(value);
1375 OPT("pass")
1377 int pass = x264_clip3( atoi(value), 0, 3 );
1378 p->rc.b_stat_write = pass & 1;
1379 p->rc.b_stat_read = pass & 2;
1381 OPT("stats")
1383 p->rc.psz_stat_in = strdup(value);
1384 p->rc.psz_stat_out = strdup(value);
1386 OPT("qcomp")
1387 p->rc.f_qcompress = atof(value);
1388 OPT("mbtree")
1389 p->rc.b_mb_tree = atobool(value);
1390 OPT("qblur")
1391 p->rc.f_qblur = atof(value);
1392 OPT2("cplxblur", "cplx-blur")
1393 p->rc.f_complexity_blur = atof(value);
1394 OPT("zones")
1395 p->rc.psz_zones = strdup(value);
1396 OPT("crop-rect")
1397 b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
1398 &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
1399 OPT("psnr")
1400 p->analyse.b_psnr = atobool(value);
1401 OPT("ssim")
1402 p->analyse.b_ssim = atobool(value);
1403 OPT("aud")
1404 p->b_aud = atobool(value);
1405 OPT("sps-id")
1406 p->i_sps_id = atoi(value);
1407 OPT("opts")
1409 #define OPTS_SET( psz_x, prefix, flag ) \
1410 if( !strncasecmp( value, prefix, strlen(prefix) ) ) \
1412 if( p->i_opts_write & flag ) \
1414 free(psz_x); \
1415 psz_x = NULL; \
1417 psz_x = strdup( value + strlen(prefix) ); \
1418 p->i_opts_write |= flag; \
1420 OPTS_SET( p->psz_opts[0], "preinfo:" , X264_OPTS_PREINFO )
1421 else OPTS_SET( p->psz_opts[0], "0:" , X264_OPTS_PREINFO )
1422 else OPTS_SET( p->psz_opts[1], "postinfo:", X264_OPTS_POSTINFO )
1423 else OPTS_SET( p->psz_opts[1], "1:" , X264_OPTS_POSTINFO )
1424 else OPTS_SET( p->psz_opts[2], "preopt:" , X264_OPTS_PREOPT )
1425 else OPTS_SET( p->psz_opts[2], "2:" , X264_OPTS_PREOPT )
1426 else OPTS_SET( p->psz_opts[3], "postopt:" , X264_OPTS_POSTOPT )
1427 else OPTS_SET( p->psz_opts[3], "3:" , X264_OPTS_POSTOPT )
1428 else
1430 int flag = strlen(value) == 1 && isdigit(value[0]) ? atoi(value) : atobool(value);
1431 b_error |= flag < X264_OPTS_NONE || flag > X264_OPTS_FULL;
1432 p->i_opts_write &= ~X264_OPTS_FULL; // clear basic flag bit
1433 p->i_opts_write |= flag; // write basic flag bit
1435 #undef OPTS_SET
1437 OPT("global-header")
1438 p->b_repeat_headers = !atobool(value);
1439 OPT("repeat-headers")
1440 p->b_repeat_headers = atobool(value);
1441 OPT("annexb")
1442 p->b_annexb = atobool(value);
1443 OPT("force-cfr")
1444 p->b_vfr_input = !atobool(value);
1445 OPT("nal-hrd")
1446 b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
1447 OPT("filler")
1448 p->rc.b_filler = atobool(value);
1449 OPT("pic-struct")
1450 p->b_pic_struct = atobool(value);
1451 OPT("fake-interlaced")
1452 p->b_fake_interlaced = atobool(value);
1453 OPT("frame-packing")
1454 p->i_frame_packing = atoi(value);
1455 OPT("stitchable")
1456 p->b_stitchable = atobool(value);
1457 OPT("opencl")
1458 p->b_opencl = atobool( value );
1459 OPT("opencl-clbin")
1460 p->psz_clbin_file = strdup( value );
1461 OPT("opencl-device")
1462 p->i_opencl_device = atoi( value );
1463 else
1465 b_error = 1;
1466 errortype = X264_PARAM_BAD_NAME;
1468 #undef OPT
1469 #undef OPT2
1470 #undef atobool
1471 #undef atoi
1472 #undef atof
1474 if( name_buf )
1475 free( name_buf );
1477 b_error |= value_was_null && !name_was_bool;
1478 return b_error ? errortype : 0;
1481 /****************************************************************************
1482 * x264_log:
1483 ****************************************************************************/
1484 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
1486 if( !h || i_level <= h->param.i_log_level )
1488 va_list arg;
1489 va_start( arg, psz_fmt );
1490 if( !h )
1491 x264_log_default( NULL, i_level, psz_fmt, arg );
1492 else
1493 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
1494 va_end( arg );
1497 if( h && h->param.psz_log_file && i_level <= h->param.i_log_file_level )
1499 va_list arg;
1500 va_start( arg, psz_fmt );
1501 x264_log_file( h->param.psz_log_file, i_level, psz_fmt, arg );
1502 va_end( arg );
1506 static void x264_log_file( char *p_file_name, int i_level, const char *psz_fmt, va_list arg )
1508 char *psz_prefix;
1509 switch( i_level )
1511 case X264_LOG_ERROR:
1512 psz_prefix = "error";
1513 break;
1514 case X264_LOG_WARNING:
1515 psz_prefix = "warning";
1516 break;
1517 case X264_LOG_INFO:
1518 psz_prefix = "info";
1519 break;
1520 case X264_LOG_DEBUG:
1521 psz_prefix = "debug";
1522 break;
1523 default:
1524 psz_prefix = "unknown";
1525 break;
1527 FILE *p_log_file = x264_fopen( p_file_name, "ab" );
1528 if( p_log_file )
1530 fprintf( p_log_file, "x264 [%s]: ", psz_prefix );
1531 x264_vfprintf( p_log_file, psz_fmt, arg );
1532 fclose( p_log_file );
1536 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
1538 char *psz_prefix;
1539 switch( i_level )
1541 case X264_LOG_ERROR:
1542 psz_prefix = "error";
1543 break;
1544 case X264_LOG_WARNING:
1545 psz_prefix = "warning";
1546 break;
1547 case X264_LOG_INFO:
1548 psz_prefix = "info";
1549 break;
1550 case X264_LOG_DEBUG:
1551 psz_prefix = "debug";
1552 break;
1553 default:
1554 psz_prefix = "unknown";
1555 break;
1557 fprintf( stderr, "x264 [%s]: ", psz_prefix );
1558 x264_vfprintf( stderr, psz_fmt, arg );
1561 /****************************************************************************
1562 * x264_picture_init:
1563 ****************************************************************************/
1564 void x264_picture_init( x264_picture_t *pic )
1566 memset( pic, 0, sizeof( x264_picture_t ) );
1567 pic->i_type = X264_TYPE_AUTO;
1568 pic->i_qpplus1 = X264_QP_AUTO;
1569 pic->i_pic_struct = PIC_STRUCT_AUTO;
1572 /****************************************************************************
1573 * x264_picture_alloc:
1574 ****************************************************************************/
1575 int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
1577 typedef struct
1579 int planes;
1580 int width_fix8[3];
1581 int height_fix8[3];
1582 } x264_csp_tab_t;
1584 static const x264_csp_tab_t x264_csp_tab[] =
1586 [X264_CSP_I420] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1587 [X264_CSP_YV12] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1588 [X264_CSP_NV12] = { 2, { 256*1, 256*1 }, { 256*1, 256/2 }, },
1589 [X264_CSP_NV21] = { 2, { 256*1, 256*1 }, { 256*1, 256/2 }, },
1590 [X264_CSP_I422] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1591 [X264_CSP_YV16] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1592 [X264_CSP_NV16] = { 2, { 256*1, 256*1 }, { 256*1, 256*1 }, },
1593 [X264_CSP_YUYV] = { 1, { 256*2 }, { 256*1 }, },
1594 [X264_CSP_UYVY] = { 1, { 256*2 }, { 256*1 }, },
1595 [X264_CSP_I444] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1596 [X264_CSP_YV24] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1597 [X264_CSP_BGR] = { 1, { 256*3 }, { 256*1 }, },
1598 [X264_CSP_BGRA] = { 1, { 256*4 }, { 256*1 }, },
1599 [X264_CSP_RGB] = { 1, { 256*3 }, { 256*1 }, },
1602 int csp = i_csp & X264_CSP_MASK;
1603 if( csp <= X264_CSP_NONE || csp >= X264_CSP_MAX || csp == X264_CSP_V210 )
1604 return -1;
1605 x264_picture_init( pic );
1606 pic->img.i_csp = i_csp;
1607 pic->img.i_plane = x264_csp_tab[csp].planes;
1608 int depth_factor = i_csp & X264_CSP_HIGH_DEPTH ? 2 : 1;
1609 int plane_offset[3] = {0};
1610 int frame_size = 0;
1611 for( int i = 0; i < pic->img.i_plane; i++ )
1613 int stride = (((int64_t)i_width * x264_csp_tab[csp].width_fix8[i]) >> 8) * depth_factor;
1614 int plane_size = (((int64_t)i_height * x264_csp_tab[csp].height_fix8[i]) >> 8) * stride;
1615 pic->img.i_stride[i] = stride;
1616 plane_offset[i] = frame_size;
1617 frame_size += plane_size;
1619 pic->img.plane[0] = x264_malloc( frame_size );
1620 if( !pic->img.plane[0] )
1621 return -1;
1622 for( int i = 1; i < pic->img.i_plane; i++ )
1623 pic->img.plane[i] = pic->img.plane[0] + plane_offset[i];
1624 return 0;
1627 /****************************************************************************
1628 * x264_picture_clean:
1629 ****************************************************************************/
1630 void x264_picture_clean( x264_picture_t *pic )
1632 x264_free( pic->img.plane[0] );
1634 /* just to be safe */
1635 memset( pic, 0, sizeof( x264_picture_t ) );
1638 /****************************************************************************
1639 * x264_malloc:
1640 ****************************************************************************/
1641 void *x264_malloc( int i_size )
1643 uint8_t *align_buf = NULL;
1644 #if HAVE_MALLOC_H
1645 #if HAVE_THP
1646 #define HUGE_PAGE_SIZE 2*1024*1024
1647 #define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
1648 /* Attempt to allocate huge pages to reduce TLB misses. */
1649 if( i_size >= HUGE_PAGE_THRESHOLD )
1651 align_buf = memalign( HUGE_PAGE_SIZE, i_size );
1652 if( align_buf )
1654 /* Round up to the next huge page boundary if we are close enough. */
1655 size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
1656 madvise( align_buf, madv_size, MADV_HUGEPAGE );
1659 else
1660 #undef HUGE_PAGE_SIZE
1661 #undef HUGE_PAGE_THRESHOLD
1662 #endif
1663 align_buf = memalign( NATIVE_ALIGN, i_size );
1664 #else
1665 uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
1666 if( buf )
1668 align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
1669 align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
1670 *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
1672 #endif
1673 if( !align_buf )
1674 x264_log( NULL, X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
1675 return align_buf;
1678 /****************************************************************************
1679 * x264_free:
1680 ****************************************************************************/
1681 void x264_free( void *p )
1683 if( p )
1685 #if HAVE_MALLOC_H
1686 free( p );
1687 #else
1688 free( *( ( ( void **) p ) - 1 ) );
1689 #endif
1693 /****************************************************************************
1694 * x264_reduce_fraction:
1695 ****************************************************************************/
1696 #define REDUCE_FRACTION( name, type )\
1697 void name( type *n, type *d )\
1699 type a = *n; \
1700 type b = *d; \
1701 type c; \
1702 if( !a || !b ) \
1703 return; \
1704 c = a % b; \
1705 while( c ) \
1707 a = b; \
1708 b = c; \
1709 c = a % b; \
1711 *n /= b; \
1712 *d /= b; \
1715 REDUCE_FRACTION( x264_reduce_fraction , uint32_t )
1716 REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
1718 /****************************************************************************
1719 * x264_ntsc_fps:
1720 ****************************************************************************/
1721 void x264_ntsc_fps( uint32_t *fps_num, uint32_t *fps_den )
1723 if( !*fps_num || !*fps_den )
1724 return;
1726 #define X264_NTSC_TIMEBASE 1001
1727 const double f_ntsc_mod6_quotient = (double) X264_NTSC_TIMEBASE / 6.;
1728 const double f_fps = (double) *fps_num / (double) *fps_den;
1729 const double f_interval = 1000. / f_fps;
1731 const uint32_t i_nearest_ntsc_mod6_num = (uint32_t) ( f_ntsc_mod6_quotient / f_interval + 0.5 );
1732 if( !i_nearest_ntsc_mod6_num )
1733 return;
1734 const double f_nearest_ntsc_interval = f_ntsc_mod6_quotient / (double) i_nearest_ntsc_mod6_num;
1736 if ( fabs ( f_interval - f_nearest_ntsc_interval ) < ( 1. / ( f_fps + 1. ) ) ) // error < ( 1 / fps + 1 )
1738 *fps_num = i_nearest_ntsc_mod6_num * 6000;
1739 *fps_den = X264_NTSC_TIMEBASE;
1741 #undef X264_NTSC_TIMEBASE
1743 return;
1746 /****************************************************************************
1747 * x264_slurp_file:
1748 ****************************************************************************/
1749 char *x264_slurp_file( const char *filename )
1751 int b_error = 0;
1752 int64_t i_size;
1753 char *buf;
1754 FILE *fh = x264_fopen( filename, "rb" );
1755 if( !fh )
1756 return NULL;
1758 b_error |= fseek( fh, 0, SEEK_END ) < 0;
1759 b_error |= ( i_size = ftell( fh ) ) <= 0;
1760 if( WORD_SIZE == 4 )
1761 b_error |= i_size > INT32_MAX;
1762 b_error |= fseek( fh, 0, SEEK_SET ) < 0;
1763 if( b_error )
1764 goto error;
1766 buf = x264_malloc( i_size+2 );
1767 if( !buf )
1768 goto error;
1770 b_error |= fread( buf, 1, i_size, fh ) != i_size;
1771 fclose( fh );
1772 if( b_error )
1774 x264_free( buf );
1775 return NULL;
1778 if( buf[i_size-1] != '\n' )
1779 buf[i_size++] = '\n';
1780 buf[i_size] = '\0';
1782 return buf;
1783 error:
1784 fclose( fh );
1785 return NULL;
1788 /****************************************************************************
1789 * x264_param2string:
1790 ****************************************************************************/
1791 char *x264_param2string( x264_param_t *p, int b_res )
1793 int len = 1000;
1794 char *buf, *s;
1795 if( p->rc.psz_zones )
1796 len += strlen(p->rc.psz_zones);
1797 buf = s = x264_malloc( len );
1798 if( !buf )
1799 return NULL;
1801 if( b_res )
1803 s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
1804 s += sprintf( s, "fps=%u/%u ", p->i_fps_num, p->i_fps_den );
1805 s += sprintf( s, "timebase=%u/%u ", p->i_timebase_num, p->i_timebase_den );
1806 s += sprintf( s, "bitdepth=%d ", BIT_DEPTH );
1809 if( p->b_opencl )
1810 s += sprintf( s, "opencl=%d ", p->b_opencl );
1811 s += sprintf( s, "cabac=%d", p->b_cabac );
1812 s += sprintf( s, " ref=%d", p->i_frame_reference );
1813 s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
1814 p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
1815 s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
1816 s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
1817 s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
1818 s += sprintf( s, " psy=%d", p->analyse.b_psy );
1819 if( p->analyse.b_psy )
1821 s += sprintf( s, " fade_compensate=%.2f", p->rc.f_fade_compensate );
1822 s += sprintf( s, " psy_rd=%.2f:%.2f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis );
1824 s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
1825 s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
1826 s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
1827 s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
1828 s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
1829 s += sprintf( s, " cqm=%d", p->i_cqm_preset );
1830 s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
1831 s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
1832 s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
1833 s += sprintf( s, " threads=%d", p->i_threads );
1834 s += sprintf( s, " lookahead_threads=%d", p->i_lookahead_threads );
1835 s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
1836 if( p->i_slice_count )
1837 s += sprintf( s, " slices=%d", p->i_slice_count );
1838 if( p->i_slice_count_max )
1839 s += sprintf( s, " slices_max=%d", p->i_slice_count_max );
1840 if( p->i_slice_max_size )
1841 s += sprintf( s, " slice_max_size=%d", p->i_slice_max_size );
1842 if( p->i_slice_max_mbs )
1843 s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
1844 if( p->i_slice_min_mbs )
1845 s += sprintf( s, " slice_min_mbs=%d", p->i_slice_min_mbs );
1846 s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
1847 s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
1848 s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : p->b_fake_interlaced ? "fake" : "0" );
1849 s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
1850 if( p->b_stitchable )
1851 s += sprintf( s, " stitchable=%d", p->b_stitchable );
1853 s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1854 s += sprintf( s, " fgo=%d", p->analyse.i_fgo );
1856 s += sprintf( s, " bframes=%d", p->i_bframe );
1857 if( p->i_bframe )
1859 s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1860 p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1861 p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
1863 s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1865 if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1866 s += sprintf( s, " keyint=infinite" );
1867 else
1868 s += sprintf( s, " keyint=%d", p->i_keyint_max );
1869 s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1870 p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1872 if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1873 s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1875 s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1876 ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1877 : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1878 if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1880 if( p->rc.i_rc_method == X264_RC_CRF )
1881 s += sprintf( s, " crf=%.4f", p->rc.f_rf_constant );
1882 else
1883 s += sprintf( s, " bitrate=%d ratetol=%.1f",
1884 p->rc.i_bitrate, p->rc.f_rate_tolerance );
1885 s += sprintf( s, " qcomp=%.2f qpmin=%d:%d:%d qpmax=%d:%d:%d qpstep=%d",
1886 p->rc.f_qcompress,
1887 p->rc.i_qp_min[SLICE_TYPE_I], p->rc.i_qp_min[SLICE_TYPE_P], p->rc.i_qp_min[SLICE_TYPE_B],
1888 p->rc.i_qp_max[SLICE_TYPE_I], p->rc.i_qp_max[SLICE_TYPE_P], p->rc.i_qp_max[SLICE_TYPE_B],
1889 p->rc.i_qp_step );
1890 if( p->rc.b_stat_read )
1891 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1892 p->rc.f_complexity_blur, p->rc.f_qblur );
1893 if( p->rc.i_vbv_buffer_size )
1895 s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1896 p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1897 if( p->rc.i_rc_method == X264_RC_CRF )
1898 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1901 else if( p->rc.i_rc_method == X264_RC_CQP )
1902 s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1904 if( p->rc.i_vbv_buffer_size )
1905 s += sprintf( s, " nal_hrd=%s filler=%d", x264_nal_hrd_names[p->i_nal_hrd], p->rc.b_filler );
1906 if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
1907 s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
1908 p->crop_rect.i_right, p->crop_rect.i_bottom );
1909 if( p->i_frame_packing >= 0 )
1910 s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
1912 if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1914 s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1915 if( p->i_bframe && !p->rc.b_mb_tree )
1916 s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1917 s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1918 if( p->rc.i_aq_mode )
1920 s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1921 s += sprintf( s, " aq-sensitivity=%.2f", p->rc.f_aq_sensitivity );
1922 s += sprintf( s, " aq-factor=%.2f:%.2f:%.2f", p->rc.f_aq_ifactor,
1923 p->rc.f_aq_pfactor,
1924 p->rc.f_aq_bfactor );
1926 s += sprintf( s, " aq2=%d", p->rc.b_aq2 );
1927 if( p->rc.b_aq2 )
1929 s += sprintf( s, ":%.2f", p->rc.f_aq2_strength );
1930 s += sprintf( s, " aq2-sensitivity=%.2f", p->rc.f_aq2_sensitivity );
1931 s += sprintf( s, " aq2-factor=%.2f:%.2f:%.2f", p->rc.f_aq2_ifactor,
1932 p->rc.f_aq2_pfactor,
1933 p->rc.f_aq2_bfactor );
1935 s += sprintf( s, " aq3=%d", p->rc.i_aq3_mode );
1936 if( p->rc.i_aq3_mode )
1938 s += sprintf( s, ":[%.2f:%.2f]:[%.2f:%.2f]:[%.2f:%.2f]:[%.2f:%.2f]",
1939 p->rc.f_aq3_strengths[0][0], p->rc.f_aq3_strengths[1][0], p->rc.f_aq3_strengths[0][1], p->rc.f_aq3_strengths[1][1],
1940 p->rc.f_aq3_strengths[0][2], p->rc.f_aq3_strengths[1][2], p->rc.f_aq3_strengths[0][3], p->rc.f_aq3_strengths[1][3] );
1941 s += sprintf( s, " aq3-sensitivity=%.2f", p->rc.f_aq3_sensitivity );
1942 s += sprintf( s, " aq3-factor=[%.2f:%.2f]:[%.2f:%.2f]:[%.2f:%.2f]", p->rc.f_aq3_ifactor[0], p->rc.f_aq3_ifactor[1],
1943 p->rc.f_aq3_pfactor[0], p->rc.f_aq3_pfactor[1],
1944 p->rc.f_aq3_bfactor[0], p->rc.f_aq3_bfactor[1] );
1945 s += sprintf( s, " aq3-boundary=%d:%d:%d", p->rc.i_aq3_boundary[0], p->rc.i_aq3_boundary[1], p->rc.i_aq3_boundary[2] );
1947 if( p->rc.psz_zones )
1948 s += sprintf( s, " zones=%s", p->rc.psz_zones );
1949 else if( p->rc.i_zones )
1950 s += sprintf( s, " zones" );
1953 return buf;