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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
35 #include <io.h> /* _setmode() */
36 #include <fcntl.h> /* _O_BINARY */
39 #include "common/common.h"
42 #define DATA_MAX 3000000
43 uint8_t data[DATA_MAX];
46 static int i_ctrl_c = 0;
47 static void SigIntHandler( int a )
52 static void Help( void );
53 static int Parse( int argc, char **argv, x264_param_t *param, FILE **p_fin, FILE **p_fout, int *pb_decompress );
54 static int Encode( x264_param_t *param, FILE *fyuv, FILE *fout );
55 static int Decode( x264_param_t *param, FILE *fh26l, FILE *fout );
57 /****************************************************************************
59 ****************************************************************************/
60 int main( int argc, char **argv )
71 _setmode(_fileno(stdin), _O_BINARY); /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
72 _setmode(_fileno(stdout), _O_BINARY);
75 x264_param_default( ¶m );
77 /* Parse command line */
78 if( Parse( argc, argv, ¶m, &fin, &fout, &b_decompress ) < 0 )
83 /* Control-C handler */
84 signal( SIGINT, SigIntHandler );
87 i_ret = Decode( ¶m, fin, fout );
89 i_ret = Encode( ¶m, fin, fout );
94 /*****************************************************************************
96 *****************************************************************************/
97 static void Help( void )
100 "x264 build:0x%4.4x\n"
101 "Syntax: x264 [options] [-o out.h26l] in.yuv widthxheigh\n"
103 " -h, --help Print this help\n"
105 " -I, --idrframe <integer> Each 'number' I frames are IDR frames\n"
106 " -i, --iframe <integer> Max interval between I frames\n"
107 " --scenecut <integer> How aggresively to insert extra I frames\n"
108 " -b, --bframe <integer> Number of B-frames between I and P\n"
110 " -c, --cabac Enable CABAC\n"
111 " -r, --ref <integer> Number of references\n"
112 " -n, --nf Disable loop filter\n"
113 " -f, --filter <alpha:beta> Loop filter AplhaCO and Beta parameters\n"
115 " -q, --qp <integer> Set QP\n"
116 " -B, --bitrate <integer> Set bitrate\n"
117 " --qpmin <integer> Set min QP\n"
118 " --qpmax <integer> Set max QP\n"
119 " --qpstep <integer> Set max QP step\n"
120 " --rcsens <integer> RC sensitivity\n"
121 " --rcbuf <integer> Size of VBV buffer\n"
122 " --rcinitbuf <integer> Initial VBV buffer occupancy\n"
123 " --ipratio <float> QP factor between I and P\n"
124 " --pbratio <float> QP factor between P and B\n"
126 " -p, --pass <1|2> Enable 2 pass ratecontrol\n"
127 " --stats <string> Filename for 2 pass stats\n"
128 " --rceq <string> Ratecontrol equation\n"
129 " --qcomp <float> 0.0 => CBR, 1.0 => CQP, 0.6 => default\n"
130 " --cplxblur <float> reduce fluctuations in QP (before curve compression)\n"
131 " --qblur <float> reduce fluctuations in QP (after curve compression)\n"
134 " -A, --analyse <string> Analyse options:\n"
136 " - psub16x16,psub8x8\n"
138 " --subme <integer> Subpixel motion estimation quality\n"
140 " -s, --sar width:height Specify Sample Aspect Ratio\n"
141 " --frames <integer> Maximum number of frames to encode\n"
142 " -o, --output Specify output file\n"
144 " --no-asm Disable any CPU optims\n"
145 " --no-psnr Disable PSNR computaion\n"
146 " --quiet Quiet Mode\n"
152 /*****************************************************************************
154 *****************************************************************************/
155 static int Parse( int argc, char **argv,
157 FILE **p_fin, FILE **p_fout, int *pb_decompress )
159 char *psz_filename = NULL;
166 /* Parse command line options */
167 opterr = 0; // no error message
170 int long_options_index;
171 #define OPT_QPMIN 256
172 #define OPT_QPMAX 257
173 #define OPT_QPSTEP 258
174 #define OPT_RCSENS 259
175 #define OPT_IPRATIO 260
176 #define OPT_PBRATIO 261
177 #define OPT_RCBUF 262
178 #define OPT_RCIBUF 263
179 #define OPT_RCSTATS 264
181 #define OPT_QCOMP 266
182 #define OPT_NOPSNR 267
183 #define OPT_QUIET 268
184 #define OPT_SUBME 269
185 #define OPT_SCENECUT 270
186 #define OPT_QBLUR 271
187 #define OPT_CPLXBLUR 272
188 #define OPT_FRAMES 273
190 static struct option long_options[] =
192 { "help", no_argument, NULL, 'h' },
193 { "bitrate", required_argument, NULL, 'B' },
194 { "bframe", required_argument, NULL, 'b' },
195 { "iframe", required_argument, NULL, 'i' },
196 { "idrframe",required_argument, NULL, 'I' },
197 { "scenecut",required_argument, NULL, OPT_SCENECUT },
198 { "nf", no_argument, NULL, 'n' },
199 { "filter", required_argument, NULL, 'f' },
200 { "cabac", no_argument, NULL, 'c' },
201 { "qp", required_argument, NULL, 'q' },
202 { "qpmin", required_argument, NULL, OPT_QPMIN },
203 { "qpmax", required_argument, NULL, OPT_QPMAX },
204 { "qpstep", required_argument, NULL, OPT_QPSTEP },
205 { "ref", required_argument, NULL, 'r' },
206 { "no-asm", no_argument, NULL, 'C' },
207 { "sar", required_argument, NULL, 's' },
208 { "frames", required_argument, NULL, OPT_FRAMES },
209 { "output", required_argument, NULL, 'o' },
210 { "analyse", required_argument, NULL, 'A' },
211 { "subme", required_argument, NULL, OPT_SUBME },
212 { "rcsens", required_argument, NULL, OPT_RCSENS },
213 { "rcbuf", required_argument, NULL, OPT_RCBUF },
214 { "rcinitbuf",required_argument, NULL, OPT_RCIBUF },
215 { "ipratio", required_argument, NULL, OPT_IPRATIO },
216 { "pbratio", required_argument, NULL, OPT_PBRATIO },
217 { "pass", required_argument, NULL, 'p' },
218 { "stats", required_argument, NULL, OPT_RCSTATS },
219 { "rceq", required_argument, NULL, OPT_RCEQ },
220 { "qcomp", required_argument, NULL, OPT_QCOMP },
221 { "qblur", required_argument, NULL, OPT_QBLUR },
222 { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
223 { "no-psnr", no_argument, NULL, OPT_NOPSNR },
224 { "quiet", no_argument, NULL, OPT_QUIET },
230 c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:no:s:A:p:",
231 long_options, &long_options_index);
247 param->rc.i_bitrate = atol( optarg );
251 param->i_bframe = atol( optarg );
254 param->i_iframe = atol( optarg );
257 param->i_idrframe = atol( optarg );
260 param->i_scenecut_threshold = atol( optarg );
263 param->b_deblocking_filter = 0;
267 char *p = strchr( optarg, ':' );
270 param->i_deblocking_filter_alphac0 = atoi( optarg );
271 param->i_deblocking_filter_beta = atoi( p );
276 param->rc.i_qp_constant = atoi( optarg );
279 param->rc.i_qp_min = atoi( optarg );
282 param->rc.i_qp_max = atoi( optarg );
285 param->rc.i_qp_step = atoi( optarg );
288 param->i_frame_reference = atoi( optarg );
300 param->i_maxframes = atoi( optarg );
303 if( ( *p_fout = fopen( optarg, "wb" ) ) == NULL )
305 fprintf( stderr, "cannot open output file `%s'\n", optarg );
311 char *p = strchr( optarg, ':' );
314 param->vui.i_sar_width = atoi( optarg );
315 param->vui.i_sar_height = atoi( p + 1 );
320 param->analyse.inter = 0;
321 if( strstr( optarg, "none" ) ) param->analyse.inter = 0x000000;
322 if( strstr( optarg, "all" ) ) param->analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8;
324 if( strstr( optarg, "i4x4" ) ) param->analyse.inter |= X264_ANALYSE_I4x4;
325 if( strstr( optarg, "psub16x16" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
326 if( strstr( optarg, "psub8x8" ) ) param->analyse.inter |= X264_ANALYSE_PSUB8x8;
329 param->analyse.i_subpel_refine = atoi(optarg);
332 param->rc.i_rc_buffer_size = atoi(optarg);
335 param->rc.i_rc_init_buffer = atoi(optarg);
338 param->rc.i_rc_sens = atoi(optarg);
341 param->rc.f_ip_factor = atof(optarg);
344 param->rc.f_pb_factor = atof(optarg);
348 int i_pass = atoi(optarg);
350 param->rc.b_stat_write = 1;
351 else if( i_pass == 2 )
352 param->rc.b_stat_read = 1;
353 else if( i_pass > 2 ) /* XXX untested */
354 param->rc.b_stat_read =
355 param->rc.b_stat_write = 1;
359 param->rc.psz_stat_in = optarg;
360 param->rc.psz_stat_out = optarg;
363 param->rc.psz_rc_eq = optarg;
366 param->rc.f_qcompress = atof(optarg);
369 param->rc.f_qblur = atof(optarg);
372 param->rc.f_complexity_blur = atof(optarg);
375 param->analyse.b_psnr = 0;
378 param->i_log_level = X264_LOG_NONE;
381 fprintf( stderr, "unknown option (%c)\n", optopt );
386 /* Get the file name */
387 if( optind > argc - 1 )
392 psz_filename = argv[optind++];
394 if( !(*pb_decompress) )
396 char *psz_size = NULL;
400 if( optind > argc - 1 )
402 char *psz = psz_filename;
404 /* try to parse the file name */
407 while( *psz && ( *psz < '0' || *psz > '9' ) ) psz++;
408 x = strchr( psz, 'x' );
411 if( ( x[1] >= '0' && x[1] <= '9' ) )
417 if( psz_size == NULL )
422 fprintf( stderr, "x264: file name gives %dx%d\n", atoi(psz), atoi(x+1) );
426 psz_size = argv[optind++];
429 param->i_width = strtol( psz_size, &p, 0 );
430 param->i_height = strtol( p+1, &p, 0 );
434 if( !strcmp( psz_filename, "-" ) )
439 else if( ( *p_fin = fopen( psz_filename, "rb" ) ) == NULL )
441 fprintf( stderr, "could not open input file '%s'\n", psz_filename );
448 /*****************************************************************************
450 *****************************************************************************/
451 static int Decode( x264_param_t *param, FILE *fh26l, FILE *fout )
453 fprintf( stderr, "decompressor not working (help is welcome)\n" );
461 if( ( h = x264_decoder_open( ¶m ) ) == NULL )
463 fprintf( stderr, "x264_decoder_open failed\n" );
467 i_start = x264_mdate();
471 nal.p_payload = malloc( DATA_MAX );
475 uint8_t *p, *p_next, *end;
478 if( i_data < DATA_MAX && !b_eof )
480 int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );
498 /* extract one nal */
502 if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
511 fprintf( stderr, "garbage (i_data = %d)\n", i_data );
517 while( p_next < end - 3 )
519 if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
526 if( p_next == end - 3 && i_data < DATA_MAX )
531 /* decode this nal */
532 i_size = p_next - p - 3;
539 fprintf( stderr, "nal too large (FIXME) ?\n" );
544 x264_nal_decode( &nal, p +3, i_size );
546 /* decode the content of the nal */
547 x264_decoder_decode( h, &pic, &nal );
555 for( i = 0; i < pic->i_plane;i++ )
560 i_div = i==0 ? 1 : 2;
561 for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )
563 fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, fout );
568 memmove( &data[0], p_next, end - p_next );
569 i_data -= p_next - &data[0];
572 i_end = x264_mdate();
573 free( nal.p_payload );
574 fprintf( stderr, "\n" );
576 x264_decoder_close( h );
585 double fps = (double)i_frame * (double)1000000 /
586 (double)( i_end - i_start );
587 fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );
592 /*****************************************************************************
594 *****************************************************************************/
595 static int Encode( x264_param_t *param, FILE *fyuv, FILE *fout )
600 int i_frame, i_frame_total;
601 int64_t i_start, i_end;
605 if( !fseek( fyuv, 0, SEEK_END ) )
607 int64_t i_size = ftell( fyuv );
608 fseek( fyuv, 0, SEEK_SET );
609 i_frame_total = (int)(i_size / ( param->i_width * param->i_height * 3 / 2 ));
612 if( ( h = x264_encoder_open( param ) ) == NULL )
614 fprintf( stderr, "x264_encoder_open failed\n" );
618 /* Create a new pic */
619 x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
621 i_start = x264_mdate();
622 for( i_frame = 0, i_file = 0; i_ctrl_c == 0 ; i_frame++ )
629 if (param->i_maxframes!=0 && i_frame>=param->i_maxframes)
633 if( fread( pic.img.plane[0], 1, param->i_width * param->i_height, fyuv ) <= 0 ||
634 fread( pic.img.plane[1], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 ||
635 fread( pic.img.plane[2], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 )
640 /* Do not force any parameters */
641 pic.i_type = X264_TYPE_AUTO;
642 if( x264_encoder_encode( h, &nal, &i_nal, &pic ) < 0 )
644 fprintf( stderr, "x264_encoder_encode failed\n" );
647 for( i = 0; i < i_nal; i++ )
653 if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
655 i_file += fwrite( data, 1, i_size, fout );
657 else if( i_size < 0 )
660 "need to increase buffer size (size=%d)\n", -i_size );
664 i_end = x264_mdate();
665 x264_picture_clean( &pic );
666 x264_encoder_close( h );
667 fprintf( stderr, "\n" );
677 double fps = (double)i_frame * (double)1000000 /
678 (double)( i_end - i_start );
680 fprintf( stderr, "encoded %d frames %ffps %lld kb/s\n", i_frame, fps, i_file * 8 * 25 / i_frame / 1000 );