]> git.sesse.net Git - x264/blob - common/common.c
Add support for AVC-Intra Class 200
[x264] / common / common.c
1 /*****************************************************************************
2  * common.c: misc common functions
3  *****************************************************************************
4  * Copyright (C) 2003-2014 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #include "common.h"
28
29 #include <stdarg.h>
30 #include <ctype.h>
31
32 #if HAVE_MALLOC_H
33 #include <malloc.h>
34 #endif
35 #if HAVE_THP
36 #include <sys/mman.h>
37 #endif
38
39 const int x264_bit_depth = BIT_DEPTH;
40
41 const int x264_chroma_format = X264_CHROMA_FORMAT;
42
43 static void x264_log_default( void *, int, const char *, va_list );
44
45 /****************************************************************************
46  * x264_param_default:
47  ****************************************************************************/
48 void x264_param_default( x264_param_t *param )
49 {
50     /* */
51     memset( param, 0, sizeof( x264_param_t ) );
52
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;
59
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->i_slice_max_size = 0;
77     param->i_slice_max_mbs = 0;
78     param->i_slice_count = 0;
79
80     /* Encoder parameters */
81     param->i_frame_reference = 3;
82     param->i_keyint_max = 250;
83     param->i_keyint_min = X264_KEYINT_MIN_AUTO;
84     param->i_bframe = 3;
85     param->i_scenecut_threshold = 40;
86     param->i_bframe_adaptive = X264_B_ADAPT_FAST;
87     param->i_bframe_bias = 0;
88     param->i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
89     param->b_interlaced = 0;
90     param->b_constrained_intra = 0;
91
92     param->b_deblocking_filter = 1;
93     param->i_deblocking_filter_alphac0 = 0;
94     param->i_deblocking_filter_beta = 0;
95
96     param->b_cabac = 1;
97     param->i_cabac_init_idc = 0;
98
99     param->rc.i_rc_method = X264_RC_CRF;
100     param->rc.i_bitrate = 0;
101     param->rc.f_rate_tolerance = 1.0;
102     param->rc.i_vbv_max_bitrate = 0;
103     param->rc.i_vbv_buffer_size = 0;
104     param->rc.f_vbv_buffer_init = 0.9;
105     param->rc.i_qp_constant = 23 + QP_BD_OFFSET;
106     param->rc.f_rf_constant = 23;
107     param->rc.i_qp_min = 0;
108     param->rc.i_qp_max = QP_MAX;
109     param->rc.i_qp_step = 4;
110     param->rc.f_ip_factor = 1.4;
111     param->rc.f_pb_factor = 1.3;
112     param->rc.i_aq_mode = X264_AQ_VARIANCE;
113     param->rc.f_aq_strength = 1.0;
114     param->rc.i_lookahead = 40;
115
116     param->rc.b_stat_write = 0;
117     param->rc.psz_stat_out = "x264_2pass.log";
118     param->rc.b_stat_read = 0;
119     param->rc.psz_stat_in = "x264_2pass.log";
120     param->rc.f_qcompress = 0.6;
121     param->rc.f_qblur = 0.5;
122     param->rc.f_complexity_blur = 20;
123     param->rc.i_zones = 0;
124     param->rc.b_mb_tree = 1;
125
126     /* Log */
127     param->pf_log = x264_log_default;
128     param->p_log_private = NULL;
129     param->i_log_level = X264_LOG_INFO;
130
131     /* */
132     param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
133     param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
134                          | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
135     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
136     param->analyse.i_me_method = X264_ME_HEX;
137     param->analyse.f_psy_rd = 1.0;
138     param->analyse.b_psy = 1;
139     param->analyse.f_psy_trellis = 0;
140     param->analyse.i_me_range = 16;
141     param->analyse.i_subpel_refine = 7;
142     param->analyse.b_mixed_references = 1;
143     param->analyse.b_chroma_me = 1;
144     param->analyse.i_mv_range_thread = -1;
145     param->analyse.i_mv_range = -1; // set from level_idc
146     param->analyse.i_chroma_qp_offset = 0;
147     param->analyse.b_fast_pskip = 1;
148     param->analyse.b_weighted_bipred = 1;
149     param->analyse.i_weighted_pred = X264_WEIGHTP_SMART;
150     param->analyse.b_dct_decimate = 1;
151     param->analyse.b_transform_8x8 = 1;
152     param->analyse.i_trellis = 1;
153     param->analyse.i_luma_deadzone[0] = 21;
154     param->analyse.i_luma_deadzone[1] = 11;
155     param->analyse.b_psnr = 0;
156     param->analyse.b_ssim = 0;
157
158     param->i_cqm_preset = X264_CQM_FLAT;
159     memset( param->cqm_4iy, 16, sizeof( param->cqm_4iy ) );
160     memset( param->cqm_4py, 16, sizeof( param->cqm_4py ) );
161     memset( param->cqm_4ic, 16, sizeof( param->cqm_4ic ) );
162     memset( param->cqm_4pc, 16, sizeof( param->cqm_4pc ) );
163     memset( param->cqm_8iy, 16, sizeof( param->cqm_8iy ) );
164     memset( param->cqm_8py, 16, sizeof( param->cqm_8py ) );
165     memset( param->cqm_8ic, 16, sizeof( param->cqm_8ic ) );
166     memset( param->cqm_8pc, 16, sizeof( param->cqm_8pc ) );
167
168     param->b_repeat_headers = 1;
169     param->b_annexb = 1;
170     param->b_aud = 0;
171     param->b_vfr_input = 1;
172     param->i_nal_hrd = X264_NAL_HRD_NONE;
173     param->b_tff = 1;
174     param->b_pic_struct = 0;
175     param->b_fake_interlaced = 0;
176     param->i_frame_packing = -1;
177     param->b_opencl = 0;
178     param->i_opencl_device = 0;
179     param->opencl_device_id = NULL;
180     param->psz_clbin_file = NULL;
181 }
182
183 static int x264_param_apply_preset( x264_param_t *param, const char *preset )
184 {
185     char *end;
186     int i = strtol( preset, &end, 10 );
187     if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
188         preset = x264_preset_names[i];
189
190     if( !strcasecmp( preset, "ultrafast" ) )
191     {
192         param->i_frame_reference = 1;
193         param->i_scenecut_threshold = 0;
194         param->b_deblocking_filter = 0;
195         param->b_cabac = 0;
196         param->i_bframe = 0;
197         param->analyse.intra = 0;
198         param->analyse.inter = 0;
199         param->analyse.b_transform_8x8 = 0;
200         param->analyse.i_me_method = X264_ME_DIA;
201         param->analyse.i_subpel_refine = 0;
202         param->rc.i_aq_mode = 0;
203         param->analyse.b_mixed_references = 0;
204         param->analyse.i_trellis = 0;
205         param->i_bframe_adaptive = X264_B_ADAPT_NONE;
206         param->rc.b_mb_tree = 0;
207         param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
208         param->analyse.b_weighted_bipred = 0;
209         param->rc.i_lookahead = 0;
210     }
211     else if( !strcasecmp( preset, "superfast" ) )
212     {
213         param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
214         param->analyse.i_me_method = X264_ME_DIA;
215         param->analyse.i_subpel_refine = 1;
216         param->i_frame_reference = 1;
217         param->analyse.b_mixed_references = 0;
218         param->analyse.i_trellis = 0;
219         param->rc.b_mb_tree = 0;
220         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
221         param->rc.i_lookahead = 0;
222     }
223     else if( !strcasecmp( preset, "veryfast" ) )
224     {
225         param->analyse.i_me_method = X264_ME_HEX;
226         param->analyse.i_subpel_refine = 2;
227         param->i_frame_reference = 1;
228         param->analyse.b_mixed_references = 0;
229         param->analyse.i_trellis = 0;
230         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
231         param->rc.i_lookahead = 10;
232     }
233     else if( !strcasecmp( preset, "faster" ) )
234     {
235         param->analyse.b_mixed_references = 0;
236         param->i_frame_reference = 2;
237         param->analyse.i_subpel_refine = 4;
238         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
239         param->rc.i_lookahead = 20;
240     }
241     else if( !strcasecmp( preset, "fast" ) )
242     {
243         param->i_frame_reference = 2;
244         param->analyse.i_subpel_refine = 6;
245         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
246         param->rc.i_lookahead = 30;
247     }
248     else if( !strcasecmp( preset, "medium" ) )
249     {
250         /* Default is medium */
251     }
252     else if( !strcasecmp( preset, "slow" ) )
253     {
254         param->analyse.i_me_method = X264_ME_UMH;
255         param->analyse.i_subpel_refine = 8;
256         param->i_frame_reference = 5;
257         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
258         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
259         param->rc.i_lookahead = 50;
260     }
261     else if( !strcasecmp( preset, "slower" ) )
262     {
263         param->analyse.i_me_method = X264_ME_UMH;
264         param->analyse.i_subpel_refine = 9;
265         param->i_frame_reference = 8;
266         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
267         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
268         param->analyse.inter |= X264_ANALYSE_PSUB8x8;
269         param->analyse.i_trellis = 2;
270         param->rc.i_lookahead = 60;
271     }
272     else if( !strcasecmp( preset, "veryslow" ) )
273     {
274         param->analyse.i_me_method = X264_ME_UMH;
275         param->analyse.i_subpel_refine = 10;
276         param->analyse.i_me_range = 24;
277         param->i_frame_reference = 16;
278         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
279         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
280         param->analyse.inter |= X264_ANALYSE_PSUB8x8;
281         param->analyse.i_trellis = 2;
282         param->i_bframe = 8;
283         param->rc.i_lookahead = 60;
284     }
285     else if( !strcasecmp( preset, "placebo" ) )
286     {
287         param->analyse.i_me_method = X264_ME_TESA;
288         param->analyse.i_subpel_refine = 11;
289         param->analyse.i_me_range = 24;
290         param->i_frame_reference = 16;
291         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
292         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
293         param->analyse.inter |= X264_ANALYSE_PSUB8x8;
294         param->analyse.b_fast_pskip = 0;
295         param->analyse.i_trellis = 2;
296         param->i_bframe = 16;
297         param->rc.i_lookahead = 60;
298     }
299     else
300     {
301         x264_log( NULL, X264_LOG_ERROR, "invalid preset '%s'\n", preset );
302         return -1;
303     }
304     return 0;
305 }
306
307 static int x264_param_apply_tune( x264_param_t *param, const char *tune )
308 {
309     char *tmp = x264_malloc( strlen( tune ) + 1 );
310     if( !tmp )
311         return -1;
312     tmp = strcpy( tmp, tune );
313     char *s = strtok( tmp, ",./-+" );
314     int psy_tuning_used = 0;
315     while( s )
316     {
317         if( !strncasecmp( s, "film", 4 ) )
318         {
319             if( psy_tuning_used++ ) goto psy_failure;
320             param->i_deblocking_filter_alphac0 = -1;
321             param->i_deblocking_filter_beta = -1;
322             param->analyse.f_psy_trellis = 0.15;
323         }
324         else if( !strncasecmp( s, "animation", 9 ) )
325         {
326             if( psy_tuning_used++ ) goto psy_failure;
327             param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
328             param->i_deblocking_filter_alphac0 = 1;
329             param->i_deblocking_filter_beta = 1;
330             param->analyse.f_psy_rd = 0.4;
331             param->rc.f_aq_strength = 0.6;
332             param->i_bframe += 2;
333         }
334         else if( !strncasecmp( s, "grain", 5 ) )
335         {
336             if( psy_tuning_used++ ) goto psy_failure;
337             param->i_deblocking_filter_alphac0 = -2;
338             param->i_deblocking_filter_beta = -2;
339             param->analyse.f_psy_trellis = 0.25;
340             param->analyse.b_dct_decimate = 0;
341             param->rc.f_pb_factor = 1.1;
342             param->rc.f_ip_factor = 1.1;
343             param->rc.f_aq_strength = 0.5;
344             param->analyse.i_luma_deadzone[0] = 6;
345             param->analyse.i_luma_deadzone[1] = 6;
346             param->rc.f_qcompress = 0.8;
347         }
348         else if( !strncasecmp( s, "stillimage", 10 ) )
349         {
350             if( psy_tuning_used++ ) goto psy_failure;
351             param->i_deblocking_filter_alphac0 = -3;
352             param->i_deblocking_filter_beta = -3;
353             param->analyse.f_psy_rd = 2.0;
354             param->analyse.f_psy_trellis = 0.7;
355             param->rc.f_aq_strength = 1.2;
356         }
357         else if( !strncasecmp( s, "psnr", 4 ) )
358         {
359             if( psy_tuning_used++ ) goto psy_failure;
360             param->rc.i_aq_mode = X264_AQ_NONE;
361             param->analyse.b_psy = 0;
362         }
363         else if( !strncasecmp( s, "ssim", 4 ) )
364         {
365             if( psy_tuning_used++ ) goto psy_failure;
366             param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
367             param->analyse.b_psy = 0;
368         }
369         else if( !strncasecmp( s, "fastdecode", 10 ) )
370         {
371             param->b_deblocking_filter = 0;
372             param->b_cabac = 0;
373             param->analyse.b_weighted_bipred = 0;
374             param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
375         }
376         else if( !strncasecmp( s, "zerolatency", 11 ) )
377         {
378             param->rc.i_lookahead = 0;
379             param->i_sync_lookahead = 0;
380             param->i_bframe = 0;
381             param->b_sliced_threads = 1;
382             param->b_vfr_input = 0;
383             param->rc.b_mb_tree = 0;
384         }
385         else if( !strncasecmp( s, "touhou", 6 ) )
386         {
387             if( psy_tuning_used++ ) goto psy_failure;
388             param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
389             param->i_deblocking_filter_alphac0 = -1;
390             param->i_deblocking_filter_beta = -1;
391             param->analyse.f_psy_trellis = 0.2;
392             param->rc.f_aq_strength = 1.3;
393             if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
394                 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
395         }
396         else
397         {
398             x264_log( NULL, X264_LOG_ERROR, "invalid tune '%s'\n", s );
399             x264_free( tmp );
400             return -1;
401         }
402         if( 0 )
403         {
404     psy_failure:
405             x264_log( NULL, X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %s\n", s );
406         }
407         s = strtok( NULL, ",./-+" );
408     }
409     x264_free( tmp );
410     return 0;
411 }
412
413 int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
414 {
415     x264_param_default( param );
416
417     if( preset && x264_param_apply_preset( param, preset ) < 0 )
418         return -1;
419     if( tune && x264_param_apply_tune( param, tune ) < 0 )
420         return -1;
421     return 0;
422 }
423
424 void x264_param_apply_fastfirstpass( x264_param_t *param )
425 {
426     /* Set faster options in case of turbo firstpass. */
427     if( param->rc.b_stat_write && !param->rc.b_stat_read )
428     {
429         param->i_frame_reference = 1;
430         param->analyse.b_transform_8x8 = 0;
431         param->analyse.inter = 0;
432         param->analyse.i_me_method = X264_ME_DIA;
433         param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
434         param->analyse.i_trellis = 0;
435         param->analyse.b_fast_pskip = 1;
436     }
437 }
438
439 static int profile_string_to_int( const char *str )
440 {
441     if( !strcasecmp( str, "baseline" ) )
442         return PROFILE_BASELINE;
443     if( !strcasecmp( str, "main" ) )
444         return PROFILE_MAIN;
445     if( !strcasecmp( str, "high" ) )
446         return PROFILE_HIGH;
447     if( !strcasecmp( str, "high10" ) )
448         return PROFILE_HIGH10;
449     if( !strcasecmp( str, "high422" ) )
450         return PROFILE_HIGH422;
451     if( !strcasecmp( str, "high444" ) )
452         return PROFILE_HIGH444_PREDICTIVE;
453     return -1;
454 }
455
456 int x264_param_apply_profile( x264_param_t *param, const char *profile )
457 {
458     if( !profile )
459         return 0;
460
461     int p = profile_string_to_int( profile );
462     if( p < 0 )
463     {
464         x264_log( NULL, X264_LOG_ERROR, "invalid profile: %s\n", profile );
465         return -1;
466     }
467     if( p < PROFILE_HIGH444_PREDICTIVE && ((param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
468         (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0)) )
469     {
470         x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
471         return -1;
472     }
473     if( p < PROFILE_HIGH444_PREDICTIVE && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I444 )
474     {
475         x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:4:4\n", profile );
476         return -1;
477     }
478     if( p < PROFILE_HIGH422 && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I422 )
479     {
480         x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support 4:2:2\n", profile );
481         return -1;
482     }
483     if( p < PROFILE_HIGH10 && BIT_DEPTH > 8 )
484     {
485         x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d\n", profile, BIT_DEPTH );
486         return -1;
487     }
488
489     if( p == PROFILE_BASELINE )
490     {
491         param->analyse.b_transform_8x8 = 0;
492         param->b_cabac = 0;
493         param->i_cqm_preset = X264_CQM_FLAT;
494         param->psz_cqm_file = NULL;
495         param->i_bframe = 0;
496         param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
497         if( param->b_interlaced )
498         {
499             x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
500             return -1;
501         }
502         if( param->b_fake_interlaced )
503         {
504             x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
505             return -1;
506         }
507     }
508     else if( p == PROFILE_MAIN )
509     {
510         param->analyse.b_transform_8x8 = 0;
511         param->i_cqm_preset = X264_CQM_FLAT;
512         param->psz_cqm_file = NULL;
513     }
514     return 0;
515 }
516
517 static int parse_enum( const char *arg, const char * const *names, int *dst )
518 {
519     for( int i = 0; names[i]; i++ )
520         if( !strcmp( arg, names[i] ) )
521         {
522             *dst = i;
523             return 0;
524         }
525     return -1;
526 }
527
528 static int parse_cqm( const char *str, uint8_t *cqm, int length )
529 {
530     int i = 0;
531     do {
532         int coef;
533         if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
534             return -1;
535         cqm[i++] = coef;
536     } while( i < length && (str = strchr( str, ',' )) && str++ );
537     return (i == length) ? 0 : -1;
538 }
539
540 static int x264_atobool( const char *str, int *b_error )
541 {
542     if( !strcmp(str, "1") ||
543         !strcmp(str, "true") ||
544         !strcmp(str, "yes") )
545         return 1;
546     if( !strcmp(str, "0") ||
547         !strcmp(str, "false") ||
548         !strcmp(str, "no") )
549         return 0;
550     *b_error = 1;
551     return 0;
552 }
553
554 static int x264_atoi( const char *str, int *b_error )
555 {
556     char *end;
557     int v = strtol( str, &end, 0 );
558     if( end == str || *end != '\0' )
559         *b_error = 1;
560     return v;
561 }
562
563 static double x264_atof( const char *str, int *b_error )
564 {
565     char *end;
566     double v = strtod( str, &end );
567     if( end == str || *end != '\0' )
568         *b_error = 1;
569     return v;
570 }
571
572 #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
573 #undef atoi
574 #undef atof
575 #define atoi(str) x264_atoi( str, &b_error )
576 #define atof(str) x264_atof( str, &b_error )
577
578 int x264_param_parse( x264_param_t *p, const char *name, const char *value )
579 {
580     char *name_buf = NULL;
581     int b_error = 0;
582     int name_was_bool;
583     int value_was_null = !value;
584     int i;
585
586     if( !name )
587         return X264_PARAM_BAD_NAME;
588     if( !value )
589         value = "true";
590
591     if( value[0] == '=' )
592         value++;
593
594     if( strchr( name, '_' ) ) // s/_/-/g
595     {
596         char *c;
597         name_buf = strdup(name);
598         while( (c = strchr( name_buf, '_' )) )
599             *c = '-';
600         name = name_buf;
601     }
602
603     if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
604         (!strncmp( name, "no", 2 ) && (i = 2)) )
605     {
606         name += i;
607         value = atobool(value) ? "false" : "true";
608     }
609     name_was_bool = 0;
610
611 #define OPT(STR) else if( !strcmp( name, STR ) )
612 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
613     if(0);
614     OPT("asm")
615     {
616         p->cpu = isdigit(value[0]) ? atoi(value) :
617                  !strcmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
618         if( b_error )
619         {
620             char *buf = strdup(value);
621             char *tok, UNUSED *saveptr=NULL, *init;
622             b_error = 0;
623             p->cpu = 0;
624             for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
625             {
626                 for( i=0; x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name); i++ );
627                 p->cpu |= x264_cpu_names[i].flags;
628                 if( !x264_cpu_names[i].flags )
629                     b_error = 1;
630             }
631             free( buf );
632             if( (p->cpu&X264_CPU_SSSE3) && !(p->cpu&X264_CPU_SSE2_IS_SLOW) )
633                 p->cpu |= X264_CPU_SSE2_IS_FAST;
634         }
635     }
636     OPT("threads")
637     {
638         if( !strcmp(value, "auto") )
639             p->i_threads = X264_THREADS_AUTO;
640         else
641             p->i_threads = atoi(value);
642     }
643     OPT("lookahead-threads")
644     {
645         if( !strcmp(value, "auto") )
646             p->i_lookahead_threads = X264_THREADS_AUTO;
647         else
648             p->i_lookahead_threads = atoi(value);
649     }
650     OPT("sliced-threads")
651         p->b_sliced_threads = atobool(value);
652     OPT("sync-lookahead")
653     {
654         if( !strcmp(value, "auto") )
655             p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
656         else
657             p->i_sync_lookahead = atoi(value);
658     }
659     OPT2("deterministic", "n-deterministic")
660         p->b_deterministic = atobool(value);
661     OPT("cpu-independent")
662         p->b_cpu_independent = atobool(value);
663     OPT2("level", "level-idc")
664     {
665         if( !strcmp(value, "1b") )
666             p->i_level_idc = 9;
667         else if( atof(value) < 6 )
668             p->i_level_idc = (int)(10*atof(value)+.5);
669         else
670             p->i_level_idc = atoi(value);
671     }
672     OPT("bluray-compat")
673         p->b_bluray_compat = atobool(value);
674     OPT("avcintra-class")
675         p->i_avcintra_class = atoi(value);
676     OPT("sar")
677     {
678         b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
679                     2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
680     }
681     OPT("overscan")
682         b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
683     OPT("videoformat")
684         b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
685     OPT("fullrange")
686         b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
687     OPT("colorprim")
688         b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
689     OPT("transfer")
690         b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
691     OPT("colormatrix")
692         b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
693     OPT("chromaloc")
694     {
695         p->vui.i_chroma_loc = atoi(value);
696         b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
697     }
698     OPT("fps")
699     {
700         if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) == 2 )
701             ;
702         else
703         {
704             float fps = atof(value);
705             if( fps > 0 && fps <= INT_MAX/1000 )
706             {
707                 p->i_fps_num = (int)(fps * 1000 + .5);
708                 p->i_fps_den = 1000;
709             }
710             else
711             {
712                 p->i_fps_num = atoi(value);
713                 p->i_fps_den = 1;
714             }
715         }
716     }
717     OPT2("ref", "frameref")
718         p->i_frame_reference = atoi(value);
719     OPT("dpb-size")
720         p->i_dpb_size = atoi(value);
721     OPT("keyint")
722     {
723         if( strstr( value, "infinite" ) )
724             p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
725         else
726             p->i_keyint_max = atoi(value);
727     }
728     OPT2("min-keyint", "keyint-min")
729     {
730         p->i_keyint_min = atoi(value);
731         if( p->i_keyint_max < p->i_keyint_min )
732             p->i_keyint_max = p->i_keyint_min;
733     }
734     OPT("scenecut")
735     {
736         p->i_scenecut_threshold = atobool(value);
737         if( b_error || p->i_scenecut_threshold )
738         {
739             b_error = 0;
740             p->i_scenecut_threshold = atoi(value);
741         }
742     }
743     OPT("intra-refresh")
744         p->b_intra_refresh = atobool(value);
745     OPT("bframes")
746         p->i_bframe = atoi(value);
747     OPT("b-adapt")
748     {
749         p->i_bframe_adaptive = atobool(value);
750         if( b_error )
751         {
752             b_error = 0;
753             p->i_bframe_adaptive = atoi(value);
754         }
755     }
756     OPT("b-bias")
757         p->i_bframe_bias = atoi(value);
758     OPT("b-pyramid")
759     {
760         b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
761         if( b_error )
762         {
763             b_error = 0;
764             p->i_bframe_pyramid = atoi(value);
765         }
766     }
767     OPT("open-gop")
768         p->b_open_gop = atobool(value);
769     OPT("nf")
770         p->b_deblocking_filter = !atobool(value);
771     OPT2("filter", "deblock")
772     {
773         if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
774             2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
775         {
776             p->b_deblocking_filter = 1;
777         }
778         else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
779         {
780             p->b_deblocking_filter = 1;
781             p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
782         }
783         else
784             p->b_deblocking_filter = atobool(value);
785     }
786     OPT("slice-max-size")
787         p->i_slice_max_size = atoi(value);
788     OPT("slice-max-mbs")
789         p->i_slice_max_mbs = atoi(value);
790     OPT("slice-min-mbs")
791         p->i_slice_min_mbs = atoi(value);
792     OPT("slices")
793         p->i_slice_count = atoi(value);
794     OPT("slices-max")
795         p->i_slice_count_max = atoi(value);
796     OPT("cabac")
797         p->b_cabac = atobool(value);
798     OPT("cabac-idc")
799         p->i_cabac_init_idc = atoi(value);
800     OPT("interlaced")
801         p->b_interlaced = atobool(value);
802     OPT("tff")
803         p->b_interlaced = p->b_tff = atobool(value);
804     OPT("bff")
805     {
806         p->b_interlaced = atobool(value);
807         p->b_tff = !p->b_interlaced;
808     }
809     OPT("constrained-intra")
810         p->b_constrained_intra = atobool(value);
811     OPT("cqm")
812     {
813         if( strstr( value, "flat" ) )
814             p->i_cqm_preset = X264_CQM_FLAT;
815         else if( strstr( value, "jvt" ) )
816             p->i_cqm_preset = X264_CQM_JVT;
817         else
818             p->psz_cqm_file = strdup(value);
819     }
820     OPT("cqmfile")
821         p->psz_cqm_file = strdup(value);
822     OPT("cqm4")
823     {
824         p->i_cqm_preset = X264_CQM_CUSTOM;
825         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
826         b_error |= parse_cqm( value, p->cqm_4py, 16 );
827         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
828         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
829     }
830     OPT("cqm8")
831     {
832         p->i_cqm_preset = X264_CQM_CUSTOM;
833         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
834         b_error |= parse_cqm( value, p->cqm_8py, 64 );
835         b_error |= parse_cqm( value, p->cqm_8ic, 64 );
836         b_error |= parse_cqm( value, p->cqm_8pc, 64 );
837     }
838     OPT("cqm4i")
839     {
840         p->i_cqm_preset = X264_CQM_CUSTOM;
841         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
842         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
843     }
844     OPT("cqm4p")
845     {
846         p->i_cqm_preset = X264_CQM_CUSTOM;
847         b_error |= parse_cqm( value, p->cqm_4py, 16 );
848         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
849     }
850     OPT("cqm4iy")
851     {
852         p->i_cqm_preset = X264_CQM_CUSTOM;
853         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
854     }
855     OPT("cqm4ic")
856     {
857         p->i_cqm_preset = X264_CQM_CUSTOM;
858         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
859     }
860     OPT("cqm4py")
861     {
862         p->i_cqm_preset = X264_CQM_CUSTOM;
863         b_error |= parse_cqm( value, p->cqm_4py, 16 );
864     }
865     OPT("cqm4pc")
866     {
867         p->i_cqm_preset = X264_CQM_CUSTOM;
868         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
869     }
870     OPT("cqm8i")
871     {
872         p->i_cqm_preset = X264_CQM_CUSTOM;
873         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
874         b_error |= parse_cqm( value, p->cqm_8ic, 64 );
875     }
876     OPT("cqm8p")
877     {
878         p->i_cqm_preset = X264_CQM_CUSTOM;
879         b_error |= parse_cqm( value, p->cqm_8py, 64 );
880         b_error |= parse_cqm( value, p->cqm_8pc, 64 );
881     }
882     OPT("log")
883         p->i_log_level = atoi(value);
884     OPT("dump-yuv")
885         p->psz_dump_yuv = strdup(value);
886     OPT2("analyse", "partitions")
887     {
888         p->analyse.inter = 0;
889         if( strstr( value, "none" ) )  p->analyse.inter =  0;
890         if( strstr( value, "all" ) )   p->analyse.inter = ~0;
891
892         if( strstr( value, "i4x4" ) )  p->analyse.inter |= X264_ANALYSE_I4x4;
893         if( strstr( value, "i8x8" ) )  p->analyse.inter |= X264_ANALYSE_I8x8;
894         if( strstr( value, "p8x8" ) )  p->analyse.inter |= X264_ANALYSE_PSUB16x16;
895         if( strstr( value, "p4x4" ) )  p->analyse.inter |= X264_ANALYSE_PSUB8x8;
896         if( strstr( value, "b8x8" ) )  p->analyse.inter |= X264_ANALYSE_BSUB16x16;
897     }
898     OPT("8x8dct")
899         p->analyse.b_transform_8x8 = atobool(value);
900     OPT2("weightb", "weight-b")
901         p->analyse.b_weighted_bipred = atobool(value);
902     OPT("weightp")
903         p->analyse.i_weighted_pred = atoi(value);
904     OPT2("direct", "direct-pred")
905         b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
906     OPT("chroma-qp-offset")
907         p->analyse.i_chroma_qp_offset = atoi(value);
908     OPT("me")
909         b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
910     OPT2("merange", "me-range")
911         p->analyse.i_me_range = atoi(value);
912     OPT2("mvrange", "mv-range")
913         p->analyse.i_mv_range = atoi(value);
914     OPT2("mvrange-thread", "mv-range-thread")
915         p->analyse.i_mv_range_thread = atoi(value);
916     OPT2("subme", "subq")
917         p->analyse.i_subpel_refine = atoi(value);
918     OPT("psy-rd")
919     {
920         if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
921             2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
922             2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
923         { }
924         else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
925         {
926             p->analyse.f_psy_trellis = 0;
927         }
928         else
929         {
930             p->analyse.f_psy_rd = 0;
931             p->analyse.f_psy_trellis = 0;
932         }
933     }
934     OPT("psy")
935         p->analyse.b_psy = atobool(value);
936     OPT("chroma-me")
937         p->analyse.b_chroma_me = atobool(value);
938     OPT("mixed-refs")
939         p->analyse.b_mixed_references = atobool(value);
940     OPT("trellis")
941         p->analyse.i_trellis = atoi(value);
942     OPT("fast-pskip")
943         p->analyse.b_fast_pskip = atobool(value);
944     OPT("dct-decimate")
945         p->analyse.b_dct_decimate = atobool(value);
946     OPT("deadzone-inter")
947         p->analyse.i_luma_deadzone[0] = atoi(value);
948     OPT("deadzone-intra")
949         p->analyse.i_luma_deadzone[1] = atoi(value);
950     OPT("nr")
951         p->analyse.i_noise_reduction = atoi(value);
952     OPT("bitrate")
953     {
954         p->rc.i_bitrate = atoi(value);
955         p->rc.i_rc_method = X264_RC_ABR;
956     }
957     OPT2("qp", "qp_constant")
958     {
959         p->rc.i_qp_constant = atoi(value);
960         p->rc.i_rc_method = X264_RC_CQP;
961     }
962     OPT("crf")
963     {
964         p->rc.f_rf_constant = atof(value);
965         p->rc.i_rc_method = X264_RC_CRF;
966     }
967     OPT("crf-max")
968         p->rc.f_rf_constant_max = atof(value);
969     OPT("rc-lookahead")
970         p->rc.i_lookahead = atoi(value);
971     OPT2("qpmin", "qp-min")
972         p->rc.i_qp_min = atoi(value);
973     OPT2("qpmax", "qp-max")
974         p->rc.i_qp_max = atoi(value);
975     OPT2("qpstep", "qp-step")
976         p->rc.i_qp_step = atoi(value);
977     OPT("ratetol")
978         p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
979     OPT("vbv-maxrate")
980         p->rc.i_vbv_max_bitrate = atoi(value);
981     OPT("vbv-bufsize")
982         p->rc.i_vbv_buffer_size = atoi(value);
983     OPT("vbv-init")
984         p->rc.f_vbv_buffer_init = atof(value);
985     OPT2("ipratio", "ip-factor")
986         p->rc.f_ip_factor = atof(value);
987     OPT2("pbratio", "pb-factor")
988         p->rc.f_pb_factor = atof(value);
989     OPT("aq-mode")
990         p->rc.i_aq_mode = atoi(value);
991     OPT("aq-strength")
992         p->rc.f_aq_strength = atof(value);
993     OPT("pass")
994     {
995         int pass = x264_clip3( atoi(value), 0, 3 );
996         p->rc.b_stat_write = pass & 1;
997         p->rc.b_stat_read = pass & 2;
998     }
999     OPT("stats")
1000     {
1001         p->rc.psz_stat_in = strdup(value);
1002         p->rc.psz_stat_out = strdup(value);
1003     }
1004     OPT("qcomp")
1005         p->rc.f_qcompress = atof(value);
1006     OPT("mbtree")
1007         p->rc.b_mb_tree = atobool(value);
1008     OPT("qblur")
1009         p->rc.f_qblur = atof(value);
1010     OPT2("cplxblur", "cplx-blur")
1011         p->rc.f_complexity_blur = atof(value);
1012     OPT("zones")
1013         p->rc.psz_zones = strdup(value);
1014     OPT("crop-rect")
1015         b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
1016                                                  &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
1017     OPT("psnr")
1018         p->analyse.b_psnr = atobool(value);
1019     OPT("ssim")
1020         p->analyse.b_ssim = atobool(value);
1021     OPT("aud")
1022         p->b_aud = atobool(value);
1023     OPT("sps-id")
1024         p->i_sps_id = atoi(value);
1025     OPT("global-header")
1026         p->b_repeat_headers = !atobool(value);
1027     OPT("repeat-headers")
1028         p->b_repeat_headers = atobool(value);
1029     OPT("annexb")
1030         p->b_annexb = atobool(value);
1031     OPT("force-cfr")
1032         p->b_vfr_input = !atobool(value);
1033     OPT("nal-hrd")
1034         b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
1035     OPT("filler")
1036         p->rc.b_filler = atobool(value);
1037     OPT("pic-struct")
1038         p->b_pic_struct = atobool(value);
1039     OPT("fake-interlaced")
1040         p->b_fake_interlaced = atobool(value);
1041     OPT("frame-packing")
1042         p->i_frame_packing = atoi(value);
1043     OPT("stitchable")
1044         p->b_stitchable = atobool(value);
1045     OPT("opencl")
1046         p->b_opencl = atobool( value );
1047     OPT("opencl-clbin")
1048         p->psz_clbin_file = strdup( value );
1049     OPT("opencl-device")
1050         p->i_opencl_device = atoi( value );
1051     else
1052         return X264_PARAM_BAD_NAME;
1053 #undef OPT
1054 #undef OPT2
1055 #undef atobool
1056 #undef atoi
1057 #undef atof
1058
1059     if( name_buf )
1060         free( name_buf );
1061
1062     b_error |= value_was_null && !name_was_bool;
1063     return b_error ? X264_PARAM_BAD_VALUE : 0;
1064 }
1065
1066 /****************************************************************************
1067  * x264_log:
1068  ****************************************************************************/
1069 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
1070 {
1071     if( !h || i_level <= h->param.i_log_level )
1072     {
1073         va_list arg;
1074         va_start( arg, psz_fmt );
1075         if( !h )
1076             x264_log_default( NULL, i_level, psz_fmt, arg );
1077         else
1078             h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
1079         va_end( arg );
1080     }
1081 }
1082
1083 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
1084 {
1085     char *psz_prefix;
1086     switch( i_level )
1087     {
1088         case X264_LOG_ERROR:
1089             psz_prefix = "error";
1090             break;
1091         case X264_LOG_WARNING:
1092             psz_prefix = "warning";
1093             break;
1094         case X264_LOG_INFO:
1095             psz_prefix = "info";
1096             break;
1097         case X264_LOG_DEBUG:
1098             psz_prefix = "debug";
1099             break;
1100         default:
1101             psz_prefix = "unknown";
1102             break;
1103     }
1104     fprintf( stderr, "x264 [%s]: ", psz_prefix );
1105     x264_vfprintf( stderr, psz_fmt, arg );
1106 }
1107
1108 /****************************************************************************
1109  * x264_picture_init:
1110  ****************************************************************************/
1111 void x264_picture_init( x264_picture_t *pic )
1112 {
1113     memset( pic, 0, sizeof( x264_picture_t ) );
1114     pic->i_type = X264_TYPE_AUTO;
1115     pic->i_qpplus1 = X264_QP_AUTO;
1116     pic->i_pic_struct = PIC_STRUCT_AUTO;
1117 }
1118
1119 /****************************************************************************
1120  * x264_picture_alloc:
1121  ****************************************************************************/
1122 int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
1123 {
1124     typedef struct
1125     {
1126         int planes;
1127         int width_fix8[3];
1128         int height_fix8[3];
1129     } x264_csp_tab_t;
1130
1131     static const x264_csp_tab_t x264_csp_tab[] =
1132     {
1133         [X264_CSP_I420] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1134         [X264_CSP_YV12] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1135         [X264_CSP_NV12] = { 2, { 256*1, 256*1 },        { 256*1, 256/2 },       },
1136         [X264_CSP_I422] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1137         [X264_CSP_YV16] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1138         [X264_CSP_NV16] = { 2, { 256*1, 256*1 },        { 256*1, 256*1 },       },
1139         [X264_CSP_I444] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1140         [X264_CSP_YV24] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1141         [X264_CSP_BGR]  = { 1, { 256*3 },               { 256*1 },              },
1142         [X264_CSP_BGRA] = { 1, { 256*4 },               { 256*1 },              },
1143         [X264_CSP_RGB]  = { 1, { 256*3 },               { 256*1 },              },
1144     };
1145
1146     int csp = i_csp & X264_CSP_MASK;
1147     if( csp <= X264_CSP_NONE || csp >= X264_CSP_MAX || csp == X264_CSP_V210 )
1148         return -1;
1149     x264_picture_init( pic );
1150     pic->img.i_csp = i_csp;
1151     pic->img.i_plane = x264_csp_tab[csp].planes;
1152     int depth_factor = i_csp & X264_CSP_HIGH_DEPTH ? 2 : 1;
1153     int plane_offset[3] = {0};
1154     int frame_size = 0;
1155     for( int i = 0; i < pic->img.i_plane; i++ )
1156     {
1157         int stride = (((int64_t)i_width * x264_csp_tab[csp].width_fix8[i]) >> 8) * depth_factor;
1158         int plane_size = (((int64_t)i_height * x264_csp_tab[csp].height_fix8[i]) >> 8) * stride;
1159         pic->img.i_stride[i] = stride;
1160         plane_offset[i] = frame_size;
1161         frame_size += plane_size;
1162     }
1163     pic->img.plane[0] = x264_malloc( frame_size );
1164     if( !pic->img.plane[0] )
1165         return -1;
1166     for( int i = 1; i < pic->img.i_plane; i++ )
1167         pic->img.plane[i] = pic->img.plane[0] + plane_offset[i];
1168     return 0;
1169 }
1170
1171 /****************************************************************************
1172  * x264_picture_clean:
1173  ****************************************************************************/
1174 void x264_picture_clean( x264_picture_t *pic )
1175 {
1176     x264_free( pic->img.plane[0] );
1177
1178     /* just to be safe */
1179     memset( pic, 0, sizeof( x264_picture_t ) );
1180 }
1181
1182 /****************************************************************************
1183  * x264_malloc:
1184  ****************************************************************************/
1185 void *x264_malloc( int i_size )
1186 {
1187     uint8_t *align_buf = NULL;
1188 #if HAVE_MALLOC_H
1189 #if HAVE_THP
1190 #define HUGE_PAGE_SIZE 2*1024*1024
1191 #define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
1192     /* Attempt to allocate huge pages to reduce TLB misses. */
1193     if( i_size >= HUGE_PAGE_THRESHOLD )
1194     {
1195         align_buf = memalign( HUGE_PAGE_SIZE, i_size );
1196         if( align_buf )
1197         {
1198             /* Round up to the next huge page boundary if we are close enough. */
1199             size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
1200             madvise( align_buf, madv_size, MADV_HUGEPAGE );
1201         }
1202     }
1203     else
1204 #undef HUGE_PAGE_SIZE
1205 #undef HUGE_PAGE_THRESHOLD
1206 #endif
1207         align_buf = memalign( NATIVE_ALIGN, i_size );
1208 #else
1209     uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
1210     if( buf )
1211     {
1212         align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
1213         align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
1214         *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
1215     }
1216 #endif
1217     if( !align_buf )
1218         x264_log( NULL, X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
1219     return align_buf;
1220 }
1221
1222 /****************************************************************************
1223  * x264_free:
1224  ****************************************************************************/
1225 void x264_free( void *p )
1226 {
1227     if( p )
1228     {
1229 #if HAVE_MALLOC_H
1230         free( p );
1231 #else
1232         free( *( ( ( void **) p ) - 1 ) );
1233 #endif
1234     }
1235 }
1236
1237 /****************************************************************************
1238  * x264_reduce_fraction:
1239  ****************************************************************************/
1240 #define REDUCE_FRACTION( name, type )\
1241 void name( type *n, type *d )\
1242 {                   \
1243     type a = *n;    \
1244     type b = *d;    \
1245     type c;         \
1246     if( !a || !b )  \
1247         return;     \
1248     c = a % b;      \
1249     while( c )      \
1250     {               \
1251         a = b;      \
1252         b = c;      \
1253         c = a % b;  \
1254     }               \
1255     *n /= b;        \
1256     *d /= b;        \
1257 }
1258
1259 REDUCE_FRACTION( x264_reduce_fraction  , uint32_t )
1260 REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
1261
1262 /****************************************************************************
1263  * x264_slurp_file:
1264  ****************************************************************************/
1265 char *x264_slurp_file( const char *filename )
1266 {
1267     int b_error = 0;
1268     size_t i_size;
1269     char *buf;
1270     FILE *fh = x264_fopen( filename, "rb" );
1271     if( !fh )
1272         return NULL;
1273     b_error |= fseek( fh, 0, SEEK_END ) < 0;
1274     b_error |= ( i_size = ftell( fh ) ) <= 0;
1275     b_error |= fseek( fh, 0, SEEK_SET ) < 0;
1276     if( b_error )
1277         goto error;
1278     buf = x264_malloc( i_size+2 );
1279     if( !buf )
1280         goto error;
1281     b_error |= fread( buf, 1, i_size, fh ) != i_size;
1282     if( buf[i_size-1] != '\n' )
1283         buf[i_size++] = '\n';
1284     buf[i_size] = 0;
1285     fclose( fh );
1286     if( b_error )
1287     {
1288         x264_free( buf );
1289         return NULL;
1290     }
1291     return buf;
1292 error:
1293     fclose( fh );
1294     return NULL;
1295 }
1296
1297 /****************************************************************************
1298  * x264_param2string:
1299  ****************************************************************************/
1300 char *x264_param2string( x264_param_t *p, int b_res )
1301 {
1302     int len = 1000;
1303     char *buf, *s;
1304     if( p->rc.psz_zones )
1305         len += strlen(p->rc.psz_zones);
1306     buf = s = x264_malloc( len );
1307     if( !buf )
1308         return NULL;
1309
1310     if( b_res )
1311     {
1312         s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
1313         s += sprintf( s, "fps=%u/%u ", p->i_fps_num, p->i_fps_den );
1314         s += sprintf( s, "timebase=%u/%u ", p->i_timebase_num, p->i_timebase_den );
1315         s += sprintf( s, "bitdepth=%d ", BIT_DEPTH );
1316     }
1317
1318     if( p->b_opencl )
1319         s += sprintf( s, "opencl=%d ", p->b_opencl );
1320     s += sprintf( s, "cabac=%d", p->b_cabac );
1321     s += sprintf( s, " ref=%d", p->i_frame_reference );
1322     s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
1323                   p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
1324     s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
1325     s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
1326     s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
1327     s += sprintf( s, " psy=%d", p->analyse.b_psy );
1328     if( p->analyse.b_psy )
1329         s += sprintf( s, " psy_rd=%.2f:%.2f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis );
1330     s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
1331     s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
1332     s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
1333     s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
1334     s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
1335     s += sprintf( s, " cqm=%d", p->i_cqm_preset );
1336     s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
1337     s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
1338     s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
1339     s += sprintf( s, " threads=%d", p->i_threads );
1340     s += sprintf( s, " lookahead_threads=%d", p->i_lookahead_threads );
1341     s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
1342     if( p->i_slice_count )
1343         s += sprintf( s, " slices=%d", p->i_slice_count );
1344     if( p->i_slice_count_max )
1345         s += sprintf( s, " slices_max=%d", p->i_slice_count_max );
1346     if( p->i_slice_max_size )
1347         s += sprintf( s, " slice_max_size=%d", p->i_slice_max_size );
1348     if( p->i_slice_max_mbs )
1349         s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
1350     if( p->i_slice_min_mbs )
1351         s += sprintf( s, " slice_min_mbs=%d", p->i_slice_min_mbs );
1352     s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
1353     s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
1354     s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : p->b_fake_interlaced ? "fake" : "0" );
1355     s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
1356     if( p->b_stitchable )
1357         s += sprintf( s, " stitchable=%d", p->b_stitchable );
1358
1359     s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1360
1361     s += sprintf( s, " bframes=%d", p->i_bframe );
1362     if( p->i_bframe )
1363     {
1364         s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1365                       p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1366                       p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
1367     }
1368     s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1369
1370     if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1371         s += sprintf( s, " keyint=infinite" );
1372     else
1373         s += sprintf( s, " keyint=%d", p->i_keyint_max );
1374     s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1375                   p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1376
1377     if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1378         s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1379
1380     s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1381                                ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1382                                : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1383     if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1384     {
1385         if( p->rc.i_rc_method == X264_RC_CRF )
1386             s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
1387         else
1388             s += sprintf( s, " bitrate=%d ratetol=%.1f",
1389                           p->rc.i_bitrate, p->rc.f_rate_tolerance );
1390         s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1391                       p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
1392         if( p->rc.b_stat_read )
1393             s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1394                           p->rc.f_complexity_blur, p->rc.f_qblur );
1395         if( p->rc.i_vbv_buffer_size )
1396         {
1397             s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1398                           p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1399             if( p->rc.i_rc_method == X264_RC_CRF )
1400                 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1401         }
1402     }
1403     else if( p->rc.i_rc_method == X264_RC_CQP )
1404         s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1405
1406     if( p->rc.i_vbv_buffer_size )
1407         s += sprintf( s, " nal_hrd=%s filler=%d", x264_nal_hrd_names[p->i_nal_hrd], p->rc.b_filler );
1408     if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
1409         s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
1410                                                    p->crop_rect.i_right, p->crop_rect.i_bottom );
1411     if( p->i_frame_packing >= 0 )
1412         s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
1413
1414     if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1415     {
1416         s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1417         if( p->i_bframe && !p->rc.b_mb_tree )
1418             s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1419         s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1420         if( p->rc.i_aq_mode )
1421             s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1422         if( p->rc.psz_zones )
1423             s += sprintf( s, " zones=%s", p->rc.psz_zones );
1424         else if( p->rc.i_zones )
1425             s += sprintf( s, " zones" );
1426     }
1427
1428     return buf;
1429 }
1430