]> git.sesse.net Git - x264/blob - x264.c
allow custom deadzones for non-trellis quantization.
[x264] / 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_progress;
64     int i_seek;
65     hnd_t hin;
66     hnd_t hout;
67     FILE *qpfile;
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, int b_longhelp );
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 char const *strtable_lookup( const char * const table[], int index )
114 {
115     int i = 0; while( table[i] ) i++;
116     return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
117 }
118
119 /*****************************************************************************
120  * Help:
121  *****************************************************************************/
122 static void Help( x264_param_t *defaults, int b_longhelp )
123 {
124 #define H0 printf
125 #define H1 if(b_longhelp) printf
126     H0( "x264 core:%d%s\n"
127         "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
128         "\n"
129         "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
130         "  or YUV4MPEG 4:2:0 (*.y4m),\n"
131         "  or AVI or Avisynth if compiled with AVIS support (%s).\n"
132         "Outfile type is selected by filename:\n"
133         " .264 -> Raw bytestream\n"
134         " .mkv -> Matroska\n"
135         " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
136         "\n"
137         "Options:\n"
138         "\n"
139         "  -h, --help                  List the more commonly used options\n"
140         "      --longhelp              List all options\n"
141         "\n",
142         X264_BUILD, X264_VERSION,
143 #ifdef AVIS_INPUT
144         "yes",
145 #else
146         "no",
147 #endif
148 #ifdef MP4_OUTPUT
149         "yes"
150 #else
151         "no"
152 #endif
153       );
154     H0( "Frame-type options:\n" );
155     H0( "\n" );
156     H0( "  -I, --keyint <integer>      Maximum GOP size [%d]\n", defaults->i_keyint_max );
157     H1( "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n", defaults->i_keyint_min );
158     H1( "      --scenecut <integer>    How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
159     H0( "  -b, --bframes <integer>     Number of B-frames between I and P [%d]\n", defaults->i_bframe );
160     H1( "      --no-b-adapt            Disable adaptive B-frame decision\n" );
161     H1( "      --b-bias <integer>      Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
162     H0( "      --b-pyramid             Keep some B-frames as references\n" );
163     H0( "      --no-cabac              Disable CABAC\n" );
164     H0( "  -r, --ref <integer>         Number of reference frames [%d]\n", defaults->i_frame_reference );
165     H1( "      --nf                    Disable loop filter\n" );
166     H0( "  -f, --filter <alpha:beta>   Loop filter AlphaC0 and Beta parameters [%d:%d]\n",
167                                        defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
168     H0( "      --interlaced            Enable pure-interlaced mode\n" );
169     H0( "\n" );
170     H0( "Ratecontrol:\n" );
171     H0( "\n" );
172     H0( "  -q, --qp <integer>          Set QP (0=lossless) [%d]\n", defaults->rc.i_qp_constant );
173     H0( "  -B, --bitrate <integer>     Set bitrate (kbit/s)\n" );
174     H0( "      --crf <integer>         Quality-based VBR (nominal QP)\n" );
175     H1( "      --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
176     H0( "      --vbv-bufsize <integer> Enable CBR and set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
177     H1( "      --vbv-init <float>      Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
178     H1( "      --qpmin <integer>       Set min QP [%d]\n", defaults->rc.i_qp_min );
179     H1( "      --qpmax <integer>       Set max QP [%d]\n", defaults->rc.i_qp_max );
180     H1( "      --qpstep <integer>      Set max QP step [%d]\n", defaults->rc.i_qp_step );
181     H0( "      --ratetol <float>       Allowed variance of average bitrate [%.1f]\n", defaults->rc.f_rate_tolerance );
182     H0( "      --ipratio <float>       QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
183     H0( "      --pbratio <float>       QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
184     H1( "      --chroma-qp-offset <integer>  QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
185     H0( "\n" );
186     H0( "  -p, --pass <1|2|3>          Enable multipass ratecontrol\n"
187         "                                  - 1: First pass, creates stats file\n"
188         "                                  - 2: Last pass, does not overwrite stats file\n"
189         "                                  - 3: Nth pass, overwrites stats file\n" );
190     H0( "      --stats <string>        Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
191     H1( "      --rceq <string>         Ratecontrol equation [\"%s\"]\n", defaults->rc.psz_rc_eq );
192     H0( "      --qcomp <float>         QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n", defaults->rc.f_qcompress );
193     H1( "      --cplxblur <float>      Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
194     H1( "      --qblur <float>         Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
195     H0( "      --zones <zone0>/<zone1>/...  Tweak the bitrate of some regions of the video\n" );
196     H1( "                              Each zone is of the form\n"
197         "                                  <start frame>,<end frame>,<option>\n"
198         "                                  where <option> is either\n"
199         "                                      q=<integer> (force QP)\n"
200         "                                  or  b=<float> (bitrate multiplier)\n" );
201     H1( "      --qpfile <string>       Force frametypes and QPs\n" );
202     H0( "\n" );
203     H0( "Analysis:\n" );
204     H0( "\n" );
205     H0( "  -A, --analyse <string>      Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
206         "                                  - p8x8, p4x4, b8x8, i8x8, i4x4\n"
207         "                                  - none, all\n"
208         "                                  (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
209     H0( "      --direct <string>       Direct MV prediction mode [\"%s\"]\n"
210         "                                  - none, spatial, temporal, auto\n",
211                                        strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
212     H0( "  -w, --weightb               Weighted prediction for B-frames\n" );
213     H0( "      --me <string>           Integer pixel motion estimation method [\"%s\"]\n",
214                                        strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
215     H1( "                                  - dia: diamond search, radius 1 (fast)\n"
216         "                                  - hex: hexagonal search, radius 2\n"
217         "                                  - umh: uneven multi-hexagon search\n"
218         "                                  - esa: exhaustive search (slow)\n" );
219     else H0( "                                  - dia, hex, umh\n" );
220     H0( "      --merange <integer>     Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
221     H0( "  -m, --subme <integer>       Subpixel motion estimation and partition\n"
222         "                                  decision quality: 1=fast, 7=best. [%d]\n", defaults->analyse.i_subpel_refine );
223     H0( "      --b-rdo                 RD based mode decision for B-frames. Requires subme 6.\n" );
224     H0( "      --mixed-refs            Decide references on a per partition basis\n" );
225     H1( "      --no-chroma-me          Ignore chroma in motion estimation\n" );
226     H1( "      --bime                  Jointly optimize both MVs in B-frames\n" );
227     H0( "  -8, --8x8dct                Adaptive spatial transform size\n" );
228     H0( "  -t, --trellis <integer>     Trellis RD quantization. Requires CABAC. [%d]\n"
229         "                                  - 0: disabled\n"
230         "                                  - 1: enabled only on the final encode of a MB\n"
231         "                                  - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
232     H0( "      --no-fast-pskip         Disables early SKIP detection on P-frames\n" );
233     H0( "      --no-dct-decimate       Disables coefficient thresholding on P-frames\n" );
234     H0( "      --nr <integer>          Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
235     H1( "\n" );
236     H1( "      --deadzone-inter <int>  Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
237     H1( "      --deadzone-intra <int>  Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
238     H1( "                                  Deadzones should be in the range 0 - 32.\n" );
239     H1( "      --cqm <string>          Preset quant matrices [\"flat\"]\n"
240         "                                  - jvt, flat\n" );
241     H0( "      --cqmfile <string>      Read custom quant matrices from a JM-compatible file\n" );
242     H1( "                                  Overrides any other --cqm* options.\n" );
243     H1( "      --cqm4 <list>           Set all 4x4 quant matrices\n"
244         "                                  Takes a comma-separated list of 16 integers.\n" );
245     H1( "      --cqm8 <list>           Set all 8x8 quant matrices\n"
246         "                                  Takes a comma-separated list of 64 integers.\n" );
247     H1( "      --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
248         "                              Set both luma and chroma quant matrices\n" );
249     H1( "      --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
250         "                              Set individual quant matrices\n" );
251     H1( "\n" );
252     H1( "Video Usability Info (Annex E):\n" );
253     H1( "The VUI settings are not used by the encoder but are merely suggestions to\n" );
254     H1( "the playback equipment. See doc/vui.txt for details. Use at your own risk.\n" );
255     H1( "\n" );
256     H1( "      --overscan <string>     Specify crop overscan setting [\"%s\"]\n"
257         "                                  - undef, show, crop\n",
258                                        strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ) );
259     H1( "      --videoformat <string>  Specify video format [\"%s\"]\n"
260         "                                  - component, pal, ntsc, secam, mac, undef\n",
261                                        strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ) );
262     H1( "      --fullrange <string>    Specify full range samples setting [\"%s\"]\n"
263         "                                  - off, on\n",
264                                        strtable_lookup( x264_fullrange_names, defaults->vui.b_fullrange ) );
265     H1( "      --colorprim <string>    Specify color primaries [\"%s\"]\n"
266         "                                  - undef, bt709, bt470m, bt470bg\n"
267         "                                    smpte170m, smpte240m, film\n",
268                                        strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ) );
269     H1( "      --transfer <string>     Specify transfer characteristics [\"%s\"]\n"
270         "                                  - undef, bt709, bt470m, bt470bg, linear,\n"
271         "                                    log100, log316, smpte170m, smpte240m\n",
272                                        strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ) );
273     H1( "      --colormatrix <string>  Specify color matrix setting [\"%s\"]\n"
274         "                                  - undef, bt709, fcc, bt470bg\n"
275         "                                    smpte170m, smpte240m, GBR, YCgCo\n",
276                                        strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
277     H1( "      --chromaloc <integer>   Specify chroma sample location (0 to 5) [%d]\n",
278                                        defaults->vui.i_chroma_loc );
279     H0( "\n" );
280     H0( "Input/Output:\n" );
281     H0( "\n" );
282     H0( "  -o, --output                Specify output file\n" );
283     H0( "      --sar width:height      Specify Sample Aspect Ratio\n" );
284     H0( "      --fps <float|rational>  Specify framerate\n" );
285     H0( "      --seek <integer>        First frame to encode\n" );
286     H0( "      --frames <integer>      Maximum number of frames to encode\n" );
287     H0( "      --level <string>        Specify level (as defined by Annex A)\n" );
288     H0( "\n" );
289     H0( "  -v, --verbose               Print stats for each frame\n" );
290     H0( "      --progress              Show a progress indicator while encoding\n" );
291     H0( "      --quiet                 Quiet Mode\n" );
292     H0( "      --no-psnr               Disable PSNR computation\n" );
293     H0( "      --no-ssim               Disable SSIM computation\n" );
294     H0( "      --threads <integer>     Parallel encoding (uses slices)\n" );
295     H0( "      --thread-input          Run Avisynth in its own thread\n" );
296     H1( "      --no-asm                Disable all CPU optimizations\n" );
297     H1( "      --visualize             Show MB types overlayed on the encoded video\n" );
298     H1( "      --sps-id <integer>      Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
299     H1( "      --aud                   Use access unit delimiters\n" );
300     H0( "\n" );
301 }
302
303 /*****************************************************************************
304  * Parse:
305  *****************************************************************************/
306 static int  Parse( int argc, char **argv,
307                    x264_param_t *param, cli_opt_t *opt )
308 {
309     char *psz_filename = NULL;
310     x264_param_t defaults = *param;
311     char *psz;
312     int b_avis = 0;
313     int b_y4m = 0;
314     int b_thread_input = 0;
315
316     memset( opt, 0, sizeof(cli_opt_t) );
317
318     /* Default input file driver */
319     p_open_infile = open_file_yuv;
320     p_get_frame_total = get_frame_total_yuv;
321     p_read_frame = read_frame_yuv;
322     p_close_infile = close_file_yuv;
323
324     /* Default output file driver */
325     p_open_outfile = open_file_bsf;
326     p_set_outfile_param = set_param_bsf;
327     p_write_nalu = write_nalu_bsf;
328     p_set_eop = set_eop_bsf;
329     p_close_outfile = close_file_bsf;
330
331     /* Parse command line options */
332     for( ;; )
333     {
334         int b_error = 0;
335         int long_options_index = -1;
336
337 #define OPT_FRAMES 256
338 #define OPT_SEEK 257
339 #define OPT_QPFILE 258
340 #define OPT_THREAD_INPUT 259
341 #define OPT_QUIET 260
342 #define OPT_PROGRESS 261
343 #define OPT_VISUALIZE 262
344 #define OPT_LONGHELP 263
345
346         static struct option long_options[] =
347         {
348             { "help",    no_argument,       NULL, 'h' },
349             { "longhelp",no_argument,       NULL, OPT_LONGHELP },
350             { "version", no_argument,       NULL, 'V' },
351             { "bitrate", required_argument, NULL, 'B' },
352             { "bframes", required_argument, NULL, 'b' },
353             { "no-b-adapt", no_argument,    NULL, 0 },
354             { "b-bias",  required_argument, NULL, 0 },
355             { "b-pyramid", no_argument,     NULL, 0 },
356             { "min-keyint",required_argument,NULL,'i' },
357             { "keyint",  required_argument, NULL, 'I' },
358             { "scenecut",required_argument, NULL, 0 },
359             { "nf",      no_argument,       NULL, 0 },
360             { "filter",  required_argument, NULL, 'f' },
361             { "interlaced", no_argument,    NULL, 0 },
362             { "no-cabac",no_argument,       NULL, 0 },
363             { "qp",      required_argument, NULL, 'q' },
364             { "qpmin",   required_argument, NULL, 0 },
365             { "qpmax",   required_argument, NULL, 0 },
366             { "qpstep",  required_argument, NULL, 0 },
367             { "crf",     required_argument, NULL, 0 },
368             { "ref",     required_argument, NULL, 'r' },
369             { "no-asm",  no_argument,       NULL, 0 },
370             { "sar",     required_argument, NULL, 0 },
371             { "fps",     required_argument, NULL, 0 },
372             { "frames",  required_argument, NULL, OPT_FRAMES },
373             { "seek",    required_argument, NULL, OPT_SEEK },
374             { "output",  required_argument, NULL, 'o' },
375             { "analyse", required_argument, NULL, 'A' },
376             { "direct",  required_argument, NULL, 0 },
377             { "weightb", no_argument,       NULL, 'w' },
378             { "me",      required_argument, NULL, 0 },
379             { "merange", required_argument, NULL, 0 },
380             { "subme",   required_argument, NULL, 'm' },
381             { "b-rdo",   no_argument,       NULL, 0 },
382             { "mixed-refs", no_argument,    NULL, 0 },
383             { "no-chroma-me", no_argument,  NULL, 0 },
384             { "bime",    no_argument,       NULL, 0 },
385             { "8x8dct",  no_argument,       NULL, '8' },
386             { "trellis", required_argument, NULL, 't' },
387             { "no-fast-pskip", no_argument, NULL, 0 },
388             { "no-dct-decimate", no_argument, NULL, 0 },
389             { "deadzone-inter", required_argument, NULL, '0' },
390             { "deadzone-intra", required_argument, NULL, '0' },
391             { "level",   required_argument, NULL, 0 },
392             { "ratetol", required_argument, NULL, 0 },
393             { "vbv-maxrate", required_argument, NULL, 0 },
394             { "vbv-bufsize", required_argument, NULL, 0 },
395             { "vbv-init", required_argument,NULL,  0 },
396             { "ipratio", required_argument, NULL, 0 },
397             { "pbratio", required_argument, NULL, 0 },
398             { "chroma-qp-offset", required_argument, NULL, 0 },
399             { "pass",    required_argument, NULL, 'p' },
400             { "stats",   required_argument, NULL, 0 },
401             { "rceq",    required_argument, NULL, 0 },
402             { "qcomp",   required_argument, NULL, 0 },
403             { "qblur",   required_argument, NULL, 0 },
404             { "cplxblur",required_argument, NULL, 0 },
405             { "zones",   required_argument, NULL, 0 },
406             { "qpfile",  required_argument, NULL, OPT_QPFILE },
407             { "threads", required_argument, NULL, 0 },
408             { "thread-input", no_argument,  NULL, OPT_THREAD_INPUT },
409             { "no-psnr", no_argument,       NULL, 0 },
410             { "no-ssim", no_argument,       NULL, 0 },
411             { "quiet",   no_argument,       NULL, OPT_QUIET },
412             { "verbose", no_argument,       NULL, 'v' },
413             { "progress",no_argument,       NULL, OPT_PROGRESS },
414             { "visualize",no_argument,      NULL, OPT_VISUALIZE },
415             { "sps-id",  required_argument, NULL, 0 },
416             { "aud",     no_argument,       NULL, 0 },
417             { "nr",      required_argument, NULL, 0 },
418             { "cqm",     required_argument, NULL, 0 },
419             { "cqmfile", required_argument, NULL, 0 },
420             { "cqm4",    required_argument, NULL, 0 },
421             { "cqm4i",   required_argument, NULL, 0 },
422             { "cqm4iy",  required_argument, NULL, 0 },
423             { "cqm4ic",  required_argument, NULL, 0 },
424             { "cqm4p",   required_argument, NULL, 0 },
425             { "cqm4py",  required_argument, NULL, 0 },
426             { "cqm4pc",  required_argument, NULL, 0 },
427             { "cqm8",    required_argument, NULL, 0 },
428             { "cqm8i",   required_argument, NULL, 0 },
429             { "cqm8p",   required_argument, NULL, 0 },
430             { "overscan", required_argument, NULL, 0 },
431             { "videoformat", required_argument, NULL, 0 },
432             { "fullrange", required_argument, NULL, 0 },
433             { "colorprim", required_argument, NULL, 0 },
434             { "transfer", required_argument, NULL, 0 },
435             { "colormatrix", required_argument, NULL, 0 },
436             { "chromaloc", required_argument, NULL, 0 },
437             {0, 0, 0, 0}
438         };
439
440         int c = getopt_long( argc, argv, "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw",
441                              long_options, &long_options_index);
442
443         if( c == -1 )
444         {
445             break;
446         }
447
448         switch( c )
449         {
450             case 'h':
451                 Help( &defaults, 0 );
452                 exit(0);
453             case OPT_LONGHELP:
454                 Help( &defaults, 1 );
455                 exit(0);
456             case 'V':
457 #ifdef X264_POINTVER
458                 printf( "x264 "X264_POINTVER"\n" );
459 #else
460                 printf( "x264 0.%d.X\n", X264_BUILD );
461 #endif
462                 exit(0);
463             case OPT_FRAMES:
464                 param->i_frame_total = atoi( optarg );
465                 break;
466             case OPT_SEEK:
467                 opt->i_seek = atoi( optarg );
468                 break;
469             case 'o':
470                 if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
471                 {
472 #ifdef MP4_OUTPUT
473                     p_open_outfile = open_file_mp4;
474                     p_write_nalu = write_nalu_mp4;
475                     p_set_outfile_param = set_param_mp4;
476                     p_set_eop = set_eop_mp4;
477                     p_close_outfile = close_file_mp4;
478 #else
479                     fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
480                     return -1;
481 #endif
482                 }
483                 else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
484                 {
485                     p_open_outfile = open_file_mkv;
486                     p_write_nalu = write_nalu_mkv;
487                     p_set_outfile_param = set_param_mkv;
488                     p_set_eop = set_eop_mkv;
489                     p_close_outfile = close_file_mkv;
490                 }
491                 if( !strcmp(optarg, "-") )
492                     opt->hout = stdout;
493                 else if( p_open_outfile( optarg, &opt->hout ) )
494                 {
495                     fprintf( stderr, "x264 [error]: can't open output file `%s'\n", optarg );
496                     return -1;
497                 }
498                 break;
499             case OPT_QPFILE:
500                 opt->qpfile = fopen( optarg, "r" );
501                 if( !opt->qpfile )
502                 {
503                     fprintf( stderr, "x264 [error]: can't open `%s'\n", optarg );
504                     return -1;
505                 }
506                 param->i_scenecut_threshold = -1;
507                 param->b_bframe_adaptive = 0;
508                 break;
509             case OPT_THREAD_INPUT:
510                 b_thread_input = 1;
511                 break;
512             case OPT_QUIET:
513                 param->i_log_level = X264_LOG_NONE;
514                 break;
515             case 'v':
516                 param->i_log_level = X264_LOG_DEBUG;
517                 break;
518             case OPT_PROGRESS:
519                 opt->b_progress = 1;
520                 break;
521             case OPT_VISUALIZE:
522 #ifdef VISUALIZE
523                 param->b_visualize = 1;
524                 b_exit_on_ctrl_c = 1;
525 #else
526                 fprintf( stderr, "x264 [warning]: not compiled with visualization support\n" );
527 #endif
528                 break;
529             default:
530             {
531                 int i;
532                 if( long_options_index < 0 )
533                 {
534                     for( i = 0; long_options[i].name; i++ )
535                         if( long_options[i].val == c )
536                         {
537                             long_options_index = i;
538                             break;
539                         }
540                     if( long_options_index < 0 )
541                     {
542                         /* getopt_long already printed an error message */
543                         return -1;
544                     }
545                 }
546
547                 b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg ? optarg : "true" );
548             }
549         }
550
551         if( b_error )
552         {
553             const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
554             fprintf( stderr, "x264 [error]: invalid argument: %s = %s\n", name, optarg );
555             return -1;
556         }
557     }
558
559     /* Get the file name */
560     if( optind > argc - 1 || !opt->hout )
561     {
562         fprintf( stderr, "x264 [error]: No %s file. Run x264 --help for a list of options.\n",
563                  optind > argc - 1 ? "input" : "output" );
564         return -1;
565     }
566     psz_filename = argv[optind++];
567
568     /* check demuxer type */
569     psz = psz_filename + strlen(psz_filename) - 1;
570     while( psz > psz_filename && *psz != '.' )
571         psz--;
572     if( !strncasecmp( psz, ".avi", 4 ) || !strncasecmp( psz, ".avs", 4 ) )
573         b_avis = 1;
574     if( !strncasecmp( psz, ".y4m", 4 ) )
575         b_y4m = 1;
576
577     if( !(b_avis || b_y4m) ) // raw yuv
578     {
579         if( optind > argc - 1 )
580         {
581             /* try to parse the file name */
582             for( psz = psz_filename; *psz; psz++ )
583             {
584                 if( *psz >= '0' && *psz <= '9'
585                     && sscanf( psz, "%ux%u", &param->i_width, &param->i_height ) == 2 )
586                 {
587                     if( param->i_log_level >= X264_LOG_INFO )
588                         fprintf( stderr, "x264 [info]: file name gives %dx%d\n", param->i_width, param->i_height );
589                     break;
590                 }
591             }
592         }
593         else
594         {
595             sscanf( argv[optind++], "%ux%u", &param->i_width, &param->i_height );
596         }
597     }
598         
599     if( !(b_avis || b_y4m) && ( !param->i_width || !param->i_height ) )
600     {
601         fprintf( stderr, "x264 [error]: Rawyuv input requires a resolution.\n" );
602         return -1;
603     }
604
605     /* open the input */
606     {
607         if( b_avis )
608         {
609 #ifdef AVIS_INPUT
610             p_open_infile = open_file_avis;
611             p_get_frame_total = get_frame_total_avis;
612             p_read_frame = read_frame_avis;
613             p_close_infile = close_file_avis;
614 #else
615             fprintf( stderr, "x264 [error]: not compiled with AVIS input support\n" );
616             return -1;
617 #endif
618         }
619         if ( b_y4m )
620         {
621             p_open_infile = open_file_y4m;
622             p_get_frame_total = get_frame_total_y4m;
623             p_read_frame = read_frame_y4m;
624             p_close_infile = close_file_y4m;
625         }
626
627         if( p_open_infile( psz_filename, &opt->hin, param ) )
628         {
629             fprintf( stderr, "x264 [error]: could not open input file '%s'\n", psz_filename );
630             return -1;
631         }
632     }
633
634 #ifdef HAVE_PTHREAD
635     if( b_thread_input || param->i_threads > 1 )
636     {
637         if( open_file_thread( NULL, &opt->hin, param ) )
638         {
639             fprintf( stderr, "x264 [warning]: threaded input failed\n" );
640         }
641         else
642         {
643             p_open_infile = open_file_thread;
644             p_get_frame_total = get_frame_total_thread;
645             p_read_frame = read_frame_thread;
646             p_close_infile = close_file_thread;
647         }
648     }
649 #endif
650
651     return 0;
652 }
653
654 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
655 {
656     int num = -1, qp;
657     char type;
658     while( num < i_frame )
659     {
660         int ret = fscanf( opt->qpfile, "%d %c %d\n", &num, &type, &qp );
661         if( num < i_frame )
662             continue;
663         pic->i_qpplus1 = qp+1;
664         if     ( type == 'I' ) pic->i_type = X264_TYPE_IDR;
665         else if( type == 'i' ) pic->i_type = X264_TYPE_I;
666         else if( type == 'P' ) pic->i_type = X264_TYPE_P;
667         else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
668         else if( type == 'b' ) pic->i_type = X264_TYPE_B;
669         else ret = 0;
670         if( ret != 3 || qp < 0 || qp > 51 || num > i_frame )
671         {
672             fprintf( stderr, "x264 [error]: can't parse qpfile for frame %d\n", i_frame );
673             fclose( opt->qpfile );
674             opt->qpfile = NULL;
675             pic->i_type = X264_TYPE_AUTO;
676             pic->i_qpplus1 = 0;
677             break;
678         }
679     }
680 }
681
682 /*****************************************************************************
683  * Decode:
684  *****************************************************************************/
685
686 static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
687 {
688     x264_picture_t pic_out;
689     x264_nal_t *nal;
690     int i_nal, i;
691     int i_file = 0;
692
693     if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
694     {
695         fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
696     }
697
698     for( i = 0; i < i_nal; i++ )
699     {
700         int i_size;
701         int i_data;
702
703         i_data = DATA_MAX;
704         if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
705         {
706             i_file += p_write_nalu( hout, data, i_size );
707         }
708         else if( i_size < 0 )
709         {
710             fprintf( stderr, "x264 [error]: need to increase buffer size (size=%d)\n", -i_size );
711         }
712     }
713     if (i_nal)
714         p_set_eop( hout, &pic_out );
715
716     return i_file;
717 }
718
719 /*****************************************************************************
720  * Encode:
721  *****************************************************************************/
722 static int  Encode( x264_param_t *param, cli_opt_t *opt )
723 {
724     x264_t *h;
725     x264_picture_t pic;
726
727     int     i_frame, i_frame_total;
728     int64_t i_start, i_end;
729     int64_t i_file;
730     int     i_frame_size;
731     int     i_progress;
732
733     i_frame_total = p_get_frame_total( opt->hin );
734     i_frame_total -= opt->i_seek;
735     if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
736         && param->i_frame_total > 0 )
737         i_frame_total = param->i_frame_total;
738     param->i_frame_total = i_frame_total;
739
740     if( ( h = x264_encoder_open( param ) ) == NULL )
741     {
742         fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
743         p_close_infile( opt->hin );
744         p_close_outfile( opt->hout );
745         return -1;
746     }
747
748     if( p_set_outfile_param( opt->hout, param ) )
749     {
750         fprintf( stderr, "x264 [error]: can't set outfile param\n" );
751         p_close_infile( opt->hin );
752         p_close_outfile( opt->hout );
753         return -1;
754     }
755
756     /* Create a new pic */
757     x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
758
759     i_start = x264_mdate();
760
761     /* Encode frames */
762     for( i_frame = 0, i_file = 0, i_progress = 0;
763          b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
764     {
765         if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
766             break;
767
768         pic.i_pts = (int64_t)i_frame * param->i_fps_den;
769
770         if( opt->qpfile )
771             parse_qpfile( opt, &pic, i_frame + opt->i_seek );
772         else
773         {
774             /* Do not force any parameters */
775             pic.i_type = X264_TYPE_AUTO;
776             pic.i_qpplus1 = 0;
777         }
778
779         i_file += Encode_frame( h, opt->hout, &pic );
780
781         i_frame++;
782
783         /* update status line (up to 1000 times per input file) */
784         if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG && 
785             ( i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
786                             : i_frame % 10 == 0 ) )
787         {
788             int64_t i_elapsed = x264_mdate() - i_start;
789             double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
790             if( i_frame_total )
791             {
792                 int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
793                 i_progress = i_frame * 1000 / i_frame_total;
794                 fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps, eta %d:%02d:%02d  \r",
795                          i_frame, i_frame_total, (float)i_progress / 10, fps,
796                          eta/3600, (eta/60)%60, eta%60 );
797             }
798             else
799                 fprintf( stderr, "encoded frames: %d, %.2f fps   \r", i_frame, fps );
800             fflush( stderr ); // needed in windows
801         }
802     }
803     /* Flush delayed B-frames */
804     do {
805         i_file +=
806         i_frame_size = Encode_frame( h, opt->hout, NULL );
807     } while( i_frame_size );
808
809     i_end = x264_mdate();
810     x264_picture_clean( &pic );
811     x264_encoder_close( h );
812     fprintf( stderr, "\n" );
813
814     if( b_ctrl_c )
815         fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );
816
817     p_close_infile( opt->hin );
818     p_close_outfile( opt->hout );
819
820     if( i_frame > 0 )
821     {
822         double fps = (double)i_frame * (double)1000000 /
823                      (double)( i_end - i_start );
824
825         fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,
826                  (double) i_file * 8 * param->i_fps_num /
827                  ( (double) param->i_fps_den * i_frame * 1000 ) );
828     }
829
830     return 0;
831 }