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 <assert.h>
     12 #include <math.h>
     13 #include "./vpx_dsp_rtcd.h"
     14 #include "vpx_mem/vpx_mem.h"
     15 #include "vpx_ports/mem.h"
     16 
     17 #include "vp9/common/vp9_quant_common.h"
     18 #include "vp9/common/vp9_seg_common.h"
     19 
     20 #include "vp9/encoder/vp9_encoder.h"
     21 #include "vp9/encoder/vp9_quantize.h"
     22 #include "vp9/encoder/vp9_rd.h"
     23 
     24 void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     25                        int skip_block, const int16_t *round_ptr,
     26                        const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
     27                        tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
     28                        uint16_t *eob_ptr, const int16_t *scan,
     29                        const int16_t *iscan) {
     30   int i, eob = -1;
     31   (void)iscan;
     32   (void)skip_block;
     33   assert(!skip_block);
     34 
     35   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     36   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     37 
     38   // Quantization pass: All coefficients with index >= zero_flag are
     39   // skippable. Note: zero_flag can be zero.
     40   for (i = 0; i < n_coeffs; i++) {
     41     const int rc = scan[i];
     42     const int coeff = coeff_ptr[rc];
     43     const int coeff_sign = (coeff >> 31);
     44     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
     45 
     46     int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
     47     tmp = (tmp * quant_ptr[rc != 0]) >> 16;
     48 
     49     qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
     50     dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
     51 
     52     if (tmp) eob = i;
     53   }
     54   *eob_ptr = eob + 1;
     55 }
     56 
     57 #if CONFIG_VP9_HIGHBITDEPTH
     58 void vp9_highbd_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     59                               int skip_block, const int16_t *round_ptr,
     60                               const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
     61                               tran_low_t *dqcoeff_ptr,
     62                               const int16_t *dequant_ptr, uint16_t *eob_ptr,
     63                               const int16_t *scan, const int16_t *iscan) {
     64   int i;
     65   int eob = -1;
     66 
     67   (void)iscan;
     68   (void)skip_block;
     69   assert(!skip_block);
     70 
     71   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
     72   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
     73 
     74   // Quantization pass: All coefficients with index >= zero_flag are
     75   // skippable. Note: zero_flag can be zero.
     76   for (i = 0; i < n_coeffs; i++) {
     77     const int rc = scan[i];
     78     const int coeff = coeff_ptr[rc];
     79     const int coeff_sign = (coeff >> 31);
     80     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
     81     const int64_t tmp = abs_coeff + round_ptr[rc != 0];
     82     const int abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 16);
     83     qcoeff_ptr[rc] = (tran_low_t)(abs_qcoeff ^ coeff_sign) - coeff_sign;
     84     dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
     85     if (abs_qcoeff) eob = i;
     86   }
     87   *eob_ptr = eob + 1;
     88 }
     89 #endif
     90 
     91 // TODO(jingning) Refactor this file and combine functions with similar
     92 // operations.
     93 void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
     94                              int skip_block, const int16_t *round_ptr,
     95                              const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
     96                              tran_low_t *dqcoeff_ptr,
     97                              const int16_t *dequant_ptr, uint16_t *eob_ptr,
     98                              const int16_t *scan, const int16_t *iscan) {
     99   int i, eob = -1;
    100   (void)iscan;
    101   (void)skip_block;
    102   assert(!skip_block);
    103 
    104   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    105   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    106 
    107   for (i = 0; i < n_coeffs; i++) {
    108     const int rc = scan[i];
    109     const int coeff = coeff_ptr[rc];
    110     const int coeff_sign = (coeff >> 31);
    111     int tmp = 0;
    112     int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    113 
    114     if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
    115       abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
    116       abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
    117       tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
    118       qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
    119       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
    120     }
    121 
    122     if (tmp) eob = i;
    123   }
    124   *eob_ptr = eob + 1;
    125 }
    126 
    127 #if CONFIG_VP9_HIGHBITDEPTH
    128 void vp9_highbd_quantize_fp_32x32_c(
    129     const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
    130     const int16_t *round_ptr, const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
    131     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
    132     const int16_t *scan, const int16_t *iscan) {
    133   int i, eob = -1;
    134 
    135   (void)iscan;
    136   (void)skip_block;
    137   assert(!skip_block);
    138 
    139   memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
    140   memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
    141 
    142   for (i = 0; i < n_coeffs; i++) {
    143     int abs_qcoeff = 0;
    144     const int rc = scan[i];
    145     const int coeff = coeff_ptr[rc];
    146     const int coeff_sign = (coeff >> 31);
    147     const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    148 
    149     if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
    150       const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
    151       abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 15);
    152       qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
    153       dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
    154     }
    155 
    156     if (abs_qcoeff) eob = i;
    157   }
    158   *eob_ptr = eob + 1;
    159 }
    160 #endif
    161 
    162 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
    163                                 const int16_t *scan, const int16_t *iscan) {
    164   MACROBLOCKD *const xd = &x->e_mbd;
    165   struct macroblock_plane *p = &x->plane[plane];
    166   struct macroblockd_plane *pd = &xd->plane[plane];
    167   tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block),
    168              *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
    169   const int n_coeffs = 4 * 4;
    170 
    171   if (x->skip_block) {
    172     memset(qcoeff, 0, n_coeffs * sizeof(*qcoeff));
    173     memset(dqcoeff, 0, n_coeffs * sizeof(*dqcoeff));
    174     return;
    175   }
    176 
    177 #if CONFIG_VP9_HIGHBITDEPTH
    178   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    179     vpx_highbd_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs,
    180                           x->skip_block, p->zbin, p->round, p->quant,
    181                           p->quant_shift, qcoeff, dqcoeff, pd->dequant,
    182                           &p->eobs[block], scan, iscan);
    183     return;
    184   }
    185 #endif
    186   vpx_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs, x->skip_block,
    187                  p->zbin, p->round, p->quant, p->quant_shift, qcoeff, dqcoeff,
    188                  pd->dequant, &p->eobs[block], scan, iscan);
    189 }
    190 
    191 static void invert_quant(int16_t *quant, int16_t *shift, int d) {
    192   unsigned t;
    193   int l, m;
    194   t = d;
    195   for (l = 0; t > 1; l++) t >>= 1;
    196   m = 1 + (1 << (16 + l)) / d;
    197   *quant = (int16_t)(m - (1 << 16));
    198   *shift = 1 << (16 - l);
    199 }
    200 
    201 static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
    202   const int quant = vp9_dc_quant(q, 0, bit_depth);
    203 #if CONFIG_VP9_HIGHBITDEPTH
    204   switch (bit_depth) {
    205     case VPX_BITS_8: return q == 0 ? 64 : (quant < 148 ? 84 : 80);
    206     case VPX_BITS_10: return q == 0 ? 64 : (quant < 592 ? 84 : 80);
    207     case VPX_BITS_12: return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
    208     default:
    209       assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
    210       return -1;
    211   }
    212 #else
    213   (void)bit_depth;
    214   return q == 0 ? 64 : (quant < 148 ? 84 : 80);
    215 #endif
    216 }
    217 
    218 void vp9_init_quantizer(VP9_COMP *cpi) {
    219   VP9_COMMON *const cm = &cpi->common;
    220   QUANTS *const quants = &cpi->quants;
    221   int i, q, quant;
    222 
    223   for (q = 0; q < QINDEX_RANGE; q++) {
    224     const int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
    225     const int qrounding_factor = q == 0 ? 64 : 48;
    226 
    227     for (i = 0; i < 2; ++i) {
    228       int qrounding_factor_fp = i == 0 ? 48 : 42;
    229       if (q == 0) qrounding_factor_fp = 64;
    230 
    231       // y
    232       quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
    233                      : vp9_ac_quant(q, 0, cm->bit_depth);
    234       invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
    235       quants->y_quant_fp[q][i] = (1 << 16) / quant;
    236       quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
    237       quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
    238       quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
    239       cpi->y_dequant[q][i] = quant;
    240 
    241       // uv
    242       quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
    243                      : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
    244       invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
    245                    quant);
    246       quants->uv_quant_fp[q][i] = (1 << 16) / quant;
    247       quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
    248       quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
    249       quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
    250       cpi->uv_dequant[q][i] = quant;
    251     }
    252 
    253     for (i = 2; i < 8; i++) {
    254       quants->y_quant[q][i] = quants->y_quant[q][1];
    255       quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
    256       quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
    257       quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
    258       quants->y_zbin[q][i] = quants->y_zbin[q][1];
    259       quants->y_round[q][i] = quants->y_round[q][1];
    260       cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
    261 
    262       quants->uv_quant[q][i] = quants->uv_quant[q][1];
    263       quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
    264       quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
    265       quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
    266       quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
    267       quants->uv_round[q][i] = quants->uv_round[q][1];
    268       cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
    269     }
    270   }
    271 }
    272 
    273 void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
    274   const VP9_COMMON *const cm = &cpi->common;
    275   MACROBLOCKD *const xd = &x->e_mbd;
    276   QUANTS *const quants = &cpi->quants;
    277   const int segment_id = xd->mi[0]->segment_id;
    278   const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
    279   const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
    280   int i;
    281 
    282   // Y
    283   x->plane[0].quant = quants->y_quant[qindex];
    284   x->plane[0].quant_fp = quants->y_quant_fp[qindex];
    285   x->plane[0].round_fp = quants->y_round_fp[qindex];
    286   x->plane[0].quant_shift = quants->y_quant_shift[qindex];
    287   x->plane[0].zbin = quants->y_zbin[qindex];
    288   x->plane[0].round = quants->y_round[qindex];
    289   xd->plane[0].dequant = cpi->y_dequant[qindex];
    290 
    291   x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
    292   x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
    293 
    294   // UV
    295   for (i = 1; i < 3; i++) {
    296     x->plane[i].quant = quants->uv_quant[qindex];
    297     x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
    298     x->plane[i].round_fp = quants->uv_round_fp[qindex];
    299     x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
    300     x->plane[i].zbin = quants->uv_zbin[qindex];
    301     x->plane[i].round = quants->uv_round[qindex];
    302     xd->plane[i].dequant = cpi->uv_dequant[qindex];
    303 
    304     x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
    305     x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
    306   }
    307 
    308   x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
    309   x->q_index = qindex;
    310 
    311   set_error_per_bit(x, rdmult);
    312 
    313   vp9_initialize_me_consts(cpi, x, x->q_index);
    314 }
    315 
    316 void vp9_frame_init_quantizer(VP9_COMP *cpi) {
    317   vp9_init_plane_quantizers(cpi, &cpi->td.mb);
    318 }
    319 
    320 void vp9_set_quantizer(VP9_COMMON *cm, int q) {
    321   // quantizer has to be reinitialized with vp9_init_quantizer() if any
    322   // delta_q changes.
    323   cm->base_qindex = q;
    324   cm->y_dc_delta_q = 0;
    325   cm->uv_dc_delta_q = 0;
    326   cm->uv_ac_delta_q = 0;
    327 }
    328 
    329 // Table that converts 0-63 Q-range values passed in outside to the Qindex
    330 // range used internally.
    331 static const int quantizer_to_qindex[] = {
    332   0,   4,   8,   12,  16,  20,  24,  28,  32,  36,  40,  44,  48,
    333   52,  56,  60,  64,  68,  72,  76,  80,  84,  88,  92,  96,  100,
    334   104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
    335   156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
    336   208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
    337 };
    338 
    339 int vp9_quantizer_to_qindex(int quantizer) {
    340   return quantizer_to_qindex[quantizer];
    341 }
    342 
    343 int vp9_qindex_to_quantizer(int qindex) {
    344   int quantizer;
    345 
    346   for (quantizer = 0; quantizer < 64; ++quantizer)
    347     if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
    348 
    349   return 63;
    350 }
    351