- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_quantize.c
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 <math.h>
12 #include "vpx_mem/vpx_mem.h"
13
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"
18
19 #include "vp9/common/vp9_seg_common.h"
20
21 #ifdef ENC_DEBUG
22 extern int enc_debug;
23 #endif
24
25 void vp9_quantize_b_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
26                       int skip_block,
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) {
33   int i, rc, eob;
34   int zbins[2], nzbins[2], zbin;
35   int x, y, z, sz;
36   int zero_flag = n_coeffs;
37
38   vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
39   vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
40
41   eob = -1;
42
43   // Base ZBIN
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;
48
49   if (!skip_block) {
50     // Pre-scan pass
51     for (i = n_coeffs - 1; i >= 0; i--) {
52       rc = scan[i];
53       z = coeff_ptr[rc];
54
55       if (z < zbins[rc != 0] && z > nzbins[rc != 0]) {
56         zero_flag--;
57       } else {
58         break;
59       }
60     }
61
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++) {
65       rc = scan[i];
66       z  = coeff_ptr[rc];
67
68       zbin = (zbins[rc != 0]);
69
70       sz = (z >> 31);                               // sign of z
71       x  = (z ^ sz) - sz;
72
73       if (x >= zbin) {
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
81
82         if (y) {
83           eob = i;                                  // last nonzero coeffs
84         }
85       }
86     }
87   }
88   *eob_ptr = eob + 1;
89 }
90
91 void vp9_quantize_b_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
92                             int skip_block,
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) {
100   int i, rc, eob;
101   int zbins[2], nzbins[2];
102   int x, y, z, sz;
103   int idx = 0;
104   int idx_arr[1024];
105
106   vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
107   vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
108
109   eob = -1;
110
111   // Base ZBIN
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;
116
117   if (!skip_block) {
118     // Pre-scan pass
119     for (i = 0; i < n_coeffs; i++) {
120       rc = scan[i];
121       z = coeff_ptr[rc];
122
123       // If the coefficient is out of the base ZBIN range, keep it for
124       // quantization.
125       if (z >= zbins[rc != 0] || z <= nzbins[rc != 0])
126         idx_arr[idx++] = i;
127     }
128
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]];
133
134       z = coeff_ptr[rc];
135       sz = (z >> 31);                               // sign of z
136       x  = (z ^ sz) - sz;                           // x = abs(z)
137
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)
142
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
146
147       if (y)
148         eob = idx_arr[i];                         // last nonzero coeffs
149     }
150   }
151   *eob_ptr = eob + 1;
152 }
153
154 struct plane_block_idx {
155   int plane;
156   int block;
157 };
158
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,
162                                                      int b_idx) {
163   const int v_offset = y_blocks * 5 / 4;
164   struct plane_block_idx res;
165
166   if (b_idx < y_blocks) {
167     res.plane = 0;
168     res.block = b_idx;
169   } else if (b_idx < v_offset) {
170     res.plane = 1;
171     res.block = b_idx - y_blocks;
172   } else {
173     assert(b_idx < y_blocks * 3 / 2);
174     res.plane = 2;
175     res.block = b_idx - v_offset;
176   }
177   return res;
178 }
179
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];
186
187   vp9_quantize_b(BLOCK_OFFSET(p->coeff, pb_idx.block),
188            16, x->skip_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);
193 }
194
195 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
196   unsigned t;
197   int l;
198   t = d;
199   for (l = 0; t > 1; l++)
200     t >>= 1;
201   t = 1 + (1 << (16 + l)) / d;
202   *quant = (int16_t)(t - (1 << 16));
203   *shift = 1 << (16 - l);
204 }
205
206 void vp9_init_quantizer(VP9_COMP *cpi) {
207   int i, q;
208   VP9_COMMON *const cm = &cpi->common;
209
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;
213
214     // y
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;
222     }
223
224     // uv
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;
232     }
233
234 #if CONFIG_ALPHA
235     // alpha
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;
243     }
244 #endif
245
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];
252
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];
258
259 #if CONFIG_ALPHA
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];
265 #endif
266     }
267   }
268 }
269
270 void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) {
271   int i;
272   VP9_COMMON *const cm = &cpi->common;
273   MACROBLOCKD *xd = &x->e_mbd;
274   int zbin_extra;
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);
278
279   int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
280
281   // Y
282   zbin_extra = (cpi->common.y_dequant[qindex][1] *
283                  (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
284
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];
291
292   // UV
293   zbin_extra = (cpi->common.uv_dequant[qindex][1] *
294                 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
295
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];
303   }
304
305 #if CONFIG_ALPHA
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];
312 #endif
313
314   x->skip_block = vp9_segfeature_active(&cpi->common.seg, segment_id,
315                                         SEG_LVL_SKIP);
316
317   /* save this macroblock QIndex for vp9_update_zbin_extra() */
318   x->e_mbd.q_index = qindex;
319
320   /* R/D setup */
321   cpi->mb.errorperbit = rdmult >> 6;
322   cpi->mb.errorperbit += (cpi->mb.errorperbit == 0);
323
324   vp9_initialize_me_consts(cpi, xd->q_index);
325 }
326
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;
333
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;
337 }
338
339 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
340   // Clear Zbin mode boost for default case
341   cpi->zbin_mode_boost = 0;
342
343   // MB level quantizer setup
344   vp9_mb_init_quantizer(cpi, &cpi->mb);
345 }
346
347 void vp9_set_quantizer(struct VP9_COMP *cpi, int q) {
348   VP9_COMMON *cm = &cpi->common;
349
350   cm->base_qindex = q;
351
352   // if any of the delta_q values are changing update flag will
353   // have to be set.
354   cm->y_dc_delta_q = 0;
355   cm->uv_dc_delta_q = 0;
356   cm->uv_ac_delta_q = 0;
357
358   // quantizer has to be reinitialized if any delta_q changes.
359   // As there are not any here for now this is inactive code.
360   // if(update)
361   //    vp9_init_quantizer(cpi);
362 }