| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <math.h> | 11 #include <math.h> |
| 12 #include "vpx_mem/vpx_mem.h" | 12 #include "vpx_mem/vpx_mem.h" |
| 13 | 13 |
| 14 #include "vp9/encoder/vp9_onyx_int.h" | 14 #include "vp9/encoder/vp9_onyx_int.h" |
| 15 #include "vp9/encoder/vp9_rdopt.h" | 15 #include "vp9/encoder/vp9_rdopt.h" |
| 16 #include "vp9/encoder/vp9_quantize.h" | 16 #include "vp9/encoder/vp9_quantize.h" |
| 17 #include "vp9/common/vp9_quant_common.h" | 17 #include "vp9/common/vp9_quant_common.h" |
| 18 | 18 |
| 19 #include "vp9/common/vp9_seg_common.h" | 19 #include "vp9/common/vp9_seg_common.h" |
| 20 | 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, | 21 void vp9_quantize_b_c(const int16_t *coeff_ptr, intptr_t count, |
| 26 int skip_block, | 22 int skip_block, |
| 27 const int16_t *zbin_ptr, const int16_t *round_ptr, | 23 const int16_t *zbin_ptr, const int16_t *round_ptr, |
| 28 const int16_t *quant_ptr, const int16_t *quant_shift_ptr, | 24 const int16_t *quant_ptr, const int16_t *quant_shift_ptr, |
| 29 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr, | 25 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr, |
| 30 const int16_t *dequant_ptr, | 26 const int16_t *dequant_ptr, |
| 31 int zbin_oq_value, uint16_t *eob_ptr, | 27 int zbin_oq_value, uint16_t *eob_ptr, |
| 32 const int16_t *scan, const int16_t *iscan) { | 28 const int16_t *scan, const int16_t *iscan) { |
| 33 int i, non_zero_count = count, eob = -1; | 29 int i, non_zero_count = count, eob = -1; |
| 34 const int zbins[2] = { zbin_ptr[0] + zbin_oq_value, | 30 const int zbins[2] = { zbin_ptr[0] + zbin_oq_value, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 72 |
| 77 void vp9_quantize_b_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs, | 73 void vp9_quantize_b_32x32_c(const int16_t *coeff_ptr, intptr_t n_coeffs, |
| 78 int skip_block, | 74 int skip_block, |
| 79 const int16_t *zbin_ptr, const int16_t *round_ptr, | 75 const int16_t *zbin_ptr, const int16_t *round_ptr, |
| 80 const int16_t *quant_ptr, | 76 const int16_t *quant_ptr, |
| 81 const int16_t *quant_shift_ptr, | 77 const int16_t *quant_shift_ptr, |
| 82 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr, | 78 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr, |
| 83 const int16_t *dequant_ptr, | 79 const int16_t *dequant_ptr, |
| 84 int zbin_oq_value, uint16_t *eob_ptr, | 80 int zbin_oq_value, uint16_t *eob_ptr, |
| 85 const int16_t *scan, const int16_t *iscan) { | 81 const int16_t *scan, const int16_t *iscan) { |
| 86 int i, rc, eob; | 82 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1), |
| 87 int zbins[2], nzbins[2]; | 83 ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1) }; |
| 88 int x, y, z, sz; | 84 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1}; |
| 85 |
| 89 int idx = 0; | 86 int idx = 0; |
| 90 int idx_arr[1024]; | 87 int idx_arr[1024]; |
| 88 int i, eob = -1; |
| 91 | 89 |
| 92 vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t)); | 90 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(int16_t)); |
| 93 vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t)); | 91 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 | 92 |
| 103 if (!skip_block) { | 93 if (!skip_block) { |
| 104 // Pre-scan pass | 94 // Pre-scan pass |
| 105 for (i = 0; i < n_coeffs; i++) { | 95 for (i = 0; i < n_coeffs; i++) { |
| 106 rc = scan[i]; | 96 const int rc = scan[i]; |
| 107 z = coeff_ptr[rc]; | 97 const int coeff = coeff_ptr[rc]; |
| 108 | 98 |
| 109 // If the coefficient is out of the base ZBIN range, keep it for | 99 // If the coefficient is out of the base ZBIN range, keep it for |
| 110 // quantization. | 100 // quantization. |
| 111 if (z >= zbins[rc != 0] || z <= nzbins[rc != 0]) | 101 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0]) |
| 112 idx_arr[idx++] = i; | 102 idx_arr[idx++] = i; |
| 113 } | 103 } |
| 114 | 104 |
| 115 // Quantization pass: only process the coefficients selected in | 105 // Quantization pass: only process the coefficients selected in |
| 116 // pre-scan pass. Note: idx can be zero. | 106 // pre-scan pass. Note: idx can be zero. |
| 117 for (i = 0; i < idx; i++) { | 107 for (i = 0; i < idx; i++) { |
| 118 rc = scan[idx_arr[i]]; | 108 const int rc = scan[idx_arr[i]]; |
| 109 const int coeff = coeff_ptr[rc]; |
| 110 const int coeff_sign = (coeff >> 31); |
| 111 int tmp; |
| 112 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 113 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); |
| 114 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX); |
| 115 tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) * |
| 116 quant_shift_ptr[rc != 0]) >> 15; |
| 119 | 117 |
| 120 z = coeff_ptr[rc]; | 118 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; |
| 121 sz = (z >> 31); // sign of z | 119 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2; |
| 122 x = (z ^ sz) - sz; // x = abs(z) | |
| 123 | 120 |
| 124 x += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); | 121 if (tmp) |
| 125 x = clamp(x, INT16_MIN, INT16_MAX); | 122 eob = idx_arr[i]; |
| 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 } | 123 } |
| 136 } | 124 } |
| 137 *eob_ptr = eob + 1; | 125 *eob_ptr = eob + 1; |
| 138 } | 126 } |
| 139 | 127 |
| 140 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block, | 128 void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block, |
| 141 const int16_t *scan, const int16_t *iscan) { | 129 const int16_t *scan, const int16_t *iscan) { |
| 142 MACROBLOCKD *const xd = &x->e_mbd; | 130 MACROBLOCKD *const xd = &x->e_mbd; |
| 143 struct macroblock_plane* p = &x->plane[plane]; | 131 struct macroblock_plane *p = &x->plane[plane]; |
| 144 struct macroblockd_plane* pd = &xd->plane[plane]; | 132 struct macroblockd_plane *pd = &xd->plane[plane]; |
| 145 | 133 |
| 146 vp9_quantize_b(BLOCK_OFFSET(p->coeff, block), | 134 vp9_quantize_b(BLOCK_OFFSET(p->coeff, block), |
| 147 16, x->skip_block, | 135 16, x->skip_block, |
| 148 p->zbin, p->round, p->quant, p->quant_shift, | 136 p->zbin, p->round, p->quant, p->quant_shift, |
| 149 BLOCK_OFFSET(p->qcoeff, block), | 137 BLOCK_OFFSET(p->qcoeff, block), |
| 150 BLOCK_OFFSET(pd->dqcoeff, block), | 138 BLOCK_OFFSET(pd->dqcoeff, block), |
| 151 pd->dequant, p->zbin_extra, &p->eobs[block], scan, iscan); | 139 pd->dequant, p->zbin_extra, &p->eobs[block], scan, iscan); |
| 152 } | 140 } |
| 153 | 141 |
| 154 static void invert_quant(int16_t *quant, int16_t *shift, int d) { | 142 static void invert_quant(int16_t *quant, int16_t *shift, int d) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 cpi->a_quant_shift[q][i] = cpi->a_quant_shift[q][1]; | 208 cpi->a_quant_shift[q][i] = cpi->a_quant_shift[q][1]; |
| 221 cpi->a_zbin[q][i] = cpi->a_zbin[q][1]; | 209 cpi->a_zbin[q][i] = cpi->a_zbin[q][1]; |
| 222 cpi->a_round[q][i] = cpi->a_round[q][1]; | 210 cpi->a_round[q][i] = cpi->a_round[q][1]; |
| 223 cm->a_dequant[q][i] = cm->a_dequant[q][1]; | 211 cm->a_dequant[q][i] = cm->a_dequant[q][1]; |
| 224 #endif | 212 #endif |
| 225 } | 213 } |
| 226 } | 214 } |
| 227 } | 215 } |
| 228 | 216 |
| 229 void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) { | 217 void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) { |
| 218 const VP9_COMMON *const cm = &cpi->common; |
| 219 MACROBLOCKD *xd = &x->e_mbd; |
| 220 const int segment_id = xd->mi_8x8[0]->mbmi.segment_id; |
| 221 const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex); |
| 222 const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q); |
| 223 const int zbin = cpi->zbin_mode_boost + x->act_zbin_adj; |
| 230 int i; | 224 int i; |
| 231 VP9_COMMON *const cm = &cpi->common; | |
| 232 MACROBLOCKD *xd = &x->e_mbd; | |
| 233 int zbin_extra; | |
| 234 int segment_id = xd->mi_8x8[0]->mbmi.segment_id; | |
| 235 const int qindex = vp9_get_qindex(&cpi->common.seg, segment_id, | |
| 236 cpi->common.base_qindex); | |
| 237 | |
| 238 int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q); | |
| 239 | 225 |
| 240 // Y | 226 // Y |
| 241 zbin_extra = (cpi->common.y_dequant[qindex][1] * | |
| 242 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; | |
| 243 | |
| 244 x->plane[0].quant = cpi->y_quant[qindex]; | 227 x->plane[0].quant = cpi->y_quant[qindex]; |
| 245 x->plane[0].quant_shift = cpi->y_quant_shift[qindex]; | 228 x->plane[0].quant_shift = cpi->y_quant_shift[qindex]; |
| 246 x->plane[0].zbin = cpi->y_zbin[qindex]; | 229 x->plane[0].zbin = cpi->y_zbin[qindex]; |
| 247 x->plane[0].round = cpi->y_round[qindex]; | 230 x->plane[0].round = cpi->y_round[qindex]; |
| 248 x->plane[0].zbin_extra = (int16_t)zbin_extra; | 231 x->plane[0].zbin_extra = (int16_t)((cm->y_dequant[qindex][1] * zbin) >> 7); |
| 249 x->e_mbd.plane[0].dequant = cpi->common.y_dequant[qindex]; | 232 xd->plane[0].dequant = cm->y_dequant[qindex]; |
| 250 | 233 |
| 251 // UV | 234 // UV |
| 252 zbin_extra = (cpi->common.uv_dequant[qindex][1] * | |
| 253 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; | |
| 254 | |
| 255 for (i = 1; i < 3; i++) { | 235 for (i = 1; i < 3; i++) { |
| 256 x->plane[i].quant = cpi->uv_quant[qindex]; | 236 x->plane[i].quant = cpi->uv_quant[qindex]; |
| 257 x->plane[i].quant_shift = cpi->uv_quant_shift[qindex]; | 237 x->plane[i].quant_shift = cpi->uv_quant_shift[qindex]; |
| 258 x->plane[i].zbin = cpi->uv_zbin[qindex]; | 238 x->plane[i].zbin = cpi->uv_zbin[qindex]; |
| 259 x->plane[i].round = cpi->uv_round[qindex]; | 239 x->plane[i].round = cpi->uv_round[qindex]; |
| 260 x->plane[i].zbin_extra = (int16_t)zbin_extra; | 240 x->plane[i].zbin_extra = (int16_t)((cm->uv_dequant[qindex][1] * zbin) >> 7); |
| 261 x->e_mbd.plane[i].dequant = cpi->common.uv_dequant[qindex]; | 241 xd->plane[i].dequant = cm->uv_dequant[qindex]; |
| 262 } | 242 } |
| 263 | 243 |
| 264 #if CONFIG_ALPHA | 244 #if CONFIG_ALPHA |
| 265 x->plane[3].quant = cpi->a_quant[qindex]; | 245 x->plane[3].quant = cpi->a_quant[qindex]; |
| 266 x->plane[3].quant_shift = cpi->a_quant_shift[qindex]; | 246 x->plane[3].quant_shift = cpi->a_quant_shift[qindex]; |
| 267 x->plane[3].zbin = cpi->a_zbin[qindex]; | 247 x->plane[3].zbin = cpi->a_zbin[qindex]; |
| 268 x->plane[3].round = cpi->a_round[qindex]; | 248 x->plane[3].round = cpi->a_round[qindex]; |
| 269 x->plane[3].zbin_extra = (int16_t)zbin_extra; | 249 x->plane[3].zbin_extra = (int16_t)zbin_extra; |
| 270 x->e_mbd.plane[3].dequant = cpi->common.a_dequant[qindex]; | 250 xd->plane[3].dequant = cm->a_dequant[qindex]; |
| 271 #endif | 251 #endif |
| 272 | 252 |
| 273 x->skip_block = vp9_segfeature_active(&cpi->common.seg, segment_id, | 253 x->skip_block = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP); |
| 274 SEG_LVL_SKIP); | |
| 275 | |
| 276 /* save this macroblock QIndex for vp9_update_zbin_extra() */ | |
| 277 x->q_index = qindex; | 254 x->q_index = qindex; |
| 278 | 255 |
| 279 /* R/D setup */ | 256 x->errorperbit = rdmult >> 6; |
| 280 cpi->mb.errorperbit = rdmult >> 6; | 257 x->errorperbit += (x->errorperbit == 0); |
| 281 cpi->mb.errorperbit += (cpi->mb.errorperbit == 0); | |
| 282 | 258 |
| 283 vp9_initialize_me_consts(cpi, x->q_index); | 259 vp9_initialize_me_consts(cpi, x->q_index); |
| 284 } | 260 } |
| 285 | 261 |
| 286 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) { | 262 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) { |
| 287 const int qindex = x->q_index; | 263 const int qindex = x->q_index; |
| 288 const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] * | 264 const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] * |
| 289 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; | 265 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; |
| 290 const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] * | 266 const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] * |
| 291 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; | 267 (cpi->zbin_mode_boost + x->act_zbin_adj)) >> 7; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 312 // have to be set. | 288 // have to be set. |
| 313 cm->y_dc_delta_q = 0; | 289 cm->y_dc_delta_q = 0; |
| 314 cm->uv_dc_delta_q = 0; | 290 cm->uv_dc_delta_q = 0; |
| 315 cm->uv_ac_delta_q = 0; | 291 cm->uv_ac_delta_q = 0; |
| 316 | 292 |
| 317 // quantizer has to be reinitialized if any delta_q changes. | 293 // quantizer has to be reinitialized if any delta_q changes. |
| 318 // As there are not any here for now this is inactive code. | 294 // As there are not any here for now this is inactive code. |
| 319 // if(update) | 295 // if(update) |
| 320 // vp9_init_quantizer(cpi); | 296 // vp9_init_quantizer(cpi); |
| 321 } | 297 } |
| OLD | NEW |