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( x264_param_t *defaults );
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( x264_param_t *defaults )
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 [%d]\n"
106 " -i, --iframe <integer> Max interval between I frames [%d]\n"
107 " --scenecut <integer> How aggresively to insert extra I frames [%d]\n"
108 " -b, --bframe <integer> Number of B-frames between I and P [%d]\n"
110 " -c, --cabac Enable CABAC\n"
111 " -r, --ref <integer> Number of reference frames [%d]\n"
112 " -n, --nf Disable loop filter\n"
113 " -f, --filter <alpha:beta> Loop filter AplhaCO and Beta parameters [%d]\n"
115 " -q, --qp <integer> Set QP [%d]\n"
116 " -B, --bitrate <integer> Set bitrate\n"
117 " --qpmin <integer> Set min QP [%d]\n"
118 " --qpmax <integer> Set max QP [%d]\n"
119 " --qpstep <integer> Set max QP step [%d]\n"
120 " --rcsens <integer> CBR ratecontrol sensitivity [%d]\n"
121 " --rcbuf <integer> Size of VBV buffer [%d]\n"
122 " --rcinitbuf <integer> Initial VBV buffer occupancy [%d]\n"
123 " --ipratio <float> QP factor between I and P [%.2f]\n"
124 " --pbratio <float> QP factor between P and B [%.2f]\n"
126 " -p, --pass <1|2> Enable 2 pass ratecontrol\n"
127 " --stats <string> Filename for 2 pass stats [\"%s\"]\n"
128 " --rceq <string> Ratecontrol equation [\"%s\"]\n"
129 " --qcomp <float> 0.0 => CBR, 1.0 => CQP [%.2f]\n"
130 " --cplxblur <float> reduce fluctuations in QP (before curve compression) [%.1f]\n"
131 " --qblur <float> reduce fluctuations in QP (after curve compression) [%.1f]\n"
134 " -A, --analyse <string> Analyse options: [\"i4x4|psub16x16|bsub16x16\"]\n"
136 " - psub16x16, psub8x8, bsub16x16\n"
138 " --direct <string> Direct MV prediction mode [\"temporal\"]\n"
139 " - none, spatial, temporal\n"
140 " -m, --subme <integer> Subpixel motion estimation quality: 1=fast, 5=best. [%d]\n"
142 " -s, --sar width:height Specify Sample Aspect Ratio\n"
143 " --fps <float|rational> Specify framerate\n"
144 " --frames <integer> Maximum number of frames to encode\n"
145 " -o, --output Specify output file\n"
147 " --no-asm Disable any CPU optims\n"
148 " --no-psnr Disable PSNR computaion\n"
149 " --quiet Quiet Mode\n"
152 defaults->i_idrframe,
154 defaults->i_scenecut_threshold,
156 defaults->i_frame_reference,
157 defaults->i_deblocking_filter_alphac0,
158 defaults->rc.i_qp_constant,
159 defaults->rc.i_qp_min,
160 defaults->rc.i_qp_max,
161 defaults->rc.i_qp_step,
162 defaults->rc.i_rc_sens,
163 defaults->rc.i_rc_buffer_size,
164 defaults->rc.i_rc_init_buffer,
165 defaults->rc.f_ip_factor,
166 defaults->rc.f_pb_factor,
167 defaults->rc.psz_stat_out,
168 defaults->rc.psz_rc_eq,
169 defaults->rc.f_qcompress,
170 defaults->rc.f_complexity_blur,
171 defaults->rc.f_qblur,
172 defaults->analyse.i_subpel_refine
176 /*****************************************************************************
178 *****************************************************************************/
179 static int Parse( int argc, char **argv,
181 FILE **p_fin, FILE **p_fout, int *pb_decompress )
183 char *psz_filename = NULL;
184 x264_param_t defaults = *param;
191 /* Parse command line options */
192 opterr = 0; // no error message
195 int long_options_index;
196 #define OPT_QPMIN 256
197 #define OPT_QPMAX 257
198 #define OPT_QPSTEP 258
199 #define OPT_RCSENS 259
200 #define OPT_IPRATIO 260
201 #define OPT_PBRATIO 261
202 #define OPT_RCBUF 262
203 #define OPT_RCIBUF 263
204 #define OPT_RCSTATS 264
206 #define OPT_QCOMP 266
207 #define OPT_NOPSNR 267
208 #define OPT_QUIET 268
209 #define OPT_SCENECUT 270
210 #define OPT_QBLUR 271
211 #define OPT_CPLXBLUR 272
212 #define OPT_FRAMES 273
214 #define OPT_DIRECT 275
216 static struct option long_options[] =
218 { "help", no_argument, NULL, 'h' },
219 { "bitrate", required_argument, NULL, 'B' },
220 { "bframe", required_argument, NULL, 'b' },
221 { "iframe", required_argument, NULL, 'i' },
222 { "idrframe",required_argument, NULL, 'I' },
223 { "scenecut",required_argument, NULL, OPT_SCENECUT },
224 { "nf", no_argument, NULL, 'n' },
225 { "filter", required_argument, NULL, 'f' },
226 { "cabac", no_argument, NULL, 'c' },
227 { "qp", required_argument, NULL, 'q' },
228 { "qpmin", required_argument, NULL, OPT_QPMIN },
229 { "qpmax", required_argument, NULL, OPT_QPMAX },
230 { "qpstep", required_argument, NULL, OPT_QPSTEP },
231 { "ref", required_argument, NULL, 'r' },
232 { "no-asm", no_argument, NULL, 'C' },
233 { "sar", required_argument, NULL, 's' },
234 { "fps", required_argument, NULL, OPT_FPS },
235 { "frames", required_argument, NULL, OPT_FRAMES },
236 { "output", required_argument, NULL, 'o' },
237 { "analyse", required_argument, NULL, 'A' },
238 { "direct", required_argument, NULL, OPT_DIRECT },
239 { "subme", required_argument, NULL, 'm' },
240 { "rcsens", required_argument, NULL, OPT_RCSENS },
241 { "rcbuf", required_argument, NULL, OPT_RCBUF },
242 { "rcinitbuf",required_argument,NULL, OPT_RCIBUF },
243 { "ipratio", required_argument, NULL, OPT_IPRATIO },
244 { "pbratio", required_argument, NULL, OPT_PBRATIO },
245 { "pass", required_argument, NULL, 'p' },
246 { "stats", required_argument, NULL, OPT_RCSTATS },
247 { "rceq", required_argument, NULL, OPT_RCEQ },
248 { "qcomp", required_argument, NULL, OPT_QCOMP },
249 { "qblur", required_argument, NULL, OPT_QBLUR },
250 { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
251 { "no-psnr", no_argument, NULL, OPT_NOPSNR },
252 { "quiet", no_argument, NULL, OPT_QUIET },
258 c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:no:s:A:p:",
259 long_options, &long_options_index);
275 param->rc.i_bitrate = atol( optarg );
279 param->i_bframe = atol( optarg );
282 param->i_iframe = atol( optarg );
285 param->i_idrframe = atol( optarg );
288 param->i_scenecut_threshold = atol( optarg );
291 param->b_deblocking_filter = 0;
295 char *p = strchr( optarg, ':' );
298 param->i_deblocking_filter_alphac0 = atoi( optarg );
299 param->i_deblocking_filter_beta = atoi( p );
304 param->rc.i_qp_constant = atoi( optarg );
307 param->rc.i_qp_min = atoi( optarg );
310 param->rc.i_qp_max = atoi( optarg );
313 param->rc.i_qp_step = atoi( optarg );
316 param->i_frame_reference = atoi( optarg );
328 param->i_maxframes = atoi( optarg );
331 if( ( *p_fout = fopen( optarg, "wb" ) ) == NULL )
333 fprintf( stderr, "cannot open output file `%s'\n", optarg );
339 char *p = strchr( optarg, ':' );
342 param->vui.i_sar_width = atoi( optarg );
343 param->vui.i_sar_height = atoi( p + 1 );
350 if( sscanf( optarg, "%d/%d", ¶m->i_fps_num, ¶m->i_fps_den ) == 2 )
352 else if( sscanf( optarg, "%f", &fps ) )
354 param->i_fps_num = (int)(fps * 1000 + .5);
355 param->i_fps_den = 1000;
359 fprintf( stderr, "bad fps `%s'\n", optarg );
364 param->analyse.inter = 0;
365 if( strstr( optarg, "none" ) ) param->analyse.inter = 0x000000;
366 if( strstr( optarg, "all" ) ) param->analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8;
368 if( strstr( optarg, "i4x4" ) ) param->analyse.inter |= X264_ANALYSE_I4x4;
369 if( strstr( optarg, "psub16x16" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
370 if( strstr( optarg, "psub8x8" ) ) param->analyse.inter |= X264_ANALYSE_PSUB8x8;
371 if( strstr( optarg, "bsub16x16" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16;
374 if( strstr( optarg, "temporal" ) )
375 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
376 else if( strstr( optarg, "spatial" ) )
377 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
378 else if( strstr( optarg, "none" ) )
379 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
381 param->analyse.i_direct_mv_pred = atoi( optarg );
384 param->analyse.i_subpel_refine = atoi(optarg);
387 param->rc.i_rc_buffer_size = atoi(optarg);
390 param->rc.i_rc_init_buffer = atoi(optarg);
393 param->rc.i_rc_sens = atoi(optarg);
396 param->rc.f_ip_factor = atof(optarg);
399 param->rc.f_pb_factor = atof(optarg);
403 int i_pass = atoi(optarg);
405 param->rc.b_stat_write = 1;
406 else if( i_pass == 2 )
407 param->rc.b_stat_read = 1;
408 else if( i_pass > 2 ) /* XXX untested */
409 param->rc.b_stat_read =
410 param->rc.b_stat_write = 1;
414 param->rc.psz_stat_in = optarg;
415 param->rc.psz_stat_out = optarg;
418 param->rc.psz_rc_eq = optarg;
421 param->rc.f_qcompress = atof(optarg);
424 param->rc.f_qblur = atof(optarg);
427 param->rc.f_complexity_blur = atof(optarg);
430 param->analyse.b_psnr = 0;
433 param->i_log_level = X264_LOG_NONE;
436 fprintf( stderr, "unknown option (%c)\n", optopt );
441 /* Get the file name */
442 if( optind > argc - 1 )
447 psz_filename = argv[optind++];
449 if( !(*pb_decompress) )
451 char *psz_size = NULL;
455 if( optind > argc - 1 )
457 char *psz = psz_filename;
459 /* try to parse the file name */
462 while( *psz && ( *psz < '0' || *psz > '9' ) ) psz++;
463 x = strchr( psz, 'x' );
466 if( ( x[1] >= '0' && x[1] <= '9' ) )
472 if( psz_size == NULL )
477 fprintf( stderr, "x264: file name gives %dx%d\n", atoi(psz), atoi(x+1) );
481 psz_size = argv[optind++];
484 param->i_width = strtol( psz_size, &p, 0 );
485 param->i_height = strtol( p+1, &p, 0 );
489 if( !strcmp( psz_filename, "-" ) )
494 else if( ( *p_fin = fopen( psz_filename, "rb" ) ) == NULL )
496 fprintf( stderr, "could not open input file '%s'\n", psz_filename );
503 /*****************************************************************************
505 *****************************************************************************/
506 static int Decode( x264_param_t *param, FILE *fh26l, FILE *fout )
508 fprintf( stderr, "decompressor not working (help is welcome)\n" );
516 if( ( h = x264_decoder_open( ¶m ) ) == NULL )
518 fprintf( stderr, "x264_decoder_open failed\n" );
522 i_start = x264_mdate();
526 nal.p_payload = malloc( DATA_MAX );
530 uint8_t *p, *p_next, *end;
533 if( i_data < DATA_MAX && !b_eof )
535 int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );
553 /* extract one nal */
557 if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
566 fprintf( stderr, "garbage (i_data = %d)\n", i_data );
572 while( p_next < end - 3 )
574 if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
581 if( p_next == end - 3 && i_data < DATA_MAX )
586 /* decode this nal */
587 i_size = p_next - p - 3;
594 fprintf( stderr, "nal too large (FIXME) ?\n" );
599 x264_nal_decode( &nal, p +3, i_size );
601 /* decode the content of the nal */
602 x264_decoder_decode( h, &pic, &nal );
610 for( i = 0; i < pic->i_plane;i++ )
615 i_div = i==0 ? 1 : 2;
616 for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )
618 fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, fout );
623 memmove( &data[0], p_next, end - p_next );
624 i_data -= p_next - &data[0];
627 i_end = x264_mdate();
628 free( nal.p_payload );
629 fprintf( stderr, "\n" );
631 x264_decoder_close( h );
640 double fps = (double)i_frame * (double)1000000 /
641 (double)( i_end - i_start );
642 fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );
647 /*****************************************************************************
649 *****************************************************************************/
650 static int Encode( x264_param_t *param, FILE *fyuv, FILE *fout )
655 int i_frame, i_frame_total;
656 int64_t i_start, i_end;
660 if( !fseek( fyuv, 0, SEEK_END ) )
662 int64_t i_size = ftell( fyuv );
663 fseek( fyuv, 0, SEEK_SET );
664 i_frame_total = (int)(i_size / ( param->i_width * param->i_height * 3 / 2 ));
667 if( ( h = x264_encoder_open( param ) ) == NULL )
669 fprintf( stderr, "x264_encoder_open failed\n" );
673 /* Create a new pic */
674 x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
676 i_start = x264_mdate();
677 for( i_frame = 0, i_file = 0; i_ctrl_c == 0 ; i_frame++ )
684 if (param->i_maxframes!=0 && i_frame>=param->i_maxframes)
688 if( fread( pic.img.plane[0], 1, param->i_width * param->i_height, fyuv ) <= 0 ||
689 fread( pic.img.plane[1], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 ||
690 fread( pic.img.plane[2], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 )
695 /* Do not force any parameters */
696 pic.i_type = X264_TYPE_AUTO;
697 if( x264_encoder_encode( h, &nal, &i_nal, &pic ) < 0 )
699 fprintf( stderr, "x264_encoder_encode failed\n" );
702 for( i = 0; i < i_nal; i++ )
708 if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
710 i_file += fwrite( data, 1, i_size, fout );
712 else if( i_size < 0 )
715 "need to increase buffer size (size=%d)\n", -i_size );
719 i_end = x264_mdate();
720 x264_picture_clean( &pic );
721 x264_encoder_close( h );
722 fprintf( stderr, "\n" );
732 double fps = (double)i_frame * (double)1000000 /
733 (double)( i_end - i_start );
735 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps, (double) i_file * 8 * param->i_fps_num / ( param->i_fps_den * i_frame * 1000 ) );