2012-12-03 13:50:14 +01:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2012-12-03 13:50:14 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Paginated token buffer
|
|
|
|
//
|
|
|
|
// A 'token' is a bit value associated with a probability, either fixed
|
|
|
|
// or a later-to-be-determined after statistics have been collected.
|
|
|
|
// For dynamic probability, we just record the slot id (idx) for the probability
|
|
|
|
// value in the final probability array (uint8_t* probas in VP8EmitTokens).
|
2013-03-12 00:37:42 +01:00
|
|
|
//
|
2012-12-03 13:50:14 +01:00
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-10-07 23:15:11 +02:00
|
|
|
#include "src/enc/cost_enc.h"
|
|
|
|
#include "src/enc/vp8i_enc.h"
|
|
|
|
#include "src/utils/utils.h"
|
2012-12-03 13:50:14 +01:00
|
|
|
|
2013-03-12 00:37:42 +01:00
|
|
|
#if !defined(DISABLE_TOKEN_BUFFER)
|
|
|
|
|
|
|
|
// we use pages to reduce the number of memcpy()
|
2014-05-05 23:26:14 +02:00
|
|
|
#define MIN_PAGE_SIZE 8192 // minimum number of token per page
|
2013-03-12 00:37:42 +01:00
|
|
|
#define FIXED_PROBA_BIT (1u << 14)
|
2012-12-03 13:50:14 +01:00
|
|
|
|
2015-03-03 00:48:28 +01:00
|
|
|
typedef uint16_t token_t; // bit #15: bit value
|
|
|
|
// bit #14: flags for constant proba or idx
|
|
|
|
// bits #0..13: slot or constant proba
|
2012-12-03 13:50:14 +01:00
|
|
|
struct VP8Tokens {
|
2014-05-05 23:26:14 +02:00
|
|
|
VP8Tokens* next_; // pointer to next page
|
2012-12-03 13:50:14 +01:00
|
|
|
};
|
2014-05-05 23:26:14 +02:00
|
|
|
// Token data is located in memory just after the next_ field.
|
|
|
|
// This macro is used to return their address and hide the trick.
|
2015-03-03 00:48:28 +01:00
|
|
|
#define TOKEN_DATA(p) ((const token_t*)&(p)[1])
|
2012-12-03 13:50:14 +01:00
|
|
|
|
2013-03-01 09:58:14 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2014-05-05 23:26:14 +02:00
|
|
|
void VP8TBufferInit(VP8TBuffer* const b, int page_size) {
|
2012-12-03 13:50:14 +01:00
|
|
|
b->tokens_ = NULL;
|
|
|
|
b->pages_ = NULL;
|
|
|
|
b->last_page_ = &b->pages_;
|
|
|
|
b->left_ = 0;
|
2014-05-05 23:26:14 +02:00
|
|
|
b->page_size_ = (page_size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : page_size;
|
2012-12-03 13:50:14 +01:00
|
|
|
b->error_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VP8TBufferClear(VP8TBuffer* const b) {
|
|
|
|
if (b != NULL) {
|
2015-02-05 04:56:34 +01:00
|
|
|
VP8Tokens* p = b->pages_;
|
2012-12-03 13:50:14 +01:00
|
|
|
while (p != NULL) {
|
2015-02-05 04:56:34 +01:00
|
|
|
VP8Tokens* const next = p->next_;
|
|
|
|
WebPSafeFree(p);
|
2012-12-03 13:50:14 +01:00
|
|
|
p = next;
|
|
|
|
}
|
2014-05-05 23:26:14 +02:00
|
|
|
VP8TBufferInit(b, b->page_size_);
|
2012-12-03 13:50:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int TBufferNewPage(VP8TBuffer* const b) {
|
2014-05-05 23:26:14 +02:00
|
|
|
VP8Tokens* page = NULL;
|
|
|
|
if (!b->error_) {
|
2015-03-03 00:48:28 +01:00
|
|
|
const size_t size = sizeof(*page) + b->page_size_ * sizeof(token_t);
|
2014-05-05 23:26:14 +02:00
|
|
|
page = (VP8Tokens*)WebPSafeMalloc(1ULL, size);
|
|
|
|
}
|
2012-12-03 13:50:14 +01:00
|
|
|
if (page == NULL) {
|
|
|
|
b->error_ = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-05 23:26:14 +02:00
|
|
|
page->next_ = NULL;
|
|
|
|
|
2013-03-01 09:58:14 +01:00
|
|
|
*b->last_page_ = page;
|
2012-12-03 13:50:14 +01:00
|
|
|
b->last_page_ = &page->next_;
|
2014-05-05 23:26:14 +02:00
|
|
|
b->left_ = b->page_size_;
|
2015-03-03 00:48:28 +01:00
|
|
|
b->tokens_ = (token_t*)TOKEN_DATA(page);
|
2012-12-03 13:50:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-03-03 00:48:28 +01:00
|
|
|
#define TOKEN_ID(t, b, ctx) \
|
|
|
|
(NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))
|
2012-12-03 13:50:14 +01:00
|
|
|
|
2016-07-20 01:28:26 +02:00
|
|
|
static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,
|
|
|
|
uint32_t proba_idx,
|
|
|
|
proba_t* const stats) {
|
2013-03-12 00:37:42 +01:00
|
|
|
assert(proba_idx < FIXED_PROBA_BIT);
|
2015-03-03 00:48:28 +01:00
|
|
|
assert(bit <= 1);
|
2012-12-03 13:50:14 +01:00
|
|
|
if (b->left_ > 0 || TBufferNewPage(b)) {
|
|
|
|
const int slot = --b->left_;
|
2013-03-12 00:37:42 +01:00
|
|
|
b->tokens_[slot] = (bit << 15) | proba_idx;
|
2012-12-03 13:50:14 +01:00
|
|
|
}
|
2016-07-20 01:28:26 +02:00
|
|
|
VP8RecordStats(bit, stats);
|
2012-12-03 13:50:14 +01:00
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
2013-03-12 00:37:42 +01:00
|
|
|
static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b,
|
2015-03-03 00:48:28 +01:00
|
|
|
uint32_t bit, uint32_t proba) {
|
2012-12-03 13:50:14 +01:00
|
|
|
assert(proba < 256);
|
2015-03-03 00:48:28 +01:00
|
|
|
assert(bit <= 1);
|
2012-12-03 13:50:14 +01:00
|
|
|
if (b->left_ > 0 || TBufferNewPage(b)) {
|
|
|
|
const int slot = --b->left_;
|
2013-03-12 00:37:42 +01:00
|
|
|
b->tokens_[slot] = (bit << 15) | FIXED_PROBA_BIT | proba;
|
2012-12-03 13:50:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 01:28:26 +02:00
|
|
|
int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
|
2013-03-12 00:37:42 +01:00
|
|
|
VP8TBuffer* const tokens) {
|
2016-07-20 01:28:26 +02:00
|
|
|
const int16_t* const coeffs = res->coeffs;
|
|
|
|
const int coeff_type = res->coeff_type;
|
|
|
|
const int last = res->last;
|
|
|
|
int n = res->first;
|
2015-03-03 00:48:28 +01:00
|
|
|
uint32_t base_id = TOKEN_ID(coeff_type, n, ctx);
|
2016-07-20 01:28:26 +02:00
|
|
|
// 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)) {
|
2012-12-03 13:50:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n < 16) {
|
|
|
|
const int c = coeffs[n++];
|
|
|
|
const int sign = c < 0;
|
2015-03-03 00:48:28 +01:00
|
|
|
const uint32_t v = sign ? -c : c;
|
2016-07-20 01:28:26 +02:00
|
|
|
if (!AddToken(tokens, v != 0, base_id + 1, s + 1)) {
|
2015-03-03 00:48:28 +01:00
|
|
|
base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0); // ctx=0
|
2016-07-20 01:28:26 +02:00
|
|
|
s = res->stats[VP8EncBands[n]][0];
|
2012-12-03 13:50:14 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-07-20 01:28:26 +02:00
|
|
|
if (!AddToken(tokens, v > 1, base_id + 2, s + 2)) {
|
2015-03-03 00:48:28 +01:00
|
|
|
base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1); // ctx=1
|
2016-07-20 01:28:26 +02:00
|
|
|
s = res->stats[VP8EncBands[n]][1];
|
2012-12-03 13:50:14 +01:00
|
|
|
} else {
|
2016-07-20 01:28:26 +02:00
|
|
|
if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) {
|
2016-08-04 03:53:40 +02:00
|
|
|
if (AddToken(tokens, v != 2, base_id + 4, s + 4)) {
|
2016-07-20 01:28:26 +02:00
|
|
|
AddToken(tokens, v == 4, base_id + 5, s + 5);
|
2016-08-04 03:53:40 +02:00
|
|
|
}
|
2016-07-20 01:28:26 +02:00
|
|
|
} else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) {
|
|
|
|
if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) {
|
2013-03-12 00:37:42 +01:00
|
|
|
AddConstantToken(tokens, v == 6, 159);
|
2012-12-03 13:50:14 +01:00
|
|
|
} else {
|
2013-03-12 00:37:42 +01:00
|
|
|
AddConstantToken(tokens, v >= 9, 165);
|
|
|
|
AddConstantToken(tokens, !(v & 1), 145);
|
2012-12-03 13:50:14 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int mask;
|
|
|
|
const uint8_t* tab;
|
2015-03-03 00:48:28 +01:00
|
|
|
uint32_t residue = v - 3;
|
|
|
|
if (residue < (8 << 1)) { // VP8Cat3 (3b)
|
2016-07-20 01:28:26 +02:00
|
|
|
AddToken(tokens, 0, base_id + 8, s + 8);
|
|
|
|
AddToken(tokens, 0, base_id + 9, s + 9);
|
2015-03-03 00:48:28 +01:00
|
|
|
residue -= (8 << 0);
|
2012-12-03 13:50:14 +01:00
|
|
|
mask = 1 << 2;
|
|
|
|
tab = VP8Cat3;
|
2015-03-03 00:48:28 +01:00
|
|
|
} else if (residue < (8 << 2)) { // VP8Cat4 (4b)
|
2016-07-20 01:28:26 +02:00
|
|
|
AddToken(tokens, 0, base_id + 8, s + 8);
|
|
|
|
AddToken(tokens, 1, base_id + 9, s + 9);
|
2015-03-03 00:48:28 +01:00
|
|
|
residue -= (8 << 1);
|
2012-12-03 13:50:14 +01:00
|
|
|
mask = 1 << 3;
|
|
|
|
tab = VP8Cat4;
|
2015-03-03 00:48:28 +01:00
|
|
|
} else if (residue < (8 << 3)) { // VP8Cat5 (5b)
|
2016-07-20 01:28:26 +02:00
|
|
|
AddToken(tokens, 1, base_id + 8, s + 8);
|
|
|
|
AddToken(tokens, 0, base_id + 10, s + 9);
|
2015-03-03 00:48:28 +01:00
|
|
|
residue -= (8 << 2);
|
2012-12-03 13:50:14 +01:00
|
|
|
mask = 1 << 4;
|
|
|
|
tab = VP8Cat5;
|
|
|
|
} else { // VP8Cat6 (11b)
|
2016-07-20 01:28:26 +02:00
|
|
|
AddToken(tokens, 1, base_id + 8, s + 8);
|
|
|
|
AddToken(tokens, 1, base_id + 10, s + 9);
|
2015-03-03 00:48:28 +01:00
|
|
|
residue -= (8 << 3);
|
2012-12-03 13:50:14 +01:00
|
|
|
mask = 1 << 10;
|
|
|
|
tab = VP8Cat6;
|
|
|
|
}
|
|
|
|
while (mask) {
|
2015-03-03 00:48:28 +01:00
|
|
|
AddConstantToken(tokens, !!(residue & mask), *tab++);
|
2012-12-03 13:50:14 +01:00
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 00:48:28 +01:00
|
|
|
base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2
|
2016-07-20 01:28:26 +02:00
|
|
|
s = res->stats[VP8EncBands[n]][2];
|
2012-12-03 13:50:14 +01:00
|
|
|
}
|
2013-03-12 00:37:42 +01:00
|
|
|
AddConstantToken(tokens, sign, 128);
|
2016-07-20 01:28:26 +02:00
|
|
|
if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) {
|
2012-12-03 13:50:14 +01:00
|
|
|
return 1; // EOB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TOKEN_ID
|
|
|
|
|
2013-03-12 00:37:42 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Final coding pass, with known probabilities
|
|
|
|
|
|
|
|
int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,
|
2012-12-03 13:50:14 +01:00
|
|
|
const uint8_t* const probas, int final_pass) {
|
2013-03-01 09:58:14 +01:00
|
|
|
const VP8Tokens* p = b->pages_;
|
2014-06-13 08:45:12 +02:00
|
|
|
assert(!b->error_);
|
2012-12-03 13:50:14 +01:00
|
|
|
while (p != NULL) {
|
|
|
|
const VP8Tokens* const next = p->next_;
|
|
|
|
const int N = (next == NULL) ? b->left_ : 0;
|
2014-05-05 23:26:14 +02:00
|
|
|
int n = b->page_size_;
|
|
|
|
const token_t* const tokens = TOKEN_DATA(p);
|
2012-12-03 13:50:14 +01:00
|
|
|
while (n-- > N) {
|
2014-05-05 23:26:14 +02:00
|
|
|
const token_t token = tokens[n];
|
2013-03-12 00:37:42 +01:00
|
|
|
const int bit = (token >> 15) & 1;
|
|
|
|
if (token & FIXED_PROBA_BIT) {
|
|
|
|
VP8PutBit(bw, bit, token & 0xffu); // constant proba
|
2012-12-03 13:50:14 +01:00
|
|
|
} else {
|
2013-03-12 00:37:42 +01:00
|
|
|
VP8PutBit(bw, bit, probas[token & 0x3fffu]);
|
2012-12-03 13:50:14 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-27 23:27:32 +01:00
|
|
|
if (final_pass) WebPSafeFree((void*)p);
|
2012-12-03 13:50:14 +01:00
|
|
|
p = next;
|
|
|
|
}
|
2013-03-12 00:37:42 +01:00
|
|
|
if (final_pass) b->pages_ = NULL;
|
2012-12-03 13:50:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-09-11 10:08:49 +02:00
|
|
|
// Size estimation
|
|
|
|
size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas) {
|
|
|
|
size_t size = 0;
|
|
|
|
const VP8Tokens* p = b->pages_;
|
2014-06-13 08:45:12 +02:00
|
|
|
assert(!b->error_);
|
2013-09-11 10:08:49 +02:00
|
|
|
while (p != NULL) {
|
|
|
|
const VP8Tokens* const next = p->next_;
|
|
|
|
const int N = (next == NULL) ? b->left_ : 0;
|
2014-05-05 23:26:14 +02:00
|
|
|
int n = b->page_size_;
|
|
|
|
const token_t* const tokens = TOKEN_DATA(p);
|
2013-09-11 10:08:49 +02:00
|
|
|
while (n-- > N) {
|
2014-05-05 23:26:14 +02:00
|
|
|
const token_t token = tokens[n];
|
2013-09-11 10:08:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-03 13:50:14 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2013-03-12 00:37:42 +01:00
|
|
|
|
|
|
|
#else // DISABLE_TOKEN_BUFFER
|
2013-03-01 09:58:14 +01:00
|
|
|
|
2017-07-01 23:46:28 +02:00
|
|
|
void VP8TBufferInit(VP8TBuffer* const b, int page_size) {
|
2013-03-01 09:58:14 +01:00
|
|
|
(void)b;
|
2017-07-01 23:46:28 +02:00
|
|
|
(void)page_size;
|
2013-03-01 09:58:14 +01:00
|
|
|
}
|
|
|
|
void VP8TBufferClear(VP8TBuffer* const b) {
|
|
|
|
(void)b;
|
|
|
|
}
|
2012-12-03 13:50:14 +01:00
|
|
|
|
2013-03-12 00:37:42 +01:00
|
|
|
#endif // !DISABLE_TOKEN_BUFFER
|
2012-12-03 13:50:14 +01:00
|
|
|
|