]> git.sesse.net Git - x264/blobdiff - x264.c
Fix x264 compilation on Apple GCC
[x264] / x264.c
diff --git a/x264.c b/x264.c
index 57db499cfee975018c96e866027ec0c83002f75b..4834961ebd75aa5ae56fedd7fc223ea419cd4907 100644 (file)
--- a/x264.c
+++ b/x264.c
@@ -5,6 +5,8 @@
  *
  * Authors: Loren Merritt <lorenm@u.washington.edu>
  *          Laurent Aimar <fenrir@via.ecp.fr>
+ *          Steven Walters <kemuri9@gmail.com>
+ *          Kieran Kunhya <kieran@kunhya.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -61,15 +63,39 @@ typedef struct {
 cli_input_t input;
 static cli_output_t output;
 
-/* i/o modules that work with pipes (and fifos) */
-static const char * const stdin_format_names[] = { "yuv", "y4m", 0 };
-static const char * const stdout_format_names[] = { "raw", "mkv", "flv", 0 };
+static const char * const demuxer_names[] =
+{
+    "auto",
+    "yuv",
+    "y4m",
+#ifdef AVS_INPUT
+    "avs",
+#endif
+#ifdef LAVF_INPUT
+    "lavf",
+#endif
+#ifdef FFMS_INPUT
+    "ffms",
+#endif
+    0
+};
+
+static const char * const muxer_names[] =
+{
+    "auto",
+    "raw",
+    "mkv",
+    "flv",
+#ifdef MP4_OUTPUT
+    "mp4",
+#endif
+    0
+};
 
 static void Help( x264_param_t *defaults, int longhelp );
 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 );
 
-
 /****************************************************************************
  * main:
  ****************************************************************************/
@@ -114,11 +140,25 @@ static char const *strtable_lookup( const char * const table[], int index )
     return ( ( index >= 0 && index < i ) ? table[ index ] : "???" );
 }
 
+static char *stringify_names( char *buf, const char * const names[] )
+{
+    int i = 0;
+    char *p = buf;
+    for( p[0] = 0; names[i]; i++ )
+    {
+        p += sprintf( p, "%s", names[i] );
+        if( names[i+1] )
+            p += sprintf( p, ", " );
+    }
+    return buf;
+}
+
 /*****************************************************************************
  * Help:
  *****************************************************************************/
 static void Help( x264_param_t *defaults, int longhelp )
 {
+    char buf[50];
 #define H0 printf
 #define H1 if(longhelp>=1) printf
 #define H2 if(longhelp==2) printf
@@ -128,6 +168,7 @@ static void Help( x264_param_t *defaults, int longhelp )
         "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n"
         "  or YUV4MPEG 4:2:0 (*.y4m),\n"
         "  or Avisynth if compiled with support (%s).\n"
+        "  or libav* formats if compiled with lavf support (%s) or ffms support (%s).\n"
         "Outfile type is selected by filename:\n"
         " .264 -> Raw bytestream\n"
         " .mkv -> Matroska\n"
@@ -142,9 +183,17 @@ static void Help( x264_param_t *defaults, int longhelp )
         "\n",
         X264_BUILD, X264_VERSION,
 #ifdef AVS_INPUT
-        "native",
-#elif defined(VFW_INPUT)
-        "vfw (fallback)",
+        "yes",
+#else
+        "no",
+#endif
+#ifdef LAVF_INPUT
+        "yes",
+#else
+        "no",
+#endif
+#ifdef FFMS_INPUT
+        "yes",
 #else
         "no",
 #endif
@@ -199,7 +248,7 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                    --partitions i8x8,i4x4 --ref 1\n"
         "                                    --subme 1 --trellis 0 --weightp 0\n"
         "                                  - faster:\n"
-        "                                    --no-mbtree --no-mixed-refs --refs 2\n"
+        "                                    --no-mbtree --no-mixed-refs --ref 2\n"
         "                                    --subme 4 --weightp 1\n"
         "                                  - fast\n"
         "                                    --rc-lookahead 30 --ref 2 --subme 6\n"
@@ -214,13 +263,13 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                    --ref 8 --subme 9 --trellis 2\n"
         "                                  - veryslow\n"
         "                                    --b-adapt 2 --bframes 8 --direct auto\n"
-        "                                    --me umh --me-range 24 --partitions all\n"
+        "                                    --me umh --merange 24 --partitions all\n"
         "                                    --ref 16 --subme 10 --trellis 2\n"
         "                                    --rc-lookahead 60\n"
         "                                  - placebo\n"
         "                                    --bframes 16 --b-adapt 2 --direct auto\n"
         "                                    --slow-firstpass --no-fast-pskip\n"
-        "                                    --me tesa --me-range 24 --partitions all\n"
+        "                                    --me tesa --merange 24 --partitions all\n"
         "                                    --rc-lookahead 60 --ref 16 --subme 10\n"
         "                                    --trellis 2\n" );
     else H0( "                                  - ultrafast,veryfast,faster,fast,medium\n"
@@ -264,8 +313,9 @@ static void Help( x264_param_t *defaults, int longhelp )
     H2( "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n", defaults->i_keyint_min );
     H2( "      --no-scenecut           Disable adaptive I-frame decision\n" );
     H2( "      --scenecut <integer>    How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
+    H2( "      --intra-refresh         Use Periodic Intra Refresh instead of IDR frames\n" );
     H1( "  -b, --bframes <integer>     Number of B-frames between I and P [%d]\n", defaults->i_bframe );
-    H1( "      --b-adapt               Adaptive B-frame decision method [%d]\n"
+    H1( "      --b-adapt <integer>     Adaptive B-frame decision method [%d]\n"
         "                                  Higher values may lower threading efficiency.\n"
         "                                  - 0: Disabled\n"
         "                                  - 1: Fast\n"
@@ -312,9 +362,8 @@ static void Help( x264_param_t *defaults, int longhelp )
     H1( "      --aq-strength <float>   Reduces blocking and blurring in flat and\n"
         "                              textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
     H1( "\n" );
-    H2( "  -p, --pass <1|2|3>          Enable multipass ratecontrol\n" );
-    else H0( "  -p, --pass <1|2>            Enable multipass ratecontrol\n" );
-    H0( "                                  - 1: First pass, creates stats file\n"
+    H0( "  -p, --pass <integer>        Enable multipass ratecontrol\n"
+        "                                  - 1: First pass, creates stats file\n"
         "                                  - 2: Last pass, does not overwrite stats file\n" );
     H2( "                                  - 3: Nth pass, overwrites stats file\n" );
     H1( "      --stats <string>        Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
@@ -343,7 +392,7 @@ static void Help( x264_param_t *defaults, int longhelp )
         "                                  - none, spatial, temporal, auto\n",
                                        strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
     H2( "      --no-weightb            Disable weighted prediction for B-frames\n" );
-    H1( "      --weightp               Weighted prediction for P-frames [%d]\n"
+    H1( "      --weightp <integer>     Weighted prediction for P-frames [%d]\n"
         "                              - 0: Disabled\n"
         "                              - 1: Blind offset\n"
         "                              - 2: Smart analysis\n", defaults->analyse.i_weighted_pred );
@@ -433,10 +482,11 @@ static void Help( x264_param_t *defaults, int longhelp )
     H0( "Input/Output:\n" );
     H0( "\n" );
     H0( "  -o, --output                Specify output file\n" );
-    H1( "      --stdout                Specify stdout format [\"%s\"]\n"
-        "                                  - raw, mkv, flv\n", stdout_format_names[0] );
-    H1( "      --stdin                 Specify stdin format [\"%s\"]\n"
-        "                                  - yuv, y4m\n", stdin_format_names[0] );
+    H1( "      --muxer <string>        Specify output container format [\"%s\"]\n"
+        "                                  - %s\n", muxer_names[0], stringify_names( buf, muxer_names ) );
+    H1( "      --demuxer <string>      Specify input container format [\"%s\"]\n"
+        "                                  - %s\n", demuxer_names[0], stringify_names( buf, demuxer_names ) );
+    H1( "      --index <string>        Filename for input index file\n" );
     H0( "      --sar width:height      Specify Sample Aspect Ratio\n" );
     H0( "      --fps <float|rational>  Specify framerate\n" );
     H0( "      --seek <integer>        First frame to encode\n" );
@@ -459,6 +509,7 @@ static void Help( x264_param_t *defaults, int longhelp )
     H2( "      --dump-yuv <string>     Save reconstructed frames\n" );
     H2( "      --sps-id <integer>      Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
     H2( "      --aud                   Use access unit delimiters\n" );
+    H2( "      --force-cfr             Force constant framerate timestamp generation\n" );
     H0( "\n" );
 }
 
@@ -476,8 +527,10 @@ static void Help( x264_param_t *defaults, int longhelp )
 #define OPT_SLOWFIRSTPASS 267
 #define OPT_FULLHELP 268
 #define OPT_FPS 269
-#define OPT_STDOUT_FORMAT 270
-#define OPT_STDIN_FORMAT 271
+#define OPT_MUXER 270
+#define OPT_DEMUXER 271
+#define OPT_INDEX 272
+#define OPT_INTERLACED 273
 
 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
 static struct option long_options[] =
@@ -498,13 +551,15 @@ static struct option long_options[] =
     { "b-pyramid",   required_argument, NULL, 0 },
     { "min-keyint",  required_argument, NULL, 'i' },
     { "keyint",      required_argument, NULL, 'I' },
+    { "intra-refresh",     no_argument, NULL, 0 },
     { "scenecut",    required_argument, NULL, 0 },
     { "no-scenecut",       no_argument, NULL, 0 },
     { "nf",                no_argument, NULL, 0 },
     { "no-deblock",        no_argument, NULL, 0 },
     { "filter",      required_argument, NULL, 0 },
     { "deblock",     required_argument, NULL, 'f' },
-    { "interlaced",        no_argument, NULL, 0 },
+    { "interlaced",        no_argument, NULL, OPT_INTERLACED },
+    { "no-interlaced",     no_argument, NULL, OPT_INTERLACED },
     { "constrained-intra", no_argument, NULL, 0 },
     { "cabac",             no_argument, NULL, 0 },
     { "no-cabac",          no_argument, NULL, 0 },
@@ -522,8 +577,11 @@ static struct option long_options[] =
     { "frames",      required_argument, NULL, OPT_FRAMES },
     { "seek",        required_argument, NULL, OPT_SEEK },
     { "output",      required_argument, NULL, 'o' },
-    { "stdout",      required_argument, NULL, OPT_STDOUT_FORMAT },
-    { "stdin",       required_argument, NULL, OPT_STDIN_FORMAT },
+    { "muxer",       required_argument, NULL, OPT_MUXER },
+    { "demuxer",     required_argument, NULL, OPT_DEMUXER },
+    { "stdout",      required_argument, NULL, OPT_MUXER },
+    { "stdin",       required_argument, NULL, OPT_DEMUXER },
+    { "index",       required_argument, NULL, OPT_INDEX },
     { "analyse",     required_argument, NULL, 0 },
     { "partitions",  required_argument, NULL, 'A' },
     { "direct",      required_argument, NULL, 0 },
@@ -606,91 +664,125 @@ static struct option long_options[] =
     { "transfer",    required_argument, NULL, 0 },
     { "colormatrix", required_argument, NULL, 0 },
     { "chromaloc",   required_argument, NULL, 0 },
+    { "force-cfr",         no_argument, NULL, 0 },
     {0, 0, 0, 0}
 };
 
-static int select_output( char *filename, const char *pipe_format, x264_param_t *param )
+static int select_output( const char *muxer, char *filename, x264_param_t *param )
 {
     const char *ext = get_filename_extension( filename );
-    if( !strcmp( filename, "-" ) )
-        ext = pipe_format;
+    if( !strcmp( filename, "-" ) || strcasecmp( muxer, "auto" ) )
+        ext = muxer;
 
     if( !strcasecmp( ext, "mp4" ) )
     {
 #ifdef MP4_OUTPUT
-        output = mp4_output; // FIXME use b_annexb=0
+        output = mp4_output;
+        param->b_annexb = 0;
+        param->b_aud = 0;
+        param->b_repeat_headers = 0;
 #else
         fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
         return -1;
 #endif
     }
     else if( !strcasecmp( ext, "mkv" ) )
-        output = mkv_output; // FIXME use b_annexb=0
+    {
+        output = mkv_output;
+        param->b_annexb = 0;
+        param->b_aud = 0;
+        param->b_repeat_headers = 0;
+    }
     else if( !strcasecmp( ext, "flv" ) )
     {
         output = flv_output;
         param->b_annexb = 0;
+        param->b_aud = 0;
+        param->b_repeat_headers = 0;
     }
     else
         output = raw_output;
     return 0;
 }
 
-static int select_input( char *filename, char *resolution, const char *pipe_format, x264_param_t *param )
+static int select_input( const char *demuxer, char *used_demuxer, char *filename,
+                         hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
 {
     const char *ext = get_filename_extension( filename );
-    if( !strcmp( filename, "-" ) )
-        ext = pipe_format;
+    int b_regular = strcmp( filename, "-" );
+    int b_auto = !strcasecmp( demuxer, "auto" );
+    if( !b_regular && b_auto )
+        ext = "yuv";
+    if( b_regular )
+    {
+        FILE *f = fopen( filename, "r" );
+        if( !f )
+        {
+            fprintf( stderr, "x264 [error]: could not open input file `%s'\n", filename );
+            return -1;
+        }
+        b_regular = x264_is_regular_file( f );
+        fclose( f );
+    }
+    const char *module = b_auto ? ext : demuxer;
 
-    if( !strcasecmp( ext, "avi" ) || !strcasecmp( ext, "avs" ) )
+    if( !strcasecmp( module, "avs" ) || !strcasecmp( ext, "d2v" ) || !strcasecmp( ext, "dga" ) )
     {
-#if defined(AVS_INPUT) || defined(VFW_INPUT)
+#ifdef AVS_INPUT
         input = avs_input;
+        module = "avs";
 #else
         fprintf( stderr, "x264 [error]: not compiled with AVS input support\n" );
         return -1;
 #endif
     }
-    else if( !strcasecmp( ext, "y4m" ) )
+    else if( !strcasecmp( module, "y4m" ) )
         input = y4m_input;
-    else if( !strcasecmp( ext, "yuv" ) )
+    else if( !strcasecmp( module, "yuv" ) )
+        input = yuv_input;
+    else
     {
-        if( !resolution )
+#ifdef FFMS_INPUT
+        if( b_regular && (b_auto || !strcasecmp( demuxer, "ffms" )) &&
+            !ffms_input.open_file( filename, p_handle, info, opt ) )
         {
-            /* try to parse the file name */
-            char *p;
-            for( p = filename; *p; p++ )
-                if( *p >= '0' && *p <= '9' &&
-                    sscanf( p, "%ux%u", &param->i_width, &param->i_height ) == 2 )
-                {
-                    if( param->i_log_level >= X264_LOG_INFO )
-                        fprintf( stderr, "x264 [info]: %dx%d (given by file name) @ %.2f fps\n", param->i_width,
-                                 param->i_height, (double)param->i_fps_num / param->i_fps_den );
-                    break;
-                }
+            module = "ffms";
+            b_auto = 0;
+            input = ffms_input;
         }
-        else
+#endif
+#ifdef LAVF_INPUT
+        if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
+            (!b_regular || !lavf_input.open_file( filename, p_handle, info, opt )) )
         {
-            sscanf( resolution, "%ux%u", &param->i_width, &param->i_height );
-            if( param->i_log_level >= X264_LOG_INFO )
-                fprintf( stderr, "x264 [info]: %dx%d @ %.2f fps\n", param->i_width, param->i_height,
-                         (double)param->i_fps_num / param->i_fps_den );
+            module = "lavf";
+            b_auto = 0;
+            input = lavf_input;
         }
-        if( !param->i_width || !param->i_height )
+#endif
+#ifdef AVS_INPUT
+        if( b_regular && (b_auto || !strcasecmp( demuxer, "avs" )) &&
+            !avs_input.open_file( filename, p_handle, info, opt ) )
         {
-            fprintf( stderr, "x264 [error]: Rawyuv input requires a resolution.\n" );
-            return -1;
+            module = "avs";
+            b_auto = 0;
+            input = avs_input;
         }
-        input = yuv_input;
-    }
-    else
-    {
-#ifdef AVS_INPUT
-        input = avs_input;
-#else
-        input = yuv_input;
 #endif
+        if( b_auto && !yuv_input.open_file( filename, p_handle, info, opt ) )
+        {
+            module = "yuv";
+            b_auto = 0;
+            input = yuv_input;
+        }
+
+        if( !(*p_handle) )
+        {
+            fprintf( stderr, "x264 [error]: could not open input file `%s' via any method!\n", filename );
+            return -1;
+        }
     }
+    strcpy( used_demuxer, module );
 
     return 0;
 }
@@ -698,13 +790,12 @@ static int select_input( char *filename, char *resolution, const char *pipe_form
 /*****************************************************************************
  * Parse:
  *****************************************************************************/
-static int  Parse( int argc, char **argv,
-                   x264_param_t *param, cli_opt_t *opt )
+static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
 {
     char *input_filename = NULL;
-    const char *stdin_format = stdin_format_names[0];
+    const char *demuxer = demuxer_names[0];
     char *output_filename = NULL;
-    const char *stdout_format = stdout_format_names[0];
+    const char *muxer = muxer_names[0];
     x264_param_t defaults = *param;
     char *profile = NULL;
     int b_thread_input = 0;
@@ -712,9 +803,12 @@ static int  Parse( int argc, char **argv,
     int b_pass1 = 0;
     int b_user_ref = 0;
     int b_user_fps = 0;
+    int b_user_interlaced = 0;
     int i;
+    cli_input_opt_t input_opt;
 
     memset( opt, 0, sizeof(cli_opt_t) );
+    memset( &input_opt, 0, sizeof(cli_input_opt_t) );
     opt->b_progress = 1;
 
     /* Presets are applied before all other options. */
@@ -824,7 +918,7 @@ static int  Parse( int argc, char **argv,
             }
             else
             {
-                fprintf( stderr, "x264 [error]: invalid preset: %s\n", optarg );
+                fprintf( stderr, "x264 [error]: invalid preset '%s'\n", optarg );
                 return -1;
             }
         }
@@ -905,7 +999,7 @@ static int  Parse( int argc, char **argv,
             }
             else
             {
-                fprintf( stderr, "x264 [error]: invalid tune: %s\n", optarg );
+                fprintf( stderr, "x264 [error]: invalid tune '%s'\n", optarg );
                 return -1;
             }
         }
@@ -951,39 +1045,42 @@ static int  Parse( int argc, char **argv,
 #endif
                 exit(0);
             case OPT_FRAMES:
-                param->i_frame_total = atoi( optarg );
+                param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
                 break;
             case OPT_SEEK:
-                opt->i_seek = atoi( optarg );
+                opt->i_seek = input_opt.seek = X264_MAX( atoi( optarg ), 0 );
                 break;
             case 'o':
                 output_filename = optarg;
                 break;
-            case OPT_STDOUT_FORMAT:
-                for( i = 0; stdout_format_names[i] && strcasecmp( stdout_format_names[i], optarg ); )
+            case OPT_MUXER:
+                for( i = 0; muxer_names[i] && strcasecmp( muxer_names[i], optarg ); )
                     i++;
-                if( !stdout_format_names[i] )
+                if( !muxer_names[i] )
                 {
-                    fprintf( stderr, "x264 [error]: invalid stdout format `%s'\n", optarg );
+                    fprintf( stderr, "x264 [error]: invalid muxer '%s'\n", optarg );
                     return -1;
                 }
-                stdout_format = optarg;
+                muxer = optarg;
                 break;
-            case OPT_STDIN_FORMAT:
-                for( i = 0; stdin_format_names[i] && strcasecmp( stdin_format_names[i], optarg ); )
+            case OPT_DEMUXER:
+                for( i = 0; demuxer_names[i] && strcasecmp( demuxer_names[i], optarg ); )
                     i++;
-                if( !stdin_format_names[i] )
+                if( !demuxer_names[i] )
                 {
-                    fprintf( stderr, "x264 [error]: invalid stdin format `%s'\n", optarg );
+                    fprintf( stderr, "x264 [error]: invalid demuxer '%s'\n", optarg );
                     return -1;
                 }
-                stdin_format = optarg;
+                demuxer = optarg;
+                break;
+            case OPT_INDEX:
+                input_opt.index = optarg;
                 break;
             case OPT_QPFILE:
                 opt->qpfile = fopen( optarg, "rb" );
                 if( !opt->qpfile )
                 {
-                    fprintf( stderr, "x264 [error]: can't open `%s'\n", optarg );
+                    fprintf( stderr, "x264 [error]: can't open qpfile `%s'\n", optarg );
                     return -1;
                 }
                 else if( !x264_is_regular_file( opt->qpfile ) )
@@ -1031,6 +1128,9 @@ static int  Parse( int argc, char **argv,
             case OPT_FPS:
                 b_user_fps = 1;
                 goto generic_option;
+            case OPT_INTERLACED:
+                b_user_interlaced = 1;
+                goto generic_option;
             default:
 generic_option:
             {
@@ -1118,9 +1218,8 @@ generic_option:
                  optind > argc - 1 ? "input" : "output" );
         return -1;
     }
-    input_filename = argv[optind++];
 
-    if( select_output( output_filename, stdout_format, param ) )
+    if( select_output( muxer, output_filename, param ) )
         return -1;
     if( output.open_file( output_filename, &opt->hout ) )
     {
@@ -1128,31 +1227,73 @@ generic_option:
         return -1;
     }
 
-    if( select_input( input_filename, optind < argc ? argv[optind++] : NULL, stdin_format, param ) )
+    input_filename = argv[optind++];
+    input_opt.resolution = optind < argc ? argv[optind++] : NULL;
+    video_info_t info = {0};
+    char demuxername[5];
+
+    /* set info flags to param flags to be overwritten by demuxer as necessary. */
+    info.csp        = param->i_csp;
+    info.fps_num    = param->i_fps_num;
+    info.fps_den    = param->i_fps_den;
+    info.interlaced = param->b_interlaced;
+    info.sar_width  = param->vui.i_sar_width;
+    info.sar_height = param->vui.i_sar_height;
+    info.vfr        = param->b_vfr_input;
+
+    if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
         return -1;
 
+    if( !opt->hin && input.open_file( input_filename, &opt->hin, &info, &input_opt ) )
     {
-        int i_fps_num = param->i_fps_num;
-        int i_fps_den = param->i_fps_den;
+        fprintf( stderr, "x264 [error]: could not open input file `%s'\n", input_filename );
+        return -1;
+    }
 
-        if( input.open_file( input_filename, &opt->hin, param ) )
-        {
-            fprintf( stderr, "x264 [error]: could not open input file `%s'\n", input_filename );
-            return -1;
-        }
-        /* Restore the user's frame rate if fps has been explicitly set on the commandline. */
-        if( b_user_fps )
-        {
-            param->i_fps_num = i_fps_num;
-            param->i_fps_den = i_fps_den;
-        }
+    x264_reduce_fraction( &info.sar_width, &info.sar_height );
+    x264_reduce_fraction( &info.fps_num, &info.fps_den );
+    if( param->i_log_level >= X264_LOG_INFO )
+        fprintf( stderr, "%s [info]: %dx%d%c %d:%d @ %d/%d fps (%cfr)\n", demuxername, info.width,
+                 info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
+                 info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
+
+    /* set param flags from the info flags as necessary */
+    param->i_csp       = info.csp;
+    param->i_height    = info.height;
+    param->b_vfr_input = info.vfr;
+    param->i_width     = info.width;
+    if( !b_user_interlaced && info.interlaced )
+    {
+        fprintf( stderr, "x264 [warning]: input appears to be interlaced, enabling interlaced mode.\n"
+                         "                If you want otherwise, use --no-interlaced\n" );
+        param->b_interlaced = 1;
+    }
+    if( !b_user_fps )
+    {
+        param->i_fps_num = info.fps_num;
+        param->i_fps_den = info.fps_den;
+    }
+    if( param->b_vfr_input )
+    {
+        param->i_timebase_num = info.timebase_num;
+        param->i_timebase_den = info.timebase_den;
+    }
+    else
+    {
+        param->i_timebase_den = param->i_fps_num;
+        param->i_timebase_num = param->i_fps_den;
+    }
+    if( !param->vui.i_sar_width || !param->vui.i_sar_height )
+    {
+        param->vui.i_sar_width  = info.sar_width;
+        param->vui.i_sar_height = info.sar_height;
     }
 
 #ifdef HAVE_PTHREAD
     if( b_thread_input || param->i_threads > 1
         || (param->i_threads == X264_THREADS_AUTO && x264_cpu_num_processors() > 1) )
     {
-        if( thread_input.open_file( NULL, &opt->hin, param ) )
+        if( thread_input.open_file( NULL, &opt->hin, &info, NULL ) )
         {
             fprintf( stderr, "x264 [error]: threaded input failed\n" );
             return -1;
@@ -1226,38 +1367,36 @@ static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
  * Encode:
  *****************************************************************************/
 
-static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
+static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *last_pts )
 {
     x264_picture_t pic_out;
     x264_nal_t *nal;
-    int i_nal, i, i_nalu_size;
-    int i_file = 0;
+    int i_nal;
+    int i_frame_size = 0;
+
+    i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
 
-    if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
+    if( i_frame_size < 0 )
     {
         fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
         return -1;
     }
 
-    for( i = 0; i < i_nal; i++ )
+    if( i_frame_size )
     {
-        i_nalu_size = output.write_nalu( hout, nal[i].p_payload, nal[i].i_payload, &pic_out );
-        if( i_nalu_size < 0 )
-            return -1;
-        i_file += i_nalu_size;
+        i_frame_size = output.write_frame( hout, nal[0].p_payload, i_frame_size, &pic_out );
+        *last_pts = pic_out.i_pts;
     }
-    if( i_nal )
-        output.set_eop( hout, &pic_out );
 
-    return i_file;
+    return i_frame_size;
 }
 
-static void Print_status( int64_t i_start, int i_frame, int i_frame_total, int64_t i_file, x264_param_t *param )
+static void Print_status( int64_t i_start, int i_frame, int i_frame_total, int64_t i_file, x264_param_t *param, int64_t last_pts )
 {
     char    buf[200];
     int64_t i_elapsed = x264_mdate() - i_start;
     double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
-    double bitrate = (double) i_file * 8 * param->i_fps_num / ( (double) param->i_fps_den * i_frame * 1000 );
+    double bitrate = (double) i_file * 8 / ( (double) last_pts * 1000 * param->i_timebase_num / param->i_timebase_den );
     if( i_frame_total )
     {
         int eta = i_elapsed * (i_frame_total - i_frame) / ((int64_t)i_frame * 1000000);
@@ -1281,9 +1420,16 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
 
     int     i_frame, i_frame_total, i_frame_output;
     int64_t i_start, i_end;
-    int64_t i_file;
+    int64_t i_file = 0;
     int     i_frame_size;
     int     i_update_interval;
+    int64_t last_pts = 0;
+#   define  MAX_PTS_WARNING 3 /* arbitrary */
+    int     pts_warning_cnt = 0;
+    int64_t largest_pts = -1;
+    int64_t second_largest_pts = -1;
+    int64_t ticks_per_frame;
+    double  duration;
 
     opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
     i_frame_total = input.get_frame_total( opt->hin );
@@ -1305,7 +1451,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     {
         fprintf( stderr, "x264 [error]: can't set outfile param\n" );
         input.close_file( opt->hin );
-        output.close_file( opt->hout );
+        output.close_file( opt->hout, largest_pts, second_largest_pts );
         return -1;
     }
 
@@ -1317,14 +1463,53 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     }
 
     i_start = x264_mdate();
+    /* ticks/frame = ticks/second / frames/second */
+    ticks_per_frame = (int64_t)h->param.i_timebase_den * h->param.i_fps_den / h->param.i_timebase_num / h->param.i_fps_num;
+    if( ticks_per_frame < 1 )
+    {
+        fprintf( stderr, "x264 [error]: ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame );
+        return -1;
+    }
+
+    if( !h->param.b_repeat_headers )
+    {
+        // Write SPS/PPS/SEI
+        x264_nal_t *headers;
+        int i_nal;
+
+        if( x264_encoder_headers( h, &headers, &i_nal ) < 0 )
+        {
+            fprintf( stderr, "x264 [error]: x264_encoder_headers failed\n" );
+            return -1;
+        }
+
+        if( (i_file = output.write_headers( opt->hout, headers )) < 0 )
+            return -1;
+    }
 
     /* Encode frames */
-    for( i_frame = 0, i_file = 0, i_frame_output = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
+    for( i_frame = 0, i_frame_output = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
     {
         if( input.read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )
             break;
 
-        pic.i_pts = (int64_t)i_frame * param->i_fps_den;
+        if( !param->b_vfr_input )
+            pic.i_pts = i_frame;
+        if( pic.i_pts <= largest_pts )
+        {
+            if( h->param.i_log_level >= X264_LOG_WARNING )
+            {
+                if( h->param.i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
+                    fprintf( stderr, "x264 [warning]: non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
+                             i_frame, pic.i_pts, largest_pts );
+                else if( pts_warning_cnt == MAX_PTS_WARNING )
+                    fprintf( stderr, "x264 [warning]: too many nonmonotonic pts warnings, suppressing further ones\n" );
+                pts_warning_cnt++;
+            }
+            pic.i_pts = largest_pts + ticks_per_frame;
+        }
+        second_largest_pts = largest_pts;
+        largest_pts = pic.i_pts;
 
         if( opt->qpfile )
             parse_qpfile( opt, &pic, i_frame + opt->i_seek );
@@ -1335,7 +1520,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
             pic.i_qpplus1 = 0;
         }
 
-        i_frame_size = Encode_frame( h, opt->hout, &pic );
+        i_frame_size = Encode_frame( h, opt->hout, &pic, &last_pts );
         if( i_frame_size < 0 )
             return -1;
         i_file += i_frame_size;
@@ -1349,20 +1534,28 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
 
         /* update status line (up to 1000 times per input file) */
         if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
-            Print_status( i_start, i_frame_output, i_frame_total, i_file, param );
+            Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
     }
     /* Flush delayed frames */
     while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
     {
-        i_frame_size = Encode_frame( h, opt->hout, NULL );
+        i_frame_size = Encode_frame( h, opt->hout, NULL, &last_pts );
         if( i_frame_size < 0 )
             return -1;
         i_file += i_frame_size;
         if( i_frame_size )
             i_frame_output++;
         if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
-            Print_status( i_start, i_frame_output, i_frame_total, i_file, param );
+            Print_status( i_start, i_frame_output, i_frame_total, i_file, param, last_pts );
     }
+    if( pts_warning_cnt >= MAX_PTS_WARNING && h->param.i_log_level < X264_LOG_DEBUG )
+        fprintf( stderr, "x264 [warning]: %d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
+
+    /* duration algorithm fails when only 1 frame is output */
+    if( i_frame_output == 1 )
+        duration = (double)param->i_fps_den / param->i_fps_num;
+    else
+        duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
 
     i_end = x264_mdate();
     input.picture_clean( &pic );
@@ -1376,7 +1569,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
         fprintf( stderr, "aborted at input frame %d, output frame %d\n", opt->i_seek + i_frame, i_frame_output );
 
     input.close_file( opt->hin );
-    output.close_file( opt->hout );
+    output.close_file( opt->hout, largest_pts, second_largest_pts );
 
     if( i_frame_output > 0 )
     {
@@ -1384,8 +1577,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
                      (double)( i_end - i_start );
 
         fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame_output, fps,
-                 (double) i_file * 8 * param->i_fps_num /
-                 ( (double) param->i_fps_den * i_frame_output * 1000 ) );
+                 (double) i_file * 8 / ( 1000 * duration ) );
     }
 
     return 0;