Merge "Fix the bug that PVQ commit broke dering" into nextgenv2
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 956c6c5..439a6b4 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -211,6 +211,23 @@
 } FILTER_INTRA_MODE_INFO;
 #endif  // CONFIG_FILTER_INTRA
 
+#if CONFIG_VAR_TX
+#define TXB_COEFF_COST_MAP_SIZE (2 * MAX_MIB_SIZE)
+
+// TODO(angiebird): Merge RD_COST and RD_STATS
+typedef struct RD_STATS {
+  int rate;
+  int64_t dist;
+  int64_t sse;
+  int skip;
+#if CONFIG_RD_DEBUG
+  int txb_coeff_cost[MAX_MB_PLANE];
+  int txb_coeff_cost_map[MAX_MB_PLANE][TXB_COEFF_COST_MAP_SIZE]
+                        [TXB_COEFF_COST_MAP_SIZE];
+#endif
+} RD_STATS;
+#endif  // CONFIG_VAR_TX
+
 // This structure now relates to 8x8 block regions.
 typedef struct {
   // Common for both INTER and INTRA blocks
@@ -283,7 +300,7 @@
   int current_q_index;
 #endif
 #if CONFIG_RD_DEBUG
-  int64_t txb_coeff_cost[MAX_MB_PLANE];
+  RD_STATS rd_stats;
   int mi_row;
   int mi_col;
 #endif
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 43452a8..4b47dd4 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1929,7 +1929,7 @@
   }
 #if CONFIG_RD_DEBUG
   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
-    if (m->mbmi.txb_coeff_cost[plane] != txb_coeff_cost[plane]) {
+    if (m->mbmi.rd_stats.txb_coeff_cost[plane] != txb_coeff_cost[plane]) {
       dump_mode_info(m);
       assert(0);
     }
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index fa497cb..8334842 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -3217,7 +3217,19 @@
   rd_stats->rate += txb_coeff_cost;
   rd_stats->skip &= (p->eobs[block] == 0);
 #if CONFIG_RD_DEBUG
-  rd_stats->txb_coeff_cost[plane] += txb_coeff_cost;
+  {
+    int idx, idy;
+    rd_stats->txb_coeff_cost[plane] += txb_coeff_cost;
+
+    for (idy = 0; idy < txb_h; ++idy)
+      for (idx = 0; idx < txb_w; ++idx)
+        rd_stats->txb_coeff_cost_map[plane][blk_row + idy][blk_col + idx] = 0;
+
+    rd_stats->txb_coeff_cost_map[plane][blk_row][blk_col] = txb_coeff_cost;
+
+    assert(blk_row < 16);
+    assert(blk_col < 16);
+  }
 #endif
 }
 
@@ -3595,7 +3607,7 @@
   mbmi->tx_size = best_tx;
 #if CONFIG_RD_DEBUG
   // record plane y's transform block coefficient cost
-  mbmi->txb_coeff_cost[0] = rd_stats->txb_coeff_cost[0];
+  mbmi->rd_stats = *rd_stats;
 #endif
   memcpy(x->blk_skip[0], best_blk_skip, sizeof(best_blk_skip[0]) * n4);
 }
@@ -7084,11 +7096,14 @@
 
 #if CONFIG_REF_MV
 #if CONFIG_EXT_INTER
-  if (this_mode == NEAREST_NEARESTMV) {
+  if (this_mode == NEAREST_NEARESTMV)
 #else
-  if (this_mode == NEARESTMV && is_comp_pred) {
-    uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
+  if (this_mode == NEARESTMV && is_comp_pred)
 #endif  // CONFIG_EXT_INTER
+  {
+#if !CONFIG_EXT_INTER
+    uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
+#endif
     if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) {
       cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv;
       cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv;
@@ -7826,8 +7841,7 @@
           inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, ref_best_rd - rdcosty);
 #if CONFIG_RD_DEBUG
       // record uv planes' transform block coefficient cost
-      mbmi->txb_coeff_cost[1] = rd_stats_uv.txb_coeff_cost[1];
-      mbmi->txb_coeff_cost[2] = rd_stats_uv.txb_coeff_cost[2];
+      if (is_cost_valid_uv) av1_merge_rd_stats(&mbmi->rd_stats, &rd_stats_uv);
 #endif
       *rate_uv = rd_stats_uv.rate;
       distortion_uv = rd_stats_uv.dist;
diff --git a/av1/encoder/rdopt.h b/av1/encoder/rdopt.h
index cb9666a..678c0db 100644
--- a/av1/encoder/rdopt.h
+++ b/av1/encoder/rdopt.h
@@ -27,17 +27,6 @@
 struct RD_COST;
 
 #if CONFIG_VAR_TX
-// TODO(angiebird): Merge RD_COST and RD_STATS
-typedef struct RD_STATS {
-  int rate;
-  int64_t dist;
-  int64_t sse;
-  int skip;
-#if CONFIG_RD_DEBUG
-  int txb_coeff_cost[MAX_MB_PLANE];
-#endif
-} RD_STATS;
-
 static INLINE void av1_init_rd_stats(RD_STATS *rd_stats) {
 #if CONFIG_RD_DEBUG
   int plane;
@@ -47,8 +36,13 @@
   rd_stats->sse = 0;
   rd_stats->skip = 1;
 #if CONFIG_RD_DEBUG
-  for (plane = 0; plane < MAX_MB_PLANE; ++plane)
+  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
+    int r, c;
     rd_stats->txb_coeff_cost[plane] = 0;
+    for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r)
+      for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c)
+        rd_stats->txb_coeff_cost_map[plane][r][c] = 0;
+  }
 #endif
 }
 
@@ -61,8 +55,13 @@
   rd_stats->sse = INT64_MAX;
   rd_stats->skip = 0;
 #if CONFIG_RD_DEBUG
-  for (plane = 0; plane < MAX_MB_PLANE; ++plane)
+  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
+    int r, c;
     rd_stats->txb_coeff_cost[plane] = INT_MAX;
+    for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r)
+      for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c)
+        rd_stats->txb_coeff_cost_map[plane][r][c] = INT_MAX;
+  }
 #endif
 }
 
@@ -76,8 +75,19 @@
   rd_stats_dst->sse += rd_stats_src->sse;
   rd_stats_dst->skip &= rd_stats_src->skip;
 #if CONFIG_RD_DEBUG
-  for (plane = 0; plane < MAX_MB_PLANE; ++plane)
+  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
+    int r, c;
+    int ref_txb_coeff_cost = 0;
     rd_stats_dst->txb_coeff_cost[plane] += rd_stats_src->txb_coeff_cost[plane];
+    // TODO(angiebird): optimize this part
+    for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r)
+      for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
+        rd_stats_dst->txb_coeff_cost_map[plane][r][c] +=
+            rd_stats_src->txb_coeff_cost_map[plane][r][c];
+        ref_txb_coeff_cost += rd_stats_dst->txb_coeff_cost_map[plane][r][c];
+      }
+    assert(ref_txb_coeff_cost == rd_stats_dst->txb_coeff_cost[plane]);
+  }
 #endif
 }
 #endif