2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include "vpx_mem/vpx_mem.h"
|
|
|
|
|
|
|
|
#include "quantize.h"
|
|
|
|
#include "entropy.h"
|
|
|
|
#include "predictdc.h"
|
|
|
|
|
2010-11-08 16:28:54 +01:00
|
|
|
#define EXACT_QUANT
|
2010-11-11 18:41:07 +01:00
|
|
|
|
|
|
|
#ifdef EXACT_FASTQUANT
|
2010-05-18 17:58:33 +02:00
|
|
|
void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
|
|
|
|
{
|
|
|
|
int i, rc, eob;
|
|
|
|
int zbin;
|
|
|
|
int x, y, z, sz;
|
2010-10-22 02:04:30 +02:00
|
|
|
short *coeff_ptr = b->coeff;
|
|
|
|
short *zbin_ptr = b->zbin;
|
|
|
|
short *round_ptr = b->round;
|
2010-12-28 20:51:46 +01:00
|
|
|
short *quant_ptr = b->quant_fast;
|
2010-10-22 02:04:30 +02:00
|
|
|
short *quant_shift_ptr = b->quant_shift;
|
|
|
|
short *qcoeff_ptr = d->qcoeff;
|
|
|
|
short *dqcoeff_ptr = d->dqcoeff;
|
|
|
|
short *dequant_ptr = d->dequant;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
vpx_memset(qcoeff_ptr, 0, 32);
|
|
|
|
vpx_memset(dqcoeff_ptr, 0, 32);
|
|
|
|
|
|
|
|
eob = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
rc = vp8_default_zig_zag1d[i];
|
|
|
|
z = coeff_ptr[rc];
|
|
|
|
zbin = zbin_ptr[rc] ;
|
|
|
|
|
|
|
|
sz = (z >> 31); // sign of z
|
|
|
|
x = (z ^ sz) - sz; // x = abs(z)
|
|
|
|
|
|
|
|
if (x >= zbin)
|
|
|
|
{
|
2010-06-29 02:15:09 +02:00
|
|
|
x += round_ptr[rc];
|
|
|
|
y = (((x * quant_ptr[rc]) >> 16) + x)
|
|
|
|
>> quant_shift_ptr[rc]; // quantize (x)
|
2010-05-18 17:58:33 +02:00
|
|
|
x = (y ^ sz) - sz; // get the sign back
|
|
|
|
qcoeff_ptr[rc] = x; // write to destination
|
|
|
|
dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
|
|
|
|
|
|
|
|
if (y)
|
|
|
|
{
|
|
|
|
eob = i; // last nonzero coeffs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d->eob = eob + 1;
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:41:07 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
|
|
|
|
{
|
|
|
|
int i, rc, eob;
|
|
|
|
int x, y, z, sz;
|
|
|
|
short *coeff_ptr = b->coeff;
|
|
|
|
short *round_ptr = b->round;
|
2010-12-28 20:51:46 +01:00
|
|
|
short *quant_ptr = b->quant_fast;
|
2010-11-11 18:41:07 +01:00
|
|
|
short *qcoeff_ptr = d->qcoeff;
|
|
|
|
short *dqcoeff_ptr = d->dqcoeff;
|
|
|
|
short *dequant_ptr = d->dequant;
|
|
|
|
|
|
|
|
eob = -1;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
rc = vp8_default_zig_zag1d[i];
|
|
|
|
z = coeff_ptr[rc];
|
|
|
|
|
|
|
|
sz = (z >> 31); // sign of z
|
|
|
|
x = (z ^ sz) - sz; // x = abs(z)
|
|
|
|
|
|
|
|
y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
|
|
|
|
x = (y ^ sz) - sz; // get the sign back
|
|
|
|
qcoeff_ptr[rc] = x; // write to destination
|
|
|
|
dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
|
|
|
|
|
|
|
|
if (y)
|
|
|
|
{
|
|
|
|
eob = i; // last nonzero coeffs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d->eob = eob + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EXACT_QUANT
|
2010-05-18 17:58:33 +02:00
|
|
|
void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
|
|
|
|
{
|
|
|
|
int i, rc, eob;
|
|
|
|
int zbin;
|
|
|
|
int x, y, z, sz;
|
2010-10-22 02:04:30 +02:00
|
|
|
short *zbin_boost_ptr = b->zrun_zbin_boost;
|
|
|
|
short *coeff_ptr = b->coeff;
|
|
|
|
short *zbin_ptr = b->zbin;
|
|
|
|
short *round_ptr = b->round;
|
|
|
|
short *quant_ptr = b->quant;
|
|
|
|
short *quant_shift_ptr = b->quant_shift;
|
|
|
|
short *qcoeff_ptr = d->qcoeff;
|
|
|
|
short *dqcoeff_ptr = d->dqcoeff;
|
|
|
|
short *dequant_ptr = d->dequant;
|
|
|
|
short zbin_oq_value = b->zbin_extra;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
vpx_memset(qcoeff_ptr, 0, 32);
|
|
|
|
vpx_memset(dqcoeff_ptr, 0, 32);
|
|
|
|
|
|
|
|
eob = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
rc = vp8_default_zig_zag1d[i];
|
|
|
|
z = coeff_ptr[rc];
|
|
|
|
|
|
|
|
zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
|
|
|
|
|
|
|
|
zbin_boost_ptr ++;
|
|
|
|
sz = (z >> 31); // sign of z
|
|
|
|
x = (z ^ sz) - sz; // x = abs(z)
|
|
|
|
|
|
|
|
if (x >= zbin)
|
|
|
|
{
|
2010-06-29 02:15:09 +02:00
|
|
|
x += round_ptr[rc];
|
|
|
|
y = (((x * quant_ptr[rc]) >> 16) + x)
|
|
|
|
>> quant_shift_ptr[rc]; // quantize (x)
|
2010-05-18 17:58:33 +02:00
|
|
|
x = (y ^ sz) - sz; // get the sign back
|
2011-01-11 15:41:57 +01:00
|
|
|
qcoeff_ptr[rc] = x; // write to destination
|
|
|
|
dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
if (y)
|
|
|
|
{
|
|
|
|
eob = i; // last nonzero coeffs
|
2011-01-11 15:41:57 +01:00
|
|
|
zbin_boost_ptr = b->zrun_zbin_boost; // reset zero runlength
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d->eob = eob + 1;
|
|
|
|
}
|
2010-10-11 22:49:52 +02:00
|
|
|
|
|
|
|
/* Perform regular quantization, with unbiased rounding and no zero bin. */
|
|
|
|
void vp8_strict_quantize_b(BLOCK *b, BLOCKD *d)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rc;
|
|
|
|
int eob;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int z;
|
|
|
|
int sz;
|
|
|
|
short *coeff_ptr;
|
|
|
|
short *quant_ptr;
|
|
|
|
short *quant_shift_ptr;
|
|
|
|
short *qcoeff_ptr;
|
|
|
|
short *dqcoeff_ptr;
|
|
|
|
short *dequant_ptr;
|
|
|
|
|
2010-10-22 02:04:30 +02:00
|
|
|
coeff_ptr = b->coeff;
|
|
|
|
quant_ptr = b->quant;
|
|
|
|
quant_shift_ptr = b->quant_shift;
|
|
|
|
qcoeff_ptr = d->qcoeff;
|
|
|
|
dqcoeff_ptr = d->dqcoeff;
|
|
|
|
dequant_ptr = d->dequant;
|
2010-10-11 22:49:52 +02:00
|
|
|
eob = - 1;
|
|
|
|
vpx_memset(qcoeff_ptr, 0, 32);
|
|
|
|
vpx_memset(dqcoeff_ptr, 0, 32);
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
int dq;
|
|
|
|
int round;
|
|
|
|
|
|
|
|
/*TODO: These arrays should be stored in zig-zag order.*/
|
|
|
|
rc = vp8_default_zig_zag1d[i];
|
|
|
|
z = coeff_ptr[rc];
|
|
|
|
dq = dequant_ptr[rc];
|
|
|
|
round = dq >> 1;
|
|
|
|
/* Sign of z. */
|
|
|
|
sz = -(z < 0);
|
|
|
|
x = (z + sz) ^ sz;
|
|
|
|
x += round;
|
|
|
|
if (x >= dq)
|
|
|
|
{
|
|
|
|
/* Quantize x. */
|
|
|
|
y = (((x * quant_ptr[rc]) >> 16) + x) >> quant_shift_ptr[rc];
|
|
|
|
/* Put the sign back. */
|
|
|
|
x = (y + sz) ^ sz;
|
|
|
|
/* Save the coefficient and its dequantized value. */
|
|
|
|
qcoeff_ptr[rc] = x;
|
|
|
|
dqcoeff_ptr[rc] = x * dq;
|
|
|
|
/* Remember the last non-zero coefficient. */
|
|
|
|
if (y)
|
|
|
|
eob = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d->eob = eob + 1;
|
|
|
|
}
|
|
|
|
|
2010-07-28 19:44:17 +02:00
|
|
|
#else
|
2010-09-02 19:33:01 +02:00
|
|
|
|
|
|
|
void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
|
|
|
|
{
|
|
|
|
int i, rc, eob;
|
|
|
|
int zbin;
|
|
|
|
int x, y, z, sz;
|
2010-10-22 02:04:30 +02:00
|
|
|
short *zbin_boost_ptr = b->zrun_zbin_boost;
|
|
|
|
short *coeff_ptr = b->coeff;
|
|
|
|
short *zbin_ptr = b->zbin;
|
|
|
|
short *round_ptr = b->round;
|
|
|
|
short *quant_ptr = b->quant;
|
|
|
|
short *qcoeff_ptr = d->qcoeff;
|
|
|
|
short *dqcoeff_ptr = d->dqcoeff;
|
|
|
|
short *dequant_ptr = d->dequant;
|
|
|
|
short zbin_oq_value = b->zbin_extra;
|
2010-09-02 19:33:01 +02:00
|
|
|
|
|
|
|
vpx_memset(qcoeff_ptr, 0, 32);
|
|
|
|
vpx_memset(dqcoeff_ptr, 0, 32);
|
|
|
|
|
|
|
|
eob = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
rc = vp8_default_zig_zag1d[i];
|
|
|
|
z = coeff_ptr[rc];
|
|
|
|
|
|
|
|
//if ( i == 0 )
|
|
|
|
// zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
|
|
|
|
//else
|
|
|
|
zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
|
|
|
|
|
|
|
|
zbin_boost_ptr ++;
|
|
|
|
sz = (z >> 31); // sign of z
|
|
|
|
x = (z ^ sz) - sz; // x = abs(z)
|
|
|
|
|
|
|
|
if (x >= zbin)
|
|
|
|
{
|
|
|
|
y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
|
|
|
|
x = (y ^ sz) - sz; // get the sign back
|
|
|
|
qcoeff_ptr[rc] = x; // write to destination
|
|
|
|
dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
|
|
|
|
|
|
|
|
if (y)
|
|
|
|
{
|
|
|
|
eob = i; // last nonzero coeffs
|
|
|
|
zbin_boost_ptr = &b->zrun_zbin_boost[0]; // reset zero runlength
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d->eob = eob + 1;
|
|
|
|
}
|
|
|
|
|
2010-07-28 19:44:17 +02:00
|
|
|
#endif
|
Add trellis quantization.
Replace the exponential search for optimal rounding during
quantization with a linear Viterbi trellis and enable it
by default when using --best.
Right now this operates on top of the output of the adaptive
zero-bin quantizer in vp8_regular_quantize_b() and gives a small
gain.
It can be tested as a replacement for that quantizer by
enabling the call to vp8_strict_quantize_b(), which uses
normal rounding and no zero bin offset.
Ultimately, the quantizer will have to become a function of lambda
in order to take advantage of activity masking, since there is
limited ability to change the quantization factor itself.
However, currently vp8_strict_quantize_b() plus the trellis
quantizer (which is lambda-dependent) loses to
vp8_regular_quantize_b() alone (which is not) on my test clip.
Patch Set 3:
Fix an issue related to the cost evaluation of successor
states when a coefficient is reduced to zero. With this
issue fixed, now the trellis search almost exactly matches
the exponential search.
Patch Set 2:
Overall, the goal of this patch set is to make "trellis"
search to produce encodings that match the exponential
search version. There are three main differences between
Patch Set 2 and 1:
a. Patch set 1 did not properly account for the scale of
2nd order error, so patch set 2 disable it all together
for 2nd blocks.
b. Patch set 1 was not consistent on when to enable the
the quantization optimization. Patch set 2 restore the
condition to be consistent.
c. Patch set 1 checks quantized level L-1, and L for any
input coefficient was quantized to L. Patch set 2 limits
the candidate coefficient to those that were rounded up
to L. It is worth noting here that a strategy to check
L and L+1 for coefficients that were truncated down to L
might work.
(a and b get trellis quant to basically match the exponential
search on all mid/low rate encodings on cif set, without
a, b, trellis quant can hurt the psnr by 0.2 to .3db at
200kbps for some cif clips)
(c gets trellis quant to match the exponential search
to match at Q0 encoding, without c, trellis quant can be
1.5 to 2db lower for encodings with fixed Q at 0 on most
derf cif clips)
Change-Id: Ib1a043b665d75fbf00cb0257b7c18e90eebab95e
2010-07-02 23:35:53 +02:00
|
|
|
|
2010-05-18 17:58:33 +02:00
|
|
|
void vp8_quantize_mby(MACROBLOCK *x)
|
|
|
|
{
|
|
|
|
int i;
|
2010-08-12 22:25:43 +02:00
|
|
|
int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
|
|
|
|
&& x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2010-06-08 22:32:15 +02:00
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2010-06-08 22:32:15 +02:00
|
|
|
if(has_2nd_order)
|
2010-05-18 17:58:33 +02:00
|
|
|
x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vp8_quantize_mb(MACROBLOCK *x)
|
|
|
|
{
|
|
|
|
int i;
|
2010-08-12 22:25:43 +02:00
|
|
|
int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
|
|
|
|
&& x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2010-06-08 22:32:15 +02:00
|
|
|
for (i = 0; i < 24+has_2nd_order; i++)
|
|
|
|
x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void vp8_quantize_mbuv(MACROBLOCK *x)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 16; i < 24; i++)
|
|
|
|
x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
|
|
|
|
}
|