2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
13 #include "./vpx_dsp_rtcd.h"
14 #include "vpx_mem/vpx_mem.h"
15 #include "vpx_ports/mem.h"
17 #include "vp9/common/vp9_quant_common.h"
18 #include "vp9/common/vp9_seg_common.h"
20 #include "vp9/encoder/vp9_encoder.h"
21 #include "vp9/encoder/vp9_quantize.h"
22 #include "vp9/encoder/vp9_rd.h"
24 void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
25 int skip_block, const int16_t *round_ptr,
26 const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
27 tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
28 uint16_t *eob_ptr, const int16_t *scan,
29 const int16_t *iscan) {
35 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
36 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
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;
46 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
47 tmp = (tmp * quant_ptr[rc != 0]) >> 16;
49 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
50 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
57 #if CONFIG_VP9_HIGHBITDEPTH
58 void vp9_highbd_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
59 int skip_block, const int16_t *round_ptr,
60 const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
61 tran_low_t *dqcoeff_ptr,
62 const int16_t *dequant_ptr, uint16_t *eob_ptr,
63 const int16_t *scan, const int16_t *iscan) {
71 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
72 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
74 // Quantization pass: All coefficients with index >= zero_flag are
75 // skippable. Note: zero_flag can be zero.
76 for (i = 0; i < n_coeffs; i++) {
77 const int rc = scan[i];
78 const int coeff = coeff_ptr[rc];
79 const int coeff_sign = (coeff >> 31);
80 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
81 const int64_t tmp = abs_coeff + round_ptr[rc != 0];
82 const int abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 16);
83 qcoeff_ptr[rc] = (tran_low_t)(abs_qcoeff ^ coeff_sign) - coeff_sign;
84 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
85 if (abs_qcoeff) eob = i;
91 // TODO(jingning) Refactor this file and combine functions with similar
93 void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
94 int skip_block, const int16_t *round_ptr,
95 const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
96 tran_low_t *dqcoeff_ptr,
97 const int16_t *dequant_ptr, uint16_t *eob_ptr,
98 const int16_t *scan, const int16_t *iscan) {
104 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
105 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
107 for (i = 0; i < n_coeffs; i++) {
108 const int rc = scan[i];
109 const int coeff = coeff_ptr[rc];
110 const int coeff_sign = (coeff >> 31);
112 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
114 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
115 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
116 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
117 tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
118 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
119 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
127 #if CONFIG_VP9_HIGHBITDEPTH
128 void vp9_highbd_quantize_fp_32x32_c(
129 const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
130 const int16_t *round_ptr, const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
131 tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
132 const int16_t *scan, const int16_t *iscan) {
139 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
140 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
142 for (i = 0; i < n_coeffs; i++) {
144 const int rc = scan[i];
145 const int coeff = coeff_ptr[rc];
146 const int coeff_sign = (coeff >> 31);
147 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
149 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
150 const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
151 abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 15);
152 qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
153 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
156 if (abs_qcoeff) eob = i;
162 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
163 const int16_t *scan, const int16_t *iscan) {
164 MACROBLOCKD *const xd = &x->e_mbd;
165 struct macroblock_plane *p = &x->plane[plane];
166 struct macroblockd_plane *pd = &xd->plane[plane];
167 tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block),
168 *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
169 const int n_coeffs = 4 * 4;
172 memset(qcoeff, 0, n_coeffs * sizeof(*qcoeff));
173 memset(dqcoeff, 0, n_coeffs * sizeof(*dqcoeff));
177 #if CONFIG_VP9_HIGHBITDEPTH
178 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
179 vpx_highbd_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs,
180 x->skip_block, p->zbin, p->round, p->quant,
181 p->quant_shift, qcoeff, dqcoeff, pd->dequant,
182 &p->eobs[block], scan, iscan);
186 vpx_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs, x->skip_block,
187 p->zbin, p->round, p->quant, p->quant_shift, qcoeff, dqcoeff,
188 pd->dequant, &p->eobs[block], scan, iscan);
191 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
195 for (l = 0; t > 1; l++) t >>= 1;
196 m = 1 + (1 << (16 + l)) / d;
197 *quant = (int16_t)(m - (1 << 16));
198 *shift = 1 << (16 - l);
201 static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
202 const int quant = vp9_dc_quant(q, 0, bit_depth);
203 #if CONFIG_VP9_HIGHBITDEPTH
205 case VPX_BITS_8: return q == 0 ? 64 : (quant < 148 ? 84 : 80);
206 case VPX_BITS_10: return q == 0 ? 64 : (quant < 592 ? 84 : 80);
208 assert(bit_depth == VPX_BITS_12);
209 return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
213 return q == 0 ? 64 : (quant < 148 ? 84 : 80);
217 void vp9_init_quantizer(VP9_COMP *cpi) {
218 VP9_COMMON *const cm = &cpi->common;
219 QUANTS *const quants = &cpi->quants;
222 for (q = 0; q < QINDEX_RANGE; q++) {
223 int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
224 int qrounding_factor = q == 0 ? 64 : 48;
225 const int sharpness_adjustment = 16 * (7 - cpi->oxcf.sharpness) / 7;
227 if (cpi->oxcf.sharpness > 0 && q > 0) {
228 qzbin_factor = 64 + sharpness_adjustment;
229 qrounding_factor = 64 - sharpness_adjustment;
232 for (i = 0; i < 2; ++i) {
233 int qrounding_factor_fp = i == 0 ? 48 : 42;
234 if (q == 0) qrounding_factor_fp = 64;
235 if (cpi->oxcf.sharpness > 0)
236 qrounding_factor_fp = 64 - sharpness_adjustment;
238 quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
239 : vp9_ac_quant(q, 0, cm->bit_depth);
240 invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
241 quants->y_quant_fp[q][i] = (1 << 16) / quant;
242 quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
243 quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
244 quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
245 cpi->y_dequant[q][i] = quant;
248 quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
249 : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
250 invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
252 quants->uv_quant_fp[q][i] = (1 << 16) / quant;
253 quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
254 quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
255 quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
256 cpi->uv_dequant[q][i] = quant;
259 for (i = 2; i < 8; i++) {
260 quants->y_quant[q][i] = quants->y_quant[q][1];
261 quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
262 quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
263 quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
264 quants->y_zbin[q][i] = quants->y_zbin[q][1];
265 quants->y_round[q][i] = quants->y_round[q][1];
266 cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
268 quants->uv_quant[q][i] = quants->uv_quant[q][1];
269 quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
270 quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
271 quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
272 quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
273 quants->uv_round[q][i] = quants->uv_round[q][1];
274 cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
279 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
280 const VP9_COMMON *const cm = &cpi->common;
281 MACROBLOCKD *const xd = &x->e_mbd;
282 QUANTS *const quants = &cpi->quants;
283 const int segment_id = xd->mi[0]->segment_id;
284 const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
285 const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
289 x->plane[0].quant = quants->y_quant[qindex];
290 x->plane[0].quant_fp = quants->y_quant_fp[qindex];
291 memcpy(x->plane[0].round_fp, quants->y_round_fp[qindex],
292 8 * sizeof(*(x->plane[0].round_fp)));
293 x->plane[0].quant_shift = quants->y_quant_shift[qindex];
294 x->plane[0].zbin = quants->y_zbin[qindex];
295 x->plane[0].round = quants->y_round[qindex];
296 xd->plane[0].dequant = cpi->y_dequant[qindex];
297 x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
298 x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
301 for (i = 1; i < 3; i++) {
302 x->plane[i].quant = quants->uv_quant[qindex];
303 x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
304 memcpy(x->plane[i].round_fp, quants->uv_round_fp[qindex],
305 8 * sizeof(*(x->plane[i].round_fp)));
306 x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
307 x->plane[i].zbin = quants->uv_zbin[qindex];
308 x->plane[i].round = quants->uv_round[qindex];
309 xd->plane[i].dequant = cpi->uv_dequant[qindex];
310 x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
311 x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
314 x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
317 set_error_per_bit(x, rdmult);
319 vp9_initialize_me_consts(cpi, x, x->q_index);
322 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
323 vp9_init_plane_quantizers(cpi, &cpi->td.mb);
326 void vp9_set_quantizer(VP9_COMMON *cm, int q) {
327 // quantizer has to be reinitialized with vp9_init_quantizer() if any
330 cm->y_dc_delta_q = 0;
331 cm->uv_dc_delta_q = 0;
332 cm->uv_ac_delta_q = 0;
335 // Table that converts 0-63 Q-range values passed in outside to the Qindex
336 // range used internally.
337 static const int quantizer_to_qindex[] = {
338 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
339 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
340 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
341 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
342 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
345 int vp9_quantizer_to_qindex(int quantizer) {
346 return quantizer_to_qindex[quantizer];
349 int vp9_qindex_to_quantizer(int qindex) {
352 for (quantizer = 0; quantizer < 64; ++quantizer)
353 if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;