| OLD | NEW |
| 1 /* libFLAC - Free Lossless Audio Codec library | 1 /* libFLAC - Free Lossless Audio Codec library |
| 2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson | 2 * Copyright (C) 2000-2009 Josh Coalson |
| 3 * Copyright (C) 2011-2014 Xiph.Org Foundation |
| 3 * | 4 * |
| 4 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 6 * are met: | 7 * are met: |
| 7 * | 8 * |
| 8 * - Redistributions of source code must retain the above copyright | 9 * - Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| 10 * | 11 * |
| 11 * - Redistributions in binary form must reproduce the above copyright | 12 * - Redistributions in binary form must reproduce the above copyright |
| 12 * notice, this list of conditions and the following disclaimer in the | 13 * notice, this list of conditions and the following disclaimer in the |
| 13 * documentation and/or other materials provided with the distribution. | 14 * documentation and/or other materials provided with the distribution. |
| 14 * | 15 * |
| 15 * - Neither the name of the Xiph.org Foundation nor the names of its | 16 * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 * contributors may be used to endorse or promote products derived from | 17 * contributors may be used to endorse or promote products derived from |
| 17 * this software without specific prior written permission. | 18 * this software without specific prior written permission. |
| 18 * | 19 * |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR | 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 */ | 31 */ |
| 31 | 32 |
| 32 #if HAVE_CONFIG_H | 33 #ifdef HAVE_CONFIG_H |
| 33 # include <config.h> | 34 # include <config.h> |
| 34 #endif | 35 #endif |
| 35 | 36 |
| 36 #include <stdlib.h> /* for malloc() */ | 37 #include <stdlib.h> |
| 37 #include <string.h> /* for memcpy(), memset() */ | 38 #include <string.h> |
| 38 #ifdef _MSC_VER | |
| 39 #include <winsock.h> /* for ntohl() */ | |
| 40 #elif defined FLAC__SYS_DARWIN | |
| 41 #include <machine/endian.h> /* for ntohl() */ | |
| 42 #elif defined __MINGW32__ | |
| 43 #include <winsock.h> /* for ntohl() */ | |
| 44 #else | |
| 45 #include <netinet/in.h> /* for ntohl() */ | |
| 46 #endif | |
| 47 #include "private/bitmath.h" | 39 #include "private/bitmath.h" |
| 48 #include "private/bitreader.h" | 40 #include "private/bitreader.h" |
| 49 #include "private/crc.h" | 41 #include "private/crc.h" |
| 42 #include "private/macros.h" |
| 50 #include "FLAC/assert.h" | 43 #include "FLAC/assert.h" |
| 44 #include "share/compat.h" |
| 45 #include "share/endswap.h" |
| 51 | 46 |
| 52 /* Things should be fastest when this matches the machine word size */ | 47 /* Things should be fastest when this matches the machine word size */ |
| 53 /* WATCHOUT: if you change this you must also change the following #defines down
to COUNT_ZERO_MSBS below to match */ | 48 /* WATCHOUT: if you change this you must also change the following #defines down
to FLAC__clz_uint32 below to match */ |
| 54 /* WATCHOUT: there are a few places where the code will not work unless brword i
s >= 32 bits wide */ | 49 /* WATCHOUT: there are a few places where the code will not work unless uint32_t
is >= 32 bits wide */ |
| 55 /* also, some sections currently only have fast versions for 4 or 8 by
tes per word */ | 50 /* also, some sections currently only have fast versions for 4 or 8 by
tes per word */ |
| 56 typedef FLAC__uint32 brword; | 51 #define FLAC__BYTES_PER_WORD 4» » /* sizeof uint32_t */ |
| 57 #define FLAC__BYTES_PER_WORD 4 | 52 #define FLAC__BITS_PER_WORD (8 * FLAC__BYTES_PER_WORD) |
| 58 #define FLAC__BITS_PER_WORD 32 | |
| 59 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff) | 53 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff) |
| 60 /* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if
necessary to match host byte order */ | 54 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) i
f necessary to match host byte order */ |
| 61 #if WORDS_BIGENDIAN | 55 #if WORDS_BIGENDIAN |
| 62 #define SWAP_BE_WORD_TO_HOST(x) (x) | 56 #define SWAP_BE_WORD_TO_HOST(x) (x) |
| 63 #else | 57 #else |
| 64 #ifdef _MSC_VER | 58 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x) |
| 65 #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x) | |
| 66 #else | |
| 67 #define SWAP_BE_WORD_TO_HOST(x) ntohl(x) | |
| 68 #endif | 59 #endif |
| 69 #endif | |
| 70 /* counts the # of zero MSBs in a word */ | |
| 71 #define COUNT_ZERO_MSBS(word) ( \ | |
| 72 (word) <= 0xffff ? \ | |
| 73 ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary
_table[(word) >> 8] + 16 ) : \ | |
| 74 ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte
_to_unary_table[(word) >> 24] ) \ | |
| 75 ) | |
| 76 /* this alternate might be slightly faster on some systems/compilers: */ | |
| 77 #define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24
: ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffff
ff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24]))
) | |
| 78 | |
| 79 | 60 |
| 80 /* | 61 /* |
| 81 * This should be at least twice as large as the largest number of words | 62 * This should be at least twice as large as the largest number of words |
| 82 * required to represent any 'number' (in any encoding) you are going to | 63 * required to represent any 'number' (in any encoding) you are going to |
| 83 * read. With FLAC this is on the order of maybe a few hundred bits. | 64 * read. With FLAC this is on the order of maybe a few hundred bits. |
| 84 * If the buffer is smaller than that, the decoder won't be able to read | 65 * If the buffer is smaller than that, the decoder won't be able to read |
| 85 * in a whole number that is in a variable length encoding (e.g. Rice). | 66 * in a whole number that is in a variable length encoding (e.g. Rice). |
| 86 * But to be practical it should be at least 1K bytes. | 67 * But to be practical it should be at least 1K bytes. |
| 87 * | 68 * |
| 88 * Increase this number to decrease the number of read callbacks, at the | 69 * Increase this number to decrease the number of read callbacks, at the |
| 89 * expense of using more memory. Or decrease for the reverse effect, | 70 * expense of using more memory. Or decrease for the reverse effect, |
| 90 * keeping in mind the limit from the first paragraph. The optimal size | 71 * keeping in mind the limit from the first paragraph. The optimal size |
| 91 * also depends on the CPU cache size and other factors; some twiddling | 72 * also depends on the CPU cache size and other factors; some twiddling |
| 92 * may be necessary to squeeze out the best performance. | 73 * may be necessary to squeeze out the best performance. |
| 93 */ | 74 */ |
| 94 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER
_WORD; /* in words */ | 75 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER
_WORD; /* in words */ |
| 95 | 76 |
| 96 static const unsigned char byte_to_unary_table[] = { | |
| 97 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, | |
| 98 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
| 99 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
| 100 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
| 101 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 102 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 103 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 104 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
| 113 }; | |
| 114 | |
| 115 #ifdef min | |
| 116 #undef min | |
| 117 #endif | |
| 118 #define min(x,y) ((x)<(y)?(x):(y)) | |
| 119 #ifdef max | |
| 120 #undef max | |
| 121 #endif | |
| 122 #define max(x,y) ((x)>(y)?(x):(y)) | |
| 123 | |
| 124 /* adjust for compilers that can't understand using LLU suffix for uint64_t lite
rals */ | |
| 125 #ifdef _MSC_VER | |
| 126 #define FLAC__U64L(x) x##ULL | |
| 127 #else | |
| 128 #define FLAC__U64L(x) x##LLU | |
| 129 #endif | |
| 130 | |
| 131 #ifndef FLaC__INLINE | |
| 132 #define FLaC__INLINE | |
| 133 #endif | |
| 134 | |
| 135 /* WATCHOUT: assembly routines rely on the order in which these fields are decla
red */ | |
| 136 struct FLAC__BitReader { | 77 struct FLAC__BitReader { |
| 137 /* any partially-consumed word at the head will stay right-justified as
bits are consumed from the left */ | 78 /* any partially-consumed word at the head will stay right-justified as
bits are consumed from the left */ |
| 138 /* any incomplete word at the tail will be left-justified, and bytes fro
m the read callback are added on the right */ | 79 /* any incomplete word at the tail will be left-justified, and bytes fro
m the read callback are added on the right */ |
| 139 » brword *buffer; | 80 » uint32_t *buffer; |
| 140 unsigned capacity; /* in words */ | 81 unsigned capacity; /* in words */ |
| 141 unsigned words; /* # of completed words in buffer */ | 82 unsigned words; /* # of completed words in buffer */ |
| 142 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */ | 83 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */ |
| 143 unsigned consumed_words; /* #words ... */ | 84 unsigned consumed_words; /* #words ... */ |
| 144 unsigned consumed_bits; /* ... + (#bits of head word) already consumed f
rom the front of buffer */ | 85 unsigned consumed_bits; /* ... + (#bits of head word) already consumed f
rom the front of buffer */ |
| 145 unsigned read_crc16; /* the running frame CRC */ | 86 unsigned read_crc16; /* the running frame CRC */ |
| 146 unsigned crc16_align; /* the number of bits in the current consumed word
that should not be CRC'd */ | 87 unsigned crc16_align; /* the number of bits in the current consumed word
that should not be CRC'd */ |
| 147 FLAC__BitReaderReadCallback read_callback; | 88 FLAC__BitReaderReadCallback read_callback; |
| 148 void *client_data; | 89 void *client_data; |
| 149 FLAC__CPUInfo cpu_info; | |
| 150 }; | 90 }; |
| 151 | 91 |
| 152 #ifdef _MSC_VER | 92 static inline void crc16_update_word_(FLAC__BitReader *br, uint32_t word) |
| 153 /* OPT: an MSVC built-in would be better */ | |
| 154 static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x) | |
| 155 { | |
| 156 » return _byteswap_ulong(x); | |
| 157 } | |
| 158 | |
| 159 static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len) | |
| 160 { | |
| 161 » FLAC__uint32 *end; | |
| 162 » for(end = start + len; start < end; ++start) { | |
| 163 » » *start = local_swap32_(*start); | |
| 164 » } | |
| 165 } | |
| 166 #endif | |
| 167 | |
| 168 static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word) | |
| 169 { | 93 { |
| 170 register unsigned crc = br->read_crc16; | 94 register unsigned crc = br->read_crc16; |
| 171 #if FLAC__BYTES_PER_WORD == 4 | 95 #if FLAC__BYTES_PER_WORD == 4 |
| 172 switch(br->crc16_align) { | 96 switch(br->crc16_align) { |
| 173 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc); | 97 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc); |
| 174 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff
), crc); | 98 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff
), crc); |
| 175 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff)
, crc); | 99 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff)
, crc); |
| 176 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0
xff), crc); | 100 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0
xff), crc); |
| 177 } | 101 } |
| 178 #elif FLAC__BYTES_PER_WORD == 8 | 102 #elif FLAC__BYTES_PER_WORD == 8 |
| 179 switch(br->crc16_align) { | 103 switch(br->crc16_align) { |
| 180 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc); | 104 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc); |
| 181 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff
), crc); | 105 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff
), crc); |
| 182 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff
), crc); | 106 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff
), crc); |
| 183 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff
), crc); | 107 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff
), crc); |
| 184 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff
), crc); | 108 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff
), crc); |
| 185 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff
), crc); | 109 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff
), crc); |
| 186 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff)
, crc); | 110 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff)
, crc); |
| 187 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0
xff), crc); | 111 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0
xff), crc); |
| 188 } | 112 } |
| 189 #else | 113 #else |
| 190 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8) | 114 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8) |
| 191 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WOR
D-8-br->crc16_align)) & 0xff), crc); | 115 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WOR
D-8-br->crc16_align)) & 0xff), crc); |
| 192 br->read_crc16 = crc; | 116 br->read_crc16 = crc; |
| 193 #endif | 117 #endif |
| 194 br->crc16_align = 0; | 118 br->crc16_align = 0; |
| 195 } | 119 } |
| 196 | 120 |
| 197 /* would be static except it needs to be called by asm routines */ | 121 static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br) |
| 198 FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br) | |
| 199 { | 122 { |
| 200 unsigned start, end; | 123 unsigned start, end; |
| 201 size_t bytes; | 124 size_t bytes; |
| 202 FLAC__byte *target; | 125 FLAC__byte *target; |
| 203 | 126 |
| 204 /* first shift the unconsumed buffer data toward the front as much as po
ssible */ | 127 /* first shift the unconsumed buffer data toward the front as much as po
ssible */ |
| 205 if(br->consumed_words > 0) { | 128 if(br->consumed_words > 0) { |
| 206 start = br->consumed_words; | 129 start = br->consumed_words; |
| 207 end = br->words + (br->bytes? 1:0); | 130 end = br->words + (br->bytes? 1:0); |
| 208 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (en
d - start)); | 131 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (en
d - start)); |
| 209 | 132 |
| 210 br->words -= start; | 133 br->words -= start; |
| 211 br->consumed_words = 0; | 134 br->consumed_words = 0; |
| 212 } | 135 } |
| 213 | 136 |
| 214 /* | 137 /* |
| 215 * set the target for reading, taking into account word alignment and en
dianness | 138 * set the target for reading, taking into account word alignment and en
dianness |
| 216 */ | 139 */ |
| 217 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes; | 140 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes; |
| 218 if(bytes == 0) | 141 if(bytes == 0) |
| 219 return false; /* no space left, buffer is too small; see note fo
r FLAC__BITREADER_DEFAULT_CAPACITY */ | 142 return false; /* no space left, buffer is too small; see note fo
r FLAC__BITREADER_DEFAULT_CAPACITY */ |
| 220 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes; | 143 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes; |
| 221 | 144 |
| 222 » /* before reading, if the existing reader looks like this (say brword is
32 bits wide) | 145 » /* before reading, if the existing reader looks like this (say uint32_t
is 32 bits wide) |
| 223 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (par
tial tail word is left-justified) | 146 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (par
tial tail word is left-justified) |
| 224 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes se
quentially in memory) | 147 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes se
quentially in memory) |
| 225 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care) | 148 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care) |
| 226 * ^^-------target, bytes=3 | 149 * ^^-------target, bytes=3 |
| 227 * on LE machines, have to byteswap the odd tail word so nothing is | 150 * on LE machines, have to byteswap the odd tail word so nothing is |
| 228 * overwritten: | 151 * overwritten: |
| 229 */ | 152 */ |
| 230 #if WORDS_BIGENDIAN | 153 #if WORDS_BIGENDIAN |
| 231 #else | 154 #else |
| 232 if(br->bytes) | 155 if(br->bytes) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 245 return false; | 168 return false; |
| 246 | 169 |
| 247 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client: | 170 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client: |
| 248 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF | 171 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF |
| 249 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? | 172 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? |
| 250 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ?? | 173 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ?? |
| 251 * now have to byteswap on LE machines: | 174 * now have to byteswap on LE machines: |
| 252 */ | 175 */ |
| 253 #if WORDS_BIGENDIAN | 176 #if WORDS_BIGENDIAN |
| 254 #else | 177 #else |
| 255 » end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + (unsigned)bytes + (F
LAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD; | 178 » end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES
_PER_WORD-1)) / FLAC__BYTES_PER_WORD; |
| 256 # if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4) | |
| 257 » if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia3
2.bswap) { | |
| 258 » » start = br->words; | |
| 259 » » local_swap32_block_(br->buffer + start, end - start); | |
| 260 » } | |
| 261 » else | |
| 262 # endif | |
| 263 for(start = br->words; start < end; start++) | 179 for(start = br->words; start < end; start++) |
| 264 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]); | 180 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]); |
| 265 #endif | 181 #endif |
| 266 | 182 |
| 267 /* now it looks like: | 183 /* now it looks like: |
| 268 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF | 184 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF |
| 269 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? | 185 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? |
| 270 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD | 186 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD |
| 271 * finally we'll update the reader values: | 187 * finally we'll update the reader values: |
| 272 */ | 188 */ |
| 273 » end = br->words*FLAC__BYTES_PER_WORD + br->bytes + (unsigned)bytes; | 189 » end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes; |
| 274 br->words = end / FLAC__BYTES_PER_WORD; | 190 br->words = end / FLAC__BYTES_PER_WORD; |
| 275 br->bytes = end % FLAC__BYTES_PER_WORD; | 191 br->bytes = end % FLAC__BYTES_PER_WORD; |
| 276 | 192 |
| 277 return true; | 193 return true; |
| 278 } | 194 } |
| 279 | 195 |
| 280 /*********************************************************************** | 196 /*********************************************************************** |
| 281 * | 197 * |
| 282 * Class constructor/destructor | 198 * Class constructor/destructor |
| 283 * | 199 * |
| 284 ***********************************************************************/ | 200 ***********************************************************************/ |
| 285 | 201 |
| 286 FLAC__BitReader *FLAC__bitreader_new(void) | 202 FLAC__BitReader *FLAC__bitreader_new(void) |
| 287 { | 203 { |
| 288 » FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader
)); | 204 » FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader)); |
| 289 | 205 |
| 290 /* calloc() implies: | 206 /* calloc() implies: |
| 291 memset(br, 0, sizeof(FLAC__BitReader)); | 207 memset(br, 0, sizeof(FLAC__BitReader)); |
| 292 br->buffer = 0; | 208 br->buffer = 0; |
| 293 br->capacity = 0; | 209 br->capacity = 0; |
| 294 br->words = br->bytes = 0; | 210 br->words = br->bytes = 0; |
| 295 br->consumed_words = br->consumed_bits = 0; | 211 br->consumed_words = br->consumed_bits = 0; |
| 296 br->read_callback = 0; | 212 br->read_callback = 0; |
| 297 br->client_data = 0; | 213 br->client_data = 0; |
| 298 */ | 214 */ |
| 299 return br; | 215 return br; |
| 300 } | 216 } |
| 301 | 217 |
| 302 void FLAC__bitreader_delete(FLAC__BitReader *br) | 218 void FLAC__bitreader_delete(FLAC__BitReader *br) |
| 303 { | 219 { |
| 304 FLAC__ASSERT(0 != br); | 220 FLAC__ASSERT(0 != br); |
| 305 | 221 |
| 306 FLAC__bitreader_free(br); | 222 FLAC__bitreader_free(br); |
| 307 free(br); | 223 free(br); |
| 308 } | 224 } |
| 309 | 225 |
| 310 /*********************************************************************** | 226 /*********************************************************************** |
| 311 * | 227 * |
| 312 * Public class methods | 228 * Public class methods |
| 313 * | 229 * |
| 314 ***********************************************************************/ | 230 ***********************************************************************/ |
| 315 | 231 |
| 316 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__Bi
tReaderReadCallback rcb, void *cd) | 232 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback
rcb, void *cd) |
| 317 { | 233 { |
| 318 FLAC__ASSERT(0 != br); | 234 FLAC__ASSERT(0 != br); |
| 319 | 235 |
| 320 br->words = br->bytes = 0; | 236 br->words = br->bytes = 0; |
| 321 br->consumed_words = br->consumed_bits = 0; | 237 br->consumed_words = br->consumed_bits = 0; |
| 322 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY; | 238 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY; |
| 323 » br->buffer = (brword*)malloc(sizeof(brword) * br->capacity); | 239 » br->buffer = malloc(sizeof(uint32_t) * br->capacity); |
| 324 if(br->buffer == 0) | 240 if(br->buffer == 0) |
| 325 return false; | 241 return false; |
| 326 br->read_callback = rcb; | 242 br->read_callback = rcb; |
| 327 br->client_data = cd; | 243 br->client_data = cd; |
| 328 br->cpu_info = cpu; | |
| 329 | 244 |
| 330 return true; | 245 return true; |
| 331 } | 246 } |
| 332 | 247 |
| 333 void FLAC__bitreader_free(FLAC__BitReader *br) | 248 void FLAC__bitreader_free(FLAC__BitReader *br) |
| 334 { | 249 { |
| 335 FLAC__ASSERT(0 != br); | 250 FLAC__ASSERT(0 != br); |
| 336 | 251 |
| 337 if(0 != br->buffer) | 252 if(0 != br->buffer) |
| 338 free(br->buffer); | 253 free(br->buffer); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 | 308 |
| 394 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br) | 309 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br) |
| 395 { | 310 { |
| 396 FLAC__ASSERT(0 != br); | 311 FLAC__ASSERT(0 != br); |
| 397 FLAC__ASSERT(0 != br->buffer); | 312 FLAC__ASSERT(0 != br->buffer); |
| 398 FLAC__ASSERT((br->consumed_bits & 7) == 0); | 313 FLAC__ASSERT((br->consumed_bits & 7) == 0); |
| 399 FLAC__ASSERT(br->crc16_align <= br->consumed_bits); | 314 FLAC__ASSERT(br->crc16_align <= br->consumed_bits); |
| 400 | 315 |
| 401 /* CRC any tail bytes in a partially-consumed word */ | 316 /* CRC any tail bytes in a partially-consumed word */ |
| 402 if(br->consumed_bits) { | 317 if(br->consumed_bits) { |
| 403 » » const brword tail = br->buffer[br->consumed_words]; | 318 » » const uint32_t tail = br->buffer[br->consumed_words]; |
| 404 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8
) | 319 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8
) |
| 405 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >>
(FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16); | 320 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >>
(FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16); |
| 406 } | 321 } |
| 407 return br->read_crc16; | 322 return br->read_crc16; |
| 408 } | 323 } |
| 409 | 324 |
| 410 FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__Bit
Reader *br) | 325 inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader
*br) |
| 411 { | 326 { |
| 412 return ((br->consumed_bits & 7) == 0); | 327 return ((br->consumed_bits & 7) == 0); |
| 413 } | 328 } |
| 414 | 329 |
| 415 FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__B
itReader *br) | 330 inline unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitRead
er *br) |
| 416 { | 331 { |
| 417 return 8 - (br->consumed_bits & 7); | 332 return 8 - (br->consumed_bits & 7); |
| 418 } | 333 } |
| 419 | 334 |
| 420 FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitR
eader *br) | 335 inline unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader
*br) |
| 421 { | 336 { |
| 422 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8
- br->consumed_bits; | 337 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8
- br->consumed_bits; |
| 423 } | 338 } |
| 424 | 339 |
| 425 FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLA
C__uint32 *val, unsigned bits) | 340 FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *va
l, unsigned bits) |
| 426 { | 341 { |
| 427 FLAC__ASSERT(0 != br); | 342 FLAC__ASSERT(0 != br); |
| 428 FLAC__ASSERT(0 != br->buffer); | 343 FLAC__ASSERT(0 != br->buffer); |
| 429 | 344 |
| 430 FLAC__ASSERT(bits <= 32); | 345 FLAC__ASSERT(bits <= 32); |
| 431 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits); | 346 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits); |
| 432 FLAC__ASSERT(br->consumed_words <= br->words); | 347 FLAC__ASSERT(br->consumed_words <= br->words); |
| 433 | 348 |
| 434 /* WATCHOUT: code does not work with <32bit words; we can make things mu
ch faster with this assertion */ | 349 /* WATCHOUT: code does not work with <32bit words; we can make things mu
ch faster with this assertion */ |
| 435 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); | 350 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); |
| 436 | 351 |
| 437 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe chang
e to assertion */ | 352 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe chang
e to assertion */ |
| 438 *val = 0; | 353 *val = 0; |
| 439 return true; | 354 return true; |
| 440 } | 355 } |
| 441 | 356 |
| 442 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 -
br->consumed_bits < bits) { | 357 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 -
br->consumed_bits < bits) { |
| 443 if(!bitreader_read_from_client_(br)) | 358 if(!bitreader_read_from_client_(br)) |
| 444 return false; | 359 return false; |
| 445 } | 360 } |
| 446 if(br->consumed_words < br->words) { /* if we've not consumed up to a pa
rtial tail word... */ | 361 if(br->consumed_words < br->words) { /* if we've not consumed up to a pa
rtial tail word... */ |
| 447 /* OPT: taking out the consumed_bits==0 "else" case below might
make things faster if less code allows the compiler to inline this function */ | 362 /* OPT: taking out the consumed_bits==0 "else" case below might
make things faster if less code allows the compiler to inline this function */ |
| 448 if(br->consumed_bits) { | 363 if(br->consumed_bits) { |
| 449 /* this also works when consumed_bits==0, it's just a li
ttle slower than necessary for that case */ | 364 /* this also works when consumed_bits==0, it's just a li
ttle slower than necessary for that case */ |
| 450 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bi
ts; | 365 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bi
ts; |
| 451 » » » const brword word = br->buffer[br->consumed_words]; | 366 » » » const uint32_t word = br->buffer[br->consumed_words]; |
| 452 if(bits < n) { | 367 if(bits < n) { |
| 453 *val = (word & (FLAC__WORD_ALL_ONES >> br->consu
med_bits)) >> (n-bits); | 368 *val = (word & (FLAC__WORD_ALL_ONES >> br->consu
med_bits)) >> (n-bits); |
| 454 br->consumed_bits += bits; | 369 br->consumed_bits += bits; |
| 455 return true; | 370 return true; |
| 456 } | 371 } |
| 457 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)
; | 372 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)
; |
| 458 bits -= n; | 373 bits -= n; |
| 459 crc16_update_word_(br, word); | 374 crc16_update_word_(br, word); |
| 460 br->consumed_words++; | 375 br->consumed_words++; |
| 461 br->consumed_bits = 0; | 376 br->consumed_bits = 0; |
| 462 if(bits) { /* if there are still bits left to read, ther
e have to be less than 32 so they will all be in the next word */ | 377 if(bits) { /* if there are still bits left to read, ther
e have to be less than 32 so they will all be in the next word */ |
| 463 *val <<= bits; | 378 *val <<= bits; |
| 464 *val |= (br->buffer[br->consumed_words] >> (FLAC
__BITS_PER_WORD-bits)); | 379 *val |= (br->buffer[br->consumed_words] >> (FLAC
__BITS_PER_WORD-bits)); |
| 465 br->consumed_bits = bits; | 380 br->consumed_bits = bits; |
| 466 } | 381 } |
| 467 return true; | 382 return true; |
| 468 } | 383 } |
| 469 else { | 384 else { |
| 470 » » » const brword word = br->buffer[br->consumed_words]; | 385 » » » const uint32_t word = br->buffer[br->consumed_words]; |
| 471 if(bits < FLAC__BITS_PER_WORD) { | 386 if(bits < FLAC__BITS_PER_WORD) { |
| 472 *val = word >> (FLAC__BITS_PER_WORD-bits); | 387 *val = word >> (FLAC__BITS_PER_WORD-bits); |
| 473 br->consumed_bits = bits; | 388 br->consumed_bits = bits; |
| 474 return true; | 389 return true; |
| 475 } | 390 } |
| 476 /* at this point 'bits' must be == FLAC__BITS_PER_WORD;
because of previous assertions, it can't be larger */ | 391 /* at this point 'bits' must be == FLAC__BITS_PER_WORD;
because of previous assertions, it can't be larger */ |
| 477 *val = word; | 392 *val = word; |
| 478 crc16_update_word_(br, word); | 393 crc16_update_word_(br, word); |
| 479 br->consumed_words++; | 394 br->consumed_words++; |
| 480 return true; | 395 return true; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 *val |= lo; | 441 *val |= lo; |
| 527 } | 442 } |
| 528 else { | 443 else { |
| 529 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits)) | 444 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits)) |
| 530 return false; | 445 return false; |
| 531 *val = lo; | 446 *val = lo; |
| 532 } | 447 } |
| 533 return true; | 448 return true; |
| 534 } | 449 } |
| 535 | 450 |
| 536 FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReade
r *br, FLAC__uint32 *val) | 451 inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br,
FLAC__uint32 *val) |
| 537 { | 452 { |
| 538 FLAC__uint32 x8, x32 = 0; | 453 FLAC__uint32 x8, x32 = 0; |
| 539 | 454 |
| 540 /* this doesn't need to be that fast as currently it is only used for vo
rbis comments */ | 455 /* this doesn't need to be that fast as currently it is only used for vo
rbis comments */ |
| 541 | 456 |
| 542 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8)) | 457 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8)) |
| 543 return false; | 458 return false; |
| 544 | 459 |
| 545 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) | 460 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) |
| 546 return false; | 461 return false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 566 */ | 481 */ |
| 567 FLAC__ASSERT(0 != br); | 482 FLAC__ASSERT(0 != br); |
| 568 FLAC__ASSERT(0 != br->buffer); | 483 FLAC__ASSERT(0 != br->buffer); |
| 569 | 484 |
| 570 if(bits > 0) { | 485 if(bits > 0) { |
| 571 const unsigned n = br->consumed_bits & 7; | 486 const unsigned n = br->consumed_bits & 7; |
| 572 unsigned m; | 487 unsigned m; |
| 573 FLAC__uint32 x; | 488 FLAC__uint32 x; |
| 574 | 489 |
| 575 if(n != 0) { | 490 if(n != 0) { |
| 576 » » » m = min(8-n, bits); | 491 » » » m = flac_min(8-n, bits); |
| 577 if(!FLAC__bitreader_read_raw_uint32(br, &x, m)) | 492 if(!FLAC__bitreader_read_raw_uint32(br, &x, m)) |
| 578 return false; | 493 return false; |
| 579 bits -= m; | 494 bits -= m; |
| 580 } | 495 } |
| 581 m = bits / 8; | 496 m = bits / 8; |
| 582 if(m > 0) { | 497 if(m > 0) { |
| 583 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m
)) | 498 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m
)) |
| 584 return false; | 499 return false; |
| 585 bits %= 8; | 500 bits %= 8; |
| 586 } | 501 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) | 556 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 642 return false; | 557 return false; |
| 643 *val++ = (FLAC__byte)x; | 558 *val++ = (FLAC__byte)x; |
| 644 nvals--; | 559 nvals--; |
| 645 } | 560 } |
| 646 if(0 == nvals) | 561 if(0 == nvals) |
| 647 return true; | 562 return true; |
| 648 /* step 2: read whole words in chunks */ | 563 /* step 2: read whole words in chunks */ |
| 649 while(nvals >= FLAC__BYTES_PER_WORD) { | 564 while(nvals >= FLAC__BYTES_PER_WORD) { |
| 650 if(br->consumed_words < br->words) { | 565 if(br->consumed_words < br->words) { |
| 651 » » » const brword word = br->buffer[br->consumed_words++]; | 566 » » » const uint32_t word = br->buffer[br->consumed_words++]; |
| 652 #if FLAC__BYTES_PER_WORD == 4 | 567 #if FLAC__BYTES_PER_WORD == 4 |
| 653 val[0] = (FLAC__byte)(word >> 24); | 568 val[0] = (FLAC__byte)(word >> 24); |
| 654 val[1] = (FLAC__byte)(word >> 16); | 569 val[1] = (FLAC__byte)(word >> 16); |
| 655 val[2] = (FLAC__byte)(word >> 8); | 570 val[2] = (FLAC__byte)(word >> 8); |
| 656 val[3] = (FLAC__byte)word; | 571 val[3] = (FLAC__byte)word; |
| 657 #elif FLAC__BYTES_PER_WORD == 8 | 572 #elif FLAC__BYTES_PER_WORD == 8 |
| 658 val[0] = (FLAC__byte)(word >> 56); | 573 val[0] = (FLAC__byte)(word >> 56); |
| 659 val[1] = (FLAC__byte)(word >> 48); | 574 val[1] = (FLAC__byte)(word >> 48); |
| 660 val[2] = (FLAC__byte)(word >> 40); | 575 val[2] = (FLAC__byte)(word >> 40); |
| 661 val[3] = (FLAC__byte)(word >> 32); | 576 val[3] = (FLAC__byte)(word >> 32); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 677 while(nvals) { | 592 while(nvals) { |
| 678 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) | 593 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 679 return false; | 594 return false; |
| 680 *val++ = (FLAC__byte)x; | 595 *val++ = (FLAC__byte)x; |
| 681 nvals--; | 596 nvals--; |
| 682 } | 597 } |
| 683 | 598 |
| 684 return true; | 599 return true; |
| 685 } | 600 } |
| 686 | 601 |
| 687 FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br,
unsigned *val) | 602 FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *va
l) |
| 688 #if 0 /* slow but readable version */ | 603 #if 0 /* slow but readable version */ |
| 689 { | 604 { |
| 690 unsigned bit; | 605 unsigned bit; |
| 691 | 606 |
| 692 FLAC__ASSERT(0 != br); | 607 FLAC__ASSERT(0 != br); |
| 693 FLAC__ASSERT(0 != br->buffer); | 608 FLAC__ASSERT(0 != br->buffer); |
| 694 | 609 |
| 695 *val = 0; | 610 *val = 0; |
| 696 while(1) { | 611 while(1) { |
| 697 if(!FLAC__bitreader_read_bit(br, &bit)) | 612 if(!FLAC__bitreader_read_bit(br, &bit)) |
| 698 return false; | 613 return false; |
| 699 if(bit) | 614 if(bit) |
| 700 break; | 615 break; |
| 701 else | 616 else |
| 702 *val++; | 617 *val++; |
| 703 } | 618 } |
| 704 return true; | 619 return true; |
| 705 } | 620 } |
| 706 #else | 621 #else |
| 707 { | 622 { |
| 708 unsigned i; | 623 unsigned i; |
| 709 | 624 |
| 710 FLAC__ASSERT(0 != br); | 625 FLAC__ASSERT(0 != br); |
| 711 FLAC__ASSERT(0 != br->buffer); | 626 FLAC__ASSERT(0 != br->buffer); |
| 712 | 627 |
| 713 *val = 0; | 628 *val = 0; |
| 714 while(1) { | 629 while(1) { |
| 715 while(br->consumed_words < br->words) { /* if we've not consumed
up to a partial tail word... */ | 630 while(br->consumed_words < br->words) { /* if we've not consumed
up to a partial tail word... */ |
| 716 » » » brword b = br->buffer[br->consumed_words] << br->consume
d_bits; | 631 » » » uint32_t b = br->buffer[br->consumed_words] << br->consu
med_bits; |
| 717 if(b) { | 632 if(b) { |
| 718 » » » » i = COUNT_ZERO_MSBS(b); | 633 » » » » i = FLAC__clz_uint32(b); |
| 719 *val += i; | 634 *val += i; |
| 720 i++; | 635 i++; |
| 721 br->consumed_bits += i; | 636 br->consumed_bits += i; |
| 722 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /
* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */ | 637 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /
* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */ |
| 723 crc16_update_word_(br, br->buffer[br->co
nsumed_words]); | 638 crc16_update_word_(br, br->buffer[br->co
nsumed_words]); |
| 724 br->consumed_words++; | 639 br->consumed_words++; |
| 725 br->consumed_bits = 0; | 640 br->consumed_bits = 0; |
| 726 } | 641 } |
| 727 return true; | 642 return true; |
| 728 } | 643 } |
| 729 else { | 644 else { |
| 730 *val += FLAC__BITS_PER_WORD - br->consumed_bits; | 645 *val += FLAC__BITS_PER_WORD - br->consumed_bits; |
| 731 crc16_update_word_(br, br->buffer[br->consumed_w
ords]); | 646 crc16_update_word_(br, br->buffer[br->consumed_w
ords]); |
| 732 br->consumed_words++; | 647 br->consumed_words++; |
| 733 br->consumed_bits = 0; | 648 br->consumed_bits = 0; |
| 734 /* didn't find stop bit yet, have to keep going.
.. */ | 649 /* didn't find stop bit yet, have to keep going.
.. */ |
| 735 } | 650 } |
| 736 } | 651 } |
| 737 /* at this point we've eaten up all the whole words; have to try | 652 /* at this point we've eaten up all the whole words; have to try |
| 738 * reading through any tail bytes before calling the read callba
ck. | 653 * reading through any tail bytes before calling the read callba
ck. |
| 739 * this is a repeat of the above logic adjusted for the fact we | 654 * this is a repeat of the above logic adjusted for the fact we |
| 740 * don't have a whole word. note though if the client is feedin
g | 655 * don't have a whole word. note though if the client is feedin
g |
| 741 * us data a byte at a time (unlikely), br->consumed_bits may no
t | 656 * us data a byte at a time (unlikely), br->consumed_bits may no
t |
| 742 * be zero. | 657 * be zero. |
| 743 */ | 658 */ |
| 744 » » if(br->bytes) { | 659 » » if(br->bytes*8 > br->consumed_bits) { |
| 745 const unsigned end = br->bytes * 8; | 660 const unsigned end = br->bytes * 8; |
| 746 » » » brword b = (br->buffer[br->consumed_words] & (FLAC__WORD
_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits; | 661 » » » uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WO
RD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits; |
| 747 if(b) { | 662 if(b) { |
| 748 » » » » i = COUNT_ZERO_MSBS(b); | 663 » » » » i = FLAC__clz_uint32(b); |
| 749 *val += i; | 664 *val += i; |
| 750 i++; | 665 i++; |
| 751 br->consumed_bits += i; | 666 br->consumed_bits += i; |
| 752 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_
WORD); | 667 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_
WORD); |
| 753 return true; | 668 return true; |
| 754 } | 669 } |
| 755 else { | 670 else { |
| 756 *val += end - br->consumed_bits; | 671 *val += end - br->consumed_bits; |
| 757 » » » » br->consumed_bits += end; | 672 » » » » br->consumed_bits = end; |
| 758 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_
WORD); | 673 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_
WORD); |
| 759 /* didn't find stop bit yet, have to keep going.
.. */ | 674 /* didn't find stop bit yet, have to keep going.
.. */ |
| 760 } | 675 } |
| 761 } | 676 } |
| 762 if(!bitreader_read_from_client_(br)) | 677 if(!bitreader_read_from_client_(br)) |
| 763 return false; | 678 return false; |
| 764 } | 679 } |
| 765 } | 680 } |
| 766 #endif | 681 #endif |
| 767 | 682 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 786 uval = (msbs << parameter) | lsbs; | 701 uval = (msbs << parameter) | lsbs; |
| 787 if(uval & 1) | 702 if(uval & 1) |
| 788 *val = -((int)(uval >> 1)) - 1; | 703 *val = -((int)(uval >> 1)) - 1; |
| 789 else | 704 else |
| 790 *val = (int)(uval >> 1); | 705 *val = (int)(uval >> 1); |
| 791 | 706 |
| 792 return true; | 707 return true; |
| 793 } | 708 } |
| 794 | 709 |
| 795 /* this is by far the most heavily used reader call. it ain't pretty but it's f
ast */ | 710 /* this is by far the most heavily used reader call. it ain't pretty but it's f
ast */ |
| 796 /* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_u
nsigned() and FLAC__bitreader_read_raw_uint32() */ | |
| 797 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[
], unsigned nvals, unsigned parameter) | 711 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[
], unsigned nvals, unsigned parameter) |
| 798 /* OPT: possibly faster version for use with MSVC */ | |
| 799 #ifdef _MSC_VER | |
| 800 { | 712 { |
| 801 unsigned i; | |
| 802 unsigned uval = 0; | |
| 803 unsigned bits; /* the # of binary LSBs left to read to finish a rice cod
eword */ | |
| 804 | |
| 805 /* try and get br->consumed_words and br->consumed_bits into register; | 713 /* try and get br->consumed_words and br->consumed_bits into register; |
| 806 * must remember to flush them back to *br before calling other | 714 * must remember to flush them back to *br before calling other |
| 807 » * bitwriter functions that use them, and before returning */ | 715 » * bitreader functions that use them, and before returning */ |
| 808 » register unsigned cwords; | 716 » unsigned cwords, words, lsbs, msbs, x, y; |
| 809 » register unsigned cbits; | 717 » unsigned ucbits; /* keep track of the number of unconsumed bits in word
*/ |
| 810 | 718 » uint32_t b; |
| 811 » FLAC__ASSERT(0 != br); | 719 » int *val, *end; |
| 812 » FLAC__ASSERT(0 != br->buffer); | |
| 813 » /* WATCHOUT: code does not work with <32bit words; we can make things mu
ch faster with this assertion */ | |
| 814 » FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); | |
| 815 » FLAC__ASSERT(parameter < 32); | |
| 816 » /* the above two asserts also guarantee that the binary part never strad
dles more that 2 words, so we don't have to loop to read it */ | |
| 817 | |
| 818 » if(nvals == 0) | |
| 819 » » return true; | |
| 820 | |
| 821 » cbits = br->consumed_bits; | |
| 822 » cwords = br->consumed_words; | |
| 823 | |
| 824 » while(1) { | |
| 825 | |
| 826 » » /* read unary part */ | |
| 827 » » while(1) { | |
| 828 » » » while(cwords < br->words) { /* if we've not consumed up
to a partial tail word... */ | |
| 829 » » » » brword b = br->buffer[cwords] << cbits; | |
| 830 » » » » if(b) { | |
| 831 #if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__
CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 | |
| 832 » » » » » __asm { | |
| 833 » » » » » » bsr eax, b | |
| 834 » » » » » » not eax | |
| 835 » » » » » » and eax, 31 | |
| 836 » » » » » » mov i, eax | |
| 837 » » » » » } | |
| 838 #else | |
| 839 » » » » » i = COUNT_ZERO_MSBS(b); | |
| 840 #endif | |
| 841 » » » » » uval += i; | |
| 842 » » » » » bits = parameter; | |
| 843 » » » » » i++; | |
| 844 » » » » » cbits += i; | |
| 845 » » » » » if(cbits == FLAC__BITS_PER_WORD) { | |
| 846 » » » » » » crc16_update_word_(br, br->buffe
r[cwords]); | |
| 847 » » » » » » cwords++; | |
| 848 » » » » » » cbits = 0; | |
| 849 » » » » » } | |
| 850 » » » » » goto break1; | |
| 851 » » » » } | |
| 852 » » » » else { | |
| 853 » » » » » uval += FLAC__BITS_PER_WORD - cbits; | |
| 854 » » » » » crc16_update_word_(br, br->buffer[cwords
]); | |
| 855 » » » » » cwords++; | |
| 856 » » » » » cbits = 0; | |
| 857 » » » » » /* didn't find stop bit yet, have to kee
p going... */ | |
| 858 » » » » } | |
| 859 » » » } | |
| 860 » » » /* at this point we've eaten up all the whole words; hav
e to try | |
| 861 » » » * reading through any tail bytes before calling the rea
d callback. | |
| 862 » » » * this is a repeat of the above logic adjusted for the
fact we | |
| 863 » » » * don't have a whole word. note though if the client i
s feeding | |
| 864 » » » * us data a byte at a time (unlikely), br->consumed_bit
s may not | |
| 865 » » » * be zero. | |
| 866 » » » */ | |
| 867 » » » if(br->bytes) { | |
| 868 » » » » const unsigned end = br->bytes * 8; | |
| 869 » » » » brword b = (br->buffer[cwords] & (FLAC__WORD_ALL
_ONES << (FLAC__BITS_PER_WORD-end))) << cbits; | |
| 870 » » » » if(b) { | |
| 871 » » » » » i = COUNT_ZERO_MSBS(b); | |
| 872 » » » » » uval += i; | |
| 873 » » » » » bits = parameter; | |
| 874 » » » » » i++; | |
| 875 » » » » » cbits += i; | |
| 876 » » » » » FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD
); | |
| 877 » » » » » goto break1; | |
| 878 » » » » } | |
| 879 » » » » else { | |
| 880 » » » » » uval += end - cbits; | |
| 881 » » » » » cbits += end; | |
| 882 » » » » » FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD
); | |
| 883 » » » » » /* didn't find stop bit yet, have to kee
p going... */ | |
| 884 » » » » } | |
| 885 » » » } | |
| 886 » » » /* flush registers and read; bitreader_read_from_client_
() does | |
| 887 » » » * not touch br->consumed_bits at all but we still need
to set | |
| 888 » » » * it in case it fails and we have to return false. | |
| 889 » » » */ | |
| 890 » » » br->consumed_bits = cbits; | |
| 891 » » » br->consumed_words = cwords; | |
| 892 » » » if(!bitreader_read_from_client_(br)) | |
| 893 » » » » return false; | |
| 894 » » » cwords = br->consumed_words; | |
| 895 » » } | |
| 896 break1: | |
| 897 » » /* read binary part */ | |
| 898 » » FLAC__ASSERT(cwords <= br->words); | |
| 899 | |
| 900 » » if(bits) { | |
| 901 » » » while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes
*8 - cbits < bits) { | |
| 902 » » » » /* flush registers and read; bitreader_read_from
_client_() does | |
| 903 » » » » * not touch br->consumed_bits at all but we sti
ll need to set | |
| 904 » » » » * it in case it fails and we have to return fal
se. | |
| 905 » » » » */ | |
| 906 » » » » br->consumed_bits = cbits; | |
| 907 » » » » br->consumed_words = cwords; | |
| 908 » » » » if(!bitreader_read_from_client_(br)) | |
| 909 » » » » » return false; | |
| 910 » » » » cwords = br->consumed_words; | |
| 911 » » » } | |
| 912 » » » if(cwords < br->words) { /* if we've not consumed up to
a partial tail word... */ | |
| 913 » » » » if(cbits) { | |
| 914 » » » » » /* this also works when consumed_bits==0
, it's just a little slower than necessary for that case */ | |
| 915 » » » » » const unsigned n = FLAC__BITS_PER_WORD -
cbits; | |
| 916 » » » » » const brword word = br->buffer[cwords]; | |
| 917 » » » » » if(bits < n) { | |
| 918 » » » » » » uval <<= bits; | |
| 919 » » » » » » uval |= (word & (FLAC__WORD_ALL_
ONES >> cbits)) >> (n-bits); | |
| 920 » » » » » » cbits += bits; | |
| 921 » » » » » » goto break2; | |
| 922 » » » » » } | |
| 923 » » » » » uval <<= n; | |
| 924 » » » » » uval |= word & (FLAC__WORD_ALL_ONES >> c
bits); | |
| 925 » » » » » bits -= n; | |
| 926 » » » » » crc16_update_word_(br, word); | |
| 927 » » » » » cwords++; | |
| 928 » » » » » cbits = 0; | |
| 929 » » » » » if(bits) { /* if there are still bits le
ft to read, there have to be less than 32 so they will all be in the next word *
/ | |
| 930 » » » » » » uval <<= bits; | |
| 931 » » » » » » uval |= (br->buffer[cwords] >> (
FLAC__BITS_PER_WORD-bits)); | |
| 932 » » » » » » cbits = bits; | |
| 933 » » » » » } | |
| 934 » » » » » goto break2; | |
| 935 » » » » } | |
| 936 » » » » else { | |
| 937 » » » » » FLAC__ASSERT(bits < FLAC__BITS_PER_WORD)
; | |
| 938 » » » » » uval <<= bits; | |
| 939 » » » » » uval |= br->buffer[cwords] >> (FLAC__BIT
S_PER_WORD-bits); | |
| 940 » » » » » cbits = bits; | |
| 941 » » » » » goto break2; | |
| 942 » » » » } | |
| 943 » » » } | |
| 944 » » » else { | |
| 945 » » » » /* in this case we're starting our read at a par
tial tail word; | |
| 946 » » » » * the reader has guaranteed that we have at lea
st 'bits' bits | |
| 947 » » » » * available to read, which makes this case simp
ler. | |
| 948 » » » » */ | |
| 949 » » » » uval <<= bits; | |
| 950 » » » » if(cbits) { | |
| 951 » » » » » /* this also works when consumed_bits==0
, it's just a little slower than necessary for that case */ | |
| 952 » » » » » FLAC__ASSERT(cbits + bits <= br->bytes*8
); | |
| 953 » » » » » uval |= (br->buffer[cwords] & (FLAC__WOR
D_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits); | |
| 954 » » » » » cbits += bits; | |
| 955 » » » » » goto break2; | |
| 956 » » » » } | |
| 957 » » » » else { | |
| 958 » » » » » uval |= br->buffer[cwords] >> (FLAC__BIT
S_PER_WORD-bits); | |
| 959 » » » » » cbits += bits; | |
| 960 » » » » » goto break2; | |
| 961 » » » » } | |
| 962 » » » } | |
| 963 » » } | |
| 964 break2: | |
| 965 » » /* compose the value */ | |
| 966 » » *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); | |
| 967 | |
| 968 » » /* are we done? */ | |
| 969 » » --nvals; | |
| 970 » » if(nvals == 0) { | |
| 971 » » » br->consumed_bits = cbits; | |
| 972 » » » br->consumed_words = cwords; | |
| 973 » » » return true; | |
| 974 » » } | |
| 975 | |
| 976 » » uval = 0; | |
| 977 » » ++vals; | |
| 978 | |
| 979 » } | |
| 980 } | |
| 981 #else | |
| 982 { | |
| 983 » unsigned i; | |
| 984 » unsigned uval = 0; | |
| 985 | |
| 986 » /* try and get br->consumed_words and br->consumed_bits into register; | |
| 987 » * must remember to flush them back to *br before calling other | |
| 988 » * bitwriter functions that use them, and before returning */ | |
| 989 » register unsigned cwords; | |
| 990 » register unsigned cbits; | |
| 991 » unsigned ucbits; /* keep track of the number of unconsumed bits in the b
uffer */ | |
| 992 | 720 |
| 993 FLAC__ASSERT(0 != br); | 721 FLAC__ASSERT(0 != br); |
| 994 FLAC__ASSERT(0 != br->buffer); | 722 FLAC__ASSERT(0 != br->buffer); |
| 995 /* WATCHOUT: code does not work with <32bit words; we can make things mu
ch faster with this assertion */ | 723 /* WATCHOUT: code does not work with <32bit words; we can make things mu
ch faster with this assertion */ |
| 996 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); | 724 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); |
| 997 FLAC__ASSERT(parameter < 32); | 725 FLAC__ASSERT(parameter < 32); |
| 998 /* the above two asserts also guarantee that the binary part never strad
dles more than 2 words, so we don't have to loop to read it */ | 726 /* the above two asserts also guarantee that the binary part never strad
dles more than 2 words, so we don't have to loop to read it */ |
| 999 | 727 |
| 1000 » if(nvals == 0) | 728 » val = vals; |
| 1001 » » return true; | 729 » end = vals + nvals; |
| 1002 | 730 |
| 1003 » cbits = br->consumed_bits; | 731 » if(parameter == 0) { |
| 1004 » cwords = br->consumed_words; | 732 » » while(val < end) { |
| 1005 » ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; | 733 » » » /* read the unary MSBs and end bit */ |
| 734 » » » if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) |
| 735 » » » » return false; |
| 1006 | 736 |
| 1007 » while(1) { | 737 » » » *val++ = (int)(msbs >> 1) ^ -(int)(msbs & 1); |
| 1008 | |
| 1009 » » /* read unary part */ | |
| 1010 » » while(1) { | |
| 1011 » » » while(cwords < br->words) { /* if we've not consumed up
to a partial tail word... */ | |
| 1012 » » » » brword b = br->buffer[cwords] << cbits; | |
| 1013 » » » » if(b) { | |
| 1014 #if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined F
LAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__ | |
| 1015 » » » » » asm volatile ( | |
| 1016 » » » » » » "bsrl %1, %0;" | |
| 1017 » » » » » » "notl %0;" | |
| 1018 » » » » » » "andl $31, %0;" | |
| 1019 » » » » » » : "=r"(i) | |
| 1020 » » » » » » : "r"(b) | |
| 1021 » » » » » ); | |
| 1022 #else | |
| 1023 » » » » » i = COUNT_ZERO_MSBS(b); | |
| 1024 #endif | |
| 1025 » » » » » uval += i; | |
| 1026 » » » » » cbits += i; | |
| 1027 » » » » » cbits++; /* skip over stop bit */ | |
| 1028 » » » » » if(cbits >= FLAC__BITS_PER_WORD) { /* fa
ster way of testing if(cbits == FLAC__BITS_PER_WORD) */ | |
| 1029 » » » » » » crc16_update_word_(br, br->buffe
r[cwords]); | |
| 1030 » » » » » » cwords++; | |
| 1031 » » » » » » cbits = 0; | |
| 1032 » » » » » } | |
| 1033 » » » » » goto break1; | |
| 1034 » » » » } | |
| 1035 » » » » else { | |
| 1036 » » » » » uval += FLAC__BITS_PER_WORD - cbits; | |
| 1037 » » » » » crc16_update_word_(br, br->buffer[cwords
]); | |
| 1038 » » » » » cwords++; | |
| 1039 » » » » » cbits = 0; | |
| 1040 » » » » » /* didn't find stop bit yet, have to kee
p going... */ | |
| 1041 » » » » } | |
| 1042 » » » } | |
| 1043 » » » /* at this point we've eaten up all the whole words; hav
e to try | |
| 1044 » » » * reading through any tail bytes before calling the rea
d callback. | |
| 1045 » » » * this is a repeat of the above logic adjusted for the
fact we | |
| 1046 » » » * don't have a whole word. note though if the client i
s feeding | |
| 1047 » » » * us data a byte at a time (unlikely), br->consumed_bit
s may not | |
| 1048 » » » * be zero. | |
| 1049 » » » */ | |
| 1050 » » » if(br->bytes) { | |
| 1051 » » » » const unsigned end = br->bytes * 8; | |
| 1052 » » » » brword b = (br->buffer[cwords] & ~(FLAC__WORD_AL
L_ONES >> end)) << cbits; | |
| 1053 » » » » if(b) { | |
| 1054 » » » » » i = COUNT_ZERO_MSBS(b); | |
| 1055 » » » » » uval += i; | |
| 1056 » » » » » cbits += i; | |
| 1057 » » » » » cbits++; /* skip over stop bit */ | |
| 1058 » » » » » FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD
); | |
| 1059 » » » » » goto break1; | |
| 1060 » » » » } | |
| 1061 » » » » else { | |
| 1062 » » » » » uval += end - cbits; | |
| 1063 » » » » » cbits += end; | |
| 1064 » » » » » FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD
); | |
| 1065 » » » » » /* didn't find stop bit yet, have to kee
p going... */ | |
| 1066 » » » » } | |
| 1067 » » » } | |
| 1068 » » » /* flush registers and read; bitreader_read_from_client_
() does | |
| 1069 » » » * not touch br->consumed_bits at all but we still need
to set | |
| 1070 » » » * it in case it fails and we have to return false. | |
| 1071 » » » */ | |
| 1072 » » » br->consumed_bits = cbits; | |
| 1073 » » » br->consumed_words = cwords; | |
| 1074 » » » if(!bitreader_read_from_client_(br)) | |
| 1075 » » » » return false; | |
| 1076 » » » cwords = br->consumed_words; | |
| 1077 » » » ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->by
tes*8 - cbits + uval; | |
| 1078 » » » /* + uval to offset our count by the # of unary bits alr
eady | |
| 1079 » » » * consumed before the read, because we will add these b
ack | |
| 1080 » » » * in all at once at break1 | |
| 1081 » » » */ | |
| 1082 » » } | |
| 1083 break1: | |
| 1084 » » ucbits -= uval; | |
| 1085 » » ucbits--; /* account for stop bit */ | |
| 1086 | |
| 1087 » » /* read binary part */ | |
| 1088 » » FLAC__ASSERT(cwords <= br->words); | |
| 1089 | |
| 1090 » » if(parameter) { | |
| 1091 » » » while(ucbits < parameter) { | |
| 1092 » » » » /* flush registers and read; bitreader_read_from
_client_() does | |
| 1093 » » » » * not touch br->consumed_bits at all but we sti
ll need to set | |
| 1094 » » » » * it in case it fails and we have to return fal
se. | |
| 1095 » » » » */ | |
| 1096 » » » » br->consumed_bits = cbits; | |
| 1097 » » » » br->consumed_words = cwords; | |
| 1098 » » » » if(!bitreader_read_from_client_(br)) | |
| 1099 » » » » » return false; | |
| 1100 » » » » cwords = br->consumed_words; | |
| 1101 » » » » ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD
+ br->bytes*8 - cbits; | |
| 1102 » » » } | |
| 1103 » » » if(cwords < br->words) { /* if we've not consumed up to
a partial tail word... */ | |
| 1104 » » » » if(cbits) { | |
| 1105 » » » » » /* this also works when consumed_bits==0
, it's just slower than necessary for that case */ | |
| 1106 » » » » » const unsigned n = FLAC__BITS_PER_WORD -
cbits; | |
| 1107 » » » » » const brword word = br->buffer[cwords]; | |
| 1108 » » » » » if(parameter < n) { | |
| 1109 » » » » » » uval <<= parameter; | |
| 1110 » » » » » » uval |= (word & (FLAC__WORD_ALL_
ONES >> cbits)) >> (n-parameter); | |
| 1111 » » » » » » cbits += parameter; | |
| 1112 » » » » » } | |
| 1113 » » » » » else { | |
| 1114 » » » » » » uval <<= n; | |
| 1115 » » » » » » uval |= word & (FLAC__WORD_ALL_O
NES >> cbits); | |
| 1116 » » » » » » crc16_update_word_(br, word); | |
| 1117 » » » » » » cwords++; | |
| 1118 » » » » » » cbits = parameter - n; | |
| 1119 » » » » » » if(cbits) { /* parameter > n, i.
e. if there are still bits left to read, there have to be less than 32 so they w
ill all be in the next word */ | |
| 1120 » » » » » » » uval <<= cbits; | |
| 1121 » » » » » » » uval |= (br->buffer[cwor
ds] >> (FLAC__BITS_PER_WORD-cbits)); | |
| 1122 » » » » » » } | |
| 1123 » » » » » } | |
| 1124 » » » » } | |
| 1125 » » » » else { | |
| 1126 » » » » » cbits = parameter; | |
| 1127 » » » » » uval <<= parameter; | |
| 1128 » » » » » uval |= br->buffer[cwords] >> (FLAC__BIT
S_PER_WORD-cbits); | |
| 1129 » » » » } | |
| 1130 » » » } | |
| 1131 » » » else { | |
| 1132 » » » » /* in this case we're starting our read at a par
tial tail word; | |
| 1133 » » » » * the reader has guaranteed that we have at lea
st 'parameter' | |
| 1134 » » » » * bits available to read, which makes this case
simpler. | |
| 1135 » » » » */ | |
| 1136 » » » » uval <<= parameter; | |
| 1137 » » » » if(cbits) { | |
| 1138 » » » » » /* this also works when consumed_bits==0
, it's just a little slower than necessary for that case */ | |
| 1139 » » » » » FLAC__ASSERT(cbits + parameter <= br->by
tes*8); | |
| 1140 » » » » » uval |= (br->buffer[cwords] & (FLAC__WOR
D_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter); | |
| 1141 » » » » » cbits += parameter; | |
| 1142 » » » » } | |
| 1143 » » » » else { | |
| 1144 » » » » » cbits = parameter; | |
| 1145 » » » » » uval |= br->buffer[cwords] >> (FLAC__BIT
S_PER_WORD-cbits); | |
| 1146 » » » » } | |
| 1147 » » » } | |
| 1148 } | 738 } |
| 1149 | 739 |
| 1150 » » ucbits -= parameter; | 740 » » return true; |
| 741 » } |
| 742 |
| 743 » FLAC__ASSERT(parameter > 0); |
| 744 |
| 745 » cwords = br->consumed_words; |
| 746 » words = br->words; |
| 747 |
| 748 » /* if we've not consumed up to a partial tail word... */ |
| 749 » if(cwords >= words) { |
| 750 » » x = 0; |
| 751 » » goto process_tail; |
| 752 » } |
| 753 |
| 754 » ucbits = FLAC__BITS_PER_WORD - br->consumed_bits; |
| 755 » b = br->buffer[cwords] << br->consumed_bits; /* keep unconsumed bits al
igned to left */ |
| 756 |
| 757 » while(val < end) { |
| 758 » » /* read the unary MSBs and end bit */ |
| 759 » » x = y = FLAC__clz2_uint32(b); |
| 760 » » if(x == FLAC__BITS_PER_WORD) { |
| 761 » » » x = ucbits; |
| 762 » » » do { |
| 763 » » » » /* didn't find stop bit yet, have to keep going.
.. */ |
| 764 » » » » crc16_update_word_(br, br->buffer[cwords++]); |
| 765 » » » » if (cwords >= words) |
| 766 » » » » » goto incomplete_msbs; |
| 767 » » » » b = br->buffer[cwords]; |
| 768 » » » » y = FLAC__clz2_uint32(b); |
| 769 » » » » x += y; |
| 770 » » » } while(y == FLAC__BITS_PER_WORD); |
| 771 » » } |
| 772 » » b <<= y; |
| 773 » » b <<= 1; /* account for stop bit */ |
| 774 » » ucbits = (ucbits - x - 1) % FLAC__BITS_PER_WORD; |
| 775 » » msbs = x; |
| 776 |
| 777 » » /* read the binary LSBs */ |
| 778 » » x = b >> (FLAC__BITS_PER_WORD - parameter); |
| 779 » » if(parameter <= ucbits) { |
| 780 » » » ucbits -= parameter; |
| 781 » » » b <<= parameter; |
| 782 » » } else { |
| 783 » » » /* there are still bits left to read, they will all be i
n the next word */ |
| 784 » » » crc16_update_word_(br, br->buffer[cwords++]); |
| 785 » » » if (cwords >= words) |
| 786 » » » » goto incomplete_lsbs; |
| 787 » » » b = br->buffer[cwords]; |
| 788 » » » ucbits += FLAC__BITS_PER_WORD - parameter; |
| 789 » » » x |= b >> ucbits; |
| 790 » » » b <<= FLAC__BITS_PER_WORD - ucbits; |
| 791 » » } |
| 792 » » lsbs = x; |
| 1151 | 793 |
| 1152 /* compose the value */ | 794 /* compose the value */ |
| 1153 » » *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); | 795 » » x = (msbs << parameter) | lsbs; |
| 796 » » *val++ = (int)(x >> 1) ^ -(int)(x & 1); |
| 1154 | 797 |
| 1155 » » /* are we done? */ | 798 » » continue; |
| 1156 » » --nvals; | |
| 1157 » » if(nvals == 0) { | |
| 1158 » » » br->consumed_bits = cbits; | |
| 1159 » » » br->consumed_words = cwords; | |
| 1160 » » » return true; | |
| 1161 » » } | |
| 1162 | 799 |
| 1163 » » uval = 0; | 800 » » /* at this point we've eaten up all the whole words */ |
| 1164 » » ++vals; | 801 process_tail: |
| 802 » » do { |
| 803 » » » if(0) { |
| 804 incomplete_msbs: |
| 805 » » » » br->consumed_bits = 0; |
| 806 » » » » br->consumed_words = cwords; |
| 807 » » » } |
| 1165 | 808 |
| 809 /* read the unary MSBs and end bit */ |
| 810 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) |
| 811 return false; |
| 812 msbs += x; |
| 813 x = ucbits = 0; |
| 814 |
| 815 if(0) { |
| 816 incomplete_lsbs: |
| 817 br->consumed_bits = 0; |
| 818 br->consumed_words = cwords; |
| 819 } |
| 820 |
| 821 /* read the binary LSBs */ |
| 822 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter
- ucbits)) |
| 823 return false; |
| 824 lsbs = x | lsbs; |
| 825 |
| 826 /* compose the value */ |
| 827 x = (msbs << parameter) | lsbs; |
| 828 *val++ = (int)(x >> 1) ^ -(int)(x & 1); |
| 829 x = 0; |
| 830 |
| 831 cwords = br->consumed_words; |
| 832 words = br->words; |
| 833 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits; |
| 834 b = br->buffer[cwords] << br->consumed_bits; |
| 835 } while(cwords >= words && val < end); |
| 1166 } | 836 } |
| 837 |
| 838 if(ucbits == 0 && cwords < words) { |
| 839 /* don't leave the head word with no unconsumed bits */ |
| 840 crc16_update_word_(br, br->buffer[cwords++]); |
| 841 ucbits = FLAC__BITS_PER_WORD; |
| 842 } |
| 843 |
| 844 br->consumed_bits = FLAC__BITS_PER_WORD - ucbits; |
| 845 br->consumed_words = cwords; |
| 846 |
| 847 return true; |
| 1167 } | 848 } |
| 1168 #endif | |
| 1169 | 849 |
| 1170 #if 0 /* UNUSED */ | 850 #if 0 /* UNUSED */ |
| 1171 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, uns
igned parameter) | 851 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, uns
igned parameter) |
| 1172 { | 852 { |
| 1173 FLAC__uint32 lsbs = 0, msbs = 0; | 853 FLAC__uint32 lsbs = 0, msbs = 0; |
| 1174 unsigned bit, uval, k; | 854 unsigned bit, uval, k; |
| 1175 | 855 |
| 1176 FLAC__ASSERT(0 != br); | 856 FLAC__ASSERT(0 != br); |
| 1177 FLAC__ASSERT(0 != br->buffer); | 857 FLAC__ASSERT(0 != br->buffer); |
| 1178 | 858 |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1357 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ | 1037 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ |
| 1358 *val = FLAC__U64L(0xffffffffffffffff); | 1038 *val = FLAC__U64L(0xffffffffffffffff); |
| 1359 return true; | 1039 return true; |
| 1360 } | 1040 } |
| 1361 v <<= 6; | 1041 v <<= 6; |
| 1362 v |= (x & 0x3F); | 1042 v |= (x & 0x3F); |
| 1363 } | 1043 } |
| 1364 *val = v; | 1044 *val = v; |
| 1365 return true; | 1045 return true; |
| 1366 } | 1046 } |
| 1047 |
| 1048 /* These functions are declared inline in this file but are also callable as |
| 1049 * externs from elsewhere. |
| 1050 * According to the C99 spec, section 6.7.4, simply providing a function |
| 1051 * prototype in a header file without 'inline' and making the function inline |
| 1052 * in this file should be sufficient. |
| 1053 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To |
| 1054 * fix that we add extern declarations here. |
| 1055 */ |
| 1056 extern FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader
*br); |
| 1057 extern unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitRead
er *br); |
| 1058 extern unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader
*br); |
| 1059 extern FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br,
FLAC__uint32 *val); |
| OLD | NEW |