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.
12 #include "vpx_mem/vpx_mem.h"
14 #include "vp9/encoder/vp9_onyx_int.h"
15 #include "vp9/encoder/vp9_rdopt.h"
16 #include "vp9/encoder/vp9_quantize.h"
17 #include "vp9/common/vp9_quant_common.h"
19 #include "vp9/common/vp9_seg_common.h"
25 void vp9_quantize_b_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
27 const int16_t *zbin_ptr, const int16_t *round_ptr,
28 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
29 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
30 const int16_t *dequant_ptr,
31 int zbin_oq_value, uint16_t *eob_ptr,
32 const int16_t *scan, const int16_t *iscan) {
34 int zbins[2], nzbins[2], zbin;
36 int zero_flag = n_coeffs;
38 vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
39 vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
44 zbins[0] = zbin_ptr[0] + zbin_oq_value;
45 zbins[1] = zbin_ptr[1] + zbin_oq_value;
46 nzbins[0] = zbins[0] * -1;
47 nzbins[1] = zbins[1] * -1;
51 for (i = n_coeffs - 1; i >= 0; i--) {
55 if (z < zbins[rc != 0] && z > nzbins[rc != 0]) {
62 // Quantization pass: All coefficients with index >= zero_flag are
63 // skippable. Note: zero_flag can be zero.
64 for (i = 0; i < zero_flag; i++) {
68 zbin = (zbins[rc != 0]);
70 sz = (z >> 31); // sign of z
74 x += (round_ptr[rc != 0]);
75 x = clamp(x, INT16_MIN, INT16_MAX);
76 y = (((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x)) *
77 quant_shift_ptr[rc != 0]) >> 16; // quantize (x)
78 x = (y ^ sz) - sz; // get the sign back
79 qcoeff_ptr[rc] = x; // write to destination
80 dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0]; // dequantized value
83 eob = i; // last nonzero coeffs
91 void vp9_quantize_b_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
93 const int16_t *zbin_ptr, const int16_t *round_ptr,
94 const int16_t *quant_ptr,
95 const int16_t *quant_shift_ptr,
96 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
97 const int16_t *dequant_ptr,
98 int zbin_oq_value, uint16_t *eob_ptr,
99 const int16_t *scan, const int16_t *iscan) {
101 int zbins[2], nzbins[2];
106 vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
107 vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
112 zbins[0] = ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1);
113 zbins[1] = ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1);
114 nzbins[0] = zbins[0] * -1;
115 nzbins[1] = zbins[1] * -1;
119 for (i = 0; i < n_coeffs; i++) {
123 // If the coefficient is out of the base ZBIN range, keep it for
125 if (z >= zbins[rc != 0] || z <= nzbins[rc != 0])
129 // Quantization pass: only process the coefficients selected in
130 // pre-scan pass. Note: idx can be zero.
131 for (i = 0; i < idx; i++) {
132 rc = scan[idx_arr[i]];
135 sz = (z >> 31); // sign of z
136 x = (z ^ sz) - sz; // x = abs(z)
138 x += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
139 x = clamp(x, INT16_MIN, INT16_MAX);
140 y = ((((x * quant_ptr[rc != 0]) >> 16) + x) *
141 quant_shift_ptr[rc != 0]) >> 15; // quantize (x)
143 x = (y ^ sz) - sz; // get the sign back
144 qcoeff_ptr[rc] = x; // write to destination
145 dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0] / 2; // dequantized value
148 eob = idx_arr[i]; // last nonzero coeffs
154 struct plane_block_idx {
159 // TODO(jkoleszar): returning a struct so it can be used in a const context,
160 // expect to refactor this further later.
161 static INLINE struct plane_block_idx plane_block_idx(int y_blocks,
163 const int v_offset = y_blocks * 5 / 4;
164 struct plane_block_idx res;
166 if (b_idx < y_blocks) {
169 } else if (b_idx < v_offset) {
171 res.block = b_idx - y_blocks;
173 assert(b_idx < y_blocks * 3 / 2);
175 res.block = b_idx - v_offset;
180 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int y_blocks, int b_idx,
181 const int16_t *scan, const int16_t *iscan) {
182 MACROBLOCKD *const xd = &x->e_mbd;
183 const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, b_idx);
184 struct macroblock_plane* p = &x->plane[pb_idx.plane];
185 struct macroblockd_plane* pd = &xd->plane[pb_idx.plane];
187 vp9_quantize_b(BLOCK_OFFSET(p->coeff, pb_idx.block),
189 p->zbin, p->round, p->quant, p->quant_shift,
190 BLOCK_OFFSET(pd->qcoeff, pb_idx.block),
191 BLOCK_OFFSET(pd->dqcoeff, pb_idx.block),
192 pd->dequant, p->zbin_extra, &pd->eobs[pb_idx.block], scan, iscan);
195 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
199 for (l = 0; t > 1; l++)
201 t = 1 + (1 << (16 + l)) / d;
202 *quant = (int16_t)(t - (1 << 16));
203 *shift = 1 << (16 - l);
206 void vp9_init_quantizer(VP9_COMP *cpi) {
208 VP9_COMMON *const cm = &cpi->common;
210 for (q = 0; q < QINDEX_RANGE; q++) {
211 const int qzbin_factor = q == 0 ? 64 : (vp9_dc_quant(q, 0) < 148 ? 84 : 80);
212 const int qrounding_factor = q == 0 ? 64 : 48;
215 for (i = 0; i < 2; ++i) {
216 const int quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q)
217 : vp9_ac_quant(q, 0);
218 invert_quant(&cpi->y_quant[q][i], &cpi->y_quant_shift[q][i], quant);
219 cpi->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
220 cpi->y_round[q][i] = (qrounding_factor * quant) >> 7;
221 cm->y_dequant[q][i] = quant;
225 for (i = 0; i < 2; ++i) {
226 const int quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q)
227 : vp9_ac_quant(q, cm->uv_ac_delta_q);
228 invert_quant(&cpi->uv_quant[q][i], &cpi->uv_quant_shift[q][i], quant);
229 cpi->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
230 cpi->uv_round[q][i] = (qrounding_factor * quant) >> 7;
231 cm->uv_dequant[q][i] = quant;
236 for (i = 0; i < 2; ++i) {
237 const int quant = i == 0 ? vp9_dc_quant(q, cm->a_dc_delta_q)
238 : vp9_ac_quant(q, cm->a_ac_delta_q);
239 invert_quant(&cpi->a_quant[q][i], &cpi->a_quant_shift[q][i], quant);
240 cpi->a_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
241 cpi->a_round[q][i] = (qrounding_factor * quant) >> 7;
242 cm->a_dequant[q][i] = quant;
246 for (i = 2; i < 8; i++) {
247 cpi->y_quant[q][i] = cpi->y_quant[q][1];
248 cpi->y_quant_shift[q][i] = cpi->y_quant_shift[q][1];
249 cpi->y_zbin[q][i] = cpi->y_zbin[q][1];
250 cpi->y_round[q][i] = cpi->y_round[q][1];
251 cm->y_dequant[q][i] = cm->y_dequant[q][1];
253 cpi->uv_quant[q][i] = cpi->uv_quant[q][1];
254 cpi->uv_quant_shift[q][i] = cpi->uv_quant_shift[q][1];
255 cpi->uv_zbin[q][i] = cpi->uv_zbin[q][1];
256 cpi->uv_round[q][i] = cpi->uv_round[q][1];
257 cm->uv_dequant[q][i] = cm->uv_dequant[q][1];
260 cpi->a_quant[q][i] = cpi->a_quant[q][1];
261 cpi->a_quant_shift[q][i] = cpi->a_quant_shift[q][1];
262 cpi->a_zbin[q][i] = cpi->a_zbin[q][1];
263 cpi->a_round[q][i] = cpi->a_round[q][1];
264 cm->a_dequant[q][i] = cm->a_dequant[q][1];
270 void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) {
272 VP9_COMMON *const cm = &cpi->common;
273 MACROBLOCKD *xd = &x->e_mbd;
275 int segment_id = xd->mi_8x8[0]->mbmi.segment_id;
276 const int qindex = vp9_get_qindex(&cpi->common.seg, segment_id,
277 cpi->common.base_qindex);
279 int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
282 zbin_extra = (cpi->common.y_dequant[qindex][1] *
283 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
285 x->plane[0].quant = cpi->y_quant[qindex];
286 x->plane[0].quant_shift = cpi->y_quant_shift[qindex];
287 x->plane[0].zbin = cpi->y_zbin[qindex];
288 x->plane[0].round = cpi->y_round[qindex];
289 x->plane[0].zbin_extra = (int16_t)zbin_extra;
290 x->e_mbd.plane[0].dequant = cpi->common.y_dequant[qindex];
293 zbin_extra = (cpi->common.uv_dequant[qindex][1] *
294 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
296 for (i = 1; i < 3; i++) {
297 x->plane[i].quant = cpi->uv_quant[qindex];
298 x->plane[i].quant_shift = cpi->uv_quant_shift[qindex];
299 x->plane[i].zbin = cpi->uv_zbin[qindex];
300 x->plane[i].round = cpi->uv_round[qindex];
301 x->plane[i].zbin_extra = (int16_t)zbin_extra;
302 x->e_mbd.plane[i].dequant = cpi->common.uv_dequant[qindex];
306 x->plane[3].quant = cpi->a_quant[qindex];
307 x->plane[3].quant_shift = cpi->a_quant_shift[qindex];
308 x->plane[3].zbin = cpi->a_zbin[qindex];
309 x->plane[3].round = cpi->a_round[qindex];
310 x->plane[3].zbin_extra = (int16_t)zbin_extra;
311 x->e_mbd.plane[3].dequant = cpi->common.a_dequant[qindex];
314 x->skip_block = vp9_segfeature_active(&cpi->common.seg, segment_id,
317 /* save this macroblock QIndex for vp9_update_zbin_extra() */
318 x->e_mbd.q_index = qindex;
321 cpi->mb.errorperbit = rdmult >> 6;
322 cpi->mb.errorperbit += (cpi->mb.errorperbit == 0);
324 vp9_initialize_me_consts(cpi, xd->q_index);
327 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) {
328 const int qindex = x->e_mbd.q_index;
329 const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] *
330 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
331 const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] *
332 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
334 x->plane[0].zbin_extra = (int16_t)y_zbin_extra;
335 x->plane[1].zbin_extra = (int16_t)uv_zbin_extra;
336 x->plane[2].zbin_extra = (int16_t)uv_zbin_extra;
339 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
340 // Clear Zbin mode boost for default case
341 cpi->zbin_mode_boost = 0;
343 // MB level quantizer setup
344 vp9_mb_init_quantizer(cpi, &cpi->mb);
347 void vp9_set_quantizer(struct VP9_COMP *cpi, int q) {
348 VP9_COMMON *cm = &cpi->common;
352 // if any of the delta_q values are changing update flag will
354 cm->y_dc_delta_q = 0;
355 cm->uv_dc_delta_q = 0;
356 cm->uv_ac_delta_q = 0;
358 // quantizer has to be reinitialized if any delta_q changes.
359 // As there are not any here for now this is inactive code.
361 // vp9_init_quantizer(cpi);