x264.c 89 KB

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