More formatting fixes in x264 help
[x262.git] / x264.c
1 /*****************************************************************************
2  * x264: h264 encoder testing program.
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steven Walters <kemuri9@gmail.com>
9  *          Kieran Kunhya <kieran@kunhya.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #include <stdlib.h>
27 #include <math.h>
28
29 #include <signal.h>
30 #define _GNU_SOURCE
31 #include <getopt.h>
32
33 #include "common/common.h"
34 #include "common/cpu.h"
35 #include "x264.h"
36 #include "muxers.h"
37
38 #ifdef _WIN32
39 #include <windows.h>
40 #else
41 #define SetConsoleTitle(t)
42 #endif
43
44 /* Ctrl-C handler */
45 static int     b_ctrl_c = 0;
46 static int     b_exit_on_ctrl_c = 0;
47 static void    SigIntHandler( int a )
48 {
49     if( b_exit_on_ctrl_c )
50         exit(0);
51     b_ctrl_c = 1;
52 }
53
54 typedef struct {
55     int b_progress;
56     int i_seek;
57     hnd_t hin;
58     hnd_t hout;
59     FILE *qpfile;
60 } cli_opt_t;
61
62 /* i/o file operation function pointer structs */
63 cli_input_t input;
64 static cli_output_t output;
65
66 static const char * const demuxer_names[] =
67 {
68     "auto",
69     "yuv",
70     "y4m",
71 #ifdef AVS_INPUT
72     "avs",
73 #endif
74 #ifdef LAVF_INPUT
75     "lavf",
76 #endif
77 #ifdef FFMS_INPUT
78     "ffms",
79 #endif
80     0
81 };
82
83 static const char * const muxer_names[] =
84 {
85     "auto",
86     "raw",
87     "mkv",
88     "flv",
89 #ifdef MP4_OUTPUT
90     "mp4",
91 #endif
92     0
93 };
94
95 static void Help( x264_param_t *defaults, int longhelp );
96 static int  Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
97 static int  Encode( x264_param_t *param, cli_opt_t *opt );
98
99 /****************************************************************************
100  * main:
101  ****************************************************************************/
102 int main( int argc, char **argv )
103 {
104     x264_param_t param;
105     cli_opt_t opt;
106     int ret;
107
108 #ifdef PTW32_STATIC_LIB
109     pthread_win32_process_attach_np();
110     pthread_win32_thread_attach_np();
111 #endif
112
113 #ifdef _WIN32
114     _setmode(_fileno(stdin), _O_BINARY);
115     _setmode(_fileno(stdout), _O_BINARY);
116 #endif
117
118     x264_param_default( &param );
119
120     /* Parse command line */
121     if( Parse( argc, argv, &param, &opt ) < 0 )
122         return -1;
123
124     /* Control-C handler */
125     signal( SIGINT, SigIntHandler );
126
127     ret = Encode( &param, &opt );
128
129 #ifdef PTW32_STATIC_LIB
130     pthread_win32_thread_detach_np();
131     pthread_win32_process_detach_np();
132 #endif
133
134     return ret;
135 }
136
137 static char const *strtable_lookup( const char * const table[], int index )
138 {
139     int i = 0; while( table[i] ) i++;
140     return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
141 }
142
143 static char *stringify_names( char *buf, const char * const names[] )
144 {
145     int i = 0;
146     char *p = buf;
147     for( p[0] = 0; names[i]; i++ )
148     {
149         p += sprintf( p, "%s", names[i] );
150         if( names[i+1] )
151             p += sprintf( p, ", " );
152     }
153     return buf;
154 }
155
156 /*****************************************************************************
157  * Help:
158  *****************************************************************************/
159 static void Help( x264_param_t *defaults, int longhelp )
160 {
161     char buf[50];
162 #define H0 printf
163 #define H1 if(longhelp>=1) printf
164 #define H2 if(longhelp==2) printf
165     H0( "x264 core:%d%s\n"
166         "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
167         "\n"
168         "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
169         "  or YUV4MPEG 4:2:0 (*.y4m),\n"
170         "  or Avisynth if compiled with support (%s).\n"
171         "  or libav* formats if compiled with lavf support (%s) or ffms support (%s).\n"
172         "Outfile type is selected by filename:\n"
173         " .264 -> Raw bytestream\n"
174         " .mkv -> Matroska\n"
175         " .flv -> Flash Video\n"
176         " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
177         "\n"
178         "Options:\n"
179         "\n"
180         "  -h, --help                  List basic options\n"
181         "      --longhelp              List more options\n"
182         "      --fullhelp              List all options\n"
183         "\n",
184         X264_BUILD, X264_VERSION,
185 #ifdef AVS_INPUT
186         "yes",
187 #else
188         "no",
189 #endif
190 #ifdef LAVF_INPUT
191         "yes",
192 #else
193         "no",
194 #endif
195 #ifdef FFMS_INPUT
196         "yes",
197 #else
198         "no",
199 #endif
200 #ifdef MP4_OUTPUT
201         "yes"
202 #else
203         "no"
204 #endif
205       );
206     H0( "Example usage:\n" );
207     H0( "\n" );
208     H0( "      Constant quality mode:\n" );
209     H0( "            x264 --crf 24 -o <output> <input>\n" );
210     H0( "\n" );
211     H0( "      Two-pass with a bitrate of 1000kbps:\n" );
212     H0( "            x264 --pass 1 --bitrate 1000 -o <output> <input>\n" );
213     H0( "            x264 --pass 2 --bitrate 1000 -o <output> <input>\n" );
214     H0( "\n" );
215     H0( "      Lossless:\n" );
216     H0( "            x264 --crf 0 -o <output> <input>\n" );
217     H0( "\n" );
218     H0( "      Maximum PSNR at the cost of speed and visual quality:\n" );
219     H0( "            x264 --preset placebo --tune psnr -o <output> <input>\n" );
220     H0( "\n" );
221     H0( "      Constant bitrate at 1000kbps with a 2 second-buffer:\n");
222     H0( "            x264 --vbv-bufsize 2000 --bitrate 1000 -o <output> <input>\n" );
223     H0( "\n" );
224     H0( "Presets:\n" );
225     H0( "\n" );
226     H0( "      --profile               Force the limits of an H.264 profile [high]\n"
227         "                                  Overrides all settings.\n" );
228     H2( "                                  - baseline:\n"
229         "                                    --no-8x8dct --bframes 0 --no-cabac\n"
230         "                                    --cqm flat --weightp 0 No interlaced\n"
231         "                                    No lossless\n"
232         "                                  - main:\n"
233         "                                    --no-8x8dct --cqm flat No lossless\n"
234         "                                  - high:\n"
235         "                                    No lossless\n" );
236         else H0( "                                  - baseline,main,high\n" );
237     H0( "      --preset                Use a preset to select encoding settings [medium]\n"
238         "                                  Overridden by user settings.\n" );
239     H2( "                                  - ultrafast:\n"
240         "                                    --no-8x8dct --aq-mode 0 --b-adapt 0\n"
241         "                                    --bframes 0 --no-cabac --no-deblock\n"
242         "                                    --no-mbtree --me dia --no-mixed-refs\n"
243         "                                    --partitions none --ref 1 --scenecut 0\n"
244         "                                    --subme 0 --trellis 0 --no-weightb\n"
245         "                                    --weightp 0\n"
246         "                                  - veryfast:\n"
247         "                                    --no-mbtree --me dia --no-mixed-refs\n"
248         "                                    --partitions i8x8,i4x4 --ref 1\n"
249         "                                    --subme 1 --trellis 0 --weightp 0\n"
250         "                                  - faster:\n"
251         "                                    --no-mbtree --no-mixed-refs --ref 2\n"
252         "                                    --subme 4 --weightp 1\n"
253         "                                  - fast:\n"
254         "                                    --rc-lookahead 30 --ref 2 --subme 6\n"
255         "                                  - medium:\n"
256         "                                    Default settings apply.\n"
257         "                                  - slow:\n"
258         "                                    --b-adapt 2 --direct auto --me umh\n"
259         "                                    --rc-lookahead 50 --ref 5 --subme 8\n"
260         "                                  - slower:\n"
261         "                                    --b-adapt 2 --direct auto --me umh\n"
262         "                                    --partitions all --rc-lookahead 60\n"
263         "                                    --ref 8 --subme 9 --trellis 2\n"
264         "                                  - veryslow:\n"
265         "                                    --b-adapt 2 --bframes 8 --direct auto\n"
266         "                                    --me umh --merange 24 --partitions all\n"
267         "                                    --ref 16 --subme 10 --trellis 2\n"
268         "                                    --rc-lookahead 60\n"
269         "                                  - placebo:\n"
270         "                                    --bframes 16 --b-adapt 2 --direct auto\n"
271         "                                    --slow-firstpass --no-fast-pskip\n"
272         "                                    --me tesa --merange 24 --partitions all\n"
273         "                                    --rc-lookahead 60 --ref 16 --subme 10\n"
274         "                                    --trellis 2\n" );
275     else H0( "                                  - ultrafast,veryfast,faster,fast,medium\n"
276              "                                  - slow,slower,veryslow,placebo\n" );
277     H0( "      --tune                  Tune the settings for a particular type of source\n"
278         "                              or situation\n"
279         "                                  Overridden by user settings.\n"
280         "                                  Multiple tunings are separated by commas.\n"
281         "                                  Only one psy tuning can be used at a time.\n" );
282     H2( "                                  - film (psy tuning):\n"
283         "                                    --deblock -1:-1 --psy-rd <unset>:0.15\n"
284         "                                  - animation (psy tuning):\n"
285         "                                    --bframes {+2} --deblock 1:1\n"
286         "                                    --psy-rd 0.4:<unset> --aq-strength 0.6\n"
287         "                                    --ref {Double if >1 else 1}\n"
288         "                                  - grain (psy tuning):\n"
289         "                                    --aq-strength 0.5 --no-dct-decimate\n"
290         "                                    --deadzone-inter 6 --deadzone-intra 6\n"
291         "                                    --deblock -2:-2 --ipratio 1.1 \n"
292         "                                    --pbratio 1.1 --psy-rd <unset>:0.25\n"
293         "                                    --qcomp 0.8\n"
294         "                                  - psnr (psy tuning):\n"
295         "                                    --aq-mode 0 --no-psy\n"
296         "                                  - ssim (psy tuning):\n"
297         "                                    --aq-mode 2 --no-psy\n"
298         "                                  - fastdecode:\n"
299         "                                    --no-cabac --no-deblock --no-weightb\n"
300         "                                    --weightp 0\n"
301         "                                  - zerolatency:\n"
302         "                                    --bframes 0 --rc-lookahead 0\n"
303         "                                    --sync-lookahead 0 --sliced-threads\n" );
304     else H0( "                                  - psy tunings: film,animation,grain,psnr,ssim\n"
305              "                                  - other tunings: fastdecode,zerolatency\n" );
306     H1( "      --slow-firstpass        Don't use faster settings with --pass 1\n" );
307     H0( "\n" );
308     H0( "Frame-type options:\n" );
309     H0( "\n" );
310     H0( "  -I, --keyint <integer>      Maximum GOP size [%d]\n", defaults->i_keyint_max );
311     H2( "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n", defaults->i_keyint_min );
312     H2( "      --no-scenecut           Disable adaptive I-frame decision\n" );
313     H2( "      --scenecut <integer>    How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
314     H2( "      --intra-refresh         Use Periodic Intra Refresh instead of IDR frames\n" );
315     H1( "  -b, --bframes <integer>     Number of B-frames between I and P [%d]\n", defaults->i_bframe );
316     H1( "      --b-adapt <integer>     Adaptive B-frame decision method [%d]\n"
317         "                                  Higher values may lower threading efficiency.\n"
318         "                                  - 0: Disabled\n"
319         "                                  - 1: Fast\n"
320         "                                  - 2: Optimal (slow with high --bframes)\n", defaults->i_bframe_adaptive );
321     H2( "      --b-bias <integer>      Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
322     H1( "      --b-pyramid <string>    Keep some B-frames as references [%s]\n"
323         "                                  - none: Disabled\n"
324         "                                  - strict: Strictly hierarchical pyramid\n"
325         "                                  - normal: Non-strict (not Blu-ray compatible)\n",
326         strtable_lookup( x264_b_pyramid_names, defaults->i_bframe_pyramid ) );
327     H1( "      --no-cabac              Disable CABAC\n" );
328     H1( "  -r, --ref <integer>         Number of reference frames [%d]\n", defaults->i_frame_reference );
329     H1( "      --no-deblock            Disable loop filter\n" );
330     H1( "  -f, --deblock <alpha:beta>  Loop filter parameters [%d:%d]\n",
331                                        defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
332     H2( "      --slices <integer>      Number of slices per frame; forces rectangular\n"
333         "                              slices and is overridden by other slicing options\n" );
334     else H1( "      --slices <integer>      Number of slices per frame\n" );
335     H2( "      --slice-max-size <integer> Limit the size of each slice in bytes\n");
336     H2( "      --slice-max-mbs <integer> Limit the size of each slice in macroblocks\n");
337     H0( "      --interlaced            Enable pure-interlaced mode\n" );
338     H2( "      --constrained-intra     Enable constrained intra prediction.\n" );
339     H0( "\n" );
340     H0( "Ratecontrol:\n" );
341     H0( "\n" );
342     H1( "  -q, --qp <integer>          Force constant QP (0-51, 0=lossless)\n" );
343     H0( "  -B, --bitrate <integer>     Set bitrate (kbit/s)\n" );
344     H0( "      --crf <float>           Quality-based VBR (0-51, 0=lossless) [%.1f]\n", defaults->rc.f_rf_constant );
345     H1( "      --rc-lookahead <integer> Number of frames for frametype lookahead [%d]\n", defaults->rc.i_lookahead );
346     H0( "      --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
347     H0( "      --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
348     H2( "      --vbv-init <float>      Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
349     H2( "      --qpmin <integer>       Set min QP [%d]\n", defaults->rc.i_qp_min );
350     H2( "      --qpmax <integer>       Set max QP [%d]\n", defaults->rc.i_qp_max );
351     H2( "      --qpstep <integer>      Set max QP step [%d]\n", defaults->rc.i_qp_step );
352     H2( "      --ratetol <float>       Tolerance of ABR ratecontrol and VBV [%.1f]\n", defaults->rc.f_rate_tolerance );
353     H2( "      --ipratio <float>       QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
354     H2( "      --pbratio <float>       QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
355     H2( "      --chroma-qp-offset <integer>  QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
356     H2( "      --aq-mode <integer>     AQ method [%d]\n"
357         "                                  - 0: Disabled\n"
358         "                                  - 1: Variance AQ (complexity mask)\n"
359         "                                  - 2: Auto-variance AQ (experimental)\n", defaults->rc.i_aq_mode );
360     H1( "      --aq-strength <float>   Reduces blocking and blurring in flat and\n"
361         "                              textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
362     H1( "\n" );
363     H0( "  -p, --pass <integer>        Enable multipass ratecontrol\n"
364         "                                  - 1: First pass, creates stats file\n"
365         "                                  - 2: Last pass, does not overwrite stats file\n" );
366     H2( "                                  - 3: Nth pass, overwrites stats file\n" );
367     H1( "      --stats <string>        Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
368     H2( "      --no-mbtree             Disable mb-tree ratecontrol.\n");
369     H2( "      --qcomp <float>         QP curve compression [%.2f]\n", defaults->rc.f_qcompress );
370     H2( "      --cplxblur <float>      Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
371     H2( "      --qblur <float>         Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
372     H2( "      --zones <zone0>/<zone1>/...  Tweak the bitrate of regions of the video\n" );
373     H2( "                              Each zone is of the form\n"
374         "                                  <start frame>,<end frame>,<option>\n"
375         "                                  where <option> is either\n"
376         "                                      q=<integer> (force QP)\n"
377         "                                  or  b=<float> (bitrate multiplier)\n" );
378     H2( "      --qpfile <string>       Force frametypes and QPs for some or all frames\n"
379         "                              Format of each line: framenumber frametype QP\n"
380         "                              QP of -1 lets x264 choose. Frametypes: I,i,P,B,b.\n"
381         "                              QPs are restricted by qpmin/qpmax.\n" );
382     H1( "\n" );
383     H1( "Analysis:\n" );
384     H1( "\n" );
385     H1( "  -A, --partitions <string>   Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
386         "                                  - p8x8, p4x4, b8x8, i8x8, i4x4\n"
387         "                                  - none, all\n"
388         "                                  (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
389     H1( "      --direct <string>       Direct MV prediction mode [\"%s\"]\n"
390         "                                  - none, spatial, temporal, auto\n",
391                                        strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
392     H2( "      --no-weightb            Disable weighted prediction for B-frames\n" );
393     H1( "      --weightp <integer>     Weighted prediction for P-frames [%d]\n"
394         "                              - 0: Disabled\n"
395         "                              - 1: Blind offset\n"
396         "                              - 2: Smart analysis\n", defaults->analyse.i_weighted_pred );
397     H1( "      --me <string>           Integer pixel motion estimation method [\"%s\"]\n",
398                                        strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
399     H2( "                                  - dia: diamond search, radius 1 (fast)\n"
400         "                                  - hex: hexagonal search, radius 2\n"
401         "                                  - umh: uneven multi-hexagon search\n"
402         "                                  - esa: exhaustive search\n"
403         "                                  - tesa: hadamard exhaustive search (slow)\n" );
404     else H1( "                                  - dia, hex, umh\n" );
405     H2( "      --merange <integer>     Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
406     H2( "      --mvrange <integer>     Maximum motion vector length [-1 (auto)]\n" );
407     H2( "      --mvrange-thread <int>  Minimum buffer between threads [-1 (auto)]\n" );
408     H1( "  -m, --subme <integer>       Subpixel motion estimation and mode decision [%d]\n", defaults->analyse.i_subpel_refine );
409     H2( "                                  - 0: fullpel only (not recommended)\n"
410         "                                  - 1: SAD mode decision, one qpel iteration\n"
411         "                                  - 2: SATD mode decision\n"
412         "                                  - 3-5: Progressively more qpel\n"
413         "                                  - 6: RD mode decision for I/P-frames\n"
414         "                                  - 7: RD mode decision for all frames\n"
415         "                                  - 8: RD refinement for I/P-frames\n"
416         "                                  - 9: RD refinement for all frames\n"
417         "                                  - 10: QP-RD - requires trellis=2, aq-mode>0\n" );
418     else H1( "                                  decision quality: 1=fast, 10=best.\n"  );
419     H1( "      --psy-rd                Strength of psychovisual optimization [\"%.1f:%.1f\"]\n"
420         "                                  #1: RD (requires subme>=6)\n"
421         "                                  #2: Trellis (requires trellis, experimental)\n",
422                                        defaults->analyse.f_psy_rd, defaults->analyse.f_psy_trellis );
423     H2( "      --no-psy                Disable all visual optimizations that worsen\n"
424         "                              both PSNR and SSIM.\n" );
425     H2( "      --no-mixed-refs         Don't decide references on a per partition basis\n" );
426     H2( "      --no-chroma-me          Ignore chroma in motion estimation\n" );
427     H1( "      --no-8x8dct             Disable adaptive spatial transform size\n" );
428     H1( "  -t, --trellis <integer>     Trellis RD quantization. Requires CABAC. [%d]\n"
429         "                                  - 0: disabled\n"
430         "                                  - 1: enabled only on the final encode of a MB\n"
431         "                                  - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
432     H2( "      --no-fast-pskip         Disables early SKIP detection on P-frames\n" );
433     H2( "      --no-dct-decimate       Disables coefficient thresholding on P-frames\n" );
434     H1( "      --nr <integer>          Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
435     H2( "\n" );
436     H2( "      --deadzone-inter <int>  Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
437     H2( "      --deadzone-intra <int>  Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
438     H2( "                                  Deadzones should be in the range 0 - 32.\n" );
439     H2( "      --cqm <string>          Preset quant matrices [\"flat\"]\n"
440         "                                  - jvt, flat\n" );
441     H1( "      --cqmfile <string>      Read custom quant matrices from a JM-compatible file\n" );
442     H2( "                                  Overrides any other --cqm* options.\n" );
443     H2( "      --cqm4 <list>           Set all 4x4 quant matrices\n"
444         "                                  Takes a comma-separated list of 16 integers.\n" );
445     H2( "      --cqm8 <list>           Set all 8x8 quant matrices\n"
446         "                                  Takes a comma-separated list of 64 integers.\n" );
447     H2( "      --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
448         "                              Set both luma and chroma quant matrices\n" );
449     H2( "      --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
450         "                              Set individual quant matrices\n" );
451     H2( "\n" );
452     H2( "Video Usability Info (Annex E):\n" );
453     H2( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
454     H2( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
455     H2( "\n" );
456     H2( "      --overscan <string>     Specify crop overscan setting [\"%s\"]\n"
457         "                                  - undef, show, crop\n",
458                                        strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ) );
459     H2( "      --videoformat <string>  Specify video format [\"%s\"]\n"
460         "                                  - component, pal, ntsc, secam, mac, undef\n",
461                                        strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ) );
462     H2( "      --fullrange <string>    Specify full range samples setting [\"%s\"]\n"
463         "                                  - off, on\n",
464                                        strtable_lookup( x264_fullrange_names, defaults->vui.b_fullrange ) );
465     H2( "      --colorprim <string>    Specify color primaries [\"%s\"]\n"
466         "                                  - undef, bt709, bt470m, bt470bg\n"
467         "                                    smpte170m, smpte240m, film\n",
468                                        strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ) );
469     H2( "      --transfer <string>     Specify transfer characteristics [\"%s\"]\n"
470         "                                  - undef, bt709, bt470m, bt470bg, linear,\n"
471         "                                    log100, log316, smpte170m, smpte240m\n",
472                                        strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ) );
473     H2( "      --colormatrix <string>  Specify color matrix setting [\"%s\"]\n"
474         "                                  - undef, bt709, fcc, bt470bg\n"
475         "                                    smpte170m, smpte240m, GBR, YCgCo\n",
476                                        strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
477     H2( "      --chromaloc <integer>   Specify chroma sample location (0 to 5) [%d]\n",
478                                        defaults->vui.i_chroma_loc );
479     H0( "\n" );
480     H0( "Input/Output:\n" );
481     H0( "\n" );
482     H0( "  -o, --output                Specify output file\n" );
483     H1( "      --muxer <string>        Specify output container format [\"%s\"]\n"
484         "                                  - %s\n", muxer_names[0], stringify_names( buf, muxer_names ) );
485     H1( "      --demuxer <string>      Specify input container format [\"%s\"]\n"
486         "                                  - %s\n", demuxer_names[0], stringify_names( buf, demuxer_names ) );
487     H1( "      --index <string>        Filename for input index file\n" );
488     H0( "      --sar width:height      Specify Sample Aspect Ratio\n" );
489     H0( "      --fps <float|rational>  Specify framerate\n" );
490     H0( "      --seek <integer>        First frame to encode\n" );
491     H0( "      --frames <integer>      Maximum number of frames to encode\n" );
492     H0( "      --level <string>        Specify level (as defined by Annex A)\n" );
493     H1( "\n" );
494     H1( "  -v, --verbose               Print stats for each frame\n" );
495     H1( "      --no-progress           Don't show the progress indicator while encoding\n" );
496     H0( "      --quiet                 Quiet Mode\n" );
497     H1( "      --psnr                  Enable PSNR computation\n" );
498     H1( "      --ssim                  Enable SSIM computation\n" );
499     H1( "      --threads <integer>     Force a specific number of threads\n" );
500     H2( "      --sliced-threads        Low-latency but lower-efficiency threading\n" );
501     H2( "      --thread-input          Run Avisynth in its own thread\n" );
502     H2( "      --sync-lookahead <integer> Number of buffer frames for threaded lookahead\n" );
503     H2( "      --non-deterministic     Slightly improve quality of SMP, at the cost of repeatability\n" );
504     H2( "      --asm <integer>         Override CPU detection\n" );
505     H2( "      --no-asm                Disable all CPU optimizations\n" );
506     H2( "      --visualize             Show MB types overlayed on the encoded video\n" );
507     H2( "      --dump-yuv <string>     Save reconstructed frames\n" );
508     H2( "      --sps-id <integer>      Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
509     H2( "      --aud                   Use access unit delimiters\n" );
510     H2( "      --force-cfr             Force constant framerate timestamp generation\n" );
511     H0( "\n" );
512 }
513
514 #define OPT_FRAMES 256
515 #define OPT_SEEK 257
516 #define OPT_QPFILE 258
517 #define OPT_THREAD_INPUT 259
518 #define OPT_QUIET 260
519 #define OPT_NOPROGRESS 261
520 #define OPT_VISUALIZE 262
521 #define OPT_LONGHELP 263
522 #define OPT_PROFILE 264
523 #define OPT_PRESET 265
524 #define OPT_TUNE 266
525 #define OPT_SLOWFIRSTPASS 267
526 #define OPT_FULLHELP 268
527 #define OPT_FPS 269
528 #define OPT_MUXER 270
529 #define OPT_DEMUXER 271
530 #define OPT_INDEX 272
531 #define OPT_INTERLACED 273
532
533 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
534 static struct option long_options[] =
535 {
536     { "help",              no_argument, NULL, 'h' },
537     { "longhelp",          no_argument, NULL, OPT_LONGHELP },
538     { "fullhelp",          no_argument, NULL, OPT_FULLHELP },
539     { "version",           no_argument, NULL, 'V' },
540     { "profile",     required_argument, NULL, OPT_PROFILE },
541     { "preset",      required_argument, NULL, OPT_PRESET },
542     { "tune",        required_argument, NULL, OPT_TUNE },
543     { "slow-firstpass",    no_argument, NULL, OPT_SLOWFIRSTPASS },
544     { "bitrate",     required_argument, NULL, 'B' },
545     { "bframes",     required_argument, NULL, 'b' },
546     { "b-adapt",     required_argument, NULL, 0 },
547     { "no-b-adapt",        no_argument, NULL, 0 },
548     { "b-bias",      required_argument, NULL, 0 },
549     { "b-pyramid",   required_argument, NULL, 0 },
550     { "min-keyint",  required_argument, NULL, 'i' },
551     { "keyint",      required_argument, NULL, 'I' },
552     { "intra-refresh",     no_argument, NULL, 0 },
553     { "scenecut",    required_argument, NULL, 0 },
554     { "no-scenecut",       no_argument, NULL, 0 },
555     { "nf",                no_argument, NULL, 0 },
556     { "no-deblock",        no_argument, NULL, 0 },
557     { "filter",      required_argument, NULL, 0 },
558     { "deblock",     required_argument, NULL, 'f' },
559     { "interlaced",        no_argument, NULL, OPT_INTERLACED },
560     { "no-interlaced",     no_argument, NULL, OPT_INTERLACED },
561     { "constrained-intra", no_argument, NULL, 0 },
562     { "cabac",             no_argument, NULL, 0 },
563     { "no-cabac",          no_argument, NULL, 0 },
564     { "qp",          required_argument, NULL, 'q' },
565     { "qpmin",       required_argument, NULL, 0 },
566     { "qpmax",       required_argument, NULL, 0 },
567     { "qpstep",      required_argument, NULL, 0 },
568     { "crf",         required_argument, NULL, 0 },
569     { "rc-lookahead",required_argument, NULL, 0 },
570     { "ref",         required_argument, NULL, 'r' },
571     { "asm",         required_argument, NULL, 0 },
572     { "no-asm",            no_argument, NULL, 0 },
573     { "sar",         required_argument, NULL, 0 },
574     { "fps",         required_argument, NULL, OPT_FPS },
575     { "frames",      required_argument, NULL, OPT_FRAMES },
576     { "seek",        required_argument, NULL, OPT_SEEK },
577     { "output",      required_argument, NULL, 'o' },
578     { "muxer",       required_argument, NULL, OPT_MUXER },
579     { "demuxer",     required_argument, NULL, OPT_DEMUXER },
580     { "stdout",      required_argument, NULL, OPT_MUXER },
581     { "stdin",       required_argument, NULL, OPT_DEMUXER },
582     { "index",       required_argument, NULL, OPT_INDEX },
583     { "analyse",     required_argument, NULL, 0 },
584     { "partitions",  required_argument, NULL, 'A' },
585     { "direct",      required_argument, NULL, 0 },
586     { "weightb",           no_argument, NULL, 'w' },
587     { "no-weightb",        no_argument, NULL, 0 },
588     { "weightp",     required_argument, NULL, 0 },
589     { "me",          required_argument, NULL, 0 },
590     { "merange",     required_argument, NULL, 0 },
591     { "mvrange",     required_argument, NULL, 0 },
592     { "mvrange-thread", required_argument, NULL, 0 },
593     { "subme",       required_argument, NULL, 'm' },
594     { "psy-rd",      required_argument, NULL, 0 },
595     { "no-psy",            no_argument, NULL, 0 },
596     { "psy",               no_argument, NULL, 0 },
597     { "mixed-refs",        no_argument, NULL, 0 },
598     { "no-mixed-refs",     no_argument, NULL, 0 },
599     { "no-chroma-me",      no_argument, NULL, 0 },
600     { "8x8dct",            no_argument, NULL, 0 },
601     { "no-8x8dct",         no_argument, NULL, 0 },
602     { "trellis",     required_argument, NULL, 't' },
603     { "fast-pskip",        no_argument, NULL, 0 },
604     { "no-fast-pskip",     no_argument, NULL, 0 },
605     { "no-dct-decimate",   no_argument, NULL, 0 },
606     { "aq-strength", required_argument, NULL, 0 },
607     { "aq-mode",     required_argument, NULL, 0 },
608     { "deadzone-inter", required_argument, NULL, '0' },
609     { "deadzone-intra", required_argument, NULL, '0' },
610     { "level",       required_argument, NULL, 0 },
611     { "ratetol",     required_argument, NULL, 0 },
612     { "vbv-maxrate", required_argument, NULL, 0 },
613     { "vbv-bufsize", required_argument, NULL, 0 },
614     { "vbv-init",    required_argument, NULL,  0 },
615     { "ipratio",     required_argument, NULL, 0 },
616     { "pbratio",     required_argument, NULL, 0 },
617     { "chroma-qp-offset", required_argument, NULL, 0 },
618     { "pass",        required_argument, NULL, 'p' },
619     { "stats",       required_argument, NULL, 0 },
620     { "qcomp",       required_argument, NULL, 0 },
621     { "mbtree",            no_argument, NULL, 0 },
622     { "no-mbtree",         no_argument, NULL, 0 },
623     { "qblur",       required_argument, NULL, 0 },
624     { "cplxblur",    required_argument, NULL, 0 },
625     { "zones",       required_argument, NULL, 0 },
626     { "qpfile",      required_argument, NULL, OPT_QPFILE },
627     { "threads",     required_argument, NULL, 0 },
628     { "sliced-threads",    no_argument, NULL, 0 },
629     { "no-sliced-threads", no_argument, NULL, 0 },
630     { "slice-max-size",    required_argument, NULL, 0 },
631     { "slice-max-mbs",     required_argument, NULL, 0 },
632     { "slices",            required_argument, NULL, 0 },
633     { "thread-input",      no_argument, NULL, OPT_THREAD_INPUT },
634     { "sync-lookahead",    required_argument, NULL, 0 },
635     { "non-deterministic", no_argument, NULL, 0 },
636     { "psnr",              no_argument, NULL, 0 },
637     { "ssim",              no_argument, NULL, 0 },
638     { "quiet",             no_argument, NULL, OPT_QUIET },
639     { "verbose",           no_argument, NULL, 'v' },
640     { "no-progress",       no_argument, NULL, OPT_NOPROGRESS },
641     { "visualize",         no_argument, NULL, OPT_VISUALIZE },
642     { "dump-yuv",    required_argument, NULL, 0 },
643     { "sps-id",      required_argument, NULL, 0 },
644     { "aud",               no_argument, NULL, 0 },
645     { "nr",          required_argument, NULL, 0 },
646     { "cqm",         required_argument, NULL, 0 },
647     { "cqmfile",     required_argument, NULL, 0 },
648     { "cqm4",        required_argument, NULL, 0 },
649     { "cqm4i",       required_argument, NULL, 0 },
650     { "cqm4iy",      required_argument, NULL, 0 },
651     { "cqm4ic",      required_argument, NULL, 0 },
652     { "cqm4p",       required_argument, NULL, 0 },
653     { "cqm4py",      required_argument, NULL, 0 },
654     { "cqm4pc",      required_argument, NULL, 0 },
655     { "cqm8",        required_argument, NULL, 0 },
656     { "cqm8i",       required_argument, NULL, 0 },
657     { "cqm8p",       required_argument, NULL, 0 },
658     { "overscan",    required_argument, NULL, 0 },
659     { "videoformat", required_argument, NULL, 0 },
660     { "fullrange",   required_argument, NULL, 0 },
661     { "colorprim",   required_argument, NULL, 0 },
662     { "transfer",    required_argument, NULL, 0 },
663     { "colormatrix", required_argument, NULL, 0 },
664     { "chromaloc",   required_argument, NULL, 0 },
665     { "force-cfr",         no_argument, NULL, 0 },
666     {0, 0, 0, 0}
667 };
668
669 static int select_output( const char *muxer, char *filename, x264_param_t *param )
670 {
671     const char *ext = get_filename_extension( filename );
672     if( !strcmp( filename, "-" ) || strcasecmp( muxer, "auto" ) )
673         ext = muxer;
674
675     if( !strcasecmp( ext, "mp4" ) )
676     {
677 #ifdef MP4_OUTPUT
678         output = mp4_output;
679         param->b_annexb = 0;
680         param->b_aud = 0;
681         param->b_dts_compress = 0;
682         param->b_repeat_headers = 0;
683 #else
684         fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
685         return -1;
686 #endif
687     }
688     else if( !strcasecmp( ext, "mkv" ) )
689     {
690         output = mkv_output;
691         param->b_annexb = 0;
692         param->b_aud = 0;
693         param->b_dts_compress = 0;
694         param->b_repeat_headers = 0;
695     }
696     else if( !strcasecmp( ext, "flv" ) )
697     {
698         output = flv_output;
699         param->b_annexb = 0;
700         param->b_aud = 0;
701         param->b_dts_compress = 1;
702         param->b_repeat_headers = 0;
703     }
704     else
705         output = raw_output;
706     return 0;
707 }
708
709 static int select_input( const char *demuxer, char *used_demuxer, char *filename,
710                          hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
711 {
712     const char *ext = get_filename_extension( filename );
713     int b_regular = strcmp( filename, "-" );
714     int b_auto = !strcasecmp( demuxer, "auto" );
715     if( !b_regular && b_auto )
716         ext = "yuv";
717     if( b_regular )
718     {
719         FILE *f = fopen( filename, "r" );
720         if( f )
721         {
722             b_regular = x264_is_regular_file( f );
723             fclose( f );
724         }
725     }
726     const char *module = b_auto ? ext : demuxer;
727
728     if( !strcasecmp( module, "avs" ) || !strcasecmp( ext, "d2v" ) || !strcasecmp( ext, "dga" ) )
729     {
730 #ifdef AVS_INPUT
731         input = avs_input;
732         module = "avs";
733 #else
734         fprintf( stderr, "x264 [error]: not compiled with AVS input support\n" );
735         return -1;
736 #endif
737     }
738     else if( !strcasecmp( module, "y4m" ) )
739         input = y4m_input;
740     else if( !strcasecmp( module, "yuv" ) )
741         input = yuv_input;
742     else
743     {
744 #ifdef FFMS_INPUT
745         if( b_regular && (b_auto || !strcasecmp( demuxer, "ffms" )) &&
746             !ffms_input.open_file( filename, p_handle, info, opt ) )
747         {
748             module = "ffms";
749             b_auto = 0;
750             input = ffms_input;
751         }
752 #endif
753 #ifdef LAVF_INPUT
754         if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
755             !lavf_input.open_file( filename, p_handle, info, opt ) )
756         {
757             module = "lavf";
758             b_auto = 0;
759             input = lavf_input;
760         }
761 #endif
762 #ifdef AVS_INPUT
763         if( b_regular && (b_auto || !strcasecmp( demuxer, "avs" )) &&
764             !avs_input.open_file( filename, p_handle, info, opt ) )
765         {
766             module = "avs";
767             b_auto = 0;
768             input = avs_input;
769         }
770 #endif
771         if( b_auto && !yuv_input.open_file( filename, p_handle, info, opt ) )
772         {
773             module = "yuv";
774             b_auto = 0;
775             input = yuv_input;
776         }
777
778         if( !(*p_handle) )
779         {
780             fprintf( stderr, "x264 [error]: could not open input file `%s' via any method!\n", filename );
781             return -1;
782         }
783     }
784     strcpy( used_demuxer, module );
785
786     return 0;
787 }
788
789 /*****************************************************************************
790  * Parse:
791  *****************************************************************************/
792 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
793 {
794     char *input_filename = NULL;
795     const char *demuxer = demuxer_names[0];
796     char *output_filename = NULL;
797     const char *muxer = muxer_names[0];
798     x264_param_t defaults = *param;
799     char *profile = NULL;
800     int b_thread_input = 0;
801     int b_turbo = 1;
802     int b_pass1 = 0;
803     int b_user_ref = 0;
804     int b_user_fps = 0;
805     int b_user_interlaced = 0;
806     int i;
807     cli_input_opt_t input_opt;
808
809     memset( opt, 0, sizeof(cli_opt_t) );
810     memset( &input_opt, 0, sizeof(cli_input_opt_t) );
811     opt->b_progress = 1;
812
813     /* Presets are applied before all other options. */
814     for( optind = 0;; )
815     {
816         int c = getopt_long( argc, argv, short_options, long_options, NULL );
817         if( c == -1 )
818             break;
819
820         if( c == OPT_PRESET )
821         {
822             if( !strcasecmp( optarg, "ultrafast" ) )
823             {
824                 param->i_frame_reference = 1;
825                 param->i_scenecut_threshold = 0;
826                 param->b_deblocking_filter = 0;
827                 param->b_cabac = 0;
828                 param->i_bframe = 0;
829                 param->analyse.intra = 0;
830                 param->analyse.inter = 0;
831                 param->analyse.b_transform_8x8 = 0;
832                 param->analyse.i_me_method = X264_ME_DIA;
833                 param->analyse.i_subpel_refine = 0;
834                 param->rc.i_aq_mode = 0;
835                 param->analyse.b_mixed_references = 0;
836                 param->analyse.i_trellis = 0;
837                 param->i_bframe_adaptive = X264_B_ADAPT_NONE;
838                 param->rc.b_mb_tree = 0;
839                 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
840             }
841             else if( !strcasecmp( optarg, "veryfast" ) )
842             {
843                 param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
844                 param->analyse.i_me_method = X264_ME_DIA;
845                 param->analyse.i_subpel_refine = 1;
846                 param->i_frame_reference = 1;
847                 param->analyse.b_mixed_references = 0;
848                 param->analyse.i_trellis = 0;
849                 param->rc.b_mb_tree = 0;
850                 param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
851             }
852             else if( !strcasecmp( optarg, "faster" ) )
853             {
854                 param->analyse.b_mixed_references = 0;
855                 param->i_frame_reference = 2;
856                 param->analyse.i_subpel_refine = 4;
857                 param->rc.b_mb_tree = 0;
858                 param->analyse.i_weighted_pred = X264_WEIGHTP_BLIND;
859             }
860             else if( !strcasecmp( optarg, "fast" ) )
861             {
862                 param->i_frame_reference = 2;
863                 param->analyse.i_subpel_refine = 6;
864                 param->rc.i_lookahead = 30;
865             }
866             else if( !strcasecmp( optarg, "medium" ) )
867             {
868                 /* Default is medium */
869             }
870             else if( !strcasecmp( optarg, "slow" ) )
871             {
872                 param->analyse.i_me_method = X264_ME_UMH;
873                 param->analyse.i_subpel_refine = 8;
874                 param->i_frame_reference = 5;
875                 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
876                 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
877                 param->rc.i_lookahead = 50;
878             }
879             else if( !strcasecmp( optarg, "slower" ) )
880             {
881                 param->analyse.i_me_method = X264_ME_UMH;
882                 param->analyse.i_subpel_refine = 9;
883                 param->i_frame_reference = 8;
884                 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
885                 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
886                 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
887                 param->analyse.i_trellis = 2;
888                 param->rc.i_lookahead = 60;
889             }
890             else if( !strcasecmp( optarg, "veryslow" ) )
891             {
892                 param->analyse.i_me_method = X264_ME_UMH;
893                 param->analyse.i_subpel_refine = 10;
894                 param->analyse.i_me_range = 24;
895                 param->i_frame_reference = 16;
896                 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
897                 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
898                 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
899                 param->analyse.i_trellis = 2;
900                 param->i_bframe = 8;
901                 param->rc.i_lookahead = 60;
902             }
903             else if( !strcasecmp( optarg, "placebo" ) )
904             {
905                 param->analyse.i_me_method = X264_ME_TESA;
906                 param->analyse.i_subpel_refine = 10;
907                 param->analyse.i_me_range = 24;
908                 param->i_frame_reference = 16;
909                 param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
910                 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
911                 param->analyse.inter |= X264_ANALYSE_PSUB8x8;
912                 param->analyse.b_fast_pskip = 0;
913                 param->analyse.i_trellis = 2;
914                 param->i_bframe = 16;
915                 param->rc.i_lookahead = 60;
916                 b_turbo = 0;
917             }
918             else
919             {
920                 fprintf( stderr, "x264 [error]: invalid preset '%s'\n", optarg );
921                 return -1;
922             }
923         }
924         else if( c == '?' )
925             return -1;
926     }
927
928     /* Tunings are applied next. */
929     for( optind = 0;; )
930     {
931         int c = getopt_long( argc, argv, short_options, long_options, NULL );
932         if( c == -1 )
933             break;
934
935         if( c == OPT_TUNE )
936         {
937             char *s = strtok( optarg, ",./-+" );
938             int psy_tuning_used = 0;
939             while( s )
940             {
941                 if( !strncasecmp( s, "film", 4 ) )
942                 {
943                     if( psy_tuning_used ) goto psy_failure;
944                     param->i_deblocking_filter_alphac0 = -1;
945                     param->i_deblocking_filter_beta = -1;
946                     param->analyse.f_psy_trellis = 0.15;
947                     psy_tuning_used = 1;
948                 }
949                 else if( !strncasecmp( s, "animation", 9 ) )
950                 {
951                     if( psy_tuning_used ) goto psy_failure;
952                     param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
953                     param->i_deblocking_filter_alphac0 = 1;
954                     param->i_deblocking_filter_beta = 1;
955                     param->analyse.f_psy_rd = 0.4;
956                     param->rc.f_aq_strength = 0.6;
957                     param->i_bframe += 2;
958                     psy_tuning_used = 1;
959                 }
960                 else if( !strncasecmp( s, "grain", 5 ) )
961                 {
962                     if( psy_tuning_used ) goto psy_failure;
963                     param->i_deblocking_filter_alphac0 = -2;
964                     param->i_deblocking_filter_beta = -2;
965                     param->analyse.f_psy_trellis = 0.25;
966                     param->analyse.b_dct_decimate = 0;
967                     param->rc.f_pb_factor = 1.1;
968                     param->rc.f_ip_factor = 1.1;
969                     param->rc.f_aq_strength = 0.5;
970                     param->analyse.i_luma_deadzone[0] = 6;
971                     param->analyse.i_luma_deadzone[1] = 6;
972                     param->rc.f_qcompress = 0.8;
973                     psy_tuning_used = 1;
974                 }
975                 else if( !strncasecmp( s, "psnr", 4 ) )
976                 {
977                     if( psy_tuning_used ) goto psy_failure;
978                     param->rc.i_aq_mode = X264_AQ_NONE;
979                     param->analyse.b_psy = 0;
980                     psy_tuning_used = 1;
981                 }
982                 else if( !strncasecmp( s, "ssim", 4 ) )
983                 {
984                     if( psy_tuning_used ) goto psy_failure;
985                     param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
986                     param->analyse.b_psy = 0;
987                     psy_tuning_used = 1;
988                 }
989                 else if( !strncasecmp( s, "fastdecode", 10 ) )
990                 {
991                     param->b_deblocking_filter = 0;
992                     param->b_cabac = 0;
993                     param->analyse.b_weighted_bipred = 0;
994                     param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
995                 }
996                 else if( !strncasecmp( s, "zerolatency", 11 ) )
997                 {
998                     param->rc.i_lookahead = 0;
999                     param->i_sync_lookahead = 0;
1000                     param->i_bframe = 0;
1001                     param->b_sliced_threads = 1;
1002                 }
1003                 else if( !strncasecmp( s, "touhou", 6 ) )
1004                 {
1005                     if( psy_tuning_used ) goto psy_failure;
1006                     param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
1007                     param->i_deblocking_filter_alphac0 = -1;
1008                     param->i_deblocking_filter_beta = -1;
1009                     param->analyse.f_psy_trellis = 0.2;
1010                     param->rc.f_aq_strength = 1.3;
1011                     if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
1012                         param->analyse.inter |= X264_ANALYSE_PSUB8x8;
1013                     psy_tuning_used = 1;
1014                 }
1015                 else
1016                 {
1017                     fprintf( stderr, "x264 [error]: invalid tune '%s'\n", s );
1018                     return -1;
1019                 }
1020                 if( 0 )
1021                 {
1022 psy_failure:
1023                     fprintf( stderr, "x264 [warning]: only 1 psy tuning can be used: ignoring tune %s\n", s );
1024                 }
1025                 s = strtok( NULL, ",./-+" );
1026             }
1027         }
1028         else if( c == '?' )
1029             return -1;
1030     }
1031
1032     /* Parse command line options */
1033     for( optind = 0;; )
1034     {
1035         int b_error = 0;
1036         int long_options_index = -1;
1037
1038         int c = getopt_long( argc, argv, short_options, long_options, &long_options_index );
1039
1040         if( c == -1 )
1041         {
1042             break;
1043         }
1044
1045         switch( c )
1046         {
1047             case 'h':
1048                 Help( &defaults, 0 );
1049                 exit(0);
1050             case OPT_LONGHELP:
1051                 Help( &defaults, 1 );
1052                 exit(0);
1053             case OPT_FULLHELP:
1054                 Help( &defaults, 2 );
1055                 exit(0);
1056             case 'V':
1057 #ifdef X264_POINTVER
1058                 printf( "x264 "X264_POINTVER"\n" );
1059 #else
1060                 printf( "x264 0.%d.X\n", X264_BUILD );
1061 #endif
1062                 printf( "built on " __DATE__ ", " );
1063 #ifdef __GNUC__
1064                 printf( "gcc: " __VERSION__ "\n" );
1065 #else
1066                 printf( "using a non-gcc compiler\n" );
1067 #endif
1068                 exit(0);
1069             case OPT_FRAMES:
1070                 param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
1071                 break;
1072             case OPT_SEEK:
1073                 opt->i_seek = input_opt.seek = X264_MAX( atoi( optarg ), 0 );
1074                 break;
1075             case 'o':
1076                 output_filename = optarg;
1077                 break;
1078             case OPT_MUXER:
1079                 for( i = 0; muxer_names[i] && strcasecmp( muxer_names[i], optarg ); )
1080                     i++;
1081                 if( !muxer_names[i] )
1082                 {
1083                     fprintf( stderr, "x264 [error]: invalid muxer '%s'\n", optarg );
1084                     return -1;
1085                 }
1086                 muxer = optarg;
1087                 break;
1088             case OPT_DEMUXER:
1089                 for( i = 0; demuxer_names[i] && strcasecmp( demuxer_names[i], optarg ); )
1090                     i++;
1091                 if( !demuxer_names[i] )
1092                 {
1093                     fprintf( stderr, "x264 [error]: invalid demuxer '%s'\n", optarg );
1094                     return -1;
1095                 }
1096                 demuxer = optarg;
1097                 break;
1098             case OPT_INDEX:
1099                 input_opt.index = optarg;
1100                 break;
1101             case OPT_QPFILE:
1102                 opt->qpfile = fopen( optarg, "rb" );
1103                 if( !opt->qpfile )
1104                 {
1105                     fprintf( stderr, "x264 [error]: can't open qpfile `%s'\n", optarg );
1106                     return -1;
1107                 }
1108                 else if( !x264_is_regular_file( opt->qpfile ) )
1109                 {
1110                     fprintf( stderr, "x264 [error]: qpfile incompatible with non-regular file `%s'\n", optarg );
1111                     fclose( opt->qpfile );
1112                     return -1;
1113                 }
1114                 break;
1115             case OPT_THREAD_INPUT:
1116                 b_thread_input = 1;
1117                 break;
1118             case OPT_QUIET:
1119                 param->i_log_level = X264_LOG_NONE;
1120                 break;
1121             case 'v':
1122                 param->i_log_level = X264_LOG_DEBUG;
1123                 break;
1124             case OPT_NOPROGRESS:
1125                 opt->b_progress = 0;
1126                 break;
1127             case OPT_VISUALIZE:
1128 #ifdef VISUALIZE
1129                 param->b_visualize = 1;
1130                 b_exit_on_ctrl_c = 1;
1131 #else
1132                 fprintf( stderr, "x264 [warning]: not compiled with visualization support\n" );
1133 #endif
1134                 break;
1135             case OPT_TUNE:
1136             case OPT_PRESET:
1137                 break;
1138             case OPT_PROFILE:
1139                 profile = optarg;
1140                 break;
1141             case OPT_SLOWFIRSTPASS:
1142                 b_turbo = 0;
1143                 break;
1144             case 'r':
1145                 b_user_ref = 1;
1146                 goto generic_option;
1147             case 'p':
1148                 b_pass1 = atoi( optarg ) == 1;
1149                 goto generic_option;
1150             case OPT_FPS:
1151                 b_user_fps = 1;
1152                 param->b_vfr_input = 0;
1153                 goto generic_option;
1154             case OPT_INTERLACED:
1155                 b_user_interlaced = 1;
1156                 goto generic_option;
1157             default:
1158 generic_option:
1159             {
1160                 int i;
1161                 if( long_options_index < 0 )
1162                 {
1163                     for( i = 0; long_options[i].name; i++ )
1164                         if( long_options[i].val == c )
1165                         {
1166                             long_options_index = i;
1167                             break;
1168                         }
1169                     if( long_options_index < 0 )
1170                     {
1171                         /* getopt_long already printed an error message */
1172                         return -1;
1173                     }
1174                 }
1175
1176                 b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg );
1177             }
1178         }
1179
1180         if( b_error )
1181         {
1182             const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
1183             fprintf( stderr, "x264 [error]: invalid argument: %s = %s\n", name, optarg );
1184             return -1;
1185         }
1186     }
1187
1188     /* Set faster options in case of turbo firstpass. */
1189     if( b_turbo && b_pass1 )
1190     {
1191         param->i_frame_reference = 1;
1192         param->analyse.b_transform_8x8 = 0;
1193         param->analyse.inter = 0;
1194         param->analyse.i_me_method = X264_ME_DIA;
1195         param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
1196         param->analyse.i_trellis = 0;
1197     }
1198
1199     /* Apply profile restrictions. */
1200     if( profile )
1201     {
1202         if( !strcasecmp( profile, "baseline" ) )
1203         {
1204             param->analyse.b_transform_8x8 = 0;
1205             param->b_cabac = 0;
1206             param->i_cqm_preset = X264_CQM_FLAT;
1207             param->i_bframe = 0;
1208             param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
1209             if( param->b_interlaced )
1210             {
1211                 fprintf( stderr, "x264 [error]: baseline profile doesn't support interlacing\n" );
1212                 return -1;
1213             }
1214         }
1215         else if( !strcasecmp( profile, "main" ) )
1216         {
1217             param->analyse.b_transform_8x8 = 0;
1218             param->i_cqm_preset = X264_CQM_FLAT;
1219         }
1220         else if( !strcasecmp( profile, "high" ) )
1221         {
1222             /* Default */
1223         }
1224         else
1225         {
1226             fprintf( stderr, "x264 [error]: invalid profile: %s\n", profile );
1227             return -1;
1228         }
1229         if( (param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0) ||
1230             (param->rc.i_rc_method == X264_RC_CRF && param->rc.f_rf_constant == 0) )
1231         {
1232             fprintf( stderr, "x264 [error]: %s profile doesn't support lossless\n", profile );
1233             return -1;
1234         }
1235     }
1236
1237     /* Get the file name */
1238     if( optind > argc - 1 || !output_filename )
1239     {
1240         fprintf( stderr, "x264 [error]: No %s file. Run x264 --help for a list of options.\n",
1241                  optind > argc - 1 ? "input" : "output" );
1242         return -1;
1243     }
1244
1245     if( select_output( muxer, output_filename, param ) )
1246         return -1;
1247     if( output.open_file( output_filename, &opt->hout ) )
1248     {
1249         fprintf( stderr, "x264 [error]: could not open output file `%s'\n", output_filename );
1250         return -1;
1251     }
1252
1253     input_filename = argv[optind++];
1254     input_opt.resolution = optind < argc ? argv[optind++] : NULL;
1255     video_info_t info = {0};
1256     char demuxername[5];
1257
1258     /* set info flags to param flags to be overwritten by demuxer as necessary. */
1259     info.csp        = param->i_csp;
1260     info.fps_num    = param->i_fps_num;
1261     info.fps_den    = param->i_fps_den;
1262     info.interlaced = param->b_interlaced;
1263     info.sar_width  = param->vui.i_sar_width;
1264     info.sar_height = param->vui.i_sar_height;
1265     info.vfr        = param->b_vfr_input;
1266
1267     if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
1268         return -1;
1269
1270     if( !opt->hin && input.open_file( input_filename, &opt->hin, &info, &input_opt ) )
1271     {
1272         fprintf( stderr, "x264 [error]: could not open input file `%s'\n", input_filename );
1273         return -1;
1274     }
1275
1276     x264_reduce_fraction( &info.sar_width, &info.sar_height );
1277     x264_reduce_fraction( &info.fps_num, &info.fps_den );
1278     if( param->i_log_level >= X264_LOG_INFO )
1279         fprintf( stderr, "%s [info]: %dx%d%c %d:%d @ %d/%d fps (%cfr)\n", demuxername, info.width,
1280                  info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
1281                  info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
1282
1283     /* set param flags from the info flags as necessary */
1284     param->i_csp       = info.csp;
1285     param->i_height    = info.height;
1286     param->b_vfr_input = info.vfr;
1287     param->i_width     = info.width;
1288     if( !b_user_interlaced && info.interlaced )
1289     {
1290         fprintf( stderr, "x264 [warning]: input appears to be interlaced, enabling interlaced mode.\n"
1291                          "                If you want otherwise, use --no-interlaced\n" );
1292         param->b_interlaced = 1;
1293     }
1294     if( !b_user_fps )
1295     {
1296         param->i_fps_num = info.fps_num;
1297         param->i_fps_den = info.fps_den;
1298     }
1299     if( param->b_vfr_input )
1300     {
1301         param->i_timebase_num = info.timebase_num;
1302         param->i_timebase_den = info.timebase_den;
1303     }
1304     else
1305     {
1306         param->i_timebase_den = param->i_fps_num;
1307         param->i_timebase_num = param->i_fps_den;
1308     }
1309     if( !param->vui.i_sar_width || !param->vui.i_sar_height )
1310     {
1311         param->vui.i_sar_width  = info.sar_width;
1312         param->vui.i_sar_height = info.sar_height;
1313     }
1314
1315 #ifdef HAVE_PTHREAD
1316     if( b_thread_input || param->i_threads > 1
1317         || (param->i_threads == X264_THREADS_AUTO && x264_cpu_num_processors() > 1) )
1318     {
1319         if( thread_input.open_file( NULL, &opt->hin, &info, NULL ) )
1320         {
1321             fprintf( stderr, "x264 [error]: threaded input failed\n" );
1322             return -1;
1323         }
1324         else
1325             input = thread_input;
1326     }
1327 #endif
1328
1329
1330     /* Automatically reduce reference frame count to match the user's target level
1331      * if the user didn't explicitly set a reference frame count. */
1332     if( !b_user_ref )
1333     {
1334         int mbs = (((param->i_width)+15)>>4) * (((param->i_height)+15)>>4);
1335         int i;
1336         for( i = 0; x264_levels[i].level_idc != 0; i++ )
1337             if( param->i_level_idc == x264_levels[i].level_idc )
1338             {
1339                 while( mbs * 384 * param->i_frame_reference > x264_levels[i].dpb
1340                        && param->i_frame_reference > 1 )
1341                 {
1342                     param->i_frame_reference--;
1343                 }
1344                 break;
1345             }
1346     }
1347
1348
1349     return 0;
1350 }
1351
1352 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
1353 {
1354     int num = -1, qp, ret;
1355     char type;
1356     uint64_t file_pos;
1357     while( num < i_frame )
1358     {
1359         file_pos = ftell( opt->qpfile );
1360         ret = fscanf( opt->qpfile, "%d %c %d\n", &num, &type, &qp );
1361         if( num > i_frame || ret == EOF )
1362         {
1363             pic->i_type = X264_TYPE_AUTO;
1364             pic->i_qpplus1 = 0;
1365             fseek( opt->qpfile, file_pos, SEEK_SET );
1366             break;
1367         }
1368         if( num < i_frame && ret == 3 )
1369             continue;
1370         pic->i_qpplus1 = qp+1;
1371         if     ( type == 'I' ) pic->i_type = X264_TYPE_IDR;
1372         else if( type == 'i' ) pic->i_type = X264_TYPE_I;
1373         else if( type == 'P' ) pic->i_type = X264_TYPE_P;
1374         else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
1375         else if( type == 'b' ) pic->i_type = X264_TYPE_B;
1376         else ret = 0;
1377         if( ret != 3 || qp < -1 || qp > 51 )
1378         {
1379             fprintf( stderr, "x264 [error]: can't parse qpfile for frame %d\n", i_frame );
1380             fclose( opt->qpfile );
1381             opt->qpfile = NULL;
1382             pic->i_type = X264_TYPE_AUTO;
1383             pic->i_qpplus1 = 0;
1384             break;
1385         }
1386     }
1387 }
1388
1389 /*****************************************************************************
1390  * Encode:
1391  *****************************************************************************/
1392
1393 static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_pts )
1394 {
1395     x264_picture_t pic_out;
1396     x264_nal_t *nal;
1397     int i_nal;
1398     int i_frame_size = 0;
1399
1400     i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
1401
1402     if( i_frame_size < 0 )
1403     {
1404         fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
1405         return -1;
1406     }
1407
1408     if( i_frame_size )
1409     {
1410         i_frame_size = output.write_frame( hout, nal[0].p_payload, i_frame_size, &pic_out );
1411         *last_pts = pic_out.i_pts;
1412     }
1413
1414     return i_frame_size;
1415 }
1416
1417 static void Print_status( int64_t i_start, int i_frame, int i_frame_total, int64_t i_file, x264_param_t *param, int64_t last_pts )
1418 {
1419     char    buf[200];
1420     int64_t i_elapsed = x264_mdate() - i_start;
1421     double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1422     double bitrate = (double) i_file * 8 / ( (double) last_pts * 1000 * param->i_timebase_num / param->i_timebase_den );
1423     if( i_frame_total )
1424     {
1425         int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1426         sprintf( buf, "x264 [%.1f%%] %d/%d frames, %.2f fps, %.2f kb/s, eta %d:%02d:%02d",
1427                  100. * i_frame / i_frame_total, i_frame, i_frame_total, fps, bitrate,
1428                  eta/3600, (eta/60)%60, eta%60 );
1429     }
1430     else
1431     {
1432         sprintf( buf, "x264 %d frames: %.2f fps, %.2f kb/s", i_frame, fps, bitrate );
1433     }
1434     fprintf( stderr, "%s  \r", buf+5 );
1435     SetConsoleTitle( buf );
1436     fflush( stderr ); // needed in windows
1437 }
1438
1439 static int  Encode( x264_param_t *param, cli_opt_t *opt )
1440 {
1441     x264_t *h;
1442     x264_picture_t pic;
1443
1444     int     i_frame, i_frame_total, i_frame_output;
1445     int64_t i_start, i_end;
1446     int64_t i_file = 0;
1447     int     i_frame_size;
1448     int     i_update_interval;
1449     int64_t last_pts = 0;
1450 #   define  MAX_PTS_WARNING 3 /* arbitrary */
1451     int     pts_warning_cnt = 0;
1452     int64_t largest_pts = -1;
1453     int64_t second_largest_pts = -1;
1454     int64_t ticks_per_frame;
1455     double  duration;
1456     int     prev_timebase_den = param->i_timebase_den / gcd( param->i_timebase_num, param->i_timebase_den );
1457     int     dts_compress_multiplier;
1458
1459     opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
1460     i_frame_total = input.get_frame_total( opt->hin );
1461     i_frame_total = X264_MAX( i_frame_total - opt->i_seek, 0 );
1462     if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
1463         && param->i_frame_total > 0 )
1464         i_frame_total = param->i_frame_total;
1465     param->i_frame_total = i_frame_total;
1466     i_update_interval = i_frame_total ? x264_clip3( i_frame_total / 1000, 1, 10 ) : 10;
1467
1468     if( ( h = x264_encoder_open( param ) ) == NULL )
1469     {
1470         fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
1471         input.close_file( opt->hin );
1472         return -1;
1473     }
1474
1475     x264_encoder_parameters( h, param );
1476
1477     dts_compress_multiplier = param->i_timebase_den / prev_timebase_den;
1478
1479     if( output.set_param( opt->hout, param ) )
1480     {
1481         fprintf( stderr, "x264 [error]: can't set outfile param\n" );
1482         input.close_file( opt->hin );
1483         output.close_file( opt->hout, largest_pts, second_largest_pts );
1484         return -1;
1485     }
1486
1487     /* Create a new pic */
1488     if( input.picture_alloc( &pic, param->i_csp, param->i_width, param->i_height ) )
1489     {
1490         fprintf( stderr, "x264 [error]: malloc failed\n" );
1491         return -1;
1492     }
1493
1494     i_start = x264_mdate();
1495     /* ticks/frame = ticks/second / frames/second */
1496     ticks_per_frame = (int64_t)param->i_timebase_den * param->i_fps_den / param->i_timebase_num / param->i_fps_num;
1497     if( ticks_per_frame < 1 )
1498     {
1499         fprintf( stderr, "x264 [error]: ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame );
1500         return -1;
1501     }
1502
1503     if( !param->b_repeat_headers )
1504     {
1505         // Write SPS/PPS/SEI
1506         x264_nal_t *headers;
1507         int i_nal;
1508
1509         if( x264_encoder_headers( h, &headers, &i_nal ) < 0 )
1510         {
1511             fprintf( stderr, "x264 [error]: x264_encoder_headers failed\n" );
1512             return -1;
1513         }
1514
1515         if( (i_file = output.write_headers( opt->hout, headers )) < 0 )
1516             return -1;
1517     }
1518
1519     /* Encode frames */
1520     for( i_frame = 0, i_frame_output = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
1521     {
1522         if( input.read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
1523             break;
1524
1525         if( !param->b_vfr_input )
1526             pic.i_pts = i_frame;
1527         if( pic.i_pts <= largest_pts )
1528         {
1529             if( param->i_log_level >= X264_LOG_WARNING )
1530             {
1531                 if( param->i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
1532                     fprintf( stderr, "x264 [warning]: non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
1533                              i_frame, pic.i_pts * dts_compress_multiplier, largest_pts * dts_compress_multiplier );
1534                 else if( pts_warning_cnt == MAX_PTS_WARNING )
1535                     fprintf( stderr, "x264 [warning]: too many nonmonotonic pts warnings, suppressing further ones\n" );
1536                 pts_warning_cnt++;
1537             }
1538             pic.i_pts = largest_pts + ticks_per_frame;
1539         }
1540         second_largest_pts = largest_pts;
1541         largest_pts = pic.i_pts;
1542
1543         if( opt->qpfile )
1544             parse_qpfile( opt, &pic, i_frame + opt->i_seek );
1545         else
1546         {
1547             /* Do not force any parameters */
1548             pic.i_type = X264_TYPE_AUTO;
1549             pic.i_qpplus1 = 0;
1550         }
1551
1552         i_frame_size = Encode_frame( h, opt->hout, &pic, &last_pts );
1553         if( i_frame_size < 0 )
1554             return -1;
1555         i_file += i_frame_size;
1556         if( i_frame_size )
1557             i_frame_output++;
1558
1559         i_frame++;
1560
1561         if( input.release_frame && input.release_frame( &pic, opt->hin ) )
1562             break;
1563
1564         /* update status line (up to 1000 times per input file) */
1565         if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
1566             Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
1567     }
1568     /* Flush delayed frames */
1569     while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
1570     {
1571         i_frame_size = Encode_frame( h, opt->hout, NULL, &last_pts );
1572         if( i_frame_size < 0 )
1573             return -1;
1574         i_file += i_frame_size;
1575         if( i_frame_size )
1576             i_frame_output++;
1577         if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
1578             Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
1579     }
1580     if( pts_warning_cnt >= MAX_PTS_WARNING && param->i_log_level < X264_LOG_DEBUG )
1581         fprintf( stderr, "x264 [warning]: %d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
1582
1583     /* duration algorithm fails when only 1 frame is output */
1584     if( i_frame_output == 1 )
1585         duration = (double)param->i_fps_den / param->i_fps_num;
1586     else
1587         duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
1588     duration *= dts_compress_multiplier;
1589
1590     i_end = x264_mdate();
1591     input.picture_clean( &pic );
1592     /* Erase progress indicator before printing encoding stats. */
1593     if( opt->b_progress )
1594         fprintf( stderr, "                                                                               \r" );
1595     x264_encoder_close( h );
1596     fprintf( stderr, "\n" );
1597
1598     if( b_ctrl_c )
1599         fprintf( stderr, "aborted at input frame %d, output frame %d\n", opt->i_seek + i_frame, i_frame_output );
1600
1601     input.close_file( opt->hin );
1602     output.close_file( opt->hout, largest_pts, second_largest_pts );
1603
1604     if( i_frame_output > 0 )
1605     {
1606         double fps = (double)i_frame_output * (double)1000000 /
1607                      (double)( i_end - i_start );
1608
1609         fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame_output, fps,
1610                  (double) i_file * 8 / ( 1000 * duration ) );
1611     }
1612
1613     return 0;
1614 }