Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 1 | // Copyright 2010 Google Inc. All Rights Reserved. |
| 2 | // |
Vikas Arora | 0406ce1 | 2013-08-09 15:57:12 -0700 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the COPYING file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // Boolean decoder |
| 11 | // |
| 12 | // Author: Skal (pascal.massimino@gmail.com) |
| 13 | // Vikas Arora (vikaas.arora@gmail.com) |
| 14 | |
| 15 | #ifndef WEBP_UTILS_BIT_READER_H_ |
| 16 | #define WEBP_UTILS_BIT_READER_H_ |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #ifdef _MSC_VER |
| 20 | #include <stdlib.h> // _byteswap_ulong |
| 21 | #endif |
James Zern | 9e80ee9 | 2015-03-17 18:54:21 -0700 | [diff] [blame] | 22 | #include "../webp/types.h" |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 23 | |
Vikas Arora | 8b72022 | 2014-01-02 16:48:02 -0800 | [diff] [blame] | 24 | #ifdef __cplusplus |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 25 | extern "C" { |
| 26 | #endif |
| 27 | |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 28 | // The Boolean decoder needs to maintain infinite precision on the value_ field. |
| 29 | // However, since range_ is only 8bit, we only need an active window of 8 bits |
| 30 | // for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls |
| 31 | // below 128, range_ is updated, and fresh bits read from the bitstream are |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 32 | // brought in as LSB. To avoid reading the fresh bits one by one (slow), we |
| 33 | // cache BITS of them ahead. The total of (BITS + 8) bits must fit into a |
| 34 | // natural register (with type bit_t). To fetch BITS bits from bitstream we |
| 35 | // use a type lbit_t. |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 36 | // |
Vikas Arora | 0406ce1 | 2013-08-09 15:57:12 -0700 | [diff] [blame] | 37 | // BITS can be any multiple of 8 from 8 to 56 (inclusive). |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 38 | // Pick values that fit natural register size. |
| 39 | |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 40 | #if defined(__i386__) || defined(_M_IX86) // x86 32bit |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 41 | #define BITS 24 |
Vikas Arora | 0406ce1 | 2013-08-09 15:57:12 -0700 | [diff] [blame] | 42 | #elif defined(__x86_64__) || defined(_M_X64) // x86 64bit |
| 43 | #define BITS 56 |
| 44 | #elif defined(__arm__) || defined(_M_ARM) // ARM |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 45 | #define BITS 24 |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 46 | #elif defined(__mips__) // MIPS |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 47 | #define BITS 24 |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 48 | #else // reasonable default |
| 49 | #define BITS 24 // TODO(skal): test aarch64 and find the proper BITS value. |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | //------------------------------------------------------------------------------ |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 53 | // Derived types and constants: |
| 54 | // bit_t = natural register type for storing 'value_' (which is BITS+8 bits) |
| 55 | // range_t = register for 'range_' (which is 8bits only) |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 56 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 57 | #if (BITS > 24) |
Vikas Arora | 0406ce1 | 2013-08-09 15:57:12 -0700 | [diff] [blame] | 58 | typedef uint64_t bit_t; |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 59 | #else |
| 60 | typedef uint32_t bit_t; |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 61 | #endif |
| 62 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 63 | typedef uint32_t range_t; |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 64 | |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 65 | //------------------------------------------------------------------------------ |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 66 | // Bitreader |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 67 | |
| 68 | typedef struct VP8BitReader VP8BitReader; |
| 69 | struct VP8BitReader { |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 70 | // boolean decoder (keep the field ordering as is!) |
| 71 | bit_t value_; // current value |
| 72 | range_t range_; // current range minus 1. In [127, 254] interval. |
| 73 | int bits_; // number of valid bits left |
| 74 | // read buffer |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 75 | const uint8_t* buf_; // next byte to be read |
| 76 | const uint8_t* buf_end_; // end of read buffer |
| 77 | int eof_; // true if input is exhausted |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | // Initialize the bit reader and the boolean decoder. |
| 81 | void VP8InitBitReader(VP8BitReader* const br, |
| 82 | const uint8_t* const start, const uint8_t* const end); |
| 83 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 84 | // Update internal pointers to displace the byte buffer by the |
| 85 | // relative offset 'offset'. |
| 86 | void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset); |
| 87 | |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 88 | // return the next value made of 'num_bits' bits |
| 89 | uint32_t VP8GetValue(VP8BitReader* const br, int num_bits); |
| 90 | static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) { |
| 91 | return VP8GetValue(br, 1); |
| 92 | } |
| 93 | |
| 94 | // return the next value with sign-extension. |
| 95 | int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits); |
| 96 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 97 | // bit_reader_inl.h will implement the following methods: |
| 98 | // static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) |
| 99 | // static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) |
| 100 | // and should be included by the .c files that actually need them. |
| 101 | // This is to avoid recompiling the whole library whenever this file is touched, |
| 102 | // and also allowing platform-specific ad-hoc hacks. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 103 | |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 104 | // ----------------------------------------------------------------------------- |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 105 | // Bitreader for lossless format |
| 106 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 107 | // maximum number of bits (inclusive) the bit-reader can handle: |
| 108 | #define VP8L_MAX_NUM_BIT_READ 24 |
| 109 | |
Vikas Arora | 8c09865 | 2015-01-28 14:08:58 -0800 | [diff] [blame] | 110 | #define VP8L_LBITS 64 // Number of bits prefetched. |
| 111 | #define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow. |
| 112 | |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 113 | typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 114 | |
| 115 | typedef struct { |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 116 | vp8l_val_t val_; // pre-fetched bits |
| 117 | const uint8_t* buf_; // input byte buffer |
| 118 | size_t len_; // buffer length |
| 119 | size_t pos_; // byte position in buf_ |
| 120 | int bit_pos_; // current bit-reading position in val_ |
| 121 | int eos_; // bitstream is finished |
| 122 | int error_; // an error occurred (buffer overflow attempt...) |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 123 | } VP8LBitReader; |
| 124 | |
| 125 | void VP8LInitBitReader(VP8LBitReader* const br, |
| 126 | const uint8_t* const start, |
| 127 | size_t length); |
| 128 | |
| 129 | // Sets a new data buffer. |
| 130 | void VP8LBitReaderSetBuffer(VP8LBitReader* const br, |
| 131 | const uint8_t* const buffer, size_t length); |
| 132 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 133 | // Reads the specified number of bits from read buffer. |
| 134 | // Flags an error in case end_of_stream or n_bits is more than the allowed limit |
| 135 | // of VP8L_MAX_NUM_BIT_READ (inclusive). |
| 136 | // Flags eos_ if this read attempt is going to cross the read buffer. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 137 | uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits); |
| 138 | |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 139 | // Return the prefetched bits, so they can be looked up. |
| 140 | static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) { |
| 141 | return (uint32_t)(br->val_ >> br->bit_pos_); |
| 142 | } |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 143 | |
Vikas Arora | 8c09865 | 2015-01-28 14:08:58 -0800 | [diff] [blame] | 144 | // Returns true if there was an attempt at reading bit past the end of |
| 145 | // the buffer. Doesn't set br->eos_ flag. |
| 146 | static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) { |
| 147 | assert(br->pos_ <= br->len_); |
| 148 | return (br->pos_ == br->len_) && (br->bit_pos_ > VP8L_LBITS); |
| 149 | } |
| 150 | |
Vikas Arora | 8b72022 | 2014-01-02 16:48:02 -0800 | [diff] [blame] | 151 | // For jumping over a number of bits in the bit stream when accessed with |
| 152 | // VP8LPrefetchBits and VP8LFillBitWindow. |
| 153 | static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) { |
| 154 | br->bit_pos_ = val; |
Vikas Arora | 8c09865 | 2015-01-28 14:08:58 -0800 | [diff] [blame] | 155 | br->eos_ = VP8LIsEndOfStream(br); |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Vikas Arora | 8b72022 | 2014-01-02 16:48:02 -0800 | [diff] [blame] | 158 | // Advances the read buffer by 4 bytes to make room for reading next 32 bits. |
Vikas Arora | 8c09865 | 2015-01-28 14:08:58 -0800 | [diff] [blame] | 159 | // Speed critical, but infrequent part of the code can be non-inlined. |
| 160 | extern void VP8LDoFillBitWindow(VP8LBitReader* const br); |
| 161 | static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) { |
| 162 | if (br->bit_pos_ >= VP8L_WBITS) VP8LDoFillBitWindow(br); |
| 163 | } |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 164 | |
Vikas Arora | 8b72022 | 2014-01-02 16:48:02 -0800 | [diff] [blame] | 165 | #ifdef __cplusplus |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 166 | } // extern "C" |
| 167 | #endif |
| 168 | |
| 169 | #endif /* WEBP_UTILS_BIT_READER_H_ */ |