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 13#include "vpx_mem/vpx_mem.h" 14 15#include "vp9/common/vp9_quant_common.h" 16#include "vp9/common/vp9_seg_common.h" 17 18#include "vp9/encoder/vp9_encoder.h" 19#include "vp9/encoder/vp9_quantize.h" 20#include "vp9/encoder/vp9_rd.h" 21 22void vp9_quantize_dc(const tran_low_t *coeff_ptr, int skip_block, 23 const int16_t *round_ptr, const int16_t quant, 24 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 25 const int16_t dequant_ptr, uint16_t *eob_ptr) { 26 const int rc = 0; 27 const int coeff = coeff_ptr[rc]; 28 const int coeff_sign = (coeff >> 31); 29 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 30 int tmp, eob = -1; 31 32 if (!skip_block) { 33 tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); 34 tmp = (tmp * quant) >> 16; 35 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 36 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr; 37 if (tmp) 38 eob = 0; 39 } 40 *eob_ptr = eob + 1; 41} 42 43#if CONFIG_VP9_HIGHBITDEPTH 44void vp9_high_quantize_dc(const tran_low_t *coeff_ptr, int skip_block, 45 const int16_t *round_ptr, const int16_t quant, 46 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 47 const int16_t dequant_ptr, uint16_t *eob_ptr) { 48 int eob = -1; 49 50 if (!skip_block) { 51 const int rc = 0; 52 const int coeff = coeff_ptr[rc]; 53 const int coeff_sign = (coeff >> 31); 54 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 55 56 const int64_t tmp = 57 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) * 58 quant) >> 16; 59 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 60 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr; 61 if (tmp) 62 eob = 0; 63 } 64 *eob_ptr = eob + 1; 65} 66#endif 67 68void vp9_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block, 69 const int16_t *round_ptr, const int16_t quant, 70 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 71 const int16_t dequant_ptr, uint16_t *eob_ptr) { 72 const int rc = 0; 73 const int coeff = coeff_ptr[rc]; 74 const int coeff_sign = (coeff >> 31); 75 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 76 int tmp, eob = -1; 77 78 if (!skip_block) { 79 80 tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); 81 tmp = (tmp * quant) >> 15; 82 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 83 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2; 84 if (tmp) 85 eob = 0; 86 } 87 *eob_ptr = eob + 1; 88} 89 90#if CONFIG_VP9_HIGHBITDEPTH 91void vp9_high_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block, 92 const int16_t *round_ptr, const int16_t quant, 93 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 94 const int16_t dequant_ptr, uint16_t *eob_ptr) { 95 int eob = -1; 96 97 if (!skip_block) { 98 const int rc = 0; 99 const int coeff = coeff_ptr[rc]; 100 const int coeff_sign = (coeff >> 31); 101 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 102 103 const int64_t tmp = 104 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) * 105 quant) >> 15; 106 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 107 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2; 108 if (tmp) 109 eob = 0; 110 } 111 *eob_ptr = eob + 1; 112} 113#endif 114 115void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, 116 int skip_block, 117 const int16_t *zbin_ptr, const int16_t *round_ptr, 118 const int16_t *quant_ptr, const int16_t *quant_shift_ptr, 119 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 120 const int16_t *dequant_ptr, 121 int zbin_oq_value, uint16_t *eob_ptr, 122 const int16_t *scan, const int16_t *iscan) { 123 int i, eob = -1; 124 // TODO(jingning) Decide the need of these arguments after the 125 // quantization process is completed. 126 (void)zbin_ptr; 127 (void)quant_shift_ptr; 128 (void)zbin_oq_value; 129 (void)iscan; 130 131 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); 132 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); 133 134 if (!skip_block) { 135 // Quantization pass: All coefficients with index >= zero_flag are 136 // skippable. Note: zero_flag can be zero. 137 for (i = 0; i < n_coeffs; i++) { 138 const int rc = scan[i]; 139 const int coeff = coeff_ptr[rc]; 140 const int coeff_sign = (coeff >> 31); 141 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 142 143 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); 144 tmp = (tmp * quant_ptr[rc != 0]) >> 16; 145 146 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 147 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0]; 148 149 if (tmp) 150 eob = i; 151 } 152 } 153 *eob_ptr = eob + 1; 154} 155 156#if CONFIG_VP9_HIGHBITDEPTH 157void vp9_high_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t count, 158 int skip_block, const int16_t *zbin_ptr, 159 const int16_t *round_ptr, const int16_t *quant_ptr, 160 const int16_t *quant_shift_ptr, 161 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 162 const int16_t *dequant_ptr, 163 int zbin_oq_value, uint16_t *eob_ptr, 164 const int16_t *scan, const int16_t *iscan) { 165 int i; 166 int eob = -1; 167 // TODO(jingning) Decide the need of these arguments after the 168 // quantization process is completed. 169 (void)zbin_ptr; 170 (void)quant_shift_ptr; 171 (void)zbin_oq_value; 172 (void)iscan; 173 174 vpx_memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr)); 175 vpx_memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr)); 176 177 if (!skip_block) { 178 // Quantization pass: All coefficients with index >= zero_flag are 179 // skippable. Note: zero_flag can be zero. 180 for (i = 0; i < count; i++) { 181 const int rc = scan[i]; 182 const int coeff = coeff_ptr[rc]; 183 const int coeff_sign = (coeff >> 31); 184 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 185 186 const int64_t tmp = 187 (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) * 188 quant_ptr[rc != 0]) >> 16; 189 190 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 191 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0]; 192 193 if (tmp) 194 eob = i; 195 } 196 } 197 *eob_ptr = eob + 1; 198} 199#endif 200 201// TODO(jingning) Refactor this file and combine functions with similar 202// operations. 203void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, 204 int skip_block, 205 const int16_t *zbin_ptr, const int16_t *round_ptr, 206 const int16_t *quant_ptr, 207 const int16_t *quant_shift_ptr, 208 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 209 const int16_t *dequant_ptr, 210 int zbin_oq_value, uint16_t *eob_ptr, 211 const int16_t *scan, const int16_t *iscan) { 212 int i, eob = -1; 213 (void)zbin_ptr; 214 (void)quant_shift_ptr; 215 (void)zbin_oq_value; 216 (void)iscan; 217 218 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); 219 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); 220 221 if (!skip_block) { 222 for (i = 0; i < n_coeffs; i++) { 223 const int rc = scan[i]; 224 const int coeff = coeff_ptr[rc]; 225 const int coeff_sign = (coeff >> 31); 226 int tmp = 0; 227 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 228 229 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) { 230 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); 231 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX); 232 tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15; 233 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 234 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2; 235 } 236 237 if (tmp) 238 eob = i; 239 } 240 } 241 *eob_ptr = eob + 1; 242} 243 244#if CONFIG_VP9_HIGHBITDEPTH 245void vp9_high_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, 246 intptr_t n_coeffs, int skip_block, 247 const int16_t *zbin_ptr, 248 const int16_t *round_ptr, 249 const int16_t *quant_ptr, 250 const int16_t *quant_shift_ptr, 251 tran_low_t *qcoeff_ptr, 252 tran_low_t *dqcoeff_ptr, 253 const int16_t *dequant_ptr, 254 int zbin_oq_value, uint16_t *eob_ptr, 255 const int16_t *scan, const int16_t *iscan) { 256 int i, eob = -1; 257 (void)zbin_ptr; 258 (void)quant_shift_ptr; 259 (void)zbin_oq_value; 260 (void)iscan; 261 262 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); 263 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); 264 265 if (!skip_block) { 266 for (i = 0; i < n_coeffs; i++) { 267 const int rc = scan[i]; 268 const int coeff = coeff_ptr[rc]; 269 const int coeff_sign = (coeff >> 31); 270 int64_t tmp = 0; 271 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 272 273 if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) { 274 tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1), 275 INT32_MIN, INT32_MAX); 276 tmp = (tmp * quant_ptr[rc != 0]) >> 15; 277 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 278 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2; 279 } 280 281 if (tmp) 282 eob = i; 283 } 284 } 285 *eob_ptr = eob + 1; 286} 287#endif 288 289void vp9_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, 290 int skip_block, 291 const int16_t *zbin_ptr, const int16_t *round_ptr, 292 const int16_t *quant_ptr, const int16_t *quant_shift_ptr, 293 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 294 const int16_t *dequant_ptr, 295 int zbin_oq_value, uint16_t *eob_ptr, 296 const int16_t *scan, const int16_t *iscan) { 297 int i, non_zero_count = (int)n_coeffs, eob = -1; 298 const int zbins[2] = { zbin_ptr[0] + zbin_oq_value, 299 zbin_ptr[1] + zbin_oq_value }; 300 const int nzbins[2] = { zbins[0] * -1, 301 zbins[1] * -1 }; 302 (void)iscan; 303 304 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); 305 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); 306 307 if (!skip_block) { 308 // Pre-scan pass 309 for (i = (int)n_coeffs - 1; i >= 0; i--) { 310 const int rc = scan[i]; 311 const int coeff = coeff_ptr[rc]; 312 313 if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0]) 314 non_zero_count--; 315 else 316 break; 317 } 318 319 // Quantization pass: All coefficients with index >= zero_flag are 320 // skippable. Note: zero_flag can be zero. 321 for (i = 0; i < non_zero_count; i++) { 322 const int rc = scan[i]; 323 const int coeff = coeff_ptr[rc]; 324 const int coeff_sign = (coeff >> 31); 325 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 326 327 if (abs_coeff >= zbins[rc != 0]) { 328 int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); 329 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * 330 quant_shift_ptr[rc != 0]) >> 16; // quantization 331 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 332 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0]; 333 334 if (tmp) 335 eob = i; 336 } 337 } 338 } 339 *eob_ptr = eob + 1; 340} 341 342#if CONFIG_VP9_HIGHBITDEPTH 343void vp9_high_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, 344 int skip_block, const int16_t *zbin_ptr, 345 const int16_t *round_ptr, const int16_t *quant_ptr, 346 const int16_t *quant_shift_ptr, 347 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 348 const int16_t *dequant_ptr, int zbin_oq_value, 349 uint16_t *eob_ptr, const int16_t *scan, 350 const int16_t *iscan) { 351 int i, non_zero_count = (int)n_coeffs, eob = -1; 352 const int zbins[2] = { zbin_ptr[0] + zbin_oq_value, 353 zbin_ptr[1] + zbin_oq_value }; 354 const int nzbins[2] = { zbins[0] * -1, 355 zbins[1] * -1 }; 356 (void)iscan; 357 358 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); 359 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); 360 361 if (!skip_block) { 362 // Pre-scan pass 363 for (i = (int)n_coeffs - 1; i >= 0; i--) { 364 const int rc = scan[i]; 365 const int coeff = coeff_ptr[rc]; 366 367 if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0]) 368 non_zero_count--; 369 else 370 break; 371 } 372 373 // Quantization pass: All coefficients with index >= zero_flag are 374 // skippable. Note: zero_flag can be zero. 375 for (i = 0; i < non_zero_count; i++) { 376 const int rc = scan[i]; 377 const int coeff = coeff_ptr[rc]; 378 const int coeff_sign = (coeff >> 31); 379 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 380 381 if (abs_coeff >= zbins[rc != 0]) { 382 int64_t tmp = clamp(abs_coeff + round_ptr[rc != 0], 383 INT32_MIN, INT32_MAX); 384 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * 385 quant_shift_ptr[rc != 0]) >> 16; // quantization 386 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 387 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0]; 388 389 if (tmp) 390 eob = i; 391 } 392 } 393 } 394 *eob_ptr = eob + 1; 395} 396#endif 397 398void vp9_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, 399 int skip_block, 400 const int16_t *zbin_ptr, const int16_t *round_ptr, 401 const int16_t *quant_ptr, 402 const int16_t *quant_shift_ptr, 403 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, 404 const int16_t *dequant_ptr, 405 int zbin_oq_value, uint16_t *eob_ptr, 406 const int16_t *scan, const int16_t *iscan) { 407 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1), 408 ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1) }; 409 const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1}; 410 411 int idx = 0; 412 int idx_arr[1024]; 413 int i, eob = -1; 414 (void)iscan; 415 416 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); 417 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); 418 419 if (!skip_block) { 420 // Pre-scan pass 421 for (i = 0; i < n_coeffs; i++) { 422 const int rc = scan[i]; 423 const int coeff = coeff_ptr[rc]; 424 425 // If the coefficient is out of the base ZBIN range, keep it for 426 // quantization. 427 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0]) 428 idx_arr[idx++] = i; 429 } 430 431 // Quantization pass: only process the coefficients selected in 432 // pre-scan pass. Note: idx can be zero. 433 for (i = 0; i < idx; i++) { 434 const int rc = scan[idx_arr[i]]; 435 const int coeff = coeff_ptr[rc]; 436 const int coeff_sign = (coeff >> 31); 437 int tmp; 438 int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 439 abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); 440 abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX); 441 tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) * 442 quant_shift_ptr[rc != 0]) >> 15; 443 444 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 445 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2; 446 447 if (tmp) 448 eob = idx_arr[i]; 449 } 450 } 451 *eob_ptr = eob + 1; 452} 453 454#if CONFIG_VP9_HIGHBITDEPTH 455void vp9_high_quantize_b_32x32_c(const tran_low_t *coeff_ptr, 456 intptr_t n_coeffs, int skip_block, 457 const int16_t *zbin_ptr, 458 const int16_t *round_ptr, 459 const int16_t *quant_ptr, 460 const int16_t *quant_shift_ptr, 461 tran_low_t *qcoeff_ptr, 462 tran_low_t *dqcoeff_ptr, 463 const int16_t *dequant_ptr, 464 int zbin_oq_value, uint16_t *eob_ptr, 465 const int16_t *scan, const int16_t *iscan) { 466 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0] + zbin_oq_value, 1), 467 ROUND_POWER_OF_TWO(zbin_ptr[1] + zbin_oq_value, 1) }; 468 const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; 469 470 int idx = 0; 471 int idx_arr[1024]; 472 int i, eob = -1; 473 (void)iscan; 474 475 vpx_memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); 476 vpx_memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); 477 478 if (!skip_block) { 479 // Pre-scan pass 480 for (i = 0; i < n_coeffs; i++) { 481 const int rc = scan[i]; 482 const int coeff = coeff_ptr[rc]; 483 484 // If the coefficient is out of the base ZBIN range, keep it for 485 // quantization. 486 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0]) 487 idx_arr[idx++] = i; 488 } 489 490 // Quantization pass: only process the coefficients selected in 491 // pre-scan pass. Note: idx can be zero. 492 for (i = 0; i < idx; i++) { 493 const int rc = scan[idx_arr[i]]; 494 const int coeff = coeff_ptr[rc]; 495 const int coeff_sign = (coeff >> 31); 496 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; 497 int64_t tmp = clamp(abs_coeff + 498 ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1), 499 INT32_MIN, INT32_MAX); 500 tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * 501 quant_shift_ptr[rc != 0]) >> 15; 502 503 qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; 504 dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2; 505 506 if (tmp) 507 eob = idx_arr[i]; 508 } 509 } 510 *eob_ptr = eob + 1; 511} 512#endif 513 514void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block, 515 const int16_t *scan, const int16_t *iscan) { 516 MACROBLOCKD *const xd = &x->e_mbd; 517 struct macroblock_plane *p = &x->plane[plane]; 518 struct macroblockd_plane *pd = &xd->plane[plane]; 519 520#if CONFIG_VP9_HIGHBITDEPTH 521 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { 522 vp9_high_quantize_b(BLOCK_OFFSET(p->coeff, block), 523 16, x->skip_block, 524 p->zbin, p->round, p->quant, p->quant_shift, 525 BLOCK_OFFSET(p->qcoeff, block), 526 BLOCK_OFFSET(pd->dqcoeff, block), 527 pd->dequant, p->zbin_extra, &p->eobs[block], 528 scan, iscan); 529 return; 530 } 531#endif 532 vp9_quantize_b(BLOCK_OFFSET(p->coeff, block), 533 16, x->skip_block, 534 p->zbin, p->round, p->quant, p->quant_shift, 535 BLOCK_OFFSET(p->qcoeff, block), 536 BLOCK_OFFSET(pd->dqcoeff, block), 537 pd->dequant, p->zbin_extra, &p->eobs[block], scan, iscan); 538} 539 540static void invert_quant(int16_t *quant, int16_t *shift, int d) { 541 unsigned t; 542 int l; 543 t = d; 544 for (l = 0; t > 1; l++) 545 t >>= 1; 546 t = 1 + (1 << (16 + l)) / d; 547 *quant = (int16_t)(t - (1 << 16)); 548 *shift = 1 << (16 - l); 549} 550 551static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) { 552 const int quant = vp9_dc_quant(q, 0, bit_depth); 553#if CONFIG_VP9_HIGHBITDEPTH 554 switch (bit_depth) { 555 case VPX_BITS_8: 556 return q == 0 ? 64 : (quant < 148 ? 84 : 80); 557 case VPX_BITS_10: 558 return q == 0 ? 64 : (quant < 592 ? 84 : 80); 559 case VPX_BITS_12: 560 return q == 0 ? 64 : (quant < 2368 ? 84 : 80); 561 default: 562 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12"); 563 return -1; 564 } 565#else 566 (void) bit_depth; 567 return q == 0 ? 64 : (quant < 148 ? 84 : 80); 568#endif 569} 570 571void vp9_init_quantizer(VP9_COMP *cpi) { 572 VP9_COMMON *const cm = &cpi->common; 573 QUANTS *const quants = &cpi->quants; 574 int i, q, quant; 575 576 for (q = 0; q < QINDEX_RANGE; q++) { 577 const int qzbin_factor = get_qzbin_factor(q, cm->bit_depth); 578 const int qrounding_factor = q == 0 ? 64 : 48; 579 580 for (i = 0; i < 2; ++i) { 581 int qrounding_factor_fp = i == 0 ? 48 : 42; 582 if (q == 0) 583 qrounding_factor_fp = 64; 584 585 // y 586 quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth) 587 : vp9_ac_quant(q, 0, cm->bit_depth); 588 invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant); 589 quants->y_quant_fp[q][i] = (1 << 16) / quant; 590 quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7; 591 quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7); 592 quants->y_round[q][i] = (qrounding_factor * quant) >> 7; 593 cm->y_dequant[q][i] = quant; 594 595 // uv 596 quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth) 597 : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth); 598 invert_quant(&quants->uv_quant[q][i], 599 &quants->uv_quant_shift[q][i], quant); 600 quants->uv_quant_fp[q][i] = (1 << 16) / quant; 601 quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7; 602 quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7); 603 quants->uv_round[q][i] = (qrounding_factor * quant) >> 7; 604 cm->uv_dequant[q][i] = quant; 605 } 606 607 for (i = 2; i < 8; i++) { 608 quants->y_quant[q][i] = quants->y_quant[q][1]; 609 quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1]; 610 quants->y_round_fp[q][i] = quants->y_round_fp[q][1]; 611 quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1]; 612 quants->y_zbin[q][i] = quants->y_zbin[q][1]; 613 quants->y_round[q][i] = quants->y_round[q][1]; 614 cm->y_dequant[q][i] = cm->y_dequant[q][1]; 615 616 quants->uv_quant[q][i] = quants->uv_quant[q][1]; 617 quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1]; 618 quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1]; 619 quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1]; 620 quants->uv_zbin[q][i] = quants->uv_zbin[q][1]; 621 quants->uv_round[q][i] = quants->uv_round[q][1]; 622 cm->uv_dequant[q][i] = cm->uv_dequant[q][1]; 623 } 624 } 625} 626 627void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) { 628 const VP9_COMMON *const cm = &cpi->common; 629 MACROBLOCKD *const xd = &x->e_mbd; 630 QUANTS *const quants = &cpi->quants; 631 const int segment_id = xd->mi[0].src_mi->mbmi.segment_id; 632 const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex); 633 const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q); 634 const int zbin = cpi->zbin_mode_boost; 635 int i; 636 637 // Y 638 x->plane[0].quant = quants->y_quant[qindex]; 639 x->plane[0].quant_fp = quants->y_quant_fp[qindex]; 640 x->plane[0].round_fp = quants->y_round_fp[qindex]; 641 x->plane[0].quant_shift = quants->y_quant_shift[qindex]; 642 x->plane[0].zbin = quants->y_zbin[qindex]; 643 x->plane[0].round = quants->y_round[qindex]; 644 x->plane[0].quant_thred[0] = cm->y_dequant[qindex][0] * 645 cm->y_dequant[qindex][0]; 646 x->plane[0].quant_thred[1] = cm->y_dequant[qindex][1] * 647 cm->y_dequant[qindex][1]; 648 x->plane[0].zbin_extra = (int16_t)((cm->y_dequant[qindex][1] * zbin) >> 7); 649 xd->plane[0].dequant = cm->y_dequant[qindex]; 650 651 // UV 652 for (i = 1; i < 3; i++) { 653 x->plane[i].quant = quants->uv_quant[qindex]; 654 x->plane[i].quant_fp = quants->uv_quant_fp[qindex]; 655 x->plane[i].round_fp = quants->uv_round_fp[qindex]; 656 x->plane[i].quant_shift = quants->uv_quant_shift[qindex]; 657 x->plane[i].zbin = quants->uv_zbin[qindex]; 658 x->plane[i].round = quants->uv_round[qindex]; 659 x->plane[i].quant_thred[0] = cm->y_dequant[qindex][0] * 660 cm->y_dequant[qindex][0]; 661 x->plane[i].quant_thred[1] = cm->y_dequant[qindex][1] * 662 cm->y_dequant[qindex][1]; 663 x->plane[i].zbin_extra = (int16_t)((cm->uv_dequant[qindex][1] * zbin) >> 7); 664 xd->plane[i].dequant = cm->uv_dequant[qindex]; 665 } 666 667 x->skip_block = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP); 668 x->q_index = qindex; 669 670 x->errorperbit = rdmult >> 6; 671 x->errorperbit += (x->errorperbit == 0); 672 673 vp9_initialize_me_consts(cpi, x->q_index); 674} 675 676void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) { 677 const int qindex = x->q_index; 678 const int y_zbin_extra = (cpi->common.y_dequant[qindex][1] * 679 cpi->zbin_mode_boost) >> 7; 680 const int uv_zbin_extra = (cpi->common.uv_dequant[qindex][1] * 681 cpi->zbin_mode_boost) >> 7; 682 683 x->plane[0].zbin_extra = (int16_t)y_zbin_extra; 684 x->plane[1].zbin_extra = (int16_t)uv_zbin_extra; 685 x->plane[2].zbin_extra = (int16_t)uv_zbin_extra; 686} 687 688void vp9_frame_init_quantizer(VP9_COMP *cpi) { 689 cpi->zbin_mode_boost = 0; 690 vp9_init_plane_quantizers(cpi, &cpi->mb); 691} 692 693void vp9_set_quantizer(VP9_COMMON *cm, int q) { 694 // quantizer has to be reinitialized with vp9_init_quantizer() if any 695 // delta_q changes. 696 cm->base_qindex = q; 697 cm->y_dc_delta_q = 0; 698 cm->uv_dc_delta_q = 0; 699 cm->uv_ac_delta_q = 0; 700} 701 702// Table that converts 0-63 Q-range values passed in outside to the Qindex 703// range used internally. 704static const int quantizer_to_qindex[] = { 705 0, 4, 8, 12, 16, 20, 24, 28, 706 32, 36, 40, 44, 48, 52, 56, 60, 707 64, 68, 72, 76, 80, 84, 88, 92, 708 96, 100, 104, 108, 112, 116, 120, 124, 709 128, 132, 136, 140, 144, 148, 152, 156, 710 160, 164, 168, 172, 176, 180, 184, 188, 711 192, 196, 200, 204, 208, 212, 216, 220, 712 224, 228, 232, 236, 240, 244, 249, 255, 713}; 714 715int vp9_quantizer_to_qindex(int quantizer) { 716 return quantizer_to_qindex[quantizer]; 717} 718 719int vp9_qindex_to_quantizer(int qindex) { 720 int quantizer; 721 722 for (quantizer = 0; quantizer < 64; ++quantizer) 723 if (quantizer_to_qindex[quantizer] >= qindex) 724 return quantizer; 725 726 return 63; 727} 728