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