Merge "Linearize extrabits writing." into nextgenv2
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index 7179e3d..d2de412 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -12,6 +12,7 @@
 #include "av1/common/entropy.h"
 #include "av1/common/blockd.h"
 #include "av1/common/onyxc_int.h"
+#include "av1/common/scan.h"
 #include "av1/common/entropymode.h"
 #include "aom_mem/aom_mem.h"
 #include "aom/aom_integer.h"
@@ -2842,6 +2843,10 @@
 #endif  // CONFIG_RANS
 }
 
+#if CONFIG_ADAPT_SCAN
+#define ADAPT_SCAN_UPDATE_RATE_16 (1 << 13)
+#endif
+
 static void adapt_coef_probs(AV1_COMMON *cm, TX_SIZE tx_size,
                              unsigned int count_sat,
                              unsigned int update_factor) {
@@ -2881,11 +2886,15 @@
 }
 
 void av1_adapt_coef_probs(AV1_COMMON *cm) {
-  TX_SIZE t;
+  TX_SIZE tx_size;
   unsigned int count_sat, update_factor;
 
+#if CONFIG_ADAPT_SCAN
+  TX_TYPE tx_type;
+#endif
+
 #if CONFIG_ENTROPY
-  if (cm->last_frame_type == KEY_FRAME) {
+  if (!frame_is_intra_only(cm) && cm->last_frame_type == KEY_FRAME) {
     update_factor = COEF_MAX_UPDATE_FACTOR_AFTER_KEY_BITS; /* adapt quickly */
     count_sat = COEF_COUNT_SAT_AFTER_KEY_BITS;
   } else {
@@ -2896,7 +2905,7 @@
     update_factor = COEF_MAX_UPDATE_FACTOR_BITS;
   }
 #else
-  if (cm->last_frame_type == KEY_FRAME) {
+  if (!frame_is_intra_only(cm) && cm->last_frame_type == KEY_FRAME) {
     update_factor = COEF_MAX_UPDATE_FACTOR_AFTER_KEY; /* adapt quickly */
     count_sat = COEF_COUNT_SAT_AFTER_KEY;
   } else {
@@ -2904,11 +2913,19 @@
     count_sat = COEF_COUNT_SAT;
   }
 #endif  // CONFIG_ENTROPY
-  for (t = TX_4X4; t <= TX_32X32; t++)
-    adapt_coef_probs(cm, t, count_sat, update_factor);
+  for (tx_size = TX_4X4; tx_size <= TX_32X32; tx_size++)
+    adapt_coef_probs(cm, tx_size, count_sat, update_factor);
 #if CONFIG_RANS
   av1_coef_pareto_cdfs(cm->fc);
 #endif  // CONFIG_RANS
+
+#if CONFIG_ADAPT_SCAN
+  for (tx_size = TX_4X4; tx_size < TX_SIZES; ++tx_size)
+    for (tx_type = TX_4X4; tx_type < TX_TYPES; ++tx_type) {
+      av1_update_scan_prob(cm, tx_size, tx_type, ADAPT_SCAN_UPDATE_RATE_16);
+      av1_update_scan_order_facade(cm, tx_size, tx_type);
+    }
+#endif
 }
 
 #if CONFIG_ENTROPY
diff --git a/av1/common/entropy.h b/av1/common/entropy.h
index 55993c1..02e7c93 100644
--- a/av1/common/entropy.h
+++ b/av1/common/entropy.h
@@ -287,6 +287,10 @@
 
 #endif  // CONFIG_ENTROPY
 
+#if CONFIG_ADAPT_SCAN
+#define ADAPT_SCAN_UPDATE_RATE_16 (1 << 13)
+#endif
+
 static INLINE aom_prob av1_merge_probs(aom_prob pre_prob,
                                        const unsigned int ct[2],
                                        unsigned int count_sat,
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index e812f15..e25dcf8 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -12,6 +12,7 @@
 #include "aom_mem/aom_mem.h"
 
 #include "av1/common/reconinter.h"
+#include "av1/common/scan.h"
 #include "av1/common/onyxc_int.h"
 #include "av1/common/seg_common.h"
 
@@ -1755,6 +1756,9 @@
   av1_default_coef_probs(cm);
   init_mode_probs(cm->fc);
   av1_init_mv_probs(cm);
+#if CONFIG_ADAPT_SCAN
+  av1_init_scan_order(cm);
+#endif
   cm->fc->initialized = 1;
 
   if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
diff --git a/av1/common/filter.h b/av1/common/filter.h
index 0ac52a9..15fc806 100644
--- a/av1/common/filter.h
+++ b/av1/common/filter.h
@@ -33,8 +33,12 @@
 
 #define SUPPORT_NONINTERPOLATING_FILTERS 0 /* turn on for experimentation */
 #define SWITCHABLE_FILTERS 5               /* Number of switchable filters */
+#define LOG_SWITCHABLE_FILTERS \
+  3 /* (1 << LOG_SWITCHABLE_FILTERS) > SWITCHABLE_FILTERS */
 #else
 #define SWITCHABLE_FILTERS 3 /* Number of switchable filters */
+#define LOG_SWITCHABLE_FILTERS \
+  2 /* (1 << LOG_SWITCHABLE_FILTERS) > SWITCHABLE_FILTERS */
 #endif                       // CONFIG_EXT_INTERP
 
 #define USE_TEMPORALFILTER_12TAP 1
diff --git a/av1/common/idct.c b/av1/common/idct.c
index eedbc79..4f33f9b 100644
--- a/av1/common/idct.c
+++ b/av1/common/idct.c
@@ -837,8 +837,10 @@
   if (eob == 1)
     // DC only DCT coefficient
     aom_idct8x8_1_add(input, dest, stride);
+#if !CONFIG_ADAPT_SCAN
   else if (eob <= 12)
     aom_idct8x8_12_add(input, dest, stride);
+#endif
   else
     aom_idct8x8_64_add(input, dest, stride);
 }
@@ -849,19 +851,22 @@
    * coefficients. Use eobs to separate different cases. */
   if (eob == 1) /* DC only DCT coefficient. */
     aom_idct16x16_1_add(input, dest, stride);
+#if !CONFIG_ADAPT_SCAN
   else if (eob <= 10)
     aom_idct16x16_10_add(input, dest, stride);
+#endif
   else
     aom_idct16x16_256_add(input, dest, stride);
 }
 
 void av1_idct32x32_add(const tran_low_t *input, uint8_t *dest, int stride,
                        int eob) {
-  if (eob == 1)
-    aom_idct32x32_1_add(input, dest, stride);
+  if (eob == 1) aom_idct32x32_1_add(input, dest, stride);
+#if !CONFIG_ADAPT_SCAN
   else if (eob <= 34)
     // non-zero coeff only in upper-left 8x8
     aom_idct32x32_34_add(input, dest, stride);
+#endif
   else
     aom_idct32x32_1024_add(input, dest, stride);
 }
@@ -1659,13 +1664,13 @@
   // TODO(yunqingwang): "eobs = 1" case is also handled in av1_short_idct8x8_c.
   // Combine that with code here.
   // DC only DCT coefficient
-  if (eob == 1) {
-    aom_highbd_idct8x8_1_add(input, dest, stride, bd);
-  } else if (eob <= 10) {
+  if (eob == 1) aom_highbd_idct8x8_1_add(input, dest, stride, bd);
+#if !CONFIG_ADAPT_SCAN
+  else if (eob <= 10)
     aom_highbd_idct8x8_10_add(input, dest, stride, bd);
-  } else {
+#endif
+  else
     aom_highbd_idct8x8_64_add(input, dest, stride, bd);
-  }
 }
 
 void av1_highbd_idct16x16_add(const tran_low_t *input, uint8_t *dest,
@@ -1673,25 +1678,25 @@
   // The calculation can be simplified if there are not many non-zero dct
   // coefficients. Use eobs to separate different cases.
   // DC only DCT coefficient.
-  if (eob == 1) {
-    aom_highbd_idct16x16_1_add(input, dest, stride, bd);
-  } else if (eob <= 10) {
+  if (eob == 1) aom_highbd_idct16x16_1_add(input, dest, stride, bd);
+#if !CONFIG_ADAPT_SCAN
+  else if (eob <= 10)
     aom_highbd_idct16x16_10_add(input, dest, stride, bd);
-  } else {
+#endif
+  else
     aom_highbd_idct16x16_256_add(input, dest, stride, bd);
-  }
 }
 
 void av1_highbd_idct32x32_add(const tran_low_t *input, uint8_t *dest,
                               int stride, int eob, int bd) {
   // Non-zero coeff only in upper-left 8x8
-  if (eob == 1) {
-    aom_highbd_idct32x32_1_add(input, dest, stride, bd);
-  } else if (eob <= 34) {
+  if (eob == 1) aom_highbd_idct32x32_1_add(input, dest, stride, bd);
+#if !CONFIG_ADAPT_SCAN
+  else if (eob <= 34)
     aom_highbd_idct32x32_34_add(input, dest, stride, bd);
-  } else {
+#endif
+  else
     aom_highbd_idct32x32_1024_add(input, dest, stride, bd);
-  }
 }
 
 void av1_highbd_inv_txfm_add_4x4(const tran_low_t *input, uint8_t *dest,
diff --git a/av1/common/reconinter.c b/av1/common/reconinter.c
index ad9b462..6c4ae2a 100644
--- a/av1/common/reconinter.c
+++ b/av1/common/reconinter.c
@@ -546,6 +546,7 @@
   struct macroblockd_plane *const pd = &xd->plane[plane];
 #if CONFIG_MOTION_VAR
   const MODE_INFO *mi = xd->mi[mi_col_offset + xd->mi_stride * mi_row_offset];
+  const int build_for_obmc = !(mi_col_offset == 0 && mi_row_offset == 0);
 #else
   const MODE_INFO *mi = xd->mi[0];
 #endif  // CONFIG_MOTION_VAR
@@ -567,7 +568,11 @@
 // TODO(sarahparker) enable the use of DUAL_FILTER in warped motion functions
 // in order to allow GLOBAL_MOTION and DUAL_FILTER to work together
 #if CONFIG_DUAL_FILTER
+#if CONFIG_MOTION_VAR
+  if (mi->mbmi.sb_type < BLOCK_8X8 && plane > 0 && !build_for_obmc) {
+#else
   if (mi->mbmi.sb_type < BLOCK_8X8 && plane > 0) {
+#endif  // CONFIG_MOTION_VAR
     // block size in log2
     const int b4_wl = b_width_log2_lookup[mi->mbmi.sb_type];
     const int b4_hl = b_height_log2_lookup[mi->mbmi.sb_type];
@@ -648,7 +653,11 @@
 #endif
 
 #if CONFIG_SUB8X8_MC
+#if CONFIG_MOTION_VAR
+  if (mi->mbmi.sb_type < BLOCK_8X8 && plane > 0 && !build_for_obmc) {
+#else
   if (mi->mbmi.sb_type < BLOCK_8X8 && plane > 0) {
+#endif  // CONFIG_MOTION_VAR
     // block size in log2
     const int b4_wl = b_width_log2_lookup[mi->mbmi.sb_type];
     const int b4_hl = b_height_log2_lookup[mi->mbmi.sb_type];
@@ -734,7 +743,12 @@
     struct buf_2d *const dst_buf = &pd->dst;
     uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
     const MV mv = mi->mbmi.sb_type < BLOCK_8X8
+#if CONFIG_MOTION_VAR
+                      ? (build_for_obmc ? mi->bmi[block].as_mv[ref].as_mv
+                                        : average_split_mvs(pd, mi, ref, block))
+#else
                       ? average_split_mvs(pd, mi, ref, block)
+#endif  // CONFIG_MOTION_VAR
                       : mi->mbmi.mv[ref].as_mv;
 
     // TODO(jkoleszar): This clamping is done in the incorrect place for the
@@ -1371,9 +1385,11 @@
   const TileInfo *const tile = &xd->tile;
   BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
   int i, j, mi_step, ref;
+  int mb_to_right_edge_base = xd->mb_to_right_edge;
 
   if (mi_row <= tile->mi_row_start) return;
 
+  xd->mb_to_bottom_edge += xd->n8_h * 32;
   for (i = 0; i < AOMMIN(xd->n8_w, cm->mi_cols - mi_col); i += mi_step) {
     int mi_row_offset = -1;
     int mi_col_offset = i;
@@ -1412,6 +1428,8 @@
     }
 
     xd->mb_to_left_edge = -(((mi_col + i) * MI_SIZE) * 8);
+    xd->mb_to_right_edge =
+        mb_to_right_edge_base + (xd->n8_w - i - mi_step) * 64;
     mi_x = (mi_col + i) << MI_SIZE_LOG2;
     mi_y = mi_row << MI_SIZE_LOG2;
 
@@ -1425,19 +1443,19 @@
         const PARTITION_TYPE bp = BLOCK_8X8 - above_mbmi->sb_type;
         const int have_vsplit = bp != PARTITION_HORZ;
         const int have_hsplit = bp != PARTITION_VERT;
-        const int num_4x4_w = 2 >> ((!have_vsplit) | pd->subsampling_x);
-        const int num_4x4_h = 2 >> ((!have_hsplit) | pd->subsampling_y);
-        const int pw = 8 >> (have_vsplit | pd->subsampling_x);
+        const int num_4x4_w = 2 >> !have_vsplit;
+        const int num_4x4_h = 2 >> !have_hsplit;
+        const int pw = 8 >> (have_vsplit + pd->subsampling_x);
         int x, y;
 
         for (y = 0; y < num_4x4_h; ++y)
           for (x = 0; x < num_4x4_w; ++x) {
-            if ((bp == PARTITION_HORZ || bp == PARTITION_SPLIT) && y == 0 &&
-                !pd->subsampling_y)
+            if ((bp == PARTITION_HORZ || bp == PARTITION_SPLIT) && y == 0)
               continue;
 
             build_inter_predictors(xd, j, mi_col_offset, mi_row_offset,
-                                   y * 2 + x, bw, bh, 4 * x, 0, pw, bh,
+                                   y * 2 + x, bw, bh,
+                                   (4 * x) >> pd->subsampling_x, 0, pw, bh,
 #if CONFIG_SUPERTX && CONFIG_EXT_INTER
                                    0, 0,
 #endif  // CONFIG_SUPERTX && CONFIG_EXT_INTER
@@ -1457,6 +1475,8 @@
 #endif  // CONFIG_EXT_INTER
   }
   xd->mb_to_left_edge = -((mi_col * MI_SIZE) * 8);
+  xd->mb_to_right_edge = mb_to_right_edge_base;
+  xd->mb_to_bottom_edge -= xd->n8_h * 32;
 }
 
 void av1_build_prediction_by_left_preds(const AV1_COMMON *cm, MACROBLOCKD *xd,
@@ -1468,9 +1488,11 @@
   const TileInfo *const tile = &xd->tile;
   BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
   int i, j, mi_step, ref;
+  int mb_to_bottom_edge_base = xd->mb_to_bottom_edge;
 
   if (mi_col == 0 || (mi_col - 1 < tile->mi_col_start)) return;
 
+  xd->mb_to_right_edge += xd->n8_w * 32;
   for (i = 0; i < AOMMIN(xd->n8_h, cm->mi_rows - mi_row); i += mi_step) {
     int mi_row_offset = i;
     int mi_col_offset = -1;
@@ -1509,6 +1531,8 @@
     }
 
     xd->mb_to_top_edge = -(((mi_row + i) * MI_SIZE) * 8);
+    xd->mb_to_bottom_edge =
+        mb_to_bottom_edge_base + (xd->n8_h - i - mi_step) * 64;
     mi_x = mi_col << MI_SIZE_LOG2;
     mi_y = (mi_row + i) << MI_SIZE_LOG2;
 
@@ -1522,19 +1546,19 @@
         const PARTITION_TYPE bp = BLOCK_8X8 - left_mbmi->sb_type;
         const int have_vsplit = bp != PARTITION_HORZ;
         const int have_hsplit = bp != PARTITION_VERT;
-        const int num_4x4_w = 2 >> ((!have_vsplit) | pd->subsampling_x);
-        const int num_4x4_h = 2 >> ((!have_hsplit) | pd->subsampling_y);
-        const int ph = 8 >> (have_hsplit | pd->subsampling_y);
+        const int num_4x4_w = 2 >> !have_vsplit;
+        const int num_4x4_h = 2 >> !have_hsplit;
+        const int ph = 8 >> (have_hsplit + pd->subsampling_y);
         int x, y;
 
         for (y = 0; y < num_4x4_h; ++y)
           for (x = 0; x < num_4x4_w; ++x) {
-            if ((bp == PARTITION_VERT || bp == PARTITION_SPLIT) && x == 0 &&
-                !pd->subsampling_x)
+            if ((bp == PARTITION_VERT || bp == PARTITION_SPLIT) && x == 0)
               continue;
 
             build_inter_predictors(xd, j, mi_col_offset, mi_row_offset,
-                                   y * 2 + x, bw, bh, 0, 4 * y, bw, ph,
+                                   y * 2 + x, bw, bh, 0,
+                                   (4 * y) >> pd->subsampling_y, bw, ph,
 #if CONFIG_SUPERTX && CONFIG_EXT_INTER
                                    0, 0,
 #endif  // CONFIG_SUPERTX && CONFIG_EXT_INTER
@@ -1554,6 +1578,54 @@
 #endif  // CONFIG_EXT_INTER
   }
   xd->mb_to_top_edge = -((mi_row * MI_SIZE) * 8);
+  xd->mb_to_bottom_edge = mb_to_bottom_edge_base;
+  xd->mb_to_right_edge -= xd->n8_w * 32;
+}
+
+void av1_build_obmc_inter_predictors_sb(const AV1_COMMON *cm, MACROBLOCKD *xd,
+                                        int mi_row, int mi_col) {
+#if CONFIG_AOM_HIGHBITDEPTH
+  DECLARE_ALIGNED(16, uint8_t, tmp_buf1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
+  DECLARE_ALIGNED(16, uint8_t, tmp_buf2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
+#else
+  DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_SB_SQUARE]);
+  DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_SB_SQUARE]);
+#endif  // CONFIG_AOM_HIGHBITDEPTH
+  uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
+  int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
+  int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
+  int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
+  int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
+  int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
+  int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
+
+#if CONFIG_AOM_HIGHBITDEPTH
+  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
+    int len = sizeof(uint16_t);
+    dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
+    dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * len);
+    dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * 2 * len);
+    dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
+    dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * len);
+    dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * 2 * len);
+  } else {
+#endif  // CONFIG_AOM_HIGHBITDEPTH
+    dst_buf1[0] = tmp_buf1;
+    dst_buf1[1] = tmp_buf1 + MAX_SB_SQUARE;
+    dst_buf1[2] = tmp_buf1 + MAX_SB_SQUARE * 2;
+    dst_buf2[0] = tmp_buf2;
+    dst_buf2[1] = tmp_buf2 + MAX_SB_SQUARE;
+    dst_buf2[2] = tmp_buf2 + MAX_SB_SQUARE * 2;
+#if CONFIG_AOM_HIGHBITDEPTH
+  }
+#endif  // CONFIG_AOM_HIGHBITDEPTH
+  av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, dst_buf1,
+                                      dst_width1, dst_height1, dst_stride1);
+  av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, dst_buf2,
+                                     dst_width2, dst_height2, dst_stride2);
+  av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
+  av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, dst_buf1, dst_stride1,
+                                  dst_buf2, dst_stride2);
 }
 #endif  // CONFIG_MOTION_VAR
 
diff --git a/av1/common/reconinter.h b/av1/common/reconinter.h
index 5f62f0a..3eec384 100644
--- a/av1/common/reconinter.h
+++ b/av1/common/reconinter.h
@@ -535,6 +535,8 @@
                                         int tmp_width[MAX_MB_PLANE],
                                         int tmp_height[MAX_MB_PLANE],
                                         int tmp_stride[MAX_MB_PLANE]);
+void av1_build_obmc_inter_predictors_sb(const AV1_COMMON *cm, MACROBLOCKD *xd,
+                                        int mi_row, int mi_col);
 #endif  // CONFIG_MOTION_VAR
 
 #if CONFIG_EXT_INTER
diff --git a/av1/common/scan.h b/av1/common/scan.h
index 407c9ec..af39993 100644
--- a/av1/common/scan.h
+++ b/av1/common/scan.h
@@ -82,6 +82,10 @@
 
 static INLINE const SCAN_ORDER *get_scan(const AV1_COMMON *cm, TX_SIZE tx_size,
                                          TX_TYPE tx_type, int is_inter) {
+#if CONFIG_ADAPT_SCAN
+  (void)is_inter;
+  return &cm->fc->sc[tx_size][tx_type];
+#else  // CONFIG_ADAPT_SCAN
   (void)cm;
 #if CONFIG_EXT_TX
   return is_inter ? &av1_inter_scan_orders[tx_size][tx_type]
@@ -90,6 +94,7 @@
   (void)is_inter;
   return &av1_intra_scan_orders[tx_size][tx_type];
 #endif  // CONFIG_EXT_TX
+#endif  // CONFIG_ADAPT_SCAN
 }
 
 #ifdef __cplusplus
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 165609a..efb6465 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -303,6 +303,9 @@
     const int eob =
         av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
                                 tx_type, &max_scan_line, r, mbmi->segment_id);
+#if CONFIG_ADAPT_SCAN
+    av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
+#endif
     if (eob)
       inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
                               max_scan_line, eob);
@@ -384,6 +387,9 @@
   const int eob =
       av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
                               &max_scan_line, r, segment_id);
+#if CONFIG_ADAPT_SCAN
+  av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
+#endif
   if (eob)
     inverse_transform_block(xd, plane, tx_type, tx_size,
                             &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
@@ -1264,49 +1270,7 @@
     av1_build_inter_predictors_sb(xd, mi_row, mi_col, AOMMAX(bsize, BLOCK_8X8));
 #if CONFIG_MOTION_VAR
     if (mbmi->motion_mode == OBMC_CAUSAL) {
-#if CONFIG_AOM_HIGHBITDEPTH
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
-#else
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_SB_SQUARE]);
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_SB_SQUARE]);
-#endif  // CONFIG_AOM_HIGHBITDEPTH
-      uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
-      int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-
-      assert(mbmi->sb_type >= BLOCK_8X8);
-#if CONFIG_AOM_HIGHBITDEPTH
-      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
-        int len = sizeof(uint16_t);
-        dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
-        dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * len);
-        dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * 2 * len);
-        dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
-        dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * len);
-        dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * 2 * len);
-      } else {
-#endif  // CONFIG_AOM_HIGHBITDEPTH
-        dst_buf1[0] = tmp_buf1;
-        dst_buf1[1] = tmp_buf1 + MAX_SB_SQUARE;
-        dst_buf1[2] = tmp_buf1 + MAX_SB_SQUARE * 2;
-        dst_buf2[0] = tmp_buf2;
-        dst_buf2[1] = tmp_buf2 + MAX_SB_SQUARE;
-        dst_buf2[2] = tmp_buf2 + MAX_SB_SQUARE * 2;
-#if CONFIG_AOM_HIGHBITDEPTH
-      }
-#endif  // CONFIG_AOM_HIGHBITDEPTH
-      av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, dst_buf1,
-                                          dst_width1, dst_height1, dst_stride1);
-      av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, dst_buf2,
-                                         dst_width2, dst_height2, dst_stride2);
-      av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
-      av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, dst_buf1,
-                                      dst_stride1, dst_buf2, dst_stride2);
+      av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
     }
 #endif  // CONFIG_MOTION_VAR
 
@@ -2275,7 +2239,7 @@
 
 static InterpFilter read_interp_filter(struct aom_read_bit_buffer *rb) {
   return aom_rb_read_bit(rb) ? SWITCHABLE
-                             : aom_rb_read_literal(rb, 2 + CONFIG_EXT_INTERP);
+                             : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
 }
 
 static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
@@ -2814,9 +2778,9 @@
 #endif
 #if CONFIG_ACCOUNTING
       if (pbi->acct_enabled) {
-        tile_data->bit_reader.accounting = &pbi->accounting;
+        td->bit_reader.accounting = &pbi->accounting;
       } else {
-        tile_data->bit_reader.accounting = NULL;
+        td->bit_reader.accounting = NULL;
       }
 #endif
       av1_init_macroblockd(cm, &td->xd, td->dqcoeff);
@@ -2839,8 +2803,8 @@
       TileData *const td = pbi->tile_data + tile_cols * row + col;
 #if CONFIG_ACCOUNTING
       if (pbi->acct_enabled) {
-        tile_data->bit_reader.accounting->last_tell_frac =
-            aom_reader_tell_frac(&tile_data->bit_reader);
+        td->bit_reader.accounting->last_tell_frac =
+            aom_reader_tell_frac(&td->bit_reader);
       }
 #endif
 
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index e8c00c5..ef4a73e 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2914,7 +2914,7 @@
                                 struct aom_write_bit_buffer *wb) {
   aom_wb_write_bit(wb, filter == SWITCHABLE);
   if (filter != SWITCHABLE)
-    aom_wb_write_literal(wb, filter, 2 + CONFIG_EXT_INTERP);
+    aom_wb_write_literal(wb, filter, LOG_SWITCHABLE_FILTERS);
 }
 
 static void fix_interp_filter(AV1_COMMON *cm, FRAME_COUNTS *counts) {
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 5060e45..ffb5d46 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1327,8 +1327,8 @@
 #if CONFIG_DUAL_FILTER
       update_filter_type_count(td->counts, xd, mbmi);
 #else
-      const int ctx = av1_get_pred_context_switchable_interp(xd);
-      ++td->counts->switchable_interp[ctx][mbmi->interp_filter];
+      const int pred_ctx = av1_get_pred_context_switchable_interp(xd);
+      ++td->counts->switchable_interp[pred_ctx][mbmi->interp_filter];
 #endif
     }
 
@@ -5177,50 +5177,7 @@
 
 #if CONFIG_MOTION_VAR
     if (mbmi->motion_mode == OBMC_CAUSAL) {
-#if CONFIG_AOM_HIGHBITDEPTH
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
-#else
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_SB_SQUARE]);
-      DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_SB_SQUARE]);
-#endif  // CONFIG_AOM_HIGHBITDEPTH
-      uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
-      int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-      int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
-
-      assert(mbmi->sb_type >= BLOCK_8X8);
-
-#if CONFIG_AOM_HIGHBITDEPTH
-      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
-        int len = sizeof(uint16_t);
-        dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
-        dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * len);
-        dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * 2 * len);
-        dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
-        dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * len);
-        dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * 2 * len);
-      } else {
-#endif  // CONFIG_AOM_HIGHBITDEPTH
-        dst_buf1[0] = tmp_buf1;
-        dst_buf1[1] = tmp_buf1 + MAX_SB_SQUARE;
-        dst_buf1[2] = tmp_buf1 + MAX_SB_SQUARE * 2;
-        dst_buf2[0] = tmp_buf2;
-        dst_buf2[1] = tmp_buf2 + MAX_SB_SQUARE;
-        dst_buf2[2] = tmp_buf2 + MAX_SB_SQUARE * 2;
-#if CONFIG_AOM_HIGHBITDEPTH
-      }
-#endif  // CONFIG_AOM_HIGHBITDEPTH
-      av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, dst_buf1,
-                                          dst_width1, dst_height1, dst_stride1);
-      av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, dst_buf2,
-                                         dst_width2, dst_height2, dst_stride2);
-      av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
-      av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, dst_buf1,
-                                      dst_stride1, dst_buf2, dst_stride2);
+      av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
     }
 #endif  // CONFIG_MOTION_VAR
 
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index dc97ddf..0f7fcca 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -535,6 +535,9 @@
   }
 
   av1_init_mv_probs(cm);
+#if CONFIG_ADAPT_SCAN
+  av1_init_scan_order(cm);
+#endif
   av1_initialize_rd_consts(cpi);
 
   // Tiling is ignored in the first pass.
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index 69273828..43ed837 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -504,6 +504,14 @@
 
   *tp = t;
 
+#if CONFIG_ADAPT_SCAN
+  // Since dqcoeff is not available here, we pass qcoeff into
+  // av1_update_scan_count_facade(). The update behavior should be the same
+  // because av1_update_scan_count_facade() only cares if coefficients are zero
+  // or not.
+  av1_update_scan_count_facade((AV1_COMMON *)cm, tx_size, tx_type, qcoeff, c);
+#endif
+
   av1_set_contexts(xd, pd, tx_size, c > 0, blk_col, blk_row);
 }
 
diff --git a/test/av1_inv_txfm_test.cc b/test/av1_inv_txfm_test.cc
index ff358b6..84e2402 100644
--- a/test/av1_inv_txfm_test.cc
+++ b/test/av1_inv_txfm_test.cc
@@ -137,6 +137,7 @@
   InvTxfmFunc partial_itxfm_;
 };
 
+#if !CONFIG_ADAPT_SCAN
 TEST_P(AV1PartialIDctTest, RunQuantCheck) {
   int size;
   switch (tx_size_) {
@@ -256,6 +257,7 @@
   EXPECT_EQ(0, max_error)
       << "Error: partial inverse transform produces different results";
 }
+#endif
 using std::tr1::make_tuple;
 
 INSTANTIATE_TEST_CASE_P(
diff --git a/test/test.mk b/test/test.mk
index 61ea1d0..2d18f69 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -113,6 +113,7 @@
 #LIBAOM_TEST_SRCS-yes                   += encoder_parms_get_to_decoder.cc
 endif
 
+LIBAOM_TEST_SRCS-$(CONFIG_ADAPT_SCAN)  += scan_test.cc
 #LIBAOM_TEST_SRCS-yes                   += convolve_test.cc
 LIBAOM_TEST_SRCS-yes                   += lpf_8_test.cc
 LIBAOM_TEST_SRCS-$(CONFIG_CLPF)        += clpf_test.cc