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>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #include <time.h>//Myo
27 #include <sys/timeb.h>//Myo
33 #include "common/common.h"
34 #include "common/cpu.h"
45 #define SetConsoleTitle(t)
48 uint8_t *mux_buffer
= NULL
;
49 int mux_buffer_size
= 0;
52 static int b_ctrl_c
= 0;
53 static int b_exit_on_ctrl_c
= 0;
54 static void SigIntHandler( int a
)
56 if( b_exit_on_ctrl_c
)
69 /* input file operation function pointers */
70 int (*p_open_infile
)( char *psz_filename
, hnd_t
*p_handle
, x264_param_t
*p_param
);
71 int (*p_get_frame_total
)( hnd_t handle
);
72 int (*p_read_frame
)( x264_picture_t
*p_pic
, hnd_t handle
, int i_frame
);
73 int (*p_close_infile
)( hnd_t handle
);
75 /* output file operation function pointers */
76 static int (*p_open_outfile
)( char *psz_filename
, hnd_t
*p_handle
);
77 static int (*p_set_outfile_param
)( hnd_t handle
, x264_param_t
*p_param
);
78 static int (*p_write_nalu
)( hnd_t handle
, uint8_t *p_nal
, int i_size
);
79 static int (*p_set_eop
)( hnd_t handle
, x264_picture_t
*p_picture
);
80 static int (*p_close_outfile
)( hnd_t handle
);
82 static void Help( x264_param_t
*defaults
, int b_longhelp
);
83 static int Parse( int argc
, char **argv
, x264_param_t
*param
, cli_opt_t
*opt
);
84 static int Encode( x264_param_t
*param
, cli_opt_t
*opt
);
87 /****************************************************************************
89 ****************************************************************************/
90 int main( int argc
, char **argv
)
96 struct timeb tstruct1
;//Myo
97 struct timeb tstruct2
;//Myo
99 #ifdef PTW32_STATIC_LIB
100 pthread_win32_process_attach_np();
101 pthread_win32_thread_attach_np();
105 _setmode(_fileno(stdin
), _O_BINARY
);
106 _setmode(_fileno(stdout
), _O_BINARY
);
109 ftime (&tstruct1
);// start time ms, Myo
110 x264_param_default( ¶m
);
112 /* Parse command line */
113 if( Parse( argc
, argv
, ¶m
, &opt
) < 0 )
116 /* Control-C handler */
117 signal( SIGINT
, SigIntHandler
);
119 ret
= Encode( ¶m
, &opt
);
121 #ifdef PTW32_STATIC_LIB
122 pthread_win32_thread_detach_np();
123 pthread_win32_process_detach_np();
126 ftime(&tstruct2
);// end time ms, Myo
127 me_tmp_time
=(tstruct2
.time
*1000+tstruct2
.millitm
) - (tstruct1
.time
*1000+tstruct1
.millitm
);//Myo
128 printf( "\nEncoding Time = %d\n", me_tmp_time
);//Myo
132 static char const *strtable_lookup( const char * const table
[], int index
)
134 int i
= 0; while( table
[i
] ) i
++;
135 return ( ( index
>= 0 && index
< i
) ? table
[ index
] : "???" );
138 /*****************************************************************************
140 *****************************************************************************/
141 static void Help( x264_param_t
*defaults
, int b_longhelp
)
144 #define H1 if(b_longhelp) printf
145 H0( "x264 core:%d%s\n"
146 "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
148 "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
149 " or YUV4MPEG 4:2:0 (*.y4m),\n"
150 " or AVI or Avisynth if compiled with AVIS support (%s).\n"
151 "Outfile type is selected by filename:\n"
152 " .264 -> Raw bytestream\n"
153 " .mkv -> Matroska\n"
154 " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
158 " -h, --help List the more commonly used options\n"
159 " --longhelp List all options\n"
161 X264_BUILD
, X264_VERSION
,
173 H0( "Frame-type options:\n" );
175 H0( " -I, --keyint <integer> Maximum GOP size [%d]\n", defaults
->i_keyint_max
);
176 H1( " -i, --min-keyint <integer> Minimum GOP size [%d]\n", defaults
->i_keyint_min
);
177 H1( " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n", defaults
->i_scenecut_threshold
);
178 H1( " --pre-scenecut Faster, less precise scenecut detection.\n"
179 " Required and implied by multi-threading.\n" );
180 H0( " -b, --bframes <integer> Number of B-frames between I and P [%d]\n", defaults
->i_bframe
);
181 H1( " --b-adapt Adaptive B-frame decision method [%d]\n"
182 " Higher values may lower threading efficiency.\n"
185 " - 2: Optimal (slow with high --bframes)\n", defaults
->i_bframe_adaptive
);
186 H1( " --b-bias <integer> Influences how often B-frames are used [%d]\n", defaults
->i_bframe_bias
);
187 H0( " --b-pyramid Keep some B-frames as references\n" );
188 H0( " --no-cabac Disable CABAC\n" );
189 H0( " -r, --ref <integer> Number of reference frames [%d]\n", defaults
->i_frame_reference
);
190 H1( " --no-deblock Disable loop filter\n" );
191 H0( " -f, --deblock <alpha:beta> Loop filter AlphaC0 and Beta parameters [%d:%d]\n",
192 defaults
->i_deblocking_filter_alphac0
, defaults
->i_deblocking_filter_beta
);
193 H0( " --interlaced Enable pure-interlaced mode\n" );
195 H0( "Ratecontrol:\n" );
197 H0( " -q, --qp <integer> Set QP (0=lossless) [%d]\n", defaults
->rc
.i_qp_constant
);
198 H0( " -B, --bitrate <integer> Set bitrate (kbit/s)\n" );
199 H0( " --crf <float> Quality-based VBR (nominal QP)\n" );
200 H1( " --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults
->rc
.i_vbv_max_bitrate
);
201 H0( " --vbv-bufsize <integer> Enable CBR and set size of the VBV buffer (kbit) [%d]\n", defaults
->rc
.i_vbv_buffer_size
);
202 H1( " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n", defaults
->rc
.f_vbv_buffer_init
);
203 H1( " --qpmin <integer> Set min QP [%d]\n", defaults
->rc
.i_qp_min
);
204 H1( " --qpmax <integer> Set max QP [%d]\n", defaults
->rc
.i_qp_max
);
205 H1( " --qpstep <integer> Set max QP step [%d]\n", defaults
->rc
.i_qp_step
);
206 H0( " --ratetol <float> Allowed variance of average bitrate [%.1f]\n", defaults
->rc
.f_rate_tolerance
);
207 H0( " --ipratio <float> QP factor between I and P [%.2f]\n", defaults
->rc
.f_ip_factor
);
208 H0( " --pbratio <float> QP factor between P and B [%.2f]\n", defaults
->rc
.f_pb_factor
);
209 H1( " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n", defaults
->analyse
.i_chroma_qp_offset
);
210 H1( " --aq-mode <integer> AQ method [%d]\n"
212 " - 1: Variance AQ (complexity mask)\n", defaults
->rc
.i_aq_mode
);
213 H0( " --aq-strength <float> Reduces blocking and blurring in flat and\n"
214 " textured areas. [%.1f]\n"
216 " - 1.5: strong AQ\n", defaults
->rc
.f_aq_strength
);
218 H0( " -p, --pass <1|2|3> Enable multipass ratecontrol\n"
219 " - 1: First pass, creates stats file\n"
220 " - 2: Last pass, does not overwrite stats file\n"
221 " - 3: Nth pass, overwrites stats file\n" );
222 H0( " --stats <string> Filename for 2 pass stats [\"%s\"]\n", defaults
->rc
.psz_stat_out
);
223 H0( " --qcomp <float> QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n", defaults
->rc
.f_qcompress
);
224 H1( " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults
->rc
.f_complexity_blur
);
225 H1( " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults
->rc
.f_qblur
);
226 H0( " --zones <zone0>/<zone1>/... Tweak the bitrate of some regions of the video\n" );
227 H1( " Each zone is of the form\n"
228 " <start frame>,<end frame>,<option>\n"
229 " where <option> is either\n"
230 " q=<integer> (force QP)\n"
231 " or b=<float> (bitrate multiplier)\n" );
232 H1( " --qpfile <string> Force frametypes and QPs\n" );
236 H0( " -A, --partitions <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
237 " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
239 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
240 H0( " --direct <string> Direct MV prediction mode [\"%s\"]\n"
241 " - none, spatial, temporal, auto\n",
242 strtable_lookup( x264_direct_pred_names
, defaults
->analyse
.i_direct_mv_pred
) );
243 H1( " --direct-8x8 <-1|0|1> Direct prediction size [%d]\n"
246 " - -1: smallest possible according to level\n",
247 defaults
->analyse
.i_direct_8x8_inference
);
248 H0( " -w, --weightb Weighted prediction for B-frames\n" );
249 H0( " --me <string> Integer pixel motion estimation method [\"%s\"]\n",
250 strtable_lookup( x264_motion_est_names
, defaults
->analyse
.i_me_method
) );
251 H1( " - dia: diamond search, radius 1 (fast)\n"
252 " - hex: hexagonal search, radius 2\n"
253 " - umh: uneven multi-hexagon search\n"
254 " - esa: exhaustive search\n"
255 " - tesa: hadamard exhaustive search (slow)\n" );
256 else H0( " - dia, hex, umh\n" );
257 H0( " --merange <integer> Maximum motion vector search range [%d]\n", defaults
->analyse
.i_me_range
);
258 H1( " --mvrange <integer> Maximum motion vector length [-1 (auto)]\n" );
259 H1( " --mvrange-thread <int> Minimum buffer between threads [-1 (auto)]\n" );
260 H0( " -m, --subme <integer> Subpixel motion estimation and mode decision [%d]\n", defaults
->analyse
.i_subpel_refine
);
261 H1( " - 0: fullpel only (not recommended)\n"
262 " - 1: SAD mode decision, one qpel iteration\n"
263 " - 2: SATD mode decision\n"
264 " - 3-5: Progressively more qpel\n"
265 " - 6: RD mode decision for I/P-frames\n"
266 " - 7: RD mode decision for all frames\n"
267 " - 8: RD refinement for I/P-frames\n"
268 " - 9: RD refinement for all frames\n" );
269 else H0( " decision quality: 1=fast, 9=best.\n" );
270 H0( " --psy-rd Strength of psychovisual optimization [\"%.1f:%.1f\"]\n"
271 " #1: RD (requires subme>=6)\n"
272 " #2: Trellis (requires trellis, experimental)\n",
273 defaults
->analyse
.f_psy_rd
, defaults
->analyse
.f_psy_trellis
);
274 H0( " --mixed-refs Decide references on a per partition basis\n" );
275 H1( " --no-chroma-me Ignore chroma in motion estimation\n" );
276 H0( " -8, --8x8dct Adaptive spatial transform size\n" );
277 H0( " -t, --trellis <integer> Trellis RD quantization. Requires CABAC. [%d]\n"
279 " - 1: enabled only on the final encode of a MB\n"
280 " - 2: enabled on all mode decisions\n", defaults
->analyse
.i_trellis
);
281 H0( " --no-fast-pskip Disables early SKIP detection on P-frames\n" );
282 H0( " --no-dct-decimate Disables coefficient thresholding on P-frames\n" );
283 H0( " --nr <integer> Noise reduction [%d]\n", defaults
->analyse
.i_noise_reduction
);
285 H1( " --deadzone-inter <int> Set the size of the inter luma quantization deadzone [%d]\n", defaults
->analyse
.i_luma_deadzone
[0] );
286 H1( " --deadzone-intra <int> Set the size of the intra luma quantization deadzone [%d]\n", defaults
->analyse
.i_luma_deadzone
[1] );
287 H1( " Deadzones should be in the range 0 - 32.\n" );
288 H1( " --cqm <string> Preset quant matrices [\"flat\"]\n"
290 H0( " --cqmfile <string> Read custom quant matrices from a JM-compatible file\n" );
291 H1( " Overrides any other --cqm* options.\n" );
292 H1( " --cqm4 <list> Set all 4x4 quant matrices\n"
293 " Takes a comma-separated list of 16 integers.\n" );
294 H1( " --cqm8 <list> Set all 8x8 quant matrices\n"
295 " Takes a comma-separated list of 64 integers.\n" );
296 H1( " --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
297 " Set both luma and chroma quant matrices\n" );
298 H1( " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
299 " Set individual quant matrices\n" );
301 H1( "Video Usability Info (Annex E):\n" );
302 H1( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
303 H1( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
305 H1( " --overscan <string> Specify crop overscan setting [\"%s\"]\n"
306 " - undef, show, crop\n",
307 strtable_lookup( x264_overscan_names
, defaults
->vui
.i_overscan
) );
308 H1( " --videoformat <string> Specify video format [\"%s\"]\n"
309 " - component, pal, ntsc, secam, mac, undef\n",
310 strtable_lookup( x264_vidformat_names
, defaults
->vui
.i_vidformat
) );
311 H1( " --fullrange <string> Specify full range samples setting [\"%s\"]\n"
313 strtable_lookup( x264_fullrange_names
, defaults
->vui
.b_fullrange
) );
314 H1( " --colorprim <string> Specify color primaries [\"%s\"]\n"
315 " - undef, bt709, bt470m, bt470bg\n"
316 " smpte170m, smpte240m, film\n",
317 strtable_lookup( x264_colorprim_names
, defaults
->vui
.i_colorprim
) );
318 H1( " --transfer <string> Specify transfer characteristics [\"%s\"]\n"
319 " - undef, bt709, bt470m, bt470bg, linear,\n"
320 " log100, log316, smpte170m, smpte240m\n",
321 strtable_lookup( x264_transfer_names
, defaults
->vui
.i_transfer
) );
322 H1( " --colormatrix <string> Specify color matrix setting [\"%s\"]\n"
323 " - undef, bt709, fcc, bt470bg\n"
324 " smpte170m, smpte240m, GBR, YCgCo\n",
325 strtable_lookup( x264_colmatrix_names
, defaults
->vui
.i_colmatrix
) );
326 H1( " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n",
327 defaults
->vui
.i_chroma_loc
);
329 H0( "Input/Output:\n" );
331 H0( " -o, --output Specify output file\n" );
332 H0( " --sar width:height Specify Sample Aspect Ratio\n" );
333 H0( " --fps <float|rational> Specify framerate\n" );
334 H0( " --seek <integer> First frame to encode\n" );
335 H0( " --frames <integer> Maximum number of frames to encode\n" );
336 H0( " --level <string> Specify level (as defined by Annex A)\n" );
338 H0( " -v, --verbose Print stats for each frame\n" );
339 H0( " --progress Show a progress indicator while encoding\n" );
340 H0( " --quiet Quiet Mode\n" );
341 H0( " --no-psnr Disable PSNR computation\n" );
342 H0( " --no-ssim Disable SSIM computation\n" );
343 H0( " --threads <integer> Parallel encoding\n" );
344 H0( " --thread-input Run Avisynth in its own thread\n" );
345 H1( " --non-deterministic Slightly improve quality of SMP, at the cost of repeatability\n" );
346 H1( " --asm <integer> Override CPU detection\n" );
347 H1( " --no-asm Disable all CPU optimizations\n" );
348 H1( " --visualize Show MB types overlayed on the encoded video\n" );
349 H1( " --dump-yuv <string> Save reconstructed frames\n" );
350 H1( " --sps-id <integer> Set SPS and PPS id numbers [%d]\n", defaults
->i_sps_id
);
351 H1( " --aud Use access unit delimiters\n" );
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 */
387 int long_options_index
= -1;
389 #define OPT_FRAMES 256
391 #define OPT_QPFILE 258
392 #define OPT_THREAD_INPUT 259
393 #define OPT_QUIET 260
394 #define OPT_PROGRESS 261
395 #define OPT_VISUALIZE 262
396 #define OPT_LONGHELP 263
398 static struct option long_options
[] =
400 { "help", no_argument
, NULL
, 'h' },
401 { "longhelp",no_argument
, NULL
, OPT_LONGHELP
},
402 { "version", no_argument
, NULL
, 'V' },
403 { "bitrate", required_argument
, NULL
, 'B' },
404 { "bframes", required_argument
, NULL
, 'b' },
405 { "b-adapt", required_argument
, NULL
, 0 },
406 { "no-b-adapt", no_argument
, NULL
, 0 },
407 { "b-bias", required_argument
, NULL
, 0 },
408 { "b-pyramid", no_argument
, NULL
, 0 },
409 { "min-keyint",required_argument
,NULL
,'i' },
410 { "keyint", required_argument
, NULL
, 'I' },
411 { "scenecut",required_argument
, NULL
, 0 },
412 { "pre-scenecut", no_argument
, NULL
, 0 },
413 { "nf", no_argument
, NULL
, 0 },
414 { "no-deblock", no_argument
, NULL
, 0 },
415 { "filter", required_argument
, NULL
, 0 },
416 { "deblock", required_argument
, NULL
, 'f' },
417 { "interlaced", no_argument
, NULL
, 0 },
418 { "no-cabac",no_argument
, NULL
, 0 },
419 { "qp", required_argument
, NULL
, 'q' },
420 { "qpmin", required_argument
, NULL
, 0 },
421 { "qpmax", required_argument
, NULL
, 0 },
422 { "qpstep", required_argument
, NULL
, 0 },
423 { "crf", required_argument
, NULL
, 0 },
424 { "ref", required_argument
, NULL
, 'r' },
425 { "asm", required_argument
, NULL
, 0 },
426 { "no-asm", no_argument
, NULL
, 0 },
427 { "sar", required_argument
, NULL
, 0 },
428 { "fps", required_argument
, NULL
, 0 },
429 { "frames", required_argument
, NULL
, OPT_FRAMES
},
430 { "seek", required_argument
, NULL
, OPT_SEEK
},
431 { "output", required_argument
, NULL
, 'o' },
432 { "analyse", required_argument
, NULL
, 0 },
433 { "partitions", required_argument
, NULL
, 'A' },
434 { "direct", required_argument
, NULL
, 0 },
435 { "direct-8x8", required_argument
, NULL
, 0 },
436 { "weightb", no_argument
, NULL
, 'w' },
437 { "me", required_argument
, NULL
, 0 },
438 { "merange", required_argument
, NULL
, 0 },
439 { "mvrange", required_argument
, NULL
, 0 },
440 { "mvrange-thread", required_argument
, NULL
, 0 },
441 { "subme", required_argument
, NULL
, 'm' },
442 { "psy-rd", required_argument
, NULL
, 0 },
443 { "mixed-refs", no_argument
, NULL
, 0 },
444 { "no-chroma-me", no_argument
, NULL
, 0 },
445 { "8x8dct", no_argument
, NULL
, '8' },
446 { "trellis", required_argument
, NULL
, 't' },
447 { "no-fast-pskip", no_argument
, NULL
, 0 },
448 { "no-dct-decimate", no_argument
, NULL
, 0 },
449 { "aq-strength", required_argument
, NULL
, 0 },
450 { "aq-mode", required_argument
, NULL
, 0 },
451 { "deadzone-inter", required_argument
, NULL
, '0' },
452 { "deadzone-intra", required_argument
, NULL
, '0' },
453 { "level", required_argument
, NULL
, 0 },
454 { "ratetol", required_argument
, NULL
, 0 },
455 { "vbv-maxrate", required_argument
, NULL
, 0 },
456 { "vbv-bufsize", required_argument
, NULL
, 0 },
457 { "vbv-init", required_argument
,NULL
, 0 },
458 { "ipratio", required_argument
, NULL
, 0 },
459 { "pbratio", required_argument
, NULL
, 0 },
460 { "chroma-qp-offset", required_argument
, NULL
, 0 },
461 { "pass", required_argument
, NULL
, 'p' },
462 { "stats", required_argument
, NULL
, 0 },
463 { "qcomp", required_argument
, NULL
, 0 },
464 { "qblur", required_argument
, NULL
, 0 },
465 { "cplxblur",required_argument
, NULL
, 0 },
466 { "zones", required_argument
, NULL
, 0 },
467 { "qpfile", required_argument
, NULL
, OPT_QPFILE
},
468 { "threads", required_argument
, NULL
, 0 },
469 { "thread-input", no_argument
, NULL
, OPT_THREAD_INPUT
},
470 { "non-deterministic", no_argument
, NULL
, 0 },
471 { "no-psnr", no_argument
, NULL
, 0 },
472 { "no-ssim", no_argument
, NULL
, 0 },
473 { "quiet", no_argument
, NULL
, OPT_QUIET
},
474 { "verbose", no_argument
, NULL
, 'v' },
475 { "progress",no_argument
, NULL
, OPT_PROGRESS
},
476 { "visualize",no_argument
, NULL
, OPT_VISUALIZE
},
477 { "dump-yuv",required_argument
, NULL
, 0 },
478 { "sps-id", required_argument
, NULL
, 0 },
479 { "aud", no_argument
, NULL
, 0 },
480 { "nr", required_argument
, NULL
, 0 },
481 { "cqm", required_argument
, NULL
, 0 },
482 { "cqmfile", required_argument
, NULL
, 0 },
483 { "cqm4", required_argument
, NULL
, 0 },
484 { "cqm4i", required_argument
, NULL
, 0 },
485 { "cqm4iy", required_argument
, NULL
, 0 },
486 { "cqm4ic", required_argument
, NULL
, 0 },
487 { "cqm4p", required_argument
, NULL
, 0 },
488 { "cqm4py", required_argument
, NULL
, 0 },
489 { "cqm4pc", required_argument
, NULL
, 0 },
490 { "cqm8", required_argument
, NULL
, 0 },
491 { "cqm8i", required_argument
, NULL
, 0 },
492 { "cqm8p", required_argument
, NULL
, 0 },
493 { "overscan", required_argument
, NULL
, 0 },
494 { "videoformat", required_argument
, NULL
, 0 },
495 { "fullrange", required_argument
, NULL
, 0 },
496 { "colorprim", required_argument
, NULL
, 0 },
497 { "transfer", required_argument
, NULL
, 0 },
498 { "colormatrix", required_argument
, NULL
, 0 },
499 { "chromaloc", required_argument
, NULL
, 0 },
503 int c
= getopt_long( argc
, argv
, "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw",
504 long_options
, &long_options_index
);
514 Help( &defaults
, 0 );
517 Help( &defaults
, 1 );
521 printf( "x264 "X264_POINTVER
"\n" );
523 printf( "x264 0.%d.X\n", X264_BUILD
);
525 printf( "built on " __DATE__
", " );
527 printf( "gcc: " __VERSION__
"\n" );
529 printf( "using a non-gcc compiler\n" );
533 param
->i_frame_total
= atoi( optarg
);
536 opt
->i_seek
= atoi( optarg
);
539 if( !strncasecmp(optarg
+ strlen(optarg
) - 4, ".mp4", 4) )
542 p_open_outfile
= open_file_mp4
;
543 p_write_nalu
= write_nalu_mp4
;
544 p_set_outfile_param
= set_param_mp4
;
545 p_set_eop
= set_eop_mp4
;
546 p_close_outfile
= close_file_mp4
;
548 fprintf( stderr
, "x264 [error]: not compiled with MP4 output support\n" );
552 else if( !strncasecmp(optarg
+ strlen(optarg
) - 4, ".mkv", 4) )
554 p_open_outfile
= open_file_mkv
;
555 p_write_nalu
= write_nalu_mkv
;
556 p_set_outfile_param
= set_param_mkv
;
557 p_set_eop
= set_eop_mkv
;
558 p_close_outfile
= close_file_mkv
;
560 if( !strcmp(optarg
, "-") )
562 else if( p_open_outfile( optarg
, &opt
->hout
) )
564 fprintf( stderr
, "x264 [error]: can't open output file `%s'\n", optarg
);
569 opt
->qpfile
= fopen( optarg
, "r" );
572 fprintf( stderr
, "x264 [error]: can't open `%s'\n", optarg
);
575 param
->i_scenecut_threshold
= -1;
576 param
->i_bframe_adaptive
= X264_B_ADAPT_NONE
;
578 case OPT_THREAD_INPUT
:
582 param
->i_log_level
= X264_LOG_NONE
;
585 param
->i_log_level
= X264_LOG_DEBUG
;
592 param
->b_visualize
= 1;
593 b_exit_on_ctrl_c
= 1;
595 fprintf( stderr
, "x264 [warning]: not compiled with visualization support\n" );
601 if( long_options_index
< 0 )
603 for( i
= 0; long_options
[i
].name
; i
++ )
604 if( long_options
[i
].val
== c
)
606 long_options_index
= i
;
609 if( long_options_index
< 0 )
611 /* getopt_long already printed an error message */
616 b_error
|= x264_param_parse( param
, long_options
[long_options_index
].name
, optarg
);
622 const char *name
= long_options_index
> 0 ? long_options
[long_options_index
].name
: argv
[optind
-2];
623 fprintf( stderr
, "x264 [error]: invalid argument: %s = %s\n", name
, optarg
);
628 /* Get the file name */
629 if( optind
> argc
- 1 || !opt
->hout
)
631 fprintf( stderr
, "x264 [error]: No %s file. Run x264 --help for a list of options.\n",
632 optind
> argc
- 1 ? "input" : "output" );
635 psz_filename
= argv
[optind
++];
637 /* check demuxer type */
638 psz
= psz_filename
+ strlen(psz_filename
) - 1;
639 while( psz
> psz_filename
&& *psz
!= '.' )
641 if( !strncasecmp( psz
, ".avi", 4 ) || !strncasecmp( psz
, ".avs", 4 ) )
643 if( !strncasecmp( psz
, ".y4m", 4 ) )
646 if( !(b_avis
|| b_y4m
) ) // raw yuv
648 if( optind
> argc
- 1 )
650 /* try to parse the file name */
651 for( psz
= psz_filename
; *psz
; psz
++ )
653 if( *psz
>= '0' && *psz
<= '9'
654 && sscanf( psz
, "%ux%u", ¶m
->i_width
, ¶m
->i_height
) == 2 )
656 if( param
->i_log_level
>= X264_LOG_INFO
)
657 fprintf( stderr
, "x264 [info]: file name gives %dx%d\n", param
->i_width
, param
->i_height
);
664 sscanf( argv
[optind
++], "%ux%u", ¶m
->i_width
, ¶m
->i_height
);
668 if( !(b_avis
|| b_y4m
) && ( !param
->i_width
|| !param
->i_height
) )
670 fprintf( stderr
, "x264 [error]: Rawyuv input requires a resolution.\n" );
679 p_open_infile
= open_file_avis
;
680 p_get_frame_total
= get_frame_total_avis
;
681 p_read_frame
= read_frame_avis
;
682 p_close_infile
= close_file_avis
;
684 fprintf( stderr
, "x264 [error]: not compiled with AVIS input support\n" );
690 p_open_infile
= open_file_y4m
;
691 p_get_frame_total
= get_frame_total_y4m
;
692 p_read_frame
= read_frame_y4m
;
693 p_close_infile
= close_file_y4m
;
696 if( p_open_infile( psz_filename
, &opt
->hin
, param
) )
698 fprintf( stderr
, "x264 [error]: could not open input file '%s'\n", psz_filename
);
704 if( b_thread_input
|| param
->i_threads
> 1
705 || (param
->i_threads
== 0 && x264_cpu_num_processors() > 1) )
707 if( open_file_thread( NULL
, &opt
->hin
, param
) )
709 fprintf( stderr
, "x264 [warning]: threaded input failed\n" );
713 p_open_infile
= open_file_thread
;
714 p_get_frame_total
= get_frame_total_thread
;
715 p_read_frame
= read_frame_thread
;
716 p_close_infile
= close_file_thread
;
724 static void parse_qpfile( cli_opt_t
*opt
, x264_picture_t
*pic
, int i_frame
)
726 int num
= -1, qp
, ret
;
729 while( num
< i_frame
)
731 file_pos
= ftell( opt
->qpfile
);
732 ret
= fscanf( opt
->qpfile
, "%d %c %d\n", &num
, &type
, &qp
);
733 if( num
> i_frame
|| ret
== EOF
)
735 pic
->i_type
= X264_TYPE_AUTO
;
737 fseek( opt
->qpfile
, file_pos
, SEEK_SET
);
742 pic
->i_qpplus1
= qp
+1;
743 if ( type
== 'I' ) pic
->i_type
= X264_TYPE_IDR
;
744 else if( type
== 'i' ) pic
->i_type
= X264_TYPE_I
;
745 else if( type
== 'P' ) pic
->i_type
= X264_TYPE_P
;
746 else if( type
== 'B' ) pic
->i_type
= X264_TYPE_BREF
;
747 else if( type
== 'b' ) pic
->i_type
= X264_TYPE_B
;
749 if( ret
!= 3 || qp
< -1 || qp
> 51 )
751 fprintf( stderr
, "x264 [error]: can't parse qpfile for frame %d\n", i_frame
);
752 fclose( opt
->qpfile
);
754 pic
->i_type
= X264_TYPE_AUTO
;
761 /*****************************************************************************
763 *****************************************************************************/
765 static int Encode_frame( x264_t
*h
, hnd_t hout
, x264_picture_t
*pic
)
767 x264_picture_t pic_out
;
772 if( x264_encoder_encode( h
, &nal
, &i_nal
, pic
, &pic_out
) < 0 )
774 fprintf( stderr
, "x264 [error]: x264_encoder_encode failed\n" );
777 for( i
= 0; i
< i_nal
; i
++ )
781 if( mux_buffer_size
< nal
[i
].i_payload
* 3/2 + 4 )
783 mux_buffer_size
= nal
[i
].i_payload
* 2 + 4;
784 x264_free( mux_buffer
);
785 mux_buffer
= x264_malloc( mux_buffer_size
);
788 i_size
= mux_buffer_size
;
789 x264_nal_encode( mux_buffer
, &i_size
, 1, &nal
[i
] );
790 i_file
+= p_write_nalu( hout
, mux_buffer
, i_size
);
793 p_set_eop( hout
, &pic_out
);
798 static int Encode( x264_param_t
*param
, cli_opt_t
*opt
)
803 int i_frame
, i_frame_total
;
804 int64_t i_start
, i_end
;
807 int i_update_interval
;
810 opt
->b_progress
&= param
->i_log_level
< X264_LOG_DEBUG
;
811 i_frame_total
= p_get_frame_total( opt
->hin
);
812 i_frame_total
-= opt
->i_seek
;
813 if( ( i_frame_total
== 0 || param
->i_frame_total
< i_frame_total
)
814 && param
->i_frame_total
> 0 )
815 i_frame_total
= param
->i_frame_total
;
816 param
->i_frame_total
= i_frame_total
;
817 i_update_interval
= i_frame_total
? x264_clip3( i_frame_total
/ 1000, 1, 10 ) : 10;
819 if( ( h
= x264_encoder_open( param
) ) == NULL
)
821 fprintf( stderr
, "x264 [error]: x264_encoder_open failed\n" );
822 p_close_infile( opt
->hin
);
826 if( p_set_outfile_param( opt
->hout
, param
) )
828 fprintf( stderr
, "x264 [error]: can't set outfile param\n" );
829 p_close_infile( opt
->hin
);
830 p_close_outfile( opt
->hout
);
834 /* Create a new pic */
835 x264_picture_alloc( &pic
, X264_CSP_I420
, param
->i_width
, param
->i_height
);
837 i_start
= x264_mdate();
840 for( i_frame
= 0, i_file
= 0; b_ctrl_c
== 0 && (i_frame
< i_frame_total
|| i_frame_total
== 0); )
842 if( p_read_frame( &pic
, opt
->hin
, i_frame
+ opt
->i_seek
) )
845 pic
.i_pts
= (int64_t)i_frame
* param
->i_fps_den
;
848 parse_qpfile( opt
, &pic
, i_frame
+ opt
->i_seek
);
851 /* Do not force any parameters */
852 pic
.i_type
= X264_TYPE_AUTO
;
856 i_file
+= Encode_frame( h
, opt
->hout
, &pic
);
860 /* update status line (up to 1000 times per input file) */
861 if( opt
->b_progress
&& i_frame
% i_update_interval
== 0 )
863 int64_t i_elapsed
= x264_mdate() - i_start
;
864 double fps
= i_elapsed
> 0 ? i_frame
* 1000000. / i_elapsed
: 0;
865 double bitrate
= (double) i_file
* 8 * param
->i_fps_num
/ ( (double) param
->i_fps_den
* i_frame
* 1000 );
868 int eta
= i_elapsed
* (i_frame_total
- i_frame
) / ((int64_t)i_frame
* 1000000);
869 sprintf( buf
, "x264 [%.1f%%] %d/%d frames, %.2f fps, %.2f kb/s, eta %d:%02d:%02d",
870 100. * i_frame
/ i_frame_total
, i_frame
, i_frame_total
, fps
, bitrate
,
871 eta
/3600, (eta
/60)%60, eta
%60 );
875 sprintf( buf
, "x264 %d frames: %.2f fps, %.2f kb/s", i_frame
, fps
, bitrate
);
877 fprintf( stderr
, "%s \r", buf
+5 );
878 SetConsoleTitle( buf
);
879 fflush( stderr
); // needed in windows
882 /* Flush delayed B-frames */
885 i_frame_size
= Encode_frame( h
, opt
->hout
, NULL
);
886 } while( i_frame_size
);
888 i_end
= x264_mdate();
889 x264_picture_clean( &pic
);
890 /* Erase progress indicator before printing encoding stats. */
891 if( opt
->b_progress
)
892 fprintf( stderr
, " \r" );
893 x264_encoder_close( h
);
894 x264_free( mux_buffer
);
895 fprintf( stderr
, "\n" );
898 fprintf( stderr
, "aborted at input frame %d\n", opt
->i_seek
+ i_frame
);
900 p_close_infile( opt
->hin
);
901 p_close_outfile( opt
->hout
);
905 double fps
= (double)i_frame
* (double)1000000 /
906 (double)( i_end
- i_start
);
908 fprintf( stderr
, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame
, fps
,
909 (double) i_file
* 8 * param
->i_fps_num
/
910 ( (double) param
->i_fps_den
* i_frame
* 1000 ) );