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