]> git.sesse.net Git - x264/blob - common/common.c
Fix SSIM calculation with sliced threads
[x264] / common / common.c
1 /*****************************************************************************
2  * common.c: misc common functions
3  *****************************************************************************
4  * Copyright (C) 2003-2011 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     param->i_frame_packing = -1;
169 }
170
171 static int x264_param_apply_preset( x264_param_t *param, const char *preset )
172 {
173     char *end;
174     int i = strtol( preset, &end, 10 );
175     if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
176         preset = x264_preset_names[i];
177
178     if( !strcasecmp( preset, "ultrafast" ) )
179     {
180         param->i_frame_reference = 1;
181         param->i_scenecut_threshold = 0;
182         param->b_deblocking_filter = 0;
183         param->b_cabac = 0;
184         param->i_bframe = 0;
185         param->analyse.intra = 0;
186         param->analyse.inter = 0;
187         param->analyse.b_transform_8x8 = 0;
188         param->analyse.i_me_method = X264_ME_DIA;
189         param->analyse.i_subpel_refine = 0;
190         param->rc.i_aq_mode = 0;
191         param->analyse.b_mixed_references = 0;
192         param->analyse.i_trellis = 0;
193         param->i_bframe_adaptive = X264_B_ADAPT_NONE;
194         param->rc.b_mb_tree = 0;
195         param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
196         param->analyse.b_weighted_bipred = 0;
197         param->rc.i_lookahead = 0;
198     }
199     else if( !strcasecmp( preset, "superfast" ) )
200     {
201         param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
202         param->analyse.i_me_method = X264_ME_DIA;
203         param->analyse.i_subpel_refine = 1;
204         param->i_frame_reference = 1;
205         param->analyse.b_mixed_references = 0;
206         param->analyse.i_trellis = 0;
207         param->rc.b_mb_tree = 0;
208         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
209         param->rc.i_lookahead = 0;
210     }
211     else if( !strcasecmp( preset, "veryfast" ) )
212     {
213         param->analyse.i_me_method = X264_ME_HEX;
214         param->analyse.i_subpel_refine = 2;
215         param->i_frame_reference = 1;
216         param->analyse.b_mixed_references = 0;
217         param->analyse.i_trellis = 0;
218         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
219         param->rc.i_lookahead = 10;
220     }
221     else if( !strcasecmp( preset, "faster" ) )
222     {
223         param->analyse.b_mixed_references = 0;
224         param->i_frame_reference = 2;
225         param->analyse.i_subpel_refine = 4;
226         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
227         param->rc.i_lookahead = 20;
228     }
229     else if( !strcasecmp( preset, "fast" ) )
230     {
231         param->i_frame_reference = 2;
232         param->analyse.i_subpel_refine = 6;
233         param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
234         param->rc.i_lookahead = 30;
235     }
236     else if( !strcasecmp( preset, "medium" ) )
237     {
238         /* Default is medium */
239     }
240     else if( !strcasecmp( preset, "slow" ) )
241     {
242         param->analyse.i_me_method = X264_ME_UMH;
243         param->analyse.i_subpel_refine = 8;
244         param->i_frame_reference = 5;
245         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
246         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
247         param->rc.i_lookahead = 50;
248     }
249     else if( !strcasecmp( preset, "slower" ) )
250     {
251         param->analyse.i_me_method = X264_ME_UMH;
252         param->analyse.i_subpel_refine = 9;
253         param->i_frame_reference = 8;
254         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
255         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
256         param->analyse.inter |= X264_ANALYSE_PSUB8x8;
257         param->analyse.i_trellis = 2;
258         param->rc.i_lookahead = 60;
259     }
260     else if( !strcasecmp( preset, "veryslow" ) )
261     {
262         param->analyse.i_me_method = X264_ME_UMH;
263         param->analyse.i_subpel_refine = 10;
264         param->analyse.i_me_range = 24;
265         param->i_frame_reference = 16;
266         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
267         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
268         param->analyse.inter |= X264_ANALYSE_PSUB8x8;
269         param->analyse.i_trellis = 2;
270         param->i_bframe = 8;
271         param->rc.i_lookahead = 60;
272     }
273     else if( !strcasecmp( preset, "placebo" ) )
274     {
275         param->analyse.i_me_method = X264_ME_TESA;
276         param->analyse.i_subpel_refine = 10;
277         param->analyse.i_me_range = 24;
278         param->i_frame_reference = 16;
279         param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
280         param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
281         param->analyse.inter |= X264_ANALYSE_PSUB8x8;
282         param->analyse.b_fast_pskip = 0;
283         param->analyse.i_trellis = 2;
284         param->i_bframe = 16;
285         param->rc.i_lookahead = 60;
286     }
287     else
288     {
289         x264_log( NULL, X264_LOG_ERROR, "invalid preset '%s'\n", preset );
290         return -1;
291     }
292     return 0;
293 }
294
295 static int x264_param_apply_tune( x264_param_t *param, const char *tune )
296 {
297     char *tmp = x264_malloc( strlen( tune ) + 1 );
298     if( !tmp )
299         return -1;
300     tmp = strcpy( tmp, tune );
301     char *s = strtok( tmp, ",./-+" );
302     int psy_tuning_used = 0;
303     while( s )
304     {
305         if( !strncasecmp( s, "film", 4 ) )
306         {
307             if( psy_tuning_used++ ) goto psy_failure;
308             param->i_deblocking_filter_alphac0 = -1;
309             param->i_deblocking_filter_beta = -1;
310             param->analyse.f_psy_trellis = 0.15;
311         }
312         else if( !strncasecmp( s, "animation", 9 ) )
313         {
314             if( psy_tuning_used++ ) goto psy_failure;
315             param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
316             param->i_deblocking_filter_alphac0 = 1;
317             param->i_deblocking_filter_beta = 1;
318             param->analyse.f_psy_rd = 0.4;
319             param->rc.f_aq_strength = 0.6;
320             param->i_bframe += 2;
321         }
322         else if( !strncasecmp( s, "grain", 5 ) )
323         {
324             if( psy_tuning_used++ ) goto psy_failure;
325             param->i_deblocking_filter_alphac0 = -2;
326             param->i_deblocking_filter_beta = -2;
327             param->analyse.f_psy_trellis = 0.25;
328             param->analyse.b_dct_decimate = 0;
329             param->rc.f_pb_factor = 1.1;
330             param->rc.f_ip_factor = 1.1;
331             param->rc.f_aq_strength = 0.5;
332             param->analyse.i_luma_deadzone[0] = 6;
333             param->analyse.i_luma_deadzone[1] = 6;
334             param->rc.f_qcompress = 0.8;
335         }
336         else if( !strncasecmp( s, "stillimage", 5 ) )
337         {
338             if( psy_tuning_used++ ) goto psy_failure;
339             param->i_deblocking_filter_alphac0 = -3;
340             param->i_deblocking_filter_beta = -3;
341             param->analyse.f_psy_rd = 2.0;
342             param->analyse.f_psy_trellis = 0.7;
343             param->rc.f_aq_strength = 1.2;
344         }
345         else if( !strncasecmp( s, "psnr", 4 ) )
346         {
347             if( psy_tuning_used++ ) goto psy_failure;
348             param->rc.i_aq_mode = X264_AQ_NONE;
349             param->analyse.b_psy = 0;
350         }
351         else if( !strncasecmp( s, "ssim", 4 ) )
352         {
353             if( psy_tuning_used++ ) goto psy_failure;
354             param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
355             param->analyse.b_psy = 0;
356         }
357         else if( !strncasecmp( s, "fastdecode", 10 ) )
358         {
359             param->b_deblocking_filter = 0;
360             param->b_cabac = 0;
361             param->analyse.b_weighted_bipred = 0;
362             param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
363         }
364         else if( !strncasecmp( s, "zerolatency", 11 ) )
365         {
366             param->rc.i_lookahead = 0;
367             param->i_sync_lookahead = 0;
368             param->i_bframe = 0;
369             param->b_sliced_threads = 1;
370             param->b_vfr_input = 0;
371             param->rc.b_mb_tree = 0;
372         }
373         else if( !strncasecmp( s, "touhou", 6 ) )
374         {
375             if( psy_tuning_used++ ) goto psy_failure;
376             param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
377             param->i_deblocking_filter_alphac0 = -1;
378             param->i_deblocking_filter_beta = -1;
379             param->analyse.f_psy_trellis = 0.2;
380             param->rc.f_aq_strength = 1.3;
381             if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
382                 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
383         }
384         else
385         {
386             x264_log( NULL, X264_LOG_ERROR, "invalid tune '%s'\n", s );
387             x264_free( tmp );
388             return -1;
389         }
390         if( 0 )
391         {
392     psy_failure:
393             x264_log( NULL, X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %s\n", s );
394         }
395         s = strtok( NULL, ",./-+" );
396     }
397     x264_free( tmp );
398     return 0;
399 }
400
401 int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
402 {
403     x264_param_default( param );
404
405     if( preset && x264_param_apply_preset( param, preset ) < 0 )
406         return -1;
407     if( tune && x264_param_apply_tune( param, tune ) < 0 )
408         return -1;
409     return 0;
410 }
411
412 void x264_param_apply_fastfirstpass( x264_param_t *param )
413 {
414     /* Set faster options in case of turbo firstpass. */
415     if( param->rc.b_stat_write && !param->rc.b_stat_read )
416     {
417         param->i_frame_reference = 1;
418         param->analyse.b_transform_8x8 = 0;
419         param->analyse.inter = 0;
420         param->analyse.i_me_method = X264_ME_DIA;
421         param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
422         param->analyse.i_trellis = 0;
423         param->analyse.b_fast_pskip = 1;
424     }
425 }
426
427 int x264_param_apply_profile( x264_param_t *param, const char *profile )
428 {
429     if( !profile )
430         return 0;
431
432 #if BIT_DEPTH > 8
433     if( !strcasecmp( profile, "baseline" ) || !strcasecmp( profile, "main" ) ||
434         !strcasecmp( profile, "high" ) )
435     {
436         x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d.\n", profile, BIT_DEPTH );
437         return -1;
438     }
439 #endif
440
441     if( !strcasecmp( profile, "baseline" ) )
442     {
443         param->analyse.b_transform_8x8 = 0;
444         param->b_cabac = 0;
445         param->i_cqm_preset = X264_CQM_FLAT;
446         param->psz_cqm_file = NULL;
447         param->i_bframe = 0;
448         param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
449         if( param->b_interlaced )
450         {
451             x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
452             return -1;
453         }
454         if( param->b_fake_interlaced )
455         {
456             x264_log( NULL, X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
457             return -1;
458         }
459     }
460     else if( !strcasecmp( profile, "main" ) )
461     {
462         param->analyse.b_transform_8x8 = 0;
463         param->i_cqm_preset = X264_CQM_FLAT;
464         param->psz_cqm_file = NULL;
465     }
466     else if( !strcasecmp( profile, "high" ) || !strcasecmp( profile, "high10" ) )
467     {
468         /* Default */
469     }
470     else
471     {
472         x264_log( NULL, X264_LOG_ERROR, "invalid profile: %s\n", profile );
473         return -1;
474     }
475     if( (param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
476         (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + QP_BD_OFFSET) <= 0) )
477     {
478         x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
479         return -1;
480     }
481     return 0;
482 }
483
484 static int parse_enum( const char *arg, const char * const *names, int *dst )
485 {
486     for( int i = 0; names[i]; i++ )
487         if( !strcmp( arg, names[i] ) )
488         {
489             *dst = i;
490             return 0;
491         }
492     return -1;
493 }
494
495 static int parse_cqm( const char *str, uint8_t *cqm, int length )
496 {
497     int i = 0;
498     do {
499         int coef;
500         if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
501             return -1;
502         cqm[i++] = coef;
503     } while( i < length && (str = strchr( str, ',' )) && str++ );
504     return (i == length) ? 0 : -1;
505 }
506
507 static int x264_atobool( const char *str, int *b_error )
508 {
509     if( !strcmp(str, "1") ||
510         !strcmp(str, "true") ||
511         !strcmp(str, "yes") )
512         return 1;
513     if( !strcmp(str, "0") ||
514         !strcmp(str, "false") ||
515         !strcmp(str, "no") )
516         return 0;
517     *b_error = 1;
518     return 0;
519 }
520
521 static int x264_atoi( const char *str, int *b_error )
522 {
523     char *end;
524     int v = strtol( str, &end, 0 );
525     if( end == str || *end != '\0' )
526         *b_error = 1;
527     return v;
528 }
529
530 static double x264_atof( const char *str, int *b_error )
531 {
532     char *end;
533     double v = strtod( str, &end );
534     if( end == str || *end != '\0' )
535         *b_error = 1;
536     return v;
537 }
538
539 #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
540 #define atoi(str) x264_atoi( str, &b_error )
541 #define atof(str) x264_atof( str, &b_error )
542
543 int x264_param_parse( x264_param_t *p, const char *name, const char *value )
544 {
545     char *name_buf = NULL;
546     int b_error = 0;
547     int name_was_bool;
548     int value_was_null = !value;
549     int i;
550
551     if( !name )
552         return X264_PARAM_BAD_NAME;
553     if( !value )
554         value = "true";
555
556     if( value[0] == '=' )
557         value++;
558
559     if( strchr( name, '_' ) ) // s/_/-/g
560     {
561         char *c;
562         name_buf = strdup(name);
563         while( (c = strchr( name_buf, '_' )) )
564             *c = '-';
565         name = name_buf;
566     }
567
568     if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
569         (!strncmp( name, "no", 2 ) && (i = 2)) )
570     {
571         name += i;
572         value = atobool(value) ? "false" : "true";
573     }
574     name_was_bool = 0;
575
576 #define OPT(STR) else if( !strcmp( name, STR ) )
577 #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
578     if(0);
579     OPT("asm")
580     {
581         p->cpu = isdigit(value[0]) ? atoi(value) :
582                  !strcmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
583         if( b_error )
584         {
585             char *buf = strdup(value);
586             char *tok, UNUSED *saveptr=NULL, *init;
587             b_error = 0;
588             p->cpu = 0;
589             for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
590             {
591                 for( i=0; x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name); i++ );
592                 p->cpu |= x264_cpu_names[i].flags;
593                 if( !x264_cpu_names[i].flags )
594                     b_error = 1;
595             }
596             free( buf );
597         }
598     }
599     OPT("threads")
600     {
601         if( !strcmp(value, "auto") )
602             p->i_threads = X264_THREADS_AUTO;
603         else
604             p->i_threads = atoi(value);
605     }
606     OPT("sliced-threads")
607         p->b_sliced_threads = atobool(value);
608     OPT("sync-lookahead")
609     {
610         if( !strcmp(value, "auto") )
611             p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
612         else
613             p->i_sync_lookahead = atoi(value);
614     }
615     OPT2("deterministic", "n-deterministic")
616         p->b_deterministic = atobool(value);
617     OPT2("level", "level-idc")
618     {
619         if( !strcmp(value, "1b") )
620             p->i_level_idc = 9;
621         else if( atof(value) < 6 )
622             p->i_level_idc = (int)(10*atof(value)+.5);
623         else
624             p->i_level_idc = atoi(value);
625     }
626     OPT("bluray-compat")
627         p->b_bluray_compat = atobool(value);
628     OPT("sar")
629     {
630         b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
631                     2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
632     }
633     OPT("overscan")
634         b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
635     OPT("videoformat")
636         b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
637     OPT("fullrange")
638         b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
639     OPT("colorprim")
640         b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
641     OPT("transfer")
642         b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
643     OPT("colormatrix")
644         b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
645     OPT("chromaloc")
646     {
647         p->vui.i_chroma_loc = atoi(value);
648         b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
649     }
650     OPT("fps")
651     {
652         if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) == 2 )
653             ;
654         else
655         {
656             float fps = atof(value);
657             p->i_fps_num = (int)(fps * 1000 + .5);
658             p->i_fps_den = 1000;
659         }
660     }
661     OPT2("ref", "frameref")
662         p->i_frame_reference = atoi(value);
663     OPT("dpb-size")
664         p->i_dpb_size = atoi(value);
665     OPT("keyint")
666     {
667         if( strstr( value, "infinite" ) )
668             p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
669         else
670             p->i_keyint_max = atoi(value);
671     }
672     OPT2("min-keyint", "keyint-min")
673     {
674         p->i_keyint_min = atoi(value);
675         if( p->i_keyint_max < p->i_keyint_min )
676             p->i_keyint_max = p->i_keyint_min;
677     }
678     OPT("scenecut")
679     {
680         p->i_scenecut_threshold = atobool(value);
681         if( b_error || p->i_scenecut_threshold )
682         {
683             b_error = 0;
684             p->i_scenecut_threshold = atoi(value);
685         }
686     }
687     OPT("intra-refresh")
688         p->b_intra_refresh = atobool(value);
689     OPT("bframes")
690         p->i_bframe = atoi(value);
691     OPT("b-adapt")
692     {
693         p->i_bframe_adaptive = atobool(value);
694         if( b_error )
695         {
696             b_error = 0;
697             p->i_bframe_adaptive = atoi(value);
698         }
699     }
700     OPT("b-bias")
701         p->i_bframe_bias = atoi(value);
702     OPT("b-pyramid")
703     {
704         b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
705         if( b_error )
706         {
707             b_error = 0;
708             p->i_bframe_pyramid = atoi(value);
709         }
710     }
711     OPT("open-gop")
712         p->b_open_gop = atobool(value);
713     OPT("nf")
714         p->b_deblocking_filter = !atobool(value);
715     OPT2("filter", "deblock")
716     {
717         if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
718             2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
719         {
720             p->b_deblocking_filter = 1;
721         }
722         else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
723         {
724             p->b_deblocking_filter = 1;
725             p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
726         }
727         else
728             p->b_deblocking_filter = atobool(value);
729     }
730     OPT("slice-max-size")
731         p->i_slice_max_size = atoi(value);
732     OPT("slice-max-mbs")
733         p->i_slice_max_mbs = atoi(value);
734     OPT("slices")
735         p->i_slice_count = atoi(value);
736     OPT("cabac")
737         p->b_cabac = atobool(value);
738     OPT("cabac-idc")
739         p->i_cabac_init_idc = atoi(value);
740     OPT("interlaced")
741         p->b_interlaced = atobool(value);
742     OPT("tff")
743         p->b_interlaced = p->b_tff = atobool(value);
744     OPT("bff")
745     {
746         p->b_interlaced = atobool(value);
747         p->b_tff = !p->b_interlaced;
748     }
749     OPT("constrained-intra")
750         p->b_constrained_intra = atobool(value);
751     OPT("cqm")
752     {
753         if( strstr( value, "flat" ) )
754             p->i_cqm_preset = X264_CQM_FLAT;
755         else if( strstr( value, "jvt" ) )
756             p->i_cqm_preset = X264_CQM_JVT;
757         else
758             p->psz_cqm_file = strdup(value);
759     }
760     OPT("cqmfile")
761         p->psz_cqm_file = strdup(value);
762     OPT("cqm4")
763     {
764         p->i_cqm_preset = X264_CQM_CUSTOM;
765         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
766         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
767         b_error |= parse_cqm( value, p->cqm_4py, 16 );
768         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
769     }
770     OPT("cqm8")
771     {
772         p->i_cqm_preset = X264_CQM_CUSTOM;
773         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
774         b_error |= parse_cqm( value, p->cqm_8py, 64 );
775     }
776     OPT("cqm4i")
777     {
778         p->i_cqm_preset = X264_CQM_CUSTOM;
779         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
780         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
781     }
782     OPT("cqm4p")
783     {
784         p->i_cqm_preset = X264_CQM_CUSTOM;
785         b_error |= parse_cqm( value, p->cqm_4py, 16 );
786         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
787     }
788     OPT("cqm4iy")
789     {
790         p->i_cqm_preset = X264_CQM_CUSTOM;
791         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
792     }
793     OPT("cqm4ic")
794     {
795         p->i_cqm_preset = X264_CQM_CUSTOM;
796         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
797     }
798     OPT("cqm4py")
799     {
800         p->i_cqm_preset = X264_CQM_CUSTOM;
801         b_error |= parse_cqm( value, p->cqm_4py, 16 );
802     }
803     OPT("cqm4pc")
804     {
805         p->i_cqm_preset = X264_CQM_CUSTOM;
806         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
807     }
808     OPT("cqm8i")
809     {
810         p->i_cqm_preset = X264_CQM_CUSTOM;
811         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
812     }
813     OPT("cqm8p")
814     {
815         p->i_cqm_preset = X264_CQM_CUSTOM;
816         b_error |= parse_cqm( value, p->cqm_8py, 64 );
817     }
818     OPT("log")
819         p->i_log_level = atoi(value);
820 #if HAVE_VISUALIZE
821     OPT("visualize")
822         p->b_visualize = atobool(value);
823 #endif
824     OPT("dump-yuv")
825         p->psz_dump_yuv = strdup(value);
826     OPT2("analyse", "partitions")
827     {
828         p->analyse.inter = 0;
829         if( strstr( value, "none" ) )  p->analyse.inter =  0;
830         if( strstr( value, "all" ) )   p->analyse.inter = ~0;
831
832         if( strstr( value, "i4x4" ) )  p->analyse.inter |= X264_ANALYSE_I4x4;
833         if( strstr( value, "i8x8" ) )  p->analyse.inter |= X264_ANALYSE_I8x8;
834         if( strstr( value, "p8x8" ) )  p->analyse.inter |= X264_ANALYSE_PSUB16x16;
835         if( strstr( value, "p4x4" ) )  p->analyse.inter |= X264_ANALYSE_PSUB8x8;
836         if( strstr( value, "b8x8" ) )  p->analyse.inter |= X264_ANALYSE_BSUB16x16;
837     }
838     OPT("8x8dct")
839         p->analyse.b_transform_8x8 = atobool(value);
840     OPT2("weightb", "weight-b")
841         p->analyse.b_weighted_bipred = atobool(value);
842     OPT("weightp")
843         p->analyse.i_weighted_pred = atoi(value);
844     OPT2("direct", "direct-pred")
845         b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
846     OPT("chroma-qp-offset")
847         p->analyse.i_chroma_qp_offset = atoi(value);
848     OPT("me")
849         b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
850     OPT2("merange", "me-range")
851         p->analyse.i_me_range = atoi(value);
852     OPT2("mvrange", "mv-range")
853         p->analyse.i_mv_range = atoi(value);
854     OPT2("mvrange-thread", "mv-range-thread")
855         p->analyse.i_mv_range_thread = atoi(value);
856     OPT2("subme", "subq")
857         p->analyse.i_subpel_refine = atoi(value);
858     OPT("psy-rd")
859     {
860         if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
861             2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
862             2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
863         { }
864         else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
865         {
866             p->analyse.f_psy_trellis = 0;
867         }
868         else
869         {
870             p->analyse.f_psy_rd = 0;
871             p->analyse.f_psy_trellis = 0;
872         }
873     }
874     OPT("psy")
875         p->analyse.b_psy = atobool(value);
876     OPT("chroma-me")
877         p->analyse.b_chroma_me = atobool(value);
878     OPT("mixed-refs")
879         p->analyse.b_mixed_references = atobool(value);
880     OPT("trellis")
881         p->analyse.i_trellis = atoi(value);
882     OPT("fast-pskip")
883         p->analyse.b_fast_pskip = atobool(value);
884     OPT("dct-decimate")
885         p->analyse.b_dct_decimate = atobool(value);
886     OPT("deadzone-inter")
887         p->analyse.i_luma_deadzone[0] = atoi(value);
888     OPT("deadzone-intra")
889         p->analyse.i_luma_deadzone[1] = atoi(value);
890     OPT("nr")
891         p->analyse.i_noise_reduction = atoi(value);
892     OPT("bitrate")
893     {
894         p->rc.i_bitrate = atoi(value);
895         p->rc.i_rc_method = X264_RC_ABR;
896     }
897     OPT2("qp", "qp_constant")
898     {
899         p->rc.i_qp_constant = atoi(value);
900         p->rc.i_rc_method = X264_RC_CQP;
901     }
902     OPT("crf")
903     {
904         p->rc.f_rf_constant = atof(value);
905         p->rc.i_rc_method = X264_RC_CRF;
906     }
907     OPT("crf-max")
908         p->rc.f_rf_constant_max = atof(value);
909     OPT("rc-lookahead")
910         p->rc.i_lookahead = atoi(value);
911     OPT2("qpmin", "qp-min")
912         p->rc.i_qp_min = atoi(value);
913     OPT2("qpmax", "qp-max")
914         p->rc.i_qp_max = atoi(value);
915     OPT2("qpstep", "qp-step")
916         p->rc.i_qp_step = atoi(value);
917     OPT("ratetol")
918         p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
919     OPT("vbv-maxrate")
920         p->rc.i_vbv_max_bitrate = atoi(value);
921     OPT("vbv-bufsize")
922         p->rc.i_vbv_buffer_size = atoi(value);
923     OPT("vbv-init")
924         p->rc.f_vbv_buffer_init = atof(value);
925     OPT2("ipratio", "ip-factor")
926         p->rc.f_ip_factor = atof(value);
927     OPT2("pbratio", "pb-factor")
928         p->rc.f_pb_factor = atof(value);
929     OPT("aq-mode")
930         p->rc.i_aq_mode = atoi(value);
931     OPT("aq-strength")
932         p->rc.f_aq_strength = atof(value);
933     OPT("pass")
934     {
935         int pass = x264_clip3( atoi(value), 0, 3 );
936         p->rc.b_stat_write = pass & 1;
937         p->rc.b_stat_read = pass & 2;
938     }
939     OPT("stats")
940     {
941         p->rc.psz_stat_in = strdup(value);
942         p->rc.psz_stat_out = strdup(value);
943     }
944     OPT("qcomp")
945         p->rc.f_qcompress = atof(value);
946     OPT("mbtree")
947         p->rc.b_mb_tree = atobool(value);
948     OPT("qblur")
949         p->rc.f_qblur = atof(value);
950     OPT2("cplxblur", "cplx-blur")
951         p->rc.f_complexity_blur = atof(value);
952     OPT("zones")
953         p->rc.psz_zones = strdup(value);
954     OPT("crop-rect")
955         b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
956                                                  &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
957     OPT("psnr")
958         p->analyse.b_psnr = atobool(value);
959     OPT("ssim")
960         p->analyse.b_ssim = atobool(value);
961     OPT("aud")
962         p->b_aud = atobool(value);
963     OPT("sps-id")
964         p->i_sps_id = atoi(value);
965     OPT("global-header")
966         p->b_repeat_headers = !atobool(value);
967     OPT("repeat-headers")
968         p->b_repeat_headers = atobool(value);
969     OPT("annexb")
970         p->b_annexb = atobool(value);
971     OPT("force-cfr")
972         p->b_vfr_input = !atobool(value);
973     OPT("nal-hrd")
974         b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
975     OPT("pic-struct")
976         p->b_pic_struct = atobool(value);
977     OPT("fake-interlaced")
978         p->b_fake_interlaced = atobool(value);
979     OPT("frame-packing")
980         p->i_frame_packing = atoi(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_WINDOWS && 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_WINDOWS && 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     size_t 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     s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
1241
1242     s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
1243
1244     s += sprintf( s, " bframes=%d", p->i_bframe );
1245     if( p->i_bframe )
1246     {
1247         s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
1248                       p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
1249                       p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
1250     }
1251     s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
1252
1253     if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
1254         s += sprintf( s, " keyint=infinite" );
1255     else
1256         s += sprintf( s, " keyint=%d", p->i_keyint_max );
1257     s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
1258                   p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
1259
1260     if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
1261         s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
1262
1263     s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
1264                                ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
1265                                : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
1266     if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
1267     {
1268         if( p->rc.i_rc_method == X264_RC_CRF )
1269             s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
1270         else
1271             s += sprintf( s, " bitrate=%d ratetol=%.1f",
1272                           p->rc.i_bitrate, p->rc.f_rate_tolerance );
1273         s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1274                       p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
1275         if( p->rc.b_stat_read )
1276             s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1277                           p->rc.f_complexity_blur, p->rc.f_qblur );
1278         if( p->rc.i_vbv_buffer_size )
1279         {
1280             s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
1281                           p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
1282             if( p->rc.i_rc_method == X264_RC_CRF )
1283                 s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
1284         }
1285     }
1286     else if( p->rc.i_rc_method == X264_RC_CQP )
1287         s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
1288
1289     if( p->rc.i_vbv_buffer_size )
1290         s += sprintf( s, " nal_hrd=%s", x264_nal_hrd_names[p->i_nal_hrd] );
1291     if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
1292         s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
1293                                                    p->crop_rect.i_right, p->crop_rect.i_bottom );
1294     if( p->i_frame_packing >= 0 )
1295         s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
1296
1297     if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
1298     {
1299         s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
1300         if( p->i_bframe && !p->rc.b_mb_tree )
1301             s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
1302         s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
1303         if( p->rc.i_aq_mode )
1304             s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
1305         if( p->rc.psz_zones )
1306             s += sprintf( s, " zones=%s", p->rc.psz_zones );
1307         else if( p->rc.i_zones )
1308             s += sprintf( s, " zones" );
1309     }
1310
1311     return buf;
1312 }
1313