]> git.sesse.net Git - x264/blob - common/common.c
Change the predictors update algorithm
[x264] / common / common.c
1 /*****************************************************************************
2  * common.c: misc common functions
3  *****************************************************************************
4  * Copyright (C) 2003-2015 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( !strcasecmp( 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         !strcasecmp(str, "true") ||
544         !strcasecmp(str, "yes") )
545         return 1;
546     if( !strcmp(str, "0") ||
547         !strcasecmp(str, "false") ||
548         !strcasecmp(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 errortype = X264_PARAM_BAD_VALUE;
583     int name_was_bool;
584     int value_was_null = !value;
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         if( !name_buf )
599             return X264_PARAM_BAD_NAME;
600         while( (c = strchr( name_buf, '_' )) )
601             *c = '-';
602         name = name_buf;
603     }
604
605     if( !strncmp( name, "no", 2 ) )
606     {
607         name += 2;
608         if( name[0] == '-' )
609             name++;
610         value = atobool(value) ? "false" : "true";
611     }
612     name_was_bool = 0;
613
614 #define OPT(STR) else if( !strcmp( name, STR ) )
615 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
616     if(0);
617     OPT("asm")
618     {
619         p->cpu = isdigit(value[0]) ? atoi(value) :
620                  !strcasecmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
621         if( b_error )
622         {
623             char *buf = strdup( value );
624             if( buf )
625             {
626                 char *tok, UNUSED *saveptr=NULL, *init;
627                 b_error = 0;
628                 p->cpu = 0;
629                 for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
630                 {
631                     int i = 0;
632                     while( x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name) )
633                         i++;
634                     p->cpu |= x264_cpu_names[i].flags;
635                     if( !x264_cpu_names[i].flags )
636                         b_error = 1;
637                 }
638                 free( buf );
639                 if( (p->cpu&X264_CPU_SSSE3) && !(p->cpu&X264_CPU_SSE2_IS_SLOW) )
640                     p->cpu |= X264_CPU_SSE2_IS_FAST;
641             }
642         }
643     }
644     OPT("threads")
645     {
646         if( !strcasecmp(value, "auto") )
647             p->i_threads = X264_THREADS_AUTO;
648         else
649             p->i_threads = atoi(value);
650     }
651     OPT("lookahead-threads")
652     {
653         if( !strcasecmp(value, "auto") )
654             p->i_lookahead_threads = X264_THREADS_AUTO;
655         else
656             p->i_lookahead_threads = atoi(value);
657     }
658     OPT("sliced-threads")
659         p->b_sliced_threads = atobool(value);
660     OPT("sync-lookahead")
661     {
662         if( !strcasecmp(value, "auto") )
663             p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
664         else
665             p->i_sync_lookahead = atoi(value);
666     }
667     OPT2("deterministic", "n-deterministic")
668         p->b_deterministic = atobool(value);
669     OPT("cpu-independent")
670         p->b_cpu_independent = atobool(value);
671     OPT2("level", "level-idc")
672     {
673         if( !strcmp(value, "1b") )
674             p->i_level_idc = 9;
675         else if( atof(value) < 6 )
676             p->i_level_idc = (int)(10*atof(value)+.5);
677         else
678             p->i_level_idc = atoi(value);
679     }
680     OPT("bluray-compat")
681         p->b_bluray_compat = atobool(value);
682     OPT("avcintra-class")
683         p->i_avcintra_class = atoi(value);
684     OPT("sar")
685     {
686         b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
687                     2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
688     }
689     OPT("overscan")
690         b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
691     OPT("videoformat")
692         b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
693     OPT("fullrange")
694         b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
695     OPT("colorprim")
696         b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
697     OPT("transfer")
698         b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
699     OPT("colormatrix")
700         b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
701     OPT("chromaloc")
702     {
703         p->vui.i_chroma_loc = atoi(value);
704         b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
705     }
706     OPT("fps")
707     {
708         if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) != 2 )
709         {
710             double fps = atof(value);
711             if( fps > 0.0 && fps <= INT_MAX/1000.0 )
712             {
713                 p->i_fps_num = (int)(fps * 1000.0 + .5);
714                 p->i_fps_den = 1000;
715             }
716             else
717             {
718                 p->i_fps_num = atoi(value);
719                 p->i_fps_den = 1;
720             }
721         }
722     }
723     OPT2("ref", "frameref")
724         p->i_frame_reference = atoi(value);
725     OPT("dpb-size")
726         p->i_dpb_size = atoi(value);
727     OPT("keyint")
728     {
729         if( strstr( value, "infinite" ) )
730             p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
731         else
732             p->i_keyint_max = atoi(value);
733     }
734     OPT2("min-keyint", "keyint-min")
735     {
736         p->i_keyint_min = atoi(value);
737         if( p->i_keyint_max < p->i_keyint_min )
738             p->i_keyint_max = p->i_keyint_min;
739     }
740     OPT("scenecut")
741     {
742         p->i_scenecut_threshold = atobool(value);
743         if( b_error || p->i_scenecut_threshold )
744         {
745             b_error = 0;
746             p->i_scenecut_threshold = atoi(value);
747         }
748     }
749     OPT("intra-refresh")
750         p->b_intra_refresh = atobool(value);
751     OPT("bframes")
752         p->i_bframe = atoi(value);
753     OPT("b-adapt")
754     {
755         p->i_bframe_adaptive = atobool(value);
756         if( b_error )
757         {
758             b_error = 0;
759             p->i_bframe_adaptive = atoi(value);
760         }
761     }
762     OPT("b-bias")
763         p->i_bframe_bias = atoi(value);
764     OPT("b-pyramid")
765     {
766         b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
767         if( b_error )
768         {
769             b_error = 0;
770             p->i_bframe_pyramid = atoi(value);
771         }
772     }
773     OPT("open-gop")
774         p->b_open_gop = atobool(value);
775     OPT("nf")
776         p->b_deblocking_filter = !atobool(value);
777     OPT2("filter", "deblock")
778     {
779         if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
780             2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
781         {
782             p->b_deblocking_filter = 1;
783         }
784         else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
785         {
786             p->b_deblocking_filter = 1;
787             p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
788         }
789         else
790             p->b_deblocking_filter = atobool(value);
791     }
792     OPT("slice-max-size")
793         p->i_slice_max_size = atoi(value);
794     OPT("slice-max-mbs")
795         p->i_slice_max_mbs = atoi(value);
796     OPT("slice-min-mbs")
797         p->i_slice_min_mbs = atoi(value);
798     OPT("slices")
799         p->i_slice_count = atoi(value);
800     OPT("slices-max")
801         p->i_slice_count_max = atoi(value);
802     OPT("cabac")
803         p->b_cabac = atobool(value);
804     OPT("cabac-idc")
805         p->i_cabac_init_idc = atoi(value);
806     OPT("interlaced")
807         p->b_interlaced = atobool(value);
808     OPT("tff")
809         p->b_interlaced = p->b_tff = atobool(value);
810     OPT("bff")
811     {
812         p->b_interlaced = atobool(value);
813         p->b_tff = !p->b_interlaced;
814     }
815     OPT("constrained-intra")
816         p->b_constrained_intra = atobool(value);
817     OPT("cqm")
818     {
819         if( strstr( value, "flat" ) )
820             p->i_cqm_preset = X264_CQM_FLAT;
821         else if( strstr( value, "jvt" ) )
822             p->i_cqm_preset = X264_CQM_JVT;
823         else
824             p->psz_cqm_file = strdup(value);
825     }
826     OPT("cqmfile")
827         p->psz_cqm_file = strdup(value);
828     OPT("cqm4")
829     {
830         p->i_cqm_preset = X264_CQM_CUSTOM;
831         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
832         b_error |= parse_cqm( value, p->cqm_4py, 16 );
833         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
834         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
835     }
836     OPT("cqm8")
837     {
838         p->i_cqm_preset = X264_CQM_CUSTOM;
839         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
840         b_error |= parse_cqm( value, p->cqm_8py, 64 );
841         b_error |= parse_cqm( value, p->cqm_8ic, 64 );
842         b_error |= parse_cqm( value, p->cqm_8pc, 64 );
843     }
844     OPT("cqm4i")
845     {
846         p->i_cqm_preset = X264_CQM_CUSTOM;
847         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
848         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
849     }
850     OPT("cqm4p")
851     {
852         p->i_cqm_preset = X264_CQM_CUSTOM;
853         b_error |= parse_cqm( value, p->cqm_4py, 16 );
854         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
855     }
856     OPT("cqm4iy")
857     {
858         p->i_cqm_preset = X264_CQM_CUSTOM;
859         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
860     }
861     OPT("cqm4ic")
862     {
863         p->i_cqm_preset = X264_CQM_CUSTOM;
864         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
865     }
866     OPT("cqm4py")
867     {
868         p->i_cqm_preset = X264_CQM_CUSTOM;
869         b_error |= parse_cqm( value, p->cqm_4py, 16 );
870     }
871     OPT("cqm4pc")
872     {
873         p->i_cqm_preset = X264_CQM_CUSTOM;
874         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
875     }
876     OPT("cqm8i")
877     {
878         p->i_cqm_preset = X264_CQM_CUSTOM;
879         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
880         b_error |= parse_cqm( value, p->cqm_8ic, 64 );
881     }
882     OPT("cqm8p")
883     {
884         p->i_cqm_preset = X264_CQM_CUSTOM;
885         b_error |= parse_cqm( value, p->cqm_8py, 64 );
886         b_error |= parse_cqm( value, p->cqm_8pc, 64 );
887     }
888     OPT("log")
889         p->i_log_level = atoi(value);
890     OPT("dump-yuv")
891         p->psz_dump_yuv = strdup(value);
892     OPT2("analyse", "partitions")
893     {
894         p->analyse.inter = 0;
895         if( strstr( value, "none" ) )  p->analyse.inter =  0;
896         if( strstr( value, "all" ) )   p->analyse.inter = ~0;
897
898         if( strstr( value, "i4x4" ) )  p->analyse.inter |= X264_ANALYSE_I4x4;
899         if( strstr( value, "i8x8" ) )  p->analyse.inter |= X264_ANALYSE_I8x8;
900         if( strstr( value, "p8x8" ) )  p->analyse.inter |= X264_ANALYSE_PSUB16x16;
901         if( strstr( value, "p4x4" ) )  p->analyse.inter |= X264_ANALYSE_PSUB8x8;
902         if( strstr( value, "b8x8" ) )  p->analyse.inter |= X264_ANALYSE_BSUB16x16;
903     }
904     OPT("8x8dct")
905         p->analyse.b_transform_8x8 = atobool(value);
906     OPT2("weightb", "weight-b")
907         p->analyse.b_weighted_bipred = atobool(value);
908     OPT("weightp")
909         p->analyse.i_weighted_pred = atoi(value);
910     OPT2("direct", "direct-pred")
911         b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
912     OPT("chroma-qp-offset")
913         p->analyse.i_chroma_qp_offset = atoi(value);
914     OPT("me")
915         b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
916     OPT2("merange", "me-range")
917         p->analyse.i_me_range = atoi(value);
918     OPT2("mvrange", "mv-range")
919         p->analyse.i_mv_range = atoi(value);
920     OPT2("mvrange-thread", "mv-range-thread")
921         p->analyse.i_mv_range_thread = atoi(value);
922     OPT2("subme", "subq")
923         p->analyse.i_subpel_refine = atoi(value);
924     OPT("psy-rd")
925     {
926         if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
927             2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
928             2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
929         { }
930         else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
931         {
932             p->analyse.f_psy_trellis = 0;
933         }
934         else
935         {
936             p->analyse.f_psy_rd = 0;
937             p->analyse.f_psy_trellis = 0;
938         }
939     }
940     OPT("psy")
941         p->analyse.b_psy = atobool(value);
942     OPT("chroma-me")
943         p->analyse.b_chroma_me = atobool(value);
944     OPT("mixed-refs")
945         p->analyse.b_mixed_references = atobool(value);
946     OPT("trellis")
947         p->analyse.i_trellis = atoi(value);
948     OPT("fast-pskip")
949         p->analyse.b_fast_pskip = atobool(value);
950     OPT("dct-decimate")
951         p->analyse.b_dct_decimate = atobool(value);
952     OPT("deadzone-inter")
953         p->analyse.i_luma_deadzone[0] = atoi(value);
954     OPT("deadzone-intra")
955         p->analyse.i_luma_deadzone[1] = atoi(value);
956     OPT("nr")
957         p->analyse.i_noise_reduction = atoi(value);
958     OPT("bitrate")
959     {
960         p->rc.i_bitrate = atoi(value);
961         p->rc.i_rc_method = X264_RC_ABR;
962     }
963     OPT2("qp", "qp_constant")
964     {
965         p->rc.i_qp_constant = atoi(value);
966         p->rc.i_rc_method = X264_RC_CQP;
967     }
968     OPT("crf")
969     {
970         p->rc.f_rf_constant = atof(value);
971         p->rc.i_rc_method = X264_RC_CRF;
972     }
973     OPT("crf-max")
974         p->rc.f_rf_constant_max = atof(value);
975     OPT("rc-lookahead")
976         p->rc.i_lookahead = atoi(value);
977     OPT2("qpmin", "qp-min")
978         p->rc.i_qp_min = atoi(value);
979     OPT2("qpmax", "qp-max")
980         p->rc.i_qp_max = atoi(value);
981     OPT2("qpstep", "qp-step")
982         p->rc.i_qp_step = atoi(value);
983     OPT("ratetol")
984         p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
985     OPT("vbv-maxrate")
986         p->rc.i_vbv_max_bitrate = atoi(value);
987     OPT("vbv-bufsize")
988         p->rc.i_vbv_buffer_size = atoi(value);
989     OPT("vbv-init")
990         p->rc.f_vbv_buffer_init = atof(value);
991     OPT2("ipratio", "ip-factor")
992         p->rc.f_ip_factor = atof(value);
993     OPT2("pbratio", "pb-factor")
994         p->rc.f_pb_factor = atof(value);
995     OPT("aq-mode")
996         p->rc.i_aq_mode = atoi(value);
997     OPT("aq-strength")
998         p->rc.f_aq_strength = atof(value);
999     OPT("pass")
1000     {
1001         int pass = x264_clip3( atoi(value), 0, 3 );
1002         p->rc.b_stat_write = pass & 1;
1003         p->rc.b_stat_read = pass & 2;
1004     }
1005     OPT("stats")
1006     {
1007         p->rc.psz_stat_in = strdup(value);
1008         p->rc.psz_stat_out = strdup(value);
1009     }
1010     OPT("qcomp")
1011         p->rc.f_qcompress = atof(value);
1012     OPT("mbtree")
1013         p->rc.b_mb_tree = atobool(value);
1014     OPT("qblur")
1015         p->rc.f_qblur = atof(value);
1016     OPT2("cplxblur", "cplx-blur")
1017         p->rc.f_complexity_blur = atof(value);
1018     OPT("zones")
1019         p->rc.psz_zones = strdup(value);
1020     OPT("crop-rect")
1021         b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
1022                                                  &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
1023     OPT("psnr")
1024         p->analyse.b_psnr = atobool(value);
1025     OPT("ssim")
1026         p->analyse.b_ssim = atobool(value);
1027     OPT("aud")
1028         p->b_aud = atobool(value);
1029     OPT("sps-id")
1030         p->i_sps_id = atoi(value);
1031     OPT("global-header")
1032         p->b_repeat_headers = !atobool(value);
1033     OPT("repeat-headers")
1034         p->b_repeat_headers = atobool(value);
1035     OPT("annexb")
1036         p->b_annexb = atobool(value);
1037     OPT("force-cfr")
1038         p->b_vfr_input = !atobool(value);
1039     OPT("nal-hrd")
1040         b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
1041     OPT("filler")
1042         p->rc.b_filler = atobool(value);
1043     OPT("pic-struct")
1044         p->b_pic_struct = atobool(value);
1045     OPT("fake-interlaced")
1046         p->b_fake_interlaced = atobool(value);
1047     OPT("frame-packing")
1048         p->i_frame_packing = atoi(value);
1049     OPT("stitchable")
1050         p->b_stitchable = atobool(value);
1051     OPT("opencl")
1052         p->b_opencl = atobool( value );
1053     OPT("opencl-clbin")
1054         p->psz_clbin_file = strdup( value );
1055     OPT("opencl-device")
1056         p->i_opencl_device = atoi( value );
1057     else
1058     {
1059         b_error = 1;
1060         errortype = X264_PARAM_BAD_NAME;
1061     }
1062 #undef OPT
1063 #undef OPT2
1064 #undef atobool
1065 #undef atoi
1066 #undef atof
1067
1068     if( name_buf )
1069         free( name_buf );
1070
1071     b_error |= value_was_null && !name_was_bool;
1072     return b_error ? errortype : 0;
1073 }
1074
1075 /****************************************************************************
1076  * x264_log:
1077  ****************************************************************************/
1078 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
1079 {
1080     if( !h || i_level <= h->param.i_log_level )
1081     {
1082         va_list arg;
1083         va_start( arg, psz_fmt );
1084         if( !h )
1085             x264_log_default( NULL, i_level, psz_fmt, arg );
1086         else
1087             h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
1088         va_end( arg );
1089     }
1090 }
1091
1092 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
1093 {
1094     char *psz_prefix;
1095     switch( i_level )
1096     {
1097         case X264_LOG_ERROR:
1098             psz_prefix = "error";
1099             break;
1100         case X264_LOG_WARNING:
1101             psz_prefix = "warning";
1102             break;
1103         case X264_LOG_INFO:
1104             psz_prefix = "info";
1105             break;
1106         case X264_LOG_DEBUG:
1107             psz_prefix = "debug";
1108             break;
1109         default:
1110             psz_prefix = "unknown";
1111             break;
1112     }
1113     fprintf( stderr, "x264 [%s]: ", psz_prefix );
1114     x264_vfprintf( stderr, psz_fmt, arg );
1115 }
1116
1117 /****************************************************************************
1118  * x264_picture_init:
1119  ****************************************************************************/
1120 void x264_picture_init( x264_picture_t *pic )
1121 {
1122     memset( pic, 0, sizeof( x264_picture_t ) );
1123     pic->i_type = X264_TYPE_AUTO;
1124     pic->i_qpplus1 = X264_QP_AUTO;
1125     pic->i_pic_struct = PIC_STRUCT_AUTO;
1126 }
1127
1128 /****************************************************************************
1129  * x264_picture_alloc:
1130  ****************************************************************************/
1131 int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
1132 {
1133     typedef struct
1134     {
1135         int planes;
1136         int width_fix8[3];
1137         int height_fix8[3];
1138     } x264_csp_tab_t;
1139
1140     static const x264_csp_tab_t x264_csp_tab[] =
1141     {
1142         [X264_CSP_I420] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1143         [X264_CSP_YV12] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
1144         [X264_CSP_NV12] = { 2, { 256*1, 256*1 },        { 256*1, 256/2 },       },
1145         [X264_CSP_NV21] = { 2, { 256*1, 256*1 },        { 256*1, 256/2 },       },
1146         [X264_CSP_I422] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1147         [X264_CSP_YV16] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
1148         [X264_CSP_NV16] = { 2, { 256*1, 256*1 },        { 256*1, 256*1 },       },
1149         [X264_CSP_I444] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1150         [X264_CSP_YV24] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
1151         [X264_CSP_BGR]  = { 1, { 256*3 },               { 256*1 },              },
1152         [X264_CSP_BGRA] = { 1, { 256*4 },               { 256*1 },              },
1153         [X264_CSP_RGB]  = { 1, { 256*3 },               { 256*1 },              },
1154     };
1155
1156     int csp = i_csp & X264_CSP_MASK;
1157     if( csp <= X264_CSP_NONE || csp >= X264_CSP_MAX || csp == X264_CSP_V210 )
1158         return -1;
1159     x264_picture_init( pic );
1160     pic->img.i_csp = i_csp;
1161     pic->img.i_plane = x264_csp_tab[csp].planes;
1162     int depth_factor = i_csp & X264_CSP_HIGH_DEPTH ? 2 : 1;
1163     int plane_offset[3] = {0};
1164     int frame_size = 0;
1165     for( int i = 0; i < pic->img.i_plane; i++ )
1166     {
1167         int stride = (((int64_t)i_width * x264_csp_tab[csp].width_fix8[i]) >> 8) * depth_factor;
1168         int plane_size = (((int64_t)i_height * x264_csp_tab[csp].height_fix8[i]) >> 8) * stride;
1169         pic->img.i_stride[i] = stride;
1170         plane_offset[i] = frame_size;
1171         frame_size += plane_size;
1172     }
1173     pic->img.plane[0] = x264_malloc( frame_size );
1174     if( !pic->img.plane[0] )
1175         return -1;
1176     for( int i = 1; i < pic->img.i_plane; i++ )
1177         pic->img.plane[i] = pic->img.plane[0] + plane_offset[i];
1178     return 0;
1179 }
1180
1181 /****************************************************************************
1182  * x264_picture_clean:
1183  ****************************************************************************/
1184 void x264_picture_clean( x264_picture_t *pic )
1185 {
1186     x264_free( pic->img.plane[0] );
1187
1188     /* just to be safe */
1189     memset( pic, 0, sizeof( x264_picture_t ) );
1190 }
1191
1192 /****************************************************************************
1193  * x264_malloc:
1194  ****************************************************************************/
1195 void *x264_malloc( int i_size )
1196 {
1197     uint8_t *align_buf = NULL;
1198 #if HAVE_MALLOC_H
1199 #if HAVE_THP
1200 #define HUGE_PAGE_SIZE 2*1024*1024
1201 #define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
1202     /* Attempt to allocate huge pages to reduce TLB misses. */
1203     if( i_size >= HUGE_PAGE_THRESHOLD )
1204     {
1205         align_buf = memalign( HUGE_PAGE_SIZE, i_size );
1206         if( align_buf )
1207         {
1208             /* Round up to the next huge page boundary if we are close enough. */
1209             size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
1210             madvise( align_buf, madv_size, MADV_HUGEPAGE );
1211         }
1212     }
1213     else
1214 #undef HUGE_PAGE_SIZE
1215 #undef HUGE_PAGE_THRESHOLD
1216 #endif
1217         align_buf = memalign( NATIVE_ALIGN, i_size );
1218 #else
1219     uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
1220     if( buf )
1221     {
1222         align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
1223         align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
1224         *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
1225     }
1226 #endif
1227     if( !align_buf )
1228         x264_log( NULL, X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
1229     return align_buf;
1230 }
1231
1232 /****************************************************************************
1233  * x264_free:
1234  ****************************************************************************/
1235 void x264_free( void *p )
1236 {
1237     if( p )
1238     {
1239 #if HAVE_MALLOC_H
1240         free( p );
1241 #else
1242         free( *( ( ( void **) p ) - 1 ) );
1243 #endif
1244     }
1245 }
1246
1247 /****************************************************************************
1248  * x264_reduce_fraction:
1249  ****************************************************************************/
1250 #define REDUCE_FRACTION( name, type )\
1251 void name( type *n, type *d )\
1252 {                   \
1253     type a = *n;    \
1254     type b = *d;    \
1255     type c;         \
1256     if( !a || !b )  \
1257         return;     \
1258     c = a % b;      \
1259     while( c )      \
1260     {               \
1261         a = b;      \
1262         b = c;      \
1263         c = a % b;  \
1264     }               \
1265     *n /= b;        \
1266     *d /= b;        \
1267 }
1268
1269 REDUCE_FRACTION( x264_reduce_fraction  , uint32_t )
1270 REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
1271
1272 /****************************************************************************
1273  * x264_slurp_file:
1274  ****************************************************************************/
1275 char *x264_slurp_file( const char *filename )
1276 {
1277     int b_error = 0;
1278     int64_t i_size;
1279     char *buf;
1280     FILE *fh = x264_fopen( filename, "rb" );
1281     if( !fh )
1282         return NULL;
1283
1284     b_error |= fseek( fh, 0, SEEK_END ) < 0;
1285     b_error |= ( i_size = ftell( fh ) ) <= 0;
1286     if( WORD_SIZE == 4 )
1287         b_error |= i_size > INT32_MAX;
1288     b_error |= fseek( fh, 0, SEEK_SET ) < 0;
1289     if( b_error )
1290         goto error;
1291
1292     buf = x264_malloc( i_size+2 );
1293     if( !buf )
1294         goto error;
1295
1296     b_error |= fread( buf, 1, i_size, fh ) != i_size;
1297     fclose( fh );
1298     if( b_error )
1299     {
1300         x264_free( buf );
1301         return NULL;
1302     }
1303
1304     if( buf[i_size-1] != '\n' )
1305         buf[i_size++] = '\n';
1306     buf[i_size] = '\0';
1307
1308     return buf;
1309 error:
1310     fclose( fh );
1311     return NULL;
1312 }
1313
1314 /****************************************************************************
1315  * x264_param2string:
1316  ****************************************************************************/
1317 char *x264_param2string( x264_param_t *p, int b_res )
1318 {
1319     int len = 1000;
1320     char *buf, *s;
1321     if( p->rc.psz_zones )
1322         len += strlen(p->rc.psz_zones);
1323     buf = s = x264_malloc( len );
1324     if( !buf )
1325         return NULL;
1326
1327     if( b_res )
1328     {
1329         s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
1330         s += sprintf( s, "fps=%u/%u ", p->i_fps_num, p->i_fps_den );
1331         s += sprintf( s, "timebase=%u/%u ", p->i_timebase_num, p->i_timebase_den );
1332         s += sprintf( s, "bitdepth=%d ", BIT_DEPTH );
1333     }
1334
1335     if( p->b_opencl )
1336         s += sprintf( s, "opencl=%d ", p->b_opencl );
1337     s += sprintf( s, "cabac=%d", p->b_cabac );
1338     s += sprintf( s, " ref=%d", p->i_frame_reference );
1339     s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
1340                   p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
1341     s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
1342     s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
1343     s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
1344     s += sprintf( s, " psy=%d", p->analyse.b_psy );
1345     if( p->analyse.b_psy )
1346         s += sprintf( s, " psy_rd=%.2f:%.2f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis );
1347     s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
1348     s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
1349     s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
1350     s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
1351     s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
1352     s += sprintf( s, " cqm=%d", p->i_cqm_preset );
1353     s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
1354     s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
1355     s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
1356     s += sprintf( s, " threads=%d", p->i_threads );
1357     s += sprintf( s, " lookahead_threads=%d", p->i_lookahead_threads );
1358     s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
1359     if( p->i_slice_count )
1360         s += sprintf( s, " slices=%d", p->i_slice_count );
1361     if( p->i_slice_count_max )
1362         s += sprintf( s, " slices_max=%d", p->i_slice_count_max );
1363     if( p->i_slice_max_size )
1364         s += sprintf( s, " slice_max_size=%d", p->i_slice_max_size );
1365     if( p->i_slice_max_mbs )
1366         s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
1367     if( p->i_slice_min_mbs )
1368         s += sprintf( s, " slice_min_mbs=%d", p->i_slice_min_mbs );
1369     s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
1370     s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
1371     s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : p->b_fake_interlaced ? "fake" : "0" );
1372     s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
1373     if( p->b_stitchable )
1374         s += sprintf( s, " stitchable=%d", p->b_stitchable );
1375
1376     s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1377
1378     s += sprintf( s, " bframes=%d", p->i_bframe );
1379     if( p->i_bframe )
1380     {
1381         s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1382                       p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1383                       p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
1384     }
1385     s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1386
1387     if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1388         s += sprintf( s, " keyint=infinite" );
1389     else
1390         s += sprintf( s, " keyint=%d", p->i_keyint_max );
1391     s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1392                   p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1393
1394     if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1395         s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1396
1397     s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1398                                ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1399                                : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1400     if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1401     {
1402         if( p->rc.i_rc_method == X264_RC_CRF )
1403             s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
1404         else
1405             s += sprintf( s, " bitrate=%d ratetol=%.1f",
1406                           p->rc.i_bitrate, p->rc.f_rate_tolerance );
1407         s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1408                       p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
1409         if( p->rc.b_stat_read )
1410             s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1411                           p->rc.f_complexity_blur, p->rc.f_qblur );
1412         if( p->rc.i_vbv_buffer_size )
1413         {
1414             s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1415                           p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1416             if( p->rc.i_rc_method == X264_RC_CRF )
1417                 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1418         }
1419     }
1420     else if( p->rc.i_rc_method == X264_RC_CQP )
1421         s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1422
1423     if( p->rc.i_vbv_buffer_size )
1424         s += sprintf( s, " nal_hrd=%s filler=%d", x264_nal_hrd_names[p->i_nal_hrd], p->rc.b_filler );
1425     if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
1426         s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
1427                                                    p->crop_rect.i_right, p->crop_rect.i_bottom );
1428     if( p->i_frame_packing >= 0 )
1429         s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
1430
1431     if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1432     {
1433         s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1434         if( p->i_bframe && !p->rc.b_mb_tree )
1435             s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1436         s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1437         if( p->rc.i_aq_mode )
1438             s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1439         if( p->rc.psz_zones )
1440             s += sprintf( s, " zones=%s", p->rc.psz_zones );
1441         else if( p->rc.i_zones )
1442             s += sprintf( s, " zones" );
1443     }
1444
1445     return buf;
1446 }
1447