Path: blob/master/thirdparty/libwebp/src/enc/token_enc.c
21550 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/dec/common_dec.h"23#include "src/dsp/dsp.h"24#include "src/enc/cost_enc.h"25#include "src/enc/vp8i_enc.h"26#include "src/utils/bit_writer_utils.h"27#include "src/utils/utils.h"28#include "src/webp/types.h"2930#if !defined(DISABLE_TOKEN_BUFFER)3132// we use pages to reduce the number of memcpy()33#define MIN_PAGE_SIZE 8192 // minimum number of token per page34#define FIXED_PROBA_BIT (1u << 14)3536typedef uint16_t token_t; // bit #15: bit value37// bit #14: flags for constant proba or idx38// bits #0..13: slot or constant proba39struct VP8Tokens {40VP8Tokens* next; // pointer to next page41};42// Token data is located in memory just after the 'next' field.43// This macro is used to return their address and hide the trick.44#define TOKEN_DATA(p) ((const token_t*)&(p)[1])4546//------------------------------------------------------------------------------4748void VP8TBufferInit(VP8TBuffer* const b, int page_size) {49b->tokens = NULL;50b->pages = NULL;51b->last_page = &b->pages;52b->left = 0;53b->page_size = (page_size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : page_size;54b->error = 0;55}5657void VP8TBufferClear(VP8TBuffer* const b) {58if (b != NULL) {59VP8Tokens* p = b->pages;60while (p != NULL) {61VP8Tokens* const next = p->next;62WebPSafeFree(p);63p = next;64}65VP8TBufferInit(b, b->page_size);66}67}6869static int TBufferNewPage(VP8TBuffer* const b) {70VP8Tokens* page = NULL;71if (!b->error) {72const size_t size = sizeof(*page) + b->page_size * sizeof(token_t);73page = (VP8Tokens*)WebPSafeMalloc(1ULL, size);74}75if (page == NULL) {76b->error = 1;77return 0;78}79page->next = NULL;8081*b->last_page = page;82b->last_page = &page->next;83b->left = b->page_size;84b->tokens = (token_t*)TOKEN_DATA(page);85return 1;86}8788//------------------------------------------------------------------------------8990#define TOKEN_ID(t, b, ctx) \91(NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))9293static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,94uint32_t proba_idx,95proba_t* const stats) {96assert(proba_idx < FIXED_PROBA_BIT);97assert(bit <= 1);98if (b->left > 0 || TBufferNewPage(b)) {99const int slot = --b->left;100b->tokens[slot] = (bit << 15) | proba_idx;101}102VP8RecordStats(bit, stats);103return bit;104}105106static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b,107uint32_t bit, uint32_t proba) {108assert(proba < 256);109assert(bit <= 1);110if (b->left > 0 || TBufferNewPage(b)) {111const int slot = --b->left;112b->tokens[slot] = (bit << 15) | FIXED_PROBA_BIT | proba;113}114}115116int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,117VP8TBuffer* const tokens) {118const int16_t* const coeffs = res->coeffs;119const int coeff_type = res->coeff_type;120const int last = res->last;121int n = res->first;122uint32_t base_id = TOKEN_ID(coeff_type, n, ctx);123// should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1124proba_t* s = res->stats[n][ctx];125if (!AddToken(tokens, last >= 0, base_id + 0, s + 0)) {126return 0;127}128129while (n < 16) {130const int c = coeffs[n++];131const int sign = c < 0;132const uint32_t v = sign ? -c : c;133if (!AddToken(tokens, v != 0, base_id + 1, s + 1)) {134base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0); // ctx=0135s = res->stats[VP8EncBands[n]][0];136continue;137}138if (!AddToken(tokens, v > 1, base_id + 2, s + 2)) {139base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1); // ctx=1140s = res->stats[VP8EncBands[n]][1];141} else {142if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) {143if (AddToken(tokens, v != 2, base_id + 4, s + 4)) {144AddToken(tokens, v == 4, base_id + 5, s + 5);145}146} else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) {147if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) {148AddConstantToken(tokens, v == 6, 159);149} else {150AddConstantToken(tokens, v >= 9, 165);151AddConstantToken(tokens, !(v & 1), 145);152}153} else {154int mask;155const uint8_t* tab;156uint32_t residue = v - 3;157if (residue < (8 << 1)) { // VP8Cat3 (3b)158AddToken(tokens, 0, base_id + 8, s + 8);159AddToken(tokens, 0, base_id + 9, s + 9);160residue -= (8 << 0);161mask = 1 << 2;162tab = VP8Cat3;163} else if (residue < (8 << 2)) { // VP8Cat4 (4b)164AddToken(tokens, 0, base_id + 8, s + 8);165AddToken(tokens, 1, base_id + 9, s + 9);166residue -= (8 << 1);167mask = 1 << 3;168tab = VP8Cat4;169} else if (residue < (8 << 3)) { // VP8Cat5 (5b)170AddToken(tokens, 1, base_id + 8, s + 8);171AddToken(tokens, 0, base_id + 10, s + 9);172residue -= (8 << 2);173mask = 1 << 4;174tab = VP8Cat5;175} else { // VP8Cat6 (11b)176AddToken(tokens, 1, base_id + 8, s + 8);177AddToken(tokens, 1, base_id + 10, s + 9);178residue -= (8 << 3);179mask = 1 << 10;180tab = VP8Cat6;181}182while (mask) {183AddConstantToken(tokens, !!(residue & mask), *tab++);184mask >>= 1;185}186}187base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2188s = res->stats[VP8EncBands[n]][2];189}190AddConstantToken(tokens, sign, 128);191if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) {192return 1; // EOB193}194}195return 1;196}197198#undef TOKEN_ID199200//------------------------------------------------------------------------------201// Final coding pass, with known probabilities202203int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,204const uint8_t* const probas, int final_pass) {205const VP8Tokens* p = b->pages;206assert(!b->error);207while (p != NULL) {208const VP8Tokens* const next = p->next;209const int N = (next == NULL) ? b->left : 0;210int n = b->page_size;211const token_t* const tokens = TOKEN_DATA(p);212while (n-- > N) {213const token_t token = tokens[n];214const int bit = (token >> 15) & 1;215if (token & FIXED_PROBA_BIT) {216VP8PutBit(bw, bit, token & 0xffu); // constant proba217} else {218VP8PutBit(bw, bit, probas[token & 0x3fffu]);219}220}221if (final_pass) WebPSafeFree((void*)p);222p = next;223}224if (final_pass) b->pages = NULL;225return 1;226}227228// Size estimation229size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas) {230size_t size = 0;231const VP8Tokens* p = b->pages;232assert(!b->error);233while (p != NULL) {234const VP8Tokens* const next = p->next;235const int N = (next == NULL) ? b->left : 0;236int n = b->page_size;237const token_t* const tokens = TOKEN_DATA(p);238while (n-- > N) {239const token_t token = tokens[n];240const int bit = token & (1 << 15);241if (token & FIXED_PROBA_BIT) {242size += VP8BitCost(bit, token & 0xffu);243} else {244size += VP8BitCost(bit, probas[token & 0x3fffu]);245}246}247p = next;248}249return size;250}251252//------------------------------------------------------------------------------253254#else // DISABLE_TOKEN_BUFFER255256void VP8TBufferInit(VP8TBuffer* const b, int page_size) {257(void)b;258(void)page_size;259}260void VP8TBufferClear(VP8TBuffer* const b) {261(void)b;262}263264#endif // !DISABLE_TOKEN_BUFFER265266267