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
));
60 const int coeff
= coeff_ptr
[0];
61 const int coeff_sign
= (coeff
>> 31);
62 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
63 const int64_t tmp
= abs_coeff
+ round_ptr
[0];
64 const uint32_t abs_qcoeff
= (uint32_t)((tmp
* quant
) >> 16);
65 qcoeff_ptr
[0] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
66 dqcoeff_ptr
[0] = qcoeff_ptr
[0] * dequant_ptr
;
74 void vp9_quantize_dc_32x32(const tran_low_t
*coeff_ptr
, int skip_block
,
75 const int16_t *round_ptr
, const int16_t quant
,
76 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
77 const int16_t dequant_ptr
, uint16_t *eob_ptr
) {
78 const int n_coeffs
= 1024;
80 const int coeff
= coeff_ptr
[rc
];
81 const int coeff_sign
= (coeff
>> 31);
82 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
85 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
86 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
89 tmp
= clamp(abs_coeff
+ ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1),
90 INT16_MIN
, INT16_MAX
);
91 tmp
= (tmp
* quant
) >> 15;
92 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
93 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
/ 2;
100 #if CONFIG_VP9_HIGHBITDEPTH
101 void vp9_highbd_quantize_dc_32x32(const tran_low_t
*coeff_ptr
,
103 const int16_t *round_ptr
,
105 tran_low_t
*qcoeff_ptr
,
106 tran_low_t
*dqcoeff_ptr
,
107 const int16_t dequant_ptr
,
109 const int n_coeffs
= 1024;
112 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
113 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
116 const int coeff
= coeff_ptr
[0];
117 const int coeff_sign
= (coeff
>> 31);
118 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
119 const int64_t tmp
= abs_coeff
+ ROUND_POWER_OF_TWO(round_ptr
[0], 1);
120 const uint32_t abs_qcoeff
= (uint32_t)((tmp
* quant
) >> 15);
121 qcoeff_ptr
[0] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
122 dqcoeff_ptr
[0] = qcoeff_ptr
[0] * dequant_ptr
/ 2;
130 void vp9_quantize_fp_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
132 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
133 const int16_t *quant_ptr
, const int16_t *quant_shift_ptr
,
134 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
135 const int16_t *dequant_ptr
,
137 const int16_t *scan
, const int16_t *iscan
) {
139 // TODO(jingning) Decide the need of these arguments after the
140 // quantization process is completed.
142 (void)quant_shift_ptr
;
145 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
146 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
149 // Quantization pass: All coefficients with index >= zero_flag are
150 // skippable. Note: zero_flag can be zero.
151 for (i
= 0; i
< n_coeffs
; i
++) {
152 const int rc
= scan
[i
];
153 const int coeff
= coeff_ptr
[rc
];
154 const int coeff_sign
= (coeff
>> 31);
155 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
157 int tmp
= clamp(abs_coeff
+ round_ptr
[rc
!= 0], INT16_MIN
, INT16_MAX
);
158 tmp
= (tmp
* quant_ptr
[rc
!= 0]) >> 16;
160 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
161 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
170 #if CONFIG_VP9_HIGHBITDEPTH
171 void vp9_highbd_quantize_fp_c(const tran_low_t
*coeff_ptr
,
174 const int16_t *zbin_ptr
,
175 const int16_t *round_ptr
,
176 const int16_t *quant_ptr
,
177 const int16_t *quant_shift_ptr
,
178 tran_low_t
*qcoeff_ptr
,
179 tran_low_t
*dqcoeff_ptr
,
180 const int16_t *dequant_ptr
,
183 const int16_t *iscan
) {
186 // TODO(jingning) Decide the need of these arguments after the
187 // quantization process is completed.
189 (void)quant_shift_ptr
;
192 memset(qcoeff_ptr
, 0, count
* sizeof(*qcoeff_ptr
));
193 memset(dqcoeff_ptr
, 0, count
* sizeof(*dqcoeff_ptr
));
196 // Quantization pass: All coefficients with index >= zero_flag are
197 // skippable. Note: zero_flag can be zero.
198 for (i
= 0; i
< count
; i
++) {
199 const int rc
= scan
[i
];
200 const int coeff
= coeff_ptr
[rc
];
201 const int coeff_sign
= (coeff
>> 31);
202 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
203 const int64_t tmp
= abs_coeff
+ round_ptr
[rc
!= 0];
204 const uint32_t abs_qcoeff
= (uint32_t)((tmp
* quant_ptr
[rc
!= 0]) >> 16);
205 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
206 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
215 // TODO(jingning) Refactor this file and combine functions with similar
217 void vp9_quantize_fp_32x32_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
219 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
220 const int16_t *quant_ptr
,
221 const int16_t *quant_shift_ptr
,
222 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
223 const int16_t *dequant_ptr
,
225 const int16_t *scan
, const int16_t *iscan
) {
228 (void)quant_shift_ptr
;
231 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
232 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
235 for (i
= 0; i
< n_coeffs
; i
++) {
236 const int rc
= scan
[i
];
237 const int coeff
= coeff_ptr
[rc
];
238 const int coeff_sign
= (coeff
>> 31);
240 int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
242 if (abs_coeff
>= (dequant_ptr
[rc
!= 0] >> 2)) {
243 abs_coeff
+= ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
244 abs_coeff
= clamp(abs_coeff
, INT16_MIN
, INT16_MAX
);
245 tmp
= (abs_coeff
* quant_ptr
[rc
!= 0]) >> 15;
246 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
247 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;
257 #if CONFIG_VP9_HIGHBITDEPTH
258 void vp9_highbd_quantize_fp_32x32_c(const tran_low_t
*coeff_ptr
,
259 intptr_t n_coeffs
, int skip_block
,
260 const int16_t *zbin_ptr
,
261 const int16_t *round_ptr
,
262 const int16_t *quant_ptr
,
263 const int16_t *quant_shift_ptr
,
264 tran_low_t
*qcoeff_ptr
,
265 tran_low_t
*dqcoeff_ptr
,
266 const int16_t *dequant_ptr
,
268 const int16_t *scan
, const int16_t *iscan
) {
271 (void)quant_shift_ptr
;
274 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
275 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
278 for (i
= 0; i
< n_coeffs
; i
++) {
279 uint32_t abs_qcoeff
= 0;
280 const int rc
= scan
[i
];
281 const int coeff
= coeff_ptr
[rc
];
282 const int coeff_sign
= (coeff
>> 31);
283 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
285 if (abs_coeff
>= (dequant_ptr
[rc
!= 0] >> 2)) {
286 const int64_t tmp
= abs_coeff
287 + ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
288 abs_qcoeff
= (uint32_t) ((tmp
* quant_ptr
[rc
!= 0]) >> 15);
289 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
290 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;
301 void vp9_quantize_b_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
303 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
304 const int16_t *quant_ptr
, const int16_t *quant_shift_ptr
,
305 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
306 const int16_t *dequant_ptr
,
308 const int16_t *scan
, const int16_t *iscan
) {
309 int i
, non_zero_count
= (int)n_coeffs
, eob
= -1;
310 const int zbins
[2] = {zbin_ptr
[0], zbin_ptr
[1]};
311 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
314 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
315 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
319 for (i
= (int)n_coeffs
- 1; i
>= 0; i
--) {
320 const int rc
= scan
[i
];
321 const int coeff
= coeff_ptr
[rc
];
323 if (coeff
< zbins
[rc
!= 0] && coeff
> nzbins
[rc
!= 0])
329 // Quantization pass: All coefficients with index >= zero_flag are
330 // skippable. Note: zero_flag can be zero.
331 for (i
= 0; i
< non_zero_count
; i
++) {
332 const int rc
= scan
[i
];
333 const int coeff
= coeff_ptr
[rc
];
334 const int coeff_sign
= (coeff
>> 31);
335 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
337 if (abs_coeff
>= zbins
[rc
!= 0]) {
338 int tmp
= clamp(abs_coeff
+ round_ptr
[rc
!= 0], INT16_MIN
, INT16_MAX
);
339 tmp
= ((((tmp
* quant_ptr
[rc
!= 0]) >> 16) + tmp
) *
340 quant_shift_ptr
[rc
!= 0]) >> 16; // quantization
341 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
342 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
352 #if CONFIG_VP9_HIGHBITDEPTH
353 void vp9_highbd_quantize_b_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
354 int skip_block
, const int16_t *zbin_ptr
,
355 const int16_t *round_ptr
, const int16_t *quant_ptr
,
356 const int16_t *quant_shift_ptr
,
357 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
358 const int16_t *dequant_ptr
,
359 uint16_t *eob_ptr
, const int16_t *scan
,
360 const int16_t *iscan
) {
361 int i
, non_zero_count
= (int)n_coeffs
, eob
= -1;
362 const int zbins
[2] = {zbin_ptr
[0], zbin_ptr
[1]};
363 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
366 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
367 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
371 for (i
= (int)n_coeffs
- 1; i
>= 0; i
--) {
372 const int rc
= scan
[i
];
373 const int coeff
= coeff_ptr
[rc
];
375 if (coeff
< zbins
[rc
!= 0] && coeff
> nzbins
[rc
!= 0])
381 // Quantization pass: All coefficients with index >= zero_flag are
382 // skippable. Note: zero_flag can be zero.
383 for (i
= 0; i
< non_zero_count
; i
++) {
384 const int rc
= scan
[i
];
385 const int coeff
= coeff_ptr
[rc
];
386 const int coeff_sign
= (coeff
>> 31);
387 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
389 if (abs_coeff
>= zbins
[rc
!= 0]) {
390 const int64_t tmp1
= abs_coeff
+ round_ptr
[rc
!= 0];
391 const int64_t tmp2
= ((tmp1
* quant_ptr
[rc
!= 0]) >> 16) + tmp1
;
392 const uint32_t abs_qcoeff
=
393 (uint32_t)((tmp2
* quant_shift_ptr
[rc
!= 0]) >> 16);
394 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
395 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
405 void vp9_quantize_b_32x32_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
407 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
408 const int16_t *quant_ptr
,
409 const int16_t *quant_shift_ptr
,
410 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
411 const int16_t *dequant_ptr
,
413 const int16_t *scan
, const int16_t *iscan
) {
414 const int zbins
[2] = {ROUND_POWER_OF_TWO(zbin_ptr
[0], 1),
415 ROUND_POWER_OF_TWO(zbin_ptr
[1], 1)};
416 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
423 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
424 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
428 for (i
= 0; i
< n_coeffs
; i
++) {
429 const int rc
= scan
[i
];
430 const int coeff
= coeff_ptr
[rc
];
432 // If the coefficient is out of the base ZBIN range, keep it for
434 if (coeff
>= zbins
[rc
!= 0] || coeff
<= nzbins
[rc
!= 0])
438 // Quantization pass: only process the coefficients selected in
439 // pre-scan pass. Note: idx can be zero.
440 for (i
= 0; i
< idx
; i
++) {
441 const int rc
= scan
[idx_arr
[i
]];
442 const int coeff
= coeff_ptr
[rc
];
443 const int coeff_sign
= (coeff
>> 31);
445 int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
446 abs_coeff
+= ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
447 abs_coeff
= clamp(abs_coeff
, INT16_MIN
, INT16_MAX
);
448 tmp
= ((((abs_coeff
* quant_ptr
[rc
!= 0]) >> 16) + abs_coeff
) *
449 quant_shift_ptr
[rc
!= 0]) >> 15;
451 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
452 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;
461 #if CONFIG_VP9_HIGHBITDEPTH
462 void vp9_highbd_quantize_b_32x32_c(const tran_low_t
*coeff_ptr
,
463 intptr_t n_coeffs
, int skip_block
,
464 const int16_t *zbin_ptr
,
465 const int16_t *round_ptr
,
466 const int16_t *quant_ptr
,
467 const int16_t *quant_shift_ptr
,
468 tran_low_t
*qcoeff_ptr
,
469 tran_low_t
*dqcoeff_ptr
,
470 const int16_t *dequant_ptr
,
472 const int16_t *scan
, const int16_t *iscan
) {
473 const int zbins
[2] = {ROUND_POWER_OF_TWO(zbin_ptr
[0], 1),
474 ROUND_POWER_OF_TWO(zbin_ptr
[1], 1)};
475 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
482 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
483 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
487 for (i
= 0; i
< n_coeffs
; i
++) {
488 const int rc
= scan
[i
];
489 const int coeff
= coeff_ptr
[rc
];
491 // If the coefficient is out of the base ZBIN range, keep it for
493 if (coeff
>= zbins
[rc
!= 0] || coeff
<= nzbins
[rc
!= 0])
497 // Quantization pass: only process the coefficients selected in
498 // pre-scan pass. Note: idx can be zero.
499 for (i
= 0; i
< idx
; i
++) {
500 const int rc
= scan
[idx_arr
[i
]];
501 const int coeff
= coeff_ptr
[rc
];
502 const int coeff_sign
= (coeff
>> 31);
503 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
504 const int64_t tmp1
= abs_coeff
505 + ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
506 const int64_t tmp2
= ((tmp1
* quant_ptr
[rc
!= 0]) >> 16) + tmp1
;
507 const uint32_t abs_qcoeff
=
508 (uint32_t)((tmp2
* quant_shift_ptr
[rc
!= 0]) >> 15);
509 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
510 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;
519 void vp9_regular_quantize_b_4x4(MACROBLOCK
*x
, int plane
, int block
,
520 const int16_t *scan
, const int16_t *iscan
) {
521 MACROBLOCKD
*const xd
= &x
->e_mbd
;
522 struct macroblock_plane
*p
= &x
->plane
[plane
];
523 struct macroblockd_plane
*pd
= &xd
->plane
[plane
];
525 #if CONFIG_VP9_HIGHBITDEPTH
526 if (xd
->cur_buf
->flags
& YV12_FLAG_HIGHBITDEPTH
) {
527 vp9_highbd_quantize_b(BLOCK_OFFSET(p
->coeff
, block
),
529 p
->zbin
, p
->round
, p
->quant
, p
->quant_shift
,
530 BLOCK_OFFSET(p
->qcoeff
, block
),
531 BLOCK_OFFSET(pd
->dqcoeff
, block
),
532 pd
->dequant
, &p
->eobs
[block
],
537 vp9_quantize_b(BLOCK_OFFSET(p
->coeff
, block
),
539 p
->zbin
, p
->round
, p
->quant
, p
->quant_shift
,
540 BLOCK_OFFSET(p
->qcoeff
, block
),
541 BLOCK_OFFSET(pd
->dqcoeff
, block
),
542 pd
->dequant
, &p
->eobs
[block
], scan
, iscan
);
545 static void invert_quant(int16_t *quant
, int16_t *shift
, int d
) {
549 for (l
= 0; t
> 1; l
++)
551 t
= 1 + (1 << (16 + l
)) / d
;
552 *quant
= (int16_t)(t
- (1 << 16));
553 *shift
= 1 << (16 - l
);
556 static int get_qzbin_factor(int q
, vpx_bit_depth_t bit_depth
) {
557 const int quant
= vp9_dc_quant(q
, 0, bit_depth
);
558 #if CONFIG_VP9_HIGHBITDEPTH
561 return q
== 0 ? 64 : (quant
< 148 ? 84 : 80);
563 return q
== 0 ? 64 : (quant
< 592 ? 84 : 80);
565 return q
== 0 ? 64 : (quant
< 2368 ? 84 : 80);
567 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
572 return q
== 0 ? 64 : (quant
< 148 ? 84 : 80);
576 void vp9_init_quantizer(VP9_COMP
*cpi
) {
577 VP9_COMMON
*const cm
= &cpi
->common
;
578 QUANTS
*const quants
= &cpi
->quants
;
581 for (q
= 0; q
< QINDEX_RANGE
; q
++) {
582 const int qzbin_factor
= get_qzbin_factor(q
, cm
->bit_depth
);
583 const int qrounding_factor
= q
== 0 ? 64 : 48;
585 for (i
= 0; i
< 2; ++i
) {
586 int qrounding_factor_fp
= i
== 0 ? 48 : 42;
588 qrounding_factor_fp
= 64;
591 quant
= i
== 0 ? vp9_dc_quant(q
, cm
->y_dc_delta_q
, cm
->bit_depth
)
592 : vp9_ac_quant(q
, 0, cm
->bit_depth
);
593 invert_quant(&quants
->y_quant
[q
][i
], &quants
->y_quant_shift
[q
][i
], quant
);
594 quants
->y_quant_fp
[q
][i
] = (1 << 16) / quant
;
595 quants
->y_round_fp
[q
][i
] = (qrounding_factor_fp
* quant
) >> 7;
596 quants
->y_zbin
[q
][i
] = ROUND_POWER_OF_TWO(qzbin_factor
* quant
, 7);
597 quants
->y_round
[q
][i
] = (qrounding_factor
* quant
) >> 7;
598 cpi
->y_dequant
[q
][i
] = quant
;
601 quant
= i
== 0 ? vp9_dc_quant(q
, cm
->uv_dc_delta_q
, cm
->bit_depth
)
602 : vp9_ac_quant(q
, cm
->uv_ac_delta_q
, cm
->bit_depth
);
603 invert_quant(&quants
->uv_quant
[q
][i
],
604 &quants
->uv_quant_shift
[q
][i
], quant
);
605 quants
->uv_quant_fp
[q
][i
] = (1 << 16) / quant
;
606 quants
->uv_round_fp
[q
][i
] = (qrounding_factor_fp
* quant
) >> 7;
607 quants
->uv_zbin
[q
][i
] = ROUND_POWER_OF_TWO(qzbin_factor
* quant
, 7);
608 quants
->uv_round
[q
][i
] = (qrounding_factor
* quant
) >> 7;
609 cpi
->uv_dequant
[q
][i
] = quant
;
612 for (i
= 2; i
< 8; i
++) {
613 quants
->y_quant
[q
][i
] = quants
->y_quant
[q
][1];
614 quants
->y_quant_fp
[q
][i
] = quants
->y_quant_fp
[q
][1];
615 quants
->y_round_fp
[q
][i
] = quants
->y_round_fp
[q
][1];
616 quants
->y_quant_shift
[q
][i
] = quants
->y_quant_shift
[q
][1];
617 quants
->y_zbin
[q
][i
] = quants
->y_zbin
[q
][1];
618 quants
->y_round
[q
][i
] = quants
->y_round
[q
][1];
619 cpi
->y_dequant
[q
][i
] = cpi
->y_dequant
[q
][1];
621 quants
->uv_quant
[q
][i
] = quants
->uv_quant
[q
][1];
622 quants
->uv_quant_fp
[q
][i
] = quants
->uv_quant_fp
[q
][1];
623 quants
->uv_round_fp
[q
][i
] = quants
->uv_round_fp
[q
][1];
624 quants
->uv_quant_shift
[q
][i
] = quants
->uv_quant_shift
[q
][1];
625 quants
->uv_zbin
[q
][i
] = quants
->uv_zbin
[q
][1];
626 quants
->uv_round
[q
][i
] = quants
->uv_round
[q
][1];
627 cpi
->uv_dequant
[q
][i
] = cpi
->uv_dequant
[q
][1];
632 void vp9_init_plane_quantizers(VP9_COMP
*cpi
, MACROBLOCK
*x
) {
633 const VP9_COMMON
*const cm
= &cpi
->common
;
634 MACROBLOCKD
*const xd
= &x
->e_mbd
;
635 QUANTS
*const quants
= &cpi
->quants
;
636 const int segment_id
= xd
->mi
[0]->mbmi
.segment_id
;
637 const int qindex
= vp9_get_qindex(&cm
->seg
, segment_id
, cm
->base_qindex
);
638 const int rdmult
= vp9_compute_rd_mult(cpi
, qindex
+ cm
->y_dc_delta_q
);
642 x
->plane
[0].quant
= quants
->y_quant
[qindex
];
643 x
->plane
[0].quant_fp
= quants
->y_quant_fp
[qindex
];
644 x
->plane
[0].round_fp
= quants
->y_round_fp
[qindex
];
645 x
->plane
[0].quant_shift
= quants
->y_quant_shift
[qindex
];
646 x
->plane
[0].zbin
= quants
->y_zbin
[qindex
];
647 x
->plane
[0].round
= quants
->y_round
[qindex
];
648 xd
->plane
[0].dequant
= cpi
->y_dequant
[qindex
];
650 x
->plane
[0].quant_thred
[0] = x
->plane
[0].zbin
[0] * x
->plane
[0].zbin
[0];
651 x
->plane
[0].quant_thred
[1] = x
->plane
[0].zbin
[1] * x
->plane
[0].zbin
[1];
654 for (i
= 1; i
< 3; i
++) {
655 x
->plane
[i
].quant
= quants
->uv_quant
[qindex
];
656 x
->plane
[i
].quant_fp
= quants
->uv_quant_fp
[qindex
];
657 x
->plane
[i
].round_fp
= quants
->uv_round_fp
[qindex
];
658 x
->plane
[i
].quant_shift
= quants
->uv_quant_shift
[qindex
];
659 x
->plane
[i
].zbin
= quants
->uv_zbin
[qindex
];
660 x
->plane
[i
].round
= quants
->uv_round
[qindex
];
661 xd
->plane
[i
].dequant
= cpi
->uv_dequant
[qindex
];
663 x
->plane
[i
].quant_thred
[0] = x
->plane
[i
].zbin
[0] * x
->plane
[i
].zbin
[0];
664 x
->plane
[i
].quant_thred
[1] = x
->plane
[i
].zbin
[1] * x
->plane
[i
].zbin
[1];
667 x
->skip_block
= 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
, x
->q_index
);
676 void vp9_frame_init_quantizer(VP9_COMP
*cpi
) {
677 vp9_init_plane_quantizers(cpi
, &cpi
->td
.mb
);
680 void vp9_set_quantizer(VP9_COMMON
*cm
, int q
) {
681 // quantizer has to be reinitialized with vp9_init_quantizer() if any
684 cm
->y_dc_delta_q
= 0;
685 cm
->uv_dc_delta_q
= 0;
686 cm
->uv_ac_delta_q
= 0;
689 // Table that converts 0-63 Q-range values passed in outside to the Qindex
690 // range used internally.
691 static const int quantizer_to_qindex
[] = {
692 0, 4, 8, 12, 16, 20, 24, 28,
693 32, 36, 40, 44, 48, 52, 56, 60,
694 64, 68, 72, 76, 80, 84, 88, 92,
695 96, 100, 104, 108, 112, 116, 120, 124,
696 128, 132, 136, 140, 144, 148, 152, 156,
697 160, 164, 168, 172, 176, 180, 184, 188,
698 192, 196, 200, 204, 208, 212, 216, 220,
699 224, 228, 232, 236, 240, 244, 249, 255,
702 int vp9_quantizer_to_qindex(int quantizer
) {
703 return quantizer_to_qindex
[quantizer
];
706 int vp9_qindex_to_quantizer(int qindex
) {
709 for (quantizer
= 0; quantizer
< 64; ++quantizer
)
710 if (quantizer_to_qindex
[quantizer
] >= qindex
)