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 
14 #include "config/aom_dsp_rtcd.h"
15 
16 #include "aom_dsp/quantize.h"
17 #include "aom_mem/aom_mem.h"
18 #include "aom_ports/mem.h"
19 
20 #include "av1/common/idct.h"
21 #include "av1/common/quant_common.h"
22 #include "av1/common/scan.h"
23 #include "av1/common/seg_common.h"
24 
25 #include "av1/encoder/av1_quantize.h"
26 #include "av1/encoder/encoder.h"
27 #include "av1/encoder/rd.h"
28 
av1_quantize_skip(intptr_t n_coeffs,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,uint16_t * eob_ptr)29 void av1_quantize_skip(intptr_t n_coeffs, tran_low_t *qcoeff_ptr,
30                        tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
31   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
32   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
33   *eob_ptr = 0;
34 }
35 
quantize_fp_helper_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan,const qm_val_t * qm_ptr,const qm_val_t * iqm_ptr,int log_scale)36 static void quantize_fp_helper_c(
37     const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr,
38     const int16_t *round_ptr, const int16_t *quant_ptr,
39     const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
40     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
41     const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr,
42     const qm_val_t *iqm_ptr, int log_scale) {
43   int i, eob = -1;
44   // TODO(jingning) Decide the need of these arguments after the
45   // quantization process is completed.
46   (void)zbin_ptr;
47   (void)quant_shift_ptr;
48 
49   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
50   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
51 
52   if (qm_ptr == NULL && iqm_ptr == NULL) {
53     const int rounding0 = ROUND_POWER_OF_TWO(round_ptr[0], log_scale);
54     {  // rc == 0
55       const int coeff = coeff_ptr[0];
56       const int coeff_sign = (coeff >> 31);
57       int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
58       if ((abs_coeff << (1 + log_scale)) >= (int32_t)(dequant_ptr[0])) {
59         abs_coeff = clamp64(abs_coeff + rounding0, INT16_MIN, INT16_MAX);
60         const int tmp32 = (int)((abs_coeff * quant_ptr[0]) >> (16 - log_scale));
61         if (tmp32) {
62           qcoeff_ptr[0] = (tmp32 ^ coeff_sign) - coeff_sign;
63           const tran_low_t abs_dqcoeff = (tmp32 * dequant_ptr[0]) >> log_scale;
64           dqcoeff_ptr[0] = (abs_dqcoeff ^ coeff_sign) - coeff_sign;
65           eob = 0;
66         }
67       }
68     }
69     const int rounding1 = ROUND_POWER_OF_TWO(round_ptr[1], log_scale);
70     const int32_t thresh1 = (int32_t)(dequant_ptr[1]);
71     for (i = 1; i < n_coeffs; i++) {
72       const int coeff = coeff_ptr[i];
73       const int coeff_sign = (coeff >> 31);
74       int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
75       if ((abs_coeff << (1 + log_scale)) >= thresh1) {
76         abs_coeff = clamp64(abs_coeff + rounding1, INT16_MIN, INT16_MAX);
77         const int tmp32 = (int)((abs_coeff * quant_ptr[1]) >> (16 - log_scale));
78         if (tmp32) {
79           qcoeff_ptr[i] = (tmp32 ^ coeff_sign) - coeff_sign;
80           const tran_low_t abs_dqcoeff = (tmp32 * dequant_ptr[1]) >> log_scale;
81           dqcoeff_ptr[i] = (abs_dqcoeff ^ coeff_sign) - coeff_sign;
82           eob = AOMMAX(iscan[i], eob);
83         }
84       }
85     }
86   } else {
87     // Quantization pass: All coefficients with index >= zero_flag are
88     // skippable. Note: zero_flag can be zero.
89     for (i = 0; i < n_coeffs; i++) {
90       const int rc = scan[i];
91       const int coeff = coeff_ptr[rc];
92       const qm_val_t wt = qm_ptr ? qm_ptr[rc] : (1 << AOM_QM_BITS);
93       const qm_val_t iwt = iqm_ptr ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
94       const int dequant =
95           (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
96           AOM_QM_BITS;
97       const int coeff_sign = (coeff >> 31);
98       int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
99       int tmp32 = 0;
100       if (abs_coeff * wt >=
101           (dequant_ptr[rc != 0] << (AOM_QM_BITS - (1 + log_scale)))) {
102         abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale);
103         abs_coeff = clamp64(abs_coeff, INT16_MIN, INT16_MAX);
104         tmp32 = (int)((abs_coeff * wt * quant_ptr[rc != 0]) >>
105                       (16 - log_scale + AOM_QM_BITS));
106         qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
107         const tran_low_t abs_dqcoeff = (tmp32 * dequant) >> log_scale;
108         dqcoeff_ptr[rc] = (abs_dqcoeff ^ coeff_sign) - coeff_sign;
109       }
110 
111       if (tmp32) eob = i;
112     }
113   }
114   *eob_ptr = eob + 1;
115 }
116 
highbd_quantize_fp_helper_c(const tran_low_t * coeff_ptr,intptr_t count,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan,const qm_val_t * qm_ptr,const qm_val_t * iqm_ptr,int log_scale)117 static void highbd_quantize_fp_helper_c(
118     const tran_low_t *coeff_ptr, intptr_t count, const int16_t *zbin_ptr,
119     const int16_t *round_ptr, const int16_t *quant_ptr,
120     const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
121     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
122     const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr,
123     const qm_val_t *iqm_ptr, int log_scale) {
124   int i;
125   int eob = -1;
126   const int shift = 16 - log_scale;
127   // TODO(jingning) Decide the need of these arguments after the
128   // quantization process is completed.
129   (void)zbin_ptr;
130   (void)quant_shift_ptr;
131   (void)iscan;
132 
133   if (qm_ptr || iqm_ptr) {
134     // Quantization pass: All coefficients with index >= zero_flag are
135     // skippable. Note: zero_flag can be zero.
136     for (i = 0; i < count; i++) {
137       const int rc = scan[i];
138       const int coeff = coeff_ptr[rc];
139       const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
140       const qm_val_t iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
141       const int dequant =
142           (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
143           AOM_QM_BITS;
144       const int coeff_sign = (coeff >> 31);
145       const int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
146       int abs_qcoeff = 0;
147       if (abs_coeff * wt >=
148           (dequant_ptr[rc != 0] << (AOM_QM_BITS - (1 + log_scale)))) {
149         const int64_t tmp =
150             abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale);
151         abs_qcoeff =
152             (int)((tmp * quant_ptr[rc != 0] * wt) >> (shift + AOM_QM_BITS));
153         qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
154         const tran_low_t abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale;
155         dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
156         if (abs_qcoeff) eob = i;
157       } else {
158         qcoeff_ptr[rc] = 0;
159         dqcoeff_ptr[rc] = 0;
160       }
161     }
162   } else {
163     const int log_scaled_round_arr[2] = {
164       ROUND_POWER_OF_TWO(round_ptr[0], log_scale),
165       ROUND_POWER_OF_TWO(round_ptr[1], log_scale),
166     };
167     for (i = 0; i < count; i++) {
168       const int rc = scan[i];
169       const int coeff = coeff_ptr[rc];
170       const int rc01 = (rc != 0);
171       const int coeff_sign = (coeff >> 31);
172       const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
173       const int log_scaled_round = log_scaled_round_arr[rc01];
174       if ((abs_coeff << (1 + log_scale)) >= dequant_ptr[rc01]) {
175         const int quant = quant_ptr[rc01];
176         const int dequant = dequant_ptr[rc01];
177         const int64_t tmp = (int64_t)abs_coeff + log_scaled_round;
178         const int abs_qcoeff = (int)((tmp * quant) >> shift);
179         qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
180         const tran_low_t abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale;
181         if (abs_qcoeff) eob = i;
182         dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
183       } else {
184         qcoeff_ptr[rc] = 0;
185         dqcoeff_ptr[rc] = 0;
186       }
187     }
188   }
189   *eob_ptr = eob + 1;
190 }
191 
av1_quantize_fp_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)192 void av1_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
193                        const int16_t *zbin_ptr, const int16_t *round_ptr,
194                        const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
195                        tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
196                        const int16_t *dequant_ptr, uint16_t *eob_ptr,
197                        const int16_t *scan, const int16_t *iscan) {
198   quantize_fp_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr,
199                        quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr,
200                        eob_ptr, scan, iscan, NULL, NULL, 0);
201 }
202 
av1_quantize_fp_32x32_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)203 void av1_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
204                              const int16_t *zbin_ptr, const int16_t *round_ptr,
205                              const int16_t *quant_ptr,
206                              const int16_t *quant_shift_ptr,
207                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
208                              const int16_t *dequant_ptr, uint16_t *eob_ptr,
209                              const int16_t *scan, const int16_t *iscan) {
210   quantize_fp_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr,
211                        quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr,
212                        eob_ptr, scan, iscan, NULL, NULL, 1);
213 }
214 
av1_quantize_fp_64x64_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)215 void av1_quantize_fp_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
216                              const int16_t *zbin_ptr, const int16_t *round_ptr,
217                              const int16_t *quant_ptr,
218                              const int16_t *quant_shift_ptr,
219                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
220                              const int16_t *dequant_ptr, uint16_t *eob_ptr,
221                              const int16_t *scan, const int16_t *iscan) {
222   quantize_fp_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr,
223                        quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr,
224                        eob_ptr, scan, iscan, NULL, NULL, 2);
225 }
226 
av1_quantize_fp_facade(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const MACROBLOCK_PLANE * p,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,uint16_t * eob_ptr,const SCAN_ORDER * sc,const QUANT_PARAM * qparam)227 void av1_quantize_fp_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
228                             const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
229                             tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
230                             const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
231   const qm_val_t *qm_ptr = qparam->qmatrix;
232   const qm_val_t *iqm_ptr = qparam->iqmatrix;
233   if (qm_ptr != NULL && iqm_ptr != NULL) {
234     quantize_fp_helper_c(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX,
235                          p->quant_fp_QTX, p->quant_shift_QTX, qcoeff_ptr,
236                          dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
237                          sc->iscan, qm_ptr, iqm_ptr, qparam->log_scale);
238   } else {
239     switch (qparam->log_scale) {
240       case 0:
241         if (n_coeffs < 16) {
242           // TODO(jingning): Need SIMD implementation for smaller block size
243           // quantization.
244           quantize_fp_helper_c(
245               coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX,
246               p->quant_fp_QTX, p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
247               p->dequant_QTX, eob_ptr, sc->scan, sc->iscan, NULL, NULL, 0);
248         } else {
249           av1_quantize_fp(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX,
250                           p->quant_fp_QTX, p->quant_shift_QTX, qcoeff_ptr,
251                           dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
252                           sc->iscan);
253         }
254         break;
255       case 1:
256         av1_quantize_fp_32x32(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX,
257                               p->quant_fp_QTX, p->quant_shift_QTX, qcoeff_ptr,
258                               dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
259                               sc->iscan);
260         break;
261       case 2:
262         av1_quantize_fp_64x64(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX,
263                               p->quant_fp_QTX, p->quant_shift_QTX, qcoeff_ptr,
264                               dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
265                               sc->iscan);
266         break;
267       default: assert(0);
268     }
269   }
270 }
271 
av1_quantize_b_facade(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const MACROBLOCK_PLANE * p,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,uint16_t * eob_ptr,const SCAN_ORDER * sc,const QUANT_PARAM * qparam)272 void av1_quantize_b_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
273                            const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
274                            tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
275                            const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
276   const qm_val_t *qm_ptr = qparam->qmatrix;
277   const qm_val_t *iqm_ptr = qparam->iqmatrix;
278   if (qm_ptr != NULL && iqm_ptr != NULL) {
279     quantize_b_helper_c(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX,
280                         p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr,
281                         dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
282                         sc->iscan, qm_ptr, iqm_ptr, qparam->log_scale);
283   } else {
284     switch (qparam->log_scale) {
285       case 0:
286         aom_quantize_b(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX,
287                        p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr,
288                        dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
289                        sc->iscan);
290         break;
291       case 1:
292         aom_quantize_b_32x32(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX,
293                              p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr,
294                              dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
295                              sc->iscan);
296         break;
297       case 2:
298         aom_quantize_b_64x64(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX,
299                              p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr,
300                              dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
301                              sc->iscan);
302         break;
303       default: assert(0);
304     }
305   }
306 }
307 
quantize_dc(const tran_low_t * coeff_ptr,int n_coeffs,int skip_block,const int16_t * round_ptr,const int16_t quant,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t dequant_ptr,uint16_t * eob_ptr,const qm_val_t * qm_ptr,const qm_val_t * iqm_ptr,const int log_scale)308 static void quantize_dc(const tran_low_t *coeff_ptr, int n_coeffs,
309                         int skip_block, const int16_t *round_ptr,
310                         const int16_t quant, tran_low_t *qcoeff_ptr,
311                         tran_low_t *dqcoeff_ptr, const int16_t dequant_ptr,
312                         uint16_t *eob_ptr, const qm_val_t *qm_ptr,
313                         const qm_val_t *iqm_ptr, const int log_scale) {
314   const int rc = 0;
315   const int coeff = coeff_ptr[rc];
316   const int coeff_sign = (coeff >> 31);
317   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
318   int64_t tmp;
319   int eob = -1;
320   int32_t tmp32;
321   int dequant;
322 
323   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
324   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
325 
326   if (!skip_block) {
327     const int wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
328     const int iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
329     tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale),
330                 INT16_MIN, INT16_MAX);
331     tmp32 = (int32_t)((tmp * wt * quant) >> (16 - log_scale + AOM_QM_BITS));
332     qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
333     dequant = (dequant_ptr * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
334     const tran_low_t abs_dqcoeff = (tmp32 * dequant) >> log_scale;
335     dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
336     if (tmp32) eob = 0;
337   }
338   *eob_ptr = eob + 1;
339 }
340 
av1_quantize_dc_facade(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const MACROBLOCK_PLANE * p,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,uint16_t * eob_ptr,const SCAN_ORDER * sc,const QUANT_PARAM * qparam)341 void av1_quantize_dc_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
342                             const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
343                             tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
344                             const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
345   // obsolete skip_block
346   const int skip_block = 0;
347   (void)sc;
348   assert(qparam->log_scale >= 0 && qparam->log_scale < (3));
349   const qm_val_t *qm_ptr = qparam->qmatrix;
350   const qm_val_t *iqm_ptr = qparam->iqmatrix;
351   quantize_dc(coeff_ptr, (int)n_coeffs, skip_block, p->round_QTX,
352               p->quant_fp_QTX[0], qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX[0],
353               eob_ptr, qm_ptr, iqm_ptr, qparam->log_scale);
354 }
355 
av1_highbd_quantize_fp_facade(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const MACROBLOCK_PLANE * p,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,uint16_t * eob_ptr,const SCAN_ORDER * sc,const QUANT_PARAM * qparam)356 void av1_highbd_quantize_fp_facade(const tran_low_t *coeff_ptr,
357                                    intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
358                                    tran_low_t *qcoeff_ptr,
359                                    tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
360                                    const SCAN_ORDER *sc,
361                                    const QUANT_PARAM *qparam) {
362   const qm_val_t *qm_ptr = qparam->qmatrix;
363   const qm_val_t *iqm_ptr = qparam->iqmatrix;
364   if (qm_ptr != NULL && iqm_ptr != NULL) {
365     highbd_quantize_fp_helper_c(
366         coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX, p->quant_fp_QTX,
367         p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
368         sc->scan, sc->iscan, qm_ptr, iqm_ptr, qparam->log_scale);
369   } else {
370     if (n_coeffs < 16) {
371       // TODO(jingning): Need SIMD implementation for smaller block size
372       // quantization.
373       av1_highbd_quantize_fp_c(
374           coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX, p->quant_fp_QTX,
375           p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
376           sc->scan, sc->iscan, qparam->log_scale);
377       return;
378     }
379     av1_highbd_quantize_fp(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_fp_QTX,
380                            p->quant_fp_QTX, p->quant_shift_QTX, qcoeff_ptr,
381                            dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
382                            sc->iscan, qparam->log_scale);
383   }
384 }
385 
av1_highbd_quantize_b_facade(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const MACROBLOCK_PLANE * p,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,uint16_t * eob_ptr,const SCAN_ORDER * sc,const QUANT_PARAM * qparam)386 void av1_highbd_quantize_b_facade(const tran_low_t *coeff_ptr,
387                                   intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
388                                   tran_low_t *qcoeff_ptr,
389                                   tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
390                                   const SCAN_ORDER *sc,
391                                   const QUANT_PARAM *qparam) {
392   const qm_val_t *qm_ptr = qparam->qmatrix;
393   const qm_val_t *iqm_ptr = qparam->iqmatrix;
394   if (qm_ptr != NULL && iqm_ptr != NULL) {
395     highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX,
396                                p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr,
397                                dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
398                                sc->iscan, qm_ptr, iqm_ptr, qparam->log_scale);
399   } else {
400     switch (qparam->log_scale) {
401       case 0:
402         if (LIKELY(n_coeffs >= 8)) {
403           aom_highbd_quantize_b(coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX,
404                                 p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr,
405                                 dqcoeff_ptr, p->dequant_QTX, eob_ptr, sc->scan,
406                                 sc->iscan);
407         } else {
408           // TODO(luoyi): Need SIMD (e.g. sse2) for smaller block size
409           // quantization
410           aom_highbd_quantize_b_c(coeff_ptr, n_coeffs, p->zbin_QTX,
411                                   p->round_QTX, p->quant_QTX,
412                                   p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
413                                   p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
414         }
415         break;
416       case 1:
417         aom_highbd_quantize_b_32x32(
418             coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX, p->quant_QTX,
419             p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX,
420             eob_ptr, sc->scan, sc->iscan);
421         break;
422       case 2:
423         aom_highbd_quantize_b_64x64(
424             coeff_ptr, n_coeffs, p->zbin_QTX, p->round_QTX, p->quant_QTX,
425             p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX,
426             eob_ptr, sc->scan, sc->iscan);
427         break;
428       default: assert(0);
429     }
430   }
431 }
432 
highbd_quantize_dc(const tran_low_t * coeff_ptr,int n_coeffs,int skip_block,const int16_t * round_ptr,const int16_t quant,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t dequant_ptr,uint16_t * eob_ptr,const qm_val_t * qm_ptr,const qm_val_t * iqm_ptr,const int log_scale)433 static INLINE void highbd_quantize_dc(
434     const tran_low_t *coeff_ptr, int n_coeffs, int skip_block,
435     const int16_t *round_ptr, const int16_t quant, tran_low_t *qcoeff_ptr,
436     tran_low_t *dqcoeff_ptr, const int16_t dequant_ptr, uint16_t *eob_ptr,
437     const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr, const int log_scale) {
438   int eob = -1;
439 
440   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
441   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
442 
443   if (!skip_block) {
444     const qm_val_t wt = qm_ptr != NULL ? qm_ptr[0] : (1 << AOM_QM_BITS);
445     const qm_val_t iwt = iqm_ptr != NULL ? iqm_ptr[0] : (1 << AOM_QM_BITS);
446     const int coeff = coeff_ptr[0];
447     const int coeff_sign = (coeff >> 31);
448     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
449     const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[0], log_scale);
450     const int64_t tmpw = tmp * wt;
451     const int abs_qcoeff =
452         (int)((tmpw * quant) >> (16 - log_scale + AOM_QM_BITS));
453     qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
454     const int dequant =
455         (dequant_ptr * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
456 
457     const tran_low_t abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale;
458     dqcoeff_ptr[0] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
459     if (abs_qcoeff) eob = 0;
460   }
461   *eob_ptr = eob + 1;
462 }
463 
av1_highbd_quantize_dc_facade(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const MACROBLOCK_PLANE * p,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,uint16_t * eob_ptr,const SCAN_ORDER * sc,const QUANT_PARAM * qparam)464 void av1_highbd_quantize_dc_facade(const tran_low_t *coeff_ptr,
465                                    intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
466                                    tran_low_t *qcoeff_ptr,
467                                    tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
468                                    const SCAN_ORDER *sc,
469                                    const QUANT_PARAM *qparam) {
470   // obsolete skip_block
471   const int skip_block = 0;
472   const qm_val_t *qm_ptr = qparam->qmatrix;
473   const qm_val_t *iqm_ptr = qparam->iqmatrix;
474   (void)sc;
475 
476   highbd_quantize_dc(coeff_ptr, (int)n_coeffs, skip_block, p->round_QTX,
477                      p->quant_fp_QTX[0], qcoeff_ptr, dqcoeff_ptr,
478                      p->dequant_QTX[0], eob_ptr, qm_ptr, iqm_ptr,
479                      qparam->log_scale);
480 }
481 
av1_highbd_quantize_fp_c(const tran_low_t * coeff_ptr,intptr_t count,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan,int log_scale)482 void av1_highbd_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t count,
483                               const int16_t *zbin_ptr, const int16_t *round_ptr,
484                               const int16_t *quant_ptr,
485                               const int16_t *quant_shift_ptr,
486                               tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
487                               const int16_t *dequant_ptr, uint16_t *eob_ptr,
488                               const int16_t *scan, const int16_t *iscan,
489                               int log_scale) {
490   highbd_quantize_fp_helper_c(coeff_ptr, count, zbin_ptr, round_ptr, quant_ptr,
491                               quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
492                               dequant_ptr, eob_ptr, scan, iscan, NULL, NULL,
493                               log_scale);
494 }
495 
invert_quant(int16_t * quant,int16_t * shift,int d)496 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
497   uint32_t t;
498   int l, m;
499   t = d;
500   for (l = 0; t > 1; l++) t >>= 1;
501   m = 1 + (1 << (16 + l)) / d;
502   *quant = (int16_t)(m - (1 << 16));
503   *shift = 1 << (16 - l);
504 }
505 
get_qzbin_factor(int q,aom_bit_depth_t bit_depth)506 static int get_qzbin_factor(int q, aom_bit_depth_t bit_depth) {
507   const int quant = av1_dc_quant_Q3(q, 0, bit_depth);
508   switch (bit_depth) {
509     case AOM_BITS_8: return q == 0 ? 64 : (quant < 148 ? 84 : 80);
510     case AOM_BITS_10: return q == 0 ? 64 : (quant < 592 ? 84 : 80);
511     case AOM_BITS_12: return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
512     default:
513       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
514       return -1;
515   }
516 }
517 
av1_build_quantizer(aom_bit_depth_t bit_depth,int y_dc_delta_q,int u_dc_delta_q,int u_ac_delta_q,int v_dc_delta_q,int v_ac_delta_q,QUANTS * const quants,Dequants * const deq)518 void av1_build_quantizer(aom_bit_depth_t bit_depth, int y_dc_delta_q,
519                          int u_dc_delta_q, int u_ac_delta_q, int v_dc_delta_q,
520                          int v_ac_delta_q, QUANTS *const quants,
521                          Dequants *const deq) {
522   int i, q, quant_Q3, quant_QTX;
523 
524   for (q = 0; q < QINDEX_RANGE; q++) {
525     const int qzbin_factor = get_qzbin_factor(q, bit_depth);
526     const int qrounding_factor = q == 0 ? 64 : 48;
527 
528     for (i = 0; i < 2; ++i) {
529       int qrounding_factor_fp = 64;
530       // y quantizer setup with original coeff shift of Q3
531       quant_Q3 = i == 0 ? av1_dc_quant_Q3(q, y_dc_delta_q, bit_depth)
532                         : av1_ac_quant_Q3(q, 0, bit_depth);
533       // y quantizer with TX scale
534       quant_QTX = i == 0 ? av1_dc_quant_QTX(q, y_dc_delta_q, bit_depth)
535                          : av1_ac_quant_QTX(q, 0, bit_depth);
536       invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i],
537                    quant_QTX);
538       quants->y_quant_fp[q][i] = (1 << 16) / quant_QTX;
539       quants->y_round_fp[q][i] = (qrounding_factor_fp * quant_QTX) >> 7;
540       quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant_QTX, 7);
541       quants->y_round[q][i] = (qrounding_factor * quant_QTX) >> 7;
542       deq->y_dequant_QTX[q][i] = quant_QTX;
543       deq->y_dequant_Q3[q][i] = quant_Q3;
544 
545       // u quantizer setup with original coeff shift of Q3
546       quant_Q3 = i == 0 ? av1_dc_quant_Q3(q, u_dc_delta_q, bit_depth)
547                         : av1_ac_quant_Q3(q, u_ac_delta_q, bit_depth);
548       // u quantizer with TX scale
549       quant_QTX = i == 0 ? av1_dc_quant_QTX(q, u_dc_delta_q, bit_depth)
550                          : av1_ac_quant_QTX(q, u_ac_delta_q, bit_depth);
551       invert_quant(&quants->u_quant[q][i], &quants->u_quant_shift[q][i],
552                    quant_QTX);
553       quants->u_quant_fp[q][i] = (1 << 16) / quant_QTX;
554       quants->u_round_fp[q][i] = (qrounding_factor_fp * quant_QTX) >> 7;
555       quants->u_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant_QTX, 7);
556       quants->u_round[q][i] = (qrounding_factor * quant_QTX) >> 7;
557       deq->u_dequant_QTX[q][i] = quant_QTX;
558       deq->u_dequant_Q3[q][i] = quant_Q3;
559 
560       // v quantizer setup with original coeff shift of Q3
561       quant_Q3 = i == 0 ? av1_dc_quant_Q3(q, v_dc_delta_q, bit_depth)
562                         : av1_ac_quant_Q3(q, v_ac_delta_q, bit_depth);
563       // v quantizer with TX scale
564       quant_QTX = i == 0 ? av1_dc_quant_QTX(q, v_dc_delta_q, bit_depth)
565                          : av1_ac_quant_QTX(q, v_ac_delta_q, bit_depth);
566       invert_quant(&quants->v_quant[q][i], &quants->v_quant_shift[q][i],
567                    quant_QTX);
568       quants->v_quant_fp[q][i] = (1 << 16) / quant_QTX;
569       quants->v_round_fp[q][i] = (qrounding_factor_fp * quant_QTX) >> 7;
570       quants->v_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant_QTX, 7);
571       quants->v_round[q][i] = (qrounding_factor * quant_QTX) >> 7;
572       deq->v_dequant_QTX[q][i] = quant_QTX;
573       deq->v_dequant_Q3[q][i] = quant_Q3;
574     }
575 
576     for (i = 2; i < 8; i++) {  // 8: SIMD width
577       quants->y_quant[q][i] = quants->y_quant[q][1];
578       quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
579       quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
580       quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
581       quants->y_zbin[q][i] = quants->y_zbin[q][1];
582       quants->y_round[q][i] = quants->y_round[q][1];
583       deq->y_dequant_QTX[q][i] = deq->y_dequant_QTX[q][1];
584       deq->y_dequant_Q3[q][i] = deq->y_dequant_Q3[q][1];
585 
586       quants->u_quant[q][i] = quants->u_quant[q][1];
587       quants->u_quant_fp[q][i] = quants->u_quant_fp[q][1];
588       quants->u_round_fp[q][i] = quants->u_round_fp[q][1];
589       quants->u_quant_shift[q][i] = quants->u_quant_shift[q][1];
590       quants->u_zbin[q][i] = quants->u_zbin[q][1];
591       quants->u_round[q][i] = quants->u_round[q][1];
592       deq->u_dequant_QTX[q][i] = deq->u_dequant_QTX[q][1];
593       deq->u_dequant_Q3[q][i] = deq->u_dequant_Q3[q][1];
594       quants->v_quant[q][i] = quants->u_quant[q][1];
595       quants->v_quant_fp[q][i] = quants->v_quant_fp[q][1];
596       quants->v_round_fp[q][i] = quants->v_round_fp[q][1];
597       quants->v_quant_shift[q][i] = quants->v_quant_shift[q][1];
598       quants->v_zbin[q][i] = quants->v_zbin[q][1];
599       quants->v_round[q][i] = quants->v_round[q][1];
600       deq->v_dequant_QTX[q][i] = deq->v_dequant_QTX[q][1];
601       deq->v_dequant_Q3[q][i] = deq->v_dequant_Q3[q][1];
602     }
603   }
604 }
605 
av1_init_quantizer(AV1_COMP * cpi)606 void av1_init_quantizer(AV1_COMP *cpi) {
607   AV1_COMMON *const cm = &cpi->common;
608   QUANTS *const quants = &cpi->quants;
609   Dequants *const dequants = &cpi->dequants;
610   av1_build_quantizer(cm->seq_params.bit_depth, cm->y_dc_delta_q,
611                       cm->u_dc_delta_q, cm->u_ac_delta_q, cm->v_dc_delta_q,
612                       cm->v_ac_delta_q, quants, dequants);
613 }
614 
av1_init_plane_quantizers(const AV1_COMP * cpi,MACROBLOCK * x,int segment_id)615 void av1_init_plane_quantizers(const AV1_COMP *cpi, MACROBLOCK *x,
616                                int segment_id) {
617   const AV1_COMMON *const cm = &cpi->common;
618   MACROBLOCKD *const xd = &x->e_mbd;
619   const QUANTS *const quants = &cpi->quants;
620 
621   int current_qindex = AOMMAX(
622       0, AOMMIN(QINDEX_RANGE - 1, cpi->oxcf.deltaq_mode != NO_DELTA_Q
623                                       ? cm->base_qindex + xd->delta_qindex
624                                       : cm->base_qindex));
625   const int qindex = av1_get_qindex(&cm->seg, segment_id, current_qindex);
626   const int rdmult = av1_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
627   int qmlevel = (xd->lossless[segment_id] || cm->using_qmatrix == 0)
628                     ? NUM_QM_LEVELS - 1
629                     : cm->qm_y;
630 
631   // Y
632   x->plane[0].quant_QTX = quants->y_quant[qindex];
633   x->plane[0].quant_fp_QTX = quants->y_quant_fp[qindex];
634   x->plane[0].round_fp_QTX = quants->y_round_fp[qindex];
635   x->plane[0].quant_shift_QTX = quants->y_quant_shift[qindex];
636   x->plane[0].zbin_QTX = quants->y_zbin[qindex];
637   x->plane[0].round_QTX = quants->y_round[qindex];
638   x->plane[0].dequant_QTX = cpi->dequants.y_dequant_QTX[qindex];
639   memcpy(&xd->plane[0].seg_qmatrix[segment_id], cm->gqmatrix[qmlevel][0],
640          sizeof(cm->gqmatrix[qmlevel][0]));
641   memcpy(&xd->plane[0].seg_iqmatrix[segment_id], cm->giqmatrix[qmlevel][0],
642          sizeof(cm->giqmatrix[qmlevel][0]));
643   xd->plane[0].dequant_Q3 = cpi->dequants.y_dequant_Q3[qindex];
644 
645   // U
646   qmlevel = (xd->lossless[segment_id] || cm->using_qmatrix == 0)
647                 ? NUM_QM_LEVELS - 1
648                 : cm->qm_u;
649   {
650     x->plane[1].quant_QTX = quants->u_quant[qindex];
651     x->plane[1].quant_fp_QTX = quants->u_quant_fp[qindex];
652     x->plane[1].round_fp_QTX = quants->u_round_fp[qindex];
653     x->plane[1].quant_shift_QTX = quants->u_quant_shift[qindex];
654     x->plane[1].zbin_QTX = quants->u_zbin[qindex];
655     x->plane[1].round_QTX = quants->u_round[qindex];
656     x->plane[1].dequant_QTX = cpi->dequants.u_dequant_QTX[qindex];
657     memcpy(&xd->plane[1].seg_qmatrix[segment_id], cm->gqmatrix[qmlevel][1],
658            sizeof(cm->gqmatrix[qmlevel][1]));
659     memcpy(&xd->plane[1].seg_iqmatrix[segment_id], cm->giqmatrix[qmlevel][1],
660            sizeof(cm->giqmatrix[qmlevel][1]));
661     x->plane[1].dequant_QTX = cpi->dequants.u_dequant_QTX[qindex];
662     xd->plane[1].dequant_Q3 = cpi->dequants.u_dequant_Q3[qindex];
663   }
664   // V
665   qmlevel = (xd->lossless[segment_id] || cm->using_qmatrix == 0)
666                 ? NUM_QM_LEVELS - 1
667                 : cm->qm_v;
668   {
669     x->plane[2].quant_QTX = quants->v_quant[qindex];
670     x->plane[2].quant_fp_QTX = quants->v_quant_fp[qindex];
671     x->plane[2].round_fp_QTX = quants->v_round_fp[qindex];
672     x->plane[2].quant_shift_QTX = quants->v_quant_shift[qindex];
673     x->plane[2].zbin_QTX = quants->v_zbin[qindex];
674     x->plane[2].round_QTX = quants->v_round[qindex];
675     x->plane[2].dequant_QTX = cpi->dequants.v_dequant_QTX[qindex];
676     memcpy(&xd->plane[2].seg_qmatrix[segment_id], cm->gqmatrix[qmlevel][2],
677            sizeof(cm->gqmatrix[qmlevel][2]));
678     memcpy(&xd->plane[2].seg_iqmatrix[segment_id], cm->giqmatrix[qmlevel][2],
679            sizeof(cm->giqmatrix[qmlevel][2]));
680     x->plane[2].dequant_QTX = cpi->dequants.v_dequant_QTX[qindex];
681     xd->plane[2].dequant_Q3 = cpi->dequants.v_dequant_Q3[qindex];
682   }
683   x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
684   x->qindex = qindex;
685 
686   set_error_per_bit(x, rdmult);
687 
688   av1_initialize_me_consts(cpi, x, qindex);
689 }
690 
av1_frame_init_quantizer(AV1_COMP * cpi)691 void av1_frame_init_quantizer(AV1_COMP *cpi) {
692   MACROBLOCK *const x = &cpi->td.mb;
693   MACROBLOCKD *const xd = &x->e_mbd;
694   av1_init_plane_quantizers(cpi, x, xd->mi[0]->segment_id);
695 }
696 
av1_set_quantizer(AV1_COMMON * cm,int q)697 void av1_set_quantizer(AV1_COMMON *cm, int q) {
698   // quantizer has to be reinitialized with av1_init_quantizer() if any
699   // delta_q changes.
700   cm->base_qindex = AOMMAX(cm->delta_q_present_flag, q);
701   cm->y_dc_delta_q = 0;
702   cm->u_dc_delta_q = 0;
703   cm->u_ac_delta_q = 0;
704   cm->v_dc_delta_q = 0;
705   cm->v_ac_delta_q = 0;
706   cm->qm_y = aom_get_qmlevel(cm->base_qindex, cm->min_qmlevel, cm->max_qmlevel);
707   cm->qm_u = aom_get_qmlevel(cm->base_qindex + cm->u_ac_delta_q,
708                              cm->min_qmlevel, cm->max_qmlevel);
709 
710   if (!cm->seq_params.separate_uv_delta_q)
711     cm->qm_v = cm->qm_u;
712   else
713     cm->qm_v = aom_get_qmlevel(cm->base_qindex + cm->v_ac_delta_q,
714                                cm->min_qmlevel, cm->max_qmlevel);
715 }
716 
717 // Table that converts 0-63 Q-range values passed in outside to the Qindex
718 // range used internally.
719 static const int quantizer_to_qindex[] = {
720   0,   4,   8,   12,  16,  20,  24,  28,  32,  36,  40,  44,  48,
721   52,  56,  60,  64,  68,  72,  76,  80,  84,  88,  92,  96,  100,
722   104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
723   156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
724   208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
725 };
726 
av1_quantizer_to_qindex(int quantizer)727 int av1_quantizer_to_qindex(int quantizer) {
728   return quantizer_to_qindex[quantizer];
729 }
730 
av1_qindex_to_quantizer(int qindex)731 int av1_qindex_to_quantizer(int qindex) {
732   int quantizer;
733 
734   for (quantizer = 0; quantizer < 64; ++quantizer)
735     if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
736 
737   return 63;
738 }
739