]> git.sesse.net Git - x264/blob - x264.c
option '--frames' limits number of frames to encode.
[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 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <math.h>
29
30 #include <signal.h>
31 #define _GNU_SOURCE
32 #include <getopt.h>
33
34 #ifdef _MSC_VER
35 #include <io.h>     /* _setmode() */
36 #include <fcntl.h>  /* _O_BINARY */
37 #endif
38
39 #include "common/common.h"
40 #include "x264.h"
41
42 #define DATA_MAX 3000000
43 uint8_t data[DATA_MAX];
44
45 /* Ctrl-C handler */
46 static int     i_ctrl_c = 0;
47 static void    SigIntHandler( int a )
48 {
49     i_ctrl_c = 1;
50 }
51
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 );
56
57 /****************************************************************************
58  * main:
59  ****************************************************************************/
60 int main( int argc, char **argv )
61 {
62     x264_param_t param;
63
64     FILE    *fout;
65     FILE    *fin;
66
67     int     b_decompress;
68     int     i_ret;
69
70 #ifdef _MSC_VER
71     _setmode(_fileno(stdin), _O_BINARY);    /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
72     _setmode(_fileno(stdout), _O_BINARY);
73 #endif
74
75     x264_param_default( &param );
76
77     /* Parse command line */
78     if( Parse( argc, argv, &param, &fin, &fout, &b_decompress ) < 0 )
79     {
80         return -1;
81     }
82
83     /* Control-C handler */
84     signal( SIGINT, SigIntHandler );
85
86     if( b_decompress )
87         i_ret = Decode( &param, fin, fout );
88     else
89         i_ret = Encode( &param, fin, fout );
90
91     return i_ret;
92 }
93
94 /*****************************************************************************
95  * Help:
96  *****************************************************************************/
97 static void Help( void )
98 {
99     fprintf( stderr,
100              "x264 build:0x%4.4x\n"
101              "Syntax: x264 [options] [-o out.h26l] in.yuv widthxheigh\n"
102              "\n"
103              "  -h, --help                  Print this help\n"
104              "\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"
109              "\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"
114              "\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"
125              "\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"
132              "\n"
133
134              "  -A, --analyse <string>      Analyse options:\n"
135              "                                  - i4x4\n"
136              "                                  - psub16x16,psub8x8\n"
137              "                                  - none, all\n"
138              "      --subme <integer>       Subpixel motion estimation quality\n"
139              "\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"
143              "\n"
144              "      --no-asm                Disable any CPU optims\n"
145              "      --no-psnr               Disable PSNR computaion\n"
146              "      --quiet                 Quiet Mode\n"
147              "\n",
148             X264_BUILD
149            );
150 }
151
152 /*****************************************************************************
153  * Parse:
154  *****************************************************************************/
155 static int  Parse( int argc, char **argv,
156                    x264_param_t  *param,
157                    FILE **p_fin, FILE **p_fout, int *pb_decompress )
158 {
159     char *psz_filename = NULL;
160
161     /* Default output */
162     *p_fout = stdout;
163     *p_fin  = stdin;
164     *pb_decompress = 0;
165
166     /* Parse command line options */
167     opterr = 0; // no error message
168     for( ;; )
169     {
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
180 #define OPT_RCEQ 265
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
189
190         static struct option long_options[] =
191         {
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 },
225             {0, 0, 0, 0}
226         };
227
228         int c;
229
230         c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:no:s:A:p:",
231                          long_options, &long_options_index);
232
233         if( c == -1 )
234         {
235             break;
236         }
237
238         switch( c )
239         {
240             case 'h':
241                 Help();
242                 return -1;
243
244             case 0:
245                 break;
246             case 'B':
247                 param->rc.i_bitrate = atol( optarg );
248                 param->rc.b_cbr = 1;
249                 break;
250             case 'b':
251                 param->i_bframe = atol( optarg );
252                 break;
253             case 'i':
254                 param->i_iframe = atol( optarg );
255                 break;
256             case 'I':
257                 param->i_idrframe = atol( optarg );
258                 break;
259             case OPT_SCENECUT:
260                 param->i_scenecut_threshold = atol( optarg );
261                 break;
262             case 'n':
263                 param->b_deblocking_filter = 0;
264                 break;
265             case 'f':
266             {
267                 char *p = strchr( optarg, ':' );
268                 if( p )
269                 {
270                     param->i_deblocking_filter_alphac0 = atoi( optarg );
271                     param->i_deblocking_filter_beta = atoi( p );
272                 }
273                 break;
274             }
275             case 'q':
276                 param->rc.i_qp_constant = atoi( optarg );
277                 break;
278             case OPT_QPMIN:
279                 param->rc.i_qp_min = atoi( optarg );
280                 break;
281             case OPT_QPMAX:
282                 param->rc.i_qp_max = atoi( optarg );
283                 break;
284             case OPT_QPSTEP:
285                 param->rc.i_qp_step = atoi( optarg );
286                 break;
287             case 'r':
288                 param->i_frame_reference = atoi( optarg );
289                 break;
290             case 'c':
291                 param->b_cabac = 1;
292                 break;
293             case 'x':
294                 *pb_decompress = 1;
295                 break;
296             case 'C':
297                 param->cpu = 0;
298                 break;
299             case OPT_FRAMES:
300                 param->i_maxframes = atoi( optarg );
301                 break;
302             case'o':
303                 if( ( *p_fout = fopen( optarg, "wb" ) ) == NULL )
304                 {
305                     fprintf( stderr, "cannot open output file `%s'\n", optarg );
306                     return -1;
307                 }
308                 break;
309             case 's':
310             {
311                 char *p = strchr( optarg, ':' );
312                 if( p )
313                 {
314                     param->vui.i_sar_width = atoi( optarg );
315                     param->vui.i_sar_height = atoi( p + 1 );
316                 }
317                 break;
318             }
319             case 'A':
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;
323
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;
327                 break;
328             case OPT_SUBME:
329                 param->analyse.i_subpel_refine = atoi(optarg);
330                 break;
331             case OPT_RCBUF:
332                 param->rc.i_rc_buffer_size = atoi(optarg);
333                 break;
334             case OPT_RCIBUF:
335                 param->rc.i_rc_init_buffer = atoi(optarg);
336                 break;
337             case OPT_RCSENS:
338                 param->rc.i_rc_sens = atoi(optarg);
339                 break;
340             case OPT_IPRATIO:
341                 param->rc.f_ip_factor = atof(optarg);
342                 break;
343             case OPT_PBRATIO:
344                 param->rc.f_pb_factor = atof(optarg);
345                 break;
346             case 'p':
347             {
348                 int i_pass = atoi(optarg);
349                 if( i_pass == 1 )
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;
356                 break;
357             }
358             case OPT_RCSTATS:
359                 param->rc.psz_stat_in = optarg;
360                 param->rc.psz_stat_out = optarg;
361                 break;
362             case OPT_RCEQ:
363                 param->rc.psz_rc_eq = optarg;
364                break;
365             case OPT_QCOMP:
366                 param->rc.f_qcompress = atof(optarg);
367                 break;
368             case OPT_QBLUR:
369                 param->rc.f_qblur = atof(optarg);
370                 break;
371             case OPT_CPLXBLUR:
372                 param->rc.f_complexity_blur = atof(optarg);
373                 break;
374             case OPT_NOPSNR:
375                 param->analyse.b_psnr = 0;
376                 break;
377             case OPT_QUIET:
378                 param->i_log_level = X264_LOG_NONE;
379                 break;
380             default:
381                 fprintf( stderr, "unknown option (%c)\n", optopt );
382                 return -1;
383         }
384     }
385
386     /* Get the file name */
387     if( optind > argc - 1 )
388     {
389         Help();
390         return -1;
391     }
392     psz_filename = argv[optind++];
393
394     if( !(*pb_decompress) )
395     {
396         char *psz_size = NULL;
397         char *p;
398
399
400         if( optind > argc - 1 )
401         {
402             char *psz = psz_filename;
403             char *x = NULL;
404             /* try to parse the file name */
405             while( *psz )
406             {
407                 while( *psz && ( *psz < '0' || *psz > '9' ) ) psz++;
408                 x = strchr( psz, 'x' );
409                 if( !x )
410                     break;
411                 if( ( x[1] >= '0' && x[1] <= '9' ) )
412                 {
413                     psz_size = psz;
414                     break;
415                 }
416             }
417             if( psz_size == NULL )
418             {
419                 Help();
420                 return -1;
421             }
422             fprintf( stderr, "x264: file name gives %dx%d\n", atoi(psz), atoi(x+1) );
423         }
424         else
425         {
426             psz_size = argv[optind++];
427         }
428
429         param->i_width           = strtol( psz_size, &p, 0 );
430         param->i_height          = strtol( p+1, &p, 0 );
431     }
432
433     /* open the input */
434     if( !strcmp( psz_filename, "-" ) )
435     {
436         *p_fin = stdin;
437         optind++;
438     }
439     else if( ( *p_fin = fopen( psz_filename, "rb" ) ) == NULL )
440     {
441         fprintf( stderr, "could not open input file '%s'\n", psz_filename );
442         return -1;
443     }
444
445     return 0;
446 }
447
448 /*****************************************************************************
449  * Decode:
450  *****************************************************************************/
451 static int  Decode( x264_param_t  *param, FILE *fh26l, FILE *fout )
452 {
453     fprintf( stderr, "decompressor not working (help is welcome)\n" );
454     return -1;
455 #if 0
456     x264_nal_t nal;
457     int i_data;
458     int b_eof;
459
460     //param.cpu = 0;
461     if( ( h = x264_decoder_open( &param ) ) == NULL )
462     {
463         fprintf( stderr, "x264_decoder_open failed\n" );
464         return -1;
465     }
466
467     i_start = x264_mdate();
468     b_eof = 0;
469     i_frame = 0;
470     i_data  = 0;
471     nal.p_payload = malloc( DATA_MAX );
472
473     while( !i_ctrl_c )
474     {
475         uint8_t *p, *p_next, *end;
476         int i_size;
477         /* fill buffer */
478         if( i_data < DATA_MAX && !b_eof )
479         {
480             int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );
481             if( i_read <= 0 )
482             {
483                 b_eof = 1;
484             }
485             else
486             {
487                 i_data += i_read;
488             }
489         }
490
491         if( i_data < 3 )
492         {
493             break;
494         }
495
496         end = &data[i_data];
497
498         /* extract one nal */
499         p = &data[0];
500         while( p < end - 3 )
501         {
502             if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
503             {
504                 break;
505             }
506             p++;
507         }
508
509         if( p >= end - 3 )
510         {
511             fprintf( stderr, "garbage (i_data = %d)\n", i_data );
512             i_data = 0;
513             continue;
514         }
515
516         p_next = p + 3;
517         while( p_next < end - 3 )
518         {
519             if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
520             {
521                 break;
522             }
523             p_next++;
524         }
525
526         if( p_next == end - 3 && i_data < DATA_MAX )
527         {
528             p_next = end;
529         }
530
531         /* decode this nal */
532         i_size = p_next - p - 3;
533         if( i_size <= 0 )
534         {
535             if( b_eof )
536             {
537                 break;
538             }
539             fprintf( stderr, "nal too large (FIXME) ?\n" );
540             i_data = 0;
541             continue;
542         }
543
544         x264_nal_decode( &nal, p +3, i_size );
545
546         /* decode the content of the nal */
547         x264_decoder_decode( h, &pic, &nal );
548
549         if( pic != NULL )
550         {
551             int i;
552
553             i_frame++;
554
555             for( i = 0; i < pic->i_plane;i++ )
556             {
557                 int i_line;
558                 int i_div;
559
560                 i_div = i==0 ? 1 : 2;
561                 for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )
562                 {
563                     fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, fout );
564                 }
565             }
566         }
567
568         memmove( &data[0], p_next, end - p_next );
569         i_data -= p_next - &data[0];
570     }
571
572     i_end = x264_mdate();
573     free( nal.p_payload );
574     fprintf( stderr, "\n" );
575
576     x264_decoder_close( h );
577
578     fclose( fh26l );
579     if( fout != stdout )
580     {
581         fclose( fout );
582     }
583     if( i_frame > 0 )
584     {
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 );
588     }
589 #endif
590 }
591
592 /*****************************************************************************
593  * Encode:
594  *****************************************************************************/
595 static int  Encode( x264_param_t  *param, FILE *fyuv, FILE *fout )
596 {
597     x264_t *h;
598     x264_picture_t pic;
599
600     int     i_frame, i_frame_total;
601     int64_t i_start, i_end;
602     int64_t i_file;
603
604     i_frame_total = 0;
605     if( !fseek( fyuv, 0, SEEK_END ) )
606     {
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 ));
610     }
611
612     if( ( h = x264_encoder_open( param ) ) == NULL )
613     {
614         fprintf( stderr, "x264_encoder_open failed\n" );
615         return -1;
616     }
617
618     /* Create a new pic */
619     x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
620
621     i_start = x264_mdate();
622     for( i_frame = 0, i_file = 0; i_ctrl_c == 0 ; i_frame++ )
623     {
624         int         i_nal;
625         x264_nal_t  *nal;
626
627         int         i;
628
629         if (param->i_maxframes!=0 && i_frame>=param->i_maxframes)
630             break;
631
632         /* read a frame */
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 )
636         {
637             break;
638         }
639
640         /* Do not force any parameters */
641         pic.i_type = X264_TYPE_AUTO;
642         if( x264_encoder_encode( h, &nal, &i_nal, &pic ) < 0 )
643         {
644             fprintf( stderr, "x264_encoder_encode failed\n" );
645         }
646
647         for( i = 0; i < i_nal; i++ )
648         {
649             int i_size;
650             int i_data;
651
652             i_data = DATA_MAX;
653             if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
654             {
655                 i_file += fwrite( data, 1, i_size, fout );
656             }
657             else if( i_size < 0 )
658             {
659                 fprintf( stderr,
660                          "need to increase buffer size (size=%d)\n", -i_size );
661             }
662         }
663     }
664     i_end = x264_mdate();
665     x264_picture_clean( &pic );
666     x264_encoder_close( h );
667     fprintf( stderr, "\n" );
668
669     fclose( fyuv );
670     if( fout != stdout )
671     {
672         fclose( fout );
673     }
674
675     if( i_frame > 0 )
676     {
677         double fps = (double)i_frame * (double)1000000 /
678                      (double)( i_end - i_start );
679
680         fprintf( stderr, "encoded %d frames %ffps %lld kb/s\n", i_frame, fps, i_file * 8 * 25 / i_frame / 1000 );
681     }
682
683     return 0;
684 }
685
686