Improve the quantization table interface

- Add av1_build_quantize() function so that quantization
  related unit test would be able to access quant/dequant
  table without starting an encoder instance and directly
  accessing cpi.

Change-Id: I8ba429e5deb7a4e7f967996aaec1d20bff0feb3c
diff --git a/av1/encoder/av1_quantize.c b/av1/encoder/av1_quantize.c
index 6cffac2..63727df 100644
--- a/av1/encoder/av1_quantize.c
+++ b/av1/encoder/av1_quantize.c
@@ -1594,50 +1594,48 @@
 #endif
 }
 
-void av1_init_quantizer(AV1_COMP *cpi) {
-  AV1_COMMON *const cm = &cpi->common;
-  QUANTS *const quants = &cpi->quants;
+void av1_build_quantizer(aom_bit_depth_t bit_depth, int y_dc_delta_q,
+                         int uv_dc_delta_q, int uv_ac_delta_q,
+                         QUANTS *const quants, Dequants *const deq) {
   int i, q, quant;
-#if CONFIG_NEW_QUANT
-  int dq;
-#endif
 
   for (q = 0; q < QINDEX_RANGE; q++) {
-    const int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
+    const int qzbin_factor = get_qzbin_factor(q, bit_depth);
     const int qrounding_factor = q == 0 ? 64 : 48;
 
     for (i = 0; i < 2; ++i) {
       int qrounding_factor_fp = 64;
       // y
-      quant = i == 0 ? av1_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
-                     : av1_ac_quant(q, 0, cm->bit_depth);
+      quant = i == 0 ? av1_dc_quant(q, y_dc_delta_q, bit_depth)
+                     : av1_ac_quant(q, 0, bit_depth);
       invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
       quants->y_quant_fp[q][i] = (1 << 16) / quant;
       quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
       quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
       quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
-      cpi->y_dequant[q][i] = quant;
+      deq->y_dequant[q][i] = quant;
 
       // uv
-      quant = i == 0 ? av1_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
-                     : av1_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
+      quant = i == 0 ? av1_dc_quant(q, uv_dc_delta_q, bit_depth)
+                     : av1_ac_quant(q, uv_ac_delta_q, bit_depth);
       invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
                    quant);
       quants->uv_quant_fp[q][i] = (1 << 16) / quant;
       quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
       quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
       quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
-      cpi->uv_dequant[q][i] = quant;
+      deq->uv_dequant[q][i] = quant;
     }
 
 #if CONFIG_NEW_QUANT
+    int dq;
     for (dq = 0; dq < QUANT_PROFILES; dq++) {
       for (i = 0; i < COEF_BANDS; i++) {
-        const int y_quant = cpi->y_dequant[q][i != 0];
-        const int uvquant = cpi->uv_dequant[q][i != 0];
-        av1_get_dequant_val_nuq(y_quant, i, cpi->y_dequant_val_nuq[dq][q][i],
+        const int y_quant = deq->y_dequant[q][i != 0];
+        const int uvquant = deq->uv_dequant[q][i != 0];
+        av1_get_dequant_val_nuq(y_quant, i, deq->y_dequant_val_nuq[dq][q][i],
                                 quants->y_cuml_bins_nuq[dq][q][i], dq);
-        av1_get_dequant_val_nuq(uvquant, i, cpi->uv_dequant_val_nuq[dq][q][i],
+        av1_get_dequant_val_nuq(uvquant, i, deq->uv_dequant_val_nuq[dq][q][i],
                                 quants->uv_cuml_bins_nuq[dq][q][i], dq);
       }
     }
@@ -1650,7 +1648,7 @@
       quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
       quants->y_zbin[q][i] = quants->y_zbin[q][1];
       quants->y_round[q][i] = quants->y_round[q][1];
-      cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
+      deq->y_dequant[q][i] = deq->y_dequant[q][1];
 
       quants->uv_quant[q][i] = quants->uv_quant[q][1];
       quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
@@ -1658,11 +1656,19 @@
       quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
       quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
       quants->uv_round[q][i] = quants->uv_round[q][1];
-      cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
+      deq->uv_dequant[q][i] = deq->uv_dequant[q][1];
     }
   }
 }
 
+void av1_init_quantizer(AV1_COMP *cpi) {
+  AV1_COMMON *const cm = &cpi->common;
+  QUANTS *const quants = &cpi->quants;
+  Dequants *const dequants = &cpi->dequants;
+  av1_build_quantizer(cm->bit_depth, cm->y_dc_delta_q, cm->uv_dc_delta_q,
+                      cm->uv_ac_delta_q, quants, dequants);
+}
+
 void av1_init_plane_quantizers(const AV1_COMP *cpi, MACROBLOCK *x,
                                int segment_id) {
   const AV1_COMMON *const cm = &cpi->common;
@@ -1712,11 +1718,12 @@
   memcpy(&xd->plane[0].seg_iqmatrix[segment_id], cm->giqmatrix[qmlevel][0],
          sizeof(cm->giqmatrix[qmlevel][0]));
 #endif
-  xd->plane[0].dequant = cpi->y_dequant[qindex];
+  xd->plane[0].dequant = cpi->dequants.y_dequant[qindex];
 #if CONFIG_NEW_QUANT
   for (dq = 0; dq < QUANT_PROFILES; dq++) {
     x->plane[0].cuml_bins_nuq[dq] = quants->y_cuml_bins_nuq[dq][qindex];
-    xd->plane[0].dequant_val_nuq[dq] = cpi->y_dequant_val_nuq[dq][qindex];
+    xd->plane[0].dequant_val_nuq[dq] =
+        cpi->dequants.y_dequant_val_nuq[dq][qindex];
   }
 #endif  // CONFIG_NEW_QUANT
 
@@ -1734,11 +1741,12 @@
     memcpy(&xd->plane[i].seg_iqmatrix[segment_id], cm->giqmatrix[qmlevel][1],
            sizeof(cm->giqmatrix[qmlevel][1]));
 #endif
-    xd->plane[i].dequant = cpi->uv_dequant[qindex];
+    xd->plane[i].dequant = cpi->dequants.uv_dequant[qindex];
 #if CONFIG_NEW_QUANT
     for (dq = 0; dq < QUANT_PROFILES; dq++) {
       x->plane[i].cuml_bins_nuq[dq] = quants->uv_cuml_bins_nuq[dq][qindex];
-      xd->plane[i].dequant_val_nuq[dq] = cpi->uv_dequant_val_nuq[dq][qindex];
+      xd->plane[i].dequant_val_nuq[dq] =
+          cpi->dequants.uv_dequant_val_nuq[dq][qindex];
     }
 #endif  // CONFIG_NEW_QUANT
   }
diff --git a/av1/encoder/av1_quantize.h b/av1/encoder/av1_quantize.h
index c87b6b7..4bc9ccc 100644
--- a/av1/encoder/av1_quantize.h
+++ b/av1/encoder/av1_quantize.h
@@ -69,6 +69,17 @@
   DECLARE_ALIGNED(16, int16_t, uv_round[QINDEX_RANGE][8]);
 } QUANTS;
 
+typedef struct {
+  DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);   // 8: SIMD width
+  DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);  // 8: SIMD width
+#if CONFIG_NEW_QUANT
+  DECLARE_ALIGNED(16, dequant_val_type_nuq,
+                  y_dequant_val_nuq[QUANT_PROFILES][QINDEX_RANGE][COEF_BANDS]);
+  DECLARE_ALIGNED(16, dequant_val_type_nuq,
+                  uv_dequant_val_nuq[QUANT_PROFILES][QINDEX_RANGE][COEF_BANDS]);
+#endif  // CONFIG_NEW_QUANT
+} Dequants;
+
 struct AV1_COMP;
 struct AV1Common;
 
@@ -77,6 +88,10 @@
 void av1_init_plane_quantizers(const struct AV1_COMP *cpi, MACROBLOCK *x,
                                int segment_id);
 
+void av1_build_quantizer(aom_bit_depth_t bit_depth, int y_dc_delta_q,
+                         int uv_dc_delta_q, int uv_ac_delta_q,
+                         QUANTS *const quants, Dequants *const deq);
+
 void av1_init_quantizer(struct AV1_COMP *cpi);
 
 void av1_set_quantizer(struct AV1Common *cm, int q);
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 485c977..a5c45af 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -563,7 +563,7 @@
   const int is_key_frame = (cm->frame_type == KEY_FRAME);
   const int threshold_multiplier = is_key_frame ? 20 : 1;
   const int64_t threshold_base =
-      (int64_t)(threshold_multiplier * cpi->y_dequant[q][1]);
+      (int64_t)(threshold_multiplier * cpi->dequants.y_dequant[q][1]);
   if (is_key_frame) {
     thresholds[1] = threshold_base;
     thresholds[2] = threshold_base >> 2;
@@ -602,8 +602,8 @@
       if (cm->width <= 352 && cm->height <= 288)
         cpi->vbp_threshold_sad = 100;
       else
-        cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000
-                                     ? (cpi->y_dequant[q][1] << 1)
+        cpi->vbp_threshold_sad = (cpi->dequants.y_dequant[q][1] << 1) > 1000
+                                     ? (cpi->dequants.y_dequant[q][1] << 1)
                                      : 1000;
       cpi->vbp_bsize_min = BLOCK_16X16;
     }
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 017bd3b..2ef0871 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -366,14 +366,7 @@
   QUANTS quants;
   ThreadData td;
   MB_MODE_INFO_EXT *mbmi_ext_base;
-  DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);   // 8: SIMD width
-  DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);  // 8: SIMD width
-#if CONFIG_NEW_QUANT
-  DECLARE_ALIGNED(16, dequant_val_type_nuq,
-                  y_dequant_val_nuq[QUANT_PROFILES][QINDEX_RANGE][COEF_BANDS]);
-  DECLARE_ALIGNED(16, dequant_val_type_nuq,
-                  uv_dequant_val_nuq[QUANT_PROFILES][QINDEX_RANGE][COEF_BANDS]);
-#endif  // CONFIG_NEW_QUANT
+  Dequants dequants;
   AV1_COMMON common;
   AV1EncoderConfig oxcf;
   struct lookahead_ctx *lookahead;