| // Copyright 2010 Google Inc. All Rights Reserved. | |
| // | |
| // 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. | |
| // ----------------------------------------------------------------------------- | |
| // | |
| // Boolean decoder | |
| // | |
| // Author: Skal (pascal.massimino@gmail.com) | |
| // Vikas Arora (vikaas.arora@gmail.com) | |
| #ifndef WEBP_UTILS_BIT_READER_UTILS_H_ | |
| #define WEBP_UTILS_BIT_READER_UTILS_H_ | |
| #include <assert.h> | |
| #include <stddef.h> | |
| #ifdef _MSC_VER | |
| #include <stdlib.h> // _byteswap_ulong | |
| #endif | |
| #include "src/dsp/cpu.h" | |
| #include "src/utils/bounds_safety.h" | |
| #include "src/utils/endian_inl_utils.h" | |
| #include "src/utils/utils.h" | |
| #include "src/webp/types.h" | |
| WEBP_ASSUME_UNSAFE_INDEXABLE_ABI | |
| // Warning! This macro triggers quite some MACRO wizardry around func signature! | |
| #if !defined(BITTRACE) | |
| #define BITTRACE 0 // 0 = off, 1 = print bits, 2 = print bytes | |
| #endif | |
| #if (BITTRACE > 0) | |
| struct VP8BitReader; | |
| extern void BitTrace(const struct VP8BitReader* const br, const char label[]); | |
| #define BT_TRACK(br) BitTrace(br, label) | |
| #define VP8Get(BR, L) VP8GetValue(BR, 1, L) | |
| #else | |
| #define BT_TRACK(br) | |
| // We'll REMOVE the 'const char label[]' from all signatures and calls (!!): | |
| #define VP8GetValue(BR, N, L) VP8GetValue(BR, N) | |
| #define VP8Get(BR, L) VP8GetValue(BR, 1, L) | |
| #define VP8GetSignedValue(BR, N, L) VP8GetSignedValue(BR, N) | |
| #define VP8GetBit(BR, P, L) VP8GetBit(BR, P) | |
| #define VP8GetBitAlt(BR, P, L) VP8GetBitAlt(BR, P) | |
| #define VP8GetSigned(BR, V, L) VP8GetSigned(BR, V) | |
| #endif | |
| #ifdef __cplusplus | |
| extern "C" { | |
| #endif | |
| // The Boolean decoder needs to maintain infinite precision on the 'value' | |
| // field. However, since 'range' is only 8bit, we only need an active window of | |
| // 8 bits for 'value". Left bits (MSB) gets zeroed and shifted away when | |
| // 'value' falls below 128, 'range' is updated, and fresh bits read from the | |
| // bitstream are brought in as LSB. To avoid reading the fresh bits one by one | |
| // (slow), we cache BITS of them ahead. The total of (BITS + 8) bits must fit | |
| // into a natural register (with type bit_t). To fetch BITS bits from bitstream | |
| // we use a type lbit_t. | |
| // | |
| // BITS can be any multiple of 8 from 8 to 56 (inclusive). | |
| // Pick values that fit natural register size. | |
| #if defined(__i386__) || defined(_M_IX86) // x86 32bit | |
| #define BITS 24 | |
| #elif defined(__x86_64__) || defined(_M_X64) // x86 64bit | |
| #define BITS 56 | |
| #elif defined(__arm__) || defined(_M_ARM) // ARM | |
| #define BITS 24 | |
| #elif WEBP_AARCH64 // ARM 64bit | |
| #define BITS 56 | |
| #elif defined(__mips__) // MIPS | |
| #define BITS 24 | |
| #elif defined(__wasm__) // WASM | |
| #define BITS 56 | |
| #else // reasonable default | |
| #define BITS 24 | |
| #endif | |
| //------------------------------------------------------------------------------ | |
| // Derived types and constants: | |
| // bit_t = natural register type for storing 'value' (which is BITS+8 bits) | |
| // range_t = register for 'range' (which is 8bits only) | |
| #if (BITS > 24) | |
| typedef uint64_t bit_t; | |
| #else | |
| typedef uint32_t bit_t; | |
| #endif | |
| typedef uint32_t range_t; | |
| //------------------------------------------------------------------------------ | |
| // Bitreader | |
| typedef struct VP8BitReader VP8BitReader; | |
| struct VP8BitReader { | |
| // boolean decoder (keep the field ordering as is!) | |
| bit_t value; // current value | |
| range_t range; // current range minus 1. In [127, 254] interval. | |
| int bits; // number of valid bits left | |
| // read buffer | |
| const uint8_t* WEBP_ENDED_BY(buf_end) buf; // next byte to be read | |
| const uint8_t* buf_end; // end of read buffer | |
| // max packed-read position on buffer | |
| const uint8_t* WEBP_UNSAFE_INDEXABLE buf_max; | |
| int eof; // true if input is exhausted | |
| }; | |
| // Initialize the bit reader and the boolean decoder. | |
| // Returns 0 in case of error. | |
| WEBP_NODISCARD int VP8InitBitReader(VP8BitReader* const br, | |
| const uint8_t* const WEBP_COUNTED_BY(size) | |
| start, | |
| size_t size); | |
| // Sets the working read buffer. | |
| void VP8BitReaderSetBuffer(VP8BitReader* const br, | |
| const uint8_t* const WEBP_COUNTED_BY(size) start, | |
| size_t size); | |
| // Update internal pointers to displace the byte buffer by the | |
| // relative offset 'offset'. | |
| void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset); | |
| // return the next value made of 'num_bits' bits | |
| uint32_t VP8GetValue(VP8BitReader* const br, int num_bits, const char label[]); | |
| // return the next value with sign-extension. | |
| int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits, | |
| const char label[]); | |
| // bit_reader_inl.h will implement the following methods: | |
| // static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob, ...) | |
| // static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v, ...) | |
| // and should be included by the .c files that actually need them. | |
| // This is to avoid recompiling the whole library whenever this file is touched, | |
| // and also allowing platform-specific ad-hoc hacks. | |
| // ----------------------------------------------------------------------------- | |
| // Bitreader for lossless format | |
| // maximum number of bits (inclusive) the bit-reader can handle: | |
| #define VP8L_MAX_NUM_BIT_READ 24 | |
| #define VP8L_LBITS 64 // Number of bits prefetched (= bit-size of vp8l_val_t). | |
| #define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow. | |
| #define VP8L_LOG8_WBITS 4 // Number of bytes needed to store VP8L_WBITS bits. | |
| #if defined(__arm__) || defined(_M_ARM) || WEBP_AARCH64 || \ | |
| defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || \ | |
| defined(_M_X64) || defined(__wasm__) | |
| #define VP8L_USE_FAST_LOAD | |
| #endif | |
| typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit. | |
| typedef struct { | |
| vp8l_val_t val; // pre-fetched bits | |
| const uint8_t* WEBP_COUNTED_BY(len) buf; // input byte buffer | |
| size_t len; // buffer length | |
| size_t pos; // byte position in buf | |
| int bit_pos; // current bit-reading position in val | |
| int eos; // true if a bit was read past the end of buffer | |
| } VP8LBitReader; | |
| void VP8LInitBitReader(VP8LBitReader* const br, | |
| const uint8_t* const WEBP_COUNTED_BY(length) start, | |
| size_t length); | |
| // Sets a new data buffer. | |
| void VP8LBitReaderSetBuffer(VP8LBitReader* const br, | |
| const uint8_t* const WEBP_COUNTED_BY(length) buffer, | |
| size_t length); | |
| // Reads the specified number of bits from read buffer. | |
| // Flags an error in case end_of_stream or n_bits is more than the allowed limit | |
| // of VP8L_MAX_NUM_BIT_READ (inclusive). | |
| // Flags 'eos' if this read attempt is going to cross the read buffer. | |
| uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits); | |
| // Return the prefetched bits, so they can be looked up. | |
| static WEBP_ALWAYS_INLINE uint32_t VP8LPrefetchBitsFast(vp8l_val_t val, | |
| int bit_pos) { | |
| return (uint32_t)(val >> (bit_pos & (VP8L_LBITS - 1))); | |
| } | |
| static WEBP_ALWAYS_INLINE uint32_t | |
| VP8LPrefetchBits(const VP8LBitReader* const br) { | |
| return VP8LPrefetchBitsFast(br->val, br->bit_pos); | |
| } | |
| // Returns true if there was an attempt at reading bit past the end of | |
| // the buffer. Doesn't set br->eos flag. | |
| static WEBP_ALWAYS_INLINE int VP8LIsEndOfStreamFast( | |
| const VP8LBitReader* const br, int bit_pos) { | |
| assert(br->pos <= br->len); | |
| return br->eos || ((bit_pos > VP8L_LBITS) && (br->pos == br->len)); | |
| } | |
| static WEBP_ALWAYS_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) { | |
| return VP8LIsEndOfStreamFast(br, br->bit_pos); | |
| } | |
| // For jumping over a number of bits in the bit stream when accessed with | |
| // VP8LPrefetchBits and VP8LFillBitWindow. | |
| // This function does *not* set br->eos, since it's speed-critical. | |
| // Use with extreme care! | |
| static WEBP_ALWAYS_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) { | |
| br->bit_pos = val; | |
| } | |
| // Advances the read buffer by 4 bytes to make room for reading next 32 bits. | |
| // The common fast path is inlined; the near-end-of-buffer fallback is handled | |
| // by the non-inlined VP8LDoFillBitWindow. | |
| extern void VP8LDoFillBitWindow(VP8LBitReader* const br); | |
| static WEBP_ALWAYS_INLINE void VP8LFillBitWindowFast(VP8LBitReader* const br, | |
| vp8l_val_t* const val, | |
| int* const bit_pos) { | |
| if (*bit_pos >= VP8L_WBITS) { | |
| #if defined(VP8L_USE_FAST_LOAD) | |
| const size_t pos = br->pos; | |
| if (pos + sizeof(*val) < br->len) { | |
| *val = (*val >> VP8L_WBITS) | | |
| ((vp8l_val_t)HToLE32(WebPMemToUint32(br->buf + pos)) | |
| << (VP8L_LBITS - VP8L_WBITS)); | |
| *bit_pos -= VP8L_WBITS; | |
| br->pos = pos + VP8L_LOG8_WBITS; | |
| return; | |
| } | |
| #endif | |
| br->val = *val; | |
| br->bit_pos = *bit_pos; | |
| VP8LDoFillBitWindow(br); | |
| *val = br->val; | |
| *bit_pos = br->bit_pos; | |
| } | |
| } | |
| static WEBP_ALWAYS_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) { | |
| VP8LFillBitWindowFast(br, &br->val, &br->bit_pos); | |
| } | |
| #ifdef __cplusplus | |
| } // extern "C" | |
| #endif | |
| #endif // WEBP_UTILS_BIT_READER_UTILS_H_ |