blob: 47aa403430c1642cfed9573ffd5da727eeefc1b5 [file] [log] [blame]
Josh Coalson423f8042007-01-28 17:40:26 +00001/* 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 Coalson67757782007-02-17 01:16:20 +000038#ifdef _MSC_VER
Josh Coalson423f8042007-01-28 17:40:26 +000039#include <winsock.h> /* for ntohl() */
Josh Coalsonf67f2062007-02-13 02:12:13 +000040#elif defined FLAC__SYS_DARWIN
41#include <machine/endian.h> /* for ntohl() */
Josh Coalson423f8042007-01-28 17:40:26 +000042#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 Coalson423f8042007-01-28 17:40:26 +000050/* 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 */
54typedef 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 Coalsonc85056b2007-02-04 02:57:48 +000063#ifdef _MSC_VER
64#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
65#else
Josh Coalson423f8042007-01-28 17:40:26 +000066#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
67#endif
Josh Coalsonc85056b2007-02-04 02:57:48 +000068#endif
Josh Coalson423f8042007-01-28 17:40:26 +000069/* 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 */
93static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
94
95static 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 Coalsonc63cf412007-03-17 05:21:36 +0000134/* WATCHOUT: assembly routines rely on the order in which these fields are declared */
Josh Coalson423f8042007-01-28 17:40:26 +0000135struct 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 Coalsonc63cf412007-03-17 05:21:36 +0000142 unsigned consumed_words; /* #words ... */
143 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000144 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 Coalson65454092007-03-13 16:14:36 +0000148 FLAC__CPUInfo cpu_info;
Josh Coalson423f8042007-01-28 17:40:26 +0000149};
150
Josh Coalsonc85056b2007-02-04 02:57:48 +0000151#ifdef _MSC_VER
152/* OPT: an MSVC built-in would be better */
153static _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 Coalson65454092007-03-13 16:14:36 +0000158static 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
164loop1:
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
172done1:
173 }
174}
Josh Coalsonc85056b2007-02-04 02:57:48 +0000175#endif
176
Josh Coalson423f8042007-01-28 17:40:26 +0000177static 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 Coalsonc63cf412007-03-17 05:21:36 +0000206/* would be static except it needs to be called by asm routines */
207FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000208{
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 Coalson65454092007-03-13 16:14:36 +0000265# 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 Coalson423f8042007-01-28 17:40:26 +0000272 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 Coalsone3ec2ad2007-01-31 03:53:22 +0000295FLAC__BitReader *FLAC__bitreader_new(void)
Josh Coalson423f8042007-01-28 17:40:26 +0000296{
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
311void 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 Coalson65454092007-03-13 16:14:36 +0000325FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
Josh Coalson423f8042007-01-28 17:40:26 +0000326{
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 Coalson65454092007-03-13 16:14:36 +0000337 br->cpu_info = cpu;
Josh Coalson423f8042007-01-28 17:40:26 +0000338
339 return true;
340}
341
342void 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
356FLAC__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
363void 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
393void 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
403FLAC__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 Coalson423f8042007-01-28 17:40:26 +0000413 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
419FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
420{
421 return ((br->consumed_bits & 7) == 0);
422}
423
424FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
425{
426 return 8 - (br->consumed_bits & 7);
427}
428
429FLaC__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
434FLaC__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
513FLAC__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
524FLAC__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
545FLaC__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
570FLAC__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
605FLAC__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
640FLAC__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
696FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
Josh Coalson65454092007-03-13 16:14:36 +0000697#if 0 /* slow but readable version */
Josh Coalson423f8042007-01-28 17:40:26 +0000698{
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;
Josh Coalson276d6162007-03-22 03:20:12 +0000736 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
Josh Coalson423f8042007-01-28 17:40:26 +0000737 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
787FLAC__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() */
816FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
817{
818 unsigned i;
819 unsigned uval = 0;
Josh Coalson423f8042007-01-28 17:40:26 +0000820
821 /* try and get br->consumed_words and br->consumed_bits into register;
822 * must remember to flush them back to *br before calling other
823 * bitwriter functions that use them, and before returning */
824 register unsigned cwords;
825 register unsigned cbits;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000826 unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000827
828 FLAC__ASSERT(0 != br);
829 FLAC__ASSERT(0 != br->buffer);
830 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
831 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
832 FLAC__ASSERT(parameter < 32);
Josh Coalsonc63cf412007-03-17 05:21:36 +0000833 /* 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 Coalson423f8042007-01-28 17:40:26 +0000834
835 if(nvals == 0)
836 return true;
837
838 cbits = br->consumed_bits;
839 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000840 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000841
842 while(1) {
843
844 /* read unary part */
845 while(1) {
846 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
847 brword b = br->buffer[cwords] << cbits;
848 if(b) {
849#if 0 /* too slow, but this is the idea: */
850 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
851 b <<= 1;
852#else
853 i = ALIGNED_UNARY_BITS(b);
854#endif
855 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +0000856 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000857 cbits++; /* skip over stop bit */
Josh Coalson276d6162007-03-22 03:20:12 +0000858 if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
Josh Coalson423f8042007-01-28 17:40:26 +0000859 crc16_update_word_(br, br->buffer[cwords]);
860 cwords++;
861 cbits = 0;
862 }
863 goto break1;
864 }
865 else {
866 uval += FLAC__BITS_PER_WORD - cbits;
867 crc16_update_word_(br, br->buffer[cwords]);
868 cwords++;
869 cbits = 0;
870 /* didn't find stop bit yet, have to keep going... */
871 }
872 }
873 /* at this point we've eaten up all the whole words; have to try
874 * reading through any tail bytes before calling the read callback.
875 * this is a repeat of the above logic adjusted for the fact we
876 * don't have a whole word. note though if the client is feeding
877 * us data a byte at a time (unlikely), br->consumed_bits may not
878 * be zero.
879 */
880 if(br->bytes) {
881 const unsigned end = br->bytes * 8;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000882 brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000883 if(b) {
884#if 0 /* too slow, but this is the idea: */
885 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
886 b <<= 1;
887#else
888 i = ALIGNED_UNARY_BITS(b);
889#endif
890 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +0000891 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000892 cbits++; /* skip over stop bit */
Josh Coalson423f8042007-01-28 17:40:26 +0000893 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
894 goto break1;
895 }
896 else {
897 uval += end - cbits;
898 cbits += end;
899 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
900 /* didn't find stop bit yet, have to keep going... */
901 }
902 }
903 /* flush registers and read; bitreader_read_from_client_() does
904 * not touch br->consumed_bits at all but we still need to set
905 * it in case it fails and we have to return false.
906 */
907 br->consumed_bits = cbits;
908 br->consumed_words = cwords;
909 if(!bitreader_read_from_client_(br))
910 return false;
911 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000912 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
913 /* + uval to offset our count by the # of unary bits already
914 * consumed before the read, because we will add these back
915 * in all at once at break1
916 */
Josh Coalson423f8042007-01-28 17:40:26 +0000917 }
918break1:
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000919 ucbits -= uval;
920 ucbits--; /* account for stop bit */
921
Josh Coalson423f8042007-01-28 17:40:26 +0000922 /* read binary part */
923 FLAC__ASSERT(cwords <= br->words);
924
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +0000925 if(parameter) {
926 while(ucbits < parameter) {
Josh Coalson423f8042007-01-28 17:40:26 +0000927 /* flush registers and read; bitreader_read_from_client_() does
928 * not touch br->consumed_bits at all but we still need to set
929 * it in case it fails and we have to return false.
930 */
931 br->consumed_bits = cbits;
932 br->consumed_words = cwords;
933 if(!bitreader_read_from_client_(br))
934 return false;
935 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000936 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000937 }
938 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
939 if(cbits) {
940 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
941 const unsigned n = FLAC__BITS_PER_WORD - cbits;
942 const brword word = br->buffer[cwords];
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +0000943 if(parameter < n) {
944 uval <<= parameter;
945 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
946 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +0000947 goto break2;
948 }
949 uval <<= n;
950 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
Josh Coalson423f8042007-01-28 17:40:26 +0000951 crc16_update_word_(br, word);
952 cwords++;
Josh Coalson276d6162007-03-22 03:20:12 +0000953 cbits = parameter - n;
954 if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
955 uval <<= cbits;
956 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
Josh Coalson423f8042007-01-28 17:40:26 +0000957 }
958 goto break2;
959 }
960 else {
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +0000961 cbits = parameter;
Josh Coalson276d6162007-03-22 03:20:12 +0000962 uval <<= parameter;
963 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +0000964 goto break2;
965 }
966 }
967 else {
968 /* in this case we're starting our read at a partial tail word;
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +0000969 * the reader has guaranteed that we have at least 'parameter'
970 * bits available to read, which makes this case simpler.
Josh Coalson423f8042007-01-28 17:40:26 +0000971 */
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +0000972 uval <<= parameter;
Josh Coalson423f8042007-01-28 17:40:26 +0000973 if(cbits) {
974 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +0000975 FLAC__ASSERT(cbits + parameter <= br->bytes*8);
976 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
977 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +0000978 goto break2;
979 }
980 else {
Josh Coalson276d6162007-03-22 03:20:12 +0000981 cbits = parameter;
982 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +0000983 goto break2;
984 }
985 }
986 }
987break2:
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000988 ucbits -= parameter;
989
Josh Coalson423f8042007-01-28 17:40:26 +0000990 /* compose the value */
991 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
992
993 /* are we done? */
994 --nvals;
995 if(nvals == 0) {
996 br->consumed_bits = cbits;
997 br->consumed_words = cwords;
998 return true;
999 }
1000
1001 uval = 0;
1002 ++vals;
1003
1004 }
1005}
1006
1007#if 0 /* UNUSED */
1008FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1009{
1010 FLAC__uint32 lsbs = 0, msbs = 0;
1011 unsigned bit, uval, k;
1012
1013 FLAC__ASSERT(0 != br);
1014 FLAC__ASSERT(0 != br->buffer);
1015
1016 k = FLAC__bitmath_ilog2(parameter);
1017
1018 /* read the unary MSBs and end bit */
1019 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1020 return false;
1021
1022 /* read the binary LSBs */
1023 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1024 return false;
1025
1026 if(parameter == 1u<<k) {
1027 /* compose the value */
1028 uval = (msbs << k) | lsbs;
1029 }
1030 else {
1031 unsigned d = (1 << (k+1)) - parameter;
1032 if(lsbs >= d) {
1033 if(!FLAC__bitreader_read_bit(br, &bit))
1034 return false;
1035 lsbs <<= 1;
1036 lsbs |= bit;
1037 lsbs -= d;
1038 }
1039 /* compose the value */
1040 uval = msbs * parameter + lsbs;
1041 }
1042
1043 /* unfold unsigned to signed */
1044 if(uval & 1)
1045 *val = -((int)(uval >> 1)) - 1;
1046 else
1047 *val = (int)(uval >> 1);
1048
1049 return true;
1050}
1051
1052FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1053{
1054 FLAC__uint32 lsbs, msbs = 0;
1055 unsigned bit, k;
1056
1057 FLAC__ASSERT(0 != br);
1058 FLAC__ASSERT(0 != br->buffer);
1059
1060 k = FLAC__bitmath_ilog2(parameter);
1061
1062 /* read the unary MSBs and end bit */
1063 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1064 return false;
1065
1066 /* read the binary LSBs */
1067 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1068 return false;
1069
1070 if(parameter == 1u<<k) {
1071 /* compose the value */
1072 *val = (msbs << k) | lsbs;
1073 }
1074 else {
1075 unsigned d = (1 << (k+1)) - parameter;
1076 if(lsbs >= d) {
1077 if(!FLAC__bitreader_read_bit(br, &bit))
1078 return false;
1079 lsbs <<= 1;
1080 lsbs |= bit;
1081 lsbs -= d;
1082 }
1083 /* compose the value */
1084 *val = msbs * parameter + lsbs;
1085 }
1086
1087 return true;
1088}
1089#endif /* UNUSED */
1090
1091/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1092FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1093{
1094 FLAC__uint32 v = 0;
1095 FLAC__uint32 x;
1096 unsigned i;
1097
1098 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1099 return false;
1100 if(raw)
1101 raw[(*rawlen)++] = (FLAC__byte)x;
1102 if(!(x & 0x80)) { /* 0xxxxxxx */
1103 v = x;
1104 i = 0;
1105 }
1106 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1107 v = x & 0x1F;
1108 i = 1;
1109 }
1110 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1111 v = x & 0x0F;
1112 i = 2;
1113 }
1114 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1115 v = x & 0x07;
1116 i = 3;
1117 }
1118 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1119 v = x & 0x03;
1120 i = 4;
1121 }
1122 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1123 v = x & 0x01;
1124 i = 5;
1125 }
1126 else {
1127 *val = 0xffffffff;
1128 return true;
1129 }
1130 for( ; i; i--) {
1131 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1132 return false;
1133 if(raw)
1134 raw[(*rawlen)++] = (FLAC__byte)x;
1135 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1136 *val = 0xffffffff;
1137 return true;
1138 }
1139 v <<= 6;
1140 v |= (x & 0x3F);
1141 }
1142 *val = v;
1143 return true;
1144}
1145
1146/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1147FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1148{
1149 FLAC__uint64 v = 0;
1150 FLAC__uint32 x;
1151 unsigned i;
1152
1153 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1154 return false;
1155 if(raw)
1156 raw[(*rawlen)++] = (FLAC__byte)x;
1157 if(!(x & 0x80)) { /* 0xxxxxxx */
1158 v = x;
1159 i = 0;
1160 }
1161 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1162 v = x & 0x1F;
1163 i = 1;
1164 }
1165 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1166 v = x & 0x0F;
1167 i = 2;
1168 }
1169 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1170 v = x & 0x07;
1171 i = 3;
1172 }
1173 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1174 v = x & 0x03;
1175 i = 4;
1176 }
1177 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1178 v = x & 0x01;
1179 i = 5;
1180 }
1181 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1182 v = 0;
1183 i = 6;
1184 }
1185 else {
1186 *val = FLAC__U64L(0xffffffffffffffff);
1187 return true;
1188 }
1189 for( ; i; i--) {
1190 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1191 return false;
1192 if(raw)
1193 raw[(*rawlen)++] = (FLAC__byte)x;
1194 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1195 *val = FLAC__U64L(0xffffffffffffffff);
1196 return true;
1197 }
1198 v <<= 6;
1199 v |= (x & 0x3F);
1200 }
1201 *val = v;
1202 return true;
1203}