diff options
author | Dave Chapman <dave@dchapman.com> | 2005-02-16 19:33:19 +0000 |
---|---|---|
committer | Dave Chapman <dave@dchapman.com> | 2005-02-16 19:33:19 +0000 |
commit | aa97e4d4981d61808a558c5ab36be6d3bcc2c4f6 (patch) | |
tree | a66b2fcd87f37b26e2d4f360e6c2a9db53eb1b5b /apps/codecs/libFLAC/bitbuffer.c | |
parent | 9b32a1988f848145d96ba2be8cba86e837196df3 (diff) | |
download | rockbox-aa97e4d4981d61808a558c5ab36be6d3bcc2c4f6.tar.gz rockbox-aa97e4d4981d61808a558c5ab36be6d3bcc2c4f6.zip |
Initial import of libFLAC from flac-1.1.2.tar.gz
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@5983 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/libFLAC/bitbuffer.c')
-rw-r--r-- | apps/codecs/libFLAC/bitbuffer.c | 2690 |
1 files changed, 2690 insertions, 0 deletions
diff --git a/apps/codecs/libFLAC/bitbuffer.c b/apps/codecs/libFLAC/bitbuffer.c new file mode 100644 index 0000000000..8f5aefa67b --- /dev/null +++ b/apps/codecs/libFLAC/bitbuffer.c | |||
@@ -0,0 +1,2690 @@ | |||
1 | /* libFLAC - Free Lossless Audio Codec library | ||
2 | * Copyright (C) 2000,2001,2002,2003,2004,2005 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 | #include <stdlib.h> /* for malloc() */ | ||
33 | #include <string.h> /* for memcpy(), memset() */ | ||
34 | #include "private/bitbuffer.h" | ||
35 | #include "private/bitmath.h" | ||
36 | #include "private/crc.h" | ||
37 | #include "FLAC/assert.h" | ||
38 | |||
39 | /* | ||
40 | * Along the way you will see two versions of some functions, selected | ||
41 | * by a FLAC__NO_MANUAL_INLINING macro. One is the simplified, more | ||
42 | * readable, and slow version, and the other is the same function | ||
43 | * where crucial parts have been manually inlined and are much faster. | ||
44 | * | ||
45 | */ | ||
46 | |||
47 | /* | ||
48 | * Some optimization strategies are slower with older versions of MSVC | ||
49 | */ | ||
50 | #if defined _MSC_VER && _MSC_VER <= 1200 | ||
51 | #define FLAC__OLD_MSVC_FLAVOR | ||
52 | #endif | ||
53 | |||
54 | /* | ||
55 | * This should be at least twice as large as the largest number of blurbs | ||
56 | * required to represent any 'number' (in any encoding) you are going to | ||
57 | * read. With FLAC this is on the order of maybe a few hundred bits. | ||
58 | * If the buffer is smaller than that, the decoder won't be able to read | ||
59 | * in a whole number that is in a variable length encoding (e.g. Rice). | ||
60 | * | ||
61 | * The number we are actually using here is based on what would be the | ||
62 | * approximate maximum size of a verbatim frame at the default block size, | ||
63 | * for CD audio (4096 sample * 4 bytes per sample), plus some wiggle room. | ||
64 | * 32kbytes sounds reasonable. For kicks we subtract out 64 bytes for any | ||
65 | * alignment or malloc overhead. | ||
66 | * | ||
67 | * Increase this number to decrease the number of read callbacks, at the | ||
68 | * expense of using more memory. Or decrease for the reverse effect, | ||
69 | * keeping in mind the limit from the first paragraph. | ||
70 | */ | ||
71 | static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */ | ||
72 | |||
73 | #ifndef FLAC__OLD_MSVC_FLAVOR | ||
74 | static const unsigned char byte_to_unary_table[] = { | ||
75 | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, | ||
76 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | ||
77 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | ||
78 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | ||
79 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
80 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
81 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
82 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
83 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
84 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
85 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
86 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
87 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
88 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
89 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
90 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
91 | }; | ||
92 | #endif | ||
93 | |||
94 | #if FLAC__BITS_PER_BLURB == 8 | ||
95 | #define FLAC__BITS_PER_BLURB_LOG2 3 | ||
96 | #define FLAC__BYTES_PER_BLURB 1 | ||
97 | #define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff) | ||
98 | #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80) | ||
99 | #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b)) | ||
100 | #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc)); | ||
101 | #ifndef FLAC__OLD_MSVC_FLAVOR | ||
102 | #define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb]) | ||
103 | #endif | ||
104 | #elif FLAC__BITS_PER_BLURB == 32 | ||
105 | #define FLAC__BITS_PER_BLURB_LOG2 5 | ||
106 | #define FLAC__BYTES_PER_BLURB 4 | ||
107 | #define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff) | ||
108 | #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000) | ||
109 | #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b)) | ||
110 | #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb)); | ||
111 | #ifndef FLAC__OLD_MSVC_FLAVOR | ||
112 | #define FLAC__ALIGNED_BLURB_UNARY(blurb) ((blurb) <= 0xff ? byte_to_unary_table[blurb] + 24 : ((blurb) <= 0xffff ? byte_to_unary_table[(blurb) >> 8] + 16 : ((blurb) <= 0xffffff ? byte_to_unary_table[(blurb) >> 16] + 8 : byte_to_unary_table[(blurb) >> 24]))) | ||
113 | #endif | ||
114 | #else | ||
115 | /* ERROR, only sizes of 8 and 32 are supported */ | ||
116 | #endif | ||
117 | |||
118 | #define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2) | ||
119 | |||
120 | #ifdef min | ||
121 | #undef min | ||
122 | #endif | ||
123 | #define min(x,y) ((x)<(y)?(x):(y)) | ||
124 | #ifdef max | ||
125 | #undef max | ||
126 | #endif | ||
127 | #define max(x,y) ((x)>(y)?(x):(y)) | ||
128 | |||
129 | /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ | ||
130 | #ifdef _MSC_VER | ||
131 | #define FLAC__U64L(x) x | ||
132 | #else | ||
133 | #define FLAC__U64L(x) x##LLU | ||
134 | #endif | ||
135 | |||
136 | #ifndef FLaC__INLINE | ||
137 | #define FLaC__INLINE | ||
138 | #endif | ||
139 | |||
140 | struct FLAC__BitBuffer { | ||
141 | FLAC__blurb *buffer; | ||
142 | unsigned capacity; /* in blurbs */ | ||
143 | unsigned blurbs, bits; | ||
144 | unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */ | ||
145 | unsigned consumed_blurbs, consumed_bits; | ||
146 | unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */ | ||
147 | FLAC__uint16 read_crc16; | ||
148 | #if FLAC__BITS_PER_BLURB == 32 | ||
149 | unsigned crc16_align; | ||
150 | #endif | ||
151 | FLAC__blurb save_head, save_tail; | ||
152 | }; | ||
153 | |||
154 | #if FLAC__BITS_PER_BLURB == 32 | ||
155 | static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb) | ||
156 | { | ||
157 | if(bb->crc16_align == 0) { | ||
158 | FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16); | ||
159 | FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16); | ||
160 | FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16); | ||
161 | FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16); | ||
162 | } | ||
163 | else if(bb->crc16_align == 8) { | ||
164 | FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16); | ||
165 | FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16); | ||
166 | FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16); | ||
167 | } | ||
168 | else if(bb->crc16_align == 16) { | ||
169 | FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16); | ||
170 | FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16); | ||
171 | } | ||
172 | else if(bb->crc16_align == 24) { | ||
173 | FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16); | ||
174 | } | ||
175 | bb->crc16_align = 0; | ||
176 | } | ||
177 | #endif | ||
178 | |||
179 | /* | ||
180 | * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it | ||
181 | * does not shift left what is consumed, it just chops off the end, whether | ||
182 | * there is unconsumed data there or not. This is OK because currently we | ||
183 | * never shrink the buffer, but if this ever changes, we'll have to do some | ||
184 | * fixups here. | ||
185 | */ | ||
186 | static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity) | ||
187 | { | ||
188 | FLAC__blurb *new_buffer; | ||
189 | |||
190 | FLAC__ASSERT(0 != bb); | ||
191 | FLAC__ASSERT(0 != bb->buffer); | ||
192 | |||
193 | if(bb->capacity == new_capacity) | ||
194 | return true; | ||
195 | |||
196 | new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb)); | ||
197 | if(new_buffer == 0) | ||
198 | return false; | ||
199 | memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity)); | ||
200 | if(new_capacity < bb->blurbs+(bb->bits?1:0)) { | ||
201 | bb->blurbs = new_capacity; | ||
202 | bb->bits = 0; | ||
203 | bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity); | ||
204 | } | ||
205 | if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) { | ||
206 | bb->consumed_blurbs = new_capacity; | ||
207 | bb->consumed_bits = 0; | ||
208 | bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity); | ||
209 | } | ||
210 | free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */ | ||
211 | bb->buffer = new_buffer; | ||
212 | bb->capacity = new_capacity; | ||
213 | return true; | ||
214 | } | ||
215 | |||
216 | static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add) | ||
217 | { | ||
218 | unsigned new_capacity; | ||
219 | |||
220 | FLAC__ASSERT(min_blurbs_to_add > 0); | ||
221 | |||
222 | new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add); | ||
223 | return bitbuffer_resize_(bb, new_capacity); | ||
224 | } | ||
225 | |||
226 | static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add) | ||
227 | { | ||
228 | FLAC__ASSERT(0 != bb); | ||
229 | FLAC__ASSERT(0 != bb->buffer); | ||
230 | |||
231 | if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add) | ||
232 | return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2); | ||
233 | else | ||
234 | return true; | ||
235 | } | ||
236 | |||
237 | static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
238 | { | ||
239 | unsigned bytes; | ||
240 | FLAC__byte *target; | ||
241 | |||
242 | /* first shift the unconsumed buffer data toward the front as much as possible */ | ||
243 | if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) { | ||
244 | #if FLAC__BITS_PER_BLURB == 8 | ||
245 | /* | ||
246 | * memset and memcpy are usually implemented in assembly language | ||
247 | * by the system libc, and they can be much faster | ||
248 | */ | ||
249 | const unsigned r_end = bb->blurbs + (bb->bits? 1:0); | ||
250 | const unsigned r = bb->consumed_blurbs, l = r_end - r; | ||
251 | memmove(&bb->buffer[0], &bb->buffer[r], l); | ||
252 | memset(&bb->buffer[l], 0, r); | ||
253 | #elif FLAC__BITS_PER_BLURB == 32 | ||
254 | /* still needs optimization */ | ||
255 | const unsigned r_end = bb->blurbs + (bb->bits? 1:0); | ||
256 | unsigned l = 0, r = bb->consumed_blurbs; | ||
257 | for( ; r < r_end; l++, r++) | ||
258 | bb->buffer[l] = bb->buffer[r]; | ||
259 | for( ; l < r_end; l++) | ||
260 | bb->buffer[l] = 0; | ||
261 | #else | ||
262 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
263 | #endif /* FLAC__BITS_PER_BLURB == 32 or 8 */ | ||
264 | |||
265 | bb->blurbs -= bb->consumed_blurbs; | ||
266 | bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs); | ||
267 | bb->consumed_blurbs = 0; | ||
268 | bb->total_consumed_bits = bb->consumed_bits; | ||
269 | } | ||
270 | |||
271 | /* grow if we need to */ | ||
272 | if(bb->capacity <= 1) { | ||
273 | if(!bitbuffer_resize_(bb, 16)) | ||
274 | return false; | ||
275 | } | ||
276 | |||
277 | /* set the target for reading, taking into account blurb alignment */ | ||
278 | #if FLAC__BITS_PER_BLURB == 8 | ||
279 | /* blurb == byte, so no gyrations necessary: */ | ||
280 | target = bb->buffer + bb->blurbs; | ||
281 | bytes = bb->capacity - bb->blurbs; | ||
282 | #elif FLAC__BITS_PER_BLURB == 32 | ||
283 | /* @@@ WATCHOUT: code currently only works for big-endian: */ | ||
284 | FLAC__ASSERT((bb->bits & 7) == 0); | ||
285 | target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3); | ||
286 | bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */ | ||
287 | #else | ||
288 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
289 | #endif | ||
290 | |||
291 | /* finally, read in some data */ | ||
292 | if(!read_callback(target, &bytes, client_data)) | ||
293 | return false; | ||
294 | |||
295 | /* now we have to handle partial blurb cases: */ | ||
296 | #if FLAC__BITS_PER_BLURB == 8 | ||
297 | /* blurb == byte, so no gyrations necessary: */ | ||
298 | bb->blurbs += bytes; | ||
299 | bb->total_bits += FLAC__BLURBS_TO_BITS(bytes); | ||
300 | #elif FLAC__BITS_PER_BLURB == 32 | ||
301 | /* @@@ WATCHOUT: code currently only works for big-endian: */ | ||
302 | { | ||
303 | const unsigned aligned_bytes = (bb->bits >> 3) + bytes; | ||
304 | bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */ | ||
305 | bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */ | ||
306 | bb->total_bits += (bytes << 3); | ||
307 | } | ||
308 | #else | ||
309 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
310 | #endif | ||
311 | return true; | ||
312 | } | ||
313 | |||
314 | /*********************************************************************** | ||
315 | * | ||
316 | * Class constructor/destructor | ||
317 | * | ||
318 | ***********************************************************************/ | ||
319 | |||
320 | FLAC__BitBuffer *FLAC__bitbuffer_new() | ||
321 | { | ||
322 | FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer)); | ||
323 | |||
324 | /* calloc() implies: | ||
325 | memset(bb, 0, sizeof(FLAC__BitBuffer)); | ||
326 | bb->buffer = 0; | ||
327 | bb->capacity = 0; | ||
328 | bb->blurbs = bb->bits = bb->total_bits = 0; | ||
329 | bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0; | ||
330 | */ | ||
331 | return bb; | ||
332 | } | ||
333 | |||
334 | void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb) | ||
335 | { | ||
336 | FLAC__ASSERT(0 != bb); | ||
337 | |||
338 | FLAC__bitbuffer_free(bb); | ||
339 | free(bb); | ||
340 | } | ||
341 | |||
342 | /*********************************************************************** | ||
343 | * | ||
344 | * Public class methods | ||
345 | * | ||
346 | ***********************************************************************/ | ||
347 | |||
348 | FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb) | ||
349 | { | ||
350 | FLAC__ASSERT(0 != bb); | ||
351 | |||
352 | bb->buffer = 0; | ||
353 | bb->capacity = 0; | ||
354 | bb->blurbs = bb->bits = bb->total_bits = 0; | ||
355 | bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0; | ||
356 | |||
357 | return FLAC__bitbuffer_clear(bb); | ||
358 | } | ||
359 | |||
360 | FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes) | ||
361 | { | ||
362 | FLAC__ASSERT(0 != bb); | ||
363 | FLAC__ASSERT(bytes > 0); | ||
364 | |||
365 | if(!FLAC__bitbuffer_init(bb)) | ||
366 | return false; | ||
367 | |||
368 | if(!bitbuffer_ensure_size_(bb, bytes << 3)) | ||
369 | return false; | ||
370 | |||
371 | FLAC__ASSERT(0 != buffer); | ||
372 | /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */ | ||
373 | memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes); | ||
374 | bb->blurbs = bytes / FLAC__BYTES_PER_BLURB; | ||
375 | bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3; | ||
376 | bb->total_bits = bytes << 3; | ||
377 | return true; | ||
378 | } | ||
379 | |||
380 | FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src) | ||
381 | { | ||
382 | unsigned bits_to_add = src->total_bits - src->total_consumed_bits; | ||
383 | |||
384 | FLAC__ASSERT(0 != dest); | ||
385 | FLAC__ASSERT(0 != src); | ||
386 | |||
387 | if(bits_to_add == 0) | ||
388 | return true; | ||
389 | if(dest->bits != src->consumed_bits) | ||
390 | return false; | ||
391 | if(!bitbuffer_ensure_size_(dest, bits_to_add)) | ||
392 | return false; | ||
393 | if(dest->bits == 0) { | ||
394 | memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0))); | ||
395 | } | ||
396 | else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) { | ||
397 | dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits); | ||
398 | dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1)); | ||
399 | memcpy(dest->buffer+dest->blurbs+1, src->buffer+src->consumed_blurbs+1, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs-1 + ((src->bits)? 1:0))); | ||
400 | } | ||
401 | else { | ||
402 | dest->buffer[dest->blurbs] <<= bits_to_add; | ||
403 | dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1)); | ||
404 | } | ||
405 | dest->bits = src->bits; | ||
406 | dest->total_bits += bits_to_add; | ||
407 | dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB; | ||
408 | |||
409 | return true; | ||
410 | } | ||
411 | |||
412 | void FLAC__bitbuffer_free(FLAC__BitBuffer *bb) | ||
413 | { | ||
414 | FLAC__ASSERT(0 != bb); | ||
415 | |||
416 | if(0 != bb->buffer) | ||
417 | free(bb->buffer); | ||
418 | bb->buffer = 0; | ||
419 | bb->capacity = 0; | ||
420 | bb->blurbs = bb->bits = bb->total_bits = 0; | ||
421 | bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0; | ||
422 | } | ||
423 | |||
424 | FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb) | ||
425 | { | ||
426 | if(bb->buffer == 0) { | ||
427 | bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY; | ||
428 | bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb)); | ||
429 | if(bb->buffer == 0) | ||
430 | return false; | ||
431 | } | ||
432 | else { | ||
433 | memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0)); | ||
434 | } | ||
435 | bb->blurbs = bb->bits = bb->total_bits = 0; | ||
436 | bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0; | ||
437 | return true; | ||
438 | } | ||
439 | |||
440 | FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src) | ||
441 | { | ||
442 | FLAC__ASSERT(0 != dest); | ||
443 | FLAC__ASSERT(0 != dest->buffer); | ||
444 | FLAC__ASSERT(0 != src); | ||
445 | FLAC__ASSERT(0 != src->buffer); | ||
446 | |||
447 | if(dest->capacity < src->capacity) | ||
448 | if(!bitbuffer_resize_(dest, src->capacity)) | ||
449 | return false; | ||
450 | memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1)); | ||
451 | dest->blurbs = src->blurbs; | ||
452 | dest->bits = src->bits; | ||
453 | dest->total_bits = src->total_bits; | ||
454 | dest->consumed_blurbs = src->consumed_blurbs; | ||
455 | dest->consumed_bits = src->consumed_bits; | ||
456 | dest->total_consumed_bits = src->total_consumed_bits; | ||
457 | dest->read_crc16 = src->read_crc16; | ||
458 | return true; | ||
459 | } | ||
460 | |||
461 | void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed) | ||
462 | { | ||
463 | FLAC__ASSERT(0 != bb); | ||
464 | FLAC__ASSERT(0 != bb->buffer); | ||
465 | FLAC__ASSERT((bb->consumed_bits & 7) == 0); | ||
466 | |||
467 | bb->read_crc16 = seed; | ||
468 | #if FLAC__BITS_PER_BLURB == 8 | ||
469 | /* no need to do anything */ | ||
470 | #elif FLAC__BITS_PER_BLURB == 32 | ||
471 | bb->crc16_align = bb->consumed_bits; | ||
472 | #else | ||
473 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
474 | #endif | ||
475 | } | ||
476 | |||
477 | FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb) | ||
478 | { | ||
479 | FLAC__ASSERT(0 != bb); | ||
480 | FLAC__ASSERT(0 != bb->buffer); | ||
481 | FLAC__ASSERT((bb->bits & 7) == 0); | ||
482 | FLAC__ASSERT((bb->consumed_bits & 7) == 0); | ||
483 | |||
484 | #if FLAC__BITS_PER_BLURB == 8 | ||
485 | /* no need to do anything */ | ||
486 | #elif FLAC__BITS_PER_BLURB == 32 | ||
487 | /*@@@ BUG: even though this probably can't happen with FLAC, need to fix the case where we are called here for the very first blurb and crc16_align is > 0 */ | ||
488 | if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { | ||
489 | if(bb->consumed_bits == 8) { | ||
490 | const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs]; | ||
491 | FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16); | ||
492 | } | ||
493 | else if(bb->consumed_bits == 16) { | ||
494 | const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs]; | ||
495 | FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16); | ||
496 | FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16); | ||
497 | } | ||
498 | else if(bb->consumed_bits == 24) { | ||
499 | const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs]; | ||
500 | FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16); | ||
501 | FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16); | ||
502 | FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16); | ||
503 | } | ||
504 | } | ||
505 | else { | ||
506 | if(bb->consumed_bits == 8) { | ||
507 | const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs]; | ||
508 | FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16); | ||
509 | } | ||
510 | else if(bb->consumed_bits == 16) { | ||
511 | const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs]; | ||
512 | FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16); | ||
513 | FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16); | ||
514 | } | ||
515 | else if(bb->consumed_bits == 24) { | ||
516 | const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs]; | ||
517 | FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16); | ||
518 | FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16); | ||
519 | FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16); | ||
520 | } | ||
521 | } | ||
522 | bb->crc16_align = bb->consumed_bits; | ||
523 | #else | ||
524 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
525 | #endif | ||
526 | return bb->read_crc16; | ||
527 | } | ||
528 | |||
529 | FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb) | ||
530 | { | ||
531 | FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */ | ||
532 | |||
533 | #if FLAC__BITS_PER_BLURB == 8 | ||
534 | return FLAC__crc16(bb->buffer, bb->blurbs); | ||
535 | #elif FLAC__BITS_PER_BLURB == 32 | ||
536 | /* @@@ WATCHOUT: code currently only works for big-endian: */ | ||
537 | return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3)); | ||
538 | #else | ||
539 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
540 | #endif | ||
541 | } | ||
542 | |||
543 | FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb) | ||
544 | { | ||
545 | FLAC__ASSERT(0 != bb); | ||
546 | FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */ | ||
547 | FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */ | ||
548 | #if FLAC__BITS_PER_BLURB == 8 | ||
549 | return FLAC__crc8(bb->buffer, bb->blurbs); | ||
550 | #elif FLAC__BITS_PER_BLURB == 32 | ||
551 | /* @@@ WATCHOUT: code currently only works for big-endian: */ | ||
552 | return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3)); | ||
553 | #else | ||
554 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
555 | #endif | ||
556 | } | ||
557 | |||
558 | FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb) | ||
559 | { | ||
560 | return ((bb->bits & 7) == 0); | ||
561 | } | ||
562 | |||
563 | FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb) | ||
564 | { | ||
565 | return ((bb->consumed_bits & 7) == 0); | ||
566 | } | ||
567 | |||
568 | unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb) | ||
569 | { | ||
570 | return 8 - (bb->consumed_bits & 7); | ||
571 | } | ||
572 | |||
573 | unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb) | ||
574 | { | ||
575 | FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0); | ||
576 | return (bb->total_bits - bb->total_consumed_bits) >> 3; | ||
577 | } | ||
578 | |||
579 | void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes) | ||
580 | { | ||
581 | FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0); | ||
582 | #if FLAC__BITS_PER_BLURB == 8 | ||
583 | *buffer = bb->buffer + bb->consumed_blurbs; | ||
584 | *bytes = bb->blurbs - bb->consumed_blurbs; | ||
585 | #elif FLAC__BITS_PER_BLURB == 32 | ||
586 | /* @@@ WATCHOUT: code currently only works for big-endian: */ | ||
587 | *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3); | ||
588 | *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3; | ||
589 | #else | ||
590 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
591 | #endif | ||
592 | } | ||
593 | |||
594 | void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb) | ||
595 | { | ||
596 | #if FLAC__BITS_PER_BLURB == 8 | ||
597 | (void)bb; | ||
598 | #elif FLAC__BITS_PER_BLURB == 32 | ||
599 | /* @@@ WATCHOUT: code currently only works for big-endian: */ | ||
600 | (void)bb; | ||
601 | #else | ||
602 | FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */ | ||
603 | #endif | ||
604 | } | ||
605 | |||
606 | FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits) | ||
607 | { | ||
608 | unsigned n; | ||
609 | |||
610 | FLAC__ASSERT(0 != bb); | ||
611 | FLAC__ASSERT(0 != bb->buffer); | ||
612 | |||
613 | if(bits == 0) | ||
614 | return true; | ||
615 | if(!bitbuffer_ensure_size_(bb, bits)) | ||
616 | return false; | ||
617 | bb->total_bits += bits; | ||
618 | while(bits > 0) { | ||
619 | n = min(FLAC__BITS_PER_BLURB - bb->bits, bits); | ||
620 | bb->buffer[bb->blurbs] <<= n; | ||
621 | bits -= n; | ||
622 | bb->bits += n; | ||
623 | if(bb->bits == FLAC__BITS_PER_BLURB) { | ||
624 | bb->blurbs++; | ||
625 | bb->bits = 0; | ||
626 | } | ||
627 | } | ||
628 | return true; | ||
629 | } | ||
630 | |||
631 | FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits) | ||
632 | { | ||
633 | unsigned n, k; | ||
634 | |||
635 | FLAC__ASSERT(0 != bb); | ||
636 | FLAC__ASSERT(0 != bb->buffer); | ||
637 | |||
638 | FLAC__ASSERT(bits <= 32); | ||
639 | if(bits == 0) | ||
640 | return true; | ||
641 | /* inline the size check so we don't incure a function call unnecessarily */ | ||
642 | if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) { | ||
643 | if(!bitbuffer_ensure_size_(bb, bits)) | ||
644 | return false; | ||
645 | } | ||
646 | |||
647 | /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */ | ||
648 | if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */ | ||
649 | val &= (~(0xffffffff << bits)); /* zero-out unused bits */ | ||
650 | |||
651 | bb->total_bits += bits; | ||
652 | while(bits > 0) { | ||
653 | n = FLAC__BITS_PER_BLURB - bb->bits; | ||
654 | if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */ | ||
655 | if(bits < FLAC__BITS_PER_BLURB) { | ||
656 | bb->buffer[bb->blurbs] = (FLAC__blurb)val; | ||
657 | bb->bits = bits; | ||
658 | break; | ||
659 | } | ||
660 | else if(bits == FLAC__BITS_PER_BLURB) { | ||
661 | bb->buffer[bb->blurbs++] = (FLAC__blurb)val; | ||
662 | break; | ||
663 | } | ||
664 | else { | ||
665 | k = bits - FLAC__BITS_PER_BLURB; | ||
666 | bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k); | ||
667 | /* we know k < 32 so no need to protect against the gcc bug mentioned above */ | ||
668 | val &= (~(0xffffffff << k)); | ||
669 | bits -= FLAC__BITS_PER_BLURB; | ||
670 | } | ||
671 | } | ||
672 | else if(bits <= n) { | ||
673 | bb->buffer[bb->blurbs] <<= bits; | ||
674 | bb->buffer[bb->blurbs] |= val; | ||
675 | if(bits == n) { | ||
676 | bb->blurbs++; | ||
677 | bb->bits = 0; | ||
678 | } | ||
679 | else | ||
680 | bb->bits += bits; | ||
681 | break; | ||
682 | } | ||
683 | else { | ||
684 | k = bits - n; | ||
685 | bb->buffer[bb->blurbs] <<= n; | ||
686 | bb->buffer[bb->blurbs] |= (val >> k); | ||
687 | /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */ | ||
688 | val &= (~(0xffffffff << k)); | ||
689 | bits -= n; | ||
690 | bb->blurbs++; | ||
691 | bb->bits = 0; | ||
692 | } | ||
693 | } | ||
694 | |||
695 | return true; | ||
696 | } | ||
697 | |||
698 | FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits) | ||
699 | { | ||
700 | return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits); | ||
701 | } | ||
702 | |||
703 | FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits) | ||
704 | { | ||
705 | static const FLAC__uint64 mask[] = { | ||
706 | 0, | ||
707 | FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F), | ||
708 | FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF), | ||
709 | FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF), | ||
710 | FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF), | ||
711 | FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF), | ||
712 | FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF), | ||
713 | FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF), | ||
714 | FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF), | ||
715 | FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF), | ||
716 | FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF), | ||
717 | FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF), | ||
718 | FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF), | ||
719 | FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF), | ||
720 | FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF), | ||
721 | FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF), | ||
722 | FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF) | ||
723 | }; | ||
724 | unsigned n, k; | ||
725 | |||
726 | FLAC__ASSERT(0 != bb); | ||
727 | FLAC__ASSERT(0 != bb->buffer); | ||
728 | |||
729 | FLAC__ASSERT(bits <= 64); | ||
730 | if(bits == 0) | ||
731 | return true; | ||
732 | if(!bitbuffer_ensure_size_(bb, bits)) | ||
733 | return false; | ||
734 | val &= mask[bits]; | ||
735 | bb->total_bits += bits; | ||
736 | while(bits > 0) { | ||
737 | if(bb->bits == 0) { | ||
738 | if(bits < FLAC__BITS_PER_BLURB) { | ||
739 | bb->buffer[bb->blurbs] = (FLAC__blurb)val; | ||
740 | bb->bits = bits; | ||
741 | break; | ||
742 | } | ||
743 | else if(bits == FLAC__BITS_PER_BLURB) { | ||
744 | bb->buffer[bb->blurbs++] = (FLAC__blurb)val; | ||
745 | break; | ||
746 | } | ||
747 | else { | ||
748 | k = bits - FLAC__BITS_PER_BLURB; | ||
749 | bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k); | ||
750 | /* we know k < 64 so no need to protect against the gcc bug mentioned above */ | ||
751 | val &= (~(FLAC__U64L(0xffffffffffffffff) << k)); | ||
752 | bits -= FLAC__BITS_PER_BLURB; | ||
753 | } | ||
754 | } | ||
755 | else { | ||
756 | n = min(FLAC__BITS_PER_BLURB - bb->bits, bits); | ||
757 | k = bits - n; | ||
758 | bb->buffer[bb->blurbs] <<= n; | ||
759 | bb->buffer[bb->blurbs] |= (val >> k); | ||
760 | /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */ | ||
761 | val &= (~(FLAC__U64L(0xffffffffffffffff) << k)); | ||
762 | bits -= n; | ||
763 | bb->bits += n; | ||
764 | if(bb->bits == FLAC__BITS_PER_BLURB) { | ||
765 | bb->blurbs++; | ||
766 | bb->bits = 0; | ||
767 | } | ||
768 | } | ||
769 | } | ||
770 | |||
771 | return true; | ||
772 | } | ||
773 | |||
774 | #if 0 /* UNUSED */ | ||
775 | FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits) | ||
776 | { | ||
777 | return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits); | ||
778 | } | ||
779 | #endif | ||
780 | |||
781 | FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val) | ||
782 | { | ||
783 | /* this doesn't need to be that fast as currently it is only used for vorbis comments */ | ||
784 | |||
785 | /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */ | ||
786 | if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8)) | ||
787 | return false; | ||
788 | if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8)) | ||
789 | return false; | ||
790 | if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8)) | ||
791 | return false; | ||
792 | if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8)) | ||
793 | return false; | ||
794 | |||
795 | return true; | ||
796 | } | ||
797 | |||
798 | FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals) | ||
799 | { | ||
800 | unsigned i; | ||
801 | |||
802 | /* this could be faster but currently we don't need it to be */ | ||
803 | for(i = 0; i < nvals; i++) { | ||
804 | if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8)) | ||
805 | return false; | ||
806 | } | ||
807 | |||
808 | return true; | ||
809 | } | ||
810 | |||
811 | FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val) | ||
812 | { | ||
813 | if(val < 32) | ||
814 | return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val); | ||
815 | else if(val < 64) | ||
816 | return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val); | ||
817 | else { | ||
818 | if(!FLAC__bitbuffer_write_zeroes(bb, val)) | ||
819 | return false; | ||
820 | return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1); | ||
821 | } | ||
822 | } | ||
823 | |||
824 | unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter) | ||
825 | { | ||
826 | unsigned msbs, uval; | ||
827 | |||
828 | /* fold signed to unsigned */ | ||
829 | if(val < 0) | ||
830 | /* equivalent to | ||
831 | * (unsigned)(((--val) << 1) - 1); | ||
832 | * but without the overflow problem at MININT | ||
833 | */ | ||
834 | uval = (unsigned)(((-(++val)) << 1) + 1); | ||
835 | else | ||
836 | uval = (unsigned)(val << 1); | ||
837 | |||
838 | msbs = uval >> parameter; | ||
839 | |||
840 | return 1 + parameter + msbs; | ||
841 | } | ||
842 | |||
843 | #if 0 /* UNUSED */ | ||
844 | unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter) | ||
845 | { | ||
846 | unsigned bits, msbs, uval; | ||
847 | unsigned k; | ||
848 | |||
849 | FLAC__ASSERT(parameter > 0); | ||
850 | |||
851 | /* fold signed to unsigned */ | ||
852 | if(val < 0) | ||
853 | /* equivalent to | ||
854 | * (unsigned)(((--val) << 1) - 1); | ||
855 | * but without the overflow problem at MININT | ||
856 | */ | ||
857 | uval = (unsigned)(((-(++val)) << 1) + 1); | ||
858 | else | ||
859 | uval = (unsigned)(val << 1); | ||
860 | |||
861 | k = FLAC__bitmath_ilog2(parameter); | ||
862 | if(parameter == 1u<<k) { | ||
863 | FLAC__ASSERT(k <= 30); | ||
864 | |||
865 | msbs = uval >> k; | ||
866 | bits = 1 + k + msbs; | ||
867 | } | ||
868 | else { | ||
869 | unsigned q, r, d; | ||
870 | |||
871 | d = (1 << (k+1)) - parameter; | ||
872 | q = uval / parameter; | ||
873 | r = uval - (q * parameter); | ||
874 | |||
875 | bits = 1 + q + k; | ||
876 | if(r >= d) | ||
877 | bits++; | ||
878 | } | ||
879 | return bits; | ||
880 | } | ||
881 | |||
882 | unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter) | ||
883 | { | ||
884 | unsigned bits, msbs; | ||
885 | unsigned k; | ||
886 | |||
887 | FLAC__ASSERT(parameter > 0); | ||
888 | |||
889 | k = FLAC__bitmath_ilog2(parameter); | ||
890 | if(parameter == 1u<<k) { | ||
891 | FLAC__ASSERT(k <= 30); | ||
892 | |||
893 | msbs = uval >> k; | ||
894 | bits = 1 + k + msbs; | ||
895 | } | ||
896 | else { | ||
897 | unsigned q, r, d; | ||
898 | |||
899 | d = (1 << (k+1)) - parameter; | ||
900 | q = uval / parameter; | ||
901 | r = uval - (q * parameter); | ||
902 | |||
903 | bits = 1 + q + k; | ||
904 | if(r >= d) | ||
905 | bits++; | ||
906 | } | ||
907 | return bits; | ||
908 | } | ||
909 | #endif /* UNUSED */ | ||
910 | |||
911 | #ifdef FLAC__SYMMETRIC_RICE | ||
912 | FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter) | ||
913 | { | ||
914 | unsigned total_bits, interesting_bits, msbs; | ||
915 | FLAC__uint32 pattern; | ||
916 | |||
917 | FLAC__ASSERT(0 != bb); | ||
918 | FLAC__ASSERT(0 != bb->buffer); | ||
919 | FLAC__ASSERT(parameter <= 31); | ||
920 | |||
921 | /* init pattern with the unary end bit and the sign bit */ | ||
922 | if(val < 0) { | ||
923 | pattern = 3; | ||
924 | val = -val; | ||
925 | } | ||
926 | else | ||
927 | pattern = 2; | ||
928 | |||
929 | msbs = val >> parameter; | ||
930 | interesting_bits = 2 + parameter; | ||
931 | total_bits = interesting_bits + msbs; | ||
932 | pattern <<= parameter; | ||
933 | pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */ | ||
934 | |||
935 | if(total_bits <= 32) { | ||
936 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits)) | ||
937 | return false; | ||
938 | } | ||
939 | else { | ||
940 | /* write the unary MSBs */ | ||
941 | if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) | ||
942 | return false; | ||
943 | /* write the unary end bit, the sign bit, and binary LSBs */ | ||
944 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits)) | ||
945 | return false; | ||
946 | } | ||
947 | return true; | ||
948 | } | ||
949 | |||
950 | #if 0 /* UNUSED */ | ||
951 | FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow) | ||
952 | { | ||
953 | unsigned total_bits, interesting_bits, msbs; | ||
954 | FLAC__uint32 pattern; | ||
955 | |||
956 | FLAC__ASSERT(0 != bb); | ||
957 | FLAC__ASSERT(0 != bb->buffer); | ||
958 | FLAC__ASSERT(parameter <= 31); | ||
959 | |||
960 | *overflow = false; | ||
961 | |||
962 | /* init pattern with the unary end bit and the sign bit */ | ||
963 | if(val < 0) { | ||
964 | pattern = 3; | ||
965 | val = -val; | ||
966 | } | ||
967 | else | ||
968 | pattern = 2; | ||
969 | |||
970 | msbs = val >> parameter; | ||
971 | interesting_bits = 2 + parameter; | ||
972 | total_bits = interesting_bits + msbs; | ||
973 | pattern <<= parameter; | ||
974 | pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */ | ||
975 | |||
976 | if(total_bits <= 32) { | ||
977 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits)) | ||
978 | return false; | ||
979 | } | ||
980 | else if(total_bits > max_bits) { | ||
981 | *overflow = true; | ||
982 | return true; | ||
983 | } | ||
984 | else { | ||
985 | /* write the unary MSBs */ | ||
986 | if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) | ||
987 | return false; | ||
988 | /* write the unary end bit, the sign bit, and binary LSBs */ | ||
989 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits)) | ||
990 | return false; | ||
991 | } | ||
992 | return true; | ||
993 | } | ||
994 | #endif /* UNUSED */ | ||
995 | |||
996 | FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter) | ||
997 | { | ||
998 | unsigned total_bits, val_bits; | ||
999 | FLAC__uint32 pattern; | ||
1000 | |||
1001 | FLAC__ASSERT(0 != bb); | ||
1002 | FLAC__ASSERT(0 != bb->buffer); | ||
1003 | FLAC__ASSERT(parameter <= 31); | ||
1004 | |||
1005 | val_bits = FLAC__bitmath_silog2(val); | ||
1006 | total_bits = 2 + parameter + 5 + val_bits; | ||
1007 | |||
1008 | if(total_bits <= 32) { | ||
1009 | pattern = 3; | ||
1010 | pattern <<= (parameter + 5); | ||
1011 | pattern |= val_bits; | ||
1012 | pattern <<= val_bits; | ||
1013 | pattern |= (val & ((1 << val_bits) - 1)); | ||
1014 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits)) | ||
1015 | return false; | ||
1016 | } | ||
1017 | else { | ||
1018 | /* write the '-0' escape code first */ | ||
1019 | if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter)) | ||
1020 | return false; | ||
1021 | /* write the length */ | ||
1022 | if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5)) | ||
1023 | return false; | ||
1024 | /* write the value */ | ||
1025 | if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits)) | ||
1026 | return false; | ||
1027 | } | ||
1028 | return true; | ||
1029 | } | ||
1030 | #endif /* ifdef FLAC__SYMMETRIC_RICE */ | ||
1031 | |||
1032 | FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter) | ||
1033 | { | ||
1034 | unsigned total_bits, interesting_bits, msbs, uval; | ||
1035 | FLAC__uint32 pattern; | ||
1036 | |||
1037 | FLAC__ASSERT(0 != bb); | ||
1038 | FLAC__ASSERT(0 != bb->buffer); | ||
1039 | FLAC__ASSERT(parameter <= 30); | ||
1040 | |||
1041 | /* fold signed to unsigned */ | ||
1042 | if(val < 0) | ||
1043 | /* equivalent to | ||
1044 | * (unsigned)(((--val) << 1) - 1); | ||
1045 | * but without the overflow problem at MININT | ||
1046 | */ | ||
1047 | uval = (unsigned)(((-(++val)) << 1) + 1); | ||
1048 | else | ||
1049 | uval = (unsigned)(val << 1); | ||
1050 | |||
1051 | msbs = uval >> parameter; | ||
1052 | interesting_bits = 1 + parameter; | ||
1053 | total_bits = interesting_bits + msbs; | ||
1054 | pattern = 1 << parameter; /* the unary end bit */ | ||
1055 | pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */ | ||
1056 | |||
1057 | if(total_bits <= 32) { | ||
1058 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits)) | ||
1059 | return false; | ||
1060 | } | ||
1061 | else { | ||
1062 | /* write the unary MSBs */ | ||
1063 | if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) | ||
1064 | return false; | ||
1065 | /* write the unary end bit and binary LSBs */ | ||
1066 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits)) | ||
1067 | return false; | ||
1068 | } | ||
1069 | return true; | ||
1070 | } | ||
1071 | |||
1072 | #if 0 /* UNUSED */ | ||
1073 | FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow) | ||
1074 | { | ||
1075 | unsigned total_bits, interesting_bits, msbs, uval; | ||
1076 | FLAC__uint32 pattern; | ||
1077 | |||
1078 | FLAC__ASSERT(0 != bb); | ||
1079 | FLAC__ASSERT(0 != bb->buffer); | ||
1080 | FLAC__ASSERT(parameter <= 30); | ||
1081 | |||
1082 | *overflow = false; | ||
1083 | |||
1084 | /* fold signed to unsigned */ | ||
1085 | if(val < 0) | ||
1086 | /* equivalent to | ||
1087 | * (unsigned)(((--val) << 1) - 1); | ||
1088 | * but without the overflow problem at MININT | ||
1089 | */ | ||
1090 | uval = (unsigned)(((-(++val)) << 1) + 1); | ||
1091 | else | ||
1092 | uval = (unsigned)(val << 1); | ||
1093 | |||
1094 | msbs = uval >> parameter; | ||
1095 | interesting_bits = 1 + parameter; | ||
1096 | total_bits = interesting_bits + msbs; | ||
1097 | pattern = 1 << parameter; /* the unary end bit */ | ||
1098 | pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */ | ||
1099 | |||
1100 | if(total_bits <= 32) { | ||
1101 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits)) | ||
1102 | return false; | ||
1103 | } | ||
1104 | else if(total_bits > max_bits) { | ||
1105 | *overflow = true; | ||
1106 | return true; | ||
1107 | } | ||
1108 | else { | ||
1109 | /* write the unary MSBs */ | ||
1110 | if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) | ||
1111 | return false; | ||
1112 | /* write the unary end bit and binary LSBs */ | ||
1113 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits)) | ||
1114 | return false; | ||
1115 | } | ||
1116 | return true; | ||
1117 | } | ||
1118 | #endif /* UNUSED */ | ||
1119 | |||
1120 | #if 0 /* UNUSED */ | ||
1121 | FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter) | ||
1122 | { | ||
1123 | unsigned total_bits, msbs, uval; | ||
1124 | unsigned k; | ||
1125 | |||
1126 | FLAC__ASSERT(0 != bb); | ||
1127 | FLAC__ASSERT(0 != bb->buffer); | ||
1128 | FLAC__ASSERT(parameter > 0); | ||
1129 | |||
1130 | /* fold signed to unsigned */ | ||
1131 | if(val < 0) | ||
1132 | /* equivalent to | ||
1133 | * (unsigned)(((--val) << 1) - 1); | ||
1134 | * but without the overflow problem at MININT | ||
1135 | */ | ||
1136 | uval = (unsigned)(((-(++val)) << 1) + 1); | ||
1137 | else | ||
1138 | uval = (unsigned)(val << 1); | ||
1139 | |||
1140 | k = FLAC__bitmath_ilog2(parameter); | ||
1141 | if(parameter == 1u<<k) { | ||
1142 | unsigned pattern; | ||
1143 | |||
1144 | FLAC__ASSERT(k <= 30); | ||
1145 | |||
1146 | msbs = uval >> k; | ||
1147 | total_bits = 1 + k + msbs; | ||
1148 | pattern = 1 << k; /* the unary end bit */ | ||
1149 | pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */ | ||
1150 | |||
1151 | if(total_bits <= 32) { | ||
1152 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits)) | ||
1153 | return false; | ||
1154 | } | ||
1155 | else { | ||
1156 | /* write the unary MSBs */ | ||
1157 | if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) | ||
1158 | return false; | ||
1159 | /* write the unary end bit and binary LSBs */ | ||
1160 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1)) | ||
1161 | return false; | ||
1162 | } | ||
1163 | } | ||
1164 | else { | ||
1165 | unsigned q, r, d; | ||
1166 | |||
1167 | d = (1 << (k+1)) - parameter; | ||
1168 | q = uval / parameter; | ||
1169 | r = uval - (q * parameter); | ||
1170 | /* write the unary MSBs */ | ||
1171 | if(!FLAC__bitbuffer_write_zeroes(bb, q)) | ||
1172 | return false; | ||
1173 | /* write the unary end bit */ | ||
1174 | if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1)) | ||
1175 | return false; | ||
1176 | /* write the binary LSBs */ | ||
1177 | if(r >= d) { | ||
1178 | if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1)) | ||
1179 | return false; | ||
1180 | } | ||
1181 | else { | ||
1182 | if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k)) | ||
1183 | return false; | ||
1184 | } | ||
1185 | } | ||
1186 | return true; | ||
1187 | } | ||
1188 | |||
1189 | FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter) | ||
1190 | { | ||
1191 | unsigned total_bits, msbs; | ||
1192 | unsigned k; | ||
1193 | |||
1194 | FLAC__ASSERT(0 != bb); | ||
1195 | FLAC__ASSERT(0 != bb->buffer); | ||
1196 | FLAC__ASSERT(parameter > 0); | ||
1197 | |||
1198 | k = FLAC__bitmath_ilog2(parameter); | ||
1199 | if(parameter == 1u<<k) { | ||
1200 | unsigned pattern; | ||
1201 | |||
1202 | FLAC__ASSERT(k <= 30); | ||
1203 | |||
1204 | msbs = uval >> k; | ||
1205 | total_bits = 1 + k + msbs; | ||
1206 | pattern = 1 << k; /* the unary end bit */ | ||
1207 | pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */ | ||
1208 | |||
1209 | if(total_bits <= 32) { | ||
1210 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits)) | ||
1211 | return false; | ||
1212 | } | ||
1213 | else { | ||
1214 | /* write the unary MSBs */ | ||
1215 | if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) | ||
1216 | return false; | ||
1217 | /* write the unary end bit and binary LSBs */ | ||
1218 | if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1)) | ||
1219 | return false; | ||
1220 | } | ||
1221 | } | ||
1222 | else { | ||
1223 | unsigned q, r, d; | ||
1224 | |||
1225 | d = (1 << (k+1)) - parameter; | ||
1226 | q = uval / parameter; | ||
1227 | r = uval - (q * parameter); | ||
1228 | /* write the unary MSBs */ | ||
1229 | if(!FLAC__bitbuffer_write_zeroes(bb, q)) | ||
1230 | return false; | ||
1231 | /* write the unary end bit */ | ||
1232 | if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1)) | ||
1233 | return false; | ||
1234 | /* write the binary LSBs */ | ||
1235 | if(r >= d) { | ||
1236 | if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1)) | ||
1237 | return false; | ||
1238 | } | ||
1239 | else { | ||
1240 | if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k)) | ||
1241 | return false; | ||
1242 | } | ||
1243 | } | ||
1244 | return true; | ||
1245 | } | ||
1246 | #endif /* UNUSED */ | ||
1247 | |||
1248 | FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val) | ||
1249 | { | ||
1250 | FLAC__bool ok = 1; | ||
1251 | |||
1252 | FLAC__ASSERT(0 != bb); | ||
1253 | FLAC__ASSERT(0 != bb->buffer); | ||
1254 | |||
1255 | FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */ | ||
1256 | |||
1257 | if(val < 0x80) { | ||
1258 | return FLAC__bitbuffer_write_raw_uint32(bb, val, 8); | ||
1259 | } | ||
1260 | else if(val < 0x800) { | ||
1261 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8); | ||
1262 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8); | ||
1263 | } | ||
1264 | else if(val < 0x10000) { | ||
1265 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8); | ||
1266 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8); | ||
1267 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8); | ||
1268 | } | ||
1269 | else if(val < 0x200000) { | ||
1270 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8); | ||
1271 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8); | ||
1272 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8); | ||
1273 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8); | ||
1274 | } | ||
1275 | else if(val < 0x4000000) { | ||
1276 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8); | ||
1277 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8); | ||
1278 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8); | ||
1279 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8); | ||
1280 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8); | ||
1281 | } | ||
1282 | else { | ||
1283 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8); | ||
1284 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8); | ||
1285 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8); | ||
1286 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8); | ||
1287 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8); | ||
1288 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8); | ||
1289 | } | ||
1290 | |||
1291 | return ok; | ||
1292 | } | ||
1293 | |||
1294 | FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val) | ||
1295 | { | ||
1296 | FLAC__bool ok = 1; | ||
1297 | |||
1298 | FLAC__ASSERT(0 != bb); | ||
1299 | FLAC__ASSERT(0 != bb->buffer); | ||
1300 | |||
1301 | FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */ | ||
1302 | |||
1303 | if(val < 0x80) { | ||
1304 | return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8); | ||
1305 | } | ||
1306 | else if(val < 0x800) { | ||
1307 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8); | ||
1308 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8); | ||
1309 | } | ||
1310 | else if(val < 0x10000) { | ||
1311 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8); | ||
1312 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); | ||
1313 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8); | ||
1314 | } | ||
1315 | else if(val < 0x200000) { | ||
1316 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8); | ||
1317 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); | ||
1318 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); | ||
1319 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8); | ||
1320 | } | ||
1321 | else if(val < 0x4000000) { | ||
1322 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8); | ||
1323 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8); | ||
1324 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); | ||
1325 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); | ||
1326 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8); | ||
1327 | } | ||
1328 | else if(val < 0x80000000) { | ||
1329 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8); | ||
1330 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8); | ||
1331 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8); | ||
1332 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); | ||
1333 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); | ||
1334 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8); | ||
1335 | } | ||
1336 | else { | ||
1337 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8); | ||
1338 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8); | ||
1339 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8); | ||
1340 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8); | ||
1341 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); | ||
1342 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); | ||
1343 | ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8); | ||
1344 | } | ||
1345 | |||
1346 | return ok; | ||
1347 | } | ||
1348 | |||
1349 | FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb) | ||
1350 | { | ||
1351 | /* 0-pad to byte boundary */ | ||
1352 | if(bb->bits & 7u) | ||
1353 | return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u)); | ||
1354 | else | ||
1355 | return true; | ||
1356 | } | ||
1357 | |||
1358 | FLAC__bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1359 | { | ||
1360 | /* to avoid a drastic speed penalty we don't: | ||
1361 | FLAC__ASSERT(0 != bb); | ||
1362 | FLAC__ASSERT(0 != bb->buffer); | ||
1363 | FLAC__ASSERT(bb->bits == 0); | ||
1364 | */ | ||
1365 | |||
1366 | while(1) { | ||
1367 | if(bb->total_consumed_bits < bb->total_bits) { | ||
1368 | *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0; | ||
1369 | return true; | ||
1370 | } | ||
1371 | else { | ||
1372 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1373 | return false; | ||
1374 | } | ||
1375 | } | ||
1376 | } | ||
1377 | |||
1378 | FLAC__bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1379 | { | ||
1380 | /* to avoid a drastic speed penalty we don't: | ||
1381 | FLAC__ASSERT(0 != bb); | ||
1382 | FLAC__ASSERT(0 != bb->buffer); | ||
1383 | FLAC__ASSERT(bb->bits == 0); | ||
1384 | */ | ||
1385 | |||
1386 | while(1) { | ||
1387 | if(bb->total_consumed_bits < bb->total_bits) { | ||
1388 | *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0; | ||
1389 | bb->consumed_bits++; | ||
1390 | if(bb->consumed_bits == FLAC__BITS_PER_BLURB) { | ||
1391 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1392 | bb->consumed_blurbs++; | ||
1393 | bb->consumed_bits = 0; | ||
1394 | } | ||
1395 | bb->total_consumed_bits++; | ||
1396 | return true; | ||
1397 | } | ||
1398 | else { | ||
1399 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1400 | return false; | ||
1401 | } | ||
1402 | } | ||
1403 | } | ||
1404 | |||
1405 | FLAC__bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1406 | { | ||
1407 | /* to avoid a drastic speed penalty we don't: | ||
1408 | FLAC__ASSERT(0 != bb); | ||
1409 | FLAC__ASSERT(0 != bb->buffer); | ||
1410 | FLAC__ASSERT(bb->bits == 0); | ||
1411 | */ | ||
1412 | |||
1413 | while(1) { | ||
1414 | if(bb->total_consumed_bits < bb->total_bits) { | ||
1415 | *val <<= 1; | ||
1416 | *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0; | ||
1417 | bb->consumed_bits++; | ||
1418 | if(bb->consumed_bits == FLAC__BITS_PER_BLURB) { | ||
1419 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1420 | bb->consumed_blurbs++; | ||
1421 | bb->consumed_bits = 0; | ||
1422 | } | ||
1423 | bb->total_consumed_bits++; | ||
1424 | return true; | ||
1425 | } | ||
1426 | else { | ||
1427 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1428 | return false; | ||
1429 | } | ||
1430 | } | ||
1431 | } | ||
1432 | |||
1433 | FLAC__bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1434 | { | ||
1435 | /* to avoid a drastic speed penalty we don't: | ||
1436 | FLAC__ASSERT(0 != bb); | ||
1437 | FLAC__ASSERT(0 != bb->buffer); | ||
1438 | FLAC__ASSERT(bb->bits == 0); | ||
1439 | */ | ||
1440 | |||
1441 | while(1) { | ||
1442 | if(bb->total_consumed_bits < bb->total_bits) { | ||
1443 | *val <<= 1; | ||
1444 | *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0; | ||
1445 | bb->consumed_bits++; | ||
1446 | if(bb->consumed_bits == FLAC__BITS_PER_BLURB) { | ||
1447 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1448 | bb->consumed_blurbs++; | ||
1449 | bb->consumed_bits = 0; | ||
1450 | } | ||
1451 | bb->total_consumed_bits++; | ||
1452 | return true; | ||
1453 | } | ||
1454 | else { | ||
1455 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1456 | return false; | ||
1457 | } | ||
1458 | } | ||
1459 | } | ||
1460 | |||
1461 | FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1462 | #ifdef FLAC__NO_MANUAL_INLINING | ||
1463 | { | ||
1464 | unsigned i; | ||
1465 | |||
1466 | FLAC__ASSERT(0 != bb); | ||
1467 | FLAC__ASSERT(0 != bb->buffer); | ||
1468 | |||
1469 | FLAC__ASSERT(bits <= 32); | ||
1470 | |||
1471 | *val = 0; | ||
1472 | for(i = 0; i < bits; i++) { | ||
1473 | if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data)) | ||
1474 | return false; | ||
1475 | } | ||
1476 | return true; | ||
1477 | } | ||
1478 | #else | ||
1479 | { | ||
1480 | unsigned i, bits_ = bits; | ||
1481 | FLAC__uint32 v = 0; | ||
1482 | |||
1483 | FLAC__ASSERT(0 != bb); | ||
1484 | FLAC__ASSERT(0 != bb->buffer); | ||
1485 | |||
1486 | FLAC__ASSERT(bits <= 32); | ||
1487 | FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits); | ||
1488 | |||
1489 | if(bits == 0) { | ||
1490 | *val = 0; | ||
1491 | return true; | ||
1492 | } | ||
1493 | |||
1494 | while(bb->total_consumed_bits + bits > bb->total_bits) { | ||
1495 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1496 | return false; | ||
1497 | } | ||
1498 | #if FLAC__BITS_PER_BLURB > 8 | ||
1499 | if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/ | ||
1500 | #endif | ||
1501 | if(bb->consumed_bits) { | ||
1502 | i = FLAC__BITS_PER_BLURB - bb->consumed_bits; | ||
1503 | if(i <= bits_) { | ||
1504 | v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits); | ||
1505 | bits_ -= i; | ||
1506 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1507 | bb->consumed_blurbs++; | ||
1508 | bb->consumed_bits = 0; | ||
1509 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1510 | } | ||
1511 | else { | ||
1512 | *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_); | ||
1513 | bb->consumed_bits += bits_; | ||
1514 | bb->total_consumed_bits += bits_; | ||
1515 | return true; | ||
1516 | } | ||
1517 | } | ||
1518 | #if FLAC__BITS_PER_BLURB == 32 | ||
1519 | /* note that we know bits_ cannot be > 32 because of previous assertions */ | ||
1520 | if(bits_ == FLAC__BITS_PER_BLURB) { | ||
1521 | v = bb->buffer[bb->consumed_blurbs]; | ||
1522 | CRC16_UPDATE_BLURB(bb, v, bb->read_crc16); | ||
1523 | bb->consumed_blurbs++; | ||
1524 | /* bb->consumed_bits is already 0 */ | ||
1525 | bb->total_consumed_bits += bits; | ||
1526 | *val = v; | ||
1527 | return true; | ||
1528 | } | ||
1529 | #else | ||
1530 | while(bits_ >= FLAC__BITS_PER_BLURB) { | ||
1531 | v <<= FLAC__BITS_PER_BLURB; | ||
1532 | v |= bb->buffer[bb->consumed_blurbs]; | ||
1533 | bits_ -= FLAC__BITS_PER_BLURB; | ||
1534 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1535 | bb->consumed_blurbs++; | ||
1536 | /* bb->consumed_bits is already 0 */ | ||
1537 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1538 | } | ||
1539 | #endif | ||
1540 | if(bits_ > 0) { | ||
1541 | v <<= bits_; | ||
1542 | v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_)); | ||
1543 | bb->consumed_bits = bits_; | ||
1544 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1545 | } | ||
1546 | bb->total_consumed_bits += bits; | ||
1547 | *val = v; | ||
1548 | #if FLAC__BITS_PER_BLURB > 8 | ||
1549 | } | ||
1550 | else { | ||
1551 | *val = 0; | ||
1552 | for(i = 0; i < bits; i++) { | ||
1553 | if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data)) | ||
1554 | return false; | ||
1555 | } | ||
1556 | } | ||
1557 | #endif | ||
1558 | return true; | ||
1559 | } | ||
1560 | #endif | ||
1561 | |||
1562 | FLAC__bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1563 | #ifdef FLAC__NO_MANUAL_INLINING | ||
1564 | { | ||
1565 | unsigned i; | ||
1566 | FLAC__uint32 v; | ||
1567 | |||
1568 | FLAC__ASSERT(0 != bb); | ||
1569 | FLAC__ASSERT(0 != bb->buffer); | ||
1570 | |||
1571 | FLAC__ASSERT(bits <= 32); | ||
1572 | |||
1573 | if(bits == 0) { | ||
1574 | *val = 0; | ||
1575 | return true; | ||
1576 | } | ||
1577 | |||
1578 | v = 0; | ||
1579 | for(i = 0; i < bits; i++) { | ||
1580 | if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data)) | ||
1581 | return false; | ||
1582 | } | ||
1583 | |||
1584 | /* fix the sign */ | ||
1585 | i = 32 - bits; | ||
1586 | if(i) { | ||
1587 | v <<= i; | ||
1588 | *val = (FLAC__int32)v; | ||
1589 | *val >>= i; | ||
1590 | } | ||
1591 | else | ||
1592 | *val = (FLAC__int32)v; | ||
1593 | |||
1594 | return true; | ||
1595 | } | ||
1596 | #else | ||
1597 | { | ||
1598 | unsigned i, bits_ = bits; | ||
1599 | FLAC__uint32 v = 0; | ||
1600 | |||
1601 | FLAC__ASSERT(0 != bb); | ||
1602 | FLAC__ASSERT(0 != bb->buffer); | ||
1603 | |||
1604 | FLAC__ASSERT(bits <= 32); | ||
1605 | FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits); | ||
1606 | |||
1607 | if(bits == 0) { | ||
1608 | *val = 0; | ||
1609 | return true; | ||
1610 | } | ||
1611 | |||
1612 | while(bb->total_consumed_bits + bits > bb->total_bits) { | ||
1613 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1614 | return false; | ||
1615 | } | ||
1616 | #if FLAC__BITS_PER_BLURB > 8 | ||
1617 | if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/ | ||
1618 | #endif | ||
1619 | if(bb->consumed_bits) { | ||
1620 | i = FLAC__BITS_PER_BLURB - bb->consumed_bits; | ||
1621 | if(i <= bits_) { | ||
1622 | v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits); | ||
1623 | bits_ -= i; | ||
1624 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1625 | bb->consumed_blurbs++; | ||
1626 | bb->consumed_bits = 0; | ||
1627 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1628 | } | ||
1629 | else { | ||
1630 | /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */ | ||
1631 | v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)); | ||
1632 | v <<= (32-i); | ||
1633 | *val = (FLAC__int32)v; | ||
1634 | *val >>= (32-bits_); | ||
1635 | bb->consumed_bits += bits_; | ||
1636 | bb->total_consumed_bits += bits_; | ||
1637 | return true; | ||
1638 | } | ||
1639 | } | ||
1640 | #if FLAC__BITS_PER_BLURB == 32 | ||
1641 | /* note that we know bits_ cannot be > 32 because of previous assertions */ | ||
1642 | if(bits_ == FLAC__BITS_PER_BLURB) { | ||
1643 | v = bb->buffer[bb->consumed_blurbs]; | ||
1644 | bits_ = 0; | ||
1645 | CRC16_UPDATE_BLURB(bb, v, bb->read_crc16); | ||
1646 | bb->consumed_blurbs++; | ||
1647 | /* bb->consumed_bits is already 0 */ | ||
1648 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1649 | } | ||
1650 | #else | ||
1651 | while(bits_ >= FLAC__BITS_PER_BLURB) { | ||
1652 | v <<= FLAC__BITS_PER_BLURB; | ||
1653 | v |= bb->buffer[bb->consumed_blurbs]; | ||
1654 | bits_ -= FLAC__BITS_PER_BLURB; | ||
1655 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1656 | bb->consumed_blurbs++; | ||
1657 | /* bb->consumed_bits is already 0 */ | ||
1658 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1659 | } | ||
1660 | #endif | ||
1661 | if(bits_ > 0) { | ||
1662 | v <<= bits_; | ||
1663 | v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_)); | ||
1664 | bb->consumed_bits = bits_; | ||
1665 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1666 | } | ||
1667 | bb->total_consumed_bits += bits; | ||
1668 | #if FLAC__BITS_PER_BLURB > 8 | ||
1669 | } | ||
1670 | else { | ||
1671 | for(i = 0; i < bits; i++) { | ||
1672 | if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data)) | ||
1673 | return false; | ||
1674 | } | ||
1675 | } | ||
1676 | #endif | ||
1677 | |||
1678 | /* fix the sign */ | ||
1679 | i = 32 - bits; | ||
1680 | if(i) { | ||
1681 | v <<= i; | ||
1682 | *val = (FLAC__int32)v; | ||
1683 | *val >>= i; | ||
1684 | } | ||
1685 | else | ||
1686 | *val = (FLAC__int32)v; | ||
1687 | |||
1688 | return true; | ||
1689 | } | ||
1690 | #endif | ||
1691 | |||
1692 | FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1693 | #ifdef FLAC__NO_MANUAL_INLINING | ||
1694 | { | ||
1695 | unsigned i; | ||
1696 | |||
1697 | FLAC__ASSERT(0 != bb); | ||
1698 | FLAC__ASSERT(0 != bb->buffer); | ||
1699 | |||
1700 | FLAC__ASSERT(bits <= 64); | ||
1701 | |||
1702 | *val = 0; | ||
1703 | for(i = 0; i < bits; i++) { | ||
1704 | if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data)) | ||
1705 | return false; | ||
1706 | } | ||
1707 | return true; | ||
1708 | } | ||
1709 | #else | ||
1710 | { | ||
1711 | unsigned i, bits_ = bits; | ||
1712 | FLAC__uint64 v = 0; | ||
1713 | |||
1714 | FLAC__ASSERT(0 != bb); | ||
1715 | FLAC__ASSERT(0 != bb->buffer); | ||
1716 | |||
1717 | FLAC__ASSERT(bits <= 64); | ||
1718 | FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits); | ||
1719 | |||
1720 | if(bits == 0) { | ||
1721 | *val = 0; | ||
1722 | return true; | ||
1723 | } | ||
1724 | |||
1725 | while(bb->total_consumed_bits + bits > bb->total_bits) { | ||
1726 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1727 | return false; | ||
1728 | } | ||
1729 | #if FLAC__BITS_PER_BLURB > 8 | ||
1730 | if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/ | ||
1731 | #endif | ||
1732 | if(bb->consumed_bits) { | ||
1733 | i = FLAC__BITS_PER_BLURB - bb->consumed_bits; | ||
1734 | if(i <= bits_) { | ||
1735 | v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits); | ||
1736 | bits_ -= i; | ||
1737 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1738 | bb->consumed_blurbs++; | ||
1739 | bb->consumed_bits = 0; | ||
1740 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1741 | } | ||
1742 | else { | ||
1743 | *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_); | ||
1744 | bb->consumed_bits += bits_; | ||
1745 | bb->total_consumed_bits += bits_; | ||
1746 | return true; | ||
1747 | } | ||
1748 | } | ||
1749 | while(bits_ >= FLAC__BITS_PER_BLURB) { | ||
1750 | v <<= FLAC__BITS_PER_BLURB; | ||
1751 | v |= bb->buffer[bb->consumed_blurbs]; | ||
1752 | bits_ -= FLAC__BITS_PER_BLURB; | ||
1753 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1754 | bb->consumed_blurbs++; | ||
1755 | /* bb->consumed_bits is already 0 */ | ||
1756 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1757 | } | ||
1758 | if(bits_ > 0) { | ||
1759 | v <<= bits_; | ||
1760 | v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_)); | ||
1761 | bb->consumed_bits = bits_; | ||
1762 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1763 | } | ||
1764 | bb->total_consumed_bits += bits; | ||
1765 | *val = v; | ||
1766 | #if FLAC__BITS_PER_BLURB > 8 | ||
1767 | } | ||
1768 | else { | ||
1769 | *val = 0; | ||
1770 | for(i = 0; i < bits; i++) { | ||
1771 | if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data)) | ||
1772 | return false; | ||
1773 | } | ||
1774 | } | ||
1775 | #endif | ||
1776 | return true; | ||
1777 | } | ||
1778 | #endif | ||
1779 | |||
1780 | #if 0 /* UNUSED */ | ||
1781 | FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1782 | #ifdef FLAC__NO_MANUAL_INLINING | ||
1783 | { | ||
1784 | unsigned i; | ||
1785 | FLAC__uint64 v; | ||
1786 | |||
1787 | FLAC__ASSERT(0 != bb); | ||
1788 | FLAC__ASSERT(0 != bb->buffer); | ||
1789 | |||
1790 | FLAC__ASSERT(bits <= 64); | ||
1791 | |||
1792 | v = 0; | ||
1793 | for(i = 0; i < bits; i++) { | ||
1794 | if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data)) | ||
1795 | return false; | ||
1796 | } | ||
1797 | /* fix the sign */ | ||
1798 | i = 64 - bits; | ||
1799 | if(i) { | ||
1800 | v <<= i; | ||
1801 | *val = (FLAC__int64)v; | ||
1802 | *val >>= i; | ||
1803 | } | ||
1804 | else | ||
1805 | *val = (FLAC__int64)v; | ||
1806 | |||
1807 | return true; | ||
1808 | } | ||
1809 | #else | ||
1810 | { | ||
1811 | unsigned i, bits_ = bits; | ||
1812 | FLAC__uint64 v = 0; | ||
1813 | |||
1814 | FLAC__ASSERT(0 != bb); | ||
1815 | FLAC__ASSERT(0 != bb->buffer); | ||
1816 | |||
1817 | FLAC__ASSERT(bits <= 64); | ||
1818 | FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits); | ||
1819 | |||
1820 | if(bits == 0) { | ||
1821 | *val = 0; | ||
1822 | return true; | ||
1823 | } | ||
1824 | |||
1825 | while(bb->total_consumed_bits + bits > bb->total_bits) { | ||
1826 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1827 | return false; | ||
1828 | } | ||
1829 | #if FLAC__BITS_PER_BLURB > 8 | ||
1830 | if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/ | ||
1831 | #endif | ||
1832 | if(bb->consumed_bits) { | ||
1833 | i = FLAC__BITS_PER_BLURB - bb->consumed_bits; | ||
1834 | if(i <= bits_) { | ||
1835 | v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits); | ||
1836 | bits_ -= i; | ||
1837 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1838 | bb->consumed_blurbs++; | ||
1839 | bb->consumed_bits = 0; | ||
1840 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1841 | } | ||
1842 | else { | ||
1843 | /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */ | ||
1844 | v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)); | ||
1845 | v <<= (64-i); | ||
1846 | *val = (FLAC__int64)v; | ||
1847 | *val >>= (64-bits_); | ||
1848 | bb->consumed_bits += bits_; | ||
1849 | bb->total_consumed_bits += bits_; | ||
1850 | return true; | ||
1851 | } | ||
1852 | } | ||
1853 | while(bits_ >= FLAC__BITS_PER_BLURB) { | ||
1854 | v <<= FLAC__BITS_PER_BLURB; | ||
1855 | v |= bb->buffer[bb->consumed_blurbs]; | ||
1856 | bits_ -= FLAC__BITS_PER_BLURB; | ||
1857 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
1858 | bb->consumed_blurbs++; | ||
1859 | /* bb->consumed_bits is already 0 */ | ||
1860 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1861 | } | ||
1862 | if(bits_ > 0) { | ||
1863 | v <<= bits_; | ||
1864 | v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_)); | ||
1865 | bb->consumed_bits = bits_; | ||
1866 | /* we hold off updating bb->total_consumed_bits until the end */ | ||
1867 | } | ||
1868 | bb->total_consumed_bits += bits; | ||
1869 | #if FLAC__BITS_PER_BLURB > 8 | ||
1870 | } | ||
1871 | else { | ||
1872 | for(i = 0; i < bits; i++) { | ||
1873 | if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data)) | ||
1874 | return false; | ||
1875 | } | ||
1876 | } | ||
1877 | #endif | ||
1878 | |||
1879 | /* fix the sign */ | ||
1880 | i = 64 - bits; | ||
1881 | if(i) { | ||
1882 | v <<= i; | ||
1883 | *val = (FLAC__int64)v; | ||
1884 | *val >>= i; | ||
1885 | } | ||
1886 | else | ||
1887 | *val = (FLAC__int64)v; | ||
1888 | |||
1889 | return true; | ||
1890 | } | ||
1891 | #endif | ||
1892 | #endif | ||
1893 | |||
1894 | FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1895 | { | ||
1896 | FLAC__uint32 x8, x32 = 0; | ||
1897 | |||
1898 | /* this doesn't need to be that fast as currently it is only used for vorbis comments */ | ||
1899 | |||
1900 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data)) | ||
1901 | return false; | ||
1902 | |||
1903 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data)) | ||
1904 | return false; | ||
1905 | x32 |= (x8 << 8); | ||
1906 | |||
1907 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data)) | ||
1908 | return false; | ||
1909 | x32 |= (x8 << 16); | ||
1910 | |||
1911 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data)) | ||
1912 | return false; | ||
1913 | x32 |= (x8 << 24); | ||
1914 | |||
1915 | *val = x32; | ||
1916 | return true; | ||
1917 | } | ||
1918 | |||
1919 | FLAC__bool FLAC__bitbuffer_skip_bits_no_crc(FLAC__BitBuffer *bb, unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1920 | { | ||
1921 | /* | ||
1922 | * @@@ a slightly faster implementation is possible but | ||
1923 | * probably not that useful since this is only called a | ||
1924 | * couple of times in the metadata readers. | ||
1925 | */ | ||
1926 | FLAC__ASSERT(0 != bb); | ||
1927 | FLAC__ASSERT(0 != bb->buffer); | ||
1928 | |||
1929 | if(bits > 0) { | ||
1930 | const unsigned n = bb->consumed_bits & 7; | ||
1931 | unsigned m; | ||
1932 | FLAC__uint32 x; | ||
1933 | |||
1934 | if(n != 0) { | ||
1935 | m = min(8-n, bits); | ||
1936 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data)) | ||
1937 | return false; | ||
1938 | bits -= m; | ||
1939 | } | ||
1940 | m = bits / 8; | ||
1941 | if(m > 0) { | ||
1942 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data)) | ||
1943 | return false; | ||
1944 | bits %= 8; | ||
1945 | } | ||
1946 | if(bits > 0) { | ||
1947 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data)) | ||
1948 | return false; | ||
1949 | } | ||
1950 | } | ||
1951 | |||
1952 | return true; | ||
1953 | } | ||
1954 | |||
1955 | FLAC__bool FLAC__bitbuffer_read_byte_block_aligned_no_crc(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1956 | { | ||
1957 | FLAC__ASSERT(0 != bb); | ||
1958 | FLAC__ASSERT(0 != bb->buffer); | ||
1959 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb)); | ||
1960 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb)); | ||
1961 | #if FLAC__BITS_PER_BLURB == 8 | ||
1962 | while(nvals > 0) { | ||
1963 | unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs); | ||
1964 | if(chunk == 0) { | ||
1965 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
1966 | return false; | ||
1967 | } | ||
1968 | else { | ||
1969 | if(0 != val) { | ||
1970 | memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk); | ||
1971 | val += FLAC__BYTES_PER_BLURB * chunk; | ||
1972 | } | ||
1973 | nvals -= chunk; | ||
1974 | bb->consumed_blurbs += chunk; | ||
1975 | bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2); | ||
1976 | } | ||
1977 | } | ||
1978 | #else | ||
1979 | @@@ need to write this still | ||
1980 | FLAC__ASSERT(0); | ||
1981 | #endif | ||
1982 | |||
1983 | return true; | ||
1984 | } | ||
1985 | |||
1986 | FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
1987 | #ifdef FLAC__NO_MANUAL_INLINING | ||
1988 | { | ||
1989 | unsigned bit, val_ = 0; | ||
1990 | |||
1991 | FLAC__ASSERT(0 != bb); | ||
1992 | FLAC__ASSERT(0 != bb->buffer); | ||
1993 | |||
1994 | while(1) { | ||
1995 | if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data)) | ||
1996 | return false; | ||
1997 | if(bit) | ||
1998 | break; | ||
1999 | else | ||
2000 | val_++; | ||
2001 | } | ||
2002 | *val = val_; | ||
2003 | return true; | ||
2004 | } | ||
2005 | #else | ||
2006 | { | ||
2007 | unsigned i, val_ = 0; | ||
2008 | unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB; | ||
2009 | FLAC__blurb b; | ||
2010 | |||
2011 | FLAC__ASSERT(0 != bb); | ||
2012 | FLAC__ASSERT(0 != bb->buffer); | ||
2013 | |||
2014 | #if FLAC__BITS_PER_BLURB > 8 | ||
2015 | if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/ | ||
2016 | #endif | ||
2017 | if(bb->consumed_bits) { | ||
2018 | b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits; | ||
2019 | if(b) { | ||
2020 | for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++) | ||
2021 | b <<= 1; | ||
2022 | *val = i; | ||
2023 | i++; | ||
2024 | bb->consumed_bits += i; | ||
2025 | bb->total_consumed_bits += i; | ||
2026 | if(bb->consumed_bits == FLAC__BITS_PER_BLURB) { | ||
2027 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
2028 | bb->consumed_blurbs++; | ||
2029 | bb->consumed_bits = 0; | ||
2030 | } | ||
2031 | return true; | ||
2032 | } | ||
2033 | else { | ||
2034 | val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits; | ||
2035 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
2036 | bb->consumed_blurbs++; | ||
2037 | bb->consumed_bits = 0; | ||
2038 | bb->total_consumed_bits += val_; | ||
2039 | } | ||
2040 | } | ||
2041 | while(1) { | ||
2042 | if(bb->consumed_blurbs >= total_blurbs_) { | ||
2043 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
2044 | return false; | ||
2045 | total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB; | ||
2046 | } | ||
2047 | b = bb->buffer[bb->consumed_blurbs]; | ||
2048 | if(b) { | ||
2049 | for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++) | ||
2050 | b <<= 1; | ||
2051 | val_ += i; | ||
2052 | i++; | ||
2053 | bb->consumed_bits = i; | ||
2054 | *val = val_; | ||
2055 | if(i == FLAC__BITS_PER_BLURB) { | ||
2056 | CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16); | ||
2057 | bb->consumed_blurbs++; | ||
2058 | bb->consumed_bits = 0; | ||
2059 | } | ||
2060 | bb->total_consumed_bits += i; | ||
2061 | return true; | ||
2062 | } | ||
2063 | else { | ||
2064 | val_ += FLAC__BITS_PER_BLURB; | ||
2065 | CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16); | ||
2066 | bb->consumed_blurbs++; | ||
2067 | /* bb->consumed_bits is already 0 */ | ||
2068 | bb->total_consumed_bits += FLAC__BITS_PER_BLURB; | ||
2069 | } | ||
2070 | } | ||
2071 | #if FLAC__BITS_PER_BLURB > 8 | ||
2072 | } | ||
2073 | else { | ||
2074 | while(1) { | ||
2075 | if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data)) | ||
2076 | return false; | ||
2077 | if(i) | ||
2078 | break; | ||
2079 | else | ||
2080 | val_++; | ||
2081 | } | ||
2082 | *val = val_; | ||
2083 | return true; | ||
2084 | } | ||
2085 | #endif | ||
2086 | } | ||
2087 | #endif | ||
2088 | |||
2089 | #ifdef FLAC__SYMMETRIC_RICE | ||
2090 | FLAC__bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
2091 | { | ||
2092 | FLAC__uint32 sign = 0, lsbs = 0, msbs = 0; | ||
2093 | |||
2094 | FLAC__ASSERT(0 != bb); | ||
2095 | FLAC__ASSERT(0 != bb->buffer); | ||
2096 | FLAC__ASSERT(parameter <= 31); | ||
2097 | |||
2098 | /* read the unary MSBs and end bit */ | ||
2099 | if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data)) | ||
2100 | return false; | ||
2101 | |||
2102 | /* read the sign bit */ | ||
2103 | if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data)) | ||
2104 | return false; | ||
2105 | |||
2106 | /* read the binary LSBs */ | ||
2107 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data)) | ||
2108 | return false; | ||
2109 | |||
2110 | /* compose the value */ | ||
2111 | *val = (msbs << parameter) | lsbs; | ||
2112 | if(sign) | ||
2113 | *val = -(*val); | ||
2114 | |||
2115 | return true; | ||
2116 | } | ||
2117 | #endif /* ifdef FLAC__SYMMETRIC_RICE */ | ||
2118 | |||
2119 | FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
2120 | { | ||
2121 | FLAC__uint32 lsbs = 0, msbs = 0; | ||
2122 | unsigned uval; | ||
2123 | |||
2124 | FLAC__ASSERT(0 != bb); | ||
2125 | FLAC__ASSERT(0 != bb->buffer); | ||
2126 | FLAC__ASSERT(parameter <= 31); | ||
2127 | |||
2128 | /* read the unary MSBs and end bit */ | ||
2129 | if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data)) | ||
2130 | return false; | ||
2131 | |||
2132 | /* read the binary LSBs */ | ||
2133 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data)) | ||
2134 | return false; | ||
2135 | |||
2136 | /* compose the value */ | ||
2137 | uval = (msbs << parameter) | lsbs; | ||
2138 | if(uval & 1) | ||
2139 | *val = -((int)(uval >> 1)) - 1; | ||
2140 | else | ||
2141 | *val = (int)(uval >> 1); | ||
2142 | |||
2143 | return true; | ||
2144 | } | ||
2145 | |||
2146 | FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[], unsigned nvals, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
2147 | #ifdef FLAC__OLD_MSVC_FLAVOR | ||
2148 | { | ||
2149 | const FLAC__blurb *buffer = bb->buffer; | ||
2150 | |||
2151 | unsigned i, j, val_i = 0; | ||
2152 | unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0; | ||
2153 | FLAC__blurb blurb, save_blurb; | ||
2154 | unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */ | ||
2155 | |||
2156 | FLAC__ASSERT(0 != bb); | ||
2157 | FLAC__ASSERT(0 != bb->buffer); | ||
2158 | FLAC__ASSERT(parameter <= 31); | ||
2159 | |||
2160 | if(nvals == 0) | ||
2161 | return true; | ||
2162 | |||
2163 | i = bb->consumed_blurbs; | ||
2164 | /* | ||
2165 | * We unroll the main loop to take care of partially consumed blurbs here. | ||
2166 | */ | ||
2167 | if(bb->consumed_bits > 0) { | ||
2168 | save_blurb = blurb = buffer[i]; | ||
2169 | cbits = bb->consumed_bits; | ||
2170 | blurb <<= cbits; | ||
2171 | |||
2172 | while(1) { | ||
2173 | if(state == 0) { | ||
2174 | if(blurb) { | ||
2175 | for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++) | ||
2176 | blurb <<= 1; | ||
2177 | msbs += j; | ||
2178 | |||
2179 | /* dispose of the unary end bit */ | ||
2180 | blurb <<= 1; | ||
2181 | j++; | ||
2182 | cbits += j; | ||
2183 | |||
2184 | uval = 0; | ||
2185 | lsbs_left = parameter; | ||
2186 | state++; | ||
2187 | if(cbits == FLAC__BITS_PER_BLURB) { | ||
2188 | cbits = 0; | ||
2189 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2190 | break; | ||
2191 | } | ||
2192 | } | ||
2193 | else { | ||
2194 | msbs += FLAC__BITS_PER_BLURB - cbits; | ||
2195 | cbits = 0; | ||
2196 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2197 | break; | ||
2198 | } | ||
2199 | } | ||
2200 | else { | ||
2201 | const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits; | ||
2202 | if(lsbs_left >= available_bits) { | ||
2203 | uval <<= available_bits; | ||
2204 | uval |= (blurb >> cbits); | ||
2205 | cbits = 0; | ||
2206 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2207 | |||
2208 | if(lsbs_left == available_bits) { | ||
2209 | /* compose the value */ | ||
2210 | uval |= (msbs << parameter); | ||
2211 | if(uval & 1) | ||
2212 | vals[val_i++] = -((int)(uval >> 1)) - 1; | ||
2213 | else | ||
2214 | vals[val_i++] = (int)(uval >> 1); | ||
2215 | if(val_i == nvals) | ||
2216 | break; | ||
2217 | |||
2218 | msbs = 0; | ||
2219 | state = 0; | ||
2220 | } | ||
2221 | |||
2222 | lsbs_left -= available_bits; | ||
2223 | break; | ||
2224 | } | ||
2225 | else { | ||
2226 | uval <<= lsbs_left; | ||
2227 | uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left)); | ||
2228 | blurb <<= lsbs_left; | ||
2229 | cbits += lsbs_left; | ||
2230 | |||
2231 | /* compose the value */ | ||
2232 | uval |= (msbs << parameter); | ||
2233 | if(uval & 1) | ||
2234 | vals[val_i++] = -((int)(uval >> 1)) - 1; | ||
2235 | else | ||
2236 | vals[val_i++] = (int)(uval >> 1); | ||
2237 | if(val_i == nvals) { | ||
2238 | /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */ | ||
2239 | i--; | ||
2240 | break; | ||
2241 | } | ||
2242 | |||
2243 | msbs = 0; | ||
2244 | state = 0; | ||
2245 | } | ||
2246 | } | ||
2247 | } | ||
2248 | i++; | ||
2249 | |||
2250 | bb->consumed_blurbs = i; | ||
2251 | bb->consumed_bits = cbits; | ||
2252 | bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits; | ||
2253 | } | ||
2254 | |||
2255 | /* | ||
2256 | * Now that we are blurb-aligned the logic is slightly simpler | ||
2257 | */ | ||
2258 | while(val_i < nvals) { | ||
2259 | for( ; i < bb->blurbs && val_i < nvals; i++) { | ||
2260 | save_blurb = blurb = buffer[i]; | ||
2261 | cbits = 0; | ||
2262 | while(1) { | ||
2263 | if(state == 0) { | ||
2264 | if(blurb) { | ||
2265 | for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++) | ||
2266 | blurb <<= 1; | ||
2267 | msbs += j; | ||
2268 | |||
2269 | /* dispose of the unary end bit */ | ||
2270 | blurb <<= 1; | ||
2271 | j++; | ||
2272 | cbits += j; | ||
2273 | |||
2274 | uval = 0; | ||
2275 | lsbs_left = parameter; | ||
2276 | state++; | ||
2277 | if(cbits == FLAC__BITS_PER_BLURB) { | ||
2278 | cbits = 0; | ||
2279 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2280 | break; | ||
2281 | } | ||
2282 | } | ||
2283 | else { | ||
2284 | msbs += FLAC__BITS_PER_BLURB - cbits; | ||
2285 | cbits = 0; | ||
2286 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2287 | break; | ||
2288 | } | ||
2289 | } | ||
2290 | else { | ||
2291 | const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits; | ||
2292 | if(lsbs_left >= available_bits) { | ||
2293 | uval <<= available_bits; | ||
2294 | uval |= (blurb >> cbits); | ||
2295 | cbits = 0; | ||
2296 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2297 | |||
2298 | if(lsbs_left == available_bits) { | ||
2299 | /* compose the value */ | ||
2300 | uval |= (msbs << parameter); | ||
2301 | if(uval & 1) | ||
2302 | vals[val_i++] = -((int)(uval >> 1)) - 1; | ||
2303 | else | ||
2304 | vals[val_i++] = (int)(uval >> 1); | ||
2305 | if(val_i == nvals) | ||
2306 | break; | ||
2307 | |||
2308 | msbs = 0; | ||
2309 | state = 0; | ||
2310 | } | ||
2311 | |||
2312 | lsbs_left -= available_bits; | ||
2313 | break; | ||
2314 | } | ||
2315 | else { | ||
2316 | uval <<= lsbs_left; | ||
2317 | uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left)); | ||
2318 | blurb <<= lsbs_left; | ||
2319 | cbits += lsbs_left; | ||
2320 | |||
2321 | /* compose the value */ | ||
2322 | uval |= (msbs << parameter); | ||
2323 | if(uval & 1) | ||
2324 | vals[val_i++] = -((int)(uval >> 1)) - 1; | ||
2325 | else | ||
2326 | vals[val_i++] = (int)(uval >> 1); | ||
2327 | if(val_i == nvals) { | ||
2328 | /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */ | ||
2329 | i--; | ||
2330 | break; | ||
2331 | } | ||
2332 | |||
2333 | msbs = 0; | ||
2334 | state = 0; | ||
2335 | } | ||
2336 | } | ||
2337 | } | ||
2338 | } | ||
2339 | bb->consumed_blurbs = i; | ||
2340 | bb->consumed_bits = cbits; | ||
2341 | bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits; | ||
2342 | if(val_i < nvals) { | ||
2343 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
2344 | return false; | ||
2345 | /* these must be zero because we can only get here if we got to the end of the buffer */ | ||
2346 | FLAC__ASSERT(bb->consumed_blurbs == 0); | ||
2347 | FLAC__ASSERT(bb->consumed_bits == 0); | ||
2348 | i = 0; | ||
2349 | } | ||
2350 | } | ||
2351 | |||
2352 | return true; | ||
2353 | } | ||
2354 | #else | ||
2355 | { | ||
2356 | const FLAC__blurb *buffer = bb->buffer; | ||
2357 | |||
2358 | unsigned i, j, val_i = nvals; | ||
2359 | unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0; | ||
2360 | FLAC__blurb blurb, save_blurb; | ||
2361 | unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */ | ||
2362 | |||
2363 | FLAC__ASSERT(0 != bb); | ||
2364 | FLAC__ASSERT(0 != bb->buffer); | ||
2365 | FLAC__ASSERT(parameter <= 31); | ||
2366 | |||
2367 | if(nvals == 0) | ||
2368 | return true; | ||
2369 | |||
2370 | cbits = bb->consumed_bits; | ||
2371 | i = bb->consumed_blurbs; | ||
2372 | while(val_i != 0) { | ||
2373 | for( ; i < bb->blurbs; i++) { | ||
2374 | blurb = (save_blurb = buffer[i]) << cbits; | ||
2375 | while(1) { | ||
2376 | if(state == 0) { | ||
2377 | if(blurb) { | ||
2378 | j = FLAC__ALIGNED_BLURB_UNARY(blurb); | ||
2379 | msbs += j; | ||
2380 | j++; | ||
2381 | cbits += j; | ||
2382 | |||
2383 | uval = 0; | ||
2384 | lsbs_left = parameter; | ||
2385 | state++; | ||
2386 | if(cbits == FLAC__BITS_PER_BLURB) { | ||
2387 | cbits = 0; | ||
2388 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2389 | break; | ||
2390 | } | ||
2391 | blurb <<= j; | ||
2392 | } | ||
2393 | else { | ||
2394 | msbs += FLAC__BITS_PER_BLURB - cbits; | ||
2395 | cbits = 0; | ||
2396 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2397 | break; | ||
2398 | } | ||
2399 | } | ||
2400 | else { | ||
2401 | const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits; | ||
2402 | if(lsbs_left >= available_bits) { | ||
2403 | uval <<= available_bits; | ||
2404 | uval |= (blurb >> cbits); | ||
2405 | cbits = 0; | ||
2406 | CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); | ||
2407 | |||
2408 | if(lsbs_left == available_bits) { | ||
2409 | /* compose the value */ | ||
2410 | uval |= (msbs << parameter); | ||
2411 | *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); | ||
2412 | --val_i; | ||
2413 | if(val_i == 0) { | ||
2414 | i++; | ||
2415 | goto break2; | ||
2416 | } | ||
2417 | ++vals; | ||
2418 | |||
2419 | msbs = 0; | ||
2420 | state = 0; | ||
2421 | } | ||
2422 | |||
2423 | lsbs_left -= available_bits; | ||
2424 | break; | ||
2425 | } | ||
2426 | else { | ||
2427 | cbits += lsbs_left; | ||
2428 | uval <<= lsbs_left; | ||
2429 | uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left)); | ||
2430 | blurb <<= lsbs_left; | ||
2431 | |||
2432 | /* compose the value */ | ||
2433 | uval |= (msbs << parameter); | ||
2434 | *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); | ||
2435 | --val_i; | ||
2436 | if(val_i == 0) | ||
2437 | goto break2; | ||
2438 | ++vals; | ||
2439 | |||
2440 | msbs = 0; | ||
2441 | state = 0; | ||
2442 | } | ||
2443 | } | ||
2444 | } | ||
2445 | } | ||
2446 | break2: | ||
2447 | bb->consumed_blurbs = i; | ||
2448 | bb->consumed_bits = cbits; | ||
2449 | bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits; | ||
2450 | if(val_i != 0) { | ||
2451 | if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) | ||
2452 | return false; | ||
2453 | /* these must be zero because we can only get here if we got to the end of the buffer */ | ||
2454 | FLAC__ASSERT(bb->consumed_blurbs == 0); | ||
2455 | FLAC__ASSERT(bb->consumed_bits == 0); | ||
2456 | i = 0; | ||
2457 | } | ||
2458 | } | ||
2459 | |||
2460 | return true; | ||
2461 | } | ||
2462 | #endif | ||
2463 | |||
2464 | #if 0 /* UNUSED */ | ||
2465 | FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
2466 | { | ||
2467 | FLAC__uint32 lsbs = 0, msbs = 0; | ||
2468 | unsigned bit, uval, k; | ||
2469 | |||
2470 | FLAC__ASSERT(0 != bb); | ||
2471 | FLAC__ASSERT(0 != bb->buffer); | ||
2472 | |||
2473 | k = FLAC__bitmath_ilog2(parameter); | ||
2474 | |||
2475 | /* read the unary MSBs and end bit */ | ||
2476 | if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data)) | ||
2477 | return false; | ||
2478 | |||
2479 | /* read the binary LSBs */ | ||
2480 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data)) | ||
2481 | return false; | ||
2482 | |||
2483 | if(parameter == 1u<<k) { | ||
2484 | /* compose the value */ | ||
2485 | uval = (msbs << k) | lsbs; | ||
2486 | } | ||
2487 | else { | ||
2488 | unsigned d = (1 << (k+1)) - parameter; | ||
2489 | if(lsbs >= d) { | ||
2490 | if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data)) | ||
2491 | return false; | ||
2492 | lsbs <<= 1; | ||
2493 | lsbs |= bit; | ||
2494 | lsbs -= d; | ||
2495 | } | ||
2496 | /* compose the value */ | ||
2497 | uval = msbs * parameter + lsbs; | ||
2498 | } | ||
2499 | |||
2500 | /* unfold unsigned to signed */ | ||
2501 | if(uval & 1) | ||
2502 | *val = -((int)(uval >> 1)) - 1; | ||
2503 | else | ||
2504 | *val = (int)(uval >> 1); | ||
2505 | |||
2506 | return true; | ||
2507 | } | ||
2508 | |||
2509 | FLAC__bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data) | ||
2510 | { | ||
2511 | FLAC__uint32 lsbs, msbs = 0; | ||
2512 | unsigned bit, k; | ||
2513 | |||
2514 | FLAC__ASSERT(0 != bb); | ||
2515 | FLAC__ASSERT(0 != bb->buffer); | ||
2516 | |||
2517 | k = FLAC__bitmath_ilog2(parameter); | ||
2518 | |||
2519 | /* read the unary MSBs and end bit */ | ||
2520 | if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data)) | ||
2521 | return false; | ||
2522 | |||
2523 | /* read the binary LSBs */ | ||
2524 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data)) | ||
2525 | return false; | ||
2526 | |||
2527 | if(parameter == 1u<<k) { | ||
2528 | /* compose the value */ | ||
2529 | *val = (msbs << k) | lsbs; | ||
2530 | } | ||
2531 | else { | ||
2532 | unsigned d = (1 << (k+1)) - parameter; | ||
2533 | if(lsbs >= d) { | ||
2534 | if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data)) | ||
2535 | return false; | ||
2536 | lsbs <<= 1; | ||
2537 | lsbs |= bit; | ||
2538 | lsbs -= d; | ||
2539 | } | ||
2540 | /* compose the value */ | ||
2541 | *val = msbs * parameter + lsbs; | ||
2542 | } | ||
2543 | |||
2544 | return true; | ||
2545 | } | ||
2546 | #endif /* UNUSED */ | ||
2547 | |||
2548 | /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */ | ||
2549 | FLAC__bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data, FLAC__byte *raw, unsigned *rawlen) | ||
2550 | { | ||
2551 | FLAC__uint32 v = 0; | ||
2552 | FLAC__uint32 x; | ||
2553 | unsigned i; | ||
2554 | |||
2555 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data)) | ||
2556 | return false; | ||
2557 | if(raw) | ||
2558 | raw[(*rawlen)++] = (FLAC__byte)x; | ||
2559 | if(!(x & 0x80)) { /* 0xxxxxxx */ | ||
2560 | v = x; | ||
2561 | i = 0; | ||
2562 | } | ||
2563 | else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ | ||
2564 | v = x & 0x1F; | ||
2565 | i = 1; | ||
2566 | } | ||
2567 | else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ | ||
2568 | v = x & 0x0F; | ||
2569 | i = 2; | ||
2570 | } | ||
2571 | else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ | ||
2572 | v = x & 0x07; | ||
2573 | i = 3; | ||
2574 | } | ||
2575 | else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ | ||
2576 | v = x & 0x03; | ||
2577 | i = 4; | ||
2578 | } | ||
2579 | else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ | ||
2580 | v = x & 0x01; | ||
2581 | i = 5; | ||
2582 | } | ||
2583 | else { | ||
2584 | *val = 0xffffffff; | ||
2585 | return true; | ||
2586 | } | ||
2587 | for( ; i; i--) { | ||
2588 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data)) | ||
2589 | return false; | ||
2590 | if(raw) | ||
2591 | raw[(*rawlen)++] = (FLAC__byte)x; | ||
2592 | if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ | ||
2593 | *val = 0xffffffff; | ||
2594 | return true; | ||
2595 | } | ||
2596 | v <<= 6; | ||
2597 | v |= (x & 0x3F); | ||
2598 | } | ||
2599 | *val = v; | ||
2600 | return true; | ||
2601 | } | ||
2602 | |||
2603 | /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */ | ||
2604 | FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data, FLAC__byte *raw, unsigned *rawlen) | ||
2605 | { | ||
2606 | FLAC__uint64 v = 0; | ||
2607 | FLAC__uint32 x; | ||
2608 | unsigned i; | ||
2609 | |||
2610 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data)) | ||
2611 | return false; | ||
2612 | if(raw) | ||
2613 | raw[(*rawlen)++] = (FLAC__byte)x; | ||
2614 | if(!(x & 0x80)) { /* 0xxxxxxx */ | ||
2615 | v = x; | ||
2616 | i = 0; | ||
2617 | } | ||
2618 | else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ | ||
2619 | v = x & 0x1F; | ||
2620 | i = 1; | ||
2621 | } | ||
2622 | else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ | ||
2623 | v = x & 0x0F; | ||
2624 | i = 2; | ||
2625 | } | ||
2626 | else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ | ||
2627 | v = x & 0x07; | ||
2628 | i = 3; | ||
2629 | } | ||
2630 | else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ | ||
2631 | v = x & 0x03; | ||
2632 | i = 4; | ||
2633 | } | ||
2634 | else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ | ||
2635 | v = x & 0x01; | ||
2636 | i = 5; | ||
2637 | } | ||
2638 | else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */ | ||
2639 | v = 0; | ||
2640 | i = 6; | ||
2641 | } | ||
2642 | else { | ||
2643 | *val = FLAC__U64L(0xffffffffffffffff); | ||
2644 | return true; | ||
2645 | } | ||
2646 | for( ; i; i--) { | ||
2647 | if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data)) | ||
2648 | return false; | ||
2649 | if(raw) | ||
2650 | raw[(*rawlen)++] = (FLAC__byte)x; | ||
2651 | if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ | ||
2652 | *val = FLAC__U64L(0xffffffffffffffff); | ||
2653 | return true; | ||
2654 | } | ||
2655 | v <<= 6; | ||
2656 | v |= (x & 0x3F); | ||
2657 | } | ||
2658 | *val = v; | ||
2659 | return true; | ||
2660 | } | ||
2661 | |||
2662 | void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out) | ||
2663 | { | ||
2664 | unsigned i, j; | ||
2665 | if(bb == 0) { | ||
2666 | fprintf(out, "bitbuffer is NULL\n"); | ||
2667 | } | ||
2668 | else { | ||
2669 | fprintf(out, "bitbuffer: capacity=%u blurbs=%u bits=%u total_bits=%u consumed: blurbs=%u, bits=%u, total_bits=%u\n", bb->capacity, bb->blurbs, bb->bits, bb->total_bits, bb->consumed_blurbs, bb->consumed_bits, bb->total_consumed_bits); | ||
2670 | |||
2671 | for(i = 0; i < bb->blurbs; i++) { | ||
2672 | fprintf(out, "%08X: ", i); | ||
2673 | for(j = 0; j < FLAC__BITS_PER_BLURB; j++) | ||
2674 | if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits) | ||
2675 | fprintf(out, "."); | ||
2676 | else | ||
2677 | fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0); | ||
2678 | fprintf(out, "\n"); | ||
2679 | } | ||
2680 | if(bb->bits > 0) { | ||
2681 | fprintf(out, "%08X: ", i); | ||
2682 | for(j = 0; j < bb->bits; j++) | ||
2683 | if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits) | ||
2684 | fprintf(out, "."); | ||
2685 | else | ||
2686 | fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0); | ||
2687 | fprintf(out, "\n"); | ||
2688 | } | ||
2689 | } | ||
2690 | } | ||