Remove compatibility workarounds
[x264.git] / x264.c
blobf5e37a6ca58167e996bfd60a285816e0409d09d7
1 /*****************************************************************************
2 * x264: top-level x264cli functions
3 *****************************************************************************
4 * Copyright (C) 2003-2019 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
8 * Steven Walters <kemuri9@gmail.com>
9 * Fiona Glaser <fiona@x264.com>
10 * Kieran Kunhya <kieran@kunhya.com>
11 * Henrik Gramner <henrik@gramner.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
27 * This program is also available under a commercial proprietary license.
28 * For more information, contact us at licensing@x264.com.
29 *****************************************************************************/
31 #ifdef _WIN32
32 /* The following two defines must be located before the inclusion of any system header files. */
33 #define WINVER 0x0500
34 #define _WIN32_WINNT 0x0500
35 #include <windows.h>
36 #include <io.h> /* _setmode() */
37 #include <fcntl.h> /* _O_BINARY */
38 #endif
40 #include <signal.h>
41 #include <getopt.h>
42 #include "x264cli.h"
43 #include "input/input.h"
44 #include "output/output.h"
45 #include "filters/filters.h"
47 #define QP_MAX_SPEC (51+6*2)
48 #define QP_MAX (QP_MAX_SPEC+18)
50 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "x264", __VA_ARGS__ )
52 #if HAVE_LAVF
53 #undef DECLARE_ALIGNED
54 #include <libavformat/avformat.h>
55 #include <libavutil/pixfmt.h>
56 #include <libavutil/pixdesc.h>
57 #endif
59 #if HAVE_SWSCALE
60 #undef DECLARE_ALIGNED
61 #include <libswscale/swscale.h>
62 #endif
64 #if HAVE_FFMS
65 #include <ffms.h>
66 #endif
68 #ifdef _WIN32
69 #define CONSOLE_TITLE_SIZE 200
70 static wchar_t org_console_title[CONSOLE_TITLE_SIZE] = L"";
72 void x264_cli_set_console_title( const char *title )
74 wchar_t title_utf16[CONSOLE_TITLE_SIZE];
75 if( utf8_to_utf16( title, title_utf16 ) )
76 SetConsoleTitleW( title_utf16 );
79 static int utf16_to_ansi( const wchar_t *utf16, char *ansi, int size )
81 int invalid;
82 return WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, utf16, -1, ansi, size, NULL, &invalid ) && !invalid;
85 /* Some external libraries doesn't support Unicode in filenames,
86 * as a workaround we can try to get an ANSI filename instead. */
87 int x264_ansi_filename( const char *filename, char *ansi_filename, int size, int create_file )
89 wchar_t filename_utf16[MAX_PATH];
90 if( utf8_to_utf16( filename, filename_utf16 ) )
92 if( create_file )
94 /* Create the file using the Unicode filename if it doesn't already exist. */
95 FILE *fh = _wfopen( filename_utf16, L"ab" );
96 if( fh )
97 fclose( fh );
100 /* Check if the filename already is valid ANSI. */
101 if( utf16_to_ansi( filename_utf16, ansi_filename, size ) )
102 return 1;
104 /* Check for a legacy 8.3 short filename. */
105 int short_length = GetShortPathNameW( filename_utf16, filename_utf16, MAX_PATH );
106 if( short_length > 0 && short_length < MAX_PATH )
107 if( utf16_to_ansi( filename_utf16, ansi_filename, size ) )
108 return 1;
110 return 0;
113 /* Retrieve command line arguments as UTF-8. */
114 static int get_argv_utf8( int *argc_ptr, char ***argv_ptr )
116 int ret = 0;
117 wchar_t **argv_utf16 = CommandLineToArgvW( GetCommandLineW(), argc_ptr );
118 if( argv_utf16 )
120 int argc = *argc_ptr;
121 int offset = (argc+1) * sizeof(char*);
122 int size = offset;
124 for( int i = 0; i < argc; i++ )
125 size += WideCharToMultiByte( CP_UTF8, 0, argv_utf16[i], -1, NULL, 0, NULL, NULL );
127 char **argv = *argv_ptr = malloc( size );
128 if( argv )
130 for( int i = 0; i < argc; i++ )
132 argv[i] = (char*)argv + offset;
133 offset += WideCharToMultiByte( CP_UTF8, 0, argv_utf16[i], -1, argv[i], size-offset, NULL, NULL );
135 argv[argc] = NULL;
136 ret = 1;
138 LocalFree( argv_utf16 );
140 return ret;
142 #endif
144 /* Ctrl-C handler */
145 static volatile int b_ctrl_c = 0;
146 static void sigint_handler( int a )
148 b_ctrl_c = 1;
151 typedef struct {
152 int b_progress;
153 int i_seek;
154 hnd_t hin;
155 hnd_t hout;
156 FILE *qpfile;
157 FILE *tcfile_out;
158 double timebase_convert_multiplier;
159 int i_pulldown;
160 } cli_opt_t;
162 /* file i/o operation structs */
163 cli_input_t cli_input;
164 static cli_output_t cli_output;
166 /* video filter operation struct */
167 static cli_vid_filter_t filter;
169 const char * const x264_avcintra_class_names[] = { "50", "100", "200", 0 };
170 const char * const x264_cqm_names[] = { "flat", "jvt", 0 };
171 const char * const x264_log_level_names[] = { "none", "error", "warning", "info", "debug", 0 };
172 const char * const x264_partition_names[] = { "p8x8", "p4x4", "b8x8", "i8x8", "i4x4", "none", "all", 0 };
173 const char * const x264_pulldown_names[] = { "none", "22", "32", "64", "double", "triple", "euro", 0 };
174 const char * const x264_range_names[] = { "auto", "tv", "pc", 0 };
176 const char * const x264_output_csp_names[] =
178 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I400
179 "i400",
180 #endif
181 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I420
182 "i420",
183 #endif
184 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I422
185 "i422",
186 #endif
187 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I444
188 "i444", "rgb",
189 #endif
193 const char * const x264_valid_profile_names[] =
195 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT <= X264_CSP_I420
196 #if HAVE_BITDEPTH8
197 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I420
198 "baseline", "main",
199 #endif
200 "high",
201 #endif
202 #if HAVE_BITDEPTH10
203 "high10",
204 #endif
205 #endif
206 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I422
207 "high422",
208 #endif
209 "high444", 0
212 const char * const x264_demuxer_names[] =
214 "auto", "raw", "y4m",
215 #if HAVE_AVS
216 "avs",
217 #endif
218 #if HAVE_LAVF
219 "lavf",
220 #endif
221 #if HAVE_FFMS
222 "ffms",
223 #endif
227 const char * const x264_muxer_names[] =
229 "auto", "raw", "mkv", "flv",
230 #if HAVE_GPAC || HAVE_LSMASH
231 "mp4",
232 #endif
236 static const char * const chroma_format_names[] =
238 [0] = "all",
239 [X264_CSP_I400] = "i400",
240 [X264_CSP_I420] = "i420",
241 [X264_CSP_I422] = "i422",
242 [X264_CSP_I444] = "i444"
245 typedef struct
247 int mod;
248 uint8_t pattern[24];
249 float fps_factor;
250 } cli_pulldown_t;
252 enum pulldown_type_e
254 X264_PULLDOWN_22 = 1,
255 X264_PULLDOWN_32,
256 X264_PULLDOWN_64,
257 X264_PULLDOWN_DOUBLE,
258 X264_PULLDOWN_TRIPLE,
259 X264_PULLDOWN_EURO
262 #define TB PIC_STRUCT_TOP_BOTTOM
263 #define BT PIC_STRUCT_BOTTOM_TOP
264 #define TBT PIC_STRUCT_TOP_BOTTOM_TOP
265 #define BTB PIC_STRUCT_BOTTOM_TOP_BOTTOM
267 static const cli_pulldown_t pulldown_values[] =
269 [X264_PULLDOWN_22] = {1, {TB}, 1.0},
270 [X264_PULLDOWN_32] = {4, {TBT, BT, BTB, TB}, 1.25},
271 [X264_PULLDOWN_64] = {2, {PIC_STRUCT_DOUBLE, PIC_STRUCT_TRIPLE}, 1.0},
272 [X264_PULLDOWN_DOUBLE] = {1, {PIC_STRUCT_DOUBLE}, 2.0},
273 [X264_PULLDOWN_TRIPLE] = {1, {PIC_STRUCT_TRIPLE}, 3.0},
274 [X264_PULLDOWN_EURO] = {24, {TBT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT,
275 BTB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB}, 25.0/24.0}
278 #undef TB
279 #undef BT
280 #undef TBT
281 #undef BTB
283 // indexed by pic_struct enum
284 static const float pulldown_frame_duration[10] = { 0.0, 1, 0.5, 0.5, 1, 1, 1.5, 1.5, 2, 3 };
286 static void help( x264_param_t *defaults, int longhelp );
287 static int parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
288 static int encode( x264_param_t *param, cli_opt_t *opt );
290 /* logging and printing for within the cli system */
291 static int cli_log_level;
292 void x264_cli_log( const char *name, int i_level, const char *fmt, ... )
294 if( i_level > cli_log_level )
295 return;
296 char *s_level;
297 switch( i_level )
299 case X264_LOG_ERROR:
300 s_level = "error";
301 break;
302 case X264_LOG_WARNING:
303 s_level = "warning";
304 break;
305 case X264_LOG_INFO:
306 s_level = "info";
307 break;
308 case X264_LOG_DEBUG:
309 s_level = "debug";
310 break;
311 default:
312 s_level = "unknown";
313 break;
315 fprintf( stderr, "%s [%s]: ", name, s_level );
316 va_list arg;
317 va_start( arg, fmt );
318 x264_vfprintf( stderr, fmt, arg );
319 va_end( arg );
322 void x264_cli_printf( int i_level, const char *fmt, ... )
324 if( i_level > cli_log_level )
325 return;
326 va_list arg;
327 va_start( arg, fmt );
328 x264_vfprintf( stderr, fmt, arg );
329 va_end( arg );
332 static void print_version_info( void )
334 #ifdef X264_POINTVER
335 printf( "x264 "X264_POINTVER"\n" );
336 #else
337 printf( "x264 0.%d.X\n", X264_BUILD );
338 #endif
339 #if HAVE_SWSCALE
340 printf( "(libswscale %d.%d.%d)\n", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO );
341 #endif
342 #if HAVE_LAVF
343 printf( "(libavformat %d.%d.%d)\n", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO );
344 #endif
345 #if HAVE_FFMS
346 printf( "(ffmpegsource %d.%d.%d.%d)\n", FFMS_VERSION >> 24, (FFMS_VERSION & 0xff0000) >> 16, (FFMS_VERSION & 0xff00) >> 8, FFMS_VERSION & 0xff );
347 #endif
348 printf( "built on " __DATE__ ", " );
349 #ifdef __INTEL_COMPILER
350 printf( "intel: %.2f (%d)\n", __INTEL_COMPILER / 100.f, __INTEL_COMPILER_BUILD_DATE );
351 #elif defined(__GNUC__)
352 printf( "gcc: " __VERSION__ "\n" );
353 #elif defined(_MSC_FULL_VER)
354 printf( "msvc: %.2f (%u)\n", _MSC_VER / 100.f, _MSC_FULL_VER );
355 #else
356 printf( "using an unknown compiler\n" );
357 #endif
358 printf( "x264 configuration: --chroma-format=%s\n", chroma_format_names[X264_CHROMA_FORMAT] );
359 printf( "libx264 configuration: --chroma-format=%s\n", chroma_format_names[x264_chroma_format] );
360 printf( "x264 license: " );
361 #if HAVE_GPL
362 printf( "GPL version 2 or later\n" );
363 #else
364 printf( "Non-GPL commercial\n" );
365 #endif
366 #if HAVE_SWSCALE
367 const char *license = swscale_license();
368 printf( "libswscale%s%s license: %s\n", HAVE_LAVF ? "/libavformat" : "", HAVE_FFMS ? "/ffmpegsource" : "" , license );
369 if( !strcmp( license, "nonfree and unredistributable" ) ||
370 (!HAVE_GPL && (!strcmp( license, "GPL version 2 or later" )
371 || !strcmp( license, "GPL version 3 or later" ))))
372 printf( "WARNING: This binary is unredistributable!\n" );
373 #endif
376 static int main_internal( int argc, char **argv )
378 if( argc == 4 && !strcmp( argv[1], "--autocomplete" ) )
379 return x264_cli_autocomplete( argv[2], argv[3] );
381 x264_param_t param;
382 cli_opt_t opt = {0};
383 int ret = 0;
385 FAIL_IF_ERROR( x264_threading_init(), "unable to initialize threading\n" );
387 #ifdef _WIN32
388 FAIL_IF_ERROR( !get_argv_utf8( &argc, &argv ), "unable to convert command line to UTF-8\n" );
390 GetConsoleTitleW( org_console_title, CONSOLE_TITLE_SIZE );
391 _setmode( _fileno( stdin ), _O_BINARY );
392 _setmode( _fileno( stdout ), _O_BINARY );
393 _setmode( _fileno( stderr ), _O_BINARY );
394 #endif
396 /* Parse command line */
397 if( parse( argc, argv, &param, &opt ) < 0 )
398 ret = -1;
400 #ifdef _WIN32
401 /* Restore title; it can be changed by input modules */
402 SetConsoleTitleW( org_console_title );
403 #endif
405 /* Control-C handler */
406 signal( SIGINT, sigint_handler );
408 if( !ret )
409 ret = encode( &param, &opt );
411 /* clean up handles */
412 if( filter.free )
413 filter.free( opt.hin );
414 else if( opt.hin )
415 cli_input.close_file( opt.hin );
416 if( opt.hout )
417 cli_output.close_file( opt.hout, 0, 0 );
418 if( opt.tcfile_out )
419 fclose( opt.tcfile_out );
420 if( opt.qpfile )
421 fclose( opt.qpfile );
423 #ifdef _WIN32
424 SetConsoleTitleW( org_console_title );
425 free( argv );
426 #endif
428 return ret;
431 int main( int argc, char **argv )
433 return x264_stack_align( main_internal, argc, argv );
436 static char const *strtable_lookup( const char * const table[], int idx )
438 int i = 0; while( table[i] ) i++;
439 return ( ( idx >= 0 && idx < i ) ? table[ idx ] : "???" );
442 static char *stringify_names( char *buf, const char * const names[] )
444 int i = 0;
445 char *p = buf;
446 for( p[0] = 0; names[i]; i++ )
448 p += sprintf( p, "%s", names[i] );
449 if( names[i+1] )
450 p += sprintf( p, ", " );
452 return buf;
455 #define INDENT " "
456 #define INDENT_LEN 32 // strlen( INDENT )
457 #define SEPARATOR ", "
458 #define SEPARATOR_LEN 2 // strlen( SEPARATOR )
460 static void print_csp_name_internal( const char *name, size_t *line_len, int last )
462 if( name )
464 size_t name_len = strlen( name );
465 if( *line_len + name_len > (80 - SEPARATOR_LEN) )
467 printf( "\n" INDENT );
468 *line_len = INDENT_LEN;
470 printf( "%s", name );
471 *line_len += name_len;
472 if( !last )
474 printf( SEPARATOR );
475 *line_len += SEPARATOR_LEN;
480 static void print_csp_names( int longhelp )
482 if( longhelp < 2 )
483 return;
484 printf( " - valid csps for `raw' demuxer:\n" );
485 printf( INDENT );
486 size_t line_len = INDENT_LEN;
487 for( int i = X264_CSP_NONE+1; i < X264_CSP_CLI_MAX; i++ )
488 print_csp_name_internal( x264_cli_csps[i].name, &line_len, i == X264_CSP_CLI_MAX-1 );
489 #if HAVE_LAVF
490 printf( "\n" );
491 printf( " - valid csps for `lavf' demuxer:\n" );
492 printf( INDENT );
493 line_len = INDENT_LEN;
494 for( enum AVPixelFormat i = AV_PIX_FMT_NONE+1; i < AV_PIX_FMT_NB; i++ )
495 print_csp_name_internal( av_get_pix_fmt_name( i ), &line_len, i == AV_PIX_FMT_NB-1 );
496 #endif
497 printf( "\n" );
500 static void help( x264_param_t *defaults, int longhelp )
502 char buf[50];
503 #define H0 printf
504 #define H1 if( longhelp >= 1 ) printf
505 #define H2 if( longhelp == 2 ) printf
506 H0( "x264 core:%d%s\n"
507 "Syntax: x264 [options] -o outfile infile\n"
508 "\n"
509 "Infile can be raw (in which case resolution is required),\n"
510 " or YUV4MPEG (*.y4m),\n"
511 " or Avisynth if compiled with support (%s).\n"
512 " or libav* formats if compiled with lavf support (%s) or ffms support (%s).\n"
513 "Outfile type is selected by filename:\n"
514 " .264 -> Raw bytestream\n"
515 " .mkv -> Matroska\n"
516 " .flv -> Flash Video\n"
517 " .mp4 -> MP4 if compiled with GPAC or L-SMASH support (%s)\n"
518 "Output bit depth: %s\n."
519 "\n"
520 "Options:\n"
521 "\n"
522 " -h, --help List basic options\n"
523 " --longhelp List more options\n"
524 " --fullhelp List all options\n"
525 "\n",
526 X264_BUILD, X264_VERSION,
527 #if HAVE_AVS
528 "yes",
529 #else
530 "no",
531 #endif
532 #if HAVE_LAVF
533 "yes",
534 #else
535 "no",
536 #endif
537 #if HAVE_FFMS
538 "yes",
539 #else
540 "no",
541 #endif
542 #if HAVE_GPAC
543 "gpac",
544 #elif HAVE_LSMASH
545 "lsmash",
546 #else
547 "no",
548 #endif
549 #if HAVE_BITDEPTH8 && HAVE_BITDEPTH10
550 "8/10"
551 #elif HAVE_BITDEPTH8
553 #elif HAVE_BITDEPTH10
554 "10"
555 #else
556 "none"
557 #endif
559 H0( "Example usage:\n" );
560 H0( "\n" );
561 H0( " Constant quality mode:\n" );
562 H0( " x264 --crf 24 -o <output> <input>\n" );
563 H0( "\n" );
564 H0( " Two-pass with a bitrate of 1000kbps:\n" );
565 H0( " x264 --pass 1 --bitrate 1000 -o <output> <input>\n" );
566 H0( " x264 --pass 2 --bitrate 1000 -o <output> <input>\n" );
567 H0( "\n" );
568 H0( " Lossless:\n" );
569 H0( " x264 --qp 0 -o <output> <input>\n" );
570 H0( "\n" );
571 H0( " Maximum PSNR at the cost of speed and visual quality:\n" );
572 H0( " x264 --preset placebo --tune psnr -o <output> <input>\n" );
573 H0( "\n" );
574 H0( " Constant bitrate at 1000kbps with a 2 second-buffer:\n");
575 H0( " x264 --vbv-bufsize 2000 --bitrate 1000 -o <output> <input>\n" );
576 H0( "\n" );
577 H0( "Presets:\n" );
578 H0( "\n" );
579 H0( " --profile <string> Force the limits of an H.264 profile\n"
580 " Overrides all settings.\n" );
582 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT <= X264_CSP_I420
583 #if HAVE_BITDEPTH8
584 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I420
585 " - baseline:\n"
586 " --no-8x8dct --bframes 0 --no-cabac\n"
587 " --cqm flat --weightp 0\n"
588 " No interlaced.\n"
589 " No lossless.\n"
590 " - main:\n"
591 " --no-8x8dct --cqm flat\n"
592 " No lossless.\n"
593 #endif
594 " - high:\n"
595 " No lossless.\n"
596 #endif
597 #if HAVE_BITDEPTH10
598 " - high10:\n"
599 " No lossless.\n"
600 " Support for bit depth 8-10.\n"
601 #endif
602 #endif
603 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I422
604 " - high422:\n"
605 " No lossless.\n"
606 " Support for bit depth 8-10.\n"
607 " Support for 4:2:0/4:2:2 chroma subsampling.\n"
608 #endif
609 " - high444:\n"
610 " Support for bit depth 8-10.\n"
611 " Support for 4:2:0/4:2:2/4:4:4 chroma subsampling.\n" );
612 else H0( " - %s\n", stringify_names( buf, x264_valid_profile_names ) );
613 H0( " --preset <string> Use a preset to select encoding settings [medium]\n"
614 " Overridden by user settings.\n" );
615 H2( " - ultrafast:\n"
616 " --no-8x8dct --aq-mode 0 --b-adapt 0\n"
617 " --bframes 0 --no-cabac --no-deblock\n"
618 " --no-mbtree --me dia --no-mixed-refs\n"
619 " --partitions none --rc-lookahead 0 --ref 1\n"
620 " --scenecut 0 --subme 0 --trellis 0\n"
621 " --no-weightb --weightp 0\n"
622 " - superfast:\n"
623 " --no-mbtree --me dia --no-mixed-refs\n"
624 " --partitions i8x8,i4x4 --rc-lookahead 0\n"
625 " --ref 1 --subme 1 --trellis 0 --weightp 1\n"
626 " - veryfast:\n"
627 " --no-mixed-refs --rc-lookahead 10\n"
628 " --ref 1 --subme 2 --trellis 0 --weightp 1\n"
629 " - faster:\n"
630 " --no-mixed-refs --rc-lookahead 20\n"
631 " --ref 2 --subme 4 --weightp 1\n"
632 " - fast:\n"
633 " --rc-lookahead 30 --ref 2 --subme 6\n"
634 " --weightp 1\n"
635 " - medium:\n"
636 " Default settings apply.\n"
637 " - slow:\n"
638 " --direct auto --rc-lookahead 50 --ref 5\n"
639 " --subme 8 --trellis 2\n"
640 " - slower:\n"
641 " --b-adapt 2 --direct auto --me umh\n"
642 " --partitions all --rc-lookahead 60\n"
643 " --ref 8 --subme 9 --trellis 2\n"
644 " - veryslow:\n"
645 " --b-adapt 2 --bframes 8 --direct auto\n"
646 " --me umh --merange 24 --partitions all\n"
647 " --ref 16 --subme 10 --trellis 2\n"
648 " --rc-lookahead 60\n"
649 " - placebo:\n"
650 " --bframes 16 --b-adapt 2 --direct auto\n"
651 " --slow-firstpass --no-fast-pskip\n"
652 " --me tesa --merange 24 --partitions all\n"
653 " --rc-lookahead 60 --ref 16 --subme 11\n"
654 " --trellis 2\n" );
655 else H0( " - ultrafast,superfast,veryfast,faster,fast\n"
656 " - medium,slow,slower,veryslow,placebo\n" );
657 H0( " --tune <string> Tune the settings for a particular type of source\n"
658 " or situation\n"
659 " Overridden by user settings.\n"
660 " Multiple tunings are separated by commas.\n"
661 " Only one psy tuning can be used at a time.\n" );
662 H2( " - film (psy tuning):\n"
663 " --deblock -1:-1 --psy-rd <unset>:0.15\n"
664 " - animation (psy tuning):\n"
665 " --bframes {+2} --deblock 1:1\n"
666 " --psy-rd 0.4:<unset> --aq-strength 0.6\n"
667 " --ref {Double if >1 else 1}\n"
668 " - grain (psy tuning):\n"
669 " --aq-strength 0.5 --no-dct-decimate\n"
670 " --deadzone-inter 6 --deadzone-intra 6\n"
671 " --deblock -2:-2 --ipratio 1.1\n"
672 " --pbratio 1.1 --psy-rd <unset>:0.25\n"
673 " --qcomp 0.8\n"
674 " - stillimage (psy tuning):\n"
675 " --aq-strength 1.2 --deblock -3:-3\n"
676 " --psy-rd 2.0:0.7\n"
677 " - psnr (psy tuning):\n"
678 " --aq-mode 0 --no-psy\n"
679 " - ssim (psy tuning):\n"
680 " --aq-mode 2 --no-psy\n"
681 " - fastdecode:\n"
682 " --no-cabac --no-deblock --no-weightb\n"
683 " --weightp 0\n"
684 " - zerolatency:\n"
685 " --bframes 0 --force-cfr --no-mbtree\n"
686 " --sync-lookahead 0 --sliced-threads\n"
687 " --rc-lookahead 0\n" );
688 else H0( " - psy tunings: film,animation,grain,\n"
689 " stillimage,psnr,ssim\n"
690 " - other tunings: fastdecode,zerolatency\n" );
691 H2( " --slow-firstpass Don't force these faster settings with --pass 1:\n"
692 " --no-8x8dct --me dia --partitions none\n"
693 " --ref 1 --subme {2 if >2 else unchanged}\n"
694 " --trellis 0 --fast-pskip\n" );
695 else H1( " --slow-firstpass Don't force faster settings with --pass 1\n" );
696 H0( "\n" );
697 H0( "Frame-type options:\n" );
698 H0( "\n" );
699 H0( " -I, --keyint <integer or \"infinite\"> Maximum GOP size [%d]\n", defaults->i_keyint_max );
700 H2( " -i, --min-keyint <integer> Minimum GOP size [auto]\n" );
701 H2( " --no-scenecut Disable adaptive I-frame decision\n" );
702 H2( " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
703 H2( " --intra-refresh Use Periodic Intra Refresh instead of IDR frames\n" );
704 H1( " -b, --bframes <integer> Number of B-frames between I and P [%d]\n", defaults->i_bframe );
705 H1( " --b-adapt <integer> Adaptive B-frame decision method [%d]\n"
706 " Higher values may lower threading efficiency.\n"
707 " - 0: Disabled\n"
708 " - 1: Fast\n"
709 " - 2: Optimal (slow with high --bframes)\n", defaults->i_bframe_adaptive );
710 H2( " --b-bias <integer> Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
711 H1( " --b-pyramid <string> Keep some B-frames as references [%s]\n"
712 " - none: Disabled\n"
713 " - strict: Strictly hierarchical pyramid\n"
714 " - normal: Non-strict (not Blu-ray compatible)\n",
715 strtable_lookup( x264_b_pyramid_names, defaults->i_bframe_pyramid ) );
716 H1( " --open-gop Use recovery points to close GOPs\n"
717 " Only available with b-frames\n" );
718 H1( " --no-cabac Disable CABAC\n" );
719 H1( " -r, --ref <integer> Number of reference frames [%d]\n", defaults->i_frame_reference );
720 H1( " --no-deblock Disable loop filter\n" );
721 H1( " -f, --deblock <alpha:beta> Loop filter parameters [%d:%d]\n",
722 defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
723 H2( " --slices <integer> Number of slices per frame; forces rectangular\n"
724 " slices and is overridden by other slicing options\n" );
725 else H1( " --slices <integer> Number of slices per frame\n" );
726 H2( " --slices-max <integer> Absolute maximum slices per frame; overrides\n"
727 " slice-max-size/slice-max-mbs when necessary\n" );
728 H2( " --slice-max-size <integer> Limit the size of each slice in bytes\n");
729 H2( " --slice-max-mbs <integer> Limit the size of each slice in macroblocks (max)\n");
730 H2( " --slice-min-mbs <integer> Limit the size of each slice in macroblocks (min)\n");
731 H0( " --tff Enable interlaced mode (top field first)\n" );
732 H0( " --bff Enable interlaced mode (bottom field first)\n" );
733 H2( " --constrained-intra Enable constrained intra prediction.\n" );
734 H0( " --pulldown <string> Use soft pulldown to change frame rate\n"
735 " - none, 22, 32, 64, double, triple, euro (requires cfr input)\n" );
736 H2( " --fake-interlaced Flag stream as interlaced but encode progressive.\n"
737 " Makes it possible to encode 25p and 30p Blu-Ray\n"
738 " streams. Ignored in interlaced mode.\n" );
739 H2( " --frame-packing <integer> For stereoscopic videos define frame arrangement\n"
740 " - 0: checkerboard - pixels are alternatively from L and R\n"
741 " - 1: column alternation - L and R are interlaced by column\n"
742 " - 2: row alternation - L and R are interlaced by row\n"
743 " - 3: side by side - L is on the left, R on the right\n"
744 " - 4: top bottom - L is on top, R on bottom\n"
745 " - 5: frame alternation - one view per frame\n"
746 " - 6: mono - 2D frame without any frame packing\n"
747 " - 7: tile format - L is on top-left, R split across\n" );
748 H0( "\n" );
749 H0( "Ratecontrol:\n" );
750 H0( "\n" );
751 H1( " -q, --qp <integer> Force constant QP (0-%d, 0=lossless)\n", QP_MAX );
752 H0( " -B, --bitrate <integer> Set bitrate (kbit/s)\n" );
753 H0( " --crf <float> Quality-based VBR (%d-51) [%.1f]\n", 51 - QP_MAX_SPEC, defaults->rc.f_rf_constant );
754 H1( " --rc-lookahead <integer> Number of frames for frametype lookahead [%d]\n", defaults->rc.i_lookahead );
755 H0( " --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
756 H0( " --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
757 H2( " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
758 H2( " --crf-max <float> With CRF+VBV, limit RF to this value\n"
759 " May cause VBV underflows!\n" );
760 H2( " --qpmin <integer> Set min QP [%d]\n", defaults->rc.i_qp_min );
761 H2( " --qpmax <integer> Set max QP [%d]\n", X264_MIN( defaults->rc.i_qp_max, QP_MAX ) );
762 H2( " --qpstep <integer> Set max QP step [%d]\n", defaults->rc.i_qp_step );
763 H2( " --ratetol <float> Tolerance of ABR ratecontrol and VBV [%.1f]\n", defaults->rc.f_rate_tolerance );
764 H2( " --ipratio <float> QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
765 H2( " --pbratio <float> QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
766 H2( " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
767 H2( " --aq-mode <integer> AQ method [%d]\n"
768 " - 0: Disabled\n"
769 " - 1: Variance AQ (complexity mask)\n"
770 " - 2: Auto-variance AQ\n"
771 " - 3: Auto-variance AQ with bias to dark scenes\n", defaults->rc.i_aq_mode );
772 H1( " --aq-strength <float> Reduces blocking and blurring in flat and\n"
773 " textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
774 H1( "\n" );
775 H0( " -p, --pass <integer> Enable multipass ratecontrol\n"
776 " - 1: First pass, creates stats file\n"
777 " - 2: Last pass, does not overwrite stats file\n" );
778 H2( " - 3: Nth pass, overwrites stats file\n" );
779 H1( " --stats <string> Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
780 H2( " --no-mbtree Disable mb-tree ratecontrol.\n");
781 H2( " --qcomp <float> QP curve compression [%.2f]\n", defaults->rc.f_qcompress );
782 H2( " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
783 H2( " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
784 H2( " --zones <zone0>/<zone1>/... Tweak the bitrate of regions of the video\n" );
785 H2( " Each zone is of the form\n"
786 " <start frame>,<end frame>,<option>\n"
787 " where <option> is either\n"
788 " q=<integer> (force QP)\n"
789 " or b=<float> (bitrate multiplier)\n" );
790 H2( " --qpfile <string> Force frametypes and QPs for some or all frames\n"
791 " Format of each line: framenumber frametype QP\n"
792 " QP is optional (none lets x264 choose). Frametypes: I,i,K,P,B,b.\n"
793 " K=<I or i> depending on open-gop setting\n"
794 " QPs are restricted by qpmin/qpmax.\n" );
795 H1( "\n" );
796 H1( "Analysis:\n" );
797 H1( "\n" );
798 H1( " -A, --partitions <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
799 " - %s\n"
800 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n", stringify_names( buf, x264_partition_names ) );
801 H1( " --direct <string> Direct MV prediction mode [\"%s\"]\n"
802 " - none, spatial, temporal, auto\n",
803 strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
804 H2( " --no-weightb Disable weighted prediction for B-frames\n" );
805 H1( " --weightp <integer> Weighted prediction for P-frames [%d]\n"
806 " - 0: Disabled\n"
807 " - 1: Weighted refs\n"
808 " - 2: Weighted refs + Duplicates\n", defaults->analyse.i_weighted_pred );
809 H1( " --me <string> Integer pixel motion estimation method [\"%s\"]\n",
810 strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
811 H2( " - dia: diamond search, radius 1 (fast)\n"
812 " - hex: hexagonal search, radius 2\n"
813 " - umh: uneven multi-hexagon search\n"
814 " - esa: exhaustive search\n"
815 " - tesa: hadamard exhaustive search (slow)\n" );
816 else H1( " - dia, hex, umh\n" );
817 H2( " --merange <integer> Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
818 H2( " --mvrange <integer> Maximum motion vector length [-1 (auto)]\n" );
819 H2( " --mvrange-thread <int> Minimum buffer between threads [-1 (auto)]\n" );
820 H1( " -m, --subme <integer> Subpixel motion estimation and mode decision [%d]\n", defaults->analyse.i_subpel_refine );
821 H2( " - 0: fullpel only (not recommended)\n"
822 " - 1: SAD mode decision, one qpel iteration\n"
823 " - 2: SATD mode decision\n"
824 " - 3-5: Progressively more qpel\n"
825 " - 6: RD mode decision for I/P-frames\n"
826 " - 7: RD mode decision for all frames\n"
827 " - 8: RD refinement for I/P-frames\n"
828 " - 9: RD refinement for all frames\n"
829 " - 10: QP-RD - requires trellis=2, aq-mode>0\n"
830 " - 11: Full RD: disable all early terminations\n" );
831 else H1( " decision quality: 1=fast, 11=best\n" );
832 H1( " --psy-rd <float:float> Strength of psychovisual optimization [\"%.1f:%.1f\"]\n"
833 " #1: RD (requires subme>=6)\n"
834 " #2: Trellis (requires trellis, experimental)\n",
835 defaults->analyse.f_psy_rd, defaults->analyse.f_psy_trellis );
836 H2( " --no-psy Disable all visual optimizations that worsen\n"
837 " both PSNR and SSIM.\n" );
838 H2( " --no-mixed-refs Don't decide references on a per partition basis\n" );
839 H2( " --no-chroma-me Ignore chroma in motion estimation\n" );
840 H1( " --no-8x8dct Disable adaptive spatial transform size\n" );
841 H1( " -t, --trellis <integer> Trellis RD quantization. [%d]\n"
842 " - 0: disabled\n"
843 " - 1: enabled only on the final encode of a MB\n"
844 " - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
845 H2( " --no-fast-pskip Disables early SKIP detection on P-frames\n" );
846 H2( " --no-dct-decimate Disables coefficient thresholding on P-frames\n" );
847 H1( " --nr <integer> Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
848 H2( "\n" );
849 H2( " --deadzone-inter <int> Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
850 H2( " --deadzone-intra <int> Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
851 H2( " Deadzones should be in the range 0 - 32.\n" );
852 H2( " --cqm <string> Preset quant matrices [\"%s\"]\n"
853 " - %s\n", x264_cqm_names[0], stringify_names( buf, x264_cqm_names ) );
854 H1( " --cqmfile <string> Read custom quant matrices from a JM-compatible file\n" );
855 H2( " Overrides any other --cqm* options.\n" );
856 H2( " --cqm4 <list> Set all 4x4 quant matrices\n"
857 " Takes a comma-separated list of 16 integers.\n" );
858 H2( " --cqm8 <list> Set all 8x8 quant matrices\n"
859 " Takes a comma-separated list of 64 integers.\n" );
860 H2( " --cqm4i, --cqm4p, --cqm8i, --cqm8p <list>\n"
861 " Set both luma and chroma quant matrices\n" );
862 H2( " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc <list>\n"
863 " Set individual quant matrices\n" );
864 H2( "\n" );
865 H2( "Video Usability Info (Annex E):\n" );
866 H2( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
867 H2( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
868 H2( "\n" );
869 H2( " --overscan <string> Specify crop overscan setting [\"%s\"]\n"
870 " - undef, show, crop\n",
871 strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ) );
872 H2( " --videoformat <string> Specify video format [\"%s\"]\n"
873 " - component, pal, ntsc, secam, mac, undef\n",
874 strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ) );
875 H2( " --range <string> Specify color range [\"%s\"]\n"
876 " - %s\n", x264_range_names[0], stringify_names( buf, x264_range_names ) );
877 H2( " --colorprim <string> Specify color primaries [\"%s\"]\n"
878 " - undef, bt709, bt470m, bt470bg, smpte170m,\n"
879 " smpte240m, film, bt2020, smpte428,\n"
880 " smpte431, smpte432\n",
881 strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ) );
882 H2( " --transfer <string> Specify transfer characteristics [\"%s\"]\n"
883 " - undef, bt709, bt470m, bt470bg, smpte170m,\n"
884 " smpte240m, linear, log100, log316,\n"
885 " iec61966-2-4, bt1361e, iec61966-2-1,\n"
886 " bt2020-10, bt2020-12, smpte2084, smpte428,\n"
887 " arib-std-b67\n",
888 strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ) );
889 H2( " --colormatrix <string> Specify color matrix setting [\"%s\"]\n"
890 " - undef, bt709, fcc, bt470bg, smpte170m,\n"
891 " smpte240m, GBR, YCgCo, bt2020nc, bt2020c,\n"
892 " smpte2085, chroma-derived-nc,\n"
893 " chroma-derived-c, ICtCp\n",
894 strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
895 H2( " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n",
896 defaults->vui.i_chroma_loc );
897 H2( " --alternative-transfer <string> Specify an alternative transfer\n"
898 " characteristics [\"%s\"]\n"
899 " - same values as --transfer\n",
900 strtable_lookup( x264_transfer_names, defaults->i_alternative_transfer ) );
901 H2( " --nal-hrd <string> Signal HRD information (requires vbv-bufsize)\n"
902 " - none, vbr, cbr (cbr not allowed in .mp4)\n" );
903 H2( " --filler Force hard-CBR and generate filler (implied by\n"
904 " --nal-hrd cbr)\n" );
905 H2( " --pic-struct Force pic_struct in Picture Timing SEI\n" );
906 H2( " --crop-rect <string> Add 'left,top,right,bottom' to the bitstream-level\n"
907 " cropping rectangle\n" );
909 H0( "\n" );
910 H0( "Input/Output:\n" );
911 H0( "\n" );
912 H0( " -o, --output <string> Specify output file\n" );
913 H1( " --muxer <string> Specify output container format [\"%s\"]\n"
914 " - %s\n", x264_muxer_names[0], stringify_names( buf, x264_muxer_names ) );
915 H1( " --demuxer <string> Specify input container format [\"%s\"]\n"
916 " - %s\n", x264_demuxer_names[0], stringify_names( buf, x264_demuxer_names ) );
917 H1( " --input-fmt <string> Specify input file format (requires lavf support)\n" );
918 H1( " --input-csp <string> Specify input colorspace format for raw input\n" );
919 print_csp_names( longhelp );
920 H1( " --output-csp <string> Specify output colorspace [\"%s\"]\n"
921 " - %s\n",
922 #if X264_CHROMA_FORMAT
923 x264_output_csp_names[0],
924 #else
925 "i420",
926 #endif
927 stringify_names( buf, x264_output_csp_names ) );
928 H1( " --input-depth <integer> Specify input bit depth for raw input\n" );
929 H1( " --output-depth <integer> Specify output bit depth\n" );
930 H1( " --input-range <string> Specify input color range [\"%s\"]\n"
931 " - %s\n", x264_range_names[0], stringify_names( buf, x264_range_names ) );
932 H1( " --input-res <intxint> Specify input resolution (width x height)\n" );
933 H1( " --index <string> Filename for input index file\n" );
934 H0( " --sar width:height Specify Sample Aspect Ratio\n" );
935 H0( " --fps <float|rational> Specify framerate\n" );
936 H0( " --seek <integer> First frame to encode\n" );
937 H0( " --frames <integer> Maximum number of frames to encode\n" );
938 H0( " --level <string> Specify level (as defined by Annex A)\n" );
939 H1( " --bluray-compat Enable compatibility hacks for Blu-ray support\n" );
940 H1( " --avcintra-class <integer> Use compatibility hacks for AVC-Intra class\n"
941 " - %s\n", stringify_names( buf, x264_avcintra_class_names ) );
942 H1( " --avcintra-flavor <string> AVC-Intra flavor [\"%s\"]\n"
943 " - %s\n", x264_avcintra_flavor_names[0], stringify_names( buf, x264_avcintra_flavor_names ) );
944 H1( " --stitchable Don't optimize headers based on video content\n"
945 " Ensures ability to recombine a segmented encode\n" );
946 H1( "\n" );
947 H1( " -v, --verbose Print stats for each frame\n" );
948 H1( " --no-progress Don't show the progress indicator while encoding\n" );
949 H0( " --quiet Quiet Mode\n" );
950 H1( " --log-level <string> Specify the maximum level of logging [\"%s\"]\n"
951 " - %s\n", strtable_lookup( x264_log_level_names, cli_log_level - X264_LOG_NONE ),
952 stringify_names( buf, x264_log_level_names ) );
953 H1( " --psnr Enable PSNR computation\n" );
954 H1( " --ssim Enable SSIM computation\n" );
955 H1( " --threads <integer> Force a specific number of threads\n" );
956 H2( " --lookahead-threads <integer> Force a specific number of lookahead threads\n" );
957 H2( " --sliced-threads Low-latency but lower-efficiency threading\n" );
958 H2( " --thread-input Run Avisynth in its own thread\n" );
959 H2( " --sync-lookahead <integer> Number of buffer frames for threaded lookahead\n" );
960 H2( " --non-deterministic Slightly improve quality of SMP, at the cost of repeatability\n" );
961 H2( " --cpu-independent Ensure exact reproducibility across different cpus,\n"
962 " as opposed to letting them select different algorithms\n" );
963 H2( " --asm <integer> Override CPU detection\n" );
964 H2( " --no-asm Disable all CPU optimizations\n" );
965 H2( " --opencl Enable use of OpenCL\n" );
966 H2( " --opencl-clbin <string> Specify path of compiled OpenCL kernel cache\n" );
967 H2( " --opencl-device <integer> Specify OpenCL device ordinal\n" );
968 H2( " --dump-yuv <string> Save reconstructed frames\n" );
969 H2( " --sps-id <integer> Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
970 H2( " --aud Use access unit delimiters\n" );
971 H2( " --force-cfr Force constant framerate timestamp generation\n" );
972 H2( " --tcfile-in <string> Force timestamp generation with timecode file\n" );
973 H2( " --tcfile-out <string> Output timecode v2 file from input timestamps\n" );
974 H2( " --timebase <int/int> Specify timebase numerator and denominator\n"
975 " <integer> Specify timebase numerator for input timecode file\n"
976 " or specify timebase denominator for other input\n" );
977 H2( " --dts-compress Eliminate initial delay with container DTS hack\n" );
978 H0( "\n" );
979 H0( "Filtering:\n" );
980 H0( "\n" );
981 H0( " --vf, --video-filter <filter0>/<filter1>/... Apply video filtering to the input file\n" );
982 H0( "\n" );
983 H0( " Filter options may be specified in <filter>:<option>=<value> format.\n" );
984 H0( "\n" );
985 H0( " Available filters:\n" );
986 x264_register_vid_filters();
987 x264_vid_filter_help( longhelp );
988 H0( "\n" );
991 typedef enum
993 OPT_FRAMES = 256,
994 OPT_SEEK,
995 OPT_QPFILE,
996 OPT_THREAD_INPUT,
997 OPT_QUIET,
998 OPT_NOPROGRESS,
999 OPT_LONGHELP,
1000 OPT_PROFILE,
1001 OPT_PRESET,
1002 OPT_TUNE,
1003 OPT_SLOWFIRSTPASS,
1004 OPT_FULLHELP,
1005 OPT_FPS,
1006 OPT_MUXER,
1007 OPT_DEMUXER,
1008 OPT_INDEX,
1009 OPT_INTERLACED,
1010 OPT_TCFILE_IN,
1011 OPT_TCFILE_OUT,
1012 OPT_TIMEBASE,
1013 OPT_PULLDOWN,
1014 OPT_LOG_LEVEL,
1015 OPT_VIDEO_FILTER,
1016 OPT_INPUT_FMT,
1017 OPT_INPUT_RES,
1018 OPT_INPUT_CSP,
1019 OPT_INPUT_DEPTH,
1020 OPT_OUTPUT_DEPTH,
1021 OPT_DTS_COMPRESSION,
1022 OPT_OUTPUT_CSP,
1023 OPT_INPUT_RANGE,
1024 OPT_RANGE
1025 } OptionsOPT;
1027 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
1028 static struct option long_options[] =
1030 { "help", no_argument, NULL, 'h' },
1031 { "longhelp", no_argument, NULL, OPT_LONGHELP },
1032 { "fullhelp", no_argument, NULL, OPT_FULLHELP },
1033 { "version", no_argument, NULL, 'V' },
1034 { "profile", required_argument, NULL, OPT_PROFILE },
1035 { "preset", required_argument, NULL, OPT_PRESET },
1036 { "tune", required_argument, NULL, OPT_TUNE },
1037 { "slow-firstpass", no_argument, NULL, OPT_SLOWFIRSTPASS },
1038 { "bitrate", required_argument, NULL, 'B' },
1039 { "bframes", required_argument, NULL, 'b' },
1040 { "b-adapt", required_argument, NULL, 0 },
1041 { "no-b-adapt", no_argument, NULL, 0 },
1042 { "b-bias", required_argument, NULL, 0 },
1043 { "b-pyramid", required_argument, NULL, 0 },
1044 { "open-gop", no_argument, NULL, 0 },
1045 { "bluray-compat", no_argument, NULL, 0 },
1046 { "avcintra-class", required_argument, NULL, 0 },
1047 { "avcintra-flavor", required_argument, NULL, 0 },
1048 { "min-keyint", required_argument, NULL, 'i' },
1049 { "keyint", required_argument, NULL, 'I' },
1050 { "intra-refresh", no_argument, NULL, 0 },
1051 { "scenecut", required_argument, NULL, 0 },
1052 { "no-scenecut", no_argument, NULL, 0 },
1053 { "nf", no_argument, NULL, 0 },
1054 { "no-deblock", no_argument, NULL, 0 },
1055 { "filter", required_argument, NULL, 0 },
1056 { "deblock", required_argument, NULL, 'f' },
1057 { "interlaced", no_argument, NULL, OPT_INTERLACED },
1058 { "tff", no_argument, NULL, OPT_INTERLACED },
1059 { "bff", no_argument, NULL, OPT_INTERLACED },
1060 { "no-interlaced", no_argument, NULL, OPT_INTERLACED },
1061 { "constrained-intra", no_argument, NULL, 0 },
1062 { "cabac", no_argument, NULL, 0 },
1063 { "no-cabac", no_argument, NULL, 0 },
1064 { "qp", required_argument, NULL, 'q' },
1065 { "qpmin", required_argument, NULL, 0 },
1066 { "qpmax", required_argument, NULL, 0 },
1067 { "qpstep", required_argument, NULL, 0 },
1068 { "crf", required_argument, NULL, 0 },
1069 { "rc-lookahead",required_argument, NULL, 0 },
1070 { "ref", required_argument, NULL, 'r' },
1071 { "asm", required_argument, NULL, 0 },
1072 { "no-asm", no_argument, NULL, 0 },
1073 { "opencl", no_argument, NULL, 1 },
1074 { "opencl-clbin",required_argument, NULL, 0 },
1075 { "opencl-device",required_argument, NULL, 0 },
1076 { "sar", required_argument, NULL, 0 },
1077 { "fps", required_argument, NULL, OPT_FPS },
1078 { "frames", required_argument, NULL, OPT_FRAMES },
1079 { "seek", required_argument, NULL, OPT_SEEK },
1080 { "output", required_argument, NULL, 'o' },
1081 { "muxer", required_argument, NULL, OPT_MUXER },
1082 { "demuxer", required_argument, NULL, OPT_DEMUXER },
1083 { "stdout", required_argument, NULL, OPT_MUXER },
1084 { "stdin", required_argument, NULL, OPT_DEMUXER },
1085 { "index", required_argument, NULL, OPT_INDEX },
1086 { "analyse", required_argument, NULL, 0 },
1087 { "partitions", required_argument, NULL, 'A' },
1088 { "direct", required_argument, NULL, 0 },
1089 { "weightb", no_argument, NULL, 'w' },
1090 { "no-weightb", no_argument, NULL, 0 },
1091 { "weightp", required_argument, NULL, 0 },
1092 { "me", required_argument, NULL, 0 },
1093 { "merange", required_argument, NULL, 0 },
1094 { "mvrange", required_argument, NULL, 0 },
1095 { "mvrange-thread", required_argument, NULL, 0 },
1096 { "subme", required_argument, NULL, 'm' },
1097 { "psy-rd", required_argument, NULL, 0 },
1098 { "no-psy", no_argument, NULL, 0 },
1099 { "psy", no_argument, NULL, 0 },
1100 { "mixed-refs", no_argument, NULL, 0 },
1101 { "no-mixed-refs", no_argument, NULL, 0 },
1102 { "no-chroma-me", no_argument, NULL, 0 },
1103 { "8x8dct", no_argument, NULL, '8' },
1104 { "no-8x8dct", no_argument, NULL, 0 },
1105 { "trellis", required_argument, NULL, 't' },
1106 { "fast-pskip", no_argument, NULL, 0 },
1107 { "no-fast-pskip", no_argument, NULL, 0 },
1108 { "no-dct-decimate", no_argument, NULL, 0 },
1109 { "aq-strength", required_argument, NULL, 0 },
1110 { "aq-mode", required_argument, NULL, 0 },
1111 { "deadzone-inter", required_argument, NULL, 0 },
1112 { "deadzone-intra", required_argument, NULL, 0 },
1113 { "level", required_argument, NULL, 0 },
1114 { "ratetol", required_argument, NULL, 0 },
1115 { "vbv-maxrate", required_argument, NULL, 0 },
1116 { "vbv-bufsize", required_argument, NULL, 0 },
1117 { "vbv-init", required_argument, NULL, 0 },
1118 { "crf-max", required_argument, NULL, 0 },
1119 { "ipratio", required_argument, NULL, 0 },
1120 { "pbratio", required_argument, NULL, 0 },
1121 { "chroma-qp-offset", required_argument, NULL, 0 },
1122 { "pass", required_argument, NULL, 'p' },
1123 { "stats", required_argument, NULL, 0 },
1124 { "qcomp", required_argument, NULL, 0 },
1125 { "mbtree", no_argument, NULL, 0 },
1126 { "no-mbtree", no_argument, NULL, 0 },
1127 { "qblur", required_argument, NULL, 0 },
1128 { "cplxblur", required_argument, NULL, 0 },
1129 { "zones", required_argument, NULL, 0 },
1130 { "qpfile", required_argument, NULL, OPT_QPFILE },
1131 { "threads", required_argument, NULL, 0 },
1132 { "lookahead-threads", required_argument, NULL, 0 },
1133 { "sliced-threads", no_argument, NULL, 0 },
1134 { "no-sliced-threads", no_argument, NULL, 0 },
1135 { "slice-max-size", required_argument, NULL, 0 },
1136 { "slice-max-mbs", required_argument, NULL, 0 },
1137 { "slice-min-mbs", required_argument, NULL, 0 },
1138 { "slices", required_argument, NULL, 0 },
1139 { "slices-max", required_argument, NULL, 0 },
1140 { "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
1141 { "sync-lookahead", required_argument, NULL, 0 },
1142 { "non-deterministic", no_argument, NULL, 0 },
1143 { "cpu-independent", no_argument, NULL, 0 },
1144 { "psnr", no_argument, NULL, 0 },
1145 { "ssim", no_argument, NULL, 0 },
1146 { "quiet", no_argument, NULL, OPT_QUIET },
1147 { "verbose", no_argument, NULL, 'v' },
1148 { "log-level", required_argument, NULL, OPT_LOG_LEVEL },
1149 { "no-progress", no_argument, NULL, OPT_NOPROGRESS },
1150 { "dump-yuv", required_argument, NULL, 0 },
1151 { "sps-id", required_argument, NULL, 0 },
1152 { "aud", no_argument, NULL, 0 },
1153 { "nr", required_argument, NULL, 0 },
1154 { "cqm", required_argument, NULL, 0 },
1155 { "cqmfile", required_argument, NULL, 0 },
1156 { "cqm4", required_argument, NULL, 0 },
1157 { "cqm4i", required_argument, NULL, 0 },
1158 { "cqm4iy", required_argument, NULL, 0 },
1159 { "cqm4ic", required_argument, NULL, 0 },
1160 { "cqm4p", required_argument, NULL, 0 },
1161 { "cqm4py", required_argument, NULL, 0 },
1162 { "cqm4pc", required_argument, NULL, 0 },
1163 { "cqm8", required_argument, NULL, 0 },
1164 { "cqm8i", required_argument, NULL, 0 },
1165 { "cqm8p", required_argument, NULL, 0 },
1166 { "overscan", required_argument, NULL, 0 },
1167 { "videoformat", required_argument, NULL, 0 },
1168 { "range", required_argument, NULL, OPT_RANGE },
1169 { "colorprim", required_argument, NULL, 0 },
1170 { "transfer", required_argument, NULL, 0 },
1171 { "colormatrix", required_argument, NULL, 0 },
1172 { "chromaloc", required_argument, NULL, 0 },
1173 { "force-cfr", no_argument, NULL, 0 },
1174 { "tcfile-in", required_argument, NULL, OPT_TCFILE_IN },
1175 { "tcfile-out", required_argument, NULL, OPT_TCFILE_OUT },
1176 { "timebase", required_argument, NULL, OPT_TIMEBASE },
1177 { "pic-struct", no_argument, NULL, 0 },
1178 { "crop-rect", required_argument, NULL, 0 },
1179 { "nal-hrd", required_argument, NULL, 0 },
1180 { "pulldown", required_argument, NULL, OPT_PULLDOWN },
1181 { "fake-interlaced", no_argument, NULL, 0 },
1182 { "frame-packing", required_argument, NULL, 0 },
1183 { "alternative-transfer", required_argument, NULL, 0 },
1184 { "vf", required_argument, NULL, OPT_VIDEO_FILTER },
1185 { "video-filter", required_argument, NULL, OPT_VIDEO_FILTER },
1186 { "input-fmt", required_argument, NULL, OPT_INPUT_FMT },
1187 { "input-res", required_argument, NULL, OPT_INPUT_RES },
1188 { "input-csp", required_argument, NULL, OPT_INPUT_CSP },
1189 { "input-depth", required_argument, NULL, OPT_INPUT_DEPTH },
1190 { "output-depth", required_argument, NULL, OPT_OUTPUT_DEPTH },
1191 { "dts-compress", no_argument, NULL, OPT_DTS_COMPRESSION },
1192 { "output-csp", required_argument, NULL, OPT_OUTPUT_CSP },
1193 { "input-range", required_argument, NULL, OPT_INPUT_RANGE },
1194 { "stitchable", no_argument, NULL, 0 },
1195 { "filler", no_argument, NULL, 0 },
1196 {0, 0, 0, 0}
1199 static int select_output( const char *muxer, char *filename, x264_param_t *param )
1201 const char *ext = get_filename_extension( filename );
1202 if( !strcmp( filename, "-" ) || strcasecmp( muxer, "auto" ) )
1203 ext = muxer;
1205 if( !strcasecmp( ext, "mp4" ) )
1207 #if HAVE_GPAC || HAVE_LSMASH
1208 cli_output = mp4_output;
1209 param->b_annexb = 0;
1210 param->b_repeat_headers = 0;
1211 if( param->i_nal_hrd == X264_NAL_HRD_CBR )
1213 x264_cli_log( "x264", X264_LOG_WARNING, "cbr nal-hrd is not compatible with mp4\n" );
1214 param->i_nal_hrd = X264_NAL_HRD_VBR;
1216 #else
1217 x264_cli_log( "x264", X264_LOG_ERROR, "not compiled with MP4 output support\n" );
1218 return -1;
1219 #endif
1221 else if( !strcasecmp( ext, "mkv" ) )
1223 cli_output = mkv_output;
1224 param->b_annexb = 0;
1225 param->b_repeat_headers = 0;
1227 else if( !strcasecmp( ext, "flv" ) )
1229 cli_output = flv_output;
1230 param->b_annexb = 0;
1231 param->b_repeat_headers = 0;
1233 else
1234 cli_output = raw_output;
1235 return 0;
1238 static int select_input( const char *demuxer, char *used_demuxer, char *filename,
1239 hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
1241 int b_auto = !strcasecmp( demuxer, "auto" );
1242 const char *ext = b_auto ? get_filename_extension( filename ) : "";
1243 int b_regular = strcmp( filename, "-" );
1244 if( !b_regular && b_auto )
1245 ext = "raw";
1246 b_regular = b_regular && x264_is_regular_file_path( filename );
1247 if( b_regular )
1249 FILE *f = x264_fopen( filename, "r" );
1250 if( f )
1252 b_regular = x264_is_regular_file( f );
1253 fclose( f );
1256 const char *module = b_auto ? ext : demuxer;
1258 if( !strcasecmp( module, "avs" ) || !strcasecmp( ext, "d2v" ) || !strcasecmp( ext, "dga" ) )
1260 #if HAVE_AVS
1261 cli_input = avs_input;
1262 module = "avs";
1263 #else
1264 x264_cli_log( "x264", X264_LOG_ERROR, "not compiled with AVS input support\n" );
1265 return -1;
1266 #endif
1268 else if( !strcasecmp( module, "y4m" ) )
1269 cli_input = y4m_input;
1270 else if( !strcasecmp( module, "raw" ) || !strcasecmp( ext, "yuv" ) )
1271 cli_input = raw_input;
1272 else
1274 #if HAVE_FFMS
1275 if( b_regular && (b_auto || !strcasecmp( demuxer, "ffms" )) &&
1276 !ffms_input.open_file( filename, p_handle, info, opt ) )
1278 module = "ffms";
1279 b_auto = 0;
1280 cli_input = ffms_input;
1282 #endif
1283 #if HAVE_LAVF
1284 if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
1285 !lavf_input.open_file( filename, p_handle, info, opt ) )
1287 module = "lavf";
1288 b_auto = 0;
1289 cli_input = lavf_input;
1291 #endif
1292 #if HAVE_AVS
1293 if( b_regular && (b_auto || !strcasecmp( demuxer, "avs" )) &&
1294 !avs_input.open_file( filename, p_handle, info, opt ) )
1296 module = "avs";
1297 b_auto = 0;
1298 cli_input = avs_input;
1300 #endif
1301 if( b_auto && !raw_input.open_file( filename, p_handle, info, opt ) )
1303 module = "raw";
1304 b_auto = 0;
1305 cli_input = raw_input;
1308 FAIL_IF_ERROR( !(*p_handle), "could not open input file `%s' via any method!\n", filename );
1310 strcpy( used_demuxer, module );
1312 return 0;
1315 static int init_vid_filters( char *sequence, hnd_t *handle, video_info_t *info, x264_param_t *param, int output_csp )
1317 x264_register_vid_filters();
1319 /* intialize baseline filters */
1320 if( x264_init_vid_filter( "source", handle, &filter, info, param, NULL ) ) /* wrap demuxer into a filter */
1321 return -1;
1322 if( x264_init_vid_filter( "resize", handle, &filter, info, param, "normcsp" ) ) /* normalize csps to be of a known/supported format */
1323 return -1;
1324 if( x264_init_vid_filter( "fix_vfr_pts", handle, &filter, info, param, NULL ) ) /* fix vfr pts */
1325 return -1;
1327 /* parse filter chain */
1328 for( char *p = sequence; p && *p; )
1330 int tok_len = strcspn( p, "/" );
1331 int p_len = strlen( p );
1332 p[tok_len] = 0;
1333 int name_len = strcspn( p, ":" );
1334 p[name_len] = 0;
1335 name_len += name_len != tok_len;
1336 if( x264_init_vid_filter( p, handle, &filter, info, param, p + name_len ) )
1337 return -1;
1338 p += X264_MIN( tok_len+1, p_len );
1341 /* force end result resolution */
1342 if( !param->i_width && !param->i_height )
1344 param->i_height = info->height;
1345 param->i_width = info->width;
1347 /* force the output csp to what the user specified (or the default) */
1348 param->i_csp = info->csp;
1349 int csp = info->csp & X264_CSP_MASK;
1350 if( output_csp == X264_CSP_I400 && csp != X264_CSP_I400 )
1351 param->i_csp = X264_CSP_I400;
1352 else if( output_csp == X264_CSP_I420 && (csp < X264_CSP_I420 || csp >= X264_CSP_I422) )
1353 param->i_csp = X264_CSP_I420;
1354 else if( output_csp == X264_CSP_I422 && (csp < X264_CSP_I422 || csp >= X264_CSP_I444) )
1355 param->i_csp = X264_CSP_I422;
1356 else if( output_csp == X264_CSP_I444 && (csp < X264_CSP_I444 || csp >= X264_CSP_BGR) )
1357 param->i_csp = X264_CSP_I444;
1358 else if( output_csp == X264_CSP_RGB && (csp < X264_CSP_BGR || csp > X264_CSP_RGB) )
1359 param->i_csp = X264_CSP_RGB;
1360 param->i_csp |= info->csp & X264_CSP_HIGH_DEPTH;
1361 /* if the output range is not forced, assign it to the input one now */
1362 if( param->vui.b_fullrange == RANGE_AUTO )
1363 param->vui.b_fullrange = info->fullrange;
1365 if( x264_init_vid_filter( "resize", handle, &filter, info, param, NULL ) )
1366 return -1;
1368 char args[20], name[20];
1369 sprintf( args, "bit_depth=%d", param->i_bitdepth );
1370 sprintf( name, "depth_%d", param->i_bitdepth );
1372 if( x264_init_vid_filter( name, handle, &filter, info, param, args ) )
1373 return -1;
1375 return 0;
1378 static int parse_enum_name( const char *arg, const char * const *names, const char **dst )
1380 for( int i = 0; names[i]; i++ )
1381 if( !strcasecmp( arg, names[i] ) )
1383 *dst = names[i];
1384 return 0;
1386 return -1;
1389 static int parse_enum_value( const char *arg, const char * const *names, int *dst )
1391 for( int i = 0; names[i]; i++ )
1392 if( !strcasecmp( arg, names[i] ) )
1394 *dst = i;
1395 return 0;
1397 return -1;
1400 static int parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
1402 char *input_filename = NULL;
1403 const char *demuxer = x264_demuxer_names[0];
1404 char *output_filename = NULL;
1405 const char *muxer = x264_muxer_names[0];
1406 char *tcfile_name = NULL;
1407 x264_param_t defaults;
1408 char *profile = NULL;
1409 char *vid_filters = NULL;
1410 int b_thread_input = 0;
1411 int b_turbo = 1;
1412 int b_user_ref = 0;
1413 int b_user_fps = 0;
1414 int b_user_interlaced = 0;
1415 cli_input_opt_t input_opt;
1416 cli_output_opt_t output_opt;
1417 char *preset = NULL;
1418 char *tune = NULL;
1420 x264_param_default( &defaults );
1421 cli_log_level = defaults.i_log_level;
1423 memset( &input_opt, 0, sizeof(cli_input_opt_t) );
1424 memset( &output_opt, 0, sizeof(cli_output_opt_t) );
1425 input_opt.bit_depth = 8;
1426 input_opt.input_range = input_opt.output_range = param->vui.b_fullrange = RANGE_AUTO;
1427 int output_csp = defaults.i_csp;
1428 opt->b_progress = 1;
1430 /* Presets are applied before all other options. */
1431 for( optind = 0;; )
1433 int c = getopt_long( argc, argv, short_options, long_options, NULL );
1434 if( c == -1 )
1435 break;
1436 if( c == OPT_PRESET )
1437 preset = optarg;
1438 if( c == OPT_TUNE )
1439 tune = optarg;
1440 else if( c == '?' )
1441 return -1;
1444 if( preset && !strcasecmp( preset, "placebo" ) )
1445 b_turbo = 0;
1447 if( x264_param_default_preset( param, preset, tune ) < 0 )
1448 return -1;
1450 /* Parse command line options */
1451 for( optind = 0;; )
1453 int b_error = 0;
1454 int long_options_index = -1;
1456 int c = getopt_long( argc, argv, short_options, long_options, &long_options_index );
1458 if( c == -1 )
1460 break;
1463 switch( c )
1465 case 'h':
1466 help( &defaults, 0 );
1467 exit(0);
1468 case OPT_LONGHELP:
1469 help( &defaults, 1 );
1470 exit(0);
1471 case OPT_FULLHELP:
1472 help( &defaults, 2 );
1473 exit(0);
1474 case 'V':
1475 print_version_info();
1476 exit(0);
1477 case OPT_FRAMES:
1478 param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
1479 break;
1480 case OPT_SEEK:
1481 opt->i_seek = X264_MAX( atoi( optarg ), 0 );
1482 break;
1483 case 'o':
1484 output_filename = optarg;
1485 break;
1486 case OPT_MUXER:
1487 FAIL_IF_ERROR( parse_enum_name( optarg, x264_muxer_names, &muxer ), "Unknown muxer `%s'\n", optarg );
1488 break;
1489 case OPT_DEMUXER:
1490 FAIL_IF_ERROR( parse_enum_name( optarg, x264_demuxer_names, &demuxer ), "Unknown demuxer `%s'\n", optarg );
1491 break;
1492 case OPT_INDEX:
1493 input_opt.index_file = optarg;
1494 break;
1495 case OPT_QPFILE:
1496 opt->qpfile = x264_fopen( optarg, "rb" );
1497 FAIL_IF_ERROR( !opt->qpfile, "can't open qpfile `%s'\n", optarg );
1498 if( !x264_is_regular_file( opt->qpfile ) )
1500 x264_cli_log( "x264", X264_LOG_ERROR, "qpfile incompatible with non-regular file `%s'\n", optarg );
1501 fclose( opt->qpfile );
1502 return -1;
1504 break;
1505 case OPT_THREAD_INPUT:
1506 b_thread_input = 1;
1507 break;
1508 case OPT_QUIET:
1509 cli_log_level = param->i_log_level = X264_LOG_NONE;
1510 break;
1511 case 'v':
1512 cli_log_level = param->i_log_level = X264_LOG_DEBUG;
1513 break;
1514 case OPT_LOG_LEVEL:
1515 if( !parse_enum_value( optarg, x264_log_level_names, &cli_log_level ) )
1516 cli_log_level += X264_LOG_NONE;
1517 else
1518 cli_log_level = atoi( optarg );
1519 param->i_log_level = cli_log_level;
1520 break;
1521 case OPT_NOPROGRESS:
1522 opt->b_progress = 0;
1523 break;
1524 case OPT_TUNE:
1525 case OPT_PRESET:
1526 break;
1527 case OPT_PROFILE:
1528 profile = optarg;
1529 break;
1530 case OPT_SLOWFIRSTPASS:
1531 b_turbo = 0;
1532 break;
1533 case 'r':
1534 b_user_ref = 1;
1535 goto generic_option;
1536 case OPT_FPS:
1537 b_user_fps = 1;
1538 param->b_vfr_input = 0;
1539 goto generic_option;
1540 case OPT_INTERLACED:
1541 b_user_interlaced = 1;
1542 goto generic_option;
1543 case OPT_TCFILE_IN:
1544 tcfile_name = optarg;
1545 break;
1546 case OPT_TCFILE_OUT:
1547 opt->tcfile_out = x264_fopen( optarg, "wb" );
1548 FAIL_IF_ERROR( !opt->tcfile_out, "can't open `%s'\n", optarg );
1549 break;
1550 case OPT_TIMEBASE:
1551 input_opt.timebase = optarg;
1552 break;
1553 case OPT_PULLDOWN:
1554 FAIL_IF_ERROR( parse_enum_value( optarg, x264_pulldown_names, &opt->i_pulldown ), "Unknown pulldown `%s'\n", optarg );
1555 break;
1556 case OPT_VIDEO_FILTER:
1557 vid_filters = optarg;
1558 break;
1559 case OPT_INPUT_FMT:
1560 input_opt.format = optarg;
1561 break;
1562 case OPT_INPUT_RES:
1563 input_opt.resolution = optarg;
1564 break;
1565 case OPT_INPUT_CSP:
1566 input_opt.colorspace = optarg;
1567 break;
1568 case OPT_INPUT_DEPTH:
1569 input_opt.bit_depth = atoi( optarg );
1570 break;
1571 case OPT_OUTPUT_DEPTH:
1572 param->i_bitdepth = atoi( optarg );
1573 break;
1574 case OPT_DTS_COMPRESSION:
1575 output_opt.use_dts_compress = 1;
1576 break;
1577 case OPT_OUTPUT_CSP:
1578 FAIL_IF_ERROR( parse_enum_value( optarg, x264_output_csp_names, &output_csp ), "Unknown output csp `%s'\n", optarg );
1579 // correct the parsed value to the libx264 csp value
1580 #if X264_CHROMA_FORMAT
1581 static const uint8_t output_csp_fix[] = { X264_CHROMA_FORMAT, X264_CSP_RGB };
1582 #else
1583 static const uint8_t output_csp_fix[] = { X264_CSP_I400, X264_CSP_I420, X264_CSP_I422, X264_CSP_I444, X264_CSP_RGB };
1584 #endif
1585 param->i_csp = output_csp = output_csp_fix[output_csp];
1586 break;
1587 case OPT_INPUT_RANGE:
1588 FAIL_IF_ERROR( parse_enum_value( optarg, x264_range_names, &input_opt.input_range ), "Unknown input range `%s'\n", optarg );
1589 input_opt.input_range += RANGE_AUTO;
1590 break;
1591 case OPT_RANGE:
1592 FAIL_IF_ERROR( parse_enum_value( optarg, x264_range_names, &param->vui.b_fullrange ), "Unknown range `%s'\n", optarg );
1593 input_opt.output_range = param->vui.b_fullrange += RANGE_AUTO;
1594 break;
1595 default:
1596 generic_option:
1598 if( long_options_index < 0 )
1600 for( int i = 0; long_options[i].name; i++ )
1601 if( long_options[i].val == c )
1603 long_options_index = i;
1604 break;
1606 if( long_options_index < 0 )
1608 /* getopt_long already printed an error message */
1609 return -1;
1613 b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg );
1617 if( b_error )
1619 const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
1620 x264_cli_log( "x264", X264_LOG_ERROR, "invalid argument: %s = %s\n", name, optarg );
1621 return -1;
1625 /* If first pass mode is used, apply faster settings. */
1626 if( b_turbo )
1627 x264_param_apply_fastfirstpass( param );
1629 /* Apply profile restrictions. */
1630 if( x264_param_apply_profile( param, profile ) < 0 )
1631 return -1;
1633 /* Get the file name */
1634 FAIL_IF_ERROR( optind > argc - 1 || !output_filename, "No %s file. Run x264 --help for a list of options.\n",
1635 optind > argc - 1 ? "input" : "output" );
1637 if( select_output( muxer, output_filename, param ) )
1638 return -1;
1639 FAIL_IF_ERROR( cli_output.open_file( output_filename, &opt->hout, &output_opt ), "could not open output file `%s'\n", output_filename );
1641 input_filename = argv[optind++];
1642 video_info_t info = {0};
1643 char demuxername[5];
1645 /* set info flags to be overwritten by demuxer as necessary. */
1646 info.csp = param->i_csp;
1647 info.fps_num = param->i_fps_num;
1648 info.fps_den = param->i_fps_den;
1649 info.fullrange = input_opt.input_range == RANGE_PC;
1650 info.interlaced = param->b_interlaced;
1651 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
1653 info.sar_width = param->vui.i_sar_width;
1654 info.sar_height = param->vui.i_sar_height;
1656 info.tff = param->b_tff;
1657 info.vfr = param->b_vfr_input;
1659 input_opt.seek = opt->i_seek;
1660 input_opt.progress = opt->b_progress;
1661 input_opt.output_csp = output_csp;
1663 if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
1664 return -1;
1666 FAIL_IF_ERROR( !opt->hin && cli_input.open_file( input_filename, &opt->hin, &info, &input_opt ),
1667 "could not open input file `%s'\n", input_filename );
1669 x264_reduce_fraction( &info.sar_width, &info.sar_height );
1670 x264_reduce_fraction( &info.fps_num, &info.fps_den );
1671 x264_cli_log( demuxername, X264_LOG_INFO, "%dx%d%c %u:%u @ %u/%u fps (%cfr)\n", info.width,
1672 info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
1673 info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
1675 if( tcfile_name )
1677 FAIL_IF_ERROR( b_user_fps, "--fps + --tcfile-in is incompatible.\n" );
1678 FAIL_IF_ERROR( timecode_input.open_file( tcfile_name, &opt->hin, &info, &input_opt ), "timecode input failed\n" );
1679 cli_input = timecode_input;
1681 else FAIL_IF_ERROR( !info.vfr && input_opt.timebase, "--timebase is incompatible with cfr input\n" );
1683 /* init threaded input while the information about the input video is unaltered by filtering */
1684 #if HAVE_THREAD
1685 const cli_input_t *thread_input;
1686 if( HAVE_BITDEPTH8 && param->i_bitdepth == 8 )
1687 thread_input = &thread_8_input;
1688 else if( HAVE_BITDEPTH10 && param->i_bitdepth == 10 )
1689 thread_input = &thread_10_input;
1690 else
1691 thread_input = NULL;
1693 if( thread_input && info.thread_safe && (b_thread_input || param->i_threads > 1
1694 || (param->i_threads == X264_THREADS_AUTO && x264_cpu_num_processors() > 1)) )
1696 if( thread_input->open_file( NULL, &opt->hin, &info, NULL ) )
1698 fprintf( stderr, "x264 [error]: threaded input failed\n" );
1699 return -1;
1701 cli_input = *thread_input;
1703 #endif
1705 /* override detected values by those specified by the user */
1706 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
1708 info.sar_width = param->vui.i_sar_width;
1709 info.sar_height = param->vui.i_sar_height;
1711 if( b_user_fps )
1713 info.fps_num = param->i_fps_num;
1714 info.fps_den = param->i_fps_den;
1716 if( !info.vfr )
1718 info.timebase_num = info.fps_den;
1719 info.timebase_den = info.fps_num;
1721 if( !tcfile_name && input_opt.timebase )
1723 uint64_t i_user_timebase_num;
1724 uint64_t i_user_timebase_den;
1725 int ret = sscanf( input_opt.timebase, "%"SCNu64"/%"SCNu64, &i_user_timebase_num, &i_user_timebase_den );
1726 FAIL_IF_ERROR( !ret, "invalid argument: timebase = %s\n", input_opt.timebase );
1727 if( ret == 1 )
1729 i_user_timebase_num = info.timebase_num;
1730 i_user_timebase_den = strtoul( input_opt.timebase, NULL, 10 );
1732 FAIL_IF_ERROR( i_user_timebase_num > UINT32_MAX || i_user_timebase_den > UINT32_MAX,
1733 "timebase you specified exceeds H.264 maximum\n" );
1734 opt->timebase_convert_multiplier = ((double)i_user_timebase_den / info.timebase_den)
1735 * ((double)info.timebase_num / i_user_timebase_num);
1736 info.timebase_num = i_user_timebase_num;
1737 info.timebase_den = i_user_timebase_den;
1738 info.vfr = 1;
1740 if( b_user_interlaced )
1742 info.interlaced = param->b_interlaced;
1743 info.tff = param->b_tff;
1745 if( input_opt.input_range != RANGE_AUTO )
1746 info.fullrange = input_opt.input_range;
1748 if( init_vid_filters( vid_filters, &opt->hin, &info, param, output_csp ) )
1749 return -1;
1751 /* set param flags from the post-filtered video */
1752 param->b_vfr_input = info.vfr;
1753 param->i_fps_num = info.fps_num;
1754 param->i_fps_den = info.fps_den;
1755 param->i_timebase_num = info.timebase_num;
1756 param->i_timebase_den = info.timebase_den;
1757 param->vui.i_sar_width = info.sar_width;
1758 param->vui.i_sar_height = info.sar_height;
1760 info.num_frames = X264_MAX( info.num_frames - opt->i_seek, 0 );
1761 if( (!info.num_frames || param->i_frame_total < info.num_frames)
1762 && param->i_frame_total > 0 )
1763 info.num_frames = param->i_frame_total;
1764 param->i_frame_total = info.num_frames;
1766 if( !b_user_interlaced && info.interlaced )
1768 #if HAVE_INTERLACED
1769 x264_cli_log( "x264", X264_LOG_WARNING, "input appears to be interlaced, enabling %cff interlaced mode.\n"
1770 " If you want otherwise, use --no-interlaced or --%cff\n",
1771 info.tff ? 't' : 'b', info.tff ? 'b' : 't' );
1772 param->b_interlaced = 1;
1773 param->b_tff = !!info.tff;
1774 #else
1775 x264_cli_log( "x264", X264_LOG_WARNING, "input appears to be interlaced, but not compiled with interlaced support\n" );
1776 #endif
1778 /* if the user never specified the output range and the input is now rgb, default it to pc */
1779 int csp = param->i_csp & X264_CSP_MASK;
1780 if( csp >= X264_CSP_BGR && csp <= X264_CSP_RGB )
1782 if( input_opt.output_range == RANGE_AUTO )
1783 param->vui.b_fullrange = RANGE_PC;
1784 /* otherwise fail if they specified tv */
1785 FAIL_IF_ERROR( !param->vui.b_fullrange, "RGB must be PC range" );
1788 /* Automatically reduce reference frame count to match the user's target level
1789 * if the user didn't explicitly set a reference frame count. */
1790 if( !b_user_ref )
1792 int mbs = (((param->i_width)+15)>>4) * (((param->i_height)+15)>>4);
1793 for( int i = 0; x264_levels[i].level_idc != 0; i++ )
1794 if( param->i_level_idc == x264_levels[i].level_idc )
1796 while( mbs * param->i_frame_reference > x264_levels[i].dpb && param->i_frame_reference > 1 )
1797 param->i_frame_reference--;
1798 break;
1803 return 0;
1806 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
1808 int num = -1;
1809 char type;
1810 while( num < i_frame )
1812 int64_t file_pos = ftell( opt->qpfile );
1813 int qp = -1;
1814 int ret = fscanf( opt->qpfile, "%d %c%*[ \t]%d\n", &num, &type, &qp );
1815 pic->i_type = X264_TYPE_AUTO;
1816 pic->i_qpplus1 = X264_QP_AUTO;
1817 if( num > i_frame || ret == EOF )
1819 if( file_pos < 0 || fseek( opt->qpfile, file_pos, SEEK_SET ) )
1821 x264_cli_log( "x264", X264_LOG_ERROR, "qpfile seeking failed\n" );
1822 fclose( opt->qpfile );
1823 opt->qpfile = NULL;
1825 break;
1827 if( num < i_frame && ret >= 2 )
1828 continue;
1829 if( ret == 3 && qp >= 0 )
1830 pic->i_qpplus1 = qp+1;
1831 if ( type == 'I' ) pic->i_type = X264_TYPE_IDR;
1832 else if( type == 'i' ) pic->i_type = X264_TYPE_I;
1833 else if( type == 'K' ) pic->i_type = X264_TYPE_KEYFRAME;
1834 else if( type == 'P' ) pic->i_type = X264_TYPE_P;
1835 else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
1836 else if( type == 'b' ) pic->i_type = X264_TYPE_B;
1837 else ret = 0;
1838 if( ret < 2 || qp < -1 || qp > QP_MAX )
1840 x264_cli_log( "x264", X264_LOG_ERROR, "can't parse qpfile for frame %d\n", i_frame );
1841 fclose( opt->qpfile );
1842 opt->qpfile = NULL;
1843 break;
1848 static int encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_dts )
1850 x264_picture_t pic_out;
1851 x264_nal_t *nal;
1852 int i_nal;
1853 int i_frame_size = 0;
1855 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
1857 FAIL_IF_ERROR( i_frame_size < 0, "x264_encoder_encode failed\n" );
1859 if( i_frame_size )
1861 i_frame_size = cli_output.write_frame( hout, nal[0].p_payload, i_frame_size, &pic_out );
1862 *last_dts = pic_out.i_dts;
1865 return i_frame_size;
1868 static int64_t print_status( int64_t i_start, int64_t i_previous, int i_frame, int i_frame_total, int64_t i_file, x264_param_t *param, int64_t last_ts )
1870 char buf[200];
1871 int64_t i_time = x264_mdate();
1872 if( i_previous && i_time - i_previous < UPDATE_INTERVAL )
1873 return i_previous;
1874 int64_t i_elapsed = i_time - i_start;
1875 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1876 double bitrate;
1877 if( last_ts )
1878 bitrate = (double) i_file * 8 / ( (double) last_ts * 1000 * param->i_timebase_num / param->i_timebase_den );
1879 else
1880 bitrate = (double) i_file * 8 / ( (double) 1000 * param->i_fps_den / param->i_fps_num );
1881 if( i_frame_total )
1883 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1884 sprintf( buf, "x264 [%.1f%%] %d/%d frames, %.2f fps, %.2f kb/s, eta %d:%02d:%02d",
1885 100. * i_frame / i_frame_total, i_frame, i_frame_total, fps, bitrate,
1886 eta/3600, (eta/60)%60, eta%60 );
1888 else
1889 sprintf( buf, "x264 %d frames: %.2f fps, %.2f kb/s", i_frame, fps, bitrate );
1890 fprintf( stderr, "%s \r", buf+5 );
1891 x264_cli_set_console_title( buf );
1892 fflush( stderr ); // needed in windows
1893 return i_time;
1896 static void convert_cli_to_lib_pic( x264_picture_t *lib, cli_pic_t *cli )
1898 memcpy( lib->img.i_stride, cli->img.stride, sizeof(cli->img.stride) );
1899 memcpy( lib->img.plane, cli->img.plane, sizeof(cli->img.plane) );
1900 lib->img.i_plane = cli->img.planes;
1901 lib->img.i_csp = cli->img.csp;
1902 lib->i_pts = cli->pts;
1905 #define FAIL_IF_ERROR2( cond, ... )\
1908 if( cond )\
1910 x264_cli_log( "x264", X264_LOG_ERROR, __VA_ARGS__ );\
1911 retval = -1;\
1912 goto fail;\
1914 } while( 0 )
1916 static int encode( x264_param_t *param, cli_opt_t *opt )
1918 x264_t *h = NULL;
1919 x264_picture_t pic;
1920 cli_pic_t cli_pic;
1921 const cli_pulldown_t *pulldown = NULL; // shut up gcc
1923 int i_frame = 0;
1924 int i_frame_output = 0;
1925 int64_t i_end, i_previous = 0, i_start = 0;
1926 int64_t i_file = 0;
1927 int i_frame_size;
1928 int64_t last_dts = 0;
1929 int64_t prev_dts = 0;
1930 int64_t first_dts = 0;
1931 # define MAX_PTS_WARNING 3 /* arbitrary */
1932 int pts_warning_cnt = 0;
1933 int64_t largest_pts = -1;
1934 int64_t second_largest_pts = -1;
1935 int64_t ticks_per_frame;
1936 double duration;
1937 double pulldown_pts = 0;
1938 int retval = 0;
1940 opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
1942 /* set up pulldown */
1943 if( opt->i_pulldown && !param->b_vfr_input )
1945 param->b_pulldown = 1;
1946 param->b_pic_struct = 1;
1947 pulldown = &pulldown_values[opt->i_pulldown];
1948 param->i_timebase_num = param->i_fps_den;
1949 FAIL_IF_ERROR2( fmod( param->i_fps_num * pulldown->fps_factor, 1 ),
1950 "unsupported framerate for chosen pulldown\n" );
1951 param->i_timebase_den = param->i_fps_num * pulldown->fps_factor;
1954 h = x264_encoder_open( param );
1955 FAIL_IF_ERROR2( !h, "x264_encoder_open failed\n" );
1957 x264_encoder_parameters( h, param );
1959 FAIL_IF_ERROR2( cli_output.set_param( opt->hout, param ), "can't set outfile param\n" );
1961 i_start = x264_mdate();
1963 /* ticks/frame = ticks/second / frames/second */
1964 ticks_per_frame = (int64_t)param->i_timebase_den * param->i_fps_den / param->i_timebase_num / param->i_fps_num;
1965 FAIL_IF_ERROR2( ticks_per_frame < 1 && !param->b_vfr_input, "ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame );
1966 ticks_per_frame = X264_MAX( ticks_per_frame, 1 );
1968 if( !param->b_repeat_headers )
1970 // Write SPS/PPS/SEI
1971 x264_nal_t *headers;
1972 int i_nal;
1974 FAIL_IF_ERROR2( x264_encoder_headers( h, &headers, &i_nal ) < 0, "x264_encoder_headers failed\n" );
1975 FAIL_IF_ERROR2( (i_file = cli_output.write_headers( opt->hout, headers )) < 0, "error writing headers to output file\n" );
1978 if( opt->tcfile_out )
1979 fprintf( opt->tcfile_out, "# timecode format v2\n" );
1981 /* Encode frames */
1982 for( ; !b_ctrl_c && (i_frame < param->i_frame_total || !param->i_frame_total); i_frame++ )
1984 if( filter.get_frame( opt->hin, &cli_pic, i_frame + opt->i_seek ) )
1985 break;
1986 x264_picture_init( &pic );
1987 convert_cli_to_lib_pic( &pic, &cli_pic );
1989 if( !param->b_vfr_input )
1990 pic.i_pts = i_frame;
1992 if( opt->i_pulldown && !param->b_vfr_input )
1994 pic.i_pic_struct = pulldown->pattern[ i_frame % pulldown->mod ];
1995 pic.i_pts = (int64_t)( pulldown_pts + 0.5 );
1996 pulldown_pts += pulldown_frame_duration[pic.i_pic_struct];
1998 else if( opt->timebase_convert_multiplier )
1999 pic.i_pts = (int64_t)( pic.i_pts * opt->timebase_convert_multiplier + 0.5 );
2001 if( pic.i_pts <= largest_pts )
2003 if( cli_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
2004 x264_cli_log( "x264", X264_LOG_WARNING, "non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
2005 i_frame, pic.i_pts, largest_pts );
2006 else if( pts_warning_cnt == MAX_PTS_WARNING )
2007 x264_cli_log( "x264", X264_LOG_WARNING, "too many nonmonotonic pts warnings, suppressing further ones\n" );
2008 pts_warning_cnt++;
2009 pic.i_pts = largest_pts + ticks_per_frame;
2012 second_largest_pts = largest_pts;
2013 largest_pts = pic.i_pts;
2014 if( opt->tcfile_out )
2015 fprintf( opt->tcfile_out, "%.6f\n", pic.i_pts * ((double)param->i_timebase_num / param->i_timebase_den) * 1e3 );
2017 if( opt->qpfile )
2018 parse_qpfile( opt, &pic, i_frame + opt->i_seek );
2020 prev_dts = last_dts;
2021 i_frame_size = encode_frame( h, opt->hout, &pic, &last_dts );
2022 if( i_frame_size < 0 )
2024 b_ctrl_c = 1; /* lie to exit the loop */
2025 retval = -1;
2027 else if( i_frame_size )
2029 i_file += i_frame_size;
2030 i_frame_output++;
2031 if( i_frame_output == 1 )
2032 first_dts = prev_dts = last_dts;
2035 if( filter.release_frame( opt->hin, &cli_pic, i_frame + opt->i_seek ) )
2036 break;
2038 /* update status line (up to 1000 times per input file) */
2039 if( opt->b_progress && i_frame_output )
2040 i_previous = print_status( i_start, i_previous, i_frame_output, param->i_frame_total, i_file, param, 2 * last_dts - prev_dts - first_dts );
2042 /* Flush delayed frames */
2043 while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
2045 prev_dts = last_dts;
2046 i_frame_size = encode_frame( h, opt->hout, NULL, &last_dts );
2047 if( i_frame_size < 0 )
2049 b_ctrl_c = 1; /* lie to exit the loop */
2050 retval = -1;
2052 else if( i_frame_size )
2054 i_file += i_frame_size;
2055 i_frame_output++;
2056 if( i_frame_output == 1 )
2057 first_dts = prev_dts = last_dts;
2059 if( opt->b_progress && i_frame_output )
2060 i_previous = print_status( i_start, i_previous, i_frame_output, param->i_frame_total, i_file, param, 2 * last_dts - prev_dts - first_dts );
2062 fail:
2063 if( pts_warning_cnt >= MAX_PTS_WARNING && cli_log_level < X264_LOG_DEBUG )
2064 x264_cli_log( "x264", X264_LOG_WARNING, "%d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
2066 /* duration algorithm fails when only 1 frame is output */
2067 if( i_frame_output == 1 )
2068 duration = (double)param->i_fps_den / param->i_fps_num;
2069 else if( b_ctrl_c )
2070 duration = (double)(2 * last_dts - prev_dts - first_dts) * param->i_timebase_num / param->i_timebase_den;
2071 else
2072 duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
2074 i_end = x264_mdate();
2075 /* Erase progress indicator before printing encoding stats. */
2076 if( opt->b_progress )
2077 fprintf( stderr, " \r" );
2078 if( h )
2079 x264_encoder_close( h );
2080 fprintf( stderr, "\n" );
2082 if( b_ctrl_c )
2083 fprintf( stderr, "aborted at input frame %d, output frame %d\n", opt->i_seek + i_frame, i_frame_output );
2085 cli_output.close_file( opt->hout, largest_pts, second_largest_pts );
2086 opt->hout = NULL;
2088 if( i_frame_output > 0 )
2090 double fps = (double)i_frame_output * (double)1000000 /
2091 (double)( i_end - i_start );
2093 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame_output, fps,
2094 (double) i_file * 8 / ( 1000 * duration ) );
2097 return retval;