av1_quantize.c 71.4 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
12
 */

#include <math.h>
Yaowu Xu's avatar
Yaowu Xu committed
13
#include "./aom_dsp_rtcd.h"
14
#include "aom_dsp/quantize.h"
Yaowu Xu's avatar
Yaowu Xu committed
15
#include "aom_mem/aom_mem.h"
16
#include "aom_ports/mem.h"
Jingning Han's avatar
Jingning Han committed
17

18
#include "av1/common/idct.h"
19
20
21
#include "av1/common/quant_common.h"
#include "av1/common/scan.h"
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
22

23
#include "av1/encoder/av1_quantize.h"
24
25
#include "av1/encoder/encoder.h"
#include "av1/encoder/rd.h"
Jingning Han's avatar
Jingning Han committed
26

27
#if CONFIG_NEW_QUANT
28
29
30
31
32
static INLINE int quantize_coeff_nuq(
    const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
    const int16_t dequant, const tran_low_t *cuml_bins_ptr,
    const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
    tran_low_t *dqcoeff_ptr) {
33
34
35
36
37
38
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
39
    if (tmp <= cuml_bins_ptr[i]) {
40
41
42
43
44
45
46
47
48
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
    tmp -= cuml_bins_ptr[NUQ_KNOTS - 1];
    q = NUQ_KNOTS + (((((tmp * quant) >> 16) + tmp) * quant_shift) >> 16);
  }
  if (q) {
Yaowu Xu's avatar
Yaowu Xu committed
49
    *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
50
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
51
52
53
54
55
56
57
58
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

59
60
61
62
static INLINE int quantize_coeff_bigtx_nuq(
    const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
    const int16_t dequant, const tran_low_t *cuml_bins_ptr,
    const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
63
    tran_low_t *dqcoeff_ptr, int logsizeby16) {
64
65
66
67
68
69
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
70
    if (tmp <= ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
71
72
73
74
75
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
76
    tmp -= ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16);
77
    q = NUQ_KNOTS +
78
        (((((tmp * quant) >> 16) + tmp) * quant_shift) >> (16 - logsizeby16));
79
80
  }
  if (q) {
81
    *dqcoeff_ptr = ROUND_POWER_OF_TWO(
82
        av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
Yaowu Xu's avatar
Yaowu Xu committed
83
    // *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val) >>
84
    // (logsizeby16);
85
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
86
87
88
89
90
91
92
93
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

94
95
96
97
static INLINE int quantize_coeff_fp_nuq(
    const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
    const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
    tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr) {
98
99
100
101
102
103
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
104
    if (tmp <= cuml_bins_ptr[i]) {
105
106
107
108
109
110
111
112
113
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
    q = NUQ_KNOTS +
        ((((int64_t)tmp - cuml_bins_ptr[NUQ_KNOTS - 1]) * quant) >> 16);
  }
  if (q) {
Yaowu Xu's avatar
Yaowu Xu committed
114
    *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
115
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
116
117
118
119
120
121
122
123
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

124
125
126
static INLINE int quantize_coeff_bigtx_fp_nuq(
    const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
    const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
127
    tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, int logsizeby16) {
128
129
130
131
132
133
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
134
    if (tmp <= ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
135
136
137
138
139
140
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
    q = NUQ_KNOTS +
141
        ((((int64_t)tmp -
142
           ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16)) *
143
          quant) >>
144
         (16 - logsizeby16));
145
146
  }
  if (q) {
147
    *dqcoeff_ptr = ROUND_POWER_OF_TWO(
148
        av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
Yaowu Xu's avatar
Yaowu Xu committed
149
    // *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val) >>
150
    // (logsizeby16);
151
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
152
153
154
155
156
157
158
159
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

160
161
162
void quantize_dc_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                     int skip_block, const int16_t quant,
                     const int16_t quant_shift, const int16_t dequant,
163
                     const tran_low_t *cuml_bins_ptr,
164
165
                     const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
                     tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
166
167
168
169
170
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    const int rc = 0;
171
172
    if (quantize_coeff_nuq(coeff_ptr[rc], quant, quant_shift, dequant,
                           cuml_bins_ptr, dequant_val, qcoeff_ptr, dqcoeff_ptr))
173
174
175
176
177
      eob = 0;
  }
  *eob_ptr = eob + 1;
}

178
179
180
181
182
void quantize_dc_fp_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                        int skip_block, const int16_t quant,
                        const int16_t dequant, const tran_low_t *cuml_bins_ptr,
                        const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
                        tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
183
184
185
186
187
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    const int rc = 0;
188
189
    if (quantize_coeff_fp_nuq(coeff_ptr[rc], quant, dequant, cuml_bins_ptr,
                              dequant_val, qcoeff_ptr, dqcoeff_ptr))
190
191
192
193
194
      eob = 0;
  }
  *eob_ptr = eob + 1;
}

195
196
197
void quantize_dc_32x32_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                           int skip_block, const int16_t quant,
                           const int16_t quant_shift, const int16_t dequant,
198
199
                           const tran_low_t *cuml_bins_ptr,
                           const tran_low_t *dequant_val,
200
                           tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
201
202
203
204
205
206
                           uint16_t *eob_ptr) {
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    const int rc = 0;
207
208
    if (quantize_coeff_bigtx_nuq(coeff_ptr[rc], quant, quant_shift, dequant,
                                 cuml_bins_ptr, dequant_val, qcoeff_ptr,
209
                                 dqcoeff_ptr, av1_get_tx_scale(TX_32X32)))
210
211
212
213
214
      eob = 0;
  }
  *eob_ptr = eob + 1;
}

215
216
void quantize_dc_32x32_fp_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                              int skip_block, const int16_t quant,
217
218
219
                              const int16_t dequant,
                              const tran_low_t *cuml_bins_ptr,
                              const tran_low_t *dequant_val,
220
                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
221
222
223
224
225
226
                              uint16_t *eob_ptr) {
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    const int rc = 0;
227
228
    if (quantize_coeff_bigtx_fp_nuq(coeff_ptr[rc], quant, dequant,
                                    cuml_bins_ptr, dequant_val, qcoeff_ptr,
229
                                    dqcoeff_ptr, av1_get_tx_scale(TX_32X32)))
230
231
232
233
234
      eob = 0;
  }
  *eob_ptr = eob + 1;
}

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#if CONFIG_TX64X64
void quantize_dc_64x64_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                           int skip_block, const int16_t quant,
                           const int16_t quant_shift, const int16_t dequant,
                           const tran_low_t *cuml_bins_ptr,
                           const tran_low_t *dequant_val,
                           tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
                           uint16_t *eob_ptr) {
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    const int rc = 0;
    if (quantize_coeff_bigtx_nuq(coeff_ptr[rc], quant, quant_shift, dequant,
                                 cuml_bins_ptr, dequant_val, qcoeff_ptr,
250
                                 dqcoeff_ptr, av1_get_tx_scale(TX_64X64)))
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
      eob = 0;
  }
  *eob_ptr = eob + 1;
}

void quantize_dc_64x64_fp_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                              int skip_block, const int16_t quant,
                              const int16_t dequant,
                              const tran_low_t *cuml_bins_ptr,
                              const tran_low_t *dequant_val,
                              tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
                              uint16_t *eob_ptr) {
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    const int rc = 0;
    if (quantize_coeff_bigtx_fp_nuq(coeff_ptr[rc], quant, dequant,
                                    cuml_bins_ptr, dequant_val, qcoeff_ptr,
270
                                    dqcoeff_ptr, av1_get_tx_scale(TX_64X64)))
271
272
273
274
275
276
      eob = 0;
  }
  *eob_ptr = eob + 1;
}
#endif  // CONFIG_TX64X64

277
278
279
void quantize_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                    int skip_block, const int16_t *quant_ptr,
                    const int16_t *quant_shift_ptr, const int16_t *dequant_ptr,
280
281
                    const cuml_bins_type_nuq *cuml_bins_ptr,
                    const dequant_val_type_nuq *dequant_val,
282
                    tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
283
                    uint16_t *eob_ptr, const int16_t *scan) {
284
285
286
287
288
289
290
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    int i;
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
291
292
      if (quantize_coeff_nuq(coeff_ptr[rc], quant_ptr[rc != 0],
                             quant_shift_ptr[rc != 0], dequant_ptr[rc != 0],
293
                             cuml_bins_ptr[rc != 0], dequant_val[rc != 0],
294
                             &qcoeff_ptr[rc], &dqcoeff_ptr[rc]))
295
296
297
298
299
300
        eob = i;
    }
  }
  *eob_ptr = eob + 1;
}

301
302
void quantize_fp_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                       int skip_block, const int16_t *quant_ptr,
303
304
305
                       const int16_t *dequant_ptr,
                       const cuml_bins_type_nuq *cuml_bins_ptr,
                       const dequant_val_type_nuq *dequant_val,
306
                       tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
307
                       uint16_t *eob_ptr, const int16_t *scan) {
308
309
310
311
312
313
314
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    int i;
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
315
      if (quantize_coeff_fp_nuq(coeff_ptr[rc], quant_ptr[rc != 0],
316
317
                                dequant_ptr[rc != 0], cuml_bins_ptr[rc != 0],
                                dequant_val[rc != 0], &qcoeff_ptr[rc],
318
319
320
321
322
323
324
                                &dqcoeff_ptr[rc]))
        eob = i;
    }
  }
  *eob_ptr = eob + 1;
}

325
326
void quantize_32x32_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                          int skip_block, const int16_t *quant_ptr,
327
328
329
330
                          const int16_t *quant_shift_ptr,
                          const int16_t *dequant_ptr,
                          const cuml_bins_type_nuq *cuml_bins_ptr,
                          const dequant_val_type_nuq *dequant_val,
331
                          tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
332
                          uint16_t *eob_ptr, const int16_t *scan) {
333
334
335
336
337
338
339
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    int i;
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
340
341
      if (quantize_coeff_bigtx_nuq(
              coeff_ptr[rc], quant_ptr[rc != 0], quant_shift_ptr[rc != 0],
342
343
              dequant_ptr[rc != 0], cuml_bins_ptr[rc != 0],
              dequant_val[rc != 0], &qcoeff_ptr[rc], &dqcoeff_ptr[rc],
344
              av1_get_tx_scale(TX_32X32)))
345
346
347
348
349
350
        eob = i;
    }
  }
  *eob_ptr = eob + 1;
}

351
352
void quantize_32x32_fp_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                             int skip_block, const int16_t *quant_ptr,
353
354
355
                             const int16_t *dequant_ptr,
                             const cuml_bins_type_nuq *cuml_bins_ptr,
                             const dequant_val_type_nuq *dequant_val,
356
                             tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
357
                             uint16_t *eob_ptr, const int16_t *scan) {
358
359
360
361
362
363
364
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    int i;
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
365
366
      if (quantize_coeff_bigtx_fp_nuq(
              coeff_ptr[rc], quant_ptr[rc != 0], dequant_ptr[rc != 0],
367
              cuml_bins_ptr[rc != 0], dequant_val[rc != 0], &qcoeff_ptr[rc],
368
              &dqcoeff_ptr[rc], av1_get_tx_scale(TX_32X32)))
369
370
371
372
373
        eob = i;
    }
  }
  *eob_ptr = eob + 1;
}
374
375
376
377
378
379
380
381
382

#if CONFIG_TX64X64
void quantize_64x64_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                          int skip_block, const int16_t *quant_ptr,
                          const int16_t *quant_shift_ptr,
                          const int16_t *dequant_ptr,
                          const cuml_bins_type_nuq *cuml_bins_ptr,
                          const dequant_val_type_nuq *dequant_val,
                          tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
383
                          uint16_t *eob_ptr, const int16_t *scan) {
384
385
386
387
388
389
390
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    int i;
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
391
392
      if (quantize_coeff_bigtx_nuq(
              coeff_ptr[rc], quant_ptr[rc != 0], quant_shift_ptr[rc != 0],
393
394
              dequant_ptr[rc != 0], cuml_bins_ptr[rc != 0],
              dequant_val[rc != 0], &qcoeff_ptr[rc], &dqcoeff_ptr[rc],
395
              av1_get_tx_scale(TX_64X64)))
396
397
398
399
400
401
402
403
404
405
406
407
        eob = i;
    }
  }
  *eob_ptr = eob + 1;
}

void quantize_64x64_fp_nuq_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                             int skip_block, const int16_t *quant_ptr,
                             const int16_t *dequant_ptr,
                             const cuml_bins_type_nuq *cuml_bins_ptr,
                             const dequant_val_type_nuq *dequant_val,
                             tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
408
                             uint16_t *eob_ptr, const int16_t *scan) {
409
410
411
412
413
414
415
416
417
  int eob = -1;
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  if (!skip_block) {
    int i;
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
      if (quantize_coeff_bigtx_fp_nuq(
              coeff_ptr[rc], quant_ptr[rc != 0], dequant_ptr[rc != 0],
418
              cuml_bins_ptr[rc != 0], dequant_val[rc != 0], &qcoeff_ptr[rc],
419
              &dqcoeff_ptr[rc], av1_get_tx_scale(TX_64X64)))
420
421
422
423
424
425
        eob = i;
    }
  }
  *eob_ptr = eob + 1;
}
#endif  // CONFIG_TX64X64
426
427
#endif  // CONFIG_NEW_QUANT

Yaowu Xu's avatar
Yaowu Xu committed
428
429
void av1_quantize_skip(intptr_t n_coeffs, tran_low_t *qcoeff_ptr,
                       tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr) {
Angie Chiang's avatar
Angie Chiang committed
430
431
432
433
434
  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  *eob_ptr = 0;
}

435
436
437
438
439
static void quantize_fp_helper_c(
    const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
    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,
Thomas Davies's avatar
Thomas Davies committed
440
441
    const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr,
    const qm_val_t *iqm_ptr, int log_scale) {
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  int i, eob = -1;
  // TODO(jingning) Decide the need of these arguments after the
  // quantization process is completed.
  (void)zbin_ptr;
  (void)quant_shift_ptr;
  (void)iscan;

  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));

  if (!skip_block) {
    // Quantization pass: All coefficients with index >= zero_flag are
    // skippable. Note: zero_flag can be zero.
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
      const int coeff = coeff_ptr[rc];
Thomas Davies's avatar
Thomas Davies committed
458
459
      const qm_val_t wt = qm_ptr ? qm_ptr[rc] : (1 << AOM_QM_BITS);
      const qm_val_t iwt = iqm_ptr ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
460
461
462
463
      const int dequant =
          (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
          AOM_QM_BITS;
      const int coeff_sign = (coeff >> 31);
Thomas Davies's avatar
Thomas Davies committed
464
      int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
465
466
467
468
      int tmp32 = 0;
      if (abs_coeff * wt >=
          (dequant_ptr[rc != 0] << (AOM_QM_BITS - (1 + log_scale)))) {
        abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale);
Thomas Davies's avatar
Thomas Davies committed
469
        abs_coeff = clamp64(abs_coeff, INT16_MIN, INT16_MAX);
470
        tmp32 = (int)((abs_coeff * wt * quant_ptr[rc != 0]) >>
Thomas Davies's avatar
Thomas Davies committed
471
                      (16 - log_scale + AOM_QM_BITS));
472
473
474
475
476
477
478
479
480
481
        qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
        dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant / (1 << log_scale);
      }

      if (tmp32) eob = i;
    }
  }
  *eob_ptr = eob + 1;
}

Thomas Davies's avatar
Thomas Davies committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
static void highbd_quantize_fp_helper_c(
    const tran_low_t *coeff_ptr, intptr_t count, int skip_block,
    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) {
  int i;
  int eob = -1;
  const int scale = 1 << log_scale;
  const int shift = 16 - log_scale;
  // TODO(jingning) Decide the need of these arguments after the
  // quantization process is completed.
  (void)zbin_ptr;
  (void)quant_shift_ptr;
  (void)iscan;

  memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr));

  if (!skip_block) {
    // Quantization pass: All coefficients with index >= zero_flag are
    // skippable. Note: zero_flag can be zero.
    for (i = 0; i < count; i++) {
      const int rc = scan[i];
      const int coeff = coeff_ptr[rc];
      const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
      const qm_val_t iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
      const int dequant =
          (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
          AOM_QM_BITS;
      const int coeff_sign = (coeff >> 31);
      const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
      const int64_t tmp = abs_coeff + (round_ptr[rc != 0] >> log_scale);
      const int abs_qcoeff =
          (int)((tmp * quant_ptr[rc != 0] * wt) >> (shift + AOM_QM_BITS));
      qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
      dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant / scale;
      if (abs_qcoeff) eob = i;
    }
  }
  *eob_ptr = eob + 1;
}

526
527
528
529
530
531
void av1_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                       int skip_block, 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,
Thomas Davies's avatar
Thomas Davies committed
532
                       const int16_t *iscan) {
533
534
  quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
                       quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
Thomas Davies's avatar
Thomas Davies committed
535
                       dequant_ptr, eob_ptr, scan, iscan, NULL, NULL, 0);
536
537
538
539
540
541
542
543
}

void av1_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                             int skip_block, 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,
Thomas Davies's avatar
Thomas Davies committed
544
                             const int16_t *scan, const int16_t *iscan) {
545
546
  quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
                       quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
Thomas Davies's avatar
Thomas Davies committed
547
                       dequant_ptr, eob_ptr, scan, iscan, NULL, NULL, 1);
548
549
550
551
552
553
554
555
556
}

#if CONFIG_TX64X64
void av1_quantize_fp_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                             int skip_block, 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,
Thomas Davies's avatar
Thomas Davies committed
557
                             const int16_t *scan, const int16_t *iscan) {
558
559
  quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, zbin_ptr, round_ptr,
                       quant_ptr, quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr,
Thomas Davies's avatar
Thomas Davies committed
560
                       dequant_ptr, eob_ptr, scan, iscan, NULL, NULL, 2);
561
562
563
}
#endif  // CONFIG_TX64X64

Yaowu Xu's avatar
Yaowu Xu committed
564
565
566
void 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,
567
                            const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
Angie Chiang's avatar
Angie Chiang committed
568
569
  // obsolete skip_block
  const int skip_block = 0;
570
571
572
#if CONFIG_AOM_QM
  const qm_val_t *qm_ptr = qparam->qmatrix;
  const qm_val_t *iqm_ptr = qparam->iqmatrix;
573
  if (qm_ptr != NULL && iqm_ptr != NULL) {
574
575
576
577
578
    quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                         p->round_fp_QTX, p->quant_fp_QTX, p->quant_shift_QTX,
                         qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
                         sc->scan, sc->iscan, qm_ptr, iqm_ptr,
                         qparam->log_scale);
Thomas Davies's avatar
Thomas Davies committed
579
  } else {
580
#endif
Thomas Davies's avatar
Thomas Davies committed
581
582
583
584
585
    switch (qparam->log_scale) {
      case 0:
        if (n_coeffs < 16) {
          // TODO(jingning): Need SIMD implementation for smaller block size
          // quantization.
586
587
588
589
590
          quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                               p->round_fp_QTX, p->quant_fp_QTX,
                               p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                               p->dequant_QTX, eob_ptr, sc->scan, sc->iscan,
                               NULL, NULL, qparam->log_scale);
Thomas Davies's avatar
Thomas Davies committed
591
        } else {
592
593
594
595
596
597
598
599
600
601
602
          if (qparam->tx_size == TX_4X16 || qparam->tx_size == TX_16X4 ||
              qparam->tx_size == TX_8X32 || qparam->tx_size == TX_32X8)
            av1_quantize_fp_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                              p->round_fp_QTX, p->quant_fp_QTX,
                              p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                              p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
          else
            av1_quantize_fp(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                            p->round_fp_QTX, p->quant_fp_QTX,
                            p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                            p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
603
604
605
        }
        break;
      case 1:
606
607
608
609
610
611
612
613
614
615
616
617
#if CONFIG_TX64X64
        if (qparam->tx_size == TX_16X64 || qparam->tx_size == TX_64X16)
          av1_quantize_fp_32x32_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                                  p->round_fp_QTX, p->quant_fp_QTX,
                                  p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                                  p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
        else
#endif  // CONFIG_RECT_TX_EXT && CONFIG_TX64X64
          av1_quantize_fp_32x32(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                                p->round_fp_QTX, p->quant_fp_QTX,
                                p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                                p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
618
        break;
619
#if CONFIG_TX64X64
Thomas Davies's avatar
Thomas Davies committed
620
      case 2:
621
622
623
624
        av1_quantize_fp_64x64(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                              p->round_fp_QTX, p->quant_fp_QTX,
                              p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                              p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
625
        break;
626
#endif  // CONFIG_TX64X64
Thomas Davies's avatar
Thomas Davies committed
627
628
629
      default: assert(0);
    }
#if CONFIG_AOM_QM
630
  }
Thomas Davies's avatar
Thomas Davies committed
631
#endif
Angie Chiang's avatar
Angie Chiang committed
632
633
}

Yaowu Xu's avatar
Yaowu Xu committed
634
635
void av1_quantize_b_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                           const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
636
637
                           tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
                           const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
Angie Chiang's avatar
Angie Chiang committed
638
639
  // obsolete skip_block
  const int skip_block = 0;
640
641
642
#if CONFIG_AOM_QM
  const qm_val_t *qm_ptr = qparam->qmatrix;
  const qm_val_t *iqm_ptr = qparam->iqmatrix;
643
  if (qm_ptr != NULL && iqm_ptr != NULL) {
644
645
646
647
648
    quantize_b_helper_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                        p->round_QTX, p->quant_QTX, p->quant_shift_QTX,
                        qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
                        sc->scan, sc->iscan, qm_ptr, iqm_ptr,
                        qparam->log_scale);
Thomas Davies's avatar
Thomas Davies committed
649
  } else {
650
#endif  // CONFIG_AOM_QM
Angie Chiang's avatar
Angie Chiang committed
651

Thomas Davies's avatar
Thomas Davies committed
652
653
    switch (qparam->log_scale) {
      case 0:
654
655
656
657
        aom_quantize_b(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                       p->round_QTX, p->quant_QTX, p->quant_shift_QTX,
                       qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
                       sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
658
659
        break;
      case 1:
660
661
662
663
        aom_quantize_b_32x32(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                             p->round_QTX, p->quant_QTX, p->quant_shift_QTX,
                             qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
                             sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
664
        break;
665
#if CONFIG_TX64X64
Thomas Davies's avatar
Thomas Davies committed
666
      case 2:
667
668
669
670
        aom_quantize_b_64x64(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                             p->round_QTX, p->quant_QTX, p->quant_shift_QTX,
                             qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
                             sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
671
672
673
674
        break;
#endif  // CONFIG_TX64X64
      default: assert(0);
    }
675
#if CONFIG_AOM_QM
Thomas Davies's avatar
Thomas Davies committed
676
  }
677
#endif
Thomas Davies's avatar
Thomas Davies committed
678
679
680
681
682
683
684
685
686
687
688
689
}

static void 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) {
  const int rc = 0;
  const int coeff = coeff_ptr[rc];
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
690
691
  int64_t tmp;
  int eob = -1;
Thomas Davies's avatar
Thomas Davies committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
  int32_t tmp32;
  int dequant;

  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));

  if (!skip_block) {
    const int wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
    const int iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
    tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale),
                INT16_MIN, INT16_MAX);
    tmp32 = (int32_t)((tmp * wt * quant) >> (16 - log_scale + AOM_QM_BITS));
    qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
    dequant = (dequant_ptr * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
    dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / (1 << log_scale);
    if (tmp32) eob = 0;
708
  }
Thomas Davies's avatar
Thomas Davies committed
709
  *eob_ptr = eob + 1;
Angie Chiang's avatar
Angie Chiang committed
710
711
}

Yaowu Xu's avatar
Yaowu Xu committed
712
713
714
void 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,
715
                            const SCAN_ORDER *sc, const QUANT_PARAM *qparam) {
Angie Chiang's avatar
Angie Chiang committed
716
717
  // obsolete skip_block
  const int skip_block = 0;
Thomas Davies's avatar
Thomas Davies committed
718
719
  (void)sc;
  assert(qparam->log_scale >= 0 && qparam->log_scale < (2 + CONFIG_TX64X64));
720
721
722
#if CONFIG_AOM_QM
  const qm_val_t *qm_ptr = qparam->qmatrix;
  const qm_val_t *iqm_ptr = qparam->iqmatrix;
Thomas Davies's avatar
Thomas Davies committed
723
724
725
#else
  const qm_val_t *qm_ptr = NULL;
  const qm_val_t *iqm_ptr = NULL;
726
#endif
727
728
729
  quantize_dc(coeff_ptr, (int)n_coeffs, skip_block, p->round_QTX,
              p->quant_fp_QTX[0], qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX[0],
              eob_ptr, qm_ptr, iqm_ptr, qparam->log_scale);
Angie Chiang's avatar
Angie Chiang committed
730
731
}

732
733
734
#if CONFIG_NEW_QUANT
void av1_quantize_b_nuq_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                               const MACROBLOCK_PLANE *p,
735
736
                               tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
                               uint16_t *eob_ptr, const SCAN_ORDER *sc,
737
738
739
740
741
742
743
                               const QUANT_PARAM *qparam) {
  // obsolete skip_block
  const int skip_block = 0;
  int dq = qparam->dq;

  switch (qparam->log_scale) {
    case 0:
744
745
      quantize_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_QTX,
                   p->quant_shift_QTX, p->dequant_QTX,
746
                   (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
747
                   (const dequant_val_type_nuq *)p->dequant_val_nuq_QTX[dq],
748
                   qcoeff_ptr, dqcoeff_ptr, eob_ptr, sc->scan);
749
750
      break;
    case 1:
751
752
753
754
      quantize_32x32_nuq(
          coeff_ptr, n_coeffs, skip_block, p->quant_QTX, p->quant_shift_QTX,
          p->dequant_QTX, (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
          (const dequant_val_type_nuq *)p->dequant_val_nuq_QTX[dq], qcoeff_ptr,
755
          dqcoeff_ptr, eob_ptr, sc->scan);
756
757
758
      break;
#if CONFIG_TX64X64
    case 2:
759
760
761
762
      quantize_64x64_nuq(
          coeff_ptr, n_coeffs, skip_block, p->quant_QTX, p->quant_shift_QTX,
          p->dequant_QTX, (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
          (const dequant_val_type_nuq *)p->dequant_val_nuq_QTX[dq], qcoeff_ptr,
763
          dqcoeff_ptr, eob_ptr, sc->scan);
764
765
766
767
768
769
770
771
      break;
#endif  // CONFIG_TX64X64
    default: assert(0);
  }
}

void av1_quantize_fp_nuq_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                                const MACROBLOCK_PLANE *p,
772
773
                                tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
                                uint16_t *eob_ptr, const SCAN_ORDER *sc,
774
775
776
777
778
779
780
                                const QUANT_PARAM *qparam) {
  // obsolete skip_block
  const int skip_block = 0;
  int dq = qparam->dq;

  switch (qparam->log_scale) {
    case 0:
781
782
      quantize_fp_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_fp_QTX,
                      p->dequant_QTX,
783
                      (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
784
                      (const dequant_val_type_nuq *)p->dequant_val_nuq_QTX[dq],
785
                      qcoeff_ptr, dqcoeff_ptr, eob_ptr, sc->scan);
786
787
788
      break;
    case 1:
      quantize_32x32_fp_nuq(
789
          coeff_ptr, n_coeffs, skip_block, p->quant_fp_QTX, p->dequant_QTX,
790
          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
791
          (const dequant_val_type_nuq *)p->dequant_val_nuq_QTX[dq], qcoeff_ptr,
792
          dqcoeff_ptr, eob_ptr, sc->scan);
793
794
795
796
      break;
#if CONFIG_TX64X64
    case 2:
      quantize_64x64_fp_nuq(
797
          coeff_ptr, n_coeffs, skip_block, p->quant_fp_QTX, p->dequant_QTX,
798
          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
799
          (const dequant_val_type_nuq *)p->dequant_val_nuq_QTX[dq], qcoeff_ptr,
800
          dqcoeff_ptr, eob_ptr, sc->scan);
801
802
803
804
805
806
807
808
      break;
#endif  // CONFIG_TX64X64
    default: assert(0);
  }
}

void av1_quantize_dc_nuq_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                                const MACROBLOCK_PLANE *p,
809
810
                                tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
                                uint16_t *eob_ptr, const SCAN_ORDER *sc,
811
812
813
814
815
816
817
818
                                const QUANT_PARAM *qparam) {
  // obsolete skip_block
  const int skip_block = 0;
  int dq = qparam->dq;
  (void)sc;

  switch (qparam->log_scale) {
    case 0:
819
820
821
      quantize_dc_fp_nuq(coeff_ptr, n_coeffs, skip_block, p->quant_fp_QTX[0],
                         p->dequant_QTX[0], p->cuml_bins_nuq[dq][0],
                         p->dequant_val_nuq_QTX[dq][0], qcoeff_ptr, dqcoeff_ptr,
822
823
824
                         eob_ptr);
      break;
    case 1:
825
826
827
828
      quantize_dc_32x32_fp_nuq(
          coeff_ptr, n_coeffs, skip_block, p->quant_fp_QTX[0],
          p->dequant_QTX[0], p->cuml_bins_nuq[dq][0],
          p->dequant_val_nuq_QTX[dq][0], qcoeff_ptr, dqcoeff_ptr, eob_ptr);
829
830
831
      break;
#if CONFIG_TX64X64
    case 2:
832
833
834
835
      quantize_dc_64x64_fp_nuq(
          coeff_ptr, n_coeffs, skip_block, p->quant_fp_QTX[0],
          p->dequant_QTX[0], p->cuml_bins_nuq[dq][0],
          p->dequant_val_nuq_QTX[dq][0], qcoeff_ptr, dqcoeff_ptr, eob_ptr);
836
837
838
839
840
841
842
843
844
845
846
847
848
      break;
#endif  // CONFIG_TX64X64
    default: assert(0);
  }
}
#endif  // CONFIG_NEW_QUANT

void 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) {
Angie Chiang's avatar
Angie Chiang committed
849
850
  // obsolete skip_block
  const int skip_block = 0;
851
852
853
#if CONFIG_AOM_QM
  const qm_val_t *qm_ptr = qparam->qmatrix;
  const qm_val_t *iqm_ptr = qparam->iqmatrix;
854
  if (qm_ptr != NULL && iqm_ptr != NULL) {
855
856
857
858
859
    highbd_quantize_fp_helper_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                                p->round_fp_QTX, p->quant_fp_QTX,
                                p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                                p->dequant_QTX, eob_ptr, sc->scan, sc->iscan,
                                qm_ptr, iqm_ptr, qparam->log_scale);
Thomas Davies's avatar
Thomas Davies committed
860
  } else {
861
#endif  // CONFIG_AOM_QM
Angie Chiang's avatar
Angie Chiang committed
862

Thomas Davies's avatar
Thomas Davies committed
863
864
865
    if (n_coeffs < 16) {
      // TODO(jingning): Need SIMD implementation for smaller block size
      // quantization.
866
867
868
869
      av1_highbd_quantize_fp_c(
          coeff_ptr, n_coeffs, skip_block, p->zbin_QTX, p->round_fp_QTX,
          p->quant_fp_QTX, p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
          p->dequant_QTX, eob_ptr, sc->scan, sc->iscan, qparam->log_scale);
Thomas Davies's avatar
Thomas Davies committed
870
871
      return;
    }
872

873
874
875
876
    av1_highbd_quantize_fp(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                           p->round_fp_QTX, p->quant_fp_QTX, p->quant_shift_QTX,
                           qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
                           sc->scan, sc->iscan, qparam->log_scale);
877
#if CONFIG_AOM_QM
Thomas Davies's avatar
Thomas Davies committed
878
  }
879
#endif
Angie Chiang's avatar
Angie Chiang committed
880
881
}

882
883
884
885
886
887
void 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) {
Yaowu Xu's avatar
Yaowu Xu committed
888
889
  // obsolete skip_block
  const int skip_block = 0;
890
891
892
#if CONFIG_AOM_QM
  const qm_val_t *qm_ptr = qparam->qmatrix;
  const qm_val_t *iqm_ptr = qparam->iqmatrix;
893
  if (qm_ptr != NULL && iqm_ptr != NULL) {
894
895
896
897
898
    highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                               p->round_QTX, p->quant_QTX, p->quant_shift_QTX,
                               qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX, eob_ptr,
                               sc->scan, sc->iscan, qm_ptr, iqm_ptr,
                               qparam->log_scale);
Thomas Davies's avatar
Thomas Davies committed
899
  } else {
900
#endif  // CONFIG_AOM_QM
Yaowu Xu's avatar
Yaowu Xu committed
901

Thomas Davies's avatar
Thomas Davies committed
902
903
904
    switch (qparam->log_scale) {
      case 0:
        if (LIKELY(n_coeffs >= 8)) {
905
906
907
908
          aom_highbd_quantize_b(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                                p->round_QTX, p->quant_QTX, p->quant_shift_QTX,
                                qcoeff_ptr, dqcoeff_ptr, p->dequant_QTX,
                                eob_ptr, sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
909
910
911
        } else {
          // TODO(luoyi): Need SIMD (e.g. sse2) for smaller block size
          // quantization
912
913
914
915
          aom_highbd_quantize_b_c(coeff_ptr, n_coeffs, skip_block, p->zbin_QTX,
                                  p->round_QTX, p->quant_QTX,
                                  p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
                                  p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
916
917
918
        }
        break;
      case 1:
919
920
921
922
        aom_highbd_quantize_b_32x32(
            coeff_ptr, n_coeffs, skip_block, p->zbin_QTX, p->round_QTX,
            p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
            p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
923
        break;
Cheng Chen's avatar
Cheng Chen committed
924
#if CONFIG_TX64X64
Thomas Davies's avatar
Thomas Davies committed
925
      case 2:
926
927
928
929
        aom_highbd_quantize_b_64x64(
            coeff_ptr, n_coeffs, skip_block, p->zbin_QTX, p->round_QTX,
            p->quant_QTX, p->quant_shift_QTX, qcoeff_ptr, dqcoeff_ptr,
            p->dequant_QTX, eob_ptr, sc->scan, sc->iscan);
Thomas Davies's avatar
Thomas Davies committed
930
        break;
Cheng Chen's avatar
Cheng Chen committed
931
#endif  // CONFIG_TX64X64
Thomas Davies's avatar
Thomas Davies committed
932
933
934
      default: assert(0);
    }
#if CONFIG_AOM_QM
Cheng Chen's avatar
Cheng Chen committed
935
  }
Thomas Davies's avatar
Thomas Davies committed
936
#endif
Yaowu Xu's avatar
Yaowu Xu committed
937
938
}

939
940
941
942
static INLINE void 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,
Thomas Davies's avatar
Thomas Davies committed
943
    const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr, const int log_scale) {
944
945
946
947
  int eob = -1;

  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
Thomas Davies's avatar
Thomas Davies committed
948

949
  if (!skip_block) {
Thomas Davies's avatar
Thomas Davies committed
950
951
    const qm_val_t wt = qm_ptr != NULL ? qm_ptr[0] : (1 << AOM_QM_BITS);
    const qm_val_t iwt = iqm_ptr != NULL ? iqm_ptr[0] : (1 << AOM_QM_BITS);
952
953
954
    const int coeff = coeff_ptr[0];
    const int coeff_sign = (coeff >> 31);
    const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
Thomas Davies's avatar
Thomas Davies committed
955
956
957
958
    const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[0], log_scale);
    const int64_t tmpw = tmp * wt;
    const int abs_qcoeff =
        (int)((tmpw * quant) >> (16 - log_scale + AOM_QM_BITS));
959
    qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
Thomas Davies's avatar
Thomas Davies committed
960
961
962
963
    const int dequant =
        (dequant_ptr * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;

    dqcoeff_ptr[0] = (qcoeff_ptr[0] * dequant) / (1 << log_scale);
964
965
966
967
968
969
970
971
972
973
974
    if (abs_qcoeff) eob = 0;
  }
  *eob_ptr = eob + 1;
}

void 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) {
Angie Chiang's avatar
Angie Chiang committed
975
976
  // obsolete skip_block
  const int skip_block = 0;
977
978
979
#if CONFIG_AOM_QM
  const qm_val_t *qm_ptr = qparam->qmatrix;
  const qm_val_t *iqm_ptr = qparam->iqmatrix;
Thomas Davies's avatar
Thomas Davies committed
980
981
982
#else
  const qm_val_t *qm_ptr = NULL;
  const qm_val_t *iqm_ptr = NULL;
983
#endif  // CONFIG_AOM_QM
Angie Chiang's avatar
Angie Chiang committed
984
985
986

  (void)sc;

987
988
989
990
  highbd_quantize_dc(coeff_ptr, (int)n_coeffs, skip_block, p->round_QTX,
                     p->quant_fp_QTX[0], qcoeff_ptr, dqcoeff_ptr,
                     p->dequant_QTX[0], eob_ptr, qm_ptr, iqm_ptr,
                     qparam->log_scale);
Angie Chiang's avatar
Angie Chiang committed
991
}
992
993

#if CONFIG_NEW_QUANT
994
995
996
997
998
static INLINE int highbd_quantize_coeff_nuq(
    const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
    const int16_t dequant, const tran_low_t *cuml_bins_ptr,
    const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
    tran_low_t *dqcoeff_ptr) {
999
1000
1001
1002
1003
1004
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
1005
    if (tmp <= cuml_bins_ptr[i]) {
1006
1007
1008
1009
1010
1011
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
    tmp -= cuml_bins_ptr[NUQ_KNOTS - 1];
1012
    q = NUQ_KNOTS + (int)(((((tmp * quant) >> 16) + tmp) * quant_shift) >> 16);
1013
1014
  }
  if (q) {
Yaowu Xu's avatar
Yaowu Xu committed
1015
    *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
1016
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
1017
1018
1019
1020
1021
1022
1023
1024
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

1025
1026
1027
1028
static INLINE int highbd_quantize_coeff_fp_nuq(
    const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
    const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
    tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr) {
1029
1030
1031
1032
1033
1034
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
1035
    if (tmp <= cuml_bins_ptr[i]) {
1036
1037
1038
1039
1040
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
1041
    q = NUQ_KNOTS + (int)(((tmp - cuml_bins_ptr[NUQ_KNOTS - 1]) * quant) >> 16);
1042
1043
  }
  if (q) {
Yaowu Xu's avatar
Yaowu Xu committed
1044
    *dqcoeff_ptr = av1_dequant_abscoeff_nuq(q, dequant, dequant_val);
1045
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
1046
1047
1048
1049
1050
1051
1052
1053
1054
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

static INLINE int highbd_quantize_coeff_bigtx_fp_nuq(
1055
1056
    const tran_low_t coeffv, const int16_t quant, const int16_t dequant,
    const tran_low_t *cuml_bins_ptr, const tran_low_t *dequant_val,
1057
    tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, int logsizeby16) {
1058
1059
1060
1061
1062
1063
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
1064
    if (tmp <= ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
1065
1066
1067
1068
1069
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
1070
    q = NUQ_KNOTS +
1071
1072
        (int)(((tmp -
                ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16)) *
1073
               quant) >>
1074
              (16 - logsizeby16));
1075
1076
  }
  if (q) {
1077
    *dqcoeff_ptr = ROUND_POWER_OF_TWO(
1078
        av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
1079
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
1080
1081
1082
1083
1084
1085
1086
1087
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

1088
1089
1090
1091
static INLINE int highbd_quantize_coeff_bigtx_nuq(
    const tran_low_t coeffv, const int16_t quant, const int16_t quant_shift,
    const int16_t dequant, const tran_low_t *cuml_bins_ptr,
    const tran_low_t *dequant_val, tran_low_t *qcoeff_ptr,
1092
    tran_low_t *dqcoeff_ptr, int logsizeby16) {
1093
1094
1095
1096
1097
1098
  const int coeff = coeffv;
  const int coeff_sign = (coeff >> 31);
  const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  int i, q;
  int64_t tmp = clamp(abs_coeff, INT32_MIN, INT32_MAX);
  for (i = 0; i < NUQ_KNOTS; i++) {
1099
    if (tmp <= ROUND_POWER_OF_TWO(cuml_bins_ptr[i], logsizeby16)) {
1100
1101
1102
1103
1104
      q = i;
      break;
    }
  }
  if (i == NUQ_KNOTS) {
1105
    tmp -= ROUND_POWER_OF_TWO(cuml_bins_ptr[NUQ_KNOTS - 1], logsizeby16);
1106
    q = NUQ_KNOTS + (int)(((((tmp * quant) >> 16) + tmp) * quant_shift) >>
1107
                          (16 - logsizeby16));
1108
1109
  }
  if (q) {
1110
    *dqcoeff_ptr = ROUND_POWER_OF_TWO(
1111
        av1_dequant_abscoeff_nuq(q, dequant, dequant_val), logsizeby16);
1112
    *qcoeff_ptr = (q ^ coeff_sign) - coeff_sign;
1113
1114
1115
1116
1117
1118
1119
1120
    *dqcoeff_ptr = *qcoeff_ptr < 0 ? -*dqcoeff_ptr : *dqcoeff_ptr;
  } else {
    *qcoeff_ptr = 0;
    *dqcoeff_ptr = 0;
  }
  return (q != 0);
}

1121
1122
1123
void highbd_quantize_dc_nuq(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
                            int skip_block, const int16_t quant,
                            const int16_t quant_shift, const int16_t dequant,