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 " --aud Use access unit delimiters\n"
280 X264_BUILD, X264_VERSION,
291 defaults->i_keyint_max,
292 defaults->i_keyint_min,
293 defaults->i_scenecut_threshold,
295 defaults->i_bframe_bias,
296 defaults->i_frame_reference,
297 defaults->i_deblocking_filter_alphac0,
298 defaults->i_deblocking_filter_beta,
299 defaults->rc.i_qp_constant,
300 defaults->rc.i_qp_min,
301 defaults->rc.i_qp_max,
302 defaults->rc.i_qp_step,
303 defaults->rc.f_rate_tolerance,
304 defaults->rc.i_vbv_max_bitrate,
305 defaults->rc.i_vbv_buffer_size,
306 defaults->rc.f_vbv_buffer_init,
307 defaults->rc.f_ip_factor,
308 defaults->rc.f_pb_factor,
309 defaults->analyse.i_chroma_qp_offset,
310 defaults->rc.psz_stat_out,
311 defaults->rc.psz_rc_eq,
312 defaults->rc.f_qcompress,
313 defaults->rc.f_complexity_blur,
314 defaults->rc.f_qblur,
315 strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ),
316 strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ),
317 defaults->analyse.i_me_range,
318 defaults->analyse.i_subpel_refine,
319 defaults->analyse.i_trellis,
320 defaults->analyse.i_noise_reduction,
321 strtable_lookup( overscan_str, defaults->vui.i_overscan ),
322 strtable_lookup( vidformat_str, defaults->vui.i_vidformat ),
323 strtable_lookup( fullrange_str, defaults->vui.b_fullrange ),
324 strtable_lookup( colorprim_str, defaults->vui.i_colorprim ),
325 strtable_lookup( transfer_str, defaults->vui.i_transfer ),
326 strtable_lookup( colmatrix_str, defaults->vui.i_colmatrix ),
327 defaults->vui.i_chroma_loc
331 static int parse_enum( const char *arg, const char * const *names, int *dst )
334 for( i = 0; names[i]; i++ )
335 if( !strcmp( arg, names[i] ) )
343 static int parse_cqm( const char *str, uint8_t *cqm, int length )
348 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
351 } while( i < length && (str = strchr( str, ',' )) && str++ );
352 return (i == length) ? 0 : -1;
355 /*****************************************************************************
357 *****************************************************************************/
358 static int Parse( int argc, char **argv,
359 x264_param_t *param, cli_opt_t *opt )
361 char *psz_filename = NULL;
362 x264_param_t defaults = *param;
366 int b_thread_input = 0;
368 memset( opt, 0, sizeof(cli_opt_t) );
370 /* Default input file driver */
371 p_open_infile = open_file_yuv;
372 p_get_frame_total = get_frame_total_yuv;
373 p_read_frame = read_frame_yuv;
374 p_close_infile = close_file_yuv;
376 /* Default output file driver */
377 p_open_outfile = open_file_bsf;
378 p_set_outfile_param = set_param_bsf;
379 p_write_nalu = write_nalu_bsf;
380 p_set_eop = set_eop_bsf;
381 p_close_outfile = close_file_bsf;
383 /* Parse command line options */
384 opterr = 0; // no error message
388 int long_options_index;
389 #define OPT_QPMIN 256
390 #define OPT_QPMAX 257
391 #define OPT_QPSTEP 258
392 #define OPT_IPRATIO 260
393 #define OPT_PBRATIO 261
394 #define OPT_RATETOL 262
395 #define OPT_RCSTATS 264
397 #define OPT_QCOMP 266
398 #define OPT_NOPSNR 267
399 #define OPT_QUIET 268
400 #define OPT_SCENECUT 270
401 #define OPT_QBLUR 271
402 #define OPT_CPLXBLUR 272
403 #define OPT_FRAMES 273
405 #define OPT_DIRECT 275
406 #define OPT_LEVEL 276
407 #define OPT_NOBADAPT 277
408 #define OPT_BBIAS 278
409 #define OPT_BPYRAMID 279
410 #define OPT_CHROMA_QP 280
411 #define OPT_NO_CHROMA_ME 281
412 #define OPT_NO_CABAC 282
414 #define OPT_PROGRESS 284
416 #define OPT_MERANGE 286
417 #define OPT_VBVMAXRATE 287
418 #define OPT_VBVBUFSIZE 288
419 #define OPT_VBVINIT 289
420 #define OPT_VISUALIZE 290
422 #define OPT_ZONES 292
423 #define OPT_THREADS 293
426 #define OPT_CQM4I 296
427 #define OPT_CQM4IY 297
428 #define OPT_CQM4IC 298
429 #define OPT_CQM4P 299
430 #define OPT_CQM4PY 300
431 #define OPT_CQM4PC 301
433 #define OPT_CQM8I 303
434 #define OPT_CQM8P 304
435 #define OPT_CQMFILE 305
437 #define OPT_OVERSCAN 307
438 #define OPT_VIDFORMAT 308
439 #define OPT_FULLRANGE 309
440 #define OPT_COLOURPRIM 310
441 #define OPT_TRANSFER 311
442 #define OPT_COLOURMATRIX 312
443 #define OPT_CHROMALOC 313
444 #define OPT_MIXED_REFS 314
446 #define OPT_B_RDO 316
447 #define OPT_NO_FAST_PSKIP 317
450 #define OPT_THREAD_INPUT 320
451 #define OPT_NO_DCT_DECIMATE 321
453 static struct option long_options[] =
455 { "help", no_argument, NULL, 'h' },
456 { "bitrate", required_argument, NULL, 'B' },
457 { "bframes", required_argument, NULL, 'b' },
458 { "no-b-adapt", no_argument, NULL, OPT_NOBADAPT },
459 { "b-bias", required_argument, NULL, OPT_BBIAS },
460 { "b-pyramid", no_argument, NULL, OPT_BPYRAMID },
461 { "min-keyint",required_argument,NULL,'i' },
462 { "keyint", required_argument, NULL, 'I' },
463 { "scenecut",required_argument, NULL, OPT_SCENECUT },
464 { "nf", no_argument, NULL, 'n' },
465 { "filter", required_argument, NULL, 'f' },
466 { "no-cabac",no_argument, NULL, OPT_NO_CABAC },
467 { "qp", required_argument, NULL, 'q' },
468 { "qpmin", required_argument, NULL, OPT_QPMIN },
469 { "qpmax", required_argument, NULL, OPT_QPMAX },
470 { "qpstep", required_argument, NULL, OPT_QPSTEP },
471 { "crf", required_argument, NULL, OPT_CRF },
472 { "ref", required_argument, NULL, 'r' },
473 { "no-asm", no_argument, NULL, 'C' },
474 { "sar", required_argument, NULL, OPT_SAR },
475 { "fps", required_argument, NULL, OPT_FPS },
476 { "frames", required_argument, NULL, OPT_FRAMES },
477 { "seek", required_argument, NULL, OPT_SEEK },
478 { "output", required_argument, NULL, 'o' },
479 { "analyse", required_argument, NULL, 'A' },
480 { "direct", required_argument, NULL, OPT_DIRECT },
481 { "weightb", no_argument, NULL, 'w' },
482 { "me", required_argument, NULL, OPT_ME },
483 { "merange", required_argument, NULL, OPT_MERANGE },
484 { "subme", required_argument, NULL, 'm' },
485 { "b-rdo", no_argument, NULL, OPT_B_RDO },
486 { "mixed-refs", no_argument, NULL, OPT_MIXED_REFS },
487 { "no-chroma-me", no_argument, NULL, OPT_NO_CHROMA_ME },
488 { "bime", no_argument, NULL, OPT_BIME },
489 { "8x8dct", no_argument, NULL, '8' },
490 { "trellis", required_argument, NULL, 't' },
491 { "no-fast-pskip", no_argument, NULL, OPT_NO_FAST_PSKIP },
492 { "no-dct-decimate", no_argument, NULL, OPT_NO_DCT_DECIMATE },
493 { "level", required_argument, NULL, OPT_LEVEL },
494 { "ratetol", required_argument, NULL, OPT_RATETOL },
495 { "vbv-maxrate", required_argument, NULL, OPT_VBVMAXRATE },
496 { "vbv-bufsize", required_argument, NULL, OPT_VBVBUFSIZE },
497 { "vbv-init", required_argument,NULL, OPT_VBVINIT },
498 { "ipratio", required_argument, NULL, OPT_IPRATIO },
499 { "pbratio", required_argument, NULL, OPT_PBRATIO },
500 { "chroma-qp-offset", required_argument, NULL, OPT_CHROMA_QP },
501 { "pass", required_argument, NULL, 'p' },
502 { "stats", required_argument, NULL, OPT_RCSTATS },
503 { "rceq", required_argument, NULL, OPT_RCEQ },
504 { "qcomp", required_argument, NULL, OPT_QCOMP },
505 { "qblur", required_argument, NULL, OPT_QBLUR },
506 { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
507 { "zones", required_argument, NULL, OPT_ZONES },
508 { "threads", required_argument, NULL, OPT_THREADS },
509 { "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
510 { "no-psnr", no_argument, NULL, OPT_NOPSNR },
511 { "quiet", no_argument, NULL, OPT_QUIET },
512 { "verbose", no_argument, NULL, 'v' },
513 { "progress",no_argument, NULL, OPT_PROGRESS },
514 { "visualize",no_argument, NULL, OPT_VISUALIZE },
515 { "aud", no_argument, NULL, OPT_AUD },
516 { "nr", required_argument, NULL, OPT_NR },
517 { "cqm", required_argument, NULL, OPT_CQM },
518 { "cqmfile", required_argument, NULL, OPT_CQMFILE },
519 { "cqm4", required_argument, NULL, OPT_CQM4 },
520 { "cqm4i", required_argument, NULL, OPT_CQM4I },
521 { "cqm4iy", required_argument, NULL, OPT_CQM4IY },
522 { "cqm4ic", required_argument, NULL, OPT_CQM4IC },
523 { "cqm4p", required_argument, NULL, OPT_CQM4P },
524 { "cqm4py", required_argument, NULL, OPT_CQM4PY },
525 { "cqm4pc", required_argument, NULL, OPT_CQM4PC },
526 { "cqm8", required_argument, NULL, OPT_CQM8 },
527 { "cqm8i", required_argument, NULL, OPT_CQM8I },
528 { "cqm8p", required_argument, NULL, OPT_CQM8P },
529 { "overscan", required_argument, NULL, OPT_OVERSCAN },
530 { "videoformat", required_argument, NULL, OPT_VIDFORMAT },
531 { "fullrange", required_argument, NULL, OPT_FULLRANGE },
532 { "colorprim", required_argument, NULL, OPT_COLOURPRIM },
533 { "transfer", required_argument, NULL, OPT_TRANSFER },
534 { "colormatrix", required_argument, NULL, OPT_COLOURMATRIX },
535 { "chromaloc", required_argument, NULL, OPT_CHROMALOC },
541 c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:t:vw8",
542 long_options, &long_options_index);
558 param->rc.i_bitrate = atol( optarg );
562 param->rc.i_rf_constant = atol( optarg );
565 param->i_bframe = atol( optarg );
568 param->b_bframe_adaptive = 0;
571 param->i_bframe_bias = atol( optarg );
574 param->b_bframe_pyramid = 1;
577 param->i_keyint_min = atol( optarg );
578 if( param->i_keyint_max < param->i_keyint_min )
579 param->i_keyint_max = param->i_keyint_min;
582 param->i_keyint_max = atol( optarg );
583 if( param->i_keyint_min > param->i_keyint_max )
584 param->i_keyint_min = param->i_keyint_max;
587 param->i_scenecut_threshold = atol( optarg );
590 param->b_deblocking_filter = 0;
594 char *p = strchr( optarg, ':' );
595 if( !p ) p = strchr( optarg, ',' );
596 param->i_deblocking_filter_alphac0 = atoi( optarg );
597 param->i_deblocking_filter_beta = p ? atoi( p+1 ) : param->i_deblocking_filter_alphac0;
601 param->rc.i_qp_constant = atoi( optarg );
604 param->rc.i_qp_min = atoi( optarg );
607 param->rc.i_qp_max = atoi( optarg );
610 param->rc.i_qp_step = atoi( optarg );
613 param->i_frame_reference = atoi( optarg );
619 opt->b_decompress = 1;
625 param->i_frame_total = atoi( optarg );
628 opt->i_seek = atoi( optarg );
631 if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
634 p_open_outfile = open_file_mp4;
635 p_write_nalu = write_nalu_mp4;
636 p_set_outfile_param = set_param_mp4;
637 p_set_eop = set_eop_mp4;
638 p_close_outfile = close_file_mp4;
640 fprintf( stderr, "not compiled with MP4 output support\n" );
644 else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
646 p_open_outfile = open_file_mkv;
647 p_write_nalu = write_nalu_mkv;
648 p_set_outfile_param = set_param_mkv;
649 p_set_eop = set_eop_mkv;
650 p_close_outfile = close_file_mkv;
652 if( !strcmp(optarg, "-") )
654 else if( p_open_outfile( optarg, &opt->hout ) )
656 fprintf( stderr, "cannot open output file `%s'\n", optarg );
662 char *p = strchr( optarg, ':' );
663 if( !p ) p = strchr( optarg, '/' );
666 param->vui.i_sar_width = atoi( optarg );
667 param->vui.i_sar_height = atoi( p + 1 );
674 if( sscanf( optarg, "%d/%d", ¶m->i_fps_num, ¶m->i_fps_den ) == 2 )
676 else if( sscanf( optarg, "%f", &fps ) )
678 param->i_fps_num = (int)(fps * 1000 + .5);
679 param->i_fps_den = 1000;
683 fprintf( stderr, "bad fps `%s'\n", optarg );
689 param->analyse.inter = 0;
690 if( strstr( optarg, "none" ) ) param->analyse.inter = 0;
691 if( strstr( optarg, "all" ) ) param->analyse.inter = ~0;
693 if( strstr( optarg, "i4x4" ) ) param->analyse.inter |= X264_ANALYSE_I4x4;
694 if( strstr( optarg, "i8x8" ) ) param->analyse.inter |= X264_ANALYSE_I8x8;
695 if( strstr( optarg, "p8x8" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
696 if( strstr( optarg, "p4x4" ) ) param->analyse.inter |= X264_ANALYSE_PSUB8x8;
697 if( strstr( optarg, "b8x8" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16;
700 b_error |= parse_enum( optarg, x264_direct_pred_names, ¶m->analyse.i_direct_mv_pred );
703 param->analyse.b_weighted_bipred = 1;
706 b_error |= parse_enum( optarg, x264_motion_est_names, ¶m->analyse.i_me_method );
709 param->analyse.i_me_range = atoi(optarg);
712 param->analyse.i_subpel_refine = atoi(optarg);
715 param->analyse.b_bframe_rdo = 1;
718 param->analyse.b_mixed_references = 1;
720 case OPT_NO_CHROMA_ME:
721 param->analyse.b_chroma_me = 0;
724 param->analyse.b_bidir_me = 1;
727 param->analyse.b_transform_8x8 = 1;
730 param->analyse.i_trellis = atoi(optarg);
732 case OPT_NO_FAST_PSKIP:
733 param->analyse.b_fast_pskip = 0;
735 case OPT_NO_DCT_DECIMATE:
736 param->analyse.b_dct_decimate = 0;
739 if( atof(optarg) < 6 )
740 param->i_level_idc = (int)(10*atof(optarg)+.5);
742 param->i_level_idc = atoi(optarg);
745 param->rc.f_rate_tolerance = !strncmp("inf", optarg, 3) ? 1e9 : atof(optarg);
748 param->rc.i_vbv_max_bitrate = atoi( optarg );
751 param->rc.i_vbv_buffer_size = atoi( optarg );
754 param->rc.f_vbv_buffer_init = atof(optarg);
757 param->rc.f_ip_factor = atof(optarg);
760 param->rc.f_pb_factor = atof(optarg);
763 param->analyse.i_chroma_qp_offset = atoi(optarg);
767 int i_pass = atoi(optarg);
769 param->rc.b_stat_write = 1;
770 else if( i_pass == 2 )
771 param->rc.b_stat_read = 1;
772 else if( i_pass > 2 )
773 param->rc.b_stat_read =
774 param->rc.b_stat_write = 1;
778 param->rc.psz_stat_in = optarg;
779 param->rc.psz_stat_out = optarg;
782 param->rc.psz_rc_eq = optarg;
785 param->rc.f_qcompress = atof(optarg);
788 param->rc.f_qblur = atof(optarg);
791 param->rc.f_complexity_blur = atof(optarg);
794 param->rc.psz_zones = optarg;
797 param->i_threads = atoi(optarg);
799 case OPT_THREAD_INPUT:
803 param->analyse.b_psnr = 0;
806 param->i_log_level = X264_LOG_NONE;
809 param->i_log_level = X264_LOG_DEBUG;
819 param->b_visualize = 1;
820 b_exit_on_ctrl_c = 1;
822 fprintf( stderr, "not compiled with visualization support\n" );
826 param->analyse.i_noise_reduction = atoi(optarg);
829 if( strstr( optarg, "flat" ) )
830 param->i_cqm_preset = X264_CQM_FLAT;
831 else if( strstr( optarg, "jvt" ) )
832 param->i_cqm_preset = X264_CQM_JVT;
835 fprintf( stderr, "bad CQM preset `%s'\n", optarg );
840 param->psz_cqm_file = optarg;
843 param->i_cqm_preset = X264_CQM_CUSTOM;
844 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
845 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
846 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
847 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
850 param->i_cqm_preset = X264_CQM_CUSTOM;
851 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
852 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
855 param->i_cqm_preset = X264_CQM_CUSTOM;
856 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
857 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
860 param->i_cqm_preset = X264_CQM_CUSTOM;
861 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
862 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
865 param->i_cqm_preset = X264_CQM_CUSTOM;
866 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
869 param->i_cqm_preset = X264_CQM_CUSTOM;
870 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
873 param->i_cqm_preset = X264_CQM_CUSTOM;
874 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
877 param->i_cqm_preset = X264_CQM_CUSTOM;
878 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
881 param->i_cqm_preset = X264_CQM_CUSTOM;
882 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
885 param->i_cqm_preset = X264_CQM_CUSTOM;
886 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
889 b_error |= parse_enum( optarg, overscan_str, ¶m->vui.i_overscan );
892 b_error |= parse_enum( optarg, vidformat_str, ¶m->vui.i_vidformat );
895 b_error |= parse_enum( optarg, fullrange_str, ¶m->vui.b_fullrange );
898 b_error |= parse_enum( optarg, colorprim_str, ¶m->vui.i_colorprim );
901 b_error |= parse_enum( optarg, transfer_str, ¶m->vui.i_transfer );
903 case OPT_COLOURMATRIX:
904 b_error |= parse_enum( optarg, colmatrix_str, ¶m->vui.i_colmatrix );
907 param->vui.i_chroma_loc = atoi( optarg );
908 b_error = ( param->vui.i_chroma_loc < 0 || param->vui.i_chroma_loc > 5 );
911 fprintf( stderr, "unknown option (%c)\n", optopt );
917 fprintf( stderr, "bad argument: %s %s\n", argv[optind-2], optarg );
922 /* Get the file name */
923 if( optind > argc - 1 || !opt->hout )
928 psz_filename = argv[optind++];
930 if( !opt->b_decompress )
932 if( optind > argc - 1 )
934 /* try to parse the file name */
935 for( psz = psz_filename; *psz; psz++ )
937 if( *psz >= '0' && *psz <= '9'
938 && sscanf( psz, "%ux%u", ¶m->i_width, ¶m->i_height ) == 2 )
940 if( param->i_log_level >= X264_LOG_INFO )
941 fprintf( stderr, "x264 [info]: file name gives %dx%d\n", param->i_width, param->i_height );
948 sscanf( argv[optind++], "%ux%u", ¶m->i_width, ¶m->i_height );
951 /* check avis input */
952 psz = psz_filename + strlen(psz_filename) - 1;
953 while( psz > psz_filename && *psz != '.' )
956 if( !strncasecmp( psz, ".avi", 4 ) || !strncasecmp( psz, ".avs", 4 ) )
958 if( !strncasecmp( psz, ".y4m", 4 ) )
960 if( !(b_avis || b_y4m) && ( !param->i_width || !param->i_height ) )
972 p_open_infile = open_file_avis;
973 p_get_frame_total = get_frame_total_avis;
974 p_read_frame = read_frame_avis;
975 p_close_infile = close_file_avis;
977 fprintf( stderr, "not compiled with AVIS input support\n" );
983 p_open_infile = open_file_y4m;
984 p_get_frame_total = get_frame_total_y4m;
985 p_read_frame = read_frame_y4m;
986 p_close_infile = close_file_y4m;
989 if( p_open_infile( psz_filename, &opt->hin, param ) )
991 fprintf( stderr, "could not open input file '%s'\n", psz_filename );
997 if( b_thread_input || param->i_threads > 1 )
999 if( open_file_thread( NULL, &opt->hin, param ) )
1001 fprintf( stderr, "threaded input failed\n" );
1005 p_open_infile = open_file_thread;
1006 p_get_frame_total = get_frame_total_thread;
1007 p_read_frame = read_frame_thread;
1008 p_close_infile = close_file_thread;
1016 /*****************************************************************************
1018 *****************************************************************************/
1020 static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
1022 x264_picture_t pic_out;
1027 /* Do not force any parameters */
1030 pic->i_type = X264_TYPE_AUTO;
1033 if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
1035 fprintf( stderr, "x264_encoder_encode failed\n" );
1038 for( i = 0; i < i_nal; i++ )
1044 if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
1046 i_file += p_write_nalu( hout, data, i_size );
1048 else if( i_size < 0 )
1050 fprintf( stderr, "need to increase buffer size (size=%d)\n", -i_size );
1054 p_set_eop( hout, &pic_out );
1059 /*****************************************************************************
1061 *****************************************************************************/
1062 static int Encode( x264_param_t *param, cli_opt_t *opt )
1067 int i_frame, i_frame_total;
1068 int64_t i_start, i_end;
1073 i_frame_total = p_get_frame_total( opt->hin );
1074 i_frame_total -= opt->i_seek;
1075 if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
1076 && param->i_frame_total > 0 )
1077 i_frame_total = param->i_frame_total;
1078 param->i_frame_total = i_frame_total;
1080 if( ( h = x264_encoder_open( param ) ) == NULL )
1082 fprintf( stderr, "x264_encoder_open failed\n" );
1083 p_close_infile( opt->hin );
1084 p_close_outfile( opt->hout );
1088 if( p_set_outfile_param( opt->hout, param ) )
1090 fprintf( stderr, "can't set outfile param\n" );
1091 p_close_infile( opt->hin );
1092 p_close_outfile( opt->hout );
1096 /* Create a new pic */
1097 x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
1099 i_start = x264_mdate();
1102 for( i_frame = 0, i_file = 0, i_progress = 0;
1103 b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
1105 if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
1108 pic.i_pts = (int64_t)i_frame * param->i_fps_den;
1110 i_file += Encode_frame( h, opt->hout, &pic );
1114 /* update status line (up to 1000 times per input file) */
1115 if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&
1116 ( i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
1117 : i_frame % 10 == 0 ) )
1119 int64_t i_elapsed = x264_mdate() - i_start;
1120 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1123 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1124 i_progress = i_frame * 1000 / i_frame_total;
1125 fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps, eta %d:%02d:%02d \r",
1126 i_frame, i_frame_total, (float)i_progress / 10, fps,
1127 eta/3600, (eta/60)%60, eta%60 );
1130 fprintf( stderr, "encoded frames: %d, %.2f fps \r", i_frame, fps );
1131 fflush( stderr ); // needed in windows
1134 /* Flush delayed B-frames */
1137 i_frame_size = Encode_frame( h, opt->hout, NULL );
1138 } while( i_frame_size );
1140 i_end = x264_mdate();
1141 x264_picture_clean( &pic );
1142 x264_encoder_close( h );
1143 fprintf( stderr, "\n" );
1146 fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );
1148 p_close_infile( opt->hin );
1149 p_close_outfile( opt->hout );
1153 double fps = (double)i_frame * (double)1000000 /
1154 (double)( i_end - i_start );
1156 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,
1157 (double) i_file * 8 * param->i_fps_num /
1158 ( (double) param->i_fps_den * i_frame * 1000 ) );