| OLD | NEW |
| 1 // Copyright 2010 Google Inc. All Rights Reserved. | 1 // Copyright 2010 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
| 4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
| 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
| 6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
| 7 // | 7 // |
| 8 // Boolean decoder | 8 // Boolean decoder |
| 9 // | 9 // |
| 10 // Author: Skal (pascal.massimino@gmail.com) | 10 // Author: Skal (pascal.massimino@gmail.com) |
| 11 | 11 |
| 12 #include "./bit_reader.h" | 12 #include "./bit_reader.h" |
| 13 | 13 |
| 14 #if defined(__cplusplus) || defined(c_plusplus) | 14 #if defined(__cplusplus) || defined(c_plusplus) |
| 15 extern "C" { | 15 extern "C" { |
| 16 #endif | 16 #endif |
| 17 | 17 |
| 18 #define MK(X) (((bit_t)(X) << (BITS)) | (MASK)) | 18 #ifndef USE_RIGHT_JUSTIFY |
| 19 #define MK(X) (((range_t)(X) << (BITS)) | (MASK)) |
| 20 #else |
| 21 #define MK(X) ((range_t)(X)) |
| 22 #endif |
| 19 | 23 |
| 20 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
| 21 // VP8BitReader | 25 // VP8BitReader |
| 22 | 26 |
| 23 void VP8InitBitReader(VP8BitReader* const br, | 27 void VP8InitBitReader(VP8BitReader* const br, |
| 24 const uint8_t* const start, const uint8_t* const end) { | 28 const uint8_t* const start, const uint8_t* const end) { |
| 25 assert(br != NULL); | 29 assert(br != NULL); |
| 26 assert(start != NULL); | 30 assert(start != NULL); |
| 27 assert(start <= end); | 31 assert(start <= end); |
| 28 br->range_ = MK(255 - 1); | 32 br->range_ = MK(255 - 1); |
| 29 br->buf_ = start; | 33 br->buf_ = start; |
| 30 br->buf_end_ = end; | 34 br->buf_end_ = end; |
| 31 br->value_ = 0; | 35 br->value_ = 0; |
| 32 br->missing_ = 8; // to load the very first 8bits | 36 br->bits_ = -8; // to load the very first 8bits |
| 33 br->eof_ = 0; | 37 br->eof_ = 0; |
| 34 } | 38 } |
| 35 | 39 |
| 36 const uint8_t kVP8Log2Range[128] = { | 40 const uint8_t kVP8Log2Range[128] = { |
| 37 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, | 41 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
| 38 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | 42 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 39 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | 43 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 40 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | 44 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 45 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 43 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 44 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 48 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 45 0 | 49 0 |
| 46 }; | 50 }; |
| 47 | 51 |
| 48 // range = (range << kVP8Log2Range[range]) + trailing 1's | 52 // range = (range << kVP8Log2Range[range]) + trailing 1's |
| 49 const bit_t kVP8NewRange[128] = { | 53 const range_t kVP8NewRange[128] = { |
| 50 MK(127), MK(127), MK(191), MK(127), MK(159), MK(191), MK(223), MK(127), | 54 MK(127), MK(127), MK(191), MK(127), MK(159), MK(191), MK(223), MK(127), |
| 51 MK(143), MK(159), MK(175), MK(191), MK(207), MK(223), MK(239), MK(127), | 55 MK(143), MK(159), MK(175), MK(191), MK(207), MK(223), MK(239), MK(127), |
| 52 MK(135), MK(143), MK(151), MK(159), MK(167), MK(175), MK(183), MK(191), | 56 MK(135), MK(143), MK(151), MK(159), MK(167), MK(175), MK(183), MK(191), |
| 53 MK(199), MK(207), MK(215), MK(223), MK(231), MK(239), MK(247), MK(127), | 57 MK(199), MK(207), MK(215), MK(223), MK(231), MK(239), MK(247), MK(127), |
| 54 MK(131), MK(135), MK(139), MK(143), MK(147), MK(151), MK(155), MK(159), | 58 MK(131), MK(135), MK(139), MK(143), MK(147), MK(151), MK(155), MK(159), |
| 55 MK(163), MK(167), MK(171), MK(175), MK(179), MK(183), MK(187), MK(191), | 59 MK(163), MK(167), MK(171), MK(175), MK(179), MK(183), MK(187), MK(191), |
| 56 MK(195), MK(199), MK(203), MK(207), MK(211), MK(215), MK(219), MK(223), | 60 MK(195), MK(199), MK(203), MK(207), MK(211), MK(215), MK(219), MK(223), |
| 57 MK(227), MK(231), MK(235), MK(239), MK(243), MK(247), MK(251), MK(127), | 61 MK(227), MK(231), MK(235), MK(239), MK(243), MK(247), MK(251), MK(127), |
| 58 MK(129), MK(131), MK(133), MK(135), MK(137), MK(139), MK(141), MK(143), | 62 MK(129), MK(131), MK(133), MK(135), MK(137), MK(139), MK(141), MK(143), |
| 59 MK(145), MK(147), MK(149), MK(151), MK(153), MK(155), MK(157), MK(159), | 63 MK(145), MK(147), MK(149), MK(151), MK(153), MK(155), MK(157), MK(159), |
| 60 MK(161), MK(163), MK(165), MK(167), MK(169), MK(171), MK(173), MK(175), | 64 MK(161), MK(163), MK(165), MK(167), MK(169), MK(171), MK(173), MK(175), |
| 61 MK(177), MK(179), MK(181), MK(183), MK(185), MK(187), MK(189), MK(191), | 65 MK(177), MK(179), MK(181), MK(183), MK(185), MK(187), MK(189), MK(191), |
| 62 MK(193), MK(195), MK(197), MK(199), MK(201), MK(203), MK(205), MK(207), | 66 MK(193), MK(195), MK(197), MK(199), MK(201), MK(203), MK(205), MK(207), |
| 63 MK(209), MK(211), MK(213), MK(215), MK(217), MK(219), MK(221), MK(223), | 67 MK(209), MK(211), MK(213), MK(215), MK(217), MK(219), MK(221), MK(223), |
| 64 MK(225), MK(227), MK(229), MK(231), MK(233), MK(235), MK(237), MK(239), | 68 MK(225), MK(227), MK(229), MK(231), MK(233), MK(235), MK(237), MK(239), |
| 65 MK(241), MK(243), MK(245), MK(247), MK(249), MK(251), MK(253), MK(127) | 69 MK(241), MK(243), MK(245), MK(247), MK(249), MK(251), MK(253), MK(127) |
| 66 }; | 70 }; |
| 67 | 71 |
| 68 #undef MK | 72 #undef MK |
| 69 | 73 |
| 70 void VP8LoadFinalBytes(VP8BitReader* const br) { | 74 void VP8LoadFinalBytes(VP8BitReader* const br) { |
| 71 assert(br != NULL && br->buf_ != NULL); | 75 assert(br != NULL && br->buf_ != NULL); |
| 72 // Only read 8bits at a time | 76 // Only read 8bits at a time |
| 73 if (br->buf_ < br->buf_end_) { | 77 if (br->buf_ < br->buf_end_) { |
| 74 br->value_ |= (bit_t)(*br->buf_++) << ((BITS) - 8 + br->missing_); | 78 #ifndef USE_RIGHT_JUSTIFY |
| 75 br->missing_ -= 8; | 79 br->value_ |= (bit_t)(*br->buf_++) << ((BITS) - 8 - br->bits_); |
| 76 } else { | 80 #else |
| 81 br->value_ = (bit_t)(*br->buf_++) | (br->value_ << 8); |
| 82 #endif |
| 83 br->bits_ += 8; |
| 84 } else if (!br->eof_) { |
| 85 #ifdef USE_RIGHT_JUSTIFY |
| 86 // These are not strictly needed, but it makes the behaviour |
| 87 // consistent for both USE_RIGHT_JUSTIFY and !USE_RIGHT_JUSTIFY. |
| 88 br->value_ <<= 8; |
| 89 br->bits_ += 8; |
| 90 #endif |
| 77 br->eof_ = 1; | 91 br->eof_ = 1; |
| 78 } | 92 } |
| 79 } | 93 } |
| 80 | 94 |
| 81 //------------------------------------------------------------------------------ | 95 //------------------------------------------------------------------------------ |
| 82 // Higher-level calls | 96 // Higher-level calls |
| 83 | 97 |
| 84 uint32_t VP8GetValue(VP8BitReader* const br, int bits) { | 98 uint32_t VP8GetValue(VP8BitReader* const br, int bits) { |
| 85 uint32_t v = 0; | 99 uint32_t v = 0; |
| 86 while (bits-- > 0) { | 100 while (bits-- > 0) { |
| 87 v |= VP8GetBit(br, 0x80) << bits; | 101 v |= VP8GetBit(br, 0x80) << bits; |
| 88 } | 102 } |
| 89 return v; | 103 return v; |
| 90 } | 104 } |
| 91 | 105 |
| 92 int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) { | 106 int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) { |
| 93 const int value = VP8GetValue(br, bits); | 107 const int value = VP8GetValue(br, bits); |
| 94 return VP8Get(br) ? -value : value; | 108 return VP8Get(br) ? -value : value; |
| 95 } | 109 } |
| 96 | 110 |
| 97 //------------------------------------------------------------------------------ | 111 //------------------------------------------------------------------------------ |
| 98 // VP8LBitReader | 112 // VP8LBitReader |
| 99 | 113 |
| 100 #define MAX_NUM_BIT_READ 25 | 114 #define MAX_NUM_BIT_READ 25 |
| 101 | 115 |
| 116 #define LBITS 64 // Number of bits prefetched. |
| 117 #define WBITS 32 // Minimum number of bytes needed after VP8LFillBitWindow. |
| 118 #define LOG8_WBITS 4 // Number of bytes needed to store WBITS bits. |
| 119 |
| 102 static const uint32_t kBitMask[MAX_NUM_BIT_READ] = { | 120 static const uint32_t kBitMask[MAX_NUM_BIT_READ] = { |
| 103 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, | 121 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, |
| 104 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215 | 122 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215 |
| 105 }; | 123 }; |
| 106 | 124 |
| 107 void VP8LInitBitReader(VP8LBitReader* const br, | 125 void VP8LInitBitReader(VP8LBitReader* const br, |
| 108 const uint8_t* const start, | 126 const uint8_t* const start, |
| 109 size_t length) { | 127 size_t length) { |
| 110 size_t i; | 128 size_t i; |
| 111 assert(br != NULL); | 129 assert(br != NULL); |
| 112 assert(start != NULL); | 130 assert(start != NULL); |
| 113 assert(length < 0xfffffff8u); // can't happen with a RIFF chunk. | 131 assert(length < 0xfffffff8u); // can't happen with a RIFF chunk. |
| 114 | 132 |
| 115 br->buf_ = start; | 133 br->buf_ = start; |
| 116 br->len_ = length; | 134 br->len_ = length; |
| 117 br->val_ = 0; | 135 br->val_ = 0; |
| 118 br->pos_ = 0; | 136 br->pos_ = 0; |
| 119 br->bit_pos_ = 0; | 137 br->bit_pos_ = 0; |
| 120 br->eos_ = 0; | 138 br->eos_ = 0; |
| 121 br->error_ = 0; | 139 br->error_ = 0; |
| 122 for (i = 0; i < sizeof(br->val_) && i < br->len_; ++i) { | 140 for (i = 0; i < sizeof(br->val_) && i < br->len_; ++i) { |
| 123 br->val_ |= ((uint64_t)br->buf_[br->pos_]) << (8 * i); | 141 br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (8 * i); |
| 124 ++br->pos_; | 142 ++br->pos_; |
| 125 } | 143 } |
| 126 } | 144 } |
| 127 | 145 |
| 128 void VP8LBitReaderSetBuffer(VP8LBitReader* const br, | 146 void VP8LBitReaderSetBuffer(VP8LBitReader* const br, |
| 129 const uint8_t* const buf, size_t len) { | 147 const uint8_t* const buf, size_t len) { |
| 130 assert(br != NULL); | 148 assert(br != NULL); |
| 131 assert(buf != NULL); | 149 assert(buf != NULL); |
| 132 assert(len < 0xfffffff8u); // can't happen with a RIFF chunk. | 150 assert(len < 0xfffffff8u); // can't happen with a RIFF chunk. |
| 133 br->eos_ = (br->pos_ >= len); | 151 br->eos_ = (br->pos_ >= len); |
| 134 br->buf_ = buf; | 152 br->buf_ = buf; |
| 135 br->len_ = len; | 153 br->len_ = len; |
| 136 } | 154 } |
| 137 | 155 |
| 156 // If not at EOS, reload up to LBITS byte-by-byte |
| 138 static void ShiftBytes(VP8LBitReader* const br) { | 157 static void ShiftBytes(VP8LBitReader* const br) { |
| 139 while (br->bit_pos_ >= 8 && br->pos_ < br->len_) { | 158 while (br->bit_pos_ >= 8 && br->pos_ < br->len_) { |
| 140 br->val_ >>= 8; | 159 br->val_ >>= 8; |
| 141 br->val_ |= ((uint64_t)br->buf_[br->pos_]) << 56; | 160 br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (LBITS - 8); |
| 142 ++br->pos_; | 161 ++br->pos_; |
| 143 br->bit_pos_ -= 8; | 162 br->bit_pos_ -= 8; |
| 144 } | 163 } |
| 145 } | 164 } |
| 146 | 165 |
| 147 void VP8LFillBitWindow(VP8LBitReader* const br) { | 166 void VP8LFillBitWindow(VP8LBitReader* const br) { |
| 148 if (br->bit_pos_ >= 32) { | 167 if (br->bit_pos_ >= WBITS) { |
| 149 #if defined(__x86_64__) || defined(_M_X64) | 168 #if (defined(__x86_64__) || defined(_M_X64)) |
| 150 if (br->pos_ + 8 < br->len_) { | 169 if (br->pos_ + sizeof(br->val_) < br->len_) { |
| 151 br->val_ >>= 32; | 170 br->val_ >>= WBITS; |
| 171 br->bit_pos_ -= WBITS; |
| 152 // The expression below needs a little-endian arch to work correctly. | 172 // The expression below needs a little-endian arch to work correctly. |
| 153 // This gives a large speedup for decoding speed. | 173 // This gives a large speedup for decoding speed. |
| 154 br->val_ |= *(const uint64_t *)(br->buf_ + br->pos_) << 32; | 174 br->val_ |= *(const vp8l_val_t*)(br->buf_ + br->pos_) << (LBITS - WBITS); |
| 155 br->pos_ += 4; | 175 br->pos_ += LOG8_WBITS; |
| 156 br->bit_pos_ -= 32; | 176 return; |
| 157 } else { | |
| 158 // Slow path. | |
| 159 ShiftBytes(br); | |
| 160 } | 177 } |
| 161 #else | |
| 162 // Always the slow path. | |
| 163 ShiftBytes(br); | |
| 164 #endif | 178 #endif |
| 165 } | 179 ShiftBytes(br); // Slow path. |
| 166 if (br->pos_ == br->len_ && br->bit_pos_ == 64) { | 180 if (br->pos_ == br->len_ && br->bit_pos_ == LBITS) { |
| 167 br->eos_ = 1; | 181 br->eos_ = 1; |
| 182 } |
| 168 } | 183 } |
| 169 } | 184 } |
| 170 | 185 |
| 171 uint32_t VP8LReadOneBit(VP8LBitReader* const br) { | |
| 172 const uint32_t val = (br->val_ >> br->bit_pos_) & 1; | |
| 173 // Flag an error at end_of_stream. | |
| 174 if (!br->eos_) { | |
| 175 ++br->bit_pos_; | |
| 176 if (br->bit_pos_ >= 32) { | |
| 177 ShiftBytes(br); | |
| 178 } | |
| 179 // After this last bit is read, check if eos needs to be flagged. | |
| 180 if (br->pos_ == br->len_ && br->bit_pos_ == 64) { | |
| 181 br->eos_ = 1; | |
| 182 } | |
| 183 } else { | |
| 184 br->error_ = 1; | |
| 185 } | |
| 186 return val; | |
| 187 } | |
| 188 | |
| 189 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) { | 186 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) { |
| 190 uint32_t val = 0; | |
| 191 assert(n_bits >= 0); | 187 assert(n_bits >= 0); |
| 192 // Flag an error if end_of_stream or n_bits is more than allowed limit. | 188 // Flag an error if end_of_stream or n_bits is more than allowed limit. |
| 193 if (!br->eos_ && n_bits < MAX_NUM_BIT_READ) { | 189 if (!br->eos_ && n_bits < MAX_NUM_BIT_READ) { |
| 190 const uint32_t val = |
| 191 (uint32_t)(br->val_ >> br->bit_pos_) & kBitMask[n_bits]; |
| 192 const int new_bits = br->bit_pos_ + n_bits; |
| 193 br->bit_pos_ = new_bits; |
| 194 // If this read is going to cross the read buffer, set the eos flag. | 194 // If this read is going to cross the read buffer, set the eos flag. |
| 195 if (br->pos_ == br->len_) { | 195 if (br->pos_ == br->len_) { |
| 196 if ((br->bit_pos_ + n_bits) >= 64) { | 196 if (new_bits >= LBITS) { |
| 197 br->eos_ = 1; | 197 br->eos_ = 1; |
| 198 if ((br->bit_pos_ + n_bits) > 64) return val; | |
| 199 } | 198 } |
| 200 } | 199 } |
| 201 val = (br->val_ >> br->bit_pos_) & kBitMask[n_bits]; | 200 ShiftBytes(br); |
| 202 br->bit_pos_ += n_bits; | 201 return val; |
| 203 if (br->bit_pos_ >= 40) { | |
| 204 if (br->pos_ + 5 < br->len_) { | |
| 205 br->val_ >>= 40; | |
| 206 br->val_ |= | |
| 207 (((uint64_t)br->buf_[br->pos_ + 0]) << 24) | | |
| 208 (((uint64_t)br->buf_[br->pos_ + 1]) << 32) | | |
| 209 (((uint64_t)br->buf_[br->pos_ + 2]) << 40) | | |
| 210 (((uint64_t)br->buf_[br->pos_ + 3]) << 48) | | |
| 211 (((uint64_t)br->buf_[br->pos_ + 4]) << 56); | |
| 212 br->pos_ += 5; | |
| 213 br->bit_pos_ -= 40; | |
| 214 } | |
| 215 if (br->bit_pos_ >= 8) { | |
| 216 ShiftBytes(br); | |
| 217 } | |
| 218 } | |
| 219 } else { | 202 } else { |
| 220 br->error_ = 1; | 203 br->error_ = 1; |
| 204 return 0; |
| 221 } | 205 } |
| 222 return val; | |
| 223 } | 206 } |
| 224 | 207 |
| 225 //------------------------------------------------------------------------------ | 208 //------------------------------------------------------------------------------ |
| 226 | 209 |
| 227 #if defined(__cplusplus) || defined(c_plusplus) | 210 #if defined(__cplusplus) || defined(c_plusplus) |
| 228 } // extern "C" | 211 } // extern "C" |
| 229 #endif | 212 #endif |
| OLD | NEW |