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