Refactor aom_quantize_b

Use one helper function to remove copy paste of
aom_quantize_b_c, aom_quantize_b_32x32_c, aom_quantize_b_64x64_c.
And remove redundant av1_quantize_b_c, av1_highbd_quantize_b_c.

BUG=aomedia:442

Change-Id: I3e7923a64a16af223c3e2a42dc27c0f28a9b134b
diff --git a/aom_dsp/quantize.c b/aom_dsp/quantize.c
index 1f8a4e5..3e541a3 100644
--- a/aom_dsp/quantize.c
+++ b/aom_dsp/quantize.c
@@ -195,67 +195,6 @@
 #endif  // CONFIG_TX64X64
 #endif
 
-void aom_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
-                      int skip_block, const int16_t *zbin_ptr,
-                      const int16_t *round_ptr, const int16_t *quant_ptr,
-                      const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
-                      tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
-                      uint16_t *eob_ptr, const int16_t *scan,
-                      const int16_t *iscan, const qm_val_t *qm_ptr,
-                      const qm_val_t *iqm_ptr) {
-  int i, non_zero_count = (int)n_coeffs, eob = -1;
-  const int zbins[2] = { zbin_ptr[0], zbin_ptr[1] };
-  const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
-  (void)iscan;
-
-  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
-  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
-
-  if (!skip_block) {
-    // Pre-scan pass
-    for (i = (int)n_coeffs - 1; i >= 0; i--) {
-      const int rc = scan[i];
-      const qm_val_t wt = qm_ptr[rc];
-      const int coeff = coeff_ptr[rc] * wt;
-
-      if (coeff < (zbins[rc != 0] << AOM_QM_BITS) &&
-          coeff > (nzbins[rc != 0] << AOM_QM_BITS))
-        non_zero_count--;
-      else
-        break;
-    }
-
-    // Quantization pass: All coefficients with index >= zero_flag are
-    // skippable. Note: zero_flag can be zero.
-    for (i = 0; i < non_zero_count; i++) {
-      const int rc = scan[i];
-      const qm_val_t wt = qm_ptr[rc];
-      const int coeff = coeff_ptr[rc];
-      const int coeff_sign = (coeff >> 31);
-      const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
-      int dequant;
-
-      if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) {
-        int32_t tmp32;
-        int64_t tmp =
-            clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
-        tmp = tmp * wt;
-        tmp32 = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
-                 quant_shift_ptr[rc != 0]) >>
-                (16 + AOM_QM_BITS);  // quantization
-        dequant =
-            (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >>
-            AOM_QM_BITS;
-        qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
-        dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant;
-
-        if (tmp32) eob = i;
-      }
-    }
-  }
-  *eob_ptr = eob + 1;
-}
-
 #if CONFIG_HIGHBITDEPTH
 void aom_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                              int skip_block, const int16_t *zbin_ptr,
@@ -316,136 +255,6 @@
 }
 #endif
 
-void aom_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
-                            int skip_block, const int16_t *zbin_ptr,
-                            const int16_t *round_ptr, const int16_t *quant_ptr,
-                            const int16_t *quant_shift_ptr,
-                            tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
-                            const int16_t *dequant_ptr, uint16_t *eob_ptr,
-                            const int16_t *scan, const int16_t *iscan,
-                            const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) {
-  const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 1),
-                         ROUND_POWER_OF_TWO(zbin_ptr[1], 1) };
-  const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
-
-  int idx = 0;
-  int idx_arr[1024];
-  int i, eob = -1;
-  int dequant;
-  (void)iscan;
-
-  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
-  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
-
-  if (!skip_block) {
-    // Pre-scan pass
-    for (i = 0; i < n_coeffs; i++) {
-      const int rc = scan[i];
-      const qm_val_t wt = qm_ptr[rc];
-      const int coeff = coeff_ptr[rc] * wt;
-
-      // If the coefficient is out of the base ZBIN range, keep it for
-      // quantization.
-      if (coeff >= (zbins[rc != 0] << AOM_QM_BITS) ||
-          coeff <= (nzbins[rc != 0] << AOM_QM_BITS))
-        idx_arr[idx++] = i;
-    }
-
-    // Quantization pass: only process the coefficients selected in
-    // pre-scan pass. Note: idx can be zero.
-    for (i = 0; i < idx; i++) {
-      const int rc = scan[idx_arr[i]];
-      const int coeff = coeff_ptr[rc];
-      const int coeff_sign = (coeff >> 31);
-      const qm_val_t wt = qm_ptr[rc];
-      int64_t tmp;
-      int tmp32;
-      int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
-      abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
-      tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
-      tmp = tmp * wt;
-      tmp32 = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
-               quant_shift_ptr[rc != 0]) >>
-              (15 + AOM_QM_BITS);
-
-      qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
-      dequant =
-          (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >>
-          AOM_QM_BITS;
-      dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 2;
-
-      if (tmp32) eob = idx_arr[i];
-    }
-  }
-  *eob_ptr = eob + 1;
-}
-
-#if CONFIG_TX64X64
-void aom_quantize_b_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
-                            int skip_block, const int16_t *zbin_ptr,
-                            const int16_t *round_ptr, const int16_t *quant_ptr,
-                            const int16_t *quant_shift_ptr,
-                            tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
-                            const int16_t *dequant_ptr, uint16_t *eob_ptr,
-                            const int16_t *scan, const int16_t *iscan,
-                            const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) {
-  const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 2),
-                         ROUND_POWER_OF_TWO(zbin_ptr[1], 2) };
-  const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
-
-  int idx = 0;
-  int idx_arr[4096];
-  int i, eob = -1;
-  int dequant;
-  (void)iscan;
-
-  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
-  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
-
-  if (!skip_block) {
-    // Pre-scan pass
-    for (i = 0; i < n_coeffs; i++) {
-      const int rc = scan[i];
-      const qm_val_t wt = qm_ptr[rc];
-      const int coeff = coeff_ptr[rc] * wt;
-
-      // If the coefficient is out of the base ZBIN range, keep it for
-      // quantization.
-      if (coeff >= (zbins[rc != 0] << AOM_QM_BITS) ||
-          coeff <= (nzbins[rc != 0] << AOM_QM_BITS))
-        idx_arr[idx++] = i;
-    }
-
-    // Quantization pass: only process the coefficients selected in
-    // pre-scan pass. Note: idx can be zero.
-    for (i = 0; i < idx; i++) {
-      const int rc = scan[idx_arr[i]];
-      const int coeff = coeff_ptr[rc];
-      const int coeff_sign = (coeff >> 31);
-      const qm_val_t wt = qm_ptr[rc];
-      int64_t tmp;
-      int tmp32;
-      int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
-      abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2);
-      tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
-      tmp = tmp * wt;
-      tmp32 = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
-               quant_shift_ptr[rc != 0]) >>
-              (14 + AOM_QM_BITS);
-
-      qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
-      dequant =
-          (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >>
-          AOM_QM_BITS;
-      dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 4;
-
-      if (tmp32) eob = idx_arr[i];
-    }
-  }
-  *eob_ptr = eob + 1;
-}
-#endif  // CONFIG_TX64X64
-
 #if CONFIG_HIGHBITDEPTH
 void aom_highbd_quantize_b_32x32_c(
     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
@@ -724,16 +533,22 @@
 #endif  // CONFIG_TX64X64
 #endif
 
-void aom_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
-                      int skip_block, const int16_t *zbin_ptr,
-                      const int16_t *round_ptr, const int16_t *quant_ptr,
-                      const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
-                      tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
-                      uint16_t *eob_ptr, const int16_t *scan,
-                      const int16_t *iscan) {
-  int i, non_zero_count = (int)n_coeffs, eob = -1;
-  const int zbins[2] = { zbin_ptr[0], zbin_ptr[1] };
+static void quantize_b_helper_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
+                                int skip_block, const int16_t *zbin_ptr,
+                                const int16_t *round_ptr,
+                                const int16_t *quant_ptr,
+                                const int16_t *quant_shift_ptr,
+                                tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
+                                const int16_t *dequant_ptr, uint16_t *eob_ptr,
+                                const int16_t *scan, const int16_t *iscan,
+#if CONFIG_AOM_QM
+                                const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
+#endif
+                                const int log_scale) {
+  const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale),
+                         ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale) };
   const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
+  int i, non_zero_count = (int)n_coeffs, eob = -1;
   (void)iscan;
 
   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
@@ -743,10 +558,20 @@
     // Pre-scan pass
     for (i = (int)n_coeffs - 1; i >= 0; i--) {
       const int rc = scan[i];
+#if CONFIG_AOM_QM
+      const qm_val_t wt = qm_ptr[rc];
+      const int coeff = coeff_ptr[rc] * wt;
+#else
       const int coeff = coeff_ptr[rc];
+#endif  // CONFIG_AOM_QM
 
-      if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
+#if CONFIG_AOM_QM
+      if (coeff < (zbins[rc != 0] << AOM_QM_BITS) &&
+          coeff > (nzbins[rc != 0] << AOM_QM_BITS))
         non_zero_count--;
+#else
+      if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0]) non_zero_count--;
+#endif  // CONFIG_AOM_QM
       else
         break;
     }
@@ -758,22 +583,67 @@
       const int coeff = coeff_ptr[rc];
       const int coeff_sign = (coeff >> 31);
       const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
+      int tmp32;
 
+#if CONFIG_AOM_QM
+      const qm_val_t wt = qm_ptr[rc];
+      if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) {
+#else
       if (abs_coeff >= zbins[rc != 0]) {
-        int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
-        tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
-               quant_shift_ptr[rc != 0]) >>
-              16;  // quantization
+#endif  // CONFIG_AOM_QM
+        int64_t tmp =
+            clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale),
+                  INT16_MIN, INT16_MAX);
+#if CONFIG_AOM_QM
+        const qm_val_t wt = qm_ptr[rc];
+        tmp *= wt;
+        tmp32 = (int)(((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
+                       quant_shift_ptr[rc != 0]) >>
+                      (16 - log_scale + AOM_QM_BITS));  // quantization
+#else
+        tmp32 = (int)(((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
+                       quant_shift_ptr[rc != 0]) >>
+                      (16 - log_scale));  // quantization
+#endif  // CONFIG_AOM_QM
         qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
-        dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
+#if CONFIG_AOM_QM
+        dequant =
+            (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >>
+            AOM_QM_BITS;
+        dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant / (1 << log_scale);
+#else
+        dqcoeff_ptr[rc] =
+            qcoeff_ptr[rc] * dequant_ptr[rc != 0] / (1 << log_scale);
+#endif  // CONFIG_AOM_QM
 
-        if (tmp) eob = i;
+        if (tmp32) eob = i;
       }
     }
   }
   *eob_ptr = eob + 1;
 }
 
+void aom_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
+                      int skip_block, const int16_t *zbin_ptr,
+                      const int16_t *round_ptr, const int16_t *quant_ptr,
+                      const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
+                      tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
+                      uint16_t *eob_ptr, const int16_t *scan,
+                      const int16_t *iscan
+#if CONFIG_AOM_QM
+                      ,
+                      const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
+#endif
+                      ) {
+  quantize_b_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
+                      quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
+                      dequant_ptr, eob_ptr, scan, iscan,
+#if CONFIG_AOM_QM
+                      qm_ptr, iqm_ptr,
+#endif
+                      0);
+}
+
 #if CONFIG_HIGHBITDEPTH
 void aom_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                              int skip_block, const int16_t *zbin_ptr,
@@ -831,52 +701,19 @@
                             const int16_t *quant_shift_ptr,
                             tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
                             const int16_t *dequant_ptr, uint16_t *eob_ptr,
-                            const int16_t *scan, const int16_t *iscan) {
-  const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 1),
-                         ROUND_POWER_OF_TWO(zbin_ptr[1], 1) };
-  const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
-
-  int idx = 0;
-  int idx_arr[1024];
-  int i, eob = -1;
-  (void)iscan;
-
-  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
-  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
-
-  if (!skip_block) {
-    // Pre-scan pass
-    for (i = 0; i < n_coeffs; i++) {
-      const int rc = scan[i];
-      const int coeff = coeff_ptr[rc];
-
-      // If the coefficient is out of the base ZBIN range, keep it for
-      // quantization.
-      if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
-        idx_arr[idx++] = i;
-    }
-
-    // Quantization pass: only process the coefficients selected in
-    // pre-scan pass. Note: idx can be zero.
-    for (i = 0; i < idx; i++) {
-      const int rc = scan[idx_arr[i]];
-      const int coeff = coeff_ptr[rc];
-      const int coeff_sign = (coeff >> 31);
-      int tmp;
-      int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
-      abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
-      abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
-      tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) *
-             quant_shift_ptr[rc != 0]) >>
-            15;
-
-      qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
-      dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
-
-      if (tmp) eob = idx_arr[i];
-    }
-  }
-  *eob_ptr = eob + 1;
+                            const int16_t *scan, const int16_t *iscan
+#if CONFIG_AOM_QM
+                            ,
+                            const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
+#endif
+                            ) {
+  quantize_b_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
+                      quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
+                      dequant_ptr, eob_ptr, scan, iscan,
+#if CONFIG_AOM_QM
+                      qm_ptr, iqm_ptr,
+#endif
+                      1);
 }
 
 #if CONFIG_TX64X64
@@ -886,52 +723,19 @@
                             const int16_t *quant_shift_ptr,
                             tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
                             const int16_t *dequant_ptr, uint16_t *eob_ptr,
-                            const int16_t *scan, const int16_t *iscan) {
-  const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 2),
-                         ROUND_POWER_OF_TWO(zbin_ptr[1], 2) };
-  const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
-
-  int idx = 0;
-  int idx_arr[4096];
-  int i, eob = -1;
-  (void)iscan;
-
-  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
-  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
-
-  if (!skip_block) {
-    // Pre-scan pass
-    for (i = 0; i < n_coeffs; i++) {
-      const int rc = scan[i];
-      const int coeff = coeff_ptr[rc];
-
-      // If the coefficient is out of the base ZBIN range, keep it for
-      // quantization.
-      if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
-        idx_arr[idx++] = i;
-    }
-
-    // Quantization pass: only process the coefficients selected in
-    // pre-scan pass. Note: idx can be zero.
-    for (i = 0; i < idx; i++) {
-      const int rc = scan[idx_arr[i]];
-      const int coeff = coeff_ptr[rc];
-      const int coeff_sign = (coeff >> 31);
-      int tmp;
-      int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
-      abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2);
-      abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
-      tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) *
-             quant_shift_ptr[rc != 0]) >>
-            14;
-
-      qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
-      dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 4;
-
-      if (tmp) eob = idx_arr[i];
-    }
-  }
-  *eob_ptr = eob + 1;
+                            const int16_t *scan, const int16_t *iscan
+#if CONFIG_AOM_QM
+                            ,
+                            const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
+#endif
+                            ) {
+  quantize_b_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
+                      quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
+                      dequant_ptr, eob_ptr, scan, iscan,
+#if CONFIG_AOM_QM
+                      qm_ptr, iqm_ptr,
+#endif
+                      2);
 }
 #endif  // CONFIG_TX64X64
 
diff --git a/av1/encoder/av1_quantize.c b/av1/encoder/av1_quantize.c
index 64d9055..f4bdde9 100644
--- a/av1/encoder/av1_quantize.c
+++ b/av1/encoder/av1_quantize.c
@@ -897,13 +897,43 @@
   const qm_val_t *iqm_ptr = qparam->iqmatrix;
 #endif  // CONFIG_AOM_QM
 
-  av1_highbd_quantize_b(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round,
-                        p->quant, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
-                        pd->dequant, eob_ptr, sc->scan, sc->iscan,
+  switch (qparam->log_scale) {
+    case 0:
+      aom_highbd_quantize_b(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round,
+                            p->quant, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
+                            pd->dequant, eob_ptr, sc->scan, sc->iscan
 #if CONFIG_AOM_QM
-                        qm_ptr, iqm_ptr,
+                            ,
+                            qm_ptr, iqm_ptr
 #endif
-                        qparam->log_scale);
+                            );
+      break;
+    case 1:
+      aom_highbd_quantize_b_32x32(coeff_ptr, n_coeffs, skip_block, p->zbin,
+                                  p->round, p->quant, p->quant_shift,
+                                  qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
+                                  sc->scan, sc->iscan
+#if CONFIG_AOM_QM
+                                  ,
+                                  qm_ptr, iqm_ptr
+#endif
+                                  );
+      break;
+#if CONFIG_TX64X64
+    case 2:
+      aom_highbd_quantize_b_64x64(coeff_ptr, n_coeffs, skip_block, p->zbin,
+                                  p->round, p->quant, p->quant_shift,
+                                  qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
+                                  sc->scan, sc->iscan
+#if CONFIG_AOM_QM
+                                  ,
+                                  qm_ptr, iqm_ptr
+#endif
+                                  );
+      break;
+#endif  // CONFIG_TX64X64
+    default: assert(0);
+  }
 }
 
 #if CONFIG_HIGHBITDEPTH
@@ -1537,113 +1567,6 @@
 
 #endif  // CONFIG_HIGHBITDEPTH
 
-void av1_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
-                      int skip_block, const int16_t *zbin_ptr,
-                      const int16_t *round_ptr, const int16_t *quant_ptr,
-                      const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
-                      tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
-                      uint16_t *eob_ptr, const int16_t *scan,
-                      const int16_t *iscan,
-#if CONFIG_AOM_QM
-                      const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
-#endif
-                      int log_scale) {
-  int i, non_zero_count = (int)n_coeffs, eob = -1;
-  int zbins[2] = { zbin_ptr[0], zbin_ptr[1] };
-  int round[2] = { round_ptr[0], round_ptr[1] };
-  int nzbins[2];
-  int scale = 1;
-  int shift = 16;
-  (void)iscan;
-
-  if (log_scale > 0) {
-    zbins[0] = ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale);
-    zbins[1] = ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale);
-    round[0] = ROUND_POWER_OF_TWO(round_ptr[0], log_scale);
-    round[1] = ROUND_POWER_OF_TWO(round_ptr[1], log_scale);
-    scale = 1 << log_scale;
-    shift = 16 - log_scale;
-  }
-
-  nzbins[0] = zbins[0] * -1;
-  nzbins[1] = zbins[1] * -1;
-
-  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
-  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
-
-  if (!skip_block) {
-    // Pre-scan pass
-    for (i = (int)n_coeffs - 1; i >= 0; i--) {
-      const int rc = scan[i];
-      const int coeff = coeff_ptr[rc];
-      if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
-        non_zero_count--;
-      else
-        break;
-    }
-
-    // Quantization pass: All coefficients with index >= zero_flag are
-    // skippable. Note: zero_flag can be zero.
-    for (i = 0; i < non_zero_count; i++) {
-      const int rc = scan[i];
-#if CONFIG_AOM_QM
-      const qm_val_t wt = qm_ptr[rc];
-      const qm_val_t iwt = iqm_ptr[rc];
-      const int dequant =
-          (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
-          AOM_QM_BITS;
-#endif
-      const int coeff = coeff_ptr[rc];
-      const int coeff_sign = (coeff >> 31);
-      const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
-#if CONFIG_AOM_QM
-      if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) {
-#else
-
-      if (abs_coeff >= zbins[rc != 0]) {
-#endif
-        const int64_t tmp1 = abs_coeff + round[rc != 0];
-        const int64_t tmp2 = ((tmp1 * quant_ptr[rc != 0]) >> 16) + tmp1;
-#if CONFIG_AOM_QM
-        const uint32_t abs_qcoeff = (uint32_t)(
-            (tmp2 * wt * quant_shift_ptr[rc != 0]) >> (AOM_QM_BITS + shift));
-        qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
-        dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / scale;
-#else
-        const uint32_t abs_qcoeff =
-            (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> shift);
-        qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
-        dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / scale;
-#endif  // CONFIG_AOM_QM
-        if (abs_qcoeff) eob = i;
-      }
-    }
-  }
-  *eob_ptr = eob + 1;
-}
-
-#if CONFIG_HIGHBITDEPTH
-void av1_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
-                             int skip_block, const int16_t *zbin_ptr,
-                             const int16_t *round_ptr, const int16_t *quant_ptr,
-                             const int16_t *quant_shift_ptr,
-                             tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
-                             const int16_t *dequant_ptr, uint16_t *eob_ptr,
-                             const int16_t *scan, const int16_t *iscan,
-#if CONFIG_AOM_QM
-                             const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
-#endif  // CONFIG_AOM_QM
-                             int log_scale) {
-  av1_quantize_b_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
-                   quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
-                   dequant_ptr, eob_ptr, scan, iscan,
-#if CONFIG_AOM_QM
-                   qm_ptr, iqm_ptr,
-#endif  // CONFIG_AOM_QM
-                   log_scale);
-}
-#endif  // CONFIG_HIGHBITDEPTH
-
 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
   uint32_t t;
   int l, m;