media/libvpx/vp8/encoder/quantize.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11
michael@0 12 #include <math.h>
michael@0 13 #include "vpx_mem/vpx_mem.h"
michael@0 14
michael@0 15 #include "onyx_int.h"
michael@0 16 #include "quantize.h"
michael@0 17 #include "vp8/common/quant_common.h"
michael@0 18
michael@0 19 #define EXACT_QUANT
michael@0 20
michael@0 21 #ifdef EXACT_FASTQUANT
michael@0 22 void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
michael@0 23 {
michael@0 24 int i, rc, eob;
michael@0 25 int zbin;
michael@0 26 int x, y, z, sz;
michael@0 27 short *coeff_ptr = b->coeff;
michael@0 28 short *zbin_ptr = b->zbin;
michael@0 29 short *round_ptr = b->round;
michael@0 30 short *quant_ptr = b->quant_fast;
michael@0 31 unsigned char *quant_shift_ptr = b->quant_shift;
michael@0 32 short *qcoeff_ptr = d->qcoeff;
michael@0 33 short *dqcoeff_ptr = d->dqcoeff;
michael@0 34 short *dequant_ptr = d->dequant;
michael@0 35
michael@0 36 vpx_memset(qcoeff_ptr, 0, 32);
michael@0 37 vpx_memset(dqcoeff_ptr, 0, 32);
michael@0 38
michael@0 39 eob = -1;
michael@0 40
michael@0 41 for (i = 0; i < 16; i++)
michael@0 42 {
michael@0 43 rc = vp8_default_zig_zag1d[i];
michael@0 44 z = coeff_ptr[rc];
michael@0 45 zbin = zbin_ptr[rc] ;
michael@0 46
michael@0 47 sz = (z >> 31); /* sign of z */
michael@0 48 x = (z ^ sz) - sz; /* x = abs(z) */
michael@0 49
michael@0 50 if (x >= zbin)
michael@0 51 {
michael@0 52 x += round_ptr[rc];
michael@0 53 y = ((((x * quant_ptr[rc]) >> 16) + x)
michael@0 54 * quant_shift_ptr[rc]) >> 16; /* quantize (x) */
michael@0 55 x = (y ^ sz) - sz; /* get the sign back */
michael@0 56 qcoeff_ptr[rc] = x; /* write to destination */
michael@0 57 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; /* dequantized value */
michael@0 58
michael@0 59 if (y)
michael@0 60 {
michael@0 61 eob = i; /* last nonzero coeffs */
michael@0 62 }
michael@0 63 }
michael@0 64 }
michael@0 65 *d->eob = (char)(eob + 1);
michael@0 66 }
michael@0 67
michael@0 68 #else
michael@0 69
michael@0 70 void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
michael@0 71 {
michael@0 72 int i, rc, eob;
michael@0 73 int x, y, z, sz;
michael@0 74 short *coeff_ptr = b->coeff;
michael@0 75 short *round_ptr = b->round;
michael@0 76 short *quant_ptr = b->quant_fast;
michael@0 77 short *qcoeff_ptr = d->qcoeff;
michael@0 78 short *dqcoeff_ptr = d->dqcoeff;
michael@0 79 short *dequant_ptr = d->dequant;
michael@0 80
michael@0 81 eob = -1;
michael@0 82 for (i = 0; i < 16; i++)
michael@0 83 {
michael@0 84 rc = vp8_default_zig_zag1d[i];
michael@0 85 z = coeff_ptr[rc];
michael@0 86
michael@0 87 sz = (z >> 31); /* sign of z */
michael@0 88 x = (z ^ sz) - sz; /* x = abs(z) */
michael@0 89
michael@0 90 y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */
michael@0 91 x = (y ^ sz) - sz; /* get the sign back */
michael@0 92 qcoeff_ptr[rc] = x; /* write to destination */
michael@0 93 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; /* dequantized value */
michael@0 94
michael@0 95 if (y)
michael@0 96 {
michael@0 97 eob = i; /* last nonzero coeffs */
michael@0 98 }
michael@0 99 }
michael@0 100 *d->eob = (char)(eob + 1);
michael@0 101 }
michael@0 102
michael@0 103 #endif
michael@0 104
michael@0 105 #ifdef EXACT_QUANT
michael@0 106 void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
michael@0 107 {
michael@0 108 int i, rc, eob;
michael@0 109 int zbin;
michael@0 110 int x, y, z, sz;
michael@0 111 short *zbin_boost_ptr = b->zrun_zbin_boost;
michael@0 112 short *coeff_ptr = b->coeff;
michael@0 113 short *zbin_ptr = b->zbin;
michael@0 114 short *round_ptr = b->round;
michael@0 115 short *quant_ptr = b->quant;
michael@0 116 short *quant_shift_ptr = b->quant_shift;
michael@0 117 short *qcoeff_ptr = d->qcoeff;
michael@0 118 short *dqcoeff_ptr = d->dqcoeff;
michael@0 119 short *dequant_ptr = d->dequant;
michael@0 120 short zbin_oq_value = b->zbin_extra;
michael@0 121
michael@0 122 vpx_memset(qcoeff_ptr, 0, 32);
michael@0 123 vpx_memset(dqcoeff_ptr, 0, 32);
michael@0 124
michael@0 125 eob = -1;
michael@0 126
michael@0 127 for (i = 0; i < 16; i++)
michael@0 128 {
michael@0 129 rc = vp8_default_zig_zag1d[i];
michael@0 130 z = coeff_ptr[rc];
michael@0 131
michael@0 132 zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
michael@0 133
michael@0 134 zbin_boost_ptr ++;
michael@0 135 sz = (z >> 31); /* sign of z */
michael@0 136 x = (z ^ sz) - sz; /* x = abs(z) */
michael@0 137
michael@0 138 if (x >= zbin)
michael@0 139 {
michael@0 140 x += round_ptr[rc];
michael@0 141 y = ((((x * quant_ptr[rc]) >> 16) + x)
michael@0 142 * quant_shift_ptr[rc]) >> 16; /* quantize (x) */
michael@0 143 x = (y ^ sz) - sz; /* get the sign back */
michael@0 144 qcoeff_ptr[rc] = x; /* write to destination */
michael@0 145 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; /* dequantized value */
michael@0 146
michael@0 147 if (y)
michael@0 148 {
michael@0 149 eob = i; /* last nonzero coeffs */
michael@0 150 zbin_boost_ptr = b->zrun_zbin_boost; /* reset zero runlength */
michael@0 151 }
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 *d->eob = (char)(eob + 1);
michael@0 156 }
michael@0 157
michael@0 158 /* Perform regular quantization, with unbiased rounding and no zero bin. */
michael@0 159 void vp8_strict_quantize_b_c(BLOCK *b, BLOCKD *d)
michael@0 160 {
michael@0 161 int i;
michael@0 162 int rc;
michael@0 163 int eob;
michael@0 164 int x;
michael@0 165 int y;
michael@0 166 int z;
michael@0 167 int sz;
michael@0 168 short *coeff_ptr;
michael@0 169 short *quant_ptr;
michael@0 170 short *quant_shift_ptr;
michael@0 171 short *qcoeff_ptr;
michael@0 172 short *dqcoeff_ptr;
michael@0 173 short *dequant_ptr;
michael@0 174
michael@0 175 coeff_ptr = b->coeff;
michael@0 176 quant_ptr = b->quant;
michael@0 177 quant_shift_ptr = b->quant_shift;
michael@0 178 qcoeff_ptr = d->qcoeff;
michael@0 179 dqcoeff_ptr = d->dqcoeff;
michael@0 180 dequant_ptr = d->dequant;
michael@0 181 eob = - 1;
michael@0 182 vpx_memset(qcoeff_ptr, 0, 32);
michael@0 183 vpx_memset(dqcoeff_ptr, 0, 32);
michael@0 184 for (i = 0; i < 16; i++)
michael@0 185 {
michael@0 186 int dq;
michael@0 187 int rounding;
michael@0 188
michael@0 189 /*TODO: These arrays should be stored in zig-zag order.*/
michael@0 190 rc = vp8_default_zig_zag1d[i];
michael@0 191 z = coeff_ptr[rc];
michael@0 192 dq = dequant_ptr[rc];
michael@0 193 rounding = dq >> 1;
michael@0 194 /* Sign of z. */
michael@0 195 sz = -(z < 0);
michael@0 196 x = (z + sz) ^ sz;
michael@0 197 x += rounding;
michael@0 198 if (x >= dq)
michael@0 199 {
michael@0 200 /* Quantize x. */
michael@0 201 y = ((((x * quant_ptr[rc]) >> 16) + x) * quant_shift_ptr[rc]) >> 16;
michael@0 202 /* Put the sign back. */
michael@0 203 x = (y + sz) ^ sz;
michael@0 204 /* Save the coefficient and its dequantized value. */
michael@0 205 qcoeff_ptr[rc] = x;
michael@0 206 dqcoeff_ptr[rc] = x * dq;
michael@0 207 /* Remember the last non-zero coefficient. */
michael@0 208 if (y)
michael@0 209 eob = i;
michael@0 210 }
michael@0 211 }
michael@0 212
michael@0 213 *d->eob = (char)(eob + 1);
michael@0 214 }
michael@0 215
michael@0 216 #else
michael@0 217
michael@0 218 void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
michael@0 219 {
michael@0 220 int i, rc, eob;
michael@0 221 int zbin;
michael@0 222 int x, y, z, sz;
michael@0 223 short *zbin_boost_ptr = b->zrun_zbin_boost;
michael@0 224 short *coeff_ptr = b->coeff;
michael@0 225 short *zbin_ptr = b->zbin;
michael@0 226 short *round_ptr = b->round;
michael@0 227 short *quant_ptr = b->quant;
michael@0 228 short *qcoeff_ptr = d->qcoeff;
michael@0 229 short *dqcoeff_ptr = d->dqcoeff;
michael@0 230 short *dequant_ptr = d->dequant;
michael@0 231 short zbin_oq_value = b->zbin_extra;
michael@0 232
michael@0 233 vpx_memset(qcoeff_ptr, 0, 32);
michael@0 234 vpx_memset(dqcoeff_ptr, 0, 32);
michael@0 235
michael@0 236 eob = -1;
michael@0 237
michael@0 238 for (i = 0; i < 16; i++)
michael@0 239 {
michael@0 240 rc = vp8_default_zig_zag1d[i];
michael@0 241 z = coeff_ptr[rc];
michael@0 242
michael@0 243 zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
michael@0 244
michael@0 245 zbin_boost_ptr ++;
michael@0 246 sz = (z >> 31); /* sign of z */
michael@0 247 x = (z ^ sz) - sz; /* x = abs(z) */
michael@0 248
michael@0 249 if (x >= zbin)
michael@0 250 {
michael@0 251 y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */
michael@0 252 x = (y ^ sz) - sz; /* get the sign back */
michael@0 253 qcoeff_ptr[rc] = x; /* write to destination */
michael@0 254 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; /* dequantized value */
michael@0 255
michael@0 256 if (y)
michael@0 257 {
michael@0 258 eob = i; /* last nonzero coeffs */
michael@0 259 zbin_boost_ptr = &b->zrun_zbin_boost[0]; /* reset zrl */
michael@0 260 }
michael@0 261 }
michael@0 262 }
michael@0 263
michael@0 264 *d->eob = (char)(eob + 1);
michael@0 265 }
michael@0 266
michael@0 267 #endif
michael@0 268
michael@0 269 void vp8_quantize_mby_c(MACROBLOCK *x)
michael@0 270 {
michael@0 271 int i;
michael@0 272 int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
michael@0 273 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
michael@0 274
michael@0 275 for (i = 0; i < 16; i++)
michael@0 276 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
michael@0 277
michael@0 278 if(has_2nd_order)
michael@0 279 x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
michael@0 280 }
michael@0 281
michael@0 282 void vp8_quantize_mb_c(MACROBLOCK *x)
michael@0 283 {
michael@0 284 int i;
michael@0 285 int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
michael@0 286 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
michael@0 287
michael@0 288 for (i = 0; i < 24+has_2nd_order; i++)
michael@0 289 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
michael@0 290 }
michael@0 291
michael@0 292
michael@0 293 void vp8_quantize_mbuv_c(MACROBLOCK *x)
michael@0 294 {
michael@0 295 int i;
michael@0 296
michael@0 297 for (i = 16; i < 24; i++)
michael@0 298 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
michael@0 299 }
michael@0 300
michael@0 301 /* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
michael@0 302 * these two C functions if corresponding optimized routine is not available.
michael@0 303 * NEON optimized version implements currently the fast quantization for pair
michael@0 304 * of blocks. */
michael@0 305 void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
michael@0 306 {
michael@0 307 vp8_regular_quantize_b(b1, d1);
michael@0 308 vp8_regular_quantize_b(b2, d2);
michael@0 309 }
michael@0 310
michael@0 311 void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
michael@0 312 {
michael@0 313 vp8_fast_quantize_b_c(b1, d1);
michael@0 314 vp8_fast_quantize_b_c(b2, d2);
michael@0 315 }
michael@0 316
michael@0 317
michael@0 318 static const int qrounding_factors[129] =
michael@0 319 {
michael@0 320 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 321 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 322 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 323 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 324 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 325 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 326 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 327 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 328 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 329 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 330 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 331 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 332 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 333 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 334 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 335 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 336 48
michael@0 337 };
michael@0 338
michael@0 339
michael@0 340 static const int qzbin_factors[129] =
michael@0 341 {
michael@0 342 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 343 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 344 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 345 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 346 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 347 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 348 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 349 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 350 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 351 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 352 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 353 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 354 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 355 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 356 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 357 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 358 80
michael@0 359 };
michael@0 360
michael@0 361
michael@0 362 static const int qrounding_factors_y2[129] =
michael@0 363 {
michael@0 364 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 365 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 366 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 367 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 368 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 369 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 370 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 371 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 372 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 373 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 374 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 375 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 376 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 377 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 378 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 379 48, 48, 48, 48, 48, 48, 48, 48,
michael@0 380 48
michael@0 381 };
michael@0 382
michael@0 383
michael@0 384 static const int qzbin_factors_y2[129] =
michael@0 385 {
michael@0 386 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 387 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 388 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 389 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 390 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 391 84, 84, 84, 84, 84, 84, 84, 84,
michael@0 392 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 393 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 394 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 395 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 396 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 397 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 398 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 399 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 400 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 401 80, 80, 80, 80, 80, 80, 80, 80,
michael@0 402 80
michael@0 403 };
michael@0 404
michael@0 405
michael@0 406 #define EXACT_QUANT
michael@0 407 #ifdef EXACT_QUANT
michael@0 408 static void invert_quant(int improved_quant, short *quant,
michael@0 409 short *shift, short d)
michael@0 410 {
michael@0 411 if(improved_quant)
michael@0 412 {
michael@0 413 unsigned t;
michael@0 414 int l;
michael@0 415 t = d;
michael@0 416 for(l = 0; t > 1; l++)
michael@0 417 t>>=1;
michael@0 418 t = 1 + (1<<(16+l))/d;
michael@0 419 *quant = (short)(t - (1<<16));
michael@0 420 *shift = l;
michael@0 421 /* use multiplication and constant shift by 16 */
michael@0 422 *shift = 1 << (16 - *shift);
michael@0 423 }
michael@0 424 else
michael@0 425 {
michael@0 426 *quant = (1 << 16) / d;
michael@0 427 *shift = 0;
michael@0 428 /* use multiplication and constant shift by 16 */
michael@0 429 *shift = 1 << (16 - *shift);
michael@0 430 }
michael@0 431 }
michael@0 432
michael@0 433
michael@0 434 void vp8cx_init_quantizer(VP8_COMP *cpi)
michael@0 435 {
michael@0 436 int i;
michael@0 437 int quant_val;
michael@0 438 int Q;
michael@0 439
michael@0 440 int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44,
michael@0 441 44, 44};
michael@0 442
michael@0 443 for (Q = 0; Q < QINDEX_RANGE; Q++)
michael@0 444 {
michael@0 445 /* dc values */
michael@0 446 quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
michael@0 447 cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
michael@0 448 invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
michael@0 449 cpi->Y1quant_shift[Q] + 0, quant_val);
michael@0 450 cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
michael@0 451 cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 452 cpi->common.Y1dequant[Q][0] = quant_val;
michael@0 453 cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
michael@0 454
michael@0 455 quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
michael@0 456 cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
michael@0 457 invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
michael@0 458 cpi->Y2quant_shift[Q] + 0, quant_val);
michael@0 459 cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
michael@0 460 cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
michael@0 461 cpi->common.Y2dequant[Q][0] = quant_val;
michael@0 462 cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
michael@0 463
michael@0 464 quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
michael@0 465 cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
michael@0 466 invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
michael@0 467 cpi->UVquant_shift[Q] + 0, quant_val);
michael@0 468 cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
michael@0 469 cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 470 cpi->common.UVdequant[Q][0] = quant_val;
michael@0 471 cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
michael@0 472
michael@0 473 /* all the ac values = ; */
michael@0 474 quant_val = vp8_ac_yquant(Q);
michael@0 475 cpi->Y1quant_fast[Q][1] = (1 << 16) / quant_val;
michael@0 476 invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 1,
michael@0 477 cpi->Y1quant_shift[Q] + 1, quant_val);
michael@0 478 cpi->Y1zbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
michael@0 479 cpi->Y1round[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 480 cpi->common.Y1dequant[Q][1] = quant_val;
michael@0 481 cpi->zrun_zbin_boost_y1[Q][1] = (quant_val * zbin_boost[1]) >> 7;
michael@0 482
michael@0 483 quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
michael@0 484 cpi->Y2quant_fast[Q][1] = (1 << 16) / quant_val;
michael@0 485 invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 1,
michael@0 486 cpi->Y2quant_shift[Q] + 1, quant_val);
michael@0 487 cpi->Y2zbin[Q][1] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
michael@0 488 cpi->Y2round[Q][1] = (qrounding_factors_y2[Q] * quant_val) >> 7;
michael@0 489 cpi->common.Y2dequant[Q][1] = quant_val;
michael@0 490 cpi->zrun_zbin_boost_y2[Q][1] = (quant_val * zbin_boost[1]) >> 7;
michael@0 491
michael@0 492 quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
michael@0 493 cpi->UVquant_fast[Q][1] = (1 << 16) / quant_val;
michael@0 494 invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 1,
michael@0 495 cpi->UVquant_shift[Q] + 1, quant_val);
michael@0 496 cpi->UVzbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
michael@0 497 cpi->UVround[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 498 cpi->common.UVdequant[Q][1] = quant_val;
michael@0 499 cpi->zrun_zbin_boost_uv[Q][1] = (quant_val * zbin_boost[1]) >> 7;
michael@0 500
michael@0 501 for (i = 2; i < 16; i++)
michael@0 502 {
michael@0 503 cpi->Y1quant_fast[Q][i] = cpi->Y1quant_fast[Q][1];
michael@0 504 cpi->Y1quant[Q][i] = cpi->Y1quant[Q][1];
michael@0 505 cpi->Y1quant_shift[Q][i] = cpi->Y1quant_shift[Q][1];
michael@0 506 cpi->Y1zbin[Q][i] = cpi->Y1zbin[Q][1];
michael@0 507 cpi->Y1round[Q][i] = cpi->Y1round[Q][1];
michael@0 508 cpi->zrun_zbin_boost_y1[Q][i] = (cpi->common.Y1dequant[Q][1] *
michael@0 509 zbin_boost[i]) >> 7;
michael@0 510
michael@0 511 cpi->Y2quant_fast[Q][i] = cpi->Y2quant_fast[Q][1];
michael@0 512 cpi->Y2quant[Q][i] = cpi->Y2quant[Q][1];
michael@0 513 cpi->Y2quant_shift[Q][i] = cpi->Y2quant_shift[Q][1];
michael@0 514 cpi->Y2zbin[Q][i] = cpi->Y2zbin[Q][1];
michael@0 515 cpi->Y2round[Q][i] = cpi->Y2round[Q][1];
michael@0 516 cpi->zrun_zbin_boost_y2[Q][i] = (cpi->common.Y2dequant[Q][1] *
michael@0 517 zbin_boost[i]) >> 7;
michael@0 518
michael@0 519 cpi->UVquant_fast[Q][i] = cpi->UVquant_fast[Q][1];
michael@0 520 cpi->UVquant[Q][i] = cpi->UVquant[Q][1];
michael@0 521 cpi->UVquant_shift[Q][i] = cpi->UVquant_shift[Q][1];
michael@0 522 cpi->UVzbin[Q][i] = cpi->UVzbin[Q][1];
michael@0 523 cpi->UVround[Q][i] = cpi->UVround[Q][1];
michael@0 524 cpi->zrun_zbin_boost_uv[Q][i] = (cpi->common.UVdequant[Q][1] *
michael@0 525 zbin_boost[i]) >> 7;
michael@0 526 }
michael@0 527 }
michael@0 528 }
michael@0 529 #else
michael@0 530 void vp8cx_init_quantizer(VP8_COMP *cpi)
michael@0 531 {
michael@0 532 int i;
michael@0 533 int quant_val;
michael@0 534 int Q;
michael@0 535
michael@0 536 int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
michael@0 537
michael@0 538 for (Q = 0; Q < QINDEX_RANGE; Q++)
michael@0 539 {
michael@0 540 /* dc values */
michael@0 541 quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
michael@0 542 cpi->Y1quant[Q][0] = (1 << 16) / quant_val;
michael@0 543 cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
michael@0 544 cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 545 cpi->common.Y1dequant[Q][0] = quant_val;
michael@0 546 cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
michael@0 547
michael@0 548 quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
michael@0 549 cpi->Y2quant[Q][0] = (1 << 16) / quant_val;
michael@0 550 cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
michael@0 551 cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
michael@0 552 cpi->common.Y2dequant[Q][0] = quant_val;
michael@0 553 cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
michael@0 554
michael@0 555 quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
michael@0 556 cpi->UVquant[Q][0] = (1 << 16) / quant_val;
michael@0 557 cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
michael@0 558 cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 559 cpi->common.UVdequant[Q][0] = quant_val;
michael@0 560 cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
michael@0 561
michael@0 562 /* all the ac values = ; */
michael@0 563 for (i = 1; i < 16; i++)
michael@0 564 {
michael@0 565 int rc = vp8_default_zig_zag1d[i];
michael@0 566
michael@0 567 quant_val = vp8_ac_yquant(Q);
michael@0 568 cpi->Y1quant[Q][rc] = (1 << 16) / quant_val;
michael@0 569 cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
michael@0 570 cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 571 cpi->common.Y1dequant[Q][rc] = quant_val;
michael@0 572 cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
michael@0 573
michael@0 574 quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
michael@0 575 cpi->Y2quant[Q][rc] = (1 << 16) / quant_val;
michael@0 576 cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
michael@0 577 cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
michael@0 578 cpi->common.Y2dequant[Q][rc] = quant_val;
michael@0 579 cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
michael@0 580
michael@0 581 quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
michael@0 582 cpi->UVquant[Q][rc] = (1 << 16) / quant_val;
michael@0 583 cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
michael@0 584 cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
michael@0 585 cpi->common.UVdequant[Q][rc] = quant_val;
michael@0 586 cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
michael@0 587 }
michael@0 588 }
michael@0 589 }
michael@0 590 #endif
michael@0 591
michael@0 592 #define ZBIN_EXTRA_Y \
michael@0 593 (( cpi->common.Y1dequant[QIndex][1] * \
michael@0 594 ( x->zbin_over_quant + \
michael@0 595 x->zbin_mode_boost + \
michael@0 596 x->act_zbin_adj ) ) >> 7)
michael@0 597
michael@0 598 #define ZBIN_EXTRA_UV \
michael@0 599 (( cpi->common.UVdequant[QIndex][1] * \
michael@0 600 ( x->zbin_over_quant + \
michael@0 601 x->zbin_mode_boost + \
michael@0 602 x->act_zbin_adj ) ) >> 7)
michael@0 603
michael@0 604 #define ZBIN_EXTRA_Y2 \
michael@0 605 (( cpi->common.Y2dequant[QIndex][1] * \
michael@0 606 ( (x->zbin_over_quant / 2) + \
michael@0 607 x->zbin_mode_boost + \
michael@0 608 x->act_zbin_adj ) ) >> 7)
michael@0 609
michael@0 610 void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip)
michael@0 611 {
michael@0 612 int i;
michael@0 613 int QIndex;
michael@0 614 MACROBLOCKD *xd = &x->e_mbd;
michael@0 615 int zbin_extra;
michael@0 616
michael@0 617 /* Select the baseline MB Q index. */
michael@0 618 if (xd->segmentation_enabled)
michael@0 619 {
michael@0 620 /* Abs Value */
michael@0 621 if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
michael@0 622 QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
michael@0 623 /* Delta Value */
michael@0 624 else
michael@0 625 {
michael@0 626 QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
michael@0 627 /* Clamp to valid range */
michael@0 628 QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;
michael@0 629 }
michael@0 630 }
michael@0 631 else
michael@0 632 QIndex = cpi->common.base_qindex;
michael@0 633
michael@0 634 /* This initialization should be called at least once. Use ok_to_skip to
michael@0 635 * decide if it is ok to skip.
michael@0 636 * Before encoding a frame, this function is always called with ok_to_skip
michael@0 637 * =0, which means no skiping of calculations. The "last" values are
michael@0 638 * initialized at that time.
michael@0 639 */
michael@0 640 if (!ok_to_skip || QIndex != x->q_index)
michael@0 641 {
michael@0 642
michael@0 643 xd->dequant_y1_dc[0] = 1;
michael@0 644 xd->dequant_y1[0] = cpi->common.Y1dequant[QIndex][0];
michael@0 645 xd->dequant_y2[0] = cpi->common.Y2dequant[QIndex][0];
michael@0 646 xd->dequant_uv[0] = cpi->common.UVdequant[QIndex][0];
michael@0 647
michael@0 648 for (i = 1; i < 16; i++)
michael@0 649 {
michael@0 650 xd->dequant_y1_dc[i] =
michael@0 651 xd->dequant_y1[i] = cpi->common.Y1dequant[QIndex][1];
michael@0 652 xd->dequant_y2[i] = cpi->common.Y2dequant[QIndex][1];
michael@0 653 xd->dequant_uv[i] = cpi->common.UVdequant[QIndex][1];
michael@0 654 }
michael@0 655 #if 1
michael@0 656 /*TODO: Remove dequant from BLOCKD. This is a temporary solution until
michael@0 657 * the quantizer code uses a passed in pointer to the dequant constants.
michael@0 658 * This will also require modifications to the x86 and neon assembly.
michael@0 659 * */
michael@0 660 for (i = 0; i < 16; i++)
michael@0 661 x->e_mbd.block[i].dequant = xd->dequant_y1;
michael@0 662 for (i = 16; i < 24; i++)
michael@0 663 x->e_mbd.block[i].dequant = xd->dequant_uv;
michael@0 664 x->e_mbd.block[24].dequant = xd->dequant_y2;
michael@0 665 #endif
michael@0 666
michael@0 667 /* Y */
michael@0 668 zbin_extra = ZBIN_EXTRA_Y;
michael@0 669
michael@0 670 for (i = 0; i < 16; i++)
michael@0 671 {
michael@0 672 x->block[i].quant = cpi->Y1quant[QIndex];
michael@0 673 x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
michael@0 674 x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
michael@0 675 x->block[i].zbin = cpi->Y1zbin[QIndex];
michael@0 676 x->block[i].round = cpi->Y1round[QIndex];
michael@0 677 x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
michael@0 678 x->block[i].zbin_extra = (short)zbin_extra;
michael@0 679 }
michael@0 680
michael@0 681 /* UV */
michael@0 682 zbin_extra = ZBIN_EXTRA_UV;
michael@0 683
michael@0 684 for (i = 16; i < 24; i++)
michael@0 685 {
michael@0 686 x->block[i].quant = cpi->UVquant[QIndex];
michael@0 687 x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
michael@0 688 x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
michael@0 689 x->block[i].zbin = cpi->UVzbin[QIndex];
michael@0 690 x->block[i].round = cpi->UVround[QIndex];
michael@0 691 x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
michael@0 692 x->block[i].zbin_extra = (short)zbin_extra;
michael@0 693 }
michael@0 694
michael@0 695 /* Y2 */
michael@0 696 zbin_extra = ZBIN_EXTRA_Y2;
michael@0 697
michael@0 698 x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
michael@0 699 x->block[24].quant = cpi->Y2quant[QIndex];
michael@0 700 x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
michael@0 701 x->block[24].zbin = cpi->Y2zbin[QIndex];
michael@0 702 x->block[24].round = cpi->Y2round[QIndex];
michael@0 703 x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
michael@0 704 x->block[24].zbin_extra = (short)zbin_extra;
michael@0 705
michael@0 706 /* save this macroblock QIndex for vp8_update_zbin_extra() */
michael@0 707 x->q_index = QIndex;
michael@0 708
michael@0 709 x->last_zbin_over_quant = x->zbin_over_quant;
michael@0 710 x->last_zbin_mode_boost = x->zbin_mode_boost;
michael@0 711 x->last_act_zbin_adj = x->act_zbin_adj;
michael@0 712
michael@0 713
michael@0 714
michael@0 715 }
michael@0 716 else if(x->last_zbin_over_quant != x->zbin_over_quant
michael@0 717 || x->last_zbin_mode_boost != x->zbin_mode_boost
michael@0 718 || x->last_act_zbin_adj != x->act_zbin_adj)
michael@0 719 {
michael@0 720 /* Y */
michael@0 721 zbin_extra = ZBIN_EXTRA_Y;
michael@0 722
michael@0 723 for (i = 0; i < 16; i++)
michael@0 724 x->block[i].zbin_extra = (short)zbin_extra;
michael@0 725
michael@0 726 /* UV */
michael@0 727 zbin_extra = ZBIN_EXTRA_UV;
michael@0 728
michael@0 729 for (i = 16; i < 24; i++)
michael@0 730 x->block[i].zbin_extra = (short)zbin_extra;
michael@0 731
michael@0 732 /* Y2 */
michael@0 733 zbin_extra = ZBIN_EXTRA_Y2;
michael@0 734 x->block[24].zbin_extra = (short)zbin_extra;
michael@0 735
michael@0 736 x->last_zbin_over_quant = x->zbin_over_quant;
michael@0 737 x->last_zbin_mode_boost = x->zbin_mode_boost;
michael@0 738 x->last_act_zbin_adj = x->act_zbin_adj;
michael@0 739 }
michael@0 740 }
michael@0 741
michael@0 742 void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
michael@0 743 {
michael@0 744 int i;
michael@0 745 int QIndex = x->q_index;
michael@0 746 int zbin_extra;
michael@0 747
michael@0 748 /* Y */
michael@0 749 zbin_extra = ZBIN_EXTRA_Y;
michael@0 750
michael@0 751 for (i = 0; i < 16; i++)
michael@0 752 x->block[i].zbin_extra = (short)zbin_extra;
michael@0 753
michael@0 754 /* UV */
michael@0 755 zbin_extra = ZBIN_EXTRA_UV;
michael@0 756
michael@0 757 for (i = 16; i < 24; i++)
michael@0 758 x->block[i].zbin_extra = (short)zbin_extra;
michael@0 759
michael@0 760 /* Y2 */
michael@0 761 zbin_extra = ZBIN_EXTRA_Y2;
michael@0 762 x->block[24].zbin_extra = (short)zbin_extra;
michael@0 763 }
michael@0 764 #undef ZBIN_EXTRA_Y
michael@0 765 #undef ZBIN_EXTRA_UV
michael@0 766 #undef ZBIN_EXTRA_Y2
michael@0 767
michael@0 768 void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
michael@0 769 {
michael@0 770 /* Clear Zbin mode boost for default case */
michael@0 771 cpi->mb.zbin_mode_boost = 0;
michael@0 772
michael@0 773 /* MB level quantizer setup */
michael@0 774 vp8cx_mb_init_quantizer(cpi, &cpi->mb, 0);
michael@0 775 }
michael@0 776
michael@0 777
michael@0 778 void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
michael@0 779 {
michael@0 780 VP8_COMMON *cm = &cpi->common;
michael@0 781 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
michael@0 782 int update = 0;
michael@0 783 int new_delta_q;
michael@0 784 cm->base_qindex = Q;
michael@0 785
michael@0 786 /* if any of the delta_q values are changing update flag has to be set */
michael@0 787 /* currently only y2dc_delta_q may change */
michael@0 788
michael@0 789 cm->y1dc_delta_q = 0;
michael@0 790 cm->y2ac_delta_q = 0;
michael@0 791 cm->uvdc_delta_q = 0;
michael@0 792 cm->uvac_delta_q = 0;
michael@0 793
michael@0 794 if (Q < 4)
michael@0 795 {
michael@0 796 new_delta_q = 4-Q;
michael@0 797 }
michael@0 798 else
michael@0 799 new_delta_q = 0;
michael@0 800
michael@0 801 update |= cm->y2dc_delta_q != new_delta_q;
michael@0 802 cm->y2dc_delta_q = new_delta_q;
michael@0 803
michael@0 804
michael@0 805 /* Set Segment specific quatizers */
michael@0 806 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
michael@0 807 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
michael@0 808 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
michael@0 809 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
michael@0 810
michael@0 811 /* quantizer has to be reinitialized for any delta_q changes */
michael@0 812 if(update)
michael@0 813 vp8cx_init_quantizer(cpi);
michael@0 814
michael@0 815 }

mercurial