Coverage Report

Created: 2026-02-26 06:26

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