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: michael@0: #include michael@0: #include "vpx_mem/vpx_mem.h" michael@0: michael@0: #include "onyx_int.h" michael@0: #include "quantize.h" michael@0: #include "vp8/common/quant_common.h" michael@0: michael@0: #define EXACT_QUANT michael@0: michael@0: #ifdef EXACT_FASTQUANT michael@0: void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d) michael@0: { michael@0: int i, rc, eob; michael@0: int zbin; michael@0: int x, y, z, sz; michael@0: short *coeff_ptr = b->coeff; michael@0: short *zbin_ptr = b->zbin; michael@0: short *round_ptr = b->round; michael@0: short *quant_ptr = b->quant_fast; michael@0: unsigned char *quant_shift_ptr = b->quant_shift; michael@0: short *qcoeff_ptr = d->qcoeff; michael@0: short *dqcoeff_ptr = d->dqcoeff; michael@0: short *dequant_ptr = d->dequant; michael@0: michael@0: vpx_memset(qcoeff_ptr, 0, 32); michael@0: vpx_memset(dqcoeff_ptr, 0, 32); michael@0: michael@0: eob = -1; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: rc = vp8_default_zig_zag1d[i]; michael@0: z = coeff_ptr[rc]; michael@0: zbin = zbin_ptr[rc] ; michael@0: michael@0: sz = (z >> 31); /* sign of z */ michael@0: x = (z ^ sz) - sz; /* x = abs(z) */ michael@0: michael@0: if (x >= zbin) michael@0: { michael@0: x += round_ptr[rc]; michael@0: y = ((((x * quant_ptr[rc]) >> 16) + x) michael@0: * quant_shift_ptr[rc]) >> 16; /* quantize (x) */ 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]; /* dequantized value */ michael@0: michael@0: if (y) michael@0: { michael@0: eob = i; /* last nonzero coeffs */ michael@0: } michael@0: } michael@0: } michael@0: *d->eob = (char)(eob + 1); michael@0: } michael@0: michael@0: #else michael@0: michael@0: void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d) michael@0: { michael@0: int i, rc, eob; michael@0: int x, y, z, sz; michael@0: short *coeff_ptr = b->coeff; michael@0: short *round_ptr = b->round; michael@0: short *quant_ptr = b->quant_fast; michael@0: short *qcoeff_ptr = d->qcoeff; michael@0: short *dqcoeff_ptr = d->dqcoeff; michael@0: short *dequant_ptr = d->dequant; michael@0: michael@0: eob = -1; michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: rc = vp8_default_zig_zag1d[i]; michael@0: z = coeff_ptr[rc]; michael@0: michael@0: sz = (z >> 31); /* sign of z */ michael@0: x = (z ^ sz) - sz; /* x = abs(z) */ michael@0: michael@0: y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */ 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]; /* dequantized value */ michael@0: michael@0: if (y) michael@0: { michael@0: eob = i; /* last nonzero coeffs */ michael@0: } michael@0: } michael@0: *d->eob = (char)(eob + 1); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #ifdef EXACT_QUANT michael@0: void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d) michael@0: { michael@0: int i, rc, eob; michael@0: int zbin; michael@0: int x, y, z, sz; michael@0: short *zbin_boost_ptr = b->zrun_zbin_boost; michael@0: short *coeff_ptr = b->coeff; michael@0: short *zbin_ptr = b->zbin; michael@0: short *round_ptr = b->round; michael@0: short *quant_ptr = b->quant; michael@0: short *quant_shift_ptr = b->quant_shift; michael@0: short *qcoeff_ptr = d->qcoeff; michael@0: short *dqcoeff_ptr = d->dqcoeff; michael@0: short *dequant_ptr = d->dequant; michael@0: short zbin_oq_value = b->zbin_extra; michael@0: michael@0: vpx_memset(qcoeff_ptr, 0, 32); michael@0: vpx_memset(dqcoeff_ptr, 0, 32); michael@0: michael@0: eob = -1; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: rc = vp8_default_zig_zag1d[i]; michael@0: z = coeff_ptr[rc]; michael@0: michael@0: zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value; michael@0: michael@0: zbin_boost_ptr ++; michael@0: sz = (z >> 31); /* sign of z */ michael@0: x = (z ^ sz) - sz; /* x = abs(z) */ michael@0: michael@0: if (x >= zbin) michael@0: { michael@0: x += round_ptr[rc]; michael@0: y = ((((x * quant_ptr[rc]) >> 16) + x) michael@0: * quant_shift_ptr[rc]) >> 16; /* quantize (x) */ 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]; /* dequantized value */ michael@0: michael@0: if (y) michael@0: { michael@0: eob = i; /* last nonzero coeffs */ michael@0: zbin_boost_ptr = b->zrun_zbin_boost; /* reset zero runlength */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: *d->eob = (char)(eob + 1); michael@0: } michael@0: michael@0: /* Perform regular quantization, with unbiased rounding and no zero bin. */ michael@0: void vp8_strict_quantize_b_c(BLOCK *b, BLOCKD *d) michael@0: { michael@0: int i; michael@0: int rc; michael@0: int eob; michael@0: int x; michael@0: int y; michael@0: int z; michael@0: int sz; michael@0: short *coeff_ptr; michael@0: short *quant_ptr; michael@0: short *quant_shift_ptr; michael@0: short *qcoeff_ptr; michael@0: short *dqcoeff_ptr; michael@0: short *dequant_ptr; michael@0: michael@0: coeff_ptr = b->coeff; michael@0: quant_ptr = b->quant; michael@0: quant_shift_ptr = b->quant_shift; michael@0: qcoeff_ptr = d->qcoeff; michael@0: dqcoeff_ptr = d->dqcoeff; michael@0: dequant_ptr = d->dequant; michael@0: eob = - 1; michael@0: vpx_memset(qcoeff_ptr, 0, 32); michael@0: vpx_memset(dqcoeff_ptr, 0, 32); michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: int dq; michael@0: int rounding; michael@0: michael@0: /*TODO: These arrays should be stored in zig-zag order.*/ michael@0: rc = vp8_default_zig_zag1d[i]; michael@0: z = coeff_ptr[rc]; michael@0: dq = dequant_ptr[rc]; michael@0: rounding = dq >> 1; michael@0: /* Sign of z. */ michael@0: sz = -(z < 0); michael@0: x = (z + sz) ^ sz; michael@0: x += rounding; michael@0: if (x >= dq) michael@0: { michael@0: /* Quantize x. */ michael@0: y = ((((x * quant_ptr[rc]) >> 16) + x) * quant_shift_ptr[rc]) >> 16; michael@0: /* Put the sign back. */ michael@0: x = (y + sz) ^ sz; michael@0: /* Save the coefficient and its dequantized value. */ michael@0: qcoeff_ptr[rc] = x; michael@0: dqcoeff_ptr[rc] = x * dq; michael@0: /* Remember the last non-zero coefficient. */ michael@0: if (y) michael@0: eob = i; michael@0: } michael@0: } michael@0: michael@0: *d->eob = (char)(eob + 1); michael@0: } michael@0: michael@0: #else michael@0: michael@0: void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d) michael@0: { michael@0: int i, rc, eob; michael@0: int zbin; michael@0: int x, y, z, sz; michael@0: short *zbin_boost_ptr = b->zrun_zbin_boost; michael@0: short *coeff_ptr = b->coeff; michael@0: short *zbin_ptr = b->zbin; michael@0: short *round_ptr = b->round; michael@0: short *quant_ptr = b->quant; michael@0: short *qcoeff_ptr = d->qcoeff; michael@0: short *dqcoeff_ptr = d->dqcoeff; michael@0: short *dequant_ptr = d->dequant; michael@0: short zbin_oq_value = b->zbin_extra; michael@0: michael@0: vpx_memset(qcoeff_ptr, 0, 32); michael@0: vpx_memset(dqcoeff_ptr, 0, 32); michael@0: michael@0: eob = -1; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: rc = vp8_default_zig_zag1d[i]; michael@0: z = coeff_ptr[rc]; michael@0: michael@0: zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value; michael@0: michael@0: zbin_boost_ptr ++; michael@0: sz = (z >> 31); /* sign of z */ michael@0: x = (z ^ sz) - sz; /* x = abs(z) */ michael@0: michael@0: if (x >= zbin) michael@0: { michael@0: y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */ 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]; /* dequantized value */ michael@0: michael@0: if (y) michael@0: { michael@0: eob = i; /* last nonzero coeffs */ michael@0: zbin_boost_ptr = &b->zrun_zbin_boost[0]; /* reset zrl */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: *d->eob = (char)(eob + 1); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: void vp8_quantize_mby_c(MACROBLOCK *x) michael@0: { michael@0: int i; michael@0: int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED michael@0: && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV); michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: x->quantize_b(&x->block[i], &x->e_mbd.block[i]); michael@0: michael@0: if(has_2nd_order) michael@0: x->quantize_b(&x->block[24], &x->e_mbd.block[24]); michael@0: } michael@0: michael@0: void vp8_quantize_mb_c(MACROBLOCK *x) michael@0: { michael@0: int i; michael@0: int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED michael@0: && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV); michael@0: michael@0: for (i = 0; i < 24+has_2nd_order; i++) michael@0: x->quantize_b(&x->block[i], &x->e_mbd.block[i]); michael@0: } michael@0: michael@0: michael@0: void vp8_quantize_mbuv_c(MACROBLOCK *x) michael@0: { michael@0: int i; michael@0: michael@0: for (i = 16; i < 24; i++) michael@0: x->quantize_b(&x->block[i], &x->e_mbd.block[i]); michael@0: } michael@0: michael@0: /* quantize_b_pair function pointer in MACROBLOCK structure is set to one of michael@0: * these two C functions if corresponding optimized routine is not available. michael@0: * NEON optimized version implements currently the fast quantization for pair michael@0: * of blocks. */ michael@0: void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2) michael@0: { michael@0: vp8_regular_quantize_b(b1, d1); michael@0: vp8_regular_quantize_b(b2, d2); michael@0: } michael@0: michael@0: void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2) michael@0: { michael@0: vp8_fast_quantize_b_c(b1, d1); michael@0: vp8_fast_quantize_b_c(b2, d2); michael@0: } michael@0: michael@0: michael@0: static const int qrounding_factors[129] = michael@0: { michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48 michael@0: }; michael@0: michael@0: michael@0: static const int qzbin_factors[129] = michael@0: { michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80 michael@0: }; michael@0: michael@0: michael@0: static const int qrounding_factors_y2[129] = michael@0: { michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48, 48, 48, 48, 48, 48, 48, 48, michael@0: 48 michael@0: }; michael@0: michael@0: michael@0: static const int qzbin_factors_y2[129] = michael@0: { michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 84, 84, 84, 84, 84, 84, 84, 84, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80, 80, 80, 80, 80, 80, 80, 80, michael@0: 80 michael@0: }; michael@0: michael@0: michael@0: #define EXACT_QUANT michael@0: #ifdef EXACT_QUANT michael@0: static void invert_quant(int improved_quant, short *quant, michael@0: short *shift, short d) michael@0: { michael@0: if(improved_quant) michael@0: { 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 = (short)(t - (1<<16)); michael@0: *shift = l; michael@0: /* use multiplication and constant shift by 16 */ michael@0: *shift = 1 << (16 - *shift); michael@0: } michael@0: else michael@0: { michael@0: *quant = (1 << 16) / d; michael@0: *shift = 0; michael@0: /* use multiplication and constant shift by 16 */ michael@0: *shift = 1 << (16 - *shift); michael@0: } michael@0: } michael@0: michael@0: michael@0: void vp8cx_init_quantizer(VP8_COMP *cpi) michael@0: { michael@0: int i; michael@0: int quant_val; michael@0: int Q; michael@0: michael@0: int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, michael@0: 44, 44}; michael@0: michael@0: for (Q = 0; Q < QINDEX_RANGE; Q++) michael@0: { michael@0: /* dc values */ michael@0: quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q); michael@0: cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val; michael@0: invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0, michael@0: cpi->Y1quant_shift[Q] + 0, quant_val); michael@0: cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.Y1dequant[Q][0] = quant_val; michael@0: cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7; michael@0: michael@0: quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q); michael@0: cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val; michael@0: invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0, michael@0: cpi->Y2quant_shift[Q] + 0, quant_val); michael@0: cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7; michael@0: cpi->common.Y2dequant[Q][0] = quant_val; michael@0: cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7; michael@0: michael@0: quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q); michael@0: cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val; michael@0: invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0, michael@0: cpi->UVquant_shift[Q] + 0, quant_val); michael@0: cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;; michael@0: cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.UVdequant[Q][0] = quant_val; michael@0: cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7; michael@0: michael@0: /* all the ac values = ; */ michael@0: quant_val = vp8_ac_yquant(Q); michael@0: cpi->Y1quant_fast[Q][1] = (1 << 16) / quant_val; michael@0: invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 1, michael@0: cpi->Y1quant_shift[Q] + 1, quant_val); michael@0: cpi->Y1zbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y1round[Q][1] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.Y1dequant[Q][1] = quant_val; michael@0: cpi->zrun_zbin_boost_y1[Q][1] = (quant_val * zbin_boost[1]) >> 7; michael@0: michael@0: quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q); michael@0: cpi->Y2quant_fast[Q][1] = (1 << 16) / quant_val; michael@0: invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 1, michael@0: cpi->Y2quant_shift[Q] + 1, quant_val); michael@0: cpi->Y2zbin[Q][1] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y2round[Q][1] = (qrounding_factors_y2[Q] * quant_val) >> 7; michael@0: cpi->common.Y2dequant[Q][1] = quant_val; michael@0: cpi->zrun_zbin_boost_y2[Q][1] = (quant_val * zbin_boost[1]) >> 7; michael@0: michael@0: quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q); michael@0: cpi->UVquant_fast[Q][1] = (1 << 16) / quant_val; michael@0: invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 1, michael@0: cpi->UVquant_shift[Q] + 1, quant_val); michael@0: cpi->UVzbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7; michael@0: cpi->UVround[Q][1] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.UVdequant[Q][1] = quant_val; michael@0: cpi->zrun_zbin_boost_uv[Q][1] = (quant_val * zbin_boost[1]) >> 7; michael@0: michael@0: for (i = 2; i < 16; i++) michael@0: { michael@0: cpi->Y1quant_fast[Q][i] = cpi->Y1quant_fast[Q][1]; michael@0: cpi->Y1quant[Q][i] = cpi->Y1quant[Q][1]; michael@0: cpi->Y1quant_shift[Q][i] = cpi->Y1quant_shift[Q][1]; michael@0: cpi->Y1zbin[Q][i] = cpi->Y1zbin[Q][1]; michael@0: cpi->Y1round[Q][i] = cpi->Y1round[Q][1]; michael@0: cpi->zrun_zbin_boost_y1[Q][i] = (cpi->common.Y1dequant[Q][1] * michael@0: zbin_boost[i]) >> 7; michael@0: michael@0: cpi->Y2quant_fast[Q][i] = cpi->Y2quant_fast[Q][1]; michael@0: cpi->Y2quant[Q][i] = cpi->Y2quant[Q][1]; michael@0: cpi->Y2quant_shift[Q][i] = cpi->Y2quant_shift[Q][1]; michael@0: cpi->Y2zbin[Q][i] = cpi->Y2zbin[Q][1]; michael@0: cpi->Y2round[Q][i] = cpi->Y2round[Q][1]; michael@0: cpi->zrun_zbin_boost_y2[Q][i] = (cpi->common.Y2dequant[Q][1] * michael@0: zbin_boost[i]) >> 7; michael@0: michael@0: cpi->UVquant_fast[Q][i] = cpi->UVquant_fast[Q][1]; michael@0: cpi->UVquant[Q][i] = cpi->UVquant[Q][1]; michael@0: cpi->UVquant_shift[Q][i] = cpi->UVquant_shift[Q][1]; michael@0: cpi->UVzbin[Q][i] = cpi->UVzbin[Q][1]; michael@0: cpi->UVround[Q][i] = cpi->UVround[Q][1]; michael@0: cpi->zrun_zbin_boost_uv[Q][i] = (cpi->common.UVdequant[Q][1] * michael@0: zbin_boost[i]) >> 7; michael@0: } michael@0: } michael@0: } michael@0: #else michael@0: void vp8cx_init_quantizer(VP8_COMP *cpi) michael@0: { michael@0: int i; michael@0: int quant_val; michael@0: int Q; michael@0: michael@0: int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44}; michael@0: michael@0: for (Q = 0; Q < QINDEX_RANGE; Q++) michael@0: { michael@0: /* dc values */ michael@0: quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q); michael@0: cpi->Y1quant[Q][0] = (1 << 16) / quant_val; michael@0: cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.Y1dequant[Q][0] = quant_val; michael@0: cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7; michael@0: michael@0: quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q); michael@0: cpi->Y2quant[Q][0] = (1 << 16) / quant_val; michael@0: cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7; michael@0: cpi->common.Y2dequant[Q][0] = quant_val; michael@0: cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7; michael@0: michael@0: quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q); michael@0: cpi->UVquant[Q][0] = (1 << 16) / quant_val; michael@0: cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;; michael@0: cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.UVdequant[Q][0] = quant_val; michael@0: cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7; michael@0: michael@0: /* all the ac values = ; */ michael@0: for (i = 1; i < 16; i++) michael@0: { michael@0: int rc = vp8_default_zig_zag1d[i]; michael@0: michael@0: quant_val = vp8_ac_yquant(Q); michael@0: cpi->Y1quant[Q][rc] = (1 << 16) / quant_val; michael@0: cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.Y1dequant[Q][rc] = quant_val; michael@0: cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7; michael@0: michael@0: quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q); michael@0: cpi->Y2quant[Q][rc] = (1 << 16) / quant_val; michael@0: cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7; michael@0: cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7; michael@0: cpi->common.Y2dequant[Q][rc] = quant_val; michael@0: cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7; michael@0: michael@0: quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q); michael@0: cpi->UVquant[Q][rc] = (1 << 16) / quant_val; michael@0: cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7; michael@0: cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7; michael@0: cpi->common.UVdequant[Q][rc] = quant_val; michael@0: cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #define ZBIN_EXTRA_Y \ michael@0: (( cpi->common.Y1dequant[QIndex][1] * \ michael@0: ( x->zbin_over_quant + \ michael@0: x->zbin_mode_boost + \ michael@0: x->act_zbin_adj ) ) >> 7) michael@0: michael@0: #define ZBIN_EXTRA_UV \ michael@0: (( cpi->common.UVdequant[QIndex][1] * \ michael@0: ( x->zbin_over_quant + \ michael@0: x->zbin_mode_boost + \ michael@0: x->act_zbin_adj ) ) >> 7) michael@0: michael@0: #define ZBIN_EXTRA_Y2 \ michael@0: (( cpi->common.Y2dequant[QIndex][1] * \ michael@0: ( (x->zbin_over_quant / 2) + \ michael@0: x->zbin_mode_boost + \ michael@0: x->act_zbin_adj ) ) >> 7) michael@0: michael@0: void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip) michael@0: { michael@0: int i; michael@0: int QIndex; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: int zbin_extra; michael@0: michael@0: /* Select the baseline MB Q index. */ michael@0: if (xd->segmentation_enabled) michael@0: { michael@0: /* Abs Value */ michael@0: if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA) michael@0: QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id]; michael@0: /* Delta Value */ michael@0: else michael@0: { michael@0: QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id]; michael@0: /* Clamp to valid range */ michael@0: QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0; michael@0: } michael@0: } michael@0: else michael@0: QIndex = cpi->common.base_qindex; michael@0: michael@0: /* This initialization should be called at least once. Use ok_to_skip to michael@0: * decide if it is ok to skip. michael@0: * Before encoding a frame, this function is always called with ok_to_skip michael@0: * =0, which means no skiping of calculations. The "last" values are michael@0: * initialized at that time. michael@0: */ michael@0: if (!ok_to_skip || QIndex != x->q_index) michael@0: { michael@0: michael@0: xd->dequant_y1_dc[0] = 1; michael@0: xd->dequant_y1[0] = cpi->common.Y1dequant[QIndex][0]; michael@0: xd->dequant_y2[0] = cpi->common.Y2dequant[QIndex][0]; michael@0: xd->dequant_uv[0] = cpi->common.UVdequant[QIndex][0]; michael@0: michael@0: for (i = 1; i < 16; i++) michael@0: { michael@0: xd->dequant_y1_dc[i] = michael@0: xd->dequant_y1[i] = cpi->common.Y1dequant[QIndex][1]; michael@0: xd->dequant_y2[i] = cpi->common.Y2dequant[QIndex][1]; michael@0: xd->dequant_uv[i] = cpi->common.UVdequant[QIndex][1]; michael@0: } michael@0: #if 1 michael@0: /*TODO: Remove dequant from BLOCKD. This is a temporary solution until michael@0: * the quantizer code uses a passed in pointer to the dequant constants. michael@0: * This will also require modifications to the x86 and neon assembly. michael@0: * */ michael@0: for (i = 0; i < 16; i++) michael@0: x->e_mbd.block[i].dequant = xd->dequant_y1; michael@0: for (i = 16; i < 24; i++) michael@0: x->e_mbd.block[i].dequant = xd->dequant_uv; michael@0: x->e_mbd.block[24].dequant = xd->dequant_y2; michael@0: #endif michael@0: michael@0: /* Y */ michael@0: zbin_extra = ZBIN_EXTRA_Y; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: x->block[i].quant = cpi->Y1quant[QIndex]; michael@0: x->block[i].quant_fast = cpi->Y1quant_fast[QIndex]; michael@0: x->block[i].quant_shift = cpi->Y1quant_shift[QIndex]; michael@0: x->block[i].zbin = cpi->Y1zbin[QIndex]; michael@0: x->block[i].round = cpi->Y1round[QIndex]; michael@0: x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex]; michael@0: x->block[i].zbin_extra = (short)zbin_extra; michael@0: } michael@0: michael@0: /* UV */ michael@0: zbin_extra = ZBIN_EXTRA_UV; michael@0: michael@0: for (i = 16; i < 24; i++) michael@0: { michael@0: x->block[i].quant = cpi->UVquant[QIndex]; michael@0: x->block[i].quant_fast = cpi->UVquant_fast[QIndex]; michael@0: x->block[i].quant_shift = cpi->UVquant_shift[QIndex]; michael@0: x->block[i].zbin = cpi->UVzbin[QIndex]; michael@0: x->block[i].round = cpi->UVround[QIndex]; michael@0: x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex]; michael@0: x->block[i].zbin_extra = (short)zbin_extra; michael@0: } michael@0: michael@0: /* Y2 */ michael@0: zbin_extra = ZBIN_EXTRA_Y2; michael@0: michael@0: x->block[24].quant_fast = cpi->Y2quant_fast[QIndex]; michael@0: x->block[24].quant = cpi->Y2quant[QIndex]; michael@0: x->block[24].quant_shift = cpi->Y2quant_shift[QIndex]; michael@0: x->block[24].zbin = cpi->Y2zbin[QIndex]; michael@0: x->block[24].round = cpi->Y2round[QIndex]; michael@0: x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex]; michael@0: x->block[24].zbin_extra = (short)zbin_extra; michael@0: michael@0: /* save this macroblock QIndex for vp8_update_zbin_extra() */ michael@0: x->q_index = QIndex; michael@0: michael@0: x->last_zbin_over_quant = x->zbin_over_quant; michael@0: x->last_zbin_mode_boost = x->zbin_mode_boost; michael@0: x->last_act_zbin_adj = x->act_zbin_adj; michael@0: michael@0: michael@0: michael@0: } michael@0: else if(x->last_zbin_over_quant != x->zbin_over_quant michael@0: || x->last_zbin_mode_boost != x->zbin_mode_boost michael@0: || x->last_act_zbin_adj != x->act_zbin_adj) michael@0: { michael@0: /* Y */ michael@0: zbin_extra = ZBIN_EXTRA_Y; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: x->block[i].zbin_extra = (short)zbin_extra; michael@0: michael@0: /* UV */ michael@0: zbin_extra = ZBIN_EXTRA_UV; michael@0: michael@0: for (i = 16; i < 24; i++) michael@0: x->block[i].zbin_extra = (short)zbin_extra; michael@0: michael@0: /* Y2 */ michael@0: zbin_extra = ZBIN_EXTRA_Y2; michael@0: x->block[24].zbin_extra = (short)zbin_extra; michael@0: michael@0: x->last_zbin_over_quant = x->zbin_over_quant; michael@0: x->last_zbin_mode_boost = x->zbin_mode_boost; michael@0: x->last_act_zbin_adj = x->act_zbin_adj; michael@0: } michael@0: } michael@0: michael@0: void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x) michael@0: { michael@0: int i; michael@0: int QIndex = x->q_index; michael@0: int zbin_extra; michael@0: michael@0: /* Y */ michael@0: zbin_extra = ZBIN_EXTRA_Y; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: x->block[i].zbin_extra = (short)zbin_extra; michael@0: michael@0: /* UV */ michael@0: zbin_extra = ZBIN_EXTRA_UV; michael@0: michael@0: for (i = 16; i < 24; i++) michael@0: x->block[i].zbin_extra = (short)zbin_extra; michael@0: michael@0: /* Y2 */ michael@0: zbin_extra = ZBIN_EXTRA_Y2; michael@0: x->block[24].zbin_extra = (short)zbin_extra; michael@0: } michael@0: #undef ZBIN_EXTRA_Y michael@0: #undef ZBIN_EXTRA_UV michael@0: #undef ZBIN_EXTRA_Y2 michael@0: michael@0: void vp8cx_frame_init_quantizer(VP8_COMP *cpi) michael@0: { michael@0: /* Clear Zbin mode boost for default case */ michael@0: cpi->mb.zbin_mode_boost = 0; michael@0: michael@0: /* MB level quantizer setup */ michael@0: vp8cx_mb_init_quantizer(cpi, &cpi->mb, 0); michael@0: } michael@0: michael@0: michael@0: void vp8_set_quantizer(struct VP8_COMP *cpi, int Q) michael@0: { michael@0: VP8_COMMON *cm = &cpi->common; michael@0: MACROBLOCKD *mbd = &cpi->mb.e_mbd; michael@0: int update = 0; michael@0: int new_delta_q; michael@0: cm->base_qindex = Q; michael@0: michael@0: /* if any of the delta_q values are changing update flag has to be set */ michael@0: /* currently only y2dc_delta_q may change */ michael@0: michael@0: cm->y1dc_delta_q = 0; michael@0: cm->y2ac_delta_q = 0; michael@0: cm->uvdc_delta_q = 0; michael@0: cm->uvac_delta_q = 0; michael@0: michael@0: if (Q < 4) michael@0: { michael@0: new_delta_q = 4-Q; michael@0: } michael@0: else michael@0: new_delta_q = 0; michael@0: michael@0: update |= cm->y2dc_delta_q != new_delta_q; michael@0: cm->y2dc_delta_q = new_delta_q; michael@0: michael@0: michael@0: /* Set Segment specific quatizers */ michael@0: mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0]; michael@0: mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1]; michael@0: mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2]; michael@0: mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3]; michael@0: michael@0: /* quantizer has to be reinitialized for any delta_q changes */ michael@0: if(update) michael@0: vp8cx_init_quantizer(cpi); michael@0: michael@0: }