1 /*****************************************************************************
2 * x264: h264 encoder testing program.
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: x264.c,v 1.1 2004/06/03 19:24:12 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
31 #include "common/common.h"
39 uint8_t *mux_buffer = NULL;
40 int mux_buffer_size = 0;
43 static int b_ctrl_c = 0;
44 static int b_exit_on_ctrl_c = 0;
45 static void SigIntHandler( int a )
47 if( b_exit_on_ctrl_c )
60 /* input file operation function pointers */
61 int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
62 int (*p_get_frame_total)( hnd_t handle );
63 int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame );
64 int (*p_close_infile)( hnd_t handle );
66 /* output file operation function pointers */
67 static int (*p_open_outfile)( char *psz_filename, hnd_t *p_handle );
68 static int (*p_set_outfile_param)( hnd_t handle, x264_param_t *p_param );
69 static int (*p_write_nalu)( hnd_t handle, uint8_t *p_nal, int i_size );
70 static int (*p_set_eop)( hnd_t handle, x264_picture_t *p_picture );
71 static int (*p_close_outfile)( hnd_t handle );
73 static void Help( x264_param_t *defaults, int b_longhelp );
74 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
75 static int Encode( x264_param_t *param, cli_opt_t *opt );
78 /****************************************************************************
80 ****************************************************************************/
81 int main( int argc, char **argv )
87 #ifdef PTW32_STATIC_LIB
88 pthread_win32_process_attach_np();
89 pthread_win32_thread_attach_np();
93 _setmode(_fileno(stdin), _O_BINARY);
94 _setmode(_fileno(stdout), _O_BINARY);
97 x264_param_default( ¶m );
99 /* Parse command line */
100 if( Parse( argc, argv, ¶m, &opt ) < 0 )
103 /* Control-C handler */
104 signal( SIGINT, SigIntHandler );
106 ret = Encode( ¶m, &opt );
108 #ifdef PTW32_STATIC_LIB
109 pthread_win32_thread_detach_np();
110 pthread_win32_process_detach_np();
116 static char const *strtable_lookup( const char * const table[], int index )
118 int i = 0; while( table[i] ) i++;
119 return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
122 /*****************************************************************************
124 *****************************************************************************/
125 static void Help( x264_param_t *defaults, int b_longhelp )
128 #define H1 if(b_longhelp) printf
129 H0( "x264 core:%d%s\n"
130 "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
132 "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
133 " or YUV4MPEG 4:2:0 (*.y4m),\n"
134 " or AVI or Avisynth if compiled with AVIS support (%s).\n"
135 "Outfile type is selected by filename:\n"
136 " .264 -> Raw bytestream\n"
137 " .mkv -> Matroska\n"
138 " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
142 " -h, --help List the more commonly used options\n"
143 " --longhelp List all options\n"
145 X264_BUILD, X264_VERSION,
157 H0( "Frame-type options:\n" );
159 H0( " -I, --keyint <integer> Maximum GOP size [%d]\n", defaults->i_keyint_max );
160 H1( " -i, --min-keyint <integer> Minimum GOP size [%d]\n", defaults->i_keyint_min );
161 H1( " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
162 H1( " --pre-scenecut Faster, less precise scenecut detection.\n"
163 " Required and implied by multi-threading.\n" );
164 H0( " -b, --bframes <integer> Number of B-frames between I and P [%d]\n", defaults->i_bframe );
165 H1( " --no-b-adapt Disable adaptive B-frame decision\n" );
166 H1( " --b-bias <integer> Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
167 H0( " --b-pyramid Keep some B-frames as references\n" );
168 H0( " --no-cabac Disable CABAC\n" );
169 H0( " -r, --ref <integer> Number of reference frames [%d]\n", defaults->i_frame_reference );
170 H1( " --no-deblock Disable loop filter\n" );
171 H0( " -f, --deblock <alpha:beta> Loop filter AlphaC0 and Beta parameters [%d:%d]\n",
172 defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
173 H0( " --interlaced Enable pure-interlaced mode\n" );
175 H0( "Ratecontrol:\n" );
177 H0( " -q, --qp <integer> Set QP (0=lossless) [%d]\n", defaults->rc.i_qp_constant );
178 H0( " -B, --bitrate <integer> Set bitrate (kbit/s)\n" );
179 H0( " --crf <float> Quality-based VBR (nominal QP)\n" );
180 H1( " --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
181 H0( " --vbv-bufsize <integer> Enable CBR and set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
182 H1( " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
183 H1( " --qpmin <integer> Set min QP [%d]\n", defaults->rc.i_qp_min );
184 H1( " --qpmax <integer> Set max QP [%d]\n", defaults->rc.i_qp_max );
185 H1( " --qpstep <integer> Set max QP step [%d]\n", defaults->rc.i_qp_step );
186 H0( " --ratetol <float> Allowed variance of average bitrate [%.1f]\n", defaults->rc.f_rate_tolerance );
187 H0( " --ipratio <float> QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
188 H0( " --pbratio <float> QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
189 H1( " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
191 H0( " -p, --pass <1|2|3> Enable multipass ratecontrol\n"
192 " - 1: First pass, creates stats file\n"
193 " - 2: Last pass, does not overwrite stats file\n"
194 " - 3: Nth pass, overwrites stats file\n" );
195 H0( " --stats <string> Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
196 H1( " --rceq <string> Ratecontrol equation [\"%s\"]\n", defaults->rc.psz_rc_eq );
197 H0( " --qcomp <float> QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n", defaults->rc.f_qcompress );
198 H1( " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
199 H1( " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
200 H0( " --zones <zone0>/<zone1>/... Tweak the bitrate of some regions of the video\n" );
201 H1( " Each zone is of the form\n"
202 " <start frame>,<end frame>,<option>\n"
203 " where <option> is either\n"
204 " q=<integer> (force QP)\n"
205 " or b=<float> (bitrate multiplier)\n" );
206 H1( " --qpfile <string> Force frametypes and QPs\n" );
210 H0( " -A, --partitions <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
211 " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
213 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
214 H0( " --direct <string> Direct MV prediction mode [\"%s\"]\n"
215 " - none, spatial, temporal, auto\n",
216 strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
217 H1( " --direct-8x8 <-1|0|1> Direct prediction size [%d]\n"
220 " - -1: smallest possible according to level\n",
221 defaults->analyse.i_direct_8x8_inference );
222 H0( " -w, --weightb Weighted prediction for B-frames\n" );
223 H0( " --me <string> Integer pixel motion estimation method [\"%s\"]\n",
224 strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
225 H1( " - dia: diamond search, radius 1 (fast)\n"
226 " - hex: hexagonal search, radius 2\n"
227 " - umh: uneven multi-hexagon search\n"
228 " - esa: exhaustive search\n"
229 " - tesa: hadamard exhaustive search (slow)\n" );
230 else H0( " - dia, hex, umh\n" );
231 H0( " --merange <integer> Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
232 H1( " --mvrange <integer> Maximum motion vector length [-1 (auto)]\n" );
233 H1( " --mvrange-thread <int> Minimum buffer between threads [-1 (auto)]\n" );
234 H0( " -m, --subme <integer> Subpixel motion estimation and partition\n"
235 " decision quality: 1=fast, 7=best. [%d]\n", defaults->analyse.i_subpel_refine );
236 H0( " --b-rdo RD based mode decision for B-frames. Requires subme 6.\n" );
237 H0( " --mixed-refs Decide references on a per partition basis\n" );
238 H1( " --no-chroma-me Ignore chroma in motion estimation\n" );
239 H1( " --bime Jointly optimize both MVs in B-frames\n" );
240 H0( " -8, --8x8dct Adaptive spatial transform size\n" );
241 H0( " -t, --trellis <integer> Trellis RD quantization. Requires CABAC. [%d]\n"
243 " - 1: enabled only on the final encode of a MB\n"
244 " - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
245 H0( " --no-fast-pskip Disables early SKIP detection on P-frames\n" );
246 H0( " --no-dct-decimate Disables coefficient thresholding on P-frames\n" );
247 H0( " --nr <integer> Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
249 H1( " --deadzone-inter <int> Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
250 H1( " --deadzone-intra <int> Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
251 H1( " Deadzones should be in the range 0 - 32.\n" );
252 H1( " --cqm <string> Preset quant matrices [\"flat\"]\n"
254 H0( " --cqmfile <string> Read custom quant matrices from a JM-compatible file\n" );
255 H1( " Overrides any other --cqm* options.\n" );
256 H1( " --cqm4 <list> Set all 4x4 quant matrices\n"
257 " Takes a comma-separated list of 16 integers.\n" );
258 H1( " --cqm8 <list> Set all 8x8 quant matrices\n"
259 " Takes a comma-separated list of 64 integers.\n" );
260 H1( " --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
261 " Set both luma and chroma quant matrices\n" );
262 H1( " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
263 " Set individual quant matrices\n" );
265 H1( "Video Usability Info (Annex E):\n" );
266 H1( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
267 H1( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
269 H1( " --overscan <string> Specify crop overscan setting [\"%s\"]\n"
270 " - undef, show, crop\n",
271 strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ) );
272 H1( " --videoformat <string> Specify video format [\"%s\"]\n"
273 " - component, pal, ntsc, secam, mac, undef\n",
274 strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ) );
275 H1( " --fullrange <string> Specify full range samples setting [\"%s\"]\n"
277 strtable_lookup( x264_fullrange_names, defaults->vui.b_fullrange ) );
278 H1( " --colorprim <string> Specify color primaries [\"%s\"]\n"
279 " - undef, bt709, bt470m, bt470bg\n"
280 " smpte170m, smpte240m, film\n",
281 strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ) );
282 H1( " --transfer <string> Specify transfer characteristics [\"%s\"]\n"
283 " - undef, bt709, bt470m, bt470bg, linear,\n"
284 " log100, log316, smpte170m, smpte240m\n",
285 strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ) );
286 H1( " --colormatrix <string> Specify color matrix setting [\"%s\"]\n"
287 " - undef, bt709, fcc, bt470bg\n"
288 " smpte170m, smpte240m, GBR, YCgCo\n",
289 strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
290 H1( " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n",
291 defaults->vui.i_chroma_loc );
293 H0( "Input/Output:\n" );
295 H0( " -o, --output Specify output file\n" );
296 H0( " --sar width:height Specify Sample Aspect Ratio\n" );
297 H0( " --fps <float|rational> Specify framerate\n" );
298 H0( " --seek <integer> First frame to encode\n" );
299 H0( " --frames <integer> Maximum number of frames to encode\n" );
300 H0( " --level <string> Specify level (as defined by Annex A)\n" );
302 H0( " -v, --verbose Print stats for each frame\n" );
303 H0( " --progress Show a progress indicator while encoding\n" );
304 H0( " --quiet Quiet Mode\n" );
305 H0( " --no-psnr Disable PSNR computation\n" );
306 H0( " --no-ssim Disable SSIM computation\n" );
307 H0( " --threads <integer> Parallel encoding\n" );
308 H0( " --thread-input Run Avisynth in its own thread\n" );
309 H1( " --non-deterministic Slightly improve quality of SMP, at the cost of repeatability\n" );
310 H1( " --no-asm Disable all CPU optimizations\n" );
311 H1( " --visualize Show MB types overlayed on the encoded video\n" );
312 H1( " --sps-id <integer> Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
313 H1( " --aud Use access unit delimiters\n" );
317 /*****************************************************************************
319 *****************************************************************************/
320 static int Parse( int argc, char **argv,
321 x264_param_t *param, cli_opt_t *opt )
323 char *psz_filename = NULL;
324 x264_param_t defaults = *param;
328 int b_thread_input = 0;
330 memset( opt, 0, sizeof(cli_opt_t) );
332 /* Default input file driver */
333 p_open_infile = open_file_yuv;
334 p_get_frame_total = get_frame_total_yuv;
335 p_read_frame = read_frame_yuv;
336 p_close_infile = close_file_yuv;
338 /* Default output file driver */
339 p_open_outfile = open_file_bsf;
340 p_set_outfile_param = set_param_bsf;
341 p_write_nalu = write_nalu_bsf;
342 p_set_eop = set_eop_bsf;
343 p_close_outfile = close_file_bsf;
345 /* Parse command line options */
349 int long_options_index = -1;
351 #define OPT_FRAMES 256
353 #define OPT_QPFILE 258
354 #define OPT_THREAD_INPUT 259
355 #define OPT_QUIET 260
356 #define OPT_PROGRESS 261
357 #define OPT_VISUALIZE 262
358 #define OPT_LONGHELP 263
360 static struct option long_options[] =
362 { "help", no_argument, NULL, 'h' },
363 { "longhelp",no_argument, NULL, OPT_LONGHELP },
364 { "version", no_argument, NULL, 'V' },
365 { "bitrate", required_argument, NULL, 'B' },
366 { "bframes", required_argument, NULL, 'b' },
367 { "no-b-adapt", no_argument, NULL, 0 },
368 { "b-bias", required_argument, NULL, 0 },
369 { "b-pyramid", no_argument, NULL, 0 },
370 { "min-keyint",required_argument,NULL,'i' },
371 { "keyint", required_argument, NULL, 'I' },
372 { "scenecut",required_argument, NULL, 0 },
373 { "pre-scenecut", no_argument, NULL, 0 },
374 { "nf", no_argument, NULL, 0 },
375 { "no-deblock", no_argument, NULL, 0 },
376 { "filter", required_argument, NULL, 0 },
377 { "deblock", required_argument, NULL, 'f' },
378 { "interlaced", no_argument, NULL, 0 },
379 { "no-cabac",no_argument, NULL, 0 },
380 { "qp", required_argument, NULL, 'q' },
381 { "qpmin", required_argument, NULL, 0 },
382 { "qpmax", required_argument, NULL, 0 },
383 { "qpstep", required_argument, NULL, 0 },
384 { "crf", required_argument, NULL, 0 },
385 { "ref", required_argument, NULL, 'r' },
386 { "no-asm", no_argument, NULL, 0 },
387 { "sar", required_argument, NULL, 0 },
388 { "fps", required_argument, NULL, 0 },
389 { "frames", required_argument, NULL, OPT_FRAMES },
390 { "seek", required_argument, NULL, OPT_SEEK },
391 { "output", required_argument, NULL, 'o' },
392 { "analyse", required_argument, NULL, 0 },
393 { "partitions", required_argument, NULL, 'A' },
394 { "direct", required_argument, NULL, 0 },
395 { "direct-8x8", required_argument, NULL, 0 },
396 { "weightb", no_argument, NULL, 'w' },
397 { "me", required_argument, NULL, 0 },
398 { "merange", required_argument, NULL, 0 },
399 { "mvrange", required_argument, NULL, 0 },
400 { "mvrange-thread", required_argument, NULL, 0 },
401 { "subme", required_argument, NULL, 'm' },
402 { "b-rdo", no_argument, NULL, 0 },
403 { "mixed-refs", no_argument, NULL, 0 },
404 { "no-chroma-me", no_argument, NULL, 0 },
405 { "bime", no_argument, NULL, 0 },
406 { "8x8dct", no_argument, NULL, '8' },
407 { "trellis", required_argument, NULL, 't' },
408 { "no-fast-pskip", no_argument, NULL, 0 },
409 { "no-dct-decimate", no_argument, NULL, 0 },
410 { "deadzone-inter", required_argument, NULL, '0' },
411 { "deadzone-intra", required_argument, NULL, '0' },
412 { "level", required_argument, NULL, 0 },
413 { "ratetol", required_argument, NULL, 0 },
414 { "vbv-maxrate", required_argument, NULL, 0 },
415 { "vbv-bufsize", required_argument, NULL, 0 },
416 { "vbv-init", required_argument,NULL, 0 },
417 { "ipratio", required_argument, NULL, 0 },
418 { "pbratio", required_argument, NULL, 0 },
419 { "chroma-qp-offset", required_argument, NULL, 0 },
420 { "pass", required_argument, NULL, 'p' },
421 { "stats", required_argument, NULL, 0 },
422 { "rceq", required_argument, NULL, 0 },
423 { "qcomp", required_argument, NULL, 0 },
424 { "qblur", required_argument, NULL, 0 },
425 { "cplxblur",required_argument, NULL, 0 },
426 { "zones", required_argument, NULL, 0 },
427 { "qpfile", required_argument, NULL, OPT_QPFILE },
428 { "threads", required_argument, NULL, 0 },
429 { "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
430 { "non-deterministic", no_argument, NULL, 0 },
431 { "no-psnr", no_argument, NULL, 0 },
432 { "no-ssim", no_argument, NULL, 0 },
433 { "quiet", no_argument, NULL, OPT_QUIET },
434 { "verbose", no_argument, NULL, 'v' },
435 { "progress",no_argument, NULL, OPT_PROGRESS },
436 { "visualize",no_argument, NULL, OPT_VISUALIZE },
437 { "sps-id", required_argument, NULL, 0 },
438 { "aud", no_argument, NULL, 0 },
439 { "nr", required_argument, NULL, 0 },
440 { "cqm", required_argument, NULL, 0 },
441 { "cqmfile", required_argument, NULL, 0 },
442 { "cqm4", required_argument, NULL, 0 },
443 { "cqm4i", required_argument, NULL, 0 },
444 { "cqm4iy", required_argument, NULL, 0 },
445 { "cqm4ic", required_argument, NULL, 0 },
446 { "cqm4p", required_argument, NULL, 0 },
447 { "cqm4py", required_argument, NULL, 0 },
448 { "cqm4pc", required_argument, NULL, 0 },
449 { "cqm8", required_argument, NULL, 0 },
450 { "cqm8i", required_argument, NULL, 0 },
451 { "cqm8p", required_argument, NULL, 0 },
452 { "overscan", required_argument, NULL, 0 },
453 { "videoformat", required_argument, NULL, 0 },
454 { "fullrange", required_argument, NULL, 0 },
455 { "colorprim", required_argument, NULL, 0 },
456 { "transfer", required_argument, NULL, 0 },
457 { "colormatrix", required_argument, NULL, 0 },
458 { "chromaloc", required_argument, NULL, 0 },
462 int c = getopt_long( argc, argv, "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw",
463 long_options, &long_options_index);
473 Help( &defaults, 0 );
476 Help( &defaults, 1 );
480 printf( "x264 "X264_POINTVER"\n" );
482 printf( "x264 0.%d.X\n", X264_BUILD );
486 param->i_frame_total = atoi( optarg );
489 opt->i_seek = atoi( optarg );
492 if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
495 p_open_outfile = open_file_mp4;
496 p_write_nalu = write_nalu_mp4;
497 p_set_outfile_param = set_param_mp4;
498 p_set_eop = set_eop_mp4;
499 p_close_outfile = close_file_mp4;
501 fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
505 else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
507 p_open_outfile = open_file_mkv;
508 p_write_nalu = write_nalu_mkv;
509 p_set_outfile_param = set_param_mkv;
510 p_set_eop = set_eop_mkv;
511 p_close_outfile = close_file_mkv;
513 if( !strcmp(optarg, "-") )
515 else if( p_open_outfile( optarg, &opt->hout ) )
517 fprintf( stderr, "x264 [error]: can't open output file `%s'\n", optarg );
522 opt->qpfile = fopen( optarg, "r" );
525 fprintf( stderr, "x264 [error]: can't open `%s'\n", optarg );
528 param->i_scenecut_threshold = -1;
529 param->b_bframe_adaptive = 0;
531 case OPT_THREAD_INPUT:
535 param->i_log_level = X264_LOG_NONE;
538 param->i_log_level = X264_LOG_DEBUG;
545 param->b_visualize = 1;
546 b_exit_on_ctrl_c = 1;
548 fprintf( stderr, "x264 [warning]: not compiled with visualization support\n" );
554 if( long_options_index < 0 )
556 for( i = 0; long_options[i].name; i++ )
557 if( long_options[i].val == c )
559 long_options_index = i;
562 if( long_options_index < 0 )
564 /* getopt_long already printed an error message */
569 b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg );
575 const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
576 fprintf( stderr, "x264 [error]: invalid argument: %s = %s\n", name, optarg );
581 /* Get the file name */
582 if( optind > argc - 1 || !opt->hout )
584 fprintf( stderr, "x264 [error]: No %s file. Run x264 --help for a list of options.\n",
585 optind > argc - 1 ? "input" : "output" );
588 psz_filename = argv[optind++];
590 /* check demuxer type */
591 psz = psz_filename + strlen(psz_filename) - 1;
592 while( psz > psz_filename && *psz != '.' )
594 if( !strncasecmp( psz, ".avi", 4 ) || !strncasecmp( psz, ".avs", 4 ) )
596 if( !strncasecmp( psz, ".y4m", 4 ) )
599 if( !(b_avis || b_y4m) ) // raw yuv
601 if( optind > argc - 1 )
603 /* try to parse the file name */
604 for( psz = psz_filename; *psz; psz++ )
606 if( *psz >= '0' && *psz <= '9'
607 && sscanf( psz, "%ux%u", ¶m->i_width, ¶m->i_height ) == 2 )
609 if( param->i_log_level >= X264_LOG_INFO )
610 fprintf( stderr, "x264 [info]: file name gives %dx%d\n", param->i_width, param->i_height );
617 sscanf( argv[optind++], "%ux%u", ¶m->i_width, ¶m->i_height );
621 if( !(b_avis || b_y4m) && ( !param->i_width || !param->i_height ) )
623 fprintf( stderr, "x264 [error]: Rawyuv input requires a resolution.\n" );
632 p_open_infile = open_file_avis;
633 p_get_frame_total = get_frame_total_avis;
634 p_read_frame = read_frame_avis;
635 p_close_infile = close_file_avis;
637 fprintf( stderr, "x264 [error]: not compiled with AVIS input support\n" );
643 p_open_infile = open_file_y4m;
644 p_get_frame_total = get_frame_total_y4m;
645 p_read_frame = read_frame_y4m;
646 p_close_infile = close_file_y4m;
649 if( p_open_infile( psz_filename, &opt->hin, param ) )
651 fprintf( stderr, "x264 [error]: could not open input file '%s'\n", psz_filename );
657 if( b_thread_input || param->i_threads > 1 )
659 if( open_file_thread( NULL, &opt->hin, param ) )
661 fprintf( stderr, "x264 [warning]: threaded input failed\n" );
665 p_open_infile = open_file_thread;
666 p_get_frame_total = get_frame_total_thread;
667 p_read_frame = read_frame_thread;
668 p_close_infile = close_file_thread;
676 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
680 while( num < i_frame )
682 int ret = fscanf( opt->qpfile, "%d %c %d\n", &num, &type, &qp );
685 pic->i_qpplus1 = qp+1;
686 if ( type == 'I' ) pic->i_type = X264_TYPE_IDR;
687 else if( type == 'i' ) pic->i_type = X264_TYPE_I;
688 else if( type == 'P' ) pic->i_type = X264_TYPE_P;
689 else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
690 else if( type == 'b' ) pic->i_type = X264_TYPE_B;
692 if( ret != 3 || qp < 0 || qp > 51 || num > i_frame )
694 fprintf( stderr, "x264 [error]: can't parse qpfile for frame %d\n", i_frame );
695 fclose( opt->qpfile );
697 pic->i_type = X264_TYPE_AUTO;
704 /*****************************************************************************
706 *****************************************************************************/
708 static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
710 x264_picture_t pic_out;
715 if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
717 fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
720 for( i = 0; i < i_nal; i++ )
724 if( mux_buffer_size < nal[i].i_payload * 3/2 + 4 )
726 mux_buffer_size = nal[i].i_payload * 2 + 4;
727 x264_free( mux_buffer );
728 mux_buffer = x264_malloc( mux_buffer_size );
731 i_size = mux_buffer_size;
732 x264_nal_encode( mux_buffer, &i_size, 1, &nal[i] );
733 i_file += p_write_nalu( hout, mux_buffer, i_size );
736 p_set_eop( hout, &pic_out );
741 static int Encode( x264_param_t *param, cli_opt_t *opt )
746 int i_frame, i_frame_total;
747 int64_t i_start, i_end;
752 i_frame_total = p_get_frame_total( opt->hin );
753 i_frame_total -= opt->i_seek;
754 if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
755 && param->i_frame_total > 0 )
756 i_frame_total = param->i_frame_total;
757 param->i_frame_total = i_frame_total;
759 if( ( h = x264_encoder_open( param ) ) == NULL )
761 fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
762 p_close_infile( opt->hin );
763 p_close_outfile( opt->hout );
767 if( p_set_outfile_param( opt->hout, param ) )
769 fprintf( stderr, "x264 [error]: can't set outfile param\n" );
770 p_close_infile( opt->hin );
771 p_close_outfile( opt->hout );
775 /* Create a new pic */
776 x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
778 i_start = x264_mdate();
781 for( i_frame = 0, i_file = 0, i_progress = 0;
782 b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
784 if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
787 pic.i_pts = (int64_t)i_frame * param->i_fps_den;
790 parse_qpfile( opt, &pic, i_frame + opt->i_seek );
793 /* Do not force any parameters */
794 pic.i_type = X264_TYPE_AUTO;
798 i_file += Encode_frame( h, opt->hout, &pic );
802 /* update status line (up to 1000 times per input file) */
803 if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&
804 ( i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
805 : i_frame % 10 == 0 ) )
807 int64_t i_elapsed = x264_mdate() - i_start;
808 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
811 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
812 i_progress = i_frame * 1000 / i_frame_total;
813 fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps, eta %d:%02d:%02d \r",
814 i_frame, i_frame_total, (float)i_progress / 10, fps,
815 eta/3600, (eta/60)%60, eta%60 );
818 fprintf( stderr, "encoded frames: %d, %.2f fps \r", i_frame, fps );
819 fflush( stderr ); // needed in windows
822 /* Flush delayed B-frames */
825 i_frame_size = Encode_frame( h, opt->hout, NULL );
826 } while( i_frame_size );
828 i_end = x264_mdate();
829 x264_picture_clean( &pic );
830 x264_encoder_close( h );
831 fprintf( stderr, "\n" );
834 fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );
836 p_close_infile( opt->hin );
837 p_close_outfile( opt->hout );
841 double fps = (double)i_frame * (double)1000000 /
842 (double)( i_end - i_start );
844 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,
845 (double) i_file * 8 * param->i_fps_num /
846 ( (double) param->i_fps_den * i_frame * 1000 ) );