vpxenc: merge with master
authorJohn Koleszar <jkoleszar@google.com>
Tue, 6 Nov 2012 20:02:42 +0000 (12:02 -0800)
committerJohn Koleszar <jkoleszar@google.com>
Tue, 6 Nov 2012 20:04:53 +0000 (12:04 -0800)
Change-Id: Ic3e3559384a0e72abdc9b66a19865474c2a41b38

vpxenc.c

index cf77274..484eb16 100644 (file)
--- a/vpxenc.c
+++ b/vpxenc.c
@@ -10,9 +10,6 @@
 
 #include "vpx_config.h"
 
-/* This is a simple program that encodes YV12 files and generates ivf
- * files using the new interface.
- */
 #if defined(_WIN32) || !CONFIG_OS_SUPPORT
 #define USE_POSIX_MMAP 0
 #else
@@ -26,6 +23,7 @@
 #include <limits.h>
 #include <assert.h>
 #include "vpx/vpx_encoder.h"
+#include "vpx/vpx_decoder.h"
 #if USE_POSIX_MMAP
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 #endif
-#include "vpx_config.h"
-#include "vpx_version.h"
+
+#if CONFIG_VP9_ENCODER
 #include "vpx/vp8cx.h"
+#endif
+#if CONFIG_VP9_DECODER
 #include "vpx/vp8dx.h"
-#include "vpx/vpx_decoder.h"
+#endif
+
 #include "vpx_ports/mem_ops.h"
 #include "vpx_ports/vpx_timer.h"
 #include "tools_common.h"
@@ -52,16 +53,14 @@ typedef __int64 off_t;
 #define fseeko _fseeki64
 #define ftello _ftelli64
 #elif defined(_WIN32)
-/* MinGW defines off_t, and uses f{seek,tell}o64 */
+/* MinGW defines off_t as long
+   and uses f{seek,tell}o64/off64_t for large files */
 #define fseeko fseeko64
 #define ftello ftello64
+#define off_t off64_t
 #endif
 
-#if defined(_MSC_VER)
-#define LITERALU64(n) n
-#else
-#define LITERALU64(n) n##LLU
-#endif
+#define LITERALU64(hi,lo) ((((uint64_t)hi)<<32)|lo)
 
 /* We should use 32-bit file operations in WebM file format
  * when building ARM executable file (.axf) with RVCT */
@@ -71,49 +70,76 @@ typedef long off_t;
 #define ftello ftell
 #endif
 
-static const char *exec_name;
+/* Swallow warnings about unused results of fread/fwrite */
+static size_t wrap_fread(void *ptr, size_t size, size_t nmemb,
+                         FILE *stream) {
+  return fread(ptr, size, nmemb, stream);
+}
+#define fread wrap_fread
 
+static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
+                          FILE *stream) {
+  return fwrite(ptr, size, nmemb, stream);
+}
+#define fwrite wrap_fwrite
 
 
-#define VP8_FOURCC (0x78385056)
-static const struct {
-  char const *name;
-  const vpx_codec_iface_t *(*iface)(void);
-  unsigned int             fourcc;
-  unsigned int             fourcc_mask;
-} ifaces[] = {
-#if CONFIG_VP9_DECODER
-  {"vp9",  &vpx_codec_vp8_dx,   VP8_FOURCC, 0x00FFFFFF},
-#endif
-};
+static const char *exec_name;
 
 static const struct codec_item {
-  char const *name;
+  char const              *name;
   const vpx_codec_iface_t *(*iface)(void);
+  const vpx_codec_iface_t *(*dx_iface)(void);
   unsigned int             fourcc;
-  unsigned int             fourcc_mask;
 } codecs[] = {
-#if CONFIG_VP9_ENCODER
-  {"vp9",  vpx_codec_vp8x_cx,   VP8_FOURCC, 0x00FFFFFF},
+#if CONFIG_VP9_ENCODER && CONFIG_VP9_DECODER
+  {"vp9", &vpx_codec_vp8_cx, &vpx_codec_vp8_dx, 0x30385056},
+#endif
+#if CONFIG_VP9_ENCODER && !CONFIG_VP9_DECODER
+  {"vp9", &vpx_codec_vp8_cx, NULL, 0x30385056},
 #endif
 };
 
-
 static void usage_exit();
 
+#define LOG_ERROR(label) do \
+  {\
+    const char *l=label;\
+    va_list ap;\
+    va_start(ap, fmt);\
+    if(l)\
+      fprintf(stderr, "%s: ", l);\
+    vfprintf(stderr, fmt, ap);\
+    fprintf(stderr, "\n");\
+    va_end(ap);\
+  } while(0)
+
 void die(const char *fmt, ...) {
-  va_list ap;
-  va_start(ap, fmt);
-  vfprintf(stderr, fmt, ap);
-  fprintf(stderr, "\n");
+  LOG_ERROR(NULL);
   usage_exit();
 }
 
-static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s) {
+
+void fatal(const char *fmt, ...) {
+  LOG_ERROR("Fatal");
+  exit(EXIT_FAILURE);
+}
+
+
+void warn(const char *fmt, ...) {
+  LOG_ERROR("Warning");
+}
+
+
+static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s, ...) {
+  va_list ap;
+
+  va_start(ap, s);
   if (ctx->err) {
     const char *detail = vpx_codec_error_detail(ctx);
 
-    fprintf(stderr, "%s: %s\n", s, vpx_codec_error(ctx));
+    vfprintf(stderr, s, ap);
+    fprintf(stderr, ": %s\n", vpx_codec_error(ctx));
 
     if (detail)
       fprintf(stderr, "    %s\n", detail);
@@ -161,21 +187,17 @@ int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
 
     stats->file = fopen(fpf, "rb");
 
-    if (fseek(stats->file, 0, SEEK_END)) {
-      fprintf(stderr, "First-pass stats file must be seekable!\n");
-      exit(EXIT_FAILURE);
-    }
+    if (fseek(stats->file, 0, SEEK_END))
+      fatal("First-pass stats file must be seekable!");
 
     stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
     rewind(stats->file);
 
     stats->buf.buf = malloc(stats->buf_alloc_sz);
 
-    if (!stats->buf.buf) {
-      fprintf(stderr, "Failed to allocate first-pass stats buffer (%lu bytes)\n",
-              (unsigned long)stats->buf_alloc_sz);
-      exit(EXIT_FAILURE);
-    }
+    if (!stats->buf.buf)
+      fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
+            (unsigned long)stats->buf_alloc_sz);
 
     nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
     res = (nbytes == stats->buf.sz);
@@ -222,7 +244,7 @@ void stats_close(stats_io_t *stats, int last_pass) {
 
 void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
   if (stats->file) {
-    if (fwrite(pkt, 1, len, stats->file));
+    (void) fwrite(pkt, 1, len, stats->file);
   } else {
     if (stats->buf.sz + len > stats->buf_alloc_sz) {
       size_t  new_sz = stats->buf_alloc_sz + 64 * 1024;
@@ -232,11 +254,8 @@ void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
         stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
         stats->buf.buf = new_ptr;
         stats->buf_alloc_sz = new_sz;
-      } else {
-        fprintf(stderr,
-                "\nFailed to realloc firstpass stats buffer.\n");
-        exit(EXIT_FAILURE);
-      }
+      } else
+        fatal("Failed to realloc firstpass stats buffer.");
     }
 
     memcpy(stats->buf_ptr, pkt, len);
@@ -271,9 +290,25 @@ struct detect_buffer {
 };
 
 
+struct input_state {
+  char                 *fn;
+  FILE                 *file;
+  y4m_input             y4m;
+  struct detect_buffer  detect;
+  enum video_file_type  file_type;
+  unsigned int          w;
+  unsigned int          h;
+  struct vpx_rational   framerate;
+  int                   use_i420;
+};
+
+
 #define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
-static int read_frame(FILE *f, vpx_image_t *img, unsigned int file_type,
-                      y4m_input *y4m, struct detect_buffer *detect) {
+static int read_frame(struct input_state *input, vpx_image_t *img) {
+  FILE *f = input->file;
+  enum video_file_type file_type = input->file_type;
+  y4m_input *y4m = &input->y4m;
+  struct detect_buffer *detect = &input->detect;
   int plane = 0;
   int shortread = 0;
 
@@ -288,7 +323,7 @@ static int read_frame(FILE *f, vpx_image_t *img, unsigned int file_type,
        * write_ivf_frame_header() for documentation on the frame header
        * layout.
        */
-      if (fread(junk, 1, IVF_FRAME_HDR_SZ, f));
+      (void) fread(junk, 1, IVF_FRAME_HDR_SZ, f);
     }
 
     for (plane = 0; plane < 3; plane++) {
@@ -346,13 +381,14 @@ unsigned int file_is_y4m(FILE      *infile,
 }
 
 #define IVF_FILE_HDR_SZ (32)
-unsigned int file_is_ivf(FILE *infile,
-                         unsigned int *fourcc,
-                         unsigned int *width,
-                         unsigned int *height,
-                         struct detect_buffer *detect) {
+unsigned int file_is_ivf(struct input_state *input,
+                         unsigned int *fourcc) {
   char raw_hdr[IVF_FILE_HDR_SZ];
   int is_ivf = 0;
+  FILE *infile = input->file;
+  unsigned int *width = &input->w;
+  unsigned int *height = &input->h;
+  struct detect_buffer *detect = &input->detect;
 
   if (memcmp(detect->buf, "DKIF", 4) != 0)
     return 0;
@@ -366,8 +402,8 @@ unsigned int file_is_ivf(FILE *infile,
       is_ivf = 1;
 
       if (mem_get_le16(raw_hdr + 4) != 0)
-        fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
-                " decode properly.");
+        warn("Unrecognized IVF version! This file may not decode "
+             "properly.");
 
       *fourcc = mem_get_le32(raw_hdr + 8);
     }
@@ -406,7 +442,7 @@ static void write_ivf_file_header(FILE *outfile,
   mem_put_le32(header + 24, frame_cnt);         /* length */
   mem_put_le32(header + 28, 0);                 /* unused */
 
-  if (fwrite(header, 1, 32, outfile));
+  (void) fwrite(header, 1, 32, outfile);
 }
 
 
@@ -419,11 +455,17 @@ static void write_ivf_frame_header(FILE *outfile,
     return;
 
   pts = pkt->data.frame.pts;
-  mem_put_le32(header, pkt->data.frame.sz);
+  mem_put_le32(header, (int)pkt->data.frame.sz);
   mem_put_le32(header + 4, pts & 0xFFFFFFFF);
   mem_put_le32(header + 8, pts >> 32);
 
-  if (fwrite(header, 1, 12, outfile));
+  (void) fwrite(header, 1, 12, outfile);
+}
+
+static void write_ivf_frame_size(FILE *outfile, size_t size) {
+  char             header[4];
+  mem_put_le32(header, (int)size);
+  (void) fwrite(header, 1, 4, outfile);
 }
 
 
@@ -468,13 +510,13 @@ struct EbmlGlobal {
 
 
 void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
-  if (fwrite(buffer_in, 1, len, glob->stream));
+  (void) fwrite(buffer_in, 1, len, glob->stream);
 }
 
 #define WRITE_BUFFER(s) \
   for(i = len-1; i>=0; i--)\
   { \
-    x = *(const s *)buffer_in >> (i * CHAR_BIT); \
+    x = (char)(*(const s *)buffer_in >> (i * CHAR_BIT)); \
     Ebml_Write(glob, &x, 1); \
   }
 void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len) {
@@ -520,9 +562,9 @@ static void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, u
 static void
 Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc,
                      unsigned long class_id) {
-  // todo this is always taking 8 bytes, this may need later optimization
-  // this is a key that says length unknown
-  uint64_t unknownLen =  LITERALU64(0x01FFFFFFFFFFFFFF);
+  /* todo this is always taking 8 bytes, this may need later optimization */
+  /* this is a key that says length unknown */
+  uint64_t unknownLen = LITERALU64(0x01FFFFFF, 0xFFFFFFFF);
 
   Ebml_WriteID(glob, class_id);
   *ebmlLoc = ftello(glob->stream);
@@ -539,7 +581,7 @@ Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) {
 
   /* Calculate the size of this element */
   size = pos - *ebmlLoc - 8;
-  size |=  LITERALU64(0x0100000000000000);
+  size |= LITERALU64(0x01000000, 0x00000000);
 
   /* Seek back to the beginning of the element and write the new size */
   fseeko(glob->stream, *ebmlLoc, SEEK_SET);
@@ -584,9 +626,20 @@ write_webm_seek_info(EbmlGlobal *ebml) {
     Ebml_EndSubElement(ebml, &start);
   }
   {
-    // segment info
+    /* segment info */
     EbmlLoc startInfo;
     uint64_t frame_time;
+    char version_string[64];
+
+    /* Assemble version string */
+    if (ebml->debug)
+      strcpy(version_string, "vpxenc");
+    else {
+      strcpy(version_string, "vpxenc ");
+      strncat(version_string,
+              vpx_codec_version_str(),
+              sizeof(version_string) - 1 - strlen(version_string));
+    }
 
     frame_time = (uint64_t)1000 * ebml->framerate.den
                  / ebml->framerate.num;
@@ -594,11 +647,9 @@ write_webm_seek_info(EbmlGlobal *ebml) {
     Ebml_StartSubElement(ebml, &startInfo, Info);
     Ebml_SerializeUnsigned(ebml, TimecodeScale, 1000000);
     Ebml_SerializeFloat(ebml, Segment_Duration,
-                        ebml->last_pts_ms + frame_time);
-    Ebml_SerializeString(ebml, 0x4D80,
-                         ebml->debug ? "vpxenc" : "vpxenc" VERSION_STRING);
-    Ebml_SerializeString(ebml, 0x5741,
-                         ebml->debug ? "vpxenc" : "vpxenc" VERSION_STRING);
+                        (double)(ebml->last_pts_ms + frame_time));
+    Ebml_SerializeString(ebml, 0x4D80, version_string);
+    Ebml_SerializeString(ebml, 0x5741, version_string);
     Ebml_EndSubElement(ebml, &startInfo);
   }
 }
@@ -613,16 +664,16 @@ write_webm_file_header(EbmlGlobal                *glob,
     EbmlLoc start;
     Ebml_StartSubElement(glob, &start, EBML);
     Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
-    Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1); // EBML Read Version
-    Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4); // EBML Max ID Length
-    Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8); // EBML Max Size Length
-    Ebml_SerializeString(glob, DocType, "webm"); // Doc Type
-    Ebml_SerializeUnsigned(glob, DocTypeVersion, 2); // Doc Type Version
-    Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2); // Doc Type Read Version
+    Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1);
+    Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4);
+    Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8);
+    Ebml_SerializeString(glob, DocType, "webm");
+    Ebml_SerializeUnsigned(glob, DocTypeVersion, 2);
+    Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2);
     Ebml_EndSubElement(glob, &start);
   }
   {
-    Ebml_StartSubElement(glob, &glob->startSegment, Segment); // segment
+    Ebml_StartSubElement(glob, &glob->startSegment, Segment);
     glob->position_reference = ftello(glob->stream);
     glob->framerate = *fps;
     write_webm_seek_info(glob);
@@ -640,7 +691,7 @@ write_webm_file_header(EbmlGlobal                *glob,
         Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
         glob->track_id_pos = ftello(glob->stream);
         Ebml_SerializeUnsigned32(glob, TrackUID, trackID);
-        Ebml_SerializeUnsigned(glob, TrackType, 1); // video is always 1
+        Ebml_SerializeUnsigned(glob, TrackType, 1);
         Ebml_SerializeString(glob, CodecID, "V_VP8");
         {
           unsigned int pixelWidth = cfg->g_w;
@@ -653,13 +704,13 @@ write_webm_file_header(EbmlGlobal                *glob,
           Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
           Ebml_SerializeUnsigned(glob, StereoMode, stereo_fmt);
           Ebml_SerializeFloat(glob, FrameRate, frameRate);
-          Ebml_EndSubElement(glob, &videoStart); // Video
+          Ebml_EndSubElement(glob, &videoStart);
         }
-        Ebml_EndSubElement(glob, &start); // Track Entry
+        Ebml_EndSubElement(glob, &start); /* Track Entry */
       }
       Ebml_EndSubElement(glob, &trackStart);
     }
-    // segment element is open
+    /* segment element is open */
   }
 }
 
@@ -686,7 +737,7 @@ write_webm_block(EbmlGlobal                *glob,
   if (pts_ms - glob->cluster_timecode > SHRT_MAX)
     start_cluster = 1;
   else
-    block_timecode = pts_ms - glob->cluster_timecode;
+    block_timecode = (unsigned short)pts_ms - glob->cluster_timecode;
 
   is_keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
   if (start_cluster || is_keyframe) {
@@ -696,9 +747,9 @@ write_webm_block(EbmlGlobal                *glob,
     /* Open the new cluster */
     block_timecode = 0;
     glob->cluster_open = 1;
-    glob->cluster_timecode = pts_ms;
+    glob->cluster_timecode = (uint32_t)pts_ms;
     glob->cluster_pos = ftello(glob->stream);
-    Ebml_StartSubElement(glob, &glob->startCluster, Cluster); // cluster
+    Ebml_StartSubElement(glob, &glob->startCluster, Cluster); /* cluster */
     Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode);
 
     /* Save a cue point if this is a keyframe. */
@@ -709,10 +760,8 @@ write_webm_block(EbmlGlobal                *glob,
                              (glob->cues + 1) * sizeof(struct cue_entry));
       if (new_cue_list)
         glob->cue_list = new_cue_list;
-      else {
-        fprintf(stderr, "\nFailed to realloc cue list.\n");
-        exit(EXIT_FAILURE);
-      }
+      else
+        fatal("Failed to realloc cue list.");
 
       cue = &glob->cue_list[glob->cues];
       cue->time = glob->cluster_timecode;
@@ -724,7 +773,7 @@ write_webm_block(EbmlGlobal                *glob,
   /* Write the Simple Block */
   Ebml_WriteID(glob, SimpleBlock);
 
-  block_length = pkt->data.frame.sz + 4;
+  block_length = (unsigned long)pkt->data.frame.sz + 4;
   block_length |= 0x10000000;
   Ebml_Serialize(glob, &block_length, sizeof(block_length), 4);
 
@@ -741,7 +790,7 @@ write_webm_block(EbmlGlobal                *glob,
     flags |= 0x08;
   Ebml_Write(glob, &flags, 1);
 
-  Ebml_Write(glob, pkt->data.frame.buf, pkt->data.frame.sz);
+  Ebml_Write(glob, pkt->data.frame.buf, (unsigned long)pkt->data.frame.sz);
 }
 
 
@@ -753,7 +802,7 @@ write_webm_file_footer(EbmlGlobal *glob, long hash) {
 
   {
     EbmlLoc start;
-    int i;
+    unsigned int i;
 
     glob->cue_pos = ftello(glob->stream);
     Ebml_StartSubElement(glob, &start, Cues);
@@ -771,7 +820,6 @@ write_webm_file_footer(EbmlGlobal *glob, long hash) {
         Ebml_SerializeUnsigned(glob, CueTrack, 1);
         Ebml_SerializeUnsigned64(glob, CueClusterPosition,
                                  cue->loc - glob->position_reference);
-        // Ebml_SerializeUnsigned(glob, CueBlockNumber, cue->blockNumber);
         Ebml_EndSubElement(glob, &start);
       }
       Ebml_EndSubElement(glob, &start);
@@ -847,7 +895,7 @@ static double vp8_mse2psnr(double Samples, double Peak, double Mse) {
   if ((double)Mse > 0.0)
     psnr = 10.0 * log10(Peak * Peak * Samples / Mse);
   else
-    psnr = MAX_PSNR;      // Limit to prevent / 0
+    psnr = MAX_PSNR;      /* Limit to prevent / 0 */
 
   if (psnr > MAX_PSNR)
     psnr = MAX_PSNR;
@@ -857,7 +905,6 @@ static double vp8_mse2psnr(double Samples, double Peak, double Mse) {
 
 
 #include "args.h"
-
 static const arg_def_t debugmode = ARG_DEF("D", "debug", 0,
                                            "Debug mode (makes output deterministic)");
 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
@@ -886,33 +933,29 @@ static const arg_def_t good_dl          = ARG_DEF(NULL, "good", 0,
                                                   "Use Good Quality Deadline");
 static const arg_def_t rt_dl            = ARG_DEF(NULL, "rt", 0,
                                                   "Use Realtime Quality Deadline");
+static const arg_def_t quietarg         = ARG_DEF("q", "quiet", 0,
+                                                  "Do not print encode progress");
 static const arg_def_t verbosearg       = ARG_DEF("v", "verbose", 0,
                                                   "Show encoder parameters");
 static const arg_def_t psnrarg          = ARG_DEF(NULL, "psnr", 0,
                                                   "Show PSNR in status line");
 static const arg_def_t recontest        = ARG_DEF(NULL, "test-decode", 0,
-                                                  "Test enocde/decode have machted recon buffer");
+                                                  "Test encode/decode mismatch");
 static const arg_def_t framerate        = ARG_DEF(NULL, "fps", 1,
                                                   "Stream frame rate (rate/scale)");
 static const arg_def_t use_ivf          = ARG_DEF(NULL, "ivf", 0,
                                                   "Output IVF (default is WebM)");
+static const arg_def_t out_part = ARG_DEF("P", "output-partitions", 0,
+                                          "Makes encoder output partitions. Requires IVF output!");
 static const arg_def_t q_hist_n         = ARG_DEF(NULL, "q-hist", 1,
                                                   "Show quantizer histogram (n-buckets)");
 static const arg_def_t rate_hist_n         = ARG_DEF(NULL, "rate-hist", 1,
                                                      "Show rate histogram (n-buckets)");
-#if CONFIG_LOSSLESS
-static const arg_def_t lossless_enabled = ARG_DEF(NULL, "lossless", 0,
-                                                  "Enable lossless compression");
-#endif
 static const arg_def_t *main_args[] = {
   &debugmode,
   &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &skip,
-  &deadline,
-  &best_dl, &good_dl, &rt_dl,
-  &verbosearg, &psnrarg, &recontest, &use_ivf, &q_hist_n, &rate_hist_n,
-#if CONFIG_LOSSLESS
-  &lossless_enabled,
-#endif
+  &deadline, &best_dl, &good_dl, &rt_dl,
+  &quietarg, &verbosearg, &psnrarg, &use_ivf, &out_part, &q_hist_n, &rate_hist_n,
   NULL
 };
 
@@ -1048,13 +1091,16 @@ static const arg_def_t cq_level = ARG_DEF(NULL, "cq-level", 1,
                                           "Constrained Quality Level");
 static const arg_def_t max_intra_rate_pct = ARG_DEF(NULL, "max-intra-rate", 1,
                                                     "Max I-frame bitrate (pct)");
+#if CONFIG_LOSSLESS
+static const arg_def_t lossless = ARG_DEF(NULL, "lossless", 1, "Lossless mode");
+#endif
 
 static const arg_def_t *vp8_args[] = {
   &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
   &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type,
   &tune_ssim, &cq_level, &max_intra_rate_pct,
 #if CONFIG_LOSSLESS
-  &lossless_enabled,
+  &lossless,
 #endif
   NULL
 };
@@ -1137,7 +1183,7 @@ static int merge_hist_buckets(struct hist_bucket *bucket,
   while (buckets > max_buckets) {
     int last_bucket = buckets - 1;
 
-    // merge the small bucket with an adjacent one.
+    /* merge the small bucket with an adjacent one. */
     if (small_bucket == 0)
       merge_bucket = 1;
     else if (small_bucket == last_bucket)
@@ -1230,7 +1276,7 @@ static void show_histogram(const struct hist_bucket *bucket,
     int j;
     float pct;
 
-    pct = 100.0 * (float)bucket[i].count / (float)total;
+    pct = (float)(100.0 * bucket[i].count / total);
     len = HIST_BAR_MAX * bucket[i].count / scale;
     if (len < 1)
       len = 1;
@@ -1293,7 +1339,7 @@ static void init_rate_histogram(struct rate_hist          *hist,
    */
   hist->samples = cfg->rc_buf_sz * 5 / 4 * fps->num / fps->den / 1000;
 
-  // prevent division by zero
+  /* prevent division by zero */
   if (hist->samples == 0)
     hist->samples = 1;
 
@@ -1324,7 +1370,7 @@ static void update_rate_histogram(struct rate_hist          *hist,
 
   idx = hist->frames++ % hist->samples;
   hist->pts[idx] = now;
-  hist->sz[idx] = pkt->data.frame.sz;
+  hist->sz[idx] = (int)pkt->data.frame.sz;
 
   if (now < cfg->rc_buf_initial_sz)
     return;
@@ -1345,15 +1391,15 @@ static void update_rate_histogram(struct rate_hist          *hist,
     return;
 
   avg_bitrate = sum_sz * 8 * 1000 / (now - then);
-  idx = avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000);
+  idx = (int)(avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000));
   if (idx < 0)
     idx = 0;
   if (idx > RATE_BINS - 1)
     idx = RATE_BINS - 1;
   if (hist->bucket[idx].low > avg_bitrate)
-    hist->bucket[idx].low = avg_bitrate;
+    hist->bucket[idx].low = (int)avg_bitrate;
   if (hist->bucket[idx].high < avg_bitrate)
-    hist->bucket[idx].high = avg_bitrate;
+    hist->bucket[idx].high = (int)avg_bitrate;
   hist->bucket[idx].count++;
   hist->total++;
 }
@@ -1377,7 +1423,8 @@ static void show_rate_histogram(struct rate_hist          *hist,
 }
 
 
-static int compare_img(vpx_image_t *img1, vpx_image_t *img2) {
+static int compare_img(vpx_image_t *img1, vpx_image_t *img2)
+{
   int match = 1;
   int i;
 
@@ -1386,86 +1433,113 @@ static int compare_img(vpx_image_t *img1, vpx_image_t *img2) {
   match &= (img1->h == img2->h);
 
   for (i = 0; i < img1->d_h; i++)
-    match &= (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
-                     img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
+    match &= (memcmp(img1->planes[VPX_PLANE_Y]+i*img1->stride[VPX_PLANE_Y],
+                     img2->planes[VPX_PLANE_Y]+i*img2->stride[VPX_PLANE_Y],
                      img1->d_w) == 0);
 
-  for (i = 0; i < img1->d_h / 2; i++)
-    match &= (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
-                     img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
-                     img1->d_w / 2) == 0);
+  for (i = 0; i < img1->d_h/2; i++)
+    match &= (memcmp(img1->planes[VPX_PLANE_U]+i*img1->stride[VPX_PLANE_U],
+                     img2->planes[VPX_PLANE_U]+i*img2->stride[VPX_PLANE_U],
+                     (img1->d_w + 1) / 2) == 0);
 
-  for (i = 0; i < img1->d_h / 2; i++)
-    match &= (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_U],
-                     img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_U],
-                     img1->d_w / 2) == 0);
+  for (i = 0; i < img1->d_h/2; i++)
+    match &= (memcmp(img1->planes[VPX_PLANE_V]+i*img1->stride[VPX_PLANE_U],
+                     img2->planes[VPX_PLANE_V]+i*img2->stride[VPX_PLANE_U],
+                     (img1->d_w + 1) / 2) == 0);
 
   return match;
 }
 
 
-#define ARG_CTRL_CNT_MAX 10
+#define NELEMENTS(x) (sizeof(x)/sizeof(x[0]))
+#define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map)
+
+
+/* Configuration elements common to all streams */
+struct global_config {
+  const struct codec_item  *codec;
+  int                       passes;
+  int                       pass;
+  int                       usage;
+  int                       deadline;
+  int                       use_i420;
+  int                       quiet;
+  int                       verbose;
+  int                       limit;
+  int                       skip_frames;
+  int                       show_psnr;
+  int                       test_decode;
+  int                       have_framerate;
+  struct vpx_rational       framerate;
+  int                       out_part;
+  int                       debug;
+  int                       show_q_hist_buckets;
+  int                       show_rate_hist_buckets;
+};
 
-int main(int argc, const char **argv_) {
-  vpx_codec_ctx_t        encoder;
-  const char            *in_fn = NULL, *out_fn = NULL, *stats_fn = NULL;
-  int                    i;
-  FILE                  *infile, *outfile;
-  vpx_codec_enc_cfg_t    cfg;
-  vpx_codec_err_t        res;
-  int                    pass, one_pass_only = 0;
-  stats_io_t             stats;
-  vpx_image_t            raw;
-  const struct codec_item  *codec = codecs;
-  int                    frame_avail, got_data;
 
-  struct arg               arg;
-  char                   **argv, **argi, **argj;
-  int                      arg_usage = 0, arg_passes = 1, arg_deadline = 0;
-  int                      arg_ctrls[ARG_CTRL_CNT_MAX][2], arg_ctrl_cnt = 0;
-  int                      arg_limit = 0;
-  int                      arg_skip  = 0;
-  static const arg_def_t **ctrl_args = no_args;
-  static const int        *ctrl_args_map = NULL;
-  int                      verbose = 0, show_psnr = 0, test_decode = 0;
-  int                      arg_use_i420 = 1;
-  unsigned long            cx_time = 0;
-  unsigned int             file_type, fourcc;
-  y4m_input                y4m;
-  struct vpx_rational      arg_framerate = {30, 1};
-  int                      arg_have_framerate = 0;
-  int                      write_webm = 1;
-  EbmlGlobal               ebml = {0};
-  uint32_t                 hash = 0;
-  uint64_t                 psnr_sse_total = 0;
-  uint64_t                 psnr_samples_total = 0;
-  double                   psnr_totals[4] = {0, 0, 0, 0};
-  int                      psnr_count = 0;
-  stereo_format_t          stereo_fmt = STEREO_FORMAT_MONO;
-  int                      counts[64] = {0};
-  int                      show_q_hist_buckets = 0;
-  int                      show_rate_hist_buckets = 0;
-  struct rate_hist         rate_hist = {0};
-
-  vpx_codec_ctx_t          decoder;
-  vpx_ref_frame_t          ref_enc;
-  vpx_ref_frame_t          ref_dec;
-  vpx_codec_dec_cfg_t      dec_cfg = {0};
-  int                      enc_dec_match = 1;
-  int                      first_bad_frame = -1;
-  int                      test_decode_frame = 0;
+/* Per-stream configuration */
+struct stream_config {
+  struct vpx_codec_enc_cfg  cfg;
+  const char               *out_fn;
+  const char               *stats_fn;
+  stereo_format_t           stereo_fmt;
+  int                       arg_ctrls[ARG_CTRL_CNT_MAX][2];
+  int                       arg_ctrl_cnt;
+  int                       write_webm;
+  int                       have_kf_max_dist;
+};
 
-  exec_name = argv_[0];
-  ebml.last_pts_ms = -1;
 
-  if (argc < 3)
-    usage_exit();
+struct stream_state {
+  int                       index;
+  struct stream_state      *next;
+  struct stream_config      config;
+  FILE                     *file;
+  struct rate_hist          rate_hist;
+  EbmlGlobal                ebml;
+  uint32_t                  hash;
+  uint64_t                  psnr_sse_total;
+  uint64_t                  psnr_samples_total;
+  double                    psnr_totals[4];
+  int                       psnr_count;
+  int                       counts[64];
+  vpx_codec_ctx_t           encoder;
+  unsigned int              frames_out;
+  uint64_t                  cx_time;
+  size_t                    nbytes;
+  stats_io_t                stats;
+  vpx_codec_ctx_t           decoder;
+  vpx_ref_frame_t           ref_enc;
+  vpx_ref_frame_t           ref_dec;
+  int                       mismatch_seen;
+};
 
 
-  /* First parse the codec and usage values, because we want to apply other
-   * parameters on top of the default configuration provided by the codec.
-   */
-  argv = argv_dup(argc - 1, argv_ + 1);
+void validate_positive_rational(const char          *msg,
+                                struct vpx_rational *rat) {
+  if (rat->den < 0) {
+    rat->num *= -1;
+    rat->den *= -1;
+  }
+
+  if (rat->num < 0)
+    die("Error: %s must be positive\n", msg);
+
+  if (!rat->den)
+    die("Error: %s has zero denominator\n", msg);
+}
+
+
+static void parse_global_config(struct global_config *global, char **argv) {
+  char       **argi, **argj;
+  struct arg   arg;
+
+  /* Initialize default parameters */
+  memset(global, 0, sizeof(*global));
+  global->codec = codecs;
+  global->passes = 1;
+  global->use_i420 = 1;
 
   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
     arg.argv_step = 1;
@@ -1478,655 +1552,943 @@ int main(int argc, const char **argv_) {
           k = j;
 
       if (k >= 0)
-        codec = codecs + k;
+        global->codec = codecs + k;
       else
         die("Error: Unrecognized argument (%s) to --codec\n",
             arg.val);
 
     } else if (arg_match(&arg, &passes, argi)) {
-      arg_passes = arg_parse_uint(&arg);
+      global->passes = arg_parse_uint(&arg);
 
-      if (arg_passes < 1 || arg_passes > 2)
-        die("Error: Invalid number of passes (%d)\n", arg_passes);
+      if (global->passes < 1 || global->passes > 2)
+        die("Error: Invalid number of passes (%d)\n", global->passes);
     } else if (arg_match(&arg, &pass_arg, argi)) {
-      one_pass_only = arg_parse_uint(&arg);
-
-      if (one_pass_only < 1 || one_pass_only > 2)
-        die("Error: Invalid pass selected (%d)\n", one_pass_only);
-    } else if (arg_match(&arg, &fpf_name, argi))
-      stats_fn = arg.val;
-    else if (arg_match(&arg, &usage, argi))
-      arg_usage = arg_parse_uint(&arg);
+      global->pass = arg_parse_uint(&arg);
+
+      if (global->pass < 1 || global->pass > 2)
+        die("Error: Invalid pass selected (%d)\n",
+            global->pass);
+    } else if (arg_match(&arg, &usage, argi))
+      global->usage = arg_parse_uint(&arg);
     else if (arg_match(&arg, &deadline, argi))
-      arg_deadline = arg_parse_uint(&arg);
+      global->deadline = arg_parse_uint(&arg);
     else if (arg_match(&arg, &best_dl, argi))
-      arg_deadline = VPX_DL_BEST_QUALITY;
+      global->deadline = VPX_DL_BEST_QUALITY;
     else if (arg_match(&arg, &good_dl, argi))
-      arg_deadline = VPX_DL_GOOD_QUALITY;
+      global->deadline = VPX_DL_GOOD_QUALITY;
     else if (arg_match(&arg, &rt_dl, argi))
-      arg_deadline = VPX_DL_REALTIME;
-    else if (arg_match(&arg, &use_yv12, argi)) {
-      arg_use_i420 = 0;
-    } else if (arg_match(&arg, &use_i420, argi)) {
-      arg_use_i420 = 1;
-    } else if (arg_match(&arg, &verbosearg, argi))
-      verbose = 1;
+      global->deadline = VPX_DL_REALTIME;
+    else if (arg_match(&arg, &use_yv12, argi))
+      global->use_i420 = 0;
+    else if (arg_match(&arg, &use_i420, argi))
+      global->use_i420 = 1;
+    else if (arg_match(&arg, &quietarg, argi))
+      global->quiet = 1;
+    else if (arg_match(&arg, &verbosearg, argi))
+      global->verbose = 1;
     else if (arg_match(&arg, &limit, argi))
-      arg_limit = arg_parse_uint(&arg);
+      global->limit = arg_parse_uint(&arg);
     else if (arg_match(&arg, &skip, argi))
-      arg_skip = arg_parse_uint(&arg);
+      global->skip_frames = arg_parse_uint(&arg);
     else if (arg_match(&arg, &psnrarg, argi))
-      show_psnr = 1;
+      global->show_psnr = 1;
     else if (arg_match(&arg, &recontest, argi))
-      test_decode = 1;
+      global->test_decode = 1;
     else if (arg_match(&arg, &framerate, argi)) {
-      arg_framerate = arg_parse_rational(&arg);
-      arg_have_framerate = 1;
-    } else if (arg_match(&arg, &use_ivf, argi))
-      write_webm = 0;
-    else if (arg_match(&arg, &outputfile, argi))
-      out_fn = arg.val;
+      global->framerate = arg_parse_rational(&arg);
+      validate_positive_rational(arg.name, &global->framerate);
+      global->have_framerate = 1;
+    } else if (arg_match(&arg, &out_part, argi))
+      global->out_part = 1;
     else if (arg_match(&arg, &debugmode, argi))
-      ebml.debug = 1;
+      global->debug = 1;
     else if (arg_match(&arg, &q_hist_n, argi))
-      show_q_hist_buckets = arg_parse_uint(&arg);
+      global->show_q_hist_buckets = arg_parse_uint(&arg);
     else if (arg_match(&arg, &rate_hist_n, argi))
-      show_rate_hist_buckets = arg_parse_uint(&arg);
+      global->show_rate_hist_buckets = arg_parse_uint(&arg);
     else
       argj++;
   }
 
-  /* Ensure that --passes and --pass are consistent. If --pass is set and --passes=2,
-   * ensure --fpf was set.
-   */
-  if (one_pass_only) {
+  /* Validate global config */
+
+  if (global->pass) {
     /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
-    if (one_pass_only > arg_passes) {
-      fprintf(stderr, "Warning: Assuming --pass=%d implies --passes=%d\n",
-              one_pass_only, one_pass_only);
-      arg_passes = one_pass_only;
+    if (global->pass > global->passes) {
+      warn("Assuming --pass=%d implies --passes=%d\n",
+           global->pass, global->pass);
+      global->passes = global->pass;
     }
+  }
+}
+
+
+void open_input_file(struct input_state *input) {
+  unsigned int fourcc;
+
+  /* Parse certain options from the input file, if possible */
+  input->file = strcmp(input->fn, "-") ? fopen(input->fn, "rb")
+                : set_binary_mode(stdin);
+
+  if (!input->file)
+    fatal("Failed to open input file");
 
-    if (arg_passes == 2 && !stats_fn)
-      die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only);
+  /* For RAW input sources, these bytes will applied on the first frame
+   *  in read_frame().
+   */
+  input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
+  input->detect.position = 0;
+
+  if (input->detect.buf_read == 4
+      && file_is_y4m(input->file, &input->y4m, input->detect.buf)) {
+    if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4) >= 0) {
+      input->file_type = FILE_TYPE_Y4M;
+      input->w = input->y4m.pic_w;
+      input->h = input->y4m.pic_h;
+      input->framerate.num = input->y4m.fps_n;
+      input->framerate.den = input->y4m.fps_d;
+      input->use_i420 = 0;
+    } else
+      fatal("Unsupported Y4M stream.");
+  } else if (input->detect.buf_read == 4 && file_is_ivf(input, &fourcc)) {
+    input->file_type = FILE_TYPE_IVF;
+    switch (fourcc) {
+      case 0x32315659:
+        input->use_i420 = 0;
+        break;
+      case 0x30323449:
+        input->use_i420 = 1;
+        break;
+      default:
+        fatal("Unsupported fourcc (%08x) in IVF", fourcc);
+    }
+  } else {
+    input->file_type = FILE_TYPE_RAW;
   }
+}
+
+
+static void close_input_file(struct input_state *input) {
+  fclose(input->file);
+  if (input->file_type == FILE_TYPE_Y4M)
+    y4m_input_close(&input->y4m);
+}
 
-  /* Populate encoder configuration */
-  res = vpx_codec_enc_config_default(codec->iface(), &cfg, arg_usage);
+static struct stream_state *new_stream(struct global_config *global,
+                                       struct stream_state  *prev) {
+  struct stream_state *stream;
+
+  stream = calloc(1, sizeof(*stream));
+  if (!stream)
+    fatal("Failed to allocate new stream.");
+  if (prev) {
+    memcpy(stream, prev, sizeof(*stream));
+    stream->index++;
+    prev->next = stream;
+  } else {
+    vpx_codec_err_t  res;
+
+    /* Populate encoder configuration */
+    res = vpx_codec_enc_config_default(global->codec->iface(),
+                                       &stream->config.cfg,
+                                       global->usage);
+    if (res)
+      fatal("Failed to get config: %s\n", vpx_codec_err_to_string(res));
 
-  if (res) {
-    fprintf(stderr, "Failed to get config: %s\n",
-            vpx_codec_err_to_string(res));
-    return EXIT_FAILURE;
+    /* Change the default timebase to a high enough value so that the
+     * encoder will always create strictly increasing timestamps.
+     */
+    stream->config.cfg.g_timebase.den = 1000;
+
+    /* Never use the library's default resolution, require it be parsed
+     * from the file or set on the command line.
+     */
+    stream->config.cfg.g_w = 0;
+    stream->config.cfg.g_h = 0;
+
+    /* Initialize remaining stream parameters */
+    stream->config.stereo_fmt = STEREO_FORMAT_MONO;
+    stream->config.write_webm = 1;
+    stream->ebml.last_pts_ms = -1;
+
+    /* Allows removal of the application version from the EBML tags */
+    stream->ebml.debug = global->debug;
   }
 
-  /* Change the default timebase to a high enough value so that the encoder
-   * will always create strictly increasing timestamps.
-   */
-  cfg.g_timebase.den = 1000;
+  /* Output files must be specified for each stream */
+  stream->config.out_fn = NULL;
 
-  /* Never use the library's default resolution, require it be parsed
-   * from the file or set on the command line.
-   */
-  cfg.g_w = 0;
-  cfg.g_h = 0;
+  stream->next = NULL;
+  return stream;
+}
+
+
+static int parse_stream_params(struct global_config *global,
+                               struct stream_state  *stream,
+                               char **argv) {
+  char                   **argi, **argj;
+  struct arg               arg;
+  static const arg_def_t **ctrl_args = no_args;
+  static const int        *ctrl_args_map = NULL;
+  struct stream_config    *config = &stream->config;
+  int                      eos_mark_found = 0;
+
+  /* Handle codec specific options */
+  if (global->codec->iface == vpx_codec_vp8x_cx) {
+    ctrl_args = vp8_args;
+    ctrl_args_map = vp8_arg_ctrl_map;
+  }
 
-  /* Now parse the remainder of the parameters. */
   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
     arg.argv_step = 1;
 
+    /* Once we've found an end-of-stream marker (--) we want to continue
+     * shifting arguments but not consuming them.
+     */
+    if (eos_mark_found) {
+      argj++;
+      continue;
+    } else if (!strcmp(*argj, "--")) {
+      eos_mark_found = 1;
+      continue;
+    }
+
     if (0);
+    else if (arg_match(&arg, &outputfile, argi))
+      config->out_fn = arg.val;
+    else if (arg_match(&arg, &fpf_name, argi))
+      config->stats_fn = arg.val;
+    else if (arg_match(&arg, &use_ivf, argi))
+      config->write_webm = 0;
     else if (arg_match(&arg, &threads, argi))
-      cfg.g_threads = arg_parse_uint(&arg);
+      config->cfg.g_threads = arg_parse_uint(&arg);
     else if (arg_match(&arg, &profile, argi))
-      cfg.g_profile = arg_parse_uint(&arg);
+      config->cfg.g_profile = arg_parse_uint(&arg);
     else if (arg_match(&arg, &width, argi))
-      cfg.g_w = arg_parse_uint(&arg);
+      config->cfg.g_w = arg_parse_uint(&arg);
     else if (arg_match(&arg, &height, argi))
-      cfg.g_h = arg_parse_uint(&arg);
+      config->cfg.g_h = arg_parse_uint(&arg);
     else if (arg_match(&arg, &stereo_mode, argi))
-      stereo_fmt = arg_parse_enum_or_int(&arg);
-    else if (arg_match(&arg, &timebase, argi))
-      cfg.g_timebase = arg_parse_rational(&arg);
-    else if (arg_match(&arg, &error_resilient, argi))
-      cfg.g_error_resilient = arg_parse_uint(&arg);
+      config->stereo_fmt = arg_parse_enum_or_int(&arg);
+    else if (arg_match(&arg, &timebase, argi)) {
+      config->cfg.g_timebase = arg_parse_rational(&arg);
+      validate_positive_rational(arg.name, &config->cfg.g_timebase);
+    } else if (arg_match(&arg, &error_resilient, argi))
+      config->cfg.g_error_resilient = arg_parse_uint(&arg);
     else if (arg_match(&arg, &lag_in_frames, argi))
-      cfg.g_lag_in_frames = arg_parse_uint(&arg);
+      config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
     else if (arg_match(&arg, &dropframe_thresh, argi))
-      cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
+      config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
     else if (arg_match(&arg, &resize_allowed, argi))
-      cfg.rc_resize_allowed = arg_parse_uint(&arg);
+      config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
     else if (arg_match(&arg, &resize_up_thresh, argi))
-      cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
-    else if (arg_match(&arg, &resize_down_thresh, argi))
-      cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
+      config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
     else if (arg_match(&arg, &resize_down_thresh, argi))
-      cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
+      config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
     else if (arg_match(&arg, &end_usage, argi))
-      cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
+      config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
     else if (arg_match(&arg, &target_bitrate, argi))
-      cfg.rc_target_bitrate = arg_parse_uint(&arg);
+      config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
     else if (arg_match(&arg, &min_quantizer, argi))
-      cfg.rc_min_quantizer = arg_parse_uint(&arg);
+      config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
     else if (arg_match(&arg, &max_quantizer, argi))
-      cfg.rc_max_quantizer = arg_parse_uint(&arg);
+      config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
     else if (arg_match(&arg, &undershoot_pct, argi))
-      cfg.rc_undershoot_pct = arg_parse_uint(&arg);
+      config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
     else if (arg_match(&arg, &overshoot_pct, argi))
-      cfg.rc_overshoot_pct = arg_parse_uint(&arg);
+      config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
     else if (arg_match(&arg, &buf_sz, argi))
-      cfg.rc_buf_sz = arg_parse_uint(&arg);
+      config->cfg.rc_buf_sz = arg_parse_uint(&arg);
     else if (arg_match(&arg, &buf_initial_sz, argi))
-      cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
+      config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
     else if (arg_match(&arg, &buf_optimal_sz, argi))
-      cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
+      config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
     else if (arg_match(&arg, &bias_pct, argi)) {
-      cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
+      config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
 
-      if (arg_passes < 2)
-        fprintf(stderr,
-                "Warning: option %s ignored in one-pass mode.\n",
-                arg.name);
+      if (global->passes < 2)
+        warn("option %s ignored in one-pass mode.\n", arg.name);
     } else if (arg_match(&arg, &minsection_pct, argi)) {
-      cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
+      config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
 
-      if (arg_passes < 2)
-        fprintf(stderr,
-                "Warning: option %s ignored in one-pass mode.\n",
-                arg.name);
+      if (global->passes < 2)
+        warn("option %s ignored in one-pass mode.\n", arg.name);
     } else if (arg_match(&arg, &maxsection_pct, argi)) {
-      cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
+      config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
 
-      if (arg_passes < 2)
-        fprintf(stderr,
-                "Warning: option %s ignored in one-pass mode.\n",
-                arg.name);
+      if (global->passes < 2)
+        warn("option %s ignored in one-pass mode.\n", arg.name);
     } else if (arg_match(&arg, &kf_min_dist, argi))
-      cfg.kf_min_dist = arg_parse_uint(&arg);
-    else if (arg_match(&arg, &kf_max_dist, argi))
-      cfg.kf_max_dist = arg_parse_uint(&arg);
-    else if (arg_match(&arg, &kf_disabled, argi))
-      cfg.kf_mode = VPX_KF_DISABLED;
-    else
-      argj++;
+      config->cfg.kf_min_dist = arg_parse_uint(&arg);
+    else if (arg_match(&arg, &kf_max_dist, argi)) {
+      config->cfg.kf_max_dist = arg_parse_uint(&arg);
+      config->have_kf_max_dist = 1;
+    } else if (arg_match(&arg, &kf_disabled, argi))
+      config->cfg.kf_mode = VPX_KF_DISABLED;
+    else {
+      int i, match = 0;
+
+      for (i = 0; ctrl_args[i]; i++) {
+        if (arg_match(&arg, ctrl_args[i], argi)) {
+          int j;
+          match = 1;
+
+          /* Point either to the next free element or the first
+          * instance of this control.
+          */
+          for (j = 0; j < config->arg_ctrl_cnt; j++)
+            if (config->arg_ctrls[j][0] == ctrl_args_map[i])
+              break;
+
+          /* Update/insert */
+          assert(j < ARG_CTRL_CNT_MAX);
+          if (j < ARG_CTRL_CNT_MAX) {
+            config->arg_ctrls[j][0] = ctrl_args_map[i];
+            config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
+            if (j == config->arg_ctrl_cnt)
+              config->arg_ctrl_cnt++;
+          }
+
+        }
+      }
+
+      if (!match)
+        argj++;
+    }
   }
 
-  /* Handle codec specific options */
-#if CONFIG_VP9_ENCODER
+  return eos_mark_found;
+}
 
-  if (codec->fourcc == VP8_FOURCC) {
-    ctrl_args = vp8_args;
-    ctrl_args_map = vp8_arg_ctrl_map;
+
+#define FOREACH_STREAM(func)\
+  do\
+  {\
+    struct stream_state  *stream;\
+    \
+    for(stream = streams; stream; stream = stream->next)\
+      func;\
+  }while(0)
+
+
+static void validate_stream_config(struct stream_state *stream) {
+  struct stream_state *streami;
+
+  if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
+    fatal("Stream %d: Specify stream dimensions with --width (-w) "
+          " and --height (-h)", stream->index);
+
+  for (streami = stream; streami; streami = streami->next) {
+    /* All streams require output files */
+    if (!streami->config.out_fn)
+      fatal("Stream %d: Output file is required (specify with -o)",
+            streami->index);
+
+    /* Check for two streams outputting to the same file */
+    if (streami != stream) {
+      const char *a = stream->config.out_fn;
+      const char *b = streami->config.out_fn;
+      if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
+        fatal("Stream %d: duplicate output file (from stream %d)",
+              streami->index, stream->index);
+    }
+
+    /* Check for two streams sharing a stats file. */
+    if (streami != stream) {
+      const char *a = stream->config.stats_fn;
+      const char *b = streami->config.stats_fn;
+      if (a && b && !strcmp(a, b))
+        fatal("Stream %d: duplicate stats file (from stream %d)",
+              streami->index, stream->index);
+    }
   }
+}
 
-#endif
 
-  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
-    int match = 0;
+static void set_stream_dimensions(struct stream_state *stream,
+                                  unsigned int w,
+                                  unsigned int h) {
+  if ((stream->config.cfg.g_w && stream->config.cfg.g_w != w)
+      || (stream->config.cfg.g_h && stream->config.cfg.g_h != h))
+    fatal("Stream %d: Resizing not yet supported", stream->index);
+  stream->config.cfg.g_w = w;
+  stream->config.cfg.g_h = h;
+}
 
-    arg.argv_step = 1;
 
-    for (i = 0; ctrl_args[i]; i++) {
-      if (arg_match(&arg, ctrl_args[i], argi)) {
-        match = 1;
+static void set_default_kf_interval(struct stream_state  *stream,
+                                    struct global_config *global) {
+  /* Use a max keyframe interval of 5 seconds, if none was
+   * specified on the command line.
+   */
+  if (!stream->config.have_kf_max_dist) {
+    double framerate = (double)global->framerate.num / global->framerate.den;
+    if (framerate > 0.0)
+      stream->config.cfg.kf_max_dist = (unsigned int)(5.0 * framerate);
+  }
+}
 
-        if (arg_ctrl_cnt < ARG_CTRL_CNT_MAX) {
-          arg_ctrls[arg_ctrl_cnt][0] = ctrl_args_map[i];
-          arg_ctrls[arg_ctrl_cnt][1] = arg_parse_enum_or_int(&arg);
-          arg_ctrl_cnt++;
-        }
-      }
-    }
 
-    if (!match)
-      argj++;
+static void show_stream_config(struct stream_state  *stream,
+                               struct global_config *global,
+                               struct input_state   *input) {
+
+#define SHOW(field) \
+  fprintf(stderr, "    %-28s = %d\n", #field, stream->config.cfg.field)
+
+  if (stream->index == 0) {
+    fprintf(stderr, "Codec: %s\n",
+            vpx_codec_iface_name(global->codec->iface()));
+    fprintf(stderr, "Source file: %s Format: %s\n", input->fn,
+            input->use_i420 ? "I420" : "YV12");
   }
+  if (stream->next || stream->index)
+    fprintf(stderr, "\nStream Index: %d\n", stream->index);
+  fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
+  fprintf(stderr, "Encoder parameters:\n");
+
+  SHOW(g_usage);
+  SHOW(g_threads);
+  SHOW(g_profile);
+  SHOW(g_w);
+  SHOW(g_h);
+  SHOW(g_timebase.num);
+  SHOW(g_timebase.den);
+  SHOW(g_error_resilient);
+  SHOW(g_pass);
+  SHOW(g_lag_in_frames);
+  SHOW(rc_dropframe_thresh);
+  SHOW(rc_resize_allowed);
+  SHOW(rc_resize_up_thresh);
+  SHOW(rc_resize_down_thresh);
+  SHOW(rc_end_usage);
+  SHOW(rc_target_bitrate);
+  SHOW(rc_min_quantizer);
+  SHOW(rc_max_quantizer);
+  SHOW(rc_undershoot_pct);
+  SHOW(rc_overshoot_pct);
+  SHOW(rc_buf_sz);
+  SHOW(rc_buf_initial_sz);
+  SHOW(rc_buf_optimal_sz);
+  SHOW(rc_2pass_vbr_bias_pct);
+  SHOW(rc_2pass_vbr_minsection_pct);
+  SHOW(rc_2pass_vbr_maxsection_pct);
+  SHOW(kf_mode);
+  SHOW(kf_min_dist);
+  SHOW(kf_max_dist);
+}
 
-  /* Check for unrecognized options */
-  for (argi = argv; *argi; argi++)
-    if (argi[0][0] == '-' && argi[0][1])
-      die("Error: Unrecognized option %s\n", *argi);
 
-  /* Handle non-option arguments */
-  in_fn = argv[0];
+static void open_output_file(struct stream_state *stream,
+                             struct global_config *global) {
+  const char *fn = stream->config.out_fn;
 
-  if (!in_fn)
-    usage_exit();
+  stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
 
-  if (!out_fn)
-    die("Error: Output file is required (specify with -o)\n");
+  if (!stream->file)
+    fatal("Failed to open output file");
 
-  memset(&stats, 0, sizeof(stats));
+  if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
+    fatal("WebM output to pipes not supported.");
 
-  for (pass = one_pass_only ? one_pass_only - 1 : 0; pass < arg_passes; pass++) {
-    int frames_in = 0, frames_out = 0;
-    unsigned long nbytes = 0;
-    int skip_frames = 0;
-    struct detect_buffer detect;
+  if (stream->config.write_webm) {
+    stream->ebml.stream = stream->file;
+    write_webm_file_header(&stream->ebml, &stream->config.cfg,
+                           &global->framerate,
+                           stream->config.stereo_fmt);
+  } else
+    write_ivf_file_header(stream->file, &stream->config.cfg,
+                          global->codec->fourcc, 0);
+}
 
-    /* Parse certain options from the input file, if possible */
-    infile = strcmp(in_fn, "-") ? fopen(in_fn, "rb")
-             : set_binary_mode(stdin);
 
-    if (!infile) {
-      fprintf(stderr, "Failed to open input file\n");
-      return EXIT_FAILURE;
-    }
+static void close_output_file(struct stream_state *stream,
+                              unsigned int         fourcc) {
+  if (stream->config.write_webm) {
+    write_webm_file_footer(&stream->ebml, stream->hash);
+    free(stream->ebml.cue_list);
+    stream->ebml.cue_list = NULL;
+  } else {
+    if (!fseek(stream->file, 0, SEEK_SET))
+      write_ivf_file_header(stream->file, &stream->config.cfg,
+                            fourcc,
+                            stream->frames_out);
+  }
 
-    /* For RAW input sources, these bytes will applied on the first frame
-     *  in read_frame().
-     */
-    detect.buf_read = fread(detect.buf, 1, 4, infile);
-    detect.position = 0;
-
-    if (detect.buf_read == 4 && file_is_y4m(infile, &y4m, detect.buf)) {
-      if (y4m_input_open(&y4m, infile, detect.buf, 4) >= 0) {
-        file_type = FILE_TYPE_Y4M;
-        cfg.g_w = y4m.pic_w;
-        cfg.g_h = y4m.pic_h;
-
-        /* Use the frame rate from the file only if none was specified
-         * on the command-line.
-         */
-        if (!arg_have_framerate) {
-          arg_framerate.num = y4m.fps_n;
-          arg_framerate.den = y4m.fps_d;
+  fclose(stream->file);
+}
+
+
+static void setup_pass(struct stream_state  *stream,
+                       struct global_config *global,
+                       int                   pass) {
+  if (stream->config.stats_fn) {
+    if (!stats_open_file(&stream->stats, stream->config.stats_fn,
+                         pass))
+      fatal("Failed to open statistics store");
+  } else {
+    if (!stats_open_mem(&stream->stats, pass))
+      fatal("Failed to open statistics store");
+  }
+
+  stream->config.cfg.g_pass = global->passes == 2
+                              ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
+                            : VPX_RC_ONE_PASS;
+  if (pass)
+    stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
+
+  stream->cx_time = 0;
+  stream->nbytes = 0;
+  stream->frames_out = 0;
+}
+
+
+static void initialize_encoder(struct stream_state  *stream,
+                               struct global_config *global) {
+  int i;
+  int flags = 0;
+
+  flags |= global->show_psnr ? VPX_CODEC_USE_PSNR : 0;
+  flags |= global->out_part ? VPX_CODEC_USE_OUTPUT_PARTITION : 0;
+
+  /* Construct Encoder Context */
+  vpx_codec_enc_init(&stream->encoder, global->codec->iface(),
+                     &stream->config.cfg, flags);
+  ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
+
+  /* Note that we bypass the vpx_codec_control wrapper macro because
+   * we're being clever to store the control IDs in an array. Real
+   * applications will want to make use of the enumerations directly
+   */
+  for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
+    int ctrl = stream->config.arg_ctrls[i][0];
+    int value = stream->config.arg_ctrls[i][1];
+    if (vpx_codec_control_(&stream->encoder, ctrl, value))
+      fprintf(stderr, "Error: Tried to set control %d = %d\n",
+              ctrl, value);
+
+    ctx_exit_on_error(&stream->encoder, "Failed to control codec");
+  }
+
+  if (global->test_decode) {
+    int width, height;
+
+    vpx_codec_dec_init(&stream->decoder, global->codec->dx_iface(), NULL, 0);
+
+    width = (stream->config.cfg.g_w + 15) & ~15;
+    height = (stream->config.cfg.g_h + 15) & ~15;
+    vpx_img_alloc(&stream->ref_enc.img, VPX_IMG_FMT_I420, width, height, 1);
+    vpx_img_alloc(&stream->ref_dec.img, VPX_IMG_FMT_I420, width, height, 1);
+    stream->ref_enc.frame_type = VP8_LAST_FRAME;
+    stream->ref_dec.frame_type = VP8_LAST_FRAME;
+  }
+}
+
+
+static void encode_frame(struct stream_state  *stream,
+                         struct global_config *global,
+                         struct vpx_image     *img,
+                         unsigned int          frames_in) {
+  vpx_codec_pts_t frame_start, next_frame_start;
+  struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
+  struct vpx_usec_timer timer;
+
+  frame_start = (cfg->g_timebase.den * (int64_t)(frames_in - 1)
+                 * global->framerate.den)
+                / cfg->g_timebase.num / global->framerate.num;
+  next_frame_start = (cfg->g_timebase.den * (int64_t)(frames_in)
+                      * global->framerate.den)
+                     / cfg->g_timebase.num / global->framerate.num;
+  vpx_usec_timer_start(&timer);
+  vpx_codec_encode(&stream->encoder, img, frame_start,
+                   (unsigned long)(next_frame_start - frame_start),
+                   0, global->deadline);
+  vpx_usec_timer_mark(&timer);
+  stream->cx_time += vpx_usec_timer_elapsed(&timer);
+  ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
+                    stream->index);
+}
+
+
+static void update_quantizer_histogram(struct stream_state *stream) {
+  if (stream->config.cfg.g_pass != VPX_RC_FIRST_PASS) {
+    int q;
+
+    vpx_codec_control(&stream->encoder, VP8E_GET_LAST_QUANTIZER_64, &q);
+    ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
+    stream->counts[q]++;
+  }
+}
+
+
+static void get_cx_data(struct stream_state  *stream,
+                        struct global_config *global,
+                        int                  *got_data) {
+  const vpx_codec_cx_pkt_t *pkt;
+  const struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
+  vpx_codec_iter_t iter = NULL;
+
+  while ((pkt = vpx_codec_get_cx_data(&stream->encoder, &iter))) {
+    static size_t fsize = 0;
+    static off_t ivf_header_pos = 0;
+
+    *got_data = 1;
+
+    switch (pkt->kind) {
+      case VPX_CODEC_CX_FRAME_PKT:
+        if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
+          stream->frames_out++;
         }
+        if (!global->quiet)
+          fprintf(stderr, " %6luF",
+                  (unsigned long)pkt->data.frame.sz);
+
+        update_rate_histogram(&stream->rate_hist, cfg, pkt);
+        if (stream->config.write_webm) {
+          /* Update the hash */
+          if (!stream->ebml.debug)
+            stream->hash = murmur(pkt->data.frame.buf,
+                                  (int)pkt->data.frame.sz,
+                                  stream->hash);
+
+          write_webm_block(&stream->ebml, cfg, pkt);
+        } else {
+          if (pkt->data.frame.partition_id <= 0) {
+            ivf_header_pos = ftello(stream->file);
+            fsize = pkt->data.frame.sz;
+
+            write_ivf_frame_header(stream->file, pkt);
+          } else {
+            fsize += pkt->data.frame.sz;
+
+            if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
+              off_t currpos = ftello(stream->file);
+              fseeko(stream->file, ivf_header_pos, SEEK_SET);
+              write_ivf_frame_size(stream->file, fsize);
+              fseeko(stream->file, currpos, SEEK_SET);
+            }
+          }
 
-        arg_use_i420 = 0;
-      } else {
-        fprintf(stderr, "Unsupported Y4M stream.\n");
-        return EXIT_FAILURE;
-      }
-    } else if (detect.buf_read == 4 &&
-               file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h, &detect)) {
-      file_type = FILE_TYPE_IVF;
-      switch (fourcc) {
-        case 0x32315659:
-          arg_use_i420 = 0;
-          break;
-        case 0x30323449:
-          arg_use_i420 = 1;
-          break;
-        default:
-          fprintf(stderr, "Unsupported fourcc (%08x) in IVF\n", fourcc);
-          return EXIT_FAILURE;
-      }
-    } else {
-      file_type = FILE_TYPE_RAW;
-    }
+          (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
+                        stream->file);
+        }
+        stream->nbytes += pkt->data.raw.sz;
 
-    if (!cfg.g_w || !cfg.g_h) {
-      fprintf(stderr, "Specify stream dimensions with --width (-w) "
-              " and --height (-h).\n");
-      return EXIT_FAILURE;
-    }
+        if (global->test_decode) {
+          vpx_codec_decode(&stream->decoder, pkt->data.frame.buf,
+                           pkt->data.frame.sz, NULL, 0);
+          ctx_exit_on_error(&stream->decoder, "Failed to decode frame");
+        }
+        break;
+      case VPX_CODEC_STATS_PKT:
+        stream->frames_out++;
+        fprintf(stderr, " %6luS",
+                (unsigned long)pkt->data.twopass_stats.sz);
+        stats_write(&stream->stats,
+                    pkt->data.twopass_stats.buf,
+                    pkt->data.twopass_stats.sz);
+        stream->nbytes += pkt->data.raw.sz;
+        break;
+      case VPX_CODEC_PSNR_PKT:
+
+        if (global->show_psnr) {
+          int i;
+
+          stream->psnr_sse_total += pkt->data.psnr.sse[0];
+          stream->psnr_samples_total += pkt->data.psnr.samples[0];
+          for (i = 0; i < 4; i++) {
+            if (!global->quiet)
+              fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
+            stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
+          }
+          stream->psnr_count++;
+        }
 
-#define SHOW(field) fprintf(stderr, "    %-28s = %d\n", #field, cfg.field)
-
-    if (verbose && pass == 0) {
-      fprintf(stderr, "Codec: %s\n", vpx_codec_iface_name(codec->iface()));
-      fprintf(stderr, "Source file: %s Format: %s\n", in_fn,
-              arg_use_i420 ? "I420" : "YV12");
-      fprintf(stderr, "Destination file: %s\n", out_fn);
-      fprintf(stderr, "Encoder parameters:\n");
-
-      SHOW(g_usage);
-      SHOW(g_threads);
-      SHOW(g_profile);
-      SHOW(g_w);
-      SHOW(g_h);
-      SHOW(g_timebase.num);
-      SHOW(g_timebase.den);
-      SHOW(g_error_resilient);
-      SHOW(g_pass);
-      SHOW(g_lag_in_frames);
-      SHOW(rc_dropframe_thresh);
-      SHOW(rc_resize_allowed);
-      SHOW(rc_resize_up_thresh);
-      SHOW(rc_resize_down_thresh);
-      SHOW(rc_end_usage);
-      SHOW(rc_target_bitrate);
-      SHOW(rc_min_quantizer);
-      SHOW(rc_max_quantizer);
-      SHOW(rc_undershoot_pct);
-      SHOW(rc_overshoot_pct);
-      SHOW(rc_buf_sz);
-      SHOW(rc_buf_initial_sz);
-      SHOW(rc_buf_optimal_sz);
-      SHOW(rc_2pass_vbr_bias_pct);
-      SHOW(rc_2pass_vbr_minsection_pct);
-      SHOW(rc_2pass_vbr_maxsection_pct);
-      SHOW(kf_mode);
-      SHOW(kf_min_dist);
-      SHOW(kf_max_dist);
+        break;
+      default:
+        break;
     }
+  }
+}
 
-    if (pass == (one_pass_only ? one_pass_only - 1 : 0)) {
-      if (file_type == FILE_TYPE_Y4M)
-        /*The Y4M reader does its own allocation.
-          Just initialize this here to avoid problems if we never read any
-           frames.*/
-        memset(&raw, 0, sizeof(raw));
-      else
-        vpx_img_alloc(&raw, arg_use_i420 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_YV12,
-                      cfg.g_w, cfg.g_h, 1);
 
+static void show_psnr(struct stream_state  *stream) {
+  int i;
+  double ovpsnr;
 
-      init_rate_histogram(&rate_hist, &cfg, &arg_framerate);
-    }
+  if (!stream->psnr_count)
+    return;
 
-    if (test_decode) {
-      int width = cfg.g_w;
-      int height = cfg.g_h;
-      width = (width + 15)& ~15;
-      height = (height + 15) & ~15;
+  fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
+  ovpsnr = vp8_mse2psnr((double)stream->psnr_samples_total, 255.0,
+                        (double)stream->psnr_sse_total);
+  fprintf(stderr, " %.3f", ovpsnr);
 
-      vpx_img_alloc(&ref_enc.img, VPX_IMG_FMT_I420,
-                    width, height, 1);
-      vpx_img_alloc(&ref_dec.img, VPX_IMG_FMT_I420,
-                    width, height, 1);
-    }
+  for (i = 0; i < 4; i++) {
+    fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
+  }
+  fprintf(stderr, "\n");
+}
 
-    outfile = strcmp(out_fn, "-") ? fopen(out_fn, "wb")
-              : set_binary_mode(stdout);
 
-    if (!outfile) {
-      fprintf(stderr, "Failed to open output file\n");
-      return EXIT_FAILURE;
-    }
+float usec_to_fps(uint64_t usec, unsigned int frames) {
+  return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
+}
 
-    if (write_webm && fseek(outfile, 0, SEEK_CUR)) {
-      fprintf(stderr, "WebM output to pipes not supported.\n");
-      return EXIT_FAILURE;
-    }
 
-    if (stats_fn) {
-      if (!stats_open_file(&stats, stats_fn, pass)) {
-        fprintf(stderr, "Failed to open statistics store\n");
-        return EXIT_FAILURE;
-      }
-    } else {
-      if (!stats_open_mem(&stats, pass)) {
-        fprintf(stderr, "Failed to open statistics store\n");
-        return EXIT_FAILURE;
-      }
-    }
+static void test_decode(struct stream_state  *stream, unsigned int frames_in) {
+  vpx_codec_control(&stream->encoder, VP8_COPY_REFERENCE, &stream->ref_enc);
+  ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
+  vpx_codec_control(&stream->decoder, VP8_COPY_REFERENCE, &stream->ref_dec);
+  ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
 
-    cfg.g_pass = arg_passes == 2
-                 ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
-               : VPX_RC_ONE_PASS;
-#if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
+  if (!stream->mismatch_seen
+      && !compare_img(&stream->ref_enc.img, &stream->ref_dec.img)) {
+    /* TODO(jkoleszar): make fatal. */
+    warn("Stream %d: Encode/decode mismatch on frame %d",
+         stream->index, frames_in);
+    stream->mismatch_seen = frames_in;
+  }
+}
 
-    if (pass) {
-      cfg.rc_twopass_stats_in = stats_get(&stats);
-    }
+int main(int argc, const char **argv_) {
+  int                    pass;
+  vpx_image_t            raw;
+  int                    frame_avail, got_data;
 
-#endif
+  struct input_state       input = {0};
+  struct global_config     global;
+  struct stream_state     *streams = NULL;
+  char                   **argv, **argi;
+  unsigned long            cx_time = 0;
+  int                      stream_cnt = 0;
 
-    if (write_webm) {
-      ebml.stream = outfile;
-      write_webm_file_header(&ebml, &cfg, &arg_framerate, stereo_fmt);
-    } else
-      write_ivf_file_header(outfile, &cfg, codec->fourcc, 0);
+  exec_name = argv_[0];
 
+  if (argc < 3)
+    usage_exit();
 
-    /* Construct Encoder Context */
-    vpx_codec_enc_init(&encoder, codec->iface(), &cfg,
-                       show_psnr ? VPX_CODEC_USE_PSNR : 0);
-    ctx_exit_on_error(&encoder, "Failed to initialize encoder");
+  /* Setup default input stream settings */
+  input.framerate.num = 30;
+  input.framerate.den = 1;
+  input.use_i420 = 1;
 
-    if (test_decode &&
-        vpx_codec_dec_init(&decoder, ifaces[0].iface(), &dec_cfg, 0)) {
-      fprintf(stderr,
-              "Failed to initialize decoder: %s\n",
-              vpx_codec_error(&decoder));
-      return EXIT_FAILURE;
-    }
-    /* Note that we bypass the vpx_codec_control wrapper macro because
-     * we're being clever to store the control IDs in an array. Real
-     * applications will want to make use of the enumerations directly
+  /* First parse the global configuration values, because we want to apply
+   * other parameters on top of the default configuration provided by the
+   * codec.
+   */
+  argv = argv_dup(argc - 1, argv_ + 1);
+  parse_global_config(&global, argv);
+
+  {
+    /* Now parse each stream's parameters. Using a local scope here
+     * due to the use of 'stream' as loop variable in FOREACH_STREAM
+     * loops
      */
-    for (i = 0; i < arg_ctrl_cnt; i++) {
-      if (vpx_codec_control_(&encoder, arg_ctrls[i][0], arg_ctrls[i][1]))
-        fprintf(stderr, "Error: Tried to set control %d = %d\n",
-                arg_ctrls[i][0], arg_ctrls[i][1]);
-      ctx_exit_on_error(&encoder, "Failed to control codec");
-    }
+    struct stream_state *stream = NULL;
+
+    do {
+      stream = new_stream(&global, stream);
+      stream_cnt++;
+      if (!streams)
+        streams = stream;
+    } while (parse_stream_params(&global, stream, argv));
+  }
 
-    frame_avail = 1;
-    got_data = 0;
+  /* Check for unrecognized options */
+  for (argi = argv; *argi; argi++)
+    if (argi[0][0] == '-' && argi[0][1])
+      die("Error: Unrecognized option %s\n", *argi);
 
-    skip_frames = arg_skip;
-    while (skip_frames) {
-      frame_avail = read_frame(infile, &raw, file_type, &y4m, &detect);
-      if (!frame_avail)
+  /* Handle non-option arguments */
+  input.fn = argv[0];
+
+  if (!input.fn)
+    usage_exit();
+
+  for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
+    int frames_in = 0;
+
+    open_input_file(&input);
+
+    /* If the input file doesn't specify its w/h (raw files), try to get
+     * the data from the first stream's configuration.
+     */
+    if (!input.w || !input.h)
+      FOREACH_STREAM( {
+      if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
+        input.w = stream->config.cfg.g_w;
+        input.h = stream->config.cfg.g_h;
         break;
-      skip_frames--;
-      fprintf(stderr,
-              "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
-              arg_passes, skip_frames, frames_out, nbytes);
+      }
+    });
+
+    /* Update stream configurations from the input file's parameters */
+    FOREACH_STREAM(set_stream_dimensions(stream, input.w, input.h));
+    FOREACH_STREAM(validate_stream_config(stream));
+
+    /* Ensure that --passes and --pass are consistent. If --pass is set and
+     * --passes=2, ensure --fpf was set.
+     */
+    if (global.pass && global.passes == 2)
+      FOREACH_STREAM( {
+      if (!stream->config.stats_fn)
+        die("Stream %d: Must specify --fpf when --pass=%d"
+        " and --passes=2\n", stream->index, global.pass);
+    });
+
+
+    /* Use the frame rate from the file only if none was specified
+     * on the command-line.
+     */
+    if (!global.have_framerate)
+      global.framerate = input.framerate;
+
+    FOREACH_STREAM(set_default_kf_interval(stream, &global));
+
+    /* Show configuration */
+    if (global.verbose && pass == 0)
+      FOREACH_STREAM(show_stream_config(stream, &global, &input));
+
+    if (pass == (global.pass ? global.pass - 1 : 0)) {
+      if (input.file_type == FILE_TYPE_Y4M)
+        /*The Y4M reader does its own allocation.
+          Just initialize this here to avoid problems if we never read any
+           frames.*/
+        memset(&raw, 0, sizeof(raw));
+      else
+        vpx_img_alloc(&raw,
+                      input.use_i420 ? VPX_IMG_FMT_I420
+                      : VPX_IMG_FMT_YV12,
+                      input.w, input.h, 32);
+
+      FOREACH_STREAM(init_rate_histogram(&stream->rate_hist,
+                                         &stream->config.cfg,
+                                         &global.framerate));
     }
 
+    FOREACH_STREAM(open_output_file(stream, &global));
+    FOREACH_STREAM(setup_pass(stream, &global, pass));
+    FOREACH_STREAM(initialize_encoder(stream, &global));
+
+    frame_avail = 1;
+    got_data = 0;
+
     while (frame_avail || got_data) {
-      vpx_codec_iter_t iter = NULL;
-      vpx_codec_iter_t dec_iter = NULL;
-      const vpx_codec_cx_pkt_t *pkt;
       struct vpx_usec_timer timer;
-      int64_t frame_start, next_frame_start;
 
-      if (!arg_limit || frames_in < arg_limit) {
-        frame_avail = read_frame(infile, &raw, file_type, &y4m,
-                                 &detect);
+      if (!global.limit || frames_in < global.limit) {
+        frame_avail = read_frame(&input, &raw);
+
         if (frame_avail)
           frames_in++;
+
+        if (!global.quiet) {
+          if (stream_cnt == 1)
+            fprintf(stderr,
+                    "\rPass %d/%d frame %4d/%-4d %7"PRId64"B \033[K",
+                    pass + 1, global.passes, frames_in,
+                    streams->frames_out, (int64_t)streams->nbytes);
+          else
+            fprintf(stderr,
+                    "\rPass %d/%d frame %4d %7lu %s (%.2f fps)\033[K",
+                    pass + 1, global.passes, frames_in,
+                    cx_time > 9999999 ? cx_time / 1000 : cx_time,
+                    cx_time > 9999999 ? "ms" : "us",
+                    usec_to_fps(cx_time, frames_in));
+        }
+
       } else
         frame_avail = 0;
 
-      vpx_usec_timer_start(&timer);
-
-      frame_start = (cfg.g_timebase.den * (int64_t)(frames_in - 1)
-                     * arg_framerate.den) / cfg.g_timebase.num / arg_framerate.num;
-      next_frame_start = (cfg.g_timebase.den * (int64_t)(frames_in)
-                          * arg_framerate.den)
-                         / cfg.g_timebase.num / arg_framerate.num;
-      vpx_codec_encode(&encoder, frame_avail ? &raw : NULL, frame_start,
-                       next_frame_start - frame_start,
-                       0, arg_deadline);
-      vpx_usec_timer_mark(&timer);
-      cx_time += vpx_usec_timer_elapsed(&timer);
-      ctx_exit_on_error(&encoder, "Failed to encode frame");
-
-      if (cfg.g_pass != VPX_RC_FIRST_PASS) {
-        int q;
-
-        vpx_codec_control(&encoder, VP8E_GET_LAST_QUANTIZER_64, &q);
-        ctx_exit_on_error(&encoder, "Failed to read quantizer");
-        counts[q]++;
-      }
+      if (frames_in > global.skip_frames) {
+        vpx_usec_timer_start(&timer);
+        FOREACH_STREAM(encode_frame(stream, &global,
+                                    frame_avail ? &raw : NULL,
+                                    frames_in));
+        vpx_usec_timer_mark(&timer);
+        cx_time += (unsigned long)vpx_usec_timer_elapsed(&timer);
 
-      got_data = 0;
-      test_decode_frame = 0;
-      while ((pkt = vpx_codec_get_cx_data(&encoder, &iter))) {
-        got_data = 1;
+        FOREACH_STREAM(update_quantizer_histogram(stream));
 
-        switch (pkt->kind) {
-          case VPX_CODEC_CX_FRAME_PKT:
-            fprintf(stderr,
-                    "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
-                    arg_passes, frames_in, frames_out, nbytes);
-            frames_out++;
-            fprintf(stderr, " %6luF",
-                    (unsigned long)pkt->data.frame.sz);
-
-            if (test_decode) {
-              if (!vpx_codec_decode(&decoder,
-                                    pkt->data.frame.buf,
-                                    pkt->data.frame.sz,
-                                    NULL, 0)) {
-                vpx_codec_get_frame(&decoder, &dec_iter);
-                test_decode_frame = 1;
-              } else {
-                const char *detail = vpx_codec_error_detail(&decoder);
-                fprintf(stderr, "Failed to decode frame: %s\n",
-                        vpx_codec_error(&decoder));
-                if (detail)
-                  fprintf(stderr,
-                          "  Additional information: %s\n",
-                          detail);
-              }
-            }
-
-            update_rate_histogram(&rate_hist, &cfg, pkt);
-            if (write_webm) {
-              /* Update the hash */
-              if (!ebml.debug)
-                hash = murmur(pkt->data.frame.buf,
-                              pkt->data.frame.sz, hash);
-
-              write_webm_block(&ebml, &cfg, pkt);
-            } else {
-              write_ivf_frame_header(outfile, pkt);
-              if (fwrite(pkt->data.frame.buf, 1,
-                         pkt->data.frame.sz, outfile));
-            }
-            nbytes += pkt->data.raw.sz;
-            break;
-          case VPX_CODEC_STATS_PKT:
-            fprintf(stderr,
-                    "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
-                    arg_passes, frames_in, frames_out, nbytes);
-            frames_out++;
-            fprintf(stderr, " %6luS",
-                    (unsigned long)pkt->data.twopass_stats.sz);
-            stats_write(&stats,
-                        pkt->data.twopass_stats.buf,
-                        pkt->data.twopass_stats.sz);
-            nbytes += pkt->data.raw.sz;
-            break;
-          case VPX_CODEC_PSNR_PKT:
-
-            if (show_psnr) {
-              int i;
-
-              psnr_sse_total += pkt->data.psnr.sse[0];
-              psnr_samples_total += pkt->data.psnr.samples[0];
-              for (i = 0; i < 4; i++) {
-                fprintf(stderr, "%.3lf ", pkt->data.psnr.psnr[i]);
-                psnr_totals[i] += pkt->data.psnr.psnr[i];
-              }
-              psnr_count++;
-            }
+        got_data = 0;
+        FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
 
-            break;
-          default:
-            break;
-        }
-      }
-      if (test_decode && test_decode_frame) {
-        ref_enc.frame_type = VP8_LAST_FRAME;
-        ref_dec.frame_type = VP8_LAST_FRAME;
-
-        vpx_codec_control(&encoder,
-                          VP8_COPY_REFERENCE,
-                          &ref_enc);
-        vpx_codec_control(&decoder,
-                          VP8_COPY_REFERENCE,
-                          &ref_dec);
-
-        enc_dec_match &= compare_img(&ref_enc.img,
-                                     &ref_dec.img);
-        if (!enc_dec_match && first_bad_frame < 0) {
-          first_bad_frame = frames_out - 1;
-        }
+        if (global.test_decode)
+          FOREACH_STREAM(test_decode(stream, frames_in));
       }
 
       fflush(stdout);
     }
 
-    fprintf(stderr,
-            "\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64"b/s"
-            " %7lu %s (%.2f fps)\033[K", pass + 1,
-            arg_passes, frames_in, frames_out, nbytes, nbytes * 8 / frames_in,
-            nbytes * 8 * (int64_t)arg_framerate.num / arg_framerate.den / frames_in,
-            cx_time > 9999999 ? cx_time / 1000 : cx_time,
-            cx_time > 9999999 ? "ms" : "us",
-            (float)frames_in * 1000000.0 / (float)cx_time);
-
-    if ((show_psnr) && (psnr_count > 0)) {
-      int i;
-      double ovpsnr = vp8_mse2psnr(psnr_samples_total, 255.0,
-                                   psnr_sse_total);
-
-      fprintf(stderr, "\nPSNR (Overall/Avg/Y/U/V)");
-
-      fprintf(stderr, " %.3lf", ovpsnr);
-      for (i = 0; i < 4; i++) {
-        fprintf(stderr, " %.3lf", psnr_totals[i] / psnr_count);
-      }
+    if (stream_cnt > 1)
+      fprintf(stderr, "\n");
+
+    if (!global.quiet)
+      FOREACH_STREAM(fprintf(
+                       stderr,
+                       "\rPass %d/%d frame %4d/%-4d %7"PRId64"B %7lub/f %7"PRId64"b/s"
+                       " %7"PRId64" %s (%.2f fps)\033[K\n", pass + 1,
+                       global.passes, frames_in, stream->frames_out, (int64_t)stream->nbytes,
+                       frames_in ? (unsigned long)(stream->nbytes * 8 / frames_in) : 0,
+                       frames_in ? (int64_t)stream->nbytes * 8
+                       * (int64_t)global.framerate.num / global.framerate.den
+                       / frames_in
+                       : 0,
+                       stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
+                       stream->cx_time > 9999999 ? "ms" : "us",
+                       usec_to_fps(stream->cx_time, frames_in));
+                    );
+
+    if (global.show_psnr)
+      FOREACH_STREAM(show_psnr(stream));
+
+    FOREACH_STREAM(vpx_codec_destroy(&stream->encoder));
+
+    if (global.test_decode) {
+      FOREACH_STREAM(vpx_codec_destroy(&stream->decoder));
+      FOREACH_STREAM(vpx_img_free(&stream->ref_enc.img));
+      FOREACH_STREAM(vpx_img_free(&stream->ref_dec.img));
     }
 
-    vpx_codec_destroy(&encoder);
+    close_input_file(&input);
 
-    if (test_decode)
-      vpx_codec_destroy(&decoder);
+    FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
 
-    fclose(infile);
-    if (file_type == FILE_TYPE_Y4M)
-      y4m_input_close(&y4m);
+    FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
 
-    if (write_webm) {
-      write_webm_file_footer(&ebml, hash);
-      free(ebml.cue_list);
-      ebml.cue_list = NULL;
-    } else {
-      if (!fseek(outfile, 0, SEEK_SET))
-        write_ivf_file_header(outfile, &cfg, codec->fourcc, frames_out);
-    }
-
-    fclose(outfile);
-    stats_close(&stats, arg_passes - 1);
-    fprintf(stderr, "\n");
-
-    if (one_pass_only)
+    if (global.pass)
       break;
   }
 
-  if (test_decode) {
+  if (global.show_q_hist_buckets)
+    FOREACH_STREAM(show_q_histogram(stream->counts,
+                                    global.show_q_hist_buckets));
+
+  if (global.show_rate_hist_buckets)
+    FOREACH_STREAM(show_rate_histogram(&stream->rate_hist,
+                                       &stream->config.cfg,
+                                       global.show_rate_hist_buckets));
+  FOREACH_STREAM(destroy_rate_histogram(&stream->rate_hist));
+
 #if CONFIG_INTERNAL_STATS
+  /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
+   * to match some existing utilities.
+   */
+  FOREACH_STREAM({
     FILE *f = fopen("opsnr.stt", "a");
-#endif
-    fprintf(stderr, "\n");
-    if (enc_dec_match) {
-      fprintf(stderr, "No mismatch detected in recon buffers\n");
-#if CONFIG_INTERNAL_STATS
-      fprintf(f, "No mismatch detected in recon buffers\n");
-#endif
-    } else {
-      fprintf(stderr, "First mismatch occurred in frame %d\n",
-              first_bad_frame);
-#if CONFIG_INTERNAL_STATS
+    if (stream->mismatch_seen) {
       fprintf(f, "First mismatch occurred in frame %d\n",
-              first_bad_frame);
-#endif
+              stream->mismatch_seen);
+    } else {
+      fprintf(f, "No mismatch detected in recon buffers\n");
     }
-#if CONFIG_INTERNAL_STATS
     fclose(f);
+  });
 #endif
-  }
-
-  if (show_q_hist_buckets)
-    show_q_histogram(counts, show_q_hist_buckets);
-
-  if (show_rate_hist_buckets)
-    show_rate_histogram(&rate_hist, &cfg, show_rate_hist_buckets);
-  destroy_rate_histogram(&rate_hist);
 
   vpx_img_free(&raw);
-  if (test_decode) {
-    vpx_img_free(&ref_enc.img);
-    vpx_img_free(&ref_dec.img);
-  }
   free(argv);
+  free(streams);
   return EXIT_SUCCESS;
 }