Merge "Unify set_contexts() function for encoder and decoder" into nextgenv2
diff --git a/av1/common/blockd.c b/av1/common/blockd.c
index 8938bbf..6332fed 100644
--- a/av1/common/blockd.c
+++ b/av1/common/blockd.c
@@ -92,40 +92,38 @@
 }
 
 void av1_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
-                      BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
-                      int aoff, int loff) {
+                      TX_SIZE tx_size, int has_eob, int aoff, int loff) {
   ENTROPY_CONTEXT *const a = pd->above_context + aoff;
   ENTROPY_CONTEXT *const l = pd->left_context + loff;
-  const int tx_w_in_blocks = num_4x4_blocks_wide_txsize_lookup[tx_size];
-  const int tx_h_in_blocks = num_4x4_blocks_high_txsize_lookup[tx_size];
+  const int tx_size_in_blocks = 1 << tx_size;
 
   // above
   if (has_eob && xd->mb_to_right_edge < 0) {
     int i;
-    const int blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize] +
-                            (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
-    int above_contexts = tx_w_in_blocks;
+    const int blocks_wide =
+        pd->n4_w + (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
+    int above_contexts = tx_size_in_blocks;
     if (above_contexts + aoff > blocks_wide)
       above_contexts = blocks_wide - aoff;
 
     for (i = 0; i < above_contexts; ++i) a[i] = has_eob;
-    for (i = above_contexts; i < tx_w_in_blocks; ++i) a[i] = 0;
+    for (i = above_contexts; i < tx_size_in_blocks; ++i) a[i] = 0;
   } else {
-    memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_w_in_blocks);
+    memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
   }
 
   // left
   if (has_eob && xd->mb_to_bottom_edge < 0) {
     int i;
-    const int blocks_high = num_4x4_blocks_high_lookup[plane_bsize] +
-                            (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
-    int left_contexts = tx_h_in_blocks;
+    const int blocks_high =
+        pd->n4_h + (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
+    int left_contexts = tx_size_in_blocks;
     if (left_contexts + loff > blocks_high) left_contexts = blocks_high - loff;
 
     for (i = 0; i < left_contexts; ++i) l[i] = has_eob;
-    for (i = left_contexts; i < tx_h_in_blocks; ++i) l[i] = 0;
+    for (i = left_contexts; i < tx_size_in_blocks; ++i) l[i] = 0;
   } else {
-    memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_h_in_blocks);
+    memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
   }
 }
 
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 6296faa..4d9bff9 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -757,8 +757,7 @@
                                    void *arg);
 
 void av1_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
-                      BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
-                      int aoff, int loff);
+                      TX_SIZE tx_size, int has_eob, int aoff, int loff);
 
 #if CONFIG_EXT_INTER
 static INLINE int is_interintra_allowed_bsize(const BLOCK_SIZE bsize) {
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index afc9da4..3c8eac8 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -520,6 +520,17 @@
   return len + MAX_MIB_SIZE;
 }
 
+static INLINE void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
+                                int bhl) {
+  int i;
+  for (i = 0; i < MAX_MB_PLANE; i++) {
+    xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
+    xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
+    xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
+    xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
+  }
+}
+
 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
                                   int mi_row, int bh, int mi_col, int bw,
                                   int mi_rows, int mi_cols) {
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 402dc06..67d0eea 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -393,17 +393,6 @@
   }
 }
 
-static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
-                         int bhl) {
-  int i;
-  for (i = 0; i < MAX_MB_PLANE; i++) {
-    xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
-    xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
-    xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
-    xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
-  }
-}
-
 static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                  BLOCK_SIZE bsize, int mi_row, int mi_col,
                                  int bw, int bh, int x_mis, int y_mis, int bwl,
diff --git a/av1/decoder/detokenize.c b/av1/decoder/detokenize.c
index b5f26d2..f2f74f5 100644
--- a/av1/decoder/detokenize.c
+++ b/av1/decoder/detokenize.c
@@ -294,47 +294,6 @@
   return c;
 }
 
-// TODO(slavarnway): Decode version of av1_set_context.  Modify
-// av1_set_context
-// after testing is complete, then delete this version.
-static void dec_set_contexts(const MACROBLOCKD *xd,
-                             struct macroblockd_plane *pd, TX_SIZE tx_size,
-                             int has_eob, int aoff, int loff) {
-  ENTROPY_CONTEXT *const a = pd->above_context + aoff;
-  ENTROPY_CONTEXT *const l = pd->left_context + loff;
-  const int tx_w_in_blocks = num_4x4_blocks_wide_txsize_lookup[tx_size];
-  const int tx_h_in_blocks = num_4x4_blocks_high_txsize_lookup[tx_size];
-
-  // above
-  if (has_eob && xd->mb_to_right_edge < 0) {
-    int i;
-    const int blocks_wide =
-        pd->n4_w + (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
-    int above_contexts = tx_w_in_blocks;
-    if (above_contexts + aoff > blocks_wide)
-      above_contexts = blocks_wide - aoff;
-
-    for (i = 0; i < above_contexts; ++i) a[i] = has_eob;
-    for (i = above_contexts; i < tx_w_in_blocks; ++i) a[i] = 0;
-  } else {
-    memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_w_in_blocks);
-  }
-
-  // left
-  if (has_eob && xd->mb_to_bottom_edge < 0) {
-    int i;
-    const int blocks_high =
-        pd->n4_h + (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
-    int left_contexts = tx_h_in_blocks;
-    if (left_contexts + loff > blocks_high) left_contexts = blocks_high - loff;
-
-    for (i = 0; i < left_contexts; ++i) l[i] = has_eob;
-    for (i = left_contexts; i < tx_h_in_blocks; ++i) l[i] = 0;
-  } else {
-    memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_h_in_blocks);
-  }
-}
-
 #if CONFIG_PALETTE
 void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
                                aom_reader *r) {
@@ -391,11 +350,6 @@
 #endif  // CONFIG_NEW_QUANT
                    ctx, sc->scan, sc->neighbors, r);
 #endif  // CONFIG_AOM_QM
-  dec_set_contexts(xd, pd, tx_size, eob > 0, x, y);
-  /*
-  av1_set_contexts(xd, pd,
-                    get_plane_block_size(xd->mi[0]->mbmi.sb_type, pd),
-                    tx_size, eob > 0, x, y);
-                    */
+  av1_set_contexts(xd, pd, tx_size, eob > 0, x, y);
   return eob;
 }
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 04d5282..d3b97d6 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -262,6 +262,8 @@
   MACROBLOCKD *const xd = &x->e_mbd;
   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
   const int mi_height = num_8x8_blocks_high_lookup[bsize];
+  const int bwl = b_width_log2_lookup[AOMMAX(bsize, BLOCK_8X8)];
+  const int bhl = b_height_log2_lookup[AOMMAX(bsize, BLOCK_8X8)];
 
   set_skip_context(xd, mi_row, mi_col);
 
@@ -284,6 +286,8 @@
   x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + AOM_INTERP_EXTEND;
   x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + AOM_INTERP_EXTEND;
 
+  set_plane_n4(xd, mi_width, mi_height, bwl, bhl);
+
   // Set up distance of MB to edge of frame in 1/8th pel units.
   assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
   set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width, cm->mi_rows,
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index 9ded093..67f4b5d 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -369,8 +369,8 @@
   int rate = av1_cost_coeffs(cm, x, plane, block, pt, tx_size, scan_order->scan,
                              scan_order->neighbors, 0);
   args->this_rate += rate;
-  av1_set_contexts(xd, pd, plane_bsize, tx_size, p->eobs[block] > 0, blk_col,
-                   blk_row);
+  (void)plane_bsize;
+  av1_set_contexts(xd, pd, tx_size, p->eobs[block] > 0, blk_col, blk_row);
 }
 
 static void set_entropy_context_b(int plane, int block, int blk_row,
@@ -382,8 +382,8 @@
   MACROBLOCKD *const xd = &x->e_mbd;
   struct macroblock_plane *p = &x->plane[plane];
   struct macroblockd_plane *pd = &xd->plane[plane];
-  av1_set_contexts(xd, pd, plane_bsize, tx_size, p->eobs[block] > 0, blk_col,
-                   blk_row);
+  (void)plane_bsize;
+  av1_set_contexts(xd, pd, tx_size, p->eobs[block] > 0, blk_col, blk_row);
 }
 
 static INLINE void add_token(TOKENEXTRA **t, const aom_prob *context_tree,
@@ -498,6 +498,7 @@
   int skip_eob = 0;
   int16_t token;
   EXTRABIT extra;
+  (void)plane_bsize;
   pt = get_entropy_context(tx_size, pd->above_context + blk_col,
                            pd->left_context + blk_row);
   scan = scan_order->scan;
@@ -532,7 +533,7 @@
 
   *tp = t;
 
-  av1_set_contexts(xd, pd, plane_bsize, tx_size, c > 0, blk_col, blk_row);
+  av1_set_contexts(xd, pd, tx_size, c > 0, blk_col, blk_row);
 }
 
 struct is_skippable_args {