Home | History | Annotate | Download | only in encoder
      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_quantize.h"
     16 #include "vp9/common/vp9_quant_common.h"
     17 
     18 #include "vp9/common/vp9_seg_common.h"
     19 
     20 #ifdef ENC_DEBUG
     21 extern int enc_debug;
     22 #endif
     23 
     24 void vp9_quantize_b_c(int16_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
     25                       int16_t *zbin_ptr, int16_t *round_ptr, int16_t *quant_ptr,
     26                       int16_t *quant_shift_ptr, int16_t *qcoeff_ptr,
     27                       int16_t *dqcoeff_ptr, int16_t *dequant_ptr,
     28                       int zbin_oq_value, uint16_t *eob_ptr, const int16_t *scan,
     29                       const int16_t *iscan) {
     30   int i, rc, eob;
     31   int zbins[2], nzbins[2], zbin;
     32   int x, y, z, sz;
     33   int zero_flag = n_coeffs;
     34 
     35   vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
     36   vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
     37 
     38   eob = -1;
     39 
     40   // Base ZBIN
     41   zbins[0] = zbin_ptr[0] + zbin_oq_value;
     42   zbins[1] = zbin_ptr[1] + zbin_oq_value;
     43   nzbins[0] = zbins[0] * -1;
     44   nzbins[1] = zbins[1] * -1;
     45 
     46   if (!skip_block) {
     47     // Pre-scan pass
     48     for (i = n_coeffs - 1; i >= 0; i--) {
     49       rc = scan[i];
     50       z = coeff_ptr[rc];
     51 
     52       if (z < zbins[rc != 0] && z > nzbins[rc != 0]) {
     53         zero_flag--;
     54       } else {
     55         break;
     56       }
     57     }
     58 
     59     // Quantization pass: All coefficients with index >= zero_flag are
     60     // skippable. Note: zero_flag can be zero.
     61     for (i = 0; i < zero_flag; i++) {
     62       rc = scan[i];
     63       z  = coeff_ptr[rc];
     64 
     65       zbin = (zbins[rc != 0]);
     66 
     67       sz = (z >> 31);                               // sign of z
     68       x  = (z ^ sz) - sz;
     69 
     70       if (x >= zbin) {
     71         x += (round_ptr[rc != 0]);
     72         x  = clamp(x, INT16_MIN, INT16_MAX);
     73         y  = (((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x)) *
     74               quant_shift_ptr[rc != 0]) >> 16;      // quantize (x)
     75         x  = (y ^ sz) - sz;                         // get the sign back
     76         qcoeff_ptr[rc]  = x;                        // write to destination
     77         dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0];  // dequantized value
     78 
     79         if (y) {
     80           eob = i;                                  // last nonzero coeffs
     81         }
     82       }
     83     }
     84   }
     85   *eob_ptr = eob + 1;
     86 }
     87 
     88 void vp9_quantize_b_32x32_c(int16_t *coeff_ptr, intptr_t n_coeffs,
     89                             int skip_block,
     90                             int16_t *zbin_ptr, int16_t *round_ptr,
     91                             int16_t *quant_ptr, int16_t *quant_shift_ptr,
     92                             int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
     93                             int16_t *dequant_ptr, int zbin_oq_value,
     94                             uint16_t *eob_ptr, const int16_t *scan,
     95                             const int16_t *iscan) {
     96   int i, rc, eob;
     97   int zbins[2], nzbins[2];
     98   int x, y, z, sz;
     99   int idx = 0;
    100   int idx_arr[1024];
    101 
    102   vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
    103   vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
    104 
    105   eob = -1;
    106 
    107   // Base ZBIN
    108   zbins[0] = ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1);
    109   zbins[1] = ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1);
    110   nzbins[0] = zbins[0] * -1;
    111   nzbins[1] = zbins[1] * -1;
    112 
    113   if (!skip_block) {
    114     // Pre-scan pass
    115     for (i = 0; i < n_coeffs; i++) {
    116       rc = scan[i];
    117       z = coeff_ptr[rc];
    118 
    119       // If the coefficient is out of the base ZBIN range, keep it for
    120       // quantization.
    121       if (z >= zbins[rc != 0] || z <= nzbins[rc != 0])
    122         idx_arr[idx++] = i;
    123     }
    124 
    125     // Quantization pass: only process the coefficients selected in
    126     // pre-scan pass. Note: idx can be zero.
    127     for (i = 0; i < idx; i++) {
    128       rc = scan[idx_arr[i]];
    129 
    130       z = coeff_ptr[rc];
    131       sz = (z >> 31);                               // sign of z
    132       x  = (z ^ sz) - sz;                           // x = abs(z)
    133 
    134       x += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
    135       x  = clamp(x, INT16_MIN, INT16_MAX);
    136       y  = ((((x * quant_ptr[rc != 0]) >> 16) + x) *
    137             quant_shift_ptr[rc != 0]) >> 15;      // quantize (x)
    138 
    139       x  = (y ^ sz) - sz;                         // get the sign back
    140       qcoeff_ptr[rc]  = x;                        // write to destination
    141       dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0] / 2;  // dequantized value
    142 
    143       if (y)
    144         eob = idx_arr[i];                         // last nonzero coeffs
    145     }
    146   }
    147   *eob_ptr = eob + 1;
    148 }
    149 
    150 struct plane_block_idx {
    151   int plane;
    152   int block;
    153 };
    154 
    155 // TODO(jkoleszar): returning a struct so it can be used in a const context,
    156 // expect to refactor this further later.
    157 static INLINE struct plane_block_idx plane_block_idx(int y_blocks,
    158                                                      int b_idx) {
    159   const int v_offset = y_blocks * 5 / 4;
    160   struct plane_block_idx res;
    161 
    162   if (b_idx < y_blocks) {
    163     res.plane = 0;
    164     res.block = b_idx;
    165   } else if (b_idx < v_offset) {
    166     res.plane = 1;
    167     res.block = b_idx - y_blocks;
    168   } else {
    169     assert(b_idx < y_blocks * 3 / 2);
    170     res.plane = 2;
    171     res.block = b_idx - v_offset;
    172   }
    173   return res;
    174 }
    175 
    176 void vp9_regular_quantize_b_4x4(MACROBLOCK *mb, int b_idx, TX_TYPE tx_type,
    177                                 int y_blocks) {
    178   MACROBLOCKD *const xd = &mb->e_mbd;
    179   const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, b_idx);
    180   const int16_t *scan = get_scan_4x4(tx_type);
    181   const int16_t *iscan = get_iscan_4x4(tx_type);
    182 
    183   vp9_quantize_b(BLOCK_OFFSET(mb->plane[pb_idx.plane].coeff, pb_idx.block),
    184            16, mb->skip_block,
    185            mb->plane[pb_idx.plane].zbin,
    186            mb->plane[pb_idx.plane].round,
    187            mb->plane[pb_idx.plane].quant,
    188            mb->plane[pb_idx.plane].quant_shift,
    189            BLOCK_OFFSET(xd->plane[pb_idx.plane].qcoeff, pb_idx.block),
    190            BLOCK_OFFSET(xd->plane[pb_idx.plane].dqcoeff, pb_idx.block),
    191            xd->plane[pb_idx.plane].dequant,
    192            mb->plane[pb_idx.plane].zbin_extra,
    193            &xd->plane[pb_idx.plane].eobs[pb_idx.block],
    194            scan, iscan);
    195 }
    196 
    197 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
    198   unsigned t;
    199   int l;
    200   t = d;
    201   for (l = 0; t > 1; l++)
    202     t >>= 1;
    203   t = 1 + (1 << (16 + l)) / d;
    204   *quant = (int16_t)(t - (1 << 16));
    205   *shift = 1 << (16 - l);
    206 }
    207 
    208 void vp9_init_quantizer(VP9_COMP *cpi) {
    209   int i, q;
    210   VP9_COMMON *const cm = &cpi->common;
    211 
    212   for (q = 0; q < QINDEX_RANGE; q++) {
    213     const int qzbin_factor = q == 0 ? 64 : (vp9_dc_quant(q, 0) < 148 ? 84 : 80);
    214     const int qrounding_factor = q == 0 ? 64 : 48;
    215 
    216     // y
    217     for (i = 0; i < 2; ++i) {
    218       const int quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q)
    219                                : vp9_ac_quant(q, 0);
    220       invert_quant(&cpi->y_quant[q][i], &cpi->y_quant_shift[q][i], quant);
    221       cpi->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
    222       cpi->y_round[q][i] = (qrounding_factor * quant) >> 7;
    223       cm->y_dequant[q][i] = quant;
    224     }
    225 
    226     // uv
    227     for (i = 0; i < 2; ++i) {
    228       const int quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q)
    229                                : vp9_ac_quant(q, cm->uv_ac_delta_q);
    230       invert_quant(&cpi->uv_quant[q][i], &cpi->uv_quant_shift[q][i], quant);
    231       cpi->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
    232       cpi->uv_round[q][i] = (qrounding_factor * quant) >> 7;
    233       cm->uv_dequant[q][i] = quant;
    234     }
    235 
    236 #if CONFIG_ALPHA
    237     // alpha
    238     for (i = 0; i < 2; ++i) {
    239       const int quant = i == 0 ? vp9_dc_quant(q, cm->a_dc_delta_q)
    240                                : vp9_ac_quant(q, cm->a_ac_delta_q);
    241       invert_quant(&cpi->a_quant[q][i], &cpi->a_quant_shift[q][i], quant);
    242       cpi->a_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
    243       cpi->a_round[q][i] = (qrounding_factor * quant) >> 7;
    244       cm->a_dequant[q][i] = quant;
    245     }
    246 #endif
    247 
    248     for (i = 2; i < 8; i++) {
    249       cpi->y_quant[q][i] = cpi->y_quant[q][1];
    250       cpi->y_quant_shift[q][i] = cpi->y_quant_shift[q][1];
    251       cpi->y_zbin[q][i] = cpi->y_zbin[q][1];
    252       cpi->y_round[q][i] = cpi->y_round[q][1];
    253       cm->y_dequant[q][i] = cm->y_dequant[q][1];
    254 
    255       cpi->uv_quant[q][i] = cpi->uv_quant[q][1];
    256       cpi->uv_quant_shift[q][i] = cpi->uv_quant_shift[q][1];
    257       cpi->uv_zbin[q][i] = cpi->uv_zbin[q][1];
    258       cpi->uv_round[q][i] = cpi->uv_round[q][1];
    259       cm->uv_dequant[q][i] = cm->uv_dequant[q][1];
    260 
    261 #if CONFIG_ALPHA
    262       cpi->a_quant[q][i] = cpi->a_quant[q][1];
    263       cpi->a_quant_shift[q][i] = cpi->a_quant_shift[q][1];
    264       cpi->a_zbin[q][i] = cpi->a_zbin[q][1];
    265       cpi->a_round[q][i] = cpi->a_round[q][1];
    266       cm->a_dequant[q][i] = cm->a_dequant[q][1];
    267 #endif
    268     }
    269   }
    270 }
    271 
    272 void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) {
    273   int i;
    274   MACROBLOCKD *xd = &x->e_mbd;
    275   int zbin_extra;
    276   int segment_id = xd->this_mi->mbmi.segment_id;
    277   const int qindex = vp9_get_qindex(&cpi->common.seg, segment_id,
    278                                     cpi->common.base_qindex);
    279 
    280   // Y
    281   zbin_extra = (cpi->common.y_dequant[qindex][1] *
    282                  (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
    283 
    284   x->plane[0].quant = cpi->y_quant[qindex];
    285   x->plane[0].quant_shift = cpi->y_quant_shift[qindex];
    286   x->plane[0].zbin = cpi->y_zbin[qindex];
    287   x->plane[0].round = cpi->y_round[qindex];
    288   x->plane[0].zbin_extra = (int16_t)zbin_extra;
    289   x->e_mbd.plane[0].dequant = cpi->common.y_dequant[qindex];
    290 
    291   // UV
    292   zbin_extra = (cpi->common.uv_dequant[qindex][1] *
    293                 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
    294 
    295   for (i = 1; i < 3; i++) {
    296     x->plane[i].quant = cpi->uv_quant[qindex];
    297     x->plane[i].quant_shift = cpi->uv_quant_shift[qindex];
    298     x->plane[i].zbin = cpi->uv_zbin[qindex];
    299     x->plane[i].round = cpi->uv_round[qindex];
    300     x->plane[i].zbin_extra = (int16_t)zbin_extra;
    301     x->e_mbd.plane[i].dequant = cpi->common.uv_dequant[qindex];
    302   }
    303 
    304 #if CONFIG_ALPHA
    305   x->plane[3].quant = cpi->a_quant[qindex];
    306   x->plane[3].quant_shift = cpi->a_quant_shift[qindex];
    307   x->plane[3].zbin = cpi->a_zbin[qindex];
    308   x->plane[3].round = cpi->a_round[qindex];
    309   x->plane[3].zbin_extra = (int16_t)zbin_extra;
    310   x->e_mbd.plane[3].dequant = cpi->common.a_dequant[qindex];
    311 #endif
    312 
    313   x->skip_block = vp9_segfeature_active(&cpi->common.seg, segment_id,
    314                                         SEG_LVL_SKIP);
    315 
    316   /* save this macroblock QIndex for vp9_update_zbin_extra() */
    317   x->e_mbd.q_index = qindex;
    318 }
    319 
    320 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) {
    321   const int qindex = x->e_mbd.q_index;
    322   const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] *
    323                 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
    324   const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] *
    325                   (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7;
    326 
    327   x->plane[0].zbin_extra = (int16_t)y_zbin_extra;
    328   x->plane[1].zbin_extra = (int16_t)uv_zbin_extra;
    329   x->plane[2].zbin_extra = (int16_t)uv_zbin_extra;
    330 }
    331 
    332 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
    333   // Clear Zbin mode boost for default case
    334   cpi->zbin_mode_boost = 0;
    335 
    336   // MB level quantizer setup
    337   vp9_mb_init_quantizer(cpi, &cpi->mb);
    338 }
    339 
    340 void vp9_set_quantizer(struct VP9_COMP *cpi, int Q) {
    341   VP9_COMMON *cm = &cpi->common;
    342 
    343   cm->base_qindex = Q;
    344 
    345   // if any of the delta_q values are changing update flag will
    346   // have to be set.
    347   cm->y_dc_delta_q = 0;
    348   cm->uv_dc_delta_q = 0;
    349   cm->uv_ac_delta_q = 0;
    350 
    351   // quantizer has to be reinitialized if any delta_q changes.
    352   // As there are not any here for now this is inactive code.
    353   // if(update)
    354   //    vp9_init_quantizer(cpi);
    355 }
    356