Do token recording and counting in a single loop

Change-Id: I8afd3c486b210bd67888de03e91dde7f78276f89
This commit is contained in:
hui su 2016-07-19 16:28:26 -07:00
parent 9ac74f922e
commit 0c0fb83211
5 changed files with 62 additions and 62 deletions

View File

@ -281,18 +281,6 @@ int VP8GetCostUV(VP8EncIterator* const it, const VP8ModeScore* const rd) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Recording of token probabilities. // Recording of token probabilities.
// Record proba context used
static int Record(int bit, proba_t* const stats) {
proba_t p = *stats;
if (p >= 0xffff0000u) { // an overflow is inbound.
p = ((p + 1u) >> 1) & 0x7fff7fffu; // -> divide the stats by 2.
}
// record bit count (lower 16 bits) and increment total count (upper 16 bits).
p += 0x00010000u + bit;
*stats = p;
return bit;
}
// We keep the table-free variant around for reference, in case. // We keep the table-free variant around for reference, in case.
#define USE_LEVEL_CODE_TABLE #define USE_LEVEL_CODE_TABLE
@ -303,31 +291,31 @@ int VP8RecordCoeffs(int ctx, const VP8Residual* const res) {
// should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1 // should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1
proba_t* s = res->stats[n][ctx]; proba_t* s = res->stats[n][ctx];
if (res->last < 0) { if (res->last < 0) {
Record(0, s + 0); VP8RecordStats(0, s + 0);
return 0; return 0;
} }
while (n <= res->last) { while (n <= res->last) {
int v; int v;
Record(1, s + 0); // order of record doesn't matter VP8RecordStats(1, s + 0); // order of record doesn't matter
while ((v = res->coeffs[n++]) == 0) { while ((v = res->coeffs[n++]) == 0) {
Record(0, s + 1); VP8RecordStats(0, s + 1);
s = res->stats[VP8EncBands[n]][0]; s = res->stats[VP8EncBands[n]][0];
} }
Record(1, s + 1); VP8RecordStats(1, s + 1);
if (!Record(2u < (unsigned int)(v + 1), s + 2)) { // v = -1 or 1 if (!VP8RecordStats(2u < (unsigned int)(v + 1), s + 2)) { // v = -1 or 1
s = res->stats[VP8EncBands[n]][1]; s = res->stats[VP8EncBands[n]][1];
} else { } else {
v = abs(v); v = abs(v);
#if !defined(USE_LEVEL_CODE_TABLE) #if !defined(USE_LEVEL_CODE_TABLE)
if (!Record(v > 4, s + 3)) { if (!VP8RecordStats(v > 4, s + 3)) {
if (Record(v != 2, s + 4)) if (VP8RecordStats(v != 2, s + 4))
Record(v == 4, s + 5); VP8RecordStats(v == 4, s + 5);
} else if (!Record(v > 10, s + 6)) { } else if (!VP8RecordStats(v > 10, s + 6)) {
Record(v > 6, s + 7); VP8RecordStats(v > 6, s + 7);
} else if (!Record((v >= 3 + (8 << 2)), s + 8)) { } else if (!VP8RecordStats((v >= 3 + (8 << 2)), s + 8)) {
Record((v >= 3 + (8 << 1)), s + 9); VP8RecordStats((v >= 3 + (8 << 1)), s + 9);
} else { } else {
Record((v >= 3 + (8 << 3)), s + 10); VP8RecordStats((v >= 3 + (8 << 3)), s + 10);
} }
#else #else
if (v > MAX_VARIABLE_LEVEL) { if (v > MAX_VARIABLE_LEVEL) {
@ -340,14 +328,14 @@ int VP8RecordCoeffs(int ctx, const VP8Residual* const res) {
int i; int i;
for (i = 0; (pattern >>= 1) != 0; ++i) { for (i = 0; (pattern >>= 1) != 0; ++i) {
const int mask = 2 << i; const int mask = 2 << i;
if (pattern & 1) Record(!!(bits & mask), s + 3 + i); if (pattern & 1) VP8RecordStats(!!(bits & mask), s + 3 + i);
} }
} }
#endif #endif
s = res->stats[VP8EncBands[n]][2]; s = res->stats[VP8EncBands[n]][2];
} }
} }
if (n < 16) Record(0, s + 0); if (n < 16) VP8RecordStats(0, s + 0);
return 1; return 1;
} }

View File

@ -41,6 +41,18 @@ void VP8InitResidual(int first, int coeff_type,
int VP8RecordCoeffs(int ctx, const VP8Residual* const res); int VP8RecordCoeffs(int ctx, const VP8Residual* const res);
// Record proba context used.
static WEBP_INLINE int VP8RecordStats(int bit, proba_t* const stats) {
proba_t p = *stats;
if (p >= 0xffff0000u) { // an overflow is inbound.
p = ((p + 1u) >> 1) & 0x7fff7fffu; // -> divide the stats by 2.
}
// record bit count (lower 16 bits) and increment total count (upper 16 bits).
p += 0x00010000u + bit;
*stats = p;
return bit;
}
// Cost of coding one event with probability 'proba'. // Cost of coding one event with probability 'proba'.
static WEBP_INLINE int VP8BitCost(int bit, uint8_t proba) { static WEBP_INLINE int VP8BitCost(int bit, uint8_t proba) {
return !bit ? VP8EntropyCost[proba] : VP8EntropyCost[255 - proba]; return !bit ? VP8EntropyCost[proba] : VP8EntropyCost[255 - proba];

View File

@ -406,9 +406,7 @@ static int RecordTokens(VP8EncIterator* const it, const VP8ModeScore* const rd,
VP8InitResidual(0, 1, enc, &res); VP8InitResidual(0, 1, enc, &res);
VP8SetResidualCoeffs(rd->y_dc_levels, &res); VP8SetResidualCoeffs(rd->y_dc_levels, &res);
it->top_nz_[8] = it->left_nz_[8] = it->top_nz_[8] = it->left_nz_[8] =
VP8RecordCoeffTokens(ctx, 1, VP8RecordCoeffTokens(ctx, &res, tokens);
res.first, res.last, res.coeffs, tokens);
VP8RecordCoeffs(ctx, &res);
VP8InitResidual(1, 0, enc, &res); VP8InitResidual(1, 0, enc, &res);
} else { } else {
VP8InitResidual(0, 3, enc, &res); VP8InitResidual(0, 3, enc, &res);
@ -420,9 +418,7 @@ static int RecordTokens(VP8EncIterator* const it, const VP8ModeScore* const rd,
const int ctx = it->top_nz_[x] + it->left_nz_[y]; const int ctx = it->top_nz_[x] + it->left_nz_[y];
VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res); VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res);
it->top_nz_[x] = it->left_nz_[y] = it->top_nz_[x] = it->left_nz_[y] =
VP8RecordCoeffTokens(ctx, res.coeff_type, VP8RecordCoeffTokens(ctx, &res, tokens);
res.first, res.last, res.coeffs, tokens);
VP8RecordCoeffs(ctx, &res);
} }
} }
@ -434,9 +430,7 @@ static int RecordTokens(VP8EncIterator* const it, const VP8ModeScore* const rd,
const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y]; const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y];
VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res); VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res);
it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] = it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] =
VP8RecordCoeffTokens(ctx, 2, VP8RecordCoeffTokens(ctx, &res, tokens);
res.first, res.last, res.coeffs, tokens);
VP8RecordCoeffs(ctx, &res);
} }
} }
} }

View File

@ -87,14 +87,16 @@ static int TBufferNewPage(VP8TBuffer* const b) {
#define TOKEN_ID(t, b, ctx) \ #define TOKEN_ID(t, b, ctx) \
(NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t)))) (NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))
static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,
uint32_t bit, uint32_t proba_idx) { uint32_t proba_idx,
proba_t* const stats) {
assert(proba_idx < FIXED_PROBA_BIT); assert(proba_idx < FIXED_PROBA_BIT);
assert(bit <= 1); assert(bit <= 1);
if (b->left_ > 0 || TBufferNewPage(b)) { if (b->left_ > 0 || TBufferNewPage(b)) {
const int slot = --b->left_; const int slot = --b->left_;
b->tokens_[slot] = (bit << 15) | proba_idx; b->tokens_[slot] = (bit << 15) | proba_idx;
} }
VP8RecordStats(bit, stats);
return bit; return bit;
} }
@ -108,13 +110,16 @@ static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b,
} }
} }
int VP8RecordCoeffTokens(const int ctx, const int coeff_type, int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
int first, int last,
const int16_t* const coeffs,
VP8TBuffer* const tokens) { VP8TBuffer* const tokens) {
int n = first; const int16_t* const coeffs = res->coeffs;
const int coeff_type = res->coeff_type;
const int last = res->last;
int n = res->first;
uint32_t base_id = TOKEN_ID(coeff_type, n, ctx); uint32_t base_id = TOKEN_ID(coeff_type, n, ctx);
if (!AddToken(tokens, last >= 0, base_id + 0)) { // should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1
proba_t* s = res->stats[n][ctx];
if (!AddToken(tokens, last >= 0, base_id + 0, s + 0)) {
return 0; return 0;
} }
@ -122,18 +127,20 @@ int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
const int c = coeffs[n++]; const int c = coeffs[n++];
const int sign = c < 0; const int sign = c < 0;
const uint32_t v = sign ? -c : c; const uint32_t v = sign ? -c : c;
if (!AddToken(tokens, v != 0, base_id + 1)) { if (!AddToken(tokens, v != 0, base_id + 1, s + 1)) {
base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0); // ctx=0 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0); // ctx=0
s = res->stats[VP8EncBands[n]][0];
continue; continue;
} }
if (!AddToken(tokens, v > 1, base_id + 2)) { if (!AddToken(tokens, v > 1, base_id + 2, s + 2)) {
base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1); // ctx=1 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1); // ctx=1
s = res->stats[VP8EncBands[n]][1];
} else { } else {
if (!AddToken(tokens, v > 4, base_id + 3)) { if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) {
if (AddToken(tokens, v != 2, base_id + 4)) if (AddToken(tokens, v != 2, base_id + 4, s + 4))
AddToken(tokens, v == 4, base_id + 5); AddToken(tokens, v == 4, base_id + 5, s + 5);
} else if (!AddToken(tokens, v > 10, base_id + 6)) { } else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) {
if (!AddToken(tokens, v > 6, base_id + 7)) { if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) {
AddConstantToken(tokens, v == 6, 159); AddConstantToken(tokens, v == 6, 159);
} else { } else {
AddConstantToken(tokens, v >= 9, 165); AddConstantToken(tokens, v >= 9, 165);
@ -144,26 +151,26 @@ int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
const uint8_t* tab; const uint8_t* tab;
uint32_t residue = v - 3; uint32_t residue = v - 3;
if (residue < (8 << 1)) { // VP8Cat3 (3b) if (residue < (8 << 1)) { // VP8Cat3 (3b)
AddToken(tokens, 0, base_id + 8); AddToken(tokens, 0, base_id + 8, s + 8);
AddToken(tokens, 0, base_id + 9); AddToken(tokens, 0, base_id + 9, s + 9);
residue -= (8 << 0); residue -= (8 << 0);
mask = 1 << 2; mask = 1 << 2;
tab = VP8Cat3; tab = VP8Cat3;
} else if (residue < (8 << 2)) { // VP8Cat4 (4b) } else if (residue < (8 << 2)) { // VP8Cat4 (4b)
AddToken(tokens, 0, base_id + 8); AddToken(tokens, 0, base_id + 8, s + 8);
AddToken(tokens, 1, base_id + 9); AddToken(tokens, 1, base_id + 9, s + 9);
residue -= (8 << 1); residue -= (8 << 1);
mask = 1 << 3; mask = 1 << 3;
tab = VP8Cat4; tab = VP8Cat4;
} else if (residue < (8 << 3)) { // VP8Cat5 (5b) } else if (residue < (8 << 3)) { // VP8Cat5 (5b)
AddToken(tokens, 1, base_id + 8); AddToken(tokens, 1, base_id + 8, s + 8);
AddToken(tokens, 0, base_id + 10); AddToken(tokens, 0, base_id + 10, s + 9);
residue -= (8 << 2); residue -= (8 << 2);
mask = 1 << 4; mask = 1 << 4;
tab = VP8Cat5; tab = VP8Cat5;
} else { // VP8Cat6 (11b) } else { // VP8Cat6 (11b)
AddToken(tokens, 1, base_id + 8); AddToken(tokens, 1, base_id + 8, s + 8);
AddToken(tokens, 1, base_id + 10); AddToken(tokens, 1, base_id + 10, s + 9);
residue -= (8 << 3); residue -= (8 << 3);
mask = 1 << 10; mask = 1 << 10;
tab = VP8Cat6; tab = VP8Cat6;
@ -174,9 +181,10 @@ int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
} }
} }
base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2
s = res->stats[VP8EncBands[n]][2];
} }
AddConstantToken(tokens, sign, 128); AddConstantToken(tokens, sign, 128);
if (n == 16 || !AddToken(tokens, n <= last, base_id + 0)) { if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) {
return 1; // EOB return 1; // EOB
} }
} }

View File

@ -325,9 +325,7 @@ int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,
const uint8_t* const probas, int final_pass); const uint8_t* const probas, int final_pass);
// record the coding of coefficients without knowing the probabilities yet // record the coding of coefficients without knowing the probabilities yet
int VP8RecordCoeffTokens(const int ctx, const int coeff_type, int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
int first, int last,
const int16_t* const coeffs,
VP8TBuffer* const tokens); VP8TBuffer* const tokens);
// Estimate the final coded size given a set of 'probas'. // Estimate the final coded size given a set of 'probas'.