mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-27 06:08:21 +01:00
Do token recording and counting in a single loop
Change-Id: I8afd3c486b210bd67888de03e91dde7f78276f89
This commit is contained in:
parent
9ac74f922e
commit
0c0fb83211
@ -281,18 +281,6 @@ int VP8GetCostUV(VP8EncIterator* const it, const VP8ModeScore* const rd) {
|
||||
//------------------------------------------------------------------------------
|
||||
// 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.
|
||||
#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
|
||||
proba_t* s = res->stats[n][ctx];
|
||||
if (res->last < 0) {
|
||||
Record(0, s + 0);
|
||||
VP8RecordStats(0, s + 0);
|
||||
return 0;
|
||||
}
|
||||
while (n <= res->last) {
|
||||
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) {
|
||||
Record(0, s + 1);
|
||||
VP8RecordStats(0, s + 1);
|
||||
s = res->stats[VP8EncBands[n]][0];
|
||||
}
|
||||
Record(1, s + 1);
|
||||
if (!Record(2u < (unsigned int)(v + 1), s + 2)) { // v = -1 or 1
|
||||
VP8RecordStats(1, s + 1);
|
||||
if (!VP8RecordStats(2u < (unsigned int)(v + 1), s + 2)) { // v = -1 or 1
|
||||
s = res->stats[VP8EncBands[n]][1];
|
||||
} else {
|
||||
v = abs(v);
|
||||
#if !defined(USE_LEVEL_CODE_TABLE)
|
||||
if (!Record(v > 4, s + 3)) {
|
||||
if (Record(v != 2, s + 4))
|
||||
Record(v == 4, s + 5);
|
||||
} else if (!Record(v > 10, s + 6)) {
|
||||
Record(v > 6, s + 7);
|
||||
} else if (!Record((v >= 3 + (8 << 2)), s + 8)) {
|
||||
Record((v >= 3 + (8 << 1)), s + 9);
|
||||
if (!VP8RecordStats(v > 4, s + 3)) {
|
||||
if (VP8RecordStats(v != 2, s + 4))
|
||||
VP8RecordStats(v == 4, s + 5);
|
||||
} else if (!VP8RecordStats(v > 10, s + 6)) {
|
||||
VP8RecordStats(v > 6, s + 7);
|
||||
} else if (!VP8RecordStats((v >= 3 + (8 << 2)), s + 8)) {
|
||||
VP8RecordStats((v >= 3 + (8 << 1)), s + 9);
|
||||
} else {
|
||||
Record((v >= 3 + (8 << 3)), s + 10);
|
||||
VP8RecordStats((v >= 3 + (8 << 3)), s + 10);
|
||||
}
|
||||
#else
|
||||
if (v > MAX_VARIABLE_LEVEL) {
|
||||
@ -340,14 +328,14 @@ int VP8RecordCoeffs(int ctx, const VP8Residual* const res) {
|
||||
int i;
|
||||
for (i = 0; (pattern >>= 1) != 0; ++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
|
||||
s = res->stats[VP8EncBands[n]][2];
|
||||
}
|
||||
}
|
||||
if (n < 16) Record(0, s + 0);
|
||||
if (n < 16) VP8RecordStats(0, s + 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -41,6 +41,18 @@ void VP8InitResidual(int first, int coeff_type,
|
||||
|
||||
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'.
|
||||
static WEBP_INLINE int VP8BitCost(int bit, uint8_t proba) {
|
||||
return !bit ? VP8EntropyCost[proba] : VP8EntropyCost[255 - proba];
|
||||
|
@ -406,9 +406,7 @@ static int RecordTokens(VP8EncIterator* const it, const VP8ModeScore* const rd,
|
||||
VP8InitResidual(0, 1, enc, &res);
|
||||
VP8SetResidualCoeffs(rd->y_dc_levels, &res);
|
||||
it->top_nz_[8] = it->left_nz_[8] =
|
||||
VP8RecordCoeffTokens(ctx, 1,
|
||||
res.first, res.last, res.coeffs, tokens);
|
||||
VP8RecordCoeffs(ctx, &res);
|
||||
VP8RecordCoeffTokens(ctx, &res, tokens);
|
||||
VP8InitResidual(1, 0, enc, &res);
|
||||
} else {
|
||||
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];
|
||||
VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res);
|
||||
it->top_nz_[x] = it->left_nz_[y] =
|
||||
VP8RecordCoeffTokens(ctx, res.coeff_type,
|
||||
res.first, res.last, res.coeffs, tokens);
|
||||
VP8RecordCoeffs(ctx, &res);
|
||||
VP8RecordCoeffTokens(ctx, &res, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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];
|
||||
VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res);
|
||||
it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] =
|
||||
VP8RecordCoeffTokens(ctx, 2,
|
||||
res.first, res.last, res.coeffs, tokens);
|
||||
VP8RecordCoeffs(ctx, &res);
|
||||
VP8RecordCoeffTokens(ctx, &res, tokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,14 +87,16 @@ static int TBufferNewPage(VP8TBuffer* const b) {
|
||||
#define TOKEN_ID(t, b, ctx) \
|
||||
(NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))
|
||||
|
||||
static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b,
|
||||
uint32_t bit, uint32_t proba_idx) {
|
||||
static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,
|
||||
uint32_t proba_idx,
|
||||
proba_t* const stats) {
|
||||
assert(proba_idx < FIXED_PROBA_BIT);
|
||||
assert(bit <= 1);
|
||||
if (b->left_ > 0 || TBufferNewPage(b)) {
|
||||
const int slot = --b->left_;
|
||||
b->tokens_[slot] = (bit << 15) | proba_idx;
|
||||
}
|
||||
VP8RecordStats(bit, stats);
|
||||
return bit;
|
||||
}
|
||||
|
||||
@ -108,13 +110,16 @@ static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b,
|
||||
}
|
||||
}
|
||||
|
||||
int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
|
||||
int first, int last,
|
||||
const int16_t* const coeffs,
|
||||
int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -122,18 +127,20 @@ int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
|
||||
const int c = coeffs[n++];
|
||||
const int sign = c < 0;
|
||||
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
|
||||
s = res->stats[VP8EncBands[n]][0];
|
||||
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
|
||||
s = res->stats[VP8EncBands[n]][1];
|
||||
} else {
|
||||
if (!AddToken(tokens, v > 4, base_id + 3)) {
|
||||
if (AddToken(tokens, v != 2, base_id + 4))
|
||||
AddToken(tokens, v == 4, base_id + 5);
|
||||
} else if (!AddToken(tokens, v > 10, base_id + 6)) {
|
||||
if (!AddToken(tokens, v > 6, base_id + 7)) {
|
||||
if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) {
|
||||
if (AddToken(tokens, v != 2, base_id + 4, s + 4))
|
||||
AddToken(tokens, v == 4, base_id + 5, s + 5);
|
||||
} else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) {
|
||||
if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) {
|
||||
AddConstantToken(tokens, v == 6, 159);
|
||||
} else {
|
||||
AddConstantToken(tokens, v >= 9, 165);
|
||||
@ -144,26 +151,26 @@ int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
|
||||
const uint8_t* tab;
|
||||
uint32_t residue = v - 3;
|
||||
if (residue < (8 << 1)) { // VP8Cat3 (3b)
|
||||
AddToken(tokens, 0, base_id + 8);
|
||||
AddToken(tokens, 0, base_id + 9);
|
||||
AddToken(tokens, 0, base_id + 8, s + 8);
|
||||
AddToken(tokens, 0, base_id + 9, s + 9);
|
||||
residue -= (8 << 0);
|
||||
mask = 1 << 2;
|
||||
tab = VP8Cat3;
|
||||
} else if (residue < (8 << 2)) { // VP8Cat4 (4b)
|
||||
AddToken(tokens, 0, base_id + 8);
|
||||
AddToken(tokens, 1, base_id + 9);
|
||||
AddToken(tokens, 0, base_id + 8, s + 8);
|
||||
AddToken(tokens, 1, base_id + 9, s + 9);
|
||||
residue -= (8 << 1);
|
||||
mask = 1 << 3;
|
||||
tab = VP8Cat4;
|
||||
} else if (residue < (8 << 3)) { // VP8Cat5 (5b)
|
||||
AddToken(tokens, 1, base_id + 8);
|
||||
AddToken(tokens, 0, base_id + 10);
|
||||
AddToken(tokens, 1, base_id + 8, s + 8);
|
||||
AddToken(tokens, 0, base_id + 10, s + 9);
|
||||
residue -= (8 << 2);
|
||||
mask = 1 << 4;
|
||||
tab = VP8Cat5;
|
||||
} else { // VP8Cat6 (11b)
|
||||
AddToken(tokens, 1, base_id + 8);
|
||||
AddToken(tokens, 1, base_id + 10);
|
||||
AddToken(tokens, 1, base_id + 8, s + 8);
|
||||
AddToken(tokens, 1, base_id + 10, s + 9);
|
||||
residue -= (8 << 3);
|
||||
mask = 1 << 10;
|
||||
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
|
||||
s = res->stats[VP8EncBands[n]][2];
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -325,9 +325,7 @@ int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,
|
||||
const uint8_t* const probas, int final_pass);
|
||||
|
||||
// record the coding of coefficients without knowing the probabilities yet
|
||||
int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
|
||||
int first, int last,
|
||||
const int16_t* const coeffs,
|
||||
int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
|
||||
VP8TBuffer* const tokens);
|
||||
|
||||
// Estimate the final coded size given a set of 'probas'.
|
||||
|
Loading…
Reference in New Issue
Block a user