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