1 /*****************************************************************************
2 * x264: h264 encoder testing program.
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
8 * Steven Walters <kemuri9@gmail.com>
9 * Kieran Kunhya <kieran@kunhya.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
24 *****************************************************************************/
33 #include "common/common.h"
34 #include "common/cpu.h"
41 #define SetConsoleTitle(t)
45 static int b_ctrl_c = 0;
46 static int b_exit_on_ctrl_c = 0;
47 static void SigIntHandler( int a )
49 if( b_exit_on_ctrl_c )
62 /* i/o file operation function pointer structs */
64 static cli_output_t output;
66 static const char * const demuxer_names[] =
83 static const char * const muxer_names[] =
95 static void Help( x264_param_t *defaults, int longhelp );
96 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
97 static int Encode( x264_param_t *param, cli_opt_t *opt );
99 /****************************************************************************
101 ****************************************************************************/
102 int main( int argc, char **argv )
108 #ifdef PTW32_STATIC_LIB
109 pthread_win32_process_attach_np();
110 pthread_win32_thread_attach_np();
114 _setmode(_fileno(stdin), _O_BINARY);
115 _setmode(_fileno(stdout), _O_BINARY);
118 x264_param_default( ¶m );
120 /* Parse command line */
121 if( Parse( argc, argv, ¶m, &opt ) < 0 )
124 /* Control-C handler */
125 signal( SIGINT, SigIntHandler );
127 ret = Encode( ¶m, &opt );
129 #ifdef PTW32_STATIC_LIB
130 pthread_win32_thread_detach_np();
131 pthread_win32_process_detach_np();
137 static char const *strtable_lookup( const char * const table[], int index )
139 int i = 0; while( table[i] ) i++;
140 return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
143 static char *stringify_names( char *buf, const char * const names[] )
147 for( p[0] = 0; names[i]; i++ )
149 p += sprintf( p, "%s", names[i] );
151 p += sprintf( p, ", " );
156 /*****************************************************************************
158 *****************************************************************************/
159 static void Help( x264_param_t *defaults, int longhelp )
163 #define H1 if(longhelp>=1) printf
164 #define H2 if(longhelp==2) printf
165 H0( "x264 core:%d%s\n"
166 "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
168 "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
169 " or YUV4MPEG 4:2:0 (*.y4m),\n"
170 " or Avisynth if compiled with support (%s).\n"
171 " or libav* formats if compiled with lavf support (%s) or ffms support (%s).\n"
172 "Outfile type is selected by filename:\n"
173 " .264 -> Raw bytestream\n"
174 " .mkv -> Matroska\n"
175 " .flv -> Flash Video\n"
176 " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
180 " -h, --help List basic options\n"
181 " --longhelp List more options\n"
182 " --fullhelp List all options\n"
184 X264_BUILD, X264_VERSION,
206 H0( "Example usage:\n" );
208 H0( " Constant quality mode:\n" );
209 H0( " x264 --crf 24 -o <output> <input>\n" );
211 H0( " Two-pass with a bitrate of 1000kbps:\n" );
212 H0( " x264 --pass 1 --bitrate 1000 -o <output> <input>\n" );
213 H0( " x264 --pass 2 --bitrate 1000 -o <output> <input>\n" );
215 H0( " Lossless:\n" );
216 H0( " x264 --crf 0 -o <output> <input>\n" );
218 H0( " Maximum PSNR at the cost of speed and visual quality:\n" );
219 H0( " x264 --preset placebo --tune psnr -o <output> <input>\n" );
221 H0( " Constant bitrate at 1000kbps with a 2 second-buffer:\n");
222 H0( " x264 --vbv-bufsize 2000 --bitrate 1000 -o <output> <input>\n" );
226 H0( " --profile Force the limits of an H.264 profile [high]\n"
227 " Overrides all settings.\n" );
229 " --no-8x8dct --bframes 0 --no-cabac\n"
230 " --cqm flat --weightp 0 No interlaced\n"
233 " --no-8x8dct --cqm flat No lossless\n"
236 else H0( " - baseline,main,high\n" );
237 H0( " --preset Use a preset to select encoding settings [medium]\n"
238 " Overridden by user settings.\n" );
239 H2( " - ultrafast:\n"
240 " --no-8x8dct --aq-mode 0 --b-adapt 0\n"
241 " --bframes 0 --no-cabac --no-deblock\n"
242 " --no-mbtree --me dia --no-mixed-refs\n"
243 " --partitions none --ref 1 --scenecut 0\n"
244 " --subme 0 --trellis 0 --no-weightb\n"
247 " --no-mbtree --me dia --no-mixed-refs\n"
248 " --partitions i8x8,i4x4 --ref 1\n"
249 " --subme 1 --trellis 0 --weightp 0\n"
251 " --no-mbtree --no-mixed-refs --ref 2\n"
252 " --subme 4 --weightp 1\n"
254 " --rc-lookahead 30 --ref 2 --subme 6\n"
256 " Default settings apply.\n"
258 " --b-adapt 2 --direct auto --me umh\n"
259 " --rc-lookahead 50 --ref 5 --subme 8\n"
261 " --b-adapt 2 --direct auto --me umh\n"
262 " --partitions all --rc-lookahead 60\n"
263 " --ref 8 --subme 9 --trellis 2\n"
265 " --b-adapt 2 --bframes 8 --direct auto\n"
266 " --me umh --merange 24 --partitions all\n"
267 " --ref 16 --subme 10 --trellis 2\n"
268 " --rc-lookahead 60\n"
270 " --bframes 16 --b-adapt 2 --direct auto\n"
271 " --slow-firstpass --no-fast-pskip\n"
272 " --me tesa --merange 24 --partitions all\n"
273 " --rc-lookahead 60 --ref 16 --subme 10\n"
275 else H0( " - ultrafast,veryfast,faster,fast,medium\n"
276 " - slow,slower,veryslow,placebo\n" );
277 H0( " --tune Tune the settings for a particular type of source\n"
279 " Overridden by user settings.\n"
280 " Multiple tunings are separated by commas.\n"
281 " Only one psy tuning can be used at a time.\n" );
282 H2( " - film (psy tuning):\n"
283 " --deblock -1:-1 --psy-rd <unset>:0.15\n"
284 " - animation(psy tuning):\n"
285 " --bframes {+2} --deblock 1:1\n"
286 " --psy-rd 0.4:<unset> --aq-strength 0.6\n"
287 " --ref {Double if >1 else 1}\n"
288 " - grain(psy tuning):\n"
289 " --aq-strength 0.5 --no-dct-decimate\n"
290 " --deadzone-inter 6 --deadzone-intra 6\n"
291 " --deblock -2:-2 --ipratio 1.1 \n"
292 " --pbratio 1.1 --psy-rd <unset>:0.25\n"
294 " - psnr(psy tuning):\n"
295 " --aq-mode 0 --no-psy\n"
296 " - ssim(psy tuning):\n"
297 " --aq-mode 2 --no-psy\n"
299 " --no-cabac --no-deblock --no-weightb\n"
302 " --bframes 0 --rc-lookahead 0\n"
303 " --sync-lookahead 0 --sliced-threads\n"
304 " - touhou(psy tuning):\n"
305 " --aq-strength 1.3 --deblock -1:-1\n"
306 " --partitions {p4x4 if p8x8 set}\n"
307 " --psy-rd <unset>:0.2\n"
308 " --ref {Double if >1 else 1}\n" );
309 else H0( " - psy tunings: film,animation,grain,psnr,ssim\n"
310 " - other tunings: fastdecode,zerolatency\n" );
311 H1( " --slow-firstpass Don't use faster settings with --pass 1\n" );
313 H0( "Frame-type options:\n" );
315 H0( " -I, --keyint <integer> Maximum GOP size [%d]\n", defaults->i_keyint_max );
316 H2( " -i, --min-keyint <integer> Minimum GOP size [%d]\n", defaults->i_keyint_min );
317 H2( " --no-scenecut Disable adaptive I-frame decision\n" );
318 H2( " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
319 H2( " --intra-refresh Use Periodic Intra Refresh instead of IDR frames\n" );
320 H1( " -b, --bframes <integer> Number of B-frames between I and P [%d]\n", defaults->i_bframe );
321 H1( " --b-adapt <integer> Adaptive B-frame decision method [%d]\n"
322 " Higher values may lower threading efficiency.\n"
325 " - 2: Optimal (slow with high --bframes)\n", defaults->i_bframe_adaptive );
326 H2( " --b-bias <integer> Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
327 H1( " --b-pyramid <string> Keep some B-frames as references [%s]\n"
328 " - none: Disabled\n"
329 " - strict: Strictly hierarchical pyramid\n"
330 " - normal: Non-strict (not Blu-ray compatible)\n",
331 strtable_lookup( x264_b_pyramid_names, defaults->i_bframe_pyramid ) );
332 H1( " --no-cabac Disable CABAC\n" );
333 H1( " -r, --ref <integer> Number of reference frames [%d]\n", defaults->i_frame_reference );
334 H1( " --no-deblock Disable loop filter\n" );
335 H1( " -f, --deblock <alpha:beta> Loop filter parameters [%d:%d]\n",
336 defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
337 H2( " --slices <integer> Number of slices per frame; forces rectangular\n"
338 " slices and is overridden by other slicing options\n" );
339 else H1( " --slices <integer> Number of slices per frame\n" );
340 H2( " --slice-max-size <integer> Limit the size of each slice in bytes\n");
341 H2( " --slice-max-mbs <integer> Limit the size of each slice in macroblocks\n");
342 H0( " --interlaced Enable pure-interlaced mode\n" );
343 H2( " --constrained-intra Enable constrained intra prediction.\n" );
345 H0( "Ratecontrol:\n" );
347 H1( " -q, --qp <integer> Force constant QP (0-51, 0=lossless)\n" );
348 H0( " -B, --bitrate <integer> Set bitrate (kbit/s)\n" );
349 H0( " --crf <float> Quality-based VBR (0-51, 0=lossless) [%.1f]\n", defaults->rc.f_rf_constant );
350 H1( " --rc-lookahead <integer> Number of frames for frametype lookahead [%d]\n", defaults->rc.i_lookahead );
351 H0( " --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
352 H0( " --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
353 H2( " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
354 H2( " --qpmin <integer> Set min QP [%d]\n", defaults->rc.i_qp_min );
355 H2( " --qpmax <integer> Set max QP [%d]\n", defaults->rc.i_qp_max );
356 H2( " --qpstep <integer> Set max QP step [%d]\n", defaults->rc.i_qp_step );
357 H2( " --ratetol <float> Tolerance of ABR ratecontrol and VBV [%.1f]\n", defaults->rc.f_rate_tolerance );
358 H2( " --ipratio <float> QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
359 H2( " --pbratio <float> QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
360 H2( " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
361 H2( " --aq-mode <integer> AQ method [%d]\n"
363 " - 1: Variance AQ (complexity mask)\n"
364 " - 2: Auto-variance AQ (experimental)\n", defaults->rc.i_aq_mode );
365 H1( " --aq-strength <float> Reduces blocking and blurring in flat and\n"
366 " textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
368 H0( " -p, --pass <integer> Enable multipass ratecontrol\n"
369 " - 1: First pass, creates stats file\n"
370 " - 2: Last pass, does not overwrite stats file\n" );
371 H2( " - 3: Nth pass, overwrites stats file\n" );
372 H1( " --stats <string> Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
373 H2( " --no-mbtree Disable mb-tree ratecontrol.\n");
374 H2( " --qcomp <float> QP curve compression [%.2f]\n", defaults->rc.f_qcompress );
375 H2( " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
376 H2( " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
377 H2( " --zones <zone0>/<zone1>/... Tweak the bitrate of regions of the video\n" );
378 H2( " Each zone is of the form\n"
379 " <start frame>,<end frame>,<option>\n"
380 " where <option> is either\n"
381 " q=<integer> (force QP)\n"
382 " or b=<float> (bitrate multiplier)\n" );
383 H2( " --qpfile <string> Force frametypes and QPs for some or all frames\n"
384 " Format of each line: framenumber frametype QP\n"
385 " QP of -1 lets x264 choose. Frametypes: I,i,P,B,b.\n"
386 " QPs are restricted by qpmin/qpmax.\n" );
390 H1( " -A, --partitions <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
391 " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
393 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
394 H1( " --direct <string> Direct MV prediction mode [\"%s\"]\n"
395 " - none, spatial, temporal, auto\n",
396 strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
397 H2( " --no-weightb Disable weighted prediction for B-frames\n" );
398 H1( " --weightp <integer> Weighted prediction for P-frames [%d]\n"
400 " - 1: Blind offset\n"
401 " - 2: Smart analysis\n", defaults->analyse.i_weighted_pred );
402 H1( " --me <string> Integer pixel motion estimation method [\"%s\"]\n",
403 strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
404 H2( " - dia: diamond search, radius 1 (fast)\n"
405 " - hex: hexagonal search, radius 2\n"
406 " - umh: uneven multi-hexagon search\n"
407 " - esa: exhaustive search\n"
408 " - tesa: hadamard exhaustive search (slow)\n" );
409 else H1( " - dia, hex, umh\n" );
410 H2( " --merange <integer> Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
411 H2( " --mvrange <integer> Maximum motion vector length [-1 (auto)]\n" );
412 H2( " --mvrange-thread <int> Minimum buffer between threads [-1 (auto)]\n" );
413 H1( " -m, --subme <integer> Subpixel motion estimation and mode decision [%d]\n", defaults->analyse.i_subpel_refine );
414 H2( " - 0: fullpel only (not recommended)\n"
415 " - 1: SAD mode decision, one qpel iteration\n"
416 " - 2: SATD mode decision\n"
417 " - 3-5: Progressively more qpel\n"
418 " - 6: RD mode decision for I/P-frames\n"
419 " - 7: RD mode decision for all frames\n"
420 " - 8: RD refinement for I/P-frames\n"
421 " - 9: RD refinement for all frames\n"
422 " - 10: QP-RD - requires trellis=2, aq-mode>0\n" );
423 else H1( " decision quality: 1=fast, 10=best.\n" );
424 H1( " --psy-rd Strength of psychovisual optimization [\"%.1f:%.1f\"]\n"
425 " #1: RD (requires subme>=6)\n"
426 " #2: Trellis (requires trellis, experimental)\n",
427 defaults->analyse.f_psy_rd, defaults->analyse.f_psy_trellis );
428 H2( " --no-psy Disable all visual optimizations that worsen\n"
429 " both PSNR and SSIM.\n" );
430 H2( " --no-mixed-refs Don't decide references on a per partition basis\n" );
431 H2( " --no-chroma-me Ignore chroma in motion estimation\n" );
432 H1( " --no-8x8dct Disable adaptive spatial transform size\n" );
433 H1( " -t, --trellis <integer> Trellis RD quantization. Requires CABAC. [%d]\n"
435 " - 1: enabled only on the final encode of a MB\n"
436 " - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
437 H2( " --no-fast-pskip Disables early SKIP detection on P-frames\n" );
438 H2( " --no-dct-decimate Disables coefficient thresholding on P-frames\n" );
439 H1( " --nr <integer> Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
441 H2( " --deadzone-inter <int> Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
442 H2( " --deadzone-intra <int> Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
443 H2( " Deadzones should be in the range 0 - 32.\n" );
444 H2( " --cqm <string> Preset quant matrices [\"flat\"]\n"
446 H1( " --cqmfile <string> Read custom quant matrices from a JM-compatible file\n" );
447 H2( " Overrides any other --cqm* options.\n" );
448 H2( " --cqm4 <list> Set all 4x4 quant matrices\n"
449 " Takes a comma-separated list of 16 integers.\n" );
450 H2( " --cqm8 <list> Set all 8x8 quant matrices\n"
451 " Takes a comma-separated list of 64 integers.\n" );
452 H2( " --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
453 " Set both luma and chroma quant matrices\n" );
454 H2( " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
455 " Set individual quant matrices\n" );
457 H2( "Video Usability Info (Annex E):\n" );
458 H2( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
459 H2( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
461 H2( " --overscan <string> Specify crop overscan setting [\"%s\"]\n"
462 " - undef, show, crop\n",
463 strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ) );
464 H2( " --videoformat <string> Specify video format [\"%s\"]\n"
465 " - component, pal, ntsc, secam, mac, undef\n",
466 strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ) );
467 H2( " --fullrange <string> Specify full range samples setting [\"%s\"]\n"
469 strtable_lookup( x264_fullrange_names, defaults->vui.b_fullrange ) );
470 H2( " --colorprim <string> Specify color primaries [\"%s\"]\n"
471 " - undef, bt709, bt470m, bt470bg\n"
472 " smpte170m, smpte240m, film\n",
473 strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ) );
474 H2( " --transfer <string> Specify transfer characteristics [\"%s\"]\n"
475 " - undef, bt709, bt470m, bt470bg, linear,\n"
476 " log100, log316, smpte170m, smpte240m\n",
477 strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ) );
478 H2( " --colormatrix <string> Specify color matrix setting [\"%s\"]\n"
479 " - undef, bt709, fcc, bt470bg\n"
480 " smpte170m, smpte240m, GBR, YCgCo\n",
481 strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
482 H2( " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n",
483 defaults->vui.i_chroma_loc );
485 H0( "Input/Output:\n" );
487 H0( " -o, --output Specify output file\n" );
488 H1( " --muxer <string> Specify output container format [\"%s\"]\n"
489 " - %s\n", muxer_names[0], stringify_names( buf, muxer_names ) );
490 H1( " --demuxer <string> Specify input container format [\"%s\"]\n"
491 " - %s\n", demuxer_names[0], stringify_names( buf, demuxer_names ) );
492 H1( " --index <string> Filename for input index file\n" );
493 H0( " --sar width:height Specify Sample Aspect Ratio\n" );
494 H0( " --fps <float|rational> Specify framerate\n" );
495 H0( " --seek <integer> First frame to encode\n" );
496 H0( " --frames <integer> Maximum number of frames to encode\n" );
497 H0( " --level <string> Specify level (as defined by Annex A)\n" );
499 H1( " -v, --verbose Print stats for each frame\n" );
500 H1( " --no-progress Don't show the progress indicator while encoding\n" );
501 H0( " --quiet Quiet Mode\n" );
502 H1( " --psnr Enable PSNR computation\n" );
503 H1( " --ssim Enable SSIM computation\n" );
504 H1( " --threads <integer> Force a specific number of threads\n" );
505 H2( " --sliced-threads Low-latency but lower-efficiency threading\n" );
506 H2( " --thread-input Run Avisynth in its own thread\n" );
507 H2( " --sync-lookahead <integer> Number of buffer frames for threaded lookahead\n" );
508 H2( " --non-deterministic Slightly improve quality of SMP, at the cost of repeatability\n" );
509 H2( " --asm <integer> Override CPU detection\n" );
510 H2( " --no-asm Disable all CPU optimizations\n" );
511 H2( " --visualize Show MB types overlayed on the encoded video\n" );
512 H2( " --dump-yuv <string> Save reconstructed frames\n" );
513 H2( " --sps-id <integer> Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
514 H2( " --aud Use access unit delimiters\n" );
515 H2( " --force-cfr Force constant framerate timestamp generation\n" );
519 #define OPT_FRAMES 256
521 #define OPT_QPFILE 258
522 #define OPT_THREAD_INPUT 259
523 #define OPT_QUIET 260
524 #define OPT_NOPROGRESS 261
525 #define OPT_VISUALIZE 262
526 #define OPT_LONGHELP 263
527 #define OPT_PROFILE 264
528 #define OPT_PRESET 265
530 #define OPT_SLOWFIRSTPASS 267
531 #define OPT_FULLHELP 268
533 #define OPT_MUXER 270
534 #define OPT_DEMUXER 271
535 #define OPT_INDEX 272
536 #define OPT_INTERLACED 273
538 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
539 static struct option long_options[] =
541 { "help", no_argument, NULL, 'h' },
542 { "longhelp", no_argument, NULL, OPT_LONGHELP },
543 { "fullhelp", no_argument, NULL, OPT_FULLHELP },
544 { "version", no_argument, NULL, 'V' },
545 { "profile", required_argument, NULL, OPT_PROFILE },
546 { "preset", required_argument, NULL, OPT_PRESET },
547 { "tune", required_argument, NULL, OPT_TUNE },
548 { "slow-firstpass", no_argument, NULL, OPT_SLOWFIRSTPASS },
549 { "bitrate", required_argument, NULL, 'B' },
550 { "bframes", required_argument, NULL, 'b' },
551 { "b-adapt", required_argument, NULL, 0 },
552 { "no-b-adapt", no_argument, NULL, 0 },
553 { "b-bias", required_argument, NULL, 0 },
554 { "b-pyramid", required_argument, NULL, 0 },
555 { "min-keyint", required_argument, NULL, 'i' },
556 { "keyint", required_argument, NULL, 'I' },
557 { "intra-refresh", no_argument, NULL, 0 },
558 { "scenecut", required_argument, NULL, 0 },
559 { "no-scenecut", no_argument, NULL, 0 },
560 { "nf", no_argument, NULL, 0 },
561 { "no-deblock", no_argument, NULL, 0 },
562 { "filter", required_argument, NULL, 0 },
563 { "deblock", required_argument, NULL, 'f' },
564 { "interlaced", no_argument, NULL, OPT_INTERLACED },
565 { "no-interlaced", no_argument, NULL, OPT_INTERLACED },
566 { "constrained-intra", no_argument, NULL, 0 },
567 { "cabac", no_argument, NULL, 0 },
568 { "no-cabac", no_argument, NULL, 0 },
569 { "qp", required_argument, NULL, 'q' },
570 { "qpmin", required_argument, NULL, 0 },
571 { "qpmax", required_argument, NULL, 0 },
572 { "qpstep", required_argument, NULL, 0 },
573 { "crf", required_argument, NULL, 0 },
574 { "rc-lookahead",required_argument, NULL, 0 },
575 { "ref", required_argument, NULL, 'r' },
576 { "asm", required_argument, NULL, 0 },
577 { "no-asm", no_argument, NULL, 0 },
578 { "sar", required_argument, NULL, 0 },
579 { "fps", required_argument, NULL, OPT_FPS },
580 { "frames", required_argument, NULL, OPT_FRAMES },
581 { "seek", required_argument, NULL, OPT_SEEK },
582 { "output", required_argument, NULL, 'o' },
583 { "muxer", required_argument, NULL, OPT_MUXER },
584 { "demuxer", required_argument, NULL, OPT_DEMUXER },
585 { "stdout", required_argument, NULL, OPT_MUXER },
586 { "stdin", required_argument, NULL, OPT_DEMUXER },
587 { "index", required_argument, NULL, OPT_INDEX },
588 { "analyse", required_argument, NULL, 0 },
589 { "partitions", required_argument, NULL, 'A' },
590 { "direct", required_argument, NULL, 0 },
591 { "weightb", no_argument, NULL, 'w' },
592 { "no-weightb", no_argument, NULL, 0 },
593 { "weightp", required_argument, NULL, 0 },
594 { "me", required_argument, NULL, 0 },
595 { "merange", required_argument, NULL, 0 },
596 { "mvrange", required_argument, NULL, 0 },
597 { "mvrange-thread", required_argument, NULL, 0 },
598 { "subme", required_argument, NULL, 'm' },
599 { "psy-rd", required_argument, NULL, 0 },
600 { "no-psy", no_argument, NULL, 0 },
601 { "psy", no_argument, NULL, 0 },
602 { "mixed-refs", no_argument, NULL, 0 },
603 { "no-mixed-refs", no_argument, NULL, 0 },
604 { "no-chroma-me", no_argument, NULL, 0 },
605 { "8x8dct", no_argument, NULL, 0 },
606 { "no-8x8dct", no_argument, NULL, 0 },
607 { "trellis", required_argument, NULL, 't' },
608 { "fast-pskip", no_argument, NULL, 0 },
609 { "no-fast-pskip", no_argument, NULL, 0 },
610 { "no-dct-decimate", no_argument, NULL, 0 },
611 { "aq-strength", required_argument, NULL, 0 },
612 { "aq-mode", required_argument, NULL, 0 },
613 { "deadzone-inter", required_argument, NULL, '0' },
614 { "deadzone-intra", required_argument, NULL, '0' },
615 { "level", required_argument, NULL, 0 },
616 { "ratetol", required_argument, NULL, 0 },
617 { "vbv-maxrate", required_argument, NULL, 0 },
618 { "vbv-bufsize", required_argument, NULL, 0 },
619 { "vbv-init", required_argument, NULL, 0 },
620 { "ipratio", required_argument, NULL, 0 },
621 { "pbratio", required_argument, NULL, 0 },
622 { "chroma-qp-offset", required_argument, NULL, 0 },
623 { "pass", required_argument, NULL, 'p' },
624 { "stats", required_argument, NULL, 0 },
625 { "qcomp", required_argument, NULL, 0 },
626 { "mbtree", no_argument, NULL, 0 },
627 { "no-mbtree", no_argument, NULL, 0 },
628 { "qblur", required_argument, NULL, 0 },
629 { "cplxblur", required_argument, NULL, 0 },
630 { "zones", required_argument, NULL, 0 },
631 { "qpfile", required_argument, NULL, OPT_QPFILE },
632 { "threads", required_argument, NULL, 0 },
633 { "sliced-threads", no_argument, NULL, 0 },
634 { "no-sliced-threads", no_argument, NULL, 0 },
635 { "slice-max-size", required_argument, NULL, 0 },
636 { "slice-max-mbs", required_argument, NULL, 0 },
637 { "slices", required_argument, NULL, 0 },
638 { "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
639 { "sync-lookahead", required_argument, NULL, 0 },
640 { "non-deterministic", no_argument, NULL, 0 },
641 { "psnr", no_argument, NULL, 0 },
642 { "ssim", no_argument, NULL, 0 },
643 { "quiet", no_argument, NULL, OPT_QUIET },
644 { "verbose", no_argument, NULL, 'v' },
645 { "no-progress", no_argument, NULL, OPT_NOPROGRESS },
646 { "visualize", no_argument, NULL, OPT_VISUALIZE },
647 { "dump-yuv", required_argument, NULL, 0 },
648 { "sps-id", required_argument, NULL, 0 },
649 { "aud", no_argument, NULL, 0 },
650 { "nr", required_argument, NULL, 0 },
651 { "cqm", required_argument, NULL, 0 },
652 { "cqmfile", required_argument, NULL, 0 },
653 { "cqm4", required_argument, NULL, 0 },
654 { "cqm4i", required_argument, NULL, 0 },
655 { "cqm4iy", required_argument, NULL, 0 },
656 { "cqm4ic", required_argument, NULL, 0 },
657 { "cqm4p", required_argument, NULL, 0 },
658 { "cqm4py", required_argument, NULL, 0 },
659 { "cqm4pc", required_argument, NULL, 0 },
660 { "cqm8", required_argument, NULL, 0 },
661 { "cqm8i", required_argument, NULL, 0 },
662 { "cqm8p", required_argument, NULL, 0 },
663 { "overscan", required_argument, NULL, 0 },
664 { "videoformat", required_argument, NULL, 0 },
665 { "fullrange", required_argument, NULL, 0 },
666 { "colorprim", required_argument, NULL, 0 },
667 { "transfer", required_argument, NULL, 0 },
668 { "colormatrix", required_argument, NULL, 0 },
669 { "chromaloc", required_argument, NULL, 0 },
670 { "force-cfr", no_argument, NULL, 0 },
674 static int select_output( const char *muxer, char *filename, x264_param_t *param )
676 const char *ext = get_filename_extension( filename );
677 if( !strcmp( filename, "-" ) || strcasecmp( muxer, "auto" ) )
680 if( !strcasecmp( ext, "mp4" ) )
686 param->b_dts_compress = 0;
687 param->b_repeat_headers = 0;
689 fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
693 else if( !strcasecmp( ext, "mkv" ) )
698 param->b_dts_compress = 0;
699 param->b_repeat_headers = 0;
701 else if( !strcasecmp( ext, "flv" ) )
706 param->b_dts_compress = 1;
707 param->b_repeat_headers = 0;
714 static int select_input( const char *demuxer, char *used_demuxer, char *filename,
715 hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
717 const char *ext = get_filename_extension( filename );
718 int b_regular = strcmp( filename, "-" );
719 int b_auto = !strcasecmp( demuxer, "auto" );
720 if( !b_regular && b_auto )
724 FILE *f = fopen( filename, "r" );
727 b_regular = x264_is_regular_file( f );
731 const char *module = b_auto ? ext : demuxer;
733 if( !strcasecmp( module, "avs" ) || !strcasecmp( ext, "d2v" ) || !strcasecmp( ext, "dga" ) )
739 fprintf( stderr, "x264 [error]: not compiled with AVS input support\n" );
743 else if( !strcasecmp( module, "y4m" ) )
745 else if( !strcasecmp( module, "yuv" ) )
750 if( b_regular && (b_auto || !strcasecmp( demuxer, "ffms" )) &&
751 !ffms_input.open_file( filename, p_handle, info, opt ) )
759 if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
760 !lavf_input.open_file( filename, p_handle, info, opt ) )
768 if( b_regular && (b_auto || !strcasecmp( demuxer, "avs" )) &&
769 !avs_input.open_file( filename, p_handle, info, opt ) )
776 if( b_auto && !yuv_input.open_file( filename, p_handle, info, opt ) )
785 fprintf( stderr, "x264 [error]: could not open input file `%s' via any method!\n", filename );
789 strcpy( used_demuxer, module );
794 /*****************************************************************************
796 *****************************************************************************/
797 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
799 char *input_filename = NULL;
800 const char *demuxer = demuxer_names[0];
801 char *output_filename = NULL;
802 const char *muxer = muxer_names[0];
803 x264_param_t defaults = *param;
804 char *profile = NULL;
805 int b_thread_input = 0;
810 int b_user_interlaced = 0;
812 cli_input_opt_t input_opt;
814 memset( opt, 0, sizeof(cli_opt_t) );
815 memset( &input_opt, 0, sizeof(cli_input_opt_t) );
818 /* Presets are applied before all other options. */
821 int c = getopt_long( argc, argv, short_options, long_options, NULL );
825 if( c == OPT_PRESET )
827 if( !strcasecmp( optarg, "ultrafast" ) )
829 param->i_frame_reference = 1;
830 param->i_scenecut_threshold = 0;
831 param->b_deblocking_filter = 0;
834 param->analyse.intra = 0;
835 param->analyse.inter = 0;
836 param->analyse.b_transform_8x8 = 0;
837 param->analyse.i_me_method = X264_ME_DIA;
838 param->analyse.i_subpel_refine = 0;
839 param->rc.i_aq_mode = 0;
840 param->analyse.b_mixed_references = 0;
841 param->analyse.i_trellis = 0;
842 param->i_bframe_adaptive = X264_B_ADAPT_NONE;
843 param->rc.b_mb_tree = 0;
844 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
846 else if( !strcasecmp( optarg, "veryfast" ) )
848 param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
849 param->analyse.i_me_method = X264_ME_DIA;
850 param->analyse.i_subpel_refine = 1;
851 param->i_frame_reference = 1;
852 param->analyse.b_mixed_references = 0;
853 param->analyse.i_trellis = 0;
854 param->rc.b_mb_tree = 0;
855 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
857 else if( !strcasecmp( optarg, "faster" ) )
859 param->analyse.b_mixed_references = 0;
860 param->i_frame_reference = 2;
861 param->analyse.i_subpel_refine = 4;
862 param->rc.b_mb_tree = 0;
863 param->analyse.i_weighted_pred = X264_WEIGHTP_BLIND;
865 else if( !strcasecmp( optarg, "fast" ) )
867 param->i_frame_reference = 2;
868 param->analyse.i_subpel_refine = 6;
869 param->rc.i_lookahead = 30;
871 else if( !strcasecmp( optarg, "medium" ) )
873 /* Default is medium */
875 else if( !strcasecmp( optarg, "slow" ) )
877 param->analyse.i_me_method = X264_ME_UMH;
878 param->analyse.i_subpel_refine = 8;
879 param->i_frame_reference = 5;
880 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
881 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
882 param->rc.i_lookahead = 50;
884 else if( !strcasecmp( optarg, "slower" ) )
886 param->analyse.i_me_method = X264_ME_UMH;
887 param->analyse.i_subpel_refine = 9;
888 param->i_frame_reference = 8;
889 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
890 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
891 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
892 param->analyse.i_trellis = 2;
893 param->rc.i_lookahead = 60;
895 else if( !strcasecmp( optarg, "veryslow" ) )
897 param->analyse.i_me_method = X264_ME_UMH;
898 param->analyse.i_subpel_refine = 10;
899 param->analyse.i_me_range = 24;
900 param->i_frame_reference = 16;
901 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
902 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
903 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
904 param->analyse.i_trellis = 2;
906 param->rc.i_lookahead = 60;
908 else if( !strcasecmp( optarg, "placebo" ) )
910 param->analyse.i_me_method = X264_ME_TESA;
911 param->analyse.i_subpel_refine = 10;
912 param->analyse.i_me_range = 24;
913 param->i_frame_reference = 16;
914 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
915 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
916 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
917 param->analyse.b_fast_pskip = 0;
918 param->analyse.i_trellis = 2;
919 param->i_bframe = 16;
920 param->rc.i_lookahead = 60;
925 fprintf( stderr, "x264 [error]: invalid preset '%s'\n", optarg );
933 /* Tunings are applied next. */
936 int c = getopt_long( argc, argv, short_options, long_options, NULL );
942 char *s = strtok( optarg, ",./-+" );
943 int psy_tuning_used = 0;
946 if( !strncasecmp( s, "film", 4 ) )
948 if( psy_tuning_used ) goto psy_failure;
949 param->i_deblocking_filter_alphac0 = -1;
950 param->i_deblocking_filter_beta = -1;
951 param->analyse.f_psy_trellis = 0.15;
954 else if( !strncasecmp( s, "animation", 9 ) )
956 if( psy_tuning_used ) goto psy_failure;
957 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
958 param->i_deblocking_filter_alphac0 = 1;
959 param->i_deblocking_filter_beta = 1;
960 param->analyse.f_psy_rd = 0.4;
961 param->rc.f_aq_strength = 0.6;
962 param->i_bframe += 2;
965 else if( !strncasecmp( s, "grain", 5 ) )
967 if( psy_tuning_used ) goto psy_failure;
968 param->i_deblocking_filter_alphac0 = -2;
969 param->i_deblocking_filter_beta = -2;
970 param->analyse.f_psy_trellis = 0.25;
971 param->analyse.b_dct_decimate = 0;
972 param->rc.f_pb_factor = 1.1;
973 param->rc.f_ip_factor = 1.1;
974 param->rc.f_aq_strength = 0.5;
975 param->analyse.i_luma_deadzone[0] = 6;
976 param->analyse.i_luma_deadzone[1] = 6;
977 param->rc.f_qcompress = 0.8;
980 else if( !strncasecmp( s, "psnr", 4 ) )
982 if( psy_tuning_used ) goto psy_failure;
983 param->rc.i_aq_mode = X264_AQ_NONE;
984 param->analyse.b_psy = 0;
987 else if( !strncasecmp( s, "ssim", 4 ) )
989 if( psy_tuning_used ) goto psy_failure;
990 param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
991 param->analyse.b_psy = 0;
994 else if( !strncasecmp( s, "fastdecode", 10 ) )
996 param->b_deblocking_filter = 0;
998 param->analyse.b_weighted_bipred = 0;
999 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
1001 else if( !strncasecmp( s, "zerolatency", 11 ) )
1003 param->rc.i_lookahead = 0;
1004 param->i_sync_lookahead = 0;
1005 param->i_bframe = 0;
1006 param->b_sliced_threads = 1;
1008 else if( !strncasecmp( s, "touhou", 6 ) )
1010 if( psy_tuning_used ) goto psy_failure;
1011 param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
1012 param->i_deblocking_filter_alphac0 = -1;
1013 param->i_deblocking_filter_beta = -1;
1014 param->analyse.f_psy_trellis = 0.2;
1015 param->rc.f_aq_strength = 1.3;
1016 if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
1017 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
1018 psy_tuning_used = 1;
1022 fprintf( stderr, "x264 [error]: invalid tune '%s'\n", s );
1028 fprintf( stderr, "x264 [warning]: only 1 psy tuning can be used: ignoring tune %s\n", s );
1030 s = strtok( NULL, ",./-+" );
1037 /* Parse command line options */
1041 int long_options_index = -1;
1043 int c = getopt_long( argc, argv, short_options, long_options, &long_options_index );
1053 Help( &defaults, 0 );
1056 Help( &defaults, 1 );
1059 Help( &defaults, 2 );
1062 #ifdef X264_POINTVER
1063 printf( "x264 "X264_POINTVER"\n" );
1065 printf( "x264 0.%d.X\n", X264_BUILD );
1067 printf( "built on " __DATE__ ", " );
1069 printf( "gcc: " __VERSION__ "\n" );
1071 printf( "using a non-gcc compiler\n" );
1075 param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
1078 opt->i_seek = input_opt.seek = X264_MAX( atoi( optarg ), 0 );
1081 output_filename = optarg;
1084 for( i = 0; muxer_names[i] && strcasecmp( muxer_names[i], optarg ); )
1086 if( !muxer_names[i] )
1088 fprintf( stderr, "x264 [error]: invalid muxer '%s'\n", optarg );
1094 for( i = 0; demuxer_names[i] && strcasecmp( demuxer_names[i], optarg ); )
1096 if( !demuxer_names[i] )
1098 fprintf( stderr, "x264 [error]: invalid demuxer '%s'\n", optarg );
1104 input_opt.index = optarg;
1107 opt->qpfile = fopen( optarg, "rb" );
1110 fprintf( stderr, "x264 [error]: can't open qpfile `%s'\n", optarg );
1113 else if( !x264_is_regular_file( opt->qpfile ) )
1115 fprintf( stderr, "x264 [error]: qpfile incompatible with non-regular file `%s'\n", optarg );
1116 fclose( opt->qpfile );
1120 case OPT_THREAD_INPUT:
1124 param->i_log_level = X264_LOG_NONE;
1127 param->i_log_level = X264_LOG_DEBUG;
1129 case OPT_NOPROGRESS:
1130 opt->b_progress = 0;
1134 param->b_visualize = 1;
1135 b_exit_on_ctrl_c = 1;
1137 fprintf( stderr, "x264 [warning]: not compiled with visualization support\n" );
1146 case OPT_SLOWFIRSTPASS:
1151 goto generic_option;
1153 b_pass1 = atoi( optarg ) == 1;
1154 goto generic_option;
1157 param->b_vfr_input = 0;
1158 goto generic_option;
1159 case OPT_INTERLACED:
1160 b_user_interlaced = 1;
1161 goto generic_option;
1166 if( long_options_index < 0 )
1168 for( i = 0; long_options[i].name; i++ )
1169 if( long_options[i].val == c )
1171 long_options_index = i;
1174 if( long_options_index < 0 )
1176 /* getopt_long already printed an error message */
1181 b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg );
1187 const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
1188 fprintf( stderr, "x264 [error]: invalid argument: %s = %s\n", name, optarg );
1193 /* Set faster options in case of turbo firstpass. */
1194 if( b_turbo && b_pass1 )
1196 param->i_frame_reference = 1;
1197 param->analyse.b_transform_8x8 = 0;
1198 param->analyse.inter = 0;
1199 param->analyse.i_me_method = X264_ME_DIA;
1200 param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
1201 param->analyse.i_trellis = 0;
1204 /* Apply profile restrictions. */
1207 if( !strcasecmp( profile, "baseline" ) )
1209 param->analyse.b_transform_8x8 = 0;
1211 param->i_cqm_preset = X264_CQM_FLAT;
1212 param->i_bframe = 0;
1213 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
1214 if( param->b_interlaced )
1216 fprintf( stderr, "x264 [error]: baseline profile doesn't support interlacing\n" );
1220 else if( !strcasecmp( profile, "main" ) )
1222 param->analyse.b_transform_8x8 = 0;
1223 param->i_cqm_preset = X264_CQM_FLAT;
1225 else if( !strcasecmp( profile, "high" ) )
1231 fprintf( stderr, "x264 [error]: invalid profile: %s\n", profile );
1234 if( (param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0) ||
1235 (param->rc.i_rc_method == X264_RC_CRF && param->rc.f_rf_constant == 0) )
1237 fprintf( stderr, "x264 [error]: %s profile doesn't support lossless\n", profile );
1242 /* Get the file name */
1243 if( optind > argc - 1 || !output_filename )
1245 fprintf( stderr, "x264 [error]: No %s file. Run x264 --help for a list of options.\n",
1246 optind > argc - 1 ? "input" : "output" );
1250 if( select_output( muxer, output_filename, param ) )
1252 if( output.open_file( output_filename, &opt->hout ) )
1254 fprintf( stderr, "x264 [error]: could not open output file `%s'\n", output_filename );
1258 input_filename = argv[optind++];
1259 input_opt.resolution = optind < argc ? argv[optind++] : NULL;
1260 video_info_t info = {0};
1261 char demuxername[5];
1263 /* set info flags to param flags to be overwritten by demuxer as necessary. */
1264 info.csp = param->i_csp;
1265 info.fps_num = param->i_fps_num;
1266 info.fps_den = param->i_fps_den;
1267 info.interlaced = param->b_interlaced;
1268 info.sar_width = param->vui.i_sar_width;
1269 info.sar_height = param->vui.i_sar_height;
1270 info.vfr = param->b_vfr_input;
1272 if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
1275 if( !opt->hin && input.open_file( input_filename, &opt->hin, &info, &input_opt ) )
1277 fprintf( stderr, "x264 [error]: could not open input file `%s'\n", input_filename );
1281 x264_reduce_fraction( &info.sar_width, &info.sar_height );
1282 x264_reduce_fraction( &info.fps_num, &info.fps_den );
1283 if( param->i_log_level >= X264_LOG_INFO )
1284 fprintf( stderr, "%s [info]: %dx%d%c %d:%d @ %d/%d fps (%cfr)\n", demuxername, info.width,
1285 info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
1286 info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
1288 /* set param flags from the info flags as necessary */
1289 param->i_csp = info.csp;
1290 param->i_height = info.height;
1291 param->b_vfr_input = info.vfr;
1292 param->i_width = info.width;
1293 if( !b_user_interlaced && info.interlaced )
1295 fprintf( stderr, "x264 [warning]: input appears to be interlaced, enabling interlaced mode.\n"
1296 " If you want otherwise, use --no-interlaced\n" );
1297 param->b_interlaced = 1;
1301 param->i_fps_num = info.fps_num;
1302 param->i_fps_den = info.fps_den;
1304 if( param->b_vfr_input )
1306 param->i_timebase_num = info.timebase_num;
1307 param->i_timebase_den = info.timebase_den;
1311 param->i_timebase_den = param->i_fps_num;
1312 param->i_timebase_num = param->i_fps_den;
1314 if( !param->vui.i_sar_width || !param->vui.i_sar_height )
1316 param->vui.i_sar_width = info.sar_width;
1317 param->vui.i_sar_height = info.sar_height;
1321 if( b_thread_input || param->i_threads > 1
1322 || (param->i_threads == X264_THREADS_AUTO && x264_cpu_num_processors() > 1) )
1324 if( thread_input.open_file( NULL, &opt->hin, &info, NULL ) )
1326 fprintf( stderr, "x264 [error]: threaded input failed\n" );
1330 input = thread_input;
1335 /* Automatically reduce reference frame count to match the user's target level
1336 * if the user didn't explicitly set a reference frame count. */
1339 int mbs = (((param->i_width)+15)>>4) * (((param->i_height)+15)>>4);
1341 for( i = 0; x264_levels[i].level_idc != 0; i++ )
1342 if( param->i_level_idc == x264_levels[i].level_idc )
1344 while( mbs * 384 * param->i_frame_reference > x264_levels[i].dpb
1345 && param->i_frame_reference > 1 )
1347 param->i_frame_reference--;
1357 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
1359 int num = -1, qp, ret;
1362 while( num < i_frame )
1364 file_pos = ftell( opt->qpfile );
1365 ret = fscanf( opt->qpfile, "%d %c %d\n", &num, &type, &qp );
1366 if( num > i_frame || ret == EOF )
1368 pic->i_type = X264_TYPE_AUTO;
1370 fseek( opt->qpfile, file_pos, SEEK_SET );
1373 if( num < i_frame && ret == 3 )
1375 pic->i_qpplus1 = qp+1;
1376 if ( type == 'I' ) pic->i_type = X264_TYPE_IDR;
1377 else if( type == 'i' ) pic->i_type = X264_TYPE_I;
1378 else if( type == 'P' ) pic->i_type = X264_TYPE_P;
1379 else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
1380 else if( type == 'b' ) pic->i_type = X264_TYPE_B;
1382 if( ret != 3 || qp < -1 || qp > 51 )
1384 fprintf( stderr, "x264 [error]: can't parse qpfile for frame %d\n", i_frame );
1385 fclose( opt->qpfile );
1387 pic->i_type = X264_TYPE_AUTO;
1394 /*****************************************************************************
1396 *****************************************************************************/
1398 static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_pts )
1400 x264_picture_t pic_out;
1403 int i_frame_size = 0;
1405 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
1407 if( i_frame_size < 0 )
1409 fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
1415 i_frame_size = output.write_frame( hout, nal[0].p_payload, i_frame_size, &pic_out );
1416 *last_pts = pic_out.i_pts;
1419 return i_frame_size;
1422 static void Print_status( int64_t i_start, int i_frame, int i_frame_total, int64_t i_file, x264_param_t *param, int64_t last_pts )
1425 int64_t i_elapsed = x264_mdate() - i_start;
1426 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1427 double bitrate = (double) i_file * 8 / ( (double) last_pts * 1000 * param->i_timebase_num / param->i_timebase_den );
1430 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1431 sprintf( buf, "x264 [%.1f%%] %d/%d frames, %.2f fps, %.2f kb/s, eta %d:%02d:%02d",
1432 100. * i_frame / i_frame_total, i_frame, i_frame_total, fps, bitrate,
1433 eta/3600, (eta/60)%60, eta%60 );
1437 sprintf( buf, "x264 %d frames: %.2f fps, %.2f kb/s", i_frame, fps, bitrate );
1439 fprintf( stderr, "%s \r", buf+5 );
1440 SetConsoleTitle( buf );
1441 fflush( stderr ); // needed in windows
1444 static int Encode( x264_param_t *param, cli_opt_t *opt )
1449 int i_frame, i_frame_total, i_frame_output;
1450 int64_t i_start, i_end;
1453 int i_update_interval;
1454 int64_t last_pts = 0;
1455 # define MAX_PTS_WARNING 3 /* arbitrary */
1456 int pts_warning_cnt = 0;
1457 int64_t largest_pts = -1;
1458 int64_t second_largest_pts = -1;
1459 int64_t ticks_per_frame;
1461 int prev_timebase_den = param->i_timebase_den;
1462 int dts_compress_multiplier;
1464 opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
1465 i_frame_total = input.get_frame_total( opt->hin );
1466 i_frame_total = X264_MAX( i_frame_total - opt->i_seek, 0 );
1467 if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
1468 && param->i_frame_total > 0 )
1469 i_frame_total = param->i_frame_total;
1470 param->i_frame_total = i_frame_total;
1471 i_update_interval = i_frame_total ? x264_clip3( i_frame_total / 1000, 1, 10 ) : 10;
1473 if( ( h = x264_encoder_open( param ) ) == NULL )
1475 fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
1476 input.close_file( opt->hin );
1480 x264_encoder_parameters( h, param );
1482 dts_compress_multiplier = param->i_timebase_den / prev_timebase_den;
1484 if( output.set_param( opt->hout, param ) )
1486 fprintf( stderr, "x264 [error]: can't set outfile param\n" );
1487 input.close_file( opt->hin );
1488 output.close_file( opt->hout, largest_pts, second_largest_pts );
1492 /* Create a new pic */
1493 if( input.picture_alloc( &pic, param->i_csp, param->i_width, param->i_height ) )
1495 fprintf( stderr, "x264 [error]: malloc failed\n" );
1499 i_start = x264_mdate();
1500 /* ticks/frame = ticks/second / frames/second */
1501 ticks_per_frame = (int64_t)h->param.i_timebase_den * h->param.i_fps_den / h->param.i_timebase_num / h->param.i_fps_num;
1502 if( ticks_per_frame < 1 )
1504 fprintf( stderr, "x264 [error]: ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame );
1508 if( !h->param.b_repeat_headers )
1510 // Write SPS/PPS/SEI
1511 x264_nal_t *headers;
1514 if( x264_encoder_headers( h, &headers, &i_nal ) < 0 )
1516 fprintf( stderr, "x264 [error]: x264_encoder_headers failed\n" );
1520 if( (i_file = output.write_headers( opt->hout, headers )) < 0 )
1525 for( i_frame = 0, i_frame_output = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
1527 if( input.read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
1530 if( !param->b_vfr_input )
1531 pic.i_pts = i_frame;
1532 if( pic.i_pts <= largest_pts )
1534 if( h->param.i_log_level >= X264_LOG_WARNING )
1536 if( h->param.i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
1537 fprintf( stderr, "x264 [warning]: non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
1538 i_frame, pic.i_pts * dts_compress_multiplier, largest_pts * dts_compress_multiplier );
1539 else if( pts_warning_cnt == MAX_PTS_WARNING )
1540 fprintf( stderr, "x264 [warning]: too many nonmonotonic pts warnings, suppressing further ones\n" );
1543 pic.i_pts = largest_pts + ticks_per_frame;
1545 second_largest_pts = largest_pts;
1546 largest_pts = pic.i_pts;
1549 parse_qpfile( opt, &pic, i_frame + opt->i_seek );
1552 /* Do not force any parameters */
1553 pic.i_type = X264_TYPE_AUTO;
1557 i_frame_size = Encode_frame( h, opt->hout, &pic, &last_pts );
1558 if( i_frame_size < 0 )
1560 i_file += i_frame_size;
1566 if( input.release_frame && input.release_frame( &pic, opt->hin ) )
1569 /* update status line (up to 1000 times per input file) */
1570 if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
1571 Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
1573 /* Flush delayed frames */
1574 while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
1576 i_frame_size = Encode_frame( h, opt->hout, NULL, &last_pts );
1577 if( i_frame_size < 0 )
1579 i_file += i_frame_size;
1582 if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
1583 Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
1585 if( pts_warning_cnt >= MAX_PTS_WARNING && h->param.i_log_level < X264_LOG_DEBUG )
1586 fprintf( stderr, "x264 [warning]: %d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
1588 /* duration algorithm fails when only 1 frame is output */
1589 if( i_frame_output == 1 )
1590 duration = (double)param->i_fps_den / param->i_fps_num;
1592 duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
1593 duration *= dts_compress_multiplier;
1595 i_end = x264_mdate();
1596 input.picture_clean( &pic );
1597 /* Erase progress indicator before printing encoding stats. */
1598 if( opt->b_progress )
1599 fprintf( stderr, " \r" );
1600 x264_encoder_close( h );
1601 fprintf( stderr, "\n" );
1604 fprintf( stderr, "aborted at input frame %d, output frame %d\n", opt->i_seek + i_frame, i_frame_output );
1606 input.close_file( opt->hin );
1607 output.close_file( opt->hout, largest_pts, second_largest_pts );
1609 if( i_frame_output > 0 )
1611 double fps = (double)i_frame_output * (double)1000000 /
1612 (double)( i_end - i_start );
1614 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame_output, fps,
1615 (double) i_file * 8 / ( 1000 * duration ) );