• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include <assert.h>
12 #include <math.h>
13 #include "./vpx_dsp_rtcd.h"
14 #include "vpx_mem/vpx_mem.h"
15 #include "vpx_ports/mem.h"
16 
17 #include "vp9/common/vp9_quant_common.h"
18 #include "vp9/common/vp9_seg_common.h"
19 
20 #include "vp9/encoder/vp9_encoder.h"
21 #include "vp9/encoder/vp9_quantize.h"
22 #include "vp9/encoder/vp9_rd.h"
23 
vp9_quantize_fp_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,int skip_block,const int16_t * round_ptr,const int16_t * quant_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)24 void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
25                        int skip_block, const int16_t *round_ptr,
26                        const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
27                        tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
28                        uint16_t *eob_ptr, const int16_t *scan,
29                        const int16_t *iscan) {
30   int i, eob = -1;
31   (void)iscan;
32   (void)skip_block;
33   assert(!skip_block);
34 
35   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
36   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
37 
38   // Quantization pass: All coefficients with index >= zero_flag are
39   // skippable. Note: zero_flag can be zero.
40   for (i = 0; i < n_coeffs; i++) {
41     const int rc = scan[i];
42     const int coeff = coeff_ptr[rc];
43     const int coeff_sign = (coeff >> 31);
44     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
45 
46     int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
47     tmp = (tmp * quant_ptr[rc != 0]) >> 16;
48 
49     qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
50     dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
51 
52     if (tmp) eob = i;
53   }
54   *eob_ptr = eob + 1;
55 }
56 
57 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_quantize_fp_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,int skip_block,const int16_t * round_ptr,const int16_t * quant_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)58 void vp9_highbd_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
59                               int skip_block, const int16_t *round_ptr,
60                               const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
61                               tran_low_t *dqcoeff_ptr,
62                               const int16_t *dequant_ptr, uint16_t *eob_ptr,
63                               const int16_t *scan, const int16_t *iscan) {
64   int i;
65   int eob = -1;
66 
67   (void)iscan;
68   (void)skip_block;
69   assert(!skip_block);
70 
71   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
72   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
73 
74   // Quantization pass: All coefficients with index >= zero_flag are
75   // skippable. Note: zero_flag can be zero.
76   for (i = 0; i < n_coeffs; i++) {
77     const int rc = scan[i];
78     const int coeff = coeff_ptr[rc];
79     const int coeff_sign = (coeff >> 31);
80     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
81     const int64_t tmp = abs_coeff + round_ptr[rc != 0];
82     const int abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 16);
83     qcoeff_ptr[rc] = (tran_low_t)(abs_qcoeff ^ coeff_sign) - coeff_sign;
84     dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
85     if (abs_qcoeff) eob = i;
86   }
87   *eob_ptr = eob + 1;
88 }
89 #endif
90 
91 // TODO(jingning) Refactor this file and combine functions with similar
92 // operations.
vp9_quantize_fp_32x32_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,int skip_block,const int16_t * round_ptr,const int16_t * quant_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)93 void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
94                              int skip_block, const int16_t *round_ptr,
95                              const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
96                              tran_low_t *dqcoeff_ptr,
97                              const int16_t *dequant_ptr, uint16_t *eob_ptr,
98                              const int16_t *scan, const int16_t *iscan) {
99   int i, eob = -1;
100   (void)iscan;
101   (void)skip_block;
102   assert(!skip_block);
103 
104   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
105   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
106 
107   for (i = 0; i < n_coeffs; i++) {
108     const int rc = scan[i];
109     const int coeff = coeff_ptr[rc];
110     const int coeff_sign = (coeff >> 31);
111     int tmp = 0;
112     int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
113 
114     if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
115       abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
116       abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
117       tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
118       qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
119       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
120     }
121 
122     if (tmp) eob = i;
123   }
124   *eob_ptr = eob + 1;
125 }
126 
127 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_quantize_fp_32x32_c(const tran_low_t * coeff_ptr,intptr_t n_coeffs,int skip_block,const int16_t * round_ptr,const int16_t * quant_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)128 void vp9_highbd_quantize_fp_32x32_c(
129     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
130     const int16_t *round_ptr, const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
131     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
132     const int16_t *scan, const int16_t *iscan) {
133   int i, eob = -1;
134 
135   (void)iscan;
136   (void)skip_block;
137   assert(!skip_block);
138 
139   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
140   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
141 
142   for (i = 0; i < n_coeffs; i++) {
143     int abs_qcoeff = 0;
144     const int rc = scan[i];
145     const int coeff = coeff_ptr[rc];
146     const int coeff_sign = (coeff >> 31);
147     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
148 
149     if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
150       const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
151       abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 15);
152       qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
153       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
154     }
155 
156     if (abs_qcoeff) eob = i;
157   }
158   *eob_ptr = eob + 1;
159 }
160 #endif
161 
vp9_regular_quantize_b_4x4(MACROBLOCK * x,int plane,int block,const int16_t * scan,const int16_t * iscan)162 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
163                                 const int16_t *scan, const int16_t *iscan) {
164   MACROBLOCKD *const xd = &x->e_mbd;
165   struct macroblock_plane *p = &x->plane[plane];
166   struct macroblockd_plane *pd = &xd->plane[plane];
167   tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block),
168              *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
169   const int n_coeffs = 4 * 4;
170 
171   if (x->skip_block) {
172     memset(qcoeff, 0, n_coeffs * sizeof(*qcoeff));
173     memset(dqcoeff, 0, n_coeffs * sizeof(*dqcoeff));
174     return;
175   }
176 
177 #if CONFIG_VP9_HIGHBITDEPTH
178   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
179     vpx_highbd_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs,
180                           x->skip_block, p->zbin, p->round, p->quant,
181                           p->quant_shift, qcoeff, dqcoeff, pd->dequant,
182                           &p->eobs[block], scan, iscan);
183     return;
184   }
185 #endif
186   vpx_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs, x->skip_block,
187                  p->zbin, p->round, p->quant, p->quant_shift, qcoeff, dqcoeff,
188                  pd->dequant, &p->eobs[block], scan, iscan);
189 }
190 
invert_quant(int16_t * quant,int16_t * shift,int d)191 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
192   unsigned t;
193   int l, m;
194   t = d;
195   for (l = 0; t > 1; l++) t >>= 1;
196   m = 1 + (1 << (16 + l)) / d;
197   *quant = (int16_t)(m - (1 << 16));
198   *shift = 1 << (16 - l);
199 }
200 
get_qzbin_factor(int q,vpx_bit_depth_t bit_depth)201 static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
202   const int quant = vp9_dc_quant(q, 0, bit_depth);
203 #if CONFIG_VP9_HIGHBITDEPTH
204   switch (bit_depth) {
205     case VPX_BITS_8: return q == 0 ? 64 : (quant < 148 ? 84 : 80);
206     case VPX_BITS_10: return q == 0 ? 64 : (quant < 592 ? 84 : 80);
207     default:
208       assert(bit_depth == VPX_BITS_12);
209       return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
210   }
211 #else
212   (void)bit_depth;
213   return q == 0 ? 64 : (quant < 148 ? 84 : 80);
214 #endif
215 }
216 
vp9_init_quantizer(VP9_COMP * cpi)217 void vp9_init_quantizer(VP9_COMP *cpi) {
218   VP9_COMMON *const cm = &cpi->common;
219   QUANTS *const quants = &cpi->quants;
220   int i, q, quant;
221 
222   for (q = 0; q < QINDEX_RANGE; q++) {
223     int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
224     int qrounding_factor = q == 0 ? 64 : 48;
225     const int sharpness_adjustment = 16 * (7 - cpi->oxcf.sharpness) / 7;
226 
227     if (cpi->oxcf.sharpness > 0 && q > 0) {
228       qzbin_factor = 64 + sharpness_adjustment;
229       qrounding_factor = 64 - sharpness_adjustment;
230     }
231 
232     for (i = 0; i < 2; ++i) {
233       int qrounding_factor_fp = i == 0 ? 48 : 42;
234       if (q == 0) qrounding_factor_fp = 64;
235       if (cpi->oxcf.sharpness > 0)
236         qrounding_factor_fp = 64 - sharpness_adjustment;
237       // y
238       quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
239                      : vp9_ac_quant(q, 0, cm->bit_depth);
240       invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
241       quants->y_quant_fp[q][i] = (1 << 16) / quant;
242       quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
243       quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
244       quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
245       cpi->y_dequant[q][i] = quant;
246 
247       // uv
248       quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
249                      : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
250       invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
251                    quant);
252       quants->uv_quant_fp[q][i] = (1 << 16) / quant;
253       quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
254       quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
255       quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
256       cpi->uv_dequant[q][i] = quant;
257     }
258 
259     for (i = 2; i < 8; i++) {
260       quants->y_quant[q][i] = quants->y_quant[q][1];
261       quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
262       quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
263       quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
264       quants->y_zbin[q][i] = quants->y_zbin[q][1];
265       quants->y_round[q][i] = quants->y_round[q][1];
266       cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
267 
268       quants->uv_quant[q][i] = quants->uv_quant[q][1];
269       quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
270       quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
271       quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
272       quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
273       quants->uv_round[q][i] = quants->uv_round[q][1];
274       cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
275     }
276   }
277 }
278 
vp9_init_plane_quantizers(VP9_COMP * cpi,MACROBLOCK * x)279 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
280   const VP9_COMMON *const cm = &cpi->common;
281   MACROBLOCKD *const xd = &x->e_mbd;
282   QUANTS *const quants = &cpi->quants;
283   const int segment_id = xd->mi[0]->segment_id;
284   const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
285   const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
286   int i;
287 
288   // Y
289   x->plane[0].quant = quants->y_quant[qindex];
290   x->plane[0].quant_fp = quants->y_quant_fp[qindex];
291   x->plane[0].round_fp = quants->y_round_fp[qindex];
292   x->plane[0].quant_shift = quants->y_quant_shift[qindex];
293   x->plane[0].zbin = quants->y_zbin[qindex];
294   x->plane[0].round = quants->y_round[qindex];
295   xd->plane[0].dequant = cpi->y_dequant[qindex];
296 
297   x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
298   x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
299 
300   // UV
301   for (i = 1; i < 3; i++) {
302     x->plane[i].quant = quants->uv_quant[qindex];
303     x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
304     x->plane[i].round_fp = quants->uv_round_fp[qindex];
305     x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
306     x->plane[i].zbin = quants->uv_zbin[qindex];
307     x->plane[i].round = quants->uv_round[qindex];
308     xd->plane[i].dequant = cpi->uv_dequant[qindex];
309 
310     x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
311     x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
312   }
313 
314   x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
315   x->q_index = qindex;
316 
317   set_error_per_bit(x, rdmult);
318 
319   vp9_initialize_me_consts(cpi, x, x->q_index);
320 }
321 
vp9_frame_init_quantizer(VP9_COMP * cpi)322 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
323   vp9_init_plane_quantizers(cpi, &cpi->td.mb);
324 }
325 
vp9_set_quantizer(VP9_COMMON * cm,int q)326 void vp9_set_quantizer(VP9_COMMON *cm, int q) {
327   // quantizer has to be reinitialized with vp9_init_quantizer() if any
328   // delta_q changes.
329   cm->base_qindex = q;
330   cm->y_dc_delta_q = 0;
331   cm->uv_dc_delta_q = 0;
332   cm->uv_ac_delta_q = 0;
333 }
334 
335 // Table that converts 0-63 Q-range values passed in outside to the Qindex
336 // range used internally.
337 static const int quantizer_to_qindex[] = {
338   0,   4,   8,   12,  16,  20,  24,  28,  32,  36,  40,  44,  48,
339   52,  56,  60,  64,  68,  72,  76,  80,  84,  88,  92,  96,  100,
340   104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
341   156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
342   208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
343 };
344 
vp9_quantizer_to_qindex(int quantizer)345 int vp9_quantizer_to_qindex(int quantizer) {
346   return quantizer_to_qindex[quantizer];
347 }
348 
vp9_qindex_to_quantizer(int qindex)349 int vp9_qindex_to_quantizer(int qindex) {
350   int quantizer;
351 
352   for (quantizer = 0; quantizer < 64; ++quantizer)
353     if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
354 
355   return 63;
356 }
357