1 /*****************************************************************************
2 * x264: h264 encoder/decoder 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 *****************************************************************************/
24 #define _LARGEFILE_SOURCE
25 #define _FILE_OFFSET_BITS 64
37 #include <io.h> /* _setmode() */
38 #include <fcntl.h> /* _O_BINARY */
45 #include "common/common.h"
49 #define DATA_MAX 3000000
50 uint8_t data[DATA_MAX];
53 static int b_ctrl_c = 0;
54 static int b_exit_on_ctrl_c = 0;
55 static void SigIntHandler( int a )
57 if( b_exit_on_ctrl_c )
70 /* input file operation function pointers */
71 int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
72 int (*p_get_frame_total)( hnd_t handle );
73 int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame );
74 int (*p_close_infile)( hnd_t handle );
76 /* output file operation function pointers */
77 static int (*p_open_outfile)( char *psz_filename, hnd_t *p_handle );
78 static int (*p_set_outfile_param)( hnd_t handle, x264_param_t *p_param );
79 static int (*p_write_nalu)( hnd_t handle, uint8_t *p_nal, int i_size );
80 static int (*p_set_eop)( hnd_t handle, x264_picture_t *p_picture );
81 static int (*p_close_outfile)( hnd_t handle );
83 static void Help( x264_param_t *defaults );
84 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
85 static int Encode( x264_param_t *param, cli_opt_t *opt );
88 /****************************************************************************
90 ****************************************************************************/
91 int main( int argc, char **argv )
97 _setmode(_fileno(stdin), _O_BINARY);
98 _setmode(_fileno(stdout), _O_BINARY);
101 x264_param_default( ¶m );
103 /* Parse command line */
104 if( Parse( argc, argv, ¶m, &opt ) < 0 )
107 /* Control-C handler */
108 signal( SIGINT, SigIntHandler );
110 return Encode( ¶m, &opt );
113 static const char * const overscan_str[] = { "undef", "show", "crop", NULL };
114 static const char * const vidformat_str[] = { "component", "pal", "ntsc", "secam", "mac", "undef", NULL };
115 static const char * const fullrange_str[] = { "off", "on", NULL };
116 static const char * const colorprim_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "film", NULL };
117 static const char * const transfer_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "linear", "log100", "log316", NULL };
118 static const char * const colmatrix_str[] = { "GBR", "bt709", "undef", "", "fcc", "bt470bg", "smpte170m", "smpte240m", "YCgCo", NULL };
120 static char const *strtable_lookup( const char * const table[], int index )
122 int i = 0; while( table[i] ) i++;
123 return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
126 /*****************************************************************************
128 *****************************************************************************/
129 static void Help( x264_param_t *defaults )
133 "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
135 "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
136 " or YUV4MPEG 4:2:0 (*.y4m),\n"
137 " or AVI or Avisynth if compiled with AVIS support (%s).\n"
138 "Outfile type is selected by filename:\n"
139 " .264 -> Raw bytestream\n"
140 " .mkv -> Matroska\n"
141 " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
145 " -h, --help Print this help\n"
147 "Frame-type options:\n"
149 " -I, --keyint <integer> Maximum GOP size [%d]\n"
150 " -i, --min-keyint <integer> Minimum GOP size [%d]\n"
151 " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n"
152 " -b, --bframes <integer> Number of B-frames between I and P [%d]\n"
153 " --no-b-adapt Disable adaptive B-frame decision\n"
154 " --b-bias <integer> Influences how often B-frames are used [%d]\n"
155 " --b-pyramid Keep some B-frames as references\n"
157 " --no-cabac Disable CABAC\n"
158 " -r, --ref <integer> Number of reference frames [%d]\n"
159 " --nf Disable loop filter\n"
160 " -f, --filter <alpha:beta> Loop filter AlphaC0 and Beta parameters [%d:%d]\n"
164 " -q, --qp <integer> Set QP (0=lossless) [%d]\n"
165 " -B, --bitrate <integer> Set bitrate\n"
166 " --crf <integer> Quality-based VBR (nominal QP)\n"
167 " --qpmin <integer> Set min QP [%d]\n"
168 " --qpmax <integer> Set max QP [%d]\n"
169 " --qpstep <integer> Set max QP step [%d]\n"
170 " --ratetol <float> Allowed variance of average bitrate [%.1f]\n"
171 " --vbv-maxrate <integer> Max local bitrate [%d]\n"
172 " --vbv-bufsize <integer> Size of VBV buffer [%d]\n"
173 " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n"
175 " --ipratio <float> QP factor between I and P [%.2f]\n"
176 " --pbratio <float> QP factor between P and B [%.2f]\n"
177 " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n"
179 " -p, --pass <1|2|3> Enable multipass ratecontrol:\n"
180 " - 1: First pass, creates stats file\n"
181 " - 2: Last pass, does not overwrite stats file\n"
182 " - 3: Nth pass, overwrites stats file\n"
183 " --stats <string> Filename for 2 pass stats [\"%s\"]\n"
184 " --rceq <string> Ratecontrol equation [\"%s\"]\n"
185 " --qcomp <float> QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n"
186 " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n"
187 " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n"
189 " --zones <zone0>/<zone1>/...\n"
190 " Tweak the bitrate of some regions of the video\n"
191 " Each zone is of the form\n"
192 " <start frame>,<end frame>,<option>\n"
193 " where <option> is either\n"
194 " q=<integer> (force QP)\n"
195 " or b=<float> (bitrate multiplier)\n"
199 " -A, --analyse <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
200 " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
202 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n"
203 " --direct <string> Direct MV prediction mode [\"%s\"]\n"
204 " - none, spatial, temporal, auto\n"
205 " -w, --weightb Weighted prediction for B-frames\n"
206 " --me <string> Integer pixel motion estimation method [\"%s\"]\n"
207 " - dia: diamond search, radius 1 (fast)\n"
208 " - hex: hexagonal search, radius 2\n"
209 " - umh: uneven multi-hexagon search\n"
210 " - esa: exhaustive search (slow)\n"
211 " --merange <integer> Maximum motion vector search range [%d]\n"
212 " -m, --subme <integer> Subpixel motion estimation and partition\n"
213 " decision quality: 1=fast, 7=best. [%d]\n"
214 " --b-rdo RD based mode decision for B-frames. Requires subme 6.\n"
215 " --mixed-refs Decide references on a per partition basis\n"
216 " --no-chroma-me Ignore chroma in motion estimation\n"
217 " --bime Jointly optimize both MVs in B-frames\n"
218 " -8, --8x8dct Adaptive spatial transform size\n"
219 " -t, --trellis <integer> Trellis RD quantization. Requires CABAC. [%d]\n"
221 " - 1: enabled only on the final encode of a MB\n"
222 " - 2: enabled on all mode decisions\n"
223 " --no-fast-pskip Disables early SKIP detection on P-frames\n"
224 " --no-dct-decimate Disables coefficient thresholding on P-frames\n"
225 " --nr <integer> Noise reduction [%d]\n"
227 " --cqm <string> Preset quant matrices [\"flat\"]\n"
229 " --cqmfile <string> Read quant matrices from a JM-compatible file\n"
230 " Overrides any other --cqm* options.\n"
231 " --cqm4 <list> Set all 4x4 quant matrices\n"
232 " Takes a comma-separated list of 16 integers.\n"
233 " --cqm8 <list> Set all 8x8 quant matrices\n"
234 " Takes a comma-separated list of 64 integers.\n"
235 " --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
236 " Set both luma and chroma quant matrices\n"
237 " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
238 " Set individual quant matrices\n"
240 "Video Usability Info (Annex E):\n"
241 "The VUI settings are not used by the encoder but are merely suggestions to\n"
242 "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n"
244 " --sar width:height Specify Sample Aspect Ratio\n"
245 " --overscan <string> Specify crop overscan setting [\"%s\"]\n"
246 " - undef, show, crop\n"
247 " --videoformat <string> Specify video format [\"%s\"]\n"
248 " - component, pal, ntsc, secam, mac, undef\n"
249 " --fullrange <string> Specify full range samples setting [\"%s\"]\n"
251 " --colorprim <string> Specify color primaries [\"%s\"]\n"
252 " - undef, bt709, bt470m, bt470bg\n"
253 " smpte170m, smpte240m, film\n"
254 " --transfer <string> Specify transfer characteristics [\"%s\"]\n"
255 " - undef, bt709, bt470m, bt470bg, linear,\n"
256 " log100, log316, smpte170m, smpte240m\n"
257 " --colormatrix <string> Specify color matrix setting [\"%s\"]\n"
258 " - undef, bt709, fcc, bt470bg\n"
259 " smpte170m, smpte240m, GBR, YCgCo\n"
260 " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n"
264 " --level <string> Specify level (as defined by Annex A)\n"
265 " --fps <float|rational> Specify framerate\n"
266 " --seek <integer> First frame to encode\n"
267 " --frames <integer> Maximum number of frames to encode\n"
268 " -o, --output Specify output file\n"
270 " --threads <integer> Parallel encoding (uses slices)\n"
271 " --thread-input Run Avisynth in its own thread\n"
272 " --no-asm Disable all CPU optimizations\n"
273 " --no-psnr Disable PSNR computation\n"
274 " --quiet Quiet Mode\n"
275 " -v, --verbose Print stats for each frame\n"
276 " --progress Show a progress indicator while encoding\n"
277 " --visualize Show MB types overlayed on the encoded video\n"
278 " --sps-id <integer> Set SPS and PPS id numbers [%d]\n"
279 " --aud Use access unit delimiters\n"
281 X264_BUILD, X264_VERSION,
292 defaults->i_keyint_max,
293 defaults->i_keyint_min,
294 defaults->i_scenecut_threshold,
296 defaults->i_bframe_bias,
297 defaults->i_frame_reference,
298 defaults->i_deblocking_filter_alphac0,
299 defaults->i_deblocking_filter_beta,
300 defaults->rc.i_qp_constant,
301 defaults->rc.i_qp_min,
302 defaults->rc.i_qp_max,
303 defaults->rc.i_qp_step,
304 defaults->rc.f_rate_tolerance,
305 defaults->rc.i_vbv_max_bitrate,
306 defaults->rc.i_vbv_buffer_size,
307 defaults->rc.f_vbv_buffer_init,
308 defaults->rc.f_ip_factor,
309 defaults->rc.f_pb_factor,
310 defaults->analyse.i_chroma_qp_offset,
311 defaults->rc.psz_stat_out,
312 defaults->rc.psz_rc_eq,
313 defaults->rc.f_qcompress,
314 defaults->rc.f_complexity_blur,
315 defaults->rc.f_qblur,
316 strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ),
317 strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ),
318 defaults->analyse.i_me_range,
319 defaults->analyse.i_subpel_refine,
320 defaults->analyse.i_trellis,
321 defaults->analyse.i_noise_reduction,
322 strtable_lookup( overscan_str, defaults->vui.i_overscan ),
323 strtable_lookup( vidformat_str, defaults->vui.i_vidformat ),
324 strtable_lookup( fullrange_str, defaults->vui.b_fullrange ),
325 strtable_lookup( colorprim_str, defaults->vui.i_colorprim ),
326 strtable_lookup( transfer_str, defaults->vui.i_transfer ),
327 strtable_lookup( colmatrix_str, defaults->vui.i_colmatrix ),
328 defaults->vui.i_chroma_loc,
333 static int parse_enum( const char *arg, const char * const *names, int *dst )
336 for( i = 0; names[i]; i++ )
337 if( !strcmp( arg, names[i] ) )
345 static int parse_cqm( const char *str, uint8_t *cqm, int length )
350 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
353 } while( i < length && (str = strchr( str, ',' )) && str++ );
354 return (i == length) ? 0 : -1;
357 /*****************************************************************************
359 *****************************************************************************/
360 static int Parse( int argc, char **argv,
361 x264_param_t *param, cli_opt_t *opt )
363 char *psz_filename = NULL;
364 x264_param_t defaults = *param;
368 int b_thread_input = 0;
370 memset( opt, 0, sizeof(cli_opt_t) );
372 /* Default input file driver */
373 p_open_infile = open_file_yuv;
374 p_get_frame_total = get_frame_total_yuv;
375 p_read_frame = read_frame_yuv;
376 p_close_infile = close_file_yuv;
378 /* Default output file driver */
379 p_open_outfile = open_file_bsf;
380 p_set_outfile_param = set_param_bsf;
381 p_write_nalu = write_nalu_bsf;
382 p_set_eop = set_eop_bsf;
383 p_close_outfile = close_file_bsf;
385 /* Parse command line options */
386 opterr = 0; // no error message
390 int long_options_index;
391 #define OPT_QPMIN 256
392 #define OPT_QPMAX 257
393 #define OPT_QPSTEP 258
394 #define OPT_IPRATIO 260
395 #define OPT_PBRATIO 261
396 #define OPT_RATETOL 262
397 #define OPT_RCSTATS 264
399 #define OPT_QCOMP 266
400 #define OPT_NOPSNR 267
401 #define OPT_QUIET 268
402 #define OPT_SCENECUT 270
403 #define OPT_QBLUR 271
404 #define OPT_CPLXBLUR 272
405 #define OPT_FRAMES 273
407 #define OPT_DIRECT 275
408 #define OPT_LEVEL 276
409 #define OPT_NOBADAPT 277
410 #define OPT_BBIAS 278
411 #define OPT_BPYRAMID 279
412 #define OPT_CHROMA_QP 280
413 #define OPT_NO_CHROMA_ME 281
414 #define OPT_NO_CABAC 282
416 #define OPT_PROGRESS 284
418 #define OPT_MERANGE 286
419 #define OPT_VBVMAXRATE 287
420 #define OPT_VBVBUFSIZE 288
421 #define OPT_VBVINIT 289
422 #define OPT_VISUALIZE 290
424 #define OPT_ZONES 292
425 #define OPT_THREADS 293
428 #define OPT_CQM4I 296
429 #define OPT_CQM4IY 297
430 #define OPT_CQM4IC 298
431 #define OPT_CQM4P 299
432 #define OPT_CQM4PY 300
433 #define OPT_CQM4PC 301
435 #define OPT_CQM8I 303
436 #define OPT_CQM8P 304
437 #define OPT_CQMFILE 305
439 #define OPT_OVERSCAN 307
440 #define OPT_VIDFORMAT 308
441 #define OPT_FULLRANGE 309
442 #define OPT_COLOURPRIM 310
443 #define OPT_TRANSFER 311
444 #define OPT_COLOURMATRIX 312
445 #define OPT_CHROMALOC 313
446 #define OPT_MIXED_REFS 314
448 #define OPT_B_RDO 316
449 #define OPT_NO_FAST_PSKIP 317
452 #define OPT_THREAD_INPUT 320
453 #define OPT_NO_DCT_DECIMATE 321
454 #define OPT_SPS_ID 322
456 static struct option long_options[] =
458 { "help", no_argument, NULL, 'h' },
459 { "bitrate", required_argument, NULL, 'B' },
460 { "bframes", required_argument, NULL, 'b' },
461 { "no-b-adapt", no_argument, NULL, OPT_NOBADAPT },
462 { "b-bias", required_argument, NULL, OPT_BBIAS },
463 { "b-pyramid", no_argument, NULL, OPT_BPYRAMID },
464 { "min-keyint",required_argument,NULL,'i' },
465 { "keyint", required_argument, NULL, 'I' },
466 { "scenecut",required_argument, NULL, OPT_SCENECUT },
467 { "nf", no_argument, NULL, 'n' },
468 { "filter", required_argument, NULL, 'f' },
469 { "no-cabac",no_argument, NULL, OPT_NO_CABAC },
470 { "qp", required_argument, NULL, 'q' },
471 { "qpmin", required_argument, NULL, OPT_QPMIN },
472 { "qpmax", required_argument, NULL, OPT_QPMAX },
473 { "qpstep", required_argument, NULL, OPT_QPSTEP },
474 { "crf", required_argument, NULL, OPT_CRF },
475 { "ref", required_argument, NULL, 'r' },
476 { "no-asm", no_argument, NULL, 'C' },
477 { "sar", required_argument, NULL, OPT_SAR },
478 { "fps", required_argument, NULL, OPT_FPS },
479 { "frames", required_argument, NULL, OPT_FRAMES },
480 { "seek", required_argument, NULL, OPT_SEEK },
481 { "output", required_argument, NULL, 'o' },
482 { "analyse", required_argument, NULL, 'A' },
483 { "direct", required_argument, NULL, OPT_DIRECT },
484 { "weightb", no_argument, NULL, 'w' },
485 { "me", required_argument, NULL, OPT_ME },
486 { "merange", required_argument, NULL, OPT_MERANGE },
487 { "subme", required_argument, NULL, 'm' },
488 { "b-rdo", no_argument, NULL, OPT_B_RDO },
489 { "mixed-refs", no_argument, NULL, OPT_MIXED_REFS },
490 { "no-chroma-me", no_argument, NULL, OPT_NO_CHROMA_ME },
491 { "bime", no_argument, NULL, OPT_BIME },
492 { "8x8dct", no_argument, NULL, '8' },
493 { "trellis", required_argument, NULL, 't' },
494 { "no-fast-pskip", no_argument, NULL, OPT_NO_FAST_PSKIP },
495 { "no-dct-decimate", no_argument, NULL, OPT_NO_DCT_DECIMATE },
496 { "level", required_argument, NULL, OPT_LEVEL },
497 { "ratetol", required_argument, NULL, OPT_RATETOL },
498 { "vbv-maxrate", required_argument, NULL, OPT_VBVMAXRATE },
499 { "vbv-bufsize", required_argument, NULL, OPT_VBVBUFSIZE },
500 { "vbv-init", required_argument,NULL, OPT_VBVINIT },
501 { "ipratio", required_argument, NULL, OPT_IPRATIO },
502 { "pbratio", required_argument, NULL, OPT_PBRATIO },
503 { "chroma-qp-offset", required_argument, NULL, OPT_CHROMA_QP },
504 { "pass", required_argument, NULL, 'p' },
505 { "stats", required_argument, NULL, OPT_RCSTATS },
506 { "rceq", required_argument, NULL, OPT_RCEQ },
507 { "qcomp", required_argument, NULL, OPT_QCOMP },
508 { "qblur", required_argument, NULL, OPT_QBLUR },
509 { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
510 { "zones", required_argument, NULL, OPT_ZONES },
511 { "threads", required_argument, NULL, OPT_THREADS },
512 { "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
513 { "no-psnr", no_argument, NULL, OPT_NOPSNR },
514 { "quiet", no_argument, NULL, OPT_QUIET },
515 { "verbose", no_argument, NULL, 'v' },
516 { "progress",no_argument, NULL, OPT_PROGRESS },
517 { "visualize",no_argument, NULL, OPT_VISUALIZE },
518 { "sps-id", required_argument, NULL, OPT_SPS_ID },
519 { "aud", no_argument, NULL, OPT_AUD },
520 { "nr", required_argument, NULL, OPT_NR },
521 { "cqm", required_argument, NULL, OPT_CQM },
522 { "cqmfile", required_argument, NULL, OPT_CQMFILE },
523 { "cqm4", required_argument, NULL, OPT_CQM4 },
524 { "cqm4i", required_argument, NULL, OPT_CQM4I },
525 { "cqm4iy", required_argument, NULL, OPT_CQM4IY },
526 { "cqm4ic", required_argument, NULL, OPT_CQM4IC },
527 { "cqm4p", required_argument, NULL, OPT_CQM4P },
528 { "cqm4py", required_argument, NULL, OPT_CQM4PY },
529 { "cqm4pc", required_argument, NULL, OPT_CQM4PC },
530 { "cqm8", required_argument, NULL, OPT_CQM8 },
531 { "cqm8i", required_argument, NULL, OPT_CQM8I },
532 { "cqm8p", required_argument, NULL, OPT_CQM8P },
533 { "overscan", required_argument, NULL, OPT_OVERSCAN },
534 { "videoformat", required_argument, NULL, OPT_VIDFORMAT },
535 { "fullrange", required_argument, NULL, OPT_FULLRANGE },
536 { "colorprim", required_argument, NULL, OPT_COLOURPRIM },
537 { "transfer", required_argument, NULL, OPT_TRANSFER },
538 { "colormatrix", required_argument, NULL, OPT_COLOURMATRIX },
539 { "chromaloc", required_argument, NULL, OPT_CHROMALOC },
545 c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:t:vw8",
546 long_options, &long_options_index);
562 param->rc.i_bitrate = atol( optarg );
563 param->rc.i_rc_method = X264_RC_ABR;
566 param->rc.i_rf_constant = atol( optarg );
567 param->rc.i_rc_method = X264_RC_CRF;
570 param->i_bframe = atol( optarg );
573 param->b_bframe_adaptive = 0;
576 param->i_bframe_bias = atol( optarg );
579 param->b_bframe_pyramid = 1;
582 param->i_keyint_min = atol( optarg );
583 if( param->i_keyint_max < param->i_keyint_min )
584 param->i_keyint_max = param->i_keyint_min;
587 param->i_keyint_max = atol( optarg );
588 if( param->i_keyint_min > param->i_keyint_max )
589 param->i_keyint_min = param->i_keyint_max;
592 param->i_scenecut_threshold = atol( optarg );
595 param->b_deblocking_filter = 0;
599 char *p = strchr( optarg, ':' );
600 if( !p ) p = strchr( optarg, ',' );
601 param->i_deblocking_filter_alphac0 = atoi( optarg );
602 param->i_deblocking_filter_beta = p ? atoi( p+1 ) : param->i_deblocking_filter_alphac0;
606 param->rc.i_qp_constant = atoi( optarg );
607 param->rc.i_rc_method = X264_RC_CQP;
610 param->rc.i_qp_min = atoi( optarg );
613 param->rc.i_qp_max = atoi( optarg );
616 param->rc.i_qp_step = atoi( optarg );
619 param->i_frame_reference = atoi( optarg );
625 opt->b_decompress = 1;
631 param->i_frame_total = atoi( optarg );
634 opt->i_seek = atoi( optarg );
637 if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
640 p_open_outfile = open_file_mp4;
641 p_write_nalu = write_nalu_mp4;
642 p_set_outfile_param = set_param_mp4;
643 p_set_eop = set_eop_mp4;
644 p_close_outfile = close_file_mp4;
646 fprintf( stderr, "not compiled with MP4 output support\n" );
650 else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
652 p_open_outfile = open_file_mkv;
653 p_write_nalu = write_nalu_mkv;
654 p_set_outfile_param = set_param_mkv;
655 p_set_eop = set_eop_mkv;
656 p_close_outfile = close_file_mkv;
658 if( !strcmp(optarg, "-") )
660 else if( p_open_outfile( optarg, &opt->hout ) )
662 fprintf( stderr, "cannot open output file `%s'\n", optarg );
668 char *p = strchr( optarg, ':' );
669 if( !p ) p = strchr( optarg, '/' );
672 param->vui.i_sar_width = atoi( optarg );
673 param->vui.i_sar_height = atoi( p + 1 );
680 if( sscanf( optarg, "%d/%d", ¶m->i_fps_num, ¶m->i_fps_den ) == 2 )
682 else if( sscanf( optarg, "%f", &fps ) )
684 param->i_fps_num = (int)(fps * 1000 + .5);
685 param->i_fps_den = 1000;
689 fprintf( stderr, "bad fps `%s'\n", optarg );
695 param->analyse.inter = 0;
696 if( strstr( optarg, "none" ) ) param->analyse.inter = 0;
697 if( strstr( optarg, "all" ) ) param->analyse.inter = ~0;
699 if( strstr( optarg, "i4x4" ) ) param->analyse.inter |= X264_ANALYSE_I4x4;
700 if( strstr( optarg, "i8x8" ) ) param->analyse.inter |= X264_ANALYSE_I8x8;
701 if( strstr( optarg, "p8x8" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
702 if( strstr( optarg, "p4x4" ) ) param->analyse.inter |= X264_ANALYSE_PSUB8x8;
703 if( strstr( optarg, "b8x8" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16;
706 b_error |= parse_enum( optarg, x264_direct_pred_names, ¶m->analyse.i_direct_mv_pred );
709 param->analyse.b_weighted_bipred = 1;
712 b_error |= parse_enum( optarg, x264_motion_est_names, ¶m->analyse.i_me_method );
715 param->analyse.i_me_range = atoi(optarg);
718 param->analyse.i_subpel_refine = atoi(optarg);
721 param->analyse.b_bframe_rdo = 1;
724 param->analyse.b_mixed_references = 1;
726 case OPT_NO_CHROMA_ME:
727 param->analyse.b_chroma_me = 0;
730 param->analyse.b_bidir_me = 1;
733 param->analyse.b_transform_8x8 = 1;
736 param->analyse.i_trellis = atoi(optarg);
738 case OPT_NO_FAST_PSKIP:
739 param->analyse.b_fast_pskip = 0;
741 case OPT_NO_DCT_DECIMATE:
742 param->analyse.b_dct_decimate = 0;
745 if( atof(optarg) < 6 )
746 param->i_level_idc = (int)(10*atof(optarg)+.5);
748 param->i_level_idc = atoi(optarg);
751 param->rc.f_rate_tolerance = !strncmp("inf", optarg, 3) ? 1e9 : atof(optarg);
754 param->rc.i_vbv_max_bitrate = atoi( optarg );
757 param->rc.i_vbv_buffer_size = atoi( optarg );
760 param->rc.f_vbv_buffer_init = atof(optarg);
763 param->rc.f_ip_factor = atof(optarg);
766 param->rc.f_pb_factor = atof(optarg);
769 param->analyse.i_chroma_qp_offset = atoi(optarg);
773 int i_pass = atoi(optarg);
775 param->rc.b_stat_write = 1;
776 else if( i_pass == 2 )
777 param->rc.b_stat_read = 1;
778 else if( i_pass > 2 )
779 param->rc.b_stat_read =
780 param->rc.b_stat_write = 1;
784 param->rc.psz_stat_in = optarg;
785 param->rc.psz_stat_out = optarg;
788 param->rc.psz_rc_eq = optarg;
791 param->rc.f_qcompress = atof(optarg);
794 param->rc.f_qblur = atof(optarg);
797 param->rc.f_complexity_blur = atof(optarg);
800 param->rc.psz_zones = optarg;
803 param->i_threads = atoi(optarg);
805 case OPT_THREAD_INPUT:
809 param->analyse.b_psnr = 0;
812 param->i_log_level = X264_LOG_NONE;
815 param->i_log_level = X264_LOG_DEBUG;
818 param->i_sps_id = atoi(optarg);
828 param->b_visualize = 1;
829 b_exit_on_ctrl_c = 1;
831 fprintf( stderr, "not compiled with visualization support\n" );
835 param->analyse.i_noise_reduction = atoi(optarg);
838 if( strstr( optarg, "flat" ) )
839 param->i_cqm_preset = X264_CQM_FLAT;
840 else if( strstr( optarg, "jvt" ) )
841 param->i_cqm_preset = X264_CQM_JVT;
844 fprintf( stderr, "bad CQM preset `%s'\n", optarg );
849 param->psz_cqm_file = optarg;
852 param->i_cqm_preset = X264_CQM_CUSTOM;
853 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
854 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
855 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
856 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
859 param->i_cqm_preset = X264_CQM_CUSTOM;
860 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
861 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
864 param->i_cqm_preset = X264_CQM_CUSTOM;
865 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
866 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
869 param->i_cqm_preset = X264_CQM_CUSTOM;
870 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
871 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
874 param->i_cqm_preset = X264_CQM_CUSTOM;
875 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
878 param->i_cqm_preset = X264_CQM_CUSTOM;
879 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
882 param->i_cqm_preset = X264_CQM_CUSTOM;
883 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
886 param->i_cqm_preset = X264_CQM_CUSTOM;
887 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
890 param->i_cqm_preset = X264_CQM_CUSTOM;
891 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
894 param->i_cqm_preset = X264_CQM_CUSTOM;
895 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
898 b_error |= parse_enum( optarg, overscan_str, ¶m->vui.i_overscan );
901 b_error |= parse_enum( optarg, vidformat_str, ¶m->vui.i_vidformat );
904 b_error |= parse_enum( optarg, fullrange_str, ¶m->vui.b_fullrange );
907 b_error |= parse_enum( optarg, colorprim_str, ¶m->vui.i_colorprim );
910 b_error |= parse_enum( optarg, transfer_str, ¶m->vui.i_transfer );
912 case OPT_COLOURMATRIX:
913 b_error |= parse_enum( optarg, colmatrix_str, ¶m->vui.i_colmatrix );
916 param->vui.i_chroma_loc = atoi( optarg );
917 b_error = ( param->vui.i_chroma_loc < 0 || param->vui.i_chroma_loc > 5 );
920 fprintf( stderr, "unknown option (%c)\n", optopt );
926 fprintf( stderr, "bad argument: %s %s\n", argv[optind-2], optarg );
931 /* Get the file name */
932 if( optind > argc - 1 || !opt->hout )
937 psz_filename = argv[optind++];
939 if( !opt->b_decompress )
941 if( optind > argc - 1 )
943 /* try to parse the file name */
944 for( psz = psz_filename; *psz; psz++ )
946 if( *psz >= '0' && *psz <= '9'
947 && sscanf( psz, "%ux%u", ¶m->i_width, ¶m->i_height ) == 2 )
949 if( param->i_log_level >= X264_LOG_INFO )
950 fprintf( stderr, "x264 [info]: file name gives %dx%d\n", param->i_width, param->i_height );
957 sscanf( argv[optind++], "%ux%u", ¶m->i_width, ¶m->i_height );
960 /* check avis input */
961 psz = psz_filename + strlen(psz_filename) - 1;
962 while( psz > psz_filename && *psz != '.' )
965 if( !strncasecmp( psz, ".avi", 4 ) || !strncasecmp( psz, ".avs", 4 ) )
967 if( !strncasecmp( psz, ".y4m", 4 ) )
969 if( !(b_avis || b_y4m) && ( !param->i_width || !param->i_height ) )
981 p_open_infile = open_file_avis;
982 p_get_frame_total = get_frame_total_avis;
983 p_read_frame = read_frame_avis;
984 p_close_infile = close_file_avis;
986 fprintf( stderr, "not compiled with AVIS input support\n" );
992 p_open_infile = open_file_y4m;
993 p_get_frame_total = get_frame_total_y4m;
994 p_read_frame = read_frame_y4m;
995 p_close_infile = close_file_y4m;
998 if( p_open_infile( psz_filename, &opt->hin, param ) )
1000 fprintf( stderr, "could not open input file '%s'\n", psz_filename );
1006 if( b_thread_input || param->i_threads > 1 )
1008 if( open_file_thread( NULL, &opt->hin, param ) )
1010 fprintf( stderr, "threaded input failed\n" );
1014 p_open_infile = open_file_thread;
1015 p_get_frame_total = get_frame_total_thread;
1016 p_read_frame = read_frame_thread;
1017 p_close_infile = close_file_thread;
1025 /*****************************************************************************
1027 *****************************************************************************/
1029 static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
1031 x264_picture_t pic_out;
1036 /* Do not force any parameters */
1039 pic->i_type = X264_TYPE_AUTO;
1042 if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
1044 fprintf( stderr, "x264_encoder_encode failed\n" );
1047 for( i = 0; i < i_nal; i++ )
1053 if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
1055 i_file += p_write_nalu( hout, data, i_size );
1057 else if( i_size < 0 )
1059 fprintf( stderr, "need to increase buffer size (size=%d)\n", -i_size );
1063 p_set_eop( hout, &pic_out );
1068 /*****************************************************************************
1070 *****************************************************************************/
1071 static int Encode( x264_param_t *param, cli_opt_t *opt )
1076 int i_frame, i_frame_total;
1077 int64_t i_start, i_end;
1082 i_frame_total = p_get_frame_total( opt->hin );
1083 i_frame_total -= opt->i_seek;
1084 if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
1085 && param->i_frame_total > 0 )
1086 i_frame_total = param->i_frame_total;
1087 param->i_frame_total = i_frame_total;
1089 if( ( h = x264_encoder_open( param ) ) == NULL )
1091 fprintf( stderr, "x264_encoder_open failed\n" );
1092 p_close_infile( opt->hin );
1093 p_close_outfile( opt->hout );
1097 if( p_set_outfile_param( opt->hout, param ) )
1099 fprintf( stderr, "can't set outfile param\n" );
1100 p_close_infile( opt->hin );
1101 p_close_outfile( opt->hout );
1105 /* Create a new pic */
1106 x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
1108 i_start = x264_mdate();
1111 for( i_frame = 0, i_file = 0, i_progress = 0;
1112 b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
1114 if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
1117 pic.i_pts = (int64_t)i_frame * param->i_fps_den;
1119 i_file += Encode_frame( h, opt->hout, &pic );
1123 /* update status line (up to 1000 times per input file) */
1124 if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&
1125 ( i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
1126 : i_frame % 10 == 0 ) )
1128 int64_t i_elapsed = x264_mdate() - i_start;
1129 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1132 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1133 i_progress = i_frame * 1000 / i_frame_total;
1134 fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps, eta %d:%02d:%02d \r",
1135 i_frame, i_frame_total, (float)i_progress / 10, fps,
1136 eta/3600, (eta/60)%60, eta%60 );
1139 fprintf( stderr, "encoded frames: %d, %.2f fps \r", i_frame, fps );
1140 fflush( stderr ); // needed in windows
1143 /* Flush delayed B-frames */
1146 i_frame_size = Encode_frame( h, opt->hout, NULL );
1147 } while( i_frame_size );
1149 i_end = x264_mdate();
1150 x264_picture_clean( &pic );
1151 x264_encoder_close( h );
1152 fprintf( stderr, "\n" );
1155 fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );
1157 p_close_infile( opt->hin );
1158 p_close_outfile( opt->hout );
1162 double fps = (double)i_frame * (double)1000000 /
1163 (double)( i_end - i_start );
1165 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,
1166 (double) i_file * 8 * param->i_fps_num /
1167 ( (double) param->i_fps_den * i_frame * 1000 ) );