1
/* libFLAC - Free Lossless Audio Codec library
2
* Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
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.
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.
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.
36
#include <stdlib.h> /* for malloc() */
37
#include <string.h> /* for memcpy(), memset() */
38
#if defined(_MSC_VER) && _MSC_VER <= 1200
39
#include <winsock.h> /* for ntohl() */
40
#elif defined FLAC__SYS_DARWIN
41
#include <machine/endian.h> /* for ntohl() */
43
#include <netinet/in.h> /* for ntohl() */
45
#include "private/bitmath.h"
46
#include "private/bitreader.h"
47
#include "private/crc.h"
48
#include "FLAC/assert.h"
51
* Along the way you will see two versions of some functions, selected
52
* by a FLAC__NO_MANUAL_INLINING macro. One is the simplified, more
53
* readable, and slow version, and the other is the same function
54
* where crucial parts have been manually inlined and are much faster.
58
/* Things should be fastest when this matches the machine word size */
59
/* WATCHOUT: if you change this you must also change the following #defines down to ALIGNED_UNARY_BITS below to match */
60
/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
61
/* also, some sections currently only have fast versions for 4 or 8 bytes per word */
62
typedef FLAC__uint32 brword;
63
#define FLAC__BYTES_PER_WORD 4
64
#define FLAC__BITS_PER_WORD 32
65
#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
66
#define FLAC__WORD_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
67
/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
69
#define SWAP_BE_WORD_TO_HOST(x) (x)
72
#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
74
#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
77
/* counts the # of zero MSBs in a word */
78
#define ALIGNED_UNARY_BITS(word) ( \
80
( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
81
( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
83
/* this alternate might be slightly faster on some systems/compilers: */
84
#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])) )
88
* This should be at least twice as large as the largest number of words
89
* required to represent any 'number' (in any encoding) you are going to
90
* read. With FLAC this is on the order of maybe a few hundred bits.
91
* If the buffer is smaller than that, the decoder won't be able to read
92
* in a whole number that is in a variable length encoding (e.g. Rice).
93
* But to be practical it should be at least 1K bytes.
95
* Increase this number to decrease the number of read callbacks, at the
96
* expense of using more memory. Or decrease for the reverse effect,
97
* keeping in mind the limit from the first paragraph. The optimal size
98
* also depends on the CPU cache size and other factors; some twiddling
99
* may be necessary to squeeze out the best performance.
101
static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
103
static const unsigned char byte_to_unary_table[] = {
104
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
105
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
106
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
107
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
108
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
109
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
110
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
111
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
112
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
125
#define min(x,y) ((x)<(y)?(x):(y))
129
#define max(x,y) ((x)>(y)?(x):(y))
131
/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
133
#define FLAC__U64L(x) x
135
#define FLAC__U64L(x) x##LLU
142
struct FLAC__BitReader {
143
/* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
144
/* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
146
unsigned capacity; /* in words */
147
unsigned words; /* # of completed words in buffer */
148
unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
149
unsigned consumed_words, consumed_bits; /* #words+(#bits of head word) already consumed from the front of buffer */
150
unsigned read_crc16; /* the running frame CRC */
151
unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
152
FLAC__BitReaderReadCallback read_callback;
157
/* OPT: an MSVC built-in would be better */
158
static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
160
x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
161
return (x>>16) | (x<<16);
165
static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
167
register unsigned crc = br->read_crc16;
168
#if FLAC__BYTES_PER_WORD == 4
169
switch(br->crc16_align) {
170
case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
171
case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
172
case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
173
case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
175
#elif FLAC__BYTES_PER_WORD == 8
176
switch(br->crc16_align) {
177
case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
178
case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
179
case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
180
case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
181
case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
182
case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
183
case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
184
case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
187
for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
188
crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
189
br->read_crc16 = crc;
194
static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
200
/* first shift the unconsumed buffer data toward the front as much as possible */
201
if(br->consumed_words > 0) {
202
start = br->consumed_words;
203
end = br->words + (br->bytes? 1:0);
204
memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
207
br->consumed_words = 0;
211
* set the target for reading, taking into account word alignment and endianness
213
bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
215
return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
216
target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
218
/* before reading, if the existing reader looks like this (say brword is 32 bits wide)
219
* bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
220
* buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
221
* buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
222
* ^^-------target, bytes=3
223
* on LE machines, have to byteswap the odd tail word so nothing is
229
br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
232
/* now it looks like:
233
* bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
234
* buffer[BE]: 11 22 33 44 55 ?? ?? ??
235
* buffer[LE]: 44 33 22 11 55 ?? ?? ??
236
* ^^-------target, bytes=3
239
/* read in the data; note that the callback may return a smaller number of bytes */
240
if(!br->read_callback(target, &bytes, br->client_data))
243
/* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
244
* bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
245
* buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
246
* buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
247
* now have to byteswap on LE machines:
251
end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
252
for(start = br->words; start < end; start++)
253
br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
256
/* now it looks like:
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 88 77 66 55 CC BB AA 99 ?? FF EE DD
260
* finally we'll update the reader values:
262
end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
263
br->words = end / FLAC__BYTES_PER_WORD;
264
br->bytes = end % FLAC__BYTES_PER_WORD;
269
/***********************************************************************
271
* Class constructor/destructor
273
***********************************************************************/
275
FLAC__BitReader *FLAC__bitreader_new(void)
277
FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
280
memset(br, 0, sizeof(FLAC__BitReader));
283
br->words = br->bytes = 0;
284
br->consumed_words = br->consumed_bits = 0;
285
br->read_callback = 0;
291
void FLAC__bitreader_delete(FLAC__BitReader *br)
293
FLAC__ASSERT(0 != br);
295
FLAC__bitreader_free(br);
299
/***********************************************************************
301
* Public class methods
303
***********************************************************************/
305
FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd)
307
FLAC__ASSERT(0 != br);
309
br->words = br->bytes = 0;
310
br->consumed_words = br->consumed_bits = 0;
311
br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
312
br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
315
br->read_callback = rcb;
316
br->client_data = cd;
321
void FLAC__bitreader_free(FLAC__BitReader *br)
323
FLAC__ASSERT(0 != br);
329
br->words = br->bytes = 0;
330
br->consumed_words = br->consumed_bits = 0;
331
br->read_callback = 0;
335
FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
337
br->words = br->bytes = 0;
338
br->consumed_words = br->consumed_bits = 0;
342
void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
346
fprintf(out, "bitreader is NULL\n");
349
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);
351
for(i = 0; i < br->words; i++) {
352
fprintf(out, "%08X: ", i);
353
for(j = 0; j < FLAC__BITS_PER_WORD; j++)
354
if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
357
fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
361
fprintf(out, "%08X: ", i);
362
for(j = 0; j < br->bytes*8; j++)
363
if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
366
fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
372
void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
374
FLAC__ASSERT(0 != br);
375
FLAC__ASSERT(0 != br->buffer);
376
FLAC__ASSERT((br->consumed_bits & 7) == 0);
378
br->read_crc16 = (unsigned)seed;
379
br->crc16_align = br->consumed_bits;
382
FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
384
FLAC__ASSERT(0 != br);
385
FLAC__ASSERT(0 != br->buffer);
386
FLAC__ASSERT((br->consumed_bits & 7) == 0);
387
FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
389
/* CRC any tail bytes in a partially-consumed word */
390
if(br->consumed_bits) {
391
const brword tail = br->buffer[br->consumed_words];
392
for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
393
br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
395
return br->read_crc16;
398
FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
400
return ((br->consumed_bits & 7) == 0);
403
FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
405
return 8 - (br->consumed_bits & 7);
408
FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
410
return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
413
FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
415
FLAC__ASSERT(0 != br);
416
FLAC__ASSERT(0 != br->buffer);
418
FLAC__ASSERT(bits <= 32);
419
FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
420
FLAC__ASSERT(br->consumed_words <= br->words);
422
/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
423
FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
425
if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
430
while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
431
if(!bitreader_read_from_client_(br))
434
if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
435
/* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
436
if(br->consumed_bits) {
437
/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
438
const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
439
const brword word = br->buffer[br->consumed_words];
441
*val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
442
br->consumed_bits += bits;
445
*val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
447
crc16_update_word_(br, word);
448
br->consumed_words++;
449
br->consumed_bits = 0;
450
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 */
452
*val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
453
br->consumed_bits = bits;
458
const brword word = br->buffer[br->consumed_words];
459
if(bits < FLAC__BITS_PER_WORD) {
460
*val = word >> (FLAC__BITS_PER_WORD-bits);
461
br->consumed_bits = bits;
464
/* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
466
crc16_update_word_(br, word);
467
br->consumed_words++;
472
/* in this case we're starting our read at a partial tail word;
473
* the reader has guaranteed that we have at least 'bits' bits
474
* available to read, which makes this case simpler.
476
/* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
477
if(br->consumed_bits) {
478
/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
479
FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
480
*val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
481
br->consumed_bits += bits;
485
*val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
486
br->consumed_bits += bits;
492
FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
494
/* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
495
if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
503
FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
508
if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
510
if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
517
if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
524
FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
526
FLAC__uint32 x8, x32 = 0;
528
/* this doesn't need to be that fast as currently it is only used for vorbis comments */
530
if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
533
if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
537
if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
541
if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
549
FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
552
* OPT: a faster implementation is possible but probably not that useful
553
* since this is only called a couple of times in the metadata readers.
555
FLAC__ASSERT(0 != br);
556
FLAC__ASSERT(0 != br->buffer);
559
const unsigned n = br->consumed_bits & 7;
565
if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
571
if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
576
if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
584
FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
588
FLAC__ASSERT(0 != br);
589
FLAC__ASSERT(0 != br->buffer);
590
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
592
/* step 1: skip over partial head word to get word aligned */
593
while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
594
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
600
/* step 2: skip whole words in chunks */
601
while(nvals >= FLAC__BYTES_PER_WORD) {
602
if(br->consumed_words < br->words) {
603
br->consumed_words++;
604
nvals -= FLAC__BYTES_PER_WORD;
606
else if(!bitreader_read_from_client_(br))
609
/* step 3: skip any remainder from partial tail bytes */
611
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
619
FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
623
FLAC__ASSERT(0 != br);
624
FLAC__ASSERT(0 != br->buffer);
625
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
627
/* step 1: read from partial head word to get word aligned */
628
while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
629
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
631
*val++ = (FLAC__byte)x;
636
/* step 2: read whole words in chunks */
637
while(nvals >= FLAC__BYTES_PER_WORD) {
638
if(br->consumed_words < br->words) {
639
const brword word = br->buffer[br->consumed_words++];
640
#if FLAC__BYTES_PER_WORD == 4
641
val[0] = (FLAC__byte)(word >> 24);
642
val[1] = (FLAC__byte)(word >> 16);
643
val[2] = (FLAC__byte)(word >> 8);
644
val[3] = (FLAC__byte)word;
645
#elif FLAC__BYTES_PER_WORD == 8
646
val[0] = (FLAC__byte)(word >> 56);
647
val[1] = (FLAC__byte)(word >> 48);
648
val[2] = (FLAC__byte)(word >> 40);
649
val[3] = (FLAC__byte)(word >> 32);
650
val[4] = (FLAC__byte)(word >> 24);
651
val[5] = (FLAC__byte)(word >> 16);
652
val[6] = (FLAC__byte)(word >> 8);
653
val[7] = (FLAC__byte)word;
655
for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
656
val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
658
val += FLAC__BYTES_PER_WORD;
659
nvals -= FLAC__BYTES_PER_WORD;
661
else if(!bitreader_read_from_client_(br))
664
/* step 3: read any remainder from partial tail bytes */
666
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
668
*val++ = (FLAC__byte)x;
675
FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
676
#ifdef FLAC__NO_MANUAL_INLINING
680
FLAC__ASSERT(0 != br);
681
FLAC__ASSERT(0 != br->buffer);
685
if(!FLAC__bitreader_read_bit(br, &bit))
698
FLAC__ASSERT(0 != br);
699
FLAC__ASSERT(0 != br->buffer);
703
while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
704
brword b = br->buffer[br->consumed_words] << br->consumed_bits;
706
#if 0 /* too slow, but this is the idea: */
707
for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
710
i = ALIGNED_UNARY_BITS(b);
714
br->consumed_bits += i;
715
if(br->consumed_bits == FLAC__BITS_PER_WORD) {
716
crc16_update_word_(br, br->buffer[br->consumed_words]);
717
br->consumed_words++;
718
br->consumed_bits = 0;
723
*val += FLAC__BITS_PER_WORD - br->consumed_bits;
724
crc16_update_word_(br, br->buffer[br->consumed_words]);
725
br->consumed_words++;
726
br->consumed_bits = 0;
727
/* didn't find stop bit yet, have to keep going... */
730
/* at this point we've eaten up all the whole words; have to try
731
* reading through any tail bytes before calling the read callback.
732
* this is a repeat of the above logic adjusted for the fact we
733
* don't have a whole word. note though if the client is feeding
734
* us data a byte at a time (unlikely), br->consumed_bits may not
738
const unsigned end = br->bytes * 8;
739
brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
741
#if 0 /* too slow, but this is the idea: */
742
for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
745
i = ALIGNED_UNARY_BITS(b);
749
br->consumed_bits += i;
750
FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
754
*val += end - br->consumed_bits;
755
br->consumed_bits += end;
756
FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
757
/* didn't find stop bit yet, have to keep going... */
760
if(!bitreader_read_from_client_(br))
766
FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
768
FLAC__uint32 lsbs = 0, msbs = 0;
771
FLAC__ASSERT(0 != br);
772
FLAC__ASSERT(0 != br->buffer);
773
FLAC__ASSERT(parameter <= 31);
775
/* read the unary MSBs and end bit */
776
if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
779
/* read the binary LSBs */
780
if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
783
/* compose the value */
784
uval = (msbs << parameter) | lsbs;
786
*val = -((int)(uval >> 1)) - 1;
788
*val = (int)(uval >> 1);
793
/* this is by far the most heavily used reader call. it ain't pretty but it's fast */
794
/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
795
FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
799
unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
801
/* try and get br->consumed_words and br->consumed_bits into register;
802
* must remember to flush them back to *br before calling other
803
* bitwriter functions that use them, and before returning */
804
register unsigned cwords;
805
register unsigned cbits;
807
FLAC__ASSERT(0 != br);
808
FLAC__ASSERT(0 != br->buffer);
809
/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
810
FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
811
FLAC__ASSERT(parameter < 32);
812
/* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
817
cbits = br->consumed_bits;
818
cwords = br->consumed_words;
822
/* read unary part */
824
while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
825
brword b = br->buffer[cwords] << cbits;
827
#if 0 /* too slow, but this is the idea: */
828
for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
831
i = ALIGNED_UNARY_BITS(b);
837
if(cbits == FLAC__BITS_PER_WORD) {
838
crc16_update_word_(br, br->buffer[cwords]);
845
uval += FLAC__BITS_PER_WORD - cbits;
846
crc16_update_word_(br, br->buffer[cwords]);
849
/* didn't find stop bit yet, have to keep going... */
852
/* at this point we've eaten up all the whole words; have to try
853
* reading through any tail bytes before calling the read callback.
854
* this is a repeat of the above logic adjusted for the fact we
855
* don't have a whole word. note though if the client is feeding
856
* us data a byte at a time (unlikely), br->consumed_bits may not
860
const unsigned end = br->bytes * 8;
861
brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
863
#if 0 /* too slow, but this is the idea: */
864
for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
867
i = ALIGNED_UNARY_BITS(b);
873
FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
879
FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
880
/* didn't find stop bit yet, have to keep going... */
883
/* flush registers and read; bitreader_read_from_client_() does
884
* not touch br->consumed_bits at all but we still need to set
885
* it in case it fails and we have to return false.
887
br->consumed_bits = cbits;
888
br->consumed_words = cwords;
889
if(!bitreader_read_from_client_(br))
891
cwords = br->consumed_words;
894
/* read binary part */
895
FLAC__ASSERT(cwords <= br->words);
898
while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
899
/* flush registers and read; bitreader_read_from_client_() does
900
* not touch br->consumed_bits at all but we still need to set
901
* it in case it fails and we have to return false.
903
br->consumed_bits = cbits;
904
br->consumed_words = cwords;
905
if(!bitreader_read_from_client_(br))
907
cwords = br->consumed_words;
909
if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
911
/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
912
const unsigned n = FLAC__BITS_PER_WORD - cbits;
913
const brword word = br->buffer[cwords];
916
uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
921
uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
923
crc16_update_word_(br, word);
926
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 */
928
uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
934
FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
936
uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
942
/* in this case we're starting our read at a partial tail word;
943
* the reader has guaranteed that we have at least 'bits' bits
944
* available to read, which makes this case simpler.
948
/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
949
FLAC__ASSERT(cbits + bits <= br->bytes*8);
950
uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
955
uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
962
/* compose the value */
963
*vals = (int)(uval >> 1 ^ -(int)(uval & 1));
968
br->consumed_bits = cbits;
969
br->consumed_words = cwords;
980
FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
982
FLAC__uint32 lsbs = 0, msbs = 0;
983
unsigned bit, uval, k;
985
FLAC__ASSERT(0 != br);
986
FLAC__ASSERT(0 != br->buffer);
988
k = FLAC__bitmath_ilog2(parameter);
990
/* read the unary MSBs and end bit */
991
if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
994
/* read the binary LSBs */
995
if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
998
if(parameter == 1u<<k) {
999
/* compose the value */
1000
uval = (msbs << k) | lsbs;
1003
unsigned d = (1 << (k+1)) - parameter;
1005
if(!FLAC__bitreader_read_bit(br, &bit))
1011
/* compose the value */
1012
uval = msbs * parameter + lsbs;
1015
/* unfold unsigned to signed */
1017
*val = -((int)(uval >> 1)) - 1;
1019
*val = (int)(uval >> 1);
1024
FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1026
FLAC__uint32 lsbs, msbs = 0;
1029
FLAC__ASSERT(0 != br);
1030
FLAC__ASSERT(0 != br->buffer);
1032
k = FLAC__bitmath_ilog2(parameter);
1034
/* read the unary MSBs and end bit */
1035
if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1038
/* read the binary LSBs */
1039
if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1042
if(parameter == 1u<<k) {
1043
/* compose the value */
1044
*val = (msbs << k) | lsbs;
1047
unsigned d = (1 << (k+1)) - parameter;
1049
if(!FLAC__bitreader_read_bit(br, &bit))
1055
/* compose the value */
1056
*val = msbs * parameter + lsbs;
1063
/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1064
FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1070
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1073
raw[(*rawlen)++] = (FLAC__byte)x;
1074
if(!(x & 0x80)) { /* 0xxxxxxx */
1078
else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1082
else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1086
else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1090
else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1094
else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1103
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1106
raw[(*rawlen)++] = (FLAC__byte)x;
1107
if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1118
/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1119
FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1125
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1128
raw[(*rawlen)++] = (FLAC__byte)x;
1129
if(!(x & 0x80)) { /* 0xxxxxxx */
1133
else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1137
else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1141
else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1145
else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1149
else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1153
else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1158
*val = FLAC__U64L(0xffffffffffffffff);
1162
if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1165
raw[(*rawlen)++] = (FLAC__byte)x;
1166
if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1167
*val = FLAC__U64L(0xffffffffffffffff);