diff --git a/src/enc/token.c b/src/enc/token.c index 6a63371f..4c4333a4 100644 --- a/src/enc/token.c +++ b/src/enc/token.c @@ -20,6 +20,7 @@ #include #include +#include "./cost.h" #include "./vp8enci.h" #if defined(__cplusplus) || defined(c_plusplus) @@ -238,6 +239,29 @@ int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw, return 1; } +// Size estimation +size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas) { + size_t size = 0; + const VP8Tokens* p = b->pages_; + if (b->error_) return 0; + while (p != NULL) { + const VP8Tokens* const next = p->next_; + const int N = (next == NULL) ? b->left_ : 0; + int n = MAX_NUM_TOKEN; + while (n-- > N) { + const uint16_t token = p->tokens_[n]; + const int bit = token & (1 << 15); + if (token & FIXED_PROBA_BIT) { + size += VP8BitCost(bit, token & 0xffu); + } else { + size += VP8BitCost(bit, probas[token & 0x3fffu]); + } + } + p = next; + } + return size; +} + //------------------------------------------------------------------------------ #else // DISABLE_TOKEN_BUFFER diff --git a/src/enc/vp8enci.h b/src/enc/vp8enci.h index 8505e516..da333732 100644 --- a/src/enc/vp8enci.h +++ b/src/enc/vp8enci.h @@ -379,6 +379,9 @@ int VP8RecordCoeffTokens(int ctx, int coeff_type, int first, int last, const int16_t* const coeffs, VP8TBuffer* const tokens); +// Estimate the final coded size given a set of 'probas'. +size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas); + // unused for now void VP8TokenToStats(const VP8TBuffer* const b, proba_t* const stats);