Merge "add convolution function with adjustable length" into nextgenv2
diff --git a/configure b/configure
index 458feec..c93c22c 100755
--- a/configure
+++ b/configure
@@ -611,7 +611,11 @@
           ;;
           *) check_add_cflags -Wunused-but-set-variable ;;
         esac
-        enabled extra_warnings || check_add_cflags -Wno-unused-function
+        if enabled mips || [ -z "${INLINE}" ]; then
+          enabled extra_warnings || check_add_cflags -Wno-unused-function
+        else
+          check_add_cflags -Wunused-function
+        fi
     fi
 
     if enabled icc; then
diff --git a/test/byte_alignment_test.cc b/test/byte_alignment_test.cc
index aa4b78b..3a808b0 100644
--- a/test/byte_alignment_test.cc
+++ b/test/byte_alignment_test.cc
@@ -21,14 +21,14 @@
 
 namespace {
 
+#if CONFIG_WEBM_IO
+
 const int kLegacyByteAlignment = 0;
 const int kLegacyYPlaneByteAlignment = 32;
 const int kNumPlanesToCheck = 3;
 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
 const char kVP9Md5File[] = "vp90-2-02-size-lf-1920x1080.webm.md5";
 
-#if CONFIG_WEBM_IO
-
 struct ByteAlignmentTestParam {
   int byte_alignment;
   vpx_codec_err_t expected_value;
diff --git a/test/dct16x16_test.cc b/test/dct16x16_test.cc
index 332210d..0449b52 100644
--- a/test/dct16x16_test.cc
+++ b/test/dct16x16_test.cc
@@ -293,6 +293,7 @@
   vp9_highbd_iht16x16_256_add_c(in, out, stride, tx_type, 12);
 }
 
+#if HAVE_SSE2
 void idct16x16_10_add_10_c(const tran_low_t *in, uint8_t *out, int stride) {
   vpx_highbd_idct16x16_10_add_c(in, out, stride, 10);
 }
@@ -301,7 +302,6 @@
   vpx_highbd_idct16x16_10_add_c(in, out, stride, 12);
 }
 
-#if HAVE_SSE2
 void idct16x16_256_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
   vpx_highbd_idct16x16_256_add_sse2(in, out, stride, 10);
 }
diff --git a/test/dct32x32_test.cc b/test/dct32x32_test.cc
index f7327b1..2dac10b 100644
--- a/test/dct32x32_test.cc
+++ b/test/dct32x32_test.cc
@@ -81,10 +81,6 @@
     Trans32x32Param;
 
 #if CONFIG_VP9_HIGHBITDEPTH
-void idct32x32_8(const tran_low_t *in, uint8_t *out, int stride) {
-  vpx_highbd_idct32x32_1024_add_c(in, out, stride, 8);
-}
-
 void idct32x32_10(const tran_low_t *in, uint8_t *out, int stride) {
   vpx_highbd_idct32x32_1024_add_c(in, out, stride, 10);
 }
diff --git a/test/external_frame_buffer_test.cc b/test/external_frame_buffer_test.cc
index d02dca2..2570f44 100644
--- a/test/external_frame_buffer_test.cc
+++ b/test/external_frame_buffer_test.cc
@@ -24,7 +24,6 @@
 namespace {
 
 const int kVideoNameParam = 1;
-const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
 
 struct ExternalFrameBuffer {
   uint8_t *data;
@@ -155,6 +154,8 @@
   ExternalFrameBuffer *ext_fb_list_;
 };
 
+#if CONFIG_WEBM_IO
+
 // Callback used by libvpx to request the application to return a frame
 // buffer of at least |min_size| in bytes.
 int get_vp9_frame_buffer(void *user_priv, size_t min_size,
@@ -197,6 +198,8 @@
   return 0;
 }
 
+#endif  // CONFIG_WEBM_IO
+
 // Class for testing passing in external frame buffers to libvpx.
 class ExternalFrameBufferMD5Test
     : public ::libvpx_test::DecoderTest,
@@ -278,6 +281,8 @@
 };
 
 #if CONFIG_WEBM_IO
+const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
+
 // Class for testing passing in external frame buffers to libvpx.
 class ExternalFrameBufferTest : public ::testing::Test {
  protected:
diff --git a/test/fdct8x8_test.cc b/test/fdct8x8_test.cc
index c0deaf4..72d2aed 100644
--- a/test/fdct8x8_test.cc
+++ b/test/fdct8x8_test.cc
@@ -107,6 +107,8 @@
   vp9_highbd_iht8x8_64_add_c(in, out, stride, tx_type, 12);
 }
 
+#if HAVE_SSE2
+
 void idct8x8_10_add_10_c(const tran_low_t *in, uint8_t *out, int stride) {
   vpx_highbd_idct8x8_10_add_c(in, out, stride, 10);
 }
@@ -115,7 +117,6 @@
   vpx_highbd_idct8x8_10_add_c(in, out, stride, 12);
 }
 
-#if HAVE_SSE2
 void idct8x8_10_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
   vpx_highbd_idct8x8_10_add_sse2(in, out, stride, 10);
 }
diff --git a/test/vp10_txfm_test.h b/test/vp10_txfm_test.h
index 967d38b..a3a4258 100644
--- a/test/vp10_txfm_test.h
+++ b/test/vp10_txfm_test.h
@@ -59,8 +59,8 @@
     reference_adst_1d(in, out, size);
 }
 
-static void reference_hybrid_2d(double* in, double* out, int size, int type0,
-                                int type1) {
+static INLINE void reference_hybrid_2d(double* in, double* out, int size,
+                                       int type0, int type1) {
   double* tempOut = new double[size * size];
 
   for (int r = 0; r < size; r++) {
diff --git a/vp10/common/blockd.h b/vp10/common/blockd.h
index 2e87903..4a3e67c 100644
--- a/vp10/common/blockd.h
+++ b/vp10/common/blockd.h
@@ -401,6 +401,7 @@
 #define MAX_ANGLE_DELTAS 3
 #define ANGLE_FAST_SEARCH 1
 #define ANGLE_SKIP_THRESH 0.10
+#define FILTER_FAST_SEARCH 1
 
 static uint8_t mode_to_angle_map[INTRA_MODES] = {
     0, 90, 180, 45, 135, 111, 157, 203, 67, 0,
diff --git a/vp10/common/mvref_common.h b/vp10/common/mvref_common.h
index 4187247..3a77147 100644
--- a/vp10/common/mvref_common.h
+++ b/vp10/common/mvref_common.h
@@ -218,14 +218,15 @@
 }
 
 #if CONFIG_REF_MV
-static int8_t vp10_ref_frame_type(const MV_REFERENCE_FRAME *const rf) {
+static INLINE int8_t vp10_ref_frame_type(const MV_REFERENCE_FRAME *const rf) {
   if (rf[1] > INTRA_FRAME)
     return rf[0] + ALTREF_FRAME;
 
   return rf[0];
 }
 
-static void vp10_set_ref_frame(MV_REFERENCE_FRAME *rf, int8_t ref_frame_type) {
+static INLINE void vp10_set_ref_frame(MV_REFERENCE_FRAME *rf,
+                                      int8_t ref_frame_type) {
   if (ref_frame_type > ALTREF_FRAME) {
     rf[0] = ref_frame_type - ALTREF_FRAME;
     rf[1] = ALTREF_FRAME;
@@ -236,9 +237,9 @@
   }
 }
 
-static int16_t vp10_mode_context_analyzer(const int16_t *const mode_context,
-                                          const MV_REFERENCE_FRAME *const rf,
-                                          BLOCK_SIZE bsize, int block) {
+static INLINE int16_t vp10_mode_context_analyzer(
+    const int16_t *const mode_context, const MV_REFERENCE_FRAME *const rf,
+    BLOCK_SIZE bsize, int block) {
   int16_t mode_ctx = 0;
   if (block >= 0) {
     mode_ctx = mode_context[rf[0]] & 0x00ff;
diff --git a/vp10/common/reconintra.c b/vp10/common/reconintra.c
index feda3a3..a942aa0 100644
--- a/vp10/common/reconintra.c
+++ b/vp10/common/reconintra.c
@@ -225,11 +225,6 @@
 #undef intra_pred_allsizes
 }
 
-static INLINE void memset16(uint16_t *dst, int val, int n) {
-  while (n--)
-    *dst++ = val;
-}
-
 #if CONFIG_EXT_INTRA
 #define PI 3.14159265
 #define FILTER_INTRA_PREC_BITS 10
@@ -946,9 +941,9 @@
           left_col[i] = ref[i * ref_stride - 1];
       }
       if (i < (bs << need_bottom))
-        memset16(&left_col[i], left_col[i - 1], (bs << need_bottom) - i);
+        vpx_memset16(&left_col[i], left_col[i - 1], (bs << need_bottom) - i);
     } else {
-      memset16(left_col, base + 1, bs << need_bottom);
+      vpx_memset16(left_col, base + 1, bs << need_bottom);
     }
   }
 
@@ -976,9 +971,9 @@
         i += n_topright_px;
       }
       if (i < (bs << need_right))
-        memset16(&above_row[i], above_row[i - 1], (bs << need_right) - i);
+        vpx_memset16(&above_row[i], above_row[i - 1], (bs << need_right) - i);
     } else {
-      memset16(above_row, base - 1, bs << need_right);
+      vpx_memset16(above_row, base - 1, bs << need_right);
     }
   }
 
diff --git a/vp10/common/restoration.c b/vp10/common/restoration.c
index ee720c4..c73a2f9 100644
--- a/vp10/common/restoration.c
+++ b/vp10/common/restoration.c
@@ -146,7 +146,7 @@
            width * sizeof(*data));
   }
 }
-
+#if 0  // TODO(yaowu): remove when the experiment is finalized
 // Normalized non-separable filter where weights all sum to 1
 static void loop_restoration_filter_norm(uint8_t *data, int width, int height,
                                          int stride, restoration_info_n *rst,
@@ -176,6 +176,7 @@
            width * sizeof(*data));
   }
 }
+#endif
 
 #if CONFIG_VP9_HIGHBITDEPTH
 static void loop_restoration_filter_highbd(
@@ -223,6 +224,7 @@
   }
 }
 
+#if 0  // TODO(yaowu): remove when the experiment is finalized
 // Normalized non-separable filter where weights all sum to 1
 static void loop_restoration_filter_norm_highbd(
     uint8_t *data8, int width, int height,
@@ -255,6 +257,7 @@
            width * sizeof(*data));
   }
 }
+#endif
 #endif  // CONFIG_VP9_HIGHBITDEPTH
 
 void vp10_loop_restoration_rows(YV12_BUFFER_CONFIG *frame,
diff --git a/vp10/decoder/decodeframe.c b/vp10/decoder/decodeframe.c
index eb4ed59..a26f969 100644
--- a/vp10/decoder/decodeframe.c
+++ b/vp10/decoder/decodeframe.c
@@ -867,7 +867,7 @@
     }
   }
 }
-
+#if CONFIG_SUPERTX
 static void dec_build_inter_predictors_sb_sub8x8(VP10Decoder *const pbi,
                                                  MACROBLOCKD *xd,
                                                  int mi_row, int mi_col,
@@ -910,7 +910,7 @@
     }
   }
 }
-
+#endif
 static INLINE TX_SIZE dec_get_uv_tx_size(const MB_MODE_INFO *mbmi,
                                          int n4_wl, int n4_hl) {
   // get minimum log2 num4x4s dimension
@@ -2029,7 +2029,7 @@
     vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
                        "Failed to allocate bool decoder %d", 1);
 }
-
+#if CONFIG_ANS
 static void setup_token_decoder(const uint8_t *data,
                                 const uint8_t *data_end,
                                 const size_t read_size,
@@ -2050,6 +2050,7 @@
     vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
                        "Failed to allocate token decoder %d", 1);
 }
+#endif
 
 static void read_coef_probs_common(vp10_coeff_probs_model *coef_probs,
                                    vpx_reader *r) {
diff --git a/vp10/encoder/bitstream.c b/vp10/encoder/bitstream.c
index bf245e2..ed75054 100644
--- a/vp10/encoder/bitstream.c
+++ b/vp10/encoder/bitstream.c
@@ -118,16 +118,6 @@
 #endif  // CONFIG_EXT_INTRA
 }
 
-#if CONFIG_SUPERTX
-static int vp10_check_supertx(VP10_COMMON *cm, int mi_row, int mi_col,
-                              BLOCK_SIZE bsize) {
-  MODE_INFO *mi;
-  mi = cm->mi + (mi_row * cm->mi_stride + mi_col);
-  return mi[0].mbmi.tx_size == max_txsize_lookup[bsize] &&
-         mi[0].mbmi.sb_type < bsize;
-}
-#endif  // CONFIG_SUPERTX
-
 static void write_intra_mode(vpx_writer *w, PREDICTION_MODE mode,
                              const vpx_prob *probs) {
   vp10_write_token(w, vp10_intra_mode_tree, probs, &intra_mode_encodings[mode]);
diff --git a/vp10/encoder/encodeframe.c b/vp10/encoder/encodeframe.c
index 398b75e..859ec49 100644
--- a/vp10/encoder/encodeframe.c
+++ b/vp10/encoder/encodeframe.c
@@ -3755,6 +3755,7 @@
   }
 }
 
+#if !CONFIG_VAR_TX
 static void reset_skip_tx_size(VP10_COMMON *cm, TX_SIZE max_tx_size) {
   int mi_row, mi_col;
   const int mis = cm->mi_stride;
@@ -3767,6 +3768,7 @@
     }
   }
 }
+#endif
 
 static MV_REFERENCE_FRAME get_frame_type(const VP10_COMP *cpi) {
   if (frame_is_intra_only(&cpi->common))
diff --git a/vp10/encoder/firstpass.c b/vp10/encoder/firstpass.c
index 471ef12..0404e27 100644
--- a/vp10/encoder/firstpass.c
+++ b/vp10/encoder/firstpass.c
@@ -2151,6 +2151,8 @@
   return is_viable_kf;
 }
 
+#define FRAMES_TO_CHECK_DECAY 8
+
 static void find_next_key_frame(VP10_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   int i, j;
   RATE_CONTROL *const rc = &cpi->rc;
@@ -2169,7 +2171,7 @@
   double boost_score = 0.0;
   double kf_mod_err = 0.0;
   double kf_group_err = 0.0;
-  double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
+  double recent_loop_decay[FRAMES_TO_CHECK_DECAY];
 
   vp10_zero(next_frame);
 
@@ -2196,6 +2198,10 @@
 
   kf_mod_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
 
+  // Initialize the decay rates for the recent frames to check
+  for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
+    recent_loop_decay[j] = 1.0;
+
   // Find the next keyframe.
   i = 0;
   while (twopass->stats_in < twopass->stats_in_end &&
@@ -2222,9 +2228,9 @@
       // We want to know something about the recent past... rather than
       // as used elsewhere where we are concerned with decay in prediction
       // quality since the last GF or KF.
-      recent_loop_decay[i % 8] = loop_decay_rate;
+      recent_loop_decay[i % FRAMES_TO_CHECK_DECAY] = loop_decay_rate;
       decay_accumulator = 1.0;
-      for (j = 0; j < 8; ++j)
+      for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
         decay_accumulator *= recent_loop_decay[j];
 
       // Special check for transition or high motion followed by a
diff --git a/vp10/encoder/picklpf.c b/vp10/encoder/picklpf.c
index 8f28a30..85735a4 100644
--- a/vp10/encoder/picklpf.c
+++ b/vp10/encoder/picklpf.c
@@ -34,7 +34,8 @@
   }
 }
 
-
+#if !CONFIG_LOOP_RESTORATION
+#if !JOINT_FILTER_RESTORATION_SEARCH
 static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
                                 VP10_COMP *const cpi,
                                 int filt_level, int partial_frame) {
@@ -70,6 +71,8 @@
 
   return filt_err;
 }
+#endif
+#endif
 
 #if CONFIG_LOOP_RESTORATION
 #define JOINT_FILTER_RESTORATION_SEARCH
@@ -242,6 +245,8 @@
 #endif  // JOINT_FILTER_RESTORATION_SEARCH
 #endif  // CONFIG_LOOP_RESTORATION
 
+#if !CONFIG_LOOP_RESTORATION
+#if !JOINT_FILTER_RESTORATION_SEARCH
 static int search_filter_level(const YV12_BUFFER_CONFIG *sd, VP10_COMP *cpi,
                                int partial_frame) {
   const VP10_COMMON *const cm = &cpi->common;
@@ -323,6 +328,8 @@
 
   return filt_best;
 }
+#endif
+#endif
 
 void vp10_pick_filter_level(const YV12_BUFFER_CONFIG *sd, VP10_COMP *cpi,
                            LPF_PICK_METHOD method) {
diff --git a/vp10/encoder/rdopt.c b/vp10/encoder/rdopt.c
index 3a1e89f..d42a148 100644
--- a/vp10/encoder/rdopt.c
+++ b/vp10/encoder/rdopt.c
@@ -1837,7 +1837,8 @@
       p_angle = mode_to_angle_map[mbmi->mode] +
           mbmi->angle_delta[0] * ANGLE_STEP;
       for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) {
-        if (!pick_intra_filter(p_angle) && filter != INTRA_FILTER_LINEAR)
+        if ((FILTER_FAST_SEARCH || !pick_intra_filter(p_angle)) &&
+            filter != INTRA_FILTER_LINEAR)
           continue;
         mic->mbmi.intra_filter = filter;
         super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion,
@@ -1878,7 +1879,8 @@
             mbmi->angle_delta[0] * ANGLE_STEP;
         for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) {
           mic->mbmi.intra_filter = filter;
-          if (!pick_intra_filter(p_angle) && filter != INTRA_FILTER_LINEAR)
+          if ((FILTER_FAST_SEARCH || !pick_intra_filter(p_angle)) &&
+              filter != INTRA_FILTER_LINEAR)
             continue;
           super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion,
                           &s, NULL, bsize, best_rd);
@@ -1909,7 +1911,8 @@
           mbmi->angle_delta[0] * ANGLE_STEP;
       for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) {
         mic->mbmi.intra_filter = filter;
-        if (!pick_intra_filter(p_angle) && filter != INTRA_FILTER_LINEAR)
+        if ((FILTER_FAST_SEARCH || !pick_intra_filter(p_angle)) &&
+            filter != INTRA_FILTER_LINEAR)
           continue;
         super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion,
                         &s, NULL, bsize, best_rd);
@@ -1935,6 +1938,37 @@
     }
   }
 
+  if (FILTER_FAST_SEARCH && *rate_tokenonly < INT_MAX) {
+    mbmi->angle_delta[0] = best_angle_delta;
+    p_angle = mode_to_angle_map[mbmi->mode] +
+        mbmi->angle_delta[0] * ANGLE_STEP;
+    if (pick_intra_filter(p_angle)) {
+      for (filter = INTRA_FILTER_LINEAR + 1; filter < INTRA_FILTERS; ++filter) {
+        mic->mbmi.intra_filter = filter;
+        super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion,
+                        &s, NULL, bsize, best_rd);
+        if (this_rate_tokenonly == INT_MAX)
+          continue;
+
+        this_rate = this_rate_tokenonly + rate_overhead +
+            cpi->intra_filter_cost[intra_filter_ctx][filter];
+        this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
+
+        if (this_rd < best_rd) {
+          best_rd             = this_rd;
+          best_angle_delta    = mbmi->angle_delta[0];
+          best_tx_size        = mbmi->tx_size;
+          best_filter         = mbmi->intra_filter;
+          best_tx_type        = mbmi->tx_type;
+          *rate               = this_rate;
+          *rate_tokenonly     = this_rate_tokenonly;
+          *distortion         = this_distortion;
+          *skippable          = s;
+        }
+      }
+    }
+  }
+
   mbmi->tx_size = best_tx_size;
   mbmi->angle_delta[0] = best_angle_delta;
   mic->mbmi.intra_filter = best_filter;
diff --git a/vp9/common/vp9_rtcd_defs.pl b/vp9/common/vp9_rtcd_defs.pl
index d6c86fe..d6a0ce9 100644
--- a/vp9/common/vp9_rtcd_defs.pl
+++ b/vp9/common/vp9_rtcd_defs.pl
@@ -310,6 +310,15 @@
 }
 # End vp9_high encoder functions
 
+#
+# frame based scale
+#
+if (vpx_config("CONFIG_VP9_HIGHBITDEPTH") eq "yes") {
+} else {
+  add_proto qw/void vp9_scale_and_extend_frame/, "const struct yv12_buffer_config *src, struct yv12_buffer_config *dst";
+  specialize qw/vp9_scale_and_extend_frame ssse3/;
+}
+
 }
 # end encoder functions
 1;
diff --git a/vp9/decoder/vp9_detokenize.c b/vp9/decoder/vp9_detokenize.c
index dbcf2be..dcc75b9 100644
--- a/vp9/decoder/vp9_detokenize.c
+++ b/vp9/decoder/vp9_detokenize.c
@@ -23,14 +23,6 @@
 #define EOB_CONTEXT_NODE            0
 #define ZERO_CONTEXT_NODE           1
 #define ONE_CONTEXT_NODE            2
-#define LOW_VAL_CONTEXT_NODE        0
-#define TWO_CONTEXT_NODE            1
-#define THREE_CONTEXT_NODE          2
-#define HIGH_LOW_CONTEXT_NODE       3
-#define CAT_ONE_CONTEXT_NODE        4
-#define CAT_THREEFOUR_CONTEXT_NODE  5
-#define CAT_THREE_CONTEXT_NODE      6
-#define CAT_FIVE_CONTEXT_NODE       7
 
 #define INCREMENT_COUNT(token)                              \
   do {                                                      \
diff --git a/vp9/encoder/vp9_aq_360.c b/vp9/encoder/vp9_aq_360.c
index 7f93734..f8c187c 100644
--- a/vp9/encoder/vp9_aq_360.c
+++ b/vp9/encoder/vp9_aq_360.c
@@ -21,12 +21,6 @@
 #include "vp9/encoder/vp9_rd.h"
 #include "vp9/encoder/vp9_segmentation.h"
 
-#define ENERGY_MIN (-4)
-#define ENERGY_MAX (1)
-#define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN +  1)
-#define ENERGY_IN_BOUNDS(energy)\
-  assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX)
-
 static const double rate_ratio[MAX_SEGMENTS] =
   {1.0, 0.75, 0.6, 0.5, 0.4, 0.3, 0.25};
 
diff --git a/vp9/encoder/vp9_aq_complexity.c b/vp9/encoder/vp9_aq_complexity.c
index e597322..2d979ec 100644
--- a/vp9/encoder/vp9_aq_complexity.c
+++ b/vp9/encoder/vp9_aq_complexity.c
@@ -35,9 +35,6 @@
     {-3.5, -2.5, -1.5, 100.00, 100.0},
     {-3.0, -2.0, -1.0, 100.00, 100.0} };
 
-#define DEFAULT_COMPLEXITY 64
-
-
 static int get_aq_c_strength(int q_index, vpx_bit_depth_t bit_depth) {
   // Approximate base quatizer (truncated to int)
   const int base_quant = vp9_ac_quant(q_index, 0, bit_depth) / 4;
@@ -107,7 +104,6 @@
 
 #define DEFAULT_LV_THRESH 10.0
 #define MIN_DEFAULT_LV_THRESH 8.0
-#define VAR_STRENGTH_STEP 0.25
 // Select a segment for the current block.
 // The choice of segment for a block depends on the ratio of the projected
 // bits for the block vs a target average and its spatial complexity.
diff --git a/vp9/encoder/vp9_encoder.c b/vp9/encoder/vp9_encoder.c
index 1d081fd..8a46738 100644
--- a/vp9/encoder/vp9_encoder.c
+++ b/vp9/encoder/vp9_encoder.c
@@ -62,8 +62,6 @@
 #define AM_SEGMENT_ID_INACTIVE 7
 #define AM_SEGMENT_ID_ACTIVE 0
 
-#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */
-
 #define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
                                          //  for altref computation.
 #define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
@@ -1967,11 +1965,14 @@
 
   return cpi;
 }
+
+#if CONFIG_INTERNAL_STATS
 #define SNPRINT(H, T) \
   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
 
 #define SNPRINT2(H, T, V) \
   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
+#endif  // CONFIG_INTERNAL_STATS
 
 void vp9_remove_compressor(VP9_COMP *cpi) {
   VP9_COMMON *cm;
@@ -2613,10 +2614,6 @@
 #if CONFIG_VP9_HIGHBITDEPTH
 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
                                    YV12_BUFFER_CONFIG *dst, int bd) {
-#else
-static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
-                                   YV12_BUFFER_CONFIG *dst) {
-#endif  // CONFIG_VP9_HIGHBITDEPTH
   const int src_w = src->y_crop_width;
   const int src_h = src->y_crop_height;
   const int dst_w = dst->y_crop_width;
@@ -2628,19 +2625,18 @@
   const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
   int x, y, i;
 
-  for (y = 0; y < dst_h; y += 16) {
-    for (x = 0; x < dst_w; x += 16) {
-      for (i = 0; i < MAX_MB_PLANE; ++i) {
-        const int factor = (i == 0 || i == 3 ? 1 : 2);
+  for (i = 0; i < MAX_MB_PLANE; ++i) {
+    const int factor = (i == 0 || i == 3 ? 1 : 2);
+    const int src_stride = src_strides[i];
+    const int dst_stride = dst_strides[i];
+    for (y = 0; y < dst_h; y += 16) {
+      const int y_q4 = y * (16 / factor) * src_h / dst_h;
+      for (x = 0; x < dst_w; x += 16) {
         const int x_q4 = x * (16 / factor) * src_w / dst_w;
-        const int y_q4 = y * (16 / factor) * src_h / dst_h;
-        const int src_stride = src_strides[i];
-        const int dst_stride = dst_strides[i];
         const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
-                                     src_stride + (x / factor) * src_w / dst_w;
+                                   src_stride + (x / factor) * src_w / dst_w;
         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
 
-#if CONFIG_VP9_HIGHBITDEPTH
         if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
           vpx_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
                                kernel[x_q4 & 0xf], 16 * src_w / dst_w,
@@ -2652,18 +2648,49 @@
                         kernel[y_q4 & 0xf], 16 * src_h / dst_h,
                         16 / factor, 16 / factor);
         }
-#else
-        vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
-                      kernel[x_q4 & 0xf], 16 * src_w / dst_w,
-                      kernel[y_q4 & 0xf], 16 * src_h / dst_h,
-                      16 / factor, 16 / factor);
-#endif  // CONFIG_VP9_HIGHBITDEPTH
       }
     }
   }
 
   vpx_extend_frame_borders(dst);
 }
+#else
+void vp9_scale_and_extend_frame_c(const YV12_BUFFER_CONFIG *src,
+                                  YV12_BUFFER_CONFIG *dst) {
+  const int src_w = src->y_crop_width;
+  const int src_h = src->y_crop_height;
+  const int dst_w = dst->y_crop_width;
+  const int dst_h = dst->y_crop_height;
+  const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
+  const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
+  uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
+  const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
+  const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
+  int x, y, i;
+
+  for (i = 0; i < MAX_MB_PLANE; ++i) {
+    const int factor = (i == 0 || i == 3 ? 1 : 2);
+    const int src_stride = src_strides[i];
+    const int dst_stride = dst_strides[i];
+    for (y = 0; y < dst_h; y += 16) {
+      const int y_q4 = y * (16 / factor) * src_h / dst_h;
+      for (x = 0; x < dst_w; x += 16) {
+        const int x_q4 = x * (16 / factor) * src_w / dst_w;
+        const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
+                                   src_stride + (x / factor) * src_w / dst_w;
+        uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
+
+        vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
+                      kernel[x_q4 & 0xf], 16 * src_w / dst_w,
+                      kernel[y_q4 & 0xf], 16 * src_h / dst_h,
+                      16 / factor, 16 / factor);
+      }
+    }
+  }
+
+  vpx_extend_frame_borders(dst);
+}
+#endif  // CONFIG_VP9_HIGHBITDEPTH
 
 static int scale_down(VP9_COMP *cpi, int q) {
   RATE_CONTROL *const rc = &cpi->rc;
@@ -2938,7 +2965,7 @@
                                    cm->subsampling_x, cm->subsampling_y,
                                    VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
                                    NULL, NULL, NULL);
-          scale_and_extend_frame(ref, &new_fb_ptr->buf);
+          vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf);
           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
           alloc_frame_mvs(cm, new_fb);
         }
@@ -3302,7 +3329,6 @@
   vpx_clear_system_state();
 
   set_frame_size(cpi);
-
   cpi->Source = vp9_scale_if_required(cm,
                                       cpi->un_scaled_source,
                                       &cpi->scaled_source,
@@ -3320,7 +3346,6 @@
                                              cpi->unscaled_last_source,
                                              &cpi->scaled_last_source,
                                              (cpi->oxcf.pass == 0));
-
   vp9_update_noise_estimate(cpi);
 
   if (cpi->oxcf.pass == 0 &&
@@ -3742,7 +3767,7 @@
     if (use_normative_scaler &&
         unscaled->y_width <= (scaled->y_width << 1) &&
         unscaled->y_height <= (scaled->y_height << 1))
-      scale_and_extend_frame(unscaled, scaled);
+      vp9_scale_and_extend_frame(unscaled, scaled);
     else
       scale_and_extend_frame_nonnormative(unscaled, scaled);
 #endif  // CONFIG_VP9_HIGHBITDEPTH
diff --git a/vp9/encoder/vp9_firstpass.c b/vp9/encoder/vp9_firstpass.c
index 71843ee..9d3b154 100644
--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -2260,6 +2260,8 @@
   return is_viable_kf;
 }
 
+#define FRAMES_TO_CHECK_DECAY 8
+
 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   int i, j;
   RATE_CONTROL *const rc = &cpi->rc;
@@ -2278,7 +2280,7 @@
   double boost_score = 0.0;
   double kf_mod_err = 0.0;
   double kf_group_err = 0.0;
-  double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
+  double recent_loop_decay[FRAMES_TO_CHECK_DECAY];
 
   vp9_zero(next_frame);
 
@@ -2305,6 +2307,10 @@
 
   kf_mod_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
 
+  // Initialize the decay rates for the recent frames to check
+  for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
+    recent_loop_decay[j] = 1.0;
+
   // Find the next keyframe.
   i = 0;
   while (twopass->stats_in < twopass->stats_in_end &&
@@ -2331,9 +2337,9 @@
       // We want to know something about the recent past... rather than
       // as used elsewhere where we are concerned with decay in prediction
       // quality since the last GF or KF.
-      recent_loop_decay[i % 8] = loop_decay_rate;
+      recent_loop_decay[i % FRAMES_TO_CHECK_DECAY] = loop_decay_rate;
       decay_accumulator = 1.0;
-      for (j = 0; j < 8; ++j)
+      for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
         decay_accumulator *= recent_loop_decay[j];
 
       // Special check for transition or high motion followed by a
diff --git a/vp9/encoder/vp9_mcomp.c b/vp9/encoder/vp9_mcomp.c
index 4004dd3..607941c 100644
--- a/vp9/encoder/vp9_mcomp.c
+++ b/vp9/encoder/vp9_mcomp.c
@@ -153,10 +153,10 @@
  */
 
 /* estimated cost of a motion vector (r,c) */
-#define MVC(r, c)                                       \
-    (mvcost ?                                           \
-     ((mvjcost[((r) != rr) * 2 + ((c) != rc)] +         \
-       mvcost[0][((r) - rr)] + mvcost[1][((c) - rc)]) * \
+#define MVC(r, c)                                              \
+    (mvcost ?                                                  \
+     ((unsigned)(mvjcost[((r) != rr) * 2 + ((c) != rc)] +      \
+       mvcost[0][((r) - rr)] + mvcost[1][((c) - rc)]) *        \
       error_per_bit + 4096) >> 13 : 0)
 
 
@@ -849,9 +849,9 @@
       cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
                                     get_buf_from_mv(in_what, &this_mv),
                                     in_what->stride, &sse) +
-          // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
-          mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
-                      x->errorperbit);
+                                    mv_err_cost(&this_mv, &fcenter_mv,
+                                                x->nmvjointcost, x->mvcost,
+                                                x->errorperbit);
     }
   } else {
     for (i = 0; i < 4; i++) {
@@ -863,9 +863,9 @@
         cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
                                       get_buf_from_mv(in_what, &this_mv),
                                       in_what->stride, &sse) +
-            // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
-            mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
-                        x->errorperbit);
+                                      mv_err_cost(&this_mv, &fcenter_mv,
+                                                  x->nmvjointcost, x->mvcost,
+                                                  x->errorperbit);
     }
   }
 }
diff --git a/vp9/encoder/vp9_rdopt.c b/vp9/encoder/vp9_rdopt.c
index 3166f27..f00a58c 100644
--- a/vp9/encoder/vp9_rdopt.c
+++ b/vp9/encoder/vp9_rdopt.c
@@ -3355,15 +3355,19 @@
     }
 
     if (!disable_skip) {
+      vpx_prob skip_prob = vp9_get_skip_prob(cm, xd);
       if (skippable) {
         // Back out the coefficient coding costs
         rate2 -= (rate_y + rate_uv);
 
         // Cost the skip mb case
-        rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
+        rate2 += vp9_cost_bit(skip_prob, 1);
       } else if (ref_frame != INTRA_FRAME && !xd->lossless) {
-        if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) <
-            RDCOST(x->rdmult, x->rddiv, 0, total_sse)) {
+        if (RDCOST(x->rdmult, x->rddiv,
+                   rate_y + rate_uv + vp9_cost_bit(skip_prob, 0),
+                   distortion2) <
+            RDCOST(x->rdmult, x->rddiv,
+                   vp9_cost_bit(skip_prob, 1), total_sse)) {
           // Add in the cost of the no skip flag.
           rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
         } else {
diff --git a/vp9/encoder/vp9_resize.c b/vp9/encoder/vp9_resize.c
index 59c7478..f4d0db4d 100644
--- a/vp9/encoder/vp9_resize.c
+++ b/vp9/encoder/vp9_resize.c
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "./vpx_config.h"
 #if CONFIG_VP9_HIGHBITDEPTH
 #include "vpx_dsp/vpx_dsp_common.h"
 #endif  // CONFIG_VP9_HIGHBITDEPTH
diff --git a/vp9/encoder/vp9_skin_detection.c b/vp9/encoder/vp9_skin_detection.c
index 54cc082..8e117eb 100644
--- a/vp9/encoder/vp9_skin_detection.c
+++ b/vp9/encoder/vp9_skin_detection.c
@@ -21,10 +21,11 @@
 static const int skin_mean[5][2] = {
     {7463, 9614}, {6400, 10240}, {7040, 10240}, {8320, 9280}, {6800, 9614}};
 static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157};  // q16
-static const int skin_threshold[2] = {1570636, 800000};       // q18
+static const int skin_threshold[6] = {1570636, 1400000, 800000, 800000, 800000,
+    800000};  // q18
 
 // Thresholds on luminance.
-static const int y_low = 20;
+static const int y_low = 40;
 static const int y_high = 220;
 
 // Evaluates the Mahalanobis distance measure for the input CbCr values.
@@ -55,10 +56,24 @@
       return (evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
     } else {
       int i = 0;
+      // Exit on grey.
+      if (cb == 128 && cr == 128)
+        return 0;
+      // Exit on very strong cb.
+      if (cb > 150 && cr < 110)
+        return 0;
+      // Exit on (another) low luminance threshold if either color is high.
+      if (y < 50 && (cb > 140 || cr > 140))
+        return 0;
       for (; i < 5; i++) {
-        if (evaluate_skin_color_difference(cb, cr, i) < skin_threshold[1]) {
+        if (evaluate_skin_color_difference(cb, cr, i) < skin_threshold[i + 1]) {
           return 1;
         }
+        // Exit if difference is much large than the threshold.
+        if (evaluate_skin_color_difference(cb, cr, i) >
+            (skin_threshold[i + 1] << 3)) {
+          return 0;
+        }
       }
       return 0;
     }
diff --git a/vp9/encoder/vp9_svc_layercontext.c b/vp9/encoder/vp9_svc_layercontext.c
index e0236aa..9724df4 100644
--- a/vp9/encoder/vp9_svc_layercontext.c
+++ b/vp9/encoder/vp9_svc_layercontext.c
@@ -16,7 +16,6 @@
 #include "vp9/encoder/vp9_extend.h"
 #include "vpx_dsp/vpx_dsp_common.h"
 
-#define SMALL_FRAME_FB_IDX 7
 #define SMALL_FRAME_WIDTH  32
 #define SMALL_FRAME_HEIGHT 16
 
@@ -644,6 +643,8 @@
 }
 
 #if CONFIG_SPATIAL_SVC
+#define SMALL_FRAME_FB_IDX 7
+
 int vp9_svc_start_frame(VP9_COMP *const cpi) {
   int width = 0, height = 0;
   LAYER_CONTEXT *lc;
@@ -754,7 +755,8 @@
   return 0;
 }
 
-#endif
+#undef SMALL_FRAME_FB_IDX
+#endif  // CONFIG_SPATIAL_SVC
 
 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
                                               struct lookahead_ctx *ctx,
diff --git a/vp9/encoder/x86/vp9_diamond_search_sad_avx.c b/vp9/encoder/x86/vp9_diamond_search_sad_avx.c
index 2ed3f1a..b475f8d 100644
--- a/vp9/encoder/x86/vp9_diamond_search_sad_avx.c
+++ b/vp9/encoder/x86/vp9_diamond_search_sad_avx.c
@@ -19,11 +19,11 @@
 #include "vpx_ports/mem.h"
 
 #ifdef __GNUC__
-# define __likely__(v)    __builtin_expect(v, 1)
-# define __unlikely__(v)  __builtin_expect(v, 0)
+# define LIKELY(v)    __builtin_expect(v, 1)
+# define UNLIKELY(v)  __builtin_expect(v, 0)
 #else
-# define __likely__(v)    (v)
-# define __unlikely__(v)  (v)
+# define LIKELY(v)    (v)
+# define UNLIKELY(v)  (v)
 #endif
 
 static INLINE int_mv pack_int_mv(int16_t row, int16_t col) {
@@ -50,8 +50,9 @@
                           int error_per_bit) {
   const int_mv diff = pack_int_mv(mv.as_mv.row - ref->row,
                                   mv.as_mv.col - ref->col);
-  return ROUND_POWER_OF_TWO(mv_cost(diff, x->nmvjointsadcost,
-                                    x->nmvsadcost) * error_per_bit, 8);
+  return ROUND_POWER_OF_TWO((unsigned)mv_cost(diff, x->nmvjointsadcost,
+                                              x->nmvsadcost) *
+                                              error_per_bit, 8);
 }
 
 /*****************************************************************************
@@ -162,7 +163,7 @@
       v_inside_d = _mm_cmpeq_epi32(v_these_mv_clamp_w, v_these_mv_w);
 
       // If none of them are inside, then move on
-      if (__likely__(_mm_test_all_zeros(v_inside_d, v_inside_d))) {
+      if (LIKELY(_mm_test_all_zeros(v_inside_d, v_inside_d))) {
         continue;
       }
 
@@ -268,7 +269,7 @@
         // If the local best value is not saturated, just use it, otherwise
         // find the horizontal minimum again the hard way on 32 bits.
         // This is executed rarely.
-        if (__unlikely__(local_best_sad == 0xffff)) {
+        if (UNLIKELY(local_best_sad == 0xffff)) {
           __m128i v_loval_d, v_hival_d, v_loidx_d, v_hiidx_d, v_sel_d;
 
           v_loval_d = v_sad_d;
@@ -293,7 +294,7 @@
         }
 
         // Update the global minimum if the local minimum is smaller
-        if (__likely__(local_best_sad < best_sad)) {
+        if (LIKELY(local_best_sad < best_sad)) {
           new_bmv = ((const int_mv *)&v_these_mv_w)[local_best_idx];
           new_best_address = ((const uint8_t **)v_blocka)[local_best_idx];
 
@@ -312,7 +313,7 @@
     v_ba_d = _mm_set1_epi32((intptr_t)best_address);
 #endif
 
-    if (__unlikely__(best_address == in_what)) {
+    if (UNLIKELY(best_address == in_what)) {
       (*num00)++;
     }
   }
diff --git a/vp9/encoder/x86/vp9_frame_scale_ssse3.c b/vp9/encoder/x86/vp9_frame_scale_ssse3.c
new file mode 100644
index 0000000..de903fa
--- /dev/null
+++ b/vp9/encoder/x86/vp9_frame_scale_ssse3.c
@@ -0,0 +1,211 @@
+/*
+ *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#if defined(_MSC_VER) && _MSC_VER <= 1500
+// Need to include math.h before calling tmmintrin.h/intrin.h
+// in certain versions of MSVS.
+#include <math.h>
+#endif
+#include <tmmintrin.h>  // SSSE3
+
+#include "./vp9_rtcd.h"
+#include "./vpx_dsp_rtcd.h"
+#include "./vpx_scale_rtcd.h"
+#include "vpx_scale/yv12config.h"
+
+extern void vp9_scale_and_extend_frame_c(const YV12_BUFFER_CONFIG *src,
+                                         YV12_BUFFER_CONFIG *dst);
+
+void downsample_2_to_1_ssse3(const uint8_t *src, ptrdiff_t src_stride,
+                             uint8_t *dst, ptrdiff_t dst_stride,
+                             int w, int h) {
+  const __m128i mask = _mm_set1_epi16(0x00FF);
+  const int max_width = w & ~15;
+  int y;
+  for (y = 0; y < h; ++y) {
+    int x;
+    for (x = 0; x < max_width; x += 16) {
+      const __m128i a = _mm_loadu_si128((const __m128i *)(src + x * 2 +  0));
+      const __m128i b = _mm_loadu_si128((const __m128i *)(src + x * 2 + 16));
+      const __m128i a_and = _mm_and_si128(a, mask);
+      const __m128i b_and = _mm_and_si128(b, mask);
+      const __m128i c = _mm_packus_epi16(a_and, b_and);
+      _mm_storeu_si128((__m128i *)(dst + x), c);
+    }
+    for (; x < w; ++x)
+      dst[x] = src[x * 2];
+    src += src_stride * 2;
+    dst += dst_stride;
+  }
+}
+
+static INLINE __m128i filter(const __m128i *const a, const __m128i *const b,
+                             const __m128i *const c, const __m128i *const d,
+                             const __m128i *const e, const __m128i *const f,
+                             const __m128i *const g, const __m128i *const h) {
+  const __m128i coeffs_ab =
+      _mm_set_epi8(6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1);
+  const __m128i coeffs_cd =
+      _mm_set_epi8(78, -19, 78, -19, 78, -19, 78, -19, 78, -19, 78, -19,
+                   78, -19, 78, -19);
+  const __m128i const64_x16 = _mm_set1_epi16(64);
+  const __m128i ab = _mm_unpacklo_epi8(*a, *b);
+  const __m128i cd = _mm_unpacklo_epi8(*c, *d);
+  const __m128i fe = _mm_unpacklo_epi8(*f, *e);
+  const __m128i hg = _mm_unpacklo_epi8(*h, *g);
+  const __m128i ab_terms = _mm_maddubs_epi16(ab, coeffs_ab);
+  const __m128i cd_terms = _mm_maddubs_epi16(cd, coeffs_cd);
+  const __m128i fe_terms = _mm_maddubs_epi16(fe, coeffs_cd);
+  const __m128i hg_terms = _mm_maddubs_epi16(hg, coeffs_ab);
+  // can not overflow
+  const __m128i abcd_terms = _mm_add_epi16(ab_terms, cd_terms);
+  // can not overflow
+  const __m128i fehg_terms = _mm_add_epi16(fe_terms, hg_terms);
+  // can overflow, use saturating add
+  const __m128i terms = _mm_adds_epi16(abcd_terms, fehg_terms);
+  const __m128i round = _mm_adds_epi16(terms, const64_x16);
+  const __m128i shift = _mm_srai_epi16(round, 7);
+  return _mm_packus_epi16(shift, shift);
+}
+
+static void eight_tap_row_ssse3(const uint8_t *src, uint8_t *dst, int w) {
+  const int max_width = w & ~7;
+  int x = 0;
+  for (; x < max_width; x += 8) {
+    const __m128i a = _mm_loadl_epi64((const __m128i *)(src + x + 0));
+    const __m128i b = _mm_loadl_epi64((const __m128i *)(src + x + 1));
+    const __m128i c = _mm_loadl_epi64((const __m128i *)(src + x + 2));
+    const __m128i d = _mm_loadl_epi64((const __m128i *)(src + x + 3));
+    const __m128i e = _mm_loadl_epi64((const __m128i *)(src + x + 4));
+    const __m128i f = _mm_loadl_epi64((const __m128i *)(src + x + 5));
+    const __m128i g = _mm_loadl_epi64((const __m128i *)(src + x + 6));
+    const __m128i h = _mm_loadl_epi64((const __m128i *)(src + x + 7));
+    const __m128i pack = filter(&a, &b, &c, &d, &e, &f, &g, &h);
+    _mm_storel_epi64((__m128i *)(dst + x), pack);
+  }
+}
+
+void upsample_1_to_2_ssse3(const uint8_t *src, ptrdiff_t src_stride,
+                           uint8_t *dst, ptrdiff_t dst_stride,
+                           int dst_w, int dst_h) {
+  dst_w /= 2;
+  dst_h /= 2;
+  {
+    DECLARE_ALIGNED(16, uint8_t, tmp[1920 * 8]);
+    uint8_t *tmp0 = tmp + dst_w * 0;
+    uint8_t *tmp1 = tmp + dst_w * 1;
+    uint8_t *tmp2 = tmp + dst_w * 2;
+    uint8_t *tmp3 = tmp + dst_w * 3;
+    uint8_t *tmp4 = tmp + dst_w * 4;
+    uint8_t *tmp5 = tmp + dst_w * 5;
+    uint8_t *tmp6 = tmp + dst_w * 6;
+    uint8_t *tmp7 = tmp + dst_w * 7;
+    uint8_t *tmp8 = NULL;
+    const int max_width = dst_w & ~7;
+    int y;
+    eight_tap_row_ssse3(src - src_stride * 3 - 3, tmp0, dst_w);
+    eight_tap_row_ssse3(src - src_stride * 2 - 3, tmp1, dst_w);
+    eight_tap_row_ssse3(src - src_stride * 1 - 3, tmp2, dst_w);
+    eight_tap_row_ssse3(src + src_stride * 0 - 3, tmp3, dst_w);
+    eight_tap_row_ssse3(src + src_stride * 1 - 3, tmp4, dst_w);
+    eight_tap_row_ssse3(src + src_stride * 2 - 3, tmp5, dst_w);
+    eight_tap_row_ssse3(src + src_stride * 3 - 3, tmp6, dst_w);
+    for (y = 0; y < dst_h; y++) {
+      int x;
+      eight_tap_row_ssse3(src + src_stride * 4 - 3, tmp7, dst_w);
+      for (x = 0; x < max_width; x += 8) {
+        const __m128i A = _mm_loadl_epi64((const __m128i *)(src  + x));
+        const __m128i B = _mm_loadl_epi64((const __m128i *)(tmp3 + x));
+        const __m128i AB = _mm_unpacklo_epi8(A, B);
+        __m128i C, D, CD;
+        _mm_storeu_si128((__m128i *)(dst + x * 2), AB);
+        {
+          const __m128i a =
+              _mm_loadl_epi64((const __m128i *)(src + x - src_stride * 3));
+          const __m128i b =
+              _mm_loadl_epi64((const __m128i *)(src + x - src_stride * 2));
+          const __m128i c =
+              _mm_loadl_epi64((const __m128i *)(src + x - src_stride * 1));
+          const __m128i d =
+              _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 0));
+          const __m128i e =
+              _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 1));
+          const __m128i f =
+              _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 2));
+          const __m128i g =
+              _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 3));
+          const __m128i h =
+              _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 4));
+          C = filter(&a, &b, &c, &d, &e, &f, &g, &h);
+        }
+        {
+          const __m128i a = _mm_loadl_epi64((const __m128i *)(tmp0 + x));
+          const __m128i b = _mm_loadl_epi64((const __m128i *)(tmp1 + x));
+          const __m128i c = _mm_loadl_epi64((const __m128i *)(tmp2 + x));
+          const __m128i d = _mm_loadl_epi64((const __m128i *)(tmp3 + x));
+          const __m128i e = _mm_loadl_epi64((const __m128i *)(tmp4 + x));
+          const __m128i f = _mm_loadl_epi64((const __m128i *)(tmp5 + x));
+          const __m128i g = _mm_loadl_epi64((const __m128i *)(tmp6 + x));
+          const __m128i h = _mm_loadl_epi64((const __m128i *)(tmp7 + x));
+          D = filter(&a, &b, &c, &d, &e, &f, &g, &h);
+        }
+        CD = _mm_unpacklo_epi8(C, D);
+        _mm_storeu_si128((__m128i *)(dst + x * 2 + dst_stride), CD);
+      }
+      src += src_stride;
+      dst += dst_stride * 2;
+      tmp8 = tmp0;
+      tmp0 = tmp1;
+      tmp1 = tmp2;
+      tmp2 = tmp3;
+      tmp3 = tmp4;
+      tmp4 = tmp5;
+      tmp5 = tmp6;
+      tmp6 = tmp7;
+      tmp7 = tmp8;
+    }
+  }
+}
+
+void vp9_scale_and_extend_frame_ssse3(const YV12_BUFFER_CONFIG *src,
+                                      YV12_BUFFER_CONFIG *dst) {
+  const int src_w = src->y_crop_width;
+  const int src_h = src->y_crop_height;
+  const int dst_w = dst->y_crop_width;
+  const int dst_h = dst->y_crop_height;
+  const int dst_uv_w = dst_w / 2;
+  const int dst_uv_h = dst_h / 2;
+
+  if (dst_w * 2 == src_w && dst_h * 2 == src_h) {
+    downsample_2_to_1_ssse3(src->y_buffer, src->y_stride,
+                            dst->y_buffer, dst->y_stride, dst_w, dst_h);
+    downsample_2_to_1_ssse3(src->u_buffer, src->uv_stride,
+                            dst->u_buffer, dst->uv_stride, dst_uv_w, dst_uv_h);
+    downsample_2_to_1_ssse3(src->v_buffer, src->uv_stride,
+                            dst->v_buffer, dst->uv_stride, dst_uv_w, dst_uv_h);
+    vpx_extend_frame_borders(dst);
+  } else if (dst_w == src_w * 2 && dst_h == src_h * 2) {
+    // The upsample() supports widths up to 1920 * 2.  If greater, fall back
+    // to vp9_scale_and_extend_frame_c().
+    if (dst_w/2 <= 1920) {
+      upsample_1_to_2_ssse3(src->y_buffer, src->y_stride,
+                            dst->y_buffer, dst->y_stride, dst_w, dst_h);
+      upsample_1_to_2_ssse3(src->u_buffer, src->uv_stride,
+                            dst->u_buffer, dst->uv_stride, dst_uv_w, dst_uv_h);
+      upsample_1_to_2_ssse3(src->v_buffer, src->uv_stride,
+                            dst->v_buffer, dst->uv_stride, dst_uv_w, dst_uv_h);
+      vpx_extend_frame_borders(dst);
+    } else {
+      vp9_scale_and_extend_frame_c(src, dst);
+    }
+  } else {
+    vp9_scale_and_extend_frame_c(src, dst);
+  }
+}
diff --git a/vp9/vp9cx.mk b/vp9/vp9cx.mk
index c7a334d..83a91e8 100644
--- a/vp9/vp9cx.mk
+++ b/vp9/vp9cx.mk
@@ -119,6 +119,7 @@
 
 VP9_CX_SRCS-$(HAVE_SSE2) += encoder/x86/vp9_dct_sse2.c
 VP9_CX_SRCS-$(HAVE_SSSE3) += encoder/x86/vp9_dct_ssse3.c
+VP9_CX_SRCS-$(HAVE_SSSE3) += encoder/x86/vp9_frame_scale_ssse3.c
 
 ifeq ($(CONFIG_VP9_TEMPORAL_DENOISING),yes)
 VP9_CX_SRCS-$(HAVE_SSE2) += encoder/x86/vp9_denoiser_sse2.c
diff --git a/vpx_mem/vpx_mem.c b/vpx_mem/vpx_mem.c
index b98fe83..b261fc0 100644
--- a/vpx_mem/vpx_mem.c
+++ b/vpx_mem/vpx_mem.c
@@ -9,8 +9,6 @@
  */
 
 
-#define __VPX_MEM_C__
-
 #include "vpx_mem.h"
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/vpx_scale/generic/yv12config.c b/vpx_scale/generic/yv12config.c
index e1ee46e..e8fee52 100644
--- a/vpx_scale/generic/yv12config.c
+++ b/vpx_scale/generic/yv12config.c
@@ -160,29 +160,12 @@
     const uint64_t uvplane_size = (uv_height + 2 * uv_border_h) *
                                   (uint64_t)uv_stride + byte_alignment;
 
-#if CONFIG_ALPHA
-    const int alpha_width = aligned_width;
-    const int alpha_height = aligned_height;
-    const int alpha_stride = y_stride;
-    const int alpha_border_w = border;
-    const int alpha_border_h = border;
-    const uint64_t alpha_plane_size = (alpha_height + 2 * alpha_border_h) *
-                                      (uint64_t)alpha_stride + byte_alignment;
-#if CONFIG_VP9_HIGHBITDEPTH
-    const uint64_t frame_size = (1 + use_highbitdepth) *
-        (yplane_size + 2 * uvplane_size + alpha_plane_size);
-#else
-    const uint64_t frame_size = yplane_size + 2 * uvplane_size +
-                                alpha_plane_size;
-#endif  // CONFIG_VP9_HIGHBITDEPTH
-#else
 #if CONFIG_VP9_HIGHBITDEPTH
     const uint64_t frame_size =
         (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
 #else
     const uint64_t frame_size = yplane_size + 2 * uvplane_size;
 #endif  // CONFIG_VP9_HIGHBITDEPTH
-#endif  // CONFIG_ALPHA
 
     uint8_t *buf = NULL;
 
@@ -277,14 +260,6 @@
         buf + yplane_size + uvplane_size + (uv_border_h * uv_stride) +
         uv_border_w, vp9_byte_align);
 
-#if CONFIG_ALPHA
-    ybf->alpha_width = alpha_width;
-    ybf->alpha_height = alpha_height;
-    ybf->alpha_stride = alpha_stride;
-    ybf->alpha_buffer = (uint8_t *)yv12_align_addr(
-        buf + yplane_size + 2 * uvplane_size +
-        (alpha_border_h * alpha_stride) + alpha_border_w, vp9_byte_align);
-#endif
     ybf->corrupted = 0; /* assume not corrupted by errors */
     return 0;
   }