vp9_quantize.c 13.8 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
12
13
 */

#include <math.h>
#include "vpx_mem/vpx_mem.h"

14
15
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_quantize.h"
16
#include "vp9/common/vp9_quant_common.h"
John Koleszar's avatar
John Koleszar committed
17

18
#include "vp9/common/vp9_seg_common.h"
19

20
21
22
23
#ifdef ENC_DEBUG
extern int enc_debug;
#endif

24
25
26
27
28
29
void vp9_quantize_b_c(int16_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
                      int16_t *zbin_ptr, int16_t *round_ptr, int16_t *quant_ptr,
                      int16_t *quant_shift_ptr, int16_t *qcoeff_ptr,
                      int16_t *dqcoeff_ptr, int16_t *dequant_ptr,
                      int zbin_oq_value, uint16_t *eob_ptr, const int16_t *scan,
                      const int16_t *iscan) {
Yunqing Wang's avatar
Yunqing Wang committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  int i, rc, eob;
  int zbins[2], nzbins[2], zbin;
  int x, y, z, sz;
  int zero_flag = n_coeffs;

  vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
  vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));

  eob = -1;

  // Base ZBIN
  zbins[0] = zbin_ptr[0] + zbin_oq_value;
  zbins[1] = zbin_ptr[1] + zbin_oq_value;
  nzbins[0] = zbins[0] * -1;
  nzbins[1] = zbins[1] * -1;

  if (!skip_block) {
    // Pre-scan pass
    for (i = n_coeffs - 1; i >= 0; i--) {
      rc = scan[i];
50
      z = coeff_ptr[rc];
Yunqing Wang's avatar
Yunqing Wang committed
51
52
53
54
55
56
57
58
59
60
61
62

      if (z < zbins[rc != 0] && z > nzbins[rc != 0]) {
        zero_flag--;
      } else {
        break;
      }
    }

    // Quantization pass: All coefficients with index >= zero_flag are
    // skippable. Note: zero_flag can be zero.
    for (i = 0; i < zero_flag; i++) {
      rc = scan[i];
63
      z  = coeff_ptr[rc];
Yunqing Wang's avatar
Yunqing Wang committed
64

65
      zbin = (zbins[rc != 0]);
Yunqing Wang's avatar
Yunqing Wang committed
66
67
68
69
70
71

      sz = (z >> 31);                               // sign of z
      x  = (z ^ sz) - sz;

      if (x >= zbin) {
        x += (round_ptr[rc != 0]);
72
73
        y  = (((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x)) *
              quant_shift_ptr[rc != 0]) >> 16;      // quantize (x)
Yunqing Wang's avatar
Yunqing Wang committed
74
75
        x  = (y ^ sz) - sz;                         // get the sign back
        qcoeff_ptr[rc]  = x;                        // write to destination
76
        dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0];  // dequantized value
Yunqing Wang's avatar
Yunqing Wang committed
77
78
79
80
81
82
83
84
85
86
87

        if (y) {
          eob = i;                                  // last nonzero coeffs
        }
      }
    }
  }
  *eob_ptr = eob + 1;
}

// This function works well for large transform size.
88
void vp9_quantize_b_32x32_c(int16_t *coeff_ptr, intptr_t n_coeffs,
89
                            int skip_block,
Yunqing Wang's avatar
Yunqing Wang committed
90
                            int16_t *zbin_ptr, int16_t *round_ptr,
91
                            int16_t *quant_ptr, int16_t *quant_shift_ptr,
Yunqing Wang's avatar
Yunqing Wang committed
92
93
                            int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
                            int16_t *dequant_ptr, int zbin_oq_value,
94
                            uint16_t *eob_ptr, const int16_t *scan,
95
                            const int16_t *iscan) {
Yunqing Wang's avatar
Yunqing Wang committed
96
  int i, rc, eob;
97
  int zbins[2], nzbins[2], zbin;
Yunqing Wang's avatar
Yunqing Wang committed
98
99
  int x, y, z, sz;
  int idx = 0;
100
  int idx_arr[1024];
Yunqing Wang's avatar
Yunqing Wang committed
101
102
103
104
105
106
107
108
109

  vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
  vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));

  eob = -1;

  // Base ZBIN
  zbins[0] = zbin_ptr[0] + zbin_oq_value;
  zbins[1] = zbin_ptr[1] + zbin_oq_value;
110
111
  nzbins[0] = zbins[0] * -1;
  nzbins[1] = zbins[1] * -1;
Yunqing Wang's avatar
Yunqing Wang committed
112
113
114
115
116

  if (!skip_block) {
    // Pre-scan pass
    for (i = 0; i < n_coeffs; i++) {
      rc = scan[i];
117
      z = coeff_ptr[rc] * 2;
Yunqing Wang's avatar
Yunqing Wang committed
118
119
120

      // If the coefficient is out of the base ZBIN range, keep it for
      // quantization.
121
      if (z >= zbins[rc != 0] || z <= nzbins[rc != 0])
Yunqing Wang's avatar
Yunqing Wang committed
122
123
124
125
126
127
128
129
130
        idx_arr[idx++] = i;
    }

    // Quantization pass: only process the coefficients selected in
    // pre-scan pass. Note: idx can be zero.
    for (i = 0; i < idx; i++) {
      rc = scan[idx_arr[i]];

      // Calculate ZBIN
131
      zbin = (zbins[rc != 0]);
Yunqing Wang's avatar
Yunqing Wang committed
132

133
      z = coeff_ptr[rc] * 2;
Yunqing Wang's avatar
Yunqing Wang committed
134
135
136
137
138
      sz = (z >> 31);                               // sign of z
      x  = (z ^ sz) - sz;                           // x = abs(z)

      if (x >= zbin) {
        x += (round_ptr[rc != 0]);
139
140
        y  = (((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x)) *
              quant_shift_ptr[rc != 0]) >> 16;      // quantize (x)
Yunqing Wang's avatar
Yunqing Wang committed
141
142
143

        x  = (y ^ sz) - sz;                         // get the sign back
        qcoeff_ptr[rc]  = x;                        // write to destination
144
        dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0] / 2;  // dequantized value
Yunqing Wang's avatar
Yunqing Wang committed
145
146
147
148
149
150
151
152
153

        if (y) {
          eob = idx_arr[i];                         // last nonzero coeffs
        }
      }
    }
  }
  *eob_ptr = eob + 1;
}
Daniel Kang's avatar
Daniel Kang committed
154

155
156
157
void vp9_quantize(MACROBLOCK *mb, int plane, int block, int n_coeffs,
                  TX_TYPE tx_type) {
  MACROBLOCKD *const xd = &mb->e_mbd;
158
  const int16_t *scan, *iscan;
159
160
161
162
163

  // These contexts may be available in the caller
  switch (n_coeffs) {
    case 4 * 4:
      scan = get_scan_4x4(tx_type);
164
      iscan = get_iscan_4x4(tx_type);
165
166
167
      break;
    case 8 * 8:
      scan = get_scan_8x8(tx_type);
168
      iscan = get_iscan_8x8(tx_type);
169
170
171
      break;
    case 16 * 16:
      scan = get_scan_16x16(tx_type);
172
      iscan = get_iscan_16x16(tx_type);
173
174
      break;
    default:
Paul Wilkins's avatar
Paul Wilkins committed
175
      scan = vp9_default_scan_32x32;
176
      iscan = vp9_default_iscan_32x32;
177
178
179
      break;
  }

Yunqing Wang's avatar
Yunqing Wang committed
180
181
182
  // Call different quantization for different transform size.
  if (n_coeffs >= 1024) {
    // Save index of picked coefficient in pre-scan pass.
183
184
185
186
187
188
189
190
191
192
193
194
    vp9_quantize_b_32x32(BLOCK_OFFSET(mb->plane[plane].coeff, block, 16),
                         n_coeffs, mb->skip_block,
                         mb->plane[plane].zbin,
                         mb->plane[plane].round,
                         mb->plane[plane].quant,
                         mb->plane[plane].quant_shift,
                         BLOCK_OFFSET(xd->plane[plane].qcoeff, block, 16),
                         BLOCK_OFFSET(xd->plane[plane].dqcoeff, block, 16),
                         xd->plane[plane].dequant,
                         mb->plane[plane].zbin_extra,
                         &xd->plane[plane].eobs[block],
                         scan, iscan);
Yunqing Wang's avatar
Yunqing Wang committed
195
196
  }
  else {
197
198
199
200
201
202
203
204
205
206
207
208
    vp9_quantize_b(BLOCK_OFFSET(mb->plane[plane].coeff, block, 16),
                   n_coeffs, mb->skip_block,
                   mb->plane[plane].zbin,
                   mb->plane[plane].round,
                   mb->plane[plane].quant,
                   mb->plane[plane].quant_shift,
                   BLOCK_OFFSET(xd->plane[plane].qcoeff, block, 16),
                   BLOCK_OFFSET(xd->plane[plane].dqcoeff, block, 16),
                   xd->plane[plane].dequant,
                   mb->plane[plane].zbin_extra,
                   &xd->plane[plane].eobs[block],
                   scan, iscan);
Yunqing Wang's avatar
Yunqing Wang committed
209
  }
210
211
}

212
213
214
215
void vp9_regular_quantize_b_4x4(MACROBLOCK *mb, int b_idx, TX_TYPE tx_type,
                                int y_blocks) {
  MACROBLOCKD *const xd = &mb->e_mbd;
  const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, b_idx);
216
217
  const int16_t *scan = get_scan_4x4(tx_type);
  const int16_t *iscan = get_iscan_4x4(tx_type);
218

219
  vp9_quantize_b(BLOCK_OFFSET(mb->plane[pb_idx.plane].coeff, pb_idx.block, 16),
220
221
222
223
224
225
226
227
228
229
           16, mb->skip_block,
           mb->plane[pb_idx.plane].zbin,
           mb->plane[pb_idx.plane].round,
           mb->plane[pb_idx.plane].quant,
           mb->plane[pb_idx.plane].quant_shift,
           BLOCK_OFFSET(xd->plane[pb_idx.plane].qcoeff, pb_idx.block, 16),
           BLOCK_OFFSET(xd->plane[pb_idx.plane].dqcoeff, pb_idx.block, 16),
           xd->plane[pb_idx.plane].dequant,
           mb->plane[pb_idx.plane].zbin_extra,
           &xd->plane[pb_idx.plane].eobs[pb_idx.block],
230
           scan, iscan);
231
232
}

233
static void invert_quant(int16_t *quant, int16_t *shift, int d) {
John Koleszar's avatar
John Koleszar committed
234
235
236
237
238
239
  unsigned t;
  int l;
  t = d;
  for (l = 0; t > 1; l++)
    t >>= 1;
  t = 1 + (1 << (16 + l)) / d;
240
  *quant = (int16_t)(t - (1 << 16));
241
  *shift = 1 << (16 - l);
242
243
}

244
void vp9_init_quantizer(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
245
246
  int i;
  int quant_val;
247
  int quant_uv_val;
248
249
250
#if CONFIG_ALPHA
  int quant_alpha_val;
#endif
251
  int q;
252

253
254
  for (q = 0; q < QINDEX_RANGE; q++) {
    int qzbin_factor = (vp9_dc_quant(q, 0) < 148) ? 84 : 80;
Yaowu Xu's avatar
Yaowu Xu committed
255
    int qrounding_factor = 48;
256
    if (q == 0) {
Yaowu Xu's avatar
Yaowu Xu committed
257
258
      qzbin_factor = 64;
      qrounding_factor = 64;
John Koleszar's avatar
John Koleszar committed
259
    }
260

John Koleszar's avatar
John Koleszar committed
261
    // dc values
262
    quant_val = vp9_dc_quant(q, cpi->common.y_dc_delta_q);
263
264
265
    invert_quant(cpi->y_quant[q] + 0, cpi->y_quant_shift[q] + 0, quant_val);
    cpi->y_zbin[q][0] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
    cpi->y_round[q][0] = (qrounding_factor * quant_val) >> 7;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
266
    cpi->common.y_dequant[q][0] = quant_val;
267

268
    quant_val = vp9_dc_quant(q, cpi->common.uv_dc_delta_q);
269
270
271
    invert_quant(cpi->uv_quant[q] + 0, cpi->uv_quant_shift[q] + 0, quant_val);
    cpi->uv_zbin[q][0] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
    cpi->uv_round[q][0] = (qrounding_factor * quant_val) >> 7;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
272
    cpi->common.uv_dequant[q][0] = quant_val;
John Koleszar's avatar
John Koleszar committed
273

274
275
276
277
278
279
280
281
#if CONFIG_ALPHA
    quant_val = vp9_dc_quant(q, cpi->common.a_dc_delta_q);
    invert_quant(cpi->a_quant[q] + 0, cpi->a_quant_shift[q] + 0, quant_val);
    cpi->a_zbin[q][0] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
    cpi->a_round[q][0] = (qrounding_factor * quant_val) >> 7;
    cpi->common.a_dequant[q][0] = quant_val;
#endif

282
    quant_val = vp9_ac_quant(q, 0);
283
284
285
    invert_quant(cpi->y_quant[q] + 1, cpi->y_quant_shift[q] + 1, quant_val);
    cpi->y_zbin[q][1] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
    cpi->y_round[q][1] = (qrounding_factor * quant_val) >> 7;
286
    cpi->common.y_dequant[q][1] = quant_val;
287

288
    quant_uv_val = vp9_ac_quant(q, cpi->common.uv_ac_delta_q);
289
290
291
292
    invert_quant(cpi->uv_quant[q] + 1, cpi->uv_quant_shift[q] + 1,
                 quant_uv_val);
    cpi->uv_zbin[q][1] = ROUND_POWER_OF_TWO(qzbin_factor * quant_uv_val, 7);
    cpi->uv_round[q][1] = (qrounding_factor * quant_uv_val) >> 7;
293
    cpi->common.uv_dequant[q][1] = quant_uv_val;
294

295
296
#if CONFIG_ALPHA
    quant_alpha_val = vp9_ac_quant(q, cpi->common.a_ac_delta_q);
297
298
299
300
    invert_quant(cpi->a_quant[q] + 1, cpi->a_quant_shift[q] + 1,
                 quant_alpha_val);
    cpi->a_zbin[q][1] = ROUND_POWER_OF_TWO(qzbin_factor * quant_alpha_val, 7);
    cpi->a_round[q][1] = (qrounding_factor * quant_alpha_val) >> 7;
301
302
    cpi->common.a_dequant[q][1] = quant_alpha_val;
#endif
John Koleszar's avatar
John Koleszar committed
303

304
305
306
307
308
309
    for (i = 2; i < 8; i++) {
      cpi->y_quant[q][i] = cpi->y_quant[q][1];
      cpi->y_quant_shift[q][i] = cpi->y_quant_shift[q][1];
      cpi->y_zbin[q][i] = cpi->y_zbin[q][1];
      cpi->y_round[q][i] = cpi->y_round[q][1];
      cpi->common.y_dequant[q][i] = cpi->common.y_dequant[q][1];
310

311
312
313
314
315
      cpi->uv_quant[q][i] = cpi->uv_quant[q][1];
      cpi->uv_quant_shift[q][i] = cpi->uv_quant_shift[q][1];
      cpi->uv_zbin[q][i] = cpi->uv_zbin[q][1];
      cpi->uv_round[q][i] = cpi->uv_round[q][1];
      cpi->common.uv_dequant[q][i] = cpi->common.uv_dequant[q][1];
316
317

#if CONFIG_ALPHA
318
319
320
321
322
      cpi->a_quant[q][i] = cpi->a_quant[q][1];
      cpi->a_quant_shift[q][i] = cpi->a_quant_shift[q][1];
      cpi->a_zbin[q][i] = cpi->a_zbin[q][1];
      cpi->a_round[q][i] = cpi->a_round[q][1];
      cpi->common.a_dequant[q][i] = cpi->common.a_dequant[q][1];
323
#endif
324
    }
John Koleszar's avatar
John Koleszar committed
325
326
  }
}
327

328
void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
329
330
331
332
  int i;
  MACROBLOCKD *xd = &x->e_mbd;
  int zbin_extra;
  int segment_id = xd->mode_info_context->mbmi.segment_id;
333
  const int qindex = vp9_get_qindex(xd, segment_id, cpi->common.base_qindex);
John Koleszar's avatar
John Koleszar committed
334
335

  // Y
Dmitry Kovalev's avatar
Dmitry Kovalev committed
336
337
  zbin_extra = (cpi->common.y_dequant[qindex][1] *
                 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
John Koleszar's avatar
John Koleszar committed
338

339
340
341
342
  x->plane[0].quant = cpi->y_quant[qindex];
  x->plane[0].quant_shift = cpi->y_quant_shift[qindex];
  x->plane[0].zbin = cpi->y_zbin[qindex];
  x->plane[0].round = cpi->y_round[qindex];
343
  x->plane[0].zbin_extra = (int16_t)zbin_extra;
344
  x->e_mbd.plane[0].dequant = cpi->common.y_dequant[qindex];
John Koleszar's avatar
John Koleszar committed
345
346

  // UV
Dmitry Kovalev's avatar
Dmitry Kovalev committed
347
348
  zbin_extra = (cpi->common.uv_dequant[qindex][1] *
                (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
John Koleszar's avatar
John Koleszar committed
349

350
  for (i = 1; i < 3; i++) {
351
352
353
354
    x->plane[i].quant = cpi->uv_quant[qindex];
    x->plane[i].quant_shift = cpi->uv_quant_shift[qindex];
    x->plane[i].zbin = cpi->uv_zbin[qindex];
    x->plane[i].round = cpi->uv_round[qindex];
355
    x->plane[i].zbin_extra = (int16_t)zbin_extra;
356
    x->e_mbd.plane[i].dequant = cpi->common.uv_dequant[qindex];
357
  }
John Koleszar's avatar
John Koleszar committed
358

359
360
361
362
363
364
365
366
367
#if CONFIG_ALPHA
  x->plane[3].quant = cpi->a_quant[qindex];
  x->plane[3].quant_shift = cpi->a_quant_shift[qindex];
  x->plane[3].zbin = cpi->a_zbin[qindex];
  x->plane[3].round = cpi->a_round[qindex];
  x->plane[3].zbin_extra = (int16_t)zbin_extra;
  x->e_mbd.plane[3].dequant = cpi->common.a_dequant[qindex];
#endif

368
  x->skip_block = vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
369

370
  /* save this macroblock QIndex for vp9_update_zbin_extra() */
Dmitry Kovalev's avatar
Dmitry Kovalev committed
371
  x->e_mbd.q_index = qindex;
372
373
}

374
void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
375
376
377
378
379
  const int qindex = x->e_mbd.q_index;
  const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] *
                (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
  const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] *
                  (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
380

381
382
383
  x->plane[0].zbin_extra = (int16_t)y_zbin_extra;
  x->plane[1].zbin_extra = (int16_t)uv_zbin_extra;
  x->plane[2].zbin_extra = (int16_t)uv_zbin_extra;
384
385
}

386
void vp9_frame_init_quantizer(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
387
388
  // Clear Zbin mode boost for default case
  cpi->zbin_mode_boost = 0;
389

John Koleszar's avatar
John Koleszar committed
390
  // MB level quantizer setup
391
  vp9_mb_init_quantizer(cpi, &cpi->mb);
392
393
}

394
395
void vp9_set_quantizer(struct VP9_COMP *cpi, int Q) {
  VP9_COMMON *cm = &cpi->common;
396

John Koleszar's avatar
John Koleszar committed
397
  cm->base_qindex = Q;
398

John Koleszar's avatar
John Koleszar committed
399
400
  // if any of the delta_q values are changing update flag will
  // have to be set.
401
402
403
  cm->y_dc_delta_q = 0;
  cm->uv_dc_delta_q = 0;
  cm->uv_ac_delta_q = 0;
404

John Koleszar's avatar
John Koleszar committed
405
406
407
  // quantizer has to be reinitialized if any delta_q changes.
  // As there are not any here for now this is inactive code.
  // if(update)
408
  //    vp9_init_quantizer(cpi);
409
}