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_mem/vpx_mem.h"
15 #include "vp9/common/vp9_quant_common.h"
16 #include "vp9/common/vp9_seg_common.h"
18 #include "vp9/encoder/vp9_encoder.h"
19 #include "vp9/encoder/vp9_quantize.h"
20 #include "vp9/encoder/vp9_rd.h"
22 void vp9_quantize_dc(const int16_t *coeff_ptr, int skip_block,
23 const int16_t *round_ptr, const int16_t quant,
24 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
25 const int16_t dequant_ptr, uint16_t *eob_ptr) {
30 const int coeff = coeff_ptr[rc];
31 const int coeff_sign = (coeff >> 31);
32 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
34 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
35 tmp = (tmp * quant) >> 16;
36 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
37 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
44 void vp9_quantize_dc_32x32(const int16_t *coeff_ptr, int skip_block,
45 const int16_t *round_ptr, const int16_t quant,
46 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
47 const int16_t dequant_ptr, uint16_t *eob_ptr) {
52 const int coeff = coeff_ptr[rc];
53 const int coeff_sign = (coeff >> 31);
54 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
56 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
57 tmp = (tmp * quant) >> 15;
58 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
59 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
66 void vp9_quantize_fp_c(const int16_t *coeff_ptr, intptr_t count,
68 const int16_t *zbin_ptr, const int16_t *round_ptr,
69 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
70 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
71 const int16_t *dequant_ptr,
72 int zbin_oq_value, uint16_t *eob_ptr,
73 const int16_t *scan, const int16_t *iscan) {
75 // TODO(jingning) Decide the need of these arguments after the
76 // quantization process is completed.
78 (void)quant_shift_ptr;
82 vpx_memset(qcoeff_ptr, 0, count * sizeof(int16_t));
83 vpx_memset(dqcoeff_ptr, 0, count * sizeof(int16_t));
86 // Quantization pass: All coefficients with index >= zero_flag are
87 // skippable. Note: zero_flag can be zero.
88 for (i = 0; i < count; i++) {
89 const int rc = scan[i];
90 const int coeff = coeff_ptr[rc];
91 const int coeff_sign = (coeff >> 31);
92 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
94 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
95 tmp = (tmp * quant_ptr[rc != 0]) >> 16;
97 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
98 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
107 // TODO(jingning) Refactor this file and combine functions with similar
109 void vp9_quantize_fp_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
111 const int16_t *zbin_ptr, const int16_t *round_ptr,
112 const int16_t *quant_ptr,
113 const int16_t *quant_shift_ptr,
114 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
115 const int16_t *dequant_ptr,
116 int zbin_oq_value, uint16_t *eob_ptr,
117 const int16_t *scan, const int16_t *iscan) {
120 (void)quant_shift_ptr;
124 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
125 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
128 for (i = 0; i < n_coeffs; i++) {
129 const int rc = scan[i];
130 const int coeff = coeff_ptr[rc];
131 const int coeff_sign = (coeff >> 31);
133 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
135 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
136 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
137 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
138 tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
139 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
140 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
150 void vp9_quantize_b_c(const int16_t *coeff_ptr, intptr_t count,
152 const int16_t *zbin_ptr, const int16_t *round_ptr,
153 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
154 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
155 const int16_t *dequant_ptr,
156 int zbin_oq_value, uint16_t *eob_ptr,
157 const int16_t *scan, const int16_t *iscan) {
158 int i, non_zero_count = (int)count, eob = -1;
159 const int zbins[2] = { zbin_ptr[0] + zbin_oq_value,
160 zbin_ptr[1] + zbin_oq_value };
161 const int nzbins[2] = { zbins[0] * -1,
165 vpx_memset(qcoeff_ptr, 0, count * sizeof(int16_t));
166 vpx_memset(dqcoeff_ptr, 0, count * sizeof(int16_t));
170 for (i = (int)count - 1; i >= 0; i--) {
171 const int rc = scan[i];
172 const int coeff = coeff_ptr[rc];
174 if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
180 // Quantization pass: All coefficients with index >= zero_flag are
181 // skippable. Note: zero_flag can be zero.
182 for (i = 0; i < non_zero_count; i++) {
183 const int rc = scan[i];
184 const int coeff = coeff_ptr[rc];
185 const int coeff_sign = (coeff >> 31);
186 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
188 if (abs_coeff >= zbins[rc != 0]) {
189 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
190 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
191 quant_shift_ptr[rc != 0]) >> 16; // quantization
192 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
193 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
203 void vp9_quantize_b_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
205 const int16_t *zbin_ptr, const int16_t *round_ptr,
206 const int16_t *quant_ptr,
207 const int16_t *quant_shift_ptr,
208 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
209 const int16_t *dequant_ptr,
210 int zbin_oq_value, uint16_t *eob_ptr,
211 const int16_t *scan, const int16_t *iscan) {
212 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1),
213 ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1) };
214 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
221 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
222 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
226 for (i = 0; i < n_coeffs; i++) {
227 const int rc = scan[i];
228 const int coeff = coeff_ptr[rc];
230 // If the coefficient is out of the base ZBIN range, keep it for
232 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
236 // Quantization pass: only process the coefficients selected in
237 // pre-scan pass. Note: idx can be zero.
238 for (i = 0; i < idx; i++) {
239 const int rc = scan[idx_arr[i]];
240 const int coeff = coeff_ptr[rc];
241 const int coeff_sign = (coeff >> 31);
243 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
244 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
245 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
246 tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) *
247 quant_shift_ptr[rc != 0]) >> 15;
249 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
250 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
259 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
260 const int16_t *scan, const int16_t *iscan) {
261 MACROBLOCKD *const xd = &x->e_mbd;
262 struct macroblock_plane *p = &x->plane[plane];
263 struct macroblockd_plane *pd = &xd->plane[plane];
265 vp9_quantize_b(BLOCK_OFFSET(p->coeff, block),
267 p->zbin, p->round, p->quant, p->quant_shift,
268 BLOCK_OFFSET(p->qcoeff, block),
269 BLOCK_OFFSET(pd->dqcoeff, block),
270 pd->dequant, p->zbin_extra, &p->eobs[block], scan, iscan);
273 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
277 for (l = 0; t > 1; l++)
279 t = 1 + (1 << (16 + l)) / d;
280 *quant = (int16_t)(t - (1 << 16));
281 *shift = 1 << (16 - l);
284 void vp9_init_quantizer(VP9_COMP *cpi) {
285 VP9_COMMON *const cm = &cpi->common;
286 QUANTS *const quants = &cpi->quants;
289 for (q = 0; q < QINDEX_RANGE; q++) {
290 const int qzbin_factor = q == 0 ? 64 : (vp9_dc_quant(q, 0) < 148 ? 84 : 80);
291 const int qrounding_factor = q == 0 ? 64 : 48;
293 for (i = 0; i < 2; ++i) {
294 int qrounding_factor_fp = i == 0 ? 48 : 42;
296 qrounding_factor_fp = 64;
299 quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q)
300 : vp9_ac_quant(q, 0);
301 invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
302 quants->y_quant_fp[q][i] = (1 << 16) / quant;
303 quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
304 quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
305 quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
306 cm->y_dequant[q][i] = quant;
309 quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q)
310 : vp9_ac_quant(q, cm->uv_ac_delta_q);
311 invert_quant(&quants->uv_quant[q][i],
312 &quants->uv_quant_shift[q][i], quant);
313 quants->uv_quant_fp[q][i] = (1 << 16) / quant;
314 quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
315 quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
316 quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
317 cm->uv_dequant[q][i] = quant;
320 for (i = 2; i < 8; i++) {
321 quants->y_quant[q][i] = quants->y_quant[q][1];
322 quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
323 quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
324 quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
325 quants->y_zbin[q][i] = quants->y_zbin[q][1];
326 quants->y_round[q][i] = quants->y_round[q][1];
327 cm->y_dequant[q][i] = cm->y_dequant[q][1];
329 quants->uv_quant[q][i] = quants->uv_quant[q][1];
330 quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
331 quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
332 quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
333 quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
334 quants->uv_round[q][i] = quants->uv_round[q][1];
335 cm->uv_dequant[q][i] = cm->uv_dequant[q][1];
340 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
341 const VP9_COMMON *const cm = &cpi->common;
342 MACROBLOCKD *const xd = &x->e_mbd;
343 QUANTS *const quants = &cpi->quants;
344 const int segment_id = xd->mi[0]->mbmi.segment_id;
345 const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
346 const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
347 const int zbin = cpi->zbin_mode_boost;
351 x->plane[0].quant = quants->y_quant[qindex];
352 x->plane[0].quant_fp = quants->y_quant_fp[qindex];
353 x->plane[0].round_fp = quants->y_round_fp[qindex];
354 x->plane[0].quant_shift = quants->y_quant_shift[qindex];
355 x->plane[0].zbin = quants->y_zbin[qindex];
356 x->plane[0].round = quants->y_round[qindex];
357 x->plane[0].zbin_extra = (int16_t)((cm->y_dequant[qindex][1] * zbin) >> 7);
358 xd->plane[0].dequant = cm->y_dequant[qindex];
361 for (i = 1; i < 3; i++) {
362 x->plane[i].quant = quants->uv_quant[qindex];
363 x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
364 x->plane[i].round_fp = quants->uv_round_fp[qindex];
365 x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
366 x->plane[i].zbin = quants->uv_zbin[qindex];
367 x->plane[i].round = quants->uv_round[qindex];
368 x->plane[i].zbin_extra = (int16_t)((cm->uv_dequant[qindex][1] * zbin) >> 7);
369 xd->plane[i].dequant = cm->uv_dequant[qindex];
372 x->skip_block = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
375 x->errorperbit = rdmult >> 6;
376 x->errorperbit += (x->errorperbit == 0);
378 vp9_initialize_me_consts(cpi, x->q_index);
381 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) {
382 const int qindex = x->q_index;
383 const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] *
384 cpi->zbin_mode_boost) >> 7;
385 const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] *
386 cpi->zbin_mode_boost) >> 7;
388 x->plane[0].zbin_extra = (int16_t)y_zbin_extra;
389 x->plane[1].zbin_extra = (int16_t)uv_zbin_extra;
390 x->plane[2].zbin_extra = (int16_t)uv_zbin_extra;
393 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
394 cpi->zbin_mode_boost = 0;
395 vp9_init_plane_quantizers(cpi, &cpi->mb);
398 void vp9_set_quantizer(VP9_COMMON *cm, int q) {
399 // quantizer has to be reinitialized with vp9_init_quantizer() if any
402 cm->y_dc_delta_q = 0;
403 cm->uv_dc_delta_q = 0;
404 cm->uv_ac_delta_q = 0;
407 // Table that converts 0-63 Q-range values passed in outside to the Qindex
408 // range used internally.
409 static const int quantizer_to_qindex[] = {
410 0, 4, 8, 12, 16, 20, 24, 28,
411 32, 36, 40, 44, 48, 52, 56, 60,
412 64, 68, 72, 76, 80, 84, 88, 92,
413 96, 100, 104, 108, 112, 116, 120, 124,
414 128, 132, 136, 140, 144, 148, 152, 156,
415 160, 164, 168, 172, 176, 180, 184, 188,
416 192, 196, 200, 204, 208, 212, 216, 220,
417 224, 228, 232, 236, 240, 244, 249, 255,
420 int vp9_quantizer_to_qindex(int quantizer) {
421 return quantizer_to_qindex[quantizer];
424 int vp9_qindex_to_quantizer(int qindex) {
427 for (quantizer = 0; quantizer < 64; ++quantizer)
428 if (quantizer_to_qindex[quantizer] >= qindex)