Upstream version 9.38.198.0
[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
13 #include "vpx_mem/vpx_mem.h"
14
15 #include "vp9/common/vp9_quant_common.h"
16 #include "vp9/common/vp9_seg_common.h"
17
18 #include "vp9/encoder/vp9_encoder.h"
19 #include "vp9/encoder/vp9_quantize.h"
20 #include "vp9/encoder/vp9_rd.h"
21
22 void vp9_quantize_dc(const int16_t *coeff_ptr, int skip_block,
23                      const int16_t *round_ptr, const int16_t quant,
24                      int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
25                      const int16_t dequant_ptr, uint16_t *eob_ptr) {
26   int eob = -1;
27
28   if (!skip_block) {
29     const int rc = 0;
30     const int coeff = coeff_ptr[rc];
31     const int coeff_sign = (coeff >> 31);
32     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
33
34     int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
35     tmp = (tmp * quant) >> 16;
36     qcoeff_ptr[rc]  = (tmp ^ coeff_sign) - coeff_sign;
37     dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
38     if (tmp)
39       eob = 0;
40   }
41   *eob_ptr = eob + 1;
42 }
43
44 void vp9_quantize_dc_32x32(const int16_t *coeff_ptr, int skip_block,
45                            const int16_t *round_ptr, const int16_t quant,
46                            int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
47                            const int16_t dequant_ptr, uint16_t *eob_ptr) {
48   int eob = -1;
49
50   if (!skip_block) {
51     const int rc = 0;
52     const int coeff = coeff_ptr[rc];
53     const int coeff_sign = (coeff >> 31);
54     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
55
56     int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
57     tmp = (tmp * quant) >> 15;
58     qcoeff_ptr[rc]  = (tmp ^ coeff_sign) - coeff_sign;
59     dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
60     if (tmp)
61       eob = 0;
62   }
63   *eob_ptr = eob + 1;
64 }
65
66 void vp9_quantize_fp_c(const int16_t *coeff_ptr, intptr_t count,
67                        int skip_block,
68                        const int16_t *zbin_ptr, const int16_t *round_ptr,
69                        const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
70                        int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
71                        const int16_t *dequant_ptr,
72                        int zbin_oq_value, uint16_t *eob_ptr,
73                        const int16_t *scan, const int16_t *iscan) {
74   int i, eob = -1;
75   // TODO(jingning) Decide the need of these arguments after the
76   // quantization process is completed.
77   (void)zbin_ptr;
78   (void)quant_shift_ptr;
79   (void)zbin_oq_value;
80   (void)iscan;
81
82   vpx_memset(qcoeff_ptr, 0, count * sizeof(int16_t));
83   vpx_memset(dqcoeff_ptr, 0, count * sizeof(int16_t));
84
85   if (!skip_block) {
86     // Quantization pass: All coefficients with index >= zero_flag are
87     // skippable. Note: zero_flag can be zero.
88     for (i = 0; i < count; i++) {
89       const int rc = scan[i];
90       const int coeff = coeff_ptr[rc];
91       const int coeff_sign = (coeff >> 31);
92       const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
93
94       int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
95       tmp = (tmp * quant_ptr[rc != 0]) >> 16;
96
97       qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
98       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
99
100       if (tmp)
101         eob = i;
102     }
103   }
104   *eob_ptr = eob + 1;
105 }
106
107 // TODO(jingning) Refactor this file and combine functions with similar
108 // operations.
109 void vp9_quantize_fp_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
110                              int skip_block,
111                              const int16_t *zbin_ptr, const int16_t *round_ptr,
112                              const int16_t *quant_ptr,
113                              const int16_t *quant_shift_ptr,
114                              int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
115                              const int16_t *dequant_ptr,
116                              int zbin_oq_value, uint16_t *eob_ptr,
117                              const int16_t *scan, const int16_t *iscan) {
118   int i, eob = -1;
119   (void)zbin_ptr;
120   (void)quant_shift_ptr;
121   (void)zbin_oq_value;
122   (void)iscan;
123
124   vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
125   vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
126
127   if (!skip_block) {
128     for (i = 0; i < n_coeffs; i++) {
129       const int rc = scan[i];
130       const int coeff = coeff_ptr[rc];
131       const int coeff_sign = (coeff >> 31);
132       int tmp = 0;
133       int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
134
135       if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
136         abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
137         abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
138         tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
139         qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
140         dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
141       }
142
143       if (tmp)
144         eob = i;
145     }
146   }
147   *eob_ptr = eob + 1;
148 }
149
150 void vp9_quantize_b_c(const int16_t *coeff_ptr, intptr_t count,
151                       int skip_block,
152                       const int16_t *zbin_ptr, const int16_t *round_ptr,
153                       const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
154                       int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
155                       const int16_t *dequant_ptr,
156                       int zbin_oq_value, uint16_t *eob_ptr,
157                       const int16_t *scan, const int16_t *iscan) {
158   int i, non_zero_count = (int)count, eob = -1;
159   const int zbins[2] = { zbin_ptr[0] + zbin_oq_value,
160                          zbin_ptr[1] + zbin_oq_value };
161   const int nzbins[2] = { zbins[0] * -1,
162                           zbins[1] * -1 };
163   (void)iscan;
164
165   vpx_memset(qcoeff_ptr, 0, count * sizeof(int16_t));
166   vpx_memset(dqcoeff_ptr, 0, count * sizeof(int16_t));
167
168   if (!skip_block) {
169     // Pre-scan pass
170     for (i = (int)count - 1; i >= 0; i--) {
171       const int rc = scan[i];
172       const int coeff = coeff_ptr[rc];
173
174       if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
175         non_zero_count--;
176       else
177         break;
178     }
179
180     // Quantization pass: All coefficients with index >= zero_flag are
181     // skippable. Note: zero_flag can be zero.
182     for (i = 0; i < non_zero_count; i++) {
183       const int rc = scan[i];
184       const int coeff = coeff_ptr[rc];
185       const int coeff_sign = (coeff >> 31);
186       const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
187
188       if (abs_coeff >= zbins[rc != 0]) {
189         int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
190         tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
191                   quant_shift_ptr[rc != 0]) >> 16;  // quantization
192         qcoeff_ptr[rc]  = (tmp ^ coeff_sign) - coeff_sign;
193         dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
194
195         if (tmp)
196           eob = i;
197       }
198     }
199   }
200   *eob_ptr = eob + 1;
201 }
202
203 void vp9_quantize_b_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs,
204                             int skip_block,
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                             int16_t *qcoeff_ptr, int16_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) {
212   const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1),
213                          ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1) };
214   const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
215
216   int idx = 0;
217   int idx_arr[1024];
218   int i, eob = -1;
219   (void)iscan;
220
221   vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
222   vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(int16_t));
223
224   if (!skip_block) {
225     // Pre-scan pass
226     for (i = 0; i < n_coeffs; i++) {
227       const int rc = scan[i];
228       const int coeff = coeff_ptr[rc];
229
230       // If the coefficient is out of the base ZBIN range, keep it for
231       // quantization.
232       if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
233         idx_arr[idx++] = i;
234     }
235
236     // Quantization pass: only process the coefficients selected in
237     // pre-scan pass. Note: idx can be zero.
238     for (i = 0; i < idx; i++) {
239       const int rc = scan[idx_arr[i]];
240       const int coeff = coeff_ptr[rc];
241       const int coeff_sign = (coeff >> 31);
242       int tmp;
243       int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
244       abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
245       abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
246       tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) *
247                quant_shift_ptr[rc != 0]) >> 15;
248
249       qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
250       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
251
252       if (tmp)
253         eob = idx_arr[i];
254     }
255   }
256   *eob_ptr = eob + 1;
257 }
258
259 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
260                                 const int16_t *scan, const int16_t *iscan) {
261   MACROBLOCKD *const xd = &x->e_mbd;
262   struct macroblock_plane *p = &x->plane[plane];
263   struct macroblockd_plane *pd = &xd->plane[plane];
264
265   vp9_quantize_b(BLOCK_OFFSET(p->coeff, block),
266            16, x->skip_block,
267            p->zbin, p->round, p->quant, p->quant_shift,
268            BLOCK_OFFSET(p->qcoeff, block),
269            BLOCK_OFFSET(pd->dqcoeff, block),
270            pd->dequant, p->zbin_extra, &p->eobs[block], scan, iscan);
271 }
272
273 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
274   unsigned t;
275   int l;
276   t = d;
277   for (l = 0; t > 1; l++)
278     t >>= 1;
279   t = 1 + (1 << (16 + l)) / d;
280   *quant = (int16_t)(t - (1 << 16));
281   *shift = 1 << (16 - l);
282 }
283
284 void vp9_init_quantizer(VP9_COMP *cpi) {
285   VP9_COMMON *const cm = &cpi->common;
286   QUANTS *const quants = &cpi->quants;
287   int i, q, quant;
288
289   for (q = 0; q < QINDEX_RANGE; q++) {
290     const int qzbin_factor = q == 0 ? 64 : (vp9_dc_quant(q, 0) < 148 ? 84 : 80);
291     const int qrounding_factor = q == 0 ? 64 : 48;
292
293     for (i = 0; i < 2; ++i) {
294       int qrounding_factor_fp = i == 0 ? 48 : 42;
295       if (q == 0)
296         qrounding_factor_fp = 64;
297
298       // y
299       quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q)
300                      : vp9_ac_quant(q, 0);
301       invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
302       quants->y_quant_fp[q][i] = (1 << 16) / quant;
303       quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
304       quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
305       quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
306       cm->y_dequant[q][i] = quant;
307
308       // uv
309       quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q)
310                      : vp9_ac_quant(q, cm->uv_ac_delta_q);
311       invert_quant(&quants->uv_quant[q][i],
312                    &quants->uv_quant_shift[q][i], quant);
313       quants->uv_quant_fp[q][i] = (1 << 16) / quant;
314       quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
315       quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
316       quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
317       cm->uv_dequant[q][i] = quant;
318     }
319
320     for (i = 2; i < 8; i++) {
321       quants->y_quant[q][i] = quants->y_quant[q][1];
322       quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
323       quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
324       quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
325       quants->y_zbin[q][i] = quants->y_zbin[q][1];
326       quants->y_round[q][i] = quants->y_round[q][1];
327       cm->y_dequant[q][i] = cm->y_dequant[q][1];
328
329       quants->uv_quant[q][i] = quants->uv_quant[q][1];
330       quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
331       quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
332       quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
333       quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
334       quants->uv_round[q][i] = quants->uv_round[q][1];
335       cm->uv_dequant[q][i] = cm->uv_dequant[q][1];
336     }
337   }
338 }
339
340 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
341   const VP9_COMMON *const cm = &cpi->common;
342   MACROBLOCKD *const xd = &x->e_mbd;
343   QUANTS *const quants = &cpi->quants;
344   const int segment_id = xd->mi[0]->mbmi.segment_id;
345   const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
346   const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
347   const int zbin = cpi->zbin_mode_boost;
348   int i;
349
350   // Y
351   x->plane[0].quant = quants->y_quant[qindex];
352   x->plane[0].quant_fp = quants->y_quant_fp[qindex];
353   x->plane[0].round_fp = quants->y_round_fp[qindex];
354   x->plane[0].quant_shift = quants->y_quant_shift[qindex];
355   x->plane[0].zbin = quants->y_zbin[qindex];
356   x->plane[0].round = quants->y_round[qindex];
357   x->plane[0].zbin_extra = (int16_t)((cm->y_dequant[qindex][1] * zbin) >> 7);
358   xd->plane[0].dequant = cm->y_dequant[qindex];
359
360   // UV
361   for (i = 1; i < 3; i++) {
362     x->plane[i].quant = quants->uv_quant[qindex];
363     x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
364     x->plane[i].round_fp = quants->uv_round_fp[qindex];
365     x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
366     x->plane[i].zbin = quants->uv_zbin[qindex];
367     x->plane[i].round = quants->uv_round[qindex];
368     x->plane[i].zbin_extra = (int16_t)((cm->uv_dequant[qindex][1] * zbin) >> 7);
369     xd->plane[i].dequant = cm->uv_dequant[qindex];
370   }
371
372   x->skip_block = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
373   x->q_index = qindex;
374
375   x->errorperbit = rdmult >> 6;
376   x->errorperbit += (x->errorperbit == 0);
377
378   vp9_initialize_me_consts(cpi, x->q_index);
379 }
380
381 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) {
382   const int qindex = x->q_index;
383   const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] *
384                             cpi->zbin_mode_boost) >> 7;
385   const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] *
386                              cpi->zbin_mode_boost) >> 7;
387
388   x->plane[0].zbin_extra = (int16_t)y_zbin_extra;
389   x->plane[1].zbin_extra = (int16_t)uv_zbin_extra;
390   x->plane[2].zbin_extra = (int16_t)uv_zbin_extra;
391 }
392
393 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
394   cpi->zbin_mode_boost = 0;
395   vp9_init_plane_quantizers(cpi, &cpi->mb);
396 }
397
398 void vp9_set_quantizer(VP9_COMMON *cm, int q) {
399   // quantizer has to be reinitialized with vp9_init_quantizer() if any
400   // delta_q changes.
401   cm->base_qindex = q;
402   cm->y_dc_delta_q = 0;
403   cm->uv_dc_delta_q = 0;
404   cm->uv_ac_delta_q = 0;
405 }
406
407 // Table that converts 0-63 Q-range values passed in outside to the Qindex
408 // range used internally.
409 static const int quantizer_to_qindex[] = {
410   0,    4,   8,  12,  16,  20,  24,  28,
411   32,   36,  40,  44,  48,  52,  56,  60,
412   64,   68,  72,  76,  80,  84,  88,  92,
413   96,  100, 104, 108, 112, 116, 120, 124,
414   128, 132, 136, 140, 144, 148, 152, 156,
415   160, 164, 168, 172, 176, 180, 184, 188,
416   192, 196, 200, 204, 208, 212, 216, 220,
417   224, 228, 232, 236, 240, 244, 249, 255,
418 };
419
420 int vp9_quantizer_to_qindex(int quantizer) {
421   return quantizer_to_qindex[quantizer];
422 }
423
424 int vp9_qindex_to_quantizer(int qindex) {
425   int quantizer;
426
427   for (quantizer = 0; quantizer < 64; ++quantizer)
428     if (quantizer_to_qindex[quantizer] >= qindex)
429       return quantizer;
430
431   return 63;
432 }