LCOV - code coverage report
Current view: top level - third_party/aom/av1/encoder - av1_quantize.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 220 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 21 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
       3             :  *
       4             :  * This source code is subject to the terms of the BSD 2 Clause License and
       5             :  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
       6             :  * was not distributed with this source code in the LICENSE file, you can
       7             :  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
       8             :  * Media Patent License 1.0 was not distributed with this source code in the
       9             :  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
      10             :  */
      11             : 
      12             : #include <math.h>
      13             : #include "./aom_dsp_rtcd.h"
      14             : #include "aom_dsp/quantize.h"
      15             : #include "aom_mem/aom_mem.h"
      16             : #include "aom_ports/mem.h"
      17             : 
      18             : #include "av1/common/idct.h"
      19             : #include "av1/common/quant_common.h"
      20             : #include "av1/common/scan.h"
      21             : #include "av1/common/seg_common.h"
      22             : 
      23             : #include "av1/encoder/av1_quantize.h"
      24             : #include "av1/encoder/encoder.h"
      25             : #include "av1/encoder/rd.h"
      26             : 
      27             : #if CONFIG_NEW_QUANT
      28             : static INLINE int quantize_coeff_nuq(
      29             :     const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
      30             :     const int16_t dequant, const tran_low_t *cuml_bins_ptr,
      31             :     const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
      32             :     tran_low_t *dqcoeff_ptr) {
      33             :   const int coeff = coeffv;
      34             :   const int coeff_sign = (coeff >> 31);
      35             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
      36             :   int i, q;
      37             :   int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
      38             :   for (i = 0; i < NUQ_KNOTS; i++) {
      39             :     if (tmp < cuml_bins_ptr[i]) {
      40             :       q = i;
      41             :       break;
      42             :     }
      43             :   }
      44             :   if (i == NUQ_KNOTS) {
      45             :     tmp -= cuml_bins_ptr[NUQ_KNOTS - 1];
      46             :     q = NUQ_KNOTS + (((((tmp * quant) >> 16) + tmp) * quant_shift) >> 16);
      47             :   }
      48             :   if (q) {
      49             :     *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
      50             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
      51             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
      52             :   } else {
      53             :     *qcoeff_ptr = 0;
      54             :     *dqcoeff_ptr = 0;
      55             :   }
      56             :   return (q != 0);
      57             : }
      58             : 
      59             : static INLINE int quantize_coeff_bigtx_nuq(
      60             :     const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
      61             :     const int16_t dequant, const tran_low_t *cuml_bins_ptr,
      62             :     const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
      63             :     tran_low_t *dqcoeff_ptr, int logsizeby16) {
      64             :   const int coeff = coeffv;
      65             :   const int coeff_sign = (coeff >> 31);
      66             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
      67             :   int i, q;
      68             :   int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
      69             :   for (i = 0; i < NUQ_KNOTS; i++) {
      70             :     if (tmp < ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
      71             :       q = i;
      72             :       break;
      73             :     }
      74             :   }
      75             :   if (i == NUQ_KNOTS) {
      76             :     tmp -= ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16);
      77             :     q = NUQ_KNOTS +
      78             :         (((((tmp * quant) >> 16) + tmp) * quant_shift) >> (16 - logsizeby16));
      79             :   }
      80             :   if (q) {
      81             :     *dqcoeff_ptr = ROUND_POWER_OF_TWO(
      82             :         av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
      83             :     // *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val) >>
      84             :     // (logsizeby16);
      85             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
      86             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
      87             :   } else {
      88             :     *qcoeff_ptr = 0;
      89             :     *dqcoeff_ptr = 0;
      90             :   }
      91             :   return (q != 0);
      92             : }
      93             : 
      94             : static INLINE int quantize_coeff_fp_nuq(
      95             :     const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
      96             :     const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
      97             :     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr) {
      98             :   const int coeff = coeffv;
      99             :   const int coeff_sign = (coeff >> 31);
     100             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
     101             :   int i, q;
     102             :   int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
     103             :   for (i = 0; i < NUQ_KNOTS; i++) {
     104             :     if (tmp < cuml_bins_ptr[i]) {
     105             :       q = i;
     106             :       break;
     107             :     }
     108             :   }
     109             :   if (i == NUQ_KNOTS) {
     110             :     q = NUQ_KNOTS +
     111             :         ((((int64_t)tmp - cuml_bins_ptr[NUQ_KNOTS - 1]) * quant) >> 16);
     112             :   }
     113             :   if (q) {
     114             :     *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
     115             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
     116             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
     117             :   } else {
     118             :     *qcoeff_ptr = 0;
     119             :     *dqcoeff_ptr = 0;
     120             :   }
     121             :   return (q != 0);
     122             : }
     123             : 
     124             : static INLINE int quantize_coeff_bigtx_fp_nuq(
     125             :     const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
     126             :     const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
     127             :     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, int logsizeby16) {
     128             :   const int coeff = coeffv;
     129             :   const int coeff_sign = (coeff >> 31);
     130             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
     131             :   int i, q;
     132             :   int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
     133             :   for (i = 0; i < NUQ_KNOTS; i++) {
     134             :     if (tmp < ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
     135             :       q = i;
     136             :       break;
     137             :     }
     138             :   }
     139             :   if (i == NUQ_KNOTS) {
     140             :     q = NUQ_KNOTS +
     141             :         ((((int64_t)tmp -
     142             :            ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16)) *
     143             :           quant) >>
     144             :          (16 - logsizeby16));
     145             :   }
     146             :   if (q) {
     147             :     *dqcoeff_ptr = ROUND_POWER_OF_TWO(
     148             :         av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
     149             :     // *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val) >>
     150             :     // (logsizeby16);
     151             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
     152             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
     153             :   } else {
     154             :     *qcoeff_ptr = 0;
     155             :     *dqcoeff_ptr = 0;
     156             :   }
     157             :   return (q != 0);
     158             : }
     159             : 
     160             : void quantize_dc_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     161             :                      int skip_block, const int16_t quant,
     162             :                      const int16_t quant_shift, const int16_t dequant,
     163             :                      const tran_low_t *cuml_bins_ptr,
     164             :                      const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
     165             :                      tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
     166             :   int eob = -1;
     167             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     168             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     169             :   if (!skip_block) {
     170             :     const int rc = 0;
     171             :     if (quantize_coeff_nuq(coeff_ptr[rc], quant, quant_shift, dequant,
     172             :                            cuml_bins_ptr, dequant_val, qcoeff_ptr, dqcoeff_ptr))
     173             :       eob = 0;
     174             :   }
     175             :   *eob_ptr = eob + 1;
     176             : }
     177             : 
     178             : void quantize_dc_fp_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     179             :                         int skip_block, const int16_t quant,
     180             :                         const int16_t dequant, const tran_low_t *cuml_bins_ptr,
     181             :                         const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
     182             :                         tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
     183             :   int eob = -1;
     184             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     185             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     186             :   if (!skip_block) {
     187             :     const int rc = 0;
     188             :     if (quantize_coeff_fp_nuq(coeff_ptr[rc], quant, dequant, cuml_bins_ptr,
     189             :                               dequant_val, qcoeff_ptr, dqcoeff_ptr))
     190             :       eob = 0;
     191             :   }
     192             :   *eob_ptr = eob + 1;
     193             : }
     194             : 
     195             : void quantize_dc_32x32_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     196             :                            int skip_block, const int16_t quant,
     197             :                            const int16_t quant_shift, const int16_t dequant,
     198             :                            const tran_low_t *cuml_bins_ptr,
     199             :                            const tran_low_t *dequant_val,
     200             :                            tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     201             :                            uint16_t *eob_ptr) {
     202             :   int eob = -1;
     203             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     204             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     205             :   if (!skip_block) {
     206             :     const int rc = 0;
     207             :     if (quantize_coeff_bigtx_nuq(coeff_ptr[rc], quant, quant_shift, dequant,
     208             :                                  cuml_bins_ptr, dequant_val, qcoeff_ptr,
     209             :                                  dqcoeff_ptr, av1_get_tx_scale(TX_32X32)))
     210             :       eob = 0;
     211             :   }
     212             :   *eob_ptr = eob + 1;
     213             : }
     214             : 
     215             : void quantize_dc_32x32_fp_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     216             :                               int skip_block, const int16_t quant,
     217             :                               const int16_t dequant,
     218             :                               const tran_low_t *cuml_bins_ptr,
     219             :                               const tran_low_t *dequant_val,
     220             :                               tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     221             :                               uint16_t *eob_ptr) {
     222             :   int eob = -1;
     223             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     224             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     225             :   if (!skip_block) {
     226             :     const int rc = 0;
     227             :     if (quantize_coeff_bigtx_fp_nuq(coeff_ptr[rc], quant, dequant,
     228             :                                     cuml_bins_ptr, dequant_val, qcoeff_ptr,
     229             :                                     dqcoeff_ptr, av1_get_tx_scale(TX_32X32)))
     230             :       eob = 0;
     231             :   }
     232             :   *eob_ptr = eob + 1;
     233             : }
     234             : 
     235             : #if CONFIG_TX64X64
     236             : void quantize_dc_64x64_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     237             :                            int skip_block, const int16_t quant,
     238             :                            const int16_t quant_shift, const int16_t dequant,
     239             :                            const tran_low_t *cuml_bins_ptr,
     240             :                            const tran_low_t *dequant_val,
     241             :                            tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     242             :                            uint16_t *eob_ptr) {
     243             :   int eob = -1;
     244             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     245             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     246             :   if (!skip_block) {
     247             :     const int rc = 0;
     248             :     if (quantize_coeff_bigtx_nuq(coeff_ptr[rc], quant, quant_shift, dequant,
     249             :                                  cuml_bins_ptr, dequant_val, qcoeff_ptr,
     250             :                                  dqcoeff_ptr, av1_get_tx_scale(TX_64X64)))
     251             :       eob = 0;
     252             :   }
     253             :   *eob_ptr = eob + 1;
     254             : }
     255             : 
     256             : void quantize_dc_64x64_fp_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     257             :                               int skip_block, const int16_t quant,
     258             :                               const int16_t dequant,
     259             :                               const tran_low_t *cuml_bins_ptr,
     260             :                               const tran_low_t *dequant_val,
     261             :                               tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     262             :                               uint16_t *eob_ptr) {
     263             :   int eob = -1;
     264             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     265             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     266             :   if (!skip_block) {
     267             :     const int rc = 0;
     268             :     if (quantize_coeff_bigtx_fp_nuq(coeff_ptr[rc], quant, dequant,
     269             :                                     cuml_bins_ptr, dequant_val, qcoeff_ptr,
     270             :                                     dqcoeff_ptr, av1_get_tx_scale(TX_64X64)))
     271             :       eob = 0;
     272             :   }
     273             :   *eob_ptr = eob + 1;
     274             : }
     275             : #endif  // CONFIG_TX64X64
     276             : 
     277             : void quantize_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     278             :                     int skip_block, const int16_t *quant_ptr,
     279             :                     const int16_t *quant_shift_ptr, const int16_t *dequant_ptr,
     280             :                     const cuml_bins_type_nuq *cuml_bins_ptr,
     281             :                     const dequant_val_type_nuq *dequant_val,
     282             :                     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     283             :                     uint16_t *eob_ptr, const int16_t *scan,
     284             :                     const uint8_t *band) {
     285             :   int eob = -1;
     286             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     287             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     288             :   if (!skip_block) {
     289             :     int i;
     290             :     for (i = 0; i < n_coeffs; i++) {
     291             :       const int rc = scan[i];
     292             :       if (quantize_coeff_nuq(coeff_ptr[rc], quant_ptr[rc != 0],
     293             :                              quant_shift_ptr[rc != 0], dequant_ptr[rc != 0],
     294             :                              cuml_bins_ptr[band[i]], dequant_val[band[i]],
     295             :                              &qcoeff_ptr[rc], &dqcoeff_ptr[rc]))
     296             :         eob = i;
     297             :     }
     298             :   }
     299             :   *eob_ptr = eob + 1;
     300             : }
     301             : 
     302             : void quantize_fp_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     303             :                        int skip_block, const int16_t *quant_ptr,
     304             :                        const int16_t *dequant_ptr,
     305             :                        const cuml_bins_type_nuq *cuml_bins_ptr,
     306             :                        const dequant_val_type_nuq *dequant_val,
     307             :                        tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     308             :                        uint16_t *eob_ptr, const int16_t *scan,
     309             :                        const uint8_t *band) {
     310             :   int eob = -1;
     311             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     312             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     313             :   if (!skip_block) {
     314             :     int i;
     315             :     for (i = 0; i < n_coeffs; i++) {
     316             :       const int rc = scan[i];
     317             :       if (quantize_coeff_fp_nuq(coeff_ptr[rc], quant_ptr[rc != 0],
     318             :                                 dequant_ptr[rc != 0], cuml_bins_ptr[band[i]],
     319             :                                 dequant_val[band[i]], &qcoeff_ptr[rc],
     320             :                                 &dqcoeff_ptr[rc]))
     321             :         eob = i;
     322             :     }
     323             :   }
     324             :   *eob_ptr = eob + 1;
     325             : }
     326             : 
     327             : void quantize_32x32_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     328             :                           int skip_block, const int16_t *quant_ptr,
     329             :                           const int16_t *quant_shift_ptr,
     330             :                           const int16_t *dequant_ptr,
     331             :                           const cuml_bins_type_nuq *cuml_bins_ptr,
     332             :                           const dequant_val_type_nuq *dequant_val,
     333             :                           tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     334             :                           uint16_t *eob_ptr, const int16_t *scan,
     335             :                           const uint8_t *band) {
     336             :   int eob = -1;
     337             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     338             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     339             :   if (!skip_block) {
     340             :     int i;
     341             :     for (i = 0; i < n_coeffs; i++) {
     342             :       const int rc = scan[i];
     343             :       if (quantize_coeff_bigtx_nuq(
     344             :               coeff_ptr[rc], quant_ptr[rc != 0], quant_shift_ptr[rc != 0],
     345             :               dequant_ptr[rc != 0], cuml_bins_ptr[band[i]],
     346             :               dequant_val[band[i]], &qcoeff_ptr[rc], &dqcoeff_ptr[rc],
     347             :               av1_get_tx_scale(TX_32X32)))
     348             :         eob = i;
     349             :     }
     350             :   }
     351             :   *eob_ptr = eob + 1;
     352             : }
     353             : 
     354             : void quantize_32x32_fp_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     355             :                              int skip_block, const int16_t *quant_ptr,
     356             :                              const int16_t *dequant_ptr,
     357             :                              const cuml_bins_type_nuq *cuml_bins_ptr,
     358             :                              const dequant_val_type_nuq *dequant_val,
     359             :                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     360             :                              uint16_t *eob_ptr, const int16_t *scan,
     361             :                              const uint8_t *band) {
     362             :   int eob = -1;
     363             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     364             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     365             :   if (!skip_block) {
     366             :     int i;
     367             :     for (i = 0; i < n_coeffs; i++) {
     368             :       const int rc = scan[i];
     369             :       if (quantize_coeff_bigtx_fp_nuq(
     370             :               coeff_ptr[rc], quant_ptr[rc != 0], dequant_ptr[rc != 0],
     371             :               cuml_bins_ptr[band[i]], dequant_val[band[i]], &qcoeff_ptr[rc],
     372             :               &dqcoeff_ptr[rc], av1_get_tx_scale(TX_32X32)))
     373             :         eob = i;
     374             :     }
     375             :   }
     376             :   *eob_ptr = eob + 1;
     377             : }
     378             : 
     379             : #if CONFIG_TX64X64
     380             : void quantize_64x64_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     381             :                           int skip_block, const int16_t *quant_ptr,
     382             :                           const int16_t *quant_shift_ptr,
     383             :                           const int16_t *dequant_ptr,
     384             :                           const cuml_bins_type_nuq *cuml_bins_ptr,
     385             :                           const dequant_val_type_nuq *dequant_val,
     386             :                           tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     387             :                           uint16_t *eob_ptr, const int16_t *scan,
     388             :                           const uint8_t *band) {
     389             :   int eob = -1;
     390             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     391             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     392             :   if (!skip_block) {
     393             :     int i;
     394             :     for (i = 0; i < n_coeffs; i++) {
     395             :       const int rc = scan[i];
     396             :       if (quantize_coeff_bigtx_nuq(
     397             :               coeff_ptr[rc], quant_ptr[rc != 0], quant_shift_ptr[rc != 0],
     398             :               dequant_ptr[rc != 0], cuml_bins_ptr[band[i]],
     399             :               dequant_val[band[i]], &qcoeff_ptr[rc], &dqcoeff_ptr[rc],
     400             :               av1_get_tx_scale(TX_64X64)))
     401             :         eob = i;
     402             :     }
     403             :   }
     404             :   *eob_ptr = eob + 1;
     405             : }
     406             : 
     407             : void quantize_64x64_fp_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     408             :                              int skip_block, const int16_t *quant_ptr,
     409             :                              const int16_t *dequant_ptr,
     410             :                              const cuml_bins_type_nuq *cuml_bins_ptr,
     411             :                              const dequant_val_type_nuq *dequant_val,
     412             :                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     413             :                              uint16_t *eob_ptr, const int16_t *scan,
     414             :                              const uint8_t *band) {
     415             :   int eob = -1;
     416             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     417             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     418             :   if (!skip_block) {
     419             :     int i;
     420             :     for (i = 0; i < n_coeffs; i++) {
     421             :       const int rc = scan[i];
     422             :       if (quantize_coeff_bigtx_fp_nuq(
     423             :               coeff_ptr[rc], quant_ptr[rc != 0], dequant_ptr[rc != 0],
     424             :               cuml_bins_ptr[band[i]], dequant_val[band[i]], &qcoeff_ptr[rc],
     425             :               &dqcoeff_ptr[rc], av1_get_tx_scale(TX_64X64)))
     426             :         eob = i;
     427             :     }
     428             :   }
     429             :   *eob_ptr = eob + 1;
     430             : }
     431             : #endif  // CONFIG_TX64X64
     432             : #endif  // CONFIG_NEW_QUANT
     433             : 
     434           0 : void av1_quantize_skip(intptr_t n_coeffs, tran_low_t *qcoeff_ptr,
     435             :                        tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
     436           0 :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     437           0 :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     438           0 :   *eob_ptr = 0;
     439           0 : }
     440             : 
     441           0 : static void quantize_fp_helper_c(
     442             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
     443             :     const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr,
     444             :     const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
     445             :     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
     446             :     const int16_t *scan, const int16_t *iscan,
     447             : #if CONFIG_AOM_QM
     448             :     const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
     449             : #endif
     450             :     int log_scale) {
     451           0 :   int i, eob = -1;
     452             :   // TODO(jingning) Decide the need of these arguments after the
     453             :   // quantization process is completed.
     454             :   (void)zbin_ptr;
     455             :   (void)quant_shift_ptr;
     456             :   (void)iscan;
     457             : 
     458           0 :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     459           0 :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     460             : 
     461           0 :   if (!skip_block) {
     462             :     // Quantization pass: All coefficients with index >= zero_flag are
     463             :     // skippable. Note: zero_flag can be zero.
     464           0 :     for (i = 0; i < n_coeffs; i++) {
     465           0 :       const int rc = scan[i];
     466           0 :       const int coeff = coeff_ptr[rc];
     467             : #if CONFIG_AOM_QM
     468             :       const qm_val_t wt = qm_ptr[rc];
     469             :       const qm_val_t iwt = iqm_ptr[rc];
     470             :       const int dequant =
     471             :           (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
     472             :           AOM_QM_BITS;
     473             : #endif
     474           0 :       const int coeff_sign = (coeff >> 31);
     475           0 :       int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
     476           0 :       int tmp32 = 0;
     477             : #if CONFIG_AOM_QM
     478             :       if (abs_coeff * wt >=
     479             :           (dequant_ptr[rc != 0] << (AOM_QM_BITS - (1 + log_scale)))) {
     480             : #else
     481           0 :       if (abs_coeff >= (dequant_ptr[rc != 0] >> (1 + log_scale))) {
     482             : #endif
     483           0 :         abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale);
     484           0 :         abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
     485             : #if CONFIG_AOM_QM
     486             :         tmp32 = (int)((abs_coeff * wt * quant_ptr[rc != 0]) >>
     487             :                       ((16 - log_scale) + AOM_QM_BITS));
     488             :         qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
     489             :         dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant / (1 << log_scale);
     490             : #else
     491           0 :         tmp32 = (int)((abs_coeff * quant_ptr[rc != 0]) >> (16 - log_scale));
     492           0 :         qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
     493           0 :         dqcoeff_ptr[rc] =
     494           0 :             qcoeff_ptr[rc] * dequant_ptr[rc != 0] / (1 << log_scale);
     495             : #endif
     496             :       }
     497             : 
     498           0 :       if (tmp32) eob = i;
     499             :     }
     500             :   }
     501           0 :   *eob_ptr = eob + 1;
     502           0 : }
     503             : 
     504           0 : void av1_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     505             :                        int skip_block, const int16_t *zbin_ptr,
     506             :                        const int16_t *round_ptr, const int16_t *quant_ptr,
     507             :                        const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
     508             :                        tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
     509             :                        uint16_t *eob_ptr, const int16_t *scan,
     510             :                        const int16_t *iscan
     511             : #if CONFIG_AOM_QM
     512             :                        ,
     513             :                        const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr
     514             : #endif
     515             :                        ) {
     516           0 :   quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
     517             :                        quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
     518             :                        dequant_ptr, eob_ptr, scan, iscan,
     519             : #if CONFIG_AOM_QM
     520             :                        qm_ptr, iqm_ptr,
     521             : #endif
     522             :                        0);
     523           0 : }
     524             : 
     525           0 : void av1_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     526             :                              int skip_block, const int16_t *zbin_ptr,
     527             :                              const int16_t *round_ptr, const int16_t *quant_ptr,
     528             :                              const int16_t *quant_shift_ptr,
     529             :                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     530             :                              const int16_t *dequant_ptr, uint16_t *eob_ptr,
     531             :                              const int16_t *scan, const int16_t *iscan
     532             : #if CONFIG_AOM_QM
     533             :                              ,
     534             :                              const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr
     535             : #endif
     536             :                              ) {
     537           0 :   quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
     538             :                        quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
     539             :                        dequant_ptr, eob_ptr, scan, iscan,
     540             : #if CONFIG_AOM_QM
     541             :                        qm_ptr, iqm_ptr,
     542             : #endif
     543             :                        1);
     544           0 : }
     545             : 
     546             : #if CONFIG_TX64X64
     547             : void av1_quantize_fp_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     548             :                              int skip_block, const int16_t *zbin_ptr,
     549             :                              const int16_t *round_ptr, const int16_t *quant_ptr,
     550             :                              const int16_t *quant_shift_ptr,
     551             :                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
     552             :                              const int16_t *dequant_ptr, uint16_t *eob_ptr,
     553             :                              const int16_t *scan, const int16_t *iscan
     554             : #if CONFIG_AOM_QM
     555             :                              ,
     556             :                              const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr
     557             : #endif
     558             :                              ) {
     559             :   quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
     560             :                        quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
     561             :                        dequant_ptr, eob_ptr, scan, iscan,
     562             : #if CONFIG_AOM_QM
     563             :                        qm_ptr, iqm_ptr,
     564             : #endif
     565             :                        2);
     566             : }
     567             : #endif  // CONFIG_TX64X64
     568             : 
     569           0 : void av1_quantize_fp_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     570             :                             const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
     571             :                             const MACROBLOCKD_PLANE *pd,
     572             :                             tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     573             :                             const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
     574             :   // obsolete skip_block
     575           0 :   const int skip_block = 0;
     576             : #if CONFIG_AOM_QM
     577             :   const qm_val_t *qm_ptr = qparam->qmatrix;
     578             :   const qm_val_t *iqm_ptr = qparam->iqmatrix;
     579             : #endif  // CONFIG_AOM_QM
     580             : 
     581           0 :   switch (qparam->log_scale) {
     582             :     case 0:
     583           0 :       if (n_coeffs < 16) {
     584             :         // TODO(jingning): Need SIMD implementation for smaller block size
     585             :         // quantization.
     586           0 :         quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, p->zbin,
     587             :                              p->round_fp, p->quant_fp, p->quant_shift,
     588             :                              qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
     589             :                              sc->scan, sc->iscan,
     590             : #if CONFIG_AOM_QM
     591             :                              qm_ptr, iqm_ptr,
     592             : #endif
     593             :                              qparam->log_scale);
     594             :       } else {
     595           0 :         av1_quantize_fp(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round_fp,
     596             :                         p->quant_fp, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
     597             :                         pd->dequant, eob_ptr, sc->scan, sc->iscan
     598             : #if CONFIG_AOM_QM
     599             :                         ,
     600             :                         qm_ptr, iqm_ptr
     601             : #endif
     602             :                         );
     603             :       }
     604           0 :       break;
     605             :     case 1:
     606           0 :       av1_quantize_fp_32x32(coeff_ptr, n_coeffs, skip_block, p->zbin,
     607             :                             p->round_fp, p->quant_fp, p->quant_shift,
     608             :                             qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
     609             :                             sc->scan, sc->iscan
     610             : #if CONFIG_AOM_QM
     611             :                             ,
     612             :                             qm_ptr, iqm_ptr
     613             : #endif
     614             :                             );
     615           0 :       break;
     616             : #if CONFIG_TX64X64
     617             :     case 2:
     618             :       av1_quantize_fp_64x64(coeff_ptr, n_coeffs, skip_block, p->zbin,
     619             :                             p->round_fp, p->quant_fp, p->quant_shift,
     620             :                             qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
     621             :                             sc->scan, sc->iscan
     622             : #if CONFIG_AOM_QM
     623             :                             ,
     624             :                             qm_ptr, iqm_ptr
     625             : #endif
     626             :                             );
     627             :       break;
     628             : #endif  // CONFIG_TX64X64
     629           0 :     default: assert(0);
     630             :   }
     631           0 : }
     632             : 
     633           0 : void av1_quantize_b_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     634             :                            const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
     635             :                            const MACROBLOCKD_PLANE *pd, tran_low_t *dqcoeff_ptr,
     636             :                            uint16_t *eob_ptr, const SCAN_ORDER *sc,
     637             :                            const QUANT_PARAM *qparam) {
     638             :   // obsolete skip_block
     639           0 :   const int skip_block = 0;
     640             : #if CONFIG_AOM_QM
     641             :   const qm_val_t *qm_ptr = qparam->qmatrix;
     642             :   const qm_val_t *iqm_ptr = qparam->iqmatrix;
     643             : #endif  // CONFIG_AOM_QM
     644             : 
     645           0 :   switch (qparam->log_scale) {
     646             :     case 0:
     647           0 :       aom_quantize_b(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round,
     648             :                      p->quant, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
     649             :                      pd->dequant, eob_ptr, sc->scan, sc->iscan
     650             : #if CONFIG_AOM_QM
     651             :                      ,
     652             :                      qm_ptr, iqm_ptr
     653             : #endif
     654             :                      );
     655           0 :       break;
     656             :     case 1:
     657           0 :       aom_quantize_b_32x32(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round,
     658             :                            p->quant, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
     659             :                            pd->dequant, eob_ptr, sc->scan, sc->iscan
     660             : #if CONFIG_AOM_QM
     661             :                            ,
     662             :                            qm_ptr, iqm_ptr
     663             : #endif
     664             :                            );
     665           0 :       break;
     666             : #if CONFIG_TX64X64
     667             :     case 2:
     668             :       aom_quantize_b_64x64(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round,
     669             :                            p->quant, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
     670             :                            pd->dequant, eob_ptr, sc->scan, sc->iscan
     671             : #if CONFIG_AOM_QM
     672             :                            ,
     673             :                            qm_ptr, iqm_ptr
     674             : #endif
     675             :                            );
     676             :       break;
     677             : #endif  // CONFIG_TX64X64
     678           0 :     default: assert(0);
     679             :   }
     680           0 : }
     681             : 
     682           0 : void av1_quantize_dc_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     683             :                             const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
     684             :                             const MACROBLOCKD_PLANE *pd,
     685             :                             tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     686             :                             const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
     687             :   // obsolete skip_block
     688           0 :   const int skip_block = 0;
     689             : #if CONFIG_AOM_QM
     690             :   const qm_val_t *qm_ptr = qparam->qmatrix;
     691             :   const qm_val_t *iqm_ptr = qparam->iqmatrix;
     692             : #endif  // CONFIG_AOM_QM
     693             : 
     694             :   (void)sc;
     695             : 
     696           0 :   switch (qparam->log_scale) {
     697             :     case 0:
     698           0 :       aom_quantize_dc(coeff_ptr, (int)n_coeffs, skip_block, p->round,
     699           0 :                       p->quant_fp[0], qcoeff_ptr, dqcoeff_ptr, pd->dequant[0],
     700             :                       eob_ptr
     701             : #if CONFIG_AOM_QM
     702             :                       ,
     703             :                       qm_ptr, iqm_ptr
     704             : #endif
     705             :                       );
     706           0 :       break;
     707             :     case 1:
     708           0 :       aom_quantize_dc_32x32(coeff_ptr, skip_block, p->round, p->quant_fp[0],
     709           0 :                             qcoeff_ptr, dqcoeff_ptr, pd->dequant[0], eob_ptr
     710             : #if CONFIG_AOM_QM
     711             :                             ,
     712             :                             qm_ptr, iqm_ptr
     713             : #endif
     714             :                             );
     715           0 :       break;
     716             : #if CONFIG_TX64X64
     717             :       aom_quantize_dc_64x64(coeff_ptr, skip_block, p->round, p->quant_fp[0],
     718             :                             qcoeff_ptr, dqcoeff_ptr, pd->dequant[0], eob_ptr
     719             : #if CONFIG_AOM_QM
     720             :                             ,
     721             :                             qm_ptr, iqm_ptr
     722             : #endif
     723             :                             );
     724             :     case 2: break;
     725             : #endif  // CONFIG_TX64X64
     726           0 :     default: assert(0);
     727             :   }
     728           0 : }
     729             : 
     730             : #if CONFIG_NEW_QUANT
     731             : void av1_quantize_b_nuq_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     732             :                                const MACROBLOCK_PLANE *p,
     733             :                                tran_low_t *qcoeff_ptr,
     734             :                                const MACROBLOCKD_PLANE *pd,
     735             :                                tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     736             :                                const SCAN_ORDER *sc,
     737             :                                const QUANT_PARAM *qparam) {
     738             :   // obsolete skip_block
     739             :   const int skip_block = 0;
     740             :   const uint8_t *band = get_band_translate(qparam->tx_size);
     741             :   int dq = qparam->dq;
     742             : 
     743             :   switch (qparam->log_scale) {
     744             :     case 0:
     745             :       quantize_nuq(coeff_ptr, n_coeffs, skip_block, p->quant, p->quant_shift,
     746             :                    pd->dequant,
     747             :                    (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
     748             :                    (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
     749             :                    qcoeff_ptr, dqcoeff_ptr, eob_ptr, sc->scan, band);
     750             :       break;
     751             :     case 1:
     752             :       quantize_32x32_nuq(coeff_ptr, n_coeffs, skip_block, p->quant,
     753             :                          p->quant_shift, pd->dequant,
     754             :                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
     755             :                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
     756             :                          qcoeff_ptr, dqcoeff_ptr, eob_ptr, sc->scan, band);
     757             :       break;
     758             : #if CONFIG_TX64X64
     759             :     case 2:
     760             :       quantize_64x64_nuq(coeff_ptr, n_coeffs, skip_block, p->quant,
     761             :                          p->quant_shift, pd->dequant,
     762             :                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
     763             :                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
     764             :                          qcoeff_ptr, dqcoeff_ptr, eob_ptr, sc->scan, band);
     765             :       break;
     766             : #endif  // CONFIG_TX64X64
     767             :     default: assert(0);
     768             :   }
     769             : }
     770             : 
     771             : void av1_quantize_fp_nuq_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     772             :                                 const MACROBLOCK_PLANE *p,
     773             :                                 tran_low_t *qcoeff_ptr,
     774             :                                 const MACROBLOCKD_PLANE *pd,
     775             :                                 tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     776             :                                 const SCAN_ORDER *sc,
     777             :                                 const QUANT_PARAM *qparam) {
     778             :   // obsolete skip_block
     779             :   const int skip_block = 0;
     780             :   const uint8_t *band = get_band_translate(qparam->tx_size);
     781             :   int dq = qparam->dq;
     782             : 
     783             :   switch (qparam->log_scale) {
     784             :     case 0:
     785             :       quantize_fp_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_fp, pd->dequant,
     786             :                       (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
     787             :                       (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
     788             :                       qcoeff_ptr, dqcoeff_ptr, eob_ptr, sc->scan, band);
     789             :       break;
     790             :     case 1:
     791             :       quantize_32x32_fp_nuq(
     792             :           coeff_ptr, n_coeffs, skip_block, p->quant_fp, pd->dequant,
     793             :           (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
     794             :           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff_ptr,
     795             :           dqcoeff_ptr, eob_ptr, sc->scan, band);
     796             :       break;
     797             : #if CONFIG_TX64X64
     798             :     case 2:
     799             :       quantize_64x64_fp_nuq(
     800             :           coeff_ptr, n_coeffs, skip_block, p->quant_fp, pd->dequant,
     801             :           (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
     802             :           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff_ptr,
     803             :           dqcoeff_ptr, eob_ptr, sc->scan, band);
     804             :       break;
     805             : #endif  // CONFIG_TX64X64
     806             :     default: assert(0);
     807             :   }
     808             : }
     809             : 
     810             : void av1_quantize_dc_nuq_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     811             :                                 const MACROBLOCK_PLANE *p,
     812             :                                 tran_low_t *qcoeff_ptr,
     813             :                                 const MACROBLOCKD_PLANE *pd,
     814             :                                 tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     815             :                                 const SCAN_ORDER *sc,
     816             :                                 const QUANT_PARAM *qparam) {
     817             :   // obsolete skip_block
     818             :   const int skip_block = 0;
     819             :   int dq = qparam->dq;
     820             :   (void)sc;
     821             : 
     822             :   switch (qparam->log_scale) {
     823             :     case 0:
     824             :       quantize_dc_fp_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_fp[0],
     825             :                          pd->dequant[0], p->cuml_bins_nuq[dq][0],
     826             :                          pd->dequant_val_nuq[dq][0], qcoeff_ptr, dqcoeff_ptr,
     827             :                          eob_ptr);
     828             :       break;
     829             :     case 1:
     830             :       quantize_dc_32x32_fp_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_fp[0],
     831             :                                pd->dequant[0], p->cuml_bins_nuq[dq][0],
     832             :                                pd->dequant_val_nuq[dq][0], qcoeff_ptr,
     833             :                                dqcoeff_ptr, eob_ptr);
     834             :       break;
     835             : #if CONFIG_TX64X64
     836             :     case 2:
     837             :       quantize_dc_64x64_fp_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_fp[0],
     838             :                                pd->dequant[0], p->cuml_bins_nuq[dq][0],
     839             :                                pd->dequant_val_nuq[dq][0], qcoeff_ptr,
     840             :                                dqcoeff_ptr, eob_ptr);
     841             :       break;
     842             : #endif  // CONFIG_TX64X64
     843             :     default: assert(0);
     844             :   }
     845             : }
     846             : #endif  // CONFIG_NEW_QUANT
     847             : 
     848             : #if CONFIG_HIGHBITDEPTH
     849           0 : void av1_highbd_quantize_fp_facade(const tran_low_t *coeff_ptr,
     850             :                                    intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
     851             :                                    tran_low_t *qcoeff_ptr,
     852             :                                    const MACROBLOCKD_PLANE *pd,
     853             :                                    tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     854             :                                    const SCAN_ORDER *sc,
     855             :                                    const QUANT_PARAM *qparam) {
     856             :   // obsolete skip_block
     857           0 :   const int skip_block = 0;
     858             : #if CONFIG_AOM_QM
     859             :   const qm_val_t *qm_ptr = qparam->qmatrix;
     860             :   const qm_val_t *iqm_ptr = qparam->iqmatrix;
     861             : #endif  // CONFIG_AOM_QM
     862             : 
     863           0 :   if (n_coeffs < 16) {
     864             :     // TODO(jingning): Need SIMD implementation for smaller block size
     865             :     // quantization.
     866           0 :     av1_highbd_quantize_fp_c(coeff_ptr, n_coeffs, skip_block, p->zbin,
     867             :                              p->round_fp, p->quant_fp, p->quant_shift,
     868             :                              qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
     869             :                              sc->scan, sc->iscan,
     870             : #if CONFIG_AOM_QM
     871             :                              qm_ptr, iqm_ptr,
     872             : #endif
     873             :                              qparam->log_scale);
     874           0 :     return;
     875             :   }
     876             : 
     877           0 :   av1_highbd_quantize_fp(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round_fp,
     878             :                          p->quant_fp, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
     879             :                          pd->dequant, eob_ptr, sc->scan, sc->iscan,
     880             : #if CONFIG_AOM_QM
     881             :                          qm_ptr, iqm_ptr,
     882             : #endif
     883             :                          qparam->log_scale);
     884             : }
     885             : 
     886           0 : void av1_highbd_quantize_b_facade(const tran_low_t *coeff_ptr,
     887             :                                   intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
     888             :                                   tran_low_t *qcoeff_ptr,
     889             :                                   const MACROBLOCKD_PLANE *pd,
     890             :                                   tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     891             :                                   const SCAN_ORDER *sc,
     892             :                                   const QUANT_PARAM *qparam) {
     893             :   // obsolete skip_block
     894           0 :   const int skip_block = 0;
     895             : #if CONFIG_AOM_QM
     896             :   const qm_val_t *qm_ptr = qparam->qmatrix;
     897             :   const qm_val_t *iqm_ptr = qparam->iqmatrix;
     898             : #endif  // CONFIG_AOM_QM
     899             : 
     900           0 :   switch (qparam->log_scale) {
     901             :     case 0:
     902           0 :       aom_highbd_quantize_b(coeff_ptr, n_coeffs, skip_block, p->zbin, p->round,
     903             :                             p->quant, p->quant_shift, qcoeff_ptr, dqcoeff_ptr,
     904             :                             pd->dequant, eob_ptr, sc->scan, sc->iscan
     905             : #if CONFIG_AOM_QM
     906             :                             ,
     907             :                             qm_ptr, iqm_ptr
     908             : #endif
     909             :                             );
     910           0 :       break;
     911             :     case 1:
     912           0 :       aom_highbd_quantize_b_32x32(coeff_ptr, n_coeffs, skip_block, p->zbin,
     913             :                                   p->round, p->quant, p->quant_shift,
     914             :                                   qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
     915             :                                   sc->scan, sc->iscan
     916             : #if CONFIG_AOM_QM
     917             :                                   ,
     918             :                                   qm_ptr, iqm_ptr
     919             : #endif
     920             :                                   );
     921           0 :       break;
     922             : #if CONFIG_TX64X64
     923             :     case 2:
     924             :       aom_highbd_quantize_b_64x64(coeff_ptr, n_coeffs, skip_block, p->zbin,
     925             :                                   p->round, p->quant, p->quant_shift,
     926             :                                   qcoeff_ptr, dqcoeff_ptr, pd->dequant, eob_ptr,
     927             :                                   sc->scan, sc->iscan
     928             : #if CONFIG_AOM_QM
     929             :                                   ,
     930             :                                   qm_ptr, iqm_ptr
     931             : #endif
     932             :                                   );
     933             :       break;
     934             : #endif  // CONFIG_TX64X64
     935           0 :     default: assert(0);
     936             :   }
     937           0 : }
     938             : 
     939             : #if CONFIG_HIGHBITDEPTH
     940           0 : static INLINE void highbd_quantize_dc(
     941             :     const tran_low_t *coeff_ptr, int n_coeffs, int skip_block,
     942             :     const int16_t *round_ptr, const int16_t quant, tran_low_t *qcoeff_ptr,
     943             :     tran_low_t *dqcoeff_ptr, const int16_t dequant_ptr, uint16_t *eob_ptr,
     944             : #if CONFIG_AOM_QM
     945             :     const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
     946             : #endif
     947             :     const int log_scale) {
     948           0 :   int eob = -1;
     949             : 
     950           0 :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     951           0 :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     952             : #if CONFIG_AOM_QM
     953             :   (void)qm_ptr;
     954             :   (void)iqm_ptr;
     955             : #endif
     956           0 :   if (!skip_block) {
     957           0 :     const int coeff = coeff_ptr[0];
     958           0 :     const int coeff_sign = (coeff >> 31);
     959           0 :     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
     960           0 :     const int64_t tmp = abs_coeff + round_ptr[0];
     961           0 :     const uint32_t abs_qcoeff = (uint32_t)((tmp * quant) >> (16 - log_scale));
     962           0 :     qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
     963           0 :     dqcoeff_ptr[0] = qcoeff_ptr[0] * dequant_ptr / (1 << log_scale);
     964           0 :     if (abs_qcoeff) eob = 0;
     965             :   }
     966           0 :   *eob_ptr = eob + 1;
     967           0 : }
     968             : #endif  // CONFIG_HIGHBITDEPTH
     969             : 
     970           0 : void av1_highbd_quantize_dc_facade(const tran_low_t *coeff_ptr,
     971             :                                    intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
     972             :                                    tran_low_t *qcoeff_ptr,
     973             :                                    const MACROBLOCKD_PLANE *pd,
     974             :                                    tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
     975             :                                    const SCAN_ORDER *sc,
     976             :                                    const QUANT_PARAM *qparam) {
     977             :   // obsolete skip_block
     978           0 :   const int skip_block = 0;
     979             : #if CONFIG_AOM_QM
     980             :   const qm_val_t *qm_ptr = qparam->qmatrix;
     981             :   const qm_val_t *iqm_ptr = qparam->iqmatrix;
     982             : #endif  // CONFIG_AOM_QM
     983             : 
     984             :   (void)sc;
     985             : 
     986           0 :   highbd_quantize_dc(coeff_ptr, (int)n_coeffs, skip_block, p->round,
     987           0 :                      p->quant_fp[0], qcoeff_ptr, dqcoeff_ptr, pd->dequant[0],
     988             :                      eob_ptr,
     989             : #if CONFIG_AOM_QM
     990             :                      qm_ptr, iqm_ptr,
     991             : #endif
     992             :                      qparam->log_scale);
     993           0 : }
     994             : 
     995             : #if CONFIG_NEW_QUANT
     996             : static INLINE int highbd_quantize_coeff_nuq(
     997             :     const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
     998             :     const int16_t dequant, const tran_low_t *cuml_bins_ptr,
     999             :     const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
    1000             :     tran_low_t *dqcoeff_ptr) {
    1001             :   const int coeff = coeffv;
    1002             :   const int coeff_sign = (coeff >> 31);
    1003             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    1004             :   int i, q;
    1005             :   int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
    1006             :   for (i = 0; i < NUQ_KNOTS; i++) {
    1007             :     if (tmp < cuml_bins_ptr[i]) {
    1008             :       q = i;
    1009             :       break;
    1010             :     }
    1011             :   }
    1012             :   if (i == NUQ_KNOTS) {
    1013             :     tmp -= cuml_bins_ptr[NUQ_KNOTS - 1];
    1014             :     q = NUQ_KNOTS + (int)(((((tmp * quant) >> 16) + tmp) * quant_shift) >> 16);
    1015             :   }
    1016             :   if (q) {
    1017             :     *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
    1018             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
    1019             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
    1020             :   } else {
    1021             :     *qcoeff_ptr = 0;
    1022             :     *dqcoeff_ptr = 0;
    1023             :   }
    1024             :   return (q != 0);
    1025             : }
    1026             : 
    1027             : static INLINE int highbd_quantize_coeff_fp_nuq(
    1028             :     const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
    1029             :     const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
    1030             :     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr) {
    1031             :   const int coeff = coeffv;
    1032             :   const int coeff_sign = (coeff >> 31);
    1033             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    1034             :   int i, q;
    1035             :   int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
    1036             :   for (i = 0; i < NUQ_KNOTS; i++) {
    1037             :     if (tmp < cuml_bins_ptr[i]) {
    1038             :       q = i;
    1039             :       break;
    1040             :     }
    1041             :   }
    1042             :   if (i == NUQ_KNOTS) {
    1043             :     q = NUQ_KNOTS + (int)(((tmp - cuml_bins_ptr[NUQ_KNOTS - 1]) * quant) >> 16);
    1044             :   }
    1045             :   if (q) {
    1046             :     *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
    1047             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
    1048             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
    1049             :   } else {
    1050             :     *qcoeff_ptr = 0;
    1051             :     *dqcoeff_ptr = 0;
    1052             :   }
    1053             :   return (q != 0);
    1054             : }
    1055             : 
    1056             : static INLINE int highbd_quantize_coeff_bigtx_fp_nuq(
    1057             :     const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
    1058             :     const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
    1059             :     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, int logsizeby16) {
    1060             :   const int coeff = coeffv;
    1061             :   const int coeff_sign = (coeff >> 31);
    1062             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    1063             :   int i, q;
    1064             :   int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
    1065             :   for (i = 0; i < NUQ_KNOTS; i++) {
    1066             :     if (tmp < ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
    1067             :       q = i;
    1068             :       break;
    1069             :     }
    1070             :   }
    1071             :   if (i == NUQ_KNOTS) {
    1072             :     q = NUQ_KNOTS +
    1073             :         (int)(((tmp -
    1074             :                 ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16)) *
    1075             :                quant) >>
    1076             :               (16 - logsizeby16));
    1077             :   }
    1078             :   if (q) {
    1079             :     *dqcoeff_ptr = ROUND_POWER_OF_TWO(
    1080             :         av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
    1081             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
    1082             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
    1083             :   } else {
    1084             :     *qcoeff_ptr = 0;
    1085             :     *dqcoeff_ptr = 0;
    1086             :   }
    1087             :   return (q != 0);
    1088             : }
    1089             : 
    1090             : static INLINE int highbd_quantize_coeff_bigtx_nuq(
    1091             :     const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
    1092             :     const int16_t dequant, const tran_low_t *cuml_bins_ptr,
    1093             :     const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
    1094             :     tran_low_t *dqcoeff_ptr, int logsizeby16) {
    1095             :   const int coeff = coeffv;
    1096             :   const int coeff_sign = (coeff >> 31);
    1097             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    1098             :   int i, q;
    1099             :   int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
    1100             :   for (i = 0; i < NUQ_KNOTS; i++) {
    1101             :     if (tmp < ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
    1102             :       q = i;
    1103             :       break;
    1104             :     }
    1105             :   }
    1106             :   if (i == NUQ_KNOTS) {
    1107             :     tmp -= ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16);
    1108             :     q = NUQ_KNOTS + (int)(((((tmp * quant) >> 16) + tmp) * quant_shift) >>
    1109             :                           (16 - logsizeby16));
    1110             :   }
    1111             :   if (q) {
    1112             :     *dqcoeff_ptr = ROUND_POWER_OF_TWO(
    1113             :         av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
    1114             :     *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
    1115             :     *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
    1116             :   } else {
    1117             :     *qcoeff_ptr = 0;
    1118             :     *dqcoeff_ptr = 0;
    1119             :   }
    1120             :   return (q != 0);
    1121             : }
    1122             : 
    1123             : void highbd_quantize_dc_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
    1124             :                             int skip_block, const int16_t quant,
    1125             :                             const int16_t quant_shift, const int16_t dequant,
    1126             :                             const tran_low_t *cuml_bins_ptr,
    1127             :                             const tran_low_t *dequant_val,
    1128             :                             tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
    1129             :                             uint16_t *eob_ptr) {
    1130             :   int eob = -1;
    1131             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1132             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1133             :   if (!skip_block) {
    1134             :     const int rc = 0;
    1135             :     if (highbd_quantize_coeff_nuq(coeff_ptr[rc], quant, quant_shift, dequant,
    1136             :                                   cuml_bins_ptr, dequant_val, qcoeff_ptr,
    1137             :                                   dqcoeff_ptr))
    1138             :       eob = 0;
    1139             :   }
    1140             :   *eob_ptr = eob + 1;
    1141             : }
    1142             : 
    1143             : void highbd_quantize_dc_fp_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
    1144             :                                int skip_block, const int16_t quant,
    1145             :                                const int16_t dequant,
    1146             :                                const tran_low_t *cuml_bins_ptr,
    1147             :                                const tran_low_t *dequant_val,
    1148             :                                tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
    1149             :                                uint16_t *eob_ptr) {
    1150             :   int eob = -1;
    1151             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1152             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1153             :   if (!skip_block) {
    1154             :     const int rc = 0;
    1155             :     if (highbd_quantize_coeff_fp_nuq(coeff_ptr[rc], quant, dequant,
    1156             :                                      cuml_bins_ptr, dequant_val, qcoeff_ptr,
    1157             :                                      dqcoeff_ptr))
    1158             :       eob = 0;
    1159             :   }
    1160             :   *eob_ptr = eob + 1;
    1161             : }
    1162             : 
    1163             : void highbd_quantize_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
    1164             :                            int skip_block, const int16_t *quant_ptr,
    1165             :                            const int16_t *quant_shift_ptr,
    1166             :                            const int16_t *dequant_ptr,
    1167             :                            const cuml_bins_type_nuq *cuml_bins_ptr,
    1168             :                            const dequant_val_type_nuq *dequant_val,
    1169             :                            tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
    1170             :                            uint16_t *eob_ptr, const int16_t *scan,
    1171             :                            const uint8_t *band) {
    1172             :   int eob = -1;
    1173             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1174             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1175             :   if (!skip_block) {
    1176             :     int i;
    1177             :     for (i = 0; i < n_coeffs; i++) {
    1178             :       const int rc = scan[i];
    1179             :       if (highbd_quantize_coeff_nuq(
    1180             :               coeff_ptr[rc], quant_ptr[rc != 0], quant_shift_ptr[rc != 0],
    1181             :               dequant_ptr[rc != 0], cuml_bins_ptr[band[i]],
    1182             :               dequant_val[band[i]], &qcoeff_ptr[rc], &dqcoeff_ptr[rc]))
    1183             :         eob = i;
    1184             :     }
    1185             :   }
    1186             :   *eob_ptr = eob + 1;
    1187             : }
    1188             : 
    1189             : void highbd_quantize_32x32_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
    1190             :                                  int skip_block, const int16_t *quant_ptr,
    1191             :                                  const int16_t *quant_shift_ptr,
    1192             :                                  const int16_t *dequant_ptr,
    1193             :                                  const cuml_bins_type_nuq *cuml_bins_ptr,
    1194             :                                  const dequant_val_type_nuq *dequant_val,
    1195             :                                  tran_low_t *qcoeff_ptr,
    1196             :                                  tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
    1197             :                                  const int16_t *scan, const uint8_t *band) {
    1198             :   int eob = -1;
    1199             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1200             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1201             :   if (!skip_block) {
    1202             :     int i;
    1203             :     for (i = 0; i < n_coeffs; i++) {
    1204             :       const int rc = scan[i];
    1205             :       if (highbd_quantize_coeff_bigtx_nuq(
    1206             :               coeff_ptr[rc], quant_ptr[rc != 0], quant_shift_ptr[rc != 0],
    1207             :               dequant_ptr[rc != 0], cuml_bins_ptr[band[i]],
    1208             :               dequant_val[band[i]], &qcoeff_ptr[rc], &dqcoeff_ptr[rc],
    1209             :               av1_get_tx_scale(TX_32X32)))
    1210             :         eob = i;
    1211             :     }
    1212             :   }
    1213             :   *eob_ptr = eob + 1;
    1214             : }
    1215             : 
    1216             : void highbd_quantize_32x32_fp_nuq_c(const tran_low_t *coeff_ptr,
    1217             :                                     intptr_t n_coeffs, int skip_block,
    1218             :                                     const int16_t *quant_ptr,
    1219             :                                     const int16_t *dequant_ptr,
    1220             :                                     const cuml_bins_type_nuq *cuml_bins_ptr,
    1221             :                                     const dequant_val_type_nuq *dequant_val,
    1222             :                                     tran_low_t *qcoeff_ptr,
    1223             :                                     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
    1224             :                                     const int16_t *scan, const uint8_t *band) {
    1225             :   int eob = -1;
    1226             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1227             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1228             :   if (!skip_block) {
    1229             :     int i;
    1230             :     for (i = 0; i < n_coeffs; i++) {
    1231             :       const int rc = scan[i];
    1232             :       if (highbd_quantize_coeff_bigtx_fp_nuq(
    1233             :               coeff_ptr[rc], quant_ptr[rc != 0], dequant_ptr[rc != 0],
    1234             :               cuml_bins_ptr[band[i]], dequant_val[band[i]], &qcoeff_ptr[rc],
    1235             :               &dqcoeff_ptr[rc], av1_get_tx_scale(TX_32X32)))
    1236             :         eob = i;
    1237             :     }
    1238             :   }
    1239             :   *eob_ptr = eob + 1;
    1240             : }
    1241             : 
    1242             : #if CONFIG_TX64X64
    1243             : void highbd_quantize_64x64_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
    1244             :                                  int skip_block, const int16_t *quant_ptr,
    1245             :                                  const int16_t *quant_shift_ptr,
    1246             :                                  const int16_t *dequant_ptr,
    1247             :                                  const cuml_bins_type_nuq *cuml_bins_ptr,
    1248             :                                  const dequant_val_type_nuq *dequant_val,
    1249             :                                  tran_low_t *qcoeff_ptr,
    1250             :                                  tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
    1251             :                                  const int16_t *scan, const uint8_t *band) {
    1252             :   int eob = -1;
    1253             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1254             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1255             :   if (!skip_block) {
    1256             :     int i;
    1257             :     for (i = 0; i < n_coeffs; i++) {
    1258             :       const int rc = scan[i];
    1259             :       if (highbd_quantize_coeff_bigtx_nuq(
    1260             :               coeff_ptr[rc], quant_ptr[rc != 0], quant_shift_ptr[rc != 0],
    1261             :               dequant_ptr[rc != 0], cuml_bins_ptr[band[i]],
    1262             :               dequant_val[band[i]], &qcoeff_ptr[rc], &dqcoeff_ptr[rc],
    1263             :               av1_get_tx_scale(TX_64X64)))
    1264             :         eob = i;
    1265             :     }
    1266             :   }
    1267             :   *eob_ptr = eob + 1;
    1268             : }
    1269             : 
    1270             : void highbd_quantize_64x64_fp_nuq_c(const tran_low_t *coeff_ptr,
    1271             :                                     intptr_t n_coeffs, int skip_block,
    1272             :                                     const int16_t *quant_ptr,
    1273             :                                     const int16_t *dequant_ptr,
    1274             :                                     const cuml_bins_type_nuq *cuml_bins_ptr,
    1275             :                                     const dequant_val_type_nuq *dequant_val,
    1276             :                                     tran_low_t *qcoeff_ptr,
    1277             :                                     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
    1278             :                                     const int16_t *scan, const uint8_t *band) {
    1279             :   int eob = -1;
    1280             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1281             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1282             :   if (!skip_block) {
    1283             :     int i;
    1284             :     for (i = 0; i < n_coeffs; i++) {
    1285             :       const int rc = scan[i];
    1286             :       if (highbd_quantize_coeff_bigtx_fp_nuq(
    1287             :               coeff_ptr[rc], quant_ptr[rc != 0], dequant_ptr[rc != 0],
    1288             :               cuml_bins_ptr[band[i]], dequant_val[band[i]], &qcoeff_ptr[rc],
    1289             :               &dqcoeff_ptr[rc], av1_get_tx_scale(TX_64X64)))
    1290             :         eob = i;
    1291             :     }
    1292             :   }
    1293             :   *eob_ptr = eob + 1;
    1294             : }
    1295             : #endif  // CONFIG_TX64X64
    1296             : 
    1297             : void highbd_quantize_fp_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
    1298             :                               int skip_block, const int16_t *quant_ptr,
    1299             :                               const int16_t *dequant_ptr,
    1300             :                               const cuml_bins_type_nuq *cuml_bins_ptr,
    1301             :                               const dequant_val_type_nuq *dequant_val,
    1302             :                               tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
    1303             :                               uint16_t *eob_ptr, const int16_t *scan,
    1304             :                               const uint8_t *band) {
    1305             :   int eob = -1;
    1306             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1307             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1308             :   if (!skip_block) {
    1309             :     int i;
    1310             :     for (i = 0; i < n_coeffs; i++) {
    1311             :       const int rc = scan[i];
    1312             :       if (highbd_quantize_coeff_fp_nuq(
    1313             :               coeff_ptr[rc], quant_ptr[rc != 0], dequant_ptr[rc != 0],
    1314             :               cuml_bins_ptr[band[i]], dequant_val[band[i]], &qcoeff_ptr[rc],
    1315             :               &dqcoeff_ptr[rc]))
    1316             :         eob = i;
    1317             :     }
    1318             :   }
    1319             :   *eob_ptr = eob + 1;
    1320             : }
    1321             : 
    1322             : void highbd_quantize_dc_32x32_nuq(
    1323             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
    1324             :     const int16_t quant, const int16_t quant_shift, const int16_t dequant,
    1325             :     const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
    1326             :     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
    1327             :   int eob = -1;
    1328             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1329             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1330             :   if (!skip_block) {
    1331             :     const int rc = 0;
    1332             :     if (highbd_quantize_coeff_bigtx_nuq(
    1333             :             coeff_ptr[rc], quant, quant_shift, dequant, cuml_bins_ptr,
    1334             :             dequant_val, qcoeff_ptr, dqcoeff_ptr, av1_get_tx_scale(TX_32X32)))
    1335             :       eob = 0;
    1336             :   }
    1337             :   *eob_ptr = eob + 1;
    1338             : }
    1339             : 
    1340             : void highbd_quantize_dc_32x32_fp_nuq(
    1341             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
    1342             :     const int16_t quant, const int16_t dequant, const tran_low_t *cuml_bins_ptr,
    1343             :     const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
    1344             :     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
    1345             :   int eob = -1;
    1346             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1347             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1348             :   if (!skip_block) {
    1349             :     const int rc = 0;
    1350             :     if (highbd_quantize_coeff_bigtx_fp_nuq(
    1351             :             coeff_ptr[rc], quant, dequant, cuml_bins_ptr, dequant_val,
    1352             :             qcoeff_ptr, dqcoeff_ptr, av1_get_tx_scale(TX_32X32)))
    1353             :       eob = 0;
    1354             :   }
    1355             :   *eob_ptr = eob + 1;
    1356             : }
    1357             : 
    1358             : #if CONFIG_TX64X64
    1359             : void highbd_quantize_dc_64x64_nuq(
    1360             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
    1361             :     const int16_t quant, const int16_t quant_shift, const int16_t dequant,
    1362             :     const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
    1363             :     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
    1364             :   int eob = -1;
    1365             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1366             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1367             :   if (!skip_block) {
    1368             :     const int rc = 0;
    1369             :     if (highbd_quantize_coeff_bigtx_nuq(
    1370             :             coeff_ptr[rc], quant, quant_shift, dequant, cuml_bins_ptr,
    1371             :             dequant_val, qcoeff_ptr, dqcoeff_ptr, av1_get_tx_scale(TX_64X64)))
    1372             :       eob = 0;
    1373             :   }
    1374             :   *eob_ptr = eob + 1;
    1375             : }
    1376             : 
    1377             : void highbd_quantize_dc_64x64_fp_nuq(
    1378             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
    1379             :     const int16_t quant, const int16_t dequant, const tran_low_t *cuml_bins_ptr,
    1380             :     const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
    1381             :     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
    1382             :   int eob = -1;
    1383             :   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    1384             :   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    1385             :   if (!skip_block) {
    1386             :     const int rc = 0;
    1387             :     if (highbd_quantize_coeff_bigtx_fp_nuq(
    1388             :             coeff_ptr[rc], quant, dequant, cuml_bins_ptr, dequant_val,
    1389             :             qcoeff_ptr, dqcoeff_ptr, av1_get_tx_scale(TX_64X64)))
    1390             :       eob = 0;
    1391             :   }
    1392             :   *eob_ptr = eob + 1;
    1393             : }
    1394             : #endif  // CONFIG_TX64X64
    1395             : 
    1396             : void av1_highbd_quantize_b_nuq_facade(
    1397             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
    1398             :     tran_low_t *qcoeff_ptr, const MACROBLOCKD_PLANE *pd,
    1399             :     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr, const SCAN_ORDER *sc,
    1400             :     const QUANT_PARAM *qparam) {
    1401             :   // obsolete skip_block
    1402             :   const int skip_block = 0;
    1403             :   const uint8_t *band = get_band_translate(qparam->tx_size);
    1404             :   const int dq = qparam->dq;
    1405             : 
    1406             :   switch (qparam->log_scale) {
    1407             :     case 0:
    1408             :       highbd_quantize_nuq(coeff_ptr, n_coeffs, skip_block, p->quant,
    1409             :                           p->quant_shift, pd->dequant,
    1410             :                           (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
    1411             :                           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
    1412             :                           qcoeff_ptr, dqcoeff_ptr, eob_ptr, sc->scan, band);
    1413             :       break;
    1414             :     case 1:
    1415             :       highbd_quantize_32x32_nuq(
    1416             :           coeff_ptr, n_coeffs, skip_block, p->quant, p->quant_shift,
    1417             :           pd->dequant, (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
    1418             :           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff_ptr,
    1419             :           dqcoeff_ptr, eob_ptr, sc->scan, band);
    1420             :       break;
    1421             : #if CONFIG_TX64X64
    1422             :     case 2:
    1423             :       highbd_quantize_64x64_nuq(
    1424             :           coeff_ptr, n_coeffs, skip_block, p->quant, p->quant_shift,
    1425             :           pd->dequant, (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
    1426             :           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff_ptr,
    1427             :           dqcoeff_ptr, eob_ptr, sc->scan, band);
    1428             :       break;
    1429             : #endif  // CONFIG_TX64X64
    1430             :     default: assert(0);
    1431             :   }
    1432             : }
    1433             : 
    1434             : void av1_highbd_quantize_fp_nuq_facade(
    1435             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
    1436             :     tran_low_t *qcoeff_ptr, const MACROBLOCKD_PLANE *pd,
    1437             :     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr, const SCAN_ORDER *sc,
    1438             :     const QUANT_PARAM *qparam) {
    1439             :   // obsolete skip_block
    1440             :   const int skip_block = 0;
    1441             :   const uint8_t *band = get_band_translate(qparam->tx_size);
    1442             :   const int dq = qparam->dq;
    1443             : 
    1444             :   switch (qparam->log_scale) {
    1445             :     case 0:
    1446             :       highbd_quantize_fp_nuq(
    1447             :           coeff_ptr, n_coeffs, skip_block, p->quant_fp, pd->dequant,
    1448             :           (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
    1449             :           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff_ptr,
    1450             :           dqcoeff_ptr, eob_ptr, sc->scan, band);
    1451             :       break;
    1452             :     case 1:
    1453             :       highbd_quantize_32x32_fp_nuq(
    1454             :           coeff_ptr, n_coeffs, skip_block, p->quant_fp, pd->dequant,
    1455             :           (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
    1456             :           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff_ptr,
    1457             :           dqcoeff_ptr, eob_ptr, sc->scan, band);
    1458             :       break;
    1459             : #if CONFIG_TX64X64
    1460             :     case 2:
    1461             :       highbd_quantize_64x64_fp_nuq(
    1462             :           coeff_ptr, n_coeffs, skip_block, p->quant_fp, pd->dequant,
    1463             :           (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
    1464             :           (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff_ptr,
    1465             :           dqcoeff_ptr, eob_ptr, sc->scan, band);
    1466             :       break;
    1467             : #endif  // CONFIG_TX64X64
    1468             :     default: assert(0);
    1469             :   }
    1470             : }
    1471             : 
    1472             : void av1_highbd_quantize_dc_nuq_facade(
    1473             :     const tran_low_t *coeff_ptr, intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
    1474             :     tran_low_t *qcoeff_ptr, const MACROBLOCKD_PLANE *pd,
    1475             :     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr, const SCAN_ORDER *sc,
    1476             :     const QUANT_PARAM *qparam) {
    1477             :   // obsolete skip_block
    1478             :   const int skip_block = 0;
    1479             :   const int dq = qparam->dq;
    1480             :   (void)sc;
    1481             : 
    1482             :   switch (qparam->log_scale) {
    1483             :     case 0:
    1484             :       highbd_quantize_dc_fp_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_fp[0],
    1485             :                                 pd->dequant[0], p->cuml_bins_nuq[dq][0],
    1486             :                                 pd->dequant_val_nuq[dq][0], qcoeff_ptr,
    1487             :                                 dqcoeff_ptr, eob_ptr);
    1488             :       break;
    1489             :     case 1:
    1490             :       highbd_quantize_dc_32x32_fp_nuq(
    1491             :           coeff_ptr, n_coeffs, skip_block, p->quant_fp[0], pd->dequant[0],
    1492             :           p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0], qcoeff_ptr,
    1493             :           dqcoeff_ptr, eob_ptr);
    1494             :       break;
    1495             : #if CONFIG_TX64X64
    1496             :     case 2:
    1497             :       highbd_quantize_dc_64x64_fp_nuq(
    1498             :           coeff_ptr, n_coeffs, skip_block, p->quant_fp[0], pd->dequant[0],
    1499             :           p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0], qcoeff_ptr,
    1500             :           dqcoeff_ptr, eob_ptr);
    1501             :       break;
    1502             : #endif  // CONFIG_TX64X64
    1503             :     default: assert(0);
    1504             :   }
    1505             : }
    1506             : #endif  // CONFIG_NEW_QUANT
    1507             : #endif  // CONFIG_HIGHBITDEPTH
    1508             : 
    1509             : #if CONFIG_HIGHBITDEPTH
    1510           0 : void av1_highbd_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t count,
    1511             :                               int skip_block, const int16_t *zbin_ptr,
    1512             :                               const int16_t *round_ptr,
    1513             :                               const int16_t *quant_ptr,
    1514             :                               const int16_t *quant_shift_ptr,
    1515             :                               tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
    1516             :                               const int16_t *dequant_ptr, uint16_t *eob_ptr,
    1517             :                               const int16_t *scan, const int16_t *iscan,
    1518             : #if CONFIG_AOM_QM
    1519             :                               const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr,
    1520             : #endif
    1521             :                               int log_scale) {
    1522             :   int i;
    1523           0 :   int eob = -1;
    1524           0 :   const int scale = 1 << log_scale;
    1525           0 :   const int shift = 16 - log_scale;
    1526             :   // TODO(jingning) Decide the need of these arguments after the
    1527             :   // quantization process is completed.
    1528             :   (void)zbin_ptr;
    1529             :   (void)quant_shift_ptr;
    1530             :   (void)iscan;
    1531             : 
    1532           0 :   memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr));
    1533           0 :   memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr));
    1534             : 
    1535           0 :   if (!skip_block) {
    1536             :     // Quantization pass: All coefficients with index >= zero_flag are
    1537             :     // skippable. Note: zero_flag can be zero.
    1538           0 :     for (i = 0; i < count; i++) {
    1539           0 :       const int rc = scan[i];
    1540           0 :       const int coeff = coeff_ptr[rc];
    1541             : #if CONFIG_AOM_QM
    1542             :       const qm_val_t wt = qm_ptr[rc];
    1543             :       const qm_val_t iwt = iqm_ptr[rc];
    1544             :       const int dequant =
    1545             :           (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
    1546             :           AOM_QM_BITS;
    1547             : #endif
    1548           0 :       const int coeff_sign = (coeff >> 31);
    1549           0 :       const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    1550           0 :       const int64_t tmp = abs_coeff + round_ptr[rc != 0];
    1551             : #if CONFIG_AOM_QM
    1552             :       const uint32_t abs_qcoeff =
    1553             :           (uint32_t)((tmp * quant_ptr[rc != 0] * wt) >> (shift + AOM_QM_BITS));
    1554             :       qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
    1555             :       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant / scale;
    1556             : #else
    1557           0 :       const uint32_t abs_qcoeff =
    1558           0 :           (uint32_t)((tmp * quant_ptr[rc != 0]) >> shift);
    1559           0 :       qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
    1560           0 :       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / scale;
    1561             : #endif
    1562           0 :       if (abs_qcoeff) eob = i;
    1563             :     }
    1564             :   }
    1565           0 :   *eob_ptr = eob + 1;
    1566           0 : }
    1567             : 
    1568             : #endif  // CONFIG_HIGHBITDEPTH
    1569             : 
    1570           0 : static void invert_quant(int16_t *quant, int16_t *shift, int d) {
    1571             :   uint32_t t;
    1572             :   int l, m;
    1573           0 :   t = d;
    1574           0 :   for (l = 0; t > 1; l++) t >>= 1;
    1575           0 :   m = 1 + (1 << (16 + l)) / d;
    1576           0 :   *quant = (int16_t)(m - (1 << 16));
    1577           0 :   *shift = 1 << (16 - l);
    1578           0 : }
    1579             : 
    1580           0 : static int get_qzbin_factor(int q, aom_bit_depth_t bit_depth) {
    1581           0 :   const int quant = av1_dc_quant(q, 0, bit_depth);
    1582             : #if CONFIG_HIGHBITDEPTH
    1583           0 :   switch (bit_depth) {
    1584           0 :     case AOM_BITS_8: return q == 0 ? 64 : (quant < 148 ? 84 : 80);
    1585           0 :     case AOM_BITS_10: return q == 0 ? 64 : (quant < 592 ? 84 : 80);
    1586           0 :     case AOM_BITS_12: return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
    1587             :     default:
    1588           0 :       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
    1589             :       return -1;
    1590             :   }
    1591             : #else
    1592             :   (void)bit_depth;
    1593             :   return q == 0 ? 64 : (quant < 148 ? 84 : 80);
    1594             : #endif
    1595             : }
    1596             : 
    1597           0 : void av1_build_quantizer(aom_bit_depth_t bit_depth, int y_dc_delta_q,
    1598             :                          int uv_dc_delta_q, int uv_ac_delta_q,
    1599             :                          QUANTS *const quants, Dequants *const deq) {
    1600             :   int i, q, quant;
    1601             : 
    1602           0 :   for (q = 0; q < QINDEX_RANGE; q++) {
    1603           0 :     const int qzbin_factor = get_qzbin_factor(q, bit_depth);
    1604           0 :     const int qrounding_factor = q == 0 ? 64 : 48;
    1605             : 
    1606           0 :     for (i = 0; i < 2; ++i) {
    1607           0 :       int qrounding_factor_fp = 64;
    1608             :       // y
    1609           0 :       quant = i == 0 ? av1_dc_quant(q, y_dc_delta_q, bit_depth)
    1610           0 :                      : av1_ac_quant(q, 0, bit_depth);
    1611           0 :       invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
    1612           0 :       quants->y_quant_fp[q][i] = (1 << 16) / quant;
    1613           0 :       quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
    1614           0 :       quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
    1615           0 :       quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
    1616           0 :       deq->y_dequant[q][i] = quant;
    1617             : 
    1618             :       // uv
    1619           0 :       quant = i == 0 ? av1_dc_quant(q, uv_dc_delta_q, bit_depth)
    1620           0 :                      : av1_ac_quant(q, uv_ac_delta_q, bit_depth);
    1621           0 :       invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
    1622             :                    quant);
    1623           0 :       quants->uv_quant_fp[q][i] = (1 << 16) / quant;
    1624           0 :       quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
    1625           0 :       quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
    1626           0 :       quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
    1627           0 :       deq->uv_dequant[q][i] = quant;
    1628             :     }
    1629             : 
    1630             : #if CONFIG_NEW_QUANT
    1631             :     int dq;
    1632             :     for (dq = 0; dq < QUANT_PROFILES; dq++) {
    1633             :       for (i = 0; i < COEF_BANDS; i++) {
    1634             :         const int y_quant = deq->y_dequant[q][i != 0];
    1635             :         const int uvquant = deq->uv_dequant[q][i != 0];
    1636             :         av1_get_dequant_val_nuq(y_quant, i, deq->y_dequant_val_nuq[dq][q][i],
    1637             :                                 quants->y_cuml_bins_nuq[dq][q][i], dq);
    1638             :         av1_get_dequant_val_nuq(uvquant, i, deq->uv_dequant_val_nuq[dq][q][i],
    1639             :                                 quants->uv_cuml_bins_nuq[dq][q][i], dq);
    1640             :       }
    1641             :     }
    1642             : #endif  // CONFIG_NEW_QUANT
    1643             : 
    1644           0 :     for (i = 2; i < 8; i++) {  // 8: SIMD width
    1645           0 :       quants->y_quant[q][i] = quants->y_quant[q][1];
    1646           0 :       quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
    1647           0 :       quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
    1648           0 :       quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
    1649           0 :       quants->y_zbin[q][i] = quants->y_zbin[q][1];
    1650           0 :       quants->y_round[q][i] = quants->y_round[q][1];
    1651           0 :       deq->y_dequant[q][i] = deq->y_dequant[q][1];
    1652             : 
    1653           0 :       quants->uv_quant[q][i] = quants->uv_quant[q][1];
    1654           0 :       quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
    1655           0 :       quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
    1656           0 :       quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
    1657           0 :       quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
    1658           0 :       quants->uv_round[q][i] = quants->uv_round[q][1];
    1659           0 :       deq->uv_dequant[q][i] = deq->uv_dequant[q][1];
    1660             :     }
    1661             :   }
    1662           0 : }
    1663             : 
    1664           0 : void av1_init_quantizer(AV1_COMP *cpi) {
    1665           0 :   AV1_COMMON *const cm = &cpi->common;
    1666           0 :   QUANTS *const quants = &cpi->quants;
    1667           0 :   Dequants *const dequants = &cpi->dequants;
    1668           0 :   av1_build_quantizer(cm->bit_depth, cm->y_dc_delta_q, cm->uv_dc_delta_q,
    1669             :                       cm->uv_ac_delta_q, quants, dequants);
    1670           0 : }
    1671             : 
    1672           0 : void av1_init_plane_quantizers(const AV1_COMP *cpi, MACROBLOCK *x,
    1673             :                                int segment_id) {
    1674           0 :   const AV1_COMMON *const cm = &cpi->common;
    1675           0 :   MACROBLOCKD *const xd = &x->e_mbd;
    1676           0 :   const QUANTS *const quants = &cpi->quants;
    1677             : 
    1678             : #if CONFIG_DELTA_Q
    1679             : #if CONFIG_EXT_DELTA_Q
    1680           0 :   int current_q_index = AOMMAX(
    1681             :       0, AOMMIN(QINDEX_RANGE - 1, cpi->oxcf.deltaq_mode != NO_DELTA_Q
    1682             :                                       ? cm->base_qindex + xd->delta_qindex
    1683             :                                       : cm->base_qindex));
    1684             : #else
    1685             :   int current_q_index = AOMMAX(
    1686             :       0, AOMMIN(QINDEX_RANGE - 1, cm->delta_q_present_flag
    1687             :                                       ? cm->base_qindex + xd->delta_qindex
    1688             :                                       : cm->base_qindex));
    1689             : #endif
    1690           0 :   const int qindex = av1_get_qindex(&cm->seg, segment_id, current_q_index);
    1691             : #else
    1692             :   const int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
    1693             : #endif
    1694           0 :   const int rdmult = av1_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
    1695             :   int i;
    1696             : #if CONFIG_AOM_QM
    1697             :   int minqm = cm->min_qmlevel;
    1698             :   int maxqm = cm->max_qmlevel;
    1699             :   // Quant matrix only depends on the base QP so there is only one set per frame
    1700             :   int qmlevel = (xd->lossless[segment_id] || cm->using_qmatrix == 0)
    1701             :                     ? NUM_QM_LEVELS - 1
    1702             :                     : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
    1703             : #endif
    1704             : #if CONFIG_NEW_QUANT
    1705             :   int dq;
    1706             : #endif
    1707             : 
    1708             :   // Y
    1709           0 :   x->plane[0].quant = quants->y_quant[qindex];
    1710           0 :   x->plane[0].quant_fp = quants->y_quant_fp[qindex];
    1711           0 :   x->plane[0].round_fp = quants->y_round_fp[qindex];
    1712           0 :   x->plane[0].quant_shift = quants->y_quant_shift[qindex];
    1713           0 :   x->plane[0].zbin = quants->y_zbin[qindex];
    1714           0 :   x->plane[0].round = quants->y_round[qindex];
    1715             : #if CONFIG_AOM_QM
    1716             :   memcpy(&xd->plane[0].seg_qmatrix[segment_id], cm->gqmatrix[qmlevel][0],
    1717             :          sizeof(cm->gqmatrix[qmlevel][0]));
    1718             :   memcpy(&xd->plane[0].seg_iqmatrix[segment_id], cm->giqmatrix[qmlevel][0],
    1719             :          sizeof(cm->giqmatrix[qmlevel][0]));
    1720             : #endif
    1721           0 :   xd->plane[0].dequant = cpi->dequants.y_dequant[qindex];
    1722             : #if CONFIG_NEW_QUANT
    1723             :   for (dq = 0; dq < QUANT_PROFILES; dq++) {
    1724             :     x->plane[0].cuml_bins_nuq[dq] = quants->y_cuml_bins_nuq[dq][qindex];
    1725             :     xd->plane[0].dequant_val_nuq[dq] =
    1726             :         cpi->dequants.y_dequant_val_nuq[dq][qindex];
    1727             :   }
    1728             : #endif  // CONFIG_NEW_QUANT
    1729             : 
    1730             :   // UV
    1731           0 :   for (i = 1; i < 3; i++) {
    1732           0 :     x->plane[i].quant = quants->uv_quant[qindex];
    1733           0 :     x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
    1734           0 :     x->plane[i].round_fp = quants->uv_round_fp[qindex];
    1735           0 :     x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
    1736           0 :     x->plane[i].zbin = quants->uv_zbin[qindex];
    1737           0 :     x->plane[i].round = quants->uv_round[qindex];
    1738             : #if CONFIG_AOM_QM
    1739             :     memcpy(&xd->plane[i].seg_qmatrix[segment_id], cm->gqmatrix[qmlevel][1],
    1740             :            sizeof(cm->gqmatrix[qmlevel][1]));
    1741             :     memcpy(&xd->plane[i].seg_iqmatrix[segment_id], cm->giqmatrix[qmlevel][1],
    1742             :            sizeof(cm->giqmatrix[qmlevel][1]));
    1743             : #endif
    1744           0 :     xd->plane[i].dequant = cpi->dequants.uv_dequant[qindex];
    1745             : #if CONFIG_NEW_QUANT
    1746             :     for (dq = 0; dq < QUANT_PROFILES; dq++) {
    1747             :       x->plane[i].cuml_bins_nuq[dq] = quants->uv_cuml_bins_nuq[dq][qindex];
    1748             :       xd->plane[i].dequant_val_nuq[dq] =
    1749             :           cpi->dequants.uv_dequant_val_nuq[dq][qindex];
    1750             :     }
    1751             : #endif  // CONFIG_NEW_QUANT
    1752             :   }
    1753             : 
    1754           0 :   x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
    1755           0 :   x->qindex = qindex;
    1756             : 
    1757           0 :   set_error_per_bit(x, rdmult);
    1758             : 
    1759           0 :   av1_initialize_me_consts(cpi, x, qindex);
    1760           0 : }
    1761             : 
    1762           0 : void av1_frame_init_quantizer(AV1_COMP *cpi) {
    1763           0 :   MACROBLOCK *const x = &cpi->td.mb;
    1764           0 :   MACROBLOCKD *const xd = &x->e_mbd;
    1765           0 :   av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
    1766           0 : }
    1767             : 
    1768           0 : void av1_set_quantizer(AV1_COMMON *cm, int q) {
    1769             :   // quantizer has to be reinitialized with av1_init_quantizer() if any
    1770             :   // delta_q changes.
    1771           0 :   cm->base_qindex = q;
    1772           0 :   cm->y_dc_delta_q = 0;
    1773           0 :   cm->uv_dc_delta_q = 0;
    1774           0 :   cm->uv_ac_delta_q = 0;
    1775           0 : }
    1776             : 
    1777             : // Table that converts 0-63 Q-range values passed in outside to the Qindex
    1778             : // range used internally.
    1779             : static const int quantizer_to_qindex[] = {
    1780             :   0,   4,   8,   12,  16,  20,  24,  28,  32,  36,  40,  44,  48,
    1781             :   52,  56,  60,  64,  68,  72,  76,  80,  84,  88,  92,  96,  100,
    1782             :   104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
    1783             :   156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
    1784             :   208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
    1785             : };
    1786             : 
    1787           0 : int av1_quantizer_to_qindex(int quantizer) {
    1788           0 :   return quantizer_to_qindex[quantizer];
    1789             : }
    1790             : 
    1791           0 : int av1_qindex_to_quantizer(int qindex) {
    1792             :   int quantizer;
    1793             : 
    1794           0 :   for (quantizer = 0; quantizer < 64; ++quantizer)
    1795           0 :     if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
    1796             : 
    1797           0 :   return 63;
    1798             : }

Generated by: LCOV version 1.13