Merge "Using stride (# of elements) instead of pitch (bytes) in fdct8x8."
diff --git a/examples/decoder_tmpl.c b/examples/decoder_tmpl.c
index 597fea2..3e55352 100644
--- a/examples/decoder_tmpl.c
+++ b/examples/decoder_tmpl.c
@@ -12,14 +12,14 @@
 /*
 @*INTRODUCTION
  */
-#include "vpx_config.h"
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <string.h>
 #define VPX_CODEC_DISABLE_COMPAT 1
-#include "vpx/vpx_decoder.h"
+#include "./vpx_config.h"
 #include "vpx/vp8dx.h"
+#include "vpx/vpx_decoder.h"
 #define interface (vpx_codec_vp8_dx())
 @EXTRA_INCLUDES
 
diff --git a/libs.mk b/libs.mk
index 4691a12..4062833 100644
--- a/libs.mk
+++ b/libs.mk
@@ -122,6 +122,7 @@
   CODEC_EXPORTS-yes += $(addprefix $(VP9_PREFIX),$(VP9_CX_EXPORTS))
   CODEC_SRCS-yes += $(VP9_PREFIX)vp9cx.mk vpx/vp8.h vpx/vp8cx.h
   INSTALL-LIBS-yes += include/vpx/vp8.h include/vpx/vp8cx.h
+  INSTALL-LIBS-yes += include/vpx/svc_context.h
   INSTALL_MAPS += include/vpx/% $(SRC_PATH_BARE)/$(VP9_PREFIX)/%
   CODEC_DOC_SRCS += vpx/vp8.h vpx/vp8cx.h
   CODEC_DOC_SECTIONS += vp9 vp9_encoder
diff --git a/test/dct16x16_test.cc b/test/dct16x16_test.cc
index 3d61d40..b990bf8 100644
--- a/test/dct16x16_test.cc
+++ b/test/dct16x16_test.cc
@@ -395,8 +395,7 @@
       for (int j = 0; j < kNumCoeffs; ++j)
         coeff[j] = round(out_r[j]);
 
-      const int pitch = 32;
-      REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, pitch));
+      REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, 16));
 
       for (int j = 0; j < kNumCoeffs; ++j) {
         const uint32_t diff = dst[j] - src[j];
@@ -421,7 +420,7 @@
     fwd_txfm_ = GET_PARAM(0);
     inv_txfm_ = GET_PARAM(1);
     tx_type_  = GET_PARAM(2);
-    pitch_    = 32;
+    pitch_    = 16;
     fwd_txfm_ref = fdct16x16_ref;
   }
   virtual void TearDown() { libvpx_test::ClearSystemState(); }
@@ -431,7 +430,7 @@
     fwd_txfm_(in, out, stride);
   }
   void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) {
-    inv_txfm_(out, dst, stride >> 1);
+    inv_txfm_(out, dst, stride);
   }
 
   fdct_t fwd_txfm_;
diff --git a/test/svc_test.cc b/test/svc_test.cc
new file mode 100644
index 0000000..8a3e3a2
--- /dev/null
+++ b/test/svc_test.cc
@@ -0,0 +1,190 @@
+#include <string>
+#include "third_party/googletest/src/include/gtest/gtest.h"
+#include "test/i420_video_source.h"
+#include "test/decode_test_driver.h"
+#include "vpx/vpx_encoder.h"
+#include "vpx/vp8cx.h"
+#include "test/codec_factory.h"
+
+extern "C" {
+#include "vpx/svc_context.h"
+}
+
+namespace {
+
+using libvpx_test::CodecFactory;
+using libvpx_test::VP9CodecFactory;
+using libvpx_test::Decoder;
+
+class SvcTest : public ::testing::Test {
+ protected:
+  SvcTest()
+      : codec_iface_(0),
+        test_file_name("hantro_collage_w352h288.yuv"),
+        decoder_(0) {}
+
+  virtual void SetUp() {
+    memset(&svc_, 0, sizeof(svc_));
+    svc_.first_frame_full_size = 1;
+    svc_.encoding_mode = INTER_LAYER_PREDICTION_IP;
+    svc_.log_level = SVC_LOG_DEBUG;
+    svc_.log_print = 1;
+    svc_.gop_size = 100;
+
+    codec_iface_ = vpx_codec_vp9_cx();
+    vpx_codec_err_t res =
+        vpx_codec_enc_config_default(codec_iface_, &codec_enc_, 0);
+    EXPECT_EQ(res, VPX_CODEC_OK);
+
+    codec_enc_.g_w = kWidth;
+    codec_enc_.g_h = kHeight;
+    codec_enc_.g_timebase.num = 1;
+    codec_enc_.g_timebase.den = 60;
+
+    vpx_codec_dec_cfg_t dec_cfg = {0};
+    VP9CodecFactory codec_factory;
+    decoder_ = codec_factory.CreateDecoder(dec_cfg, 0);
+  }
+
+  SvcContext svc_;
+  vpx_codec_ctx_t codec_;
+  struct vpx_codec_enc_cfg codec_enc_;
+  vpx_codec_iface_t* codec_iface_;
+  std::string test_file_name;
+  enum {
+    kWidth = 352,
+    kHeight = 288,
+  };
+
+  Decoder* decoder_;
+};
+
+TEST_F(SvcTest, SvcInit) {
+  svc_.spatial_layers = 0;  // not enough layers
+  vpx_codec_err_t res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
+  EXPECT_EQ(res, VPX_CODEC_INVALID_PARAM);
+
+  svc_.spatial_layers = 6;  // too many layers
+  res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
+  EXPECT_EQ(res, VPX_CODEC_INVALID_PARAM);
+
+  svc_.spatial_layers = 2;
+  svc_.scale_factors = "4/16,16*16";  // invalid scale values
+  res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
+  EXPECT_EQ(res, VPX_CODEC_INVALID_PARAM);
+
+  svc_.scale_factors = "4/16,16/16";  // valid scale values
+
+  res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
+  EXPECT_EQ(res, VPX_CODEC_OK);
+}
+
+// test that decoder can handle an svc frame as the first frame in a sequence
+// this test is disabled since it with the deco
+TEST_F(SvcTest, DISABLED_FirstFrameHasLayers) {
+  svc_.first_frame_full_size = 0;
+  svc_.spatial_layers = 2;
+  svc_.scale_factors = "4/16,16/16";
+  svc_.quantizer_values = "40,30";
+
+  vpx_codec_err_t res =
+      vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
+  EXPECT_EQ(res, VPX_CODEC_OK);
+
+  libvpx_test::I420VideoSource video(test_file_name, kWidth, kHeight,
+                                     codec_enc_.g_timebase.den,
+                                     codec_enc_.g_timebase.num, 0, 30);
+  video.Begin();
+
+  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
+                       video.duration(), VPX_DL_REALTIME);
+  EXPECT_EQ(res, VPX_CODEC_OK);
+
+  vpx_codec_err_t res_dec = decoder_->DecodeFrame(
+      (const uint8_t*)svc_get_buffer(&svc_), svc_get_frame_size(&svc_));
+
+  // this test fails with a decoder error
+  ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
+}
+
+TEST_F(SvcTest, EncodeThreeFrames) {
+  svc_.first_frame_full_size = 1;
+  svc_.spatial_layers = 2;
+  svc_.scale_factors = "4/16,16/16";
+  svc_.quantizer_values = "40,30";
+
+  vpx_codec_err_t res =
+      vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
+  ASSERT_EQ(res, VPX_CODEC_OK);
+
+  libvpx_test::I420VideoSource video(test_file_name, kWidth, kHeight,
+                                     codec_enc_.g_timebase.den,
+                                     codec_enc_.g_timebase.num, 0, 30);
+  // FRAME 1
+  video.Begin();
+  // this frame is full size, with only one layer
+  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
+                       video.duration(), VPX_DL_REALTIME);
+  ASSERT_EQ(res, VPX_CODEC_OK);
+  EXPECT_EQ(1, svc_is_keyframe(&svc_));
+
+  vpx_codec_err_t res_dec = decoder_->DecodeFrame(
+      (const uint8_t*)svc_get_buffer(&svc_), svc_get_frame_size(&svc_));
+  ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
+
+  // FRAME 2
+  video.Next();
+  // this is an I-frame
+  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
+                       video.duration(), VPX_DL_REALTIME);
+  ASSERT_EQ(res, VPX_CODEC_OK);
+  EXPECT_EQ(1, svc_is_keyframe(&svc_));
+
+  res_dec = decoder_->DecodeFrame((const uint8_t*)svc_get_buffer(&svc_),
+                                  svc_get_frame_size(&svc_));
+  ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
+
+  // FRAME 2
+  video.Next();
+  // this is a P-frame
+  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
+                       video.duration(), VPX_DL_REALTIME);
+  ASSERT_EQ(res, VPX_CODEC_OK);
+  EXPECT_EQ(0, svc_is_keyframe(&svc_));
+
+  res_dec = decoder_->DecodeFrame((const uint8_t*)svc_get_buffer(&svc_),
+                                  svc_get_frame_size(&svc_));
+  ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
+}
+
+TEST_F(SvcTest, GetLayerResolution) {
+  unsigned int layer_width, layer_height;
+
+  svc_.first_frame_full_size = 0;
+  svc_.spatial_layers = 2;
+  svc_.scale_factors = "4/16,8/16";
+  svc_.quantizer_values = "40,30";
+
+  vpx_codec_err_t res =
+      vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
+  EXPECT_EQ(res, VPX_CODEC_OK);
+
+  // ensure that requested layer is a valid layer
+  res = svc_get_layer_resolution(&svc_, svc_.spatial_layers, &layer_width,
+                                 &layer_height);
+  EXPECT_EQ(res, VPX_CODEC_INVALID_PARAM);
+
+  res = svc_get_layer_resolution(&svc_, 0, &layer_width,
+                                 &layer_height);
+  EXPECT_EQ(res, VPX_CODEC_OK);
+  EXPECT_EQ((unsigned int)(kWidth * 4 / 16), layer_width);
+  EXPECT_EQ((unsigned int)(kHeight * 4 / 16), layer_height);
+
+  res = svc_get_layer_resolution(&svc_, 1, &layer_width,
+                                 &layer_height);
+  EXPECT_EQ(res, VPX_CODEC_OK);
+  EXPECT_EQ((unsigned int)(kWidth * 8 / 16), layer_width);
+  EXPECT_EQ((unsigned int)(kHeight * 8 / 16), layer_height);
+}
+
+}  // namespace
diff --git a/test/test.mk b/test/test.mk
index 4a37a2e..aa8af5e 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -96,6 +96,7 @@
 LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += dct16x16_test.cc
 LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += variance_test.cc
 LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += dct32x32_test.cc
+LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += svc_test.cc
 
 endif # VP9
 
diff --git a/vp9/common/vp9_blockd.h b/vp9/common/vp9_blockd.h
index 36e7e83..0dfdd86 100644
--- a/vp9/common/vp9_blockd.h
+++ b/vp9/common/vp9_blockd.h
@@ -206,10 +206,6 @@
   int left_available;
   int right_available;
 
-  // partition contexts
-  PARTITION_CONTEXT *above_seg_context;
-  PARTITION_CONTEXT *left_seg_context;
-
   /* Distance of MB away from frame edges */
   int mb_to_left_edge;
   int mb_to_right_edge;
@@ -222,8 +218,6 @@
 
   struct subpix_fn_table  subpix;
 
-  int allow_high_precision_mv;
-
   int corrupted;
 
   unsigned char sb_index;   // index of 32x32 block inside the 64x64 block
@@ -234,44 +228,7 @@
   int q_index;
 } MACROBLOCKD;
 
-static INLINE void update_partition_context(MACROBLOCKD *xd, BLOCK_SIZE sb_type,
-                                            BLOCK_SIZE sb_size) {
-  const int bsl = b_width_log2(sb_size), bs = (1 << bsl) / 2;
-  const int bwl = b_width_log2(sb_type);
-  const int bhl = b_height_log2(sb_type);
-  const int boffset = b_width_log2(BLOCK_64X64) - bsl;
-  const char pcval0 = ~(0xe << boffset);
-  const char pcval1 = ~(0xf << boffset);
-  const char pcvalue[2] = {pcval0, pcval1};
 
-  assert(MAX(bwl, bhl) <= bsl);
-
-  // update the partition context at the end notes. set partition bits
-  // of block sizes larger than the current one to be one, and partition
-  // bits of smaller block sizes to be zero.
-  vpx_memset(xd->above_seg_context, pcvalue[bwl == bsl], bs);
-  vpx_memset(xd->left_seg_context, pcvalue[bhl == bsl], bs);
-}
-
-static INLINE int partition_plane_context(MACROBLOCKD *xd, BLOCK_SIZE sb_type) {
-  int bsl = mi_width_log2(sb_type), bs = 1 << bsl;
-  int above = 0, left = 0, i;
-  int boffset = mi_width_log2(BLOCK_64X64) - bsl;
-
-  assert(mi_width_log2(sb_type) == mi_height_log2(sb_type));
-  assert(bsl >= 0);
-  assert(boffset >= 0);
-
-  for (i = 0; i < bs; i++)
-    above |= (xd->above_seg_context[i] & (1 << boffset));
-  for (i = 0; i < bs; i++)
-    left |= (xd->left_seg_context[i] & (1 << boffset));
-
-  above = (above > 0);
-  left  = (left > 0);
-
-  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
-}
 
 static BLOCK_SIZE get_subsize(BLOCK_SIZE bsize, PARTITION_TYPE partition) {
   const BLOCK_SIZE subsize = subsize_lookup[partition][bsize];
diff --git a/vp9/common/vp9_common_data.c b/vp9/common/vp9_common_data.c
index dc41efd..f858900 100644
--- a/vp9/common/vp9_common_data.c
+++ b/vp9/common/vp9_common_data.c
@@ -115,6 +115,16 @@
   TX_16X16, TX_16X16, TX_16X16, TX_32X32
 };
 
+const TX_SIZE tx_mode_to_biggest_tx_size[TX_MODES] = {
+  TX_4X4,  // ONLY_4X4
+  TX_8X8,  // ALLOW_8X8
+  TX_16X16,  // ALLOW_16X16
+  TX_32X32,  // ALLOW_32X32
+  TX_32X32,  // TX_MODE_SELECT
+};
+
+
+
 const BLOCK_SIZE ss_size_lookup[BLOCK_SIZES][2][2] = {
 //  ss_x == 0    ss_x == 0        ss_x == 1      ss_x == 1
 //  ss_y == 0    ss_y == 1        ss_y == 0      ss_y == 1
@@ -133,3 +143,4 @@
   {{BLOCK_64X64, BLOCK_64X32},   {BLOCK_32X64,   BLOCK_32X32}},
 };
 
+
diff --git a/vp9/common/vp9_common_data.h b/vp9/common/vp9_common_data.h
index 2945cd2..c1f6405 100644
--- a/vp9/common/vp9_common_data.h
+++ b/vp9/common/vp9_common_data.h
@@ -27,6 +27,7 @@
 extern const BLOCK_SIZE subsize_lookup[PARTITION_TYPES][BLOCK_SIZES];
 extern const TX_SIZE max_txsize_lookup[BLOCK_SIZES];
 extern const TX_SIZE max_uv_txsize_lookup[BLOCK_SIZES];
+extern const TX_SIZE tx_mode_to_biggest_tx_size[TX_MODES];
 extern const BLOCK_SIZE ss_size_lookup[BLOCK_SIZES][2][2];
 
 #endif  // VP9_COMMON_VP9_COMMON_DATA_H
diff --git a/vp9/common/vp9_entropymv.c b/vp9/common/vp9_entropymv.c
index e851181..f70b571 100644
--- a/vp9/common/vp9_entropymv.c
+++ b/vp9/common/vp9_entropymv.c
@@ -175,17 +175,18 @@
   }
 }
 
+void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) {
+  if (counts != NULL) {
+    const MV_JOINT_TYPE j = vp9_get_mv_joint(mv);
+    ++counts->joints[j];
 
-void vp9_inc_mv(const MV *mv,  nmv_context_counts *counts) {
-  const MV_JOINT_TYPE j = vp9_get_mv_joint(mv);
-  ++counts->joints[j];
+    if (mv_joint_vertical(j)) {
+      inc_mv_component(mv->row, &counts->comps[0], 1, 1);
+    }
 
-  if (mv_joint_vertical(j)) {
-    inc_mv_component(mv->row, &counts->comps[0], 1, 1);
-  }
-
-  if (mv_joint_horizontal(j)) {
-    inc_mv_component(mv->col, &counts->comps[1], 1, 1);
+    if (mv_joint_horizontal(j)) {
+      inc_mv_component(mv->col, &counts->comps[1], 1, 1);
+    }
   }
 }
 
diff --git a/vp9/common/vp9_entropymv.h b/vp9/common/vp9_entropymv.h
index c42653d..d843f5b 100644
--- a/vp9/common/vp9_entropymv.h
+++ b/vp9/common/vp9_entropymv.h
@@ -112,7 +112,6 @@
 
 
 typedef struct {
-  unsigned int mvcount[MV_VALS];
   unsigned int sign[2];
   unsigned int classes[MV_CLASSES];
   unsigned int class0[CLASS0_SIZE];
diff --git a/vp9/common/vp9_findnearmv.c b/vp9/common/vp9_findnearmv.c
index ed9e982..592ef6a 100644
--- a/vp9/common/vp9_findnearmv.c
+++ b/vp9/common/vp9_findnearmv.c
@@ -22,14 +22,12 @@
 }
 
 
-void vp9_find_best_ref_mvs(MACROBLOCKD *xd,
-                           int_mv *mvlist,
-                           int_mv *nearest,
-                           int_mv *near) {
+void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
+                           int_mv *mvlist, int_mv *nearest, int_mv *near) {
   int i;
   // Make sure all the candidates are properly clamped etc
   for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
-    lower_mv_precision(&mvlist[i].as_mv, xd->allow_high_precision_mv);
+    lower_mv_precision(&mvlist[i].as_mv, allow_hp);
     clamp_mv2(&mvlist[i].as_mv, xd);
   }
   *nearest = mvlist[0];
diff --git a/vp9/common/vp9_findnearmv.h b/vp9/common/vp9_findnearmv.h
index 50dfdc7..d161d1b 100644
--- a/vp9/common/vp9_findnearmv.h
+++ b/vp9/common/vp9_findnearmv.h
@@ -23,10 +23,8 @@
 // check a list of motion vectors by sad score using a number rows of pixels
 // above and a number cols of pixels in the left to select the one with best
 // score to use as ref motion vector
-void vp9_find_best_ref_mvs(MACROBLOCKD *xd,
-                           int_mv *mvlist,
-                           int_mv *nearest,
-                           int_mv *near);
+void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
+                           int_mv *mvlist, int_mv *nearest, int_mv *near);
 
 // TODO(jingning): this mv clamping function should be block size dependent.
 static void clamp_mv2(MV *mv, const MACROBLOCKD *xd) {
@@ -43,26 +41,24 @@
                                    int block_idx, int ref_idx,
                                    int mi_row, int mi_col);
 
-static MB_PREDICTION_MODE left_block_mode(const MODE_INFO *cur_mb,
-                                          const MODE_INFO *left_mb, int b) {
+static MB_PREDICTION_MODE left_block_mode(const MODE_INFO *cur_mi,
+                                          const MODE_INFO *left_mi, int b) {
   // FIXME(rbultje, jingning): temporary hack because jenkins doesn't
   // understand this condition. This will go away soon.
-  const MODE_INFO *mi = cur_mb;
 
   if (b == 0 || b == 2) {
     /* On L edge, get from MB to left of us */
-    mi = left_mb;
-    if (!mi)
+    if (!left_mi)
       return DC_PRED;
 
-    if (is_inter_block(&mi->mbmi))
+    if (is_inter_block(&left_mi->mbmi))
       return DC_PRED;
     else
-      return mi->mbmi.sb_type < BLOCK_8X8 ? (mi->bmi + 1 + b)->as_mode
-                                          : mi->mbmi.mode;
+      return left_mi->mbmi.sb_type < BLOCK_8X8 ? left_mi->bmi[b + 1].as_mode
+                                               : left_mi->mbmi.mode;
   }
   assert(b == 1 || b == 3);
-  return (mi->bmi + b - 1)->as_mode;
+  return cur_mi->bmi[b - 1].as_mode;
 }
 
 static MB_PREDICTION_MODE above_block_mode(const MODE_INFO *cur_mb,
diff --git a/vp9/common/vp9_onyx.h b/vp9/common/vp9_onyx.h
index acb4724..452dd6b 100644
--- a/vp9/common/vp9_onyx.h
+++ b/vp9/common/vp9_onyx.h
@@ -221,8 +221,6 @@
   int vp9_set_size_literal(VP9_PTR comp, unsigned int width,
                            unsigned int height);
 
-  int vp9_switch_layer(VP9_PTR comp, int layer);
-
   void vp9_set_svc(VP9_PTR comp, int use_svc);
 
   int vp9_get_quantizer(VP9_PTR c);
diff --git a/vp9/common/vp9_onyxc_int.h b/vp9/common/vp9_onyxc_int.h
index 3111852..bc6535d 100644
--- a/vp9/common/vp9_onyxc_int.h
+++ b/vp9/common/vp9_onyxc_int.h
@@ -129,6 +129,8 @@
   // Flag signaling that the frame is encoded using only INTRA modes.
   int intra_only;
 
+  int allow_high_precision_mv;
+
   // Flag signaling that the frame context should be reset to default values.
   // 0 or 1 implies don't reset, 2 reset just the context specified in the
   // frame header, 3 reset all contexts.
@@ -251,12 +253,6 @@
   }
 }
 
-static INLINE void set_partition_seg_context(VP9_COMMON *cm, MACROBLOCKD *xd,
-                                             int mi_row, int mi_col) {
-  xd->above_seg_context = cm->above_seg_context + mi_col;
-  xd->left_seg_context = cm->left_seg_context + (mi_row & MI_MASK);
-}
-
 // return the node index in the prob tree for binary coding
 static int check_bsize_coverage(int bs, int mi_rows, int mi_cols,
                                 int mi_row, int mi_col) {
@@ -305,4 +301,53 @@
   return cm->frame_type == KEY_FRAME || cm->intra_only;
 }
 
+static INLINE void update_partition_context(VP9_COMMON *cm,
+                                            int mi_row, int mi_col,
+                                            BLOCK_SIZE sb_type,
+                                            BLOCK_SIZE sb_size) {
+  PARTITION_CONTEXT *above_ctx = cm->above_seg_context + mi_col;
+  PARTITION_CONTEXT *left_ctx = cm->left_seg_context + (mi_row & MI_MASK);
+
+  const int bsl = b_width_log2(sb_size), bs = (1 << bsl) / 2;
+  const int bwl = b_width_log2(sb_type);
+  const int bhl = b_height_log2(sb_type);
+  const int boffset = b_width_log2(BLOCK_64X64) - bsl;
+  const char pcval0 = ~(0xe << boffset);
+  const char pcval1 = ~(0xf << boffset);
+  const char pcvalue[2] = {pcval0, pcval1};
+
+  assert(MAX(bwl, bhl) <= bsl);
+
+  // update the partition context at the end notes. set partition bits
+  // of block sizes larger than the current one to be one, and partition
+  // bits of smaller block sizes to be zero.
+  vpx_memset(above_ctx, pcvalue[bwl == bsl], bs);
+  vpx_memset(left_ctx, pcvalue[bhl == bsl], bs);
+}
+
+static INLINE int partition_plane_context(const VP9_COMMON *cm,
+                                          int mi_row, int mi_col,
+                                          BLOCK_SIZE sb_type) {
+  const PARTITION_CONTEXT *above_ctx = cm->above_seg_context + mi_col;
+  const PARTITION_CONTEXT *left_ctx = cm->left_seg_context + (mi_row & MI_MASK);
+
+  int bsl = mi_width_log2(sb_type), bs = 1 << bsl;
+  int above = 0, left = 0, i;
+  int boffset = mi_width_log2(BLOCK_64X64) - bsl;
+
+  assert(mi_width_log2(sb_type) == mi_height_log2(sb_type));
+  assert(bsl >= 0);
+  assert(boffset >= 0);
+
+  for (i = 0; i < bs; i++)
+    above |= (above_ctx[i] & (1 << boffset));
+  for (i = 0; i < bs; i++)
+    left |= (left_ctx[i] & (1 << boffset));
+
+  above = (above > 0);
+  left  = (left > 0);
+
+  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
+}
+
 #endif  // VP9_COMMON_VP9_ONYXC_INT_H_
diff --git a/vp9/common/vp9_pred_common.c b/vp9/common/vp9_pred_common.c
index 9453d02..be42c56 100644
--- a/vp9/common/vp9_pred_common.c
+++ b/vp9/common/vp9_pred_common.c
@@ -16,12 +16,20 @@
 #include "vp9/common/vp9_seg_common.h"
 #include "vp9/common/vp9_treecoder.h"
 
+static INLINE const MB_MODE_INFO *get_above_mbmi(const MODE_INFO *const above) {
+  return (above != NULL) ? &above->mbmi : NULL;
+}
+
+static INLINE const MB_MODE_INFO *get_left_mbmi(const MODE_INFO *const left) {
+  return (left != NULL) ? &left->mbmi : NULL;
+}
+
 // Returns a context number for the given MB prediction signal
 unsigned char vp9_get_pred_context_switchable_interp(const MACROBLOCKD *xd) {
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const int left_in_image = xd->left_available && left_mi;
-  const int above_in_image = xd->up_available && above_mi;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const int above_in_image = above_mi != NULL;
+  const int left_in_image = left_mi != NULL;
   // Note:
   // The mode info data structure has a one element border above and to the
   // left of the entries correpsonding to real macroblocks.
@@ -53,14 +61,14 @@
 }
 // Returns a context number for the given MB prediction signal
 unsigned char vp9_get_pred_context_intra_inter(const MACROBLOCKD *xd) {
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const MB_MODE_INFO *const above_mbmi = above_mi ? &above_mi->mbmi : 0;
-  const MB_MODE_INFO *const left_mbmi = left_mi ? &left_mi->mbmi : 0;
-  const int left_in_image = xd->left_available && left_mi;
-  const int above_in_image = xd->up_available && above_mi;
-  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
+  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
+  const int above_in_image = above_mi != NULL;
+  const int left_in_image = left_mi != NULL;
   const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
+  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
 
   // The mode info data structure has a one element border above and to the
   // left of the entries corresponding to real macroblocks.
@@ -81,12 +89,12 @@
 unsigned char vp9_get_pred_context_comp_inter_inter(const VP9_COMMON *cm,
                                                     const MACROBLOCKD *xd) {
   int pred_context;
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const MB_MODE_INFO *const above_mbmi = above_mi ? &above_mi->mbmi : 0;
-  const MB_MODE_INFO *const left_mbmi = left_mi ? &left_mi->mbmi : 0;
-  const int left_in_image = xd->left_available && left_mi;
-  const int above_in_image = xd->up_available && above_mi;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
+  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
+  const int above_in_image = above_mi != NULL;
+  const int left_in_image = left_mi != NULL;
   // Note:
   // The mode info data structure has a one element border above and to the
   // left of the entries correpsonding to real macroblocks.
@@ -126,14 +134,14 @@
 unsigned char vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm,
                                               const MACROBLOCKD *xd) {
   int pred_context;
-  const MODE_INFO * const above_mi = xd->mi_8x8[-cm->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const MB_MODE_INFO *const above_mbmi = above_mi ? &above_mi->mbmi : 0;
-  const MB_MODE_INFO *const left_mbmi = left_mi ? &left_mi->mbmi : 0;
-  const int left_in_image = xd->left_available && left_mi;
-  const int above_in_image = xd->up_available && above_mi;
-  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
+  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
+  const int above_in_image = above_mi != NULL;
+  const int left_in_image = left_mi != NULL;
   const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
+  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
   // Note:
   // The mode info data structure has a one element border above and to the
   // left of the entries correpsonding to real macroblocks.
@@ -206,14 +214,14 @@
 }
 unsigned char vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) {
   int pred_context;
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const MB_MODE_INFO *const above_mbmi = above_mi ? &above_mi->mbmi : 0;
-  const MB_MODE_INFO *const left_mbmi = left_mi ? &left_mi->mbmi : 0;
-  const int left_in_image = xd->left_available && left_mi;
-  const int above_in_image = xd->up_available && above_mi;
-  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
+  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
+  const int above_in_image = above_mi != NULL;
+  const int left_in_image = left_mi != NULL;
   const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
+  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
   // Note:
   // The mode info data structure has a one element border above and to the
   // left of the entries correpsonding to real macroblocks.
@@ -272,14 +280,14 @@
 
 unsigned char vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) {
   int pred_context;
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const MB_MODE_INFO *const above_mbmi = above_mi ? &above_mi->mbmi : 0;
-  const MB_MODE_INFO *const left_mbmi = left_mi ? &left_mi->mbmi : 0;
-  const int left_in_image = xd->left_available && left_mi;
-  const int above_in_image = xd->up_available && above_mi;
-  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
+  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
+  const int above_in_image = above_mi != NULL;
+  const int left_in_image = left_mi != NULL;
   const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
+  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
 
   // Note:
   // The mode info data structure has a one element border above and to the
@@ -361,12 +369,12 @@
 // left of the entries corresponding to real blocks.
 // The prediction flags in these dummy entries are initialized to 0.
 unsigned char vp9_get_pred_context_tx_size(const MACROBLOCKD *xd) {
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const MB_MODE_INFO *const above_mbmi = above_mi ? &above_mi->mbmi : 0;
-  const MB_MODE_INFO *const left_mbmi = left_mi ? &left_mi->mbmi : 0;
-  const int left_in_image = xd->left_available && left_mi;
-  const int above_in_image = xd->up_available && above_mi;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
+  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
+  const int above_in_image = above_mi != NULL;
+  const int left_in_image = left_mi != NULL;
   const int max_tx_size = max_txsize_lookup[xd->mi_8x8[0]->mbmi.sb_type];
   int above_context = max_tx_size;
   int left_context = max_tx_size;
diff --git a/vp9/common/vp9_pred_common.h b/vp9/common/vp9_pred_common.h
index 1fdd4da..a869dc0 100644
--- a/vp9/common/vp9_pred_common.h
+++ b/vp9/common/vp9_pred_common.h
@@ -14,17 +14,25 @@
 #include "vp9/common/vp9_blockd.h"
 #include "vp9/common/vp9_onyxc_int.h"
 
+static INLINE const MODE_INFO *get_above_mi(const MACROBLOCKD *const xd) {
+  return xd->up_available ? xd->mi_8x8[-xd->mode_info_stride] : NULL;
+}
+
+static INLINE const MODE_INFO *get_left_mi(const MACROBLOCKD *const xd) {
+  return xd->left_available ? xd->mi_8x8[-1] : NULL;
+}
+
 int vp9_get_segment_id(VP9_COMMON *cm, const uint8_t *segment_ids,
                        BLOCK_SIZE bsize, int mi_row, int mi_col);
 
-
 static INLINE int vp9_get_pred_context_seg_id(const MACROBLOCKD *xd) {
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const int above_sip = above_mi ? above_mi->mbmi.seg_id_predicted : 0;
-  const int left_sip = left_mi ? left_mi->mbmi.seg_id_predicted : 0;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const int above_sip = (above_mi != NULL) ?
+                        above_mi->mbmi.seg_id_predicted : 0;
+  const int left_sip = (left_mi != NULL) ? left_mi->mbmi.seg_id_predicted : 0;
 
-  return above_sip + (xd->left_available ? left_sip : 0);
+  return above_sip + left_sip;
 }
 
 static INLINE vp9_prob vp9_get_pred_prob_seg_id(struct segmentation *seg,
@@ -35,12 +43,13 @@
 void vp9_set_pred_flag_seg_id(MACROBLOCKD *xd, uint8_t pred_flag);
 
 static INLINE int vp9_get_pred_context_mbskip(const MACROBLOCKD *xd) {
-  const MODE_INFO * const above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO * const left_mi = xd->mi_8x8[-1];
-  const int above_skip_coeff = above_mi ? above_mi->mbmi.skip_coeff : 0;
-  const int left_skip_coeff = left_mi ? left_mi->mbmi.skip_coeff : 0;
+  const MODE_INFO *const above_mi = get_above_mi(xd);
+  const MODE_INFO *const left_mi = get_left_mi(xd);
+  const int above_skip_coeff = (above_mi != NULL) ?
+                               above_mi->mbmi.skip_coeff : 0;
+  const int left_skip_coeff = (left_mi != NULL) ? left_mi->mbmi.skip_coeff : 0;
 
-  return above_skip_coeff + (xd->left_available ? left_skip_coeff : 0);
+  return above_skip_coeff + left_skip_coeff;
 }
 
 static INLINE vp9_prob vp9_get_pred_prob_mbskip(const VP9_COMMON *cm,
diff --git a/vp9/common/vp9_rtcd_defs.sh b/vp9/common/vp9_rtcd_defs.sh
index cc88c77..c34debb 100644
--- a/vp9/common/vp9_rtcd_defs.sh
+++ b/vp9/common/vp9_rtcd_defs.sh
@@ -707,7 +707,7 @@
 prototype void vp9_short_fdct32x32_rd "int16_t *InputData, int16_t *OutputData, int stride"
 specialize vp9_short_fdct32x32_rd sse2
 
-prototype void vp9_short_fdct16x16 "int16_t *InputData, int16_t *OutputData, int pitch"
+prototype void vp9_short_fdct16x16 "int16_t *InputData, int16_t *OutputData, int stride"
 specialize vp9_short_fdct16x16 sse2
 
 prototype void vp9_short_walsh4x4 "int16_t *InputData, int16_t *OutputData, int pitch"
diff --git a/vp9/common/x86/vp9_intrapred_ssse3.asm b/vp9/common/x86/vp9_intrapred_ssse3.asm
index 314d1a2..568e208 100644
--- a/vp9/common/x86/vp9_intrapred_ssse3.asm
+++ b/vp9/common/x86/vp9_intrapred_ssse3.asm
@@ -790,9 +790,8 @@
   RET
 
 INIT_MMX ssse3
-cglobal d207_predictor_4x4, 2, 5, 4, dst, stride, unused, left, goffset
+cglobal d207_predictor_4x4, 4, 5, 4, dst, stride, unused, left, goffset
   GET_GOT     goffsetq
-  movifnidn        leftq, leftmp
   movd                m0, [leftq]                ; abcd [byte]
   pshufb              m1, m0, [GLOBAL(sh_b1233)] ; bcdd [byte]
   pshufb              m3, m0, [GLOBAL(sh_b2333)] ; cddd
@@ -813,9 +812,8 @@
   RET
 
 INIT_XMM ssse3
-cglobal d207_predictor_8x8, 2, 5, 4, dst, stride, stride3, left, goffset
+cglobal d207_predictor_8x8, 4, 5, 4, dst, stride, stride3, left, goffset
   GET_GOT     goffsetq
-  movifnidn        leftq, leftmp
   movq                m3, [leftq]            ; abcdefgh [byte]
   lea           stride3q, [strideq*3]
 
@@ -848,10 +846,9 @@
   RET
 
 INIT_XMM ssse3
-cglobal d207_predictor_16x16, 2, 5, 5, dst, stride, stride3, left, goffset
+cglobal d207_predictor_16x16, 4, 5, 5, dst, stride, stride3, left, goffset
   GET_GOT     goffsetq
   lea           stride3q, [strideq*3]
-  movifnidn        leftq, leftmp
   mova                m0, [leftq]            ; abcdefghijklmnop [byte]
   pshufb              m1, m0, [GLOBAL(sh_b123456789abcdeff)] ; bcdefghijklmnopp
   pshufb              m2, m0, [GLOBAL(sh_b23456789abcdefff)]
@@ -896,10 +893,9 @@
   REP_RET
 
 INIT_XMM ssse3
-cglobal d207_predictor_32x32, 2, 5, 8, dst, stride, stride3, left, goffset
+cglobal d207_predictor_32x32, 4, 5, 8, dst, stride, stride3, left, goffset
   GET_GOT     goffsetq
   lea           stride3q, [strideq*3]
-  movifnidn        leftq, leftmp
   mova                m1, [leftq]              ;  0-15 [byte]
   mova                m2, [leftq+16]           ; 16-31 [byte]
   pshufb              m0, m2, [GLOBAL(sh_b23456789abcdefff)]
diff --git a/vp9/decoder/vp9_decodemv.c b/vp9/decoder/vp9_decodemv.c
index 6cf4f15..1674e67 100644
--- a/vp9/decoder/vp9_decodemv.c
+++ b/vp9/decoder/vp9_decodemv.c
@@ -34,7 +34,8 @@
                                             int size_group) {
   const MB_PREDICTION_MODE y_mode = read_intra_mode(r,
                                         cm->fc.y_mode_prob[size_group]);
-  ++cm->counts.y_mode[size_group][y_mode];
+  if (!cm->frame_parallel_decoding_mode)
+    ++cm->counts.y_mode[size_group][y_mode];
   return y_mode;
 }
 
@@ -42,7 +43,8 @@
                                              MB_PREDICTION_MODE y_mode) {
   const MB_PREDICTION_MODE uv_mode = read_intra_mode(r,
                                          cm->fc.uv_mode_prob[y_mode]);
-  ++cm->counts.uv_mode[y_mode][uv_mode];
+  if (!cm->frame_parallel_decoding_mode)
+    ++cm->counts.uv_mode[y_mode][uv_mode];
   return uv_mode;
 }
 
@@ -50,7 +52,8 @@
                                           uint8_t context) {
   const MB_PREDICTION_MODE mode = treed_read(r, vp9_inter_mode_tree,
                                              cm->fc.inter_mode_probs[context]);
-  ++cm->counts.inter_mode[context][inter_mode_offset(mode)];
+  if (!cm->frame_parallel_decoding_mode)
+    ++cm->counts.inter_mode[context][inter_mode_offset(mode)];
   return mode;
 }
 
@@ -69,26 +72,21 @@
       tx_size += vp9_read(r, tx_probs[2]);
   }
 
-  update_tx_counts(bsize, context, tx_size, &cm->counts.tx);
+  if (!cm->frame_parallel_decoding_mode)
+    update_tx_counts(bsize, context, tx_size, &cm->counts.tx);
   return tx_size;
 }
 
-static TX_SIZE read_tx_size(VP9D_COMP *pbi, TX_MODE tx_mode,
-                            BLOCK_SIZE bsize, int allow_select,
+static TX_SIZE read_tx_size(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                            TX_MODE tx_mode, BLOCK_SIZE bsize, int allow_select,
                             vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
-
-  if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
+  if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8) {
     return read_selected_tx_size(cm, xd, bsize, r);
-  else if (tx_mode >= ALLOW_32X32 && bsize >= BLOCK_32X32)
-    return TX_32X32;
-  else if (tx_mode >= ALLOW_16X16 && bsize >= BLOCK_16X16)
-    return TX_16X16;
-  else if (tx_mode >= ALLOW_8X8 && bsize >= BLOCK_8X8)
-    return TX_8X8;
-  else
-    return TX_4X4;
+  } else {
+    const TX_SIZE max_tx_size_block = max_txsize_lookup[bsize];
+    const TX_SIZE max_tx_size_txmode = tx_mode_to_biggest_tx_size[tx_mode];
+    return MIN(max_tx_size_block, max_tx_size_txmode);
+  }
 }
 
 static void set_segment_id(VP9_COMMON *cm, BLOCK_SIZE bsize,
@@ -107,10 +105,10 @@
       cm->last_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
 }
 
-static int read_intra_segment_id(VP9D_COMP *pbi, int mi_row, int mi_col,
+static int read_intra_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                                 int mi_row, int mi_col,
                                  vp9_reader *r) {
-  MACROBLOCKD *const xd = &pbi->mb;
-  struct segmentation *const seg = &pbi->common.seg;
+  struct segmentation *const seg = &cm->seg;
   const BLOCK_SIZE bsize = xd->mi_8x8[0]->mbmi.sb_type;
   int segment_id;
 
@@ -121,14 +119,12 @@
     return 0;
 
   segment_id = read_segment_id(r, seg);
-  set_segment_id(&pbi->common, bsize, mi_row, mi_col, segment_id);
+  set_segment_id(cm, bsize, mi_row, mi_col, segment_id);
   return segment_id;
 }
 
-static int read_inter_segment_id(VP9D_COMP *pbi, int mi_row, int mi_col,
-                                 vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
+static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                                 int mi_row, int mi_col, vp9_reader *r) {
   struct segmentation *const seg = &cm->seg;
   const BLOCK_SIZE bsize = xd->mi_8x8[0]->mbmi.sb_type;
   int pred_segment_id, segment_id;
@@ -154,37 +150,36 @@
   return segment_id;
 }
 
-static uint8_t read_skip_coeff(VP9D_COMP *pbi, int segment_id, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
+static uint8_t read_skip_coeff(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                               int segment_id, vp9_reader *r) {
   int skip_coeff = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
   if (!skip_coeff) {
     const int ctx = vp9_get_pred_context_mbskip(xd);
     skip_coeff = vp9_read(r, vp9_get_pred_prob_mbskip(cm, xd));
-    cm->counts.mbskip[ctx][skip_coeff]++;
+    if (!cm->frame_parallel_decoding_mode)
+      ++cm->counts.mbskip[ctx][skip_coeff];
   }
   return skip_coeff;
 }
 
-static void read_intra_frame_mode_info(VP9D_COMP *pbi, MODE_INFO *m,
+static void read_intra_frame_mode_info(VP9_COMMON *const cm,
+                                       MACROBLOCKD *const xd,
+                                       MODE_INFO *const m,
                                        int mi_row, int mi_col, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
   MB_MODE_INFO *const mbmi = &m->mbmi;
   const BLOCK_SIZE bsize = mbmi->sb_type;
   const MODE_INFO *above_mi = xd->mi_8x8[-cm->mode_info_stride];
-  const MODE_INFO *left_mi = xd->mi_8x8[-1];
+  const MODE_INFO *left_mi  = xd->left_available ? xd->mi_8x8[-1] : NULL;
 
-  mbmi->segment_id = read_intra_segment_id(pbi, mi_row, mi_col, r);
-  mbmi->skip_coeff = read_skip_coeff(pbi, mbmi->segment_id, r);
-  mbmi->tx_size = read_tx_size(pbi, cm->tx_mode, bsize, 1, r);
+  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_row, mi_col, r);
+  mbmi->skip_coeff = read_skip_coeff(cm, xd, mbmi->segment_id, r);
+  mbmi->tx_size = read_tx_size(cm, xd, cm->tx_mode, bsize, 1, r);
   mbmi->ref_frame[0] = INTRA_FRAME;
   mbmi->ref_frame[1] = NONE;
 
   if (bsize >= BLOCK_8X8) {
     const MB_PREDICTION_MODE A = above_block_mode(m, above_mi, 0);
-    const MB_PREDICTION_MODE L = xd->left_available ?
-                                  left_block_mode(m, left_mi, 0) : DC_PRED;
+    const MB_PREDICTION_MODE L = left_block_mode(m, left_mi, 0);
     mbmi->mode = read_intra_mode(r, vp9_kf_y_mode_prob[A][L]);
   } else {
     // Only 4x4, 4x8, 8x4 blocks
@@ -196,8 +191,7 @@
       for (idx = 0; idx < 2; idx += num_4x4_w) {
         const int ib = idy * 2 + idx;
         const MB_PREDICTION_MODE A = above_block_mode(m, above_mi, ib);
-        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
-                                     left_block_mode(m, left_mi, ib) : DC_PRED;
+        const MB_PREDICTION_MODE L = left_block_mode(m, left_mi, ib);
         const MB_PREDICTION_MODE b_mode = read_intra_mode(r,
                                               vp9_kf_y_mode_prob[A][L]);
         m->bmi[ib].as_mode = b_mode;
@@ -312,10 +306,9 @@
 }
 
 // Read the referncence frame
-static void read_ref_frames(VP9D_COMP *pbi, vp9_reader *r,
+static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                            vp9_reader *r,
                             int segment_id, MV_REFERENCE_FRAME ref_frame[2]) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
   FRAME_CONTEXT *const fc = &cm->fc;
   FRAME_COUNTS *const counts = &cm->counts;
 
@@ -328,7 +321,8 @@
 
     if (cm->comp_pred_mode == HYBRID_PREDICTION) {
       is_comp = vp9_read(r, fc->comp_inter_prob[comp_ctx]);
-      counts->comp_inter[comp_ctx][is_comp]++;
+      if (!cm->frame_parallel_decoding_mode)
+        ++counts->comp_inter[comp_ctx][is_comp];
     } else {
       is_comp = cm->comp_pred_mode == COMP_PREDICTION_ONLY;
     }
@@ -338,18 +332,21 @@
       const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
       const int ref_ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
       const int b = vp9_read(r, fc->comp_ref_prob[ref_ctx]);
-      counts->comp_ref[ref_ctx][b]++;
+      if (!cm->frame_parallel_decoding_mode)
+        ++counts->comp_ref[ref_ctx][b];
       ref_frame[fix_ref_idx] = cm->comp_fixed_ref;
       ref_frame[!fix_ref_idx] = cm->comp_var_ref[b];
     } else {
       const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
       const int bit0 = vp9_read(r, fc->single_ref_prob[ctx0][0]);
-      ++counts->single_ref[ctx0][0][bit0];
+      if (!cm->frame_parallel_decoding_mode)
+        ++counts->single_ref[ctx0][0][bit0];
       if (bit0) {
         const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
         const int bit1 = vp9_read(r, fc->single_ref_prob[ctx1][1]);
         ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
-        ++counts->single_ref[ctx1][1][bit1];
+        if (!cm->frame_parallel_decoding_mode)
+          ++counts->single_ref[ctx1][1][bit1];
       } else {
         ref_frame[0] = LAST_FRAME;
       }
@@ -381,19 +378,17 @@
 }
 
 static INLINE INTERPOLATIONFILTERTYPE read_switchable_filter_type(
-    VP9D_COMP *pbi, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
+    VP9_COMMON *const cm, MACROBLOCKD *const xd, vp9_reader *r) {
   const int ctx = vp9_get_pred_context_switchable_interp(xd);
   const int type = treed_read(r, vp9_switchable_interp_tree,
                               cm->fc.switchable_interp_prob[ctx]);
-  ++cm->counts.switchable_interp[ctx][type];
+  if (!cm->frame_parallel_decoding_mode)
+    ++cm->counts.switchable_interp[ctx][type];
   return type;
 }
 
-static void read_intra_block_mode_info(VP9D_COMP *pbi, MODE_INFO *mi,
-                                  vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
+static void read_intra_block_mode_info(VP9_COMMON *const cm, MODE_INFO *mi,
+                                       vp9_reader *r) {
   MB_MODE_INFO *const mbmi = &mi->mbmi;
   const BLOCK_SIZE bsize = mi->mbmi.sb_type;
 
@@ -425,67 +420,71 @@
   mbmi->uv_mode = read_intra_mode_uv(cm, r, mbmi->mode);
 }
 
-static INLINE void assign_mv(VP9_COMMON *cm, MB_PREDICTION_MODE mode,
+static INLINE int assign_mv(VP9_COMMON *cm, MB_PREDICTION_MODE mode,
                              int_mv mv[2], int_mv best_mv[2],
                              int_mv nearest_mv[2], int_mv near_mv[2],
                              int is_compound, int allow_hp, vp9_reader *r) {
   int i;
+  int ret = 1;
 
   switch (mode) {
-    case NEWMV:
-       read_mv(r, &mv[0].as_mv, &best_mv[0].as_mv,
-               &cm->fc.nmvc, &cm->counts.mv, allow_hp);
-       if (is_compound)
-         read_mv(r, &mv[1].as_mv, &best_mv[1].as_mv,
-                 &cm->fc.nmvc, &cm->counts.mv, allow_hp);
-       break;
-    case NEARESTMV:
+    case NEWMV: {
+      nmv_context_counts *const mv_counts = cm->frame_parallel_decoding_mode ?
+                                            NULL : &cm->counts.mv;
+      read_mv(r, &mv[0].as_mv, &best_mv[0].as_mv,
+              &cm->fc.nmvc, mv_counts, allow_hp);
+      if (is_compound)
+        read_mv(r, &mv[1].as_mv, &best_mv[1].as_mv,
+                &cm->fc.nmvc, mv_counts, allow_hp);
+      for (i = 0; i < 1 + is_compound; ++i) {
+        ret = ret && mv[i].as_mv.row < MV_UPP && mv[i].as_mv.row > MV_LOW;
+        ret = ret && mv[i].as_mv.col < MV_UPP && mv[i].as_mv.col > MV_LOW;
+      }
+      break;
+    }
+    case NEARESTMV: {
       mv[0].as_int = nearest_mv[0].as_int;
-      if (is_compound)
-        mv[1].as_int = nearest_mv[1].as_int;
+      if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
       break;
-    case NEARMV:
+    }
+    case NEARMV: {
       mv[0].as_int = near_mv[0].as_int;
-      if (is_compound)
-        mv[1].as_int = near_mv[1].as_int;
+      if (is_compound) mv[1].as_int = near_mv[1].as_int;
       break;
-    case ZEROMV:
+    }
+    case ZEROMV: {
       mv[0].as_int = 0;
-      if (is_compound)
-        mv[1].as_int = 0;
+      if (is_compound) mv[1].as_int = 0;
       break;
-    default:
-      assert(!"Invalid inter mode value.");
+    }
+    default: {
+      return 0;
+    }
   }
-
-  for (i = 0; i < 1 + is_compound; ++i) {
-    assert(mv[i].as_mv.row < MV_UPP && mv[i].as_mv.row > MV_LOW);
-    assert(mv[i].as_mv.col < MV_UPP && mv[i].as_mv.col > MV_LOW);
-  }
+  return ret;
 }
 
-static int read_is_inter_block(VP9D_COMP *pbi, int segment_id, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
-
+static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                               int segment_id, vp9_reader *r) {
   if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
     return vp9_get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) !=
            INTRA_FRAME;
   } else {
     const int ctx = vp9_get_pred_context_intra_inter(xd);
     const int is_inter = vp9_read(r, vp9_get_pred_prob_intra_inter(cm, xd));
-    ++cm->counts.intra_inter[ctx][is_inter];
+    if (!cm->frame_parallel_decoding_mode)
+      ++cm->counts.intra_inter[ctx][is_inter];
     return is_inter;
   }
 }
 
-static void read_inter_block_mode_info(VP9D_COMP *pbi, MODE_INFO *mi,
+static void read_inter_block_mode_info(VP9_COMMON *const cm,
+                                       MACROBLOCKD *const xd,
+                                       MODE_INFO *const mi,
                                        int mi_row, int mi_col, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
   MB_MODE_INFO *const mbmi = &mi->mbmi;
   const BLOCK_SIZE bsize = mbmi->sb_type;
-  const int allow_hp = xd->allow_high_precision_mv;
+  const int allow_hp = cm->allow_high_precision_mv;
 
   int_mv nearest[2], nearmv[2], best[2];
   uint8_t inter_mode_ctx;
@@ -493,7 +492,7 @@
   int is_compound;
 
   mbmi->uv_mode = DC_PRED;
-  read_ref_frames(pbi, r, mbmi->segment_id, mbmi->ref_frame);
+  read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
   ref0 = mbmi->ref_frame[0];
   is_compound = has_second_ref(mbmi);
 
@@ -516,7 +515,8 @@
 
   // nearest, nearby
   if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
-    vp9_find_best_ref_mvs(xd, mbmi->ref_mvs[ref0], &nearest[0], &nearmv[0]);
+    vp9_find_best_ref_mvs(xd, allow_hp,
+                          mbmi->ref_mvs[ref0], &nearest[0], &nearmv[0]);
     best[0].as_int = nearest[0].as_int;
   }
 
@@ -526,14 +526,15 @@
                      ref1, mbmi->ref_mvs[ref1], mi_row, mi_col);
 
     if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
-      vp9_find_best_ref_mvs(xd, mbmi->ref_mvs[ref1], &nearest[1], &nearmv[1]);
+      vp9_find_best_ref_mvs(xd, allow_hp,
+                            mbmi->ref_mvs[ref1], &nearest[1], &nearmv[1]);
       best[1].as_int = nearest[1].as_int;
     }
   }
 
-  mbmi->interp_filter = cm->mcomp_filter_type == SWITCHABLE
-                              ? read_switchable_filter_type(pbi, r)
-                              : cm->mcomp_filter_type;
+  mbmi->interp_filter = (cm->mcomp_filter_type == SWITCHABLE)
+                      ? read_switchable_filter_type(cm, xd, r)
+                      : cm->mcomp_filter_type;
 
   if (bsize < BLOCK_8X8) {
     const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];  // 1 or 2
@@ -557,8 +558,12 @@
                                           mi_row, mi_col);
         }
 
-        assign_mv(cm, b_mode, block, best, nearest, nearmv,
-                  is_compound, allow_hp, r);
+        if (!assign_mv(cm, b_mode, block, best, nearest, nearmv,
+                       is_compound, allow_hp, r)) {
+          xd->corrupted |= 1;
+          break;
+        };
+
 
         mi->bmi[j].as_mv[0].as_int = block[0].as_int;
         if (is_compound)
@@ -576,29 +581,31 @@
     mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
     mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
   } else {
-    assign_mv(cm, mbmi->mode, mbmi->mv, best, nearest, nearmv,
-              is_compound, allow_hp, r);
+    xd->corrupted |= !assign_mv(cm, mbmi->mode, mbmi->mv,
+                                best, nearest, nearmv,
+                                is_compound, allow_hp, r);
   }
 }
 
-static void read_inter_frame_mode_info(VP9D_COMP *pbi, MODE_INFO *mi,
+static void read_inter_frame_mode_info(VP9_COMMON *const cm,
+                                       MACROBLOCKD *const xd,
+                                       MODE_INFO *const mi,
                                        int mi_row, int mi_col, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
   MB_MODE_INFO *const mbmi = &mi->mbmi;
   int inter_block;
 
   mbmi->mv[0].as_int = 0;
   mbmi->mv[1].as_int = 0;
-  mbmi->segment_id = read_inter_segment_id(pbi, mi_row, mi_col, r);
-  mbmi->skip_coeff = read_skip_coeff(pbi, mbmi->segment_id, r);
-  inter_block = read_is_inter_block(pbi, mbmi->segment_id, r);
-  mbmi->tx_size = read_tx_size(pbi, cm->tx_mode, mbmi->sb_type,
+  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
+  mbmi->skip_coeff = read_skip_coeff(cm, xd, mbmi->segment_id, r);
+  inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
+  mbmi->tx_size = read_tx_size(cm, xd, cm->tx_mode, mbmi->sb_type,
                                !mbmi->skip_coeff || !inter_block, r);
 
   if (inter_block)
-    read_inter_block_mode_info(pbi, mi, mi_row, mi_col, r);
+    read_inter_block_mode_info(cm, xd, mi, mi_row, mi_col, r);
   else
-    read_intra_block_mode_info(pbi, mi, r);
+    read_intra_block_mode_info(cm, mi, r);
 }
 
 static void read_comp_pred(VP9_COMMON *cm, vp9_reader *r) {
@@ -622,8 +629,7 @@
       vp9_diff_update_prob(r, &cm->fc.comp_ref_prob[i]);
 }
 
-void vp9_prepare_read_mode_info(VP9D_COMP* pbi, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
+void vp9_prepare_read_mode_info(VP9_COMMON *cm, vp9_reader *r) {
   int k;
 
   // TODO(jkoleszar): does this clear more than MBSKIP_CONTEXTS? Maybe remove.
@@ -632,8 +638,7 @@
     vp9_diff_update_prob(r, &cm->fc.mbskip_probs[k]);
 
   if (!frame_is_intra_only(cm)) {
-    nmv_context *const nmvc = &pbi->common.fc.nmvc;
-    MACROBLOCKD *const xd = &pbi->mb;
+    nmv_context *const nmvc = &cm->fc.nmvc;
     int i, j;
 
     read_inter_mode_probs(&cm->fc, r);
@@ -654,14 +659,13 @@
       for (i = 0; i < PARTITION_TYPES - 1; ++i)
         vp9_diff_update_prob(r, &cm->fc.partition_prob[INTER_FRAME][j][i]);
 
-    read_mv_probs(r, nmvc, xd->allow_high_precision_mv);
+    read_mv_probs(r, nmvc, cm->allow_high_precision_mv);
   }
 }
 
-void vp9_read_mode_info(VP9D_COMP* pbi, int mi_row, int mi_col, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
-  MODE_INFO *mi = xd->mi_8x8[0];
+void vp9_read_mode_info(VP9_COMMON *cm, MACROBLOCKD *xd,
+                        int mi_row, int mi_col, vp9_reader *r) {
+  MODE_INFO *const mi = xd->mi_8x8[0];
   const BLOCK_SIZE bsize = mi->mbmi.sb_type;
   const int bw = 1 << mi_width_log2(bsize);
   const int bh = 1 << mi_height_log2(bsize);
@@ -670,12 +674,13 @@
   int x, y, z;
 
   if (frame_is_intra_only(cm))
-    read_intra_frame_mode_info(pbi, mi, mi_row, mi_col, r);
+    read_intra_frame_mode_info(cm, xd, mi, mi_row, mi_col, r);
   else
-    read_inter_frame_mode_info(pbi, mi, mi_row, mi_col, r);
+    read_inter_frame_mode_info(cm, xd, mi, mi_row, mi_col, r);
 
-  for (y = 0, z = 0; y < y_mis; y++, z += cm->mode_info_stride)
+  for (y = 0, z = 0; y < y_mis; y++, z += cm->mode_info_stride) {
     for (x = !y; x < x_mis; x++) {
-        xd->mi_8x8[z + x] = mi;
-      }
+      xd->mi_8x8[z + x] = mi;
+    }
+  }
 }
diff --git a/vp9/decoder/vp9_decodemv.h b/vp9/decoder/vp9_decodemv.h
index 462d2e3..981e8fe 100644
--- a/vp9/decoder/vp9_decodemv.h
+++ b/vp9/decoder/vp9_decodemv.h
@@ -14,8 +14,9 @@
 #include "vp9/decoder/vp9_onyxd_int.h"
 #include "vp9/decoder/vp9_dboolhuff.h"
 
-void vp9_prepare_read_mode_info(VP9D_COMP* pbi, vp9_reader *r);
+void vp9_prepare_read_mode_info(VP9_COMMON* cm, vp9_reader *r);
 
-void vp9_read_mode_info(VP9D_COMP* pbi, int mi_row, int mi_col, vp9_reader *r);
+void vp9_read_mode_info(VP9_COMMON *cm, MACROBLOCKD *xd,
+                        int mi_row, int mi_col, vp9_reader *r);
 
 #endif  // VP9_DECODER_VP9_DECODEMV_H_
diff --git a/vp9/decoder/vp9_decodframe.c b/vp9/decoder/vp9_decodframe.c
index ec310f4..b79ff55 100644
--- a/vp9/decoder/vp9_decodframe.c
+++ b/vp9/decoder/vp9_decodframe.c
@@ -155,9 +155,8 @@
     decode_block(plane, block, plane_bsize, tx_size, arg);
 }
 
-static int decode_tokens(VP9D_COMP *pbi, BLOCK_SIZE bsize, vp9_reader *r) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
+static int decode_tokens(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                         BLOCK_SIZE bsize, vp9_reader *r) {
   MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
 
   if (mbmi->skip_coeff) {
@@ -169,7 +168,7 @@
                                                   cm->base_qindex));
 
     // TODO(dkovalev) if (!vp9_reader_has_error(r))
-    return vp9_decode_tokens(pbi, r, bsize);
+    return vp9_decode_tokens(cm, xd, &cm->seg, r, bsize);
   }
 }
 
@@ -197,7 +196,6 @@
   xd->last_mi = cm->prev_mi ? xd->prev_mi_8x8[0] : NULL;
 
   set_skip_context(cm, xd, mi_row, mi_col);
-  set_partition_seg_context(cm, xd, mi_row, mi_col);
 
   // Distance of Mb to the various image edges. These are specified to 8th pel
   // as they are always compared to values that are in 1/8th pel units
@@ -206,25 +204,25 @@
   setup_dst_planes(xd, &cm->yv12_fb[cm->new_fb_idx], mi_row, mi_col);
 }
 
-static void set_ref(VP9D_COMP *pbi, int i, int mi_row, int mi_col) {
-  VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
+static void set_ref(VP9_COMMON *const cm, MACROBLOCKD *const xd,
+                    int idx, int mi_row, int mi_col) {
   MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
-  const int ref = mbmi->ref_frame[i] - LAST_FRAME;
+  const int ref = mbmi->ref_frame[idx] - LAST_FRAME;
   const YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[ref]];
   const struct scale_factors *sf = &cm->active_ref_scale[ref];
   if (!vp9_is_valid_scale(sf))
     vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
                        "Invalid scale factors");
 
-  xd->scale_factor[i] = *sf;
-  setup_pre_planes(xd, i, cfg, mi_row, mi_col, sf);
+  xd->scale_factor[idx] = *sf;
+  setup_pre_planes(xd, idx, cfg, mi_row, mi_col, sf);
   xd->corrupted |= cfg->corrupted;
 }
 
 static void decode_modes_b(VP9D_COMP *pbi, int tile_col,
                            int mi_row, int mi_col,
                            vp9_reader *r, BLOCK_SIZE bsize, int index) {
+  VP9_COMMON *const cm = &pbi->common;
   MACROBLOCKD *const xd = &pbi->mb;
   const int less8x8 = bsize < BLOCK_8X8;
   MB_MODE_INFO *mbmi;
@@ -235,14 +233,14 @@
       return;
 
   set_offsets(pbi, bsize, tile_col, mi_row, mi_col);
-  vp9_read_mode_info(pbi, mi_row, mi_col, r);
+  vp9_read_mode_info(cm, xd, mi_row, mi_col, r);
 
   if (less8x8)
     bsize = BLOCK_8X8;
 
   // Has to be called after set_offsets
   mbmi = &xd->mi_8x8[0]->mbmi;
-  eobtotal = decode_tokens(pbi, bsize, r);
+  eobtotal = decode_tokens(cm, xd, bsize, r);
 
   if (!is_inter_block(mbmi)) {
     // Intra reconstruction
@@ -257,9 +255,9 @@
         mbmi->skip_coeff = 1;  // skip loopfilter
     }
 
-    set_ref(pbi, 0, mi_row, mi_col);
+    set_ref(cm, xd, 0, mi_row, mi_col);
     if (has_second_ref(mbmi))
-      set_ref(pbi, 1, mi_row, mi_col);
+      set_ref(cm, xd, 1, mi_row, mi_col);
 
     xd->subpix.filter_x = xd->subpix.filter_y =
         vp9_get_filter_kernel(mbmi->interp_filter);
@@ -271,12 +269,10 @@
   xd->corrupted |= vp9_reader_has_error(r);
 }
 
-
 static void decode_modes_sb(VP9D_COMP *pbi, int tile_col,
                             int mi_row, int mi_col,
                             vp9_reader* r, BLOCK_SIZE bsize, int index) {
   VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
   const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2;
   PARTITION_TYPE partition = PARTITION_NONE;
   BLOCK_SIZE subsize;
@@ -291,8 +287,7 @@
     int pl;
     const int idx = check_bsize_coverage(hbs, cm->mi_rows, cm->mi_cols,
                                          mi_row, mi_col);
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    pl = partition_plane_context(xd, bsize);
+    pl = partition_plane_context(cm, mi_row, mi_col, bsize);
 
     if (idx == 0)
       partition = treed_read(r, vp9_partition_tree,
@@ -303,7 +298,8 @@
     else
       partition = PARTITION_SPLIT;
 
-    cm->counts.partition[pl][partition]++;
+    if (!cm->frame_parallel_decoding_mode)
+      ++cm->counts.partition[pl][partition];
   }
 
   subsize = get_subsize(bsize, partition);
@@ -336,27 +332,24 @@
 
   // update partition context
   if (bsize >= BLOCK_8X8 &&
-      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    update_partition_context(xd, subsize, bsize);
-  }
+      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
+    update_partition_context(cm, mi_row, mi_col, subsize, bsize);
 }
 
-static void setup_token_decoder(VP9D_COMP *pbi,
-                                const uint8_t *data, size_t read_size,
+static void setup_token_decoder(const uint8_t *data,
+                                const uint8_t *data_end,
+                                size_t read_size,
+                                struct vpx_internal_error_info *error_info,
                                 vp9_reader *r) {
-  VP9_COMMON *cm = &pbi->common;
-  const uint8_t *data_end = pbi->source + pbi->source_sz;
-
   // Validate the calculated partition length. If the buffer
   // described by the partition can't be fully read, then restrict
   // it to the portion that can be (for EC mode) or throw an error.
   if (!read_is_valid(data, read_size, data_end))
-    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
+    vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
                        "Truncated packet or corrupt tile length");
 
   if (vp9_reader_init(r, data, read_size))
-    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
+    vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
                        "Failed to allocate bool decoder %d", 1);
 }
 
@@ -707,9 +700,9 @@
       vp9_get_tile_row_offsets(cm, tile_row);
       for (tile_col = tile_cols - 1; tile_col >= 0; tile_col--) {
         vp9_get_tile_col_offsets(cm, tile_col);
-        setup_token_decoder(pbi, data_ptr2[tile_row][tile_col],
+        setup_token_decoder(data_ptr2[tile_row][tile_col], data_end,
                             data_end - data_ptr2[tile_row][tile_col],
-                            &residual_bc);
+                            &cm->error, &residual_bc);
         decode_tile(pbi, &residual_bc, tile_col);
         if (tile_row == tile_rows - 1 && tile_col == tile_cols - 1)
           bc_bak = residual_bc;
@@ -738,7 +731,7 @@
           size = data_end - data;
         }
 
-        setup_token_decoder(pbi, data, size, &residual_bc);
+        setup_token_decoder(data, data_end, size, &cm->error, &residual_bc);
         decode_tile(pbi, &residual_bc, tile_col);
         data += size;
       }
@@ -798,7 +791,6 @@
 static size_t read_uncompressed_header(VP9D_COMP *pbi,
                                        struct vp9_read_bit_buffer *rb) {
   VP9_COMMON *const cm = &pbi->common;
-  MACROBLOCKD *const xd = &pbi->mb;
   size_t sz;
   int i;
 
@@ -877,7 +869,7 @@
 
       setup_frame_size_with_refs(pbi, rb);
 
-      xd->allow_high_precision_mv = vp9_rb_read_bit(rb);
+      cm->allow_high_precision_mv = vp9_rb_read_bit(rb);
       cm->mcomp_filter_type = read_interp_filter_type(rb);
 
       for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i)
@@ -927,7 +919,7 @@
     read_tx_probs(&cm->fc.tx_probs, &r);
   read_coef_probs(&cm->fc, cm->tx_mode, &r);
 
-  vp9_prepare_read_mode_info(pbi, &r);
+  vp9_prepare_read_mode_info(cm, &r);
 
   return vp9_reader_has_error(&r);
 }
@@ -1032,7 +1024,7 @@
 
     if (!frame_is_intra_only(cm)) {
       vp9_adapt_mode_probs(cm);
-      vp9_adapt_mv_probs(cm, xd->allow_high_precision_mv);
+      vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
     }
   }
 
diff --git a/vp9/decoder/vp9_detokenize.c b/vp9/decoder/vp9_detokenize.c
index 58976ec..2f5b136 100644
--- a/vp9/decoder/vp9_detokenize.c
+++ b/vp9/decoder/vp9_detokenize.c
@@ -113,7 +113,8 @@
       pt = get_coef_context(nb, token_cache, c);
     band = get_coef_band(band_translate, c);
     prob = coef_probs[band][pt];
-    counts->eob_branch[tx_size][type][ref][band][pt]++;
+    if (!cm->frame_parallel_decoding_mode)
+      ++counts->eob_branch[tx_size][type][ref][band][pt];
     if (!vp9_read(r, prob[EOB_CONTEXT_NODE]))
       break;
 
@@ -198,14 +199,18 @@
     WRITE_COEF_CONTINUE(val, DCT_VAL_CATEGORY6);
   }
 
-  if (c < seg_eob)
-    coef_counts[type][ref][band][pt][DCT_EOB_MODEL_TOKEN]++;
+  if (c < seg_eob) {
+    if (!cm->frame_parallel_decoding_mode)
+      ++coef_counts[type][ref][band][pt][DCT_EOB_MODEL_TOKEN];
+  }
 
   return c;
 }
 
 struct decode_block_args {
-  VP9D_COMP *pbi;
+  VP9_COMMON *cm;
+  MACROBLOCKD *xd;
+  struct segmentation *seg;
   vp9_reader *r;
   int *eobtotal;
 };
@@ -215,8 +220,8 @@
   const struct decode_block_args* const arg = argv;
 
   // find the maximum eob for this transform size, adjusted by segment
-  MACROBLOCKD *xd = &arg->pbi->mb;
-  struct segmentation *seg = &arg->pbi->common.seg;
+  MACROBLOCKD *xd = arg->xd;
+  const struct segmentation *seg = arg->seg;
   struct macroblockd_plane* pd = &xd->plane[plane];
   const int segment_id = xd->mi_8x8[0]->mbmi.segment_id;
   const int seg_eob = get_tx_eob(seg, segment_id, tx_size);
@@ -226,7 +231,7 @@
   pt = get_entropy_context(tx_size, pd->above_context + aoff,
                                     pd->left_context + loff);
 
-  eob = decode_coefs(&arg->pbi->common, xd, arg->r, block,
+  eob = decode_coefs(arg->cm, xd, arg->r, block,
                      pd->plane_type, seg_eob, BLOCK_OFFSET(pd->qcoeff, block),
                      tx_size, pd->dequant, pt);
 
@@ -236,9 +241,11 @@
   *arg->eobtotal += eob;
 }
 
-int vp9_decode_tokens(VP9D_COMP *pbi, vp9_reader *r, BLOCK_SIZE bsize) {
+int vp9_decode_tokens(VP9_COMMON *cm, MACROBLOCKD *xd,
+                      struct segmentation *seg,
+                      vp9_reader *r, BLOCK_SIZE bsize) {
   int eobtotal = 0;
-  struct decode_block_args args = {pbi, r, &eobtotal};
-  foreach_transformed_block(&pbi->mb, bsize, decode_block, &args);
+  struct decode_block_args args = {cm, xd, seg, r, &eobtotal};
+  foreach_transformed_block(xd, bsize, decode_block, &args);
   return eobtotal;
 }
diff --git a/vp9/decoder/vp9_detokenize.h b/vp9/decoder/vp9_detokenize.h
index cf07c56..0fb4c3c 100644
--- a/vp9/decoder/vp9_detokenize.h
+++ b/vp9/decoder/vp9_detokenize.h
@@ -15,6 +15,8 @@
 #include "vp9/decoder/vp9_onyxd_int.h"
 #include "vp9/decoder/vp9_dboolhuff.h"
 
-int vp9_decode_tokens(VP9D_COMP* pbi, vp9_reader *r, BLOCK_SIZE bsize);
+int vp9_decode_tokens(VP9_COMMON *cm, MACROBLOCKD *xd,
+                      struct segmentation *seg,
+                      vp9_reader *r, BLOCK_SIZE bsize);
 
 #endif  // VP9_DECODER_VP9_DETOKENIZE_H_
diff --git a/vp9/encoder/vp9_bitstream.c b/vp9/encoder/vp9_bitstream.c
index ed795f0..0f89219 100644
--- a/vp9/encoder/vp9_bitstream.c
+++ b/vp9/encoder/vp9_bitstream.c
@@ -402,7 +402,7 @@
   const int segment_id = mi->segment_id;
   int skip_coeff;
   const BLOCK_SIZE bsize = mi->sb_type;
-  const int allow_hp = xd->allow_high_precision_mv;
+  const int allow_hp = cm->allow_high_precision_mv;
 
 #ifdef ENTROPY_STATS
   active_section = 9;
@@ -526,7 +526,7 @@
   const int ym = m->mbmi.mode;
   const int segment_id = m->mbmi.segment_id;
   MODE_INFO *above_mi = mi_8x8[-xd->mode_info_stride];
-  MODE_INFO *left_mi = mi_8x8[-1];
+  MODE_INFO *left_mi = xd->left_available ? mi_8x8[-1] : NULL;
 
   if (seg->update_map)
     write_segment_id(bc, seg, m->mbmi.segment_id);
@@ -538,8 +538,7 @@
 
   if (m->mbmi.sb_type >= BLOCK_8X8) {
     const MB_PREDICTION_MODE A = above_block_mode(m, above_mi, 0);
-    const MB_PREDICTION_MODE L = xd->left_available ?
-                                 left_block_mode(m, left_mi, 0) : DC_PRED;
+    const MB_PREDICTION_MODE L = left_block_mode(m, left_mi, 0);
     write_intra_mode(bc, ym, vp9_kf_y_mode_prob[A][L]);
   } else {
     int idx, idy;
@@ -549,8 +548,7 @@
       for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
         int i = idy * 2 + idx;
         const MB_PREDICTION_MODE A = above_block_mode(m, above_mi, i);
-        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
-                                     left_block_mode(m, left_mi, i) : DC_PRED;
+        const MB_PREDICTION_MODE L = left_block_mode(m, left_mi, i);
         const int bm = m->bmi[i].as_mode;
 #ifdef ENTROPY_STATS
         ++intra_mode_stats[A][L][bm];
@@ -600,7 +598,6 @@
                            int mi_row, int mi_col, BLOCK_SIZE bsize,
                            int index) {
   VP9_COMMON *const cm = &cpi->common;
-  MACROBLOCKD *xd = &cpi->mb.e_mbd;
   const int mis = cm->mode_info_stride;
   int bsl = b_width_log2(bsize);
   int bs = (1 << bsl) / 4;  // mode_info step for subsize
@@ -621,8 +618,7 @@
     int pl;
     const int idx = check_bsize_coverage(bs, cm->mi_rows, cm->mi_cols,
                                          mi_row, mi_col);
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    pl = partition_plane_context(xd, bsize);
+    pl = partition_plane_context(cm, mi_row, mi_col, bsize);
     // encode the partition information
     if (idx == 0)
       write_token(bc, vp9_partition_tree,
@@ -664,10 +660,8 @@
 
   // update partition context
   if (bsize >= BLOCK_8X8 &&
-      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    update_partition_context(xd, subsize, bsize);
-  }
+      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
+    update_partition_context(cm, mi_row, mi_col, subsize, bsize);
 }
 
 static void write_modes(VP9_COMP *cpi, vp9_writer* const bc,
@@ -1309,7 +1303,6 @@
 static void write_uncompressed_header(VP9_COMP *cpi,
                                       struct vp9_write_bit_buffer *wb) {
   VP9_COMMON *const cm = &cpi->common;
-  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
 
   // frame marker bits
   vp9_wb_write_literal(wb, 0x2, 2);
@@ -1374,7 +1367,7 @@
 
       write_frame_size_with_refs(cpi, wb);
 
-      vp9_wb_write_bit(wb, xd->allow_high_precision_mv);
+      vp9_wb_write_bit(wb, cm->allow_high_precision_mv);
 
       fix_mcomp_filter_type(cpi);
       write_interp_filter_type(cm->mcomp_filter_type, wb);
@@ -1472,7 +1465,7 @@
                   (unsigned int *)cpi->partition_count[i]);
     }
 
-    vp9_write_nmv_probs(cpi, xd->allow_high_precision_mv, &header_bc);
+    vp9_write_nmv_probs(cpi, cm->allow_high_precision_mv, &header_bc);
   }
 
   vp9_stop_encode(&header_bc);
diff --git a/vp9/encoder/vp9_block.h b/vp9/encoder/vp9_block.h
index fe76804..da47cd5 100644
--- a/vp9/encoder/vp9_block.h
+++ b/vp9/encoder/vp9_block.h
@@ -26,7 +26,8 @@
 // Structure to hold snapshot of coding context during the mode picking process
 typedef struct {
   MODE_INFO mic;
-  uint8_t zcoeff_blk[256];
+  uint8_t *zcoeff_blk;
+  int num_4x4_blk;
   int skip;
   int_mv best_ref_mv;
   int_mv second_best_ref_mv;
@@ -114,7 +115,7 @@
   int **mvsadcost;
 
   int mbmode_cost[MB_MODE_COUNT];
-  unsigned inter_mode_cost[INTER_MODE_CONTEXTS][MB_MODE_COUNT - NEARESTMV];
+  unsigned inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
   int intra_uv_mode_cost[2][MB_MODE_COUNT];
   int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
   int switchable_interp_costs[SWITCHABLE_FILTERS + 1]
@@ -177,6 +178,45 @@
                          int y_blocks);
 };
 
+// TODO(jingning): the variables used here are little complicated. need further
+// refactoring on organizing the temporary buffers, when recursive
+// partition down to 4x4 block size is enabled.
+static PICK_MODE_CONTEXT *get_block_context(MACROBLOCK *x, BLOCK_SIZE bsize) {
+  MACROBLOCKD *const xd = &x->e_mbd;
+
+  switch (bsize) {
+    case BLOCK_64X64:
+      return &x->sb64_context;
+    case BLOCK_64X32:
+      return &x->sb64x32_context[xd->sb_index];
+    case BLOCK_32X64:
+      return &x->sb32x64_context[xd->sb_index];
+    case BLOCK_32X32:
+      return &x->sb32_context[xd->sb_index];
+    case BLOCK_32X16:
+      return &x->sb32x16_context[xd->sb_index][xd->mb_index];
+    case BLOCK_16X32:
+      return &x->sb16x32_context[xd->sb_index][xd->mb_index];
+    case BLOCK_16X16:
+      return &x->mb_context[xd->sb_index][xd->mb_index];
+    case BLOCK_16X8:
+      return &x->sb16x8_context[xd->sb_index][xd->mb_index][xd->b_index];
+    case BLOCK_8X16:
+      return &x->sb8x16_context[xd->sb_index][xd->mb_index][xd->b_index];
+    case BLOCK_8X8:
+      return &x->sb8x8_context[xd->sb_index][xd->mb_index][xd->b_index];
+    case BLOCK_8X4:
+      return &x->sb8x4_context[xd->sb_index][xd->mb_index][xd->b_index];
+    case BLOCK_4X8:
+      return &x->sb4x8_context[xd->sb_index][xd->mb_index][xd->b_index];
+    case BLOCK_4X4:
+      return &x->ab4x4_context[xd->sb_index][xd->mb_index][xd->b_index];
+    default:
+      assert(0);
+      return NULL;
+  }
+}
+
 struct rdcost_block_args {
   MACROBLOCK *x;
   ENTROPY_CONTEXT t_above[16];
diff --git a/vp9/encoder/vp9_dct.c b/vp9/encoder/vp9_dct.c
index 6dc966c..461df63 100644
--- a/vp9/encoder/vp9_dct.c
+++ b/vp9/encoder/vp9_dct.c
@@ -301,14 +301,13 @@
   }
 }
 
-void vp9_short_fdct16x16_c(int16_t *input, int16_t *output, int pitch) {
+void vp9_short_fdct16x16_c(int16_t *input, int16_t *output, int stride) {
   // The 2D transform is done with two passes which are actually pretty
   // similar. In the first one, we transform the columns and transpose
   // the results. In the second one, we transform the rows. To achieve that,
   // as the first pass results are transposed, we tranpose the columns (that
   // is the transposed rows) and transpose the results (so that it goes back
   // in normal/row positions).
-  const int stride = pitch >> 1;
   int pass;
   // We need an intermediate buffer between passes.
   int16_t intermediate[256];
diff --git a/vp9/encoder/vp9_encodeframe.c b/vp9/encoder/vp9_encodeframe.c
index 2f6d458..98284a6 100644
--- a/vp9/encoder/vp9_encodeframe.c
+++ b/vp9/encoder/vp9_encodeframe.c
@@ -30,7 +30,6 @@
 #include "vp9/common/vp9_reconinter.h"
 #include "vp9/common/vp9_seg_common.h"
 #include "vp9/common/vp9_tile_common.h"
-
 #include "vp9/encoder/vp9_encodeframe.h"
 #include "vp9/encoder/vp9_encodeintra.h"
 #include "vp9/encoder/vp9_encodemb.h"
@@ -46,14 +45,6 @@
 #define DBG_PRNT_SEGMAP 0
 
 
-static const TX_SIZE tx_mode_to_biggest_tx_size[TX_MODES] = {
-  TX_4X4,  // ONLY_4X4
-  TX_8X8,  // ONLY_8X8
-  TX_16X16,  // ONLY_16X16
-  TX_32X32,  // ONLY_32X32
-  TX_32X32,  // TX_MODE_SELECT
-};
-
 // #define ENC_DEBUG
 #ifdef ENC_DEBUG
 int enc_debug = 0;
@@ -419,7 +410,7 @@
 
   x->skip = ctx->skip;
   vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
-             sizeof(ctx->zcoeff_blk));
+             sizeof(uint8_t) * ctx->num_4x4_blk);
 
   if (!output_enabled)
     return;
@@ -509,7 +500,6 @@
   const struct segmentation *const seg = &cm->seg;
 
   set_skip_context(cm, xd, mi_row, mi_col);
-  set_partition_seg_context(cm, xd, mi_row, mi_col);
 
   // Activity map pointer
   x->mb_activity_ptr = &cpi->mb_activity_map[idx_map];
@@ -699,45 +689,6 @@
   }
 }
 
-// TODO(jingning): the variables used here are little complicated. need further
-// refactoring on organizing the temporary buffers, when recursive
-// partition down to 4x4 block size is enabled.
-static PICK_MODE_CONTEXT *get_block_context(MACROBLOCK *x, BLOCK_SIZE bsize) {
-  MACROBLOCKD *const xd = &x->e_mbd;
-
-  switch (bsize) {
-    case BLOCK_64X64:
-      return &x->sb64_context;
-    case BLOCK_64X32:
-      return &x->sb64x32_context[xd->sb_index];
-    case BLOCK_32X64:
-      return &x->sb32x64_context[xd->sb_index];
-    case BLOCK_32X32:
-      return &x->sb32_context[xd->sb_index];
-    case BLOCK_32X16:
-      return &x->sb32x16_context[xd->sb_index][xd->mb_index];
-    case BLOCK_16X32:
-      return &x->sb16x32_context[xd->sb_index][xd->mb_index];
-    case BLOCK_16X16:
-      return &x->mb_context[xd->sb_index][xd->mb_index];
-    case BLOCK_16X8:
-      return &x->sb16x8_context[xd->sb_index][xd->mb_index][xd->b_index];
-    case BLOCK_8X16:
-      return &x->sb8x16_context[xd->sb_index][xd->mb_index][xd->b_index];
-    case BLOCK_8X8:
-      return &x->sb8x8_context[xd->sb_index][xd->mb_index][xd->b_index];
-    case BLOCK_8X4:
-      return &x->sb8x4_context[xd->sb_index][xd->mb_index][xd->b_index];
-    case BLOCK_4X8:
-      return &x->sb4x8_context[xd->sb_index][xd->mb_index][xd->b_index];
-    case BLOCK_4X4:
-      return &x->ab4x4_context[xd->sb_index][xd->mb_index][xd->b_index];
-    default:
-      assert(0);
-      return NULL;
-  }
-}
-
 static BLOCK_SIZE *get_sb_partitioning(MACROBLOCK *x, BLOCK_SIZE bsize) {
   MACROBLOCKD *const xd = &x->e_mbd;
   switch (bsize) {
@@ -867,8 +818,7 @@
 
   c1 = BLOCK_4X4;
   if (bsize >= BLOCK_8X8) {
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    pl = partition_plane_context(xd, bsize);
+    pl = partition_plane_context(cm, mi_row, mi_col, bsize);
     c1 = *(get_sb_partitioning(x, bsize));
   }
   partition = partition_lookup[bsl][c1];
@@ -910,10 +860,8 @@
       break;
   }
 
-  if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8) {
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    update_partition_context(xd, c1, bsize);
-  }
+  if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
+    update_partition_context(cm, mi_row, mi_col, c1, bsize);
 }
 
 // Check to see if the given partition size is allowed for a specified number
@@ -1104,8 +1052,7 @@
       pick_sb_modes(cpi, mi_row, mi_col, &none_rate, &none_dist, bsize,
                     get_block_context(x, bsize), INT64_MAX);
 
-      set_partition_seg_context(cm, xd, mi_row, mi_col);
-      pl = partition_plane_context(xd, bsize);
+      pl = partition_plane_context(cm, mi_row, mi_col, bsize);
       none_rate += x->partition_cost[pl][PARTITION_NONE];
 
       restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
@@ -1195,8 +1142,8 @@
     default:
       assert(0);
   }
-  set_partition_seg_context(cm, xd, mi_row, mi_col);
-  pl = partition_plane_context(xd, bsize);
+
+  pl = partition_plane_context(cm, mi_row, mi_col, bsize);
   if (last_part_rate < INT_MAX)
     last_part_rate += x->partition_cost[pl][partition];
 
@@ -1246,12 +1193,10 @@
 
       split_rate += rt;
       split_dist += dt;
-      set_partition_seg_context(cm, xd, mi_row + y_idx, mi_col + x_idx);
-      pl = partition_plane_context(xd, bsize);
+      pl = partition_plane_context(cm, mi_row + y_idx, mi_col + x_idx, bsize);
       split_rate += x->partition_cost[pl][PARTITION_NONE];
     }
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    pl = partition_plane_context(xd, bsize);
+    pl = partition_plane_context(cm, mi_row, mi_col, bsize);
     if (split_rate < INT_MAX) {
       split_rate += x->partition_cost[pl][PARTITION_SPLIT];
 
@@ -1580,8 +1525,7 @@
                   get_block_context(x, bsize), best_rd);
     if (this_rate != INT_MAX) {
       if (bsize >= BLOCK_8X8) {
-        set_partition_seg_context(cm, xd, mi_row, mi_col);
-        pl = partition_plane_context(xd, bsize);
+        pl = partition_plane_context(cm, mi_row, mi_col, bsize);
         this_rate += x->partition_cost[pl][PARTITION_NONE];
       }
       sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
@@ -1641,8 +1585,7 @@
       }
     }
     if (sum_rd < best_rd && i == 4) {
-      set_partition_seg_context(cm, xd, mi_row, mi_col);
-      pl = partition_plane_context(xd, bsize);
+      pl = partition_plane_context(cm, mi_row, mi_col, bsize);
       sum_rate += x->partition_cost[pl][PARTITION_SPLIT];
       sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
       if (sum_rd < best_rd) {
@@ -1698,8 +1641,7 @@
       }
     }
     if (sum_rd < best_rd) {
-      set_partition_seg_context(cm, xd, mi_row, mi_col);
-      pl = partition_plane_context(xd, bsize);
+      pl = partition_plane_context(cm, mi_row, mi_col, bsize);
       sum_rate += x->partition_cost[pl][PARTITION_HORZ];
       sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
       if (sum_rd < best_rd) {
@@ -1741,8 +1683,7 @@
       }
     }
     if (sum_rd < best_rd) {
-      set_partition_seg_context(cm, xd, mi_row, mi_col);
-      pl = partition_plane_context(xd, bsize);
+      pl = partition_plane_context(cm, mi_row, mi_col, bsize);
       sum_rate += x->partition_cost[pl][PARTITION_VERT];
       sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
       if (sum_rd < best_rd) {
@@ -1774,7 +1715,6 @@
 static void rd_pick_reference_frame(VP9_COMP *cpi, int mi_row, int mi_col) {
   VP9_COMMON * const cm = &cpi->common;
   MACROBLOCK * const x = &cpi->mb;
-  MACROBLOCKD * const xd = &x->e_mbd;
   int bsl = b_width_log2(BLOCK_64X64), bs = 1 << bsl;
   int ms = bs / 2;
   ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
@@ -1794,8 +1734,7 @@
     cpi->set_ref_frame_mask = 1;
     pick_sb_modes(cpi, mi_row, mi_col, &r, &d, BLOCK_64X64,
                   get_block_context(x, BLOCK_64X64), INT64_MAX);
-    set_partition_seg_context(cm, xd, mi_row, mi_col);
-    pl = partition_plane_context(xd, BLOCK_64X64);
+    pl = partition_plane_context(cm, mi_row, mi_col, BLOCK_64X64);
     r += x->partition_cost[pl][PARTITION_NONE];
 
     *(get_sb_partitioning(x, BLOCK_64X64)) = BLOCK_64X64;
diff --git a/vp9/encoder/vp9_encodemb.c b/vp9/encoder/vp9_encodemb.c
index 5fe44c9..c3a2317 100644
--- a/vp9/encoder/vp9_encodemb.c
+++ b/vp9/encoder/vp9_encodemb.c
@@ -379,7 +379,7 @@
       xoff = 16 * (block & twmask);
       yoff = 16 * (block >> twl);
       src_diff = p->src_diff + 4 * bw * yoff + xoff;
-      vp9_short_fdct16x16(src_diff, coeff, bw * 8);
+      vp9_short_fdct16x16(src_diff, coeff, bw * 4);
       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, iscan);
@@ -559,7 +559,7 @@
       if (tx_type != DCT_DCT)
         vp9_short_fht16x16(src_diff, coeff, bw * 4, tx_type);
       else
-        vp9_short_fdct16x16(src_diff, coeff, bw * 8);
+        vp9_short_fdct16x16(src_diff, coeff, bw * 4);
       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, iscan);
diff --git a/vp9/encoder/vp9_mbgraph.c b/vp9/encoder/vp9_mbgraph.c
index ea4c9e8..6443631 100644
--- a/vp9/encoder/vp9_mbgraph.c
+++ b/vp9/encoder/vp9_mbgraph.c
@@ -61,7 +61,7 @@
     best_err = cpi->find_fractional_mv_step(
         x,
         &dst_mv->as_mv, &ref_mv->as_mv,
-        xd->allow_high_precision_mv,
+        cpi->common.allow_high_precision_mv,
         x->errorperbit, &v_fn_ptr,
         0, cpi->sf.subpel_iters_per_step, NULL, NULL,
         & distortion, &sse);
diff --git a/vp9/encoder/vp9_onyx_if.c b/vp9/encoder/vp9_onyx_if.c
index 54b3d43..b52a101 100644
--- a/vp9/encoder/vp9_onyx_if.c
+++ b/vp9/encoder/vp9_onyx_if.c
@@ -237,8 +237,9 @@
   return active_best_quality;
 }
 
-static void set_mvcost(MACROBLOCK *mb) {
-  if (mb->e_mbd.allow_high_precision_mv) {
+static void set_mvcost(VP9_COMP *cpi) {
+  MACROBLOCK *const mb = &cpi->mb;
+  if (cpi->common.allow_high_precision_mv) {
     mb->mvcost = mb->nmvcost_hp;
     mb->mvsadcost = mb->nmvsadcost_hp;
   } else {
@@ -826,6 +827,7 @@
         sf->last_partitioning_redo_frequency = 3;
 
         sf->adaptive_rd_thresh = 2;
+        sf->recode_loop = 2;
         sf->mode_skip_start = 11;
         sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
         sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
@@ -1261,8 +1263,8 @@
   cm->reset_frame_context = 0;
 
   setup_features(cm);
-  cpi->mb.e_mbd.allow_high_precision_mv = 0;  // Default mv precision
-  set_mvcost(&cpi->mb);
+  cpi->common.allow_high_precision_mv = 0;  // Default mv precision
+  set_mvcost(cpi);
 
   {
     int i;
@@ -1414,6 +1416,94 @@
   } while (++i <= MV_MAX);
 }
 
+static void init_pick_mode_context(VP9_COMP *cpi) {
+  int i;
+  MACROBLOCK  *x  = &cpi->mb;
+  MACROBLOCKD *xd = &x->e_mbd;
+  VP9_COMMON  *cm = &cpi->common;
+
+  for (i = 0; i < BLOCK_SIZES; ++i) {
+    const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
+    const int num_4x4_h = num_4x4_blocks_high_lookup[i];
+    const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
+    if (i < BLOCK_16X16) {
+      for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) {
+        for (xd->mb_index = 0; xd->mb_index < 4; ++xd->mb_index) {
+          for (xd->b_index = 0; xd->b_index < 16 / num_4x4_blk; ++xd->b_index) {
+            PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+            ctx->num_4x4_blk = num_4x4_blk;
+            CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
+                            vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
+          }
+        }
+      }
+    } else if (i < BLOCK_32X32) {
+      for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) {
+        for (xd->mb_index = 0; xd->mb_index < 64 / num_4x4_blk;
+                               ++xd->mb_index) {
+          PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+          ctx->num_4x4_blk = num_4x4_blk;
+          CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
+                          vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
+        }
+      }
+    } else if (i < BLOCK_64X64) {
+      for (xd->sb_index = 0; xd->sb_index < 256 / num_4x4_blk; ++xd->sb_index) {
+        PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+        ctx->num_4x4_blk = num_4x4_blk;
+        CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
+                        vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
+      }
+    } else {
+      PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+      ctx->num_4x4_blk = num_4x4_blk;
+      CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
+                      vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
+    }
+  }
+}
+
+static void free_pick_mode_context(MACROBLOCK *x) {
+  int i;
+  MACROBLOCKD *xd = &x->e_mbd;
+
+  for (i = 0; i < BLOCK_SIZES; ++i) {
+    const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
+    const int num_4x4_h = num_4x4_blocks_high_lookup[i];
+    const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
+    if (i < BLOCK_16X16) {
+      for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) {
+        for (xd->mb_index = 0; xd->mb_index < 4; ++xd->mb_index) {
+          for (xd->b_index = 0; xd->b_index < 16 / num_4x4_blk; ++xd->b_index) {
+            PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+            vpx_free(ctx->zcoeff_blk);
+            ctx->zcoeff_blk = 0;
+          }
+        }
+      }
+    } else if (i < BLOCK_32X32) {
+      for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) {
+        for (xd->mb_index = 0; xd->mb_index < 64 / num_4x4_blk;
+                               ++xd->mb_index) {
+          PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+          vpx_free(ctx->zcoeff_blk);
+          ctx->zcoeff_blk = 0;
+        }
+      }
+    } else if (i < BLOCK_64X64) {
+      for (xd->sb_index = 0; xd->sb_index < 256 / num_4x4_blk; ++xd->sb_index) {
+        PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+        vpx_free(ctx->zcoeff_blk);
+        ctx->zcoeff_blk = 0;
+      }
+    } else {
+      PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
+      vpx_free(ctx->zcoeff_blk);
+      ctx->zcoeff_blk = 0;
+    }
+  }
+}
+
 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
   int i, j;
   volatile union {
@@ -1450,6 +1540,8 @@
 
   init_config((VP9_PTR)cpi, oxcf);
 
+  init_pick_mode_context(cpi);
+
   cm->current_video_frame   = 0;
   cpi->kf_overspend_bits            = 0;
   cpi->kf_bitrate_adjustment        = 0;
@@ -1742,7 +1834,7 @@
   vp9_zero(cpi->y_uv_mode_count);
 
 #ifdef MODE_TEST_HIT_STATS
-  vp9_zero(cpi->mode_test_hits)
+  vp9_zero(cpi->mode_test_hits);
 #endif
 
   return (VP9_PTR) cpi;
@@ -1913,6 +2005,7 @@
 #endif
   }
 
+  free_pick_mode_context(&cpi->mb);
   dealloc_compressor_data(cpi);
   vpx_free(cpi->mb.ss);
   vpx_free(cpi->tok);
@@ -2782,11 +2875,12 @@
 
   // Limit Q range for the adaptive loop.
   if (cm->frame_type == KEY_FRAME && !cpi->this_key_frame_forced) {
-    *top_index = cpi->active_best_quality;
+    *top_index =
+      (cpi->active_worst_quality + cpi->active_best_quality * 3) / 4;
   } else if (!cpi->is_src_frame_alt_ref &&
              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
     *top_index =
-      (cpi->active_worst_quality + cpi->active_best_quality * 3) / 4;
+      (cpi->active_worst_quality + cpi->active_best_quality) / 2;
   } else {
     *top_index = cpi->active_worst_quality;
   }
@@ -2818,7 +2912,6 @@
                                       unsigned char *dest,
                                       unsigned int *frame_flags) {
   VP9_COMMON *const cm = &cpi->common;
-  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
   TX_SIZE t;
   int q;
   int frame_over_shoot_limit;
@@ -2987,8 +3080,8 @@
   if (!frame_is_intra_only(cm)) {
     cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
     /* TODO: Decide this more intelligently */
-    xd->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH;
-    set_mvcost(&cpi->mb);
+    cm->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH;
+    set_mvcost(cpi);
   }
 
 #if CONFIG_VP9_POSTPROC
@@ -3277,7 +3370,7 @@
     if (!cpi->common.error_resilient_mode &&
         !cpi->common.frame_parallel_decoding_mode) {
       vp9_adapt_mode_probs(&cpi->common);
-      vp9_adapt_mv_probs(&cpi->common, cpi->mb.e_mbd.allow_high_precision_mv);
+      vp9_adapt_mv_probs(&cpi->common, cpi->common.allow_high_precision_mv);
     }
   }
 
@@ -3293,8 +3386,9 @@
   cpi->total_byte_count += (*size);
   cpi->projected_frame_size = (*size) << 3;
 
+  // Post encode loop adjustment of Q prediction.
   if (!active_worst_qchanged)
-    vp9_update_rate_correction_factors(cpi, 2);
+    vp9_update_rate_correction_factors(cpi, (cpi->sf.recode_loop) ? 2 : 0);
 
   cpi->last_q[cm->frame_type] = cm->base_qindex;
 
@@ -3369,6 +3463,7 @@
 
   cpi->buffer_level = cpi->bits_off_target;
 
+#ifndef DISABLE_RC_LONG_TERM_MEM
   // Update bits left to the kf and gf groups to account for overshoot or
   // undershoot on these frames
   if (cm->frame_type == KEY_FRAME) {
@@ -3382,6 +3477,7 @@
 
     cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0);
   }
+#endif
 
 #if 0
   output_frame_level_debug_stats(cpi);
@@ -3601,8 +3697,8 @@
 
   cpi->source = NULL;
 
-  cpi->mb.e_mbd.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV;
-  set_mvcost(&cpi->mb);
+  cpi->common.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV;
+  set_mvcost(cpi);
 
   // Should we code an alternate reference frame.
   if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) {
@@ -4130,37 +4226,9 @@
   return 0;
 }
 
-int vp9_switch_layer(VP9_PTR comp, int layer) {
-  VP9_COMP *cpi = (VP9_COMP *)comp;
-
-  if (cpi->use_svc) {
-    cpi->current_layer = layer;
-
-    // Use buffer i for layer i LST
-    cpi->lst_fb_idx = layer;
-
-    // Use buffer i-1 for layer i Alt (Inter-layer prediction)
-    if (layer != 0) cpi->alt_fb_idx = layer - 1;
-
-    // Use the rest for Golden
-    if (layer < 2 * cpi->number_spatial_layers - NUM_REF_FRAMES)
-      cpi->gld_fb_idx = cpi->lst_fb_idx;
-    else
-      cpi->gld_fb_idx = 2 * cpi->number_spatial_layers - 1 - layer;
-
-    printf("Switching to layer %d:\n", layer);
-    printf("Using references: LST/GLD/ALT [%d|%d|%d]\n", cpi->lst_fb_idx,
-           cpi->gld_fb_idx, cpi->alt_fb_idx);
-  } else {
-    printf("Switching layer not supported. Enable SVC first \n");
-  }
-  return 0;
-}
-
 void vp9_set_svc(VP9_PTR comp, int use_svc) {
   VP9_COMP *cpi = (VP9_COMP *)comp;
   cpi->use_svc = use_svc;
-  if (cpi->use_svc) printf("Enabled SVC encoder \n");
   return;
 }
 
diff --git a/vp9/encoder/vp9_onyx_int.h b/vp9/encoder/vp9_onyx_int.h
index 2e5c7bc..7187884 100644
--- a/vp9/encoder/vp9_onyx_int.h
+++ b/vp9/encoder/vp9_onyx_int.h
@@ -33,8 +33,8 @@
 #if CONFIG_ONESHOTQ
 #define ONE_SHOT_Q_ESTIMATE 0
 #define STRICT_ONE_SHOT_Q 0
-#define DISABLE_RC_LONG_TERM_MEM 0
 #endif
+#define DISABLE_RC_LONG_TERM_MEM 0
 
 // #define MODE_TEST_HIT_STATS
 
diff --git a/vp9/encoder/vp9_ratectrl.c b/vp9/encoder/vp9_ratectrl.c
index 224d1e4..0aa3a68 100644
--- a/vp9/encoder/vp9_ratectrl.c
+++ b/vp9/encoder/vp9_ratectrl.c
@@ -288,7 +288,7 @@
   if (correction_factor > 102) {
     // We are not already at the worst allowable quality
     correction_factor =
-        (int)(100.5 + ((correction_factor - 100) * adjustment_limit));
+        (int)(100 + ((correction_factor - 100) * adjustment_limit));
     rate_correction_factor =
         ((rate_correction_factor * correction_factor) / 100);
 
@@ -298,7 +298,7 @@
   } else if (correction_factor < 99) {
     // We are not already at the best allowable quality
     correction_factor =
-        (int)(100.5 - ((100 - correction_factor) * adjustment_limit));
+        (int)(100 - ((100 - correction_factor) * adjustment_limit));
     rate_correction_factor =
         ((rate_correction_factor * correction_factor) / 100);
 
diff --git a/vp9/encoder/vp9_rdopt.c b/vp9/encoder/vp9_rdopt.c
index 7add494..4e4dbac 100644
--- a/vp9/encoder/vp9_rdopt.c
+++ b/vp9/encoder/vp9_rdopt.c
@@ -248,23 +248,22 @@
 
   set_block_thresholds(cpi);
 
-  fill_token_costs(cpi->mb.token_costs, cpi->common.fc.coef_probs);
+  fill_token_costs(cpi->mb.token_costs, cm->fc.coef_probs);
 
   for (i = 0; i < NUM_PARTITION_CONTEXTS; i++)
     vp9_cost_tokens(cpi->mb.partition_cost[i],
-                    cpi->common.fc.partition_prob[cpi->common.frame_type][i],
+                    cm->fc.partition_prob[cm->frame_type][i],
                     vp9_partition_tree);
 
   /*rough estimate for costing*/
   vp9_init_mode_costs(cpi);
 
-  if (!frame_is_intra_only(&cpi->common)) {
+  if (!frame_is_intra_only(cm)) {
     vp9_build_nmv_cost_table(
         cpi->mb.nmvjointcost,
-        cpi->mb.e_mbd.allow_high_precision_mv ?
-        cpi->mb.nmvcost_hp : cpi->mb.nmvcost,
-        &cpi->common.fc.nmvc,
-        cpi->mb.e_mbd.allow_high_precision_mv, 1, 1);
+        cm->allow_high_precision_mv ? cpi->mb.nmvcost_hp : cpi->mb.nmvcost,
+        &cm->fc.nmvc,
+        cm->allow_high_precision_mv, 1, 1);
 
     for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
       MB_PREDICTION_MODE m;
@@ -272,8 +271,8 @@
       for (m = NEARESTMV; m < MB_MODE_COUNT; m++)
         cpi->mb.inter_mode_cost[i][inter_mode_offset(m)] =
             cost_token(vp9_inter_mode_tree,
-                       cpi->common.fc.inter_mode_probs[i],
-                       vp9_inter_mode_encodings + inter_mode_offset(m));
+                       cm->fc.inter_mode_probs[i],
+                       &vp9_inter_mode_encodings[inter_mode_offset(m)]);
     }
   }
 }
@@ -715,22 +714,12 @@
                                      BLOCK_SIZE bs) {
   const TX_SIZE max_tx_size = max_txsize_lookup[bs];
   VP9_COMMON *const cm = &cpi->common;
+  const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[cm->tx_mode];
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
-  if (max_tx_size == TX_32X32 &&
-      (cm->tx_mode == ALLOW_32X32 ||
-       cm->tx_mode == TX_MODE_SELECT)) {
-    mbmi->tx_size = TX_32X32;
-  } else if (max_tx_size >= TX_16X16 &&
-             (cm->tx_mode == ALLOW_16X16 ||
-              cm->tx_mode == ALLOW_32X32 ||
-              cm->tx_mode == TX_MODE_SELECT)) {
-    mbmi->tx_size = TX_16X16;
-  } else if (cm->tx_mode != ONLY_4X4) {
-    mbmi->tx_size = TX_8X8;
-  } else {
-    mbmi->tx_size = TX_4X4;
-  }
+
+  mbmi->tx_size = MIN(max_tx_size, largest_tx_size);
+
   txfm_rd_in_plane(x, &cpi->rdcost_stack, rate, distortion, skip,
                    &sse[mbmi->tx_size], ref_best_rd, 0, bs,
                    mbmi->tx_size);
@@ -1161,7 +1150,7 @@
   MACROBLOCKD *const xd = &mb->e_mbd;
   MODE_INFO *const mic = xd->mi_8x8[0];
   const MODE_INFO *above_mi = xd->mi_8x8[-xd->mode_info_stride];
-  const MODE_INFO *left_mi = xd->mi_8x8[-1];
+  const MODE_INFO *left_mi = xd->left_available ? xd->mi_8x8[-1] : NULL;
   const BLOCK_SIZE bsize = xd->mi_8x8[0]->mbmi.sb_type;
   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
@@ -1187,9 +1176,7 @@
       i = idy * 2 + idx;
       if (cpi->common.frame_type == KEY_FRAME) {
         const MB_PREDICTION_MODE A = above_block_mode(mic, above_mi, i);
-        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
-                                     left_block_mode(mic, left_mi, i) :
-                                     DC_PRED;
+        const MB_PREDICTION_MODE L = left_block_mode(mic, left_mi, i);
 
         bmode_costs  = mb->y_mode_costs[A][L];
       }
@@ -1248,15 +1235,14 @@
   for (mode = DC_PRED; mode <= TM_PRED; mode++) {
     int64_t local_tx_cache[TX_MODES];
     MODE_INFO *above_mi = xd->mi_8x8[-xd->mode_info_stride];
-    MODE_INFO *left_mi = xd->mi_8x8[-1];
+    MODE_INFO *left_mi = xd->left_available ? xd->mi_8x8[-1] : NULL;
 
     if (!(cpi->sf.intra_y_mode_mask[max_txsize_lookup[bsize]] & (1 << mode)))
       continue;
 
     if (cpi->common.frame_type == KEY_FRAME) {
       const MB_PREDICTION_MODE A = above_block_mode(mic, above_mi, 0);
-      const MB_PREDICTION_MODE L = xd->left_available ?
-                                   left_block_mode(mic, left_mi, 0) : DC_PRED;
+      const MB_PREDICTION_MODE L = left_block_mode(mic, left_mi, 0);
 
       bmode_costs = x->y_mode_costs[A][L];
     }
@@ -1870,7 +1856,7 @@
             cpi->find_fractional_mv_step(x,
                                          &mode_mv[NEWMV].as_mv,
                                          &bsi->ref_mv->as_mv,
-                                         x->e_mbd.allow_high_precision_mv,
+                                         cpi->common.allow_high_precision_mv,
                                          x->errorperbit, v_fn_ptr,
                                          0, cpi->sf.subpel_iters_per_step,
                                          x->nmvjointcost, x->mvcost,
@@ -2303,7 +2289,7 @@
                    mbmi->ref_mvs[frame_type], mi_row, mi_col);
 
   // Candidate refinement carried out at encoder and decoder
-  vp9_find_best_ref_mvs(xd,
+  vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv,
                         mbmi->ref_mvs[frame_type],
                         &frame_nearest_mv[frame_type],
                         &frame_near_mv[frame_type]);
@@ -2451,7 +2437,7 @@
     int dis;  /* TODO: use dis in distortion calculation later. */
     unsigned int sse;
     cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv.as_mv,
-                                 xd->allow_high_precision_mv,
+                                 cm->allow_high_precision_mv,
                                  x->errorperbit,
                                  &cpi->fn_ptr[block_size],
                                  0, cpi->sf.subpel_iters_per_step,
@@ -2587,7 +2573,7 @@
       bestsme = cpi->find_fractional_mv_step_comp(
           x, &tmp_mv.as_mv,
           &ref_mv[id].as_mv,
-          xd->allow_high_precision_mv,
+          cpi->common.allow_high_precision_mv,
           x->errorperbit,
           &cpi->fn_ptr[block_size],
           0, cpi->sf.subpel_iters_per_step,
@@ -3149,8 +3135,7 @@
   unsigned int mode_mask = 0;
   int64_t mode_distortions[MB_MODE_COUNT] = {-1};
   int64_t frame_distortions[MAX_REF_FRAMES] = {-1};
-  int intra_cost_penalty = 20 * vp9_dc_quant(cpi->common.base_qindex,
-                                             cpi->common.y_dc_delta_q);
+  int intra_cost_penalty = 20 * vp9_dc_quant(cm->base_qindex, cm->y_dc_delta_q);
   const int bws = num_8x8_blocks_wide_lookup[bsize] / 2;
   const int bhs = num_8x8_blocks_high_lookup[bsize] / 2;
   int best_skip2 = 0;
@@ -3331,7 +3316,7 @@
     // Evaluate all sub-pel filters irrespective of whether we can use
     // them for this frame.
     mbmi->interp_filter = cm->mcomp_filter_type;
-    vp9_setup_interp_filters(xd, mbmi->interp_filter, &cpi->common);
+    vp9_setup_interp_filters(xd, mbmi->interp_filter, cm);
 
     if (comp_pred) {
       if (!(cpi->ref_frame_flags & flag_list[second_ref_frame]))
@@ -3468,7 +3453,7 @@
         continue;
     }
 
-    if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
+    if (cm->comp_pred_mode == HYBRID_PREDICTION) {
       rate2 += compmode_cost;
     }
 
@@ -3587,7 +3572,7 @@
         best_mbmode = *mbmi;
         best_skip2 = this_skip2;
         vpx_memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mbmi->tx_size],
-                   sizeof(ctx->zcoeff_blk));
+                   sizeof(uint8_t) * ctx->num_4x4_blk);
 
         // TODO(debargha): enhance this test with a better distortion prediction
         // based on qp, activity mask and history
@@ -3612,7 +3597,7 @@
     if (!disable_skip && ref_frame != INTRA_FRAME) {
       int single_rd, hybrid_rd, single_rate, hybrid_rate;
 
-      if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
+      if (cm->comp_pred_mode == HYBRID_PREDICTION) {
         single_rate = rate2 - compmode_cost;
         hybrid_rate = rate2;
       } else {
@@ -4327,7 +4312,7 @@
         best_mbmode = *mbmi;
         best_skip2 = this_skip2;
         vpx_memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mbmi->tx_size],
-                   sizeof(ctx->zcoeff_blk));
+                   sizeof(uint8_t) * ctx->num_4x4_blk);
 
         for (i = 0; i < 4; i++)
           best_bmodes[i] = xd->mi_8x8[0]->bmi[i];
diff --git a/vp9/encoder/vp9_temporal_filter.c b/vp9/encoder/vp9_temporal_filter.c
index 5cf8143..6ea0579 100644
--- a/vp9/encoder/vp9_temporal_filter.c
+++ b/vp9/encoder/vp9_temporal_filter.c
@@ -166,7 +166,7 @@
     // Ignore mv costing by sending NULL pointer instead of cost array
     bestsme = cpi->find_fractional_mv_step(x, &ref_mv->as_mv,
                                            &best_ref_mv1.as_mv,
-                                           xd->allow_high_precision_mv,
+                                           cpi->common.allow_high_precision_mv,
                                            x->errorperbit,
                                            &cpi->fn_ptr[BLOCK_16X16],
                                            0, cpi->sf.subpel_iters_per_step,
diff --git a/vp9/encoder/x86/vp9_dct_sse2.c b/vp9/encoder/x86/vp9_dct_sse2.c
index 52a0528..88c133b 100644
--- a/vp9/encoder/x86/vp9_dct_sse2.c
+++ b/vp9/encoder/x86/vp9_dct_sse2.c
@@ -1055,14 +1055,13 @@
   write_buffer_8x8(output, in, 8);
 }
 
-void vp9_short_fdct16x16_sse2(int16_t *input, int16_t *output, int pitch) {
+void vp9_short_fdct16x16_sse2(int16_t *input, int16_t *output, int stride) {
   // The 2D transform is done with two passes which are actually pretty
   // similar. In the first one, we transform the columns and transpose
   // the results. In the second one, we transform the rows. To achieve that,
   // as the first pass results are transposed, we tranpose the columns (that
   // is the transposed rows) and transpose the results (so that it goes back
   // in normal/row positions).
-  const int stride = pitch >> 1;
   int pass;
   // We need an intermediate buffer between passes.
   DECLARE_ALIGNED_ARRAY(16, int16_t, intermediate, 256);
diff --git a/vp9/vp9_cx_iface.c b/vp9/vp9_cx_iface.c
index fb380e1..b744ec6 100644
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -442,8 +442,6 @@
       MAP(VP8E_SET_ARNR_TYPE,               xcfg.arnr_type);
       MAP(VP8E_SET_TUNING,                  xcfg.tuning);
       MAP(VP8E_SET_CQ_LEVEL,                xcfg.cq_level);
-      MAP(VP9E_SET_MAX_Q,                   ctx->cfg.rc_max_quantizer);
-      MAP(VP9E_SET_MIN_Q,                   ctx->cfg.rc_min_quantizer);
       MAP(VP8E_SET_MAX_INTRA_BITRATE_PCT,   xcfg.rc_max_intra_bitrate_pct);
       MAP(VP9E_SET_LOSSLESS,                xcfg.lossless);
       MAP(VP9E_SET_FRAME_PARALLEL_DECODING, xcfg.frame_parallel_decoding_mode);
@@ -1029,62 +1027,6 @@
   }
 }
 
-static vpx_codec_err_t vp9e_set_width(vpx_codec_alg_priv_t *ctx, int ctr_id,
-                                      va_list args) {
-  unsigned int *data = va_arg(args, unsigned int *);
-  if (data) {
-    int res;
-    res = vp9_set_size_literal(ctx->cpi, *data, 0);
-    if (!res) {
-      return VPX_CODEC_OK;
-    } else {
-      return VPX_CODEC_INVALID_PARAM;
-    }
-  } else {
-    return VPX_CODEC_INVALID_PARAM;
-  }
-}
-
-static vpx_codec_err_t vp9e_set_height(vpx_codec_alg_priv_t *ctx,
-                                       int ctr_id,
-                                       va_list args) {
-  unsigned int *data =  va_arg(args, unsigned int *);
-
-  if (data) {
-    int res;
-    res = vp9_set_size_literal(ctx->cpi, 0, *data);
-
-    if (!res) {
-      return VPX_CODEC_OK;
-    } else {
-      return VPX_CODEC_INVALID_PARAM;
-    }
-  } else {
-    return VPX_CODEC_INVALID_PARAM;
-  }
-}
-
-static vpx_codec_err_t vp9e_set_layer(vpx_codec_alg_priv_t *ctx,
-                                      int ctr_id,
-                                      va_list args) {
-  unsigned int *data =  va_arg(args, unsigned int *);
-
-  if (data) {
-    int res;
-    res = 0;
-
-    res = vp9_switch_layer(ctx->cpi, *data);
-
-    if (!res) {
-      return VPX_CODEC_OK;
-    } else {
-      return VPX_CODEC_INVALID_PARAM;
-    }
-  } else {
-    return VPX_CODEC_INVALID_PARAM;
-  }
-}
-
 static vpx_codec_err_t vp9e_set_svc(vpx_codec_alg_priv_t *ctx, int ctr_id,
                                     va_list args) {
   int data = va_arg(args, int);
@@ -1092,6 +1034,36 @@
   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;
+
+  if (!data) {
+    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;
+
+  set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
+  vp9_change_config(ctx->cpi, &ctx->oxcf);
+
+  return VPX_CODEC_OK;
+}
+
 static vpx_codec_ctrl_fn_map_t vp9e_ctf_maps[] = {
   {VP8_SET_REFERENCE,                 vp9e_set_reference},
   {VP8_COPY_REFERENCE,                vp9e_copy_reference},
@@ -1116,16 +1088,12 @@
   {VP8E_SET_ARNR_TYPE,                set_param},
   {VP8E_SET_TUNING,                   set_param},
   {VP8E_SET_CQ_LEVEL,                 set_param},
-  {VP9E_SET_MAX_Q,                    set_param},
-  {VP9E_SET_MIN_Q,                    set_param},
   {VP8E_SET_MAX_INTRA_BITRATE_PCT,    set_param},
   {VP9E_SET_LOSSLESS,                 set_param},
   {VP9E_SET_FRAME_PARALLEL_DECODING,  set_param},
   {VP9_GET_REFERENCE,                 get_reference},
-  {VP9E_SET_WIDTH,                    vp9e_set_width},
-  {VP9E_SET_HEIGHT,                   vp9e_set_height},
-  {VP9E_SET_LAYER,                    vp9e_set_layer},
   {VP9E_SET_SVC,                      vp9e_set_svc},
+  {VP9E_SET_SVC_PARAMETERS,           vp9e_set_svc_parameters},
   { -1, NULL},
 };
 
diff --git a/vp9_spatial_scalable_encoder.c b/vp9_spatial_scalable_encoder.c
index 8bb582f..847d9c4 100644
--- a/vp9_spatial_scalable_encoder.c
+++ b/vp9_spatial_scalable_encoder.c
@@ -23,24 +23,22 @@
 #define VPX_CODEC_DISABLE_COMPAT 1
 #include "vpx/vpx_encoder.h"
 #include "vpx/vp8cx.h"
+#include "vpx/svc_context.h"
+
 #define interface (vpx_codec_vp9_cx())
 #define fourcc 0x30395056
 #define IVF_FILE_HDR_SZ (32)
 #define IVF_FRAME_HDR_SZ (12)
-#define NUM_BUFFERS 8
 
 char *input_filename;
 char *output_filename;
 unsigned int number_frames_to_code = 60 * 60;
 unsigned int number_frames_to_skip = 0;
-unsigned int number_spatial_layers = 5;
-unsigned int key_period = 100;
+unsigned int gop_size = 100;
 
-typedef enum ENCODING_MODE {
-  INTER_LAYER_PREDICTION_I,
-  INTER_LAYER_PREDICTION_IP,
-  USE_GOLDEN_FRAME
-} ENCODING_MODE;
+char *scaling_factor;
+char *quantizer;
+SVC_ENCODING_MODE encoding_mode = INTER_LAYER_PREDICTION_IP;
 
 static void mem_put_le16(char *mem, unsigned int val) {
   mem[0] = val;
@@ -57,7 +55,10 @@
 static void usage(char *program_name) {
   printf(
       "Usage: %s [-f frames] [-s skip_frames] [-w width] [-h height] \n\t"
-      "[-n rate_num] [-d rate_den] [-b bitrate] [-l layers] "
+      "[-n rate_num] [-d rate_den] [-b bitrate] [-l layers] [-g gop_size] \n\t"
+      "[-z dummy_frame (default 1) \n\t"
+      "[-q quantizer (lowest to highest)] \n\t"
+      "[-r 1/16th scale factor (lowest to highest layer)] "
       "<input_filename> <output_filename>\n",
       basename(program_name));
   exit(EXIT_FAILURE);
@@ -83,78 +84,98 @@
 static int read_frame(FILE *f, vpx_image_t *img) {
   size_t nbytes, to_read;
   int res = 1;
+  int plane;
 
-  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");
+  for (plane = 0; plane < 3; plane++) {
+    unsigned char *ptr;
+    int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
+    int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
+    int r;
+
+    switch (plane) {
+      case 1:
+        ptr = img->planes[VPX_PLANE_U];
+        break;
+      case 2:
+        ptr = img->planes[VPX_PLANE_V];
+        break;
+      default:
+        ptr = img->planes[plane];
+    }
+    for (r = 0; r < h; r++) {
+      to_read = w;
+
+      nbytes = fread(ptr, 1, to_read, f);
+      if (nbytes != to_read) {
+        res = 0;
+        if (nbytes > 0)
+          printf("Warning: Read partial frame. Check your width & height!\n");
+        break;
+      }
+      ptr += img->stride[plane];
+    }
+    if (!res) break;
   }
   return res;
 }
 
-static int read_dummy_frame(vpx_image_t *img) {
-  size_t to_read;
-
-  to_read = img->w * img->h * 3 / 2;
-  memset(img->planes[0], 129, to_read);
+static int create_dummy_frame(vpx_image_t *img) {
+  size_t buf_size;
+  buf_size = img->w * img->h * 3 / 2;
+  memset(img->planes[0], 129, buf_size);
   return 1;
 }
 
-static void write_ivf_file_header(FILE *outfile, const vpx_codec_enc_cfg_t *cfg,
-                                  int frame_cnt) {
+static void write_ivf_file_header(FILE *outfile, unsigned int width,
+                                  unsigned int height, int timebase_num,
+                                  int timebase_den, 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 */
+  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, width);        /* width */
+  mem_put_le16(header + 14, height);       /* height */
+  mem_put_le32(header + 16, timebase_den); /* rate */
+  mem_put_le32(header + 20, 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) {
+static void write_ivf_frame_header(FILE *outfile, vpx_codec_pts_t pts,
+                                   size_t sz) {
   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, sz);
   mem_put_le32(header + 4, pts & 0xFFFFFFFF);
   mem_put_le32(header + 8, pts >> 32);
 
   (void)fwrite(header, 1, 12, outfile);
 }
 
-static void check_parameters() {
-  if (number_spatial_layers > 5) die("Cannot support more than 5 layers");
-}
-
-static void parse_command_line(int argc, char **argv,
-                               vpx_codec_enc_cfg_t *cfg) {
+static void parse_command_line(int argc, char **argv, SvcContext *svc_ctx,
+                               vpx_codec_enc_cfg_t *enc_cfg) {
   unsigned int width = 1920;
   unsigned int height = 1080;
   unsigned int timebase_num = 1;
   unsigned int timebase_den = 60;
   unsigned int bitrate = 1000;
+  unsigned int number_spatial_layers = 5;
+  int use_dummy_frame = 1;
+
   int c;
   vpx_codec_err_t res;
+  int r = 0;
+  int q = 0;
 
   opterr = 0;
-  while ((c = getopt(argc, argv, "f:w:h:n:d:b:s:l:p:")) != -1) switch (c) {
+  while ((c = getopt(argc, argv, "f:w:h:n:d:b:s:l:g:r:q:z:")) != -1)
+    switch (c) {
       case 'f':
         number_frames_to_code = atoi(optarg);
         break;
@@ -179,8 +200,17 @@
       case 'l':
         number_spatial_layers = atoi(optarg);
         break;
-      case 'p':
-        key_period = atoi(optarg);
+      case 'g':
+        gop_size = atoi(optarg);
+        break;
+      case 'r':
+        scaling_factor = optarg;
+        break;
+      case 'q':
+        quantizer = optarg;
+        break;
+      case 'z':
+        use_dummy_frame = atoi(optarg);
         break;
       case '?':
         usage(argv[0]);
@@ -191,14 +221,28 @@
     usage(argv[0]);
   }
 
+  if ((r != 0 && q != 0 && r != q) || (r != number_spatial_layers && r != 0)) {
+    usage(argv[0]);
+  }
+
   input_filename = argv[optind];
   output_filename = argv[optind + 1];
 
   if (width < 16 || width % 2 || height < 16 || height % 2)
     die("Invalid resolution: %d x %d", width, height);
 
+  // initialize SvcContext
+  svc_ctx->log_level = SVC_LOG_DEBUG;
+  svc_ctx->spatial_layers = number_spatial_layers;
+  svc_ctx->encoding_mode = encoding_mode;
+  svc_ctx->gop_size = gop_size;
+  svc_ctx->quantizer_values = quantizer;
+  svc_ctx->scale_factors = scaling_factor;
+  // when using a dummy frame, that frame is only encoded to be full size
+  svc_ctx->first_frame_full_size = use_dummy_frame;
+
   /* Populate encoder configuration */
-  res = vpx_codec_enc_config_default(interface, cfg, 0);
+  res = vpx_codec_enc_config_default(interface, enc_cfg, 0);
   if (res) {
     die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
   }
@@ -206,200 +250,52 @@
       "Codec %s\nframes: %d, skip: %d, layers: %d\n"
       "width %d, height: %d, \n"
       "num: %d, den: %d, bitrate: %d, \n"
-      "key period: %d \n",
+      "gop size: %d, use_dummy_frame: %d \n",
       vpx_codec_iface_name(interface), number_frames_to_code,
       number_frames_to_skip, number_spatial_layers, width, height, timebase_num,
-      timebase_den, bitrate, key_period);
+      timebase_den, bitrate, gop_size, use_dummy_frame);
 
-  // Do minimal check at the application level. Encoder parameters will be
-  // checked internally
-  check_parameters();
-
-  cfg->rc_target_bitrate = bitrate;
-  cfg->g_w = width;
-  cfg->g_h = height;
-  cfg->g_timebase.num = timebase_num;
-  cfg->g_timebase.den = timebase_den;
-  cfg->ss_number_layers = number_spatial_layers;
-}
-
-static void set_default_configuration(vpx_codec_enc_cfg_t *cfg) {
-  /* Real time parameters */
-  cfg->rc_dropframe_thresh = 0;
-  cfg->rc_end_usage = VPX_CBR;
-  cfg->rc_resize_allowed = 0;
-  cfg->rc_min_quantizer = 33;
-  cfg->rc_max_quantizer = 33;
-  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;
-
-  /* Disable automatic keyframe placement */
-  cfg->kf_mode = VPX_KF_DISABLED;
-  cfg->kf_min_dist = cfg->kf_max_dist = 3000;
-}
-
-static void initialize_codec(vpx_codec_ctx_t *codec, vpx_codec_enc_cfg_t *cfg) {
-  int max_intra_size_pct;
-
-  /* Initialize codec */
-  if (vpx_codec_enc_init(codec, interface, cfg, VPX_CODEC_USE_PSNR))
-    die_codec(codec, "Failed to initialize encoder");
-
-  vpx_codec_control(codec, VP9E_SET_SVC, 1);
-  /* Cap CPU & first I-frame size */
-  vpx_codec_control(codec, VP8E_SET_CPUUSED, 1);
-  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);
-}
-
-static int calculate_layer(int frame_cnt, int number_spatial_layers) {
-  if (frame_cnt == 0)
-    return 0;
-  else
-    return (frame_cnt + number_spatial_layers - 1) % number_spatial_layers;
-}
-
-static void switch_to_layer(int layer, unsigned int initial_width,
-                            unsigned int initial_height,
-                            vpx_codec_ctx_t *codec) {
-  // Set layer size
-  int scaling_factor_num[MAX_LAYERS] = {2, 1, 4, 2, 1};
-  int scaling_factor_den[MAX_LAYERS] = {9, 3, 9, 3, 1};
-
-  int quantizer[MAX_LAYERS] = {60, 53, 39, 33, 27};
-
-  unsigned int current_width;
-  unsigned int current_height;
-
-  current_width = initial_width *
-                  scaling_factor_num[layer + 5 - number_spatial_layers] /
-                  scaling_factor_den[layer + 5 - number_spatial_layers];
-  current_height = initial_height *
-                   scaling_factor_num[layer + 5 - number_spatial_layers] /
-                   scaling_factor_den[layer + 5 - number_spatial_layers];
-
-  current_width += current_width % 2;
-  current_height += current_height % 2;
-
-  vpx_codec_control(codec, VP9E_SET_WIDTH, &current_width);
-  vpx_codec_control(codec, VP9E_SET_HEIGHT, &current_height);
-
-  // Set layer context
-  vpx_codec_control(codec, VP9E_SET_LAYER, &layer);
-  vpx_codec_control(codec, VP9E_SET_MAX_Q,
-                    quantizer[layer + 5 - number_spatial_layers]);
-  vpx_codec_control(codec, VP9E_SET_MIN_Q,
-                    quantizer[layer + 5 - number_spatial_layers]);
-}
-
-static int get_flag(int is_I_frame_in_layer, int layer, ENCODING_MODE mode) {
-  // First layer
-  switch (mode) {
-    case INTER_LAYER_PREDICTION_I:
-      if (is_I_frame_in_layer && layer == 0) return VPX_EFLAG_FORCE_KF;
-      if (layer == 0)
-        return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-               VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
-      else if (is_I_frame_in_layer)
-        return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-               VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST;
-      else
-        return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-               VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
-      break;
-
-    case INTER_LAYER_PREDICTION_IP:
-      if (is_I_frame_in_layer && layer == 0) return VPX_EFLAG_FORCE_KF;
-      if (layer == 0)
-        return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-               VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
-      else if (is_I_frame_in_layer)
-        return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-               VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST;
-      else
-        return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
-      break;
-
-    case USE_GOLDEN_FRAME:
-      if (is_I_frame_in_layer && layer == 0) return VPX_EFLAG_FORCE_KF;
-      if (2 * number_spatial_layers - NUM_BUFFERS <= layer) {
-        if (layer == 0)
-          return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-                 VP8_EFLAG_NO_REF_ARF;
-        else if (is_I_frame_in_layer)
-          return VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF |
-                 VP8_EFLAG_NO_REF_LAST;
-        else
-          return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
-      } else {
-        if (layer == 0)
-          return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-                 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
-        else if (is_I_frame_in_layer)
-          return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-                 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST;
-        else
-          return VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
-                 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
-      }
-      break;
-    default:
-      return VPX_EFLAG_FORCE_KF;
-  }
+  enc_cfg->rc_target_bitrate = bitrate;
+  enc_cfg->g_w = width;
+  enc_cfg->g_h = height;
+  enc_cfg->g_timebase.num = timebase_num;
+  enc_cfg->g_timebase.den = timebase_den;
 }
 
 int main(int argc, char **argv) {
-  FILE *infile, *outfile[MAX_LAYERS];
+  FILE *infile, *outfile;
   vpx_codec_ctx_t codec;
-  vpx_codec_enc_cfg_t cfg;
+  vpx_codec_enc_cfg_t enc_cfg;
+  SvcContext svc_ctx;
+  int i;
   int frame_cnt = 0;
   vpx_image_t raw;
-  int frame_avail = 1;
-  int got_data = 0;
-  int i;
-  int frames_in_layer[MAX_LAYERS] = {0};
   clock_t before;
   clock_t after;
+  vpx_codec_err_t res;
   int pts = 0;            /* PTS starts at 0 */
   int frame_duration = 1; /* 1 timebase tick per frame */
 
-  parse_command_line(argc, argv, &cfg);
+  memset(&svc_ctx, 0, sizeof(svc_ctx));
+  svc_ctx.log_print = 1;
+  parse_command_line(argc, argv, &svc_ctx, &enc_cfg);
 
   // Allocate image buffer
-  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, cfg.g_w, cfg.g_h, 32))
-    die("Failed to allocate image", cfg.g_w, cfg.g_h);
+  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
+    die("Failed to allocate image", enc_cfg.g_w, enc_cfg.g_h);
 
-  set_default_configuration(&cfg);
-
-  /* Open input file */
   if (!(infile = fopen(input_filename, "rb")))
     die("Failed to open %s for reading", argv[1]);
 
-  /* Open output file  */
-  for (i = 0; i < number_spatial_layers; i++) {
-    char file_name[512];
-    snprintf(file_name, sizeof(file_name), "%s_%d.ivf", output_filename, 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);
-  }
+  if (!(outfile = fopen(output_filename, "wb")))
+    die("Failed to open %s for writing", output_filename);
 
-  initialize_codec(&codec, &cfg);
+  // Initialize codec
+  if (vpx_svc_init(&svc_ctx, &codec, interface, &enc_cfg) != VPX_CODEC_OK)
+    die("Failed to initialize encoder");
+
+  write_ivf_file_header(outfile, enc_cfg.g_w, enc_cfg.g_h,
+                        enc_cfg.g_timebase.num, enc_cfg.g_timebase.den, 0);
 
   // skip initial frames
   for (i = 0; i < number_frames_to_skip; i++) {
@@ -407,81 +303,47 @@
   }
 
   before = clock();
-  // Encoding frames
-  while ((frame_avail || got_data) &&
-         frame_cnt <= number_frames_to_code * number_spatial_layers) {
-    int flags = 0;
-    vpx_codec_iter_t iter = NULL;
-    const vpx_codec_cx_pkt_t *pkt;
-
-    int layer = calculate_layer(frame_cnt, number_spatial_layers);
-    int is_I_frame_in_layer =
-        (((frame_cnt - 1) / number_spatial_layers % key_period) == 0);
-    int is_dummy = (frame_cnt == 0);
-
-    if (is_dummy) {  // Dummy frame
-      flags = VPX_EFLAG_FORCE_KF;
-      frame_avail = read_dummy_frame(&raw);
-
-    } else {  // Regular frame
-      // Read a new frame only at the base layer
-      if (layer == 0) frame_avail = read_frame(infile, &raw);
-      switch_to_layer(layer, cfg.g_w, cfg.g_h, &codec);
-      flags = get_flag(is_I_frame_in_layer, layer, INTER_LAYER_PREDICTION_I);
+  // Encode frames
+  while (frame_cnt <= number_frames_to_code) {
+    if (frame_cnt == 0 && svc_ctx.first_frame_full_size) {
+      create_dummy_frame(&raw);
+    } else {
+      if (!read_frame(infile, &raw)) break;
     }
-
-    // Actual Encoding
-    if (vpx_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags,
-                         VPX_DL_REALTIME))
+    res = vpx_svc_encode(&svc_ctx, &codec, &raw, pts, frame_duration,
+                         VPX_DL_REALTIME);
+    printf("%s", svc_get_message(&svc_ctx));
+    if (res != VPX_CODEC_OK) {
       die_codec(&codec, "Failed to encode frame");
-
-    got_data = 0;
-    // Process data / Get PSNR statistics
-    while ((pkt = vpx_codec_get_cx_data(&codec, &iter))) {
-      got_data = 1;
-      switch (pkt->kind) {
-        case VPX_CODEC_CX_FRAME_PKT:
-          for (i = layer; i < number_spatial_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;
-        case VPX_CODEC_PSNR_PKT:
-          if (frame_cnt != 0)
-            printf(
-                "Processed Frame %d, layer %d, PSNR(Total/Y/U/V): "
-                "%2.3f  %2.3f  %2.3f  %2.3f \n",
-                (frame_cnt - 1) / number_spatial_layers + 1, layer,
-                pkt->data.psnr.psnr[0], pkt->data.psnr.psnr[1],
-                pkt->data.psnr.psnr[2], pkt->data.psnr.psnr[3]);
-          break;
-        default:
-          break;
-      }
+    }
+    if (svc_get_frame_size(&svc_ctx) > 0) {
+      write_ivf_frame_header(outfile, pts, svc_get_frame_size(&svc_ctx));
+      (void)fwrite(svc_get_buffer(&svc_ctx), 1, svc_get_frame_size(&svc_ctx),
+                   outfile);
     }
     frame_cnt++;
-    // TODO(ivan): Modify ts later if(!layer)
     pts += frame_duration;
-  }
-  // end while
+  }  // end encode frames loop
 
   after = clock();
   printf("Processed %d frames in different resolutions in %ld ms.\n",
-         frame_cnt - 1, (int)(after - before) / (CLOCKS_PER_SEC / 1000));
+         frame_cnt - svc_ctx.first_frame_full_size,
+         (int)(after - before) / (CLOCKS_PER_SEC / 1000));
 
   fclose(infile);
-
   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 < number_spatial_layers; i++) {
-    if (!fseek(outfile[i], 0, SEEK_SET)) {
-      write_ivf_file_header(outfile[i], &cfg, frames_in_layer[i]);
-    }
-    fclose(outfile[i]);
+  // rewrite the output file headers with the actual frame count
+  if (!fseek(outfile, 0, SEEK_SET)) {
+    write_ivf_file_header(outfile, enc_cfg.g_w, enc_cfg.g_h,
+                          enc_cfg.g_timebase.num, enc_cfg.g_timebase.den,
+                          frame_cnt);
   }
+  fclose(outfile);
+
+  // display average size, psnr
+  svc_dump_statistics(&svc_ctx);
+  printf("%s", svc_get_message(&svc_ctx));
 
   return EXIT_SUCCESS;
 }
diff --git a/vpx/exports_enc b/vpx/exports_enc
index 3d56749..e6c540e 100644
--- a/vpx/exports_enc
+++ b/vpx/exports_enc
@@ -6,3 +6,12 @@
 text vpx_codec_get_global_headers
 text vpx_codec_get_preview_frame
 text vpx_codec_set_cx_data_buf
+text vpx_svc_init
+text vpx_svc_encode
+text svc_dump_statistics
+text svc_get_message
+text svc_get_buffer
+text svc_get_frame_size
+text svc_get_encode_frame_count
+text svc_is_keyframe
+text svc_set_keyframe
\ No newline at end of file
diff --git a/vpx/src/svc_encodeframe.c b/vpx/src/svc_encodeframe.c
new file mode 100644
index 0000000..c2f65a7
--- /dev/null
+++ b/vpx/src/svc_encodeframe.c
@@ -0,0 +1,793 @@
+/**
+ * @file
+ * VP9 SVC encoding support via libvpx
+ */
+
+#define VPX_DISABLE_CTRL_TYPECHECKS 1
+#define VPX_CODEC_DISABLE_COMPAT 1
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include "vpx/vpx_encoder.h"
+#include "vpx/vp8cx.h"
+#include "vpx/svc_context.h"
+
+#define SVC_REFERENCE_FRAMES 8
+
+static const char* DEFAULT_QUANTIZER_VALUES = "60,53,39,33,27";
+static const char* DEFAULT_SCALE_FACTORS = "4/16,5/16,7/16,11/16,16/16";
+static char* colors[VPX_SS_MAX_LAYERS] = {"\x1B[35m", "\x1B[32m", "\x1B[33m",
+                                          "\x1B[34m", "\x1B[36m"};
+static char* normal_color = "\x1B[0m";
+
+typedef struct SvcInternal {
+  // values extracted from options
+  int scaling_factor_num[VPX_SS_MAX_LAYERS];
+  int scaling_factor_den[VPX_SS_MAX_LAYERS];
+  int quantizer[VPX_SS_MAX_LAYERS];
+
+  // accumulated statistics
+  double psnr_in_layer[VPX_SS_MAX_LAYERS];
+  int bytes_in_layer[VPX_SS_MAX_LAYERS];
+
+  // codec encoding values
+  int width;
+  int height;
+
+  // state variables
+  int encode_frame_count;
+  int frame_within_gop;
+  vpx_enc_frame_flags_t enc_frame_flags;
+  int layers;
+  int layer;
+  int is_keyframe;
+
+  size_t frame_size;
+  size_t buffer_size;
+  void* buffer;
+
+  char message_buffer[2048];
+  vpx_codec_ctx_t* codec_ctx;
+} SvcInternal;
+
+// One encoded frame layer
+struct LayerData {
+  void* buf;    // compressed data buffer
+  size_t size;  // length of compressed data
+  struct LayerData* next;
+};
+
+// forward references
+static void svc_log_reset(SvcContext* svc_ctx);
+static int svc_log(SvcContext* svc_ctx, int level, char* fmt, ...);
+static vpx_codec_err_t svc_parse_input_parameters(SvcContext* svc_ctx);
+static int vpx_svc_dummy_frame(SvcContext* svc_ctx, SvcInternal* si);
+
+// create LayerData from encoder output
+static struct LayerData* ld_create(void* buf, size_t size) {
+  struct LayerData* layer_data;
+
+  layer_data = malloc(sizeof(struct LayerData));
+  if (layer_data == NULL) {
+    return NULL;
+  }
+  layer_data->buf = malloc(size);
+  if (layer_data->buf == NULL) {
+    return NULL;
+  }
+  memcpy(layer_data->buf, buf, size);
+  layer_data->size = size;
+  return layer_data;
+}
+
+// free LayerData
+static void ld_free(struct LayerData* layer_data) {
+  if (layer_data->buf) {
+    free(layer_data->buf);
+    layer_data->buf = NULL;
+  }
+  free(layer_data);
+}
+
+// add layer data to list
+static void ld_list_add(struct LayerData** list, struct LayerData* layer_data) {
+  struct LayerData** p = list;
+
+  while (*p != NULL) p = &(*p)->next;
+  *p = layer_data;
+  layer_data->next = NULL;
+}
+
+// get accumulated size of layer data
+static size_t ld_list_get_buffer_size(struct LayerData* list) {
+  struct LayerData* p;
+  size_t size = 0;
+
+  for (p = list; p != NULL; p = p->next) {
+    size += p->size;
+  }
+  return size;
+}
+
+// copy layer data to buffer
+static void ld_list_copy_to_buffer(struct LayerData* list, uint8_t* buffer) {
+  struct LayerData* p;
+
+  for (p = list; p != NULL; p = p->next) {
+    buffer[0] = 1;
+    memcpy(buffer, p->buf, p->size);
+    buffer += p->size;
+  }
+}
+
+// free layer data list
+static void ld_list_free(struct LayerData* list) {
+  struct LayerData* p = list;
+
+  while (p) {
+    list = list->next;
+    ld_free(p);
+    p = list;
+  }
+}
+
+// Superframe Index
+#define SUPERFRAME_SLOTS (8)
+#define SUPERFRAME_BUFFER_SIZE (SUPERFRAME_SLOTS * sizeof(uint32_t) + 2)
+
+struct Superframe {
+  int count;
+  uint32_t sizes[SUPERFRAME_SLOTS];
+  uint32_t magnitude;
+  uint8_t buffer[SUPERFRAME_BUFFER_SIZE];
+  size_t index_size;
+};
+
+static void sf_create_index(struct Superframe* sf) {
+  uint8_t marker = 0xc0;
+  int mag, mask;
+  uint8_t* bufp;
+  int i, j;
+  int this_sz;
+
+  if (sf->count == 0 || sf->count >= 8) return;
+
+  /* Add the number of frames to the marker byte */
+  marker |= sf->count - 1;
+
+  /* Choose the magnitude */
+  for (mag = 0, mask = 0xff; mag < 4; mag++) {
+    if (sf->magnitude < mask) break;
+    mask <<= 8;
+    mask |= 0xff;
+  }
+  marker |= mag << 3;
+
+  /* Write the index */
+  sf->index_size = 2 + (mag + 1) * sf->count;
+  bufp = sf->buffer;
+
+  *bufp++ = marker;
+  for (i = 0; i < sf->count; i++) {
+    this_sz = sf->sizes[i];
+
+    for (j = 0; j <= mag; j++) {
+      *bufp++ = this_sz & 0xff;
+      this_sz >>= 8;
+    }
+  }
+  *bufp++ = marker;
+}
+
+static vpx_codec_err_t svc_invalid_scale_factor(SvcContext* svc_ctx) {
+  svc_log(svc_ctx, SVC_LOG_ERROR, "svc-scale-factors: invalid value\n");
+  return VPX_CODEC_INVALID_PARAM;
+}
+
+static vpx_codec_err_t svc_parse_input_parameters(SvcContext* svc_ctx) {
+  char* input_string;
+  char* token;
+  const char* delim = ",";
+  char* save_ptr;
+  int found;
+  int i, q;
+  int64_t num, den;
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+
+  // parse quantizer_values
+  if (svc_ctx->quantizer_values == NULL ||
+      strlen(svc_ctx->quantizer_values) == 0) {
+    input_string = strdup(DEFAULT_QUANTIZER_VALUES);
+  } else {
+    input_string = strdup(svc_ctx->quantizer_values);
+  }
+
+  found = 0;
+  token = strtok_r(input_string, delim, &save_ptr);
+  for (i = 0; i < si->layers; i++) {
+    if (token != NULL) {
+      q = atoi(token);
+      if (q <= 0 || q > 100) {
+        svc_log(svc_ctx, SVC_LOG_ERROR,
+                "svc-quantizer-values: invalid value\n");
+        return VPX_CODEC_INVALID_PARAM;
+      }
+      token = strtok_r(NULL, delim, &save_ptr);
+      found = i + 1;
+    } else {
+      q = 0;
+    }
+    si->quantizer[i + VPX_SS_MAX_LAYERS - si->layers] = q;
+  }
+  free(input_string);
+  if (found != si->layers) {
+    svc_log(svc_ctx, SVC_LOG_ERROR,
+            "svc-quantizer-values: %d values required, but only %d specified\n",
+            si->layers, found);
+    return VPX_CODEC_INVALID_PARAM;
+  }
+
+  // parse scale_factors
+  if (svc_ctx->scale_factors == NULL || strlen(svc_ctx->scale_factors) == 0) {
+    input_string = strdup(DEFAULT_SCALE_FACTORS);
+  } else {
+    input_string = strdup(svc_ctx->scale_factors);
+  }
+  found = 0;
+  token = strtok_r(input_string, delim, &save_ptr);
+  for (i = 0; i < si->layers; i++) {
+    num = den = 1;
+    if (token != NULL) {
+      num = strtol(token, &token, 10);
+      if (num <= 0) return svc_invalid_scale_factor(svc_ctx);
+      if (*token++ != '/') return svc_invalid_scale_factor(svc_ctx);
+      den = strtol(token, &token, 10);
+      if (den <= 0) return svc_invalid_scale_factor(svc_ctx);
+
+      token = strtok_r(NULL, delim, &save_ptr);
+      found = i + 1;
+    }
+    si->scaling_factor_num[i + VPX_SS_MAX_LAYERS - si->layers] = (int)num;
+    si->scaling_factor_den[i + VPX_SS_MAX_LAYERS - si->layers] = (int)den;
+  }
+  free(input_string);
+  if (found != si->layers) {
+    svc_log(svc_ctx, SVC_LOG_ERROR,
+            "svc-scale-factors: %d values required, but only %d specified\n",
+            si->layers, found);
+    return VPX_CODEC_INVALID_PARAM;
+  }
+  return VPX_CODEC_OK;
+}
+
+vpx_codec_err_t vpx_svc_init(SvcContext* svc_ctx, vpx_codec_ctx_t* codec_ctx,
+                             vpx_codec_iface_t* iface,
+                             vpx_codec_enc_cfg_t* enc_cfg) {
+  int max_intra_size_pct;
+  vpx_codec_err_t res;
+  SvcInternal* si;
+
+  if (svc_ctx->internal == NULL) {
+    svc_ctx->internal = malloc(sizeof(SvcInternal));
+  }
+  si = (SvcInternal*)svc_ctx->internal;
+  memset(si, 0, sizeof(SvcInternal));
+  si->codec_ctx = codec_ctx;
+
+  if (svc_ctx->spatial_layers < 1 ||
+      svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS) {
+    svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n",
+            svc_ctx->spatial_layers);
+    return VPX_CODEC_INVALID_PARAM;
+  }
+
+  if (svc_ctx->gop_size < 2) {
+    svc_log(svc_ctx, SVC_LOG_ERROR, "gop_size too small: %d\n",
+            svc_ctx->gop_size);
+    return VPX_CODEC_INVALID_PARAM;
+  }
+
+  si->width = enc_cfg->g_w;
+  si->height = enc_cfg->g_h;
+
+  // use SvcInternal value for number of layers to enable forcing one layer
+  // for first frame
+  si->layers = svc_ctx->spatial_layers;
+
+  // parse quantizer values and scale factors
+  res = svc_parse_input_parameters(svc_ctx);
+  if (res != VPX_CODEC_OK) return res;
+
+  // initialize encoder configuration
+  enc_cfg->ss_number_layers = si->layers;
+  // force single pass
+  enc_cfg->g_pass = VPX_RC_ONE_PASS;
+  // Lag in frames not currently supported
+  enc_cfg->g_lag_in_frames = 0;
+
+  // TODO(ivanmaltz): determine if these values need to be set explicitly for
+  // svc, or if the normal default/override mechanism can be used
+  enc_cfg->rc_dropframe_thresh = 0;
+  enc_cfg->rc_end_usage = VPX_CBR;
+  enc_cfg->rc_resize_allowed = 0;
+  enc_cfg->rc_min_quantizer = 33;
+  enc_cfg->rc_max_quantizer = 33;
+  enc_cfg->rc_undershoot_pct = 100;
+  enc_cfg->rc_overshoot_pct = 15;
+  enc_cfg->rc_buf_initial_sz = 500;
+  enc_cfg->rc_buf_optimal_sz = 600;
+  enc_cfg->rc_buf_sz = 1000;
+
+  enc_cfg->g_error_resilient = 1;
+  enc_cfg->kf_mode = VPX_KF_DISABLED;
+  enc_cfg->kf_min_dist = enc_cfg->kf_max_dist = 3000;
+
+  // Initialize codec
+  res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR);
+  if (res != VPX_CODEC_OK) {
+    svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n");
+    return res;
+  }
+
+  vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1);
+
+  // Cap CPU & first I-frame size
+  vpx_codec_control(codec_ctx, VP8E_SET_CPUUSED, 1);
+  vpx_codec_control(codec_ctx, VP8E_SET_STATIC_THRESHOLD, 1);
+  vpx_codec_control(codec_ctx, VP8E_SET_NOISE_SENSITIVITY, 1);
+  vpx_codec_control(codec_ctx, VP8E_SET_TOKEN_PARTITIONS, 1);
+
+  max_intra_size_pct =
+      (int)(((double)enc_cfg->rc_buf_optimal_sz * 0.5) *
+            ((double)enc_cfg->g_timebase.den / enc_cfg->g_timebase.num) / 10.0);
+  vpx_codec_control(codec_ctx, VP8E_SET_MAX_INTRA_BITRATE_PCT,
+                    max_intra_size_pct);
+  return VPX_CODEC_OK;
+}
+
+// SVC Algorithm flags - these get mapped to VP8_EFLAG_* defined in vp8cx.h
+
+// encoder should reference the last frame
+#define USE_LAST (1 << 0)
+
+// encoder should reference the alt ref frame
+#define USE_ARF (1 << 1)
+
+// encoder should reference the golden frame
+#define USE_GF (1 << 2)
+
+// encoder should copy current frame to the last frame buffer
+#define UPDATE_LAST (1 << 3)
+
+// encoder should copy current frame to the alt ref frame buffer
+#define UPDATE_ARF (1 << 4)
+
+// encoder should copy current frame to the golden frame
+#define UPDATE_GF (1 << 5)
+
+static int map_vp8_flags(int svc_flags) {
+  int flags = 0;
+
+  if (!(svc_flags & USE_LAST)) flags |= VP8_EFLAG_NO_REF_LAST;
+
+  if (!(svc_flags & USE_ARF)) flags |= VP8_EFLAG_NO_REF_ARF;
+
+  if (!(svc_flags & USE_GF)) flags |= VP8_EFLAG_NO_REF_GF;
+
+  if (svc_flags & UPDATE_LAST) {
+    // last is updated automatically
+  } else {
+    flags |= VP8_EFLAG_NO_UPD_LAST;
+  }
+
+  if (svc_flags & UPDATE_ARF) {
+    flags |= VP8_EFLAG_FORCE_ARF;
+  } else {
+    flags |= VP8_EFLAG_NO_UPD_ARF;
+  }
+
+  if (svc_flags & UPDATE_GF) {
+    flags |= VP8_EFLAG_FORCE_GF;
+  } else {
+    flags |= VP8_EFLAG_NO_UPD_GF;
+  }
+
+  return flags;
+}
+
+static void calculate_enc_frame_flags(SvcContext* svc_ctx) {
+  vpx_enc_frame_flags_t flags = VPX_EFLAG_FORCE_KF;
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+
+  int is_keyframe = (si->frame_within_gop == 0);
+
+  // keyframe layer zero is identical for all modes
+  if ((is_keyframe && si->layer == 0) || vpx_svc_dummy_frame(svc_ctx, si)) {
+    si->enc_frame_flags = VPX_EFLAG_FORCE_KF;
+    return;
+  }
+
+  switch (svc_ctx->encoding_mode) {
+    case ALT_INTER_LAYER_PREDICTION_IP:
+      if (si->layer == 0) {
+        flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
+      } else if (is_keyframe) {
+        if (si->layer == si->layers - 1) {
+          flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
+        } else {
+          flags = map_vp8_flags(USE_ARF | UPDATE_LAST | UPDATE_GF);
+        }
+      } else {
+        flags = map_vp8_flags(USE_LAST | USE_ARF | UPDATE_LAST);
+      }
+      break;
+    case INTER_LAYER_PREDICTION_I:
+      if (si->layer == 0) {
+        flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
+      } else if (is_keyframe) {
+        flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
+      } else {
+        flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
+      }
+      break;
+    case INTER_LAYER_PREDICTION_IP:
+      if (si->layer == 0) {
+        flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
+      } else if (is_keyframe) {
+        flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
+      } else {
+        flags = map_vp8_flags(USE_LAST | USE_ARF | UPDATE_LAST);
+      }
+      break;
+    case USE_GOLDEN_FRAME:
+      if (2 * si->layers - SVC_REFERENCE_FRAMES <= si->layer) {
+        if (si->layer == 0) {
+          flags = map_vp8_flags(USE_LAST | USE_GF | UPDATE_LAST);
+        } else if (is_keyframe) {
+          flags = map_vp8_flags(USE_ARF | UPDATE_LAST | UPDATE_GF);
+        } else {
+          flags = map_vp8_flags(USE_LAST | USE_ARF | USE_GF | UPDATE_LAST);
+        }
+      } else {
+        if (si->layer == 0) {
+          flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
+        } else if (is_keyframe) {
+          flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
+        } else {
+          flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
+        }
+      }
+      break;
+    default:
+      svc_log(svc_ctx, SVC_LOG_ERROR, "unexpected encoding mode: %d\n",
+              svc_ctx->encoding_mode);
+      break;
+  }
+  si->enc_frame_flags = flags;
+}
+
+vpx_codec_err_t svc_get_layer_resolution(SvcContext* svc_ctx, int layer,
+                                         unsigned int* width,
+                                         unsigned int* height) {
+  int w, h, index, num, den;
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+
+  if (layer < 0 || layer >= si->layers) return VPX_CODEC_INVALID_PARAM;
+
+  index = layer + VPX_SS_MAX_LAYERS - si->layers;
+  num = si->scaling_factor_num[index];
+  den = si->scaling_factor_den[index];
+  if (num == 0 || den == 0) return VPX_CODEC_INVALID_PARAM;
+
+  w = si->width * num / den;
+  h = si->height * num / den;
+
+  // make height and width even to make chrome player happy
+  w += w % 2;
+  h += h % 2;
+
+  *width = w;
+  *height = h;
+
+  return VPX_CODEC_OK;
+}
+
+static void set_svc_parameters(SvcContext* svc_ctx,
+                               vpx_codec_ctx_t* codec_ctx) {
+  int layer, layer_index;
+  vpx_svc_parameters_t svc_params;
+  int use_higher_layer;
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+
+  memset(&svc_params, 0, sizeof(svc_params));
+  svc_params.layer = si->layer;
+  svc_params.flags = si->enc_frame_flags;
+
+  layer = si->layer;
+  if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
+      si->frame_within_gop == 0) {
+    // layers 1 & 3 don't exist in this mode, use the higher one
+    if (layer == 0 || layer == 2) {
+      layer += 1;
+    }
+  }
+  if (VPX_CODEC_OK != svc_get_layer_resolution(svc_ctx, layer,
+                                               &svc_params.width,
+                                               &svc_params.height)) {
+    svc_log(svc_ctx, SVC_LOG_ERROR, "svc_get_layer_resolution failed\n");
+  }
+  layer_index = layer + VPX_SS_MAX_LAYERS - si->layers;
+  svc_params.min_quantizer = si->quantizer[layer_index];
+  svc_params.max_quantizer = si->quantizer[layer_index];
+  svc_params.distance_from_i_frame = si->frame_within_gop;
+
+  // Use buffer i for layer i LST
+  svc_params.lst_fb_idx = si->layer;
+
+  // Use buffer i-1 for layer i Alt (Inter-layer prediction)
+  if (si->layer != 0) {
+    use_higher_layer =
+        svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
+        si->frame_within_gop == 0;
+    svc_params.alt_fb_idx = use_higher_layer ? si->layer - 2 : si->layer - 1;
+  }
+
+  if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP) {
+    svc_params.gld_fb_idx = si->layer + 1;
+  } else {
+    if (si->layer < 2 * si->layers - SVC_REFERENCE_FRAMES)
+      svc_params.gld_fb_idx = svc_params.lst_fb_idx;
+    else
+      svc_params.gld_fb_idx = 2 * si->layers - 1 - si->layer;
+  }
+
+  svc_log(svc_ctx, SVC_LOG_DEBUG, "%sSVC frame: %d, layer: %d, %dx%d, q: %d\n",
+          svc_ctx->log_print ? colors[si->layer] : "",  //
+          si->encode_frame_count, si->layer, svc_params.width,
+          svc_params.height, svc_params.min_quantizer);
+
+  if (svc_params.flags == VPX_EFLAG_FORCE_KF) {
+    svc_log(svc_ctx, SVC_LOG_DEBUG, "flags == VPX_EFLAG_FORCE_KF\n");
+  } else {
+    svc_log(
+        svc_ctx, SVC_LOG_DEBUG, "Using:    LST/GLD/ALT [%2d|%2d|%2d]\n",
+        svc_params.flags & VP8_EFLAG_NO_REF_LAST ? -1 : svc_params.lst_fb_idx,
+        svc_params.flags & VP8_EFLAG_NO_REF_GF ? -1 : svc_params.gld_fb_idx,
+        svc_params.flags & VP8_EFLAG_NO_REF_ARF ? -1 : svc_params.alt_fb_idx);
+    svc_log(
+        svc_ctx, SVC_LOG_DEBUG, "Updating: LST/GLD/ALT [%2d|%2d|%2d]\n",
+        svc_params.flags & VP8_EFLAG_NO_UPD_LAST ? -1 : svc_params.lst_fb_idx,
+        svc_params.flags & VP8_EFLAG_NO_UPD_GF ? -1 : svc_params.gld_fb_idx,
+        svc_params.flags & VP8_EFLAG_NO_UPD_ARF ? -1 : svc_params.alt_fb_idx);
+  }
+
+  vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &svc_params);
+}
+
+/**
+ * Helper to check if the current frame is the first, full resolution dummy.
+ */
+static int vpx_svc_dummy_frame(SvcContext* svc_ctx, SvcInternal* si) {
+  return svc_ctx->first_frame_full_size == 1 && si->encode_frame_count == 0;
+}
+
+/**
+ * Encode a frame into multiple layers
+ * Create a superframe containing the individual layers
+ */
+vpx_codec_err_t vpx_svc_encode(SvcContext* svc_ctx, vpx_codec_ctx_t* codec_ctx,
+                               struct vpx_image* rawimg, vpx_codec_pts_t pts,
+                               int64_t duration, int deadline) {
+  vpx_codec_err_t res;
+  vpx_codec_iter_t iter;
+  const vpx_codec_cx_pkt_t* cx_pkt;
+  struct LayerData* cx_layer_list = NULL;
+  struct LayerData* layer_data;
+  size_t frame_pkt_size;
+  struct Superframe superframe;
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+
+  memset(&superframe, 0, sizeof(superframe));
+  svc_log_reset(svc_ctx);
+
+  si->layers = vpx_svc_dummy_frame(svc_ctx, si) ? 1 : svc_ctx->spatial_layers;
+  if (si->frame_within_gop >= svc_ctx->gop_size ||
+      si->encode_frame_count == 0 ||
+      (si->encode_frame_count == 1 && svc_ctx->first_frame_full_size == 1)) {
+    si->frame_within_gop = 0;
+  }
+  si->is_keyframe = (si->frame_within_gop == 0);
+  si->frame_size = 0;
+
+  svc_log(svc_ctx, SVC_LOG_DEBUG,
+          "vpx_svc_encode  layers: %d, frame_count: %d, frame_within_gop: %d\n",
+          si->layers, si->encode_frame_count, si->frame_within_gop);
+
+  // encode each layer
+  for (si->layer = 0; si->layer < si->layers; si->layer++) {
+    if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
+        si->is_keyframe && (si->layer == 1 || si->layer == 3)) {
+      svc_log(svc_ctx, SVC_LOG_DEBUG, "Skip encoding layer %d\n", si->layer);
+      continue;
+    }
+    calculate_enc_frame_flags(svc_ctx);
+
+    if (vpx_svc_dummy_frame(svc_ctx, si)) {
+      // do not set svc parameters, use normal encode
+      svc_log(svc_ctx, SVC_LOG_DEBUG, "encoding full size first frame\n");
+    } else {
+      set_svc_parameters(svc_ctx, codec_ctx);
+    }
+    res = vpx_codec_encode(codec_ctx, rawimg, pts, duration,
+                           si->enc_frame_flags, deadline);
+    if (res != VPX_CODEC_OK) {
+      return res;
+    }
+    // save compressed data
+    iter = NULL;
+    while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) {
+      switch (cx_pkt->kind) {
+        case VPX_CODEC_CX_FRAME_PKT:
+          frame_pkt_size = cx_pkt->data.frame.sz;
+          if (!vpx_svc_dummy_frame(svc_ctx, si)) {
+            si->bytes_in_layer[si->layer] += frame_pkt_size;
+
+            svc_log(svc_ctx, SVC_LOG_DEBUG,
+                    "SVC frame: %d, layer: %d, size: %ld%s\n",
+                    si->encode_frame_count, si->layer, frame_pkt_size,
+                    svc_ctx->log_print ? normal_color : "");
+          }
+          layer_data = ld_create(cx_pkt->data.frame.buf, frame_pkt_size);
+          if (layer_data == NULL) {
+            svc_log(svc_ctx, SVC_LOG_ERROR, "Error allocating LayerData\n");
+            return 0;
+          }
+          ld_list_add(&cx_layer_list, layer_data);
+
+          // save layer size in superframe index
+          superframe.sizes[superframe.count++] = frame_pkt_size;
+          superframe.magnitude |= frame_pkt_size;
+          break;
+        case VPX_CODEC_PSNR_PKT:
+          if (!vpx_svc_dummy_frame(svc_ctx, si)) {
+            svc_log(svc_ctx, SVC_LOG_DEBUG,
+                    "%sSVC frame: %d, layer: %d, PSNR(Total/Y/U/V): "
+                    "%2.3f  %2.3f  %2.3f  %2.3f \n",
+                    svc_ctx->log_print ? colors[si->layer] : "",
+                    si->encode_frame_count, si->layer,
+                    cx_pkt->data.psnr.psnr[0], cx_pkt->data.psnr.psnr[1],
+                    cx_pkt->data.psnr.psnr[2], cx_pkt->data.psnr.psnr[3]);
+            si->psnr_in_layer[si->layer] += cx_pkt->data.psnr.psnr[0];
+          }
+          break;
+        default:
+          break;
+      }
+    }
+  }
+  // add superframe index to layer data list
+  if (!vpx_svc_dummy_frame(svc_ctx, si)) {
+    sf_create_index(&superframe);
+    layer_data = ld_create(superframe.buffer, superframe.index_size);
+    ld_list_add(&cx_layer_list, layer_data);
+  }
+  // get accumulated size of layer data
+  si->frame_size = ld_list_get_buffer_size(cx_layer_list);
+  if (si->frame_size == 0) return VPX_CODEC_ERROR;
+
+  // all layers encoded, create single buffer with concatenated layers
+  if (si->frame_size > si->buffer_size) {
+    free(si->buffer);
+    si->buffer = malloc(si->frame_size);
+    si->buffer_size = si->frame_size;
+  }
+  // copy layer data into packet
+  ld_list_copy_to_buffer(cx_layer_list, si->buffer);
+
+  ld_list_free(cx_layer_list);
+
+  svc_log(svc_ctx, SVC_LOG_DEBUG,
+          "SVC frame: %d, kf: %d, size: %ld, pts: %ld\n",
+          si->encode_frame_count, si->is_keyframe, si->frame_size, pts);
+  si->frame_within_gop++;
+  si->encode_frame_count++;
+
+  return VPX_CODEC_OK;
+}
+
+char* svc_get_message(SvcContext* svc_ctx) {
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+  return si->message_buffer;
+}
+
+void* svc_get_buffer(SvcContext* svc_ctx) {
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+  return si->buffer;
+}
+
+int svc_get_frame_size(SvcContext* svc_ctx) {
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+  return si->frame_size;
+}
+
+int svc_get_encode_frame_count(SvcContext* svc_ctx) {
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+  return si->encode_frame_count;
+}
+
+int svc_is_keyframe(SvcContext* svc_ctx) {
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+  return (si->is_keyframe);
+}
+
+void svc_set_keyframe(SvcContext* svc_ctx) {
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+  si->frame_within_gop = 0;
+}
+
+void svc_dump_statistics(SvcContext* svc_ctx) {
+  int number_of_frames, number_of_keyframes, encode_frame_count;
+  int i;
+  int bytes_total = 0;
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+
+  svc_log_reset(svc_ctx);
+
+  encode_frame_count = si->encode_frame_count;
+  if (svc_ctx->first_frame_full_size) encode_frame_count--;
+  if (si->encode_frame_count <= 0) return;
+
+  svc_log(svc_ctx, SVC_LOG_INFO, "\n");
+  number_of_keyframes = encode_frame_count / svc_ctx->gop_size + 1;
+  for (i = 0; i < si->layers; i++) {
+    number_of_frames = encode_frame_count;
+
+    if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
+        (i == 1 || i == 3)) {
+      number_of_frames -= number_of_keyframes;
+    }
+    svc_log(svc_ctx, SVC_LOG_INFO, "Layer %d PSNR=[%2.3f], Bytes=[%d]\n", i,
+            (double)si->psnr_in_layer[i] / number_of_frames,
+            si->bytes_in_layer[i]);
+    bytes_total += si->bytes_in_layer[i];
+  }
+
+  // only display statistics once
+  si->encode_frame_count = 0;
+
+  svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%d]\n", bytes_total);
+}
+
+static void svc_log_reset(SvcContext* svc_ctx) {
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+  si->message_buffer[0] = '\0';
+}
+
+static int svc_log(SvcContext* svc_ctx, int level, char* fmt, ...) {
+  char buf[512];
+  int retval = 0;
+  va_list ap;
+  SvcInternal* si = (SvcInternal*)svc_ctx->internal;
+
+  if (level > svc_ctx->log_level) {
+    return retval;
+  }
+
+  va_start(ap, fmt);
+  retval = vsnprintf(buf, sizeof(buf), fmt, ap);
+  va_end(ap);
+
+  if (svc_ctx->log_print) {
+    printf("%s", buf);
+  } else {
+    strncat(si->message_buffer, buf,
+            sizeof(si->message_buffer) - strlen(si->message_buffer) - 1);
+  }
+
+  if (level == SVC_LOG_ERROR) {
+    si->codec_ctx->err_detail = si->message_buffer;
+  }
+  return retval;
+}
diff --git a/vpx/svc_context.h b/vpx/svc_context.h
new file mode 100644
index 0000000..a0653f0
--- /dev/null
+++ b/vpx/svc_context.h
@@ -0,0 +1,62 @@
+/**
+ * SvcContext - input parameters and state to encode a multi-layered
+ * spatial SVC frame
+ */
+
+#ifndef VPX_SVC_CONTEXT_H_
+#define VPX_SVC_CONTEXT_H_
+
+typedef enum SVC_ENCODING_MODE {
+  INTER_LAYER_PREDICTION_I,
+  ALT_INTER_LAYER_PREDICTION_IP,
+  INTER_LAYER_PREDICTION_IP,
+  USE_GOLDEN_FRAME
+} SVC_ENCODING_MODE;
+
+typedef enum SVC_LOG_LEVEL {
+  SVC_LOG_ERROR,
+  SVC_LOG_INFO,
+  SVC_LOG_DEBUG,
+} SVC_LOG_LEVEL;
+
+typedef struct {
+  // public interface to svc_command options
+  int enabled;                      // set to non-zero to enable svc encoding
+  int spatial_layers;               // number of layers
+  int first_frame_full_size;        // set to one to force first frame full size
+  SVC_ENCODING_MODE encoding_mode;  // svc encoding strategy
+
+  // the following lists are ordered from highest resolution to lowest
+  // if the strings are null, default values are used
+  const char* quantizer_values;  // quantizer values, e.g., "27,33,39,53,60"
+  const char* scale_factors;     // layer scale factors, e.g.,
+                                 // "16/16,11/16,7/16,5/16,4/16"
+  int gop_size;                  // distance between keyframes
+
+  SVC_LOG_LEVEL log_level;  // amount of information to display
+  int log_print;  // when set, printf log messages instead of returning the
+                  // message with svc_get_message
+
+  // private storage for vpx_svc_encode
+  void* internal;
+} SvcContext;
+
+vpx_codec_err_t vpx_svc_init(SvcContext* svc_ctx, vpx_codec_ctx_t* codec_ctx,
+                             vpx_codec_iface_t* iface,
+                             vpx_codec_enc_cfg_t* cfg);
+
+vpx_codec_err_t vpx_svc_encode(SvcContext* svc_ctx, vpx_codec_ctx_t* codec_ctx,
+                               struct vpx_image* rawimg, vpx_codec_pts_t pts,
+                               int64_t duration, int deadline);
+
+void svc_dump_statistics(SvcContext* svc_ctx);
+char* svc_get_message(SvcContext* svc_ctx);
+void* svc_get_buffer(SvcContext* svc_ctx);
+vpx_codec_err_t svc_get_layer_resolution(SvcContext* svc_ctx, int layer,
+                              unsigned int* width, unsigned int* height);
+int svc_get_frame_size(SvcContext* svc_ctx);
+int svc_get_encode_frame_count(SvcContext* svc_ctx);
+int svc_is_keyframe(SvcContext* svc_ctx);
+void svc_set_keyframe(SvcContext* svc_ctx);
+
+#endif /* VPX_SVC_CONTEXT_H_ */
diff --git a/vpx/vp8.h b/vpx/vp8.h
index ff71503..57d3cae 100644
--- a/vpx/vp8.h
+++ b/vpx/vp8.h
@@ -8,7 +8,6 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-
 /*!\defgroup vp8 VP8
  * \ingroup codecs
  * VP8 is vpx's newest video compression algorithm that uses motion
@@ -31,6 +30,9 @@
 #ifndef VP8_H
 #define VP8_H
 
+#include "./vpx_codec.h"
+#include "./vpx_image.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/vpx/vp8cx.h b/vpx/vp8cx.h
index 92fdb00..4bd7103 100644
--- a/vpx/vp8cx.h
+++ b/vpx/vp8cx.h
@@ -195,13 +195,8 @@
   VP9E_SET_TILE_ROWS,
   VP9E_SET_FRAME_PARALLEL_DECODING,
 
-  VP9E_SET_WIDTH              = 99,
-  VP9E_SET_HEIGHT,
-  VP9E_SET_LAYER,
   VP9E_SET_SVC,
-
-  VP9E_SET_MAX_Q,
-  VP9E_SET_MIN_Q
+  VP9E_SET_SVC_PARAMETERS
 };
 
 /*!\brief vpx 1-D scaling mode
@@ -283,6 +278,23 @@
   VP8_TUNE_SSIM
 } vp8e_tuning;
 
+/*!\brief  vp9 svc parameters
+ *
+ * This defines parameters for svc encoding.
+ *
+ */
+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 flags;                  /**<  encode frame flags */
+  int max_quantizer;          /**< max quantizer for current layer */
+  int min_quantizer;          /**< min quantizer for current layer */
+  int distance_from_i_frame;  /**< frame number within current gop */
+  int lst_fb_idx;             /**< last frame frame buffer index */
+  int gld_fb_idx;             /**< golden frame frame buffer index */
+  int alt_fb_idx;             /**< alt reference frame frame buffer index */
+} vpx_svc_parameters_t;
 
 /*!\brief VP8 encoder control function parameter type
  *
@@ -303,11 +315,8 @@
 VPX_CTRL_USE_TYPE(VP8E_SET_ACTIVEMAP,          vpx_active_map_t *)
 VPX_CTRL_USE_TYPE(VP8E_SET_SCALEMODE,          vpx_scaling_mode_t *)
 
-VPX_CTRL_USE_TYPE(VP9E_SET_LAYER,              int *)
 VPX_CTRL_USE_TYPE(VP9E_SET_SVC,                int)
-
-VPX_CTRL_USE_TYPE(VP9E_SET_WIDTH,              unsigned int *)
-VPX_CTRL_USE_TYPE(VP9E_SET_HEIGHT,             unsigned int *)
+VPX_CTRL_USE_TYPE(VP9E_SET_SVC_PARAMETERS,     vpx_svc_parameters_t *)
 
 VPX_CTRL_USE_TYPE(VP8E_SET_CPUUSED,            int)
 VPX_CTRL_USE_TYPE(VP8E_SET_ENABLEAUTOALTREF,   unsigned int)
@@ -334,8 +343,6 @@
 
 VPX_CTRL_USE_TYPE(VP9E_SET_FRAME_PARALLEL_DECODING, unsigned int)
 
-VPX_CTRL_USE_TYPE(VP9E_SET_MAX_Q,      unsigned int)
-VPX_CTRL_USE_TYPE(VP9E_SET_MIN_Q,      unsigned int)
 /*! @} - end defgroup vp8_encoder */
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/vpx/vpx_codec.mk b/vpx/vpx_codec.mk
index 3d5510f..549c249 100644
--- a/vpx/vpx_codec.mk
+++ b/vpx/vpx_codec.mk
@@ -15,6 +15,8 @@
 API_SRCS-$(CONFIG_VP8_ENCODER) += vp8cx.h
 API_DOC_SRCS-$(CONFIG_VP8_ENCODER) += vp8.h
 API_DOC_SRCS-$(CONFIG_VP8_ENCODER) += vp8cx.h
+API_SRCS-$(CONFIG_VP9_ENCODER) += src/svc_encodeframe.c
+API_SRCS-$(CONFIG_VP9_ENCODER) += svc_context.h
 
 API_SRCS-$(CONFIG_VP8_DECODER) += vp8.h
 API_SRCS-$(CONFIG_VP8_DECODER) += vp8dx.h