~ubuntu-branches/ubuntu/raring/flac/raring

« back to all changes in this revision

Viewing changes to src/libFLAC/bitreader.c

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Kwan
  • Date: 2007-05-29 22:56:36 UTC
  • mto: (8.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20070529225636-p8lkii0r0kp50pns
Tags: upstream-1.1.4
ImportĀ upstreamĀ versionĀ 1.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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() */
 
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() */
 
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
 
 
50
/*
 
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.
 
55
 *
 
56
 */
 
57
 
 
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 */
 
68
#if WORDS_BIGENDIAN
 
69
#define SWAP_BE_WORD_TO_HOST(x) (x)
 
70
#else
 
71
#ifdef _MSC_VER
 
72
#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
 
73
#else
 
74
#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
 
75
#endif
 
76
#endif
 
77
/* counts the # of zero MSBs in a word */
 
78
#define ALIGNED_UNARY_BITS(word) ( \
 
79
        (word) <= 0xffff ? \
 
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] ) \
 
82
)
 
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])) )
 
85
 
 
86
 
 
87
/*
 
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.
 
94
 *
 
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.
 
100
 */
 
101
static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
 
102
 
 
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
 
120
};
 
121
 
 
122
#ifdef min
 
123
#undef min
 
124
#endif
 
125
#define min(x,y) ((x)<(y)?(x):(y))
 
126
#ifdef max
 
127
#undef max
 
128
#endif
 
129
#define max(x,y) ((x)>(y)?(x):(y))
 
130
 
 
131
/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
 
132
#ifdef _MSC_VER
 
133
#define FLAC__U64L(x) x
 
134
#else
 
135
#define FLAC__U64L(x) x##LLU
 
136
#endif
 
137
 
 
138
#ifndef FLaC__INLINE
 
139
#define FLaC__INLINE
 
140
#endif
 
141
 
 
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 */
 
145
        brword *buffer;
 
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;
 
153
        void *client_data;
 
154
};
 
155
 
 
156
#ifdef _MSC_VER
 
157
/* OPT: an MSVC built-in would be better */
 
158
static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
 
159
{
 
160
        x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
 
161
        return (x>>16) | (x<<16);
 
162
}
 
163
#endif
 
164
 
 
165
static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
 
166
{
 
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);
 
174
        }
 
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);
 
185
        }
 
186
#else
 
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;
 
190
#endif
 
191
        br->crc16_align = 0;
 
192
}
 
193
 
 
194
static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
 
195
{
 
196
        unsigned start, end;
 
197
        size_t bytes;
 
198
        FLAC__byte *target;
 
199
 
 
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));
 
205
 
 
206
                br->words -= start;
 
207
                br->consumed_words = 0;
 
208
        }
 
209
 
 
210
        /*
 
211
         * set the target for reading, taking into account word alignment and endianness
 
212
         */
 
213
        bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
 
214
        if(bytes == 0)
 
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;
 
217
 
 
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
 
224
         * overwritten:
 
225
         */
 
226
#if WORDS_BIGENDIAN
 
227
#else
 
228
        if(br->bytes)
 
229
                br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
 
230
#endif
 
231
 
 
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
 
237
         */
 
238
 
 
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))
 
241
                return false;
 
242
 
 
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:
 
248
         */
 
249
#if WORDS_BIGENDIAN
 
250
#else
 
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]);
 
254
#endif
 
255
 
 
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:
 
261
         */
 
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;
 
265
 
 
266
        return true;
 
267
}
 
268
 
 
269
/***********************************************************************
 
270
 *
 
271
 * Class constructor/destructor
 
272
 *
 
273
 ***********************************************************************/
 
274
 
 
275
FLAC__BitReader *FLAC__bitreader_new(void)
 
276
{
 
277
        FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
 
278
 
 
279
        /* calloc() implies:
 
280
                memset(br, 0, sizeof(FLAC__BitReader));
 
281
                br->buffer = 0;
 
282
                br->capacity = 0;
 
283
                br->words = br->bytes = 0;
 
284
                br->consumed_words = br->consumed_bits = 0;
 
285
                br->read_callback = 0;
 
286
                br->client_data = 0;
 
287
        */
 
288
        return br;
 
289
}
 
290
 
 
291
void FLAC__bitreader_delete(FLAC__BitReader *br)
 
292
{
 
293
        FLAC__ASSERT(0 != br);
 
294
 
 
295
        FLAC__bitreader_free(br);
 
296
        free(br);
 
297
}
 
298
 
 
299
/***********************************************************************
 
300
 *
 
301
 * Public class methods
 
302
 *
 
303
 ***********************************************************************/
 
304
 
 
305
FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd)
 
306
{
 
307
        FLAC__ASSERT(0 != br);
 
308
 
 
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);
 
313
        if(br->buffer == 0)
 
314
                return false;
 
315
        br->read_callback = rcb;
 
316
        br->client_data = cd;
 
317
 
 
318
        return true;
 
319
}
 
320
 
 
321
void FLAC__bitreader_free(FLAC__BitReader *br)
 
322
{
 
323
        FLAC__ASSERT(0 != br);
 
324
 
 
325
        if(0 != br->buffer)
 
326
                free(br->buffer);
 
327
        br->buffer = 0;
 
328
        br->capacity = 0;
 
329
        br->words = br->bytes = 0;
 
330
        br->consumed_words = br->consumed_bits = 0;
 
331
        br->read_callback = 0;
 
332
        br->client_data = 0;
 
333
}
 
334
 
 
335
FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
 
336
{
 
337
        br->words = br->bytes = 0;
 
338
        br->consumed_words = br->consumed_bits = 0;
 
339
        return true;
 
340
}
 
341
 
 
342
void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
 
343
{
 
344
        unsigned i, j;
 
345
        if(br == 0) {
 
346
                fprintf(out, "bitreader is NULL\n");
 
347
        }
 
348
        else {
 
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);
 
350
 
 
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))
 
355
                                        fprintf(out, ".");
 
356
                                else
 
357
                                        fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
 
358
                        fprintf(out, "\n");
 
359
                }
 
360
                if(br->bytes > 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))
 
364
                                        fprintf(out, ".");
 
365
                                else
 
366
                                        fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
 
367
                        fprintf(out, "\n");
 
368
                }
 
369
        }
 
370
}
 
371
 
 
372
void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
 
373
{
 
374
        FLAC__ASSERT(0 != br);
 
375
        FLAC__ASSERT(0 != br->buffer);
 
376
        FLAC__ASSERT((br->consumed_bits & 7) == 0);
 
377
 
 
378
        br->read_crc16 = (unsigned)seed;
 
379
        br->crc16_align = br->consumed_bits;
 
380
}
 
381
 
 
382
FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
 
383
{
 
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);
 
388
 
 
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);
 
394
        }
 
395
        return br->read_crc16;
 
396
}
 
397
 
 
398
FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
 
399
{
 
400
        return ((br->consumed_bits & 7) == 0);
 
401
}
 
402
 
 
403
FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
 
404
{
 
405
        return 8 - (br->consumed_bits & 7);
 
406
}
 
407
 
 
408
FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
 
409
{
 
410
        return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
 
411
}
 
412
 
 
413
FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
 
414
{
 
415
        FLAC__ASSERT(0 != br);
 
416
        FLAC__ASSERT(0 != br->buffer);
 
417
 
 
418
        FLAC__ASSERT(bits <= 32);
 
419
        FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
 
420
        FLAC__ASSERT(br->consumed_words <= br->words);
 
421
 
 
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);
 
424
 
 
425
        if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
 
426
                *val = 0;
 
427
                return true;
 
428
        }
 
429
 
 
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))
 
432
                        return false;
 
433
        }
 
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];
 
440
                        if(bits < n) {
 
441
                                *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
 
442
                                br->consumed_bits += bits;
 
443
                                return true;
 
444
                        }
 
445
                        *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
 
446
                        bits -= n;
 
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 */
 
451
                                *val <<= bits;
 
452
                                *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
 
453
                                br->consumed_bits = bits;
 
454
                        }
 
455
                        return true;
 
456
                }
 
457
                else {
 
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;
 
462
                                return true;
 
463
                        }
 
464
                        /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
 
465
                        *val = word;
 
466
                        crc16_update_word_(br, word);
 
467
                        br->consumed_words++;
 
468
                        return true;
 
469
                }
 
470
        }
 
471
        else {
 
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.
 
475
                 */
 
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;
 
482
                        return true;
 
483
                }
 
484
                else {
 
485
                        *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
 
486
                        br->consumed_bits += bits;
 
487
                        return true;
 
488
                }
 
489
        }
 
490
}
 
491
 
 
492
FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
 
493
{
 
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))
 
496
                return false;
 
497
        /* sign-extend: */
 
498
        *val <<= (32-bits);
 
499
        *val >>= (32-bits);
 
500
        return true;
 
501
}
 
502
 
 
503
FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
 
504
{
 
505
        FLAC__uint32 hi, lo;
 
506
 
 
507
        if(bits > 32) {
 
508
                if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
 
509
                        return false;
 
510
                if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
 
511
                        return false;
 
512
                *val = hi;
 
513
                *val <<= 32;
 
514
                *val |= lo;
 
515
        }
 
516
        else {
 
517
                if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
 
518
                        return false;
 
519
                *val = lo;
 
520
        }
 
521
        return true;
 
522
}
 
523
 
 
524
FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
 
525
{
 
526
        FLAC__uint32 x8, x32 = 0;
 
527
 
 
528
        /* this doesn't need to be that fast as currently it is only used for vorbis comments */
 
529
 
 
530
        if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
 
531
                return false;
 
532
 
 
533
        if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
 
534
                return false;
 
535
        x32 |= (x8 << 8);
 
536
 
 
537
        if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
 
538
                return false;
 
539
        x32 |= (x8 << 16);
 
540
 
 
541
        if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
 
542
                return false;
 
543
        x32 |= (x8 << 24);
 
544
 
 
545
        *val = x32;
 
546
        return true;
 
547
}
 
548
 
 
549
FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
 
550
{
 
551
        /*
 
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.
 
554
         */
 
555
        FLAC__ASSERT(0 != br);
 
556
        FLAC__ASSERT(0 != br->buffer);
 
557
 
 
558
        if(bits > 0) {
 
559
                const unsigned n = br->consumed_bits & 7;
 
560
                unsigned m;
 
561
                FLAC__uint32 x;
 
562
 
 
563
                if(n != 0) {
 
564
                        m = min(8-n, bits);
 
565
                        if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
 
566
                                return false;
 
567
                        bits -= m;
 
568
                }
 
569
                m = bits / 8;
 
570
                if(m > 0) {
 
571
                        if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
 
572
                                return false;
 
573
                        bits %= 8;
 
574
                }
 
575
                if(bits > 0) {
 
576
                        if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
 
577
                                return false;
 
578
                }
 
579
        }
 
580
 
 
581
        return true;
 
582
}
 
583
 
 
584
FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
 
585
{
 
586
        FLAC__uint32 x;
 
587
 
 
588
        FLAC__ASSERT(0 != br);
 
589
        FLAC__ASSERT(0 != br->buffer);
 
590
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
 
591
 
 
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))
 
595
                        return false;
 
596
                nvals--;
 
597
        }
 
598
        if(0 == nvals)
 
599
                return true;
 
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;
 
605
                }
 
606
                else if(!bitreader_read_from_client_(br))
 
607
                        return false;
 
608
        }
 
609
        /* step 3: skip any remainder from partial tail bytes */
 
610
        while(nvals) {
 
611
                if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
 
612
                        return false;
 
613
                nvals--;
 
614
        }
 
615
 
 
616
        return true;
 
617
}
 
618
 
 
619
FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
 
620
{
 
621
        FLAC__uint32 x;
 
622
 
 
623
        FLAC__ASSERT(0 != br);
 
624
        FLAC__ASSERT(0 != br->buffer);
 
625
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
 
626
 
 
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))
 
630
                        return false;
 
631
                *val++ = (FLAC__byte)x;
 
632
                nvals--;
 
633
        }
 
634
        if(0 == nvals)
 
635
                return true;
 
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;
 
654
#else
 
655
                        for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
 
656
                                val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
 
657
#endif
 
658
                        val += FLAC__BYTES_PER_WORD;
 
659
                        nvals -= FLAC__BYTES_PER_WORD;
 
660
                }
 
661
                else if(!bitreader_read_from_client_(br))
 
662
                        return false;
 
663
        }
 
664
        /* step 3: read any remainder from partial tail bytes */
 
665
        while(nvals) {
 
666
                if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
 
667
                        return false;
 
668
                *val++ = (FLAC__byte)x;
 
669
                nvals--;
 
670
        }
 
671
 
 
672
        return true;
 
673
}
 
674
 
 
675
FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
 
676
#ifdef FLAC__NO_MANUAL_INLINING
 
677
{
 
678
        unsigned bit;
 
679
 
 
680
        FLAC__ASSERT(0 != br);
 
681
        FLAC__ASSERT(0 != br->buffer);
 
682
 
 
683
        *val = 0;
 
684
        while(1) {
 
685
                if(!FLAC__bitreader_read_bit(br, &bit))
 
686
                        return false;
 
687
                if(bit)
 
688
                        break;
 
689
                else
 
690
                        *val++;
 
691
        }
 
692
        return true;
 
693
}
 
694
#else
 
695
{
 
696
        unsigned i;
 
697
 
 
698
        FLAC__ASSERT(0 != br);
 
699
        FLAC__ASSERT(0 != br->buffer);
 
700
 
 
701
        *val = 0;
 
702
        while(1) {
 
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;
 
705
                        if(b) {
 
706
#if 0 /* too slow, but this is the idea: */
 
707
                                for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
 
708
                                        b <<= 1;
 
709
#else
 
710
                                i = ALIGNED_UNARY_BITS(b);
 
711
#endif
 
712
                                *val += i;
 
713
                                i++;
 
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;
 
719
                                }
 
720
                                return true;
 
721
                        }
 
722
                        else {
 
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... */
 
728
                        }
 
729
                }
 
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
 
735
                 * be zero.
 
736
                 */
 
737
                if(br->bytes) {
 
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;
 
740
                        if(b) {
 
741
#if 0 /* too slow, but this is the idea: */
 
742
                                for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
 
743
                                        b <<= 1;
 
744
#else
 
745
                                i = ALIGNED_UNARY_BITS(b);
 
746
#endif
 
747
                                *val += i;
 
748
                                i++;
 
749
                                br->consumed_bits += i;
 
750
                                FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
 
751
                                return true;
 
752
                        }
 
753
                        else {
 
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... */
 
758
                        }
 
759
                }
 
760
                if(!bitreader_read_from_client_(br))
 
761
                        return false;
 
762
        }
 
763
}
 
764
#endif
 
765
 
 
766
FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
 
767
{
 
768
        FLAC__uint32 lsbs = 0, msbs = 0;
 
769
        unsigned uval;
 
770
 
 
771
        FLAC__ASSERT(0 != br);
 
772
        FLAC__ASSERT(0 != br->buffer);
 
773
        FLAC__ASSERT(parameter <= 31);
 
774
 
 
775
        /* read the unary MSBs and end bit */
 
776
        if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
 
777
                return false;
 
778
 
 
779
        /* read the binary LSBs */
 
780
        if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
 
781
                return false;
 
782
 
 
783
        /* compose the value */
 
784
        uval = (msbs << parameter) | lsbs;
 
785
        if(uval & 1)
 
786
                *val = -((int)(uval >> 1)) - 1;
 
787
        else
 
788
                *val = (int)(uval >> 1);
 
789
 
 
790
        return true;
 
791
}
 
792
 
 
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)
 
796
{
 
797
        unsigned i;
 
798
        unsigned uval = 0;
 
799
        unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
 
800
 
 
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;
 
806
 
 
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 */
 
813
 
 
814
        if(nvals == 0)
 
815
                return true;
 
816
 
 
817
        cbits = br->consumed_bits;
 
818
        cwords = br->consumed_words;
 
819
 
 
820
        while(1) {
 
821
 
 
822
                /* read unary part */
 
823
                while(1) {
 
824
                        while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
 
825
                                brword b = br->buffer[cwords] << cbits;
 
826
                                if(b) {
 
827
#if 0 /* too slow, but this is the idea: */
 
828
                                        for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
 
829
                                                b <<= 1;
 
830
#else
 
831
                                        i = ALIGNED_UNARY_BITS(b);
 
832
#endif
 
833
                                        uval += i;
 
834
                                        bits = parameter;
 
835
                                        i++;
 
836
                                        cbits += i;
 
837
                                        if(cbits == FLAC__BITS_PER_WORD) {
 
838
                                                crc16_update_word_(br, br->buffer[cwords]);
 
839
                                                cwords++;
 
840
                                                cbits = 0;
 
841
                                        }
 
842
                                        goto break1;
 
843
                                }
 
844
                                else {
 
845
                                        uval += FLAC__BITS_PER_WORD - cbits;
 
846
                                        crc16_update_word_(br, br->buffer[cwords]);
 
847
                                        cwords++;
 
848
                                        cbits = 0;
 
849
                                        /* didn't find stop bit yet, have to keep going... */
 
850
                                }
 
851
                        }
 
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
 
857
                         * be zero.
 
858
                         */
 
859
                        if(br->bytes) {
 
860
                                const unsigned end = br->bytes * 8;
 
861
                                brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
 
862
                                if(b) {
 
863
#if 0 /* too slow, but this is the idea: */
 
864
                                        for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
 
865
                                                b <<= 1;
 
866
#else
 
867
                                        i = ALIGNED_UNARY_BITS(b);
 
868
#endif
 
869
                                        uval += i;
 
870
                                        bits = parameter;
 
871
                                        i++;
 
872
                                        cbits += i;
 
873
                                        FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
 
874
                                        goto break1;
 
875
                                }
 
876
                                else {
 
877
                                        uval += end - cbits;
 
878
                                        cbits += end;
 
879
                                        FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
 
880
                                        /* didn't find stop bit yet, have to keep going... */
 
881
                                }
 
882
                        }
 
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.
 
886
                         */
 
887
                        br->consumed_bits = cbits;
 
888
                        br->consumed_words = cwords;
 
889
                        if(!bitreader_read_from_client_(br))
 
890
                                return false;
 
891
                        cwords = br->consumed_words;
 
892
                }
 
893
break1:
 
894
                /* read binary part */
 
895
                FLAC__ASSERT(cwords <= br->words);
 
896
 
 
897
                if(bits) {
 
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.
 
902
                                 */
 
903
                                br->consumed_bits = cbits;
 
904
                                br->consumed_words = cwords;
 
905
                                if(!bitreader_read_from_client_(br))
 
906
                                        return false;
 
907
                                cwords = br->consumed_words;
 
908
                        }
 
909
                        if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
 
910
                                if(cbits) {
 
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];
 
914
                                        if(bits < n) {
 
915
                                                uval <<= bits;
 
916
                                                uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
 
917
                                                cbits += bits;
 
918
                                                goto break2;
 
919
                                        }
 
920
                                        uval <<= n;
 
921
                                        uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
 
922
                                        bits -= n;
 
923
                                        crc16_update_word_(br, word);
 
924
                                        cwords++;
 
925
                                        cbits = 0;
 
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 */
 
927
                                                uval <<= bits;
 
928
                                                uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
 
929
                                                cbits = bits;
 
930
                                        }
 
931
                                        goto break2;
 
932
                                }
 
933
                                else {
 
934
                                        FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
 
935
                                        uval <<= bits;
 
936
                                        uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
 
937
                                        cbits = bits;
 
938
                                        goto break2;
 
939
                                }
 
940
                        }
 
941
                        else {
 
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.
 
945
                                 */
 
946
                                uval <<= bits;
 
947
                                if(cbits) {
 
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);
 
951
                                        cbits += bits;
 
952
                                        goto break2;
 
953
                                }
 
954
                                else {
 
955
                                        uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
 
956
                                        cbits += bits;
 
957
                                        goto break2;
 
958
                                }
 
959
                        }
 
960
                }
 
961
break2:
 
962
                /* compose the value */
 
963
                *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
 
964
 
 
965
                /* are we done? */
 
966
                --nvals;
 
967
                if(nvals == 0) {
 
968
                        br->consumed_bits = cbits;
 
969
                        br->consumed_words = cwords;
 
970
                        return true;
 
971
                }
 
972
 
 
973
                uval = 0;
 
974
                ++vals;
 
975
 
 
976
        }
 
977
}
 
978
 
 
979
#if 0 /* UNUSED */
 
980
FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
 
981
{
 
982
        FLAC__uint32 lsbs = 0, msbs = 0;
 
983
        unsigned bit, uval, k;
 
984
 
 
985
        FLAC__ASSERT(0 != br);
 
986
        FLAC__ASSERT(0 != br->buffer);
 
987
 
 
988
        k = FLAC__bitmath_ilog2(parameter);
 
989
 
 
990
        /* read the unary MSBs and end bit */
 
991
        if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
 
992
                return false;
 
993
 
 
994
        /* read the binary LSBs */
 
995
        if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
 
996
                return false;
 
997
 
 
998
        if(parameter == 1u<<k) {
 
999
                /* compose the value */
 
1000
                uval = (msbs << k) | lsbs;
 
1001
        }
 
1002
        else {
 
1003
                unsigned d = (1 << (k+1)) - parameter;
 
1004
                if(lsbs >= d) {
 
1005
                        if(!FLAC__bitreader_read_bit(br, &bit))
 
1006
                                return false;
 
1007
                        lsbs <<= 1;
 
1008
                        lsbs |= bit;
 
1009
                        lsbs -= d;
 
1010
                }
 
1011
                /* compose the value */
 
1012
                uval = msbs * parameter + lsbs;
 
1013
        }
 
1014
 
 
1015
        /* unfold unsigned to signed */
 
1016
        if(uval & 1)
 
1017
                *val = -((int)(uval >> 1)) - 1;
 
1018
        else
 
1019
                *val = (int)(uval >> 1);
 
1020
 
 
1021
        return true;
 
1022
}
 
1023
 
 
1024
FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
 
1025
{
 
1026
        FLAC__uint32 lsbs, msbs = 0;
 
1027
        unsigned bit, k;
 
1028
 
 
1029
        FLAC__ASSERT(0 != br);
 
1030
        FLAC__ASSERT(0 != br->buffer);
 
1031
 
 
1032
        k = FLAC__bitmath_ilog2(parameter);
 
1033
 
 
1034
        /* read the unary MSBs and end bit */
 
1035
        if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
 
1036
                return false;
 
1037
 
 
1038
        /* read the binary LSBs */
 
1039
        if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
 
1040
                return false;
 
1041
 
 
1042
        if(parameter == 1u<<k) {
 
1043
                /* compose the value */
 
1044
                *val = (msbs << k) | lsbs;
 
1045
        }
 
1046
        else {
 
1047
                unsigned d = (1 << (k+1)) - parameter;
 
1048
                if(lsbs >= d) {
 
1049
                        if(!FLAC__bitreader_read_bit(br, &bit))
 
1050
                                return false;
 
1051
                        lsbs <<= 1;
 
1052
                        lsbs |= bit;
 
1053
                        lsbs -= d;
 
1054
                }
 
1055
                /* compose the value */
 
1056
                *val = msbs * parameter + lsbs;
 
1057
        }
 
1058
 
 
1059
        return true;
 
1060
}
 
1061
#endif /* UNUSED */
 
1062
 
 
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)
 
1065
{
 
1066
        FLAC__uint32 v = 0;
 
1067
        FLAC__uint32 x;
 
1068
        unsigned i;
 
1069
 
 
1070
        if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
 
1071
                return false;
 
1072
        if(raw)
 
1073
                raw[(*rawlen)++] = (FLAC__byte)x;
 
1074
        if(!(x & 0x80)) { /* 0xxxxxxx */
 
1075
                v = x;
 
1076
                i = 0;
 
1077
        }
 
1078
        else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
 
1079
                v = x & 0x1F;
 
1080
                i = 1;
 
1081
        }
 
1082
        else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
 
1083
                v = x & 0x0F;
 
1084
                i = 2;
 
1085
        }
 
1086
        else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
 
1087
                v = x & 0x07;
 
1088
                i = 3;
 
1089
        }
 
1090
        else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
 
1091
                v = x & 0x03;
 
1092
                i = 4;
 
1093
        }
 
1094
        else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
 
1095
                v = x & 0x01;
 
1096
                i = 5;
 
1097
        }
 
1098
        else {
 
1099
                *val = 0xffffffff;
 
1100
                return true;
 
1101
        }
 
1102
        for( ; i; i--) {
 
1103
                if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
 
1104
                        return false;
 
1105
                if(raw)
 
1106
                        raw[(*rawlen)++] = (FLAC__byte)x;
 
1107
                if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
 
1108
                        *val = 0xffffffff;
 
1109
                        return true;
 
1110
                }
 
1111
                v <<= 6;
 
1112
                v |= (x & 0x3F);
 
1113
        }
 
1114
        *val = v;
 
1115
        return true;
 
1116
}
 
1117
 
 
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)
 
1120
{
 
1121
        FLAC__uint64 v = 0;
 
1122
        FLAC__uint32 x;
 
1123
        unsigned i;
 
1124
 
 
1125
        if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
 
1126
                return false;
 
1127
        if(raw)
 
1128
                raw[(*rawlen)++] = (FLAC__byte)x;
 
1129
        if(!(x & 0x80)) { /* 0xxxxxxx */
 
1130
                v = x;
 
1131
                i = 0;
 
1132
        }
 
1133
        else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
 
1134
                v = x & 0x1F;
 
1135
                i = 1;
 
1136
        }
 
1137
        else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
 
1138
                v = x & 0x0F;
 
1139
                i = 2;
 
1140
        }
 
1141
        else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
 
1142
                v = x & 0x07;
 
1143
                i = 3;
 
1144
        }
 
1145
        else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
 
1146
                v = x & 0x03;
 
1147
                i = 4;
 
1148
        }
 
1149
        else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
 
1150
                v = x & 0x01;
 
1151
                i = 5;
 
1152
        }
 
1153
        else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
 
1154
                v = 0;
 
1155
                i = 6;
 
1156
        }
 
1157
        else {
 
1158
                *val = FLAC__U64L(0xffffffffffffffff);
 
1159
                return true;
 
1160
        }
 
1161
        for( ; i; i--) {
 
1162
                if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
 
1163
                        return false;
 
1164
                if(raw)
 
1165
                        raw[(*rawlen)++] = (FLAC__byte)x;
 
1166
                if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
 
1167
                        *val = FLAC__U64L(0xffffffffffffffff);
 
1168
                        return true;
 
1169
                }
 
1170
                v <<= 6;
 
1171
                v |= (x & 0x3F);
 
1172
        }
 
1173
        *val = v;
 
1174
        return true;
 
1175
}