1 /*****************************************************************************
2 * x264: h264 encoder/decoder testing program.
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: x264.c,v 1.1 2004/06/03 19:24:12 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
35 #include <io.h> /* _setmode() */
36 #include <fcntl.h> /* _O_BINARY */
45 #include <gpac/isomedia.h>
49 #include "common/common.h"
58 #define DATA_MAX 3000000
59 uint8_t data[DATA_MAX];
64 static int b_ctrl_c = 0;
65 static int b_exit_on_ctrl_c = 0;
66 static void SigIntHandler( int a )
68 if( b_exit_on_ctrl_c )
82 /* input file operation function pointers */
83 static int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
84 static int (*p_get_frame_total)( hnd_t handle, int i_width, int i_height );
85 static int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
86 static int (*p_close_infile)( hnd_t handle );
88 static int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
89 static int get_frame_total_yuv( hnd_t handle, int i_width, int i_height );
90 static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
91 static int close_file_yuv( hnd_t handle );
94 static int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
95 static int get_frame_total_avis( hnd_t handle, int i_width, int i_height );
96 static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
97 static int close_file_avis( hnd_t handle );
100 /* output file operation function pointers */
101 static int (*p_open_outfile)( char *psz_filename, hnd_t *p_handle );
102 static int (*p_set_outfile_param)( hnd_t handle, x264_param_t *p_param );
103 static int (*p_write_nalu)( hnd_t handle, uint8_t *p_nal, int i_size );
104 static int (*p_set_eop)( hnd_t handle, x264_picture_t *p_picture );
105 static int (*p_close_outfile)( hnd_t handle );
107 static int open_file_bsf( char *psz_filename, hnd_t *p_handle );
108 static int set_param_bsf( hnd_t handle, x264_param_t *p_param );
109 static int write_nalu_bsf( hnd_t handle, uint8_t *p_nal, int i_size );
110 static int set_eop_bsf( hnd_t handle, x264_picture_t *p_picture );
111 static int close_file_bsf( hnd_t handle );
114 static int open_file_mp4( char *psz_filename, hnd_t *p_handle );
115 static int set_param_mp4( hnd_t handle, x264_param_t *p_param );
116 static int write_nalu_mp4( hnd_t handle, uint8_t *p_nal, int i_size );
117 static int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture );
118 static int close_file_mp4( hnd_t handle );
121 static int open_file_mkv( char *psz_filename, hnd_t *p_handle );
122 static int set_param_mkv( hnd_t handle, x264_param_t *p_param );
123 static int write_nalu_mkv( hnd_t handle, uint8_t *p_nal, int i_size );
124 static int set_eop_mkv( hnd_t handle, x264_picture_t *p_picture );
125 static int close_file_mkv( hnd_t handle );
127 static void Help( x264_param_t *defaults );
128 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
129 static int Encode( x264_param_t *param, cli_opt_t *opt );
132 /****************************************************************************
134 ****************************************************************************/
135 int main( int argc, char **argv )
141 _setmode(_fileno(stdin), _O_BINARY); /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
142 _setmode(_fileno(stdout), _O_BINARY);
145 x264_param_default( ¶m );
147 /* Parse command line */
148 if( Parse( argc, argv, ¶m, &opt ) < 0 )
151 /* Control-C handler */
152 signal( SIGINT, SigIntHandler );
154 return Encode( ¶m, &opt );
157 /*****************************************************************************
159 *****************************************************************************/
160 static void Help( x264_param_t *defaults )
164 "Syntax: x264 [options] -o outfile infile [widthxheight]\n"
166 "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
167 " or AVI or Avisynth if compiled with AVIS support (%s).\n"
168 "Outfile type is selected by filename:\n"
169 " .264 -> Raw bytestream\n"
170 " .mkv -> Matroska\n"
171 " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
175 " -h, --help Print this help\n"
177 "Frame-type options:\n"
179 " -I, --keyint <integer> Maximum GOP size [%d]\n"
180 " -i, --min-keyint <integer> Minimum GOP size [%d]\n"
181 " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n"
182 " -b, --bframes <integer> Number of B-frames between I and P [%d]\n"
183 " --no-b-adapt Disable adaptive B-frame decision\n"
184 " --b-bias <integer> Influences how often B-frames are used [%d]\n"
185 " --b-pyramid Keep some B-frames as references\n"
187 " --no-cabac Disable CABAC\n"
188 " -r, --ref <integer> Number of reference frames [%d]\n"
189 " --nf Disable loop filter\n"
190 " -f, --filter <alpha:beta> Loop filter AlphaC0 and Beta parameters [%d:%d]\n"
194 " -q, --qp <integer> Set QP (0=lossless) [%d]\n"
195 " -B, --bitrate <integer> Set bitrate\n"
196 " --qpmin <integer> Set min QP [%d]\n"
197 " --qpmax <integer> Set max QP [%d]\n"
198 " --qpstep <integer> Set max QP step [%d]\n"
199 " --ratetol <float> Allowed variance of average bitrate [%.1f]\n"
200 " --vbv-maxrate <integer> Max local bitrate [%d]\n"
201 " --vbv-bufsize <integer> Size of VBV buffer [%d]\n"
202 " --vbv-init <float> Initial VBV buffer occupancy [%.1f]\n"
204 " --ipratio <float> QP factor between I and P [%.2f]\n"
205 " --pbratio <float> QP factor between P and B [%.2f]\n"
206 " --chroma-qp-offset <integer> QP difference between chroma and luma [%d]\n"
208 " -p, --pass <1|2|3> Enable multipass ratecontrol:\n"
209 " - 1: First pass, creates stats file\n"
210 " - 2: Last pass, does not overwrite stats file\n"
211 " - 3: Nth pass, overwrites stats file\n"
212 " --stats <string> Filename for 2 pass stats [\"%s\"]\n"
213 " --rceq <string> Ratecontrol equation [\"%s\"]\n"
214 " --qcomp <float> QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n"
215 " --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n"
216 " --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n"
218 " --zones <zone0>/<zone1>/...\n"
219 " Tweak the bitrate of some regions of the video\n"
220 " Each zone is of the form\n"
221 " <start frame>,<end frame>,<option>\n"
222 " where <option> is either\n"
223 " q=<integer> (force QP)\n"
224 " or b=<float> (bitrate multiplier)\n"
228 " -A, --analyse <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
229 " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
231 " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n"
232 " --direct <string> Direct MV prediction mode [\"temporal\"]\n"
233 " - none, spatial, temporal\n"
234 " -w, --weightb Weighted prediction for B-frames\n"
235 " --me <string> Integer pixel motion estimation method [\"%s\"]\n"
236 " - dia: diamond search, radius 1 (fast)\n"
237 " - hex: hexagonal search, radius 2\n"
238 " - umh: uneven multi-hexagon search\n"
239 " - esa: exhaustive search (slow)\n"
240 " --merange <integer> Maximum motion vector search range [%d]\n"
241 " -m, --subme <integer> Subpixel motion estimation and partition\n"
242 " decision quality: 1=fast, 6=best. [%d]\n"
243 " --no-chroma-me Ignore chroma in motion estimation\n"
244 " -8, --8x8dct Adaptive spatial transform size\n"
246 " --cqm <string> Preset quant matrices [\"flat\"]\n"
248 " --cqmfile <string> Read quant matrices from a JM-compatible file\n"
249 " Overrides any other --cqm* options.\n"
250 " --cqm4 <list> Set all 4x4 quant matrices\n"
251 " Takes a comma-separated list of 16 integers.\n"
252 " --cqm8 <list> Set all 8x8 quant matrices\n"
253 " Takes a comma-separated list of 64 integers.\n"
254 " --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
255 " Set both luma and chroma quant matrices\n"
256 " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
257 " Set individual quant matrices\n"
261 " --level <integer> Specify level (as defined by Annex A)\n"
262 " --sar width:height Specify Sample Aspect Ratio\n"
263 " --fps <float|rational> Specify framerate\n"
264 " --seek <integer> First frame to encode\n"
265 " --frames <integer> Maximum number of frames to encode\n"
266 " -o, --output Specify output file\n"
268 " --threads <integer> Parallel encoding (uses slices)\n"
269 " --no-asm Disable all CPU optimizations\n"
270 " --no-psnr Disable PSNR computation\n"
271 " --quiet Quiet Mode\n"
272 " -v, --verbose Print stats for each frame\n"
273 " --progress Show a progress indicator while encoding\n"
274 " --visualize Show MB types overlayed on the encoded video\n"
275 " --aud Use access unit delimiters\n"
277 X264_BUILD, X264_VERSION,
288 defaults->i_keyint_max,
289 defaults->i_keyint_min,
290 defaults->i_scenecut_threshold,
292 defaults->i_bframe_bias,
293 defaults->i_frame_reference,
294 defaults->i_deblocking_filter_alphac0,
295 defaults->i_deblocking_filter_beta,
296 defaults->rc.i_qp_constant,
297 defaults->rc.i_qp_min,
298 defaults->rc.i_qp_max,
299 defaults->rc.i_qp_step,
300 defaults->rc.f_rate_tolerance,
301 defaults->rc.i_vbv_max_bitrate,
302 defaults->rc.i_vbv_buffer_size,
303 defaults->rc.f_vbv_buffer_init,
304 defaults->rc.f_ip_factor,
305 defaults->rc.f_pb_factor,
306 defaults->analyse.i_chroma_qp_offset,
307 defaults->rc.psz_stat_out,
308 defaults->rc.psz_rc_eq,
309 defaults->rc.f_qcompress,
310 defaults->rc.f_complexity_blur,
311 defaults->rc.f_qblur,
312 defaults->analyse.i_me_method==X264_ME_DIA ? "dia"
313 : defaults->analyse.i_me_method==X264_ME_HEX ? "hex"
314 : defaults->analyse.i_me_method==X264_ME_UMH ? "umh"
315 : defaults->analyse.i_me_method==X264_ME_ESA ? "esa" : NULL,
316 defaults->analyse.i_me_range,
317 defaults->analyse.i_subpel_refine
321 static int parse_cqm( const char *str, uint8_t *cqm, int length )
326 if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
329 } while( i < length && (str = strchr( str, ',' )) && str++ );
330 return (i == length) ? 0 : -1;
333 /*****************************************************************************
335 *****************************************************************************/
336 static int Parse( int argc, char **argv,
337 x264_param_t *param, cli_opt_t *opt )
339 char *psz_filename = NULL;
340 x264_param_t defaults = *param;
344 memset( opt, 0, sizeof(cli_opt_t) );
346 /* Default input file driver */
347 p_open_infile = open_file_yuv;
348 p_get_frame_total = get_frame_total_yuv;
349 p_read_frame = read_frame_yuv;
350 p_close_infile = close_file_yuv;
352 /* Default output file driver */
353 p_open_outfile = open_file_bsf;
354 p_set_outfile_param = set_param_bsf;
355 p_write_nalu = write_nalu_bsf;
356 p_set_eop = set_eop_bsf;
357 p_close_outfile = close_file_bsf;
359 /* Parse command line options */
360 opterr = 0; // no error message
364 int long_options_index;
365 #define OPT_QPMIN 256
366 #define OPT_QPMAX 257
367 #define OPT_QPSTEP 258
368 #define OPT_IPRATIO 260
369 #define OPT_PBRATIO 261
370 #define OPT_RATETOL 262
371 #define OPT_RCSTATS 264
373 #define OPT_QCOMP 266
374 #define OPT_NOPSNR 267
375 #define OPT_QUIET 268
376 #define OPT_SCENECUT 270
377 #define OPT_QBLUR 271
378 #define OPT_CPLXBLUR 272
379 #define OPT_FRAMES 273
381 #define OPT_DIRECT 275
382 #define OPT_LEVEL 276
383 #define OPT_NOBADAPT 277
384 #define OPT_BBIAS 278
385 #define OPT_BPYRAMID 279
386 #define OPT_CHROMA_QP 280
387 #define OPT_NO_CHROMA_ME 281
388 #define OPT_NO_CABAC 282
390 #define OPT_PROGRESS 284
392 #define OPT_MERANGE 286
393 #define OPT_VBVMAXRATE 287
394 #define OPT_VBVBUFSIZE 288
395 #define OPT_VBVINIT 289
396 #define OPT_VISUALIZE 290
398 #define OPT_ZONES 292
399 #define OPT_THREADS 293
402 #define OPT_CQM4I 296
403 #define OPT_CQM4IY 297
404 #define OPT_CQM4IC 298
405 #define OPT_CQM4P 299
406 #define OPT_CQM4PY 300
407 #define OPT_CQM4PC 301
409 #define OPT_CQM8I 303
410 #define OPT_CQM8P 304
411 #define OPT_CQMFILE 305
413 static struct option long_options[] =
415 { "help", no_argument, NULL, 'h' },
416 { "bitrate", required_argument, NULL, 'B' },
417 { "bframes", required_argument, NULL, 'b' },
418 { "no-b-adapt", no_argument, NULL, OPT_NOBADAPT },
419 { "b-bias", required_argument, NULL, OPT_BBIAS },
420 { "b-pyramid", no_argument, NULL, OPT_BPYRAMID },
421 { "min-keyint",required_argument,NULL,'i' },
422 { "keyint", required_argument, NULL, 'I' },
423 { "scenecut",required_argument, NULL, OPT_SCENECUT },
424 { "nf", no_argument, NULL, 'n' },
425 { "filter", required_argument, NULL, 'f' },
426 { "no-cabac",no_argument, NULL, OPT_NO_CABAC },
427 { "qp", required_argument, NULL, 'q' },
428 { "qpmin", required_argument, NULL, OPT_QPMIN },
429 { "qpmax", required_argument, NULL, OPT_QPMAX },
430 { "qpstep", required_argument, NULL, OPT_QPSTEP },
431 { "ref", required_argument, NULL, 'r' },
432 { "no-asm", no_argument, NULL, 'C' },
433 { "sar", required_argument, NULL, 's' },
434 { "fps", required_argument, NULL, OPT_FPS },
435 { "frames", required_argument, NULL, OPT_FRAMES },
436 { "seek", required_argument, NULL, OPT_SEEK },
437 { "output", required_argument, NULL, 'o' },
438 { "analyse", required_argument, NULL, 'A' },
439 { "direct", required_argument, NULL, OPT_DIRECT },
440 { "weightb", no_argument, NULL, 'w' },
441 { "me", required_argument, NULL, OPT_ME },
442 { "merange", required_argument, NULL, OPT_MERANGE },
443 { "subme", required_argument, NULL, 'm' },
444 { "no-chroma-me", no_argument, NULL, OPT_NO_CHROMA_ME },
445 { "8x8dct", no_argument, NULL, '8' },
446 { "level", required_argument, NULL, OPT_LEVEL },
447 { "ratetol", required_argument, NULL, OPT_RATETOL },
448 { "vbv-maxrate", required_argument, NULL, OPT_VBVMAXRATE },
449 { "vbv-bufsize", required_argument, NULL, OPT_VBVBUFSIZE },
450 { "vbv-init", required_argument,NULL, OPT_VBVINIT },
451 { "ipratio", required_argument, NULL, OPT_IPRATIO },
452 { "pbratio", required_argument, NULL, OPT_PBRATIO },
453 { "chroma-qp-offset", required_argument, NULL, OPT_CHROMA_QP },
454 { "pass", required_argument, NULL, 'p' },
455 { "stats", required_argument, NULL, OPT_RCSTATS },
456 { "rceq", required_argument, NULL, OPT_RCEQ },
457 { "qcomp", required_argument, NULL, OPT_QCOMP },
458 { "qblur", required_argument, NULL, OPT_QBLUR },
459 { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
460 { "zones", required_argument, NULL, OPT_ZONES },
461 { "threads", required_argument, NULL, OPT_THREADS },
462 { "no-psnr", no_argument, NULL, OPT_NOPSNR },
463 { "quiet", no_argument, NULL, OPT_QUIET },
464 { "verbose", no_argument, NULL, 'v' },
465 { "progress",no_argument, NULL, OPT_PROGRESS },
466 { "visualize",no_argument, NULL, OPT_VISUALIZE },
467 { "aud", no_argument, NULL, OPT_AUD },
468 { "cqm", required_argument, NULL, OPT_CQM },
469 { "cqmfile", required_argument, NULL, OPT_CQMFILE },
470 { "cqm4", required_argument, NULL, OPT_CQM4 },
471 { "cqm4i", required_argument, NULL, OPT_CQM4I },
472 { "cqm4iy", required_argument, NULL, OPT_CQM4IY },
473 { "cqm4ic", required_argument, NULL, OPT_CQM4IC },
474 { "cqm4p", required_argument, NULL, OPT_CQM4P },
475 { "cqm4py", required_argument, NULL, OPT_CQM4PY },
476 { "cqm4pc", required_argument, NULL, OPT_CQM4PC },
477 { "cqm8", required_argument, NULL, OPT_CQM8 },
478 { "cqm8i", required_argument, NULL, OPT_CQM8I },
479 { "cqm8p", required_argument, NULL, OPT_CQM8P },
485 c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:vw8",
486 long_options, &long_options_index);
502 param->rc.i_bitrate = atol( optarg );
506 param->i_bframe = atol( optarg );
509 param->b_bframe_adaptive = 0;
512 param->i_bframe_bias = atol( optarg );
515 param->b_bframe_pyramid = 1;
518 param->i_keyint_min = atol( optarg );
519 if( param->i_keyint_max < param->i_keyint_min )
520 param->i_keyint_max = param->i_keyint_min;
523 param->i_keyint_max = atol( optarg );
524 if( param->i_keyint_min > param->i_keyint_max )
525 param->i_keyint_min = param->i_keyint_max;
528 param->i_scenecut_threshold = atol( optarg );
531 param->b_deblocking_filter = 0;
535 char *p = strchr( optarg, ':' );
536 param->i_deblocking_filter_alphac0 = atoi( optarg );
537 param->i_deblocking_filter_beta = p ? atoi( p+1 ) : param->i_deblocking_filter_alphac0;
541 param->rc.i_qp_constant = atoi( optarg );
544 param->rc.i_qp_min = atoi( optarg );
547 param->rc.i_qp_max = atoi( optarg );
550 param->rc.i_qp_step = atoi( optarg );
553 param->i_frame_reference = atoi( optarg );
559 opt->b_decompress = 1;
565 opt->i_maxframes = atoi( optarg );
568 opt->i_seek = atoi( optarg );
571 if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
574 p_open_outfile = open_file_mp4;
575 p_write_nalu = write_nalu_mp4;
576 p_set_outfile_param = set_param_mp4;
577 p_set_eop = set_eop_mp4;
578 p_close_outfile = close_file_mp4;
580 fprintf( stderr, "not compiled with MP4 output support\n" );
584 else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
586 p_open_outfile = open_file_mkv;
587 p_write_nalu = write_nalu_mkv;
588 p_set_outfile_param = set_param_mkv;
589 p_set_eop = set_eop_mkv;
590 p_close_outfile = close_file_mkv;
592 if( !strcmp(optarg, "-") )
594 else if( p_open_outfile( optarg, &opt->hout ) )
596 fprintf( stderr, "cannot open output file `%s'\n", optarg );
602 char *p = strchr( optarg, ':' );
605 param->vui.i_sar_width = atoi( optarg );
606 param->vui.i_sar_height = atoi( p + 1 );
613 if( sscanf( optarg, "%d/%d", ¶m->i_fps_num, ¶m->i_fps_den ) == 2 )
615 else if( sscanf( optarg, "%f", &fps ) )
617 param->i_fps_num = (int)(fps * 1000 + .5);
618 param->i_fps_den = 1000;
622 fprintf( stderr, "bad fps `%s'\n", optarg );
628 param->analyse.inter = 0;
629 if( strstr( optarg, "none" ) ) param->analyse.inter = 0;
630 if( strstr( optarg, "all" ) ) param->analyse.inter = ~0;
632 if( strstr( optarg, "i4x4" ) ) param->analyse.inter |= X264_ANALYSE_I4x4;
633 if( strstr( optarg, "i8x8" ) ) param->analyse.inter |= X264_ANALYSE_I8x8;
634 if( strstr( optarg, "p8x8" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
635 if( strstr( optarg, "p4x4" ) ) param->analyse.inter |= X264_ANALYSE_PSUB8x8;
636 if( strstr( optarg, "b8x8" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16;
639 if( strstr( optarg, "temporal" ) )
640 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
641 else if( strstr( optarg, "spatial" ) )
642 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
643 else if( strstr( optarg, "none" ) )
644 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
646 param->analyse.i_direct_mv_pred = atoi( optarg );
649 param->analyse.b_weighted_bipred = 1;
652 param->analyse.i_me_method =
653 strstr( optarg, "dia" ) ? X264_ME_DIA :
654 strstr( optarg, "hex" ) ? X264_ME_HEX :
655 strstr( optarg, "umh" ) ? X264_ME_UMH :
656 strstr( optarg, "esa" ) ? X264_ME_ESA : -1;
657 if( param->analyse.i_me_method == -1 )
659 fprintf( stderr, "bad ME method `%s'\n", optarg );
664 param->analyse.i_me_range = atoi(optarg);
667 param->analyse.i_subpel_refine = atoi(optarg);
669 case OPT_NO_CHROMA_ME:
670 param->analyse.b_chroma_me = 0;
673 param->analyse.b_transform_8x8 = 1;
676 param->i_level_idc = atoi(optarg);
679 param->rc.f_rate_tolerance = !strncmp("inf", optarg, 3) ? 1e9 : atof(optarg);
682 param->rc.i_vbv_max_bitrate = atoi( optarg );
685 param->rc.i_vbv_buffer_size = atoi( optarg );
688 param->rc.f_vbv_buffer_init = atof(optarg);
691 param->rc.f_ip_factor = atof(optarg);
694 param->rc.f_pb_factor = atof(optarg);
697 param->analyse.i_chroma_qp_offset = atoi(optarg);
701 int i_pass = atoi(optarg);
703 param->rc.b_stat_write = 1;
704 else if( i_pass == 2 )
705 param->rc.b_stat_read = 1;
706 else if( i_pass > 2 )
707 param->rc.b_stat_read =
708 param->rc.b_stat_write = 1;
712 param->rc.psz_stat_in = optarg;
713 param->rc.psz_stat_out = optarg;
716 param->rc.psz_rc_eq = optarg;
719 param->rc.f_qcompress = atof(optarg);
722 param->rc.f_qblur = atof(optarg);
725 param->rc.f_complexity_blur = atof(optarg);
728 param->rc.psz_zones = optarg;
731 param->i_threads = atoi(optarg);
734 param->analyse.b_psnr = 0;
737 param->i_log_level = X264_LOG_NONE;
740 param->i_log_level = X264_LOG_DEBUG;
750 param->b_visualize = 1;
751 b_exit_on_ctrl_c = 1;
753 fprintf( stderr, "not compiled with visualization support\n" );
757 if( strstr( optarg, "flat" ) )
758 param->i_cqm_preset = X264_CQM_FLAT;
759 else if( strstr( optarg, "jvt" ) )
760 param->i_cqm_preset = X264_CQM_JVT;
763 fprintf( stderr, "bad CQM preset `%s'\n", optarg );
768 param->psz_cqm_file = optarg;
771 param->i_cqm_preset = X264_CQM_CUSTOM;
772 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
773 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
774 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
775 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
778 param->i_cqm_preset = X264_CQM_CUSTOM;
779 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
780 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
783 param->i_cqm_preset = X264_CQM_CUSTOM;
784 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
785 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
788 param->i_cqm_preset = X264_CQM_CUSTOM;
789 b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
790 b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
793 param->i_cqm_preset = X264_CQM_CUSTOM;
794 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
797 param->i_cqm_preset = X264_CQM_CUSTOM;
798 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
801 param->i_cqm_preset = X264_CQM_CUSTOM;
802 b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
805 param->i_cqm_preset = X264_CQM_CUSTOM;
806 b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
809 param->i_cqm_preset = X264_CQM_CUSTOM;
810 b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
813 param->i_cqm_preset = X264_CQM_CUSTOM;
814 b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
817 fprintf( stderr, "unknown option (%c)\n", optopt );
823 fprintf( stderr, "bad argument (%s)\n", optarg );
828 /* Get the file name */
829 if( optind > argc - 1 || !opt->hout )
834 psz_filename = argv[optind++];
836 if( !opt->b_decompress )
838 if( optind > argc - 1 )
840 /* try to parse the file name */
841 for( psz = psz_filename; *psz; psz++ )
843 if( *psz >= '0' && *psz <= '9'
844 && sscanf( psz, "%ux%u", ¶m->i_width, ¶m->i_height ) == 2 )
846 if( param->i_log_level >= X264_LOG_INFO )
847 fprintf( stderr, "x264 [info]: file name gives %dx%d\n", param->i_width, param->i_height );
854 sscanf( argv[optind++], "%ux%u", ¶m->i_width, ¶m->i_height );
857 /* check avis input */
858 psz = psz_filename + strlen(psz_filename) - 1;
859 while( psz > psz_filename && *psz != '.' )
862 if( !strncasecmp( psz, ".avi", 4 ) || !strncasecmp( psz, ".avs", 4 ) )
865 if( !b_avis && ( !param->i_width || !param->i_height ) )
873 if( !strcmp( psz_filename, "-" ) )
883 p_open_infile = open_file_avis;
884 p_get_frame_total = get_frame_total_avis;
885 p_read_frame = read_frame_avis;
886 p_close_infile = close_file_avis;
888 fprintf( stderr, "not compiled with AVIS input support\n" );
892 if( p_open_infile( psz_filename, &opt->hin, param ) )
894 fprintf( stderr, "could not open input file '%s'\n", psz_filename );
902 /*****************************************************************************
904 *****************************************************************************/
906 static int Decode( x264_param_t *param, FILE *fh26l, hnd_t hout )
908 fprintf( stderr, "decompressor not working (help is welcome)\n" );
915 if( ( h = x264_decoder_open( ¶m ) ) == NULL )
917 fprintf( stderr, "x264_decoder_open failed\n" );
921 i_start = x264_mdate();
925 nal.p_payload = malloc( DATA_MAX );
929 uint8_t *p, *p_next, *end;
932 if( i_data < DATA_MAX && !b_eof )
934 int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );
952 /* extract one nal */
956 if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
965 fprintf( stderr, "garbage (i_data = %d)\n", i_data );
971 while( p_next < end - 3 )
973 if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
980 if( p_next == end - 3 && i_data < DATA_MAX )
985 /* decode this nal */
986 i_size = p_next - p - 3;
993 fprintf( stderr, "nal too large (FIXME) ?\n" );
998 x264_nal_decode( &nal, p +3, i_size );
1000 /* decode the content of the nal */
1001 x264_decoder_decode( h, &pic, &nal );
1009 for( i = 0; i < pic->i_plane;i++ )
1014 i_div = i==0 ? 1 : 2;
1015 for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )
1017 fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, hout );
1022 memmove( &data[0], p_next, end - p_next );
1023 i_data -= p_next - &data[0];
1026 i_end = x264_mdate();
1027 free( nal.p_payload );
1028 fprintf( stderr, "\n" );
1030 x264_decoder_close( h );
1033 if( hout != stdout )
1039 double fps = (double)i_frame * (double)1000000 /
1040 (double)( i_end - i_start );
1041 fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );
1046 static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
1048 x264_picture_t pic_out;
1053 /* Do not force any parameters */
1056 pic->i_type = X264_TYPE_AUTO;
1059 if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
1061 fprintf( stderr, "x264_encoder_encode failed\n" );
1064 for( i = 0; i < i_nal; i++ )
1070 if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
1072 i_file += p_write_nalu( hout, data, i_size );
1074 else if( i_size < 0 )
1076 fprintf( stderr, "need to increase buffer size (size=%d)\n", -i_size );
1080 p_set_eop( hout, &pic_out );
1085 /*****************************************************************************
1087 *****************************************************************************/
1088 static int Encode( x264_param_t *param, cli_opt_t *opt )
1093 int i_frame, i_frame_total;
1094 int64_t i_start, i_end;
1099 i_frame_total = p_get_frame_total( opt->hin, param->i_width, param->i_height );
1101 if( ( h = x264_encoder_open( param ) ) == NULL )
1103 fprintf( stderr, "x264_encoder_open failed\n" );
1104 p_close_infile( opt->hin );
1105 p_close_outfile( opt->hout );
1109 if( p_set_outfile_param( opt->hout, param ) )
1111 fprintf( stderr, "can't set outfile param\n" );
1112 p_close_infile( opt->hin );
1113 p_close_outfile( opt->hout );
1117 /* Create a new pic */
1118 x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
1120 i_start = x264_mdate();
1122 i_frame_total -= opt->i_seek;
1123 if( opt->i_maxframes > 0 && opt->i_maxframes < i_frame_total )
1124 i_frame_total = opt->i_maxframes;
1125 for( i_frame = 0, i_file = 0, i_progress = 0;
1126 b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
1128 if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek, param->i_width, param->i_height ) )
1131 pic.i_pts = (int64_t)i_frame * param->i_fps_den;
1133 i_file += Encode_frame( h, opt->hout, &pic );
1137 /* update status line (up to 1000 times per input file) */
1138 if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&
1139 ( i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
1140 : i_frame % 10 == 0 ) )
1142 int64_t i_elapsed = x264_mdate() - i_start;
1143 double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
1146 i_progress = i_frame * 1000 / i_frame_total;
1147 fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps \r", i_frame,
1148 i_frame_total, (float)i_progress / 10, fps );
1151 fprintf( stderr, "encoded frames: %d, %.2f fps \r", i_frame, fps );
1152 fflush( stderr ); // needed in windows
1155 /* Flush delayed B-frames */
1158 i_frame_size = Encode_frame( h, opt->hout, NULL );
1159 } while( i_frame_size );
1161 i_end = x264_mdate();
1162 x264_picture_clean( &pic );
1163 x264_encoder_close( h );
1164 fprintf( stderr, "\n" );
1167 fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );
1169 p_close_infile( opt->hin );
1170 p_close_outfile( opt->hout );
1174 double fps = (double)i_frame * (double)1000000 /
1175 (double)( i_end - i_start );
1177 fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,
1178 (double) i_file * 8 * param->i_fps_num / ( param->i_fps_den * i_frame * 1000 ) );
1184 /* raw 420 yuv file operation */
1185 static int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
1187 if ((*p_handle = fopen(psz_filename, "rb")) == NULL)
1192 static int get_frame_total_yuv( hnd_t handle, int i_width, int i_height )
1194 FILE *f = (FILE *)handle;
1195 int i_frame_total = 0;
1197 if( !fseek( f, 0, SEEK_END ) )
1199 int64_t i_size = ftell( f );
1200 fseek( f, 0, SEEK_SET );
1201 i_frame_total = (int)(i_size / ( i_width * i_height * 3 / 2 ));
1204 return i_frame_total;
1207 static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height )
1209 static int prev_frame = -1;
1210 FILE *f = (FILE *)handle;
1212 if( i_frame != prev_frame+1 )
1213 if( fseek( f, i_frame * i_width * i_height * 3 / 2, SEEK_SET ) )
1216 if( fread( p_pic->img.plane[0], 1, i_width * i_height, f ) <= 0
1217 || fread( p_pic->img.plane[1], 1, i_width * i_height / 4, f ) <= 0
1218 || fread( p_pic->img.plane[2], 1, i_width * i_height / 4, f ) <= 0 )
1221 prev_frame = i_frame;
1226 static int close_file_yuv(hnd_t handle)
1230 return fclose((FILE *)handle);
1234 /* avs/avi input file support under cygwin */
1238 static int gcd(int a, int b)
1252 static int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
1255 PAVISTREAM p_avi = NULL;
1261 if( AVIStreamOpenFromFile( &p_avi, psz_filename, streamtypeVIDEO, 0, OF_READ, NULL ) )
1267 if( AVIStreamInfo(p_avi, &info, sizeof(AVISTREAMINFO)) )
1269 AVIStreamRelease(p_avi);
1274 // check input format
1275 if (info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2'))
1277 fprintf( stderr, "avis [error]: unsupported input format (%c%c%c%c)\n",
1278 (char)(info.fccHandler & 0xff), (char)((info.fccHandler >> 8) & 0xff),
1279 (char)((info.fccHandler >> 16) & 0xff), (char)((info.fccHandler >> 24)) );
1281 AVIStreamRelease(p_avi);
1287 p_param->i_width = info.rcFrame.right - info.rcFrame.left;
1288 p_param->i_height = info.rcFrame.bottom - info.rcFrame.top;
1289 i = gcd(info.dwRate, info.dwScale);
1290 p_param->i_fps_den = info.dwScale / i;
1291 p_param->i_fps_num = info.dwRate / i;
1293 fprintf( stderr, "avis [info]: %dx%d @ %.2f fps (%d frames)\n",
1294 p_param->i_width, p_param->i_height,
1295 (double)p_param->i_fps_num / (double)p_param->i_fps_den,
1296 (int)info.dwLength );
1298 *p_handle = (hnd_t)p_avi;
1303 static int get_frame_total_avis( hnd_t handle, int i_width, int i_height )
1305 PAVISTREAM p_avi = (PAVISTREAM)handle;
1308 if( AVIStreamInfo(p_avi, &info, sizeof(AVISTREAMINFO)) )
1311 return info.dwLength;
1314 static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height )
1316 PAVISTREAM p_avi = (PAVISTREAM)handle;
1318 p_pic->img.i_csp = X264_CSP_YV12;
1320 if( AVIStreamRead(p_avi, i_frame, 1, p_pic->img.plane[0], i_width * i_height * 3 / 2, NULL, NULL ) )
1326 static int close_file_avis( hnd_t handle )
1328 PAVISTREAM p_avi = (PAVISTREAM)handle;
1330 AVIStreamRelease(p_avi);
1339 static int open_file_bsf( char *psz_filename, hnd_t *p_handle )
1341 if ((*p_handle = fopen(psz_filename, "w+b")) == NULL)
1347 static int set_param_bsf( hnd_t handle, x264_param_t *p_param )
1352 static int write_nalu_bsf( hnd_t handle, uint8_t *p_nalu, int i_size )
1354 if (fwrite(p_nalu, i_size, 1, (FILE *)handle) > 0)
1359 static int set_eop_bsf( hnd_t handle, x264_picture_t *p_picture )
1364 static int close_file_bsf( hnd_t handle )
1366 if ((handle == NULL) || (handle == stdout))
1369 return fclose((FILE *)handle);
1372 /* -- mp4 muxing support ------------------------------------------------- */
1378 GF_AVCConfig *p_config;
1379 GF_ISOSample *p_sample;
1391 static void recompute_bitrate_mp4(GF_ISOFile *p_file, int i_track)
1393 u32 i, count, di, timescale, time_wnd, rate;
1398 esd = gf_isom_get_esd(p_file, i_track, 1);
1401 esd->decoderConfig->avgBitrate = 0;
1402 esd->decoderConfig->maxBitrate = 0;
1403 rate = time_wnd = 0;
1405 timescale = gf_isom_get_media_timescale(p_file, i_track);
1406 count = gf_isom_get_sample_count(p_file, i_track);
1407 for (i=0; i<count; i++) {
1408 GF_ISOSample *samp = gf_isom_get_sample_info(p_file, i_track, i+1, &di, &offset);
1410 if (samp->dataLength>esd->decoderConfig->bufferSizeDB) esd->decoderConfig->bufferSizeDB = samp->dataLength;
1412 if (esd->decoderConfig->bufferSizeDB < samp->dataLength) esd->decoderConfig->bufferSizeDB = samp->dataLength;
1413 esd->decoderConfig->avgBitrate += samp->dataLength;
1414 rate += samp->dataLength;
1415 if (samp->DTS > time_wnd + timescale) {
1416 if (rate > esd->decoderConfig->maxBitrate) esd->decoderConfig->maxBitrate = rate;
1417 time_wnd = samp->DTS;
1421 gf_isom_sample_del(&samp);
1424 br = (Double) (s64) gf_isom_get_media_duration(p_file, i_track);
1426 esd->decoderConfig->avgBitrate = (u32) (esd->decoderConfig->avgBitrate / br);
1428 esd->decoderConfig->avgBitrate *= 8;
1429 esd->decoderConfig->maxBitrate *= 8;
1431 gf_isom_change_mpeg4_description(p_file, i_track, 1, esd);
1432 gf_odf_desc_del((GF_Descriptor *) esd);
1436 static int close_file_mp4( hnd_t handle )
1438 mp4_t *p_mp4 = (mp4_t *)handle;
1443 if (p_mp4->p_config)
1444 gf_odf_avc_cfg_del(p_mp4->p_config);
1446 if (p_mp4->p_sample)
1448 if (p_mp4->p_sample->data)
1449 free(p_mp4->p_sample->data);
1451 gf_isom_sample_del(&p_mp4->p_sample);
1456 recompute_bitrate_mp4(p_mp4->p_file, p_mp4->i_track);
1457 gf_isom_set_pl_indication(p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15);
1458 gf_isom_set_storage_mode(p_mp4->p_file, GF_ISOM_STORE_FLAT);
1459 gf_isom_close(p_mp4->p_file);
1467 static int open_file_mp4( char *psz_filename, hnd_t *p_handle )
1473 if ((p_mp4 = (mp4_t *)malloc(sizeof(mp4_t))) == NULL)
1476 memset(p_mp4, 0, sizeof(mp4_t));
1477 p_mp4->p_file = gf_isom_open(psz_filename, GF_ISOM_OPEN_WRITE, NULL);
1479 if ((p_mp4->p_sample = gf_isom_sample_new()) == NULL)
1481 close_file_mp4( p_mp4 );
1485 gf_isom_set_brand_info(p_mp4->p_file, GF_ISOM_BRAND_AVC1, 0);
1493 static int set_param_mp4( hnd_t handle, x264_param_t *p_param )
1495 mp4_t *p_mp4 = (mp4_t *)handle;
1497 p_mp4->i_track = gf_isom_new_track(p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
1498 p_param->i_fps_num);
1500 p_mp4->p_config = gf_odf_avc_cfg_new();
1501 gf_isom_avc_config_new(p_mp4->p_file, p_mp4->i_track, p_mp4->p_config,
1502 NULL, NULL, &p_mp4->i_descidx);
1504 gf_isom_set_visual_info(p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx,
1505 p_param->i_width, p_param->i_height);
1507 p_mp4->p_sample->data = (char *)malloc(p_param->i_width * p_param->i_height * 3 / 2);
1508 if (p_mp4->p_sample->data == NULL)
1511 p_mp4->i_time_res = p_param->i_fps_num;
1512 p_mp4->i_time_inc = p_param->i_fps_den;
1513 p_mp4->i_init_delay = p_param->i_bframe ? (p_param->b_bframe_pyramid ? 2 : 1) : 0;
1514 p_mp4->i_init_delay *= p_mp4->i_time_inc;
1515 fprintf(stderr, "mp4 [info]: initial delay %d (scale %d)\n",
1516 p_mp4->i_init_delay, p_mp4->i_time_res);
1522 static int write_nalu_mp4( hnd_t handle, uint8_t *p_nalu, int i_size )
1524 mp4_t *p_mp4 = (mp4_t *)handle;
1525 GF_AVCConfigSlot *p_slot;
1526 uint8_t type = p_nalu[4] & 0x1f;
1535 p_mp4->p_config->configurationVersion = 1;
1536 p_mp4->p_config->AVCProfileIndication = p_nalu[5];
1537 p_mp4->p_config->profile_compatibility = p_nalu[6];
1538 p_mp4->p_config->AVCLevelIndication = p_nalu[7];
1539 p_slot = (GF_AVCConfigSlot *)malloc(sizeof(GF_AVCConfigSlot));
1540 p_slot->size = i_size - 4;
1541 p_slot->data = (char *)malloc(p_slot->size);
1542 memcpy(p_slot->data, p_nalu + 4, i_size - 4);
1543 gf_list_add(p_mp4->p_config->sequenceParameterSets, p_slot);
1553 p_slot = (GF_AVCConfigSlot *)malloc(sizeof(GF_AVCConfigSlot));
1554 p_slot->size = i_size - 4;
1555 p_slot->data = (char *)malloc(p_slot->size);
1556 memcpy(p_slot->data, p_nalu + 4, i_size - 4);
1557 gf_list_add(p_mp4->p_config->pictureParameterSets, p_slot);
1561 gf_isom_avc_config_update(p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config);
1569 psize = i_size - 4 ;
1570 memcpy(p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size);
1571 p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 0] = (psize >> 24) & 0xff;
1572 p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 1] = (psize >> 16) & 0xff;
1573 p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 2] = (psize >> 8) & 0xff;
1574 p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 3] = (psize >> 0) & 0xff;
1575 p_mp4->p_sample->dataLength += i_size;
1582 static int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture )
1584 mp4_t *p_mp4 = (mp4_t *)handle;
1585 uint32_t dts = p_mp4->i_numframe * p_mp4->i_time_inc;
1586 uint32_t pts = p_picture->i_pts;
1587 int offset = p_mp4->i_init_delay + pts - dts;
1589 p_mp4->p_sample->IsRAP = p_picture->i_type == X264_TYPE_IDR ? 1 : 0;
1590 p_mp4->p_sample->DTS = dts;
1591 p_mp4->p_sample->CTS_Offset = offset;
1592 gf_isom_add_sample(p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample);
1594 p_mp4->p_sample->dataLength = 0;
1595 p_mp4->i_numframe++;
1603 /* -- mkv muxing support ------------------------------------------------- */
1609 int sps_len, pps_len;
1611 int width, height, d_width, d_height;
1613 int64_t frame_duration;
1616 int b_header_written;
1617 char b_writing_frame;
1620 static int write_header_mkv( mkv_t *p_mkv )
1626 if( p_mkv->sps == NULL || p_mkv->pps == NULL ||
1627 p_mkv->width == 0 || p_mkv->height == 0 ||
1628 p_mkv->d_width == 0 || p_mkv->d_height == 0)
1631 avcC_len = 5 + 1 + 2 + p_mkv->sps_len + 1 + 2 + p_mkv->pps_len;
1632 avcC = malloc(avcC_len);
1637 avcC[1] = p_mkv->sps[1];
1638 avcC[2] = p_mkv->sps[2];
1639 avcC[3] = p_mkv->sps[3];
1640 avcC[4] = 0xfe; // nalu size length is three bytes
1641 avcC[5] = 0xe1; // one sps
1643 avcC[6] = p_mkv->sps_len >> 8;
1644 avcC[7] = p_mkv->sps_len;
1646 memcpy(avcC+8, p_mkv->sps, p_mkv->sps_len);
1648 avcC[8+p_mkv->sps_len] = 1; // one pps
1649 avcC[9+p_mkv->sps_len] = p_mkv->pps_len >> 8;
1650 avcC[10+p_mkv->sps_len] = p_mkv->pps_len;
1652 memcpy( avcC+11+p_mkv->sps_len, p_mkv->pps, p_mkv->pps_len );
1654 ret = mk_writeHeader( p_mkv->w, "x264", "V_MPEG4/ISO/AVC",
1655 avcC, avcC_len, p_mkv->frame_duration, 50000,
1656 p_mkv->width, p_mkv->height,
1657 p_mkv->d_width, p_mkv->d_height );
1661 p_mkv->b_header_written = 1;
1666 static int open_file_mkv( char *psz_filename, hnd_t *p_handle )
1672 p_mkv = malloc(sizeof(*p_mkv));
1676 memset(p_mkv, 0, sizeof(*p_mkv));
1678 p_mkv->w = mk_createWriter(psz_filename);
1679 if (p_mkv->w == NULL)
1690 static int set_param_mkv( hnd_t handle, x264_param_t *p_param )
1692 mkv_t *p_mkv = handle;
1695 if( p_param->i_fps_num > 0 )
1697 p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
1698 (int64_t)1000000000 / p_param->i_fps_num;
1699 p_mkv->fps_num = p_param->i_fps_num;
1703 p_mkv->frame_duration = 0;
1707 p_mkv->width = p_param->i_width;
1708 p_mkv->height = p_param->i_height;
1710 if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
1712 dw = (int64_t)p_param->i_width * p_param->vui.i_sar_width;
1713 dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height;
1717 dw = p_param->i_width;
1718 dh = p_param->i_height;
1721 if( dw > 0 && dh > 0 )
1723 int64_t a = dw, b = dh;
1738 p_mkv->d_width = (int)dw;
1739 p_mkv->d_height = (int)dh;
1744 static int write_nalu_mkv( hnd_t handle, uint8_t *p_nalu, int i_size )
1746 mkv_t *p_mkv = handle;
1747 uint8_t type = p_nalu[4] & 0x1f;
1757 p_mkv->sps = malloc(i_size - 4);
1758 if (p_mkv->sps == NULL)
1760 p_mkv->sps_len = i_size - 4;
1761 memcpy(p_mkv->sps, p_nalu + 4, i_size - 4);
1769 p_mkv->pps = malloc(i_size - 4);
1770 if (p_mkv->pps == NULL)
1772 p_mkv->pps_len = i_size - 4;
1773 memcpy(p_mkv->pps, p_nalu + 4, i_size - 4);
1781 if( !p_mkv->b_writing_frame )
1783 if( mk_startFrame(p_mkv->w) < 0 )
1785 p_mkv->b_writing_frame = 1;
1787 psize = i_size - 4 ;
1788 dsize[0] = psize >> 16;
1789 dsize[1] = psize >> 8;
1791 if( mk_addFrameData(p_mkv->w, dsize, 3) < 0 ||
1792 mk_addFrameData(p_mkv->w, p_nalu + 4, i_size - 4) < 0 )
1800 if( !p_mkv->b_header_written && p_mkv->pps && p_mkv->sps &&
1801 write_header_mkv(p_mkv) < 0 )
1807 static int set_eop_mkv( hnd_t handle, x264_picture_t *p_picture )
1809 mkv_t *p_mkv = handle;
1810 int64_t i_stamp = (int64_t)(p_picture->i_pts * 1e9 / p_mkv->fps_num);
1812 p_mkv->b_writing_frame = 0;
1814 return mk_setFrameFlags( p_mkv->w, i_stamp,
1815 p_picture->i_type == X264_TYPE_IDR );
1818 static int close_file_mkv( hnd_t handle )
1820 mkv_t *p_mkv = handle;
1828 ret = mk_close(p_mkv->w);