1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <assert.h>
12 #include <math.h>
13 #include "./vpx_dsp_rtcd.h"
14 #include "vpx_mem/vpx_mem.h"
15 #include "vpx_ports/mem.h"
16
17 #include "vp9/common/vp9_quant_common.h"
18 #include "vp9/common/vp9_scan.h"
19 #include "vp9/common/vp9_seg_common.h"
20
21 #include "vp9/encoder/vp9_encoder.h"
22 #include "vp9/encoder/vp9_quantize.h"
23 #include "vp9/encoder/vp9_rd.h"
24
vp9_quantize_fp_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)25 void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
26 const struct macroblock_plane *const mb_plane,
27 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
28 const int16_t *dequant_ptr, uint16_t *eob_ptr,
29 const struct ScanOrder *const scan_order) {
30 int i, eob = -1;
31 const int16_t *round_ptr = mb_plane->round_fp;
32 const int16_t *quant_ptr = mb_plane->quant_fp;
33 const int16_t *scan = scan_order->scan;
34
35 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
36 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
37
38 // Quantization pass: All coefficients with index >= zero_flag are
39 // skippable. Note: zero_flag can be zero.
40 for (i = 0; i < n_coeffs; i++) {
41 const int rc = scan[i];
42 const int coeff = coeff_ptr[rc];
43 const int coeff_sign = (coeff >> 31);
44 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
45
46 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
47 tmp = (tmp * quant_ptr[rc != 0]) >> 16;
48
49 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
50 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
51
52 if (tmp) eob = i;
53 }
54 *eob_ptr = eob + 1;
55 }
56
57 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_quantize_fp_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)58 void vp9_highbd_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
59 const struct macroblock_plane *const mb_plane,
60 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
61 const int16_t *dequant_ptr, uint16_t *eob_ptr,
62 const struct ScanOrder *const scan_order) {
63 int i;
64 int eob = -1;
65 const int16_t *round_ptr = mb_plane->round_fp;
66 const int16_t *quant_ptr = mb_plane->quant_fp;
67 const int16_t *scan = scan_order->scan;
68
69 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
70 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
71
72 // Quantization pass: All coefficients with index >= zero_flag are
73 // skippable. Note: zero_flag can be zero.
74 for (i = 0; i < n_coeffs; i++) {
75 const int rc = scan[i];
76 const int coeff = coeff_ptr[rc];
77 const int coeff_sign = (coeff >> 31);
78 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
79 const int64_t tmp = abs_coeff + round_ptr[rc != 0];
80 const int abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 16);
81 qcoeff_ptr[rc] = (tran_low_t)(abs_qcoeff ^ coeff_sign) - coeff_sign;
82 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
83 if (abs_qcoeff) eob = i;
84 }
85 *eob_ptr = eob + 1;
86 }
87 #endif
88
89 // TODO(jingning) Refactor this file and combine functions with similar
90 // operations.
vp9_quantize_fp_32x32_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)91 void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
92 const struct macroblock_plane *const mb_plane,
93 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
94 const int16_t *dequant_ptr, uint16_t *eob_ptr,
95 const struct ScanOrder *const scan_order) {
96 int i, eob = -1;
97 const int16_t *round_ptr = mb_plane->round_fp;
98 const int16_t *quant_ptr = mb_plane->quant_fp;
99 const int16_t *scan = scan_order->scan;
100
101 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
102 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
103
104 for (i = 0; i < n_coeffs; i++) {
105 const int rc = scan[i];
106 const int coeff = coeff_ptr[rc];
107 const int coeff_sign = (coeff >> 31);
108 int tmp = 0;
109 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
110
111 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
112 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
113 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
114 tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
115 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
116 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
117 }
118
119 if (tmp) eob = i;
120 }
121 *eob_ptr = eob + 1;
122 }
123
124 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_quantize_fp_32x32_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)125 void vp9_highbd_quantize_fp_32x32_c(
126 const tran_low_t *coeff_ptr, intptr_t n_coeffs,
127 const struct macroblock_plane *const mb_plane, tran_low_t *qcoeff_ptr,
128 tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
129 const struct ScanOrder *const scan_order) {
130 int i, eob = -1;
131 const int16_t *round_ptr = mb_plane->round_fp;
132 const int16_t *quant_ptr = mb_plane->quant_fp;
133 const int16_t *scan = scan_order->scan;
134
135 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
136 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
137
138 for (i = 0; i < n_coeffs; i++) {
139 int abs_qcoeff = 0;
140 const int rc = scan[i];
141 const int coeff = coeff_ptr[rc];
142 const int coeff_sign = (coeff >> 31);
143 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
144
145 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
146 const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
147 abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 15);
148 qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
149 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
150 }
151
152 if (abs_qcoeff) eob = i;
153 }
154 *eob_ptr = eob + 1;
155 }
156 #endif
157
invert_quant(int16_t * quant,int16_t * shift,int d)158 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
159 unsigned t;
160 int l, m;
161 t = d;
162 for (l = 0; t > 1; l++) t >>= 1;
163 m = 1 + (1 << (16 + l)) / d;
164 *quant = (int16_t)(m - (1 << 16));
165 *shift = 1 << (16 - l);
166 }
167
get_qzbin_factor(int q,vpx_bit_depth_t bit_depth)168 static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
169 const int quant = vp9_dc_quant(q, 0, bit_depth);
170 #if CONFIG_VP9_HIGHBITDEPTH
171 switch (bit_depth) {
172 case VPX_BITS_8: return q == 0 ? 64 : (quant < 148 ? 84 : 80);
173 case VPX_BITS_10: return q == 0 ? 64 : (quant < 592 ? 84 : 80);
174 default:
175 assert(bit_depth == VPX_BITS_12);
176 return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
177 }
178 #else
179 (void)bit_depth;
180 return q == 0 ? 64 : (quant < 148 ? 84 : 80);
181 #endif
182 }
183
vp9_init_quantizer(VP9_COMP * cpi)184 void vp9_init_quantizer(VP9_COMP *cpi) {
185 VP9_COMMON *const cm = &cpi->common;
186 QUANTS *const quants = &cpi->quants;
187 int i, q, quant;
188
189 for (q = 0; q < QINDEX_RANGE; q++) {
190 int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
191 int qrounding_factor = q == 0 ? 64 : 48;
192 const int sharpness_adjustment = 16 * (7 - cpi->oxcf.sharpness) / 7;
193
194 if (cpi->oxcf.sharpness > 0 && q > 0) {
195 qzbin_factor = 64 + sharpness_adjustment;
196 qrounding_factor = 64 - sharpness_adjustment;
197 }
198
199 for (i = 0; i < 2; ++i) {
200 int qrounding_factor_fp = i == 0 ? 48 : 42;
201 if (q == 0) qrounding_factor_fp = 64;
202 if (cpi->oxcf.sharpness > 0)
203 qrounding_factor_fp = 64 - sharpness_adjustment;
204 // y
205 quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
206 : vp9_ac_quant(q, 0, cm->bit_depth);
207 invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
208 quants->y_quant_fp[q][i] = (1 << 16) / quant;
209 quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
210 quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
211 quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
212 cpi->y_dequant[q][i] = quant;
213
214 // uv
215 quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
216 : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
217 invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
218 quant);
219 quants->uv_quant_fp[q][i] = (1 << 16) / quant;
220 quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
221 quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
222 quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
223 cpi->uv_dequant[q][i] = quant;
224 }
225
226 for (i = 2; i < 8; i++) {
227 quants->y_quant[q][i] = quants->y_quant[q][1];
228 quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
229 quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
230 quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
231 quants->y_zbin[q][i] = quants->y_zbin[q][1];
232 quants->y_round[q][i] = quants->y_round[q][1];
233 cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
234
235 quants->uv_quant[q][i] = quants->uv_quant[q][1];
236 quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
237 quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
238 quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
239 quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
240 quants->uv_round[q][i] = quants->uv_round[q][1];
241 cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
242 }
243 }
244 }
245
vp9_init_plane_quantizers(VP9_COMP * cpi,MACROBLOCK * x)246 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
247 const VP9_COMMON *const cm = &cpi->common;
248 MACROBLOCKD *const xd = &x->e_mbd;
249 QUANTS *const quants = &cpi->quants;
250 const int segment_id = xd->mi[0]->segment_id;
251 const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
252 const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
253 int i;
254
255 // Y
256 x->plane[0].quant = quants->y_quant[qindex];
257 x->plane[0].quant_fp = quants->y_quant_fp[qindex];
258 x->plane[0].round_fp = quants->y_round_fp[qindex];
259 x->plane[0].quant_shift = quants->y_quant_shift[qindex];
260 x->plane[0].zbin = quants->y_zbin[qindex];
261 x->plane[0].round = quants->y_round[qindex];
262 xd->plane[0].dequant = cpi->y_dequant[qindex];
263 x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
264 x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
265
266 // UV
267 for (i = 1; i < 3; i++) {
268 x->plane[i].quant = quants->uv_quant[qindex];
269 x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
270 x->plane[i].round_fp = quants->uv_round_fp[qindex];
271 x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
272 x->plane[i].zbin = quants->uv_zbin[qindex];
273 x->plane[i].round = quants->uv_round[qindex];
274 xd->plane[i].dequant = cpi->uv_dequant[qindex];
275 x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
276 x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
277 }
278
279 x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
280 x->q_index = qindex;
281
282 set_error_per_bit(x, rdmult);
283
284 vp9_initialize_me_consts(cpi, x, x->q_index);
285 }
286
vp9_frame_init_quantizer(VP9_COMP * cpi)287 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
288 vp9_init_plane_quantizers(cpi, &cpi->td.mb);
289 }
290
vp9_set_quantizer(VP9_COMP * cpi,int q)291 void vp9_set_quantizer(VP9_COMP *cpi, int q) {
292 VP9_COMMON *cm = &cpi->common;
293 // quantizer has to be reinitialized with vp9_init_quantizer() if any
294 // delta_q changes.
295 cm->base_qindex = q;
296 cm->y_dc_delta_q = 0;
297 cm->uv_dc_delta_q = 0;
298 cm->uv_ac_delta_q = 0;
299 if (cpi->oxcf.delta_q_uv != 0) {
300 cm->uv_dc_delta_q = cm->uv_ac_delta_q = cpi->oxcf.delta_q_uv;
301 vp9_init_quantizer(cpi);
302 }
303 }
304
305 // Table that converts 0-63 Q-range values passed in outside to the Qindex
306 // range used internally.
307 static const int quantizer_to_qindex[] = {
308 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
309 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
310 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
311 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
312 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
313 };
314
vp9_quantizer_to_qindex(int quantizer)315 int vp9_quantizer_to_qindex(int quantizer) {
316 return quantizer_to_qindex[quantizer];
317 }
318
vp9_qindex_to_quantizer(int qindex)319 int vp9_qindex_to_quantizer(int qindex) {
320 int quantizer;
321
322 for (quantizer = 0; quantizer < 64; ++quantizer)
323 if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
324
325 return 63;
326 }
327