Merge "EC_ADAPT: send updates for the correct nodes." into nextgenv2
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index a7eb71e..e8069d6 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -142,10 +142,10 @@
     return AOM_CODEC_INVALID_PARAM; \
   } while (0)
 
-#define RANGE_CHECK(p, memb, lo, hi)                                 \
-  do {                                                               \
-    if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
-      ERROR(#memb " out of range [" #lo ".." #hi "]");               \
+#define RANGE_CHECK(p, memb, lo, hi)                   \
+  do {                                                 \
+    if (!((p)->memb >= (lo) && (p)->memb <= (hi)))     \
+      ERROR(#memb " out of range [" #lo ".." #hi "]"); \
   } while (0)
 
 #define RANGE_CHECK_HI(p, memb, hi)                                     \
@@ -176,7 +176,7 @@
   RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
   RANGE_CHECK_BOOL(extra_cfg, lossless);
   RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 1);
-  RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
+  RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
   RANGE_CHECK_HI(cfg, g_threads, 64);
   RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
   RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
@@ -189,8 +189,8 @@
   RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
   RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
   RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_LAST_PASS);
-  RANGE_CHECK(extra_cfg, min_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
-  RANGE_CHECK(extra_cfg, max_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
+  RANGE_CHECK_HI(extra_cfg, min_gf_interval, MAX_LAG_BUFFERS - 1);
+  RANGE_CHECK_HI(extra_cfg, max_gf_interval, MAX_LAG_BUFFERS - 1);
   if (extra_cfg->max_gf_interval > 0) {
     RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
   }
@@ -200,8 +200,8 @@
   }
 
   if (cfg->rc_resize_allowed == 1) {
-    RANGE_CHECK(cfg, rc_scaled_width, 0, cfg->g_w);
-    RANGE_CHECK(cfg, rc_scaled_height, 0, cfg->g_h);
+    RANGE_CHECK_HI(cfg, rc_scaled_width, cfg->g_w);
+    RANGE_CHECK_HI(cfg, rc_scaled_height, cfg->g_h);
   }
 
   // AV1 does not support a lower bound on the keyframe interval in
@@ -212,9 +212,9 @@
         "kf_min_dist not supported in auto mode, use 0 "
         "or kf_max_dist instead.");
 
-  RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, 2);
+  RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 2);
 #if CONFIG_EXT_REFS
-  RANGE_CHECK(extra_cfg, enable_auto_bwd_ref, 0, 2);
+  RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
 #endif  // CONFIG_EXT_REFS
   RANGE_CHECK(extra_cfg, cpu_used, -8, 8);
   RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
@@ -239,13 +239,13 @@
       RANGE_CHECK(extra_cfg, tile_rows, 1, 64);
   }
 #else
-  RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
-  RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
+  RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
+  RANGE_CHECK_HI(extra_cfg, tile_rows, 2);
 #endif  // CONFIG_EXT_TILE
   RANGE_CHECK_HI(extra_cfg, sharpness, 7);
-  RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
+  RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
   RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
-  RANGE_CHECK(extra_cfg, cq_level, 0, 63);
+  RANGE_CHECK_HI(extra_cfg, cq_level, 63);
   RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
   RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
   RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
diff --git a/av1/common/pred_common.c b/av1/common/pred_common.c
index 35067f2..5b7c2ec 100644
--- a/av1/common/pred_common.c
+++ b/av1/common/pred_common.c
@@ -756,36 +756,24 @@
     } else if (above_intra || left_intra) {  // intra/inter or inter/intra
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
 
-      if (!has_second_ref(edge_mbmi))
+      if (!has_second_ref(edge_mbmi))  // single
         pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
-      else
-        pred_context = 1 + (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) ||
-                            !CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[1]));
+      else  // comp
+        pred_context = 2;
     } else {  // inter/inter
       const int above_has_second = has_second_ref(above_mbmi);
       const int left_has_second = has_second_ref(left_mbmi);
 
       const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
-      const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
       const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
-      const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
 
-      if (above_has_second && left_has_second) {
-        pred_context =
-            1 + (!CHECK_BACKWARD_REFS(above0) || !CHECK_BACKWARD_REFS(above1) ||
-                 !CHECK_BACKWARD_REFS(left0) || !CHECK_BACKWARD_REFS(left1));
-      } else if (above_has_second || left_has_second) {
+      if (above_has_second && left_has_second) {  // comp/comp
+        pred_context = 2;
+      } else if (above_has_second || left_has_second) {  // single/comp
         const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
-        const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
-        const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
 
-        if (!CHECK_BACKWARD_REFS(rfs))
-          pred_context =
-              3 + (!CHECK_BACKWARD_REFS(crf1) || !CHECK_BACKWARD_REFS(crf2));
-        else
-          pred_context =
-              !CHECK_BACKWARD_REFS(crf1) || !CHECK_BACKWARD_REFS(crf2);
-      } else {
+        pred_context = (!CHECK_BACKWARD_REFS(rfs)) ? 4 : 1;
+      } else {  // single/single
         pred_context = 2 * (!CHECK_BACKWARD_REFS(above0)) +
                        2 * (!CHECK_BACKWARD_REFS(left0));
       }
@@ -794,12 +782,11 @@
     const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
     if (!is_inter_block(edge_mbmi)) {  // intra
       pred_context = 2;
-    } else {  // inter
-      if (!has_second_ref(edge_mbmi))
+    } else {                           // inter
+      if (!has_second_ref(edge_mbmi))  // single
         pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
-      else
-        pred_context = 1 + (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) ||
-                            !CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[1]));
+      else  // comp
+        pred_context = 2;
     }
   } else {  // no edges available
     pred_context = 2;
@@ -833,12 +820,12 @@
       pred_context = 2;
     } else if (above_intra || left_intra) {  // intra/inter or inter/intra
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
-      if (!has_second_ref(edge_mbmi)) {
+      if (!has_second_ref(edge_mbmi)) {  // single
         if (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]))
           pred_context = 3;
         else
           pred_context = 4 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME);
-      } else {
+      } else {  // comp
         pred_context = 1 +
                        2 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME ||
                             edge_mbmi->ref_frame[1] == BWDREF_FRAME);
@@ -851,14 +838,14 @@
       const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
       const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
 
-      if (above_has_second && left_has_second) {
+      if (above_has_second && left_has_second) {  // comp/comp
         if (above0 == left0 && above1 == left1)
           pred_context =
               3 * (above0 == BWDREF_FRAME || above1 == BWDREF_FRAME ||
                    left0 == BWDREF_FRAME || left1 == BWDREF_FRAME);
         else
           pred_context = 2;
-      } else if (above_has_second || left_has_second) {
+      } else if (above_has_second || left_has_second) {  // single/comp
         const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
@@ -869,7 +856,7 @@
           pred_context = (crf1 == BWDREF_FRAME || crf2 == BWDREF_FRAME);
         else
           pred_context = 1 + 2 * (crf1 == BWDREF_FRAME || crf2 == BWDREF_FRAME);
-      } else {
+      } else {  // single/single
         if (!CHECK_BACKWARD_REFS(above0) && !CHECK_BACKWARD_REFS(left0)) {
           pred_context = 2 + (above0 == left0);
         } else if (!CHECK_BACKWARD_REFS(above0) ||
@@ -890,9 +877,9 @@
         (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) &&
          !has_second_ref(edge_mbmi)))
       pred_context = 2;
-    else if (!has_second_ref(edge_mbmi))
+    else if (!has_second_ref(edge_mbmi))  // single
       pred_context = 4 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME);
-    else
+    else  // comp
       pred_context = 3 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME ||
                           edge_mbmi->ref_frame[1] == BWDREF_FRAME);
   } else {  // no edges available (2)
@@ -927,12 +914,12 @@
       pred_context = 2;
     } else if (above_intra || left_intra) {  // intra/inter or inter/intra
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
-      if (!has_second_ref(edge_mbmi)) {
+      if (!has_second_ref(edge_mbmi)) {  // single
         if (CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]))
           pred_context = 3;
         else
           pred_context = 4 * CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]);
-      } else {
+      } else {  // comp
         pred_context = 1 +
                        2 * (CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]) ||
                             CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[1]));
@@ -945,14 +932,14 @@
       const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
       const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
 
-      if (above_has_second && left_has_second) {
+      if (above_has_second && left_has_second) {  // comp/comp
         if (above0 == left0 && above1 == left1)
           pred_context =
               3 * (CHECK_LAST_OR_LAST2(above0) || CHECK_LAST_OR_LAST2(above1) ||
                    CHECK_LAST_OR_LAST2(left0) || CHECK_LAST_OR_LAST2(left1));
         else
           pred_context = 2;
-      } else if (above_has_second || left_has_second) {
+      } else if (above_has_second || left_has_second) {  // single/comp
         const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
@@ -966,7 +953,7 @@
         else
           pred_context =
               1 + 2 * (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
-      } else {
+      } else {  // single/single
         if (CHECK_BACKWARD_REFS(above0) && CHECK_BACKWARD_REFS(left0)) {
           pred_context = 2 + (above0 == left0);
         } else if (CHECK_BACKWARD_REFS(above0) || CHECK_BACKWARD_REFS(left0)) {
@@ -986,9 +973,9 @@
         (CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) &&
          !has_second_ref(edge_mbmi)))
       pred_context = 2;
-    else if (!has_second_ref(edge_mbmi))
+    else if (!has_second_ref(edge_mbmi))  // single
       pred_context = 4 * (CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]));
-    else
+    else  // comp
       pred_context = 3 * (CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]) ||
                           CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[1]));
   } else {  // no edges available (2)
@@ -1023,12 +1010,12 @@
       pred_context = 2;
     } else if (above_intra || left_intra) {  // intra/inter or inter/intra
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
-      if (!has_second_ref(edge_mbmi)) {
+      if (!has_second_ref(edge_mbmi)) {  // single
         if (!CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]))
           pred_context = 3;
         else
           pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
-      } else {
+      } else {  // comp
         pred_context = 1 +
                        2 * (edge_mbmi->ref_frame[0] == LAST_FRAME ||
                             edge_mbmi->ref_frame[1] == LAST_FRAME);
@@ -1041,13 +1028,13 @@
       const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
       const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
 
-      if (above_has_second && left_has_second) {
+      if (above_has_second && left_has_second) {  // comp/comp
         if (above0 == left0 && above1 == left1)
           pred_context = 3 * (above0 == LAST_FRAME || above1 == LAST_FRAME ||
                               left0 == LAST_FRAME || left1 == LAST_FRAME);
         else
           pred_context = 2;
-      } else if (above_has_second || left_has_second) {
+      } else if (above_has_second || left_has_second) {  // single/comp
         const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
@@ -1058,7 +1045,7 @@
           pred_context = (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
         else
           pred_context = 1 + 2 * (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
-      } else {
+      } else {  // single/single
         if (!CHECK_LAST_OR_LAST2(above0) && !CHECK_LAST_OR_LAST2(left0)) {
           pred_context = 2 + (above0 == left0);
         } else if (!CHECK_LAST_OR_LAST2(above0) ||
@@ -1078,9 +1065,9 @@
         (!CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]) &&
          !has_second_ref(edge_mbmi)))
       pred_context = 2;
-    else if (!has_second_ref(edge_mbmi))
+    else if (!has_second_ref(edge_mbmi))  // single
       pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
-    else
+    else  // comp
       pred_context = 3 * (edge_mbmi->ref_frame[0] == LAST_FRAME ||
                           edge_mbmi->ref_frame[1] == LAST_FRAME);
   } else {  // no edges available (2)
@@ -1115,12 +1102,12 @@
       pred_context = 2;
     } else if (above_intra || left_intra) {  // intra/inter or inter/intra
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
-      if (!has_second_ref(edge_mbmi)) {
+      if (!has_second_ref(edge_mbmi)) {  // single
         if (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]))
           pred_context = 3;
         else
           pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST3_FRAME);
-      } else {
+      } else {  // comp
         pred_context = 1 +
                        2 * (edge_mbmi->ref_frame[0] == LAST3_FRAME ||
                             edge_mbmi->ref_frame[1] == LAST3_FRAME);
@@ -1133,13 +1120,13 @@
       const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
       const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
 
-      if (above_has_second && left_has_second) {
+      if (above_has_second && left_has_second) {  // comp/comp
         if (above0 == left0 && above1 == left1)
           pred_context = 3 * (above0 == LAST3_FRAME || above1 == LAST3_FRAME ||
                               left0 == LAST3_FRAME || left1 == LAST3_FRAME);
         else
           pred_context = 2;
-      } else if (above_has_second || left_has_second) {
+      } else if (above_has_second || left_has_second) {  // single/comp
         const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
@@ -1150,7 +1137,7 @@
           pred_context = (crf1 == LAST3_FRAME || crf2 == LAST3_FRAME);
         else
           pred_context = 1 + 2 * (crf1 == LAST3_FRAME || crf2 == LAST3_FRAME);
-      } else {
+      } else {  // single/single
         if (!CHECK_GOLDEN_OR_LAST3(above0) && !CHECK_GOLDEN_OR_LAST3(left0)) {
           pred_context = 2 + (above0 == left0);
         } else if (!CHECK_GOLDEN_OR_LAST3(above0) ||
@@ -1171,9 +1158,9 @@
         (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]) &&
          !has_second_ref(edge_mbmi)))
       pred_context = 2;
-    else if (!has_second_ref(edge_mbmi))
+    else if (!has_second_ref(edge_mbmi))  // single
       pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST3_FRAME);
-    else
+    else  // comp
       pred_context = 3 * (edge_mbmi->ref_frame[0] == LAST3_FRAME ||
                           edge_mbmi->ref_frame[1] == LAST3_FRAME);
   } else {  // no edges available (2)
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 2e69a14..8d217f9 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1761,20 +1761,21 @@
       assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
       for (i = 0; i < MAX_MB_PLANE; ++i) {
         const struct macroblockd_plane *const pd = &xd->plane[i];
-        const int num_4x4_w = pd->n4_w;
-        const int num_4x4_h = pd->n4_h;
         int row, col;
         const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
-        const int stepr = num_4x4_blocks_high_txsize_lookup[tx_size];
-        const int stepc = num_4x4_blocks_wide_txsize_lookup[tx_size];
-        const int max_blocks_wide =
-            num_4x4_w + (xd->mb_to_right_edge >= 0
+        const int stepr = tx_size_high_unit[tx_size];
+        const int stepc = tx_size_wide_unit[tx_size];
+        int max_blocks_wide =
+            pd->width + (xd->mb_to_right_edge >= 0
                              ? 0
-                             : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
-        const int max_blocks_high =
-            num_4x4_h +
+                             : xd->mb_to_right_edge >> (3 + pd->subsampling_x));
+        int max_blocks_high =
+            pd->height +
             (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
-                                                  (5 + pd->subsampling_y));
+                                                  (3 + pd->subsampling_y));
+
+        max_blocks_wide >>= tx_size_wide_log2[0];
+        max_blocks_high >>= tx_size_wide_log2[0];
 
         for (row = 0; row < max_blocks_high; row += stepr)
           for (col = 0; col < max_blocks_wide; col += stepc)
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 21725d7..bf50bc4 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1305,8 +1305,8 @@
 #if CONFIG_VAR_TX
   {
     const TX_SIZE mtx = mbmi->tx_size;
-    const int num_4x4_blocks_wide = num_4x4_blocks_wide_txsize_lookup[mtx] >> 1;
-    const int num_4x4_blocks_high = num_4x4_blocks_high_txsize_lookup[mtx] >> 1;
+    const int num_4x4_blocks_wide = tx_size_wide_unit[mtx] >> 1;
+    const int num_4x4_blocks_high = tx_size_high_unit[mtx] >> 1;
     int idy, idx;
     mbmi->inter_tx_size[0][0] = mtx;
     for (idy = 0; idy < num_4x4_blocks_high; ++idy)
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 3a37fd9..dbb7dc4 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -2918,9 +2918,12 @@
   TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
   const SCAN_ORDER *const scan_order =
       get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
-
   BLOCK_SIZE txm_bsize = txsize_to_bsize[tx_size];
-  int bh = 4 * num_4x4_blocks_wide_lookup[txm_bsize];
+  int bh = block_size_high[txm_bsize];
+  int bw = block_size_wide[txm_bsize];
+  int txb_h = tx_size_high_unit[tx_size];
+  int txb_w = tx_size_wide_unit[tx_size];
+
   int src_stride = p->src.stride;
   uint8_t *src = &p->src.buf[4 * blk_row * src_stride + 4 * blk_col];
   uint8_t *dst = &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col];
@@ -2930,20 +2933,21 @@
 #else
   DECLARE_ALIGNED(16, uint8_t, rec_buffer[MAX_TX_SQUARE]);
 #endif  // CONFIG_AOM_HIGHBITDEPTH
-  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
+  int max_blocks_high = block_size_high[plane_bsize];
+  int max_blocks_wide = block_size_wide[plane_bsize];
+  const int diff_stride = max_blocks_wide;
   const int16_t *diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];
-
-  int max_blocks_high = num_4x4_blocks_high_lookup[plane_bsize];
-  int max_blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize];
-
 #if CONFIG_EXT_TX
   assert(tx_size < TX_SIZES);
 #endif  // CONFIG_EXT_TX
 
   if (xd->mb_to_bottom_edge < 0)
-    max_blocks_high += xd->mb_to_bottom_edge >> (5 + pd->subsampling_y);
+    max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y);
   if (xd->mb_to_right_edge < 0)
-    max_blocks_wide += xd->mb_to_right_edge >> (5 + pd->subsampling_x);
+    max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x);
+
+  max_blocks_high >>= tx_size_wide_log2[0];
+  max_blocks_wide >>= tx_size_wide_log2[0];
 
 #if CONFIG_NEW_QUANT
   av1_xform_quant_fp_nuq(cm, x, plane, block, blk_row, blk_col, plane_bsize,
@@ -2960,22 +2964,21 @@
   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     rec_buffer = CONVERT_TO_BYTEPTR(rec_buffer16);
     aom_highbd_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL,
-                             0, NULL, 0, bh, bh, xd->bd);
+                             0, NULL, 0, bw, bh, xd->bd);
   } else {
     rec_buffer = (uint8_t *)rec_buffer16;
     aom_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL, 0,
-                      NULL, 0, bh, bh);
+                      NULL, 0, bw, bh);
   }
 #else
   aom_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL, 0, NULL,
-                    0, bh, bh);
+                    0, bw, bh);
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
-  if (blk_row + (bh >> 2) > max_blocks_high ||
-      blk_col + (bh >> 2) > max_blocks_wide) {
+  if (blk_row + txb_h > max_blocks_high || blk_col + txb_w > max_blocks_wide) {
     int idx, idy;
-    int blocks_height = AOMMIN(bh >> 2, max_blocks_high - blk_row);
-    int blocks_width = AOMMIN(bh >> 2, max_blocks_wide - blk_col);
+    int blocks_height = AOMMIN(txb_h, max_blocks_high - blk_row);
+    int blocks_width = AOMMIN(txb_w, max_blocks_wide - blk_col);
     tmp = 0;
     for (idy = 0; idy < blocks_height; idy += 2) {
       for (idx = 0; idx < blocks_width; idx += 2) {
@@ -2984,7 +2987,7 @@
       }
     }
   } else {
-    tmp = aom_sum_squares_2d_i16(diff, diff_stride, bh);
+    tmp = sum_squares_2d(diff, diff_stride, tx_size);
   }
 
 #if CONFIG_AOM_HIGHBITDEPTH
@@ -3010,12 +3013,12 @@
     inv_txfm_add(dqcoeff, rec_buffer, MAX_TX_SIZE, &inv_txfm_param);
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
-    if ((bh >> 2) + blk_col > max_blocks_wide ||
-        (bh >> 2) + blk_row > max_blocks_high) {
+    if (txb_h + blk_col > max_blocks_wide ||
+        txb_w + blk_row > max_blocks_high) {
       int idx, idy;
       unsigned int this_dist;
-      int blocks_height = AOMMIN(bh >> 2, max_blocks_high - blk_row);
-      int blocks_width = AOMMIN(bh >> 2, max_blocks_wide - blk_col);
+      int blocks_height = AOMMIN(txb_h, max_blocks_high - blk_row);
+      int blocks_width = AOMMIN(txb_w, max_blocks_wide - blk_col);
       tmp = 0;
       for (idy = 0; idy < blocks_height; idy += 2) {
         for (idx = 0; idx < blocks_width; idx += 2) {
diff --git a/configure b/configure
index ba1bcad..44bfe02 100755
--- a/configure
+++ b/configure
@@ -629,6 +629,7 @@
         check_add_cflags -Wuninitialized
         check_add_cflags -Wunused
         check_add_cflags -Wsign-compare
+        check_add_cflags -Wlogical-op
         # Enabling the following warning (in combination with -Wunused above)
         # for C++ generates errors in third_party code including googletest and
         # libyuv. So enable it only for C code.