Fix a bug when combining new-quant + supertx

Previously, we assumed that av1_init_plane_quantizers is always called with
segment_id == xd->mi[0]->mbmi.segment_id (and use the latter to derive the value
of 'qindex' to use in the quantizer). But this is no longer true when supertx
is enabled. This patch instead remembers the value of 'qindex' derived from
the latest call to av1_init_plane_quantizers and uses that directly.

Change-Id: Ifa1c5bf74cad29942ff79b88ca92c231bc07f336
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 310325e..828340f 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -81,7 +81,7 @@
   MB_MODE_INFO_EXT *mbmi_ext;
   int skip_block;
   int select_tx_size;
-  int q_index;
+  int qindex;
 
   // The equivalent error at the current rdmult of one whole bit (not one
   // bitcost unit).
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 9dc6a2e..4534f82 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -852,7 +852,6 @@
 
     if (cyclic_refresh_segment_id_boosted(segment_id)) {
       int q = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
-      assert(q == xd->qindex[segment_id]);
       set_vbp_thresholds(cpi, thresholds, q);
     }
   }
@@ -1606,7 +1605,6 @@
   av1_init_plane_quantizers(cpi, x, segment_id);
   aom_clear_system_state();
   segment_qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
-  assert(segment_qindex == x->e_mbd.qindex[segment_id]);
   return av1_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
 }
 
@@ -4675,7 +4673,6 @@
                            : cm->base_qindex;
     xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
                       cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
-    xd->qindex[i] = qindex;
   }
 
   if (!cm->seg.enabled && xd->lossless[0]) x->optimize = 0;
diff --git a/av1/encoder/encodemb.c b/av1/encoder/encodemb.c
index 8914ba5..7b6b431 100644
--- a/av1/encoder/encodemb.c
+++ b/av1/encoder/encodemb.c
@@ -95,8 +95,7 @@
 #endif
   const int shift = get_tx_scale(xd, tx_type, tx_size);
 #if CONFIG_NEW_QUANT
-  int dq = get_dq_profile_from_ctx(xd->qindex[xd->mi[0]->mbmi.segment_id], ctx,
-                                   ref, plane_type);
+  int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
   const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
 #else
   const int dq_step[2] = { dequant_ptr[0] >> shift, dequant_ptr[1] >> shift };
@@ -123,8 +122,7 @@
   int shortcut = 0;
   int next_shortcut = 0;
 
-  assert((xd->qindex[xd->mi[0]->mbmi.segment_id] == 0) ^
-         (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
+  assert((mb->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
 
   token_costs += band;
 
@@ -518,8 +516,7 @@
   tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
   tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
   tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
-  int dq = get_dq_profile_from_ctx(xd->qindex[xd->mi[0]->mbmi.segment_id], ctx,
-                                   is_inter, plane_type);
+  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
   uint16_t *const eob = &p->eobs[block];
   const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
   const int16_t *src_diff;
@@ -527,8 +524,7 @@
 
   FWD_TXFM_PARAM fwd_txfm_param;
 
-  assert((xd->qindex[xd->mi[0]->mbmi.segment_id] == 0) ^
-         (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
+  assert((x->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
 
   fwd_txfm_param.tx_type = tx_type;
   fwd_txfm_param.tx_size = tx_size;
@@ -588,8 +584,7 @@
   PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
   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);
-  int dq = get_dq_profile_from_ctx(xd->qindex[xd->mi[0]->mbmi.segment_id], ctx,
-                                   is_inter, plane_type);
+  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
   tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
   tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
   tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
@@ -600,8 +595,7 @@
 
   FWD_TXFM_PARAM fwd_txfm_param;
 
-  assert((xd->qindex[xd->mi[0]->mbmi.segment_id] == 0) ^
-         (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
+  assert((x->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
 
   fwd_txfm_param.tx_type = tx_type;
   fwd_txfm_param.tx_size = tx_size;
@@ -665,13 +659,11 @@
   const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
   const int16_t *src_diff;
   const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
-  int dq = get_dq_profile_from_ctx(xd->qindex[xd->mi[0]->mbmi.segment_id], ctx,
-                                   is_inter, plane_type);
+  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
 
   FWD_TXFM_PARAM fwd_txfm_param;
 
-  assert((xd->qindex[xd->mi[0]->mbmi.segment_id] == 0) ^
-         (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
+  assert((x->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
 
   fwd_txfm_param.tx_type = tx_type;
   fwd_txfm_param.tx_size = tx_size;
@@ -730,13 +722,11 @@
   const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
   const int16_t *src_diff;
   const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
-  int dq = get_dq_profile_from_ctx(xd->qindex[xd->mi[0]->mbmi.segment_id], ctx,
-                                   is_inter, plane_type);
+  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
 
   FWD_TXFM_PARAM fwd_txfm_param;
 
-  assert((xd->qindex[xd->mi[0]->mbmi.segment_id] == 0) ^
-         (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
+  assert((x->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
 
   fwd_txfm_param.tx_type = tx_type;
   fwd_txfm_param.tx_size = tx_size;
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index 466cb9c..9239fc2 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -585,7 +585,6 @@
 #if CONFIG_SUPERTX
       xd->mi[0]->mbmi.segment_id_supertx = 0;
 #endif  // CONFIG_SUPERTX
-      xd->qindex[xd->mi[0]->mbmi.segment_id] = qindex;
       xd->lossless[xd->mi[0]->mbmi.segment_id] = (qindex == 0);
       xd->mi[0]->mbmi.mode = DC_PRED;
       xd->mi[0]->mbmi.tx_size =
diff --git a/av1/encoder/quantize.c b/av1/encoder/quantize.c
index db2fdb8..771f94b 100644
--- a/av1/encoder/quantize.c
+++ b/av1/encoder/quantize.c
@@ -1293,11 +1293,11 @@
   }
 
   x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
-  x->q_index = qindex;
+  x->qindex = qindex;
 
   set_error_per_bit(x, rdmult);
 
-  av1_initialize_me_consts(cpi, x, x->q_index);
+  av1_initialize_me_consts(cpi, x, qindex);
 }
 
 void av1_frame_init_quantizer(AV1_COMP *cpi) {