OLD | NEW |
(Empty) | |
| 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 #include "vpx_mem/vpx_mem.h" |
| 13 |
| 14 #include "vp9/encoder/vp9_onyx_int.h" |
| 15 #include "vp9/encoder/vp9_quantize.h" |
| 16 #include "vp9/common/vp9_quant_common.h" |
| 17 |
| 18 #include "vp9/common/vp9_seg_common.h" |
| 19 |
| 20 #ifdef ENC_DEBUG |
| 21 extern int enc_debug; |
| 22 #endif |
| 23 |
| 24 void vp9_ht_quantize_b_4x4(BLOCK *b, BLOCKD *d, TX_TYPE tx_type) { |
| 25 int i, rc, eob; |
| 26 int zbin; |
| 27 int x, y, z, sz; |
| 28 short *zbin_boost_ptr = b->zrun_zbin_boost; |
| 29 short *coeff_ptr = b->coeff; |
| 30 short *zbin_ptr = b->zbin; |
| 31 short *round_ptr = b->round; |
| 32 short *quant_ptr = b->quant; |
| 33 unsigned char *quant_shift_ptr = b->quant_shift; |
| 34 short *qcoeff_ptr = d->qcoeff; |
| 35 short *dqcoeff_ptr = d->dqcoeff; |
| 36 short *dequant_ptr = d->dequant; |
| 37 short zbin_oq_value = b->zbin_extra; |
| 38 |
| 39 int const *pt_scan ; |
| 40 |
| 41 switch (tx_type) { |
| 42 case ADST_DCT : |
| 43 pt_scan = vp9_row_scan; |
| 44 break; |
| 45 |
| 46 case DCT_ADST : |
| 47 pt_scan = vp9_col_scan; |
| 48 break; |
| 49 |
| 50 default : |
| 51 pt_scan = vp9_default_zig_zag1d; |
| 52 break; |
| 53 } |
| 54 |
| 55 vpx_memset(qcoeff_ptr, 0, 32); |
| 56 vpx_memset(dqcoeff_ptr, 0, 32); |
| 57 |
| 58 eob = -1; |
| 59 |
| 60 for (i = 0; i < b->eob_max_offset; i++) { |
| 61 rc = pt_scan[i]; |
| 62 z = coeff_ptr[rc]; |
| 63 |
| 64 zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value; |
| 65 zbin_boost_ptr ++; |
| 66 |
| 67 sz = (z >> 31); // sign of z |
| 68 x = (z ^ sz) - sz; // x = abs(z) |
| 69 |
| 70 if (x >= zbin) { |
| 71 x += round_ptr[rc]; |
| 72 y = (((x * quant_ptr[rc]) >> 16) + x) |
| 73 >> quant_shift_ptr[rc]; // quantize (x) |
| 74 x = (y ^ sz) - sz; // get the sign back |
| 75 qcoeff_ptr[rc] = x; // write to destination |
| 76 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value |
| 77 |
| 78 if (y) { |
| 79 eob = i; // last nonzero coeffs |
| 80 zbin_boost_ptr = b->zrun_zbin_boost; // reset zero runlength |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 d->eob = eob + 1; |
| 86 } |
| 87 |
| 88 void vp9_regular_quantize_b_4x4(BLOCK *b, BLOCKD *d) { |
| 89 int i, rc, eob; |
| 90 int zbin; |
| 91 int x, y, z, sz; |
| 92 short *zbin_boost_ptr = b->zrun_zbin_boost; |
| 93 short *coeff_ptr = b->coeff; |
| 94 short *zbin_ptr = b->zbin; |
| 95 short *round_ptr = b->round; |
| 96 short *quant_ptr = b->quant; |
| 97 unsigned char *quant_shift_ptr = b->quant_shift; |
| 98 short *qcoeff_ptr = d->qcoeff; |
| 99 short *dqcoeff_ptr = d->dqcoeff; |
| 100 short *dequant_ptr = d->dequant; |
| 101 short zbin_oq_value = b->zbin_extra; |
| 102 |
| 103 vpx_memset(qcoeff_ptr, 0, 32); |
| 104 vpx_memset(dqcoeff_ptr, 0, 32); |
| 105 |
| 106 eob = -1; |
| 107 |
| 108 for (i = 0; i < b->eob_max_offset; i++) { |
| 109 rc = vp9_default_zig_zag1d[i]; |
| 110 z = coeff_ptr[rc]; |
| 111 |
| 112 zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value; |
| 113 zbin_boost_ptr ++; |
| 114 |
| 115 sz = (z >> 31); // sign of z |
| 116 x = (z ^ sz) - sz; // x = abs(z) |
| 117 |
| 118 if (x >= zbin) { |
| 119 x += round_ptr[rc]; |
| 120 |
| 121 y = (((x * quant_ptr[rc]) >> 16) + x) |
| 122 >> quant_shift_ptr[rc]; // quantize (x) |
| 123 x = (y ^ sz) - sz; // get the sign back |
| 124 qcoeff_ptr[rc] = x; // write to destination |
| 125 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value |
| 126 |
| 127 if (y) { |
| 128 eob = i; // last nonzero coeffs |
| 129 zbin_boost_ptr = b->zrun_zbin_boost; // reset zero runlength |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 d->eob = eob + 1; |
| 135 } |
| 136 |
| 137 void vp9_quantize_mby_4x4_c(MACROBLOCK *x) { |
| 138 int i; |
| 139 int has_2nd_order = get_2nd_order_usage(&x->e_mbd); |
| 140 |
| 141 for (i = 0; i < 16; i++) { |
| 142 TX_TYPE tx_type = get_tx_type_4x4(&x->e_mbd, &x->e_mbd.block[i]); |
| 143 if (tx_type != DCT_DCT) { |
| 144 assert(has_2nd_order == 0); |
| 145 vp9_ht_quantize_b_4x4(&x->block[i], &x->e_mbd.block[i], tx_type); |
| 146 } else { |
| 147 x->quantize_b_4x4(&x->block[i], &x->e_mbd.block[i]); |
| 148 } |
| 149 } |
| 150 if (has_2nd_order) { |
| 151 x->quantize_b_4x4(&x->block[24], &x->e_mbd.block[24]); |
| 152 } else { |
| 153 vpx_memset(x->e_mbd.block[24].qcoeff, 0, |
| 154 16 * sizeof(x->e_mbd.block[24].qcoeff[0])); |
| 155 vpx_memset(x->e_mbd.block[24].dqcoeff, 0, |
| 156 16 * sizeof(x->e_mbd.block[24].dqcoeff[0])); |
| 157 x->e_mbd.block[24].eob = 0; |
| 158 } |
| 159 } |
| 160 |
| 161 void vp9_quantize_mbuv_4x4_c(MACROBLOCK *x) { |
| 162 int i; |
| 163 |
| 164 for (i = 16; i < 24; i++) |
| 165 x->quantize_b_4x4(&x->block[i], &x->e_mbd.block[i]); |
| 166 } |
| 167 |
| 168 void vp9_quantize_mb_4x4_c(MACROBLOCK *x) { |
| 169 vp9_quantize_mby_4x4_c(x); |
| 170 vp9_quantize_mbuv_4x4_c(x); |
| 171 } |
| 172 |
| 173 void vp9_regular_quantize_b_2x2(BLOCK *b, BLOCKD *d) { |
| 174 int i, rc, eob; |
| 175 int zbin; |
| 176 int x, y, z, sz; |
| 177 short *zbin_boost_ptr = b->zrun_zbin_boost; |
| 178 int zbin_zrun_index = 0; |
| 179 short *coeff_ptr = b->coeff; |
| 180 short *zbin_ptr = b->zbin; |
| 181 short *round_ptr = b->round; |
| 182 short *quant_ptr = b->quant; |
| 183 unsigned char *quant_shift_ptr = b->quant_shift; |
| 184 short *qcoeff_ptr = d->qcoeff; |
| 185 short *dqcoeff_ptr = d->dqcoeff; |
| 186 short *dequant_ptr = d->dequant; |
| 187 short zbin_oq_value = b->zbin_extra; |
| 188 // double q2nd = 4; |
| 189 vpx_memset(qcoeff_ptr, 0, 32); |
| 190 vpx_memset(dqcoeff_ptr, 0, 32); |
| 191 |
| 192 eob = -1; |
| 193 |
| 194 for (i = 0; i < b->eob_max_offset_8x8; i++) { |
| 195 rc = vp9_default_zig_zag1d[i]; |
| 196 z = coeff_ptr[rc]; |
| 197 |
| 198 zbin_boost_ptr = &b->zrun_zbin_boost[zbin_zrun_index]; |
| 199 zbin_zrun_index += 4; |
| 200 zbin = (zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value); |
| 201 |
| 202 sz = (z >> 31); // sign of z |
| 203 x = (z ^ sz) - sz; // x = abs(z) |
| 204 |
| 205 if (x >= zbin) { |
| 206 x += (round_ptr[rc]); |
| 207 y = ((int)((int)(x * quant_ptr[rc]) >> 16) + x) |
| 208 >> quant_shift_ptr[rc]; // quantize (x) |
| 209 x = (y ^ sz) - sz; // get the sign back |
| 210 qcoeff_ptr[rc] = x; // write to destination |
| 211 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value |
| 212 |
| 213 if (y) { |
| 214 eob = i; // last nonzero coeffs |
| 215 zbin_zrun_index = 0; |
| 216 } |
| 217 } |
| 218 } |
| 219 |
| 220 d->eob = eob + 1; |
| 221 } |
| 222 |
| 223 void vp9_regular_quantize_b_8x8(BLOCK *b, BLOCKD *d) { |
| 224 int i, rc, eob; |
| 225 int zbin; |
| 226 int x, y, z, sz; |
| 227 short *zbin_boost_ptr = b->zrun_zbin_boost_8x8; |
| 228 short *coeff_ptr = b->coeff; |
| 229 short *zbin_ptr = b->zbin_8x8; |
| 230 short *round_ptr = b->round; |
| 231 short *quant_ptr = b->quant; |
| 232 unsigned char *quant_shift_ptr = b->quant_shift; |
| 233 short *qcoeff_ptr = d->qcoeff; |
| 234 short *dqcoeff_ptr = d->dqcoeff; |
| 235 short *dequant_ptr = d->dequant; |
| 236 short zbin_oq_value = b->zbin_extra; |
| 237 |
| 238 vpx_memset(qcoeff_ptr, 0, 64 * sizeof(short)); |
| 239 vpx_memset(dqcoeff_ptr, 0, 64 * sizeof(short)); |
| 240 |
| 241 eob = -1; |
| 242 |
| 243 for (i = 0; i < b->eob_max_offset_8x8; i++) { |
| 244 rc = vp9_default_zig_zag1d_8x8[i]; |
| 245 z = coeff_ptr[rc]; |
| 246 |
| 247 zbin = (zbin_ptr[rc != 0] + *zbin_boost_ptr + zbin_oq_value); |
| 248 zbin_boost_ptr++; |
| 249 |
| 250 sz = (z >> 31); // sign of z |
| 251 x = (z ^ sz) - sz; // x = abs(z) |
| 252 |
| 253 if (x >= zbin) { |
| 254 x += (round_ptr[rc != 0]); |
| 255 y = ((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x)) |
| 256 >> quant_shift_ptr[rc != 0]; // quantize (x) |
| 257 x = (y ^ sz) - sz; // get the sign back |
| 258 qcoeff_ptr[rc] = x; // write to destination |
| 259 dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0]; // dequantized value |
| 260 |
| 261 if (y) { |
| 262 eob = i; // last nonzero coeffs |
| 263 zbin_boost_ptr = b->zrun_zbin_boost_8x8; |
| 264 } |
| 265 } |
| 266 } |
| 267 |
| 268 d->eob = eob + 1; |
| 269 } |
| 270 |
| 271 void vp9_quantize_mby_8x8(MACROBLOCK *x) { |
| 272 int i; |
| 273 int has_2nd_order = get_2nd_order_usage(&x->e_mbd); |
| 274 |
| 275 for (i = 0; i < 16; i ++) { |
| 276 x->e_mbd.block[i].eob = 0; |
| 277 } |
| 278 x->e_mbd.block[24].eob = 0; |
| 279 for (i = 0; i < 16; i += 4) { |
| 280 int ib = (i & 8) + ((i & 4) >> 1); |
| 281 TX_TYPE tx_type = get_tx_type_8x8(&x->e_mbd, &x->e_mbd.block[ib]); |
| 282 if (tx_type != DCT_DCT) |
| 283 assert(has_2nd_order == 0); |
| 284 x->quantize_b_8x8(&x->block[i], &x->e_mbd.block[i]); |
| 285 } |
| 286 |
| 287 if (has_2nd_order) { |
| 288 x->quantize_b_2x2(&x->block[24], &x->e_mbd.block[24]); |
| 289 } else { |
| 290 vpx_memset(x->e_mbd.block[24].qcoeff, 0, |
| 291 16 * sizeof(x->e_mbd.block[24].qcoeff[0])); |
| 292 vpx_memset(x->e_mbd.block[24].dqcoeff, 0, |
| 293 16 * sizeof(x->e_mbd.block[24].dqcoeff[0])); |
| 294 x->e_mbd.block[24].eob = 0; |
| 295 } |
| 296 } |
| 297 |
| 298 void vp9_quantize_mbuv_8x8(MACROBLOCK *x) { |
| 299 int i; |
| 300 |
| 301 for (i = 16; i < 24; i ++) |
| 302 x->e_mbd.block[i].eob = 0; |
| 303 for (i = 16; i < 24; i += 4) |
| 304 x->quantize_b_8x8(&x->block[i], &x->e_mbd.block[i]); |
| 305 } |
| 306 |
| 307 void vp9_quantize_mb_8x8(MACROBLOCK *x) { |
| 308 vp9_quantize_mby_8x8(x); |
| 309 vp9_quantize_mbuv_8x8(x); |
| 310 } |
| 311 |
| 312 void vp9_quantize_mby_16x16(MACROBLOCK *x) { |
| 313 int i; |
| 314 |
| 315 for (i = 0; i < 16; i++) |
| 316 x->e_mbd.block[i].eob = 0; |
| 317 x->e_mbd.block[24].eob = 0; |
| 318 x->quantize_b_16x16(&x->block[0], &x->e_mbd.block[0]); |
| 319 } |
| 320 |
| 321 void vp9_quantize_mb_16x16(MACROBLOCK *x) { |
| 322 vp9_quantize_mby_16x16(x); |
| 323 vp9_quantize_mbuv_8x8(x); |
| 324 } |
| 325 |
| 326 void vp9_regular_quantize_b_16x16(BLOCK *b, BLOCKD *d) { |
| 327 int i, rc, eob; |
| 328 int zbin; |
| 329 int x, y, z, sz; |
| 330 short *zbin_boost_ptr = b->zrun_zbin_boost_16x16; |
| 331 short *coeff_ptr = b->coeff; |
| 332 short *zbin_ptr = b->zbin_16x16; |
| 333 short *round_ptr = b->round; |
| 334 short *quant_ptr = b->quant; |
| 335 unsigned char *quant_shift_ptr = b->quant_shift; |
| 336 short *qcoeff_ptr = d->qcoeff; |
| 337 short *dqcoeff_ptr = d->dqcoeff; |
| 338 short *dequant_ptr = d->dequant; |
| 339 short zbin_oq_value = b->zbin_extra; |
| 340 |
| 341 vpx_memset(qcoeff_ptr, 0, 256*sizeof(short)); |
| 342 vpx_memset(dqcoeff_ptr, 0, 256*sizeof(short)); |
| 343 |
| 344 eob = -1; |
| 345 for (i = 0; i < b->eob_max_offset_16x16; i++) { |
| 346 rc = vp9_default_zig_zag1d_16x16[i]; |
| 347 z = coeff_ptr[rc]; |
| 348 |
| 349 zbin = (zbin_ptr[rc!=0] + *zbin_boost_ptr + zbin_oq_value); |
| 350 zbin_boost_ptr ++; |
| 351 |
| 352 sz = (z >> 31); // sign of z |
| 353 x = (z ^ sz) - sz; // x = abs(z) |
| 354 |
| 355 if (x >= zbin) { |
| 356 x += (round_ptr[rc!=0]); |
| 357 y = ((int)(((int)(x * quant_ptr[rc!=0]) >> 16) + x)) |
| 358 >> quant_shift_ptr[rc!=0]; // quantize (x) |
| 359 x = (y ^ sz) - sz; // get the sign back |
| 360 qcoeff_ptr[rc] = x; // write to destination |
| 361 dqcoeff_ptr[rc] = x * dequant_ptr[rc!=0]; // dequantized value |
| 362 |
| 363 if (y) { |
| 364 eob = i; // last nonzero coeffs |
| 365 zbin_boost_ptr = b->zrun_zbin_boost_16x16; |
| 366 } |
| 367 } |
| 368 } |
| 369 |
| 370 d->eob = eob + 1; |
| 371 } |
| 372 |
| 373 /* quantize_b_pair function pointer in MACROBLOCK structure is set to one of |
| 374 * these two C functions if corresponding optimized routine is not available. |
| 375 * NEON optimized version implements currently the fast quantization for pair |
| 376 * of blocks. */ |
| 377 void vp9_regular_quantize_b_4x4_pair(BLOCK *b1, BLOCK *b2, |
| 378 BLOCKD *d1, BLOCKD *d2) { |
| 379 vp9_regular_quantize_b_4x4(b1, d1); |
| 380 vp9_regular_quantize_b_4x4(b2, d2); |
| 381 } |
| 382 |
| 383 static void invert_quant(short *quant, |
| 384 unsigned char *shift, short d) { |
| 385 unsigned t; |
| 386 int l; |
| 387 t = d; |
| 388 for (l = 0; t > 1; l++) |
| 389 t >>= 1; |
| 390 t = 1 + (1 << (16 + l)) / d; |
| 391 *quant = (short)(t - (1 << 16)); |
| 392 *shift = l; |
| 393 } |
| 394 |
| 395 void vp9_init_quantizer(VP9_COMP *cpi) { |
| 396 int i; |
| 397 int quant_val; |
| 398 int Q; |
| 399 static const int zbin_boost[16] = { 0, 0, 8, 10, 12, 14, 16, 20, |
| 400 24, 28, 32, 36, 40, 44, 44, 44 |
| 401 }; |
| 402 |
| 403 static const int zbin_boost_8x8[64] = { 0, 0, 0, 8, 8, 8, 10, 12, |
| 404 14, 16, 18, 20, 22, 24, 26, 28, |
| 405 30, 32, 34, 36, 38, 40, 42, 44, |
| 406 46, 48, 48, 48, 48, 48, 48, 48, |
| 407 48, 48, 48, 48, 48, 48, 48, 48, |
| 408 48, 48, 48, 48, 48, 48, 48, 48, |
| 409 48, 48, 48, 48, 48, 48, 48, 48, |
| 410 48, 48, 48, 48, 48, 48, 48, 48 |
| 411 }; |
| 412 static const int zbin_boost_16x16[256] = { |
| 413 0, 0, 0, 8, 8, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, |
| 414 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 48, 48, 48, 48, 48, 48, |
| 415 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 416 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 417 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 418 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 419 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 420 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 421 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 422 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 423 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 424 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 425 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 426 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 427 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 428 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
| 429 }; |
| 430 int qrounding_factor = 48; |
| 431 |
| 432 |
| 433 for (Q = 0; Q < QINDEX_RANGE; Q++) { |
| 434 int qzbin_factor = (vp9_dc_quant(Q, 0) < 148) ? 84 : 80; |
| 435 |
| 436 #if CONFIG_LOSSLESS |
| 437 if (cpi->oxcf.lossless) { |
| 438 if (Q == 0) { |
| 439 qzbin_factor = 64; |
| 440 qrounding_factor = 64; |
| 441 } |
| 442 } |
| 443 #endif |
| 444 |
| 445 // dc values |
| 446 quant_val = vp9_dc_quant(Q, cpi->common.y1dc_delta_q); |
| 447 invert_quant(cpi->Y1quant[Q] + 0, |
| 448 cpi->Y1quant_shift[Q] + 0, quant_val); |
| 449 cpi->Y1zbin[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 450 cpi->Y1zbin_8x8[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 451 cpi->Y1zbin_16x16[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 452 cpi->Y1round[Q][0] = (qrounding_factor * quant_val) >> 7; |
| 453 cpi->common.Y1dequant[Q][0] = quant_val; |
| 454 cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7; |
| 455 cpi->zrun_zbin_boost_y1_8x8[Q][0] = |
| 456 ((quant_val * zbin_boost_8x8[0]) + 64) >> 7; |
| 457 cpi->zrun_zbin_boost_y1_16x16[Q][0] = ((quant_val * zbin_boost_16x16[0]) + 6
4) >> 7; |
| 458 |
| 459 |
| 460 quant_val = vp9_dc2quant(Q, cpi->common.y2dc_delta_q); |
| 461 invert_quant(cpi->Y2quant[Q] + 0, |
| 462 cpi->Y2quant_shift[Q] + 0, quant_val); |
| 463 cpi->Y2zbin[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 464 cpi->Y2zbin_8x8[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 465 cpi->Y2zbin_16x16[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 466 cpi->Y2round[Q][0] = (qrounding_factor * quant_val) >> 7; |
| 467 cpi->common.Y2dequant[Q][0] = quant_val; |
| 468 cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7; |
| 469 cpi->zrun_zbin_boost_y2_8x8[Q][0] = |
| 470 ((quant_val * zbin_boost_8x8[0]) + 64) >> 7; |
| 471 cpi->zrun_zbin_boost_y2_16x16[Q][0] = ((quant_val * zbin_boost_16x16[0]) + 6
4) >> 7; |
| 472 |
| 473 quant_val = vp9_dc_uv_quant(Q, cpi->common.uvdc_delta_q); |
| 474 invert_quant(cpi->UVquant[Q] + 0, |
| 475 cpi->UVquant_shift[Q] + 0, quant_val); |
| 476 cpi->UVzbin[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 477 cpi->UVzbin_8x8[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 478 cpi->UVzbin_16x16[Q][0] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 479 cpi->UVround[Q][0] = (qrounding_factor * quant_val) >> 7; |
| 480 cpi->common.UVdequant[Q][0] = quant_val; |
| 481 cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7; |
| 482 cpi->zrun_zbin_boost_uv_8x8[Q][0] = |
| 483 ((quant_val * zbin_boost_8x8[0]) + 64) >> 7; |
| 484 cpi->zrun_zbin_boost_uv_16x16[Q][0] = ((quant_val * zbin_boost_16x16[0]) + 6
4) >> 7; |
| 485 |
| 486 // all the 4x4 ac values =; |
| 487 for (i = 1; i < 16; i++) { |
| 488 int rc = vp9_default_zig_zag1d[i]; |
| 489 |
| 490 quant_val = vp9_ac_yquant(Q); |
| 491 invert_quant(cpi->Y1quant[Q] + rc, |
| 492 cpi->Y1quant_shift[Q] + rc, quant_val); |
| 493 cpi->Y1zbin[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 494 cpi->Y1round[Q][rc] = (qrounding_factor * quant_val) >> 7; |
| 495 cpi->common.Y1dequant[Q][rc] = quant_val; |
| 496 cpi->zrun_zbin_boost_y1[Q][i] = |
| 497 ((quant_val * zbin_boost[i]) + 64) >> 7; |
| 498 |
| 499 quant_val = vp9_ac2quant(Q, cpi->common.y2ac_delta_q); |
| 500 invert_quant(cpi->Y2quant[Q] + rc, |
| 501 cpi->Y2quant_shift[Q] + rc, quant_val); |
| 502 cpi->Y2zbin[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 503 cpi->Y2round[Q][rc] = (qrounding_factor * quant_val) >> 7; |
| 504 cpi->common.Y2dequant[Q][rc] = quant_val; |
| 505 cpi->zrun_zbin_boost_y2[Q][i] = |
| 506 ((quant_val * zbin_boost[i]) + 64) >> 7; |
| 507 |
| 508 quant_val = vp9_ac_uv_quant(Q, cpi->common.uvac_delta_q); |
| 509 invert_quant(cpi->UVquant[Q] + rc, |
| 510 cpi->UVquant_shift[Q] + rc, quant_val); |
| 511 cpi->UVzbin[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 512 cpi->UVround[Q][rc] = (qrounding_factor * quant_val) >> 7; |
| 513 cpi->common.UVdequant[Q][rc] = quant_val; |
| 514 cpi->zrun_zbin_boost_uv[Q][i] = |
| 515 ((quant_val * zbin_boost[i]) + 64) >> 7; |
| 516 } |
| 517 |
| 518 // 8x8 structures... only zbin seperated out for now |
| 519 // This needs cleaning up for 8x8 especially if we are to add |
| 520 // support for non flat Q matices |
| 521 for (i = 1; i < 64; i++) { |
| 522 int rc = vp9_default_zig_zag1d_8x8[i]; |
| 523 |
| 524 quant_val = vp9_ac_yquant(Q); |
| 525 cpi->Y1zbin_8x8[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 526 cpi->zrun_zbin_boost_y1_8x8[Q][i] = |
| 527 ((quant_val * zbin_boost_8x8[i]) + 64) >> 7; |
| 528 |
| 529 quant_val = vp9_ac2quant(Q, cpi->common.y2ac_delta_q); |
| 530 cpi->Y2zbin_8x8[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 531 cpi->zrun_zbin_boost_y2_8x8[Q][i] = |
| 532 ((quant_val * zbin_boost_8x8[i]) + 64) >> 7; |
| 533 |
| 534 quant_val = vp9_ac_uv_quant(Q, cpi->common.uvac_delta_q); |
| 535 cpi->UVzbin_8x8[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 536 cpi->zrun_zbin_boost_uv_8x8[Q][i] = |
| 537 ((quant_val * zbin_boost_8x8[i]) + 64) >> 7; |
| 538 } |
| 539 |
| 540 // 16x16 structures. Same comment above applies. |
| 541 for (i = 1; i < 256; i++) { |
| 542 int rc = vp9_default_zig_zag1d_16x16[i]; |
| 543 |
| 544 quant_val = vp9_ac_yquant(Q); |
| 545 cpi->Y1zbin_16x16[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 546 cpi->zrun_zbin_boost_y1_16x16[Q][i] = ((quant_val * zbin_boost_16x16[i]) +
64) >> 7; |
| 547 |
| 548 quant_val = vp9_ac2quant(Q, cpi->common.y2ac_delta_q); |
| 549 cpi->Y2zbin_16x16[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 550 cpi->zrun_zbin_boost_y2_16x16[Q][i] = ((quant_val * zbin_boost_16x16[i]) +
64) >> 7; |
| 551 |
| 552 quant_val = vp9_ac_uv_quant(Q, cpi->common.uvac_delta_q); |
| 553 cpi->UVzbin_16x16[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7; |
| 554 cpi->zrun_zbin_boost_uv_16x16[Q][i] = ((quant_val * zbin_boost_16x16[i]) +
64) >> 7; |
| 555 } |
| 556 } |
| 557 } |
| 558 |
| 559 void vp9_mb_init_quantizer(VP9_COMP *cpi, MACROBLOCK *x) { |
| 560 int i; |
| 561 int QIndex; |
| 562 MACROBLOCKD *xd = &x->e_mbd; |
| 563 int zbin_extra; |
| 564 int segment_id = xd->mode_info_context->mbmi.segment_id; |
| 565 |
| 566 // Select the baseline MB Q index allowing for any segment level change. |
| 567 if (vp9_segfeature_active(xd, segment_id, SEG_LVL_ALT_Q)) { |
| 568 // Abs Value |
| 569 if (xd->mb_segment_abs_delta == SEGMENT_ABSDATA) |
| 570 QIndex = vp9_get_segdata(xd, segment_id, SEG_LVL_ALT_Q); |
| 571 |
| 572 // Delta Value |
| 573 else { |
| 574 QIndex = cpi->common.base_qindex + |
| 575 vp9_get_segdata(xd, segment_id, SEG_LVL_ALT_Q); |
| 576 |
| 577 // Clamp to valid range |
| 578 QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0; |
| 579 } |
| 580 } else |
| 581 QIndex = cpi->common.base_qindex; |
| 582 |
| 583 // Y |
| 584 zbin_extra = (cpi->common.Y1dequant[QIndex][1] * |
| 585 (cpi->zbin_over_quant + |
| 586 cpi->zbin_mode_boost + |
| 587 x->act_zbin_adj)) >> 7; |
| 588 |
| 589 for (i = 0; i < 16; i++) { |
| 590 x->block[i].quant = cpi->Y1quant[QIndex]; |
| 591 x->block[i].quant_shift = cpi->Y1quant_shift[QIndex]; |
| 592 x->block[i].zbin = cpi->Y1zbin[QIndex]; |
| 593 x->block[i].zbin_8x8 = cpi->Y1zbin_8x8[QIndex]; |
| 594 x->block[i].zbin_16x16 = cpi->Y1zbin_16x16[QIndex]; |
| 595 x->block[i].round = cpi->Y1round[QIndex]; |
| 596 x->e_mbd.block[i].dequant = cpi->common.Y1dequant[QIndex]; |
| 597 x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex]; |
| 598 x->block[i].zrun_zbin_boost_8x8 = cpi->zrun_zbin_boost_y1_8x8[QIndex]; |
| 599 x->block[i].zrun_zbin_boost_16x16 = cpi->zrun_zbin_boost_y1_16x16[QIndex]; |
| 600 x->block[i].zbin_extra = (short)zbin_extra; |
| 601 |
| 602 // Segment max eob offset feature. |
| 603 if (vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB)) { |
| 604 x->block[i].eob_max_offset = |
| 605 vp9_get_segdata(xd, segment_id, SEG_LVL_EOB); |
| 606 x->block[i].eob_max_offset_8x8 = |
| 607 vp9_get_segdata(xd, segment_id, SEG_LVL_EOB); |
| 608 x->block[i].eob_max_offset_16x16 = |
| 609 vp9_get_segdata(xd, segment_id, SEG_LVL_EOB); |
| 610 } else { |
| 611 x->block[i].eob_max_offset = 16; |
| 612 x->block[i].eob_max_offset_8x8 = 64; |
| 613 x->block[i].eob_max_offset_16x16 = 256; |
| 614 } |
| 615 } |
| 616 |
| 617 // UV |
| 618 zbin_extra = (cpi->common.UVdequant[QIndex][1] * |
| 619 (cpi->zbin_over_quant + |
| 620 cpi->zbin_mode_boost + |
| 621 x->act_zbin_adj)) >> 7; |
| 622 |
| 623 for (i = 16; i < 24; i++) { |
| 624 x->block[i].quant = cpi->UVquant[QIndex]; |
| 625 x->block[i].quant_shift = cpi->UVquant_shift[QIndex]; |
| 626 x->block[i].zbin = cpi->UVzbin[QIndex]; |
| 627 x->block[i].zbin_8x8 = cpi->UVzbin_8x8[QIndex]; |
| 628 x->block[i].zbin_16x16 = cpi->UVzbin_16x16[QIndex]; |
| 629 x->block[i].round = cpi->UVround[QIndex]; |
| 630 x->e_mbd.block[i].dequant = cpi->common.UVdequant[QIndex]; |
| 631 x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex]; |
| 632 x->block[i].zrun_zbin_boost_8x8 = cpi->zrun_zbin_boost_uv_8x8[QIndex]; |
| 633 x->block[i].zrun_zbin_boost_16x16 = cpi->zrun_zbin_boost_uv_16x16[QIndex]; |
| 634 |
| 635 x->block[i].zbin_extra = (short)zbin_extra; |
| 636 |
| 637 // Segment max eob offset feature. |
| 638 if (vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB)) { |
| 639 x->block[i].eob_max_offset = |
| 640 vp9_get_segdata(xd, segment_id, SEG_LVL_EOB); |
| 641 x->block[i].eob_max_offset_8x8 = |
| 642 vp9_get_segdata(xd, segment_id, SEG_LVL_EOB); |
| 643 } else { |
| 644 x->block[i].eob_max_offset = 16; |
| 645 x->block[i].eob_max_offset_8x8 = 64; |
| 646 } |
| 647 } |
| 648 |
| 649 // Y2 |
| 650 zbin_extra = (cpi->common.Y2dequant[QIndex][1] * |
| 651 ((cpi->zbin_over_quant / 2) + |
| 652 cpi->zbin_mode_boost + |
| 653 x->act_zbin_adj)) >> 7; |
| 654 |
| 655 x->block[24].quant = cpi->Y2quant[QIndex]; |
| 656 x->block[24].quant_shift = cpi->Y2quant_shift[QIndex]; |
| 657 x->block[24].zbin = cpi->Y2zbin[QIndex]; |
| 658 x->block[24].zbin_8x8 = cpi->Y2zbin_8x8[QIndex]; |
| 659 x->block[24].zbin_16x16 = cpi->Y2zbin_16x16[QIndex]; |
| 660 x->block[24].round = cpi->Y2round[QIndex]; |
| 661 x->e_mbd.block[24].dequant = cpi->common.Y2dequant[QIndex]; |
| 662 x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex]; |
| 663 x->block[24].zrun_zbin_boost_8x8 = cpi->zrun_zbin_boost_y2_8x8[QIndex]; |
| 664 x->block[24].zrun_zbin_boost_16x16 = cpi->zrun_zbin_boost_y2_16x16[QIndex]; |
| 665 x->block[24].zbin_extra = (short)zbin_extra; |
| 666 |
| 667 // TBD perhaps not use for Y2 |
| 668 // Segment max eob offset feature. |
| 669 if (vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB)) { |
| 670 x->block[24].eob_max_offset = |
| 671 vp9_get_segdata(xd, segment_id, SEG_LVL_EOB); |
| 672 x->block[24].eob_max_offset_8x8 = |
| 673 vp9_get_segdata(xd, segment_id, SEG_LVL_EOB); |
| 674 } else { |
| 675 x->block[24].eob_max_offset = 16; |
| 676 x->block[24].eob_max_offset_8x8 = 4; |
| 677 } |
| 678 |
| 679 /* save this macroblock QIndex for vp9_update_zbin_extra() */ |
| 680 x->e_mbd.q_index = QIndex; |
| 681 } |
| 682 |
| 683 void vp9_update_zbin_extra(VP9_COMP *cpi, MACROBLOCK *x) { |
| 684 int i; |
| 685 int QIndex = x->e_mbd.q_index; |
| 686 int zbin_extra; |
| 687 |
| 688 // Y |
| 689 zbin_extra = (cpi->common.Y1dequant[QIndex][1] * |
| 690 (cpi->zbin_over_quant + |
| 691 cpi->zbin_mode_boost + |
| 692 x->act_zbin_adj)) >> 7; |
| 693 for (i = 0; i < 16; i++) { |
| 694 x->block[i].zbin_extra = (short)zbin_extra; |
| 695 } |
| 696 |
| 697 // UV |
| 698 zbin_extra = (cpi->common.UVdequant[QIndex][1] * |
| 699 (cpi->zbin_over_quant + |
| 700 cpi->zbin_mode_boost + |
| 701 x->act_zbin_adj)) >> 7; |
| 702 |
| 703 for (i = 16; i < 24; i++) { |
| 704 x->block[i].zbin_extra = (short)zbin_extra; |
| 705 } |
| 706 |
| 707 // Y2 |
| 708 zbin_extra = (cpi->common.Y2dequant[QIndex][1] * |
| 709 ((cpi->zbin_over_quant / 2) + |
| 710 cpi->zbin_mode_boost + |
| 711 x->act_zbin_adj)) >> 7; |
| 712 |
| 713 x->block[24].zbin_extra = (short)zbin_extra; |
| 714 } |
| 715 |
| 716 void vp9_frame_init_quantizer(VP9_COMP *cpi) { |
| 717 // Clear Zbin mode boost for default case |
| 718 cpi->zbin_mode_boost = 0; |
| 719 |
| 720 // MB level quantizer setup |
| 721 vp9_mb_init_quantizer(cpi, &cpi->mb); |
| 722 } |
| 723 |
| 724 void vp9_set_quantizer(struct VP9_COMP *cpi, int Q) { |
| 725 VP9_COMMON *cm = &cpi->common; |
| 726 |
| 727 cm->base_qindex = Q; |
| 728 |
| 729 // if any of the delta_q values are changing update flag will |
| 730 // have to be set. |
| 731 cm->y1dc_delta_q = 0; |
| 732 cm->y2ac_delta_q = 0; |
| 733 cm->uvdc_delta_q = 0; |
| 734 cm->uvac_delta_q = 0; |
| 735 cm->y2dc_delta_q = 0; |
| 736 |
| 737 // quantizer has to be reinitialized if any delta_q changes. |
| 738 // As there are not any here for now this is inactive code. |
| 739 // if(update) |
| 740 // vp9_init_quantizer(cpi); |
| 741 } |
OLD | NEW |