]> git.sesse.net Git - x264/blob - x264.c
6268ecb2fb062eed99f4507aad04a216cd4eb635
[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 #ifdef AVIS_INPUT
40 #include <windows.h>
41 #include <vfw.h>
42 #endif
43
44 #ifdef MP4_OUTPUT
45 #include <gpac/m4_author.h>
46 #endif
47
48 #include "common/common.h"
49 #include "x264.h"
50 #ifndef _MSC_VER
51 #include "config.h"
52 #endif
53
54 #define DATA_MAX 3000000
55 uint8_t data[DATA_MAX];
56
57 typedef void *hnd_t;
58
59 /* Ctrl-C handler */
60 static int     i_ctrl_c = 0;
61 static void    SigIntHandler( int a )
62 {
63     i_ctrl_c = 1;
64 }
65
66 /* input file operation function pointers */
67 static int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
68 static int (*p_get_frame_total)( hnd_t handle, int i_width, int i_height );
69 static int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height );
70 static int (*p_close_infile)( hnd_t handle );
71
72 static int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
73 static int get_frame_total_yuv( hnd_t handle, int i_width, int i_height );
74 static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height );
75 static int close_file_yuv( hnd_t handle );
76
77 #ifdef AVIS_INPUT
78 static int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
79 static int get_frame_total_avis( hnd_t handle, int i_width, int i_height );
80 static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height );
81 static int close_file_avis( hnd_t handle );
82 #endif
83
84 /* output file operation function pointers */
85 static int (*p_open_outfile)( char *psz_filename, hnd_t *p_handle );
86 static int (*p_set_outfile_param)( hnd_t handle, x264_param_t *p_param );
87 static int (*p_write_nalu)( hnd_t handle, uint8_t *p_nal, int i_size );
88 static int (*p_set_eop)( hnd_t handle, x264_picture_t *p_picture );
89 static int (*p_close_outfile)( hnd_t handle );
90
91 static int open_file_bsf( char *psz_filename, hnd_t *p_handle );
92 static int set_param_bsf( hnd_t handle, x264_param_t *p_param );
93 static int write_nalu_bsf( hnd_t handle, uint8_t *p_nal, int i_size );
94 static int set_eop_bsf( hnd_t handle,  x264_picture_t *p_picture );
95 static int close_file_bsf( hnd_t handle );
96
97 #ifdef MP4_OUTPUT
98 static int open_file_mp4( char *psz_filename, hnd_t *p_handle );
99 static int set_param_mp4( hnd_t handle, x264_param_t *p_param );
100 static int write_nalu_mp4( hnd_t handle, uint8_t *p_nal, int i_size );
101 static int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture );
102 static int close_file_mp4( hnd_t handle );
103 #endif
104
105 static void Help( x264_param_t *defaults );
106 static int  Parse( int argc, char **argv, x264_param_t  *param, hnd_t *p_hin, hnd_t *p_hout, int *pb_decompress, int *b_progress );
107 static int  Encode( x264_param_t  *param, hnd_t hin,  hnd_t hout, int b_progress );
108 static int  Decode( x264_param_t  *param, FILE *fh26l, hnd_t hout );
109
110
111 /****************************************************************************
112  * main:
113  ****************************************************************************/
114 int main( int argc, char **argv )
115 {
116     x264_param_t param;
117
118     hnd_t   hout;
119     hnd_t   hin;
120
121     int     b_decompress;
122     int     b_progress;
123     int     i_ret;
124
125 #ifdef _MSC_VER
126     _setmode(_fileno(stdin), _O_BINARY);    /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
127     _setmode(_fileno(stdout), _O_BINARY);
128 #endif
129
130     x264_param_default( &param );
131
132     /* Parse command line */
133     if( Parse( argc, argv, &param, &hin, &hout, &b_decompress, &b_progress ) < 0 )
134     {
135         return -1;
136     }
137
138     /* Control-C handler */
139     signal( SIGINT, SigIntHandler );
140
141     if( b_decompress )
142         i_ret = Decode( &param, hin, hout );
143     else
144         i_ret = Encode( &param, hin, hout, b_progress );
145
146     return i_ret;
147 }
148
149 /*****************************************************************************
150  * Help:
151  *****************************************************************************/
152 static void Help( x264_param_t *defaults )
153 {
154     fprintf( stderr,
155              "x264 core:%d%s\n"
156              "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
157              "\n"
158              "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
159              "  or AVI or Avisynth if compiled with AVIS support (%s).\n"
160              "Outfile type is selected by filename:\n"
161              " .264 -> Raw bytestream\n"
162              " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
163              "\n"
164              "Options:\n"
165              "\n"
166              "  -h, --help                  Print this help\n"
167              "\n"
168              "  -I, --keyint <integer>      Maximum GOP size [%d]\n"
169              "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n"
170              "      --scenecut <integer>    How aggressively to insert extra I-frames [%d]\n"
171              "  -b, --bframe <integer>      Number of B-frames between I and P [%d]\n"
172              "      --no-b-adapt            Disable adaptive B-frame decision\n"
173              "      --b-bias <integer>      Influences how often B-frames are used [%d]\n"
174              "      --b-pyramid             Keep some B-frames as references\n"
175              "\n"
176              "      --no-cabac              Disable CABAC\n"
177              "  -r, --ref <integer>         Number of reference frames [%d]\n"
178              "      --nf                    Disable loop filter\n"
179              "  -f, --filter <alpha:beta>   Loop filter AlphaC0 and Beta parameters [%d:%d]\n"
180              "\n"
181              "  -q, --qp <integer>          Set QP [%d]\n"
182              "  -B, --bitrate <integer>     Set bitrate\n"
183              "      --qpmin <integer>       Set min QP [%d]\n"
184              "      --qpmax <integer>       Set max QP [%d]\n"
185              "      --qpstep <integer>      Set max QP step [%d]\n"
186              "      --rcsens <integer>      CBR ratecontrol sensitivity [%d]\n"
187              "      --rcbuf <integer>       Size of VBV buffer [%d]\n"
188              "      --rcinitbuf <integer>   Initial VBV buffer occupancy [%d]\n"
189              "      --ipratio <float>       QP factor between I and P [%.2f]\n"
190              "      --pbratio <float>       QP factor between P and B [%.2f]\n"
191              "      --chroma-qp-offset <integer>  QP difference between chroma and luma [%d]\n"
192              "\n"
193              "  -p, --pass <1|2|3>          Enable multipass ratecontrol:\n"
194              "                                  - 1: First pass, creates stats file\n"
195              "                                  - 2: Last pass, does not overwrite stats file\n"
196              "                                  - 3: Nth pass, overwrites stats file\n"
197              "      --stats <string>        Filename for 2 pass stats [\"%s\"]\n"
198              "      --rceq <string>         Ratecontrol equation [\"%s\"]\n"
199              "      --qcomp <float>         QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n"
200              "      --cplxblur <float>      Reduce fluctuations in QP (before curve compression) [%.1f]\n"
201              "      --qblur <float>         Reduce fluctuations in QP (after curve compression) [%.1f]\n"
202              "\n"
203              "  -A, --analyse <string>      Analyse options: [\"i4x4,p8x8,b8x8\"]\n"
204              "                                  - i4x4\n"
205              "                                  - p8x8, p4x4, b8x8\n"
206              "                                  - none, all\n"
207              "      --direct <string>       Direct MV prediction mode [\"temporal\"]\n"
208              "                                  - none, spatial, temporal\n"
209              "  -w, --weightb               Weighted prediction for B-frames\n"
210              "      --me <string>           Integer pixel motion estimation method [\"%s\"]\n"
211              "                                  - dia: diamond search, radius 1 (fast)\n"
212              "                                  - hex: hexagonal search, radius 2\n"
213              "                                  - esa: exhaustive search algorithm (slow)\n"
214              "      --merange <integer>     Maximum motion vector search range [%d]\n"
215              "  -m, --subme <integer>       Subpixel motion estimation quality: 1=fast, 5=best. [%d]\n"
216              "      --no-chroma-me          Ignore chroma in motion estimation\n"
217              "\n"
218              "      --level <integer>       Specify level (as defined by Annex A)\n"
219              "      --sar width:height      Specify Sample Aspect Ratio\n"
220              "      --fps <float|rational>  Specify framerate\n"
221              "      --frames <integer>      Maximum number of frames to encode\n"
222              "  -o, --output                Specify output file\n"
223              "\n"
224              "      --no-asm                Disable all CPU optimizations\n"
225              "      --no-psnr               Disable PSNR computation\n"
226              "      --quiet                 Quiet Mode\n"
227              "  -v, --verbose               Print stats for each frame\n"
228              "      --progress              Show a progress indicator while encoding\n"
229              "      --aud                   Use access unit delimiters\n"
230              "\n",
231             X264_BUILD, X264_VERSION,
232 #ifdef AVIS_INPUT
233             "yes",
234 #else
235             "no",
236 #endif
237 #ifdef MP4_OUTPUT
238             "yes",
239 #else
240             "no",
241 #endif
242             defaults->i_keyint_max,
243             defaults->i_keyint_min,
244             defaults->i_scenecut_threshold,
245             defaults->i_bframe,
246             defaults->i_bframe_bias,
247             defaults->i_frame_reference,
248             defaults->i_deblocking_filter_alphac0,
249             defaults->i_deblocking_filter_beta,
250             defaults->rc.i_qp_constant,
251             defaults->rc.i_qp_min,
252             defaults->rc.i_qp_max,
253             defaults->rc.i_qp_step,
254             defaults->rc.i_rc_sens,
255             defaults->rc.i_rc_buffer_size,
256             defaults->rc.i_rc_init_buffer,
257             defaults->rc.f_ip_factor,
258             defaults->rc.f_pb_factor,
259             defaults->analyse.i_chroma_qp_offset,
260             defaults->rc.psz_stat_out,
261             defaults->rc.psz_rc_eq,
262             defaults->rc.f_qcompress,
263             defaults->rc.f_complexity_blur,
264             defaults->rc.f_qblur,
265             defaults->analyse.i_me_method==X264_ME_DIA ? "dia"
266             : defaults->analyse.i_me_method==X264_ME_HEX ? "hex"
267             : defaults->analyse.i_me_method==X264_ME_ESA ? "esa" : NULL,
268             defaults->analyse.i_me_range,
269             defaults->analyse.i_subpel_refine
270            );
271 }
272
273 /*****************************************************************************
274  * Parse:
275  *****************************************************************************/
276 static int  Parse( int argc, char **argv,
277                    x264_param_t  *param,
278                    hnd_t *p_hin, hnd_t *p_hout, int *pb_decompress, int *b_progress )
279 {
280     char *psz_filename = NULL;
281     x264_param_t defaults = *param;
282     char *psz;
283     char b_avis = 0;
284
285     /* Default output */
286     *p_hout = NULL;
287     *p_hin  = NULL;
288     *pb_decompress = 0;
289     *b_progress = 0;
290
291     /* Default input file driver */
292     p_open_infile = open_file_yuv;
293     p_get_frame_total = get_frame_total_yuv;
294     p_read_frame = read_frame_yuv;
295     p_close_infile = close_file_yuv;
296
297     /* Default output file driver */
298     p_open_outfile = open_file_bsf;
299     p_set_outfile_param = set_param_bsf;
300     p_write_nalu = write_nalu_bsf;
301     p_set_eop = set_eop_bsf;
302     p_close_outfile = close_file_bsf;
303
304     /* Parse command line options */
305     opterr = 0; // no error message
306     for( ;; )
307     {
308         int long_options_index;
309 #define OPT_QPMIN 256
310 #define OPT_QPMAX 257
311 #define OPT_QPSTEP 258
312 #define OPT_RCSENS 259
313 #define OPT_IPRATIO 260
314 #define OPT_PBRATIO 261
315 #define OPT_RCBUF 262
316 #define OPT_RCIBUF 263
317 #define OPT_RCSTATS 264
318 #define OPT_RCEQ 265
319 #define OPT_QCOMP 266
320 #define OPT_NOPSNR 267
321 #define OPT_QUIET 268
322 #define OPT_SCENECUT 270
323 #define OPT_QBLUR 271
324 #define OPT_CPLXBLUR 272
325 #define OPT_FRAMES 273
326 #define OPT_FPS 274
327 #define OPT_DIRECT 275
328 #define OPT_LEVEL 276
329 #define OPT_NOBADAPT 277
330 #define OPT_BBIAS 278
331 #define OPT_BPYRAMID 279
332 #define OPT_CHROMA_QP 280
333 #define OPT_NO_CHROMA_ME 281
334 #define OPT_NO_CABAC 282
335 #define OPT_AUD 283
336 #define OPT_PROGRESS 284
337 #define OPT_ME 285
338 #define OPT_MERANGE 286
339
340         static struct option long_options[] =
341         {
342             { "help",    no_argument,       NULL, 'h' },
343             { "bitrate", required_argument, NULL, 'B' },
344             { "bframe",  required_argument, NULL, 'b' },
345             { "no-b-adapt", no_argument,    NULL, OPT_NOBADAPT },
346             { "b-bias",  required_argument, NULL, OPT_BBIAS },
347             { "b-pyramid", no_argument,     NULL, OPT_BPYRAMID },
348             { "min-keyint",required_argument,NULL,'i' },
349             { "keyint",  required_argument, NULL, 'I' },
350             { "scenecut",required_argument, NULL, OPT_SCENECUT },
351             { "nf",      no_argument,       NULL, 'n' },
352             { "filter",  required_argument, NULL, 'f' },
353             { "no-cabac",no_argument,       NULL, OPT_NO_CABAC },
354             { "qp",      required_argument, NULL, 'q' },
355             { "qpmin",   required_argument, NULL, OPT_QPMIN },
356             { "qpmax",   required_argument, NULL, OPT_QPMAX },
357             { "qpstep",  required_argument, NULL, OPT_QPSTEP },
358             { "ref",     required_argument, NULL, 'r' },
359             { "no-asm",  no_argument,       NULL, 'C' },
360             { "sar",     required_argument, NULL, 's' },
361             { "fps",     required_argument, NULL, OPT_FPS },
362             { "frames",  required_argument, NULL, OPT_FRAMES },
363             { "output",  required_argument, NULL, 'o' },
364             { "analyse", required_argument, NULL, 'A' },
365             { "direct",  required_argument, NULL, OPT_DIRECT },
366             { "weightb", no_argument,       NULL, 'w' },
367             { "me",      required_argument, NULL, OPT_ME },
368             { "merange", required_argument, NULL, OPT_MERANGE },
369             { "subme",   required_argument, NULL, 'm' },
370             { "no-chroma-me", no_argument,  NULL, OPT_NO_CHROMA_ME },
371             { "level",   required_argument, NULL, OPT_LEVEL },
372             { "rcsens",  required_argument, NULL, OPT_RCSENS },
373             { "rcbuf",   required_argument, NULL, OPT_RCBUF },
374             { "rcinitbuf",required_argument,NULL, OPT_RCIBUF },
375             { "ipratio", required_argument, NULL, OPT_IPRATIO },
376             { "pbratio", required_argument, NULL, OPT_PBRATIO },
377             { "chroma-qp-offset", required_argument, NULL, OPT_CHROMA_QP },
378             { "pass",    required_argument, NULL, 'p' },
379             { "stats",   required_argument, NULL, OPT_RCSTATS },
380             { "rceq",    required_argument, NULL, OPT_RCEQ },
381             { "qcomp",   required_argument, NULL, OPT_QCOMP },
382             { "qblur",   required_argument, NULL, OPT_QBLUR },
383             { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
384             { "no-psnr", no_argument,       NULL, OPT_NOPSNR },
385             { "quiet",   no_argument,       NULL, OPT_QUIET },
386             { "verbose", no_argument,       NULL, 'v' },
387             { "progress",no_argument,       NULL, OPT_PROGRESS },
388             { "aud",     no_argument,       NULL, OPT_AUD },
389             {0, 0, 0, 0}
390         };
391
392         int c;
393
394         c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:vw",
395                          long_options, &long_options_index);
396
397         if( c == -1 )
398         {
399             break;
400         }
401
402         switch( c )
403         {
404             case 'h':
405                 Help( &defaults );
406                 return -1;
407
408             case 0:
409                 break;
410             case 'B':
411                 param->rc.i_bitrate = atol( optarg );
412                 param->rc.b_cbr = 1;
413                 break;
414             case 'b':
415                 param->i_bframe = atol( optarg );
416                 break;
417             case OPT_NOBADAPT:
418                 param->b_bframe_adaptive = 0;
419                 break;
420             case OPT_BBIAS:
421                 param->i_bframe_bias = atol( optarg );
422                 break;
423             case OPT_BPYRAMID:
424                 param->b_bframe_pyramid = 1;
425                 break;
426             case 'i':
427                 param->i_keyint_min = atol( optarg );
428                 break;
429             case 'I':
430                 param->i_keyint_max = atol( optarg );
431                 break;
432             case OPT_SCENECUT:
433                 param->i_scenecut_threshold = atol( optarg );
434                 break;
435             case 'n':
436                 param->b_deblocking_filter = 0;
437                 break;
438             case 'f':
439             {
440                 char *p = strchr( optarg, ':' );
441                 param->i_deblocking_filter_alphac0 = atoi( optarg );
442                 param->i_deblocking_filter_beta = p ? atoi( p+1 ) : param->i_deblocking_filter_alphac0;
443                 break;
444             }
445             case 'q':
446                 param->rc.i_qp_constant = atoi( optarg );
447                 break;
448             case OPT_QPMIN:
449                 param->rc.i_qp_min = atoi( optarg );
450                 break;
451             case OPT_QPMAX:
452                 param->rc.i_qp_max = atoi( optarg );
453                 break;
454             case OPT_QPSTEP:
455                 param->rc.i_qp_step = atoi( optarg );
456                 break;
457             case 'r':
458                 param->i_frame_reference = atoi( optarg );
459                 break;
460             case OPT_NO_CABAC:
461                 param->b_cabac = 0;
462                 break;
463             case 'x':
464                 *pb_decompress = 1;
465                 break;
466             case 'C':
467                 param->cpu = 0;
468                 break;
469             case OPT_FRAMES:
470                 param->i_maxframes = atoi( optarg );
471                 break;
472             case'o':
473                 if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
474                 {
475 #ifdef MP4_OUTPUT
476                     p_open_outfile = open_file_mp4;
477                     p_write_nalu = write_nalu_mp4;
478                     p_set_outfile_param = set_param_mp4;
479                     p_set_eop = set_eop_mp4;
480                     p_close_outfile = close_file_mp4;
481 #else
482                     fprintf( stderr, "not compiled with MP4 output support\n" );
483                     return -1;
484 #endif
485                 }
486                 if( !strcmp(optarg, "-") )
487                     *p_hout = stdout;
488                 else if( p_open_outfile( optarg, p_hout ) )
489                 {
490                     fprintf( stderr, "cannot open output file `%s'\n", optarg );
491                     return -1;
492                 }
493                 break;
494             case 's':
495             {
496                 char *p = strchr( optarg, ':' );
497                 if( p )
498                 {
499                     param->vui.i_sar_width = atoi( optarg );
500                     param->vui.i_sar_height = atoi( p + 1 );
501                 }
502                 break;
503             }
504             case OPT_FPS:
505             {
506                 float fps;
507                 if( sscanf( optarg, "%d/%d", &param->i_fps_num, &param->i_fps_den ) == 2 )
508                     ;
509                 else if( sscanf( optarg, "%f", &fps ) )
510                 {
511                     param->i_fps_num = (int)(fps * 1000 + .5);
512                     param->i_fps_den = 1000;
513                 }
514                 else
515                 {
516                     fprintf( stderr, "bad fps `%s'\n", optarg );
517                     return -1;
518                 }
519                 break;
520             }
521             case 'A':
522                 param->analyse.inter = 0;
523                 if( strstr( optarg, "none" ) )  param->analyse.inter = 0x000000;
524                 if( strstr( optarg, "all" ) )   param->analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16;
525
526                 if( strstr( optarg, "i4x4" ) )  param->analyse.inter |= X264_ANALYSE_I4x4;
527                 if( strstr( optarg, "p8x8" ) )  param->analyse.inter |= X264_ANALYSE_PSUB16x16;
528                 if( strstr( optarg, "p4x4" ) )  param->analyse.inter |= X264_ANALYSE_PSUB8x8;
529                 if( strstr( optarg, "b8x8" ) )  param->analyse.inter |= X264_ANALYSE_BSUB16x16;
530                 break;
531             case OPT_DIRECT:
532                 if( strstr( optarg, "temporal" ) )
533                     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
534                 else if( strstr( optarg, "spatial" ) )
535                     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
536                 else if( strstr( optarg, "none" ) )
537                     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
538                 else
539                     param->analyse.i_direct_mv_pred = atoi( optarg );
540                 break;
541             case 'w':
542                 param->analyse.b_weighted_bipred = 1;
543                 break;
544             case OPT_ME:
545                 if( strstr( optarg, "dia" ) )
546                     param->analyse.i_me_method = X264_ME_DIA;
547                 else if( strstr( optarg, "hex" ) )
548                     param->analyse.i_me_method = X264_ME_HEX;
549                 else if( strstr( optarg, "esa" ) )
550                     param->analyse.i_me_method = X264_ME_ESA;
551                 else
552                 {
553                     fprintf( stderr, "bad ME method `%s'\n", optarg );
554                     return -1;
555                 }
556                 break;
557             case OPT_MERANGE:
558                 param->analyse.i_me_range = atoi(optarg);
559                 break;
560             case 'm':
561                 param->analyse.i_subpel_refine = atoi(optarg);
562                 break;
563             case OPT_NO_CHROMA_ME:
564                 param->analyse.b_chroma_me = 0;
565                 break;
566             case OPT_LEVEL:
567                 param->i_level_idc = atoi(optarg);
568                 break;
569             case OPT_RCBUF:
570                 param->rc.i_rc_buffer_size = atoi(optarg);
571                 break;
572             case OPT_RCIBUF:
573                 param->rc.i_rc_init_buffer = atoi(optarg);
574                 break;
575             case OPT_RCSENS:
576                 param->rc.i_rc_sens = atoi(optarg);
577                 break;
578             case OPT_IPRATIO:
579                 param->rc.f_ip_factor = atof(optarg);
580                 break;
581             case OPT_PBRATIO:
582                 param->rc.f_pb_factor = atof(optarg);
583                 break;
584             case OPT_CHROMA_QP:
585                 param->analyse.i_chroma_qp_offset = atoi(optarg);
586                 break;
587             case 'p':
588             {
589                 int i_pass = atoi(optarg);
590                 if( i_pass == 1 )
591                     param->rc.b_stat_write = 1;
592                 else if( i_pass == 2 )
593                     param->rc.b_stat_read = 1;
594                 else if( i_pass > 2 )
595                     param->rc.b_stat_read =
596                     param->rc.b_stat_write = 1;
597                 break;
598             }
599             case OPT_RCSTATS:
600                 param->rc.psz_stat_in = optarg;
601                 param->rc.psz_stat_out = optarg;
602                 break;
603             case OPT_RCEQ:
604                 param->rc.psz_rc_eq = optarg;
605                break;
606             case OPT_QCOMP:
607                 param->rc.f_qcompress = atof(optarg);
608                 break;
609             case OPT_QBLUR:
610                 param->rc.f_qblur = atof(optarg);
611                 break;
612             case OPT_CPLXBLUR:
613                 param->rc.f_complexity_blur = atof(optarg);
614                 break;
615             case OPT_NOPSNR:
616                 param->analyse.b_psnr = 0;
617                 break;
618             case OPT_QUIET:
619                 param->i_log_level = X264_LOG_NONE;
620                 break;
621             case 'v':
622                 param->i_log_level = X264_LOG_DEBUG;
623                 break;
624             case OPT_AUD:
625                 param->b_aud = 1;
626                 break;
627             case OPT_PROGRESS:
628                 *b_progress = 1;
629                 break;
630             default:
631                 fprintf( stderr, "unknown option (%c)\n", optopt );
632                 return -1;
633         }
634     }
635
636     /* Get the file name */
637     if( optind > argc - 1 || !*p_hout )
638     {
639         Help( &defaults );
640         return -1;
641     }
642     psz_filename = argv[optind++];
643
644     if( !(*pb_decompress) )
645     {
646         if( optind > argc - 1 )
647         {
648             /* try to parse the file name */
649             for( psz = psz_filename; *psz; psz++ )
650             {
651                 if( *psz >= '0' && *psz <= '9'
652                     && sscanf( psz, "%ux%u", &param->i_width, &param->i_height ) == 2 )
653                 {
654                     if( param->i_log_level > X264_LOG_NONE )
655                         fprintf( stderr, "x264: file name gives %dx%d\n", param->i_width, param->i_height );
656                     break;
657                 }
658             }
659         }
660         else
661         {
662             sscanf( argv[optind++], "%ux%u", &param->i_width, &param->i_height );
663         }
664         
665         /* check avis input */
666         psz = psz_filename + strlen(psz_filename) - 1;
667         while( psz > psz_filename && *psz != '.' )
668             psz--;
669
670         if( !strncasecmp( psz, ".avi", 4 ) || !strncasecmp( psz, ".avs", 4 ) )
671             b_avis = 1;
672
673         if( !b_avis && ( !param->i_width || !param->i_height ) )
674         {
675             Help( &defaults );
676             return -1;
677         }
678     }
679
680     /* open the input */
681     if( !strcmp( psz_filename, "-" ) )
682     {
683         *p_hin = stdin;
684         optind++;
685     }
686     else
687     {
688         if( b_avis )
689         {
690 #ifdef AVIS_INPUT
691             p_open_infile = open_file_avis;
692             p_get_frame_total = get_frame_total_avis;
693             p_read_frame = read_frame_avis;
694             p_close_infile = close_file_avis;
695 #else
696             fprintf( stderr, "not compiled with AVIS input support\n" );
697             return -1;
698 #endif
699         }
700         if( p_open_infile( psz_filename, p_hin, param ) )
701         {
702             fprintf( stderr, "could not open input file '%s'\n", psz_filename );
703             return -1;
704         }
705     }
706
707     return 0;
708 }
709
710 /*****************************************************************************
711  * Decode:
712  *****************************************************************************/
713 static int  Decode( x264_param_t  *param, FILE *fh26l, hnd_t hout )
714 {
715     fprintf( stderr, "decompressor not working (help is welcome)\n" );
716     return -1;
717 #if 0
718     x264_nal_t nal;
719     int i_data;
720     int b_eof;
721
722     //param.cpu = 0;
723     if( ( h = x264_decoder_open( &param ) ) == NULL )
724     {
725         fprintf( stderr, "x264_decoder_open failed\n" );
726         return -1;
727     }
728
729     i_start = x264_mdate();
730     b_eof = 0;
731     i_frame = 0;
732     i_data  = 0;
733     nal.p_payload = malloc( DATA_MAX );
734
735     while( !i_ctrl_c )
736     {
737         uint8_t *p, *p_next, *end;
738         int i_size;
739         /* fill buffer */
740         if( i_data < DATA_MAX && !b_eof )
741         {
742             int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );
743             if( i_read <= 0 )
744             {
745                 b_eof = 1;
746             }
747             else
748             {
749                 i_data += i_read;
750             }
751         }
752
753         if( i_data < 3 )
754         {
755             break;
756         }
757
758         end = &data[i_data];
759
760         /* extract one nal */
761         p = &data[0];
762         while( p < end - 3 )
763         {
764             if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
765             {
766                 break;
767             }
768             p++;
769         }
770
771         if( p >= end - 3 )
772         {
773             fprintf( stderr, "garbage (i_data = %d)\n", i_data );
774             i_data = 0;
775             continue;
776         }
777
778         p_next = p + 3;
779         while( p_next < end - 3 )
780         {
781             if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
782             {
783                 break;
784             }
785             p_next++;
786         }
787
788         if( p_next == end - 3 && i_data < DATA_MAX )
789         {
790             p_next = end;
791         }
792
793         /* decode this nal */
794         i_size = p_next - p - 3;
795         if( i_size <= 0 )
796         {
797             if( b_eof )
798             {
799                 break;
800             }
801             fprintf( stderr, "nal too large (FIXME) ?\n" );
802             i_data = 0;
803             continue;
804         }
805
806         x264_nal_decode( &nal, p +3, i_size );
807
808         /* decode the content of the nal */
809         x264_decoder_decode( h, &pic, &nal );
810
811         if( pic != NULL )
812         {
813             int i;
814
815             i_frame++;
816
817             for( i = 0; i < pic->i_plane;i++ )
818             {
819                 int i_line;
820                 int i_div;
821
822                 i_div = i==0 ? 1 : 2;
823                 for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )
824                 {
825                     fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, hout );
826                 }
827             }
828         }
829
830         memmove( &data[0], p_next, end - p_next );
831         i_data -= p_next - &data[0];
832     }
833
834     i_end = x264_mdate();
835     free( nal.p_payload );
836     fprintf( stderr, "\n" );
837
838     x264_decoder_close( h );
839
840     fclose( fh26l );
841     if( hout != stdout )
842     {
843         fclose( hout );
844     }
845     if( i_frame > 0 )
846     {
847         double fps = (double)i_frame * (double)1000000 /
848                      (double)( i_end - i_start );
849         fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );
850     }
851 #endif
852 }
853
854 static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
855 {
856     x264_picture_t pic_out;
857     x264_nal_t *nal;
858     int i_nal, i;
859     int i_file = 0;
860
861     /* Do not force any parameters */
862     if( pic )
863     {
864         pic->i_type = X264_TYPE_AUTO;
865         pic->i_qpplus1 = 0;
866     }
867     if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
868     {
869         fprintf( stderr, "x264_encoder_encode failed\n" );
870     }
871
872     for( i = 0; i < i_nal; i++ )
873     {
874         int i_size;
875         int i_data;
876
877         i_data = DATA_MAX;
878         if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
879         {
880             i_file += p_write_nalu( hout, data, i_size );
881         }
882         else if( i_size < 0 )
883         {
884             fprintf( stderr, "need to increase buffer size (size=%d)\n", -i_size );
885         }
886     }
887     if (i_nal)
888         p_set_eop( hout, &pic_out );
889
890     return i_file;
891 }
892
893 /*****************************************************************************
894  * Encode:
895  *****************************************************************************/
896 static int  Encode( x264_param_t  *param, hnd_t hin, hnd_t hout, int b_progress )
897 {
898     x264_t *h;
899     x264_picture_t pic;
900
901     int     i_frame, i_frame_total;
902     int64_t i_start, i_end;
903     int64_t i_file;
904     int     i_frame_size;
905     int     i_progress;
906
907     i_frame_total = p_get_frame_total( hin, param->i_width, param->i_height );
908
909     if( ( h = x264_encoder_open( param ) ) == NULL )
910     {
911         fprintf( stderr, "x264_encoder_open failed\n" );
912         p_close_infile( hin );
913         p_close_outfile( hout );
914         return -1;
915     }
916
917     if( p_set_outfile_param( hout, param ) )
918     {
919         fprintf( stderr, "can't set outfile param\n" );
920         p_close_infile( hin );
921         p_close_outfile( hout );
922         return -1;
923     }
924
925     /* Create a new pic */
926     x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
927
928     i_start = x264_mdate();
929     /* Encode frames */
930     if( param->i_maxframes > 0 && param->i_maxframes < i_frame_total )
931         i_frame_total = param->i_maxframes;
932     for( i_frame = 0, i_file = 0, i_progress = 0; i_ctrl_c == 0 && i_frame < i_frame_total; )
933     {
934         if( p_read_frame( &pic, hin, param->i_width, param->i_height ) )
935             break;
936
937         pic.i_pts = i_frame * param->i_fps_den;
938
939         i_file += Encode_frame( h, hout, &pic );
940
941         i_frame++;
942
943         /* update status line (up to 1000 times per input file) */
944         if( b_progress && param->i_log_level < X264_LOG_DEBUG && 
945             i_frame * 1000 / i_frame_total > i_progress )
946         {
947             int64_t i_elapsed = x264_mdate() - i_start;
948             double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
949             i_progress = i_frame * 1000 / i_frame_total;
950             fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps   \r", i_frame, 
951                      i_frame_total, (float)i_progress / 10, fps );
952             fflush( stderr ); // needed in windows
953         }
954     }
955     /* Flush delayed B-frames */
956     do {
957         i_file +=
958         i_frame_size = Encode_frame( h, hout, NULL );
959     } while( i_frame_size );
960
961     i_end = x264_mdate();
962     x264_picture_clean( &pic );
963     x264_encoder_close( h );
964     fprintf( stderr, "\n" );
965
966     p_close_infile( hin );
967     p_close_outfile( hout );
968
969     if( i_frame > 0 )
970     {
971         double fps = (double)i_frame * (double)1000000 /
972                      (double)( i_end - i_start );
973
974         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 ) );
975     }
976
977     return 0;
978 }
979
980 /* raw 420 yuv file operation */
981 static int open_file_yuv( char *psz_filename, hnd_t *p_handle , x264_param_t *p_param )
982 {
983     if ((*p_handle = fopen(psz_filename, "rb")) == NULL)
984         return -1;
985     return 0;
986 }
987
988 static int get_frame_total_yuv( hnd_t handle, int i_width, int i_height )
989 {
990     FILE *f = (FILE *)handle;
991     int i_frame_total = 0;
992
993     if( !fseek( f, 0, SEEK_END ) )
994     {
995         int64_t i_size = ftell( f );
996         fseek( f, 0, SEEK_SET );
997         i_frame_total = (int)(i_size / ( i_width * i_height * 3 / 2 ));
998     }
999
1000     return i_frame_total;
1001 }
1002
1003 static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height )
1004 {
1005     FILE *f = (FILE *)handle;
1006
1007     if( fread( p_pic->img.plane[0], 1, i_width * i_height, f ) <= 0
1008             || fread( p_pic->img.plane[1], 1, i_width * i_height / 4, f ) <= 0
1009             || fread( p_pic->img.plane[2], 1, i_width * i_height / 4, f ) <= 0 )
1010         return -1;
1011
1012     return 0;
1013 }
1014
1015 static int close_file_yuv(hnd_t handle)
1016 {
1017     if (handle == NULL)
1018         return 0;
1019     return fclose((FILE *)handle);
1020 }
1021
1022
1023 /* avs/avi input file support under cygwin */
1024
1025 #ifdef AVIS_INPUT
1026
1027 static int gcd(int a, int b)
1028 {
1029     int c;
1030
1031     while (1)
1032     {
1033         c = a % b;
1034         if (!c)
1035             return b;
1036         a = b;
1037         b = c;
1038     }
1039 }
1040
1041 static int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
1042 {
1043     AVISTREAMINFO info;
1044     PAVISTREAM p_avi = NULL;
1045     int i;
1046
1047     *p_handle = NULL;
1048
1049     AVIFileInit();
1050     if( AVIStreamOpenFromFile( &p_avi, psz_filename, streamtypeVIDEO, 0, OF_READ, NULL ) )
1051     {
1052         AVIFileExit();
1053         return -1;
1054     }
1055
1056     if( AVIStreamInfo(p_avi, &info, sizeof(AVISTREAMINFO)) )
1057     {
1058         AVIStreamRelease(p_avi);
1059         AVIFileExit();
1060         return -1;
1061     }
1062
1063     // check input format
1064     if (info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2'))
1065     {
1066         fprintf( stderr, "avis [error]: unsupported input format (%c%c%c%c)\n",
1067             (char)(info.fccHandler & 0xff), (char)((info.fccHandler >> 8) & 0xff),
1068             (char)((info.fccHandler >> 16) & 0xff), (char)((info.fccHandler >> 24)) );
1069
1070         AVIStreamRelease(p_avi);
1071         AVIFileExit();
1072
1073         return -1;
1074     }
1075
1076     p_param->i_width = info.rcFrame.right - info.rcFrame.left;
1077     p_param->i_height = info.rcFrame.bottom - info.rcFrame.top;
1078     i = gcd(info.dwRate, info.dwScale);
1079     p_param->i_fps_den = info.dwScale / i;
1080     p_param->i_fps_num = info.dwRate / i;
1081
1082     fprintf( stderr, "avis [info]: %dx%d @ %.2f fps (%d frames)\n",  
1083         p_param->i_width, p_param->i_height,
1084         (double)p_param->i_fps_num / (double)p_param->i_fps_den,
1085         (int)info.dwLength );
1086
1087     *p_handle = (hnd_t)p_avi;
1088
1089     return 0;
1090 }
1091
1092 static int get_frame_total_avis( hnd_t handle, int i_width, int i_height )
1093 {
1094     PAVISTREAM p_avi = (PAVISTREAM)handle;
1095     AVISTREAMINFO info;
1096
1097     if( AVIStreamInfo(p_avi, &info, sizeof(AVISTREAMINFO)) )
1098         return -1;
1099
1100     return info.dwLength;
1101 }
1102
1103 static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height )
1104 {
1105     static int i_index = 0;
1106     PAVISTREAM p_avi = (PAVISTREAM)handle;
1107     
1108     p_pic->img.i_csp = X264_CSP_YV12;
1109     
1110     if(  AVIStreamRead(p_avi, i_index, 1, p_pic->img.plane[0], i_width * i_height * 3 / 2, NULL, NULL ) )
1111         return -1;
1112
1113     i_index++;
1114
1115     return 0;
1116 }
1117
1118 static int close_file_avis( hnd_t handle )
1119 {
1120     PAVISTREAM p_avi = (PAVISTREAM)handle;
1121
1122     AVIStreamRelease(p_avi);
1123     AVIFileExit();
1124
1125     return 0;
1126 }
1127
1128 #endif
1129
1130
1131 static int open_file_bsf( char *psz_filename, hnd_t *p_handle )
1132 {
1133     if ((*p_handle = fopen(psz_filename, "w+b")) == NULL)
1134         return -1;
1135
1136     return 0;
1137 }
1138
1139 static int set_param_bsf( hnd_t handle, x264_param_t *p_param )
1140 {
1141     return 0;
1142 }
1143
1144 static int write_nalu_bsf( hnd_t handle, uint8_t *p_nalu, int i_size )
1145 {
1146     if (fwrite(p_nalu, i_size, 1, (FILE *)handle) > 0)
1147         return i_size;
1148     return -1;
1149 }
1150
1151 static int set_eop_bsf( hnd_t handle,  x264_picture_t *p_picture )
1152 {
1153     return 0;
1154 }
1155
1156 static int close_file_bsf( hnd_t handle )
1157 {
1158     if ((handle == NULL) || (handle == stdout))
1159         return 0;
1160
1161     return fclose((FILE *)handle);
1162 }
1163
1164 /* -- mp4 muxing support ------------------------------------------------- */
1165 #ifdef MP4_OUTPUT
1166
1167 typedef struct
1168 {
1169     M4File *p_file;
1170     AVCConfig *p_config;
1171     M4Sample *p_sample;
1172     int i_track;
1173     int i_descidx;
1174     int i_time_inc;
1175     int i_time_res;
1176     int i_numframe;
1177     int i_init_delay;
1178     uint8_t b_sps;
1179     uint8_t b_pps;
1180 } mp4_t;
1181
1182
1183 static void recompute_bitrate_mp4(M4File *p_file, int i_track)
1184 {
1185     u32 i, count, di, timescale, time_wnd, rate;
1186     u64 offset;
1187     Double br;
1188     ESDescriptor *esd;
1189
1190     esd = M4_GetStreamDescriptor(p_file, i_track, 1);
1191     if (!esd) return;
1192
1193     esd->decoderConfig->avgBitrate = 0;
1194     esd->decoderConfig->maxBitrate = 0;
1195     rate = time_wnd = 0;
1196
1197     timescale = M4_GetMediaTimeScale(p_file, i_track);
1198     count = M4_GetSampleCount(p_file, i_track);
1199     for (i=0; i<count; i++) {
1200         M4Sample *samp = M4_GetSampleInfo(p_file, i_track, i+1, &di, &offset);
1201
1202         if (samp->dataLength>esd->decoderConfig->bufferSizeDB) esd->decoderConfig->bufferSizeDB = samp->dataLength;
1203
1204         if (esd->decoderConfig->bufferSizeDB < samp->dataLength) esd->decoderConfig->bufferSizeDB = samp->dataLength;
1205         esd->decoderConfig->avgBitrate += samp->dataLength;
1206         rate += samp->dataLength;
1207         if (samp->DTS > time_wnd + timescale) {
1208             if (rate > esd->decoderConfig->maxBitrate) esd->decoderConfig->maxBitrate = rate;
1209             time_wnd = samp->DTS;
1210             rate = 0;
1211         }
1212
1213         M4_DeleteSample(&samp);
1214     }
1215
1216     br = (Double) (s64) M4_GetMediaDuration(p_file, i_track);
1217     br /= timescale;
1218     esd->decoderConfig->avgBitrate = (u32) (esd->decoderConfig->avgBitrate / br);
1219     /*move to bps*/
1220     esd->decoderConfig->avgBitrate *= 8;
1221     esd->decoderConfig->maxBitrate *= 8;
1222
1223     M4_ChangeStreamDescriptor(p_file, i_track, 1, esd);
1224     OD_DeleteDescriptor((Descriptor **)&esd);
1225 }
1226
1227
1228 static int close_file_mp4( hnd_t handle )
1229 {
1230     mp4_t *p_mp4 = (mp4_t *)handle;
1231
1232     if (p_mp4 == NULL)
1233         return 0;
1234
1235     if (p_mp4->p_config)
1236         AVC_DeleteConfig(p_mp4->p_config);
1237
1238     if (p_mp4->p_sample)
1239     {
1240         if (p_mp4->p_sample->data)
1241             free(p_mp4->p_sample->data);
1242
1243         M4_DeleteSample(&p_mp4->p_sample);
1244     }
1245
1246     if (p_mp4->p_file)
1247     {
1248         recompute_bitrate_mp4(p_mp4->p_file, p_mp4->i_track);
1249         M4_SetMoviePLIndication(p_mp4->p_file, M4_PL_VISUAL, 0x15);
1250         M4_SetStorageMode(p_mp4->p_file, M4_FLAT);
1251         M4_MovieClose(p_mp4->p_file);
1252     }
1253
1254     free(p_mp4);
1255
1256     return 0;
1257 }
1258
1259 static int open_file_mp4( char *psz_filename, hnd_t *p_handle )
1260 {
1261     mp4_t *p_mp4;
1262
1263     *p_handle = NULL;
1264
1265     if ((p_mp4 = (mp4_t *)malloc(sizeof(mp4_t))) == NULL)
1266         return -1;
1267
1268     memset(p_mp4, 0, sizeof(mp4_t));
1269     p_mp4->p_file = M4_MovieOpen(psz_filename, M4_OPEN_WRITE);
1270
1271     if ((p_mp4->p_sample = M4_NewSample()) == NULL)
1272     {
1273         close_file_mp4( p_mp4 );
1274         return -1;
1275     }
1276
1277     M4_SetMovieVersionInfo(p_mp4->p_file, H264_AVC_File, 0);
1278
1279     *p_handle = p_mp4;
1280
1281     return 0;
1282 }
1283
1284
1285 static int set_param_mp4( hnd_t handle, x264_param_t *p_param )
1286 {
1287     mp4_t *p_mp4 = (mp4_t *)handle;
1288
1289     p_mp4->i_track = M4_NewTrack(p_mp4->p_file, 0, M4_VisualMediaType, 
1290         p_param->i_fps_num);
1291
1292     p_mp4->p_config = AVC_NewConfig();
1293     M4_AVC_NewStreamConfig(p_mp4->p_file, p_mp4->i_track, p_mp4->p_config, 
1294         NULL, NULL, &p_mp4->i_descidx);
1295
1296     M4_SetVisualEntrySize(p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, 
1297         p_param->i_width, p_param->i_height);
1298
1299     p_mp4->p_sample->data = (char *)malloc(p_param->i_width * p_param->i_height * 3 / 2);
1300     if (p_mp4->p_sample->data == NULL)
1301         return -1;
1302
1303     p_mp4->i_time_res = p_param->i_fps_num;
1304     p_mp4->i_time_inc = p_param->i_fps_den;
1305     p_mp4->i_init_delay = p_param->i_bframe ? (p_param->b_bframe_pyramid ? 2 : 1) : 0;
1306     p_mp4->i_init_delay *= p_mp4->i_time_inc;
1307     fprintf(stderr, "mp4 [info]: initial delay %d (scale %d)\n", 
1308         p_mp4->i_init_delay, p_mp4->i_time_res);
1309
1310     return 0;
1311 }
1312
1313
1314 static int write_nalu_mp4( hnd_t handle, uint8_t *p_nalu, int i_size )
1315 {
1316     mp4_t *p_mp4 = (mp4_t *)handle;
1317     AVCConfigSlot *p_slot;
1318     uint8_t type = p_nalu[4] & 0x1f;
1319     int psize;
1320
1321     switch(type)
1322     {
1323     // sps
1324     case 0x07:
1325         if (!p_mp4->b_sps)
1326         {
1327             p_mp4->p_config->configurationVersion = 1;
1328             p_mp4->p_config->AVCProfileIndication = p_nalu[5];
1329             p_mp4->p_config->profile_compatibility = p_nalu[6];
1330             p_mp4->p_config->AVCLevelIndication = p_nalu[7];
1331             p_slot = (AVCConfigSlot *)malloc(sizeof(AVCConfigSlot));
1332             p_slot->size = i_size - 4;
1333             p_slot->data = (char *)malloc(p_slot->size);
1334             memcpy(p_slot->data, p_nalu + 4, i_size - 4);
1335             ChainAddEntry(p_mp4->p_config->sequenceParameterSets, p_slot);
1336             p_slot = NULL;
1337             p_mp4->b_sps = 1;
1338         }
1339         break;
1340
1341     // pps      
1342     case 0x08:
1343         if (!p_mp4->b_pps)
1344         {
1345             p_slot = (AVCConfigSlot *)malloc(sizeof(AVCConfigSlot));
1346             p_slot->size = i_size - 4;
1347             p_slot->data = (char *)malloc(p_slot->size);
1348             memcpy(p_slot->data, p_nalu + 4, i_size - 4);
1349             ChainAddEntry(p_mp4->p_config->pictureParameterSets, p_slot);
1350             p_slot = NULL;
1351             p_mp4->b_pps = 1;
1352             if (p_mp4->b_sps)
1353                 M4_AVC_UpdateStreamConfig(p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config);
1354         }
1355         break;
1356
1357     // slice, sei
1358     case 0x1:
1359     case 0x5:
1360     case 0x6:
1361         psize = i_size - 4 ;
1362         memcpy(p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size);
1363         p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 0] = (psize >> 24) & 0xff;
1364         p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 1] = (psize >> 16) & 0xff;
1365         p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 2] = (psize >> 8) & 0xff;
1366         p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 3] = (psize >> 0) & 0xff;
1367         p_mp4->p_sample->dataLength += i_size;
1368         break;
1369     }
1370
1371     return i_size;
1372 }
1373
1374 static int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture )
1375 {
1376     mp4_t *p_mp4 = (mp4_t *)handle;
1377     uint32_t dts = p_mp4->i_numframe * p_mp4->i_time_inc;
1378     uint32_t pts = p_picture->i_pts;
1379     int offset = p_mp4->i_init_delay + pts - dts;
1380
1381     p_mp4->p_sample->IsRAP = p_picture->i_type == X264_TYPE_IDR ? 1 : 0;
1382     p_mp4->p_sample->DTS = dts;
1383     p_mp4->p_sample->CTS_Offset = offset;
1384     M4_AddSample(p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample);
1385
1386     p_mp4->p_sample->dataLength = 0;
1387     p_mp4->i_numframe++;
1388
1389     return 0;
1390 }
1391
1392 #endif