]> git.sesse.net Git - x264/blob - x264.c
fix: "x264 -A all" didn't include b8x8 types.
[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( 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 );
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( x264_param_t *defaults )
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 [%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"
109              "\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"
114              "\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"
125              "\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"
132              "\n"
133
134              "  -A, --analyse <string>      Analyse options: [\"i4x4|psub16x16|bsub16x16\"]\n"
135              "                                  - i4x4\n"
136              "                                  - psub16x16, psub8x8, bsub16x16\n"
137              "                                  - none, all\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"
141              "\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"
146              "\n"
147              "      --no-asm                Disable any CPU optims\n"
148              "      --no-psnr               Disable PSNR computaion\n"
149              "      --quiet                 Quiet Mode\n"
150              "  -v, --verbose               Print stats for each frame\n"
151              "\n",
152             X264_BUILD,
153             defaults->i_idrframe,
154             defaults->i_iframe,
155             defaults->i_scenecut_threshold,
156             defaults->i_bframe,
157             defaults->i_frame_reference,
158             defaults->i_deblocking_filter_alphac0,
159             defaults->rc.i_qp_constant,
160             defaults->rc.i_qp_min,
161             defaults->rc.i_qp_max,
162             defaults->rc.i_qp_step,
163             defaults->rc.i_rc_sens,
164             defaults->rc.i_rc_buffer_size,
165             defaults->rc.i_rc_init_buffer,
166             defaults->rc.f_ip_factor,
167             defaults->rc.f_pb_factor,
168             defaults->rc.psz_stat_out,
169             defaults->rc.psz_rc_eq,
170             defaults->rc.f_qcompress,
171             defaults->rc.f_complexity_blur,
172             defaults->rc.f_qblur,
173             defaults->analyse.i_subpel_refine
174            );
175 }
176
177 /*****************************************************************************
178  * Parse:
179  *****************************************************************************/
180 static int  Parse( int argc, char **argv,
181                    x264_param_t  *param,
182                    FILE **p_fin, FILE **p_fout, int *pb_decompress )
183 {
184     char *psz_filename = NULL;
185     x264_param_t defaults = *param;
186
187     /* Default output */
188     *p_fout = stdout;
189     *p_fin  = stdin;
190     *pb_decompress = 0;
191
192     /* Parse command line options */
193     opterr = 0; // no error message
194     for( ;; )
195     {
196         int long_options_index;
197 #define OPT_QPMIN 256
198 #define OPT_QPMAX 257
199 #define OPT_QPSTEP 258
200 #define OPT_RCSENS 259
201 #define OPT_IPRATIO 260
202 #define OPT_PBRATIO 261
203 #define OPT_RCBUF 262
204 #define OPT_RCIBUF 263
205 #define OPT_RCSTATS 264
206 #define OPT_RCEQ 265
207 #define OPT_QCOMP 266
208 #define OPT_NOPSNR 267
209 #define OPT_QUIET 268
210 #define OPT_SCENECUT 270
211 #define OPT_QBLUR 271
212 #define OPT_CPLXBLUR 272
213 #define OPT_FRAMES 273
214 #define OPT_FPS 274
215 #define OPT_DIRECT 275
216
217         static struct option long_options[] =
218         {
219             { "help",    no_argument,       NULL, 'h' },
220             { "bitrate", required_argument, NULL, 'B' },
221             { "bframe",  required_argument, NULL, 'b' },
222             { "iframe",  required_argument, NULL, 'i' },
223             { "idrframe",required_argument, NULL, 'I' },
224             { "scenecut",required_argument, NULL, OPT_SCENECUT },
225             { "nf",      no_argument,       NULL, 'n' },
226             { "filter",  required_argument, NULL, 'f' },
227             { "cabac",   no_argument,       NULL, 'c' },
228             { "qp",      required_argument, NULL, 'q' },
229             { "qpmin",   required_argument, NULL, OPT_QPMIN },
230             { "qpmax",   required_argument, NULL, OPT_QPMAX },
231             { "qpstep",  required_argument, NULL, OPT_QPSTEP },
232             { "ref",     required_argument, NULL, 'r' },
233             { "no-asm",  no_argument,       NULL, 'C' },
234             { "sar",     required_argument, NULL, 's' },
235             { "fps",     required_argument, NULL, OPT_FPS },
236             { "frames",  required_argument, NULL, OPT_FRAMES },
237             { "output",  required_argument, NULL, 'o' },
238             { "analyse", required_argument, NULL, 'A' },
239             { "direct",  required_argument, NULL, OPT_DIRECT },
240             { "subme",   required_argument, NULL, 'm' },
241             { "rcsens",  required_argument, NULL, OPT_RCSENS },
242             { "rcbuf",   required_argument, NULL, OPT_RCBUF },
243             { "rcinitbuf",required_argument,NULL, OPT_RCIBUF },
244             { "ipratio", required_argument, NULL, OPT_IPRATIO },
245             { "pbratio", required_argument, NULL, OPT_PBRATIO },
246             { "pass",    required_argument, NULL, 'p' },
247             { "stats",   required_argument, NULL, OPT_RCSTATS },
248             { "rceq",    required_argument, NULL, OPT_RCEQ },
249             { "qcomp",   required_argument, NULL, OPT_QCOMP },
250             { "qblur",   required_argument, NULL, OPT_QBLUR },
251             { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
252             { "no-psnr", no_argument,       NULL, OPT_NOPSNR },
253             { "quiet",   no_argument,       NULL, OPT_QUIET },
254             { "verbose", no_argument,       NULL, 'v' },
255             {0, 0, 0, 0}
256         };
257
258         int c;
259
260         c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:nf:o:s:A:m:p:v",
261                          long_options, &long_options_index);
262
263         if( c == -1 )
264         {
265             break;
266         }
267
268         switch( c )
269         {
270             case 'h':
271                 Help( &defaults );
272                 return -1;
273
274             case 0:
275                 break;
276             case 'B':
277                 param->rc.i_bitrate = atol( optarg );
278                 param->rc.b_cbr = 1;
279                 break;
280             case 'b':
281                 param->i_bframe = atol( optarg );
282                 break;
283             case 'i':
284                 param->i_iframe = atol( optarg );
285                 break;
286             case 'I':
287                 param->i_idrframe = atol( optarg );
288                 break;
289             case OPT_SCENECUT:
290                 param->i_scenecut_threshold = atol( optarg );
291                 break;
292             case 'n':
293                 param->b_deblocking_filter = 0;
294                 break;
295             case 'f':
296             {
297                 char *p = strchr( optarg, ':' );
298                 if( p )
299                 {
300                     param->i_deblocking_filter_alphac0 = atoi( optarg );
301                     param->i_deblocking_filter_beta = atoi( p );
302                 }
303                 break;
304             }
305             case 'q':
306                 param->rc.i_qp_constant = atoi( optarg );
307                 break;
308             case OPT_QPMIN:
309                 param->rc.i_qp_min = atoi( optarg );
310                 break;
311             case OPT_QPMAX:
312                 param->rc.i_qp_max = atoi( optarg );
313                 break;
314             case OPT_QPSTEP:
315                 param->rc.i_qp_step = atoi( optarg );
316                 break;
317             case 'r':
318                 param->i_frame_reference = atoi( optarg );
319                 break;
320             case 'c':
321                 param->b_cabac = 1;
322                 break;
323             case 'x':
324                 *pb_decompress = 1;
325                 break;
326             case 'C':
327                 param->cpu = 0;
328                 break;
329             case OPT_FRAMES:
330                 param->i_maxframes = atoi( optarg );
331                 break;
332             case'o':
333                 if( ( *p_fout = fopen( optarg, "wb" ) ) == NULL )
334                 {
335                     fprintf( stderr, "cannot open output file `%s'\n", optarg );
336                     return -1;
337                 }
338                 break;
339             case 's':
340             {
341                 char *p = strchr( optarg, ':' );
342                 if( p )
343                 {
344                     param->vui.i_sar_width = atoi( optarg );
345                     param->vui.i_sar_height = atoi( p + 1 );
346                 }
347                 break;
348             }
349             case OPT_FPS:
350             {
351                 float fps;
352                 if( sscanf( optarg, "%d/%d", &param->i_fps_num, &param->i_fps_den ) == 2 )
353                     ;
354                 else if( sscanf( optarg, "%f", &fps ) )
355                 {
356                     param->i_fps_num = (int)(fps * 1000 + .5);
357                     param->i_fps_den = 1000;
358                 }
359                 else
360                 {
361                     fprintf( stderr, "bad fps `%s'\n", optarg );
362                     return -1;
363                 }
364             }
365             case 'A':
366                 param->analyse.inter = 0;
367                 if( strstr( optarg, "none" ) )  param->analyse.inter = 0x000000;
368                 if( strstr( optarg, "all" ) )   param->analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16;
369
370                 if( strstr( optarg, "i4x4" ) )      param->analyse.inter |= X264_ANALYSE_I4x4;
371                 if( strstr( optarg, "psub16x16" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
372                 if( strstr( optarg, "psub8x8" ) )   param->analyse.inter |= X264_ANALYSE_PSUB8x8;
373                 if( strstr( optarg, "bsub16x16" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16;
374                 break;
375             case OPT_DIRECT:
376                 if( strstr( optarg, "temporal" ) )
377                     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
378                 else if( strstr( optarg, "spatial" ) )
379                     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
380                 else if( strstr( optarg, "none" ) )
381                     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
382                 else
383                     param->analyse.i_direct_mv_pred = atoi( optarg );
384                 break;
385             case 'm':
386                 param->analyse.i_subpel_refine = atoi(optarg);
387                 break;
388             case OPT_RCBUF:
389                 param->rc.i_rc_buffer_size = atoi(optarg);
390                 break;
391             case OPT_RCIBUF:
392                 param->rc.i_rc_init_buffer = atoi(optarg);
393                 break;
394             case OPT_RCSENS:
395                 param->rc.i_rc_sens = atoi(optarg);
396                 break;
397             case OPT_IPRATIO:
398                 param->rc.f_ip_factor = atof(optarg);
399                 break;
400             case OPT_PBRATIO:
401                 param->rc.f_pb_factor = atof(optarg);
402                 break;
403             case 'p':
404             {
405                 int i_pass = atoi(optarg);
406                 if( i_pass == 1 )
407                     param->rc.b_stat_write = 1;
408                 else if( i_pass == 2 )
409                     param->rc.b_stat_read = 1;
410                 else if( i_pass > 2 )
411                     param->rc.b_stat_read =
412                     param->rc.b_stat_write = 1;
413                 break;
414             }
415             case OPT_RCSTATS:
416                 param->rc.psz_stat_in = optarg;
417                 param->rc.psz_stat_out = optarg;
418                 break;
419             case OPT_RCEQ:
420                 param->rc.psz_rc_eq = optarg;
421                break;
422             case OPT_QCOMP:
423                 param->rc.f_qcompress = atof(optarg);
424                 break;
425             case OPT_QBLUR:
426                 param->rc.f_qblur = atof(optarg);
427                 break;
428             case OPT_CPLXBLUR:
429                 param->rc.f_complexity_blur = atof(optarg);
430                 break;
431             case OPT_NOPSNR:
432                 param->analyse.b_psnr = 0;
433                 break;
434             case OPT_QUIET:
435                 param->i_log_level = X264_LOG_NONE;
436                 break;
437             case 'v':
438                 param->i_log_level = X264_LOG_DEBUG;
439                 break;
440             default:
441                 fprintf( stderr, "unknown option (%c)\n", optopt );
442                 return -1;
443         }
444     }
445
446     /* Get the file name */
447     if( optind > argc - 1 )
448     {
449         Help( &defaults );
450         return -1;
451     }
452     psz_filename = argv[optind++];
453
454     if( !(*pb_decompress) )
455     {
456         char *psz_size = NULL;
457         char *p;
458
459
460         if( optind > argc - 1 )
461         {
462             char *psz = psz_filename;
463             char *x = NULL;
464             /* try to parse the file name */
465             while( *psz )
466             {
467                 while( *psz && ( *psz < '0' || *psz > '9' ) ) psz++;
468                 x = strchr( psz, 'x' );
469                 if( !x )
470                     break;
471                 if( ( x[1] >= '0' && x[1] <= '9' ) )
472                 {
473                     psz_size = psz;
474                     break;
475                 }
476             }
477             if( psz_size == NULL )
478             {
479                 Help( &defaults );
480                 return -1;
481             }
482             fprintf( stderr, "x264: file name gives %dx%d\n", atoi(psz), atoi(x+1) );
483         }
484         else
485         {
486             psz_size = argv[optind++];
487         }
488
489         param->i_width           = strtol( psz_size, &p, 0 );
490         param->i_height          = strtol( p+1, &p, 0 );
491     }
492
493     /* open the input */
494     if( !strcmp( psz_filename, "-" ) )
495     {
496         *p_fin = stdin;
497         optind++;
498     }
499     else if( ( *p_fin = fopen( psz_filename, "rb" ) ) == NULL )
500     {
501         fprintf( stderr, "could not open input file '%s'\n", psz_filename );
502         return -1;
503     }
504
505     return 0;
506 }
507
508 /*****************************************************************************
509  * Decode:
510  *****************************************************************************/
511 static int  Decode( x264_param_t  *param, FILE *fh26l, FILE *fout )
512 {
513     fprintf( stderr, "decompressor not working (help is welcome)\n" );
514     return -1;
515 #if 0
516     x264_nal_t nal;
517     int i_data;
518     int b_eof;
519
520     //param.cpu = 0;
521     if( ( h = x264_decoder_open( &param ) ) == NULL )
522     {
523         fprintf( stderr, "x264_decoder_open failed\n" );
524         return -1;
525     }
526
527     i_start = x264_mdate();
528     b_eof = 0;
529     i_frame = 0;
530     i_data  = 0;
531     nal.p_payload = malloc( DATA_MAX );
532
533     while( !i_ctrl_c )
534     {
535         uint8_t *p, *p_next, *end;
536         int i_size;
537         /* fill buffer */
538         if( i_data < DATA_MAX && !b_eof )
539         {
540             int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );
541             if( i_read <= 0 )
542             {
543                 b_eof = 1;
544             }
545             else
546             {
547                 i_data += i_read;
548             }
549         }
550
551         if( i_data < 3 )
552         {
553             break;
554         }
555
556         end = &data[i_data];
557
558         /* extract one nal */
559         p = &data[0];
560         while( p < end - 3 )
561         {
562             if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
563             {
564                 break;
565             }
566             p++;
567         }
568
569         if( p >= end - 3 )
570         {
571             fprintf( stderr, "garbage (i_data = %d)\n", i_data );
572             i_data = 0;
573             continue;
574         }
575
576         p_next = p + 3;
577         while( p_next < end - 3 )
578         {
579             if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
580             {
581                 break;
582             }
583             p_next++;
584         }
585
586         if( p_next == end - 3 && i_data < DATA_MAX )
587         {
588             p_next = end;
589         }
590
591         /* decode this nal */
592         i_size = p_next - p - 3;
593         if( i_size <= 0 )
594         {
595             if( b_eof )
596             {
597                 break;
598             }
599             fprintf( stderr, "nal too large (FIXME) ?\n" );
600             i_data = 0;
601             continue;
602         }
603
604         x264_nal_decode( &nal, p +3, i_size );
605
606         /* decode the content of the nal */
607         x264_decoder_decode( h, &pic, &nal );
608
609         if( pic != NULL )
610         {
611             int i;
612
613             i_frame++;
614
615             for( i = 0; i < pic->i_plane;i++ )
616             {
617                 int i_line;
618                 int i_div;
619
620                 i_div = i==0 ? 1 : 2;
621                 for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )
622                 {
623                     fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, fout );
624                 }
625             }
626         }
627
628         memmove( &data[0], p_next, end - p_next );
629         i_data -= p_next - &data[0];
630     }
631
632     i_end = x264_mdate();
633     free( nal.p_payload );
634     fprintf( stderr, "\n" );
635
636     x264_decoder_close( h );
637
638     fclose( fh26l );
639     if( fout != stdout )
640     {
641         fclose( fout );
642     }
643     if( i_frame > 0 )
644     {
645         double fps = (double)i_frame * (double)1000000 /
646                      (double)( i_end - i_start );
647         fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );
648     }
649 #endif
650 }
651
652 /*****************************************************************************
653  * Encode:
654  *****************************************************************************/
655 static int  Encode( x264_param_t  *param, FILE *fyuv, FILE *fout )
656 {
657     x264_t *h;
658     x264_picture_t pic;
659
660     int     i_frame, i_frame_total;
661     int64_t i_start, i_end;
662     int64_t i_file;
663
664     i_frame_total = 0;
665     if( !fseek( fyuv, 0, SEEK_END ) )
666     {
667         int64_t i_size = ftell( fyuv );
668         fseek( fyuv, 0, SEEK_SET );
669         i_frame_total = (int)(i_size / ( param->i_width * param->i_height * 3 / 2 ));
670     }
671
672     if( ( h = x264_encoder_open( param ) ) == NULL )
673     {
674         fprintf( stderr, "x264_encoder_open failed\n" );
675         return -1;
676     }
677
678     /* Create a new pic */
679     x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
680
681     i_start = x264_mdate();
682     for( i_frame = 0, i_file = 0; i_ctrl_c == 0 ; i_frame++ )
683     {
684         int         i_nal;
685         x264_nal_t  *nal;
686
687         int         i;
688
689         if (param->i_maxframes!=0 && i_frame>=param->i_maxframes)
690             break;
691
692         /* read a frame */
693         if( fread( pic.img.plane[0], 1, param->i_width * param->i_height, fyuv ) <= 0 ||
694             fread( pic.img.plane[1], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 ||
695             fread( pic.img.plane[2], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 )
696         {
697             break;
698         }
699
700         /* Do not force any parameters */
701         pic.i_type = X264_TYPE_AUTO;
702         if( x264_encoder_encode( h, &nal, &i_nal, &pic ) < 0 )
703         {
704             fprintf( stderr, "x264_encoder_encode failed\n" );
705         }
706
707         for( i = 0; i < i_nal; i++ )
708         {
709             int i_size;
710             int i_data;
711
712             i_data = DATA_MAX;
713             if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
714             {
715                 i_file += fwrite( data, 1, i_size, fout );
716             }
717             else if( i_size < 0 )
718             {
719                 fprintf( stderr,
720                          "need to increase buffer size (size=%d)\n", -i_size );
721             }
722         }
723     }
724     i_end = x264_mdate();
725     x264_picture_clean( &pic );
726     x264_encoder_close( h );
727     fprintf( stderr, "\n" );
728
729     fclose( fyuv );
730     if( fout != stdout )
731     {
732         fclose( fout );
733     }
734
735     if( i_frame > 0 )
736     {
737         double fps = (double)i_frame * (double)1000000 /
738                      (double)( i_end - i_start );
739
740         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 ) );
741     }
742
743     return 0;
744 }
745
746