1 /*****************************************************************************
2 * x264: top-level x264cli functions
3 *****************************************************************************
4 * Copyright (C) 2003-2016 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 *****************************************************************************/
32 /* The following two defines must be located before the inclusion of any system header files. */
34 #define _WIN32_WINNT 0x0500
36 #include <io.h> /* _setmode() */
37 #include <fcntl.h> /* _O_BINARY */
42 #include "common/common.h"
44 #include "input/input.h"
45 #include "output/output.h"
46 #include "filters/filters.h"
48 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "x264", __VA_ARGS__ )
51 #undef DECLARE_ALIGNED
52 #include <libavformat/avformat.h>
53 #include <libavutil/pixfmt.h>
54 #include <libavutil/pixdesc.h>
58 #undef DECLARE_ALIGNED
59 #include <libswscale/swscale.h>
67 #define CONSOLE_TITLE_SIZE 200
68 static wchar_t org_console_title[CONSOLE_TITLE_SIZE] = L"";
70 void x264_cli_set_console_title( const char *title )
72 wchar_t title_utf16[CONSOLE_TITLE_SIZE];
73 if( utf8_to_utf16( title, title_utf16 ) )
74 SetConsoleTitleW( title_utf16 );
77 static int utf16_to_ansi( const wchar_t *utf16, char *ansi, int size )
80 return WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, utf16, -1, ansi, size, NULL, &invalid ) && !invalid;
83 /* Some external libraries doesn't support Unicode in filenames,
84 * as a workaround we can try to get an ANSI filename instead. */
85 int x264_ansi_filename( const char *filename, char *ansi_filename, int size, int create_file )
87 wchar_t filename_utf16[MAX_PATH];
88 if( utf8_to_utf16( filename, filename_utf16 ) )
92 /* Create the file using the Unicode filename if it doesn't already exist. */
93 FILE *fh = _wfopen( filename_utf16, L"ab" );
98 /* Check if the filename already is valid ANSI. */
99 if( utf16_to_ansi( filename_utf16, ansi_filename, size ) )
102 /* Check for a legacy 8.3 short filename. */
103 int short_length = GetShortPathNameW( filename_utf16, filename_utf16, MAX_PATH );
104 if( short_length > 0 && short_length < MAX_PATH )
105 if( utf16_to_ansi( filename_utf16, ansi_filename, size ) )
111 /* Retrieve command line arguments as UTF-8. */
112 static int get_argv_utf8( int *argc_ptr, char ***argv_ptr )
115 wchar_t **argv_utf16 = CommandLineToArgvW( GetCommandLineW(), argc_ptr );
118 int argc = *argc_ptr;
119 int offset = (argc+1) * sizeof(char*);
122 for( int i = 0; i < argc; i++ )
123 size += WideCharToMultiByte( CP_UTF8, 0, argv_utf16[i], -1, NULL, 0, NULL, NULL );
125 char **argv = *argv_ptr = malloc( size );
128 for( int i = 0; i < argc; i++ )
130 argv[i] = (char*)argv + offset;
131 offset += WideCharToMultiByte( CP_UTF8, 0, argv_utf16[i], -1, argv[i], size-offset, NULL, NULL );
136 LocalFree( argv_utf16 );
143 static volatile int b_ctrl_c = 0;
144 static void sigint_handler( int a )
156 double timebase_convert_multiplier;
160 /* file i/o operation structs */
161 cli_input_t cli_input;
162 static cli_output_t cli_output;
164 /* video filter operation struct */
165 static cli_vid_filter_t filter;
167 static const char * const demuxer_names[] =
184 static const char * const muxer_names[] =
190 #if HAVE_GPAC || HAVE_LSMASH
196 static const char * const pulldown_names[] = { "none", "22", "32", "64", "double", "triple", "euro", 0 };
197 static const char * const log_level_names[] = { "none", "error", "warning", "info", "debug", 0 };
198 static const char * const output_csp_names[] =
200 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I420
203 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I422
206 #if !X264_CHROMA_FORMAT || X264_CHROMA_FORMAT == X264_CSP_I444
211 static const char * const chroma_format_names[] =
214 [X264_CSP_I420] = "i420",
215 [X264_CSP_I422] = "i422",
216 [X264_CSP_I444] = "i444"
219 static const char * const range_names[] = { "auto", "tv", "pc", 0 };
230 X264_PULLDOWN_22 = 1,
233 X264_PULLDOWN_DOUBLE,
234 X264_PULLDOWN_TRIPLE,
238 #define TB PIC_STRUCT_TOP_BOTTOM
239 #define BT PIC_STRUCT_BOTTOM_TOP
240 #define TBT PIC_STRUCT_TOP_BOTTOM_TOP
241 #define BTB PIC_STRUCT_BOTTOM_TOP_BOTTOM
243 static const cli_pulldown_t pulldown_values[] =
245 [X264_PULLDOWN_22] = {1, {TB}, 1.0},
246 [X264_PULLDOWN_32] = {4, {TBT, BT, BTB, TB}, 1.25},
247 [X264_PULLDOWN_64] = {2, {PIC_STRUCT_DOUBLE, PIC_STRUCT_TRIPLE}, 1.0},
248 [X264_PULLDOWN_DOUBLE] = {1, {PIC_STRUCT_DOUBLE}, 2.0},
249 [X264_PULLDOWN_TRIPLE] = {1, {PIC_STRUCT_TRIPLE}, 3.0},
250 [X264_PULLDOWN_EURO] = {24, {TBT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT,
251 BTB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB}, 25.0/24.0}
259 // indexed by pic_struct enum
260 static const float pulldown_frame_duration[10] = { 0.0, 1, 0.5, 0.5, 1, 1, 1.5, 1.5, 2, 3 };
262 static void help( x264_param_t *defaults, int longhelp );
263 static int parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
264 static int encode( x264_param_t *param, cli_opt_t *opt );
266 /* logging and printing for within the cli system */
267 static int cli_log_level;
268 void x264_cli_log( const char *name, int i_level, const char *fmt, ... )
270 if( i_level > cli_log_level )
278 case X264_LOG_WARNING:
291 fprintf( stderr, "%s [%s]: ", name, s_level );
293 va_start( arg, fmt );
294 x264_vfprintf( stderr, fmt, arg );
298 void x264_cli_printf( int i_level, const char *fmt, ... )
300 if( i_level > cli_log_level )
303 va_start( arg, fmt );
304 x264_vfprintf( stderr, fmt, arg );
308 static void print_version_info( void )
311 printf( "x264 "X264_POINTVER"\n" );
313 printf( "x264 0.%d.X\n", X264_BUILD );
316 printf( "(libswscale %d.%d.%d)\n", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO );
319 printf( "(libavformat %d.%d.%d)\n", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO );
322 printf( "(ffmpegsource %d.%d.%d.%d)\n", FFMS_VERSION >> 24, (FFMS_VERSION & 0xff0000) >> 16, (FFMS_VERSION & 0xff00) >> 8, FFMS_VERSION & 0xff );
324 printf( "built on " __DATE__ ", " );
325 #ifdef __INTEL_COMPILER
326 printf( "intel: %.2f (%d)\n", __INTEL_COMPILER / 100.f, __INTEL_COMPILER_BUILD_DATE );
327 #elif defined(__GNUC__)
328 printf( "gcc: " __VERSION__ "\n" );
329 #elif defined(_MSC_FULL_VER)
330 printf( "msvc: %.2f (%u)\n", _MSC_VER / 100.f, _MSC_FULL_VER );
332 printf( "using an unknown compiler\n" );
334 printf( "x264 configuration: --bit-depth=%d --chroma-format=%s\n", X264_BIT_DEPTH, chroma_format_names[X264_CHROMA_FORMAT] );
335 printf( "libx264 configuration: --bit-depth=%d --chroma-format=%s\n", x264_bit_depth, chroma_format_names[x264_chroma_format] );
336 printf( "x264 license: " );
338 printf( "GPL version 2 or later\n" );
340 printf( "Non-GPL commercial\n" );
343 const char *license = swscale_license();
344 printf( "libswscale%s%s license: %s\n", HAVE_LAVF ? "/libavformat" : "", HAVE_FFMS ? "/ffmpegsource" : "" , license );
345 if( !strcmp( license, "nonfree and unredistributable" ) ||
346 (!HAVE_GPL && (!strcmp( license, "GPL version 2 or later" )
347 || !strcmp( license, "GPL version 3 or later" ))))
348 printf( "WARNING: This binary is unredistributable!\n" );
352 int main( int argc, char **argv )
358 FAIL_IF_ERROR( x264_threading_init(), "unable to initialize threading\n" )
361 FAIL_IF_ERROR( !get_argv_utf8( &argc, &argv ), "unable to convert command line to UTF-8\n" )
363 GetConsoleTitleW( org_console_title, CONSOLE_TITLE_SIZE );
364 _setmode( _fileno( stdin ), _O_BINARY );
365 _setmode( _fileno( stdout ), _O_BINARY );
366 _setmode( _fileno( stderr ), _O_BINARY );
369 /* Parse command line */
370 if( parse( argc, argv, ¶m, &opt ) < 0 )
374 /* Restore title; it can be changed by input modules */
375 SetConsoleTitleW( org_console_title );
378 /* Control-C handler */
379 signal( SIGINT, sigint_handler );
382 ret = encode( ¶m, &opt );
384 /* clean up handles */
386 filter.free( opt.hin );
388 cli_input.close_file( opt.hin );
390 cli_output.close_file( opt.hout, 0, 0 );
392 fclose( opt.tcfile_out );
394 fclose( opt.qpfile );
397 SetConsoleTitleW( org_console_title );
404 static char const *strtable_lookup( const char * const table[], int idx )
406 int i = 0; while( table[i] ) i++;
407 return ( ( idx >= 0 && idx < i ) ? table[ idx ] : "???" );
410 static char *stringify_names( char *buf, const char * const names[] )
414 for( p[0] = 0; names[i]; i++ )
416 p += sprintf( p, "%s", names[i] );
418 p += sprintf( p, ", " );
423 static void print_csp_names( int longhelp )
428 printf( " - valid csps for `raw' demuxer:\n" );
430 for( int i = X264_CSP_NONE+1; i < X264_CSP_CLI_MAX; i++ )
432 if( x264_cli_csps[i].name )
434 printf( "%s", x264_cli_csps[i].name );
435 if( i+1 < X264_CSP_CLI_MAX )
441 printf( " - valid csps for `lavf' demuxer:\n" );
443 size_t line_len = strlen( INDENT );
444 for( enum AVPixelFormat i = AV_PIX_FMT_NONE+1; i < AV_PIX_FMT_NB; i++ )
446 const char *pfname = av_get_pix_fmt_name( i );
449 size_t name_len = strlen( pfname );
450 if( line_len + name_len > (80 - strlen( ", " )) )
452 printf( "\n" INDENT );
453 line_len = strlen( INDENT );
455 printf( "%s", pfname );
456 line_len += name_len;
457 if( i+1 < AV_PIX_FMT_NB )
468 static void help( x264_param_t *defaults, int longhelp )
472 #define H1 if(longhelp>=1) printf
473 #define H2 if(longhelp==2) printf
474 H0( "x264 core:%d%s\n"
475 "Syntax: x264 [options] -o outfile infile\n"
477 "Infile can be raw (in which case resolution is required),\n"
478 " or YUV4MPEG (*.y4m),\n"
479 " or Avisynth if compiled with support (%s).\n"
480 " or libav* formats if compiled with lavf support (%s) or ffms support (%s).\n"
481 "Outfile type is selected by filename:\n"
482 " .264 -> Raw bytestream\n"
483 " .mkv -> Matroska\n"
484 " .flv -> Flash Video\n"
485 " .mp4 -> MP4 if compiled with GPAC or L-SMASH support (%s)\n"
486 "Output bit depth: %d (configured at compile time)\n"
490 " -h, --help List basic options\n"
491 " --longhelp List more options\n"
492 " --fullhelp List all options\n"
494 X264_BUILD, X264_VERSION,
519 H0( "Example usage:\n" );
521 H0( " Constant quality mode:\n" );
522 H0( " x264 --crf 24 -o <output> <input>\n" );
524 H0( " Two-pass with a bitrate of 1000kbps:\n" );
525 H0( " x264 --pass 1 --bitrate 1000 -o <output> <input>\n" );
526 H0( " x264 --pass 2 --bitrate 1000 -o <output> <input>\n" );
528 H0( " Lossless:\n" );
529 H0( " x264 --qp 0 -o <output> <input>\n" );
531 H0( " Maximum PSNR at the cost of speed and visual quality:\n" );
532 H0( " x264 --preset placebo --tune psnr -o <output> <input>\n" );
534 H0( " Constant bitrate at 1000kbps with a 2 second-buffer:\n");
535 H0( " x264 --vbv-bufsize 2000 --bitrate 1000 -o <output> <input>\n" );
539 H0( " --profile <string> Force the limits of an H.264 profile\n"
540 " Overrides all settings.\n" );
542 #if X264_CHROMA_FORMAT <= X264_CSP_I420
543 #if X264_BIT_DEPTH==8
545 " --no-8x8dct --bframes 0 --no-cabac\n"
546 " --cqm flat --weightp 0\n"
550 " --no-8x8dct --cqm flat\n"
557 " Support for bit depth 8-10.\n"
559 #if X264_CHROMA_FORMAT <= X264_CSP_I422
562 " Support for bit depth 8-10.\n"
563 " Support for 4:2:0/4:2:2 chroma subsampling.\n"
566 " Support for bit depth 8-10.\n"
567 " Support for 4:2:0/4:2:2/4:4:4 chroma subsampling.\n" );
570 #if X264_CHROMA_FORMAT <= X264_CSP_I420
571 #if X264_BIT_DEPTH==8
572 "baseline,main,high,"
576 #if X264_CHROMA_FORMAT <= X264_CSP_I422
581 H0( " --preset <string> Use a preset to select encoding settings [medium]\n"
582 " Overridden by user settings.\n" );
583 H2( " - ultrafast:\n"
584 " --no-8x8dct --aq-mode 0 --b-adapt 0\n"
585 " --bframes 0 --no-cabac --no-deblock\n"
586 " --no-mbtree --me dia --no-mixed-refs\n"
587 " --partitions none --rc-lookahead 0 --ref 1\n"
588 " --scenecut 0 --subme 0 --trellis 0\n"
589 " --no-weightb --weightp 0\n"
591 " --no-mbtree --me dia --no-mixed-refs\n"
592 " --partitions i8x8,i4x4 --rc-lookahead 0\n"
593 " --ref 1 --subme 1 --trellis 0 --weightp 1\n"
595 " --no-mixed-refs --rc-lookahead 10\n"
596 " --ref 1 --subme 2 --trellis 0 --weightp 1\n"
598 " --no-mixed-refs --rc-lookahead 20\n"
599 " --ref 2 --subme 4 --weightp 1\n"
601 " --rc-lookahead 30 --ref 2 --subme 6\n"
604 " Default settings apply.\n"
606 " --b-adapt 2 --direct auto --me umh\n"
607 " --rc-lookahead 50 --ref 5 --subme 8\n"
609 " --b-adapt 2 --direct auto --me umh\n"
610 " --partitions all --rc-lookahead 60\n"
611 " --ref 8 --subme 9 --trellis 2\n"
613 " --b-adapt 2 --bframes 8 --direct auto\n"
614 " --me umh --merange 24 --partitions all\n"
615 " --ref 16 --subme 10 --trellis 2\n"
616 " --rc-lookahead 60\n"
618 " --bframes 16 --b-adapt 2 --direct auto\n"
619 " --slow-firstpass --no-fast-pskip\n"
620 " --me tesa --merange 24 --partitions all\n"
621 " --rc-lookahead 60 --ref 16 --subme 11\n"
623 else H0( " - ultrafast,superfast,veryfast,faster,fast\n"
624 " - medium,slow,slower,veryslow,placebo\n" );
625 H0( " --tune <string> Tune the settings for a particular type of source\n"
627 " Overridden by user settings.\n"
628 " Multiple tunings are separated by commas.\n"
629 " Only one psy tuning can be used at a time.\n" );
630 H2( " - film (psy tuning):\n"
631 " --deblock -1:-1 --psy-rd <unset>:0.15\n"
632 " - animation (psy tuning):\n"
633 " --bframes {+2} --deblock 1:1\n"
634 " --psy-rd 0.4:<unset> --aq-strength 0.6\n"
635 " --ref {Double if >1 else 1}\n"
636 " - grain (psy tuning):\n"
637 " --aq-strength 0.5 --no-dct-decimate\n"
638 " --deadzone-inter 6 --deadzone-intra 6\n"
639 " --deblock -2:-2 --ipratio 1.1 \n"
640 " --pbratio 1.1 --psy-rd <unset>:0.25\n"
642 " - stillimage (psy tuning):\n"
643 " --aq-strength 1.2 --deblock -3:-3\n"
644 " --psy-rd 2.0:0.7\n"
645 " - psnr (psy tuning):\n"
646 " --aq-mode 0 --no-psy\n"
647 " - ssim (psy tuning):\n"
648 " --aq-mode 2 --no-psy\n"
650 " --no-cabac --no-deblock --no-weightb\n"
653 " --bframes 0 --force-cfr --no-mbtree\n"
654 " --sync-lookahead 0 --sliced-threads\n"
655 " --rc-lookahead 0\n" );
656 else H0( " - psy tunings: film,animation,grain,\n"
657 " stillimage,psnr,ssim\n"
658 " - other tunings: fastdecode,zerolatency\n" );
659 H2( " --slow-firstpass Don't force these faster settings with --pass 1:\n"
660 " --no-8x8dct --me dia --partitions none\n"
661 " --ref 1 --subme {2 if >2 else unchanged}\n"
662 " --trellis 0 --fast-pskip\n" );
663 else H1( " --slow-firstpass Don't force faster settings with --pass 1\n" );
665 H0( "Frame-type options:\n" );
667 H0( " -I, --keyint <integer or \"infinite\"> Maximum GOP size [%d]\n", defaults->i_keyint_max );
668 H2( " -i, --min-keyint <integer> Minimum GOP size [auto]\n" );
669 H2( " --no-scenecut Disable adaptive I-frame decision\n" );
670 H2( " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
671 H2( " --intra-refresh Use Periodic Intra Refresh instead of IDR frames\n" );
672 H1( " -b, --bframes <integer> Number of B-frames between I and P [%d]\n", defaults->i_bframe );
673 H1( " --b-adapt <integer> Adaptive B-frame decision method [%d]\n"
674 " Higher values may lower threading efficiency.\n"
677 " - 2: Optimal (slow with high --bframes)\n", defaults->i_bframe_adaptive );
678 H2( " --b-bias <integer> Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
679 H1( " --b-pyramid <string> Keep some B-frames as references [%s]\n"
680 " - none: Disabled\n"
681 " - strict: Strictly hierarchical pyramid\n"
682 " - normal: Non-strict (not Blu-ray compatible)\n",
683 strtable_lookup( x264_b_pyramid_names, defaults->i_bframe_pyramid ) );
684 H1( " --open-gop Use recovery points to close GOPs\n"
685 " Only available with b-frames\n" );
686 H1( " --no-cabac Disable CABAC\n" );
687 H1( " -r, --ref <integer> Number of reference frames [%d]\n", defaults->i_frame_reference );
688 H1( " --no-deblock Disable loop filter\n" );
689 H1( " -f, --deblock <alpha:beta> Loop filter parameters [%d:%d]\n",
690 defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
691 H2( " --slices <integer> Number of slices per frame; forces rectangular\n"
692 " slices and is overridden by other slicing options\n" );
693 else H1( " --slices <integer> Number of slices per frame\n" );
694 H2( " --slices-max <integer> Absolute maximum slices per frame; overrides\n"
695 " slice-max-size/slice-max-mbs when necessary\n" );
696 H2( " --slice-max-size <integer> Limit the size of each slice in bytes\n");
697 H2( " --slice-max-mbs <integer> Limit the size of each slice in macroblocks (max)\n");
698 H2( " --slice-min-mbs <integer> Limit the size of each slice in macroblocks (min)\n");
699 H0( " --tff Enable interlaced mode (top field first)\n" );
700 H0( " --bff Enable interlaced mode (bottom field first)\n" );
701 H2( " --constrained-intra Enable constrained intra prediction.\n" );
702 H0( " --pulldown <string> Use soft pulldown to change frame rate\n"
703 " - none, 22, 32, 64, double, triple, euro (requires cfr input)\n" );
704 H2( " --fake-interlaced Flag stream as interlaced but encode progressive.\n"
705 " Makes it possible to encode 25p and 30p Blu-Ray\n"
706 " streams. Ignored in interlaced mode.\n" );
707 H2( " --frame-packing <integer> For stereoscopic videos define frame arrangement\n"
708 " - 0: checkerboard - pixels are alternatively from L and R\n"
709 " - 1: column alternation - L and R are interlaced by column\n"
710 " - 2: row alternation - L and R are interlaced by row\n"
711 " - 3: side by side - L is on the left, R on the right\n"
712 " - 4: top bottom - L is on top, R on bottom\n"
713 " - 5: frame alternation - one view per frame\n"
714 " - 6: mono - 2D frame without any frame packing\n"
715 " - 7: tile format - L is on top-left, R split across\n" );
717 H0( "Ratecontrol:\n" );
719 H1( " -q, --qp <integer> Force constant QP (0-%d, 0=lossless)\n", QP_MAX );
720 H0( " -B, --bitrate <integer> Set bitrate (kbit/s)\n" );
721 H0( " --crf <float> Quality-based VBR (%d-51) [%.1f]\n", 51 - QP_MAX_SPEC, defaults->rc.f_rf_constant );
722 H1( " --rc-lookahead <integer> Number of frames for frametype lookahead [%d]\n", defaults->rc.i_lookahead );
723 H0( " --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
724 H0( " --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
725 H2( " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
726 H2( " --crf-max <float> With CRF+VBV, limit RF to this value\n"
727 " May cause VBV underflows!\n" );
728 H2( " --qpmin <integer> Set min QP [%d]\n", defaults->rc.i_qp_min );
729 H2( " --qpmax <integer> Set max QP [%d]\n", defaults->rc.i_qp_max );
730 H2( " --qpstep <integer> Set max QP step [%d]\n", defaults->rc.i_qp_step );
731 H2( " --ratetol <float> Tolerance of ABR ratecontrol and VBV [%.1f]\n", defaults->rc.f_rate_tolerance );
732 H2( " --ipratio <float> QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
733 H2( " --pbratio <float> QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
734 H2( " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
735 H2( " --aq-mode <integer> AQ method [%d]\n"
737 " - 1: Variance AQ (complexity mask)\n"
738 " - 2: Auto-variance AQ\n"
739 " - 3: Auto-variance AQ with bias to dark scenes\n", defaults->rc.i_aq_mode );
740 H1( " --aq-strength <float> Reduces blocking and blurring in flat and\n"
741 " textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
743 H0( " -p, --pass <integer> Enable multipass ratecontrol\n"
744 " - 1: First pass, creates stats file\n"
745 " - 2: Last pass, does not overwrite stats file\n" );
746 H2( " - 3: Nth pass, overwrites stats file\n" );
747 H1( " --stats <string> Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
748 H2( " --no-mbtree Disable mb-tree ratecontrol.\n");
749 H2( " --qcomp <float> QP curve compression [%.2f]\n", defaults->rc.f_qcompress );
750 H2( " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
751 H2( " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
752 H2( " --zones <zone0>/<zone1>/... Tweak the bitrate of regions of the video\n" );
753 H2( " Each zone is of the form\n"
754 " <start frame>,<end frame>,<option>\n"
755 " where <option> is either\n"
756 " q=<integer> (force QP)\n"
757 " or b=<float> (bitrate multiplier)\n" );
758 H2( " --qpfile <string> Force frametypes and QPs for some or all frames\n"
759 " Format of each line: framenumber frametype QP\n"
760 " QP is optional (none lets x264 choose). Frametypes: I,i,K,P,B,b.\n"
761 " K=<I or i> depending on open-gop setting\n"
762 " QPs are restricted by qpmin/qpmax.\n" );
766 H1( " -A, --partitions <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
767 " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
769 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
770 H1( " --direct <string> Direct MV prediction mode [\"%s\"]\n"
771 " - none, spatial, temporal, auto\n",
772 strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
773 H2( " --no-weightb Disable weighted prediction for B-frames\n" );
774 H1( " --weightp <integer> Weighted prediction for P-frames [%d]\n"
776 " - 1: Weighted refs\n"
777 " - 2: Weighted refs + Duplicates\n", defaults->analyse.i_weighted_pred );
778 H1( " --me <string> Integer pixel motion estimation method [\"%s\"]\n",
779 strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
780 H2( " - dia: diamond search, radius 1 (fast)\n"
781 " - hex: hexagonal search, radius 2\n"
782 " - umh: uneven multi-hexagon search\n"
783 " - esa: exhaustive search\n"
784 " - tesa: hadamard exhaustive search (slow)\n" );
785 else H1( " - dia, hex, umh\n" );
786 H2( " --merange <integer> Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
787 H2( " --mvrange <integer> Maximum motion vector length [-1 (auto)]\n" );
788 H2( " --mvrange-thread <int> Minimum buffer between threads [-1 (auto)]\n" );
789 H1( " -m, --subme <integer> Subpixel motion estimation and mode decision [%d]\n", defaults->analyse.i_subpel_refine );
790 H2( " - 0: fullpel only (not recommended)\n"
791 " - 1: SAD mode decision, one qpel iteration\n"
792 " - 2: SATD mode decision\n"
793 " - 3-5: Progressively more qpel\n"
794 " - 6: RD mode decision for I/P-frames\n"
795 " - 7: RD mode decision for all frames\n"
796 " - 8: RD refinement for I/P-frames\n"
797 " - 9: RD refinement for all frames\n"
798 " - 10: QP-RD - requires trellis=2, aq-mode>0\n"
799 " - 11: Full RD: disable all early terminations\n" );
800 else H1( " decision quality: 1=fast, 11=best\n" );
801 H1( " --psy-rd <float:float> Strength of psychovisual optimization [\"%.1f:%.1f\"]\n"
802 " #1: RD (requires subme>=6)\n"
803 " #2: Trellis (requires trellis, experimental)\n",
804 defaults->analyse.f_psy_rd, defaults->analyse.f_psy_trellis );
805 H2( " --no-psy Disable all visual optimizations that worsen\n"
806 " both PSNR and SSIM.\n" );
807 H2( " --no-mixed-refs Don't decide references on a per partition basis\n" );
808 H2( " --no-chroma-me Ignore chroma in motion estimation\n" );
809 H1( " --no-8x8dct Disable adaptive spatial transform size\n" );
810 H1( " -t, --trellis <integer> Trellis RD quantization. [%d]\n"
812 " - 1: enabled only on the final encode of a MB\n"
813 " - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
814 H2( " --no-fast-pskip Disables early SKIP detection on P-frames\n" );
815 H2( " --no-dct-decimate Disables coefficient thresholding on P-frames\n" );
816 H1( " --nr <integer> Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
818 H2( " --deadzone-inter <int> Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
819 H2( " --deadzone-intra <int> Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
820 H2( " Deadzones should be in the range 0 - 32.\n" );
821 H2( " --cqm <string> Preset quant matrices [\"flat\"]\n"
823 H1( " --cqmfile <string> Read custom quant matrices from a JM-compatible file\n" );
824 H2( " Overrides any other --cqm* options.\n" );
825 H2( " --cqm4 <list> Set all 4x4 quant matrices\n"
826 " Takes a comma-separated list of 16 integers.\n" );
827 H2( " --cqm8 <list> Set all 8x8 quant matrices\n"
828 " Takes a comma-separated list of 64 integers.\n" );
829 H2( " --cqm4i, --cqm4p, --cqm8i, --cqm8p <list>\n"
830 " Set both luma and chroma quant matrices\n" );
831 H2( " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc <list>\n"
832 " Set individual quant matrices\n" );
834 H2( "Video Usability Info (Annex E):\n" );
835 H2( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
836 H2( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
838 H2( " --overscan <string> Specify crop overscan setting [\"%s\"]\n"
839 " - undef, show, crop\n",
840 strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ) );
841 H2( " --videoformat <string> Specify video format [\"%s\"]\n"
842 " - component, pal, ntsc, secam, mac, undef\n",
843 strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ) );
844 H2( " --range <string> Specify color range [\"%s\"]\n"
845 " - %s\n", range_names[0], stringify_names( buf, range_names ) );
846 H2( " --colorprim <string> Specify color primaries [\"%s\"]\n"
847 " - undef, bt709, bt470m, bt470bg, smpte170m,\n"
848 " smpte240m, film, bt2020\n",
849 strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ) );
850 H2( " --transfer <string> Specify transfer characteristics [\"%s\"]\n"
851 " - undef, bt709, bt470m, bt470bg, smpte170m,\n"
852 " smpte240m, linear, log100, log316,\n"
853 " iec61966-2-4, bt1361e, iec61966-2-1,\n"
854 " bt2020-10, bt2020-12\n",
855 strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ) );
856 H2( " --colormatrix <string> Specify color matrix setting [\"%s\"]\n"
857 " - undef, bt709, fcc, bt470bg, smpte170m,\n"
858 " smpte240m, GBR, YCgCo, bt2020nc, bt2020c\n",
859 strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
860 H2( " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n",
861 defaults->vui.i_chroma_loc );
863 H2( " --nal-hrd <string> Signal HRD information (requires vbv-bufsize)\n"
864 " - none, vbr, cbr (cbr not allowed in .mp4)\n" );
865 H2( " --filler Force hard-CBR and generate filler (implied by\n"
866 " --nal-hrd cbr)\n" );
867 H2( " --pic-struct Force pic_struct in Picture Timing SEI\n" );
868 H2( " --crop-rect <string> Add 'left,top,right,bottom' to the bitstream-level\n"
869 " cropping rectangle\n" );
872 H0( "Input/Output:\n" );
874 H0( " -o, --output <string> Specify output file\n" );
875 H1( " --muxer <string> Specify output container format [\"%s\"]\n"
876 " - %s\n", muxer_names[0], stringify_names( buf, muxer_names ) );
877 H1( " --demuxer <string> Specify input container format [\"%s\"]\n"
878 " - %s\n", demuxer_names[0], stringify_names( buf, demuxer_names ) );
879 H1( " --input-fmt <string> Specify input file format (requires lavf support)\n" );
880 H1( " --input-csp <string> Specify input colorspace format for raw input\n" );
881 print_csp_names( longhelp );
882 H1( " --output-csp <string> Specify output colorspace [\"%s\"]\n"
883 " - %s\n", output_csp_names[0], stringify_names( buf, output_csp_names ) );
884 H1( " --input-depth <integer> Specify input bit depth for raw input\n" );
885 H1( " --input-range <string> Specify input color range [\"%s\"]\n"
886 " - %s\n", range_names[0], stringify_names( buf, range_names ) );
887 H1( " --input-res <intxint> Specify input resolution (width x height)\n" );
888 H1( " --index <string> Filename for input index file\n" );
889 H0( " --sar width:height Specify Sample Aspect Ratio\n" );
890 H0( " --fps <float|rational> Specify framerate\n" );
891 H0( " --seek <integer> First frame to encode\n" );
892 H0( " --frames <integer> Maximum number of frames to encode\n" );
893 H0( " --level <string> Specify level (as defined by Annex A)\n" );
894 H1( " --bluray-compat Enable compatibility hacks for Blu-ray support\n" );
895 H1( " --avcintra-class <integer> Use compatibility hacks for AVC-Intra class\n"
896 " - 50, 100, 200\n" );
897 H1( " --stitchable Don't optimize headers based on video content\n"
898 " Ensures ability to recombine a segmented encode\n" );
900 H1( " -v, --verbose Print stats for each frame\n" );
901 H1( " --no-progress Don't show the progress indicator while encoding\n" );
902 H0( " --quiet Quiet Mode\n" );
903 H1( " --log-level <string> Specify the maximum level of logging [\"%s\"]\n"
904 " - %s\n", strtable_lookup( log_level_names, cli_log_level - X264_LOG_NONE ),
905 stringify_names( buf, log_level_names ) );
906 H1( " --psnr Enable PSNR computation\n" );
907 H1( " --ssim Enable SSIM computation\n" );
908 H1( " --threads <integer> Force a specific number of threads\n" );
909 H2( " --lookahead-threads <integer> Force a specific number of lookahead threads\n" );
910 H2( " --sliced-threads Low-latency but lower-efficiency threading\n" );
911 H2( " --thread-input Run Avisynth in its own thread\n" );
912 H2( " --sync-lookahead <integer> Number of buffer frames for threaded lookahead\n" );
913 H2( " --non-deterministic Slightly improve quality of SMP, at the cost of repeatability\n" );
914 H2( " --cpu-independent Ensure exact reproducibility across different cpus,\n"
915 " as opposed to letting them select different algorithms\n" );
916 H2( " --asm <integer> Override CPU detection\n" );
917 H2( " --no-asm Disable all CPU optimizations\n" );
918 H2( " --opencl Enable use of OpenCL\n" );
919 H2( " --opencl-clbin <string> Specify path of compiled OpenCL kernel cache\n" );
920 H2( " --opencl-device <integer> Specify OpenCL device ordinal\n" );
921 H2( " --dump-yuv <string> Save reconstructed frames\n" );
922 H2( " --sps-id <integer> Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
923 H2( " --aud Use access unit delimiters\n" );
924 H2( " --force-cfr Force constant framerate timestamp generation\n" );
925 H2( " --tcfile-in <string> Force timestamp generation with timecode file\n" );
926 H2( " --tcfile-out <string> Output timecode v2 file from input timestamps\n" );
927 H2( " --timebase <int/int> Specify timebase numerator and denominator\n"
928 " <integer> Specify timebase numerator for input timecode file\n"
929 " or specify timebase denominator for other input\n" );
930 H2( " --dts-compress Eliminate initial delay with container DTS hack\n" );
932 H0( "Filtering:\n" );
934 H0( " --vf, --video-filter <filter0>/<filter1>/... Apply video filtering to the input file\n" );
936 H0( " Filter options may be specified in <filter>:<option>=<value> format.\n" );
938 H0( " Available filters:\n" );
939 x264_register_vid_filters();
940 x264_vid_filter_help( longhelp );
979 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
980 static struct option long_options[] =
982 { "help", no_argument, NULL, 'h' },
983 { "longhelp", no_argument, NULL, OPT_LONGHELP },
984 { "fullhelp", no_argument, NULL, OPT_FULLHELP },
985 { "version", no_argument, NULL, 'V' },
986 { "profile", required_argument, NULL, OPT_PROFILE },
987 { "preset", required_argument, NULL, OPT_PRESET },
988 { "tune", required_argument, NULL, OPT_TUNE },
989 { "slow-firstpass", no_argument, NULL, OPT_SLOWFIRSTPASS },
990 { "bitrate", required_argument, NULL, 'B' },
991 { "bframes", required_argument, NULL, 'b' },
992 { "b-adapt", required_argument, NULL, 0 },
993 { "no-b-adapt", no_argument, NULL, 0 },
994 { "b-bias", required_argument, NULL, 0 },
995 { "b-pyramid", required_argument, NULL, 0 },
996 { "open-gop", no_argument, NULL, 0 },
997 { "bluray-compat", no_argument, NULL, 0 },
998 { "avcintra-class", required_argument, NULL, 0 },
999 { "min-keyint", required_argument, NULL, 'i' },
1000 { "keyint", required_argument, NULL, 'I' },
1001 { "intra-refresh", no_argument, NULL, 0 },
1002 { "scenecut", required_argument, NULL, 0 },
1003 { "no-scenecut", no_argument, NULL, 0 },
1004 { "nf", no_argument, NULL, 0 },
1005 { "no-deblock", no_argument, NULL, 0 },
1006 { "filter", required_argument, NULL, 0 },
1007 { "deblock", required_argument, NULL, 'f' },
1008 { "interlaced", no_argument, NULL, OPT_INTERLACED },
1009 { "tff", no_argument, NULL, OPT_INTERLACED },
1010 { "bff", no_argument, NULL, OPT_INTERLACED },
1011 { "no-interlaced", no_argument, NULL, OPT_INTERLACED },
1012 { "constrained-intra", no_argument, NULL, 0 },
1013 { "cabac", no_argument, NULL, 0 },
1014 { "no-cabac", no_argument, NULL, 0 },
1015 { "qp", required_argument, NULL, 'q' },
1016 { "qpmin", required_argument, NULL, 0 },
1017 { "qpmax", required_argument, NULL, 0 },
1018 { "qpstep", required_argument, NULL, 0 },
1019 { "crf", required_argument, NULL, 0 },
1020 { "rc-lookahead",required_argument, NULL, 0 },
1021 { "ref", required_argument, NULL, 'r' },
1022 { "asm", required_argument, NULL, 0 },
1023 { "no-asm", no_argument, NULL, 0 },
1024 { "opencl", no_argument, NULL, 1 },
1025 { "opencl-clbin",required_argument, NULL, 0 },
1026 { "opencl-device",required_argument, NULL, 0 },
1027 { "sar", required_argument, NULL, 0 },
1028 { "fps", required_argument, NULL, OPT_FPS },
1029 { "frames", required_argument, NULL, OPT_FRAMES },
1030 { "seek", required_argument, NULL, OPT_SEEK },
1031 { "output", required_argument, NULL, 'o' },
1032 { "muxer", required_argument, NULL, OPT_MUXER },
1033 { "demuxer", required_argument, NULL, OPT_DEMUXER },
1034 { "stdout", required_argument, NULL, OPT_MUXER },
1035 { "stdin", required_argument, NULL, OPT_DEMUXER },
1036 { "index", required_argument, NULL, OPT_INDEX },
1037 { "analyse", required_argument, NULL, 0 },
1038 { "partitions", required_argument, NULL, 'A' },
1039 { "direct", required_argument, NULL, 0 },
1040 { "weightb", no_argument, NULL, 'w' },
1041 { "no-weightb", no_argument, NULL, 0 },
1042 { "weightp", required_argument, NULL, 0 },
1043 { "me", required_argument, NULL, 0 },
1044 { "merange", required_argument, NULL, 0 },
1045 { "mvrange", required_argument, NULL, 0 },
1046 { "mvrange-thread", required_argument, NULL, 0 },
1047 { "subme", required_argument, NULL, 'm' },
1048 { "psy-rd", required_argument, NULL, 0 },
1049 { "no-psy", no_argument, NULL, 0 },
1050 { "psy", no_argument, NULL, 0 },
1051 { "mixed-refs", no_argument, NULL, 0 },
1052 { "no-mixed-refs", no_argument, NULL, 0 },
1053 { "no-chroma-me", no_argument, NULL, 0 },
1054 { "8x8dct", no_argument, NULL, '8' },
1055 { "no-8x8dct", no_argument, NULL, 0 },
1056 { "trellis", required_argument, NULL, 't' },
1057 { "fast-pskip", no_argument, NULL, 0 },
1058 { "no-fast-pskip", no_argument, NULL, 0 },
1059 { "no-dct-decimate", no_argument, NULL, 0 },
1060 { "aq-strength", required_argument, NULL, 0 },
1061 { "aq-mode", required_argument, NULL, 0 },
1062 { "deadzone-inter", required_argument, NULL, 0 },
1063 { "deadzone-intra", required_argument, NULL, 0 },
1064 { "level", required_argument, NULL, 0 },
1065 { "ratetol", required_argument, NULL, 0 },
1066 { "vbv-maxrate", required_argument, NULL, 0 },
1067 { "vbv-bufsize", required_argument, NULL, 0 },
1068 { "vbv-init", required_argument, NULL, 0 },
1069 { "crf-max", required_argument, NULL, 0 },
1070 { "ipratio", required_argument, NULL, 0 },
1071 { "pbratio", required_argument, NULL, 0 },
1072 { "chroma-qp-offset", required_argument, NULL, 0 },
1073 { "pass", required_argument, NULL, 'p' },
1074 { "stats", required_argument, NULL, 0 },
1075 { "qcomp", required_argument, NULL, 0 },
1076 { "mbtree", no_argument, NULL, 0 },
1077 { "no-mbtree", no_argument, NULL, 0 },
1078 { "qblur", required_argument, NULL, 0 },
1079 { "cplxblur", required_argument, NULL, 0 },
1080 { "zones", required_argument, NULL, 0 },
1081 { "qpfile", required_argument, NULL, OPT_QPFILE },
1082 { "threads", required_argument, NULL, 0 },
1083 { "lookahead-threads", required_argument, NULL, 0 },
1084 { "sliced-threads", no_argument, NULL, 0 },
1085 { "no-sliced-threads", no_argument, NULL, 0 },
1086 { "slice-max-size", required_argument, NULL, 0 },
1087 { "slice-max-mbs", required_argument, NULL, 0 },
1088 { "slice-min-mbs", required_argument, NULL, 0 },
1089 { "slices", required_argument, NULL, 0 },
1090 { "slices-max", required_argument, NULL, 0 },
1091 { "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
1092 { "sync-lookahead", required_argument, NULL, 0 },
1093 { "non-deterministic", no_argument, NULL, 0 },
1094 { "cpu-independent", no_argument, NULL, 0 },
1095 { "psnr", no_argument, NULL, 0 },
1096 { "ssim", no_argument, NULL, 0 },
1097 { "quiet", no_argument, NULL, OPT_QUIET },
1098 { "verbose", no_argument, NULL, 'v' },
1099 { "log-level", required_argument, NULL, OPT_LOG_LEVEL },
1100 { "no-progress", no_argument, NULL, OPT_NOPROGRESS },
1101 { "dump-yuv", required_argument, NULL, 0 },
1102 { "sps-id", required_argument, NULL, 0 },
1103 { "aud", no_argument, NULL, 0 },
1104 { "nr", required_argument, NULL, 0 },
1105 { "cqm", required_argument, NULL, 0 },
1106 { "cqmfile", required_argument, NULL, 0 },
1107 { "cqm4", required_argument, NULL, 0 },
1108 { "cqm4i", required_argument, NULL, 0 },
1109 { "cqm4iy", required_argument, NULL, 0 },
1110 { "cqm4ic", required_argument, NULL, 0 },
1111 { "cqm4p", required_argument, NULL, 0 },
1112 { "cqm4py", required_argument, NULL, 0 },
1113 { "cqm4pc", required_argument, NULL, 0 },
1114 { "cqm8", required_argument, NULL, 0 },
1115 { "cqm8i", required_argument, NULL, 0 },
1116 { "cqm8p", required_argument, NULL, 0 },
1117 { "overscan", required_argument, NULL, 0 },
1118 { "videoformat", required_argument, NULL, 0 },
1119 { "range", required_argument, NULL, OPT_RANGE },
1120 { "colorprim", required_argument, NULL, 0 },
1121 { "transfer", required_argument, NULL, 0 },
1122 { "colormatrix", required_argument, NULL, 0 },
1123 { "chromaloc", required_argument, NULL, 0 },
1124 { "force-cfr", no_argument, NULL, 0 },
1125 { "tcfile-in", required_argument, NULL, OPT_TCFILE_IN },
1126 { "tcfile-out", required_argument, NULL, OPT_TCFILE_OUT },
1127 { "timebase", required_argument, NULL, OPT_TIMEBASE },
1128 { "pic-struct", no_argument, NULL, 0 },
1129 { "crop-rect", required_argument, NULL, 0 },
1130 { "nal-hrd", required_argument, NULL, 0 },
1131 { "pulldown", required_argument, NULL, OPT_PULLDOWN },
1132 { "fake-interlaced", no_argument, NULL, 0 },
1133 { "frame-packing", required_argument, NULL, 0 },
1134 { "vf", required_argument, NULL, OPT_VIDEO_FILTER },
1135 { "video-filter", required_argument, NULL, OPT_VIDEO_FILTER },
1136 { "input-fmt", required_argument, NULL, OPT_INPUT_FMT },
1137 { "input-res", required_argument, NULL, OPT_INPUT_RES },
1138 { "input-csp", required_argument, NULL, OPT_INPUT_CSP },
1139 { "input-depth", required_argument, NULL, OPT_INPUT_DEPTH },
1140 { "dts-compress", no_argument, NULL, OPT_DTS_COMPRESSION },
1141 { "output-csp", required_argument, NULL, OPT_OUTPUT_CSP },
1142 { "input-range", required_argument, NULL, OPT_INPUT_RANGE },
1143 { "stitchable", no_argument, NULL, 0 },
1144 { "filler", no_argument, NULL, 0 },
1148 static int select_output( const char *muxer, char *filename, x264_param_t *param )
1150 const char *ext = get_filename_extension( filename );
1151 if( !strcmp( filename, "-" ) || strcasecmp( muxer, "auto" ) )
1154 if( !strcasecmp( ext, "mp4" ) )
1156 #if HAVE_GPAC || HAVE_LSMASH
1157 cli_output = mp4_output;
1158 param->b_annexb = 0;
1159 param->b_repeat_headers = 0;
1160 if( param->i_nal_hrd == X264_NAL_HRD_CBR )
1162 x264_cli_log( "x264", X264_LOG_WARNING, "cbr nal-hrd is not compatible with mp4\n" );
1163 param->i_nal_hrd = X264_NAL_HRD_VBR;
1166 x264_cli_log( "x264", X264_LOG_ERROR, "not compiled with MP4 output support\n" );
1170 else if( !strcasecmp( ext, "mkv" ) )
1172 cli_output = mkv_output;
1173 param->b_annexb = 0;
1174 param->b_repeat_headers = 0;
1176 else if( !strcasecmp( ext, "flv" ) )
1178 cli_output = flv_output;
1179 param->b_annexb = 0;
1180 param->b_repeat_headers = 0;
1183 cli_output = raw_output;
1187 static int select_input( const char *demuxer, char *used_demuxer, char *filename,
1188 hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
1190 int b_auto = !strcasecmp( demuxer, "auto" );
1191 const char *ext = b_auto ? get_filename_extension( filename ) : "";
1192 int b_regular = strcmp( filename, "-" );
1193 if( !b_regular && b_auto )
1195 b_regular = b_regular && x264_is_regular_file_path( filename );
1198 FILE *f = x264_fopen( filename, "r" );
1201 b_regular = x264_is_regular_file( f );
1205 const char *module = b_auto ? ext : demuxer;
1207 if( !strcasecmp( module, "avs" ) || !strcasecmp( ext, "d2v" ) || !strcasecmp( ext, "dga" ) )
1210 cli_input = avs_input;
1213 x264_cli_log( "x264", X264_LOG_ERROR, "not compiled with AVS input support\n" );
1217 else if( !strcasecmp( module, "y4m" ) )
1218 cli_input = y4m_input;
1219 else if( !strcasecmp( module, "raw" ) || !strcasecmp( ext, "yuv" ) )
1220 cli_input = raw_input;
1224 if( b_regular && (b_auto || !strcasecmp( demuxer, "ffms" )) &&
1225 !ffms_input.open_file( filename, p_handle, info, opt ) )
1229 cli_input = ffms_input;
1233 if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
1234 !lavf_input.open_file( filename, p_handle, info, opt ) )
1238 cli_input = lavf_input;
1242 if( b_regular && (b_auto || !strcasecmp( demuxer, "avs" )) &&
1243 !avs_input.open_file( filename, p_handle, info, opt ) )
1247 cli_input = avs_input;
1250 if( b_auto && !raw_input.open_file( filename, p_handle, info, opt ) )
1254 cli_input = raw_input;
1257 FAIL_IF_ERROR( !(*p_handle), "could not open input file `%s' via any method!\n", filename )
1259 strcpy( used_demuxer, module );
1264 static int init_vid_filters( char *sequence, hnd_t *handle, video_info_t *info, x264_param_t *param, int output_csp )
1266 x264_register_vid_filters();
1268 /* intialize baseline filters */
1269 if( x264_init_vid_filter( "source", handle, &filter, info, param, NULL ) ) /* wrap demuxer into a filter */
1271 if( x264_init_vid_filter( "resize", handle, &filter, info, param, "normcsp" ) ) /* normalize csps to be of a known/supported format */
1273 if( x264_init_vid_filter( "fix_vfr_pts", handle, &filter, info, param, NULL ) ) /* fix vfr pts */
1276 /* parse filter chain */
1277 for( char *p = sequence; p && *p; )
1279 int tok_len = strcspn( p, "/" );
1280 int p_len = strlen( p );
1282 int name_len = strcspn( p, ":" );
1284 name_len += name_len != tok_len;
1285 if( x264_init_vid_filter( p, handle, &filter, info, param, p + name_len ) )
1287 p += X264_MIN( tok_len+1, p_len );
1290 /* force end result resolution */
1291 if( !param->i_width && !param->i_height )
1293 param->i_height = info->height;
1294 param->i_width = info->width;
1296 /* force the output csp to what the user specified (or the default) */
1297 param->i_csp = info->csp;
1298 int csp = info->csp & X264_CSP_MASK;
1299 if( output_csp == X264_CSP_I420 && (csp < X264_CSP_I420 || csp >= X264_CSP_I422) )
1300 param->i_csp = X264_CSP_I420;
1301 else if( output_csp == X264_CSP_I422 && (csp < X264_CSP_I422 || csp >= X264_CSP_I444) )
1302 param->i_csp = X264_CSP_I422;
1303 else if( output_csp == X264_CSP_I444 && (csp < X264_CSP_I444 || csp >= X264_CSP_BGR) )
1304 param->i_csp = X264_CSP_I444;
1305 else if( output_csp == X264_CSP_RGB && (csp < X264_CSP_BGR || csp > X264_CSP_RGB) )
1306 param->i_csp = X264_CSP_RGB;
1307 param->i_csp |= info->csp & X264_CSP_HIGH_DEPTH;
1308 /* if the output range is not forced, assign it to the input one now */
1309 if( param->vui.b_fullrange == RANGE_AUTO )
1310 param->vui.b_fullrange = info->fullrange;
1312 if( x264_init_vid_filter( "resize", handle, &filter, info, param, NULL ) )
1316 sprintf( args, "bit_depth=%d", x264_bit_depth );
1318 if( x264_init_vid_filter( "depth", handle, &filter, info, param, args ) )
1324 static int parse_enum_name( const char *arg, const char * const *names, const char **dst )
1326 for( int i = 0; names[i]; i++ )
1327 if( !strcasecmp( arg, names[i] ) )
1335 static int parse_enum_value( const char *arg, const char * const *names, int *dst )
1337 for( int i = 0; names[i]; i++ )
1338 if( !strcasecmp( arg, names[i] ) )
1346 static int parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
1348 char *input_filename = NULL;
1349 const char *demuxer = demuxer_names[0];
1350 char *output_filename = NULL;
1351 const char *muxer = muxer_names[0];
1352 char *tcfile_name = NULL;
1353 x264_param_t defaults;
1354 char *profile = NULL;
1355 char *vid_filters = NULL;
1356 int b_thread_input = 0;
1360 int b_user_interlaced = 0;
1361 cli_input_opt_t input_opt;
1362 cli_output_opt_t output_opt;
1363 char *preset = NULL;
1366 x264_param_default( &defaults );
1367 cli_log_level = defaults.i_log_level;
1369 memset( &input_opt, 0, sizeof(cli_input_opt_t) );
1370 memset( &output_opt, 0, sizeof(cli_output_opt_t) );
1371 input_opt.bit_depth = 8;
1372 input_opt.input_range = input_opt.output_range = param->vui.b_fullrange = RANGE_AUTO;
1373 int output_csp = defaults.i_csp;
1374 opt->b_progress = 1;
1376 /* Presets are applied before all other options. */
1379 int c = getopt_long( argc, argv, short_options, long_options, NULL );
1382 if( c == OPT_PRESET )
1390 if( preset && !strcasecmp( preset, "placebo" ) )
1393 if( x264_param_default_preset( param, preset, tune ) < 0 )
1396 /* Parse command line options */
1400 int long_options_index = -1;
1402 int c = getopt_long( argc, argv, short_options, long_options, &long_options_index );
1412 help( &defaults, 0 );
1415 help( &defaults, 1 );
1418 help( &defaults, 2 );
1421 print_version_info();
1424 param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
1427 opt->i_seek = X264_MAX( atoi( optarg ), 0 );
1430 output_filename = optarg;
1433 FAIL_IF_ERROR( parse_enum_name( optarg, muxer_names, &muxer ), "Unknown muxer `%s'\n", optarg )
1436 FAIL_IF_ERROR( parse_enum_name( optarg, demuxer_names, &demuxer ), "Unknown demuxer `%s'\n", optarg )
1439 input_opt.index_file = optarg;
1442 opt->qpfile = x264_fopen( optarg, "rb" );
1443 FAIL_IF_ERROR( !opt->qpfile, "can't open qpfile `%s'\n", optarg )
1444 if( !x264_is_regular_file( opt->qpfile ) )
1446 x264_cli_log( "x264", X264_LOG_ERROR, "qpfile incompatible with non-regular file `%s'\n", optarg );
1447 fclose( opt->qpfile );
1451 case OPT_THREAD_INPUT:
1455 cli_log_level = param->i_log_level = X264_LOG_NONE;
1458 cli_log_level = param->i_log_level = X264_LOG_DEBUG;
1461 if( !parse_enum_value( optarg, log_level_names, &cli_log_level ) )
1462 cli_log_level += X264_LOG_NONE;
1464 cli_log_level = atoi( optarg );
1465 param->i_log_level = cli_log_level;
1467 case OPT_NOPROGRESS:
1468 opt->b_progress = 0;
1476 case OPT_SLOWFIRSTPASS:
1481 goto generic_option;
1484 param->b_vfr_input = 0;
1485 goto generic_option;
1486 case OPT_INTERLACED:
1487 b_user_interlaced = 1;
1488 goto generic_option;
1490 tcfile_name = optarg;
1492 case OPT_TCFILE_OUT:
1493 opt->tcfile_out = x264_fopen( optarg, "wb" );
1494 FAIL_IF_ERROR( !opt->tcfile_out, "can't open `%s'\n", optarg )
1497 input_opt.timebase = optarg;
1500 FAIL_IF_ERROR( parse_enum_value( optarg, pulldown_names, &opt->i_pulldown ), "Unknown pulldown `%s'\n", optarg )
1502 case OPT_VIDEO_FILTER:
1503 vid_filters = optarg;
1506 input_opt.format = optarg;
1509 input_opt.resolution = optarg;
1512 input_opt.colorspace = optarg;
1514 case OPT_INPUT_DEPTH:
1515 input_opt.bit_depth = atoi( optarg );
1517 case OPT_DTS_COMPRESSION:
1518 output_opt.use_dts_compress = 1;
1520 case OPT_OUTPUT_CSP:
1521 FAIL_IF_ERROR( parse_enum_value( optarg, output_csp_names, &output_csp ), "Unknown output csp `%s'\n", optarg )
1522 // correct the parsed value to the libx264 csp value
1523 #if X264_CHROMA_FORMAT
1524 static const uint8_t output_csp_fix[] = { X264_CHROMA_FORMAT, X264_CSP_RGB };
1526 static const uint8_t output_csp_fix[] = { X264_CSP_I420, X264_CSP_I422, X264_CSP_I444, X264_CSP_RGB };
1528 param->i_csp = output_csp = output_csp_fix[output_csp];
1530 case OPT_INPUT_RANGE:
1531 FAIL_IF_ERROR( parse_enum_value( optarg, range_names, &input_opt.input_range ), "Unknown input range `%s'\n", optarg )
1532 input_opt.input_range += RANGE_AUTO;
1535 FAIL_IF_ERROR( parse_enum_value( optarg, range_names, ¶m->vui.b_fullrange ), "Unknown range `%s'\n", optarg );
1536 input_opt.output_range = param->vui.b_fullrange += RANGE_AUTO;
1541 if( long_options_index < 0 )
1543 for( int i = 0; long_options[i].name; i++ )
1544 if( long_options[i].val == c )
1546 long_options_index = i;
1549 if( long_options_index < 0 )
1551 /* getopt_long already printed an error message */
1556 b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg );
1562 const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
1563 x264_cli_log( "x264", X264_LOG_ERROR, "invalid argument: %s = %s\n", name, optarg );
1568 /* If first pass mode is used, apply faster settings. */
1570 x264_param_apply_fastfirstpass( param );
1572 /* Apply profile restrictions. */
1573 if( x264_param_apply_profile( param, profile ) < 0 )
1576 /* Get the file name */
1577 FAIL_IF_ERROR( optind > argc - 1 || !output_filename, "No %s file. Run x264 --help for a list of options.\n",
1578 optind > argc - 1 ? "input" : "output" )
1580 if( select_output( muxer, output_filename, param ) )
1582 FAIL_IF_ERROR( cli_output.open_file( output_filename, &opt->hout, &output_opt ), "could not open output file `%s'\n", output_filename )
1584 input_filename = argv[optind++];
1585 video_info_t info = {0};
1586 char demuxername[5];
1588 /* set info flags to be overwritten by demuxer as necessary. */
1589 info.csp = param->i_csp;
1590 info.fps_num = param->i_fps_num;
1591 info.fps_den = param->i_fps_den;
1592 info.fullrange = input_opt.input_range == RANGE_PC;
1593 info.interlaced = param->b_interlaced;
1594 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
1596 info.sar_width = param->vui.i_sar_width;
1597 info.sar_height = param->vui.i_sar_height;
1599 info.tff = param->b_tff;
1600 info.vfr = param->b_vfr_input;
1602 input_opt.seek = opt->i_seek;
1603 input_opt.progress = opt->b_progress;
1604 input_opt.output_csp = output_csp;
1606 if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
1609 FAIL_IF_ERROR( !opt->hin && cli_input.open_file( input_filename, &opt->hin, &info, &input_opt ),
1610 "could not open input file `%s'\n", input_filename )
1612 x264_reduce_fraction( &info.sar_width, &info.sar_height );
1613 x264_reduce_fraction( &info.fps_num, &info.fps_den );
1614 x264_cli_log( demuxername, X264_LOG_INFO, "%dx%d%c %u:%u @ %u/%u fps (%cfr)\n", info.width,
1615 info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
1616 info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
1620 FAIL_IF_ERROR( b_user_fps, "--fps + --tcfile-in is incompatible.\n" )
1621 FAIL_IF_ERROR( timecode_input.open_file( tcfile_name, &opt->hin, &info, &input_opt ), "timecode input failed\n" )
1622 cli_input = timecode_input;
1624 else FAIL_IF_ERROR( !info.vfr && input_opt.timebase, "--timebase is incompatible with cfr input\n" )
1626 /* init threaded input while the information about the input video is unaltered by filtering */
1628 if( info.thread_safe && (b_thread_input || param->i_threads > 1
1629 || (param->i_threads == X264_THREADS_AUTO && x264_cpu_num_processors() > 1)) )
1631 if( thread_input.open_file( NULL, &opt->hin, &info, NULL ) )
1633 fprintf( stderr, "x264 [error]: threaded input failed\n" );
1636 cli_input = thread_input;
1640 /* override detected values by those specified by the user */
1641 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
1643 info.sar_width = param->vui.i_sar_width;
1644 info.sar_height = param->vui.i_sar_height;
1648 info.fps_num = param->i_fps_num;
1649 info.fps_den = param->i_fps_den;
1653 info.timebase_num = info.fps_den;
1654 info.timebase_den = info.fps_num;
1656 if( !tcfile_name && input_opt.timebase )
1658 uint64_t i_user_timebase_num;
1659 uint64_t i_user_timebase_den;
1660 int ret = sscanf( input_opt.timebase, "%"SCNu64"/%"SCNu64, &i_user_timebase_num, &i_user_timebase_den );
1661 FAIL_IF_ERROR( !ret, "invalid argument: timebase = %s\n", input_opt.timebase )
1664 i_user_timebase_num = info.timebase_num;
1665 i_user_timebase_den = strtoul( input_opt.timebase, NULL, 10 );
1667 FAIL_IF_ERROR( i_user_timebase_num > UINT32_MAX || i_user_timebase_den > UINT32_MAX,
1668 "timebase you specified exceeds H.264 maximum\n" )
1669 opt->timebase_convert_multiplier = ((double)i_user_timebase_den / info.timebase_den)
1670 * ((double)info.timebase_num / i_user_timebase_num);
1671 info.timebase_num = i_user_timebase_num;
1672 info.timebase_den = i_user_timebase_den;
1675 if( b_user_interlaced )
1677 info.interlaced = param->b_interlaced;
1678 info.tff = param->b_tff;
1680 if( input_opt.input_range != RANGE_AUTO )
1681 info.fullrange = input_opt.input_range;
1683 if( init_vid_filters( vid_filters, &opt->hin, &info, param, output_csp ) )
1686 /* set param flags from the post-filtered video */
1687 param->b_vfr_input = info.vfr;
1688 param->i_fps_num = info.fps_num;
1689 param->i_fps_den = info.fps_den;
1690 param->i_timebase_num = info.timebase_num;
1691 param->i_timebase_den = info.timebase_den;
1692 param->vui.i_sar_width = info.sar_width;
1693 param->vui.i_sar_height = info.sar_height;
1695 info.num_frames = X264_MAX( info.num_frames - opt->i_seek, 0 );
1696 if( (!info.num_frames || param->i_frame_total < info.num_frames)
1697 && param->i_frame_total > 0 )
1698 info.num_frames = param->i_frame_total;
1699 param->i_frame_total = info.num_frames;
1701 if( !b_user_interlaced && info.interlaced )
1704 x264_cli_log( "x264", X264_LOG_WARNING, "input appears to be interlaced, enabling %cff interlaced mode.\n"
1705 " If you want otherwise, use --no-interlaced or --%cff\n",
1706 info.tff ? 't' : 'b', info.tff ? 'b' : 't' );
1707 param->b_interlaced = 1;
1708 param->b_tff = !!info.tff;
1710 x264_cli_log( "x264", X264_LOG_WARNING, "input appears to be interlaced, but not compiled with interlaced support\n" );
1713 /* if the user never specified the output range and the input is now rgb, default it to pc */
1714 int csp = param->i_csp & X264_CSP_MASK;
1715 if( csp >= X264_CSP_BGR && csp <= X264_CSP_RGB )
1717 if( input_opt.output_range == RANGE_AUTO )
1718 param->vui.b_fullrange = RANGE_PC;
1719 /* otherwise fail if they specified tv */
1720 FAIL_IF_ERROR( !param->vui.b_fullrange, "RGB must be PC range" )
1723 /* Automatically reduce reference frame count to match the user's target level
1724 * if the user didn't explicitly set a reference frame count. */
1727 int mbs = (((param->i_width)+15)>>4) * (((param->i_height)+15)>>4);
1728 for( int i = 0; x264_levels[i].level_idc != 0; i++ )
1729 if( param->i_level_idc == x264_levels[i].level_idc )
1731 while( mbs * param->i_frame_reference > x264_levels[i].dpb && param->i_frame_reference > 1 )
1732 param->i_frame_reference--;
1741 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
1745 while( num < i_frame )
1747 int64_t file_pos = ftell( opt->qpfile );
1749 int ret = fscanf( opt->qpfile, "%d %c%*[ \t]%d\n", &num, &type, &qp );
1750 pic->i_type = X264_TYPE_AUTO;
1751 pic->i_qpplus1 = X264_QP_AUTO;
1752 if( num > i_frame || ret == EOF )
1754 if( file_pos < 0 || fseek( opt->qpfile, file_pos, SEEK_SET ) )
1756 x264_cli_log( "x264", X264_LOG_ERROR, "qpfile seeking failed\n" );
1757 fclose( opt->qpfile );
1762 if( num < i_frame && ret >= 2 )
1764 if( ret == 3 && qp >= 0 )
1765 pic->i_qpplus1 = qp+1;
1766 if ( type == 'I' ) pic->i_type = X264_TYPE_IDR;
1767 else if( type == 'i' ) pic->i_type = X264_TYPE_I;
1768 else if( type == 'K' ) pic->i_type = X264_TYPE_KEYFRAME;
1769 else if( type == 'P' ) pic->i_type = X264_TYPE_P;
1770 else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
1771 else if( type == 'b' ) pic->i_type = X264_TYPE_B;
1773 if( ret < 2 || qp < -1 || qp > QP_MAX )
1775 x264_cli_log( "x264", X264_LOG_ERROR, "can't parse qpfile for frame %d\n", i_frame );
1776 fclose( opt->qpfile );
1783 static int encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_dts )
1785 x264_picture_t pic_out;
1788 int i_frame_size = 0;
1790 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
1792 FAIL_IF_ERROR( i_frame_size < 0, "x264_encoder_encode failed\n" );
1796 i_frame_size = cli_output.write_frame( hout, nal[0].p_payload, i_frame_size, &pic_out );
1797 *last_dts = pic_out.i_dts;
1800 return i_frame_size;
1803 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 )
1806 int64_t i_time = x264_mdate();
1807 if( i_previous && i_time - i_previous < UPDATE_INTERVAL )
1809 int64_t i_elapsed = i_time - i_start;
1810 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1813 bitrate = (double) i_file * 8 / ( (double) last_ts * 1000 * param->i_timebase_num / param->i_timebase_den );
1815 bitrate = (double) i_file * 8 / ( (double) 1000 * param->i_fps_den / param->i_fps_num );
1818 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1819 sprintf( buf, "x264 [%.1f%%] %d/%d frames, %.2f fps, %.2f kb/s, eta %d:%02d:%02d",
1820 100. * i_frame / i_frame_total, i_frame, i_frame_total, fps, bitrate,
1821 eta/3600, (eta/60)%60, eta%60 );
1824 sprintf( buf, "x264 %d frames: %.2f fps, %.2f kb/s", i_frame, fps, bitrate );
1825 fprintf( stderr, "%s \r", buf+5 );
1826 x264_cli_set_console_title( buf );
1827 fflush( stderr ); // needed in windows
1831 static void convert_cli_to_lib_pic( x264_picture_t *lib, cli_pic_t *cli )
1833 memcpy( lib->img.i_stride, cli->img.stride, sizeof(cli->img.stride) );
1834 memcpy( lib->img.plane, cli->img.plane, sizeof(cli->img.plane) );
1835 lib->img.i_plane = cli->img.planes;
1836 lib->img.i_csp = cli->img.csp;
1837 lib->i_pts = cli->pts;
1840 #define FAIL_IF_ERROR2( cond, ... )\
1843 x264_cli_log( "x264", X264_LOG_ERROR, __VA_ARGS__ );\
1848 static int encode( x264_param_t *param, cli_opt_t *opt )
1853 const cli_pulldown_t *pulldown = NULL; // shut up gcc
1856 int i_frame_output = 0;
1857 int64_t i_end, i_previous = 0, i_start = 0;
1860 int64_t last_dts = 0;
1861 int64_t prev_dts = 0;
1862 int64_t first_dts = 0;
1863 # define MAX_PTS_WARNING 3 /* arbitrary */
1864 int pts_warning_cnt = 0;
1865 int64_t largest_pts = -1;
1866 int64_t second_largest_pts = -1;
1867 int64_t ticks_per_frame;
1869 double pulldown_pts = 0;
1872 opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
1874 /* set up pulldown */
1875 if( opt->i_pulldown && !param->b_vfr_input )
1877 param->b_pulldown = 1;
1878 param->b_pic_struct = 1;
1879 pulldown = &pulldown_values[opt->i_pulldown];
1880 param->i_timebase_num = param->i_fps_den;
1881 FAIL_IF_ERROR2( fmod( param->i_fps_num * pulldown->fps_factor, 1 ),
1882 "unsupported framerate for chosen pulldown\n" )
1883 param->i_timebase_den = param->i_fps_num * pulldown->fps_factor;
1886 h = x264_encoder_open( param );
1887 FAIL_IF_ERROR2( !h, "x264_encoder_open failed\n" );
1889 x264_encoder_parameters( h, param );
1891 FAIL_IF_ERROR2( cli_output.set_param( opt->hout, param ), "can't set outfile param\n" );
1893 i_start = x264_mdate();
1895 /* ticks/frame = ticks/second / frames/second */
1896 ticks_per_frame = (int64_t)param->i_timebase_den * param->i_fps_den / param->i_timebase_num / param->i_fps_num;
1897 FAIL_IF_ERROR2( ticks_per_frame < 1 && !param->b_vfr_input, "ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame )
1898 ticks_per_frame = X264_MAX( ticks_per_frame, 1 );
1900 if( !param->b_repeat_headers )
1902 // Write SPS/PPS/SEI
1903 x264_nal_t *headers;
1906 FAIL_IF_ERROR2( x264_encoder_headers( h, &headers, &i_nal ) < 0, "x264_encoder_headers failed\n" )
1907 FAIL_IF_ERROR2( (i_file = cli_output.write_headers( opt->hout, headers )) < 0, "error writing headers to output file\n" );
1910 if( opt->tcfile_out )
1911 fprintf( opt->tcfile_out, "# timecode format v2\n" );
1914 for( ; !b_ctrl_c && (i_frame < param->i_frame_total || !param->i_frame_total); i_frame++ )
1916 if( filter.get_frame( opt->hin, &cli_pic, i_frame + opt->i_seek ) )
1918 x264_picture_init( &pic );
1919 convert_cli_to_lib_pic( &pic, &cli_pic );
1921 if( !param->b_vfr_input )
1922 pic.i_pts = i_frame;
1924 if( opt->i_pulldown && !param->b_vfr_input )
1926 pic.i_pic_struct = pulldown->pattern[ i_frame % pulldown->mod ];
1927 pic.i_pts = (int64_t)( pulldown_pts + 0.5 );
1928 pulldown_pts += pulldown_frame_duration[pic.i_pic_struct];
1930 else if( opt->timebase_convert_multiplier )
1931 pic.i_pts = (int64_t)( pic.i_pts * opt->timebase_convert_multiplier + 0.5 );
1933 if( pic.i_pts <= largest_pts )
1935 if( cli_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
1936 x264_cli_log( "x264", X264_LOG_WARNING, "non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
1937 i_frame, pic.i_pts, largest_pts );
1938 else if( pts_warning_cnt == MAX_PTS_WARNING )
1939 x264_cli_log( "x264", X264_LOG_WARNING, "too many nonmonotonic pts warnings, suppressing further ones\n" );
1941 pic.i_pts = largest_pts + ticks_per_frame;
1944 second_largest_pts = largest_pts;
1945 largest_pts = pic.i_pts;
1946 if( opt->tcfile_out )
1947 fprintf( opt->tcfile_out, "%.6f\n", pic.i_pts * ((double)param->i_timebase_num / param->i_timebase_den) * 1e3 );
1950 parse_qpfile( opt, &pic, i_frame + opt->i_seek );
1952 prev_dts = last_dts;
1953 i_frame_size = encode_frame( h, opt->hout, &pic, &last_dts );
1954 if( i_frame_size < 0 )
1956 b_ctrl_c = 1; /* lie to exit the loop */
1959 else if( i_frame_size )
1961 i_file += i_frame_size;
1963 if( i_frame_output == 1 )
1964 first_dts = prev_dts = last_dts;
1967 if( filter.release_frame( opt->hin, &cli_pic, i_frame + opt->i_seek ) )
1970 /* update status line (up to 1000 times per input file) */
1971 if( opt->b_progress && i_frame_output )
1972 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 );
1974 /* Flush delayed frames */
1975 while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
1977 prev_dts = last_dts;
1978 i_frame_size = encode_frame( h, opt->hout, NULL, &last_dts );
1979 if( i_frame_size < 0 )
1981 b_ctrl_c = 1; /* lie to exit the loop */
1984 else if( i_frame_size )
1986 i_file += i_frame_size;
1988 if( i_frame_output == 1 )
1989 first_dts = prev_dts = last_dts;
1991 if( opt->b_progress && i_frame_output )
1992 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 );
1995 if( pts_warning_cnt >= MAX_PTS_WARNING && cli_log_level < X264_LOG_DEBUG )
1996 x264_cli_log( "x264", X264_LOG_WARNING, "%d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
1998 /* duration algorithm fails when only 1 frame is output */
1999 if( i_frame_output == 1 )
2000 duration = (double)param->i_fps_den / param->i_fps_num;
2002 duration = (double)(2 * last_dts - prev_dts - first_dts) * param->i_timebase_num / param->i_timebase_den;
2004 duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
2006 i_end = x264_mdate();
2007 /* Erase progress indicator before printing encoding stats. */
2008 if( opt->b_progress )
2009 fprintf( stderr, " \r" );
2011 x264_encoder_close( h );
2012 fprintf( stderr, "\n" );
2015 fprintf( stderr, "aborted at input frame %d, output frame %d\n", opt->i_seek + i_frame, i_frame_output );
2017 cli_output.close_file( opt->hout, largest_pts, second_largest_pts );
2020 if( i_frame_output > 0 )
2022 double fps = (double)i_frame_output * (double)1000000 /
2023 (double)( i_end - i_start );
2025 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame_output, fps,
2026 (double) i_file * 8 / ( 1000 * duration ) );