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