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