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 )
61 double timebase_convert_multiplier;
65 /* i/o file operation function pointer structs */
67 static cli_output_t output;
69 static const char * const demuxer_names[] =
86 static const char * const muxer_names[] =
98 static const char * const pulldown_names[] = { "none", "22", "32", "64", "double", "triple", "euro", 0 };
108 X264_PULLDOWN_22 = 1,
111 X264_PULLDOWN_DOUBLE,
112 X264_PULLDOWN_TRIPLE,
116 #define TB PIC_STRUCT_TOP_BOTTOM
117 #define BT PIC_STRUCT_BOTTOM_TOP
118 #define TBT PIC_STRUCT_TOP_BOTTOM_TOP
119 #define BTB PIC_STRUCT_BOTTOM_TOP_BOTTOM
121 static const cli_pulldown_t pulldown_values[] =
123 [X264_PULLDOWN_22] = {1, {TB}, 2.0},
124 [X264_PULLDOWN_32] = {4, {TBT, BT, BTB, TB}, 1.25},
125 [X264_PULLDOWN_64] = {2, {PIC_STRUCT_DOUBLE, PIC_STRUCT_TRIPLE}, 1.0},
126 [X264_PULLDOWN_DOUBLE] = {1, {PIC_STRUCT_DOUBLE}, 2.0},
127 [X264_PULLDOWN_TRIPLE] = {1, {PIC_STRUCT_TRIPLE}, 3.0},
128 [X264_PULLDOWN_EURO] = {24, {TBT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT,
129 BTB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB}, 25.0/24.0}
137 // indexed by pic_struct enum
138 static const float pulldown_frame_duration[10] = { 0.0, 1, 0.5, 0.5, 1, 1, 1.5, 1.5, 2, 3 };
140 static void Help( x264_param_t *defaults, int longhelp );
141 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
142 static int Encode( x264_param_t *param, cli_opt_t *opt );
144 /****************************************************************************
146 ****************************************************************************/
147 int main( int argc, char **argv )
153 #ifdef PTW32_STATIC_LIB
154 pthread_win32_process_attach_np();
155 pthread_win32_thread_attach_np();
159 _setmode(_fileno(stdin), _O_BINARY);
160 _setmode(_fileno(stdout), _O_BINARY);
163 /* Parse command line */
164 if( Parse( argc, argv, ¶m, &opt ) < 0 )
167 /* Control-C handler */
168 signal( SIGINT, SigIntHandler );
170 ret = Encode( ¶m, &opt );
172 #ifdef PTW32_STATIC_LIB
173 pthread_win32_thread_detach_np();
174 pthread_win32_process_detach_np();
180 static char const *strtable_lookup( const char * const table[], int index )
182 int i = 0; while( table[i] ) i++;
183 return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
186 static char *stringify_names( char *buf, const char * const names[] )
190 for( p[0] = 0; names[i]; i++ )
192 p += sprintf( p, "%s", names[i] );
194 p += sprintf( p, ", " );
199 /*****************************************************************************
201 *****************************************************************************/
202 static void Help( x264_param_t *defaults, int longhelp )
206 #define H1 if(longhelp>=1) printf
207 #define H2 if(longhelp==2) printf
208 H0( "x264 core:%d%s\n"
209 "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
211 "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
212 " or YUV4MPEG 4:2:0 (*.y4m),\n"
213 " or Avisynth if compiled with support (%s).\n"
214 " or libav* formats if compiled with lavf support (%s) or ffms support (%s).\n"
215 "Outfile type is selected by filename:\n"
216 " .264 -> Raw bytestream\n"
217 " .mkv -> Matroska\n"
218 " .flv -> Flash Video\n"
219 " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
223 " -h, --help List basic options\n"
224 " --longhelp List more options\n"
225 " --fullhelp List all options\n"
227 X264_BUILD, X264_VERSION,
249 H0( "Example usage:\n" );
251 H0( " Constant quality mode:\n" );
252 H0( " x264 --crf 24 -o <output> <input>\n" );
254 H0( " Two-pass with a bitrate of 1000kbps:\n" );
255 H0( " x264 --pass 1 --bitrate 1000 -o <output> <input>\n" );
256 H0( " x264 --pass 2 --bitrate 1000 -o <output> <input>\n" );
258 H0( " Lossless:\n" );
259 H0( " x264 --crf 0 -o <output> <input>\n" );
261 H0( " Maximum PSNR at the cost of speed and visual quality:\n" );
262 H0( " x264 --preset placebo --tune psnr -o <output> <input>\n" );
264 H0( " Constant bitrate at 1000kbps with a 2 second-buffer:\n");
265 H0( " x264 --vbv-bufsize 2000 --bitrate 1000 -o <output> <input>\n" );
269 H0( " --profile Force the limits of an H.264 profile [high]\n"
270 " Overrides all settings.\n" );
272 " --no-8x8dct --bframes 0 --no-cabac\n"
273 " --cqm flat --weightp 0 No interlaced\n"
276 " --no-8x8dct --cqm flat No lossless\n"
279 else H0( " - baseline,main,high\n" );
280 H0( " --preset Use a preset to select encoding settings [medium]\n"
281 " Overridden by user settings.\n" );
282 H2( " - ultrafast:\n"
283 " --no-8x8dct --aq-mode 0 --b-adapt 0\n"
284 " --bframes 0 --no-cabac --no-deblock\n"
285 " --no-mbtree --me dia --no-mixed-refs\n"
286 " --partitions none --ref 1 --scenecut 0\n"
287 " --subme 0 --trellis 0 --no-weightb\n"
290 " --no-mbtree --me dia --no-mixed-refs\n"
291 " --partitions i8x8,i4x4 --ref 1\n"
292 " --subme 1 --trellis 0 --weightp 0\n"
294 " --no-mbtree --no-mixed-refs --ref 2\n"
295 " --subme 4 --weightp 1\n"
297 " --rc-lookahead 30 --ref 2 --subme 6\n"
299 " Default settings apply.\n"
301 " --b-adapt 2 --direct auto --me umh\n"
302 " --rc-lookahead 50 --ref 5 --subme 8\n"
304 " --b-adapt 2 --direct auto --me umh\n"
305 " --partitions all --rc-lookahead 60\n"
306 " --ref 8 --subme 9 --trellis 2\n"
308 " --b-adapt 2 --bframes 8 --direct auto\n"
309 " --me umh --merange 24 --partitions all\n"
310 " --ref 16 --subme 10 --trellis 2\n"
311 " --rc-lookahead 60\n"
313 " --bframes 16 --b-adapt 2 --direct auto\n"
314 " --slow-firstpass --no-fast-pskip\n"
315 " --me tesa --merange 24 --partitions all\n"
316 " --rc-lookahead 60 --ref 16 --subme 10\n"
318 else H0( " - ultrafast,veryfast,faster,fast,medium\n"
319 " - slow,slower,veryslow,placebo\n" );
320 H0( " --tune Tune the settings for a particular type of source\n"
322 " Overridden by user settings.\n"
323 " Multiple tunings are separated by commas.\n"
324 " Only one psy tuning can be used at a time.\n" );
325 H2( " - film (psy tuning):\n"
326 " --deblock -1:-1 --psy-rd <unset>:0.15\n"
327 " - animation (psy tuning):\n"
328 " --bframes {+2} --deblock 1:1\n"
329 " --psy-rd 0.4:<unset> --aq-strength 0.6\n"
330 " --ref {Double if >1 else 1}\n"
331 " - grain (psy tuning):\n"
332 " --aq-strength 0.5 --no-dct-decimate\n"
333 " --deadzone-inter 6 --deadzone-intra 6\n"
334 " --deblock -2:-2 --ipratio 1.1 \n"
335 " --pbratio 1.1 --psy-rd <unset>:0.25\n"
337 " - psnr (psy tuning):\n"
338 " --aq-mode 0 --no-psy\n"
339 " - ssim (psy tuning):\n"
340 " --aq-mode 2 --no-psy\n"
342 " --no-cabac --no-deblock --no-weightb\n"
345 " --bframes 0 --force-cfr --rc-lookahead 0\n"
346 " --sync-lookahead 0 --sliced-threads\n" );
347 else H0( " - psy tunings: film,animation,grain,psnr,ssim\n"
348 " - other tunings: fastdecode,zerolatency\n" );
349 H1( " --slow-firstpass Don't use faster settings with --pass 1\n" );
351 H0( "Frame-type options:\n" );
353 H0( " -I, --keyint <integer> Maximum GOP size [%d]\n", defaults->i_keyint_max );
354 H2( " -i, --min-keyint <integer> Minimum GOP size [%d]\n", defaults->i_keyint_min );
355 H2( " --no-scenecut Disable adaptive I-frame decision\n" );
356 H2( " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
357 H2( " --intra-refresh Use Periodic Intra Refresh instead of IDR frames\n" );
358 H1( " -b, --bframes <integer> Number of B-frames between I and P [%d]\n", defaults->i_bframe );
359 H1( " --b-adapt <integer> Adaptive B-frame decision method [%d]\n"
360 " Higher values may lower threading efficiency.\n"
363 " - 2: Optimal (slow with high --bframes)\n", defaults->i_bframe_adaptive );
364 H2( " --b-bias <integer> Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
365 H1( " --b-pyramid <string> Keep some B-frames as references [%s]\n"
366 " - none: Disabled\n"
367 " - strict: Strictly hierarchical pyramid\n"
368 " - normal: Non-strict (not Blu-ray compatible)\n",
369 strtable_lookup( x264_b_pyramid_names, defaults->i_bframe_pyramid ) );
370 H1( " --no-cabac Disable CABAC\n" );
371 H1( " -r, --ref <integer> Number of reference frames [%d]\n", defaults->i_frame_reference );
372 H1( " --no-deblock Disable loop filter\n" );
373 H1( " -f, --deblock <alpha:beta> Loop filter parameters [%d:%d]\n",
374 defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
375 H2( " --slices <integer> Number of slices per frame; forces rectangular\n"
376 " slices and is overridden by other slicing options\n" );
377 else H1( " --slices <integer> Number of slices per frame\n" );
378 H2( " --slice-max-size <integer> Limit the size of each slice in bytes\n");
379 H2( " --slice-max-mbs <integer> Limit the size of each slice in macroblocks\n");
380 H0( " --tff Enable interlaced mode (top field first)\n" );
381 H0( " --bff Enable interlaced mode (bottom field first)\n" );
382 H2( " --constrained-intra Enable constrained intra prediction.\n" );
384 H0( "Ratecontrol:\n" );
386 H1( " -q, --qp <integer> Force constant QP (0-51, 0=lossless)\n" );
387 H0( " -B, --bitrate <integer> Set bitrate (kbit/s)\n" );
388 H0( " --crf <float> Quality-based VBR (0-51, 0=lossless) [%.1f]\n", defaults->rc.f_rf_constant );
389 H1( " --rc-lookahead <integer> Number of frames for frametype lookahead [%d]\n", defaults->rc.i_lookahead );
390 H0( " --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
391 H0( " --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
392 H2( " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
393 H2( " --qpmin <integer> Set min QP [%d]\n", defaults->rc.i_qp_min );
394 H2( " --qpmax <integer> Set max QP [%d]\n", defaults->rc.i_qp_max );
395 H2( " --qpstep <integer> Set max QP step [%d]\n", defaults->rc.i_qp_step );
396 H2( " --ratetol <float> Tolerance of ABR ratecontrol and VBV [%.1f]\n", defaults->rc.f_rate_tolerance );
397 H2( " --ipratio <float> QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
398 H2( " --pbratio <float> QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
399 H2( " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
400 H2( " --aq-mode <integer> AQ method [%d]\n"
402 " - 1: Variance AQ (complexity mask)\n"
403 " - 2: Auto-variance AQ (experimental)\n", defaults->rc.i_aq_mode );
404 H1( " --aq-strength <float> Reduces blocking and blurring in flat and\n"
405 " textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
407 H0( " -p, --pass <integer> Enable multipass ratecontrol\n"
408 " - 1: First pass, creates stats file\n"
409 " - 2: Last pass, does not overwrite stats file\n" );
410 H2( " - 3: Nth pass, overwrites stats file\n" );
411 H1( " --stats <string> Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
412 H2( " --no-mbtree Disable mb-tree ratecontrol.\n");
413 H2( " --qcomp <float> QP curve compression [%.2f]\n", defaults->rc.f_qcompress );
414 H2( " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
415 H2( " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
416 H2( " --zones <zone0>/<zone1>/... Tweak the bitrate of regions of the video\n" );
417 H2( " Each zone is of the form\n"
418 " <start frame>,<end frame>,<option>\n"
419 " where <option> is either\n"
420 " q=<integer> (force QP)\n"
421 " or b=<float> (bitrate multiplier)\n" );
422 H2( " --qpfile <string> Force frametypes and QPs for some or all frames\n"
423 " Format of each line: framenumber frametype QP\n"
424 " QP of -1 lets x264 choose. Frametypes: I,i,P,B,b.\n"
425 " QPs are restricted by qpmin/qpmax.\n" );
429 H1( " -A, --partitions <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
430 " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
432 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
433 H1( " --direct <string> Direct MV prediction mode [\"%s\"]\n"
434 " - none, spatial, temporal, auto\n",
435 strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
436 H2( " --no-weightb Disable weighted prediction for B-frames\n" );
437 H1( " --weightp <integer> Weighted prediction for P-frames [%d]\n"
439 " - 1: Blind offset\n"
440 " - 2: Smart analysis\n", defaults->analyse.i_weighted_pred );
441 H1( " --me <string> Integer pixel motion estimation method [\"%s\"]\n",
442 strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
443 H2( " - dia: diamond search, radius 1 (fast)\n"
444 " - hex: hexagonal search, radius 2\n"
445 " - umh: uneven multi-hexagon search\n"
446 " - esa: exhaustive search\n"
447 " - tesa: hadamard exhaustive search (slow)\n" );
448 else H1( " - dia, hex, umh\n" );
449 H2( " --merange <integer> Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
450 H2( " --mvrange <integer> Maximum motion vector length [-1 (auto)]\n" );
451 H2( " --mvrange-thread <int> Minimum buffer between threads [-1 (auto)]\n" );
452 H1( " -m, --subme <integer> Subpixel motion estimation and mode decision [%d]\n", defaults->analyse.i_subpel_refine );
453 H2( " - 0: fullpel only (not recommended)\n"
454 " - 1: SAD mode decision, one qpel iteration\n"
455 " - 2: SATD mode decision\n"
456 " - 3-5: Progressively more qpel\n"
457 " - 6: RD mode decision for I/P-frames\n"
458 " - 7: RD mode decision for all frames\n"
459 " - 8: RD refinement for I/P-frames\n"
460 " - 9: RD refinement for all frames\n"
461 " - 10: QP-RD - requires trellis=2, aq-mode>0\n" );
462 else H1( " decision quality: 1=fast, 10=best.\n" );
463 H1( " --psy-rd Strength of psychovisual optimization [\"%.1f:%.1f\"]\n"
464 " #1: RD (requires subme>=6)\n"
465 " #2: Trellis (requires trellis, experimental)\n",
466 defaults->analyse.f_psy_rd, defaults->analyse.f_psy_trellis );
467 H2( " --no-psy Disable all visual optimizations that worsen\n"
468 " both PSNR and SSIM.\n" );
469 H2( " --no-mixed-refs Don't decide references on a per partition basis\n" );
470 H2( " --no-chroma-me Ignore chroma in motion estimation\n" );
471 H1( " --no-8x8dct Disable adaptive spatial transform size\n" );
472 H1( " -t, --trellis <integer> Trellis RD quantization. Requires CABAC. [%d]\n"
474 " - 1: enabled only on the final encode of a MB\n"
475 " - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
476 H2( " --no-fast-pskip Disables early SKIP detection on P-frames\n" );
477 H2( " --no-dct-decimate Disables coefficient thresholding on P-frames\n" );
478 H1( " --nr <integer> Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
480 H2( " --deadzone-inter <int> Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
481 H2( " --deadzone-intra <int> Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
482 H2( " Deadzones should be in the range 0 - 32.\n" );
483 H2( " --cqm <string> Preset quant matrices [\"flat\"]\n"
485 H1( " --cqmfile <string> Read custom quant matrices from a JM-compatible file\n" );
486 H2( " Overrides any other --cqm* options.\n" );
487 H2( " --cqm4 <list> Set all 4x4 quant matrices\n"
488 " Takes a comma-separated list of 16 integers.\n" );
489 H2( " --cqm8 <list> Set all 8x8 quant matrices\n"
490 " Takes a comma-separated list of 64 integers.\n" );
491 H2( " --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
492 " Set both luma and chroma quant matrices\n" );
493 H2( " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
494 " Set individual quant matrices\n" );
496 H2( "Video Usability Info (Annex E):\n" );
497 H2( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
498 H2( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
500 H2( " --overscan <string> Specify crop overscan setting [\"%s\"]\n"
501 " - undef, show, crop\n",
502 strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ) );
503 H2( " --videoformat <string> Specify video format [\"%s\"]\n"
504 " - component, pal, ntsc, secam, mac, undef\n",
505 strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ) );
506 H2( " --fullrange <string> Specify full range samples setting [\"%s\"]\n"
508 strtable_lookup( x264_fullrange_names, defaults->vui.b_fullrange ) );
509 H2( " --colorprim <string> Specify color primaries [\"%s\"]\n"
510 " - undef, bt709, bt470m, bt470bg\n"
511 " smpte170m, smpte240m, film\n",
512 strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ) );
513 H2( " --transfer <string> Specify transfer characteristics [\"%s\"]\n"
514 " - undef, bt709, bt470m, bt470bg, linear,\n"
515 " log100, log316, smpte170m, smpte240m\n",
516 strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ) );
517 H2( " --colormatrix <string> Specify color matrix setting [\"%s\"]\n"
518 " - undef, bt709, fcc, bt470bg\n"
519 " smpte170m, smpte240m, GBR, YCgCo\n",
520 strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
521 H2( " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n",
522 defaults->vui.i_chroma_loc );
524 H2( " --nal-hrd <string> Signal HRD information (requires vbv-bufsize)\n"
525 " - none, vbr, cbr (cbr not allowed in .mp4)\n" );
526 H2( " --pic-struct Force pic_struct in Picture Timing SEI\n" );
529 H0( "Input/Output:\n" );
531 H0( " -o, --output Specify output file\n" );
532 H1( " --muxer <string> Specify output container format [\"%s\"]\n"
533 " - %s\n", muxer_names[0], stringify_names( buf, muxer_names ) );
534 H1( " --demuxer <string> Specify input container format [\"%s\"]\n"
535 " - %s\n", demuxer_names[0], stringify_names( buf, demuxer_names ) );
536 H1( " --index <string> Filename for input index file\n" );
537 H0( " --sar width:height Specify Sample Aspect Ratio\n" );
538 H0( " --fps <float|rational> Specify framerate\n" );
539 H0( " --seek <integer> First frame to encode\n" );
540 H0( " --frames <integer> Maximum number of frames to encode\n" );
541 H0( " --level <string> Specify level (as defined by Annex A)\n" );
543 H1( " -v, --verbose Print stats for each frame\n" );
544 H1( " --no-progress Don't show the progress indicator while encoding\n" );
545 H0( " --quiet Quiet Mode\n" );
546 H1( " --psnr Enable PSNR computation\n" );
547 H1( " --ssim Enable SSIM computation\n" );
548 H1( " --threads <integer> Force a specific number of threads\n" );
549 H2( " --sliced-threads Low-latency but lower-efficiency threading\n" );
550 H2( " --thread-input Run Avisynth in its own thread\n" );
551 H2( " --sync-lookahead <integer> Number of buffer frames for threaded lookahead\n" );
552 H2( " --non-deterministic Slightly improve quality of SMP, at the cost of repeatability\n" );
553 H2( " --asm <integer> Override CPU detection\n" );
554 H2( " --no-asm Disable all CPU optimizations\n" );
555 H2( " --visualize Show MB types overlayed on the encoded video\n" );
556 H2( " --dump-yuv <string> Save reconstructed frames\n" );
557 H2( " --sps-id <integer> Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
558 H2( " --aud Use access unit delimiters\n" );
559 H2( " --force-cfr Force constant framerate timestamp generation\n" );
560 H2( " --tcfile-in <string> Force timestamp generation with timecode file\n" );
561 H2( " --tcfile-out <string> Output timecode v2 file from input timestamps\n" );
562 H2( " --timebase <int/int> Specify timebase numerator and denominator\n"
563 " <integer> Specify timebase numerator for input timecode file\n"
564 " or specify timebase denominator for other input\n" );
565 H0( " --pulldown <string> Use soft pulldown to change frame rate\n"
566 " - none, 22, 32, 64, double, triple, euro (requires cfr input)\n" );
570 #define OPT_FRAMES 256
572 #define OPT_QPFILE 258
573 #define OPT_THREAD_INPUT 259
574 #define OPT_QUIET 260
575 #define OPT_NOPROGRESS 261
576 #define OPT_VISUALIZE 262
577 #define OPT_LONGHELP 263
578 #define OPT_PROFILE 264
579 #define OPT_PRESET 265
581 #define OPT_SLOWFIRSTPASS 267
582 #define OPT_FULLHELP 268
584 #define OPT_MUXER 270
585 #define OPT_DEMUXER 271
586 #define OPT_INDEX 272
587 #define OPT_INTERLACED 273
588 #define OPT_TCFILE_IN 274
589 #define OPT_TCFILE_OUT 275
590 #define OPT_TIMEBASE 276
591 #define OPT_PULLDOWN 277
593 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
594 static struct option long_options[] =
596 { "help", no_argument, NULL, 'h' },
597 { "longhelp", no_argument, NULL, OPT_LONGHELP },
598 { "fullhelp", no_argument, NULL, OPT_FULLHELP },
599 { "version", no_argument, NULL, 'V' },
600 { "profile", required_argument, NULL, OPT_PROFILE },
601 { "preset", required_argument, NULL, OPT_PRESET },
602 { "tune", required_argument, NULL, OPT_TUNE },
603 { "slow-firstpass", no_argument, NULL, OPT_SLOWFIRSTPASS },
604 { "bitrate", required_argument, NULL, 'B' },
605 { "bframes", required_argument, NULL, 'b' },
606 { "b-adapt", required_argument, NULL, 0 },
607 { "no-b-adapt", no_argument, NULL, 0 },
608 { "b-bias", required_argument, NULL, 0 },
609 { "b-pyramid", required_argument, NULL, 0 },
610 { "min-keyint", required_argument, NULL, 'i' },
611 { "keyint", required_argument, NULL, 'I' },
612 { "intra-refresh", no_argument, NULL, 0 },
613 { "scenecut", required_argument, NULL, 0 },
614 { "no-scenecut", no_argument, NULL, 0 },
615 { "nf", no_argument, NULL, 0 },
616 { "no-deblock", no_argument, NULL, 0 },
617 { "filter", required_argument, NULL, 0 },
618 { "deblock", required_argument, NULL, 'f' },
619 { "interlaced", no_argument, NULL, OPT_INTERLACED },
620 { "tff", no_argument, NULL, OPT_INTERLACED },
621 { "bff", no_argument, NULL, OPT_INTERLACED },
622 { "no-interlaced", no_argument, NULL, OPT_INTERLACED },
623 { "constrained-intra", no_argument, NULL, 0 },
624 { "cabac", no_argument, NULL, 0 },
625 { "no-cabac", no_argument, NULL, 0 },
626 { "qp", required_argument, NULL, 'q' },
627 { "qpmin", required_argument, NULL, 0 },
628 { "qpmax", required_argument, NULL, 0 },
629 { "qpstep", required_argument, NULL, 0 },
630 { "crf", required_argument, NULL, 0 },
631 { "rc-lookahead",required_argument, NULL, 0 },
632 { "ref", required_argument, NULL, 'r' },
633 { "asm", required_argument, NULL, 0 },
634 { "no-asm", no_argument, NULL, 0 },
635 { "sar", required_argument, NULL, 0 },
636 { "fps", required_argument, NULL, OPT_FPS },
637 { "frames", required_argument, NULL, OPT_FRAMES },
638 { "seek", required_argument, NULL, OPT_SEEK },
639 { "output", required_argument, NULL, 'o' },
640 { "muxer", required_argument, NULL, OPT_MUXER },
641 { "demuxer", required_argument, NULL, OPT_DEMUXER },
642 { "stdout", required_argument, NULL, OPT_MUXER },
643 { "stdin", required_argument, NULL, OPT_DEMUXER },
644 { "index", required_argument, NULL, OPT_INDEX },
645 { "analyse", required_argument, NULL, 0 },
646 { "partitions", required_argument, NULL, 'A' },
647 { "direct", required_argument, NULL, 0 },
648 { "weightb", no_argument, NULL, 'w' },
649 { "no-weightb", no_argument, NULL, 0 },
650 { "weightp", required_argument, NULL, 0 },
651 { "me", required_argument, NULL, 0 },
652 { "merange", required_argument, NULL, 0 },
653 { "mvrange", required_argument, NULL, 0 },
654 { "mvrange-thread", required_argument, NULL, 0 },
655 { "subme", required_argument, NULL, 'm' },
656 { "psy-rd", required_argument, NULL, 0 },
657 { "no-psy", no_argument, NULL, 0 },
658 { "psy", no_argument, NULL, 0 },
659 { "mixed-refs", no_argument, NULL, 0 },
660 { "no-mixed-refs", no_argument, NULL, 0 },
661 { "no-chroma-me", no_argument, NULL, 0 },
662 { "8x8dct", no_argument, NULL, 0 },
663 { "no-8x8dct", no_argument, NULL, 0 },
664 { "trellis", required_argument, NULL, 't' },
665 { "fast-pskip", no_argument, NULL, 0 },
666 { "no-fast-pskip", no_argument, NULL, 0 },
667 { "no-dct-decimate", no_argument, NULL, 0 },
668 { "aq-strength", required_argument, NULL, 0 },
669 { "aq-mode", required_argument, NULL, 0 },
670 { "deadzone-inter", required_argument, NULL, '0' },
671 { "deadzone-intra", required_argument, NULL, '0' },
672 { "level", required_argument, NULL, 0 },
673 { "ratetol", required_argument, NULL, 0 },
674 { "vbv-maxrate", required_argument, NULL, 0 },
675 { "vbv-bufsize", required_argument, NULL, 0 },
676 { "vbv-init", required_argument, NULL, 0 },
677 { "ipratio", required_argument, NULL, 0 },
678 { "pbratio", required_argument, NULL, 0 },
679 { "chroma-qp-offset", required_argument, NULL, 0 },
680 { "pass", required_argument, NULL, 'p' },
681 { "stats", required_argument, NULL, 0 },
682 { "qcomp", required_argument, NULL, 0 },
683 { "mbtree", no_argument, NULL, 0 },
684 { "no-mbtree", no_argument, NULL, 0 },
685 { "qblur", required_argument, NULL, 0 },
686 { "cplxblur", required_argument, NULL, 0 },
687 { "zones", required_argument, NULL, 0 },
688 { "qpfile", required_argument, NULL, OPT_QPFILE },
689 { "threads", required_argument, NULL, 0 },
690 { "sliced-threads", no_argument, NULL, 0 },
691 { "no-sliced-threads", no_argument, NULL, 0 },
692 { "slice-max-size", required_argument, NULL, 0 },
693 { "slice-max-mbs", required_argument, NULL, 0 },
694 { "slices", required_argument, NULL, 0 },
695 { "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
696 { "sync-lookahead", required_argument, NULL, 0 },
697 { "non-deterministic", no_argument, NULL, 0 },
698 { "psnr", no_argument, NULL, 0 },
699 { "ssim", no_argument, NULL, 0 },
700 { "quiet", no_argument, NULL, OPT_QUIET },
701 { "verbose", no_argument, NULL, 'v' },
702 { "no-progress", no_argument, NULL, OPT_NOPROGRESS },
703 { "visualize", no_argument, NULL, OPT_VISUALIZE },
704 { "dump-yuv", required_argument, NULL, 0 },
705 { "sps-id", required_argument, NULL, 0 },
706 { "aud", no_argument, NULL, 0 },
707 { "nr", required_argument, NULL, 0 },
708 { "cqm", required_argument, NULL, 0 },
709 { "cqmfile", required_argument, NULL, 0 },
710 { "cqm4", required_argument, NULL, 0 },
711 { "cqm4i", required_argument, NULL, 0 },
712 { "cqm4iy", required_argument, NULL, 0 },
713 { "cqm4ic", required_argument, NULL, 0 },
714 { "cqm4p", required_argument, NULL, 0 },
715 { "cqm4py", required_argument, NULL, 0 },
716 { "cqm4pc", required_argument, NULL, 0 },
717 { "cqm8", required_argument, NULL, 0 },
718 { "cqm8i", required_argument, NULL, 0 },
719 { "cqm8p", required_argument, NULL, 0 },
720 { "overscan", required_argument, NULL, 0 },
721 { "videoformat", required_argument, NULL, 0 },
722 { "fullrange", required_argument, NULL, 0 },
723 { "colorprim", required_argument, NULL, 0 },
724 { "transfer", required_argument, NULL, 0 },
725 { "colormatrix", required_argument, NULL, 0 },
726 { "chromaloc", required_argument, NULL, 0 },
727 { "force-cfr", no_argument, NULL, 0 },
728 { "tcfile-in", required_argument, NULL, OPT_TCFILE_IN },
729 { "tcfile-out", required_argument, NULL, OPT_TCFILE_OUT },
730 { "timebase", required_argument, NULL, OPT_TIMEBASE },
731 { "pic-struct", no_argument, NULL, 0 },
732 { "nal-hrd", required_argument, NULL, 0 },
733 { "pulldown", required_argument, NULL, OPT_PULLDOWN },
737 static int select_output( const char *muxer, char *filename, x264_param_t *param )
739 const char *ext = get_filename_extension( filename );
740 if( !strcmp( filename, "-" ) || strcasecmp( muxer, "auto" ) )
743 if( !strcasecmp( ext, "mp4" ) )
748 param->b_dts_compress = 0;
749 param->b_repeat_headers = 0;
750 if( param->i_nal_hrd == X264_NAL_HRD_CBR )
752 fprintf( stderr, "x264 [warning]: cbr nal-hrd is not compatible with mp4\n" );
753 param->i_nal_hrd = X264_NAL_HRD_VBR;
756 fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
760 else if( !strcasecmp( ext, "mkv" ) )
764 param->b_dts_compress = 0;
765 param->b_repeat_headers = 0;
767 else if( !strcasecmp( ext, "flv" ) )
771 param->b_dts_compress = 1;
772 param->b_repeat_headers = 0;
779 static int select_input( const char *demuxer, char *used_demuxer, char *filename,
780 hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
782 const char *ext = get_filename_extension( filename );
783 int b_regular = strcmp( filename, "-" );
784 int b_auto = !strcasecmp( demuxer, "auto" );
785 if( !b_regular && b_auto )
789 FILE *f = fopen( filename, "r" );
792 b_regular = x264_is_regular_file( f );
796 const char *module = b_auto ? ext : demuxer;
798 if( !strcasecmp( module, "avs" ) || !strcasecmp( ext, "d2v" ) || !strcasecmp( ext, "dga" ) )
804 fprintf( stderr, "x264 [error]: not compiled with AVS input support\n" );
808 else if( !strcasecmp( module, "y4m" ) )
810 else if( !strcasecmp( module, "yuv" ) )
815 if( b_regular && (b_auto || !strcasecmp( demuxer, "ffms" )) &&
816 !ffms_input.open_file( filename, p_handle, info, opt ) )
824 if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
825 !lavf_input.open_file( filename, p_handle, info, opt ) )
833 if( b_regular && (b_auto || !strcasecmp( demuxer, "avs" )) &&
834 !avs_input.open_file( filename, p_handle, info, opt ) )
841 if( b_auto && !yuv_input.open_file( filename, p_handle, info, opt ) )
850 fprintf( stderr, "x264 [error]: could not open input file `%s' via any method!\n", filename );
854 strcpy( used_demuxer, module );
859 /*****************************************************************************
861 *****************************************************************************/
862 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
864 char *input_filename = NULL;
865 const char *demuxer = demuxer_names[0];
866 char *output_filename = NULL;
867 const char *muxer = muxer_names[0];
868 char *tcfile_name = NULL;
869 x264_param_t defaults;
870 char *profile = NULL;
871 int b_thread_input = 0;
875 int b_user_interlaced = 0;
877 cli_input_opt_t input_opt;
881 x264_param_default( &defaults );
883 memset( opt, 0, sizeof(cli_opt_t) );
884 memset( &input_opt, 0, sizeof(cli_input_opt_t) );
887 /* Presets are applied before all other options. */
890 int c = getopt_long( argc, argv, short_options, long_options, NULL );
893 if( c == OPT_PRESET )
901 if( preset && !strcasecmp( preset, "placebo" ) )
904 if( x264_param_default_preset( param, preset, tune ) < 0 )
907 /* Parse command line options */
911 int long_options_index = -1;
913 int c = getopt_long( argc, argv, short_options, long_options, &long_options_index );
923 Help( &defaults, 0 );
926 Help( &defaults, 1 );
929 Help( &defaults, 2 );
933 printf( "x264 "X264_POINTVER"\n" );
935 printf( "x264 0.%d.X\n", X264_BUILD );
937 printf( "built on " __DATE__ ", " );
939 printf( "gcc: " __VERSION__ "\n" );
941 printf( "using a non-gcc compiler\n" );
945 param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
948 opt->i_seek = input_opt.seek = X264_MAX( atoi( optarg ), 0 );
951 output_filename = optarg;
954 for( i = 0; muxer_names[i] && strcasecmp( muxer_names[i], optarg ); )
956 if( !muxer_names[i] )
958 fprintf( stderr, "x264 [error]: invalid muxer '%s'\n", optarg );
964 for( i = 0; demuxer_names[i] && strcasecmp( demuxer_names[i], optarg ); )
966 if( !demuxer_names[i] )
968 fprintf( stderr, "x264 [error]: invalid demuxer '%s'\n", optarg );
974 input_opt.index = optarg;
977 opt->qpfile = fopen( optarg, "rb" );
980 fprintf( stderr, "x264 [error]: can't open qpfile `%s'\n", optarg );
983 else if( !x264_is_regular_file( opt->qpfile ) )
985 fprintf( stderr, "x264 [error]: qpfile incompatible with non-regular file `%s'\n", optarg );
986 fclose( opt->qpfile );
990 case OPT_THREAD_INPUT:
994 param->i_log_level = X264_LOG_NONE;
997 param->i_log_level = X264_LOG_DEBUG;
1000 opt->b_progress = 0;
1004 param->b_visualize = 1;
1005 b_exit_on_ctrl_c = 1;
1007 fprintf( stderr, "x264 [warning]: not compiled with visualization support\n" );
1016 case OPT_SLOWFIRSTPASS:
1021 goto generic_option;
1024 param->b_vfr_input = 0;
1025 goto generic_option;
1026 case OPT_INTERLACED:
1027 b_user_interlaced = 1;
1028 goto generic_option;
1030 tcfile_name = optarg;
1032 case OPT_TCFILE_OUT:
1033 opt->tcfile_out = fopen( optarg, "wb" );
1034 if( !opt->tcfile_out )
1036 fprintf( stderr, "x264 [error]: can't open `%s'\n", optarg );
1041 input_opt.timebase = optarg;
1044 for( i = 0; pulldown_names[i] && strcasecmp( pulldown_names[i], optarg ); )
1046 if( !pulldown_names[i] )
1048 fprintf( stderr, "x264 [error]: invalid pulldown '%s'\n", optarg );
1051 opt->i_pulldown = i;
1057 if( long_options_index < 0 )
1059 for( i = 0; long_options[i].name; i++ )
1060 if( long_options[i].val == c )
1062 long_options_index = i;
1065 if( long_options_index < 0 )
1067 /* getopt_long already printed an error message */
1072 b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg );
1078 const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
1079 fprintf( stderr, "x264 [error]: invalid argument: %s = %s\n", name, optarg );
1084 /* If first pass mode is used, apply faster settings. */
1086 x264_param_apply_fastfirstpass( param );
1088 /* Apply profile restrictions. */
1089 if( x264_param_apply_profile( param, profile ) < 0 )
1092 /* Get the file name */
1093 if( optind > argc - 1 || !output_filename )
1095 fprintf( stderr, "x264 [error]: No %s file. Run x264 --help for a list of options.\n",
1096 optind > argc - 1 ? "input" : "output" );
1100 if( select_output( muxer, output_filename, param ) )
1102 if( output.open_file( output_filename, &opt->hout ) )
1104 fprintf( stderr, "x264 [error]: could not open output file `%s'\n", output_filename );
1108 input_filename = argv[optind++];
1109 input_opt.resolution = optind < argc ? argv[optind++] : NULL;
1110 video_info_t info = {0};
1111 char demuxername[5];
1113 /* set info flags to param flags to be overwritten by demuxer as necessary. */
1114 info.csp = param->i_csp;
1115 info.fps_num = param->i_fps_num;
1116 info.fps_den = param->i_fps_den;
1117 info.interlaced = param->b_interlaced;
1118 info.sar_width = param->vui.i_sar_width;
1119 info.sar_height = param->vui.i_sar_height;
1120 info.vfr = param->b_vfr_input;
1122 if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
1125 if( !opt->hin && input.open_file( input_filename, &opt->hin, &info, &input_opt ) )
1127 fprintf( stderr, "x264 [error]: could not open input file `%s'\n", input_filename );
1131 x264_reduce_fraction( &info.sar_width, &info.sar_height );
1132 x264_reduce_fraction( &info.fps_num, &info.fps_den );
1133 if( param->i_log_level >= X264_LOG_INFO )
1134 fprintf( stderr, "%s [info]: %dx%d%c %d:%d @ %d/%d fps (%cfr)\n", demuxername, info.width,
1135 info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
1136 info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
1142 fprintf( stderr, "x264 [error]: --fps + --tcfile-in is incompatible.\n" );
1145 if( timecode_input.open_file( tcfile_name, &opt->hin, &info, &input_opt ) )
1147 fprintf( stderr, "x264 [error]: timecode input failed\n" );
1151 input = timecode_input;
1153 else if( !info.vfr && input_opt.timebase )
1155 fprintf( stderr, "x264 [error]: --timebase is incompatible with cfr input\n" );
1159 /* set param flags from the info flags as necessary */
1160 param->i_csp = info.csp;
1161 param->i_height = info.height;
1162 param->b_vfr_input = info.vfr;
1163 param->i_width = info.width;
1164 if( !b_user_interlaced && info.interlaced )
1166 fprintf( stderr, "x264 [warning]: input appears to be interlaced, enabling interlaced mode.\n"
1167 " If you want otherwise, use --no-interlaced\n" );
1168 param->b_interlaced = 1;
1172 param->i_fps_num = info.fps_num;
1173 param->i_fps_den = info.fps_den;
1175 if( param->b_vfr_input )
1177 param->i_timebase_num = info.timebase_num;
1178 param->i_timebase_den = info.timebase_den;
1182 param->i_timebase_num = param->i_fps_den;
1183 param->i_timebase_den = param->i_fps_num;
1185 if( !tcfile_name && input_opt.timebase )
1187 int i_user_timebase_num;
1188 int i_user_timebase_den;
1189 int ret = sscanf( input_opt.timebase, "%d/%d", &i_user_timebase_num, &i_user_timebase_den );
1192 fprintf( stderr, "x264 [error]: invalid argument: timebase = %s\n", input_opt.timebase );
1197 i_user_timebase_num = param->i_timebase_num;
1198 i_user_timebase_den = atoi( input_opt.timebase );
1200 opt->timebase_convert_multiplier = ((double)i_user_timebase_den / param->i_timebase_den)
1201 * ((double)param->i_timebase_num / i_user_timebase_num);
1202 if( opt->timebase_convert_multiplier < 1 )
1204 fprintf( stderr, "x264 [error]: timebase you specified will generate nonmonotonic pts: %d/%d\n",
1205 i_user_timebase_num, i_user_timebase_den );
1208 param->i_timebase_num = i_user_timebase_num;
1209 param->i_timebase_den = i_user_timebase_den;
1210 param->b_vfr_input = 1;
1212 if( !param->vui.i_sar_width || !param->vui.i_sar_height )
1214 param->vui.i_sar_width = info.sar_width;
1215 param->vui.i_sar_height = info.sar_height;
1219 if( b_thread_input || param->i_threads > 1
1220 || (param->i_threads == X264_THREADS_AUTO && x264_cpu_num_processors() > 1) )
1222 if( thread_input.open_file( NULL, &opt->hin, &info, NULL ) )
1224 fprintf( stderr, "x264 [error]: threaded input failed\n" );
1228 input = thread_input;
1233 /* Automatically reduce reference frame count to match the user's target level
1234 * if the user didn't explicitly set a reference frame count. */
1237 int mbs = (((param->i_width)+15)>>4) * (((param->i_height)+15)>>4);
1239 for( i = 0; x264_levels[i].level_idc != 0; i++ )
1240 if( param->i_level_idc == x264_levels[i].level_idc )
1242 while( mbs * 384 * param->i_frame_reference > x264_levels[i].dpb
1243 && param->i_frame_reference > 1 )
1245 param->i_frame_reference--;
1255 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
1257 int num = -1, qp, ret;
1260 while( num < i_frame )
1262 file_pos = ftell( opt->qpfile );
1263 ret = fscanf( opt->qpfile, "%d %c %d\n", &num, &type, &qp );
1264 if( num > i_frame || ret == EOF )
1266 pic->i_type = X264_TYPE_AUTO;
1268 fseek( opt->qpfile, file_pos, SEEK_SET );
1271 if( num < i_frame && ret == 3 )
1273 pic->i_qpplus1 = qp+1;
1274 if ( type == 'I' ) pic->i_type = X264_TYPE_IDR;
1275 else if( type == 'i' ) pic->i_type = X264_TYPE_I;
1276 else if( type == 'P' ) pic->i_type = X264_TYPE_P;
1277 else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
1278 else if( type == 'b' ) pic->i_type = X264_TYPE_B;
1280 if( ret != 3 || qp < -1 || qp > 51 )
1282 fprintf( stderr, "x264 [error]: can't parse qpfile for frame %d\n", i_frame );
1283 fclose( opt->qpfile );
1285 pic->i_type = X264_TYPE_AUTO;
1292 /*****************************************************************************
1294 *****************************************************************************/
1296 static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_pts )
1298 x264_picture_t pic_out;
1301 int i_frame_size = 0;
1303 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
1305 if( i_frame_size < 0 )
1307 fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
1313 i_frame_size = output.write_frame( hout, nal[0].p_payload, i_frame_size, &pic_out );
1314 *last_pts = pic_out.i_pts;
1317 return i_frame_size;
1320 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 )
1323 int64_t i_elapsed = x264_mdate() - i_start;
1324 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1325 double bitrate = (double) i_file * 8 / ( (double) last_pts * 1000 * param->i_timebase_num / param->i_timebase_den );
1328 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1329 sprintf( buf, "x264 [%.1f%%] %d/%d frames, %.2f fps, %.2f kb/s, eta %d:%02d:%02d",
1330 100. * i_frame / i_frame_total, i_frame, i_frame_total, fps, bitrate,
1331 eta/3600, (eta/60)%60, eta%60 );
1335 sprintf( buf, "x264 %d frames: %.2f fps, %.2f kb/s", i_frame, fps, bitrate );
1337 fprintf( stderr, "%s \r", buf+5 );
1338 SetConsoleTitle( buf );
1339 fflush( stderr ); // needed in windows
1342 static int Encode( x264_param_t *param, cli_opt_t *opt )
1346 const cli_pulldown_t *pulldown = NULL; // shut up gcc
1348 int i_frame, i_frame_total, i_frame_output;
1349 int64_t i_start, i_end;
1352 int i_update_interval;
1353 int64_t last_pts = 0;
1354 # define MAX_PTS_WARNING 3 /* arbitrary */
1355 int pts_warning_cnt = 0;
1356 int64_t largest_pts = -1;
1357 int64_t second_largest_pts = -1;
1358 int64_t ticks_per_frame;
1360 int prev_timebase_den = param->i_timebase_den / gcd( param->i_timebase_num, param->i_timebase_den );
1361 int dts_compress_multiplier;
1362 double pulldown_pts = 0;
1364 opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
1365 i_frame_total = input.get_frame_total( opt->hin );
1366 i_frame_total = X264_MAX( i_frame_total - opt->i_seek, 0 );
1367 if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
1368 && param->i_frame_total > 0 )
1369 i_frame_total = param->i_frame_total;
1370 param->i_frame_total = i_frame_total;
1371 i_update_interval = i_frame_total ? x264_clip3( i_frame_total / 1000, 1, 10 ) : 10;
1373 /* set up pulldown */
1374 if( opt->i_pulldown && !param->b_vfr_input )
1376 param->b_pic_struct = 1;
1377 pulldown = &pulldown_values[opt->i_pulldown];
1378 param->i_timebase_num = param->i_fps_den;
1379 if( fmod( param->i_fps_num * pulldown->fps_factor, 1 ) )
1381 fprintf( stderr, "x264 [error]: unsupported framerate for chosen pulldown\n" );
1384 param->i_timebase_den = param->i_fps_num * pulldown->fps_factor;
1387 if( ( h = x264_encoder_open( param ) ) == NULL )
1389 fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
1390 input.close_file( opt->hin );
1394 x264_encoder_parameters( h, param );
1396 dts_compress_multiplier = param->i_timebase_den / prev_timebase_den;
1398 if( output.set_param( opt->hout, param ) )
1400 fprintf( stderr, "x264 [error]: can't set outfile param\n" );
1401 input.close_file( opt->hin );
1402 output.close_file( opt->hout, largest_pts, second_largest_pts );
1406 /* Create a new pic */
1407 if( input.picture_alloc( &pic, param->i_csp, param->i_width, param->i_height ) )
1409 fprintf( stderr, "x264 [error]: malloc failed\n" );
1413 i_start = x264_mdate();
1414 /* ticks/frame = ticks/second / frames/second */
1415 ticks_per_frame = (int64_t)param->i_timebase_den * param->i_fps_den / param->i_timebase_num / param->i_fps_num;
1416 if( ticks_per_frame < 1 )
1418 fprintf( stderr, "x264 [error]: ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame );
1422 if( !param->b_repeat_headers )
1424 // Write SPS/PPS/SEI
1425 x264_nal_t *headers;
1428 if( x264_encoder_headers( h, &headers, &i_nal ) < 0 )
1430 fprintf( stderr, "x264 [error]: x264_encoder_headers failed\n" );
1434 if( (i_file = output.write_headers( opt->hout, headers )) < 0 )
1438 if( opt->tcfile_out )
1439 fprintf( opt->tcfile_out, "# timecode format v2\n" );
1442 for( i_frame = 0, i_frame_output = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
1444 if( input.read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
1447 if( !param->b_vfr_input )
1448 pic.i_pts = i_frame;
1450 if( opt->i_pulldown && !param->b_vfr_input )
1452 pic.i_pic_struct = pulldown->pattern[ i_frame % pulldown->mod ];
1453 pic.i_pts = (int64_t)( pulldown_pts + 0.5 );
1454 pulldown_pts += pulldown_frame_duration[pic.i_pic_struct];
1456 else if( opt->timebase_convert_multiplier )
1457 pic.i_pts = (int64_t)( pic.i_pts * opt->timebase_convert_multiplier + 0.5 );
1459 int64_t output_pts = pic.i_pts * dts_compress_multiplier; /* pts libx264 returns */
1461 if( pic.i_pts <= largest_pts )
1463 if( param->i_log_level >= X264_LOG_WARNING )
1465 if( param->i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
1466 fprintf( stderr, "x264 [warning]: non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
1467 i_frame, output_pts, largest_pts * dts_compress_multiplier );
1468 else if( pts_warning_cnt == MAX_PTS_WARNING )
1469 fprintf( stderr, "x264 [warning]: too many nonmonotonic pts warnings, suppressing further ones\n" );
1472 pic.i_pts = largest_pts + ticks_per_frame;
1473 output_pts = pic.i_pts * dts_compress_multiplier;
1476 second_largest_pts = largest_pts;
1477 largest_pts = pic.i_pts;
1478 if( opt->tcfile_out )
1479 fprintf( opt->tcfile_out, "%.6f\n", output_pts * ((double)param->i_timebase_num / param->i_timebase_den) * 1e3 );
1482 parse_qpfile( opt, &pic, i_frame + opt->i_seek );
1485 /* Do not force any parameters */
1486 pic.i_type = X264_TYPE_AUTO;
1490 i_frame_size = Encode_frame( h, opt->hout, &pic, &last_pts );
1491 if( i_frame_size < 0 )
1493 i_file += i_frame_size;
1499 if( input.release_frame && input.release_frame( &pic, opt->hin ) )
1502 /* update status line (up to 1000 times per input file) */
1503 if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
1504 Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
1506 /* Flush delayed frames */
1507 while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
1509 i_frame_size = Encode_frame( h, opt->hout, NULL, &last_pts );
1510 if( i_frame_size < 0 )
1512 i_file += i_frame_size;
1515 if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
1516 Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
1518 if( pts_warning_cnt >= MAX_PTS_WARNING && param->i_log_level < X264_LOG_DEBUG )
1519 fprintf( stderr, "x264 [warning]: %d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
1521 /* duration algorithm fails when only 1 frame is output */
1522 if( i_frame_output == 1 )
1523 duration = (double)param->i_fps_den / param->i_fps_num;
1525 duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
1526 if( !(opt->i_pulldown && !param->b_vfr_input) )
1527 duration *= dts_compress_multiplier;
1529 i_end = x264_mdate();
1530 input.picture_clean( &pic );
1531 /* Erase progress indicator before printing encoding stats. */
1532 if( opt->b_progress )
1533 fprintf( stderr, " \r" );
1534 x264_encoder_close( h );
1535 fprintf( stderr, "\n" );
1538 fprintf( stderr, "aborted at input frame %d, output frame %d\n", opt->i_seek + i_frame, i_frame_output );
1540 if( opt->tcfile_out )
1542 fclose( opt->tcfile_out );
1543 opt->tcfile_out = NULL;
1546 input.close_file( opt->hin );
1547 output.close_file( opt->hout, largest_pts, second_largest_pts );
1549 if( i_frame_output > 0 )
1551 double fps = (double)i_frame_output * (double)1000000 /
1552 (double)( i_end - i_start );
1554 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame_output, fps,
1555 (double) i_file * 8 / ( 1000 * duration ) );