mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
295 lines
9.1 KiB
295 lines
9.1 KiB
12 years ago
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
||
|
//
|
||
12 years ago
|
// 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.
|
||
12 years ago
|
// -----------------------------------------------------------------------------
|
||
|
//
|
||
|
// 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).
|
||
|
//
|
||
|
// Author: Skal (pascal.massimino@gmail.com)
|
||
|
|
||
|
#include <assert.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
8 years ago
|
#include "./cost_enc.h"
|
||
|
#include "./vp8i_enc.h"
|
||
|
#include "../utils/utils.h"
|
||
12 years ago
|
|
||
|
#if !defined(DISABLE_TOKEN_BUFFER)
|
||
|
|
||
|
// we use pages to reduce the number of memcpy()
|
||
8 years ago
|
#define MIN_PAGE_SIZE 8192 // minimum number of token per page
|
||
12 years ago
|
#define FIXED_PROBA_BIT (1u << 14)
|
||
|
|
||
8 years ago
|
typedef uint16_t token_t; // bit #15: bit value
|
||
|
// bit #14: flags for constant proba or idx
|
||
|
// bits #0..13: slot or constant proba
|
||
12 years ago
|
struct VP8Tokens {
|
||
8 years ago
|
VP8Tokens* next_; // pointer to next page
|
||
12 years ago
|
};
|
||
8 years ago
|
// Token data is located in memory just after the next_ field.
|
||
|
// This macro is used to return their address and hide the trick.
|
||
|
#define TOKEN_DATA(p) ((const token_t*)&(p)[1])
|
||
12 years ago
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
8 years ago
|
void VP8TBufferInit(VP8TBuffer* const b, int page_size) {
|
||
12 years ago
|
b->tokens_ = NULL;
|
||
|
b->pages_ = NULL;
|
||
|
b->last_page_ = &b->pages_;
|
||
|
b->left_ = 0;
|
||
8 years ago
|
b->page_size_ = (page_size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : page_size;
|
||
12 years ago
|
b->error_ = 0;
|
||
|
}
|
||
|
|
||
|
void VP8TBufferClear(VP8TBuffer* const b) {
|
||
|
if (b != NULL) {
|
||
8 years ago
|
VP8Tokens* p = b->pages_;
|
||
12 years ago
|
while (p != NULL) {
|
||
8 years ago
|
VP8Tokens* const next = p->next_;
|
||
|
WebPSafeFree(p);
|
||
12 years ago
|
p = next;
|
||
|
}
|
||
8 years ago
|
VP8TBufferInit(b, b->page_size_);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
static int TBufferNewPage(VP8TBuffer* const b) {
|
||
8 years ago
|
VP8Tokens* page = NULL;
|
||
|
if (!b->error_) {
|
||
|
const size_t size = sizeof(*page) + b->page_size_ * sizeof(token_t);
|
||
|
page = (VP8Tokens*)WebPSafeMalloc(1ULL, size);
|
||
|
}
|
||
12 years ago
|
if (page == NULL) {
|
||
|
b->error_ = 1;
|
||
|
return 0;
|
||
|
}
|
||
8 years ago
|
page->next_ = NULL;
|
||
|
|
||
12 years ago
|
*b->last_page_ = page;
|
||
|
b->last_page_ = &page->next_;
|
||
8 years ago
|
b->left_ = b->page_size_;
|
||
|
b->tokens_ = (token_t*)TOKEN_DATA(page);
|
||
12 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
8 years ago
|
#define TOKEN_ID(t, b, ctx) \
|
||
|
(NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))
|
||
12 years ago
|
|
||
8 years ago
|
static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,
|
||
|
uint32_t proba_idx,
|
||
|
proba_t* const stats) {
|
||
12 years ago
|
assert(proba_idx < FIXED_PROBA_BIT);
|
||
8 years ago
|
assert(bit <= 1);
|
||
12 years ago
|
if (b->left_ > 0 || TBufferNewPage(b)) {
|
||
|
const int slot = --b->left_;
|
||
|
b->tokens_[slot] = (bit << 15) | proba_idx;
|
||
|
}
|
||
8 years ago
|
VP8RecordStats(bit, stats);
|
||
12 years ago
|
return bit;
|
||
|
}
|
||
|
|
||
|
static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b,
|
||
8 years ago
|
uint32_t bit, uint32_t proba) {
|
||
12 years ago
|
assert(proba < 256);
|
||
8 years ago
|
assert(bit <= 1);
|
||
12 years ago
|
if (b->left_ > 0 || TBufferNewPage(b)) {
|
||
|
const int slot = --b->left_;
|
||
|
b->tokens_[slot] = (bit << 15) | FIXED_PROBA_BIT | proba;
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
|
||
12 years ago
|
VP8TBuffer* const tokens) {
|
||
8 years ago
|
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);
|
||
|
// 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)) {
|
||
12 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
|
while (n < 16) {
|
||
|
const int c = coeffs[n++];
|
||
|
const int sign = c < 0;
|
||
8 years ago
|
const uint32_t v = sign ? -c : c;
|
||
|
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];
|
||
12 years ago
|
continue;
|
||
|
}
|
||
8 years ago
|
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];
|
||
12 years ago
|
} else {
|
||
8 years ago
|
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)) {
|
||
12 years ago
|
AddConstantToken(tokens, v == 6, 159);
|
||
|
} else {
|
||
|
AddConstantToken(tokens, v >= 9, 165);
|
||
|
AddConstantToken(tokens, !(v & 1), 145);
|
||
|
}
|
||
|
} else {
|
||
|
int mask;
|
||
|
const uint8_t* tab;
|
||
8 years ago
|
uint32_t residue = v - 3;
|
||
|
if (residue < (8 << 1)) { // VP8Cat3 (3b)
|
||
|
AddToken(tokens, 0, base_id + 8, s + 8);
|
||
|
AddToken(tokens, 0, base_id + 9, s + 9);
|
||
|
residue -= (8 << 0);
|
||
12 years ago
|
mask = 1 << 2;
|
||
|
tab = VP8Cat3;
|
||
8 years ago
|
} else if (residue < (8 << 2)) { // VP8Cat4 (4b)
|
||
|
AddToken(tokens, 0, base_id + 8, s + 8);
|
||
|
AddToken(tokens, 1, base_id + 9, s + 9);
|
||
|
residue -= (8 << 1);
|
||
12 years ago
|
mask = 1 << 3;
|
||
|
tab = VP8Cat4;
|
||
8 years ago
|
} else if (residue < (8 << 3)) { // VP8Cat5 (5b)
|
||
|
AddToken(tokens, 1, base_id + 8, s + 8);
|
||
|
AddToken(tokens, 0, base_id + 10, s + 9);
|
||
|
residue -= (8 << 2);
|
||
12 years ago
|
mask = 1 << 4;
|
||
|
tab = VP8Cat5;
|
||
|
} else { // VP8Cat6 (11b)
|
||
8 years ago
|
AddToken(tokens, 1, base_id + 8, s + 8);
|
||
|
AddToken(tokens, 1, base_id + 10, s + 9);
|
||
|
residue -= (8 << 3);
|
||
12 years ago
|
mask = 1 << 10;
|
||
|
tab = VP8Cat6;
|
||
|
}
|
||
|
while (mask) {
|
||
8 years ago
|
AddConstantToken(tokens, !!(residue & mask), *tab++);
|
||
12 years ago
|
mask >>= 1;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2
|
||
|
s = res->stats[VP8EncBands[n]][2];
|
||
12 years ago
|
}
|
||
|
AddConstantToken(tokens, sign, 128);
|
||
8 years ago
|
if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) {
|
||
12 years ago
|
return 1; // EOB
|
||
|
}
|
||
|
}
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
#undef TOKEN_ID
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// This function works, but isn't currently used. Saved for later.
|
||
|
|
||
|
#if 0
|
||
|
|
||
|
static void 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;
|
||
|
}
|
||
|
|
||
|
void VP8TokenToStats(const VP8TBuffer* const b, proba_t* const stats) {
|
||
|
const VP8Tokens* p = b->pages_;
|
||
|
while (p != NULL) {
|
||
|
const int N = (p->next_ == NULL) ? b->left_ : 0;
|
||
|
int n = MAX_NUM_TOKEN;
|
||
8 years ago
|
const token_t* const tokens = TOKEN_DATA(p);
|
||
12 years ago
|
while (n-- > N) {
|
||
8 years ago
|
const token_t token = tokens[n];
|
||
12 years ago
|
if (!(token & FIXED_PROBA_BIT)) {
|
||
|
Record((token >> 15) & 1, stats + (token & 0x3fffu));
|
||
|
}
|
||
|
}
|
||
|
p = p->next_;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif // 0
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Final coding pass, with known probabilities
|
||
|
|
||
|
int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,
|
||
|
const uint8_t* const probas, int final_pass) {
|
||
|
const VP8Tokens* p = b->pages_;
|
||
8 years ago
|
assert(!b->error_);
|
||
12 years ago
|
while (p != NULL) {
|
||
|
const VP8Tokens* const next = p->next_;
|
||
|
const int N = (next == NULL) ? b->left_ : 0;
|
||
8 years ago
|
int n = b->page_size_;
|
||
|
const token_t* const tokens = TOKEN_DATA(p);
|
||
12 years ago
|
while (n-- > N) {
|
||
8 years ago
|
const token_t token = tokens[n];
|
||
12 years ago
|
const int bit = (token >> 15) & 1;
|
||
|
if (token & FIXED_PROBA_BIT) {
|
||
|
VP8PutBit(bw, bit, token & 0xffu); // constant proba
|
||
|
} else {
|
||
|
VP8PutBit(bw, bit, probas[token & 0x3fffu]);
|
||
|
}
|
||
|
}
|
||
8 years ago
|
if (final_pass) WebPSafeFree((void*)p);
|
||
12 years ago
|
p = next;
|
||
|
}
|
||
|
if (final_pass) b->pages_ = NULL;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
8 years ago
|
// Size estimation
|
||
|
size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas) {
|
||
|
size_t size = 0;
|
||
|
const VP8Tokens* p = b->pages_;
|
||
|
assert(!b->error_);
|
||
|
while (p != NULL) {
|
||
|
const VP8Tokens* const next = p->next_;
|
||
|
const int N = (next == NULL) ? b->left_ : 0;
|
||
|
int n = b->page_size_;
|
||
|
const token_t* const tokens = TOKEN_DATA(p);
|
||
|
while (n-- > N) {
|
||
|
const token_t token = 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;
|
||
|
}
|
||
|
|
||
12 years ago
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
#else // DISABLE_TOKEN_BUFFER
|
||
|
|
||
|
void VP8TBufferInit(VP8TBuffer* const b) {
|
||
|
(void)b;
|
||
|
}
|
||
|
void VP8TBufferClear(VP8TBuffer* const b) {
|
||
|
(void)b;
|
||
|
}
|
||
|
|
||
|
#endif // !DISABLE_TOKEN_BUFFER
|
||
|
|