Path: blob/master/thirdparty/libwebp/src/enc/token_enc.c
9913 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Paginated token buffer10//11// A 'token' is a bit value associated with a probability, either fixed12// or a later-to-be-determined after statistics have been collected.13// For dynamic probability, we just record the slot id (idx) for the probability14// value in the final probability array (uint8_t* probas in VP8EmitTokens).15//16// Author: Skal ([email protected])1718#include <assert.h>19#include <stdlib.h>20#include <string.h>2122#include "src/enc/cost_enc.h"23#include "src/enc/vp8i_enc.h"24#include "src/utils/utils.h"2526#if !defined(DISABLE_TOKEN_BUFFER)2728// we use pages to reduce the number of memcpy()29#define MIN_PAGE_SIZE 8192 // minimum number of token per page30#define FIXED_PROBA_BIT (1u << 14)3132typedef uint16_t token_t; // bit #15: bit value33// bit #14: flags for constant proba or idx34// bits #0..13: slot or constant proba35struct VP8Tokens {36VP8Tokens* next_; // pointer to next page37};38// Token data is located in memory just after the next_ field.39// This macro is used to return their address and hide the trick.40#define TOKEN_DATA(p) ((const token_t*)&(p)[1])4142//------------------------------------------------------------------------------4344void VP8TBufferInit(VP8TBuffer* const b, int page_size) {45b->tokens_ = NULL;46b->pages_ = NULL;47b->last_page_ = &b->pages_;48b->left_ = 0;49b->page_size_ = (page_size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : page_size;50b->error_ = 0;51}5253void VP8TBufferClear(VP8TBuffer* const b) {54if (b != NULL) {55VP8Tokens* p = b->pages_;56while (p != NULL) {57VP8Tokens* const next = p->next_;58WebPSafeFree(p);59p = next;60}61VP8TBufferInit(b, b->page_size_);62}63}6465static int TBufferNewPage(VP8TBuffer* const b) {66VP8Tokens* page = NULL;67if (!b->error_) {68const size_t size = sizeof(*page) + b->page_size_ * sizeof(token_t);69page = (VP8Tokens*)WebPSafeMalloc(1ULL, size);70}71if (page == NULL) {72b->error_ = 1;73return 0;74}75page->next_ = NULL;7677*b->last_page_ = page;78b->last_page_ = &page->next_;79b->left_ = b->page_size_;80b->tokens_ = (token_t*)TOKEN_DATA(page);81return 1;82}8384//------------------------------------------------------------------------------8586#define TOKEN_ID(t, b, ctx) \87(NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))8889static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,90uint32_t proba_idx,91proba_t* const stats) {92assert(proba_idx < FIXED_PROBA_BIT);93assert(bit <= 1);94if (b->left_ > 0 || TBufferNewPage(b)) {95const int slot = --b->left_;96b->tokens_[slot] = (bit << 15) | proba_idx;97}98VP8RecordStats(bit, stats);99return bit;100}101102static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b,103uint32_t bit, uint32_t proba) {104assert(proba < 256);105assert(bit <= 1);106if (b->left_ > 0 || TBufferNewPage(b)) {107const int slot = --b->left_;108b->tokens_[slot] = (bit << 15) | FIXED_PROBA_BIT | proba;109}110}111112int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,113VP8TBuffer* const tokens) {114const int16_t* const coeffs = res->coeffs;115const int coeff_type = res->coeff_type;116const int last = res->last;117int n = res->first;118uint32_t base_id = TOKEN_ID(coeff_type, n, ctx);119// should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1120proba_t* s = res->stats[n][ctx];121if (!AddToken(tokens, last >= 0, base_id + 0, s + 0)) {122return 0;123}124125while (n < 16) {126const int c = coeffs[n++];127const int sign = c < 0;128const uint32_t v = sign ? -c : c;129if (!AddToken(tokens, v != 0, base_id + 1, s + 1)) {130base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0); // ctx=0131s = res->stats[VP8EncBands[n]][0];132continue;133}134if (!AddToken(tokens, v > 1, base_id + 2, s + 2)) {135base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1); // ctx=1136s = res->stats[VP8EncBands[n]][1];137} else {138if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) {139if (AddToken(tokens, v != 2, base_id + 4, s + 4)) {140AddToken(tokens, v == 4, base_id + 5, s + 5);141}142} else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) {143if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) {144AddConstantToken(tokens, v == 6, 159);145} else {146AddConstantToken(tokens, v >= 9, 165);147AddConstantToken(tokens, !(v & 1), 145);148}149} else {150int mask;151const uint8_t* tab;152uint32_t residue = v - 3;153if (residue < (8 << 1)) { // VP8Cat3 (3b)154AddToken(tokens, 0, base_id + 8, s + 8);155AddToken(tokens, 0, base_id + 9, s + 9);156residue -= (8 << 0);157mask = 1 << 2;158tab = VP8Cat3;159} else if (residue < (8 << 2)) { // VP8Cat4 (4b)160AddToken(tokens, 0, base_id + 8, s + 8);161AddToken(tokens, 1, base_id + 9, s + 9);162residue -= (8 << 1);163mask = 1 << 3;164tab = VP8Cat4;165} else if (residue < (8 << 3)) { // VP8Cat5 (5b)166AddToken(tokens, 1, base_id + 8, s + 8);167AddToken(tokens, 0, base_id + 10, s + 9);168residue -= (8 << 2);169mask = 1 << 4;170tab = VP8Cat5;171} else { // VP8Cat6 (11b)172AddToken(tokens, 1, base_id + 8, s + 8);173AddToken(tokens, 1, base_id + 10, s + 9);174residue -= (8 << 3);175mask = 1 << 10;176tab = VP8Cat6;177}178while (mask) {179AddConstantToken(tokens, !!(residue & mask), *tab++);180mask >>= 1;181}182}183base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2184s = res->stats[VP8EncBands[n]][2];185}186AddConstantToken(tokens, sign, 128);187if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) {188return 1; // EOB189}190}191return 1;192}193194#undef TOKEN_ID195196//------------------------------------------------------------------------------197// Final coding pass, with known probabilities198199int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,200const uint8_t* const probas, int final_pass) {201const VP8Tokens* p = b->pages_;202assert(!b->error_);203while (p != NULL) {204const VP8Tokens* const next = p->next_;205const int N = (next == NULL) ? b->left_ : 0;206int n = b->page_size_;207const token_t* const tokens = TOKEN_DATA(p);208while (n-- > N) {209const token_t token = tokens[n];210const int bit = (token >> 15) & 1;211if (token & FIXED_PROBA_BIT) {212VP8PutBit(bw, bit, token & 0xffu); // constant proba213} else {214VP8PutBit(bw, bit, probas[token & 0x3fffu]);215}216}217if (final_pass) WebPSafeFree((void*)p);218p = next;219}220if (final_pass) b->pages_ = NULL;221return 1;222}223224// Size estimation225size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas) {226size_t size = 0;227const VP8Tokens* p = b->pages_;228assert(!b->error_);229while (p != NULL) {230const VP8Tokens* const next = p->next_;231const int N = (next == NULL) ? b->left_ : 0;232int n = b->page_size_;233const token_t* const tokens = TOKEN_DATA(p);234while (n-- > N) {235const token_t token = tokens[n];236const int bit = token & (1 << 15);237if (token & FIXED_PROBA_BIT) {238size += VP8BitCost(bit, token & 0xffu);239} else {240size += VP8BitCost(bit, probas[token & 0x3fffu]);241}242}243p = next;244}245return size;246}247248//------------------------------------------------------------------------------249250#else // DISABLE_TOKEN_BUFFER251252void VP8TBufferInit(VP8TBuffer* const b, int page_size) {253(void)b;254(void)page_size;255}256void VP8TBufferClear(VP8TBuffer* const b) {257(void)b;258}259260#endif // !DISABLE_TOKEN_BUFFER261262263264