Merge "vp9_dthread: interleave mutex/cond alloc+init"
diff --git a/examples.mk b/examples.mk
index b2bdf68..23f2285 100644
--- a/examples.mk
+++ b/examples.mk
@@ -54,9 +54,6 @@
 vpxenc.SRCS                 += $(LIBYUV_SRCS)
 vpxenc.GUID                  = 548DEC74-7A15-4B2B-AFC3-AA102E7C25C1
 vpxenc.DESCRIPTION           = Full featured encoder
-UTILS-$(CONFIG_VP8_ENCODER)    += vp8_scalable_patterns.c
-vp8_scalable_patterns.GUID   = 0D6A210B-F482-4D6F-8570-4A9C01ACC88C
-vp8_scalable_patterns.DESCRIPTION = Temporal Scalability Encoder
 UTILS-$(CONFIG_VP9_ENCODER)    += vp9_spatial_scalable_encoder.c
 vp9_spatial_scalable_encoder.SRCS += args.c args.h
 vp9_spatial_scalable_encoder.SRCS += ivfenc.c ivfenc.h
@@ -73,24 +70,40 @@
 #example_xma.GUID             = A955FC4A-73F1-44F7-135E-30D84D32F022
 #example_xma.DESCRIPTION      = External Memory Allocation mode usage
 
+EXAMPLES-$(CONFIG_ENCODERS)         += vpx_temporal_scalable_patterns.c
+vpx_temporal_scalable_patterns.SRCS += ivfenc.c ivfenc.h
+vpx_temporal_scalable_patterns.SRCS += tools_common.c tools_common.h
+vpx_temporal_scalable_patterns.SRCS += video_common.h
+vpx_temporal_scalable_patterns.SRCS += video_writer.h video_writer.c
+vpx_temporal_scalable_patterns.GUID  = B18C08F2-A439-4502-A78E-849BE3D60947
+vpx_temporal_scalable_patterns.DESCRIPTION = Temporal Scalability Encoder
 EXAMPLES-$(CONFIG_VP8_DECODER)     += simple_decoder.c
 simple_decoder.GUID                 = D3BBF1E9-2427-450D-BBFF-B2843C1D44CC
 simple_decoder.SRCS                += ivfdec.h ivfdec.c
 simple_decoder.SRCS                += tools_common.h tools_common.c
+simple_decoder.SRCS                += video_common.h
+simple_decoder.SRCS                += video_reader.h video_reader.c
 simple_decoder.DESCRIPTION          = Simplified decoder loop
 EXAMPLES-$(CONFIG_VP8_DECODER)     += postproc.c
 postproc.SRCS                      += ivfdec.h ivfdec.c
 postproc.SRCS                      += tools_common.h tools_common.c
+postproc.SRCS                      += video_common.h
+postproc.SRCS                      += video_reader.h video_reader.c
 postproc.GUID                       = 65E33355-F35E-4088-884D-3FD4905881D7
 postproc.DESCRIPTION                = Decoder postprocessor control
 EXAMPLES-$(CONFIG_VP8_DECODER)     += decode_to_md5.c
 decode_to_md5.SRCS                 += md5_utils.h md5_utils.c
 decode_to_md5.SRCS                 += ivfdec.h ivfdec.c
 decode_to_md5.SRCS                 += tools_common.h tools_common.c
+decode_to_md5.SRCS                 += video_common.h
+decode_to_md5.SRCS                 += video_reader.h video_reader.c
 decode_to_md5.GUID                  = 59120B9B-2735-4BFE-B022-146CA340FE42
 decode_to_md5.DESCRIPTION           = Frame by frame MD5 checksum
-
 EXAMPLES-$(CONFIG_VP8_ENCODER)  += simple_encoder.c
+simple_encoder.SRCS             += ivfenc.h ivfenc.c
+simple_encoder.SRCS             += tools_common.h tools_common.c
+simple_encoder.SRCS             += video_common.h
+simple_encoder.SRCS             += video_writer.h video_writer.c
 simple_encoder.GUID              = 4607D299-8A71-4D2C-9B1D-071899B6FBFD
 simple_encoder.DESCRIPTION       = Simplified encoder loop
 EXAMPLES-$(CONFIG_VP8_ENCODER)  += twopass_encoder.c
@@ -103,6 +116,8 @@
 EXAMPLES-$(CONFIG_VP8_ENCODER)  += decode_with_drops.c
 decode_with_drops.SRCS          += ivfdec.h ivfdec.c
 decode_with_drops.SRCS          += tools_common.h tools_common.c
+decode_with_drops.SRCS          += video_common.h
+decode_with_drops.SRCS          += video_reader.h video_reader.c
 endif
 decode_with_drops.GUID           = CE5C53C4-8DDA-438A-86ED-0DDD3CDB8D26
 decode_with_drops.DESCRIPTION    = Drops frames while decoding
diff --git a/examples/decode_to_md5.c b/examples/decode_to_md5.c
index bba2182..077513c 100644
--- a/examples/decode_to_md5.c
+++ b/examples/decode_to_md5.c
@@ -38,9 +38,9 @@
 #include "vpx/vp8dx.h"
 #include "vpx/vpx_decoder.h"
 
-#include "./ivfdec.h"
 #include "./md5_utils.h"
 #include "./tools_common.h"
+#include "./video_reader.h"
 #include "./vpx_config.h"
 
 static void get_image_md5(const vpx_image_t *img, unsigned char digest[16]) {
@@ -79,41 +79,42 @@
 }
 
 int main(int argc, char **argv) {
-  FILE *infile, *outfile;
+  int frame_cnt = 0;
+  FILE *outfile = NULL;
   vpx_codec_ctx_t codec;
-  vpx_codec_iface_t *iface;
-  int flags = 0, frame_cnt = 0;
-  vpx_video_t *video;
+  vpx_codec_iface_t *iface = NULL;
+  VpxVideoReader *reader = NULL;
+  const VpxVideoInfo *info = NULL;
 
   exec_name = argv[0];
 
   if (argc != 3)
-    die("Invalid number of arguments");
+    die("Invalid number of arguments.");
 
-  if (!(infile = fopen(argv[1], "rb")))
-    die("Failed to open %s for reading", argv[1]);
+  reader = vpx_video_reader_open(argv[1]);
+  if (!reader)
+    die("Failed to open %s for reading.", argv[1]);
 
   if (!(outfile = fopen(argv[2], "wb")))
-    die("Failed to open %s for writing", argv[2]);
+    die("Failed to open %s for writing.", argv[2]);
 
-  video = vpx_video_open_file(infile);
-  if (!video)
-    die("%s is not an IVF file.", argv[1]);
+  info = vpx_video_reader_get_info(reader);
 
-  iface = get_codec_interface(vpx_video_get_fourcc(video));
+  iface = get_codec_interface(info->codec_fourcc);
   if (!iface)
-    die("Unknown FOURCC code.");
+    die("Unknown input codec.");
 
   printf("Using %s\n", vpx_codec_iface_name(iface));
 
-  if (vpx_codec_dec_init(&codec, iface, NULL, flags))
+  if (vpx_codec_dec_init(&codec, iface, NULL, 0))
     die_codec(&codec, "Failed to initialize decoder");
 
-  while (vpx_video_read_frame(video)) {
+  while (vpx_video_reader_read_frame(reader)) {
     vpx_codec_iter_t iter = NULL;
     vpx_image_t *img = NULL;
     size_t frame_size = 0;
-    const unsigned char *frame = vpx_video_get_frame(video, &frame_size);
+    const unsigned char *frame = vpx_video_reader_get_frame(reader,
+                                                            &frame_size);
     if (vpx_codec_decode(&codec, frame, frame_size, NULL, 0))
       die_codec(&codec, "Failed to decode frame");
 
@@ -129,11 +130,10 @@
 
   printf("Processed %d frames.\n", frame_cnt);
   if (vpx_codec_destroy(&codec))
-    die_codec(&codec, "Failed to destroy codec");
+    die_codec(&codec, "Failed to destroy codec.");
 
-  vpx_video_close(video);
+  vpx_video_reader_close(reader);
 
   fclose(outfile);
-  fclose(infile);
   return EXIT_SUCCESS;
 }
diff --git a/examples/decode_with_drops.c b/examples/decode_with_drops.c
index 12686de..e8fc076 100644
--- a/examples/decode_with_drops.c
+++ b/examples/decode_with_drops.c
@@ -56,14 +56,13 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "./ivfdec.h"
-
 #define VPX_CODEC_DISABLE_COMPAT 1
 
 #include "vpx/vp8dx.h"
 #include "vpx/vpx_decoder.h"
 
 #include "./tools_common.h"
+#include "./video_reader.h"
 #include "./vpx_config.h"
 
 static const char *exec_name;
@@ -74,52 +73,55 @@
 }
 
 int main(int argc, char **argv) {
-  FILE *infile, *outfile;
+  int frame_cnt = 0;
+  FILE *outfile = NULL;
   vpx_codec_ctx_t codec;
-  vpx_codec_iface_t *iface;
-  int flags = 0, frame_cnt = 0;
-  vpx_video_t *video;
-  int n, m, is_range;
-  char *nptr;
+  vpx_codec_iface_t *iface = NULL;
+  VpxVideoReader *reader = NULL;
+  const VpxVideoInfo *info = NULL;
+  int n = 0;
+  int m = 0;
+  int is_range = 0;
+  char *nptr = NULL;
 
   exec_name = argv[0];
 
   if (argc != 4)
-    die("Invalid number of arguments");
+    die("Invalid number of arguments.");
 
-  if (!(infile = fopen(argv[1], "rb")))
-    die("Failed to open %s for reading", argv[1]);
+  reader = vpx_video_reader_open(argv[1]);
+  if (!reader)
+    die("Failed to open %s for reading.", argv[1]);
 
   if (!(outfile = fopen(argv[2], "wb")))
-    die("Failed to open %s for writing", argv[2]);
+    die("Failed to open %s for writing.", argv[2]);
 
   n = strtol(argv[3], &nptr, 0);
   m = strtol(nptr + 1, NULL, 0);
   is_range = (*nptr == '-');
   if (!n || !m || (*nptr != '-' && *nptr != '/'))
-    die("Couldn't parse pattern %s\n", argv[3]);
+    die("Couldn't parse pattern %s.\n", argv[3]);
 
-  video = vpx_video_open_file(infile);
-  if (!video)
-    die("%s is not a supported input file.", argv[1]);
+  info = vpx_video_reader_get_info(reader);
 
-  iface = get_codec_interface(vpx_video_get_fourcc(video));
+  iface = get_codec_interface(info->codec_fourcc);
   if (!iface)
-    die("Unknown FOURCC code.");
+    die("Unknown input codec.");
 
   printf("Using %s\n", vpx_codec_iface_name(iface));
 
-  if (vpx_codec_dec_init(&codec, iface, NULL, flags))
-    die_codec(&codec, "Failed to initialize decoder");
+  if (vpx_codec_dec_init(&codec, iface, NULL, 0))
+    die_codec(&codec, "Failed to initialize decoder.");
 
-  while (vpx_video_read_frame(video)) {
+  while (vpx_video_reader_read_frame(reader)) {
     vpx_codec_iter_t iter = NULL;
     vpx_image_t *img = NULL;
     size_t frame_size = 0;
     int skip;
-    const unsigned char *frame = vpx_video_get_frame(video, &frame_size);
+    const unsigned char *frame = vpx_video_reader_get_frame(reader,
+                                                            &frame_size);
     if (vpx_codec_decode(&codec, frame, frame_size, NULL, 0))
-      die_codec(&codec, "Failed to decode frame");
+      die_codec(&codec, "Failed to decode frame.");
 
     ++frame_cnt;
 
@@ -140,15 +142,13 @@
 
   printf("Processed %d frames.\n", frame_cnt);
   if (vpx_codec_destroy(&codec))
-    die_codec(&codec, "Failed to destroy codec");
+    die_codec(&codec, "Failed to destroy codec.");
 
   printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
-         vpx_video_get_width(video), vpx_video_get_height(video), argv[2]);
+         info->frame_width, info->frame_height, argv[2]);
 
-  vpx_video_close(video);
-
+  vpx_video_reader_close(reader);
   fclose(outfile);
-  fclose(infile);
 
   return EXIT_SUCCESS;
 }
diff --git a/examples/postproc.c b/examples/postproc.c
index 4ec2d1f..7281f1e 100644
--- a/examples/postproc.c
+++ b/examples/postproc.c
@@ -43,14 +43,13 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "./ivfdec.h"
-
 #define VPX_CODEC_DISABLE_COMPAT 1
 
 #include "vpx/vp8dx.h"
 #include "vpx/vpx_decoder.h"
 
 #include "./tools_common.h"
+#include "./video_reader.h"
 #include "./vpx_config.h"
 
 static const char *exec_name;
@@ -61,35 +60,34 @@
 }
 
 int main(int argc, char **argv) {
-  FILE *infile, *outfile;
-  vpx_codec_ctx_t codec;
-  vpx_codec_iface_t *iface;
   int frame_cnt = 0;
-  vpx_video_t *video;
+  FILE *outfile = NULL;
+  vpx_codec_ctx_t codec;
   vpx_codec_err_t res;
+  vpx_codec_iface_t *iface = NULL;
+  VpxVideoReader *reader = NULL;
+  const VpxVideoInfo *info = NULL;
 
   exec_name = argv[0];
 
   if (argc != 3)
-    die("Invalid number of arguments");
+    die("Invalid number of arguments.");
 
-  if (!(infile = fopen(argv[1], "rb")))
-    die("Failed to open %s for reading", argv[1]);
+  reader = vpx_video_reader_open(argv[1]);
+  if (!reader)
+    die("Failed to open %s for reading.", argv[1]);
 
   if (!(outfile = fopen(argv[2], "wb")))
     die("Failed to open %s for writing", argv[2]);
 
-  video = vpx_video_open_file(infile);
-  if (!video)
-    die("%s is not a supported input file.", argv[1]);
+  info = vpx_video_reader_get_info(reader);
 
-  iface = get_codec_interface(vpx_video_get_fourcc(video));
+  iface = get_codec_interface(info->codec_fourcc);
   if (!iface)
-    die("Unknown FOURCC code.");
+    die("Unknown input codec.");
 
   printf("Using %s\n", vpx_codec_iface_name(iface));
 
-
   res = vpx_codec_dec_init(&codec, iface, NULL, VPX_CODEC_USE_POSTPROC);
   if (res == VPX_CODEC_INCAPABLE) {
     printf("NOTICE: Postproc not supported.\n");
@@ -97,13 +95,14 @@
   }
 
   if (res)
-    die_codec(&codec, "Failed to initialize decoder");
+    die_codec(&codec, "Failed to initialize decoder.");
 
-  while (vpx_video_read_frame(video)) {
+  while (vpx_video_reader_read_frame(reader)) {
     vpx_codec_iter_t iter = NULL;
     vpx_image_t *img = NULL;
     size_t frame_size = 0;
-    const unsigned char *frame = vpx_video_get_frame(video, &frame_size);
+    const unsigned char *frame = vpx_video_reader_get_frame(reader,
+                                                            &frame_size);
 
     ++frame_cnt;
 
@@ -111,12 +110,12 @@
       vp8_postproc_cfg_t pp = {0, 0, 0};
 
     if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
-      die_codec(&codec, "Failed to turn off postproc");
+      die_codec(&codec, "Failed to turn off postproc.");
     } else if (frame_cnt % 30 == 16) {
       vp8_postproc_cfg_t pp = {VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE,
                                4, 0};
       if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
-        die_codec(&codec, "Failed to turn on postproc");
+        die_codec(&codec, "Failed to turn on postproc.");
     };
 
     // Decode the frame with 15ms deadline
@@ -133,11 +132,10 @@
     die_codec(&codec, "Failed to destroy codec");
 
   printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
-         vpx_video_get_width(video), vpx_video_get_height(video), argv[2]);
+         info->frame_width, info->frame_height, argv[2]);
 
-  vpx_video_close(video);
+  vpx_video_reader_close(reader);
 
   fclose(outfile);
-  fclose(infile);
   return EXIT_SUCCESS;
 }
diff --git a/examples/simple_decoder.c b/examples/simple_decoder.c
index 23399f4..4dc9308 100644
--- a/examples/simple_decoder.c
+++ b/examples/simple_decoder.c
@@ -86,8 +86,8 @@
 #include "vpx/vp8dx.h"
 #include "vpx/vpx_decoder.h"
 
-#include "./ivfdec.h"
 #include "./tools_common.h"
+#include "./video_reader.h"
 #include "./vpx_config.h"
 
 static const char *exec_name;
@@ -98,43 +98,44 @@
 }
 
 int main(int argc, char **argv) {
-  FILE *infile, *outfile;
+  int frame_cnt = 0;
+  FILE *outfile = NULL;
   vpx_codec_ctx_t codec;
-  vpx_codec_iface_t *iface;
-  int flags = 0, frame_cnt = 0;
-  vpx_video_t *video;
+  vpx_codec_iface_t *iface = NULL;
+  VpxVideoReader *reader = NULL;
+  const VpxVideoInfo *info = NULL;
 
   exec_name = argv[0];
 
   if (argc != 3)
-    die("Invalid number of arguments");
+    die("Invalid number of arguments.");
 
-  if (!(infile = fopen(argv[1], "rb")))
-    die("Failed to open %s for reading", argv[1]);
+  reader = vpx_video_reader_open(argv[1]);
+  if (!reader)
+    die("Failed to open %s for reading.", argv[1]);
 
   if (!(outfile = fopen(argv[2], "wb")))
-    die("Failed to open %s for writing", argv[2]);
+    die("Failed to open %s for writing.", argv[2]);
 
-  video = vpx_video_open_file(infile);
-  if (!video)
-    die("%s is not an IVF file.", argv[1]);
+  info = vpx_video_reader_get_info(reader);
 
-  iface = get_codec_interface(vpx_video_get_fourcc(video));
+  iface = get_codec_interface(info->codec_fourcc);
   if (!iface)
-    die("Unknown FOURCC code.");
+    die("Unknown input codec.");
 
   printf("Using %s\n", vpx_codec_iface_name(iface));
 
-  if (vpx_codec_dec_init(&codec, iface, NULL, flags))
-    die_codec(&codec, "Failed to initialize decoder");
+  if (vpx_codec_dec_init(&codec, iface, NULL, 0))
+    die_codec(&codec, "Failed to initialize decoder.");
 
-  while (vpx_video_read_frame(video)) {
+  while (vpx_video_reader_read_frame(reader)) {
     vpx_codec_iter_t iter = NULL;
     vpx_image_t *img = NULL;
     size_t frame_size = 0;
-    const unsigned char *frame = vpx_video_get_frame(video, &frame_size);
+    const unsigned char *frame = vpx_video_reader_get_frame(reader,
+                                                            &frame_size);
     if (vpx_codec_decode(&codec, frame, frame_size, NULL, 0))
-      die_codec(&codec, "Failed to decode frame");
+      die_codec(&codec, "Failed to decode frame.");
 
     while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
       vpx_img_write(img, outfile);
@@ -147,12 +148,11 @@
     die_codec(&codec, "Failed to destroy codec");
 
   printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
-         vpx_video_get_width(video), vpx_video_get_height(video), argv[2]);
+         info->frame_width, info->frame_height, argv[2]);
 
-  vpx_video_close(video);
+  vpx_video_reader_close(reader);
 
   fclose(outfile);
-  fclose(infile);
 
   return EXIT_SUCCESS;
 }
diff --git a/examples/simple_encoder.c b/examples/simple_encoder.c
index e64a962..0ae012c 100644
--- a/examples/simple_encoder.c
+++ b/examples/simple_encoder.c
@@ -83,194 +83,131 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <string.h>
+
 #define VPX_CODEC_DISABLE_COMPAT 1
-#include "vpx/vpx_encoder.h"
+
 #include "vpx/vp8cx.h"
+#include "vpx/vpx_encoder.h"
+
+#include "./tools_common.h"
+#include "./video_writer.h"
+
 #define interface (vpx_codec_vp8_cx())
-#define fourcc    0x30385056
 
-#define IVF_FILE_HDR_SZ  (32)
-#define IVF_FRAME_HDR_SZ (12)
+static const char *exec_name;
 
-static void mem_put_le16(char *mem, unsigned int val) {
-    mem[0] = val;
-    mem[1] = val>>8;
-}
-
-static void mem_put_le32(char *mem, unsigned int val) {
-    mem[0] = val;
-    mem[1] = val>>8;
-    mem[2] = val>>16;
-    mem[3] = val>>24;
-}
-
-static void die(const char *fmt, ...) {
-    va_list ap;
-
-    va_start(ap, fmt);
-    vprintf(fmt, ap);
-    if(fmt[strlen(fmt)-1] != '\n')
-        printf("\n");
-    exit(EXIT_FAILURE);
-}
-
-static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
-    const char *detail = vpx_codec_error_detail(ctx);
-
-    printf("%s: %s\n", s, vpx_codec_error(ctx));
-    if(detail)
-        printf("    %s\n",detail);
-    exit(EXIT_FAILURE);
+void usage_exit() {
+  fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile>\n", exec_name);
+  exit(EXIT_FAILURE);
 }
 
 static int read_frame(FILE *f, vpx_image_t *img) {
-    size_t nbytes, to_read;
-    int    res = 1;
-
-    to_read = img->w*img->h*3/2;
-    nbytes = fread(img->planes[0], 1, to_read, f);
-    if(nbytes != to_read) {
-        res = 0;
-        if(nbytes > 0)
-            printf("Warning: Read partial frame. Check your width & height!\n");
-    }
-    return res;
+  int res = 1;
+  size_t to_read = img->w * img->h * 3 / 2;
+  size_t nbytes = fread(img->planes[0], 1, to_read, f);
+  if (nbytes != to_read) {
+    res = 0;
+    if (nbytes > 0)
+      printf("Warning: Read partial frame. Check your width & height!\n");
+  }
+  return res;
 }
 
-static void write_ivf_file_header(FILE *outfile,
-                                  const vpx_codec_enc_cfg_t *cfg,
-                                  int frame_cnt) {
-    char header[32];
-
-    if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
-        return;
-    header[0] = 'D';
-    header[1] = 'K';
-    header[2] = 'I';
-    header[3] = 'F';
-    mem_put_le16(header+4,  0);                   /* version */
-    mem_put_le16(header+6,  32);                  /* headersize */
-    mem_put_le32(header+8,  fourcc);              /* headersize */
-    mem_put_le16(header+12, cfg->g_w);            /* width */
-    mem_put_le16(header+14, cfg->g_h);            /* height */
-    mem_put_le32(header+16, cfg->g_timebase.den); /* rate */
-    mem_put_le32(header+20, cfg->g_timebase.num); /* scale */
-    mem_put_le32(header+24, frame_cnt);           /* length */
-    mem_put_le32(header+28, 0);                   /* unused */
-
-    (void) fwrite(header, 1, 32, outfile);
-}
-
-
-static void write_ivf_frame_header(FILE *outfile,
-                                   const vpx_codec_cx_pkt_t *pkt)
-{
-    char             header[12];
-    vpx_codec_pts_t  pts;
-
-    if(pkt->kind != VPX_CODEC_CX_FRAME_PKT)
-        return;
-
-    pts = pkt->data.frame.pts;
-    mem_put_le32(header, pkt->data.frame.sz);
-    mem_put_le32(header+4, pts&0xFFFFFFFF);
-    mem_put_le32(header+8, pts >> 32);
-
-    (void) fwrite(header, 1, 12, outfile);
+static int is_valid_dimension(int value) {
+  return value >= 16 && (value % 2 == 0);
 }
 
 int main(int argc, char **argv) {
-    FILE                *infile, *outfile;
-    vpx_codec_ctx_t      codec;
-    vpx_codec_enc_cfg_t  cfg;
-    int                  frame_cnt = 0;
-    vpx_image_t          raw;
-    vpx_codec_err_t      res;
-    long                 width;
-    long                 height;
-    int                  frame_avail;
-    int                  got_data;
-    int                  flags = 0;
+  FILE *infile;
+  vpx_codec_ctx_t codec;
+  vpx_codec_enc_cfg_t cfg;
+  int frame_count = 0;
+  vpx_image_t raw;
+  vpx_codec_err_t res;
+  VpxVideoInfo info;
+  VpxVideoWriter *writer;
+  const int fps = 30;  // TODO(dkovalev) add command line argument
+  const int bitrate = 100;  // kbit/s TODO(dkovalev) add command line argument
 
-    /* Open files */
-    if(argc!=5)
-        die("Usage: %s <width> <height> <infile> <outfile>\n", argv[0]);
-    width = strtol(argv[1], NULL, 0);
-    height = strtol(argv[2], NULL, 0);
-    if(width < 16 || width%2 || height <16 || height%2)
-        die("Invalid resolution: %ldx%ld", width, height);
-    if(!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 1))
-        die("Faile to allocate image", width, height);
-    if(!(outfile = fopen(argv[4], "wb")))
-        die("Failed to open %s for writing", argv[4]);
+  exec_name = argv[0];
 
-    printf("Using %s\n",vpx_codec_iface_name(interface));
+  if (argc != 5)
+    die("Invalid number of arguments");
 
-    /* Populate encoder configuration */
-    res = vpx_codec_enc_config_default(interface, &cfg, 0);
-    if(res) {
-        printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
-        return EXIT_FAILURE;
+  info.codec_fourcc = VP8_FOURCC;
+  info.frame_width = strtol(argv[1], NULL, 0);
+  info.frame_height = strtol(argv[2], NULL, 0);
+  info.time_base.numerator = 1;
+  info.time_base.denominator = fps;
+
+  if (!is_valid_dimension(info.frame_width) ||
+      !is_valid_dimension(info.frame_height))
+    die("Invalid resolution: %dx%d", info.frame_width, info.frame_height);
+
+  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
+                                             info.frame_height, 1))
+    die("Failed to allocate image");
+
+  printf("Using %s\n", vpx_codec_iface_name(interface));
+
+  res = vpx_codec_enc_config_default(interface, &cfg, 0);
+  if (res) {
+    printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
+    return EXIT_FAILURE;
+  }
+
+  cfg.g_w = info.frame_width;
+  cfg.g_h = info.frame_height;
+  cfg.g_timebase.num = info.time_base.numerator;
+  cfg.g_timebase.den = info.time_base.denominator;
+  cfg.rc_target_bitrate = bitrate;
+
+  writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
+  if (!writer)
+    die("Failed to open %s for writing.", argv[4]);
+
+  // Open input file for this encoding pass
+  if (!(infile = fopen(argv[3], "rb")))
+    die("Failed to open %s for reading.", argv[3]);
+
+  // Initialize codec
+  if (vpx_codec_enc_init(&codec, interface, &cfg, 0))
+    die_codec(&codec, "Failed to initialize encoder");
+
+
+  while (read_frame(infile, &raw)) {
+    vpx_codec_iter_t iter = NULL;
+    const vpx_codec_cx_pkt_t *pkt = NULL;
+    res = vpx_codec_encode(&codec, &raw, frame_count, 1, 0,
+                           VPX_DL_GOOD_QUALITY);
+    if (res != VPX_CODEC_OK)
+      die_codec(&codec, "Failed to encode frame");
+
+    ++frame_count;
+
+    while ((pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) {
+      if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
+        const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
+        if (!vpx_video_writer_write_frame(writer,
+                                          pkt->data.frame.buf,
+                                          pkt->data.frame.sz,
+                                          pkt->data.frame.pts))
+          die_codec(&codec, "Failed to write compressed frame.");
+        printf(keyframe ? "K" : ".");
+      }
     }
+  }
+  printf("\n");
 
-    /* Update the default configuration with our settings */
-    cfg.rc_target_bitrate = width * height * cfg.rc_target_bitrate
-                            / cfg.g_w / cfg.g_h;
-    cfg.g_w = width;
-    cfg.g_h = height;
+  fclose(infile);
 
-    write_ivf_file_header(outfile, &cfg, 0);
+  printf("Processed %d frames.\n", frame_count);
+  vpx_img_free(&raw);
+  if (vpx_codec_destroy(&codec))
+    die_codec(&codec, "Failed to destroy codec.");
 
+  vpx_video_writer_close(writer);
 
-        /* Open input file for this encoding pass */
-        if(!(infile = fopen(argv[3], "rb")))
-            die("Failed to open %s for reading", argv[3]);
-
-        /* Initialize codec */
-        if(vpx_codec_enc_init(&codec, interface, &cfg, 0))
-            die_codec(&codec, "Failed to initialize encoder");
-
-        frame_avail = 1;
-        got_data = 0;
-        while(frame_avail || got_data) {
-            vpx_codec_iter_t iter = NULL;
-            const vpx_codec_cx_pkt_t *pkt;
-
-            frame_avail = read_frame(infile, &raw);
-            if(vpx_codec_encode(&codec, frame_avail? &raw : NULL, frame_cnt,
-                                1, flags, VPX_DL_REALTIME))
-                die_codec(&codec, "Failed to encode frame");
-            got_data = 0;
-            while( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) {
-                got_data = 1;
-                switch(pkt->kind) {
-                case VPX_CODEC_CX_FRAME_PKT:
-                    write_ivf_frame_header(outfile, pkt);
-                    (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
-                                  outfile);
-                    break;
-                default:
-                    break;
-                }
-                printf(pkt->kind == VPX_CODEC_CX_FRAME_PKT
-                       && (pkt->data.frame.flags & VPX_FRAME_IS_KEY)? "K":".");
-                fflush(stdout);
-            }
-            frame_cnt++;
-        }
-        printf("\n");
-        fclose(infile);
-
-    printf("Processed %d frames.\n",frame_cnt-1);
-    vpx_img_free(&raw);
-    if(vpx_codec_destroy(&codec))
-        die_codec(&codec, "Failed to destroy codec");
-
-    /* Try to rewrite the file header with the actual frame count */
-    if(!fseek(outfile, 0, SEEK_SET))
-        write_ivf_file_header(outfile, &cfg, frame_cnt-1);
-    fclose(outfile);
-    return EXIT_SUCCESS;
+  return EXIT_SUCCESS;
 }
diff --git a/examples/vpx_temporal_scalable_patterns.c b/examples/vpx_temporal_scalable_patterns.c
new file mode 100644
index 0000000..e09c149
--- /dev/null
+++ b/examples/vpx_temporal_scalable_patterns.c
@@ -0,0 +1,551 @@
+/*
+ *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+//  This is an example demonstrating how to implement a multi-layer VP9
+//  encoding scheme based on temporal scalability for video applications
+//  that benefit from a scalable bitstream.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define VPX_CODEC_DISABLE_COMPAT 1
+#include "vpx/vp8cx.h"
+#include "vpx/vpx_encoder.h"
+
+#include "./tools_common.h"
+#include "./video_writer.h"
+
+static const char *exec_name;
+
+void usage_exit() {
+  exit(EXIT_FAILURE);
+}
+
+static int mode_to_num_layers[12] = {1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3};
+
+// Temporal scaling parameters:
+// NOTE: The 3 prediction frames cannot be used interchangeably due to
+// differences in the way they are handled throughout the code. The
+// frames should be allocated to layers in the order LAST, GF, ARF.
+// Other combinations work, but may produce slightly inferior results.
+static void set_temporal_layer_pattern(int layering_mode,
+                                       vpx_codec_enc_cfg_t *cfg,
+                                       int *layer_flags,
+                                       int *flag_periodicity) {
+  switch (layering_mode) {
+    case 0: {
+      // 1-layer.
+      int ids[1] = {0};
+      cfg->ts_periodicity = 1;
+      *flag_periodicity = 1;
+      cfg->ts_number_layers = 1;
+      cfg->ts_rate_decimator[0] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // Update L only.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_UPD_GF |
+          VP8_EFLAG_NO_UPD_ARF;
+      break;
+    }
+    case 1: {
+      // 2-layers, 2-frame period.
+      int ids[2] = {0, 1};
+      cfg->ts_periodicity = 2;
+      *flag_periodicity = 2;
+      cfg->ts_number_layers = 2;
+      cfg->ts_rate_decimator[0] = 2;
+      cfg->ts_rate_decimator[1] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+#if 1
+      // 0=L, 1=GF, Intra-layer prediction enabled.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_UPD_GF |
+          VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
+      layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_REF_ARF;
+#else
+       // 0=L, 1=GF, Intra-layer prediction disabled.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_UPD_GF |
+          VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
+      layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_LAST;
+#endif
+      break;
+    }
+    case 2: {
+      // 2-layers, 3-frame period.
+      int ids[3] = {0, 1, 1};
+      cfg->ts_periodicity = 3;
+      *flag_periodicity = 3;
+      cfg->ts_number_layers = 2;
+      cfg->ts_rate_decimator[0] = 3;
+      cfg->ts_rate_decimator[1] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, Intra-layer prediction enabled.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_REF_GF |
+          VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[1] =
+      layer_flags[2] = VP8_EFLAG_NO_REF_GF  | VP8_EFLAG_NO_REF_ARF |
+          VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
+      break;
+    }
+    case 3: {
+      // 3-layers, 6-frame period.
+      int ids[6] = {0, 2, 2, 1, 2, 2};
+      cfg->ts_periodicity = 6;
+      *flag_periodicity = 6;
+      cfg->ts_number_layers = 3;
+      cfg->ts_rate_decimator[0] = 6;
+      cfg->ts_rate_decimator[1] = 3;
+      cfg->ts_rate_decimator[2] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_REF_GF |
+          VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
+          VP8_EFLAG_NO_UPD_LAST;
+      layer_flags[1] =
+      layer_flags[2] =
+      layer_flags[4] =
+      layer_flags[5] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
+      break;
+    }
+    case 4: {
+      // 3-layers, 4-frame period.
+      int ids[4] = {0, 2, 1, 2};
+      cfg->ts_periodicity = 4;
+      *flag_periodicity = 4;
+      cfg->ts_number_layers = 3;
+      cfg->ts_rate_decimator[0] = 4;
+      cfg->ts_rate_decimator[1] = 2;
+      cfg->ts_rate_decimator[2] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_REF_GF |
+          VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
+          VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
+      layer_flags[1] =
+      layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      break;
+    }
+    case 5: {
+      // 3-layers, 4-frame period.
+      int ids[4] = {0, 2, 1, 2};
+      cfg->ts_periodicity = 4;
+      *flag_periodicity = 4;
+      cfg->ts_number_layers     = 3;
+      cfg->ts_rate_decimator[0] = 4;
+      cfg->ts_rate_decimator[1] = 2;
+      cfg->ts_rate_decimator[2] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
+      // in layer 2.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_REF_GF |
+          VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[2] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[1] =
+      layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      break;
+    }
+    case 6: {
+      // 3-layers, 4-frame period.
+      int ids[4] = {0, 2, 1, 2};
+      cfg->ts_periodicity = 4;
+      *flag_periodicity = 4;
+      cfg->ts_number_layers = 3;
+      cfg->ts_rate_decimator[0] = 4;
+      cfg->ts_rate_decimator[1] = 2;
+      cfg->ts_rate_decimator[2] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_REF_GF |
+          VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[2] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[1] =
+      layer_flags[3] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
+      break;
+    }
+    case 7: {
+      // NOTE: Probably of academic interest only.
+      // 5-layers, 16-frame period.
+      int ids[16] = {0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4};
+      cfg->ts_periodicity = 16;
+      *flag_periodicity = 16;
+      cfg->ts_number_layers = 5;
+      cfg->ts_rate_decimator[0] = 16;
+      cfg->ts_rate_decimator[1] = 8;
+      cfg->ts_rate_decimator[2] = 4;
+      cfg->ts_rate_decimator[3] = 2;
+      cfg->ts_rate_decimator[4] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      layer_flags[0]  = VPX_EFLAG_FORCE_KF;
+      layer_flags[1]  =
+      layer_flags[3]  =
+      layer_flags[5]  =
+      layer_flags[7]  =
+      layer_flags[9]  =
+      layer_flags[11] =
+      layer_flags[13] =
+      layer_flags[15] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
+          VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[2]  =
+      layer_flags[6]  =
+      layer_flags[10] =
+      layer_flags[14] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
+      layer_flags[4] =
+      layer_flags[12] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[8]  = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
+      break;
+    }
+    case 8: {
+      // 2-layers, with sync point at first frame of layer 1.
+      int ids[2] = {0, 1};
+      cfg->ts_periodicity = 2;
+      *flag_periodicity = 8;
+      cfg->ts_number_layers = 2;
+      cfg->ts_rate_decimator[0] = 2;
+      cfg->ts_rate_decimator[1] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF.
+      // ARF is used as predictor for all frames, and is only updated on
+      // key frame. Sync point every 8 frames.
+
+      // Layer 0: predict from L and ARF, update L and G.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_REF_GF |
+          VP8_EFLAG_NO_UPD_ARF;
+      // Layer 1: sync point: predict from L and ARF, and update G.
+      layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_ARF;
+      // Layer 0, predict from L and ARF, update L.
+      layer_flags[2] = VP8_EFLAG_NO_REF_GF  | VP8_EFLAG_NO_UPD_GF |
+          VP8_EFLAG_NO_UPD_ARF;
+      // Layer 1: predict from L, G and ARF, and update G.
+      layer_flags[3] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_ENTROPY;
+      // Layer 0.
+      layer_flags[4] = layer_flags[2];
+      // Layer 1.
+      layer_flags[5] = layer_flags[3];
+      // Layer 0.
+      layer_flags[6] = layer_flags[4];
+      // Layer 1.
+      layer_flags[7] = layer_flags[5];
+     break;
+    }
+    case 9: {
+      // 3-layers: Sync points for layer 1 and 2 every 8 frames.
+      int ids[4] = {0, 2, 1, 2};
+      cfg->ts_periodicity = 4;
+      *flag_periodicity = 8;
+      cfg->ts_number_layers = 3;
+      cfg->ts_rate_decimator[0] = 4;
+      cfg->ts_rate_decimator[1] = 2;
+      cfg->ts_rate_decimator[2] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, 2=ARF.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF  | VP8_EFLAG_NO_REF_GF |
+          VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
+          VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
+      layer_flags[2] = VP8_EFLAG_NO_REF_GF   | VP8_EFLAG_NO_REF_ARF |
+          VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[3] =
+      layer_flags[5] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
+      layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
+          VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[6] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_ARF;
+      layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
+          VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_ENTROPY;
+      break;
+    }
+    case 10: {
+      // 3-layers structure where ARF is used as predictor for all frames,
+      // and is only updated on key frame.
+      // Sync points for layer 1 and 2 every 8 frames.
+
+      int ids[4] = {0, 2, 1, 2};
+      cfg->ts_periodicity = 4;
+      *flag_periodicity = 8;
+      cfg->ts_number_layers = 3;
+      cfg->ts_rate_decimator[0] = 4;
+      cfg->ts_rate_decimator[1] = 2;
+      cfg->ts_rate_decimator[2] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, 2=ARF.
+      // Layer 0: predict from L and ARF; update L and G.
+      layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_ARF |
+          VP8_EFLAG_NO_REF_GF;
+      // Layer 2: sync point: predict from L and ARF; update none.
+      layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
+          VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
+          VP8_EFLAG_NO_UPD_ENTROPY;
+      // Layer 1: sync point: predict from L and ARF; update G.
+      layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF |
+          VP8_EFLAG_NO_UPD_LAST;
+      // Layer 2: predict from L, G, ARF; update none.
+      layer_flags[3] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
+          VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
+      // Layer 0: predict from L and ARF; update L.
+      layer_flags[4] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
+          VP8_EFLAG_NO_REF_GF;
+      // Layer 2: predict from L, G, ARF; update none.
+      layer_flags[5] = layer_flags[3];
+      // Layer 1: predict from L, G, ARF; update G.
+      layer_flags[6] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
+      // Layer 2: predict from L, G, ARF; update none.
+      layer_flags[7] = layer_flags[3];
+      break;
+    }
+    case 11:
+    default: {
+      // 3-layers structure as in case 10, but no sync/refresh points for
+      // layer 1 and 2.
+      int ids[4] = {0, 2, 1, 2};
+      cfg->ts_periodicity = 4;
+      *flag_periodicity = 8;
+      cfg->ts_number_layers = 3;
+      cfg->ts_rate_decimator[0] = 4;
+      cfg->ts_rate_decimator[1] = 2;
+      cfg->ts_rate_decimator[2] = 1;
+      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
+      // 0=L, 1=GF, 2=ARF.
+      // Layer 0: predict from L and ARF; update L.
+      layer_flags[0] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
+          VP8_EFLAG_NO_REF_GF;
+      layer_flags[4] = layer_flags[0];
+      // Layer 1: predict from L, G, ARF; update G.
+      layer_flags[2] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
+      layer_flags[6] = layer_flags[2];
+      // Layer 2: predict from L, G, ARF; update none.
+      layer_flags[1] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
+          VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
+      layer_flags[3] = layer_flags[1];
+      layer_flags[5] = layer_flags[1];
+      layer_flags[7] = layer_flags[1];
+      break;
+    }
+  }
+}
+
+int main(int argc, char **argv) {
+  VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS];
+  vpx_codec_ctx_t codec;
+  vpx_codec_enc_cfg_t cfg;
+  int frame_cnt = 0;
+  vpx_image_t raw;
+  vpx_codec_err_t res;
+  unsigned int width;
+  unsigned int height;
+  int frame_avail;
+  int got_data;
+  int flags = 0;
+  int i;
+  int pts = 0;  // PTS starts at 0.
+  int frame_duration = 1;  // 1 timebase tick per frame.
+  int layering_mode = 0;
+  int frames_in_layer[VPX_TS_MAX_LAYERS] = {0};
+  int layer_flags[VPX_TS_MAX_PERIODICITY] = {0};
+  int flag_periodicity = 1;
+  int max_intra_size_pct;
+  vpx_svc_layer_id_t layer_id = {0, 0};
+  char *codec_type;
+  const vpx_codec_iface_t *(*interface)(void);
+  unsigned int fourcc;
+  struct VpxInputContext input_ctx = {0};
+
+  exec_name = argv[0];
+  // Check usage and arguments.
+  if (argc < 10) {
+    die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
+        "<rate_num> <rate_den> <mode> <Rate_0> ... <Rate_nlayers-1> \n",
+        argv[0]);
+  }
+
+  codec_type = argv[3];
+  if (strncmp(codec_type, "vp9", 3) == 0) {
+#if CONFIG_VP9_ENCODER
+    interface = vpx_codec_vp9_cx;
+    fourcc = VP9_FOURCC;
+#else
+    die("Encoder vp9 selected but not configured");
+#endif
+  } else  {
+#if CONFIG_VP8_ENCODER
+    interface = vpx_codec_vp8_cx;
+    fourcc = VP8_FOURCC;
+#else
+    die("Encoder vp8 selected but not configured");
+#endif
+  }
+  printf("Using %s\n", vpx_codec_iface_name(interface()));
+
+  width = strtol(argv[4], NULL, 0);
+  height = strtol(argv[5], NULL, 0);
+  if (width < 16 || width % 2 || height < 16 || height % 2) {
+    die("Invalid resolution: %d x %d", width, height);
+  }
+
+  layering_mode = strtol(argv[8], NULL, 0);
+  if (layering_mode < 0 || layering_mode > 11) {
+    die("Invalid mode (0..11) %s", argv[8]);
+  }
+
+  if (argc != 9 + mode_to_num_layers[layering_mode]) {
+    die("Invalid number of arguments");
+  }
+
+  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
+    die("Failed to allocate image", width, height);
+  }
+
+  // Populate encoder configuration.
+  res = vpx_codec_enc_config_default(interface(), &cfg, 0);
+  if (res) {
+    printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
+    return EXIT_FAILURE;
+  }
+
+  // Update the default configuration with our settings.
+  cfg.g_w = width;
+  cfg.g_h = height;
+
+  // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
+  cfg.g_timebase.num = strtol(argv[6], NULL, 0);
+  cfg.g_timebase.den = strtol(argv[7], NULL, 0);
+
+  for (i = 9; i < 9 + mode_to_num_layers[layering_mode]; ++i) {
+    cfg.ts_target_bitrate[i - 9] = strtol(argv[i], NULL, 0);
+  }
+
+  // Real time parameters.
+  cfg.rc_dropframe_thresh = 0;
+  cfg.rc_end_usage = VPX_CBR;
+  cfg.rc_resize_allowed = 0;
+  cfg.rc_min_quantizer = 2;
+  cfg.rc_max_quantizer = 56;
+  cfg.rc_undershoot_pct = 100;
+  cfg.rc_overshoot_pct = 15;
+  cfg.rc_buf_initial_sz = 500;
+  cfg.rc_buf_optimal_sz = 600;
+  cfg.rc_buf_sz = 1000;
+
+  // Enable error resilient mode.
+  cfg.g_error_resilient = 1;
+  cfg.g_lag_in_frames   = 0;
+  cfg.kf_mode = VPX_KF_DISABLED;
+
+  // Disable automatic keyframe placement.
+  cfg.kf_min_dist = cfg.kf_max_dist = 3000;
+
+  // Default setting for bitrate: used in special case of 1 layer (case 0).
+  cfg.rc_target_bitrate = cfg.ts_target_bitrate[0];
+
+  set_temporal_layer_pattern(layering_mode,
+                             &cfg,
+                             layer_flags,
+                             &flag_periodicity);
+
+  // Open input file.
+  input_ctx.filename = argv[1];
+  if (!(input_ctx.file = fopen(input_ctx.filename, "rb"))) {
+    die("Failed to open %s for reading", argv[1]);
+  }
+
+  // Open an output file for each stream.
+  for (i = 0; i < cfg.ts_number_layers; ++i) {
+    char file_name[PATH_MAX];
+    VpxVideoInfo info;
+    info.codec_fourcc = fourcc;
+    info.frame_width = cfg.g_w;
+    info.frame_height = cfg.g_h;
+    info.time_base.numerator = cfg.g_timebase.num;
+    info.time_base.denominator = cfg.g_timebase.den;
+
+    snprintf(file_name, sizeof(file_name), "%s_%d.ivf", argv[2], i);
+    outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
+    if (!outfile[i])
+      die("Failed to open %s for writing", file_name);
+  }
+  // No spatial layers in this encoder.
+  cfg.ss_number_layers = 1;
+
+  // Initialize codec.
+  if (vpx_codec_enc_init(&codec, interface(), &cfg, 0))
+    die_codec(&codec, "Failed to initialize encoder");
+
+  vpx_codec_control(&codec, VP8E_SET_CPUUSED, -6);
+  vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, 1);
+  if (strncmp(codec_type, "vp9", 3) == 0) {
+    vpx_codec_control(&codec, VP8E_SET_CPUUSED, 3);
+    vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, 0);
+    if (vpx_codec_control(&codec, VP9E_SET_SVC, 1)) {
+      die_codec(&codec, "Failed to set SVC");
+    }
+  }
+  vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
+  vpx_codec_control(&codec, VP8E_SET_TOKEN_PARTITIONS, 1);
+  max_intra_size_pct = (int) (((double)cfg.rc_buf_optimal_sz * 0.5)
+      * ((double) cfg.g_timebase.den / cfg.g_timebase.num) / 10.0);
+  vpx_codec_control(&codec, VP8E_SET_MAX_INTRA_BITRATE_PCT, max_intra_size_pct);
+
+  frame_avail = 1;
+  while (frame_avail || got_data) {
+    vpx_codec_iter_t iter = NULL;
+    const vpx_codec_cx_pkt_t *pkt;
+    // Update the temporal layer_id. No spatial layers in this test.
+    layer_id.spatial_layer_id = 0;
+    layer_id.temporal_layer_id =
+        cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
+    vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
+    flags = layer_flags[frame_cnt % flag_periodicity];
+    frame_avail = !read_yuv_frame(&input_ctx, &raw);
+    if (vpx_codec_encode(&codec, frame_avail? &raw : NULL, pts, 1, flags,
+        VPX_DL_REALTIME)) {
+      die_codec(&codec, "Failed to encode frame");
+    }
+    // Reset KF flag.
+    if (layering_mode != 7) {
+      layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
+    }
+    got_data = 0;
+    while ( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) {
+      got_data = 1;
+      switch (pkt->kind) {
+        case VPX_CODEC_CX_FRAME_PKT:
+          for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
+              i < cfg.ts_number_layers; ++i) {
+            vpx_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
+                                         pkt->data.frame.sz, pts);
+            ++frames_in_layer[i];
+          }
+          break;
+          default:
+            break;
+      }
+    }
+    ++frame_cnt;
+    pts += frame_duration;
+  }
+  fclose(input_ctx.file);
+  printf("Processed %d frames: \n", frame_cnt - 1);
+  if (vpx_codec_destroy(&codec))
+    die_codec(&codec, "Failed to destroy codec");
+
+  // Try to rewrite the output file headers with the actual frame count.
+  for (i = 0; i < cfg.ts_number_layers; ++i)
+    vpx_video_writer_close(outfile[i]);
+
+  return EXIT_SUCCESS;
+}
diff --git a/ivfdec.c b/ivfdec.c
index b9fec26..40394a8 100644
--- a/ivfdec.c
+++ b/ivfdec.c
@@ -108,68 +108,3 @@
 
   return 1;
 }
-
-struct vpx_video {
-  FILE *file;
-  unsigned char *buffer;
-  size_t buffer_size;
-  size_t frame_size;
-  unsigned int fourcc;
-  int width;
-  int height;
-};
-
-vpx_video_t *vpx_video_open_file(FILE *file) {
-  char raw_hdr[32];
-  vpx_video_t *video;
-
-  if (fread(raw_hdr, 1, 32, file) != 32)
-    return NULL;  // Can't read file header;
-
-  if (memcmp(IVF_SIGNATURE, raw_hdr, 4) != 0)
-    return NULL;  // Wrong IVF signature
-
-  if (mem_get_le16(raw_hdr + 4) != 0)
-    return NULL;  // Wrong IVF version
-
-  video = (vpx_video_t *)malloc(sizeof(*video));
-  video->file = file;
-  video->buffer = NULL;
-  video->buffer_size = 0;
-  video->frame_size = 0;
-  video->fourcc = mem_get_le32(raw_hdr + 8);
-  video->width = mem_get_le16(raw_hdr + 12);
-  video->height = mem_get_le16(raw_hdr + 14);
-  return video;
-}
-
-void vpx_video_close(vpx_video_t *video) {
-  if (video) {
-    free(video->buffer);
-    free(video);
-  }
-}
-
-int vpx_video_get_width(vpx_video_t *video) {
-  return video->width;
-}
-
-int vpx_video_get_height(vpx_video_t *video) {
-  return video->height;
-}
-
-unsigned int vpx_video_get_fourcc(vpx_video_t *video) {
-  return video->fourcc;
-}
-
-int vpx_video_read_frame(vpx_video_t *video) {
-  return !ivf_read_frame(video->file, &video->buffer, &video->frame_size,
-                         &video->buffer_size);
-}
-
-const unsigned char *vpx_video_get_frame(vpx_video_t *video, size_t *size) {
-  if (size)
-    *size = video->frame_size;
-
-  return video->buffer;
-}
diff --git a/ivfdec.h b/ivfdec.h
index e960b4a..dd29cc6 100644
--- a/ivfdec.h
+++ b/ivfdec.h
@@ -21,34 +21,6 @@
 int ivf_read_frame(FILE *infile, uint8_t **buffer,
                    size_t *bytes_read, size_t *buffer_size);
 
-// The following code is work in progress. It is going to be in a separate file
-// and support transparent reading of IVF and Y4M formats. Right now only IVF
-// format is supported for simplicity. The main goal the API is to be
-// simple and easy to use in example code (and probably in vpxenc/vpxdec later).
-// All low-level details like memory buffer management are hidden from API
-// users.
-struct vpx_video;
-typedef struct vpx_video vpx_video_t;
-
-// Opens the input file and inspects it to determine file type. Returns an
-// opaque vpx_video_t* upon success, or NULL upon failure.
-vpx_video_t *vpx_video_open_file(FILE *file);
-
-// Frees all resources associated with vpx_video_t returned from
-// vpx_video_open_file() call
-void vpx_video_close(vpx_video_t *video);
-
-int vpx_video_get_width(vpx_video_t *video);
-int vpx_video_get_height(vpx_video_t *video);
-unsigned int vpx_video_get_fourcc(vpx_video_t *video);
-
-// Reads video frame bytes from the file and stores them into internal buffer.
-int vpx_video_read_frame(vpx_video_t *video);
-
-// Returns the pointer to internal memory buffer with frame bytes read from
-// last call to vpx_video_read_frame().
-const unsigned char *vpx_video_get_frame(vpx_video_t *video, size_t *size);
-
 #ifdef __cplusplus
 }  /* extern "C" */
 #endif
diff --git a/test/datarate_test.cc b/test/datarate_test.cc
index db7dfdb..4bcb0b2 100644
--- a/test/datarate_test.cc
+++ b/test/datarate_test.cc
@@ -200,21 +200,102 @@
     frame_number_ = 0;
     first_drop_ = 0;
     num_drops_ = 0;
-    bits_total_ = 0;
-    duration_ = 0.0;
+    // For testing up to 3 layers.
+    for (int i = 0; i < 3; ++i) {
+      bits_total_[i] = 0;
+    }
+  }
+
+  //
+  // Frame flags and layer id for temporal layers.
+  //
+
+  // For two layers, test pattern is:
+  //   1     3
+  // 0    2     .....
+  // For three layers, test pattern is:
+  //   1      3    5      7
+  //      2           6
+  // 0          4            ....
+  // LAST is always update on base/layer 0, GOLDEN is updated on layer 1.
+  // For this 3 layer example, the 2nd enhancement layer (layer 2) does not
+  // update any reference frames.
+  int SetFrameFlags(int frame_num, int num_temp_layers) {
+    int frame_flags = 0;
+    if (num_temp_layers == 2) {
+      if (frame_num % 2 == 0) {
+        // Layer 0: predict from L and ARF, update L.
+        frame_flags = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
+                      VP8_EFLAG_NO_UPD_ARF;
+      } else {
+        // Layer 1: predict from L, G and ARF, and update G.
+        frame_flags = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
+                      VP8_EFLAG_NO_UPD_ENTROPY;
+      }
+    } else if (num_temp_layers == 3) {
+      if (frame_num % 4 == 0) {
+        // Layer 0: predict from L and ARF; update L.
+        frame_flags = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
+                      VP8_EFLAG_NO_REF_GF;
+      } else if ((frame_num - 2) % 4 == 0) {
+        // Layer 1: predict from L, G, ARF; update G.
+        frame_flags = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
+      }  else if ((frame_num - 1) % 2 == 0) {
+        // Layer 2: predict from L, G, ARF; update none.
+        frame_flags = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
+                      VP8_EFLAG_NO_UPD_LAST;
+      }
+    }
+    return frame_flags;
+  }
+
+  int SetLayerId(int frame_num, int num_temp_layers) {
+    int layer_id = 0;
+    if (num_temp_layers == 2) {
+      if (frame_num % 2 == 0) {
+        layer_id = 0;
+      } else {
+        layer_id = 1;
+      }
+    } else if (num_temp_layers == 3) {
+      if (frame_num % 4 == 0) {
+        layer_id = 0;
+      } else if ((frame_num - 2) % 4 == 0) {
+        layer_id = 1;
+      } else if ((frame_num - 1) % 2 == 0) {
+        layer_id = 2;
+      }
+    }
+    return layer_id;
   }
 
   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
-                                    ::libvpx_test::Encoder *encoder) {
+                                  ::libvpx_test::Encoder *encoder) {
     if (video->frame() == 1) {
       encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
     }
+    if (cfg_.ts_number_layers > 1) {
+      if (video->frame() == 1) {
+        encoder->Control(VP9E_SET_SVC, 1);
+      }
+      vpx_svc_layer_id_t layer_id = {0, 0};
+      layer_id.spatial_layer_id = 0;
+      frame_flags_ = SetFrameFlags(video->frame(), cfg_.ts_number_layers);
+      layer_id.temporal_layer_id = SetLayerId(video->frame(),
+                                              cfg_.ts_number_layers);
+      if (video->frame() > 0) {
+       encoder->Control(VP9E_SET_SVC_LAYER_ID, &layer_id);
+      }
+    }
     const vpx_rational_t tb = video->timebase();
     timebase_ = static_cast<double>(tb.num) / tb.den;
     duration_ = 0;
   }
 
+
   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
+    int layer = SetLayerId(frame_number_, cfg_.ts_number_layers);
+
     // Time since last timestamp = duration.
     vpx_codec_pts_t duration = pkt->data.frame.pts - last_pts_;
 
@@ -227,7 +308,12 @@
         << pkt->data.frame.pts;
 
     const size_t frame_size_in_bits = pkt->data.frame.sz * 8;
-    bits_total_ += frame_size_in_bits;
+
+    // Update the total encoded bits. For temporal layers, update the cumulative
+    // encoded bits per layer.
+    for (int i = layer; i < static_cast<int>(cfg_.ts_number_layers); ++i) {
+      bits_total_[i] += frame_size_in_bits;
+    }
 
     // If first drop not set and we have a drop set it to this time.
     if (!first_drop_ && duration > 1)
@@ -244,19 +330,22 @@
   }
 
   virtual void EndPassHook(void) {
-    if (bits_total_) {
+    for (int layer = 0; layer < static_cast<int>(cfg_.ts_number_layers);
+        ++layer) {
       duration_ = (last_pts_ + 1) * timebase_;
-      // Effective file datarate:
-      effective_datarate_ = ((bits_total_) / 1000.0) / duration_;
+      if (bits_total_[layer]) {
+        // Effective file datarate:
+        effective_datarate_[layer] = (bits_total_[layer] / 1000.0) / duration_;
+      }
     }
   }
 
   vpx_codec_pts_t last_pts_;
   double timebase_;
   int frame_number_;
-  int64_t bits_total_;
+  int64_t bits_total_[3];
   double duration_;
-  double effective_datarate_;
+  double effective_datarate_[3];
   int set_cpu_used_;
   int64_t bits_in_buffer_model_;
   vpx_codec_pts_t first_drop_;
@@ -272,6 +361,7 @@
   cfg_.rc_min_quantizer = 0;
   cfg_.rc_max_quantizer = 63;
   cfg_.rc_end_usage = VPX_CBR;
+  cfg_.g_lag_in_frames = 0;
 
   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
                                        30, 1, 0, 140);
@@ -279,12 +369,10 @@
     cfg_.rc_target_bitrate = i;
     ResetModel();
     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
-    ASSERT_GE(static_cast<double>(cfg_.rc_target_bitrate),
-              effective_datarate_ * 0.85)
-        << " The datarate for the file exceeds the target by too much!";
-    ASSERT_LE(static_cast<double>(cfg_.rc_target_bitrate),
-              effective_datarate_ * 1.15)
-        << " The datarate for the file missed the target!";
+    ASSERT_GE(effective_datarate_[0], cfg_.rc_target_bitrate * 0.85)
+        << " The datarate for the file is lower than target by too much!";
+    ASSERT_LE(effective_datarate_[0], cfg_.rc_target_bitrate * 1.15)
+        << " The datarate for the file is greater than target by too much!";
   }
 }
 
@@ -309,10 +397,10 @@
     ResetModel();
     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
     ASSERT_GE(static_cast<double>(cfg_.rc_target_bitrate),
-              effective_datarate_ * 0.85)
+              effective_datarate_[0] * 0.85)
         << " The datarate for the file exceeds the target by too much!";
     ASSERT_LE(static_cast<double>(cfg_.rc_target_bitrate),
-              effective_datarate_ * 1.15)
+              effective_datarate_[0] * 1.15)
         << " The datarate for the file missed the target!"
         << cfg_.rc_target_bitrate << " "<< effective_datarate_;
   }
@@ -334,6 +422,7 @@
   cfg_.rc_max_quantizer = 50;
   cfg_.rc_end_usage = VPX_CBR;
   cfg_.rc_target_bitrate = 200;
+  cfg_.g_lag_in_frames = 0;
 
   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
                                        30, 1, 0, 140);
@@ -345,10 +434,10 @@
     cfg_.rc_dropframe_thresh = i;
     ResetModel();
     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
-    ASSERT_GE(effective_datarate_, cfg_.rc_target_bitrate * 0.85)
-           << " The datarate for the file is lower than target by too much!";
-    ASSERT_LE(effective_datarate_, cfg_.rc_target_bitrate * 1.15)
-           << " The datarate for the file is greater than target by too much!";
+    ASSERT_GE(effective_datarate_[0], cfg_.rc_target_bitrate * 0.85)
+        << " The datarate for the file is lower than target by too much!";
+    ASSERT_LE(effective_datarate_[0], cfg_.rc_target_bitrate * 1.15)
+        << " The datarate for the file is greater than target by too much!";
     ASSERT_LE(first_drop_, last_drop)
         << " The first dropped frame for drop_thresh " << i
         << " > first dropped frame for drop_thresh "
@@ -362,6 +451,81 @@
   }
 }
 
+// Check basic rate targeting for 2 temporal layers.
+TEST_P(DatarateTestVP9, BasicRateTargeting2TemporalLayers) {
+  cfg_.rc_buf_initial_sz = 500;
+  cfg_.rc_buf_optimal_sz = 500;
+  cfg_.rc_buf_sz = 1000;
+  cfg_.rc_dropframe_thresh = 1;
+  cfg_.rc_min_quantizer = 0;
+  cfg_.rc_max_quantizer = 63;
+  cfg_.rc_end_usage = VPX_CBR;
+  cfg_.g_lag_in_frames = 0;
+
+  // 2 Temporal layers, no spatial layers: Framerate decimation (2, 1).
+  cfg_.ss_number_layers = 1;
+  cfg_.ts_number_layers = 2;
+  cfg_.ts_rate_decimator[0] = 2;
+  cfg_.ts_rate_decimator[1] = 1;
+
+  ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
+                                       30, 1, 0, 200);
+  for (int i = 200; i <= 800; i += 200) {
+    cfg_.rc_target_bitrate = i;
+    ResetModel();
+    // 60-40 bitrate allocation for 2 temporal layers.
+    cfg_.ts_target_bitrate[0] = 60 * cfg_.rc_target_bitrate / 100;
+    cfg_.ts_target_bitrate[1] = cfg_.rc_target_bitrate;
+    ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
+    for (int j = 0; j < static_cast<int>(cfg_.ts_number_layers); ++j) {
+      ASSERT_GE(effective_datarate_[j], cfg_.ts_target_bitrate[j] * 0.85)
+          << " The datarate for the file is lower than target by too much, "
+              "for layer: " << j;
+      ASSERT_LE(effective_datarate_[j], cfg_.ts_target_bitrate[j] * 1.15)
+          << " The datarate for the file is greater than target by too much, "
+              "for layer: " << j;
+    }
+  }
+}
+
+// Check basic rate targeting for 3 temporal layers.
+TEST_P(DatarateTestVP9, BasicRateTargeting3TemporalLayers) {
+  cfg_.rc_buf_initial_sz = 500;
+  cfg_.rc_buf_optimal_sz = 500;
+  cfg_.rc_buf_sz = 1000;
+  cfg_.rc_dropframe_thresh = 1;
+  cfg_.rc_min_quantizer = 0;
+  cfg_.rc_max_quantizer = 63;
+  cfg_.rc_end_usage = VPX_CBR;
+  cfg_.g_lag_in_frames = 0;
+
+  // 3 Temporal layers, no spatial layers: Framerate decimation (4, 2, 1).
+  cfg_.ss_number_layers = 1;
+  cfg_.ts_number_layers = 3;
+  cfg_.ts_rate_decimator[0] = 4;
+  cfg_.ts_rate_decimator[1] = 2;
+  cfg_.ts_rate_decimator[2] = 1;
+
+  ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
+                                       30, 1, 0, 200);
+  for (int i = 200; i <= 800; i += 200) {
+    cfg_.rc_target_bitrate = i;
+    ResetModel();
+    // 40-20-40 bitrate allocation for 3 temporal layers.
+    cfg_.ts_target_bitrate[0] = 40 * cfg_.rc_target_bitrate / 100;
+    cfg_.ts_target_bitrate[1] = 60 * cfg_.rc_target_bitrate / 100;
+    cfg_.ts_target_bitrate[2] = cfg_.rc_target_bitrate;
+    ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
+    for (int j = 0; j < static_cast<int>(cfg_.ts_number_layers); ++j) {
+      ASSERT_GE(effective_datarate_[j], cfg_.ts_target_bitrate[j] * 0.85)
+          << " The datarate for the file is lower than target by too much, "
+              "for layer: " << j;
+      ASSERT_LE(effective_datarate_[j], cfg_.ts_target_bitrate[j] * 1.15)
+          << " The datarate for the file is greater than target by too much, "
+              "for layer: " << j;
+    }
+  }
+}
 VP8_INSTANTIATE_TEST_CASE(DatarateTest, ALL_TEST_MODES);
 VP9_INSTANTIATE_TEST_CASE(DatarateTestVP9,
                           ::testing::Values(::libvpx_test::kOnePassGood),
diff --git a/test/dct16x16_test.cc b/test/dct16x16_test.cc
index ce04318..8d115fa 100644
--- a/test/dct16x16_test.cc
+++ b/test/dct16x16_test.cc
@@ -273,7 +273,7 @@
 }
 
 void fht16x16_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
-  vp9_short_fht16x16_c(in, out, stride, tx_type);
+  vp9_fht16x16_c(in, out, stride, tx_type);
 }
 
 class Trans16x16TestBase {
@@ -507,10 +507,10 @@
 INSTANTIATE_TEST_CASE_P(
     C, Trans16x16HT,
     ::testing::Values(
-        make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 0),
-        make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 1),
-        make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 2),
-        make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 3)));
+        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 0),
+        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 1),
+        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 2),
+        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 3)));
 
 #if HAVE_SSE2
 INSTANTIATE_TEST_CASE_P(
@@ -521,9 +521,9 @@
 INSTANTIATE_TEST_CASE_P(
     SSE2, Trans16x16HT,
     ::testing::Values(
-        make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 0),
-        make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 1),
-        make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 2),
-        make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 3)));
+        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 0),
+        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 1),
+        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 2),
+        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 3)));
 #endif
 }  // namespace
diff --git a/test/encode_test_driver.h b/test/encode_test_driver.h
index 4dabcd5..8017a2a 100644
--- a/test/encode_test_driver.h
+++ b/test/encode_test_driver.h
@@ -123,6 +123,11 @@
     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
   }
 
+  void Control(int ctrl_id, struct vpx_svc_layer_id *arg) {
+    const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
+    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
+  }
+
   void set_deadline(unsigned long deadline) {
     deadline_ = deadline;
   }
diff --git a/test/fdct4x4_test.cc b/test/fdct4x4_test.cc
index 5db5f5c..dc66687 100644
--- a/test/fdct4x4_test.cc
+++ b/test/fdct4x4_test.cc
@@ -45,7 +45,7 @@
 }
 
 void fht4x4_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
-  vp9_short_fht4x4_c(in, out, stride, tx_type);
+  vp9_fht4x4_c(in, out, stride, tx_type);
 }
 
 class Trans4x4TestBase {
@@ -281,10 +281,10 @@
 INSTANTIATE_TEST_CASE_P(
     C, Trans4x4HT,
     ::testing::Values(
-        make_tuple(&vp9_short_fht4x4_c, &vp9_iht4x4_16_add_c, 0),
-        make_tuple(&vp9_short_fht4x4_c, &vp9_iht4x4_16_add_c, 1),
-        make_tuple(&vp9_short_fht4x4_c, &vp9_iht4x4_16_add_c, 2),
-        make_tuple(&vp9_short_fht4x4_c, &vp9_iht4x4_16_add_c, 3)));
+        make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 0),
+        make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 1),
+        make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 2),
+        make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 3)));
 
 #if HAVE_SSE2
 INSTANTIATE_TEST_CASE_P(
@@ -295,10 +295,10 @@
 INSTANTIATE_TEST_CASE_P(
     SSE2, Trans4x4HT,
     ::testing::Values(
-        make_tuple(&vp9_short_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 0),
-        make_tuple(&vp9_short_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 1),
-        make_tuple(&vp9_short_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 2),
-        make_tuple(&vp9_short_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 3)));
+        make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 0),
+        make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 1),
+        make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 2),
+        make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 3)));
 #endif
 
 }  // namespace
diff --git a/test/fdct8x8_test.cc b/test/fdct8x8_test.cc
index beef980..98aabe6 100644
--- a/test/fdct8x8_test.cc
+++ b/test/fdct8x8_test.cc
@@ -44,7 +44,7 @@
 }
 
 void fht8x8_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
-  vp9_short_fht8x8_c(in, out, stride, tx_type);
+  vp9_fht8x8_c(in, out, stride, tx_type);
 }
 
 class FwdTrans8x8TestBase {
@@ -308,10 +308,10 @@
 INSTANTIATE_TEST_CASE_P(
     C, FwdTrans8x8HT,
     ::testing::Values(
-        make_tuple(&vp9_short_fht8x8_c, &vp9_iht8x8_64_add_c, 0),
-        make_tuple(&vp9_short_fht8x8_c, &vp9_iht8x8_64_add_c, 1),
-        make_tuple(&vp9_short_fht8x8_c, &vp9_iht8x8_64_add_c, 2),
-        make_tuple(&vp9_short_fht8x8_c, &vp9_iht8x8_64_add_c, 3)));
+        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 0),
+        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 1),
+        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 2),
+        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 3)));
 
 #if HAVE_SSE2
 INSTANTIATE_TEST_CASE_P(
@@ -321,9 +321,9 @@
 INSTANTIATE_TEST_CASE_P(
     SSE2, FwdTrans8x8HT,
     ::testing::Values(
-        make_tuple(&vp9_short_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 0),
-        make_tuple(&vp9_short_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 1),
-        make_tuple(&vp9_short_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 2),
-        make_tuple(&vp9_short_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 3)));
+        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 0),
+        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 1),
+        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 2),
+        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 3)));
 #endif
 }  // namespace
diff --git a/video_common.h b/video_common.h
new file mode 100644
index 0000000..44b27a8
--- /dev/null
+++ b/video_common.h
@@ -0,0 +1,23 @@
+/*
+ *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VIDEO_COMMON_H_
+#define VIDEO_COMMON_H_
+
+#include "./tools_common.h"
+
+typedef struct {
+  uint32_t codec_fourcc;
+  int frame_width;
+  int frame_height;
+  struct VpxRational time_base;
+} VpxVideoInfo;
+
+#endif  // VIDEO_COMMON_H_
diff --git a/video_reader.c b/video_reader.c
new file mode 100644
index 0000000..4be7483
--- /dev/null
+++ b/video_reader.c
@@ -0,0 +1,81 @@
+/*
+ *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "./ivfdec.h"
+#include "./video_reader.h"
+
+static const char *const kIVFSignature = "DKIF";
+
+struct VpxVideoReaderStruct {
+  VpxVideoInfo info;
+  FILE *file;
+  uint8_t *buffer;
+  size_t buffer_size;
+  size_t frame_size;
+};
+
+VpxVideoReader *vpx_video_reader_open(const char *filename) {
+  char header[32];
+  VpxVideoReader *reader = NULL;
+  FILE *const file = fopen(filename, "rb");
+  if (!file)
+    return NULL;  // Can't open file
+
+  if (fread(header, 1, 32, file) != 32)
+    return NULL;  // Can't read file header
+
+  if (memcmp(kIVFSignature, header, 4) != 0)
+    return NULL;  // Wrong IVF signature
+
+  if (mem_get_le16(header + 4) != 0)
+    return NULL;  // Wrong IVF version
+
+  reader = calloc(1, sizeof(*reader));
+  if (!reader)
+    return NULL;  // Can't allocate VpxVideoReader
+
+  reader->file = file;
+  reader->info.codec_fourcc = mem_get_le32(header + 8);
+  reader->info.frame_width = mem_get_le16(header + 12);
+  reader->info.frame_height = mem_get_le16(header + 14);
+  reader->info.time_base.numerator = mem_get_le32(header + 16);
+  reader->info.time_base.denominator = mem_get_le32(header + 20);
+
+  return reader;
+}
+
+void vpx_video_reader_close(VpxVideoReader *reader) {
+  if (reader) {
+    fclose(reader->file);
+    free(reader->buffer);
+    free(reader);
+  }
+}
+
+int vpx_video_reader_read_frame(VpxVideoReader *reader) {
+  return !ivf_read_frame(reader->file, &reader->buffer, &reader->frame_size,
+                         &reader->buffer_size);
+}
+
+const uint8_t *vpx_video_reader_get_frame(VpxVideoReader *reader,
+                                          size_t *size) {
+  if (size)
+    *size = reader->frame_size;
+
+  return reader->buffer;
+}
+
+const VpxVideoInfo *vpx_video_reader_get_info(VpxVideoReader *reader) {
+  return &reader->info;
+}
+
diff --git a/video_reader.h b/video_reader.h
new file mode 100644
index 0000000..a62c6d7
--- /dev/null
+++ b/video_reader.h
@@ -0,0 +1,52 @@
+/*
+ *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VIDEO_READER_H_
+#define VIDEO_READER_H_
+
+#include "./video_common.h"
+
+// The following code is work in progress. It is going to  support transparent
+// reading of input files. Right now only IVF format is supported for
+// simplicity. The main goal the API is to be simple and easy to use in example
+// code and in vpxenc/vpxdec later. All low-level details like memory
+// buffer management are hidden from API users.
+struct VpxVideoReaderStruct;
+typedef struct VpxVideoReaderStruct VpxVideoReader;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Opens the input file for reading and inspects it to determine file type.
+// Returns an opaque VpxVideoReader* upon success, or NULL upon failure.
+// Right now only IVF format is supported.
+VpxVideoReader *vpx_video_reader_open(const char *filename);
+
+// Frees all resources associated with VpxVideoReader* returned from
+// vpx_video_reader_open() call.
+void vpx_video_reader_close(VpxVideoReader *reader);
+
+// Reads frame from the file and stores it in internal buffer.
+int vpx_video_reader_read_frame(VpxVideoReader *reader);
+
+// Returns the pointer to memory buffer with frame data read by last call to
+// vpx_video_reader_read_frame().
+const uint8_t *vpx_video_reader_get_frame(VpxVideoReader *reader,
+                                          size_t *size);
+
+// Fills VpxVideoInfo with information from opened video file.
+const VpxVideoInfo *vpx_video_reader_get_info(VpxVideoReader *reader);
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif
+
+#endif  // VIDEO_READER_H_
diff --git a/video_writer.c b/video_writer.c
new file mode 100644
index 0000000..3695236
--- /dev/null
+++ b/video_writer.c
@@ -0,0 +1,80 @@
+/*
+ *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <stdlib.h>
+
+#include "./ivfenc.h"
+#include "./video_writer.h"
+#include "vpx/vpx_encoder.h"
+
+struct VpxVideoWriterStruct {
+  VpxVideoInfo info;
+  FILE *file;
+  int frame_count;
+};
+
+static void write_header(FILE *file, const VpxVideoInfo *info,
+                         int frame_count) {
+  struct vpx_codec_enc_cfg cfg;
+  cfg.g_w = info->frame_width;
+  cfg.g_h = info->frame_height;
+  cfg.g_timebase.num = info->time_base.numerator;
+  cfg.g_timebase.den = info->time_base.denominator;
+
+  ivf_write_file_header(file, &cfg, info->codec_fourcc, frame_count);
+}
+
+VpxVideoWriter *vpx_video_writer_open(const char *filename,
+                                      VpxContainer container,
+                                      const VpxVideoInfo *info) {
+  if (container == kContainerIVF) {
+    VpxVideoWriter *writer = NULL;
+    FILE *const file = fopen(filename, "wb");
+    if (!file)
+      return NULL;
+
+    writer = malloc(sizeof(*writer));
+    if (!writer)
+      return NULL;
+
+    writer->frame_count = 0;
+    writer->info = *info;
+    writer->file = file;
+
+    write_header(writer->file, info, 0);
+
+    return writer;
+  }
+
+  return NULL;
+}
+
+void vpx_video_writer_close(VpxVideoWriter *writer) {
+  if (writer) {
+    // Rewriting frame header with real frame count
+    rewind(writer->file);
+    write_header(writer->file, &writer->info, writer->frame_count);
+
+    fclose(writer->file);
+    free(writer);
+  }
+}
+
+int vpx_video_writer_write_frame(VpxVideoWriter *writer,
+                                 const uint8_t *buffer, size_t size,
+                                 int64_t pts) {
+  ivf_write_frame_header(writer->file, pts, size);
+  if (fwrite(buffer, 1, size, writer->file) != size)
+    return 0;
+
+  ++writer->frame_count;
+
+  return 1;
+}
diff --git a/video_writer.h b/video_writer.h
new file mode 100644
index 0000000..5dbfe52
--- /dev/null
+++ b/video_writer.h
@@ -0,0 +1,47 @@
+/*
+ *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VIDEO_WRITER_H_
+#define VIDEO_WRITER_H_
+
+#include "./video_common.h"
+
+typedef enum {
+  kContainerIVF
+} VpxContainer;
+
+struct VpxVideoWriterStruct;
+typedef struct VpxVideoWriterStruct VpxVideoWriter;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Finds and opens writer for specified container format.
+// Returns an opaque VpxVideoWriter* upon success, or NULL upon failure.
+// Right now only IVF format is supported.
+VpxVideoWriter *vpx_video_writer_open(const char *filename,
+                                      VpxContainer container,
+                                      const VpxVideoInfo *info);
+
+// Frees all resources associated with VpxVideoWriter* returned from
+// vpx_video_writer_open() call.
+void vpx_video_writer_close(VpxVideoWriter *writer);
+
+// Writes frame bytes to the file.
+int vpx_video_writer_write_frame(VpxVideoWriter *writer,
+                                 const uint8_t *buffer, size_t size,
+                                 int64_t pts);
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif
+
+#endif  // VIDEO_WRITER_H_
diff --git a/vp8/encoder/firstpass.c b/vp8/encoder/firstpass.c
index 968c7f3..afcda9f 100644
--- a/vp8/encoder/firstpass.c
+++ b/vp8/encoder/firstpass.c
@@ -940,9 +940,9 @@
     /* Crude estimate of overhead cost from modes
      * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
      */
-    mode_cost =((((av_pct_inter - av_pct_motion) * zz_cost) +
-                (av_pct_motion * motion_cost) +
-                (av_intra * intra_cost)) * cpi->common.MBs) * 512;
+    mode_cost = (int64_t)((((av_pct_inter - av_pct_motion) * zz_cost) +
+                             (av_pct_motion * motion_cost) +
+                             (av_intra * intra_cost)) * cpi->common.MBs) * 512;
 
     return mv_cost + mode_cost;
 }
@@ -2310,7 +2310,7 @@
                 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
 
                 cpi->twopass.alt_extra_bits =
-                    (cpi->twopass.gf_group_bits * pct_extra) / 100;
+                    (int)(cpi->twopass.gf_group_bits * pct_extra) / 100;
                 cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
                 cpi->twopass.alt_extra_bits /=
                     ((cpi->baseline_gf_interval-1)>>1);
@@ -2386,7 +2386,7 @@
             target_frame_size = max_bits;
 
         if (target_frame_size > cpi->twopass.gf_group_bits)
-            target_frame_size = cpi->twopass.gf_group_bits;
+            target_frame_size = (int)cpi->twopass.gf_group_bits;
     }
 
     /* Adjust error and bits remaining */
diff --git a/vp8_scalable_patterns.c b/vp8_scalable_patterns.c
deleted file mode 100644
index 870edf1..0000000
--- a/vp8_scalable_patterns.c
+++ /dev/null
@@ -1,694 +0,0 @@
-/*
- *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-
-/*
- * This is an example demonstrating how to implement a multi-layer VP8
- * encoding scheme based on temporal scalability for video applications
- * that benefit from a scalable bitstream.
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#define VPX_CODEC_DISABLE_COMPAT 1
-#include "vpx/vpx_encoder.h"
-#include "vpx/vp8cx.h"
-#define interface (vpx_codec_vp8_cx())
-#define fourcc    0x30385056
-
-#define IVF_FILE_HDR_SZ  (32)
-#define IVF_FRAME_HDR_SZ (12)
-
-static void mem_put_le16(char *mem, unsigned int val) {
-    mem[0] = val;
-    mem[1] = val>>8;
-}
-
-static void mem_put_le32(char *mem, unsigned int val) {
-    mem[0] = val;
-    mem[1] = val>>8;
-    mem[2] = val>>16;
-    mem[3] = val>>24;
-}
-
-static void die(const char *fmt, ...) {
-    va_list ap;
-
-    va_start(ap, fmt);
-    vprintf(fmt, ap);
-    if(fmt[strlen(fmt)-1] != '\n')
-        printf("\n");
-    exit(EXIT_FAILURE);
-}
-
-static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
-    const char *detail = vpx_codec_error_detail(ctx);
-
-    printf("%s: %s\n", s, vpx_codec_error(ctx));
-    if(detail)
-        printf("    %s\n",detail);
-    exit(EXIT_FAILURE);
-}
-
-static int read_frame(FILE *f, vpx_image_t *img) {
-    size_t nbytes, to_read;
-    int    res = 1;
-
-    to_read = img->w*img->h*3/2;
-    nbytes = fread(img->planes[0], 1, to_read, f);
-    if(nbytes != to_read) {
-        res = 0;
-        if(nbytes > 0)
-            printf("Warning: Read partial frame. Check your width & height!\n");
-    }
-    return res;
-}
-
-static void write_ivf_file_header(FILE *outfile,
-                                  const vpx_codec_enc_cfg_t *cfg,
-                                  int frame_cnt) {
-    char header[32];
-
-    if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
-        return;
-    header[0] = 'D';
-    header[1] = 'K';
-    header[2] = 'I';
-    header[3] = 'F';
-    mem_put_le16(header+4,  0);                   /* version */
-    mem_put_le16(header+6,  32);                  /* headersize */
-    mem_put_le32(header+8,  fourcc);              /* headersize */
-    mem_put_le16(header+12, cfg->g_w);            /* width */
-    mem_put_le16(header+14, cfg->g_h);            /* height */
-    mem_put_le32(header+16, cfg->g_timebase.den); /* rate */
-    mem_put_le32(header+20, cfg->g_timebase.num); /* scale */
-    mem_put_le32(header+24, frame_cnt);           /* length */
-    mem_put_le32(header+28, 0);                   /* unused */
-
-    (void) fwrite(header, 1, 32, outfile);
-}
-
-
-static void write_ivf_frame_header(FILE *outfile,
-                                   const vpx_codec_cx_pkt_t *pkt)
-{
-    char             header[12];
-    vpx_codec_pts_t  pts;
-
-    if(pkt->kind != VPX_CODEC_CX_FRAME_PKT)
-        return;
-
-    pts = pkt->data.frame.pts;
-    mem_put_le32(header, pkt->data.frame.sz);
-    mem_put_le32(header+4, pts&0xFFFFFFFF);
-    mem_put_le32(header+8, pts >> 32);
-
-    (void) fwrite(header, 1, 12, outfile);
-}
-
-static int mode_to_num_layers[12] = {1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3};
-
-int main(int argc, char **argv) {
-    FILE                *infile, *outfile[VPX_TS_MAX_LAYERS];
-    vpx_codec_ctx_t      codec;
-    vpx_codec_enc_cfg_t  cfg;
-    int                  frame_cnt = 0;
-    vpx_image_t          raw;
-    vpx_codec_err_t      res;
-    unsigned int         width;
-    unsigned int         height;
-    int                  frame_avail;
-    int                  got_data;
-    int                  flags = 0;
-    int                  i;
-    int                  pts = 0;              /* PTS starts at 0 */
-    int                  frame_duration = 1;   /* 1 timebase tick per frame */
-
-    int                  layering_mode = 0;
-    int                  frames_in_layer[VPX_TS_MAX_LAYERS] = {0};
-    int                  layer_flags[VPX_TS_MAX_PERIODICITY] = {0};
-    int                  flag_periodicity;
-    int                  max_intra_size_pct;
-
-    /* Check usage and arguments */
-    if (argc < 9)
-        die("Usage: %s <infile> <outfile> <width> <height> <rate_num> "
-            " <rate_den> <mode> <Rate_0> ... <Rate_nlayers-1>\n", argv[0]);
-
-    width  = strtol (argv[3], NULL, 0);
-    height = strtol (argv[4], NULL, 0);
-    if (width < 16 || width%2 || height <16 || height%2)
-        die ("Invalid resolution: %d x %d", width, height);
-
-    if (!sscanf(argv[7], "%d", &layering_mode))
-        die ("Invalid mode %s", argv[7]);
-    if (layering_mode<0 || layering_mode>11)
-        die ("Invalid mode (0..11) %s", argv[7]);
-
-    if (argc != 8+mode_to_num_layers[layering_mode])
-        die ("Invalid number of arguments");
-
-    if (!vpx_img_alloc (&raw, VPX_IMG_FMT_I420, width, height, 32))
-        die ("Failed to allocate image", width, height);
-
-    printf("Using %s\n",vpx_codec_iface_name(interface));
-
-    /* Populate encoder configuration */
-    res = vpx_codec_enc_config_default(interface, &cfg, 0);
-    if(res) {
-        printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
-        return EXIT_FAILURE;
-    }
-
-    /* Update the default configuration with our settings */
-    cfg.g_w = width;
-    cfg.g_h = height;
-
-    /* Timebase format e.g. 30fps: numerator=1, demoninator=30 */
-    if (!sscanf (argv[5], "%d", &cfg.g_timebase.num ))
-        die ("Invalid timebase numerator %s", argv[5]);
-    if (!sscanf (argv[6], "%d", &cfg.g_timebase.den ))
-        die ("Invalid timebase denominator %s", argv[6]);
-
-    for (i=8; i<8+mode_to_num_layers[layering_mode]; i++)
-        if (!sscanf(argv[i], "%ud", &cfg.ts_target_bitrate[i-8]))
-            die ("Invalid data rate %s", argv[i]);
-
-    /* Real time parameters */
-    cfg.rc_dropframe_thresh = 0;
-    cfg.rc_end_usage        = VPX_CBR;
-    cfg.rc_resize_allowed   = 0;
-    cfg.rc_min_quantizer    = 2;
-    cfg.rc_max_quantizer    = 56;
-    cfg.rc_undershoot_pct   = 100;
-    cfg.rc_overshoot_pct    = 15;
-    cfg.rc_buf_initial_sz   = 500;
-    cfg.rc_buf_optimal_sz   = 600;
-    cfg.rc_buf_sz           = 1000;
-
-    /* Enable error resilient mode */
-    cfg.g_error_resilient = 1;
-    cfg.g_lag_in_frames   = 0;
-    cfg.kf_mode           = VPX_KF_DISABLED;
-
-    /* Disable automatic keyframe placement */
-    cfg.kf_min_dist = cfg.kf_max_dist = 3000;
-
-    /* Default setting for bitrate: used in special case of 1 layer (case 0). */
-    cfg.rc_target_bitrate = cfg.ts_target_bitrate[0];
-
-    /* Temporal scaling parameters: */
-    /* NOTE: The 3 prediction frames cannot be used interchangeably due to
-     * differences in the way they are handled throughout the code. The
-     * frames should be allocated to layers in the order LAST, GF, ARF.
-     * Other combinations work, but may produce slightly inferior results.
-     */
-    switch (layering_mode)
-    {
-    case 0:
-    {
-        /* 1-layer */
-       int ids[1] = {0};
-       cfg.ts_number_layers     = 1;
-       cfg.ts_periodicity       = 1;
-       cfg.ts_rate_decimator[0] = 1;
-       memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-       flag_periodicity = cfg.ts_periodicity;
-
-       // Update L only.
-       layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                        VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-       break;
-    }
-    case 1:
-    {
-        /* 2-layers, 2-frame period */
-        int ids[2] = {0,1};
-        cfg.ts_number_layers     = 2;
-        cfg.ts_periodicity       = 2;
-        cfg.ts_rate_decimator[0] = 2;
-        cfg.ts_rate_decimator[1] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = cfg.ts_periodicity;
-#if 1
-        /* 0=L, 1=GF, Intra-layer prediction enabled */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
-        layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
-                         VP8_EFLAG_NO_REF_ARF;
-#else
-        /* 0=L, 1=GF, Intra-layer prediction disabled */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
-        layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
-                         VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_LAST;
-#endif
-        break;
-    }
-
-    case 2:
-    {
-        /* 2-layers, 3-frame period */
-        int ids[3] = {0,1,1};
-        cfg.ts_number_layers     = 2;
-        cfg.ts_periodicity       = 3;
-        cfg.ts_rate_decimator[0] = 3;
-        cfg.ts_rate_decimator[1] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = cfg.ts_periodicity;
-
-        /* 0=L, 1=GF, Intra-layer prediction enabled */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[1] =
-        layer_flags[2] = VP8_EFLAG_NO_REF_GF  |
-                         VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
-                                                VP8_EFLAG_NO_UPD_LAST;
-        break;
-    }
-
-    case 3:
-    {
-        /* 3-layers, 6-frame period */
-        int ids[6] = {0,2,2,1,2,2};
-        cfg.ts_number_layers     = 3;
-        cfg.ts_periodicity       = 6;
-        cfg.ts_rate_decimator[0] = 6;
-        cfg.ts_rate_decimator[1] = 3;
-        cfg.ts_rate_decimator[2] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = cfg.ts_periodicity;
-
-        /* 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
-                                                VP8_EFLAG_NO_UPD_LAST;
-        layer_flags[1] =
-        layer_flags[2] =
-        layer_flags[4] =
-        layer_flags[5] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
-        break;
-    }
-
-    case 4:
-    {
-        /* 3-layers, 4-frame period */
-        int ids[4] = {0,2,1,2};
-        cfg.ts_number_layers     = 3;
-        cfg.ts_periodicity       = 4;
-        cfg.ts_rate_decimator[0] = 4;
-        cfg.ts_rate_decimator[1] = 2;
-        cfg.ts_rate_decimator[2] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = cfg.ts_periodicity;
-
-        /* 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_UPD_LAST;
-        layer_flags[1] =
-        layer_flags[3] = VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
-                         VP8_EFLAG_NO_UPD_ARF;
-        break;
-    }
-
-    case 5:
-    {
-        /* 3-layers, 4-frame period */
-        int ids[4] = {0,2,1,2};
-        cfg.ts_number_layers     = 3;
-        cfg.ts_periodicity       = 4;
-        cfg.ts_rate_decimator[0] = 4;
-        cfg.ts_rate_decimator[1] = 2;
-        cfg.ts_rate_decimator[2] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = cfg.ts_periodicity;
-
-        /* 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1,
-         * disabled in layer 2
-         */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[2] = VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[1] =
-        layer_flags[3] = VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
-                         VP8_EFLAG_NO_UPD_ARF;
-        break;
-    }
-
-    case 6:
-    {
-        /* 3-layers, 4-frame period */
-        int ids[4] = {0,2,1,2};
-        cfg.ts_number_layers     = 3;
-        cfg.ts_periodicity       = 4;
-        cfg.ts_rate_decimator[0] = 4;
-        cfg.ts_rate_decimator[1] = 2;
-        cfg.ts_rate_decimator[2] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = cfg.ts_periodicity;
-
-        /* 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[2] = VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[1] =
-        layer_flags[3] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
-        break;
-    }
-
-    case 7:
-    {
-        /* NOTE: Probably of academic interest only */
-
-        /* 5-layers, 16-frame period */
-        int ids[16] = {0,4,3,4,2,4,3,4,1,4,3,4,2,4,3,4};
-        cfg.ts_number_layers     = 5;
-        cfg.ts_periodicity       = 16;
-        cfg.ts_rate_decimator[0] = 16;
-        cfg.ts_rate_decimator[1] = 8;
-        cfg.ts_rate_decimator[2] = 4;
-        cfg.ts_rate_decimator[3] = 2;
-        cfg.ts_rate_decimator[4] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = cfg.ts_periodicity;
-
-        layer_flags[0]  = VPX_EFLAG_FORCE_KF;
-        layer_flags[1]  =
-        layer_flags[3]  =
-        layer_flags[5]  =
-        layer_flags[7]  =
-        layer_flags[9]  =
-        layer_flags[11] =
-        layer_flags[13] =
-        layer_flags[15] = VP8_EFLAG_NO_UPD_LAST |
-                          VP8_EFLAG_NO_UPD_GF   |
-                          VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[2]  =
-        layer_flags[6]  =
-        layer_flags[10] =
-        layer_flags[14] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
-        layer_flags[4]  =
-        layer_flags[12] = VP8_EFLAG_NO_REF_LAST |
-                          VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[8]  = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
-        break;
-    }
-
-    case 8:
-    {
-        /* 2-layers, with sync point at first frame of layer 1. */
-        int ids[2] = {0,1};
-        cfg.ts_number_layers     = 2;
-        cfg.ts_periodicity       = 2;
-        cfg.ts_rate_decimator[0] = 2;
-        cfg.ts_rate_decimator[1] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = 8;
-
-        /* 0=L, 1=GF */
-        // ARF is used as predictor for all frames, and is only updated on
-        // key frame. Sync point every 8 frames.
-
-        // Layer 0: predict from L and ARF, update L and G.
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_REF_GF |
-                         VP8_EFLAG_NO_UPD_ARF;
-
-        // Layer 1: sync point: predict from L and ARF, and update G.
-        layer_flags[1] = VP8_EFLAG_NO_REF_GF |
-                         VP8_EFLAG_NO_UPD_LAST |
-                         VP8_EFLAG_NO_UPD_ARF;
-
-        // Layer 0, predict from L and ARF, update L.
-        layer_flags[2] = VP8_EFLAG_NO_REF_GF  |
-                         VP8_EFLAG_NO_UPD_GF  |
-                         VP8_EFLAG_NO_UPD_ARF;
-
-        // Layer 1: predict from L, G and ARF, and update G.
-        layer_flags[3] = VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_UPD_LAST |
-                         VP8_EFLAG_NO_UPD_ENTROPY;
-
-        // Layer 0
-        layer_flags[4] = layer_flags[2];
-
-        // Layer 1
-        layer_flags[5] = layer_flags[3];
-
-        // Layer 0
-        layer_flags[6] = layer_flags[4];
-
-        // Layer 1
-        layer_flags[7] = layer_flags[5];
-        break;
-    }
-
-    case 9:
-    {
-        /* 3-layers */
-        // Sync points for layer 1 and 2 every 8 frames.
-
-        int ids[4] = {0,2,1,2};
-        cfg.ts_number_layers     = 3;
-        cfg.ts_periodicity       = 4;
-        cfg.ts_rate_decimator[0] = 4;
-        cfg.ts_rate_decimator[1] = 2;
-        cfg.ts_rate_decimator[2] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = 8;
-
-        /* 0=L, 1=GF, 2=ARF */
-        layer_flags[0] = VPX_EFLAG_FORCE_KF  |
-                         VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
-        layer_flags[2] = VP8_EFLAG_NO_REF_GF   | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[3] =
-        layer_flags[5] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
-        layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[6] = VP8_EFLAG_NO_REF_ARF |
-                         VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
-        layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
-                         VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_UPD_ENTROPY;
-        break;
-    }
-    case 10:
-    {
-        // 3-layers structure where ARF is used as predictor for all frames,
-        // and is only updated on key frame.
-        // Sync points for layer 1 and 2 every 8 frames.
-
-        int ids[4] = {0,2,1,2};
-        cfg.ts_number_layers     = 3;
-        cfg.ts_periodicity       = 4;
-        cfg.ts_rate_decimator[0] = 4;
-        cfg.ts_rate_decimator[1] = 2;
-        cfg.ts_rate_decimator[2] = 1;
-        memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-        flag_periodicity = 8;
-
-        /* 0=L, 1=GF, 2=ARF */
-
-        // Layer 0: predict from L and ARF; update L and G.
-        layer_flags[0] =  VPX_EFLAG_FORCE_KF  |
-                          VP8_EFLAG_NO_UPD_ARF |
-                          VP8_EFLAG_NO_REF_GF;
-
-        // Layer 2: sync point: predict from L and ARF; update none.
-        layer_flags[1] = VP8_EFLAG_NO_REF_GF |
-                         VP8_EFLAG_NO_UPD_GF |
-                         VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_UPD_LAST |
-                         VP8_EFLAG_NO_UPD_ENTROPY;
-
-        // Layer 1: sync point: predict from L and ARF; update G.
-        layer_flags[2] = VP8_EFLAG_NO_REF_GF |
-                         VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_UPD_LAST;
-
-        // Layer 2: predict from L, G, ARF; update none.
-        layer_flags[3] = VP8_EFLAG_NO_UPD_GF |
-                         VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_UPD_LAST |
-                         VP8_EFLAG_NO_UPD_ENTROPY;
-
-        // Layer 0: predict from L and ARF; update L.
-        layer_flags[4] = VP8_EFLAG_NO_UPD_GF |
-                         VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_REF_GF;
-
-        // Layer 2: predict from L, G, ARF; update none.
-        layer_flags[5] = layer_flags[3];
-
-        // Layer 1: predict from L, G, ARF; update G.
-        layer_flags[6] = VP8_EFLAG_NO_UPD_ARF |
-                         VP8_EFLAG_NO_UPD_LAST;
-
-        // Layer 2: predict from L, G, ARF; update none.
-        layer_flags[7] = layer_flags[3];
-        break;
-    }
-    case 11:
-    default:
-    {
-       // 3-layers structure as in case 10, but no sync/refresh points for
-       // layer 1 and 2.
-
-       int ids[4] = {0,2,1,2};
-       cfg.ts_number_layers     = 3;
-       cfg.ts_periodicity       = 4;
-       cfg.ts_rate_decimator[0] = 4;
-       cfg.ts_rate_decimator[1] = 2;
-       cfg.ts_rate_decimator[2] = 1;
-       memcpy(cfg.ts_layer_id, ids, sizeof(ids));
-
-       flag_periodicity = 8;
-
-       /* 0=L, 1=GF, 2=ARF */
-
-       // Layer 0: predict from L and ARF; update L.
-       layer_flags[0] = VP8_EFLAG_NO_UPD_GF |
-                        VP8_EFLAG_NO_UPD_ARF |
-                        VP8_EFLAG_NO_REF_GF;
-       layer_flags[4] = layer_flags[0];
-
-       // Layer 1: predict from L, G, ARF; update G.
-       layer_flags[2] = VP8_EFLAG_NO_UPD_ARF |
-                        VP8_EFLAG_NO_UPD_LAST;
-       layer_flags[6] = layer_flags[2];
-
-       // Layer 2: predict from L, G, ARF; update none.
-       layer_flags[1] = VP8_EFLAG_NO_UPD_GF |
-                        VP8_EFLAG_NO_UPD_ARF |
-                        VP8_EFLAG_NO_UPD_LAST |
-                        VP8_EFLAG_NO_UPD_ENTROPY;
-       layer_flags[3] = layer_flags[1];
-       layer_flags[5] = layer_flags[1];
-       layer_flags[7] = layer_flags[1];
-       break;
-    }
-    }
-
-    /* Open input file */
-    if(!(infile = fopen(argv[1], "rb")))
-        die("Failed to open %s for reading", argv[1]);
-
-    /* Open an output file for each stream */
-    for (i=0; i<(int)cfg.ts_number_layers; i++)
-    {
-        char file_name[512];
-        sprintf (file_name, "%s_%d.ivf", argv[2], i);
-        if (!(outfile[i] = fopen(file_name, "wb")))
-            die("Failed to open %s for writing", file_name);
-        write_ivf_file_header(outfile[i], &cfg, 0);
-    }
-
-    /* Initialize codec */
-    if (vpx_codec_enc_init (&codec, interface, &cfg, 0))
-        die_codec (&codec, "Failed to initialize encoder");
-
-    /* Cap CPU & first I-frame size */
-    vpx_codec_control (&codec, VP8E_SET_CPUUSED,                -6);
-    vpx_codec_control (&codec, VP8E_SET_STATIC_THRESHOLD,      1);
-    vpx_codec_control (&codec, VP8E_SET_NOISE_SENSITIVITY,       1);
-    vpx_codec_control(&codec, VP8E_SET_TOKEN_PARTITIONS,       1);
-
-    max_intra_size_pct = (int) (((double)cfg.rc_buf_optimal_sz * 0.5)
-                         * ((double) cfg.g_timebase.den / cfg.g_timebase.num)
-                         / 10.0);
-    /* printf ("max_intra_size_pct=%d\n", max_intra_size_pct); */
-
-    vpx_codec_control(&codec, VP8E_SET_MAX_INTRA_BITRATE_PCT,
-                      max_intra_size_pct);
-
-    frame_avail = 1;
-    while (frame_avail || got_data) {
-        vpx_codec_iter_t iter = NULL;
-        const vpx_codec_cx_pkt_t *pkt;
-
-        flags = layer_flags[frame_cnt % flag_periodicity];
-
-        frame_avail = read_frame(infile, &raw);
-        if (vpx_codec_encode(&codec, frame_avail? &raw : NULL, pts,
-                            1, flags, VPX_DL_REALTIME))
-            die_codec(&codec, "Failed to encode frame");
-
-        /* Reset KF flag */
-        if (layering_mode != 7)
-            layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
-
-        got_data = 0;
-        while ( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) {
-            got_data = 1;
-            switch (pkt->kind) {
-            case VPX_CODEC_CX_FRAME_PKT:
-                for (i=cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
-                                              i<(int)cfg.ts_number_layers; i++)
-                {
-                    write_ivf_frame_header(outfile[i], pkt);
-                    (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
-                                  outfile[i]);
-                    frames_in_layer[i]++;
-                }
-                break;
-            default:
-                break;
-            }
-        }
-        frame_cnt++;
-        pts += frame_duration;
-    }
-    fclose (infile);
-
-    printf ("Processed %d frames.\n",frame_cnt-1);
-    if (vpx_codec_destroy(&codec))
-            die_codec (&codec, "Failed to destroy codec");
-
-    /* Try to rewrite the output file headers with the actual frame count */
-    for (i=0; i<(int)cfg.ts_number_layers; i++)
-    {
-        if (!fseek(outfile[i], 0, SEEK_SET))
-            write_ivf_file_header (outfile[i], &cfg, frames_in_layer[i]);
-        fclose (outfile[i]);
-    }
-
-    return EXIT_SUCCESS;
-}
diff --git a/vp9/common/vp9_convolve.c b/vp9/common/vp9_convolve.c
index 3807ccc..d30e0b4 100644
--- a/vp9/common/vp9_convolve.c
+++ b/vp9/common/vp9_convolve.c
@@ -145,7 +145,7 @@
 }
 
 static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
-  return (const InterpKernel *)(intptr_t)f - base;
+  return (int)((const InterpKernel *)(intptr_t)f - base);
 }
 
 void vp9_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
diff --git a/vp9/common/vp9_onyx.h b/vp9/common/vp9_onyx.h
index 564e419..ac39a98 100644
--- a/vp9/common/vp9_onyx.h
+++ b/vp9/common/vp9_onyx.h
@@ -147,8 +147,12 @@
     // END DATARATE CONTROL OPTIONS
     // ----------------------------------------------------------------
 
-    // Spatial scalability
-    int ss_number_layers;
+    // Spatial and temporal scalability.
+    int ss_number_layers;  // Number of spatial layers.
+    int ts_number_layers;  // Number of temporal layers.
+    // Bitrate allocation (CBR mode) and framerate factor, for temporal layers.
+    int ts_target_bitrate[VPX_TS_MAX_LAYERS];
+    int ts_rate_decimator[VPX_TS_MAX_LAYERS];
 
     // these parameters aren't to be used in final build don't use!!!
     int play_alternate;
diff --git a/vp9/common/vp9_rtcd_defs.sh b/vp9/common/vp9_rtcd_defs.sh
index 04a40bd..7bdd11e 100644
--- a/vp9/common/vp9_rtcd_defs.sh
+++ b/vp9/common/vp9_rtcd_defs.sh
@@ -707,14 +707,14 @@
 fi
 
 # fdct functions
-prototype void vp9_short_fht4x4 "const int16_t *input, int16_t *output, int stride, int tx_type"
-specialize vp9_short_fht4x4 sse2 avx2
+prototype void vp9_fht4x4 "const int16_t *input, int16_t *output, int stride, int tx_type"
+specialize vp9_fht4x4 sse2 avx2
 
-prototype void vp9_short_fht8x8 "const int16_t *input, int16_t *output, int stride, int tx_type"
-specialize vp9_short_fht8x8 sse2 avx2
+prototype void vp9_fht8x8 "const int16_t *input, int16_t *output, int stride, int tx_type"
+specialize vp9_fht8x8 sse2 avx2
 
-prototype void vp9_short_fht16x16 "const int16_t *input, int16_t *output, int stride, int tx_type"
-specialize vp9_short_fht16x16 sse2 avx2
+prototype void vp9_fht16x16 "const int16_t *input, int16_t *output, int stride, int tx_type"
+specialize vp9_fht16x16 sse2 avx2
 
 prototype void vp9_fwht4x4 "const int16_t *input, int16_t *output, int stride"
 specialize vp9_fwht4x4
diff --git a/vp9/encoder/vp9_dct.c b/vp9/encoder/vp9_dct.c
index a840b48..a9d168c 100644
--- a/vp9/encoder/vp9_dct.c
+++ b/vp9/encoder/vp9_dct.c
@@ -18,8 +18,6 @@
 #include "vp9/common/vp9_idct.h"
 #include "vp9/common/vp9_systemdependent.h"
 
-#include "vp9/encoder/vp9_dct.h"
-
 static INLINE int fdct_round_shift(int input) {
   int rv = ROUND_POWER_OF_TWO(input, DCT_CONST_BITS);
   assert(INT16_MIN <= rv && rv <= INT16_MAX);
@@ -157,32 +155,36 @@
   { fadst4, fadst4 }   // ADST_ADST = 3
 };
 
-void vp9_short_fht4x4_c(const int16_t *input, int16_t *output,
-                        int stride, int tx_type) {
-  int16_t out[4 * 4];
-  int16_t *outptr = &out[0];
-  int i, j;
-  int16_t temp_in[4], temp_out[4];
-  const transform_2d ht = FHT_4[tx_type];
+void vp9_fht4x4_c(const int16_t *input, int16_t *output,
+                  int stride, int tx_type) {
+  if (tx_type == DCT_DCT) {
+    vp9_fdct4x4_c(input, output, stride);
+  } else {
+    int16_t out[4 * 4];
+    int16_t *outptr = &out[0];
+    int i, j;
+    int16_t temp_in[4], temp_out[4];
+    const transform_2d ht = FHT_4[tx_type];
 
-  // Columns
-  for (i = 0; i < 4; ++i) {
-    for (j = 0; j < 4; ++j)
-      temp_in[j] = input[j * stride + i] * 16;
-    if (i == 0 && temp_in[0])
-      temp_in[0] += 1;
-    ht.cols(temp_in, temp_out);
-    for (j = 0; j < 4; ++j)
-      outptr[j * 4 + i] = temp_out[j];
-  }
+    // Columns
+    for (i = 0; i < 4; ++i) {
+      for (j = 0; j < 4; ++j)
+        temp_in[j] = input[j * stride + i] * 16;
+      if (i == 0 && temp_in[0])
+        temp_in[0] += 1;
+      ht.cols(temp_in, temp_out);
+      for (j = 0; j < 4; ++j)
+        outptr[j * 4 + i] = temp_out[j];
+    }
 
-  // Rows
-  for (i = 0; i < 4; ++i) {
-    for (j = 0; j < 4; ++j)
-      temp_in[j] = out[j + i * 4];
-    ht.rows(temp_in, temp_out);
-    for (j = 0; j < 4; ++j)
-      output[j + i * 4] = (temp_out[j] + 1) >> 2;
+    // Rows
+    for (i = 0; i < 4; ++i) {
+      for (j = 0; j < 4; ++j)
+        temp_in[j] = out[j + i * 4];
+      ht.rows(temp_in, temp_out);
+      for (j = 0; j < 4; ++j)
+        output[j + i * 4] = (temp_out[j] + 1) >> 2;
+    }
   }
 }
 
@@ -565,30 +567,34 @@
   { fadst8, fadst8 }   // ADST_ADST = 3
 };
 
-void vp9_short_fht8x8_c(const int16_t *input, int16_t *output,
-                        int stride, int tx_type) {
-  int16_t out[64];
-  int16_t *outptr = &out[0];
-  int i, j;
-  int16_t temp_in[8], temp_out[8];
-  const transform_2d ht = FHT_8[tx_type];
+void vp9_fht8x8_c(const int16_t *input, int16_t *output,
+                  int stride, int tx_type) {
+  if (tx_type == DCT_DCT) {
+    vp9_fdct8x8_c(input, output, stride);
+  } else {
+    int16_t out[64];
+    int16_t *outptr = &out[0];
+    int i, j;
+    int16_t temp_in[8], temp_out[8];
+    const transform_2d ht = FHT_8[tx_type];
 
-  // Columns
-  for (i = 0; i < 8; ++i) {
-    for (j = 0; j < 8; ++j)
-      temp_in[j] = input[j * stride + i] * 4;
-    ht.cols(temp_in, temp_out);
-    for (j = 0; j < 8; ++j)
-      outptr[j * 8 + i] = temp_out[j];
-  }
+    // Columns
+    for (i = 0; i < 8; ++i) {
+      for (j = 0; j < 8; ++j)
+        temp_in[j] = input[j * stride + i] * 4;
+      ht.cols(temp_in, temp_out);
+      for (j = 0; j < 8; ++j)
+        outptr[j * 8 + i] = temp_out[j];
+    }
 
-  // Rows
-  for (i = 0; i < 8; ++i) {
-    for (j = 0; j < 8; ++j)
-      temp_in[j] = out[j + i * 8];
-    ht.rows(temp_in, temp_out);
-    for (j = 0; j < 8; ++j)
-      output[j + i * 8] = (temp_out[j] + (temp_out[j] < 0)) >> 1;
+    // Rows
+    for (i = 0; i < 8; ++i) {
+      for (j = 0; j < 8; ++j)
+        temp_in[j] = out[j + i * 8];
+      ht.rows(temp_in, temp_out);
+      for (j = 0; j < 8; ++j)
+        output[j + i * 8] = (temp_out[j] + (temp_out[j] < 0)) >> 1;
+    }
   }
 }
 
@@ -958,31 +964,34 @@
   { fadst16, fadst16 }   // ADST_ADST = 3
 };
 
-void vp9_short_fht16x16_c(const int16_t *input, int16_t *output,
-                          int stride, int tx_type) {
-  int16_t out[256];
-  int16_t *outptr = &out[0];
-  int i, j;
-  int16_t temp_in[16], temp_out[16];
-  const transform_2d ht = FHT_16[tx_type];
+void vp9_fht16x16_c(const int16_t *input, int16_t *output,
+                    int stride, int tx_type) {
+  if (tx_type == DCT_DCT) {
+    vp9_fdct16x16_c(input, output, stride);
+  } else {
+    int16_t out[256];
+    int16_t *outptr = &out[0];
+    int i, j;
+    int16_t temp_in[16], temp_out[16];
+    const transform_2d ht = FHT_16[tx_type];
 
-  // Columns
-  for (i = 0; i < 16; ++i) {
-    for (j = 0; j < 16; ++j)
-      temp_in[j] = input[j * stride + i] * 4;
-    ht.cols(temp_in, temp_out);
-    for (j = 0; j < 16; ++j)
-      outptr[j * 16 + i] = (temp_out[j] + 1 + (temp_out[j] < 0)) >> 2;
-//      outptr[j * 16 + i] = (temp_out[j] + 1 + (temp_out[j] > 0)) >> 2;
-  }
+    // Columns
+    for (i = 0; i < 16; ++i) {
+      for (j = 0; j < 16; ++j)
+        temp_in[j] = input[j * stride + i] * 4;
+      ht.cols(temp_in, temp_out);
+      for (j = 0; j < 16; ++j)
+        outptr[j * 16 + i] = (temp_out[j] + 1 + (temp_out[j] < 0)) >> 2;
+    }
 
-  // Rows
-  for (i = 0; i < 16; ++i) {
-    for (j = 0; j < 16; ++j)
-      temp_in[j] = out[j + i * 16];
-    ht.rows(temp_in, temp_out);
-    for (j = 0; j < 16; ++j)
-      output[j + i * 16] = temp_out[j];
+    // Rows
+    for (i = 0; i < 16; ++i) {
+      for (j = 0; j < 16; ++j)
+        temp_in[j] = out[j + i * 16];
+      ht.rows(temp_in, temp_out);
+      for (j = 0; j < 16; ++j)
+        output[j + i * 16] = temp_out[j];
+    }
   }
 }
 
@@ -1375,27 +1384,3 @@
       out[j + i * 32] = temp_out[j];
   }
 }
-
-void vp9_fht4x4(TX_TYPE tx_type, const int16_t *input, int16_t *output,
-                int stride) {
-  if (tx_type == DCT_DCT)
-    vp9_fdct4x4(input, output, stride);
-  else
-    vp9_short_fht4x4(input, output, stride, tx_type);
-}
-
-void vp9_fht8x8(TX_TYPE tx_type, const int16_t *input, int16_t *output,
-                int stride) {
-  if (tx_type == DCT_DCT)
-    vp9_fdct8x8(input, output, stride);
-  else
-    vp9_short_fht8x8(input, output, stride, tx_type);
-}
-
-void vp9_fht16x16(TX_TYPE tx_type, const int16_t *input, int16_t *output,
-                  int stride) {
-  if (tx_type == DCT_DCT)
-    vp9_fdct16x16(input, output, stride);
-  else
-    vp9_short_fht16x16(input, output, stride, tx_type);
-}
diff --git a/vp9/encoder/vp9_dct.h b/vp9/encoder/vp9_dct.h
deleted file mode 100644
index cf5f001..0000000
--- a/vp9/encoder/vp9_dct.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-
-#ifndef VP9_ENCODER_VP9_DCT_H_
-#define VP9_ENCODER_VP9_DCT_H_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void vp9_fht4x4(TX_TYPE tx_type, const int16_t *input, int16_t *output,
-                int stride);
-
-void vp9_fht8x8(TX_TYPE tx_type, const int16_t *input, int16_t *output,
-                int stride);
-
-void vp9_fht16x16(TX_TYPE tx_type, const int16_t *input, int16_t *output,
-                  int stride);
-
-#ifdef __cplusplus
-}  // extern "C"
-#endif
-
-#endif  // VP9_ENCODER_VP9_DCT_H_
diff --git a/vp9/encoder/vp9_encodemb.c b/vp9/encoder/vp9_encodemb.c
index 376a899..c7507c1 100644
--- a/vp9/encoder/vp9_encodemb.c
+++ b/vp9/encoder/vp9_encodemb.c
@@ -19,7 +19,6 @@
 #include "vp9/common/vp9_reconintra.h"
 #include "vp9/common/vp9_systemdependent.h"
 
-#include "vp9/encoder/vp9_dct.h"
 #include "vp9/encoder/vp9_encodemb.h"
 #include "vp9/encoder/vp9_quantize.h"
 #include "vp9/encoder/vp9_rdopt.h"
@@ -571,7 +570,7 @@
       if (!x->skip_recode) {
         vp9_subtract_block(16, 16, src_diff, diff_stride,
                            src, p->src.stride, dst, pd->dst.stride);
-        vp9_fht16x16(tx_type, src_diff, coeff, diff_stride);
+        vp9_fht16x16(src_diff, coeff, diff_stride, tx_type);
         vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                        p->quant, p->quant_shift, qcoeff, dqcoeff,
                        pd->dequant, p->zbin_extra, eob, scan_order->scan,
@@ -591,7 +590,7 @@
       if (!x->skip_recode) {
         vp9_subtract_block(8, 8, src_diff, diff_stride,
                            src, p->src.stride, dst, pd->dst.stride);
-        vp9_fht8x8(tx_type, src_diff, coeff, diff_stride);
+        vp9_fht8x8(src_diff, coeff, diff_stride, tx_type);
         vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
                        p->quant_shift, qcoeff, dqcoeff,
                        pd->dequant, p->zbin_extra, eob, scan_order->scan,
@@ -617,7 +616,7 @@
         vp9_subtract_block(4, 4, src_diff, diff_stride,
                            src, p->src.stride, dst, pd->dst.stride);
         if (tx_type != DCT_DCT)
-          vp9_short_fht4x4(src_diff, coeff, diff_stride, tx_type);
+          vp9_fht4x4(src_diff, coeff, diff_stride, tx_type);
         else
           x->fwd_txm4x4(src_diff, coeff, diff_stride);
         vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
diff --git a/vp9/encoder/vp9_firstpass.h b/vp9/encoder/vp9_firstpass.h
index 19b5981..054ecf8 100644
--- a/vp9/encoder/vp9_firstpass.h
+++ b/vp9/encoder/vp9_firstpass.h
@@ -10,20 +10,84 @@
 
 #ifndef VP9_ENCODER_VP9_FIRSTPASS_H_
 #define VP9_ENCODER_VP9_FIRSTPASS_H_
-#include "vp9/encoder/vp9_onyx_int.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-void vp9_init_first_pass(VP9_COMP *cpi);
-void vp9_rc_get_first_pass_params(VP9_COMP *cpi);
-void vp9_first_pass(VP9_COMP *cpi);
-void vp9_end_first_pass(VP9_COMP *cpi);
+typedef struct {
+  double frame;
+  double intra_error;
+  double coded_error;
+  double sr_coded_error;
+  double ssim_weighted_pred_err;
+  double pcnt_inter;
+  double pcnt_motion;
+  double pcnt_second_ref;
+  double pcnt_neutral;
+  double MVr;
+  double mvr_abs;
+  double MVc;
+  double mvc_abs;
+  double MVrv;
+  double MVcv;
+  double mv_in_out_count;
+  double new_mv_count;
+  double duration;
+  double count;
+} FIRSTPASS_STATS;
 
-void vp9_init_second_pass(VP9_COMP *cpi);
-void vp9_rc_get_second_pass_params(VP9_COMP *cpi);
-void vp9_end_second_pass(VP9_COMP *cpi);
+struct twopass_rc {
+  unsigned int section_intra_rating;
+  unsigned int next_iiratio;
+  unsigned int this_iiratio;
+  FIRSTPASS_STATS total_stats;
+  FIRSTPASS_STATS this_frame_stats;
+  FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
+  FIRSTPASS_STATS total_left_stats;
+  int first_pass_done;
+  int64_t bits_left;
+  int64_t clip_bits_total;
+  double avg_iiratio;
+  double modified_error_min;
+  double modified_error_max;
+  double modified_error_total;
+  double modified_error_left;
+  double kf_intra_err_min;
+  double gf_intra_err_min;
+  int static_scene_max_gf_interval;
+  int kf_bits;
+  // Remaining error from uncoded frames in a gf group. Two pass use only
+  int64_t gf_group_error_left;
+
+  // Projected total bits available for a key frame group of frames
+  int64_t kf_group_bits;
+
+  // Error score of frames still to be coded in kf group
+  int64_t kf_group_error_left;
+
+  // Projected Bits available for a group of frames including 1 GF or ARF
+  int64_t gf_group_bits;
+  // Bits for the golden frame or ARF - 2 pass only
+  int gf_bits;
+  int alt_extra_bits;
+
+  int sr_update_lag;
+
+  int kf_zeromotion_pct;
+  int gf_zeromotion_pct;
+};
+
+struct VP9_COMP;
+
+void vp9_init_first_pass(struct VP9_COMP *cpi);
+void vp9_rc_get_first_pass_params(struct VP9_COMP *cpi);
+void vp9_first_pass(struct VP9_COMP *cpi);
+void vp9_end_first_pass(struct VP9_COMP *cpi);
+
+void vp9_init_second_pass(struct VP9_COMP *cpi);
+void vp9_rc_get_second_pass_params(struct VP9_COMP *cpi);
+void vp9_end_second_pass(struct VP9_COMP *cpi);
 
 // Post encode update of the rate control parameters for 2-pass
 void vp9_twopass_postencode_update(struct VP9_COMP *cpi,
diff --git a/vp9/encoder/vp9_lookahead.c b/vp9/encoder/vp9_lookahead.c
index e6e59c0..4b642e2 100644
--- a/vp9/encoder/vp9_lookahead.c
+++ b/vp9/encoder/vp9_lookahead.c
@@ -11,9 +11,12 @@
 #include <stdlib.h>
 
 #include "./vpx_config.h"
+
 #include "vp9/common/vp9_common.h"
+
 #include "vp9/encoder/vp9_extend.h"
 #include "vp9/encoder/vp9_lookahead.h"
+#include "vp9/encoder/vp9_onyx_int.h"
 
 struct lookahead_ctx {
   unsigned int max_sz;         /* Absolute size of the queue */
diff --git a/vp9/encoder/vp9_mcomp.c b/vp9/encoder/vp9_mcomp.c
index 36591bd..198e11c 100644
--- a/vp9/encoder/vp9_mcomp.c
+++ b/vp9/encoder/vp9_mcomp.c
@@ -475,11 +475,9 @@
          ((col + range) <= x->mv_col_max);
 }
 
-static INLINE int check_point(const MACROBLOCK *x, const MV *mv) {
-  return (mv->col < x->mv_col_min) |
-         (mv->col > x->mv_col_max) |
-         (mv->row < x->mv_row_min) |
-         (mv->row > x->mv_row_max);
+static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
+  return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
+         (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
 }
 
 #define CHECK_BETTER \
@@ -572,7 +570,7 @@
         for (i = 0; i < num_candidates[t]; i++) {
           this_mv.row = br + candidates[t][i].row;
           this_mv.col = bc + candidates[t][i].col;
-          if (check_point(x, &this_mv))
+          if (!is_mv_in(x, &this_mv))
             continue;
           this_offset = base_offset + (this_mv.row * in_what_stride) +
                                        this_mv.col;
@@ -616,7 +614,7 @@
           for (i = 0; i < num_candidates[s]; i++) {
             this_mv.row = br + candidates[s][i].row;
             this_mv.col = bc + candidates[s][i].col;
-            if (check_point(x, &this_mv))
+            if (!is_mv_in(x, &this_mv))
               continue;
             this_offset = base_offset + (this_mv.row * in_what_stride) +
                                          this_mv.col;
@@ -656,7 +654,7 @@
           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
             this_mv.row = br + candidates[s][next_chkpts_indices[i]].row;
             this_mv.col = bc + candidates[s][next_chkpts_indices[i]].col;
-            if (check_point(x, &this_mv))
+            if (!is_mv_in(x, &this_mv))
               continue;
             this_offset = base_offset + (this_mv.row * (in_what_stride)) +
                                          this_mv.col;
@@ -695,7 +693,7 @@
         for (i = 0; i < 4; i++) {
           this_mv.row = br + neighbors[i].row;
           this_mv.col = bc + neighbors[i].col;
-          if (check_point(x, &this_mv))
+          if (!is_mv_in(x, &this_mv))
             continue;
           this_offset = base_offset + this_mv.row * in_what_stride +
                             this_mv.col;
@@ -1685,10 +1683,7 @@
       this_mv.row = ref_mv->row + neighbors[j].row;
       this_mv.col = ref_mv->col + neighbors[j].col;
 
-      if ((this_mv.col > x->mv_col_min) &&
-          (this_mv.col < x->mv_col_max) &&
-          (this_mv.row > x->mv_row_min) &&
-          (this_mv.row < x->mv_row_max)) {
+      if (is_mv_in(x, &this_mv)) {
         const uint8_t *check_here = &in_what[this_mv.row * in_what_stride +
                                                 this_mv.col];
         thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
@@ -1875,10 +1870,7 @@
       this_mv.row = ref_mv->row + neighbors[j].row;
       this_mv.col = ref_mv->col + neighbors[j].col;
 
-      if ((this_mv.col > x->mv_col_min) &&
-          (this_mv.col < x->mv_col_max) &&
-          (this_mv.row > x->mv_row_min) &&
-          (this_mv.row < x->mv_row_max)) {
+      if (is_mv_in(x, &this_mv)) {
         const uint8_t *check_here = &in_what[this_mv.row * in_what_stride +
                                                 this_mv.col];
 
diff --git a/vp9/encoder/vp9_onyx_if.c b/vp9/encoder/vp9_onyx_if.c
index b4972be..2497b84 100644
--- a/vp9/encoder/vp9_onyx_if.c
+++ b/vp9/encoder/vp9_onyx_if.c
@@ -572,7 +572,7 @@
                                    int speed) {
   int i;
   sf->adaptive_rd_thresh = 1;
-  sf->recode_loop = (speed < 1);
+  sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
   if (speed == 1) {
     sf->use_square_partition_only = !frame_is_intra_only(cm);
     sf->less_rectangular_check  = 1;
@@ -590,7 +590,7 @@
     sf->adaptive_pred_interp_filter = 1;
     sf->auto_mv_step_size = 1;
     sf->adaptive_rd_thresh = 2;
-    sf->recode_loop = 2;
+    sf->recode_loop = ALLOW_RECODE_KFARFGF;
     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
@@ -626,7 +626,7 @@
     sf->last_partitioning_redo_frequency = 3;
 
     sf->adaptive_rd_thresh = 2;
-    sf->recode_loop = 2;
+    sf->recode_loop = ALLOW_RECODE_KFARFGF;
     sf->use_lp32x32fdct = 1;
     sf->mode_skip_start = 11;
     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
@@ -743,7 +743,7 @@
                                  int speed) {
   sf->static_segmentation = 0;
   sf->adaptive_rd_thresh = 1;
-  sf->recode_loop = (speed < 1);
+  sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
   if (speed == 1) {
     sf->use_square_partition_only = !frame_is_intra_only(cm);
     sf->less_rectangular_check = 1;
@@ -761,7 +761,7 @@
     sf->adaptive_pred_interp_filter = 1;
     sf->auto_mv_step_size = 1;
     sf->adaptive_rd_thresh = 2;
-    sf->recode_loop = 2;
+    sf->recode_loop = ALLOW_RECODE_KFARFGF;
     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
@@ -797,7 +797,7 @@
     sf->last_partitioning_redo_frequency = 3;
 
     sf->adaptive_rd_thresh = 2;
-    sf->recode_loop = 2;
+    sf->recode_loop = ALLOW_RECODE_KFARFGF;
     sf->use_lp32x32fdct = 1;
     sf->mode_skip_start = 11;
     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
@@ -865,7 +865,7 @@
   // best quality defaults
   sf->RD = 1;
   sf->search_method = NSTEP;
-  sf->recode_loop = 1;
+  sf->recode_loop = ALLOW_RECODE;
   sf->subpel_search_method = SUBPEL_TREE;
   sf->subpel_iters_per_step = 2;
   sf->subpel_force_stop = 0;
@@ -933,7 +933,7 @@
 
   // No recode for 1 pass.
   if (cpi->pass == 0) {
-    sf->recode_loop = 0;
+    sf->recode_loop = DISALLOW_RECODE;
     sf->optimize_coefficients = 0;
   }
 
@@ -1144,6 +1144,109 @@
   return (llval * llnum / llden);
 }
 
+// Initialize layer context data from init_config().
+static void init_layer_context(VP9_COMP *const cpi) {
+  const VP9_CONFIG *const oxcf = &cpi->oxcf;
+  int temporal_layer = 0;
+  cpi->svc.spatial_layer_id = 0;
+  cpi->svc.temporal_layer_id = 0;
+  for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
+      ++temporal_layer) {
+    LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
+    RATE_CONTROL *const lrc = &lc->rc;
+    lrc->active_worst_quality = q_trans[oxcf->worst_allowed_q];
+    lrc->avg_frame_qindex[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
+    lrc->last_q[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
+    lrc->ni_av_qi = lrc->active_worst_quality;
+    lrc->total_actual_bits = 0;
+    lrc->total_target_vs_actual = 0;
+    lrc->ni_tot_qi = 0;
+    lrc->tot_q = 0.0;
+    lrc->ni_frames = 0;
+    lrc->rate_correction_factor = 1.0;
+    lrc->key_frame_rate_correction_factor = 1.0;
+    lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] *
+        1000;
+    lrc->buffer_level = rescale((int)(oxcf->starting_buffer_level),
+                               lc->target_bandwidth, 1000);
+    lrc->bits_off_target = lrc->buffer_level;
+  }
+}
+
+// Update the layer context from a change_config() call.
+static void update_layer_context_change_config(VP9_COMP *const cpi,
+                                               const int target_bandwidth) {
+  const VP9_CONFIG *const oxcf = &cpi->oxcf;
+  const RATE_CONTROL *const rc = &cpi->rc;
+  int temporal_layer = 0;
+  float bitrate_alloc = 1.0;
+  for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
+      ++temporal_layer) {
+    LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
+    RATE_CONTROL *const lrc = &lc->rc;
+    lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] * 1000;
+    bitrate_alloc = (float)lc->target_bandwidth / (float)target_bandwidth;
+    // Update buffer-related quantities.
+    lc->starting_buffer_level = oxcf->starting_buffer_level * bitrate_alloc;
+    lc->optimal_buffer_level = oxcf->optimal_buffer_level * bitrate_alloc;
+    lc->maximum_buffer_size = oxcf->maximum_buffer_size * bitrate_alloc;
+    lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
+    lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size);
+    // Update framerate-related quantities.
+    lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
+    lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
+    lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
+    // Update qp-related quantities.
+    lrc->worst_quality = rc->worst_quality;
+    lrc->best_quality = rc->best_quality;
+    lrc->active_worst_quality = rc->active_worst_quality;
+  }
+}
+
+// Prior to encoding the frame, update framerate-related quantities
+// for the current layer.
+static void update_layer_framerate(VP9_COMP *const cpi) {
+  int temporal_layer = cpi->svc.temporal_layer_id;
+  LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
+  RATE_CONTROL *const lrc = &lc->rc;
+  lc->framerate = cpi->oxcf.framerate /
+      cpi->oxcf.ts_rate_decimator[temporal_layer];
+  lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth /
+      lc->framerate);
+  lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
+}
+
+// Prior to encoding the frame, set the layer context, for the current layer
+// to be encoded, to the cpi struct.
+static void restore_layer_context(VP9_COMP *const cpi) {
+  int temporal_layer = cpi->svc.temporal_layer_id;
+  LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
+  int frame_since_key = cpi->rc.frames_since_key;
+  int frame_to_key = cpi->rc.frames_to_key;
+  cpi->rc = lc->rc;
+  cpi->oxcf.target_bandwidth = lc->target_bandwidth;
+  cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
+  cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
+  cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
+  cpi->output_framerate = lc->framerate;
+  // Reset the frames_since_key and frames_to_key counters to their values
+  // before the layer restore. Keep these defined for the stream (not layer).
+  cpi->rc.frames_since_key = frame_since_key;
+  cpi->rc.frames_to_key = frame_to_key;
+}
+
+// Save the layer context after encoding the frame.
+static void save_layer_context(VP9_COMP *const cpi) {
+  int temporal_layer = cpi->svc.temporal_layer_id;
+  LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
+  lc->rc = cpi->rc;
+  lc->target_bandwidth = cpi->oxcf.target_bandwidth;
+  lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
+  lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
+  lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
+  lc->framerate = cpi->output_framerate;
+}
+
 static void set_tile_limits(VP9_COMP *cpi) {
   VP9_COMMON *const cm = &cpi->common;
 
@@ -1170,6 +1273,16 @@
   cm->subsampling_y = 0;
   vp9_alloc_compressor_data(cpi);
 
+  // Spatial scalability.
+  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
+  // Temporal scalability.
+  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
+
+  if (cpi->svc.number_temporal_layers > 1 &&
+      cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+    init_layer_context(cpi);
+  }
+
   // change includes all joint functionality
   vp9_change_config(ptr, oxcf);
 
@@ -1210,9 +1323,6 @@
   cpi->gld_fb_idx = 1;
   cpi->alt_fb_idx = 2;
 
-  cpi->current_layer = 0;
-  cpi->use_svc = 0;
-
   set_tile_limits(cpi);
 
   cpi->fixed_divide[0] = 0;
@@ -1220,7 +1330,6 @@
     cpi->fixed_divide[i] = 0x80000 / i;
 }
 
-
 void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   VP9_COMMON *const cm = &cpi->common;
@@ -1312,10 +1421,10 @@
                                             cpi->oxcf.target_bandwidth, 1000);
   // Under a configuration change, where maximum_buffer_size may change,
   // keep buffer level clipped to the maximum allowed buffer size.
-  if (cpi->rc.bits_off_target > cpi->oxcf.maximum_buffer_size) {
-    cpi->rc.bits_off_target = cpi->oxcf.maximum_buffer_size;
-    cpi->rc.buffer_level = cpi->rc.bits_off_target;
-  }
+  cpi->rc.bits_off_target = MIN(cpi->rc.bits_off_target,
+                                cpi->oxcf.maximum_buffer_size);
+  cpi->rc.buffer_level = MIN(cpi->rc.buffer_level,
+                             cpi->oxcf.maximum_buffer_size);
 
   // Set up frame rate and related parameters rate control values.
   vp9_new_framerate(cpi, cpi->oxcf.framerate);
@@ -1350,6 +1459,11 @@
   }
   update_frame_size(cpi);
 
+  if (cpi->svc.number_temporal_layers > 1 &&
+      cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+    update_layer_context_change_config(cpi, cpi->oxcf.target_bandwidth);
+  }
+
   cpi->speed = cpi->oxcf.cpu_used;
 
   if (cpi->oxcf.lag_in_frames == 0) {
@@ -1573,6 +1687,8 @@
 
   vp9_create_common(cm);
 
+  cpi->use_svc = 0;
+
   init_config((VP9_PTR)cpi, oxcf);
 
   init_pick_mode_context(cpi);
@@ -1588,9 +1704,6 @@
   cpi->alt_is_last  = 0;
   cpi->gold_is_alt  = 0;
 
-  // Spatial scalability
-  cpi->number_spatial_layers = oxcf->ss_number_layers;
-
   // Create the encoder segmentation map and set all entries to 0
   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
@@ -1616,11 +1729,6 @@
                                sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
   }
 
-#ifdef ENTROPY_STATS
-  if (cpi->pass != 1)
-    init_context_counters();
-#endif
-
   /*Initialize the feed-forward activity masking.*/
   cpi->activity_avg = 90 << 12;
   cpi->key_frame_frequency = cpi->oxcf.key_freq;
@@ -2431,8 +2539,8 @@
   // Is frame recode allowed.
   // Yes if either recode mode 1 is selected or mode 2 is selected
   // and the frame is a key frame, golden frame or alt_ref_frame
-  } else if ((cpi->sf.recode_loop == 1) ||
-             ((cpi->sf.recode_loop == 2) &&
+  } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
+             ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
               (cm->frame_type == KEY_FRAME ||
                cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
     // General over and under shoot tests
@@ -2651,20 +2759,62 @@
 }
 #endif
 
+static void encode_without_recode_loop(VP9_COMP *cpi,
+                                       size_t *size,
+                                       uint8_t *dest,
+                                       int *q) {
+  VP9_COMMON *const cm = &cpi->common;
+  vp9_clear_system_state();  // __asm emms;
+  vp9_set_quantizer(cpi, *q);
+
+  // Set up entropy context depending on frame type. The decoder mandates
+  // the use of the default context, index 0, for keyframes and inter
+  // frames where the error_resilient_mode or intra_only flag is set. For
+  // other inter-frames the encoder currently uses only two contexts;
+  // context 1 for ALTREF frames and context 0 for the others.
+  if (cm->frame_type == KEY_FRAME) {
+    vp9_setup_key_frame(cpi);
+  } else {
+    if (!cm->intra_only && !cm->error_resilient_mode) {
+      cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
+    }
+    vp9_setup_inter_frame(cpi);
+  }
+  // Variance adaptive and in frame q adjustment experiments are mutually
+  // exclusive.
+  if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
+    vp9_vaq_frame_setup(cpi);
+  } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
+    setup_in_frame_q_adj(cpi);
+  }
+  // transform / motion compensation build reconstruction frame
+  vp9_encode_frame(cpi);
+
+  // Update the skip mb flag probabilities based on the distribution
+  // seen in the last encoder iteration.
+  // update_base_skip_probs(cpi);
+  vp9_clear_system_state();  // __asm emms;
+}
+
 static void encode_with_recode_loop(VP9_COMP *cpi,
                                     size_t *size,
                                     uint8_t *dest,
                                     int *q,
                                     int bottom_index,
-                                    int top_index,
-                                    int frame_over_shoot_limit,
-                                    int frame_under_shoot_limit) {
+                                    int top_index) {
   VP9_COMMON *const cm = &cpi->common;
   int loop_count = 0;
   int loop = 0;
   int overshoot_seen = 0;
   int undershoot_seen = 0;
   int q_low = bottom_index, q_high = top_index;
+  int frame_over_shoot_limit;
+  int frame_under_shoot_limit;
+
+  // Decide frame size bounds
+  vp9_rc_compute_frame_size_bounds(cpi, cpi->rc.this_frame_target,
+                                   &frame_under_shoot_limit,
+                                   &frame_over_shoot_limit);
 
   do {
     vp9_clear_system_state();  // __asm emms;
@@ -2696,7 +2846,6 @@
     }
 
     // transform / motion compensation build reconstruction frame
-
     vp9_encode_frame(cpi);
 
     // Update the skip mb flag probabilities based on the distribution
@@ -2708,7 +2857,7 @@
     // Dummy pack of the bitstream using up to date stats to get an
     // accurate estimate of output frame size to determine if we need
     // to recode.
-    if (cpi->sf.recode_loop != 0) {
+    if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
       vp9_save_coding_context(cpi);
       cpi->dummy_packing = 1;
       if (!cpi->sf.super_fast_rtc)
@@ -2911,8 +3060,6 @@
   VP9_COMMON *const cm = &cpi->common;
   TX_SIZE t;
   int q;
-  int frame_over_shoot_limit;
-  int frame_under_shoot_limit;
   int top_index;
   int bottom_index;
 
@@ -3008,7 +3155,7 @@
       cm->frame_type != KEY_FRAME) {
     if (vp9_rc_drop_frame(cpi)) {
       vp9_rc_postencode_update_drop_frame(cpi);
-      cm->current_video_frame++;
+      ++cm->current_video_frame;
       return;
     }
   }
@@ -3046,35 +3193,22 @@
   vp9_write_yuv_frame(cpi->Source);
 #endif
 
-  // Decide frame size bounds
-  vp9_rc_compute_frame_size_bounds(cpi, cpi->rc.this_frame_target,
-                                   &frame_under_shoot_limit,
-                                   &frame_over_shoot_limit);
-
   // Decide q and q bounds.
   q = vp9_rc_pick_q_and_adjust_q_bounds(cpi,
                                         &bottom_index,
                                         &top_index);
 
-  // JBB : This is realtime mode.  In real time mode the first frame
-  // should be larger. Q of 0 is disabled because we force tx size to be
-  // 16x16...
-  if (cpi->sf.super_fast_rtc) {
-    if (cm->current_video_frame == 0)
-      q /= 3;
-
-    if (q == 0)
-      q++;
-  }
-
   if (!frame_is_intra_only(cm)) {
     cm->interp_filter = DEFAULT_INTERP_FILTER;
     /* TODO: Decide this more intelligently */
     set_high_precision_mv(cpi, (q < HIGH_PRECISION_MV_QTHRESH));
   }
 
-  encode_with_recode_loop(cpi, size, dest, &q, bottom_index, top_index,
-                          frame_over_shoot_limit, frame_under_shoot_limit);
+  if (cpi->sf.recode_loop == DISALLOW_RECODE) {
+    encode_without_recode_loop(cpi, size, dest, &q);
+  } else {
+    encode_with_recode_loop(cpi, size, dest, &q, bottom_index, top_index);
+  }
 
   // Special case code to reduce pulsing when key frames are forced at a
   // fixed interval. Note the reconstruction error if it is the frame before
@@ -3133,10 +3267,6 @@
     }
   }
 
-#ifdef ENTROPY_STATS
-  vp9_update_mode_context_stats(cpi);
-#endif
-
 #if 0
   output_frame_level_debug_stats(cpi);
 #endif
@@ -3504,6 +3634,12 @@
     adjust_frame_rate(cpi);
   }
 
+  if (cpi->svc.number_temporal_layers > 1 &&
+      cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+    update_layer_framerate(cpi);
+    restore_layer_context(cpi);
+  }
+
   // start with a 0 size frame
   *size = 0;
 
@@ -3579,6 +3715,12 @@
     cpi->droppable = !frame_is_reference(cpi);
   }
 
+  // Save layer specific state.
+  if (cpi->svc.number_temporal_layers > 1 &&
+      cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+    save_layer_context(cpi);
+  }
+
   vpx_usec_timer_mark(&cmptimer);
   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
 
diff --git a/vp9/encoder/vp9_onyx_int.h b/vp9/encoder/vp9_onyx_int.h
index d0ca962..5fd8e5a 100644
--- a/vp9/encoder/vp9_onyx_int.h
+++ b/vp9/encoder/vp9_onyx_int.h
@@ -23,6 +23,7 @@
 #include "vp9/common/vp9_onyxc_int.h"
 
 #include "vp9/encoder/vp9_encodemb.h"
+#include "vp9/encoder/vp9_firstpass.h"
 #include "vp9/encoder/vp9_lookahead.h"
 #include "vp9/encoder/vp9_mbgraph.h"
 #include "vp9/encoder/vp9_mcomp.h"
@@ -44,8 +45,9 @@
 #else
 #define MIN_GF_INTERVAL             4
 #endif
-#define DEFAULT_GF_INTERVAL         7
+#define DEFAULT_GF_INTERVAL         11
 #define DEFAULT_KF_BOOST            2000
+#define DEFAULT_GF_BOOST            2000
 
 #define KEY_FRAME_CONTEXT 5
 
@@ -78,28 +80,6 @@
   FRAME_CONTEXT fc;
 } CODING_CONTEXT;
 
-typedef struct {
-  double frame;
-  double intra_error;
-  double coded_error;
-  double sr_coded_error;
-  double ssim_weighted_pred_err;
-  double pcnt_inter;
-  double pcnt_motion;
-  double pcnt_second_ref;
-  double pcnt_neutral;
-  double MVr;
-  double mvr_abs;
-  double MVc;
-  double mvc_abs;
-  double MVrv;
-  double MVcv;
-  double mv_in_out_count;
-  double new_mv_count;
-  double duration;
-  double count;
-} FIRSTPASS_STATS;
-
 // This enumerator type needs to be kept aligned with the mode order in
 // const MODE_DEFINITION vp9_mode_order[MAX_MODES] used in the rd code.
 typedef enum {
@@ -217,6 +197,17 @@
   LAST_FRAME_PARTITION_ALL = 2
 } LAST_FRAME_PARTITION_METHOD;
 
+typedef enum {
+  // No recode.
+  DISALLOW_RECODE = 0,
+  // Allow recode for KF and exceeding maximum frame bandwidth.
+  ALLOW_RECODE_KFMAXBW = 1,
+  // Allow recode only for KF/ARF/GF frames.
+  ALLOW_RECODE_KFARFGF = 2,
+  // Allow recode for all frames based on bitrate constraints.
+  ALLOW_RECODE = 3,
+} RECODE_LOOP_TYPE;
+
 typedef struct {
   // This flag refers to whether or not to perform rd optimization.
   int RD;
@@ -224,11 +215,7 @@
   // Motion search method (Diamond, NSTEP, Hex, Big Diamond, Square, etc).
   SEARCH_METHODS search_method;
 
-  // Recode_loop can be:
-  // 0 means we only encode a frame once
-  // 1 means we can re-encode based on bitrate constraints on any frame
-  // 2 means we can only recode gold, alt, and key frames.
-  int recode_loop;
+  RECODE_LOOP_TYPE recode_loop;
 
   // Subpel_search_method can only be subpel_tree which does a subpixel
   // logarithmic search that keeps stepping at 1/2 pixel units until
@@ -407,6 +394,15 @@
   int super_fast_rtc;
 } SPEED_FEATURES;
 
+typedef struct {
+  RATE_CONTROL rc;
+  int target_bandwidth;
+  int64_t starting_buffer_level;
+  int64_t optimal_buffer_level;
+  int64_t maximum_buffer_size;
+  double framerate;
+} LAYER_CONTEXT;
+
 typedef struct VP9_COMP {
   DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]);
   DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]);
@@ -451,9 +447,6 @@
   int gld_fb_idx;
   int alt_fb_idx;
 
-  int current_layer;
-  int use_svc;
-
 #if CONFIG_MULTIPLE_ARF
   int alt_ref_fb_idx[REF_FRAMES - 3];
 #endif
@@ -573,46 +566,7 @@
   uint64_t time_pick_lpf;
   uint64_t time_encode_sb_row;
 
-  struct twopass_rc {
-    unsigned int section_intra_rating;
-    unsigned int next_iiratio;
-    unsigned int this_iiratio;
-    FIRSTPASS_STATS total_stats;
-    FIRSTPASS_STATS this_frame_stats;
-    FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
-    FIRSTPASS_STATS total_left_stats;
-    int first_pass_done;
-    int64_t bits_left;
-    int64_t clip_bits_total;
-    double avg_iiratio;
-    double modified_error_min;
-    double modified_error_max;
-    double modified_error_total;
-    double modified_error_left;
-    double kf_intra_err_min;
-    double gf_intra_err_min;
-    int static_scene_max_gf_interval;
-    int kf_bits;
-    // Remaining error from uncoded frames in a gf group. Two pass use only
-    int64_t gf_group_error_left;
-
-    // Projected total bits available for a key frame group of frames
-    int64_t kf_group_bits;
-
-    // Error score of frames still to be coded in kf group
-    int64_t kf_group_error_left;
-
-    // Projected Bits available for a group of frames including 1 GF or ARF
-    int64_t gf_group_bits;
-    // Bits for the golden frame or ARF - 2 pass only
-    int gf_bits;
-    int alt_extra_bits;
-
-    int sr_update_lag;
-
-    int kf_zeromotion_pct;
-    int gf_zeromotion_pct;
-  } twopass;
+  struct twopass_rc twopass;
 
   YV12_BUFFER_CONFIG alt_ref_buffer;
   YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
@@ -669,7 +623,18 @@
   int initial_width;
   int initial_height;
 
-  int number_spatial_layers;
+  int use_svc;
+
+  struct svc {
+    int spatial_layer_id;
+    int temporal_layer_id;
+    int number_spatial_layers;
+    int number_temporal_layers;
+    // Layer context used for rate control in CBR mode, only defined for
+    // temporal layers for now.
+    LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
+  } svc;
+
   int enable_encode_breakout;   // Default value is 1. From first pass stats,
                                 // encode_breakout may be disabled.
 
diff --git a/vp9/encoder/vp9_ratectrl.c b/vp9/encoder/vp9_ratectrl.c
index abbf39b..04539c8 100644
--- a/vp9/encoder/vp9_ratectrl.c
+++ b/vp9/encoder/vp9_ratectrl.c
@@ -220,7 +220,7 @@
     // bits on this frame even if it is a constructed arf.
     // The active maximum quantizer insures that an appropriate
     // number of bits will be spent if needed for constructed ARFs.
-    target = 0;
+    target = min_frame_target;
   }
   // Clip the frame target to the maximum allowed value.
   if (target > rc->max_frame_bandwidth)
@@ -241,6 +241,26 @@
   return target;
 }
 
+
+// Update the buffer level for higher layers, given the encoded current layer.
+static void update_layer_buffer_level(VP9_COMP *const cpi,
+                                      int encoded_frame_size) {
+  int temporal_layer = 0;
+  int current_temporal_layer = cpi->svc.temporal_layer_id;
+  for (temporal_layer = current_temporal_layer + 1;
+      temporal_layer < cpi->svc.number_temporal_layers; ++temporal_layer) {
+    LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
+    RATE_CONTROL *lrc = &lc->rc;
+    int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
+        encoded_frame_size);
+    lrc->bits_off_target += bits_off_for_this_layer;
+
+    // Clip buffer level to maximum buffer size for the layer.
+    lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
+    lrc->buffer_level = lrc->bits_off_target;
+  }
+}
+
 // Update the buffer level: leaky bucket model.
 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
   const VP9_COMMON *const cm = &cpi->common;
@@ -255,14 +275,18 @@
   }
 
   // Clip the buffer level to the maximum specified buffer size.
-  rc->buffer_level = MIN(rc->bits_off_target, oxcf->maximum_buffer_size);
+  rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size);
+  rc->buffer_level = rc->bits_off_target;
+
+  if (cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+    update_layer_buffer_level(cpi, encoded_frame_size);
+  }
 }
 
 int vp9_rc_drop_frame(VP9_COMP *cpi) {
   const VP9_CONFIG *oxcf = &cpi->oxcf;
   RATE_CONTROL *const rc = &cpi->rc;
 
-
   if (!oxcf->drop_frames_water_mark) {
     return 0;
   } else {
@@ -273,7 +297,7 @@
       // If buffer is below drop_mark, for now just drop every other frame
       // (starting with the next frame) until it increases back over drop_mark.
       int drop_mark = (int)(oxcf->drop_frames_water_mark *
-                                oxcf->optimal_buffer_level / 100);
+          oxcf->optimal_buffer_level / 100);
       if ((rc->buffer_level > drop_mark) &&
           (rc->decimation_factor > 0)) {
         --rc->decimation_factor;
@@ -301,7 +325,8 @@
   if (cpi->common.frame_type == KEY_FRAME) {
     return cpi->rc.key_frame_rate_correction_factor;
   } else {
-    if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)
+    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
+        !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
       return cpi->rc.gf_rate_correction_factor;
     else
       return cpi->rc.rate_correction_factor;
@@ -312,7 +337,8 @@
   if (cpi->common.frame_type == KEY_FRAME) {
     cpi->rc.key_frame_rate_correction_factor = factor;
   } else {
-    if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)
+    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
+        !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
       cpi->rc.gf_rate_correction_factor = factor;
     else
       cpi->rc.rate_correction_factor = factor;
@@ -538,7 +564,11 @@
     if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) {
       active_best_quality = cpi->cq_target_quality;
     } else {
-      active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
+      // Use the lower of active_worst_quality and recent/average Q.
+      if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
+       active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
+      else
+        active_best_quality = inter_minq[active_worst_quality];
       // For the constrained quality mode we don't want
       // q to fall below the cq level.
       if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) &&
@@ -574,7 +604,6 @@
     *top_index = (active_worst_quality + active_best_quality) / 2;
   }
 #endif
-
   if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) {
     q = active_best_quality;
   // Special case code to try and match quality with forced key frames
@@ -810,12 +839,28 @@
 int vp9_rc_pick_q_and_adjust_q_bounds(const VP9_COMP *cpi,
                                       int *bottom_index,
                                       int *top_index) {
+  int q;
   if (cpi->pass == 0)
-    return rc_pick_q_and_adjust_q_bounds_one_pass(
+    q = rc_pick_q_and_adjust_q_bounds_one_pass(
         cpi, bottom_index, top_index);
   else
-    return rc_pick_q_and_adjust_q_bounds_two_pass(
+    q = rc_pick_q_and_adjust_q_bounds_two_pass(
         cpi, bottom_index, top_index);
+
+  // JBB : This is realtime mode.  In real time mode the first frame
+  // should be larger. Q of 0 is disabled because we force tx size to be
+  // 16x16...
+  if (cpi->sf.super_fast_rtc) {
+    if (cpi->common.current_video_frame == 0)
+      q /= 3;
+    if (q == 0)
+      q++;
+    if (q < *bottom_index)
+      *bottom_index = q;
+    else if (q > *top_index)
+      *top_index = q;
+  }
+  return q;
 }
 
 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
@@ -918,7 +963,8 @@
   rc->projected_frame_size = (bytes_used << 3);
 
   // Post encode loop adjustment of Q prediction.
-  vp9_rc_update_rate_correction_factors(cpi, (cpi->sf.recode_loop ||
+  vp9_rc_update_rate_correction_factors(
+      cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF ||
             cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0);
 
   // Keep a record of last Q and ambient average Q.
@@ -1002,21 +1048,6 @@
   cpi->rc.frames_to_key--;
 }
 
-void vp9_rc_get_svc_params(VP9_COMP *cpi) {
-  VP9_COMMON *const cm = &cpi->common;
-  if ((cm->current_video_frame == 0) ||
-      (cm->frame_flags & FRAMEFLAGS_KEY) ||
-      (cpi->oxcf.auto_key && (cpi->rc.frames_since_key %
-                              cpi->key_frame_frequency == 0))) {
-    cm->frame_type = KEY_FRAME;
-    cpi->rc.source_alt_ref_active = 0;
-  } else {
-    cm->frame_type = INTER_FRAME;
-  }
-  cpi->rc.frames_till_gf_update_due = INT_MAX;
-  cpi->rc.baseline_gf_interval = INT_MAX;
-}
-
 static int test_for_kf_one_pass(VP9_COMP *cpi) {
   // Placeholder function for auto key frame
   return 0;
@@ -1025,61 +1056,70 @@
 #define USE_ALTREF_FOR_ONE_PASS   1
 
 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
+  static const int af_ratio = 5;
   const RATE_CONTROL *rc = &cpi->rc;
-  int target = rc->av_per_frame_bandwidth;
-  target = vp9_rc_clamp_pframe_target_size(cpi, target);
-  return target;
+  int target;
+#if USE_ALTREF_FOR_ONE_PASS
+  target = (!rc->is_src_frame_alt_ref &&
+            (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ?
+      (rc->av_per_frame_bandwidth * cpi->rc.baseline_gf_interval * af_ratio) /
+      (cpi->rc.baseline_gf_interval + af_ratio - 1) :
+      (rc->av_per_frame_bandwidth * cpi->rc.baseline_gf_interval) /
+      (cpi->rc.baseline_gf_interval + af_ratio - 1);
+#else
+  target = rc->av_per_frame_bandwidth;
+#endif
+  return vp9_rc_clamp_pframe_target_size(cpi, target);
 }
 
 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
+  static const int kf_ratio = 12;
   const RATE_CONTROL *rc = &cpi->rc;
-  int target = rc->av_per_frame_bandwidth * 8;
-  target = vp9_rc_clamp_iframe_target_size(cpi, target);
-  return target;
+  int target = rc->av_per_frame_bandwidth * kf_ratio;
+  return vp9_rc_clamp_iframe_target_size(cpi, target);
 }
 
 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
   VP9_COMMON *const cm = &cpi->common;
+  RATE_CONTROL *const rc = &cpi->rc;
   int target;
   if (!cpi->refresh_alt_ref_frame &&
       (cm->current_video_frame == 0 ||
        cm->frame_flags & FRAMEFLAGS_KEY ||
-       cpi->rc.frames_to_key == 0 ||
+       rc->frames_to_key == 0 ||
        (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) {
     cm->frame_type = KEY_FRAME;
-    cpi->rc.this_key_frame_forced = cm->current_video_frame != 0 &&
-                                    cpi->rc.frames_to_key == 0;
-    cpi->rc.frames_to_key = cpi->key_frame_frequency;
-    cpi->rc.kf_boost = DEFAULT_KF_BOOST;
-    cpi->rc.source_alt_ref_active = 0;
+    rc->this_key_frame_forced = cm->current_video_frame != 0 &&
+                                rc->frames_to_key == 0;
+    rc->frames_to_key = cpi->key_frame_frequency;
+    rc->kf_boost = DEFAULT_KF_BOOST;
+    rc->source_alt_ref_active = 0;
     if (cm->current_video_frame == 0) {
-      cpi->rc.active_worst_quality = cpi->rc.worst_quality;
+      rc->active_worst_quality = rc->worst_quality;
     } else {
       // Choose active worst quality twice as large as the last q.
-      cpi->rc.active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2;
-      if (cpi->rc.active_worst_quality > cpi->rc.worst_quality)
-        cpi->rc.active_worst_quality = cpi->rc.worst_quality;
+      rc->active_worst_quality = MIN(rc->worst_quality,
+                                     rc->last_q[KEY_FRAME] * 2);
     }
   } else {
     cm->frame_type = INTER_FRAME;
     if (cm->current_video_frame == 1) {
-      cpi->rc.active_worst_quality = cpi->rc.worst_quality;
+      rc->active_worst_quality = rc->worst_quality;
     } else {
       // Choose active worst quality twice as large as the last q.
-      cpi->rc.active_worst_quality = cpi->rc.last_q[INTER_FRAME] * 2;
-      if (cpi->rc.active_worst_quality > cpi->rc.worst_quality)
-        cpi->rc.active_worst_quality = cpi->rc.worst_quality;
+      rc->active_worst_quality = MIN(rc->worst_quality,
+                                     rc->last_q[INTER_FRAME] * 2);
     }
   }
-  if (cpi->rc.frames_till_gf_update_due == 0) {
-    cpi->rc.baseline_gf_interval = DEFAULT_GF_INTERVAL;
-    cpi->rc.frames_till_gf_update_due = cpi->rc.baseline_gf_interval;
+  if (rc->frames_till_gf_update_due == 0) {
+    rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
+    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
-    if (cpi->rc.frames_till_gf_update_due > cpi->rc.frames_to_key)
-      cpi->rc.frames_till_gf_update_due = cpi->rc.frames_to_key;
+    if (rc->frames_till_gf_update_due > rc->frames_to_key)
+      rc->frames_till_gf_update_due = rc->frames_to_key;
     cpi->refresh_golden_frame = 1;
-    cpi->rc.source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
-    cpi->rc.gfu_boost = 2000;
+    rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
+    rc->gfu_boost = DEFAULT_GF_BOOST;
   }
   if (cm->frame_type == KEY_FRAME)
     target = calc_iframe_target_size_one_pass_vbr(cpi);
@@ -1149,51 +1189,74 @@
     const int pct_high = MIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
     target += (target * pct_high) / 200;
   }
-  if (target < min_frame_target)
-    target = min_frame_target;
-  return target;
+  return MAX(min_frame_target, target);
 }
 
 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
-  int target;
   const RATE_CONTROL *rc = &cpi->rc;
+
   if (cpi->common.current_video_frame == 0) {
-    target = cpi->oxcf.starting_buffer_level / 2;
+    return cpi->oxcf.starting_buffer_level / 2;
   } else {
-    int initial_boost = 32;
+    const int initial_boost = 32;
     int kf_boost = MAX(initial_boost, (int)(2 * cpi->output_framerate - 16));
     if (rc->frames_since_key < cpi->output_framerate / 2) {
       kf_boost = (int)(kf_boost * rc->frames_since_key /
                        (cpi->output_framerate / 2));
     }
-    target = ((16 + kf_boost) * rc->av_per_frame_bandwidth) >> 4;
+    return ((16 + kf_boost) * rc->av_per_frame_bandwidth) >> 4;
   }
-  return target;
+}
+
+void vp9_rc_get_svc_params(VP9_COMP *cpi) {
+  VP9_COMMON *const cm = &cpi->common;
+  int target = cpi->rc.av_per_frame_bandwidth;
+  if ((cm->current_video_frame == 0) ||
+      (cm->frame_flags & FRAMEFLAGS_KEY) ||
+      (cpi->oxcf.auto_key && (cpi->rc.frames_since_key %
+                              cpi->key_frame_frequency == 0))) {
+    cm->frame_type = KEY_FRAME;
+    cpi->rc.source_alt_ref_active = 0;
+    if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+      target = calc_iframe_target_size_one_pass_cbr(cpi);
+      cpi->rc.active_worst_quality = cpi->rc.worst_quality;
+    }
+  } else {
+    cm->frame_type = INTER_FRAME;
+    if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+      target = calc_pframe_target_size_one_pass_cbr(cpi);
+      cpi->rc.active_worst_quality =
+          calc_active_worst_quality_one_pass_cbr(cpi);
+    }
+  }
+  vp9_rc_set_frame_target(cpi, target);
+  cpi->rc.frames_till_gf_update_due = INT_MAX;
+  cpi->rc.baseline_gf_interval = INT_MAX;
 }
 
 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
   VP9_COMMON *const cm = &cpi->common;
+  RATE_CONTROL *const rc = &cpi->rc;
   int target;
   if ((cm->current_video_frame == 0 ||
       cm->frame_flags & FRAMEFLAGS_KEY ||
-      cpi->rc.frames_to_key == 0 ||
+      rc->frames_to_key == 0 ||
       (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) {
     cm->frame_type = KEY_FRAME;
-    cpi->rc.this_key_frame_forced = cm->current_video_frame != 0 &&
-                                    cpi->rc.frames_to_key == 0;
-    cpi->rc.frames_to_key = cpi->key_frame_frequency;
-    cpi->rc.kf_boost = DEFAULT_KF_BOOST;
-    cpi->rc.source_alt_ref_active = 0;
+    rc->this_key_frame_forced = cm->current_video_frame != 0 &&
+                                rc->frames_to_key == 0;
+    rc->frames_to_key = cpi->key_frame_frequency;
+    rc->kf_boost = DEFAULT_KF_BOOST;
+    rc->source_alt_ref_active = 0;
     target = calc_iframe_target_size_one_pass_cbr(cpi);
-    cpi->rc.active_worst_quality = cpi->rc.worst_quality;
+    rc->active_worst_quality = rc->worst_quality;
   } else {
     cm->frame_type = INTER_FRAME;
     target = calc_pframe_target_size_one_pass_cbr(cpi);
-    cpi->rc.active_worst_quality =
-        calc_active_worst_quality_one_pass_cbr(cpi);
+    rc->active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
   }
   vp9_rc_set_frame_target(cpi, target);
   // Don't use gf_update by default in CBR mode.
-  cpi->rc.frames_till_gf_update_due = INT_MAX;
-  cpi->rc.baseline_gf_interval = INT_MAX;
+  rc->frames_till_gf_update_due = INT_MAX;
+  rc->baseline_gf_interval = INT_MAX;
 }
diff --git a/vp9/encoder/vp9_rdopt.c b/vp9/encoder/vp9_rdopt.c
index e5230fe..7b17b85 100644
--- a/vp9/encoder/vp9_rdopt.c
+++ b/vp9/encoder/vp9_rdopt.c
@@ -1064,7 +1064,7 @@
         so = &vp9_scan_orders[TX_4X4][tx_type];
 
         if (tx_type != DCT_DCT)
-          vp9_short_fht4x4(src_diff, coeff, 8, tx_type);
+          vp9_fht4x4(src_diff, coeff, 8, tx_type);
         else
           x->fwd_txm4x4(src_diff, coeff, 8);
 
diff --git a/vp9/encoder/x86/vp9_dct_avx2.c b/vp9/encoder/x86/vp9_dct_avx2.c
index ea031fb..2b82d97 100644
--- a/vp9/encoder/x86/vp9_dct_avx2.c
+++ b/vp9/encoder/x86/vp9_dct_avx2.c
@@ -244,32 +244,36 @@
   transpose_4x4_avx2(in);
 }
 
-void vp9_short_fht4x4_avx2(const int16_t *input, int16_t *output,
-                           int stride, int tx_type) {
+void vp9_fht4x4_avx2(const int16_t *input, int16_t *output,
+                     int stride, int tx_type) {
   __m128i in[4];
-  load_buffer_4x4_avx2(input, in, stride);
+
   switch (tx_type) {
-    case 0:  // DCT_DCT
-      fdct4_avx2(in);
-      fdct4_avx2(in);
+    case DCT_DCT:
+      vp9_fdct4x4_avx2(input, output, stride);
       break;
-    case 1:  // ADST_DCT
+    case ADST_DCT:
+      load_buffer_4x4_avx2(input, in, stride);
       fadst4_avx2(in);
       fdct4_avx2(in);
+      write_buffer_4x4_avx2(output, in);
       break;
-    case 2:  // DCT_ADST
+    case DCT_ADST:
+      load_buffer_4x4_avx2(input, in, stride);
       fdct4_avx2(in);
       fadst4_avx2(in);
+      write_buffer_4x4_avx2(output, in);
       break;
-    case 3:  // ADST_ADST
+    case ADST_ADST:
+      load_buffer_4x4_avx2(input, in, stride);
       fadst4_avx2(in);
       fadst4_avx2(in);
+      write_buffer_4x4_avx2(output, in);
       break;
     default:
       assert(0);
       break;
   }
-  write_buffer_4x4_avx2(output, in);
 }
 
 void vp9_fdct8x8_avx2(const int16_t *input, int16_t *output, int stride) {
@@ -1028,33 +1032,39 @@
   array_transpose_8x8_avx2(in, in);
 }
 
-void vp9_short_fht8x8_avx2(const int16_t *input, int16_t *output,
-                           int stride, int tx_type) {
+void vp9_fht8x8_avx2(const int16_t *input, int16_t *output,
+                     int stride, int tx_type) {
   __m128i in[8];
-  load_buffer_8x8_avx2(input, in, stride);
+
   switch (tx_type) {
-    case 0:  // DCT_DCT
-      fdct8_avx2(in);
-      fdct8_avx2(in);
+    case DCT_DCT:
+      vp9_fdct8x8_avx2(input, output, stride);
       break;
-    case 1:  // ADST_DCT
+    case ADST_DCT:
+      load_buffer_8x8_avx2(input, in, stride);
       fadst8_avx2(in);
       fdct8_avx2(in);
+      right_shift_8x8_avx2(in, 1);
+      write_buffer_8x8_avx2(output, in, 8);
       break;
-    case 2:  // DCT_ADST
+    case DCT_ADST:
+      load_buffer_8x8_avx2(input, in, stride);
       fdct8_avx2(in);
       fadst8_avx2(in);
+      right_shift_8x8_avx2(in, 1);
+      write_buffer_8x8_avx2(output, in, 8);
       break;
-    case 3:  // ADST_ADST
+    case ADST_ADST:
+      load_buffer_8x8_avx2(input, in, stride);
       fadst8_avx2(in);
       fadst8_avx2(in);
+      right_shift_8x8_avx2(in, 1);
+      write_buffer_8x8_avx2(output, in, 8);
       break;
     default:
       assert(0);
       break;
   }
-  right_shift_8x8_avx2(in, 1);
-  write_buffer_8x8_avx2(output, in, 8);
 }
 
 void vp9_fdct16x16_avx2(const int16_t *input, int16_t *output, int stride) {
@@ -2534,36 +2544,39 @@
   array_transpose_16x16_avx2(in0, in1);
 }
 
-void vp9_short_fht16x16_avx2(const int16_t *input, int16_t *output,
-                             int stride, int tx_type) {
+void vp9_fht16x16_avx2(const int16_t *input, int16_t *output,
+                      int stride, int tx_type) {
   __m128i in0[16], in1[16];
-  load_buffer_16x16_avx2(input, in0, in1, stride);
+
   switch (tx_type) {
-    case 0:  // DCT_DCT
-      fdct16_avx2(in0, in1);
-      right_shift_16x16_avx2(in0, in1);
-      fdct16_avx2(in0, in1);
+    case DCT_DCT:
+      vp9_fdct16x16_avx2(input, output, stride);
       break;
-    case 1:  // ADST_DCT
+    case ADST_DCT:
+      load_buffer_16x16_avx2(input, in0, in1, stride);
       fadst16_avx2(in0, in1);
       right_shift_16x16_avx2(in0, in1);
       fdct16_avx2(in0, in1);
+      write_buffer_16x16_avx2(output, in0, in1, 16);
       break;
-    case 2:  // DCT_ADST
+    case DCT_ADST:
+      load_buffer_16x16_avx2(input, in0, in1, stride);
       fdct16_avx2(in0, in1);
       right_shift_16x16_avx2(in0, in1);
       fadst16_avx2(in0, in1);
+      write_buffer_16x16_avx2(output, in0, in1, 16);
       break;
-    case 3:  // ADST_ADST
+    case ADST_ADST:
+      load_buffer_16x16_avx2(input, in0, in1, stride);
       fadst16_avx2(in0, in1);
       right_shift_16x16_avx2(in0, in1);
       fadst16_avx2(in0, in1);
+      write_buffer_16x16_avx2(output, in0, in1, 16);
       break;
     default:
       assert(0);
       break;
   }
-  write_buffer_16x16_avx2(output, in0, in1, 16);
 }
 
 #define FDCT32x32_2D_AVX2 vp9_fdct32x32_rd_avx2
diff --git a/vp9/encoder/x86/vp9_dct_sse2.c b/vp9/encoder/x86/vp9_dct_sse2.c
index c876cc2..852cf86 100644
--- a/vp9/encoder/x86/vp9_dct_sse2.c
+++ b/vp9/encoder/x86/vp9_dct_sse2.c
@@ -242,32 +242,36 @@
   transpose_4x4(in);
 }
 
-void vp9_short_fht4x4_sse2(const int16_t *input, int16_t *output,
-                           int stride, int tx_type) {
+void vp9_fht4x4_sse2(const int16_t *input, int16_t *output,
+                     int stride, int tx_type) {
   __m128i in[4];
-  load_buffer_4x4(input, in, stride);
+
   switch (tx_type) {
-    case 0:  // DCT_DCT
-      fdct4_sse2(in);
-      fdct4_sse2(in);
+    case DCT_DCT:
+      vp9_fdct4x4_sse2(input, output, stride);
       break;
-    case 1:  // ADST_DCT
+    case ADST_DCT:
+      load_buffer_4x4(input, in, stride);
       fadst4_sse2(in);
       fdct4_sse2(in);
+      write_buffer_4x4(output, in);
       break;
-    case 2:  // DCT_ADST
+    case DCT_ADST:
+      load_buffer_4x4(input, in, stride);
       fdct4_sse2(in);
       fadst4_sse2(in);
+      write_buffer_4x4(output, in);
       break;
-    case 3:  // ADST_ADST
+    case ADST_ADST:
+      load_buffer_4x4(input, in, stride);
       fadst4_sse2(in);
       fadst4_sse2(in);
+      write_buffer_4x4(output, in);
       break;
-    default:
-      assert(0);
-      break;
+   default:
+     assert(0);
+     break;
   }
-  write_buffer_4x4(output, in);
 }
 
 void vp9_fdct8x8_sse2(const int16_t *input, int16_t *output, int stride) {
@@ -1026,33 +1030,39 @@
   array_transpose_8x8(in, in);
 }
 
-void vp9_short_fht8x8_sse2(const int16_t *input, int16_t *output,
-                           int stride, int tx_type) {
+void vp9_fht8x8_sse2(const int16_t *input, int16_t *output,
+                     int stride, int tx_type) {
   __m128i in[8];
-  load_buffer_8x8(input, in, stride);
+
   switch (tx_type) {
-    case 0:  // DCT_DCT
-      fdct8_sse2(in);
-      fdct8_sse2(in);
+    case DCT_DCT:
+      vp9_fdct8x8_sse2(input, output, stride);
       break;
-    case 1:  // ADST_DCT
+    case ADST_DCT:
+      load_buffer_8x8(input, in, stride);
       fadst8_sse2(in);
       fdct8_sse2(in);
+      right_shift_8x8(in, 1);
+      write_buffer_8x8(output, in, 8);
       break;
-    case 2:  // DCT_ADST
+    case DCT_ADST:
+      load_buffer_8x8(input, in, stride);
       fdct8_sse2(in);
       fadst8_sse2(in);
+      right_shift_8x8(in, 1);
+      write_buffer_8x8(output, in, 8);
       break;
-    case 3:  // ADST_ADST
+    case ADST_ADST:
+      load_buffer_8x8(input, in, stride);
       fadst8_sse2(in);
       fadst8_sse2(in);
+      right_shift_8x8(in, 1);
+      write_buffer_8x8(output, in, 8);
       break;
     default:
       assert(0);
       break;
   }
-  right_shift_8x8(in, 1);
-  write_buffer_8x8(output, in, 8);
 }
 
 void vp9_fdct16x16_sse2(const int16_t *input, int16_t *output, int stride) {
@@ -2532,36 +2542,39 @@
   array_transpose_16x16(in0, in1);
 }
 
-void vp9_short_fht16x16_sse2(const int16_t *input, int16_t *output,
-                             int stride, int tx_type) {
+void vp9_fht16x16_sse2(const int16_t *input, int16_t *output,
+                       int stride, int tx_type) {
   __m128i in0[16], in1[16];
-  load_buffer_16x16(input, in0, in1, stride);
+
   switch (tx_type) {
-    case 0:  // DCT_DCT
-      fdct16_sse2(in0, in1);
-      right_shift_16x16(in0, in1);
-      fdct16_sse2(in0, in1);
+    case DCT_DCT:
+      vp9_fdct16x16_sse2(input, output, stride);
       break;
-    case 1:  // ADST_DCT
+    case ADST_DCT:
+      load_buffer_16x16(input, in0, in1, stride);
       fadst16_sse2(in0, in1);
       right_shift_16x16(in0, in1);
       fdct16_sse2(in0, in1);
+      write_buffer_16x16(output, in0, in1, 16);
       break;
-    case 2:  // DCT_ADST
+    case DCT_ADST:
+      load_buffer_16x16(input, in0, in1, stride);
       fdct16_sse2(in0, in1);
       right_shift_16x16(in0, in1);
       fadst16_sse2(in0, in1);
+      write_buffer_16x16(output, in0, in1, 16);
       break;
-    case 3:  // ADST_ADST
+    case ADST_ADST:
+      load_buffer_16x16(input, in0, in1, stride);
       fadst16_sse2(in0, in1);
       right_shift_16x16(in0, in1);
       fadst16_sse2(in0, in1);
+      write_buffer_16x16(output, in0, in1, 16);
       break;
     default:
       assert(0);
       break;
   }
-  write_buffer_16x16(output, in0, in1, 16);
 }
 
 #define FDCT32x32_2D vp9_fdct32x32_rd_sse2
diff --git a/vp9/encoder/x86/vp9_quantize_ssse3.asm b/vp9/encoder/x86/vp9_quantize_ssse3.asm
index db30660..48ccef8 100644
--- a/vp9/encoder/x86/vp9_quantize_ssse3.asm
+++ b/vp9/encoder/x86/vp9_quantize_ssse3.asm
@@ -188,7 +188,8 @@
   pmaxsw                          m8, m7
   pshuflw                         m7, m8, 0x1
   pmaxsw                          m8, m7
-  pextrw                        [r2], m8, 0
+  pextrw                          r6, m8, 0
+  mov                             [r2], r6
   RET
 
   ; skip-block, i.e. just write all zeroes
@@ -214,5 +215,5 @@
 %endmacro
 
 INIT_XMM ssse3
-QUANTIZE_FN b, 6
+QUANTIZE_FN b, 7
 QUANTIZE_FN b_32x32, 7
diff --git a/vp9/vp9_cx_iface.c b/vp9/vp9_cx_iface.c
index 35d2020..ece6d52 100644
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -175,6 +175,23 @@
 
   RANGE_CHECK(cfg, ss_number_layers,      1,
               VPX_SS_MAX_LAYERS); /*Spatial layers max */
+
+  RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
+  if (cfg->ts_number_layers > 1) {
+    int i;
+    for (i = 1; i < cfg->ts_number_layers; ++i) {
+      if (cfg->ts_target_bitrate[i] < cfg->ts_target_bitrate[i-1]) {
+        ERROR("ts_target_bitrate entries are not increasing");
+      }
+    }
+    RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
+    for (i = cfg->ts_number_layers-2; i > 0; --i) {
+      if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i]) {
+        ERROR("ts_rate_decimator factors are not powers of 2");
+      }
+    }
+  }
+
   /* VP8 does not support a lower bound on the keyframe interval in
    * automatic keyframe placement mode.
    */
@@ -279,14 +296,11 @@
     oxcf->lag_in_frames = cfg.g_lag_in_frames;
   }
 
-  // VBR only supported for now.
-  // CBR code has been deprectated for experimental phase.
-  // CQ mode not yet tested
-  oxcf->end_usage        = USAGE_LOCAL_FILE_PLAYBACK;
+  oxcf->end_usage   = USAGE_LOCAL_FILE_PLAYBACK;
   if (cfg.rc_end_usage == VPX_CQ)
-    oxcf->end_usage      = USAGE_CONSTRAINED_QUALITY;
+    oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
   else if (cfg.rc_end_usage == VPX_Q)
-    oxcf->end_usage      = USAGE_CONSTANT_QUALITY;
+    oxcf->end_usage = USAGE_CONSTANT_QUALITY;
   else if (cfg.rc_end_usage == VPX_CBR)
     oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
 
@@ -342,6 +356,19 @@
   oxcf->aq_mode = vp8_cfg.aq_mode;
 
   oxcf->ss_number_layers = cfg.ss_number_layers;
+
+  oxcf->ts_number_layers = cfg.ts_number_layers;
+
+  if (oxcf->ts_number_layers > 1) {
+    memcpy(oxcf->ts_target_bitrate, cfg.ts_target_bitrate,
+           sizeof(cfg.ts_target_bitrate));
+    memcpy(oxcf->ts_rate_decimator, cfg.ts_rate_decimator,
+           sizeof(cfg.ts_rate_decimator));
+  } else if (oxcf->ts_number_layers == 1) {
+    oxcf->ts_target_bitrate[0] = oxcf->target_bandwidth;
+    oxcf->ts_rate_decimator[0] = 1;
+  }
+
   /*
   printf("Current VP9 Settings: \n");
   printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
@@ -858,10 +885,9 @@
 static vpx_codec_err_t vp9e_set_reference(vpx_codec_alg_priv_t *ctx,
                                           int ctr_id,
                                           va_list args) {
-  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
+  vpx_ref_frame_t *frame = va_arg(args, vpx_ref_frame_t *);
 
-  if (data) {
-    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
+  if (frame != NULL) {
     YV12_BUFFER_CONFIG sd;
 
     image2yuvconfig(&frame->img, &sd);
@@ -876,10 +902,9 @@
 static vpx_codec_err_t vp9e_copy_reference(vpx_codec_alg_priv_t *ctx,
                                            int ctr_id,
                                            va_list args) {
-  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
+  vpx_ref_frame_t *frame = va_arg(args, vpx_ref_frame_t *);
 
-  if (data) {
-    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
+  if (frame != NULL) {
     YV12_BUFFER_CONFIG sd;
 
     image2yuvconfig(&frame->img, &sd);
@@ -894,13 +919,13 @@
 static vpx_codec_err_t get_reference(vpx_codec_alg_priv_t *ctx,
                                      int ctr_id,
                                      va_list args) {
-  vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
+  vp9_ref_frame_t *frame = va_arg(args, vp9_ref_frame_t *);
 
-  if (data) {
+  if (frame != NULL) {
     YV12_BUFFER_CONFIG* fb;
 
-    vp9_get_reference_enc(ctx->cpi, data->idx, &fb);
-    yuvconfig2image(&data->img, fb, NULL);
+    vp9_get_reference_enc(ctx->cpi, frame->idx, &fb);
+    yuvconfig2image(&frame->img, fb, NULL);
     return VPX_CODEC_OK;
   } else {
     return VPX_CODEC_INVALID_PARAM;
@@ -911,11 +936,11 @@
                                           int ctr_id,
                                           va_list args) {
 #if CONFIG_VP9_POSTPROC
-  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
+  vp8_postproc_cfg_t *config = va_arg(args, vp8_postproc_cfg_t *);
   (void)ctr_id;
 
-  if (data) {
-    ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
+  if (config != NULL) {
+    ctx->preview_ppcfg = *config;
     return VPX_CODEC_OK;
   } else {
     return VPX_CODEC_INVALID_PARAM;
@@ -989,14 +1014,13 @@
 static vpx_codec_err_t vp9e_set_scalemode(vpx_codec_alg_priv_t *ctx,
                                           int ctr_id,
                                           va_list args) {
-  vpx_scaling_mode_t *data =  va_arg(args, vpx_scaling_mode_t *);
+  vpx_scaling_mode_t *scalemode =  va_arg(args, vpx_scaling_mode_t *);
 
-  if (data) {
+  if (scalemode != NULL) {
     int res;
-    vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data;
     res = vp9_set_internal_size(ctx->cpi,
-                                (VPX_SCALING)scalemode.h_scaling_mode,
-                                (VPX_SCALING)scalemode.v_scaling_mode);
+                                (VPX_SCALING)scalemode->h_scaling_mode,
+                                (VPX_SCALING)scalemode->v_scaling_mode);
 
     if (!res) {
       return VPX_CODEC_OK;
@@ -1012,32 +1036,54 @@
                                     va_list args) {
   int data = va_arg(args, int);
   vp9_set_svc(ctx->cpi, data);
+  // CBR mode for SVC with both temporal and spatial layers not yet supported.
+  if (data == 1 &&
+      ctx->cfg.rc_end_usage == VPX_CBR &&
+      ctx->cfg.ss_number_layers > 1 &&
+      ctx->cfg.ts_number_layers > 1) {
+    return VPX_CODEC_INVALID_PARAM;
+  }
+  return VPX_CODEC_OK;
+}
+
+static vpx_codec_err_t vp9e_set_svc_layer_id(vpx_codec_alg_priv_t *ctx,
+                                             int ctr_id,
+                                             va_list args) {
+  vpx_svc_layer_id_t *data = va_arg(args, vpx_svc_layer_id_t *);
+  VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
+  cpi->svc.spatial_layer_id = data->spatial_layer_id;
+  cpi->svc.temporal_layer_id = data->temporal_layer_id;
+  // Checks on valid layer_id input.
+  if (cpi->svc.temporal_layer_id < 0 ||
+      cpi->svc.temporal_layer_id >= ctx->cfg.ts_number_layers) {
+    return VPX_CODEC_INVALID_PARAM;
+  }
+  if (cpi->svc.spatial_layer_id < 0 ||
+      cpi->svc.spatial_layer_id >= ctx->cfg.ss_number_layers) {
+    return VPX_CODEC_INVALID_PARAM;
+  }
   return VPX_CODEC_OK;
 }
 
 static vpx_codec_err_t vp9e_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
                                                int ctr_id, va_list args) {
-  vpx_svc_parameters_t *data = va_arg(args, vpx_svc_parameters_t *);
   VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
-  vpx_svc_parameters_t params;
+  vpx_svc_parameters_t *params = va_arg(args, vpx_svc_parameters_t *);
 
-  if (data == NULL) {
+  if (params == NULL) return VPX_CODEC_INVALID_PARAM;
+
+  cpi->svc.spatial_layer_id = params->spatial_layer;
+  cpi->svc.temporal_layer_id = params->temporal_layer;
+
+  cpi->lst_fb_idx = params->lst_fb_idx;
+  cpi->gld_fb_idx = params->gld_fb_idx;
+  cpi->alt_fb_idx = params->alt_fb_idx;
+
+  if (vp9_set_size_literal(ctx->cpi, params->width, params->height) != 0)
     return VPX_CODEC_INVALID_PARAM;
-  }
 
-  params = *(vpx_svc_parameters_t *)data;
-
-  cpi->current_layer = params.layer;
-  cpi->lst_fb_idx = params.lst_fb_idx;
-  cpi->gld_fb_idx = params.gld_fb_idx;
-  cpi->alt_fb_idx = params.alt_fb_idx;
-
-  if (vp9_set_size_literal(ctx->cpi, params.width, params.height) != 0) {
-    return VPX_CODEC_INVALID_PARAM;
-  }
-
-  ctx->cfg.rc_max_quantizer = params.max_quantizer;
-  ctx->cfg.rc_min_quantizer = params.min_quantizer;
+  ctx->cfg.rc_max_quantizer = params->max_quantizer;
+  ctx->cfg.rc_min_quantizer = params->min_quantizer;
 
   set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
   vp9_change_config(ctx->cpi, &ctx->oxcf);
@@ -1076,6 +1122,7 @@
   {VP9_GET_REFERENCE,                 get_reference},
   {VP9E_SET_SVC,                      vp9e_set_svc},
   {VP9E_SET_SVC_PARAMETERS,           vp9e_set_svc_parameters},
+  {VP9E_SET_SVC_LAYER_ID,             vp9e_set_svc_layer_id},
   { -1, NULL},
 };
 
@@ -1126,7 +1173,11 @@
       9999,               /* kf_max_dist */
 
       VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
-
+      1,                  /* ts_number_layers */
+      {0},                /* ts_target_bitrate */
+      {0},                /* ts_rate_decimator */
+      0,                  /* ts_periodicity */
+      {0},                /* ts_layer_id */
 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
       "vp8.fpf"           /* first pass filename */
 #endif
diff --git a/vp9/vp9_dx_iface.c b/vp9/vp9_dx_iface.c
index 92c6cd2..0e19b0c 100644
--- a/vp9/vp9_dx_iface.c
+++ b/vp9/vp9_dx_iface.c
@@ -148,7 +148,9 @@
   {
     struct vp9_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
     const int frame_marker = vp9_rb_read_literal(&rb, 2);
-    const int version = vp9_rb_read_bit(&rb) | (vp9_rb_read_bit(&rb) << 1);
+    const int version = vp9_rb_read_bit(&rb);
+    (void) vp9_rb_read_bit(&rb);  // unused version bit
+
     if (frame_marker != VP9_FRAME_MARKER)
       return VPX_CODEC_UNSUP_BITSTREAM;
 #if CONFIG_NON420
diff --git a/vp9/vp9cx.mk b/vp9/vp9cx.mk
index c225f54..64f9f09 100644
--- a/vp9/vp9cx.mk
+++ b/vp9/vp9cx.mk
@@ -19,7 +19,6 @@
 
 VP9_CX_SRCS-yes += encoder/vp9_bitstream.c
 VP9_CX_SRCS-yes += encoder/vp9_dct.c
-VP9_CX_SRCS-yes += encoder/vp9_dct.h
 VP9_CX_SRCS-yes += encoder/vp9_encodeframe.c
 VP9_CX_SRCS-yes += encoder/vp9_encodeframe.h
 VP9_CX_SRCS-yes += encoder/vp9_encodemb.c
diff --git a/vpx/src/svc_encodeframe.c b/vpx/src/svc_encodeframe.c
index 810e881..4f5ba6f 100644
--- a/vpx/src/svc_encodeframe.c
+++ b/vpx/src/svc_encodeframe.c
@@ -95,7 +95,8 @@
 
 // create LayerData from encoder output
 static struct LayerData *ld_create(void *buf, size_t size) {
-  struct LayerData *const layer_data = malloc(sizeof(*layer_data));
+  struct LayerData *const layer_data =
+      (struct LayerData *)malloc(sizeof(*layer_data));
   if (layer_data == NULL) {
     return NULL;
   }
@@ -201,18 +202,18 @@
 static SvcInternal *get_svc_internal(SvcContext *svc_ctx) {
   if (svc_ctx == NULL) return NULL;
   if (svc_ctx->internal == NULL) {
-    SvcInternal *const si = malloc(sizeof(*si));
+    SvcInternal *const si = (SvcInternal *)malloc(sizeof(*si));
     if (si != NULL) {
       memset(si, 0, sizeof(*si));
     }
     svc_ctx->internal = si;
   }
-  return svc_ctx->internal;
+  return (SvcInternal *)svc_ctx->internal;
 }
 
 static const SvcInternal *get_const_svc_internal(const SvcContext *svc_ctx) {
   if (svc_ctx == NULL) return NULL;
-  return svc_ctx->internal;
+  return (const SvcInternal *)svc_ctx->internal;
 }
 
 static void svc_log_reset(SvcContext *svc_ctx) {
@@ -272,7 +273,7 @@
   char *save_ptr;
   int found = 0;
   int i, q;
-  int res = VPX_CODEC_OK;
+  vpx_codec_err_t res = VPX_CODEC_OK;
   SvcInternal *const si = get_svc_internal(svc_ctx);
 
   if (quantizer_values == NULL || strlen(quantizer_values) == 0) {
@@ -322,7 +323,7 @@
   int found = 0;
   int i;
   int64_t num, den;
-  int res = VPX_CODEC_OK;
+  vpx_codec_err_t res = VPX_CODEC_OK;
   SvcInternal *const si = get_svc_internal(svc_ctx);
 
   if (scale_factors == NULL || strlen(scale_factors) == 0) {
@@ -381,7 +382,7 @@
   char *option_name;
   char *option_value;
   char *input_ptr;
-  int res = VPX_CODEC_OK;
+  vpx_codec_err_t res = VPX_CODEC_OK;
 
   if (options == NULL) return VPX_CODEC_OK;
   input_string = strdup(options);
@@ -499,6 +500,7 @@
 
   // modify encoder configuration
   enc_cfg->ss_number_layers = si->layers;
+  enc_cfg->ts_number_layers = 1;  // Temporal layers not used in this encoder.
   enc_cfg->kf_mode = VPX_KF_DISABLED;
   enc_cfg->g_pass = VPX_RC_ONE_PASS;
   // Lag in frames not currently supported
@@ -691,7 +693,8 @@
   SvcInternal *const si = get_svc_internal(svc_ctx);
 
   memset(&svc_params, 0, sizeof(svc_params));
-  svc_params.layer = si->layer;
+  svc_params.temporal_layer = 0;
+  svc_params.spatial_layer = si->layer;
   svc_params.flags = si->enc_frame_flags;
 
   layer = si->layer;
@@ -817,7 +820,7 @@
               ld_create(cx_pkt->data.frame.buf, (size_t)frame_pkt_size);
           if (layer_data == NULL) {
             svc_log(svc_ctx, SVC_LOG_ERROR, "Error allocating LayerData\n");
-            return 0;
+            return VPX_CODEC_OK;
           }
           ld_list_add(&cx_layer_list, layer_data);
 
@@ -862,7 +865,7 @@
     si->buffer_size = si->frame_size;
   }
   // copy layer data into packet
-  ld_list_copy_to_buffer(cx_layer_list, si->buffer);
+  ld_list_copy_to_buffer(cx_layer_list, (uint8_t *)si->buffer);
 
   ld_list_free(cx_layer_list);
 
diff --git a/vpx/src/vpx_encoder.c b/vpx/src/vpx_encoder.c
index 778ed2f..23742c8 100644
--- a/vpx/src/vpx_encoder.c
+++ b/vpx/src/vpx_encoder.c
@@ -70,7 +70,7 @@
                                              vpx_codec_flags_t     flags,
                                              vpx_rational_t       *dsf,
                                              int                   ver) {
-  vpx_codec_err_t res = 0;
+  vpx_codec_err_t res = VPX_CODEC_OK;
 
   if (ver != VPX_ENCODER_ABI_VERSION)
     res = VPX_CODEC_ABI_MISMATCH;
@@ -207,7 +207,7 @@
                                   unsigned long               duration,
                                   vpx_enc_frame_flags_t       flags,
                                   unsigned long               deadline) {
-  vpx_codec_err_t res = 0;
+  vpx_codec_err_t res = VPX_CODEC_OK;
 
   if (!ctx || (img && !duration))
     res = VPX_CODEC_INVALID_PARAM;
diff --git a/vpx/vp8cx.h b/vpx/vp8cx.h
index 829490f..d0ac1af 100644
--- a/vpx/vp8cx.h
+++ b/vpx/vp8cx.h
@@ -194,7 +194,8 @@
   VP9E_SET_AQ_MODE,
 
   VP9E_SET_SVC,
-  VP9E_SET_SVC_PARAMETERS
+  VP9E_SET_SVC_PARAMETERS,
+  VP9E_SET_SVC_LAYER_ID
 };
 
 /*!\brief vpx 1-D scaling mode
@@ -285,7 +286,8 @@
 typedef struct vpx_svc_parameters {
   unsigned int width;         /**< width of current spatial layer */
   unsigned int height;        /**< height of current spatial layer */
-  int layer;                  /**< current layer number - 0 = base */
+  int spatial_layer;          /**< current spatial layer number - 0 = base */
+  int temporal_layer;         /**< current temporal layer number - 0 = base */
   int flags;                  /**< encode frame flags */
   int max_quantizer;          /**< max quantizer for current layer */
   int min_quantizer;          /**< min quantizer for current layer */
@@ -295,6 +297,11 @@
   int alt_fb_idx;             /**< alt reference frame frame buffer index */
 } vpx_svc_parameters_t;
 
+typedef struct vpx_svc_layer_id {
+  int spatial_layer_id;
+  int temporal_layer_id;
+} vpx_svc_layer_id_t;
+
 /*!\brief VP8 encoder control function parameter type
  *
  * Defines the data types that VP8E control functions take. Note that
@@ -316,6 +323,7 @@
 
 VPX_CTRL_USE_TYPE(VP9E_SET_SVC,                int)
 VPX_CTRL_USE_TYPE(VP9E_SET_SVC_PARAMETERS,     vpx_svc_parameters_t *)
+VPX_CTRL_USE_TYPE(VP9E_SET_SVC_LAYER_ID,       vpx_svc_layer_id_t *)
 
 VPX_CTRL_USE_TYPE(VP8E_SET_CPUUSED,            int)
 VPX_CTRL_USE_TYPE(VP8E_SET_ENABLEAUTOALTREF,   unsigned int)
diff --git a/vpx/vpx_encoder.h b/vpx/vpx_encoder.h
index 3473885..1d9f0c9 100644
--- a/vpx/vpx_encoder.h
+++ b/vpx/vpx_encoder.h
@@ -604,47 +604,48 @@
      * Spatial scalability settings (ss)
      */
 
-    /*!\brief Number of coding layers (spatial)
+    /*!\brief Number of spatial coding layers.
      *
-     * This value specifies the number of coding layers to be used.
+     * This value specifies the number of spatial coding layers to be used.
      */
     unsigned int           ss_number_layers;
 
-    /*!\brief Number of coding layers
+    /*!\brief Number of temporal coding layers.
      *
-     * This value specifies the number of coding layers to be used.
+     * This value specifies the number of temporal layers to be used.
      */
     unsigned int           ts_number_layers;
 
-    /*!\brief Target bitrate for each layer
+    /*!\brief Target bitrate for each temporal layer.
      *
-     * These values specify the target coding bitrate for each coding layer.
+     * These values specify the target coding bitrate to be used for each
+     * temporal layer.
      */
     unsigned int           ts_target_bitrate[VPX_TS_MAX_LAYERS];
 
-    /*!\brief Frame rate decimation factor for each layer
+    /*!\brief Frame rate decimation factor for each temporal layer.
      *
      * These values specify the frame rate decimation factors to apply
-     * to each layer.
+     * to each temporal layer.
      */
     unsigned int           ts_rate_decimator[VPX_TS_MAX_LAYERS];
 
-    /*!\brief Length of the sequence defining frame layer membership
+    /*!\brief Length of the sequence defining frame temporal layer membership.
      *
      * This value specifies the length of the sequence that defines the
-     * membership of frames to layers. For example, if ts_periodicity=8 then
-     * frames are assigned to coding layers with a repeated sequence of
-     * length 8.
-     */
+     * membership of frames to temporal layers. For example, if the
+     * ts_periodicity = 8, then the frames are assigned to coding layers with a
+     * repeated sequence of length 8.
+    */
     unsigned int           ts_periodicity;
 
-    /*!\brief Template defining the membership of frames to coding layers
+    /*!\brief Template defining the membership of frames to temporal layers.
      *
-     * This array defines the membership of frames to coding layers. For a
-     * 2-layer encoding that assigns even numbered frames to one layer (0)
-     * and odd numbered frames to a second layer (1) with ts_periodicity=8,
-     * then ts_layer_id = (0,1,0,1,0,1,0,1).
-     */
+     * This array defines the membership of frames to temporal coding layers.
+     * For a 2-layer encoding that assigns even numbered frames to one temporal
+     * layer (0) and odd numbered frames to a second temporal layer (1) with
+     * ts_periodicity=8, then ts_layer_id = (0,1,0,1,0,1,0,1).
+    */
     unsigned int           ts_layer_id[VPX_TS_MAX_PERIODICITY];
   } vpx_codec_enc_cfg_t; /**< alias for struct vpx_codec_enc_cfg */