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