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