Path: blob/master/3rdparty/libwebp/src/utils/bit_reader_utils.h
16358 views
// Copyright 2010 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// Boolean decoder10//11// Author: Skal ([email protected])12// Vikas Arora ([email protected])1314#ifndef WEBP_UTILS_BIT_READER_UTILS_H_15#define WEBP_UTILS_BIT_READER_UTILS_H_1617#include <assert.h>18#ifdef _MSC_VER19#include <stdlib.h> // _byteswap_ulong20#endif21#include "src/webp/types.h"2223#ifdef __cplusplus24extern "C" {25#endif2627// The Boolean decoder needs to maintain infinite precision on the value_ field.28// However, since range_ is only 8bit, we only need an active window of 8 bits29// for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls30// below 128, range_ is updated, and fresh bits read from the bitstream are31// brought in as LSB. To avoid reading the fresh bits one by one (slow), we32// cache BITS of them ahead. The total of (BITS + 8) bits must fit into a33// natural register (with type bit_t). To fetch BITS bits from bitstream we34// use a type lbit_t.35//36// BITS can be any multiple of 8 from 8 to 56 (inclusive).37// Pick values that fit natural register size.3839#if defined(__i386__) || defined(_M_IX86) // x86 32bit40#define BITS 2441#elif defined(__x86_64__) || defined(_M_X64) // x86 64bit42#define BITS 5643#elif defined(__arm__) || defined(_M_ARM) // ARM44#define BITS 2445#elif defined(__aarch64__) // ARM 64bit46#define BITS 5647#elif defined(__mips__) // MIPS48#define BITS 2449#else // reasonable default50#define BITS 2451#endif5253//------------------------------------------------------------------------------54// Derived types and constants:55// bit_t = natural register type for storing 'value_' (which is BITS+8 bits)56// range_t = register for 'range_' (which is 8bits only)5758#if (BITS > 24)59typedef uint64_t bit_t;60#else61typedef uint32_t bit_t;62#endif6364typedef uint32_t range_t;6566//------------------------------------------------------------------------------67// Bitreader6869typedef struct VP8BitReader VP8BitReader;70struct VP8BitReader {71// boolean decoder (keep the field ordering as is!)72bit_t value_; // current value73range_t range_; // current range minus 1. In [127, 254] interval.74int bits_; // number of valid bits left75// read buffer76const uint8_t* buf_; // next byte to be read77const uint8_t* buf_end_; // end of read buffer78const uint8_t* buf_max_; // max packed-read position on buffer79int eof_; // true if input is exhausted80};8182// Initialize the bit reader and the boolean decoder.83void VP8InitBitReader(VP8BitReader* const br,84const uint8_t* const start, size_t size);85// Sets the working read buffer.86void VP8BitReaderSetBuffer(VP8BitReader* const br,87const uint8_t* const start, size_t size);8889// Update internal pointers to displace the byte buffer by the90// relative offset 'offset'.91void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset);9293// return the next value made of 'num_bits' bits94uint32_t VP8GetValue(VP8BitReader* const br, int num_bits);95static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) {96return VP8GetValue(br, 1);97}9899// return the next value with sign-extension.100int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits);101102// bit_reader_inl.h will implement the following methods:103// static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob)104// static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v)105// and should be included by the .c files that actually need them.106// This is to avoid recompiling the whole library whenever this file is touched,107// and also allowing platform-specific ad-hoc hacks.108109// -----------------------------------------------------------------------------110// Bitreader for lossless format111112// maximum number of bits (inclusive) the bit-reader can handle:113#define VP8L_MAX_NUM_BIT_READ 24114115#define VP8L_LBITS 64 // Number of bits prefetched (= bit-size of vp8l_val_t).116#define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow.117118typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit.119120typedef struct {121vp8l_val_t val_; // pre-fetched bits122const uint8_t* buf_; // input byte buffer123size_t len_; // buffer length124size_t pos_; // byte position in buf_125int bit_pos_; // current bit-reading position in val_126int eos_; // true if a bit was read past the end of buffer127} VP8LBitReader;128129void VP8LInitBitReader(VP8LBitReader* const br,130const uint8_t* const start,131size_t length);132133// Sets a new data buffer.134void VP8LBitReaderSetBuffer(VP8LBitReader* const br,135const uint8_t* const buffer, size_t length);136137// Reads the specified number of bits from read buffer.138// Flags an error in case end_of_stream or n_bits is more than the allowed limit139// of VP8L_MAX_NUM_BIT_READ (inclusive).140// Flags eos_ if this read attempt is going to cross the read buffer.141uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);142143// Return the prefetched bits, so they can be looked up.144static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {145return (uint32_t)(br->val_ >> (br->bit_pos_ & (VP8L_LBITS - 1)));146}147148// Returns true if there was an attempt at reading bit past the end of149// the buffer. Doesn't set br->eos_ flag.150static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) {151assert(br->pos_ <= br->len_);152return br->eos_ || ((br->pos_ == br->len_) && (br->bit_pos_ > VP8L_LBITS));153}154155// For jumping over a number of bits in the bit stream when accessed with156// VP8LPrefetchBits and VP8LFillBitWindow.157// This function does *not* set br->eos_, since it's speed-critical.158// Use with extreme care!159static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) {160br->bit_pos_ = val;161}162163// Advances the read buffer by 4 bytes to make room for reading next 32 bits.164// Speed critical, but infrequent part of the code can be non-inlined.165extern void VP8LDoFillBitWindow(VP8LBitReader* const br);166static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) {167if (br->bit_pos_ >= VP8L_WBITS) VP8LDoFillBitWindow(br);168}169170#ifdef __cplusplus171} // extern "C"172#endif173174#endif /* WEBP_UTILS_BIT_READER_UTILS_H_ */175176177