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