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 tran_low_t *coeff_ptr, int skip_block,
23 const int16_t *round_ptr, const int16_t quant,
24 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
25 const int16_t dequant_ptr, uint16_t *eob_ptr) {
27 const int coeff = coeff_ptr[rc];
28 const int coeff_sign = (coeff >> 31);
29 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
33 tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
34 tmp = (tmp * quant) >> 16;
35 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
36 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
43 #if CONFIG_VP9_HIGHBITDEPTH
44 void vp9_high_quantize_dc(const tran_low_t *coeff_ptr, int skip_block,
45 const int16_t *round_ptr, const int16_t quant,
46 tran_low_t *qcoeff_ptr, tran_low_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;
57 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) *
59 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
60 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
68 void vp9_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block,
69 const int16_t *round_ptr, const int16_t quant,
70 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
71 const int16_t dequant_ptr, uint16_t *eob_ptr) {
73 const int coeff = coeff_ptr[rc];
74 const int coeff_sign = (coeff >> 31);
75 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
80 tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
81 tmp = (tmp * quant) >> 15;
82 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
83 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
90 #if CONFIG_VP9_HIGHBITDEPTH
91 void vp9_high_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block,
92 const int16_t *round_ptr, const int16_t quant,
93 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
94 const int16_t dequant_ptr, uint16_t *eob_ptr) {
99 const int coeff = coeff_ptr[rc];
100 const int coeff_sign = (coeff >> 31);
101 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
104 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) *
106 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
107 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
115 void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
117 const int16_t *zbin_ptr, const int16_t *round_ptr,
118 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
119 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
120 const int16_t *dequant_ptr,
121 int zbin_oq_value, uint16_t *eob_ptr,
122 const int16_t *scan, const int16_t *iscan) {
124 // TODO(jingning) Decide the need of these arguments after the
125 // quantization process is completed.
127 (void)quant_shift_ptr;
131 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
132 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
135 // Quantization pass: All coefficients with index >= zero_flag are
136 // skippable. Note: zero_flag can be zero.
137 for (i = 0; i < n_coeffs; i++) {
138 const int rc = scan[i];
139 const int coeff = coeff_ptr[rc];
140 const int coeff_sign = (coeff >> 31);
141 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
143 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
144 tmp = (tmp * quant_ptr[rc != 0]) >> 16;
146 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
147 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
156 #if CONFIG_VP9_HIGHBITDEPTH
157 void vp9_high_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t count,
158 int skip_block, const int16_t *zbin_ptr,
159 const int16_t *round_ptr, const int16_t *quant_ptr,
160 const int16_t *quant_shift_ptr,
161 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
162 const int16_t *dequant_ptr,
163 int zbin_oq_value, uint16_t *eob_ptr,
164 const int16_t *scan, const int16_t *iscan) {
167 // TODO(jingning) Decide the need of these arguments after the
168 // quantization process is completed.
170 (void)quant_shift_ptr;
174 vpx_memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr));
175 vpx_memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr));
178 // Quantization pass: All coefficients with index >= zero_flag are
179 // skippable. Note: zero_flag can be zero.
180 for (i = 0; i < count; i++) {
181 const int rc = scan[i];
182 const int coeff = coeff_ptr[rc];
183 const int coeff_sign = (coeff >> 31);
184 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
187 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) *
188 quant_ptr[rc != 0]) >> 16;
190 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
191 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
201 // TODO(jingning) Refactor this file and combine functions with similar
203 void vp9_quantize_fp_32x32_c(const tran_low_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 tran_low_t *qcoeff_ptr, tran_low_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) {
214 (void)quant_shift_ptr;
218 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
219 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
222 for (i = 0; i < n_coeffs; i++) {
223 const int rc = scan[i];
224 const int coeff = coeff_ptr[rc];
225 const int coeff_sign = (coeff >> 31);
227 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
229 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
230 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
231 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
232 tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
233 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
234 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
244 #if CONFIG_VP9_HIGHBITDEPTH
245 void vp9_high_quantize_fp_32x32_c(const tran_low_t *coeff_ptr,
246 intptr_t n_coeffs, int skip_block,
247 const int16_t *zbin_ptr,
248 const int16_t *round_ptr,
249 const int16_t *quant_ptr,
250 const int16_t *quant_shift_ptr,
251 tran_low_t *qcoeff_ptr,
252 tran_low_t *dqcoeff_ptr,
253 const int16_t *dequant_ptr,
254 int zbin_oq_value, uint16_t *eob_ptr,
255 const int16_t *scan, const int16_t *iscan) {
258 (void)quant_shift_ptr;
262 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
263 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
266 for (i = 0; i < n_coeffs; i++) {
267 const int rc = scan[i];
268 const int coeff = coeff_ptr[rc];
269 const int coeff_sign = (coeff >> 31);
271 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
273 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
274 tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
275 INT32_MIN, INT32_MAX);
276 tmp = (tmp * quant_ptr[rc != 0]) >> 15;
277 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
278 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
289 void vp9_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
291 const int16_t *zbin_ptr, const int16_t *round_ptr,
292 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
293 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
294 const int16_t *dequant_ptr,
295 int zbin_oq_value, uint16_t *eob_ptr,
296 const int16_t *scan, const int16_t *iscan) {
297 int i, non_zero_count = (int)n_coeffs, eob = -1;
298 const int zbins[2] = { zbin_ptr[0] + zbin_oq_value,
299 zbin_ptr[1] + zbin_oq_value };
300 const int nzbins[2] = { zbins[0] * -1,
304 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
305 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
309 for (i = (int)n_coeffs - 1; i >= 0; i--) {
310 const int rc = scan[i];
311 const int coeff = coeff_ptr[rc];
313 if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
319 // Quantization pass: All coefficients with index >= zero_flag are
320 // skippable. Note: zero_flag can be zero.
321 for (i = 0; i < non_zero_count; i++) {
322 const int rc = scan[i];
323 const int coeff = coeff_ptr[rc];
324 const int coeff_sign = (coeff >> 31);
325 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
327 if (abs_coeff >= zbins[rc != 0]) {
328 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
329 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
330 quant_shift_ptr[rc != 0]) >> 16; // quantization
331 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
332 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
342 #if CONFIG_VP9_HIGHBITDEPTH
343 void vp9_high_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
344 int skip_block, const int16_t *zbin_ptr,
345 const int16_t *round_ptr, const int16_t *quant_ptr,
346 const int16_t *quant_shift_ptr,
347 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
348 const int16_t *dequant_ptr, int zbin_oq_value,
349 uint16_t *eob_ptr, const int16_t *scan,
350 const int16_t *iscan) {
351 int i, non_zero_count = (int)n_coeffs, eob = -1;
352 const int zbins[2] = { zbin_ptr[0] + zbin_oq_value,
353 zbin_ptr[1] + zbin_oq_value };
354 const int nzbins[2] = { zbins[0] * -1,
358 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
359 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
363 for (i = (int)n_coeffs - 1; i >= 0; i--) {
364 const int rc = scan[i];
365 const int coeff = coeff_ptr[rc];
367 if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
373 // Quantization pass: All coefficients with index >= zero_flag are
374 // skippable. Note: zero_flag can be zero.
375 for (i = 0; i < non_zero_count; i++) {
376 const int rc = scan[i];
377 const int coeff = coeff_ptr[rc];
378 const int coeff_sign = (coeff >> 31);
379 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
381 if (abs_coeff >= zbins[rc != 0]) {
382 int64_t tmp = clamp(abs_coeff + round_ptr[rc != 0],
383 INT32_MIN, INT32_MAX);
384 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
385 quant_shift_ptr[rc != 0]) >> 16; // quantization
386 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
387 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
398 void vp9_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
400 const int16_t *zbin_ptr, const int16_t *round_ptr,
401 const int16_t *quant_ptr,
402 const int16_t *quant_shift_ptr,
403 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
404 const int16_t *dequant_ptr,
405 int zbin_oq_value, uint16_t *eob_ptr,
406 const int16_t *scan, const int16_t *iscan) {
407 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1),
408 ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1) };
409 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
416 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
417 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
421 for (i = 0; i < n_coeffs; i++) {
422 const int rc = scan[i];
423 const int coeff = coeff_ptr[rc];
425 // If the coefficient is out of the base ZBIN range, keep it for
427 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
431 // Quantization pass: only process the coefficients selected in
432 // pre-scan pass. Note: idx can be zero.
433 for (i = 0; i < idx; i++) {
434 const int rc = scan[idx_arr[i]];
435 const int coeff = coeff_ptr[rc];
436 const int coeff_sign = (coeff >> 31);
438 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
439 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
440 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
441 tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) *
442 quant_shift_ptr[rc != 0]) >> 15;
444 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
445 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
454 #if CONFIG_VP9_HIGHBITDEPTH
455 void vp9_high_quantize_b_32x32_c(const tran_low_t *coeff_ptr,
456 intptr_t n_coeffs, int skip_block,
457 const int16_t *zbin_ptr,
458 const int16_t *round_ptr,
459 const int16_t *quant_ptr,
460 const int16_t *quant_shift_ptr,
461 tran_low_t *qcoeff_ptr,
462 tran_low_t *dqcoeff_ptr,
463 const int16_t *dequant_ptr,
464 int zbin_oq_value, uint16_t *eob_ptr,
465 const int16_t *scan, const int16_t *iscan) {
466 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1),
467 ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1) };
468 const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
475 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
476 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
480 for (i = 0; i < n_coeffs; i++) {
481 const int rc = scan[i];
482 const int coeff = coeff_ptr[rc];
484 // If the coefficient is out of the base ZBIN range, keep it for
486 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
490 // Quantization pass: only process the coefficients selected in
491 // pre-scan pass. Note: idx can be zero.
492 for (i = 0; i < idx; i++) {
493 const int rc = scan[idx_arr[i]];
494 const int coeff = coeff_ptr[rc];
495 const int coeff_sign = (coeff >> 31);
496 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
497 int64_t tmp = clamp(abs_coeff +
498 ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
499 INT32_MIN, INT32_MAX);
500 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
501 quant_shift_ptr[rc != 0]) >> 15;
503 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
504 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
514 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
515 const int16_t *scan, const int16_t *iscan) {
516 MACROBLOCKD *const xd = &x->e_mbd;
517 struct macroblock_plane *p = &x->plane[plane];
518 struct macroblockd_plane *pd = &xd->plane[plane];
520 #if CONFIG_VP9_HIGHBITDEPTH
521 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
522 vp9_high_quantize_b(BLOCK_OFFSET(p->coeff, block),
524 p->zbin, p->round, p->quant, p->quant_shift,
525 BLOCK_OFFSET(p->qcoeff, block),
526 BLOCK_OFFSET(pd->dqcoeff, block),
527 pd->dequant, p->zbin_extra, &p->eobs[block],
532 vp9_quantize_b(BLOCK_OFFSET(p->coeff, block),
534 p->zbin, p->round, p->quant, p->quant_shift,
535 BLOCK_OFFSET(p->qcoeff, block),
536 BLOCK_OFFSET(pd->dqcoeff, block),
537 pd->dequant, p->zbin_extra, &p->eobs[block], scan, iscan);
540 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
544 for (l = 0; t > 1; l++)
546 t = 1 + (1 << (16 + l)) / d;
547 *quant = (int16_t)(t - (1 << 16));
548 *shift = 1 << (16 - l);
551 static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
552 const int quant = vp9_dc_quant(q, 0, bit_depth);
553 #if CONFIG_VP9_HIGHBITDEPTH
556 return q == 0 ? 64 : (quant < 148 ? 84 : 80);
558 return q == 0 ? 64 : (quant < 592 ? 84 : 80);
560 return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
562 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
567 return q == 0 ? 64 : (quant < 148 ? 84 : 80);
571 void vp9_init_quantizer(VP9_COMP *cpi) {
572 VP9_COMMON *const cm = &cpi->common;
573 QUANTS *const quants = &cpi->quants;
576 for (q = 0; q < QINDEX_RANGE; q++) {
577 const int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
578 const int qrounding_factor = q == 0 ? 64 : 48;
580 for (i = 0; i < 2; ++i) {
581 int qrounding_factor_fp = i == 0 ? 48 : 42;
583 qrounding_factor_fp = 64;
586 quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
587 : vp9_ac_quant(q, 0, cm->bit_depth);
588 invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
589 quants->y_quant_fp[q][i] = (1 << 16) / quant;
590 quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
591 quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
592 quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
593 cm->y_dequant[q][i] = quant;
596 quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
597 : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
598 invert_quant(&quants->uv_quant[q][i],
599 &quants->uv_quant_shift[q][i], quant);
600 quants->uv_quant_fp[q][i] = (1 << 16) / quant;
601 quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
602 quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
603 quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
604 cm->uv_dequant[q][i] = quant;
607 for (i = 2; i < 8; i++) {
608 quants->y_quant[q][i] = quants->y_quant[q][1];
609 quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
610 quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
611 quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
612 quants->y_zbin[q][i] = quants->y_zbin[q][1];
613 quants->y_round[q][i] = quants->y_round[q][1];
614 cm->y_dequant[q][i] = cm->y_dequant[q][1];
616 quants->uv_quant[q][i] = quants->uv_quant[q][1];
617 quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
618 quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
619 quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
620 quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
621 quants->uv_round[q][i] = quants->uv_round[q][1];
622 cm->uv_dequant[q][i] = cm->uv_dequant[q][1];
627 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
628 const VP9_COMMON *const cm = &cpi->common;
629 MACROBLOCKD *const xd = &x->e_mbd;
630 QUANTS *const quants = &cpi->quants;
631 const int segment_id = xd->mi[0].src_mi->mbmi.segment_id;
632 const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
633 const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
634 const int zbin = cpi->zbin_mode_boost;
638 x->plane[0].quant = quants->y_quant[qindex];
639 x->plane[0].quant_fp = quants->y_quant_fp[qindex];
640 x->plane[0].round_fp = quants->y_round_fp[qindex];
641 x->plane[0].quant_shift = quants->y_quant_shift[qindex];
642 x->plane[0].zbin = quants->y_zbin[qindex];
643 x->plane[0].round = quants->y_round[qindex];
644 x->plane[0].quant_thred[0] = cm->y_dequant[qindex][0] *
645 cm->y_dequant[qindex][0];
646 x->plane[0].quant_thred[1] = cm->y_dequant[qindex][1] *
647 cm->y_dequant[qindex][1];
648 x->plane[0].zbin_extra = (int16_t)((cm->y_dequant[qindex][1] * zbin) >> 7);
649 xd->plane[0].dequant = cm->y_dequant[qindex];
652 for (i = 1; i < 3; i++) {
653 x->plane[i].quant = quants->uv_quant[qindex];
654 x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
655 x->plane[i].round_fp = quants->uv_round_fp[qindex];
656 x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
657 x->plane[i].zbin = quants->uv_zbin[qindex];
658 x->plane[i].round = quants->uv_round[qindex];
659 x->plane[i].quant_thred[0] = cm->y_dequant[qindex][0] *
660 cm->y_dequant[qindex][0];
661 x->plane[i].quant_thred[1] = cm->y_dequant[qindex][1] *
662 cm->y_dequant[qindex][1];
663 x->plane[i].zbin_extra = (int16_t)((cm->uv_dequant[qindex][1] * zbin) >> 7);
664 xd->plane[i].dequant = cm->uv_dequant[qindex];
667 x->skip_block = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
670 x->errorperbit = rdmult >> 6;
671 x->errorperbit += (x->errorperbit == 0);
673 vp9_initialize_me_consts(cpi, x->q_index);
676 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) {
677 const int qindex = x->q_index;
678 const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] *
679 cpi->zbin_mode_boost) >> 7;
680 const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] *
681 cpi->zbin_mode_boost) >> 7;
683 x->plane[0].zbin_extra = (int16_t)y_zbin_extra;
684 x->plane[1].zbin_extra = (int16_t)uv_zbin_extra;
685 x->plane[2].zbin_extra = (int16_t)uv_zbin_extra;
688 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
689 cpi->zbin_mode_boost = 0;
690 vp9_init_plane_quantizers(cpi, &cpi->mb);
693 void vp9_set_quantizer(VP9_COMMON *cm, int q) {
694 // quantizer has to be reinitialized with vp9_init_quantizer() if any
697 cm->y_dc_delta_q = 0;
698 cm->uv_dc_delta_q = 0;
699 cm->uv_ac_delta_q = 0;
702 // Table that converts 0-63 Q-range values passed in outside to the Qindex
703 // range used internally.
704 static const int quantizer_to_qindex[] = {
705 0, 4, 8, 12, 16, 20, 24, 28,
706 32, 36, 40, 44, 48, 52, 56, 60,
707 64, 68, 72, 76, 80, 84, 88, 92,
708 96, 100, 104, 108, 112, 116, 120, 124,
709 128, 132, 136, 140, 144, 148, 152, 156,
710 160, 164, 168, 172, 176, 180, 184, 188,
711 192, 196, 200, 204, 208, 212, 216, 220,
712 224, 228, 232, 236, 240, 244, 249, 255,
715 int vp9_quantizer_to_qindex(int quantizer) {
716 return quantizer_to_qindex[quantizer];
719 int vp9_qindex_to_quantizer(int qindex) {
722 for (quantizer = 0; quantizer < 64; ++quantizer)
723 if (quantizer_to_qindex[quantizer] >= qindex)