#include <gpac/m4_author.h>
#endif
+
#include "common/common.h"
#include "x264.h"
+
#ifndef _MSC_VER
#include "config.h"
#endif
+#include "matroska.h"
+
#define DATA_MAX 3000000
uint8_t data[DATA_MAX];
typedef void *hnd_t;
/* Ctrl-C handler */
-static int i_ctrl_c = 0;
+static int b_ctrl_c = 0;
+static int b_exit_on_ctrl_c = 0;
static void SigIntHandler( int a )
{
- i_ctrl_c = 1;
+ if( b_exit_on_ctrl_c )
+ exit(0);
+ b_ctrl_c = 1;
}
+typedef struct {
+ int b_decompress;
+ int b_progress;
+ int i_maxframes;
+ int i_seek;
+ hnd_t hin;
+ hnd_t hout;
+} cli_opt_t;
+
/* input file operation function pointers */
static int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
static int (*p_get_frame_total)( hnd_t handle, int i_width, int i_height );
-static int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height );
+static int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
static int (*p_close_infile)( hnd_t handle );
static int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
static int get_frame_total_yuv( hnd_t handle, int i_width, int i_height );
-static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height );
+static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
static int close_file_yuv( hnd_t handle );
#ifdef AVIS_INPUT
static int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
static int get_frame_total_avis( hnd_t handle, int i_width, int i_height );
-static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height );
+static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
static int close_file_avis( hnd_t handle );
#endif
static int close_file_mp4( hnd_t handle );
#endif
+static int open_file_mkv( char *psz_filename, hnd_t *p_handle );
+static int set_param_mkv( hnd_t handle, x264_param_t *p_param );
+static int write_nalu_mkv( hnd_t handle, uint8_t *p_nal, int i_size );
+static int set_eop_mkv( hnd_t handle, x264_picture_t *p_picture );
+static int close_file_mkv( hnd_t handle );
+
static void Help( x264_param_t *defaults );
-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 );
-static int Encode( x264_param_t *param, hnd_t hin, hnd_t hout, int b_progress );
-static int Decode( x264_param_t *param, FILE *fh26l, hnd_t hout );
+static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
+static int Encode( x264_param_t *param, cli_opt_t *opt );
/****************************************************************************
int main( int argc, char **argv )
{
x264_param_t param;
-
- hnd_t hout;
- hnd_t hin;
-
- int b_decompress;
- int b_progress;
- int i_ret;
+ cli_opt_t opt;
#ifdef _MSC_VER
_setmode(_fileno(stdin), _O_BINARY); /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
x264_param_default( ¶m );
/* Parse command line */
- if( Parse( argc, argv, ¶m, &hin, &hout, &b_decompress, &b_progress ) < 0 )
- {
+ if( Parse( argc, argv, ¶m, &opt ) < 0 )
return -1;
- }
/* Control-C handler */
signal( SIGINT, SigIntHandler );
- if( b_decompress )
- i_ret = Decode( ¶m, hin, hout );
- else
- i_ret = Encode( ¶m, hin, hout, b_progress );
-
- return i_ret;
+ return Encode( ¶m, &opt );
}
/*****************************************************************************
" or AVI or Avisynth if compiled with AVIS support (%s).\n"
"Outfile type is selected by filename:\n"
" .264 -> Raw bytestream\n"
+ " .mkv -> Matroska\n"
" .mp4 -> MP4 if compiled with GPAC support (%s)\n"
"\n"
"Options:\n"
"\n"
" -h, --help Print this help\n"
"\n"
+ "Frame-type options:\n"
+ "\n"
" -I, --keyint <integer> Maximum GOP size [%d]\n"
" -i, --min-keyint <integer> Minimum GOP size [%d]\n"
" --scenecut <integer> How aggressively to insert extra I-frames [%d]\n"
" --nf Disable loop filter\n"
" -f, --filter <alpha:beta> Loop filter AlphaC0 and Beta parameters [%d:%d]\n"
"\n"
- " -q, --qp <integer> Set QP [%d]\n"
+ "Ratecontrol:\n"
+ "\n"
+ " -q, --qp <integer> Set QP (0=lossless) [%d]\n"
" -B, --bitrate <integer> Set bitrate\n"
" --qpmin <integer> Set min QP [%d]\n"
" --qpmax <integer> Set max QP [%d]\n"
" --cplxblur <float> Reduce fluctuations in QP (before curve compression) [%.1f]\n"
" --qblur <float> Reduce fluctuations in QP (after curve compression) [%.1f]\n"
"\n"
- " -A, --analyse <string> Analyse options: [\"i4x4,p8x8,b8x8\"]\n"
- " - i4x4\n"
- " - p8x8, p4x4, b8x8\n"
+ " --zones <zone0>/<zone1>/...\n"
+ " Tweak the bitrate of some regions of the video\n"
+ " Each zone is of the form\n"
+ " <start frame>,<end frame>,<option>\n"
+ " where <option> is either\n"
+ " q=<integer> (force QP)\n"
+ " or b=<float> (bitrate multiplier)\n"
+ "\n"
+ "Analysis:\n"
+ "\n"
+ " -A, --analyse <string> Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
+ " - p8x8, p4x4, b8x8, i8x8, i4x4\n"
" - none, all\n"
+ " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n"
" --direct <string> Direct MV prediction mode [\"temporal\"]\n"
" - none, spatial, temporal\n"
" -w, --weightb Weighted prediction for B-frames\n"
" --me <string> Integer pixel motion estimation method [\"%s\"]\n"
" - dia: diamond search, radius 1 (fast)\n"
" - hex: hexagonal search, radius 2\n"
- " - esa: exhaustive search algorithm (slow)\n"
+ " - umh: uneven multi-hexagon search\n"
+ " - esa: exhaustive search (slow)\n"
" --merange <integer> Maximum motion vector search range [%d]\n"
- " -m, --subme <integer> Subpixel motion estimation quality: 1=fast, 5=best. [%d]\n"
+ " -m, --subme <integer> Subpixel motion estimation and partition\n"
+ " decision quality: 1=fast, 6=best. [%d]\n"
" --no-chroma-me Ignore chroma in motion estimation\n"
+ " -8, --8x8dct Adaptive spatial transform size\n"
+ "\n"
+ " --cqm <string> Preset quant matrices [\"flat\"]\n"
+ " - jvt, flat\n"
+ " --cqmfile <string> Read quant matrices from a JM-compatible file\n"
+ " Overrides any other --cqm* options.\n"
+ " --cqm4 <list> Set all 4x4 quant matrices\n"
+ " Takes a comma-separated list of 16 integers.\n"
+ " --cqm8 <list> Set all 8x8 quant matrices\n"
+ " Takes a comma-separated list of 64 integers.\n"
+ " --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
+ " Set both luma and chroma quant matrices\n"
+ " --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
+ " Set individual quant matrices\n"
+ "\n"
+ "Input/Output:\n"
"\n"
" --level <integer> Specify level (as defined by Annex A)\n"
" --sar width:height Specify Sample Aspect Ratio\n"
" --fps <float|rational> Specify framerate\n"
+ " --seek <integer> First frame to encode\n"
" --frames <integer> Maximum number of frames to encode\n"
" -o, --output Specify output file\n"
"\n"
+ " --threads <integer> Parallel encoding (uses slices)\n"
" --no-asm Disable all CPU optimizations\n"
" --no-psnr Disable PSNR computation\n"
" --quiet Quiet Mode\n"
" -v, --verbose Print stats for each frame\n"
" --progress Show a progress indicator while encoding\n"
+ " --visualize Show MB types overlayed on the encoded video\n"
" --aud Use access unit delimiters\n"
"\n",
X264_BUILD, X264_VERSION,
defaults->rc.f_qblur,
defaults->analyse.i_me_method==X264_ME_DIA ? "dia"
: defaults->analyse.i_me_method==X264_ME_HEX ? "hex"
+ : defaults->analyse.i_me_method==X264_ME_UMH ? "umh"
: defaults->analyse.i_me_method==X264_ME_ESA ? "esa" : NULL,
defaults->analyse.i_me_range,
defaults->analyse.i_subpel_refine
);
}
+static int parse_cqm( const char *str, uint8_t *cqm, int length )
+{
+ int i = 0;
+ do {
+ int coef;
+ if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
+ return -1;
+ cqm[i++] = coef;
+ } while( i < length && (str = strchr( str, ',' )) && str++ );
+ return (i == length) ? 0 : -1;
+}
+
/*****************************************************************************
* Parse:
*****************************************************************************/
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 )
+ x264_param_t *param, cli_opt_t *opt )
{
char *psz_filename = NULL;
x264_param_t defaults = *param;
char *psz;
char b_avis = 0;
- /* Default output */
- *p_hout = NULL;
- *p_hin = NULL;
- *pb_decompress = 0;
- *b_progress = 0;
+ memset( opt, 0, sizeof(cli_opt_t) );
/* Default input file driver */
p_open_infile = open_file_yuv;
opterr = 0; // no error message
for( ;; )
{
+ int b_error = 0;
int long_options_index;
#define OPT_QPMIN 256
#define OPT_QPMAX 257
#define OPT_VBVMAXRATE 287
#define OPT_VBVBUFSIZE 288
#define OPT_VBVINIT 289
+#define OPT_VISUALIZE 290
+#define OPT_SEEK 291
+#define OPT_ZONES 292
+#define OPT_THREADS 293
+#define OPT_CQM 294
+#define OPT_CQM4 295
+#define OPT_CQM4I 296
+#define OPT_CQM4IY 297
+#define OPT_CQM4IC 298
+#define OPT_CQM4P 299
+#define OPT_CQM4PY 300
+#define OPT_CQM4PC 301
+#define OPT_CQM8 302
+#define OPT_CQM8I 303
+#define OPT_CQM8P 304
+#define OPT_CQMFILE 305
static struct option long_options[] =
{
{ "sar", required_argument, NULL, 's' },
{ "fps", required_argument, NULL, OPT_FPS },
{ "frames", required_argument, NULL, OPT_FRAMES },
+ { "seek", required_argument, NULL, OPT_SEEK },
{ "output", required_argument, NULL, 'o' },
{ "analyse", required_argument, NULL, 'A' },
{ "direct", required_argument, NULL, OPT_DIRECT },
{ "merange", required_argument, NULL, OPT_MERANGE },
{ "subme", required_argument, NULL, 'm' },
{ "no-chroma-me", no_argument, NULL, OPT_NO_CHROMA_ME },
+ { "8x8dct", no_argument, NULL, '8' },
{ "level", required_argument, NULL, OPT_LEVEL },
{ "ratetol", required_argument, NULL, OPT_RATETOL },
{ "vbv-maxrate", required_argument, NULL, OPT_VBVMAXRATE },
{ "qcomp", required_argument, NULL, OPT_QCOMP },
{ "qblur", required_argument, NULL, OPT_QBLUR },
{ "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
+ { "zones", required_argument, NULL, OPT_ZONES },
+ { "threads", required_argument, NULL, OPT_THREADS },
{ "no-psnr", no_argument, NULL, OPT_NOPSNR },
{ "quiet", no_argument, NULL, OPT_QUIET },
{ "verbose", no_argument, NULL, 'v' },
{ "progress",no_argument, NULL, OPT_PROGRESS },
+ { "visualize",no_argument, NULL, OPT_VISUALIZE },
{ "aud", no_argument, NULL, OPT_AUD },
+ { "cqm", required_argument, NULL, OPT_CQM },
+ { "cqmfile", required_argument, NULL, OPT_CQMFILE },
+ { "cqm4", required_argument, NULL, OPT_CQM4 },
+ { "cqm4i", required_argument, NULL, OPT_CQM4I },
+ { "cqm4iy", required_argument, NULL, OPT_CQM4IY },
+ { "cqm4ic", required_argument, NULL, OPT_CQM4IC },
+ { "cqm4p", required_argument, NULL, OPT_CQM4P },
+ { "cqm4py", required_argument, NULL, OPT_CQM4PY },
+ { "cqm4pc", required_argument, NULL, OPT_CQM4PC },
+ { "cqm8", required_argument, NULL, OPT_CQM8 },
+ { "cqm8i", required_argument, NULL, OPT_CQM8I },
+ { "cqm8p", required_argument, NULL, OPT_CQM8P },
{0, 0, 0, 0}
};
int c;
- c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:vw",
+ c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:vw8",
long_options, &long_options_index);
if( c == -1 )
break;
case 'i':
param->i_keyint_min = atol( optarg );
+ if( param->i_keyint_max < param->i_keyint_min )
+ param->i_keyint_max = param->i_keyint_min;
break;
case 'I':
param->i_keyint_max = atol( optarg );
+ if( param->i_keyint_min > param->i_keyint_max )
+ param->i_keyint_min = param->i_keyint_max;
break;
case OPT_SCENECUT:
param->i_scenecut_threshold = atol( optarg );
param->b_cabac = 0;
break;
case 'x':
- *pb_decompress = 1;
+ opt->b_decompress = 1;
break;
case 'C':
param->cpu = 0;
break;
case OPT_FRAMES:
- param->i_maxframes = atoi( optarg );
+ opt->i_maxframes = atoi( optarg );
+ break;
+ case OPT_SEEK:
+ opt->i_seek = atoi( optarg );
break;
case'o':
if( !strncasecmp(optarg + strlen(optarg) - 4, ".mp4", 4) )
return -1;
#endif
}
+ else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
+ {
+ p_open_outfile = open_file_mkv;
+ p_write_nalu = write_nalu_mkv;
+ p_set_outfile_param = set_param_mkv;
+ p_set_eop = set_eop_mkv;
+ p_close_outfile = close_file_mkv;
+ }
if( !strcmp(optarg, "-") )
- *p_hout = stdout;
- else if( p_open_outfile( optarg, p_hout ) )
+ opt->hout = stdout;
+ else if( p_open_outfile( optarg, &opt->hout ) )
{
fprintf( stderr, "cannot open output file `%s'\n", optarg );
return -1;
}
case 'A':
param->analyse.inter = 0;
- if( strstr( optarg, "none" ) ) param->analyse.inter = 0x000000;
- if( strstr( optarg, "all" ) ) param->analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16;
+ if( strstr( optarg, "none" ) ) param->analyse.inter = 0;
+ if( strstr( optarg, "all" ) ) param->analyse.inter = ~0;
if( strstr( optarg, "i4x4" ) ) param->analyse.inter |= X264_ANALYSE_I4x4;
+ if( strstr( optarg, "i8x8" ) ) param->analyse.inter |= X264_ANALYSE_I8x8;
if( strstr( optarg, "p8x8" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
if( strstr( optarg, "p4x4" ) ) param->analyse.inter |= X264_ANALYSE_PSUB8x8;
if( strstr( optarg, "b8x8" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16;
param->analyse.b_weighted_bipred = 1;
break;
case OPT_ME:
- if( strstr( optarg, "dia" ) )
- param->analyse.i_me_method = X264_ME_DIA;
- else if( strstr( optarg, "hex" ) )
- param->analyse.i_me_method = X264_ME_HEX;
- else if( strstr( optarg, "esa" ) )
- param->analyse.i_me_method = X264_ME_ESA;
- else
+ param->analyse.i_me_method =
+ strstr( optarg, "dia" ) ? X264_ME_DIA :
+ strstr( optarg, "hex" ) ? X264_ME_HEX :
+ strstr( optarg, "umh" ) ? X264_ME_UMH :
+ strstr( optarg, "esa" ) ? X264_ME_ESA : -1;
+ if( param->analyse.i_me_method == -1 )
{
fprintf( stderr, "bad ME method `%s'\n", optarg );
return -1;
case OPT_NO_CHROMA_ME:
param->analyse.b_chroma_me = 0;
break;
+ case '8':
+ param->analyse.b_transform_8x8 = 1;
+ break;
case OPT_LEVEL:
param->i_level_idc = atoi(optarg);
break;
case OPT_RATETOL:
- param->rc.f_rate_tolerance = atof(optarg);
+ param->rc.f_rate_tolerance = !strncmp("inf", optarg, 3) ? 1e9 : atof(optarg);
break;
case OPT_VBVMAXRATE:
param->rc.i_vbv_max_bitrate = atoi( optarg );
case OPT_CPLXBLUR:
param->rc.f_complexity_blur = atof(optarg);
break;
+ case OPT_ZONES:
+ param->rc.psz_zones = optarg;
+ break;
+ case OPT_THREADS:
+ param->i_threads = atoi(optarg);
+ break;
case OPT_NOPSNR:
param->analyse.b_psnr = 0;
break;
param->b_aud = 1;
break;
case OPT_PROGRESS:
- *b_progress = 1;
+ opt->b_progress = 1;
+ break;
+ case OPT_VISUALIZE:
+#ifdef VISUALIZE
+ param->b_visualize = 1;
+ b_exit_on_ctrl_c = 1;
+#else
+ fprintf( stderr, "not compiled with visualization support\n" );
+#endif
+ break;
+ case OPT_CQM:
+ if( strstr( optarg, "flat" ) )
+ param->i_cqm_preset = X264_CQM_FLAT;
+ else if( strstr( optarg, "jvt" ) )
+ param->i_cqm_preset = X264_CQM_JVT;
+ else
+ {
+ fprintf( stderr, "bad CQM preset `%s'\n", optarg );
+ return -1;
+ }
+ break;
+ case OPT_CQMFILE:
+ param->psz_cqm_file = optarg;
+ break;
+ case OPT_CQM4:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+ b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+ b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
+ b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
+ break;
+ case OPT_CQM8:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
+ b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
+ break;
+ case OPT_CQM4I:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+ b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+ break;
+ case OPT_CQM4P:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
+ b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
+ break;
+ case OPT_CQM4IY:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+ break;
+ case OPT_CQM4IC:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+ break;
+ case OPT_CQM4PY:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+ break;
+ case OPT_CQM4PC:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+ break;
+ case OPT_CQM8I:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
+ break;
+ case OPT_CQM8P:
+ param->i_cqm_preset = X264_CQM_CUSTOM;
+ b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
break;
default:
fprintf( stderr, "unknown option (%c)\n", optopt );
return -1;
}
+
+ if( b_error )
+ {
+ fprintf( stderr, "bad argument (%s)\n", optarg );
+ return -1;
+ }
}
/* Get the file name */
- if( optind > argc - 1 || !*p_hout )
+ if( optind > argc - 1 || !opt->hout )
{
Help( &defaults );
return -1;
}
psz_filename = argv[optind++];
- if( !(*pb_decompress) )
+ if( !opt->b_decompress )
{
if( optind > argc - 1 )
{
/* open the input */
if( !strcmp( psz_filename, "-" ) )
{
- *p_hin = stdin;
+ opt->hin = stdin;
optind++;
}
else
return -1;
#endif
}
- if( p_open_infile( psz_filename, p_hin, param ) )
+ if( p_open_infile( psz_filename, &opt->hin, param ) )
{
fprintf( stderr, "could not open input file '%s'\n", psz_filename );
return -1;
/*****************************************************************************
* Decode:
*****************************************************************************/
+#if 0
static int Decode( x264_param_t *param, FILE *fh26l, hnd_t hout )
{
fprintf( stderr, "decompressor not working (help is welcome)\n" );
return -1;
-#if 0
x264_nal_t nal;
int i_data;
int b_eof;
i_data = 0;
nal.p_payload = malloc( DATA_MAX );
- while( !i_ctrl_c )
+ while( !b_ctrl_c )
{
uint8_t *p, *p_next, *end;
int i_size;
(double)( i_end - i_start );
fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );
}
-#endif
}
+#endif
static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
{
/*****************************************************************************
* Encode:
*****************************************************************************/
-static int Encode( x264_param_t *param, hnd_t hin, hnd_t hout, int b_progress )
+static int Encode( x264_param_t *param, cli_opt_t *opt )
{
x264_t *h;
x264_picture_t pic;
int i_frame_size;
int i_progress;
- i_frame_total = p_get_frame_total( hin, param->i_width, param->i_height );
+ i_frame_total = p_get_frame_total( opt->hin, param->i_width, param->i_height );
if( ( h = x264_encoder_open( param ) ) == NULL )
{
fprintf( stderr, "x264_encoder_open failed\n" );
- p_close_infile( hin );
- p_close_outfile( hout );
+ p_close_infile( opt->hin );
+ p_close_outfile( opt->hout );
return -1;
}
- if( p_set_outfile_param( hout, param ) )
+ if( p_set_outfile_param( opt->hout, param ) )
{
fprintf( stderr, "can't set outfile param\n" );
- p_close_infile( hin );
- p_close_outfile( hout );
+ p_close_infile( opt->hin );
+ p_close_outfile( opt->hout );
return -1;
}
i_start = x264_mdate();
/* Encode frames */
- if( param->i_maxframes > 0 && param->i_maxframes < i_frame_total )
- i_frame_total = param->i_maxframes;
- for( i_frame = 0, i_file = 0, i_progress = 0; i_ctrl_c == 0 && i_frame < i_frame_total; )
+ i_frame_total -= opt->i_seek;
+ if( opt->i_maxframes > 0 && opt->i_maxframes < i_frame_total )
+ i_frame_total = opt->i_maxframes;
+ for( i_frame = 0, i_file = 0, i_progress = 0;
+ b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
{
- if( p_read_frame( &pic, hin, param->i_width, param->i_height ) )
+ if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek, param->i_width, param->i_height ) )
break;
- pic.i_pts = i_frame * param->i_fps_den;
+ pic.i_pts = (int64_t)i_frame * param->i_fps_den;
- i_file += Encode_frame( h, hout, &pic );
+ i_file += Encode_frame( h, opt->hout, &pic );
i_frame++;
/* update status line (up to 1000 times per input file) */
- if( b_progress && param->i_log_level < X264_LOG_DEBUG &&
+ if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&
i_frame * 1000 / i_frame_total > i_progress )
{
int64_t i_elapsed = x264_mdate() - i_start;
double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
i_progress = i_frame * 1000 / i_frame_total;
- fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps \r", i_frame,
+ fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps \r", i_frame,
i_frame_total, (float)i_progress / 10, fps );
fflush( stderr ); // needed in windows
}
/* Flush delayed B-frames */
do {
i_file +=
- i_frame_size = Encode_frame( h, hout, NULL );
+ i_frame_size = Encode_frame( h, opt->hout, NULL );
} while( i_frame_size );
i_end = x264_mdate();
x264_encoder_close( h );
fprintf( stderr, "\n" );
- p_close_infile( hin );
- p_close_outfile( hout );
+ if( b_ctrl_c )
+ fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );
+
+ p_close_infile( opt->hin );
+ p_close_outfile( opt->hout );
if( i_frame > 0 )
{
double fps = (double)i_frame * (double)1000000 /
(double)( i_end - i_start );
- 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 ) );
+ 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 ) );
}
return 0;
}
/* raw 420 yuv file operation */
-static int open_file_yuv( char *psz_filename, hnd_t *p_handle , x264_param_t *p_param )
+static int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
if ((*p_handle = fopen(psz_filename, "rb")) == NULL)
return -1;
return i_frame_total;
}
-static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height )
+static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height )
{
+ static int prev_frame = -1;
FILE *f = (FILE *)handle;
+ if( i_frame != prev_frame+1 )
+ if( fseek( f, i_frame * i_width * i_height * 3 / 2, SEEK_SET ) )
+ return -1;
+
if( fread( p_pic->img.plane[0], 1, i_width * i_height, f ) <= 0
|| fread( p_pic->img.plane[1], 1, i_width * i_height / 4, f ) <= 0
|| fread( p_pic->img.plane[2], 1, i_width * i_height / 4, f ) <= 0 )
return -1;
+ prev_frame = i_frame;
+
return 0;
}
return info.dwLength;
}
-static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_width, int i_height )
+static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height )
{
- static int i_index = 0;
PAVISTREAM p_avi = (PAVISTREAM)handle;
p_pic->img.i_csp = X264_CSP_YV12;
- if( AVIStreamRead(p_avi, i_index, 1, p_pic->img.plane[0], i_width * i_height * 3 / 2, NULL, NULL ) )
+ if( AVIStreamRead(p_avi, i_frame, 1, p_pic->img.plane[0], i_width * i_height * 3 / 2, NULL, NULL ) )
return -1;
- i_index++;
-
return 0;
}
}
#endif
+
+
+/* -- mkv muxing support ------------------------------------------------- */
+typedef struct
+{
+ mk_Writer *w;
+
+ uint8_t *sps, *pps;
+ int sps_len, pps_len;
+
+ int width, height, d_width, d_height;
+
+ int64_t frame_duration;
+ int fps_num;
+
+ int b_header_written;
+ char b_writing_frame;
+} mkv_t;
+
+static int write_header_mkv( mkv_t *p_mkv )
+{
+ int ret;
+ uint8_t *avcC;
+ int avcC_len;
+
+ if( p_mkv->sps == NULL || p_mkv->pps == NULL ||
+ p_mkv->width == 0 || p_mkv->height == 0 ||
+ p_mkv->d_width == 0 || p_mkv->d_height == 0)
+ return -1;
+
+ avcC_len = 5 + 1 + 2 + p_mkv->sps_len + 1 + 2 + p_mkv->pps_len;
+ avcC = malloc(avcC_len);
+ if (avcC == NULL)
+ return -1;
+
+ avcC[0] = 1;
+ avcC[1] = p_mkv->sps[1];
+ avcC[2] = p_mkv->sps[2];
+ avcC[3] = p_mkv->sps[3];
+ avcC[4] = 0xfe; // nalu size length is three bytes
+ avcC[5] = 0xe1; // one sps
+
+ avcC[6] = p_mkv->sps_len >> 8;
+ avcC[7] = p_mkv->sps_len;
+
+ memcpy(avcC+8, p_mkv->sps, p_mkv->sps_len);
+
+ avcC[8+p_mkv->sps_len] = 1; // one pps
+ avcC[9+p_mkv->sps_len] = p_mkv->pps_len >> 8;
+ avcC[10+p_mkv->sps_len] = p_mkv->pps_len;
+
+ memcpy( avcC+11+p_mkv->sps_len, p_mkv->pps, p_mkv->pps_len );
+
+ ret = mk_writeHeader( p_mkv->w, "x264", "V_MPEG4/ISO/AVC",
+ avcC, avcC_len, p_mkv->frame_duration, 50000,
+ p_mkv->width, p_mkv->height,
+ p_mkv->d_width, p_mkv->d_height );
+
+ free( avcC );
+
+ p_mkv->b_header_written = 1;
+
+ return ret;
+}
+
+static int open_file_mkv( char *psz_filename, hnd_t *p_handle )
+{
+ mkv_t *p_mkv;
+
+ *p_handle = NULL;
+
+ p_mkv = malloc(sizeof(*p_mkv));
+ if (p_mkv == NULL)
+ return -1;
+
+ memset(p_mkv, 0, sizeof(*p_mkv));
+
+ p_mkv->w = mk_createWriter(psz_filename);
+ if (p_mkv->w == NULL)
+ {
+ free(p_mkv);
+ return -1;
+ }
+
+ *p_handle = p_mkv;
+
+ return 0;
+}
+
+static int set_param_mkv( hnd_t handle, x264_param_t *p_param )
+{
+ mkv_t *p_mkv = handle;
+ int64_t dw, dh;
+
+ if( p_param->i_fps_num > 0 )
+ {
+ p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
+ (int64_t)1000000000 / p_param->i_fps_num;
+ p_mkv->fps_num = p_param->i_fps_num;
+ }
+ else
+ {
+ p_mkv->frame_duration = 0;
+ p_mkv->fps_num = 1;
+ }
+
+ p_mkv->width = p_param->i_width;
+ p_mkv->height = p_param->i_height;
+
+ if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
+ {
+ dw = (int64_t)p_param->i_width * p_param->vui.i_sar_width;
+ dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height;
+ }
+ else
+ {
+ dw = p_param->i_width;
+ dh = p_param->i_height;
+ }
+
+ if( dw > 0 && dh > 0 )
+ {
+ int64_t a = dw, b = dh;
+
+ for (;;)
+ {
+ int64_t c = a % b;
+ if( c == 0 )
+ break;
+ a = b;
+ b = c;
+ }
+
+ dw /= b;
+ dh /= b;
+ }
+
+ p_mkv->d_width = (int)dw;
+ p_mkv->d_height = (int)dh;
+
+ return 0;
+}
+
+static int write_nalu_mkv( hnd_t handle, uint8_t *p_nalu, int i_size )
+{
+ mkv_t *p_mkv = handle;
+ uint8_t type = p_nalu[4] & 0x1f;
+ uint8_t dsize[3];
+ int psize;
+
+ switch( type )
+ {
+ // sps
+ case 0x07:
+ if( !p_mkv->sps )
+ {
+ p_mkv->sps = malloc(i_size - 4);
+ if (p_mkv->sps == NULL)
+ return -1;
+ p_mkv->sps_len = i_size - 4;
+ memcpy(p_mkv->sps, p_nalu + 4, i_size - 4);
+ }
+ break;
+
+ // pps
+ case 0x08:
+ if( !p_mkv->pps )
+ {
+ p_mkv->pps = malloc(i_size - 4);
+ if (p_mkv->pps == NULL)
+ return -1;
+ p_mkv->pps_len = i_size - 4;
+ memcpy(p_mkv->pps, p_nalu + 4, i_size - 4);
+ }
+ break;
+
+ // slice, sei
+ case 0x1:
+ case 0x5:
+ case 0x6:
+ if( !p_mkv->b_writing_frame )
+ {
+ if( mk_startFrame(p_mkv->w) < 0 )
+ return -1;
+ p_mkv->b_writing_frame = 1;
+ }
+ psize = i_size - 4 ;
+ dsize[0] = psize >> 16;
+ dsize[1] = psize >> 8;
+ dsize[2] = psize;
+ if( mk_addFrameData(p_mkv->w, dsize, 3) < 0 ||
+ mk_addFrameData(p_mkv->w, p_nalu + 4, i_size - 4) < 0 )
+ return -1;
+ break;
+
+ default:
+ break;
+ }
+
+ if( !p_mkv->b_header_written && p_mkv->pps && p_mkv->sps &&
+ write_header_mkv(p_mkv) < 0 )
+ return -1;
+
+ return i_size;
+}
+
+static int set_eop_mkv( hnd_t handle, x264_picture_t *p_picture )
+{
+ mkv_t *p_mkv = handle;
+ int64_t i_stamp = (int64_t)(p_picture->i_pts * 1e9 / p_mkv->fps_num);
+
+ p_mkv->b_writing_frame = 0;
+
+ return mk_setFrameFlags( p_mkv->w, i_stamp,
+ p_picture->i_type == X264_TYPE_IDR );
+}
+
+static int close_file_mkv( hnd_t handle )
+{
+ mkv_t *p_mkv = handle;
+ int ret;
+
+ if( p_mkv->sps )
+ free( p_mkv->sps );
+ if( p_mkv->pps )
+ free( p_mkv->pps );
+
+ ret = mk_close(p_mkv->w);
+
+ free( p_mkv );
+
+ return ret;
+}
+