api change: select ratecontrol method with an enum (param.rc.i_rc_method) instead...
[x262.git] / x264.c
1 /*****************************************************************************
2  * x264: h264 encoder/decoder testing program.
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: x264.c,v 1.1 2004/06/03 19:24:12 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #define _LARGEFILE_SOURCE
25 #define _FILE_OFFSET_BITS 64
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <math.h>
31
32 #include <signal.h>
33 #define _GNU_SOURCE
34 #include <getopt.h>
35
36 #ifdef _MSC_VER
37 #include <io.h>     /* _setmode() */
38 #include <fcntl.h>  /* _O_BINARY */
39 #endif
40
41 #ifndef _MSC_VER
42 #include "config.h"
43 #endif
44
45 #include "common/common.h"
46 #include "x264.h"
47 #include "muxers.h"
48
49 #define DATA_MAX 3000000
50 uint8_t data[DATA_MAX];
51
52 /* Ctrl-C handler */
53 static int     b_ctrl_c = 0;
54 static int     b_exit_on_ctrl_c = 0;
55 static void    SigIntHandler( int a )
56 {
57     if( b_exit_on_ctrl_c )
58         exit(0);
59     b_ctrl_c = 1;
60 }
61
62 typedef struct {
63     int b_decompress;
64     int b_progress;
65     int i_seek;
66     hnd_t hin;
67     hnd_t hout;
68 } cli_opt_t;
69
70 /* input file operation function pointers */
71 int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
72 int (*p_get_frame_total)( hnd_t handle );
73 int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame );
74 int (*p_close_infile)( hnd_t handle );
75
76 /* output file operation function pointers */
77 static int (*p_open_outfile)( char *psz_filename, hnd_t *p_handle );
78 static int (*p_set_outfile_param)( hnd_t handle, x264_param_t *p_param );
79 static int (*p_write_nalu)( hnd_t handle, uint8_t *p_nal, int i_size );
80 static int (*p_set_eop)( hnd_t handle, x264_picture_t *p_picture );
81 static int (*p_close_outfile)( hnd_t handle );
82
83 static void Help( x264_param_t *defaults );
84 static int  Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
85 static int  Encode( x264_param_t *param, cli_opt_t *opt );
86
87
88 /****************************************************************************
89  * main:
90  ****************************************************************************/
91 int main( int argc, char **argv )
92 {
93     x264_param_t param;
94     cli_opt_t opt;
95
96 #ifdef _MSC_VER
97     _setmode(_fileno(stdin), _O_BINARY);
98     _setmode(_fileno(stdout), _O_BINARY);
99 #endif
100
101     x264_param_default( &param );
102
103     /* Parse command line */
104     if( Parse( argc, argv, &param, &opt ) < 0 )
105         return -1;
106
107     /* Control-C handler */
108     signal( SIGINT, SigIntHandler );
109
110     return Encode( &param, &opt );
111 }
112
113 static const char * const overscan_str[] = { "undef", "show", "crop", NULL };
114 static const char * const vidformat_str[] = { "component", "pal", "ntsc", "secam", "mac", "undef", NULL };
115 static const char * const fullrange_str[] = { "off", "on", NULL };
116 static const char * const colorprim_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "film", NULL };
117 static const char * const transfer_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "linear", "log100", "log316", NULL };
118 static const char * const colmatrix_str[] = { "GBR", "bt709", "undef", "", "fcc", "bt470bg", "smpte170m", "smpte240m", "YCgCo", NULL };
119
120 static char const *strtable_lookup( const char * const table[], int index )
121 {
122     int i = 0; while( table[i] ) i++;
123     return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
124 }
125
126 /*****************************************************************************
127  * Help:
128  *****************************************************************************/
129 static void Help( x264_param_t *defaults )
130 {
131     fprintf( stderr,
132              "x264 core:%d%s\n"
133              "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
134              "\n"
135              "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
136              "  or YUV4MPEG 4:2:0 (*.y4m),\n"
137              "  or AVI or Avisynth if compiled with AVIS support (%s).\n"
138              "Outfile type is selected by filename:\n"
139              " .264 -> Raw bytestream\n"
140              " .mkv -> Matroska\n"
141              " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
142              "\n"
143              "Options:\n"
144              "\n"
145              "  -h, --help                  Print this help\n"
146              "\n"
147              "Frame-type options:\n"
148              "\n"
149              "  -I, --keyint <integer>      Maximum GOP size [%d]\n"
150              "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n"
151              "      --scenecut <integer>    How aggressively to insert extra I-frames [%d]\n"
152              "  -b, --bframes <integer>     Number of B-frames between I and P [%d]\n"
153              "      --no-b-adapt            Disable adaptive B-frame decision\n"
154              "      --b-bias <integer>      Influences how often B-frames are used [%d]\n"
155              "      --b-pyramid             Keep some B-frames as references\n"
156              "\n"
157              "      --no-cabac              Disable CABAC\n"
158              "  -r, --ref <integer>         Number of reference frames [%d]\n"
159              "      --nf                    Disable loop filter\n"
160              "  -f, --filter <alpha:beta>   Loop filter AlphaC0 and Beta parameters [%d:%d]\n"
161              "\n"
162              "Ratecontrol:\n"
163              "\n"
164              "  -q, --qp <integer>          Set QP (0=lossless) [%d]\n"
165              "  -B, --bitrate <integer>     Set bitrate\n"
166              "      --crf <integer>         Quality-based VBR (nominal QP)\n"
167              "      --qpmin <integer>       Set min QP [%d]\n"
168              "      --qpmax <integer>       Set max QP [%d]\n"
169              "      --qpstep <integer>      Set max QP step [%d]\n"
170              "      --ratetol <float>       Allowed variance of average bitrate [%.1f]\n"
171              "      --vbv-maxrate <integer> Max local bitrate [%d]\n"
172              "      --vbv-bufsize <integer> Size of VBV buffer [%d]\n"
173              "      --vbv-init <float>      Initial VBV buffer occupancy [%.1f]\n"
174              "\n"
175              "      --ipratio <float>       QP factor between I and P [%.2f]\n"
176              "      --pbratio <float>       QP factor between P and B [%.2f]\n"
177              "      --chroma-qp-offset <integer>  QP difference between chroma and luma [%d]\n"
178              "\n"
179              "  -p, --pass <1|2|3>          Enable multipass ratecontrol:\n"
180              "                                  - 1: First pass, creates stats file\n"
181              "                                  - 2: Last pass, does not overwrite stats file\n"
182              "                                  - 3: Nth pass, overwrites stats file\n"
183              "      --stats <string>        Filename for 2 pass stats [\"%s\"]\n"
184              "      --rceq <string>         Ratecontrol equation [\"%s\"]\n"
185              "      --qcomp <float>         QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n"
186              "      --cplxblur <float>      Reduce fluctuations in QP (before curve compression) [%.1f]\n"
187              "      --qblur <float>         Reduce fluctuations in QP (after curve compression) [%.1f]\n"
188              "\n"
189              "      --zones <zone0>/<zone1>/...\n"
190              "                              Tweak the bitrate of some regions of the video\n"
191              "                              Each zone is of the form\n"
192              "                                  <start frame>,<end frame>,<option>\n"
193              "                                  where <option> is either\n"
194              "                                      q=<integer> (force QP)\n"
195              "                                  or  b=<float> (bitrate multiplier)\n"
196              "\n"
197              "Analysis:\n"
198              "\n"
199              "  -A, --analyse <string>      Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
200              "                                  - p8x8, p4x4, b8x8, i8x8, i4x4\n"
201              "                                  - none, all\n"
202              "                                  (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n"
203              "      --direct <string>       Direct MV prediction mode [\"%s\"]\n"
204              "                                  - none, spatial, temporal, auto\n"
205              "  -w, --weightb               Weighted prediction for B-frames\n"
206              "      --me <string>           Integer pixel motion estimation method [\"%s\"]\n"
207              "                                  - dia: diamond search, radius 1 (fast)\n"
208              "                                  - hex: hexagonal search, radius 2\n"
209              "                                  - umh: uneven multi-hexagon search\n"
210              "                                  - esa: exhaustive search (slow)\n"
211              "      --merange <integer>     Maximum motion vector search range [%d]\n"
212              "  -m, --subme <integer>       Subpixel motion estimation and partition\n"
213              "                                  decision quality: 1=fast, 7=best. [%d]\n"
214              "      --b-rdo                 RD based mode decision for B-frames. Requires subme 6.\n"
215              "      --mixed-refs            Decide references on a per partition basis\n"
216              "      --no-chroma-me          Ignore chroma in motion estimation\n"
217              "      --bime                  Jointly optimize both MVs in B-frames\n"
218              "  -8, --8x8dct                Adaptive spatial transform size\n"
219              "  -t, --trellis <integer>     Trellis RD quantization. Requires CABAC. [%d]\n"
220              "                                  - 0: disabled\n"
221              "                                  - 1: enabled only on the final encode of a MB\n"
222              "                                  - 2: enabled on all mode decisions\n"
223              "      --no-fast-pskip         Disables early SKIP detection on P-frames\n"
224              "      --no-dct-decimate       Disables coefficient thresholding on P-frames\n"
225              "      --nr <integer>          Noise reduction [%d]\n"
226              "\n"
227              "      --cqm <string>          Preset quant matrices [\"flat\"]\n"
228              "                                  - jvt, flat\n"
229              "      --cqmfile <string>      Read quant matrices from a JM-compatible file\n"
230              "                                  Overrides any other --cqm* options.\n"
231              "      --cqm4 <list>           Set all 4x4 quant matrices\n"
232              "                                  Takes a comma-separated list of 16 integers.\n"
233              "      --cqm8 <list>           Set all 8x8 quant matrices\n"
234              "                                  Takes a comma-separated list of 64 integers.\n"
235              "      --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
236              "                              Set both luma and chroma quant matrices\n"
237              "      --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
238              "                              Set individual quant matrices\n"
239              "\n"
240              "Video Usability Info (Annex E):\n"
241              "The VUI settings are not used by the encoder but are merely suggestions to\n"
242              "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n"
243              "\n"
244              "      --sar width:height      Specify Sample Aspect Ratio\n"
245              "      --overscan <string>     Specify crop overscan setting [\"%s\"]\n"
246              "                                  - undef, show, crop\n"
247              "      --videoformat <string>  Specify video format [\"%s\"]\n"
248              "                                  - component, pal, ntsc, secam, mac, undef\n"
249              "      --fullrange <string>    Specify full range samples setting [\"%s\"]\n"
250              "                                  - off, on\n"
251              "      --colorprim <string>    Specify color primaries [\"%s\"]\n"
252              "                                  - undef, bt709, bt470m, bt470bg\n"
253              "                                    smpte170m, smpte240m, film\n"
254              "      --transfer <string>     Specify transfer characteristics [\"%s\"]\n"
255              "                                  - undef, bt709, bt470m, bt470bg, linear,\n"
256              "                                    log100, log316, smpte170m, smpte240m\n"
257              "      --colormatrix <string>  Specify color matrix setting [\"%s\"]\n"
258              "                                  - undef, bt709, fcc, bt470bg\n"
259              "                                    smpte170m, smpte240m, GBR, YCgCo\n"
260              "      --chromaloc <integer>   Specify chroma sample location (0 to 5) [%d]\n"
261              "\n"
262              "Input/Output:\n"
263              "\n"
264              "      --level <string>        Specify level (as defined by Annex A)\n"
265              "      --fps <float|rational>  Specify framerate\n"
266              "      --seek <integer>        First frame to encode\n"
267              "      --frames <integer>      Maximum number of frames to encode\n"
268              "  -o, --output                Specify output file\n"
269              "\n"
270              "      --threads <integer>     Parallel encoding (uses slices)\n"
271              "      --thread-input          Run Avisynth in its own thread\n"
272              "      --no-asm                Disable all CPU optimizations\n"
273              "      --no-psnr               Disable PSNR computation\n"
274              "      --quiet                 Quiet Mode\n"
275              "  -v, --verbose               Print stats for each frame\n"
276              "      --progress              Show a progress indicator while encoding\n"
277              "      --visualize             Show MB types overlayed on the encoded video\n"
278              "      --sps-id <integer>      Set SPS and PPS id numbers [%d]\n"
279              "      --aud                   Use access unit delimiters\n"
280              "\n",
281             X264_BUILD, X264_VERSION,
282 #ifdef AVIS_INPUT
283             "yes",
284 #else
285             "no",
286 #endif
287 #ifdef MP4_OUTPUT
288             "yes",
289 #else
290             "no",
291 #endif
292             defaults->i_keyint_max,
293             defaults->i_keyint_min,
294             defaults->i_scenecut_threshold,
295             defaults->i_bframe,
296             defaults->i_bframe_bias,
297             defaults->i_frame_reference,
298             defaults->i_deblocking_filter_alphac0,
299             defaults->i_deblocking_filter_beta,
300             defaults->rc.i_qp_constant,
301             defaults->rc.i_qp_min,
302             defaults->rc.i_qp_max,
303             defaults->rc.i_qp_step,
304             defaults->rc.f_rate_tolerance,
305             defaults->rc.i_vbv_max_bitrate,
306             defaults->rc.i_vbv_buffer_size,
307             defaults->rc.f_vbv_buffer_init,
308             defaults->rc.f_ip_factor,
309             defaults->rc.f_pb_factor,
310             defaults->analyse.i_chroma_qp_offset,
311             defaults->rc.psz_stat_out,
312             defaults->rc.psz_rc_eq,
313             defaults->rc.f_qcompress,
314             defaults->rc.f_complexity_blur,
315             defaults->rc.f_qblur,
316             strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ),
317             strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ),
318             defaults->analyse.i_me_range,
319             defaults->analyse.i_subpel_refine,
320             defaults->analyse.i_trellis,
321             defaults->analyse.i_noise_reduction,
322             strtable_lookup( overscan_str, defaults->vui.i_overscan ),
323             strtable_lookup( vidformat_str, defaults->vui.i_vidformat ),
324             strtable_lookup( fullrange_str, defaults->vui.b_fullrange ),
325             strtable_lookup( colorprim_str, defaults->vui.i_colorprim ),
326             strtable_lookup( transfer_str, defaults->vui.i_transfer ),
327             strtable_lookup( colmatrix_str, defaults->vui.i_colmatrix ),
328             defaults->vui.i_chroma_loc,
329             defaults->i_sps_id
330            );
331 }
332
333 static int parse_enum( const char *arg, const char * const *names, int *dst )
334 {
335     int i;
336     for( i = 0; names[i]; i++ )
337         if( !strcmp( arg, names[i] ) )
338         {
339             *dst = i;
340             return 0;
341         }
342     return -1;
343 }
344
345 static int parse_cqm( const char *str, uint8_t *cqm, int length )
346 {
347     int i = 0;
348     do {
349         int coef;
350         if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
351             return -1;
352         cqm[i++] = coef;
353     } while( i < length && (str = strchr( str, ',' )) && str++ );
354     return (i == length) ? 0 : -1;
355 }
356
357 /*****************************************************************************
358  * Parse:
359  *****************************************************************************/
360 static int  Parse( int argc, char **argv,
361                    x264_param_t *param, cli_opt_t *opt )
362 {
363     char *psz_filename = NULL;
364     x264_param_t defaults = *param;
365     char *psz;
366     int b_avis = 0;
367     int b_y4m = 0;
368     int b_thread_input = 0;
369
370     memset( opt, 0, sizeof(cli_opt_t) );
371
372     /* Default input file driver */
373     p_open_infile = open_file_yuv;
374     p_get_frame_total = get_frame_total_yuv;
375     p_read_frame = read_frame_yuv;
376     p_close_infile = close_file_yuv;
377
378     /* Default output file driver */
379     p_open_outfile = open_file_bsf;
380     p_set_outfile_param = set_param_bsf;
381     p_write_nalu = write_nalu_bsf;
382     p_set_eop = set_eop_bsf;
383     p_close_outfile = close_file_bsf;
384
385     /* Parse command line options */
386     opterr = 0; // no error message
387     for( ;; )
388     {
389         int b_error = 0;
390         int long_options_index;
391 #define OPT_QPMIN 256
392 #define OPT_QPMAX 257
393 #define OPT_QPSTEP 258
394 #define OPT_IPRATIO 260
395 #define OPT_PBRATIO 261
396 #define OPT_RATETOL 262
397 #define OPT_RCSTATS 264
398 #define OPT_RCEQ 265
399 #define OPT_QCOMP 266
400 #define OPT_NOPSNR 267
401 #define OPT_QUIET 268
402 #define OPT_SCENECUT 270
403 #define OPT_QBLUR 271
404 #define OPT_CPLXBLUR 272
405 #define OPT_FRAMES 273
406 #define OPT_FPS 274
407 #define OPT_DIRECT 275
408 #define OPT_LEVEL 276
409 #define OPT_NOBADAPT 277
410 #define OPT_BBIAS 278
411 #define OPT_BPYRAMID 279
412 #define OPT_CHROMA_QP 280
413 #define OPT_NO_CHROMA_ME 281
414 #define OPT_NO_CABAC 282
415 #define OPT_AUD 283
416 #define OPT_PROGRESS 284
417 #define OPT_ME 285
418 #define OPT_MERANGE 286
419 #define OPT_VBVMAXRATE 287
420 #define OPT_VBVBUFSIZE 288
421 #define OPT_VBVINIT 289
422 #define OPT_VISUALIZE 290
423 #define OPT_SEEK 291
424 #define OPT_ZONES 292
425 #define OPT_THREADS 293
426 #define OPT_CQM 294
427 #define OPT_CQM4 295
428 #define OPT_CQM4I 296
429 #define OPT_CQM4IY 297
430 #define OPT_CQM4IC 298
431 #define OPT_CQM4P 299
432 #define OPT_CQM4PY 300
433 #define OPT_CQM4PC 301
434 #define OPT_CQM8 302
435 #define OPT_CQM8I 303
436 #define OPT_CQM8P 304
437 #define OPT_CQMFILE 305
438 #define OPT_SAR 306
439 #define OPT_OVERSCAN 307
440 #define OPT_VIDFORMAT 308
441 #define OPT_FULLRANGE 309
442 #define OPT_COLOURPRIM 310
443 #define OPT_TRANSFER 311
444 #define OPT_COLOURMATRIX 312
445 #define OPT_CHROMALOC 313
446 #define OPT_MIXED_REFS 314
447 #define OPT_CRF 315
448 #define OPT_B_RDO 316
449 #define OPT_NO_FAST_PSKIP 317
450 #define OPT_BIME 318
451 #define OPT_NR 319
452 #define OPT_THREAD_INPUT 320
453 #define OPT_NO_DCT_DECIMATE 321
454 #define OPT_SPS_ID 322
455
456         static struct option long_options[] =
457         {
458             { "help",    no_argument,       NULL, 'h' },
459             { "bitrate", required_argument, NULL, 'B' },
460             { "bframes", required_argument, NULL, 'b' },
461             { "no-b-adapt", no_argument,    NULL, OPT_NOBADAPT },
462             { "b-bias",  required_argument, NULL, OPT_BBIAS },
463             { "b-pyramid", no_argument,     NULL, OPT_BPYRAMID },
464             { "min-keyint",required_argument,NULL,'i' },
465             { "keyint",  required_argument, NULL, 'I' },
466             { "scenecut",required_argument, NULL, OPT_SCENECUT },
467             { "nf",      no_argument,       NULL, 'n' },
468             { "filter",  required_argument, NULL, 'f' },
469             { "no-cabac",no_argument,       NULL, OPT_NO_CABAC },
470             { "qp",      required_argument, NULL, 'q' },
471             { "qpmin",   required_argument, NULL, OPT_QPMIN },
472             { "qpmax",   required_argument, NULL, OPT_QPMAX },
473             { "qpstep",  required_argument, NULL, OPT_QPSTEP },
474             { "crf",     required_argument, NULL, OPT_CRF },
475             { "ref",     required_argument, NULL, 'r' },
476             { "no-asm",  no_argument,       NULL, 'C' },
477             { "sar",     required_argument, NULL, OPT_SAR },
478             { "fps",     required_argument, NULL, OPT_FPS },
479             { "frames",  required_argument, NULL, OPT_FRAMES },
480             { "seek",    required_argument, NULL, OPT_SEEK },
481             { "output",  required_argument, NULL, 'o' },
482             { "analyse", required_argument, NULL, 'A' },
483             { "direct",  required_argument, NULL, OPT_DIRECT },
484             { "weightb", no_argument,       NULL, 'w' },
485             { "me",      required_argument, NULL, OPT_ME },
486             { "merange", required_argument, NULL, OPT_MERANGE },
487             { "subme",   required_argument, NULL, 'm' },
488             { "b-rdo",   no_argument,       NULL, OPT_B_RDO },
489             { "mixed-refs", no_argument,    NULL, OPT_MIXED_REFS },
490             { "no-chroma-me", no_argument,  NULL, OPT_NO_CHROMA_ME },
491             { "bime",    no_argument,       NULL, OPT_BIME },
492             { "8x8dct",  no_argument,       NULL, '8' },
493             { "trellis", required_argument, NULL, 't' },
494             { "no-fast-pskip", no_argument, NULL, OPT_NO_FAST_PSKIP },
495             { "no-dct-decimate", no_argument, NULL, OPT_NO_DCT_DECIMATE },
496             { "level",   required_argument, NULL, OPT_LEVEL },
497             { "ratetol", required_argument, NULL, OPT_RATETOL },
498             { "vbv-maxrate", required_argument, NULL, OPT_VBVMAXRATE },
499             { "vbv-bufsize", required_argument, NULL, OPT_VBVBUFSIZE },
500             { "vbv-init", required_argument,NULL,  OPT_VBVINIT },
501             { "ipratio", required_argument, NULL, OPT_IPRATIO },
502             { "pbratio", required_argument, NULL, OPT_PBRATIO },
503             { "chroma-qp-offset", required_argument, NULL, OPT_CHROMA_QP },
504             { "pass",    required_argument, NULL, 'p' },
505             { "stats",   required_argument, NULL, OPT_RCSTATS },
506             { "rceq",    required_argument, NULL, OPT_RCEQ },
507             { "qcomp",   required_argument, NULL, OPT_QCOMP },
508             { "qblur",   required_argument, NULL, OPT_QBLUR },
509             { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
510             { "zones",   required_argument, NULL, OPT_ZONES },
511             { "threads", required_argument, NULL, OPT_THREADS },
512             { "thread-input", no_argument,  NULL, OPT_THREAD_INPUT },
513             { "no-psnr", no_argument,       NULL, OPT_NOPSNR },
514             { "quiet",   no_argument,       NULL, OPT_QUIET },
515             { "verbose", no_argument,       NULL, 'v' },
516             { "progress",no_argument,       NULL, OPT_PROGRESS },
517             { "visualize",no_argument,      NULL, OPT_VISUALIZE },
518             { "sps-id",  required_argument, NULL, OPT_SPS_ID },
519             { "aud",     no_argument,       NULL, OPT_AUD },
520             { "nr",      required_argument, NULL, OPT_NR },
521             { "cqm",     required_argument, NULL, OPT_CQM },
522             { "cqmfile", required_argument, NULL, OPT_CQMFILE },
523             { "cqm4",    required_argument, NULL, OPT_CQM4 },
524             { "cqm4i",   required_argument, NULL, OPT_CQM4I },
525             { "cqm4iy",  required_argument, NULL, OPT_CQM4IY },
526             { "cqm4ic",  required_argument, NULL, OPT_CQM4IC },
527             { "cqm4p",   required_argument, NULL, OPT_CQM4P },
528             { "cqm4py",  required_argument, NULL, OPT_CQM4PY },
529             { "cqm4pc",  required_argument, NULL, OPT_CQM4PC },
530             { "cqm8",    required_argument, NULL, OPT_CQM8 },
531             { "cqm8i",   required_argument, NULL, OPT_CQM8I },
532             { "cqm8p",   required_argument, NULL, OPT_CQM8P },
533             { "overscan", required_argument, NULL, OPT_OVERSCAN },
534             { "videoformat", required_argument, NULL, OPT_VIDFORMAT },
535             { "fullrange", required_argument, NULL, OPT_FULLRANGE },
536             { "colorprim", required_argument, NULL, OPT_COLOURPRIM },
537             { "transfer", required_argument, NULL, OPT_TRANSFER },
538             { "colormatrix", required_argument, NULL, OPT_COLOURMATRIX },
539             { "chromaloc", required_argument, NULL, OPT_CHROMALOC },
540             {0, 0, 0, 0}
541         };
542
543         int c;
544
545         c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:t:vw8",
546                          long_options, &long_options_index);
547
548         if( c == -1 )
549         {
550             break;
551         }
552
553         switch( c )
554         {
555             case 'h':
556                 Help( &defaults );
557                 return -1;
558
559             case 0:
560                 break;
561             case 'B':
562                 param->rc.i_bitrate = atol( optarg );
563                 param->rc.i_rc_method = X264_RC_ABR;
564                 break;
565             case OPT_CRF:
566                 param->rc.i_rf_constant = atol( optarg );
567                 param->rc.i_rc_method = X264_RC_CRF;
568                 break;
569             case 'b':
570                 param->i_bframe = atol( optarg );
571                 break;
572             case OPT_NOBADAPT:
573                 param->b_bframe_adaptive = 0;
574                 break;
575             case OPT_BBIAS:
576                 param->i_bframe_bias = atol( optarg );
577                 break;
578             case OPT_BPYRAMID:
579                 param->b_bframe_pyramid = 1;
580                 break;
581             case 'i':
582                 param->i_keyint_min = atol( optarg );
583                 if( param->i_keyint_max < param->i_keyint_min )
584                     param->i_keyint_max = param->i_keyint_min;
585                 break;
586             case 'I':
587                 param->i_keyint_max = atol( optarg );
588                 if( param->i_keyint_min > param->i_keyint_max )
589                     param->i_keyint_min = param->i_keyint_max;
590                 break;
591             case OPT_SCENECUT:
592                 param->i_scenecut_threshold = atol( optarg );
593                 break;
594             case 'n':
595                 param->b_deblocking_filter = 0;
596                 break;
597             case 'f':
598             {
599                 char *p = strchr( optarg, ':' );
600                 if( !p ) p = strchr( optarg, ',' );
601                 param->i_deblocking_filter_alphac0 = atoi( optarg );
602                 param->i_deblocking_filter_beta = p ? atoi( p+1 ) : param->i_deblocking_filter_alphac0;
603                 break;
604             }
605             case 'q':
606                 param->rc.i_qp_constant = atoi( optarg );
607                 param->rc.i_rc_method = X264_RC_CQP;
608                 break;
609             case OPT_QPMIN:
610                 param->rc.i_qp_min = atoi( optarg );
611                 break;
612             case OPT_QPMAX:
613                 param->rc.i_qp_max = atoi( optarg );
614                 break;
615             case OPT_QPSTEP:
616                 param->rc.i_qp_step = atoi( optarg );
617                 break;
618             case 'r':
619                 param->i_frame_reference = atoi( optarg );
620                 break;
621             case OPT_NO_CABAC:
622                 param->b_cabac = 0;
623                 break;
624             case 'x':
625                 opt->b_decompress = 1;
626                 break;
627             case 'C':
628                 param->cpu = 0;
629                 break;
630             case OPT_FRAMES:
631                 param->i_frame_total = atoi( optarg );
632                 break;
633             case OPT_SEEK:
634                 opt->i_seek = atoi( optarg );
635                 break;
636             case 'o':
637                 if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
638                 {
639 #ifdef MP4_OUTPUT
640                     p_open_outfile = open_file_mp4;
641                     p_write_nalu = write_nalu_mp4;
642                     p_set_outfile_param = set_param_mp4;
643                     p_set_eop = set_eop_mp4;
644                     p_close_outfile = close_file_mp4;
645 #else
646                     fprintf( stderr, "not compiled with MP4 output support\n" );
647                     return -1;
648 #endif
649                 }
650                 else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
651                 {
652                     p_open_outfile = open_file_mkv;
653                     p_write_nalu = write_nalu_mkv;
654                     p_set_outfile_param = set_param_mkv;
655                     p_set_eop = set_eop_mkv;
656                     p_close_outfile = close_file_mkv;
657                 }
658                 if( !strcmp(optarg, "-") )
659                     opt->hout = stdout;
660                 else if( p_open_outfile( optarg, &opt->hout ) )
661                 {
662                     fprintf( stderr, "cannot open output file `%s'\n", optarg );
663                     return -1;
664                 }
665                 break;
666             case OPT_SAR:
667             {
668                 char *p = strchr( optarg, ':' );
669                 if( !p ) p = strchr( optarg, '/' );
670                 if( p )
671                 {
672                     param->vui.i_sar_width = atoi( optarg );
673                     param->vui.i_sar_height = atoi( p + 1 );
674                 }
675                 break;
676             }
677             case OPT_FPS:
678             {
679                 float fps;
680                 if( sscanf( optarg, "%d/%d", &param->i_fps_num, &param->i_fps_den ) == 2 )
681                     ;
682                 else if( sscanf( optarg, "%f", &fps ) )
683                 {
684                     param->i_fps_num = (int)(fps * 1000 + .5);
685                     param->i_fps_den = 1000;
686                 }
687                 else
688                 {
689                     fprintf( stderr, "bad fps `%s'\n", optarg );
690                     return -1;
691                 }
692                 break;
693             }
694             case 'A':
695                 param->analyse.inter = 0;
696                 if( strstr( optarg, "none" ) )  param->analyse.inter =  0;
697                 if( strstr( optarg, "all" ) )   param->analyse.inter = ~0;
698
699                 if( strstr( optarg, "i4x4" ) )  param->analyse.inter |= X264_ANALYSE_I4x4;
700                 if( strstr( optarg, "i8x8" ) )  param->analyse.inter |= X264_ANALYSE_I8x8;
701                 if( strstr( optarg, "p8x8" ) )  param->analyse.inter |= X264_ANALYSE_PSUB16x16;
702                 if( strstr( optarg, "p4x4" ) )  param->analyse.inter |= X264_ANALYSE_PSUB8x8;
703                 if( strstr( optarg, "b8x8" ) )  param->analyse.inter |= X264_ANALYSE_BSUB16x16;
704                 break;
705             case OPT_DIRECT:
706                 b_error |= parse_enum( optarg, x264_direct_pred_names, &param->analyse.i_direct_mv_pred );
707                 break;
708             case 'w':
709                 param->analyse.b_weighted_bipred = 1;
710                 break;
711             case OPT_ME:
712                 b_error |= parse_enum( optarg, x264_motion_est_names, &param->analyse.i_me_method );
713                 break;
714             case OPT_MERANGE:
715                 param->analyse.i_me_range = atoi(optarg);
716                 break;
717             case 'm':
718                 param->analyse.i_subpel_refine = atoi(optarg);
719                 break;
720             case OPT_B_RDO:
721                 param->analyse.b_bframe_rdo = 1;
722                 break;
723             case OPT_MIXED_REFS:
724                 param->analyse.b_mixed_references = 1;
725                 break;
726             case OPT_NO_CHROMA_ME:
727                 param->analyse.b_chroma_me = 0;
728                 break;
729             case OPT_BIME:
730                 param->analyse.b_bidir_me = 1;
731                 break;
732             case '8':
733                 param->analyse.b_transform_8x8 = 1;
734                 break;
735             case 't':
736                 param->analyse.i_trellis = atoi(optarg);
737                 break;
738             case OPT_NO_FAST_PSKIP:
739                 param->analyse.b_fast_pskip = 0;
740                 break;
741             case OPT_NO_DCT_DECIMATE:
742                 param->analyse.b_dct_decimate = 0;
743                 break;
744             case OPT_LEVEL:
745                 if( atof(optarg) < 6 )
746                     param->i_level_idc = (int)(10*atof(optarg)+.5);
747                 else
748                     param->i_level_idc = atoi(optarg);
749                 break;
750             case OPT_RATETOL:
751                 param->rc.f_rate_tolerance = !strncmp("inf", optarg, 3) ? 1e9 : atof(optarg);
752                 break;
753             case OPT_VBVMAXRATE:
754                 param->rc.i_vbv_max_bitrate = atoi( optarg );
755                 break;
756             case OPT_VBVBUFSIZE:
757                 param->rc.i_vbv_buffer_size = atoi( optarg );
758                 break;
759             case OPT_VBVINIT:
760                 param->rc.f_vbv_buffer_init = atof(optarg);
761                 break;
762             case OPT_IPRATIO:
763                 param->rc.f_ip_factor = atof(optarg);
764                 break;
765             case OPT_PBRATIO:
766                 param->rc.f_pb_factor = atof(optarg);
767                 break;
768             case OPT_CHROMA_QP:
769                 param->analyse.i_chroma_qp_offset = atoi(optarg);
770                 break;
771             case 'p':
772             {
773                 int i_pass = atoi(optarg);
774                 if( i_pass == 1 )
775                     param->rc.b_stat_write = 1;
776                 else if( i_pass == 2 )
777                     param->rc.b_stat_read = 1;
778                 else if( i_pass > 2 )
779                     param->rc.b_stat_read =
780                     param->rc.b_stat_write = 1;
781                 break;
782             }
783             case OPT_RCSTATS:
784                 param->rc.psz_stat_in = optarg;
785                 param->rc.psz_stat_out = optarg;
786                 break;
787             case OPT_RCEQ:
788                 param->rc.psz_rc_eq = optarg;
789                break;
790             case OPT_QCOMP:
791                 param->rc.f_qcompress = atof(optarg);
792                 break;
793             case OPT_QBLUR:
794                 param->rc.f_qblur = atof(optarg);
795                 break;
796             case OPT_CPLXBLUR:
797                 param->rc.f_complexity_blur = atof(optarg);
798                 break;
799             case OPT_ZONES:
800                 param->rc.psz_zones = optarg;
801                 break;
802             case OPT_THREADS:
803                 param->i_threads = atoi(optarg);
804                 break;
805             case OPT_THREAD_INPUT:
806                 b_thread_input = 1;
807                 break;
808             case OPT_NOPSNR:
809                 param->analyse.b_psnr = 0;
810                 break;
811             case OPT_QUIET:
812                 param->i_log_level = X264_LOG_NONE;
813                 break;
814             case 'v':
815                 param->i_log_level = X264_LOG_DEBUG;
816                 break;
817             case OPT_SPS_ID:
818                 param->i_sps_id = atoi(optarg);
819                 break;
820             case OPT_AUD:
821                 param->b_aud = 1;
822                 break;
823             case OPT_PROGRESS:
824                 opt->b_progress = 1;
825                 break;
826             case OPT_VISUALIZE:
827 #ifdef VISUALIZE
828                 param->b_visualize = 1;
829                 b_exit_on_ctrl_c = 1;
830 #else
831                 fprintf( stderr, "not compiled with visualization support\n" );
832 #endif
833                 break;
834             case OPT_NR:
835                 param->analyse.i_noise_reduction = atoi(optarg);
836                 break;
837             case OPT_CQM:
838                 if( strstr( optarg, "flat" ) )
839                     param->i_cqm_preset = X264_CQM_FLAT;
840                 else if( strstr( optarg, "jvt" ) )
841                     param->i_cqm_preset = X264_CQM_JVT;
842                 else
843                 {
844                     fprintf( stderr, "bad CQM preset `%s'\n", optarg );
845                     return -1;
846                 }
847                 break;
848             case OPT_CQMFILE:
849                 param->psz_cqm_file = optarg;
850                 break;
851             case OPT_CQM4:
852                 param->i_cqm_preset = X264_CQM_CUSTOM;
853                 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
854                 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
855                 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
856                 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
857                 break;
858             case OPT_CQM8:
859                 param->i_cqm_preset = X264_CQM_CUSTOM;
860                 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
861                 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
862                 break;
863             case OPT_CQM4I:
864                 param->i_cqm_preset = X264_CQM_CUSTOM;
865                 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
866                 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
867                 break;
868             case OPT_CQM4P:
869                 param->i_cqm_preset = X264_CQM_CUSTOM;
870                 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
871                 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
872                 break;
873             case OPT_CQM4IY:
874                 param->i_cqm_preset = X264_CQM_CUSTOM;
875                 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
876                 break;
877             case OPT_CQM4IC:
878                 param->i_cqm_preset = X264_CQM_CUSTOM;
879                 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
880                 break;
881             case OPT_CQM4PY:
882                 param->i_cqm_preset = X264_CQM_CUSTOM;
883                 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
884                 break;
885             case OPT_CQM4PC:
886                 param->i_cqm_preset = X264_CQM_CUSTOM;
887                 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
888                 break;
889             case OPT_CQM8I:
890                 param->i_cqm_preset = X264_CQM_CUSTOM;
891                 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
892                 break;
893             case OPT_CQM8P:
894                 param->i_cqm_preset = X264_CQM_CUSTOM;
895                 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
896                 break;
897             case OPT_OVERSCAN:
898                 b_error |= parse_enum( optarg, overscan_str, &param->vui.i_overscan );
899                 break;
900             case OPT_VIDFORMAT:
901                 b_error |= parse_enum( optarg, vidformat_str, &param->vui.i_vidformat );
902                 break;
903             case OPT_FULLRANGE:
904                 b_error |= parse_enum( optarg, fullrange_str, &param->vui.b_fullrange );
905                 break;
906             case OPT_COLOURPRIM:
907                 b_error |= parse_enum( optarg, colorprim_str, &param->vui.i_colorprim );
908                 break;
909             case OPT_TRANSFER:
910                 b_error |= parse_enum( optarg, transfer_str, &param->vui.i_transfer );
911                 break;
912             case OPT_COLOURMATRIX:
913                 b_error |= parse_enum( optarg, colmatrix_str, &param->vui.i_colmatrix );
914                 break;
915             case OPT_CHROMALOC:
916                 param->vui.i_chroma_loc = atoi( optarg );
917                 b_error = ( param->vui.i_chroma_loc < 0 || param->vui.i_chroma_loc > 5 );
918                 break;
919             default:
920                 fprintf( stderr, "unknown option (%c)\n", optopt );
921                 return -1;
922         }
923
924         if( b_error )
925         {
926             fprintf( stderr, "bad argument: %s %s\n", argv[optind-2], optarg );
927             return -1;
928         }
929     }
930
931     /* Get the file name */
932     if( optind > argc - 1 || !opt->hout )
933     {
934         Help( &defaults );
935         return -1;
936     }
937     psz_filename = argv[optind++];
938
939     if( !opt->b_decompress )
940     {
941         if( optind > argc - 1 )
942         {
943             /* try to parse the file name */
944             for( psz = psz_filename; *psz; psz++ )
945             {
946                 if( *psz >= '0' && *psz <= '9'
947                     && sscanf( psz, "%ux%u", &param->i_width, &param->i_height ) == 2 )
948                 {
949                     if( param->i_log_level >= X264_LOG_INFO )
950                         fprintf( stderr, "x264 [info]: file name gives %dx%d\n", param->i_width, param->i_height );
951                     break;
952                 }
953             }
954         }
955         else
956         {
957             sscanf( argv[optind++], "%ux%u", &param->i_width, &param->i_height );
958         }
959         
960         /* check avis input */
961         psz = psz_filename + strlen(psz_filename) - 1;
962         while( psz > psz_filename && *psz != '.' )
963             psz--;
964
965         if( !strncasecmp( psz, ".avi", 4 ) || !strncasecmp( psz, ".avs", 4 ) )
966             b_avis = 1;
967         if( !strncasecmp( psz, ".y4m", 4 ) )
968             b_y4m = 1;
969         if( !(b_avis || b_y4m) && ( !param->i_width || !param->i_height ) )
970         {
971             Help( &defaults );
972             return -1;
973         }
974     }
975
976     /* open the input */
977     {
978         if( b_avis )
979         {
980 #ifdef AVIS_INPUT
981             p_open_infile = open_file_avis;
982             p_get_frame_total = get_frame_total_avis;
983             p_read_frame = read_frame_avis;
984             p_close_infile = close_file_avis;
985 #else
986             fprintf( stderr, "not compiled with AVIS input support\n" );
987             return -1;
988 #endif
989         }
990         if ( b_y4m )
991         {
992             p_open_infile = open_file_y4m;
993             p_get_frame_total = get_frame_total_y4m;
994             p_read_frame = read_frame_y4m;
995             p_close_infile = close_file_y4m;
996         }
997
998         if( p_open_infile( psz_filename, &opt->hin, param ) )
999         {
1000             fprintf( stderr, "could not open input file '%s'\n", psz_filename );
1001             return -1;
1002         }
1003     }
1004
1005 #ifdef HAVE_PTHREAD
1006     if( b_thread_input || param->i_threads > 1 )
1007     {
1008         if( open_file_thread( NULL, &opt->hin, param ) )
1009         {
1010             fprintf( stderr, "threaded input failed\n" );
1011         }
1012         else
1013         {
1014             p_open_infile = open_file_thread;
1015             p_get_frame_total = get_frame_total_thread;
1016             p_read_frame = read_frame_thread;
1017             p_close_infile = close_file_thread;
1018         }
1019     }
1020 #endif
1021
1022     return 0;
1023 }
1024
1025 /*****************************************************************************
1026  * Decode:
1027  *****************************************************************************/
1028
1029 static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
1030 {
1031     x264_picture_t pic_out;
1032     x264_nal_t *nal;
1033     int i_nal, i;
1034     int i_file = 0;
1035
1036     /* Do not force any parameters */
1037     if( pic )
1038     {
1039         pic->i_type = X264_TYPE_AUTO;
1040         pic->i_qpplus1 = 0;
1041     }
1042     if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
1043     {
1044         fprintf( stderr, "x264_encoder_encode failed\n" );
1045     }
1046
1047     for( i = 0; i < i_nal; i++ )
1048     {
1049         int i_size;
1050         int i_data;
1051
1052         i_data = DATA_MAX;
1053         if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
1054         {
1055             i_file += p_write_nalu( hout, data, i_size );
1056         }
1057         else if( i_size < 0 )
1058         {
1059             fprintf( stderr, "need to increase buffer size (size=%d)\n", -i_size );
1060         }
1061     }
1062     if (i_nal)
1063         p_set_eop( hout, &pic_out );
1064
1065     return i_file;
1066 }
1067
1068 /*****************************************************************************
1069  * Encode:
1070  *****************************************************************************/
1071 static int  Encode( x264_param_t *param, cli_opt_t *opt )
1072 {
1073     x264_t *h;
1074     x264_picture_t pic;
1075
1076     int     i_frame, i_frame_total;
1077     int64_t i_start, i_end;
1078     int64_t i_file;
1079     int     i_frame_size;
1080     int     i_progress;
1081
1082     i_frame_total = p_get_frame_total( opt->hin );
1083     i_frame_total -= opt->i_seek;
1084     if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
1085         && param->i_frame_total > 0 )
1086         i_frame_total = param->i_frame_total;
1087     param->i_frame_total = i_frame_total;
1088
1089     if( ( h = x264_encoder_open( param ) ) == NULL )
1090     {
1091         fprintf( stderr, "x264_encoder_open failed\n" );
1092         p_close_infile( opt->hin );
1093         p_close_outfile( opt->hout );
1094         return -1;
1095     }
1096
1097     if( p_set_outfile_param( opt->hout, param ) )
1098     {
1099         fprintf( stderr, "can't set outfile param\n" );
1100         p_close_infile( opt->hin );
1101         p_close_outfile( opt->hout );
1102         return -1;
1103     }
1104
1105     /* Create a new pic */
1106     x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
1107
1108     i_start = x264_mdate();
1109
1110     /* Encode frames */
1111     for( i_frame = 0, i_file = 0, i_progress = 0;
1112          b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
1113     {
1114         if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
1115             break;
1116
1117         pic.i_pts = (int64_t)i_frame * param->i_fps_den;
1118
1119         i_file += Encode_frame( h, opt->hout, &pic );
1120
1121         i_frame++;
1122
1123         /* update status line (up to 1000 times per input file) */
1124         if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG && 
1125             ( i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
1126                             : i_frame % 10 == 0 ) )
1127         {
1128             int64_t i_elapsed = x264_mdate() - i_start;
1129             double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1130             if( i_frame_total )
1131             {
1132                 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
1133                 i_progress = i_frame * 1000 / i_frame_total;
1134                 fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps, eta %d:%02d:%02d  \r",
1135                          i_frame, i_frame_total, (float)i_progress / 10, fps,
1136                          eta/3600, (eta/60)%60, eta%60 );
1137             }
1138             else
1139                 fprintf( stderr, "encoded frames: %d, %.2f fps   \r", i_frame, fps );
1140             fflush( stderr ); // needed in windows
1141         }
1142     }
1143     /* Flush delayed B-frames */
1144     do {
1145         i_file +=
1146         i_frame_size = Encode_frame( h, opt->hout, NULL );
1147     } while( i_frame_size );
1148
1149     i_end = x264_mdate();
1150     x264_picture_clean( &pic );
1151     x264_encoder_close( h );
1152     fprintf( stderr, "\n" );
1153
1154     if( b_ctrl_c )
1155         fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );
1156
1157     p_close_infile( opt->hin );
1158     p_close_outfile( opt->hout );
1159
1160     if( i_frame > 0 )
1161     {
1162         double fps = (double)i_frame * (double)1000000 /
1163                      (double)( i_end - i_start );
1164
1165         fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,
1166                  (double) i_file * 8 * param->i_fps_num /
1167                  ( (double) param->i_fps_den * i_frame * 1000 ) );
1168     }
1169
1170     return 0;
1171 }