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