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"
14 #include "vpx_ports/mem.h"
16 #include "vp9/common/vp9_quant_common.h"
17 #include "vp9/common/vp9_seg_common.h"
19 #include "vp9/encoder/vp9_encoder.h"
20 #include "vp9/encoder/vp9_quantize.h"
21 #include "vp9/encoder/vp9_rd.h"
23 void vp9_quantize_dc(const tran_low_t *coeff_ptr,
24 int n_coeffs, int skip_block,
25 const int16_t *round_ptr, const int16_t quant,
26 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
27 const int16_t dequant_ptr, uint16_t *eob_ptr) {
29 const int coeff = coeff_ptr[rc];
30 const int coeff_sign = (coeff >> 31);
31 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
34 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
35 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
38 tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
39 tmp = (tmp * quant) >> 16;
40 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
41 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
48 #if CONFIG_VP9_HIGHBITDEPTH
49 void vp9_highbd_quantize_dc(const tran_low_t *coeff_ptr,
50 int n_coeffs, int skip_block,
51 const int16_t *round_ptr, const int16_t quant,
52 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
53 const int16_t dequant_ptr, uint16_t *eob_ptr) {
56 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
57 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
61 const int coeff = coeff_ptr[rc];
62 const int coeff_sign = (coeff >> 31);
63 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
66 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) *
68 qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
69 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
77 void vp9_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block,
78 const int16_t *round_ptr, const int16_t quant,
79 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
80 const int16_t dequant_ptr, uint16_t *eob_ptr) {
81 const int n_coeffs = 1024;
83 const int coeff = coeff_ptr[rc];
84 const int coeff_sign = (coeff >> 31);
85 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
88 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
89 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
93 tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
94 INT16_MIN, INT16_MAX);
95 tmp = (tmp * quant) >> 15;
96 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
97 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
104 #if CONFIG_VP9_HIGHBITDEPTH
105 void vp9_highbd_quantize_dc_32x32(const tran_low_t *coeff_ptr,
107 const int16_t *round_ptr,
109 tran_low_t *qcoeff_ptr,
110 tran_low_t *dqcoeff_ptr,
111 const int16_t dequant_ptr,
113 const int n_coeffs = 1024;
116 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
117 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
121 const int coeff = coeff_ptr[rc];
122 const int coeff_sign = (coeff >> 31);
123 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
126 (clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
127 INT32_MIN, INT32_MAX) * quant) >> 15;
128 qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
129 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
137 void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
139 const int16_t *zbin_ptr, const int16_t *round_ptr,
140 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
141 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
142 const int16_t *dequant_ptr,
144 const int16_t *scan, const int16_t *iscan) {
146 // TODO(jingning) Decide the need of these arguments after the
147 // quantization process is completed.
149 (void)quant_shift_ptr;
152 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
153 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
156 // Quantization pass: All coefficients with index >= zero_flag are
157 // skippable. Note: zero_flag can be zero.
158 for (i = 0; i < n_coeffs; i++) {
159 const int rc = scan[i];
160 const int coeff = coeff_ptr[rc];
161 const int coeff_sign = (coeff >> 31);
162 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
164 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
165 tmp = (tmp * quant_ptr[rc != 0]) >> 16;
167 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
168 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
177 #if CONFIG_VP9_HIGHBITDEPTH
178 void vp9_highbd_quantize_fp_c(const tran_low_t *coeff_ptr,
181 const int16_t *zbin_ptr,
182 const int16_t *round_ptr,
183 const int16_t *quant_ptr,
184 const int16_t *quant_shift_ptr,
185 tran_low_t *qcoeff_ptr,
186 tran_low_t *dqcoeff_ptr,
187 const int16_t *dequant_ptr,
190 const int16_t *iscan) {
193 // TODO(jingning) Decide the need of these arguments after the
194 // quantization process is completed.
196 (void)quant_shift_ptr;
199 memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr));
200 memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr));
203 // Quantization pass: All coefficients with index >= zero_flag are
204 // skippable. Note: zero_flag can be zero.
205 for (i = 0; i < count; i++) {
206 const int rc = scan[i];
207 const int coeff = coeff_ptr[rc];
208 const int coeff_sign = (coeff >> 31);
209 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
212 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) *
213 quant_ptr[rc != 0]) >> 16;
215 qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
216 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
226 // TODO(jingning) Refactor this file and combine functions with similar
228 void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
230 const int16_t *zbin_ptr, const int16_t *round_ptr,
231 const int16_t *quant_ptr,
232 const int16_t *quant_shift_ptr,
233 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
234 const int16_t *dequant_ptr,
236 const int16_t *scan, const int16_t *iscan) {
239 (void)quant_shift_ptr;
242 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
243 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
246 for (i = 0; i < n_coeffs; i++) {
247 const int rc = scan[i];
248 const int coeff = coeff_ptr[rc];
249 const int coeff_sign = (coeff >> 31);
251 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
253 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
254 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
255 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
256 tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
257 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
258 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
268 #if CONFIG_VP9_HIGHBITDEPTH
269 void vp9_highbd_quantize_fp_32x32_c(const tran_low_t *coeff_ptr,
270 intptr_t n_coeffs, int skip_block,
271 const int16_t *zbin_ptr,
272 const int16_t *round_ptr,
273 const int16_t *quant_ptr,
274 const int16_t *quant_shift_ptr,
275 tran_low_t *qcoeff_ptr,
276 tran_low_t *dqcoeff_ptr,
277 const int16_t *dequant_ptr,
279 const int16_t *scan, const int16_t *iscan) {
282 (void)quant_shift_ptr;
285 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
286 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
289 for (i = 0; i < n_coeffs; i++) {
290 const int rc = scan[i];
291 const int coeff = coeff_ptr[rc];
292 const int coeff_sign = (coeff >> 31);
294 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
296 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
297 tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
298 INT32_MIN, INT32_MAX);
299 tmp = (tmp * quant_ptr[rc != 0]) >> 15;
300 qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
301 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
312 void vp9_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
314 const int16_t *zbin_ptr, const int16_t *round_ptr,
315 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
316 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
317 const int16_t *dequant_ptr,
319 const int16_t *scan, const int16_t *iscan) {
320 int i, non_zero_count = (int)n_coeffs, eob = -1;
321 const int zbins[2] = {zbin_ptr[0], zbin_ptr[1]};
322 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
325 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
326 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
330 for (i = (int)n_coeffs - 1; i >= 0; i--) {
331 const int rc = scan[i];
332 const int coeff = coeff_ptr[rc];
334 if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
340 // Quantization pass: All coefficients with index >= zero_flag are
341 // skippable. Note: zero_flag can be zero.
342 for (i = 0; i < non_zero_count; i++) {
343 const int rc = scan[i];
344 const int coeff = coeff_ptr[rc];
345 const int coeff_sign = (coeff >> 31);
346 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
348 if (abs_coeff >= zbins[rc != 0]) {
349 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
350 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
351 quant_shift_ptr[rc != 0]) >> 16; // quantization
352 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
353 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
363 #if CONFIG_VP9_HIGHBITDEPTH
364 void vp9_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
365 int skip_block, const int16_t *zbin_ptr,
366 const int16_t *round_ptr, const int16_t *quant_ptr,
367 const int16_t *quant_shift_ptr,
368 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
369 const int16_t *dequant_ptr,
370 uint16_t *eob_ptr, const int16_t *scan,
371 const int16_t *iscan) {
372 int i, non_zero_count = (int)n_coeffs, eob = -1;
373 const int zbins[2] = {zbin_ptr[0], zbin_ptr[1]};
374 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
377 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
378 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
382 for (i = (int)n_coeffs - 1; i >= 0; i--) {
383 const int rc = scan[i];
384 const int coeff = coeff_ptr[rc];
386 if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
392 // Quantization pass: All coefficients with index >= zero_flag are
393 // skippable. Note: zero_flag can be zero.
394 for (i = 0; i < non_zero_count; i++) {
395 const int rc = scan[i];
396 const int coeff = coeff_ptr[rc];
397 const int coeff_sign = (coeff >> 31);
398 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
400 if (abs_coeff >= zbins[rc != 0]) {
401 int64_t tmp = clamp(abs_coeff + round_ptr[rc != 0],
402 INT32_MIN, INT32_MAX);
403 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
404 quant_shift_ptr[rc != 0]) >> 16; // quantization
405 qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
406 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
417 void vp9_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
419 const int16_t *zbin_ptr, const int16_t *round_ptr,
420 const int16_t *quant_ptr,
421 const int16_t *quant_shift_ptr,
422 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
423 const int16_t *dequant_ptr,
425 const int16_t *scan, const int16_t *iscan) {
426 const int zbins[2] = {ROUND_POWER_OF_TWO(zbin_ptr[0], 1),
427 ROUND_POWER_OF_TWO(zbin_ptr[1], 1)};
428 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
435 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
436 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
440 for (i = 0; i < n_coeffs; i++) {
441 const int rc = scan[i];
442 const int coeff = coeff_ptr[rc];
444 // If the coefficient is out of the base ZBIN range, keep it for
446 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
450 // Quantization pass: only process the coefficients selected in
451 // pre-scan pass. Note: idx can be zero.
452 for (i = 0; i < idx; i++) {
453 const int rc = scan[idx_arr[i]];
454 const int coeff = coeff_ptr[rc];
455 const int coeff_sign = (coeff >> 31);
457 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
458 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
459 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
460 tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) *
461 quant_shift_ptr[rc != 0]) >> 15;
463 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
464 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
473 #if CONFIG_VP9_HIGHBITDEPTH
474 void vp9_highbd_quantize_b_32x32_c(const tran_low_t *coeff_ptr,
475 intptr_t n_coeffs, int skip_block,
476 const int16_t *zbin_ptr,
477 const int16_t *round_ptr,
478 const int16_t *quant_ptr,
479 const int16_t *quant_shift_ptr,
480 tran_low_t *qcoeff_ptr,
481 tran_low_t *dqcoeff_ptr,
482 const int16_t *dequant_ptr,
484 const int16_t *scan, const int16_t *iscan) {
485 const int zbins[2] = {ROUND_POWER_OF_TWO(zbin_ptr[0], 1),
486 ROUND_POWER_OF_TWO(zbin_ptr[1], 1)};
487 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
494 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
495 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
499 for (i = 0; i < n_coeffs; i++) {
500 const int rc = scan[i];
501 const int coeff = coeff_ptr[rc];
503 // If the coefficient is out of the base ZBIN range, keep it for
505 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
509 // Quantization pass: only process the coefficients selected in
510 // pre-scan pass. Note: idx can be zero.
511 for (i = 0; i < idx; i++) {
512 const int rc = scan[idx_arr[i]];
513 const int coeff = coeff_ptr[rc];
514 const int coeff_sign = (coeff >> 31);
515 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
516 int64_t tmp = clamp(abs_coeff +
517 ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
518 INT32_MIN, INT32_MAX);
519 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
520 quant_shift_ptr[rc != 0]) >> 15;
522 qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
523 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
533 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
534 const int16_t *scan, const int16_t *iscan) {
535 MACROBLOCKD *const xd = &x->e_mbd;
536 struct macroblock_plane *p = &x->plane[plane];
537 struct macroblockd_plane *pd = &xd->plane[plane];
539 #if CONFIG_VP9_HIGHBITDEPTH
540 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
541 vp9_highbd_quantize_b(BLOCK_OFFSET(p->coeff, block),
543 p->zbin, p->round, p->quant, p->quant_shift,
544 BLOCK_OFFSET(p->qcoeff, block),
545 BLOCK_OFFSET(pd->dqcoeff, block),
546 pd->dequant, &p->eobs[block],
551 vp9_quantize_b(BLOCK_OFFSET(p->coeff, block),
553 p->zbin, p->round, p->quant, p->quant_shift,
554 BLOCK_OFFSET(p->qcoeff, block),
555 BLOCK_OFFSET(pd->dqcoeff, block),
556 pd->dequant, &p->eobs[block], scan, iscan);
559 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
563 for (l = 0; t > 1; l++)
565 t = 1 + (1 << (16 + l)) / d;
566 *quant = (int16_t)(t - (1 << 16));
567 *shift = 1 << (16 - l);
570 static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
571 const int quant = vp9_dc_quant(q, 0, bit_depth);
572 #if CONFIG_VP9_HIGHBITDEPTH
575 return q == 0 ? 64 : (quant < 148 ? 84 : 80);
577 return q == 0 ? 64 : (quant < 592 ? 84 : 80);
579 return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
581 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
586 return q == 0 ? 64 : (quant < 148 ? 84 : 80);
590 void vp9_init_quantizer(VP9_COMP *cpi) {
591 VP9_COMMON *const cm = &cpi->common;
592 QUANTS *const quants = &cpi->quants;
595 for (q = 0; q < QINDEX_RANGE; q++) {
596 const int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
597 const int qrounding_factor = q == 0 ? 64 : 48;
599 for (i = 0; i < 2; ++i) {
600 int qrounding_factor_fp = i == 0 ? 48 : 42;
602 qrounding_factor_fp = 64;
605 quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
606 : vp9_ac_quant(q, 0, cm->bit_depth);
607 invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
608 quants->y_quant_fp[q][i] = (1 << 16) / quant;
609 quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
610 quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
611 quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
612 cpi->y_dequant[q][i] = quant;
615 quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
616 : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
617 invert_quant(&quants->uv_quant[q][i],
618 &quants->uv_quant_shift[q][i], quant);
619 quants->uv_quant_fp[q][i] = (1 << 16) / quant;
620 quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
621 quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
622 quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
623 cpi->uv_dequant[q][i] = quant;
626 for (i = 2; i < 8; i++) {
627 quants->y_quant[q][i] = quants->y_quant[q][1];
628 quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
629 quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
630 quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
631 quants->y_zbin[q][i] = quants->y_zbin[q][1];
632 quants->y_round[q][i] = quants->y_round[q][1];
633 cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
635 quants->uv_quant[q][i] = quants->uv_quant[q][1];
636 quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
637 quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
638 quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
639 quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
640 quants->uv_round[q][i] = quants->uv_round[q][1];
641 cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
646 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
647 const VP9_COMMON *const cm = &cpi->common;
648 MACROBLOCKD *const xd = &x->e_mbd;
649 QUANTS *const quants = &cpi->quants;
650 const int segment_id = xd->mi[0]->mbmi.segment_id;
651 const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
652 const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
656 x->plane[0].quant = quants->y_quant[qindex];
657 x->plane[0].quant_fp = quants->y_quant_fp[qindex];
658 x->plane[0].round_fp = quants->y_round_fp[qindex];
659 x->plane[0].quant_shift = quants->y_quant_shift[qindex];
660 x->plane[0].zbin = quants->y_zbin[qindex];
661 x->plane[0].round = quants->y_round[qindex];
662 xd->plane[0].dequant = cpi->y_dequant[qindex];
664 x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
665 x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
668 for (i = 1; i < 3; i++) {
669 x->plane[i].quant = quants->uv_quant[qindex];
670 x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
671 x->plane[i].round_fp = quants->uv_round_fp[qindex];
672 x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
673 x->plane[i].zbin = quants->uv_zbin[qindex];
674 x->plane[i].round = quants->uv_round[qindex];
675 xd->plane[i].dequant = cpi->uv_dequant[qindex];
677 x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
678 x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
681 x->skip_block = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
684 x->errorperbit = rdmult >> 6;
685 x->errorperbit += (x->errorperbit == 0);
687 vp9_initialize_me_consts(cpi, x, x->q_index);
690 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
691 vp9_init_plane_quantizers(cpi, &cpi->td.mb);
694 void vp9_set_quantizer(VP9_COMMON *cm, int q) {
695 // quantizer has to be reinitialized with vp9_init_quantizer() if any
698 cm->y_dc_delta_q = 0;
699 cm->uv_dc_delta_q = 0;
700 cm->uv_ac_delta_q = 0;
703 // Table that converts 0-63 Q-range values passed in outside to the Qindex
704 // range used internally.
705 static const int quantizer_to_qindex[] = {
706 0, 4, 8, 12, 16, 20, 24, 28,
707 32, 36, 40, 44, 48, 52, 56, 60,
708 64, 68, 72, 76, 80, 84, 88, 92,
709 96, 100, 104, 108, 112, 116, 120, 124,
710 128, 132, 136, 140, 144, 148, 152, 156,
711 160, 164, 168, 172, 176, 180, 184, 188,
712 192, 196, 200, 204, 208, 212, 216, 220,
713 224, 228, 232, 236, 240, 244, 249, 255,
716 int vp9_quantizer_to_qindex(int quantizer) {
717 return quantizer_to_qindex[quantizer];
720 int vp9_qindex_to_quantizer(int qindex) {
723 for (quantizer = 0; quantizer < 64; ++quantizer)
724 if (quantizer_to_qindex[quantizer] >= qindex)