michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include michael@0: #include "vpx_mem/vpx_mem.h" michael@0: michael@0: #include "vp9/encoder/vp9_onyx_int.h" michael@0: #include "vp9/encoder/vp9_rdopt.h" michael@0: #include "vp9/encoder/vp9_quantize.h" michael@0: #include "vp9/common/vp9_quant_common.h" michael@0: michael@0: #include "vp9/common/vp9_seg_common.h" michael@0: michael@0: #ifdef ENC_DEBUG michael@0: extern int enc_debug; michael@0: #endif michael@0: michael@0: void vp9_quantize_b_c(const int16_t *coeff_ptr, intptr_t count, michael@0: int skip_block, michael@0: const int16_t *zbin_ptr, const int16_t *round_ptr, michael@0: const int16_t *quant_ptr, const int16_t *quant_shift_ptr, michael@0: int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr, michael@0: const int16_t *dequant_ptr, michael@0: int zbin_oq_value, uint16_t *eob_ptr, michael@0: const int16_t *scan, const int16_t *iscan) { michael@0: int i, non_zero_count = count, eob = -1; michael@0: const int zbins[2] = { zbin_ptr[0] + zbin_oq_value, michael@0: zbin_ptr[1] + zbin_oq_value }; michael@0: const int nzbins[2] = { zbins[0] * -1, michael@0: zbins[1] * -1 }; michael@0: michael@0: vpx_memset(qcoeff_ptr, 0, count * sizeof(int16_t)); michael@0: vpx_memset(dqcoeff_ptr, 0, count * sizeof(int16_t)); michael@0: michael@0: if (!skip_block) { michael@0: // Pre-scan pass michael@0: for (i = count - 1; i >= 0; i--) { michael@0: const int rc = scan[i]; michael@0: const int coeff = coeff_ptr[rc]; michael@0: michael@0: if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0]) michael@0: non_zero_count--; michael@0: else michael@0: break; michael@0: } michael@0: michael@0: // Quantization pass: All coefficients with index >= zero_flag are michael@0: // skippable. Note: zero_flag can be zero. michael@0: for (i = 0; i < non_zero_count; i++) { michael@0: const int rc = scan[i]; michael@0: const int coeff = coeff_ptr[rc]; michael@0: const int coeff_sign = (coeff >> 31); michael@0: const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; michael@0: michael@0: if (abs_coeff >= zbins[rc != 0]) { michael@0: int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); michael@0: tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * michael@0: quant_shift_ptr[rc != 0]) >> 16; // quantization michael@0: qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; michael@0: dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0]; michael@0: michael@0: if (tmp) michael@0: eob = i; michael@0: } michael@0: } michael@0: } michael@0: *eob_ptr = eob + 1; michael@0: } michael@0: michael@0: void vp9_quantize_b_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs, michael@0: int skip_block, michael@0: const int16_t *zbin_ptr, const int16_t *round_ptr, michael@0: const int16_t *quant_ptr, michael@0: const int16_t *quant_shift_ptr, michael@0: int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr, michael@0: const int16_t *dequant_ptr, michael@0: int zbin_oq_value, uint16_t *eob_ptr, michael@0: const int16_t *scan, const int16_t *iscan) { michael@0: int i, rc, eob; michael@0: int zbins[2], nzbins[2]; michael@0: int x, y, z, sz; michael@0: int idx = 0; michael@0: int idx_arr[1024]; michael@0: michael@0: vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t)); michael@0: vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t)); michael@0: michael@0: eob = -1; michael@0: michael@0: // Base ZBIN michael@0: zbins[0] = ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1); michael@0: zbins[1] = ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1); michael@0: nzbins[0] = zbins[0] * -1; michael@0: nzbins[1] = zbins[1] * -1; michael@0: michael@0: if (!skip_block) { michael@0: // Pre-scan pass michael@0: for (i = 0; i < n_coeffs; i++) { michael@0: rc = scan[i]; michael@0: z = coeff_ptr[rc]; michael@0: michael@0: // If the coefficient is out of the base ZBIN range, keep it for michael@0: // quantization. michael@0: if (z >= zbins[rc != 0] || z <= nzbins[rc != 0]) michael@0: idx_arr[idx++] = i; michael@0: } michael@0: michael@0: // Quantization pass: only process the coefficients selected in michael@0: // pre-scan pass. Note: idx can be zero. michael@0: for (i = 0; i < idx; i++) { michael@0: rc = scan[idx_arr[i]]; michael@0: michael@0: z = coeff_ptr[rc]; michael@0: sz = (z >> 31); // sign of z michael@0: x = (z ^ sz) - sz; // x = abs(z) michael@0: michael@0: x += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); michael@0: x = clamp(x, INT16_MIN, INT16_MAX); michael@0: y = ((((x * quant_ptr[rc != 0]) >> 16) + x) * michael@0: quant_shift_ptr[rc != 0]) >> 15; // quantize (x) michael@0: michael@0: x = (y ^ sz) - sz; // get the sign back michael@0: qcoeff_ptr[rc] = x; // write to destination michael@0: dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0] / 2; // dequantized value michael@0: michael@0: if (y) michael@0: eob = idx_arr[i]; // last nonzero coeffs michael@0: } michael@0: } michael@0: *eob_ptr = eob + 1; michael@0: } michael@0: michael@0: struct plane_block_idx { michael@0: int plane; michael@0: int block; michael@0: }; michael@0: michael@0: // TODO(jkoleszar): returning a struct so it can be used in a const context, michael@0: // expect to refactor this further later. michael@0: static INLINE struct plane_block_idx plane_block_idx(int y_blocks, michael@0: int b_idx) { michael@0: const int v_offset = y_blocks * 5 / 4; michael@0: struct plane_block_idx res; michael@0: michael@0: if (b_idx < y_blocks) { michael@0: res.plane = 0; michael@0: res.block = b_idx; michael@0: } else if (b_idx < v_offset) { michael@0: res.plane = 1; michael@0: res.block = b_idx - y_blocks; michael@0: } else { michael@0: assert(b_idx < y_blocks * 3 / 2); michael@0: res.plane = 2; michael@0: res.block = b_idx - v_offset; michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int y_blocks, int b_idx, michael@0: const int16_t *scan, const int16_t *iscan) { michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, b_idx); michael@0: struct macroblock_plane* p = &x->plane[pb_idx.plane]; michael@0: struct macroblockd_plane* pd = &xd->plane[pb_idx.plane]; michael@0: michael@0: vp9_quantize_b(BLOCK_OFFSET(p->coeff, pb_idx.block), michael@0: 16, x->skip_block, michael@0: p->zbin, p->round, p->quant, p->quant_shift, michael@0: BLOCK_OFFSET(pd->qcoeff, pb_idx.block), michael@0: BLOCK_OFFSET(pd->dqcoeff, pb_idx.block), michael@0: pd->dequant, p->zbin_extra, &pd->eobs[pb_idx.block], scan, iscan); michael@0: } michael@0: michael@0: static void invert_quant(int16_t *quant, int16_t *shift, int d) { michael@0: unsigned t; michael@0: int l; michael@0: t = d; michael@0: for (l = 0; t > 1; l++) michael@0: t >>= 1; michael@0: t = 1 + (1 << (16 + l)) / d; michael@0: *quant = (int16_t)(t - (1 << 16)); michael@0: *shift = 1 << (16 - l); michael@0: } michael@0: michael@0: void vp9_init_quantizer(VP9_COMP *cpi) { michael@0: int i, q; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: michael@0: for (q = 0; q < QINDEX_RANGE; q++) { michael@0: const int qzbin_factor = q == 0 ? 64 : (vp9_dc_quant(q, 0) < 148 ? 84 : 80); michael@0: const int qrounding_factor = q == 0 ? 64 : 48; michael@0: michael@0: // y michael@0: for (i = 0; i < 2; ++i) { michael@0: const int quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q) michael@0: : vp9_ac_quant(q, 0); michael@0: invert_quant(&cpi->y_quant[q][i], &cpi->y_quant_shift[q][i], quant); michael@0: cpi->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7); michael@0: cpi->y_round[q][i] = (qrounding_factor * quant) >> 7; michael@0: cm->y_dequant[q][i] = quant; michael@0: } michael@0: michael@0: // uv michael@0: for (i = 0; i < 2; ++i) { michael@0: const int quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q) michael@0: : vp9_ac_quant(q, cm->uv_ac_delta_q); michael@0: invert_quant(&cpi->uv_quant[q][i], &cpi->uv_quant_shift[q][i], quant); michael@0: cpi->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7); michael@0: cpi->uv_round[q][i] = (qrounding_factor * quant) >> 7; michael@0: cm->uv_dequant[q][i] = quant; michael@0: } michael@0: michael@0: #if CONFIG_ALPHA michael@0: // alpha michael@0: for (i = 0; i < 2; ++i) { michael@0: const int quant = i == 0 ? vp9_dc_quant(q, cm->a_dc_delta_q) michael@0: : vp9_ac_quant(q, cm->a_ac_delta_q); michael@0: invert_quant(&cpi->a_quant[q][i], &cpi->a_quant_shift[q][i], quant); michael@0: cpi->a_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7); michael@0: cpi->a_round[q][i] = (qrounding_factor * quant) >> 7; michael@0: cm->a_dequant[q][i] = quant; michael@0: } michael@0: #endif michael@0: michael@0: for (i = 2; i < 8; i++) { michael@0: cpi->y_quant[q][i] = cpi->y_quant[q][1]; michael@0: cpi->y_quant_shift[q][i] = cpi->y_quant_shift[q][1]; michael@0: cpi->y_zbin[q][i] = cpi->y_zbin[q][1]; michael@0: cpi->y_round[q][i] = cpi->y_round[q][1]; michael@0: cm->y_dequant[q][i] = cm->y_dequant[q][1]; michael@0: michael@0: cpi->uv_quant[q][i] = cpi->uv_quant[q][1]; michael@0: cpi->uv_quant_shift[q][i] = cpi->uv_quant_shift[q][1]; michael@0: cpi->uv_zbin[q][i] = cpi->uv_zbin[q][1]; michael@0: cpi->uv_round[q][i] = cpi->uv_round[q][1]; michael@0: cm->uv_dequant[q][i] = cm->uv_dequant[q][1]; michael@0: michael@0: #if CONFIG_ALPHA michael@0: cpi->a_quant[q][i] = cpi->a_quant[q][1]; michael@0: cpi->a_quant_shift[q][i] = cpi->a_quant_shift[q][1]; michael@0: cpi->a_zbin[q][i] = cpi->a_zbin[q][1]; michael@0: cpi->a_round[q][i] = cpi->a_round[q][1]; michael@0: cm->a_dequant[q][i] = cm->a_dequant[q][1]; michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: michael@0: void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) { michael@0: int i; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: int zbin_extra; michael@0: int segment_id = xd->mi_8x8[0]->mbmi.segment_id; michael@0: const int qindex = vp9_get_qindex(&cpi->common.seg, segment_id, michael@0: cpi->common.base_qindex); michael@0: michael@0: int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q); michael@0: michael@0: // Y michael@0: zbin_extra = (cpi->common.y_dequant[qindex][1] * michael@0: (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; michael@0: michael@0: x->plane[0].quant = cpi->y_quant[qindex]; michael@0: x->plane[0].quant_shift = cpi->y_quant_shift[qindex]; michael@0: x->plane[0].zbin = cpi->y_zbin[qindex]; michael@0: x->plane[0].round = cpi->y_round[qindex]; michael@0: x->plane[0].zbin_extra = (int16_t)zbin_extra; michael@0: x->e_mbd.plane[0].dequant = cpi->common.y_dequant[qindex]; michael@0: michael@0: // UV michael@0: zbin_extra = (cpi->common.uv_dequant[qindex][1] * michael@0: (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; michael@0: michael@0: for (i = 1; i < 3; i++) { michael@0: x->plane[i].quant = cpi->uv_quant[qindex]; michael@0: x->plane[i].quant_shift = cpi->uv_quant_shift[qindex]; michael@0: x->plane[i].zbin = cpi->uv_zbin[qindex]; michael@0: x->plane[i].round = cpi->uv_round[qindex]; michael@0: x->plane[i].zbin_extra = (int16_t)zbin_extra; michael@0: x->e_mbd.plane[i].dequant = cpi->common.uv_dequant[qindex]; michael@0: } michael@0: michael@0: #if CONFIG_ALPHA michael@0: x->plane[3].quant = cpi->a_quant[qindex]; michael@0: x->plane[3].quant_shift = cpi->a_quant_shift[qindex]; michael@0: x->plane[3].zbin = cpi->a_zbin[qindex]; michael@0: x->plane[3].round = cpi->a_round[qindex]; michael@0: x->plane[3].zbin_extra = (int16_t)zbin_extra; michael@0: x->e_mbd.plane[3].dequant = cpi->common.a_dequant[qindex]; michael@0: #endif michael@0: michael@0: x->skip_block = vp9_segfeature_active(&cpi->common.seg, segment_id, michael@0: SEG_LVL_SKIP); michael@0: michael@0: /* save this macroblock QIndex for vp9_update_zbin_extra() */ michael@0: x->q_index = qindex; michael@0: michael@0: /* R/D setup */ michael@0: cpi->mb.errorperbit = rdmult >> 6; michael@0: cpi->mb.errorperbit += (cpi->mb.errorperbit == 0); michael@0: michael@0: vp9_initialize_me_consts(cpi, x->q_index); michael@0: } michael@0: michael@0: void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) { michael@0: const int qindex = x->q_index; michael@0: const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] * michael@0: (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; michael@0: const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] * michael@0: (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; michael@0: michael@0: x->plane[0].zbin_extra = (int16_t)y_zbin_extra; michael@0: x->plane[1].zbin_extra = (int16_t)uv_zbin_extra; michael@0: x->plane[2].zbin_extra = (int16_t)uv_zbin_extra; michael@0: } michael@0: michael@0: void vp9_frame_init_quantizer(VP9_COMP *cpi) { michael@0: // Clear Zbin mode boost for default case michael@0: cpi->zbin_mode_boost = 0; michael@0: michael@0: // MB level quantizer setup michael@0: vp9_mb_init_quantizer(cpi, &cpi->mb); michael@0: } michael@0: michael@0: void vp9_set_quantizer(struct VP9_COMP *cpi, int q) { michael@0: VP9_COMMON *cm = &cpi->common; michael@0: michael@0: cm->base_qindex = q; michael@0: michael@0: // if any of the delta_q values are changing update flag will michael@0: // have to be set. michael@0: cm->y_dc_delta_q = 0; michael@0: cm->uv_dc_delta_q = 0; michael@0: cm->uv_ac_delta_q = 0; michael@0: michael@0: // quantizer has to be reinitialized if any delta_q changes. michael@0: // As there are not any here for now this is inactive code. michael@0: // if(update) michael@0: // vp9_init_quantizer(cpi); michael@0: }