vp9_quantize.c 22.3 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
30
31
32
33
static INLINE int plane_idx(MACROBLOCKD *xd, int b_idx) {
  const BLOCK_SIZE_TYPE sb_type = xd->mode_info_context->mbmi.sb_type;
  if (b_idx < (16 << (sb_type * 2)))
    return 0;  // Y
  else if (b_idx < (20 << (sb_type * 2)))
    return 16;  // U
  assert(b_idx < (24 << (sb_type * 2)));
  return 20;  // V
}

34
35
void vp9_ht_quantize_b_4x4(MACROBLOCK *mb, int b_idx, TX_TYPE tx_type) {
  MACROBLOCKD *const xd = &mb->e_mbd;
36
37
  BLOCK *const b = &mb->block[0];
  BLOCKD *const d = &xd->block[0];
Jingning Han's avatar
Jingning Han committed
38
39
40
  int i, rc, eob;
  int zbin;
  int x, y, z, sz;
41
42
43
  int16_t *coeff_ptr       = mb->coeff + b_idx * 16;
  int16_t *qcoeff_ptr      = xd->qcoeff + b_idx * 16;
  int16_t *dqcoeff_ptr     = xd->dqcoeff + b_idx * 16;
44
45
46
47
48
49
50
  int16_t *zbin_boost_ptr  = b->zrun_zbin_boost;
  int16_t *zbin_ptr        = b->zbin;
  int16_t *round_ptr       = b->round;
  int16_t *quant_ptr       = b->quant;
  uint8_t *quant_shift_ptr = b->quant_shift;
  int16_t *dequant_ptr     = d->dequant;
  int zbin_oq_value        = b->zbin_extra;
51
  const int *pt_scan;
52
53
54
#if CONFIG_CODE_NONZEROCOUNT
  int nzc = 0;
#endif
Jingning Han's avatar
Jingning Han committed
55

56
  assert(plane_idx(xd, b_idx) == 0);
Deb Mukherjee's avatar
Deb Mukherjee committed
57
  switch (tx_type) {
58
    case ADST_DCT:
59
      pt_scan = vp9_row_scan_4x4;
Jingning Han's avatar
Jingning Han committed
60
      break;
61
    case DCT_ADST:
62
      pt_scan = vp9_col_scan_4x4;
Jingning Han's avatar
Jingning Han committed
63
      break;
64
    default:
65
      pt_scan = vp9_default_zig_zag1d_4x4;
Jingning Han's avatar
Jingning Han committed
66
67
68
69
70
71
72
73
      break;
  }

  vpx_memset(qcoeff_ptr, 0, 32);
  vpx_memset(dqcoeff_ptr, 0, 32);

  eob = -1;

Paul Wilkins's avatar
Paul Wilkins committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  if (!b->skip_block) {
    for (i = 0; i < 16; i++) {
      rc   = pt_scan[i];
      z    = coeff_ptr[rc];

      zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
      zbin_boost_ptr++;

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

      if (x >= zbin) {
        x += round_ptr[rc];
        y  = (((x * quant_ptr[rc]) >> 16) + x)
             >> quant_shift_ptr[rc];                // quantize (x)
        x  = (y ^ sz) - sz;                         // get the sign back
        qcoeff_ptr[rc]  = x;                        // write to destination
        dqcoeff_ptr[rc] = x * dequant_ptr[rc];      // dequantized value

        if (y) {
          eob = i;                                // last nonzero coeffs
95
96
97
#if CONFIG_CODE_NONZEROCOUNT
          ++nzc;                                  // number of nonzero coeffs
#endif
Paul Wilkins's avatar
Paul Wilkins committed
98
99
          zbin_boost_ptr = b->zrun_zbin_boost;    // reset zero runlength
        }
Jingning Han's avatar
Jingning Han committed
100
101
102
103
      }
    }
  }

104
  xd->eobs[b_idx] = eob + 1;
105
106
107
#if CONFIG_CODE_NONZEROCOUNT
  xd->nzcs[b_idx] = nzc;
#endif
Jingning Han's avatar
Jingning Han committed
108
109
}

110
111
void vp9_regular_quantize_b_4x4(MACROBLOCK *mb, int b_idx) {
  MACROBLOCKD *const xd = &mb->e_mbd;
112
113
114
  const int c_idx = plane_idx(xd, b_idx);
  BLOCK *const b = &mb->block[c_idx];
  BLOCKD *const d = &xd->block[c_idx];
John Koleszar's avatar
John Koleszar committed
115
116
117
  int i, rc, eob;
  int zbin;
  int x, y, z, sz;
118
119
120
  int16_t *coeff_ptr       = mb->coeff + b_idx * 16;
  int16_t *qcoeff_ptr      = xd->qcoeff + b_idx * 16;
  int16_t *dqcoeff_ptr     = xd->dqcoeff + b_idx * 16;
121
122
123
124
125
126
127
  int16_t *zbin_boost_ptr  = b->zrun_zbin_boost;
  int16_t *zbin_ptr        = b->zbin;
  int16_t *round_ptr       = b->round;
  int16_t *quant_ptr       = b->quant;
  uint8_t *quant_shift_ptr = b->quant_shift;
  int16_t *dequant_ptr     = d->dequant;
  int zbin_oq_value        = b->zbin_extra;
128
129
130
#if CONFIG_CODE_NONZEROCOUNT
  int nzc = 0;
#endif
John Koleszar's avatar
John Koleszar committed
131
132
133
134
135
136

  vpx_memset(qcoeff_ptr, 0, 32);
  vpx_memset(dqcoeff_ptr, 0, 32);

  eob = -1;

Paul Wilkins's avatar
Paul Wilkins committed
137
138
139
140
  if (!b->skip_block) {
    for (i = 0; i < 16; i++) {
      rc   = vp9_default_zig_zag1d_4x4[i];
      z    = coeff_ptr[rc];
John Koleszar's avatar
John Koleszar committed
141

Paul Wilkins's avatar
Paul Wilkins committed
142
143
      zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
      zbin_boost_ptr++;
John Koleszar's avatar
John Koleszar committed
144

Paul Wilkins's avatar
Paul Wilkins committed
145
146
      sz = (z >> 31);                                 // sign of z
      x  = (z ^ sz) - sz;                             // x = abs(z)
John Koleszar's avatar
John Koleszar committed
147

Paul Wilkins's avatar
Paul Wilkins committed
148
149
      if (x >= zbin) {
        x += round_ptr[rc];
Jingning Han's avatar
Jingning Han committed
150

Paul Wilkins's avatar
Paul Wilkins committed
151
152
153
154
155
        y  = (((x * quant_ptr[rc]) >> 16) + x)
             >> quant_shift_ptr[rc];                // quantize (x)
        x  = (y ^ sz) - sz;                         // get the sign back
        qcoeff_ptr[rc]  = x;                        // write to destination
        dqcoeff_ptr[rc] = x * dequant_ptr[rc];      // dequantized value
John Koleszar's avatar
John Koleszar committed
156

Paul Wilkins's avatar
Paul Wilkins committed
157
158
        if (y) {
          eob = i;                                // last nonzero coeffs
159
160
161
#if CONFIG_CODE_NONZEROCOUNT
          ++nzc;                                  // number of nonzero coeffs
#endif
Paul Wilkins's avatar
Paul Wilkins committed
162
163
          zbin_boost_ptr = b->zrun_zbin_boost;    // reset zero runlength
        }
John Koleszar's avatar
John Koleszar committed
164
      }
John Koleszar's avatar
John Koleszar committed
165
    }
John Koleszar's avatar
John Koleszar committed
166
  }
John Koleszar's avatar
John Koleszar committed
167

168
  xd->eobs[b_idx] = eob + 1;
169
170
171
#if CONFIG_CODE_NONZEROCOUNT
  xd->nzcs[b_idx] = nzc;
#endif
John Koleszar's avatar
John Koleszar committed
172
}
173

174
void vp9_quantize_mby_4x4(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
175
  int i;
176
177

  for (i = 0; i < 16; i++) {
178
    TX_TYPE tx_type = get_tx_type_4x4(&x->e_mbd, i);
179
    if (tx_type != DCT_DCT) {
180
      vp9_ht_quantize_b_4x4(x, i, tx_type);
181
    } else {
182
      x->quantize_b_4x4(x, i);
183
184
    }
  }
John Koleszar's avatar
John Koleszar committed
185
186
}

187
void vp9_quantize_mbuv_4x4(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
188
  int i;
John Koleszar's avatar
John Koleszar committed
189

John Koleszar's avatar
John Koleszar committed
190
  for (i = 16; i < 24; i++)
191
    x->quantize_b_4x4(x, i);
John Koleszar's avatar
John Koleszar committed
192
}
193

194
195
196
void vp9_quantize_mb_4x4(MACROBLOCK *x) {
  vp9_quantize_mby_4x4(x);
  vp9_quantize_mbuv_4x4(x);
197
}
Yaowu Xu's avatar
Yaowu Xu committed
198

199
void vp9_regular_quantize_b_8x8(MACROBLOCK *mb, int b_idx, TX_TYPE tx_type) {
200
  MACROBLOCKD *const xd = &mb->e_mbd;
201
202
203
204
205
  int16_t *qcoeff_ptr = xd->qcoeff + 16 * b_idx;
  int16_t *dqcoeff_ptr = xd->dqcoeff + 16 * b_idx;
  const int c_idx = plane_idx(xd, b_idx);
  BLOCK *const b = &mb->block[c_idx];
  BLOCKD *const d = &xd->block[c_idx];
206
207
208
209
210
211
212
213
214
215
216
217
218
  const int *pt_scan;

  switch (tx_type) {
    case ADST_DCT:
      pt_scan = vp9_row_scan_8x8;
      break;
    case DCT_ADST:
      pt_scan = vp9_col_scan_8x8;
      break;
    default:
      pt_scan = vp9_default_zig_zag1d_8x8;
      break;
  }
Paul Wilkins's avatar
Paul Wilkins committed
219
220
221
222

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

223
224
225
226
227
228
  if (!b->skip_block) {
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
    int zero_run;
    int16_t *zbin_boost_ptr = b->zrun_zbin_boost;
229
    int16_t *coeff_ptr  = mb->coeff + 16 * b_idx;
230
231
232
233
234
235
    int16_t *zbin_ptr   = b->zbin;
    int16_t *round_ptr  = b->round;
    int16_t *quant_ptr  = b->quant;
    uint8_t *quant_shift_ptr = b->quant_shift;
    int16_t *dequant_ptr = d->dequant;
    int zbin_oq_value = b->zbin_extra;
236
237
238
#if CONFIG_CODE_NONZEROCOUNT
    int nzc = 0;
#endif
239
240
241
242
243
244
245
246
247

    eob = -1;

    // Special case for DC as it is the one triggering access in various
    // tables: {zbin, quant, quant_shift, dequant}_ptr[rc != 0]
    {
      z    = coeff_ptr[0];
      zbin = (zbin_ptr[0] + zbin_boost_ptr[0] + zbin_oq_value);
      zero_run = 1;
248

249
250
      sz = (z >> 31);                                // sign of z
      x  = (z ^ sz) - sz;                            // x = abs(z)
251

252
253
254
255
256
257
258
      if (x >= zbin) {
        x += (round_ptr[0]);
        y  = ((int)(((int)(x * quant_ptr[0]) >> 16) + x))
             >> quant_shift_ptr[0];                  // quantize (x)
        x  = (y ^ sz) - sz;                          // get the sign back
        qcoeff_ptr[0]  = x;                          // write to destination
        dqcoeff_ptr[0] = x * dequant_ptr[0];         // dequantized value
259

260
261
        if (y) {
          eob = 0;                                   // last nonzero coeffs
262
263
264
#if CONFIG_CODE_NONZEROCOUNT
          ++nzc;                                  // number of nonzero coeffs
#endif
265
266
267
268
269
          zero_run = 0;
        }
      }
    }
    for (i = 1; i < 64; i++) {
270
      rc   = pt_scan[i];
Paul Wilkins's avatar
Paul Wilkins committed
271
      z    = coeff_ptr[rc];
272
273
274
275
276
      zbin = (zbin_ptr[1] + zbin_boost_ptr[zero_run] + zbin_oq_value);
      // The original code was incrementing zero_run while keeping it at
      // maximum 15 by adding "(zero_run < 15)". The same is achieved by
      // removing the opposite of the sign mask of "(zero_run - 15)".
      zero_run -= (zero_run - 15) >> 31;
Paul Wilkins's avatar
Paul Wilkins committed
277
278
279
280
281
282

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

      if (x >= zbin) {
        x += (round_ptr[rc != 0]);
283
284
        y  = ((int)(((int)(x * quant_ptr[1]) >> 16) + x))
             >> quant_shift_ptr[1];                  // quantize (x)
Paul Wilkins's avatar
Paul Wilkins committed
285
286
        x  = (y ^ sz) - sz;                          // get the sign back
        qcoeff_ptr[rc]  = x;                         // write to destination
287
        dqcoeff_ptr[rc] = x * dequant_ptr[1];        // dequantized value
Paul Wilkins's avatar
Paul Wilkins committed
288
289
290

        if (y) {
          eob = i;                                   // last nonzero coeffs
291
292
293
#if CONFIG_CODE_NONZEROCOUNT
          ++nzc;                                     // number of nonzero coeffs
#endif
Paul Wilkins's avatar
Paul Wilkins committed
294
295
          zero_run = 0;
        }
296
297
      }
    }
298
    xd->eobs[b_idx] = eob + 1;
299
300
301
#if CONFIG_CODE_NONZEROCOUNT
    xd->nzcs[b_idx] = nzc;
#endif
302
  } else {
303
    xd->eobs[b_idx] = 0;
304
305
306
#if CONFIG_CODE_NONZEROCOUNT
    xd->nzcs[b_idx] = 0;
#endif
307
308
309
  }
}

310
void vp9_quantize_mby_8x8(MACROBLOCK *x) {
311
  int i;
312

313
314
315
316
317
#if CONFIG_CODE_NONZEROCOUNT
  for (i = 0; i < 16; i ++) {
    x->e_mbd.nzcs[i] = 0;
  }
#endif
318
  for (i = 0; i < 16; i += 4) {
319
320
    TX_TYPE tx_type = get_tx_type_8x8(&x->e_mbd, (i & 8) + ((i & 4) >> 1));
    x->quantize_b_8x8(x, i, tx_type);
321
  }
322
323
}

324
void vp9_quantize_mbuv_8x8(MACROBLOCK *x) {
325
326
  int i;

327
328
329
330
331
#if CONFIG_CODE_NONZEROCOUNT
  for (i = 16; i < 24; i ++) {
    x->e_mbd.nzcs[i] = 0;
  }
#endif
John Koleszar's avatar
John Koleszar committed
332
  for (i = 16; i < 24; i += 4)
333
    x->quantize_b_8x8(x, i, DCT_DCT);
334
335
}

336
337
338
void vp9_quantize_mb_8x8(MACROBLOCK *x) {
  vp9_quantize_mby_8x8(x);
  vp9_quantize_mbuv_8x8(x);
339
}
Daniel Kang's avatar
Daniel Kang committed
340

341
void vp9_quantize_mby_16x16(MACROBLOCK *x) {
342
  TX_TYPE tx_type = get_tx_type_16x16(&x->e_mbd, 0);
343
344
345
346
347
348
#if CONFIG_CODE_NONZEROCOUNT
  int i;
  for (i = 0; i < 16; i++) {
    x->e_mbd.nzcs[i] = 0;
  }
#endif
349
  x->quantize_b_16x16(x, 0, tx_type);
Daniel Kang's avatar
Daniel Kang committed
350
351
}

352
353
354
void vp9_quantize_mb_16x16(MACROBLOCK *x) {
  vp9_quantize_mby_16x16(x);
  vp9_quantize_mbuv_8x8(x);
Daniel Kang's avatar
Daniel Kang committed
355
356
}

357
static void quantize(int16_t *zbin_boost_orig_ptr,
Paul Wilkins's avatar
Paul Wilkins committed
358
                     int16_t *coeff_ptr, int n_coeffs, int skip_block,
359
360
361
362
                     int16_t *zbin_ptr, int16_t *round_ptr, int16_t *quant_ptr,
                     uint8_t *quant_shift_ptr,
                     int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
                     int16_t *dequant_ptr, int zbin_oq_value,
363
364
365
366
367
                     uint16_t *eob_ptr,
#if CONFIG_CODE_NONZEROCOUNT
                     uint16_t *nzc_ptr,
#endif
                     const int *scan, int mul) {
Daniel Kang's avatar
Daniel Kang committed
368
369
370
  int i, rc, eob;
  int zbin;
  int x, y, z, sz;
371
  int zero_run = 0;
372
  int16_t *zbin_boost_ptr = zbin_boost_orig_ptr;
373
374
375
#if CONFIG_CODE_NONZEROCOUNT
  int nzc = 0;
#endif
Daniel Kang's avatar
Daniel Kang committed
376

377
378
  vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
  vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
Daniel Kang's avatar
Daniel Kang committed
379
380

  eob = -1;
Paul Wilkins's avatar
Paul Wilkins committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403

  if (!skip_block) {
    for (i = 0; i < n_coeffs; i++) {
      rc   = scan[i];
      z    = coeff_ptr[rc] * mul;

      zbin = (zbin_ptr[rc != 0] + zbin_boost_ptr[zero_run] + zbin_oq_value);
      zero_run += (zero_run < 15);

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

      if (x >= zbin) {
        x += (round_ptr[rc != 0]);
        y  = ((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x))
            >> quant_shift_ptr[rc != 0];            // quantize (x)
        x  = (y ^ sz) - sz;                         // get the sign back
        qcoeff_ptr[rc]  = x;                        // write to destination
        dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0] / mul;  // dequantized value

        if (y) {
          eob = i;                                  // last nonzero coeffs
          zero_run = 0;
404
405
406
#if CONFIG_CODE_NONZEROCOUNT
          ++nzc;                                    // number of nonzero coeffs
#endif
Paul Wilkins's avatar
Paul Wilkins committed
407
        }
Daniel Kang's avatar
Daniel Kang committed
408
409
410
411
      }
    }
  }

412
  *eob_ptr = eob + 1;
413
414
415
#if CONFIG_CODE_NONZEROCOUNT
  *nzc_ptr = nzc;
#endif
Daniel Kang's avatar
Daniel Kang committed
416
417
}

418
void vp9_regular_quantize_b_16x16(MACROBLOCK *mb, int b_idx, TX_TYPE tx_type) {
419
  MACROBLOCKD *const xd = &mb->e_mbd;
420
421
422
  const int c_idx = plane_idx(xd, b_idx);
  BLOCK *const b = &mb->block[c_idx];
  BLOCKD *const d = &xd->block[c_idx];
423
424
425
426
427
428
429
430
431
432
433
434
435
  const int *pt_scan;

  switch (tx_type) {
    case ADST_DCT:
      pt_scan = vp9_row_scan_16x16;
      break;
    case DCT_ADST:
      pt_scan = vp9_col_scan_16x16;
      break;
    default:
      pt_scan = vp9_default_zig_zag1d_16x16;
      break;
  }
436

437
  quantize(b->zrun_zbin_boost,
438
           mb->coeff + 16 * b_idx,
Paul Wilkins's avatar
Paul Wilkins committed
439
           256, b->skip_block,
440
           b->zbin, b->round, b->quant, b->quant_shift,
441
442
           xd->qcoeff + 16 * b_idx,
           xd->dqcoeff + 16 * b_idx,
443
444
           d->dequant,
           b->zbin_extra,
445
446
447
448
           &xd->eobs[b_idx],
#if CONFIG_CODE_NONZEROCOUNT
           &xd->nzcs[b_idx],
#endif
449
           pt_scan, 1);
450
451
}

452
453
454
455
456
void vp9_regular_quantize_b_32x32(MACROBLOCK *mb, int b_idx) {
  MACROBLOCKD *const xd = &mb->e_mbd;
  const int c_idx = plane_idx(xd, b_idx);
  BLOCK *const b = &mb->block[c_idx];
  BLOCKD *const d = &xd->block[c_idx];
Paul Wilkins's avatar
Paul Wilkins committed
457
458

  quantize(b->zrun_zbin_boost,
459
           mb->coeff + b_idx * 16,
Paul Wilkins's avatar
Paul Wilkins committed
460
461
462
           1024, b->skip_block,
           b->zbin,
           b->round, b->quant, b->quant_shift,
463
464
           xd->qcoeff + b_idx * 16,
           xd->dqcoeff + b_idx * 16,
Paul Wilkins's avatar
Paul Wilkins committed
465
466
           d->dequant,
           b->zbin_extra,
467
           &xd->eobs[b_idx],
468
#if CONFIG_CODE_NONZEROCOUNT
469
           &xd->nzcs[b_idx],
470
#endif
471
472
473
           vp9_default_zig_zag1d_32x32, 2);
}

474
475
476
477
void vp9_quantize_sby_32x32(MACROBLOCK *x) {
  vp9_regular_quantize_b_32x32(x, 0);
}

478
479
480
void vp9_quantize_sby_16x16(MACROBLOCK *x) {
  int n;

481
482
483
484
485
  for (n = 0; n < 4; n++) {
    TX_TYPE tx_type = get_tx_type_16x16(&x->e_mbd,
                                        (16 * (n & 2)) + ((n & 1) * 4));
    x->quantize_b_16x16(x, n * 16, tx_type);
  }
486
487
488
489
490
}

void vp9_quantize_sby_8x8(MACROBLOCK *x) {
  int n;

491
492
493
494
495
  for (n = 0; n < 16; n++) {
    TX_TYPE tx_type = get_tx_type_8x8(&x->e_mbd,
                                      (4 * (n & 12)) + ((n & 3) * 2));
    x->quantize_b_8x8(x, n * 4, tx_type);
  }
496
497
498
499
500
501
}

void vp9_quantize_sby_4x4(MACROBLOCK *x) {
  MACROBLOCKD *const xd = &x->e_mbd;
  int n;

502
503
504
505
506
507
508
509
  for (n = 0; n < 64; n++) {
    const TX_TYPE tx_type = get_tx_type_4x4(xd, n);
    if (tx_type != DCT_DCT) {
      vp9_ht_quantize_b_4x4(x, n, tx_type);
    } else {
      x->quantize_b_4x4(x, n);
    }
  }
510
511
}

512
void vp9_quantize_sbuv_16x16(MACROBLOCK *x) {
513
514
  x->quantize_b_16x16(x, 64, DCT_DCT);
  x->quantize_b_16x16(x, 80, DCT_DCT);
515
}
516

517
518
519
void vp9_quantize_sbuv_8x8(MACROBLOCK *x) {
  int i;

520
  for (i = 64; i < 96; i += 4)
521
    x->quantize_b_8x8(x, i, DCT_DCT);
522
523
524
525
526
}

void vp9_quantize_sbuv_4x4(MACROBLOCK *x) {
  int i;

527
528
  for (i = 64; i < 96; i++)
    x->quantize_b_4x4(x, i);
529
530
531
532
533
534
}

void vp9_quantize_sb64y_32x32(MACROBLOCK *x) {
  int n;

  for (n = 0; n < 4; n++)
535
    vp9_regular_quantize_b_32x32(x, n * 64);
536
537
538
539
540
}

void vp9_quantize_sb64y_16x16(MACROBLOCK *x) {
  int n;

541
542
543
544
545
  for (n = 0; n < 16; n++) {
    TX_TYPE tx_type = get_tx_type_16x16(&x->e_mbd,
                                        (16 * (n & 12)) + ((n & 3) * 4));
    x->quantize_b_16x16(x, n * 16, tx_type);
  }
546
547
548
549
550
}

void vp9_quantize_sb64y_8x8(MACROBLOCK *x) {
  int n;

551
552
553
554
555
  for (n = 0; n < 64; n++) {
    TX_TYPE tx_type = get_tx_type_8x8(&x->e_mbd,
                                      (4 * (n & 56)) + ((n & 7) * 2));
    x->quantize_b_8x8(x, n * 4, tx_type);
  }
556
557
558
559
560
561
}

void vp9_quantize_sb64y_4x4(MACROBLOCK *x) {
  MACROBLOCKD *const xd = &x->e_mbd;
  int n;

562
563
564
565
566
567
568
569
  for (n = 0; n < 256; n++) {
    const TX_TYPE tx_type = get_tx_type_4x4(xd, n);
    if (tx_type != DCT_DCT) {
      vp9_ht_quantize_b_4x4(x, n, tx_type);
    } else {
      x->quantize_b_4x4(x, n);
    }
  }
570
571
572
}

void vp9_quantize_sb64uv_32x32(MACROBLOCK *x) {
573
574
  vp9_regular_quantize_b_32x32(x, 256);
  vp9_regular_quantize_b_32x32(x, 320);
575
576
577
578
579
}

void vp9_quantize_sb64uv_16x16(MACROBLOCK *x) {
  int i;

580
  for (i = 256; i < 384; i += 16)
581
    x->quantize_b_16x16(x, i, DCT_DCT);
582
583
584
585
586
}

void vp9_quantize_sb64uv_8x8(MACROBLOCK *x) {
  int i;

587
  for (i = 256; i < 384; i += 4)
588
    x->quantize_b_8x8(x, i, DCT_DCT);
589
590
591
592
593
}

void vp9_quantize_sb64uv_4x4(MACROBLOCK *x) {
  int i;

594
595
  for (i = 256; i < 384; i++)
    x->quantize_b_4x4(x, i);
596
597
}

598
599
600
601
/* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
 * these two C functions if corresponding optimized routine is not available.
 * NEON optimized version implements currently the fast quantization for pair
 * of blocks. */
602
603
604
void vp9_regular_quantize_b_4x4_pair(MACROBLOCK *x, int b_idx1, int b_idx2) {
  vp9_regular_quantize_b_4x4(x, b_idx1);
  vp9_regular_quantize_b_4x4(x, b_idx2);
605
606
}

607
static void invert_quant(int16_t *quant, uint8_t *shift, int d) {
John Koleszar's avatar
John Koleszar committed
608
609
610
611
612
613
  unsigned t;
  int l;
  t = d;
  for (l = 0; t > 1; l++)
    t >>= 1;
  t = 1 + (1 << (16 + l)) / d;
614
  *quant = (int16_t)(t - (1 << 16));
John Koleszar's avatar
John Koleszar committed
615
  *shift = l;
616
617
}

618
void vp9_init_quantizer(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
619
620
  int i;
  int quant_val;
621
  int q;
622

623
624
625
  static const int zbin_boost[16] = { 0,  0,  0,  8,  8,  8, 10, 12,
                                     14, 16, 20, 24, 28, 32, 36, 40 };

626
627
  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
628
    int qrounding_factor = 48;
629
    if (q == 0) {
Yaowu Xu's avatar
Yaowu Xu committed
630
631
      qzbin_factor = 64;
      qrounding_factor = 64;
John Koleszar's avatar
John Koleszar committed
632
633
    }
    // dc values
634
635
636
637
638
639
640
641
642
643
644
645
646
    quant_val = vp9_dc_quant(q, cpi->common.y1dc_delta_q);
    invert_quant(cpi->Y1quant[q] + 0, cpi->Y1quant_shift[q] + 0, quant_val);
    cpi->Y1zbin[q][0] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
    cpi->Y1round[q][0] = (qrounding_factor * quant_val) >> 7;
    cpi->common.Y1dequant[q][0] = quant_val;
    cpi->zrun_zbin_boost_y1[q][0] = (quant_val * zbin_boost[0]) >> 7;

    quant_val = vp9_dc_uv_quant(q, cpi->common.uvdc_delta_q);
    invert_quant(cpi->UVquant[q] + 0, cpi->UVquant_shift[q] + 0, quant_val);
    cpi->UVzbin[q][0] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
    cpi->UVround[q][0] = (qrounding_factor * quant_val) >> 7;
    cpi->common.UVdequant[q][0] = quant_val;
    cpi->zrun_zbin_boost_uv[q][0] = (quant_val * zbin_boost[0]) >> 7;
John Koleszar's avatar
John Koleszar committed
647
648
649

    // all the 4x4 ac values =;
    for (i = 1; i < 16; i++) {
650
      int rc = vp9_default_zig_zag1d_4x4[i];
John Koleszar's avatar
John Koleszar committed
651

652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
      quant_val = vp9_ac_yquant(q);
      invert_quant(cpi->Y1quant[q] + rc, cpi->Y1quant_shift[q] + rc, quant_val);
      cpi->Y1zbin[q][rc] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
      cpi->Y1round[q][rc] = (qrounding_factor * quant_val) >> 7;
      cpi->common.Y1dequant[q][rc] = quant_val;
      cpi->zrun_zbin_boost_y1[q][i] =
          ROUND_POWER_OF_TWO(quant_val * zbin_boost[i], 7);

      quant_val = vp9_ac_uv_quant(q, cpi->common.uvac_delta_q);
      invert_quant(cpi->UVquant[q] + rc, cpi->UVquant_shift[q] + rc, quant_val);
      cpi->UVzbin[q][rc] = ROUND_POWER_OF_TWO(qzbin_factor * quant_val, 7);
      cpi->UVround[q][rc] = (qrounding_factor * quant_val) >> 7;
      cpi->common.UVdequant[q][rc] = quant_val;
      cpi->zrun_zbin_boost_uv[q][i] =
          ROUND_POWER_OF_TWO(quant_val * zbin_boost[i], 7);
667
    }
John Koleszar's avatar
John Koleszar committed
668
669
  }
}
670

671
void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
672
673
674
675
676
677
678
  int i;
  int QIndex;
  MACROBLOCKD *xd = &x->e_mbd;
  int zbin_extra;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

  // Select the baseline MB Q index allowing for any segment level change.
679
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_ALT_Q)) {
John Koleszar's avatar
John Koleszar committed
680
681
    // Abs Value
    if (xd->mb_segment_abs_delta == SEGMENT_ABSDATA)
682
      QIndex = vp9_get_segdata(xd, segment_id, SEG_LVL_ALT_Q);
John Koleszar's avatar
John Koleszar committed
683
684
685
686

    // Delta Value
    else {
      QIndex = cpi->common.base_qindex +
687
               vp9_get_segdata(xd, segment_id, SEG_LVL_ALT_Q);
John Koleszar's avatar
John Koleszar committed
688
689
690

      // Clamp to valid range
      QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;
691
    }
John Koleszar's avatar
John Koleszar committed
692
693
694
695
696
  } else
    QIndex = cpi->common.base_qindex;

  // Y
  zbin_extra = (cpi->common.Y1dequant[QIndex][1] *
697
                (cpi->zbin_mode_boost +
John Koleszar's avatar
John Koleszar committed
698
699
700
701
702
703
704
705
706
                 x->act_zbin_adj)) >> 7;

  for (i = 0; i < 16; i++) {
    x->block[i].quant = cpi->Y1quant[QIndex];
    x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
    x->block[i].zbin = cpi->Y1zbin[QIndex];
    x->block[i].round = cpi->Y1round[QIndex];
    x->e_mbd.block[i].dequant = cpi->common.Y1dequant[QIndex];
    x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
707
    x->block[i].zbin_extra = (int16_t)zbin_extra;
708

Paul Wilkins's avatar
Paul Wilkins committed
709
    // Segment skip feature.
Paul Wilkins's avatar
Paul Wilkins committed
710
711
    x->block[i].skip_block =
      vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
712
713
714
715
  }

  // UV
  zbin_extra = (cpi->common.UVdequant[QIndex][1] *
716
                (cpi->zbin_mode_boost +
John Koleszar's avatar
John Koleszar committed
717
718
719
720
721
722
723
724
725
                 x->act_zbin_adj)) >> 7;

  for (i = 16; i < 24; i++) {
    x->block[i].quant = cpi->UVquant[QIndex];
    x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
    x->block[i].zbin = cpi->UVzbin[QIndex];
    x->block[i].round = cpi->UVround[QIndex];
    x->e_mbd.block[i].dequant = cpi->common.UVdequant[QIndex];
    x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
726
    x->block[i].zbin_extra = (int16_t)zbin_extra;
John Koleszar's avatar
John Koleszar committed
727

Paul Wilkins's avatar
Paul Wilkins committed
728
    // Segment skip feature.
Paul Wilkins's avatar
Paul Wilkins committed
729
730
    x->block[i].skip_block =
      vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
731
732
  }

733
  /* save this macroblock QIndex for vp9_update_zbin_extra() */
Deb Mukherjee's avatar
Deb Mukherjee committed
734
  x->e_mbd.q_index = QIndex;
735
736
}

737
void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
738
  int i;
Deb Mukherjee's avatar
Deb Mukherjee committed
739
  int QIndex = x->e_mbd.q_index;
John Koleszar's avatar
John Koleszar committed
740
741
742
743
  int zbin_extra;

  // Y
  zbin_extra = (cpi->common.Y1dequant[QIndex][1] *
744
                (cpi->zbin_mode_boost +
John Koleszar's avatar
John Koleszar committed
745
746
                 x->act_zbin_adj)) >> 7;
  for (i = 0; i < 16; i++) {
747
    x->block[i].zbin_extra = (int16_t)zbin_extra;
John Koleszar's avatar
John Koleszar committed
748
  }
749

John Koleszar's avatar
John Koleszar committed
750
751
  // UV
  zbin_extra = (cpi->common.UVdequant[QIndex][1] *
752
                (cpi->zbin_mode_boost +
John Koleszar's avatar
John Koleszar committed
753
                 x->act_zbin_adj)) >> 7;
754

John Koleszar's avatar
John Koleszar committed
755
  for (i = 16; i < 24; i++) {
756
    x->block[i].zbin_extra = (int16_t)zbin_extra;
John Koleszar's avatar
John Koleszar committed
757
  }
758
759
}

760
void vp9_frame_init_quantizer(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
761
762
  // Clear Zbin mode boost for default case
  cpi->zbin_mode_boost = 0;
763

John Koleszar's avatar
John Koleszar committed
764
  // MB level quantizer setup
765
  vp9_mb_init_quantizer(cpi, &cpi->mb);
766
767
}

768
769
void vp9_set_quantizer(struct VP9_COMP *cpi, int Q) {
  VP9_COMMON *cm = &cpi->common;
770

John Koleszar's avatar
John Koleszar committed
771
  cm->base_qindex = Q;
772

773
774
775
776
  // Set lossless mode
  if (cm->base_qindex <= 4)
    cm->base_qindex = 0;

John Koleszar's avatar
John Koleszar committed
777
778
779
780
781
  // if any of the delta_q values are changing update flag will
  // have to be set.
  cm->y1dc_delta_q = 0;
  cm->uvdc_delta_q = 0;
  cm->uvac_delta_q = 0;
782

John Koleszar's avatar
John Koleszar committed
783
784
785
  // quantizer has to be reinitialized if any delta_q changes.
  // As there are not any here for now this is inactive code.
  // if(update)
786
  //    vp9_init_quantizer(cpi);
787
}