/src/flac/src/libFLAC/bitwriter.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* libFLAC - Free Lossless Audio Codec library |
2 | | * Copyright (C) 2000-2009 Josh Coalson |
3 | | * Copyright (C) 2011-2025 Xiph.Org Foundation |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * |
9 | | * - Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * |
12 | | * - Redistributions in binary form must reproduce the above copyright |
13 | | * notice, this list of conditions and the following disclaimer in the |
14 | | * documentation and/or other materials provided with the distribution. |
15 | | * |
16 | | * - Neither the name of the Xiph.org Foundation nor the names of its |
17 | | * contributors may be used to endorse or promote products derived from |
18 | | * this software without specific prior written permission. |
19 | | * |
20 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
24 | | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | | */ |
32 | | |
33 | | #ifdef HAVE_CONFIG_H |
34 | | # include <config.h> |
35 | | #endif |
36 | | |
37 | | #include <stdlib.h> |
38 | | #include <string.h> |
39 | | #include "private/bitwriter.h" |
40 | | #include "private/crc.h" |
41 | | #include "private/format.h" |
42 | | #include "private/macros.h" |
43 | | #include "private/stream_encoder.h" |
44 | | #include "FLAC/assert.h" |
45 | | #include "share/alloc.h" |
46 | | #include "share/compat.h" |
47 | | #include "share/endswap.h" |
48 | | |
49 | | /* Things should be fastest when this matches the machine word size */ |
50 | | /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */ |
51 | | /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */ |
52 | | |
53 | | #if (ENABLE_64_BIT_WORDS == 0) |
54 | | |
55 | | typedef FLAC__uint32 bwword; |
56 | | typedef FLAC__uint64 FLAC__bwtemp; |
57 | | #define FLAC__BYTES_PER_WORD 4 /* sizeof bwword */ |
58 | | #define FLAC__BITS_PER_WORD 32 |
59 | | #define FLAC__TEMP_BITS 64 |
60 | | #define FLAC__HALF_TEMP_BITS 32 |
61 | | /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */ |
62 | | #if WORDS_BIGENDIAN |
63 | | #define SWAP_BE_WORD_TO_HOST(x) (x) |
64 | | #else |
65 | | #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x) |
66 | | #endif |
67 | | |
68 | | #else |
69 | | |
70 | | typedef FLAC__uint64 bwword; |
71 | | typedef FLAC__uint64 FLAC__bwtemp; |
72 | 5.87M | #define FLAC__BYTES_PER_WORD 8 /* sizeof bwword */ |
73 | 268M | #define FLAC__BITS_PER_WORD 64 |
74 | 5.55M | #define FLAC__TEMP_BITS 64 |
75 | 592M | #define FLAC__HALF_TEMP_BITS 32 |
76 | | /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */ |
77 | | #if WORDS_BIGENDIAN |
78 | | #define SWAP_BE_WORD_TO_HOST(x) (x) |
79 | | #else |
80 | 105M | #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_64(x) |
81 | | #endif |
82 | | |
83 | | #endif |
84 | | |
85 | | /* |
86 | | * The default capacity here doesn't matter too much. The buffer always grows |
87 | | * to hold whatever is written to it. Usually the encoder will stop adding at |
88 | | * a frame or metadata block, then write that out and clear the buffer for the |
89 | | * next one. |
90 | | */ |
91 | | static const uint32_t FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */ |
92 | | /* When growing, increment with 1/4th at a time */ |
93 | | static const uint32_t FLAC__BITWRITER_DEFAULT_GROW_FRACTION = 2; /* means grow by >> 2 (1/4th) of current size */ |
94 | | |
95 | 97.2k | #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD) |
96 | 97.2k | #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits) |
97 | | |
98 | | struct FLAC__BitWriter { |
99 | | bwword *buffer; |
100 | | bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */ |
101 | | uint32_t capacity; /* capacity of buffer in words */ |
102 | | uint32_t words; /* # of complete words in buffer */ |
103 | | uint32_t bits; /* # of used bits in accum */ |
104 | | }; |
105 | | |
106 | | /* * WATCHOUT: The current implementation only grows the buffer. */ |
107 | | #ifndef __SUNPRO_C |
108 | | static |
109 | | #endif |
110 | | FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add) |
111 | 339k | { |
112 | 339k | uint32_t new_capacity; |
113 | 339k | bwword *new_buffer; |
114 | | |
115 | 339k | FLAC__ASSERT(0 != bw); |
116 | 339k | FLAC__ASSERT(0 != bw->buffer); |
117 | | |
118 | | /* calculate total words needed to store 'bits_to_add' additional bits */ |
119 | 339k | new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD); |
120 | | |
121 | | /* it's possible (due to pessimism in the growth estimation that |
122 | | * leads to this call) that we don't actually need to grow |
123 | | */ |
124 | 339k | if(bw->capacity >= new_capacity) |
125 | 330k | return true; |
126 | | |
127 | 8.99k | if(new_capacity * sizeof(bwword) > (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) |
128 | | /* Requested new capacity is larger than the largest possible metadata block, |
129 | | * which is also larger than the largest sane framesize. That means something |
130 | | * went very wrong somewhere and previous checks failed. |
131 | | * To prevent chrashing, give up */ |
132 | 12 | return false; |
133 | | |
134 | | /* As reallocation can be quite expensive, grow exponentially */ |
135 | 8.97k | if((new_capacity - bw->capacity) < (bw->capacity >> FLAC__BITWRITER_DEFAULT_GROW_FRACTION)) |
136 | 6.38k | new_capacity = bw->capacity + (bw->capacity >> FLAC__BITWRITER_DEFAULT_GROW_FRACTION); |
137 | | |
138 | | /* make sure we got everything right */ |
139 | 8.97k | FLAC__ASSERT(new_capacity > bw->capacity); |
140 | 8.97k | FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD)); |
141 | | |
142 | 8.97k | new_buffer = safe_realloc_nofree_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity); |
143 | 8.97k | if(new_buffer == 0) |
144 | 36 | return false; |
145 | 8.94k | bw->buffer = new_buffer; |
146 | 8.94k | bw->capacity = new_capacity; |
147 | 8.94k | return true; |
148 | 8.97k | } |
149 | | |
150 | | |
151 | | /*********************************************************************** |
152 | | * |
153 | | * Class constructor/destructor |
154 | | * |
155 | | ***********************************************************************/ |
156 | | |
157 | | FLAC__BitWriter *FLAC__bitwriter_new(void) |
158 | 45.4k | { |
159 | 45.4k | FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter)); |
160 | | /* note that calloc() sets all members to 0 for us */ |
161 | 45.4k | return bw; |
162 | 45.4k | } |
163 | | |
164 | | void FLAC__bitwriter_delete(FLAC__BitWriter *bw) |
165 | 45.4k | { |
166 | 45.4k | FLAC__ASSERT(0 != bw); |
167 | | |
168 | 45.4k | FLAC__bitwriter_free(bw); |
169 | 45.4k | free(bw); |
170 | 45.4k | } |
171 | | |
172 | | /*********************************************************************** |
173 | | * |
174 | | * Public class methods |
175 | | * |
176 | | ***********************************************************************/ |
177 | | |
178 | | FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw) |
179 | 44.1k | { |
180 | 44.1k | FLAC__ASSERT(0 != bw); |
181 | | |
182 | 44.1k | bw->words = bw->bits = 0; |
183 | 44.1k | bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY; |
184 | 44.1k | bw->buffer = malloc(sizeof(bwword) * bw->capacity); |
185 | 44.1k | if(bw->buffer == 0) |
186 | 0 | return false; |
187 | | |
188 | 44.1k | return true; |
189 | 44.1k | } |
190 | | |
191 | | void FLAC__bitwriter_free(FLAC__BitWriter *bw) |
192 | 45.4k | { |
193 | 45.4k | FLAC__ASSERT(0 != bw); |
194 | | |
195 | 45.4k | if(0 != bw->buffer) |
196 | 44.1k | free(bw->buffer); |
197 | 45.4k | bw->buffer = 0; |
198 | 45.4k | bw->capacity = 0; |
199 | 45.4k | bw->words = bw->bits = 0; |
200 | 45.4k | } |
201 | | |
202 | | void FLAC__bitwriter_clear(FLAC__BitWriter *bw) |
203 | 2.08M | { |
204 | 2.08M | bw->words = bw->bits = 0; |
205 | 2.08M | } |
206 | | |
207 | | FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc) |
208 | 1.97M | { |
209 | 1.97M | const FLAC__byte *buffer; |
210 | 1.97M | size_t bytes; |
211 | | |
212 | 1.97M | FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */ |
213 | | |
214 | 1.97M | if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes)) |
215 | 0 | return false; |
216 | | |
217 | 1.97M | *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes); |
218 | 1.97M | FLAC__bitwriter_release_buffer(bw); |
219 | 1.97M | return true; |
220 | 1.97M | } |
221 | | |
222 | | FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc) |
223 | 1.98M | { |
224 | 1.98M | const FLAC__byte *buffer; |
225 | 1.98M | size_t bytes; |
226 | | |
227 | 1.98M | FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */ |
228 | | |
229 | 1.98M | if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes)) |
230 | 0 | return false; |
231 | | |
232 | 1.98M | *crc = FLAC__crc8(buffer, bytes); |
233 | 1.98M | FLAC__bitwriter_release_buffer(bw); |
234 | 1.98M | return true; |
235 | 1.98M | } |
236 | | |
237 | | FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw) |
238 | 6.04M | { |
239 | 6.04M | return ((bw->bits & 7) == 0); |
240 | 6.04M | } |
241 | | |
242 | | uint32_t FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw) |
243 | 97.2k | { |
244 | 97.2k | return FLAC__TOTAL_BITS(bw); |
245 | 97.2k | } |
246 | | |
247 | | FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes) |
248 | 5.87M | { |
249 | 5.87M | FLAC__ASSERT((bw->bits & 7) == 0); |
250 | | /* double protection */ |
251 | 5.87M | if(bw->bits & 7) |
252 | 0 | return false; |
253 | | /* if we have bits in the accumulator we have to flush those to the buffer first */ |
254 | 5.87M | if(bw->bits) { |
255 | 4.93M | FLAC__ASSERT(bw->words <= bw->capacity); |
256 | 4.93M | if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD)) |
257 | 0 | return false; |
258 | | /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */ |
259 | 4.93M | bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits)); |
260 | 4.93M | } |
261 | | /* now we can just return what we have */ |
262 | 5.87M | *buffer = (FLAC__byte*)bw->buffer; |
263 | 5.87M | *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3); |
264 | 5.87M | return true; |
265 | 5.87M | } |
266 | | |
267 | | void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw) |
268 | 5.86M | { |
269 | | /* nothing to do. in the future, strict checking of a 'writer-is-in- |
270 | | * get-mode' flag could be added everywhere and then cleared here |
271 | | */ |
272 | 5.86M | (void)bw; |
273 | 5.86M | } |
274 | | |
275 | | inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits) |
276 | 819k | { |
277 | 819k | uint32_t n; |
278 | | |
279 | 819k | FLAC__ASSERT(0 != bw); |
280 | 819k | FLAC__ASSERT(0 != bw->buffer); |
281 | | |
282 | 819k | if(bits == 0) |
283 | 425 | return true; |
284 | | /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */ |
285 | 818k | if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits)) |
286 | 4 | return false; |
287 | | /* first part gets to word alignment */ |
288 | 818k | if(bw->bits) { |
289 | 818k | n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits); |
290 | 818k | bw->accum <<= n; |
291 | 818k | bits -= n; |
292 | 818k | bw->bits += n; |
293 | 818k | if(bw->bits == FLAC__BITS_PER_WORD) { |
294 | 53.0k | bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); |
295 | 53.0k | bw->bits = 0; |
296 | 53.0k | } |
297 | 765k | else |
298 | 765k | return true; |
299 | 818k | } |
300 | | /* do whole words */ |
301 | 17.7M | while(bits >= FLAC__BITS_PER_WORD) { |
302 | 17.6M | bw->buffer[bw->words++] = 0; |
303 | 17.6M | bits -= FLAC__BITS_PER_WORD; |
304 | 17.6M | } |
305 | | /* do any leftovers */ |
306 | 53.0k | if(bits > 0) { |
307 | 11.9k | bw->accum = 0; |
308 | 11.9k | bw->bits = bits; |
309 | 11.9k | } |
310 | 53.0k | return true; |
311 | 818k | } |
312 | | |
313 | | static inline FLAC__bool FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits) |
314 | 228M | { |
315 | 228M | register uint32_t left; |
316 | | |
317 | | /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ |
318 | 228M | FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); |
319 | | |
320 | 228M | if(bw == 0 || bw->buffer == 0) |
321 | 0 | return false; |
322 | | |
323 | 228M | if (bits > 32) |
324 | 0 | return false; |
325 | | |
326 | 228M | if(bits == 0) |
327 | 0 | return true; |
328 | | |
329 | 228M | FLAC__ASSERT((bits == 32) || (val>>bits == 0)); |
330 | | |
331 | | /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */ |
332 | 228M | if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits)) |
333 | 12 | return false; |
334 | | |
335 | 228M | left = FLAC__BITS_PER_WORD - bw->bits; |
336 | 228M | if(bits < left) { |
337 | 175M | bw->accum <<= bits; |
338 | 175M | bw->accum |= val; |
339 | 175M | bw->bits += bits; |
340 | 175M | } |
341 | 53.1M | else if(bw->bits) { /* WATCHOUT: if bw->bits == 0, left==FLAC__BITS_PER_WORD and bw->accum<<=left is a NOP instead of setting to 0 */ |
342 | 53.1M | bw->accum <<= left; |
343 | 53.1M | bw->accum |= val >> (bw->bits = bits - left); |
344 | 53.1M | bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); |
345 | 53.1M | bw->accum = val; /* unused top bits can contain garbage */ |
346 | 53.1M | } |
347 | 18.4E | else { /* at this point bits == FLAC__BITS_PER_WORD == 32 and bw->bits == 0 */ |
348 | 18.4E | bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST((bwword)val); |
349 | 18.4E | } |
350 | | |
351 | 228M | return true; |
352 | 228M | } |
353 | | |
354 | | inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits) |
355 | 54.9M | { |
356 | | /* check that unused bits are unset */ |
357 | 54.9M | if((bits < 32) && (val>>bits != 0)) |
358 | 0 | return false; |
359 | | |
360 | 54.9M | return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, bits); |
361 | 54.9M | } |
362 | | |
363 | | inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits) |
364 | 40.4M | { |
365 | | /* zero-out unused bits */ |
366 | 40.4M | if(bits < 32) |
367 | 22.8M | val &= (~(0xffffffff << bits)); |
368 | | |
369 | 40.4M | return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, bits); |
370 | 40.4M | } |
371 | | |
372 | | inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits) |
373 | 19.3M | { |
374 | | /* this could be a little faster but it's not used for much */ |
375 | 19.3M | if(bits > 32) { |
376 | 18.3M | return |
377 | 18.3M | FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) && |
378 | 18.3M | FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 32); |
379 | 18.3M | } |
380 | 1.06M | else |
381 | 1.06M | return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits); |
382 | 19.3M | } |
383 | | |
384 | | inline FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits) |
385 | 2.86M | { |
386 | 2.86M | FLAC__uint64 uval = val; |
387 | | /* zero-out unused bits */ |
388 | 2.86M | if(bits < 64) |
389 | 2.86M | uval &= (~(UINT64_MAX << bits)); |
390 | 2.86M | return FLAC__bitwriter_write_raw_uint64(bw, uval, bits); |
391 | 2.86M | } |
392 | | |
393 | | inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val) |
394 | 30.3k | { |
395 | | /* this doesn't need to be that fast as currently it is only used for vorbis comments */ |
396 | | |
397 | 30.3k | if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val & 0xff, 8)) |
398 | 0 | return false; |
399 | 30.3k | if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>8) & 0xff, 8)) |
400 | 0 | return false; |
401 | 30.3k | if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>16) & 0xff, 8)) |
402 | 0 | return false; |
403 | 30.3k | if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val>>24, 8)) |
404 | 0 | return false; |
405 | | |
406 | 30.3k | return true; |
407 | 30.3k | } |
408 | | |
409 | | inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals) |
410 | 65.0k | { |
411 | 65.0k | uint32_t i; |
412 | | |
413 | | /* grow capacity upfront to prevent constant reallocation during writes */ |
414 | 65.0k | if(bw->capacity <= bw->words + nvals / (FLAC__BITS_PER_WORD / 8) + 1 && !bitwriter_grow_(bw, nvals * 8)) |
415 | 2 | return false; |
416 | | |
417 | | /* this could be faster but currently we don't need it to be since it's only used for writing metadata */ |
418 | 109M | for(i = 0; i < nvals; i++) { |
419 | 109M | if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)(vals[i]), 8)) |
420 | 0 | return false; |
421 | 109M | } |
422 | | |
423 | 65.0k | return true; |
424 | 65.0k | } |
425 | | |
426 | | FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, uint32_t val) |
427 | 600k | { |
428 | 600k | if(val < 32) |
429 | 600k | return FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, ++val); |
430 | 18.4E | else |
431 | 18.4E | return |
432 | 18.4E | FLAC__bitwriter_write_zeroes(bw, val) && |
433 | 18.4E | FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, 1); |
434 | 600k | } |
435 | | |
436 | | #if 0 /* UNUSED */ |
437 | | uint32_t FLAC__bitwriter_rice_bits(FLAC__int32 val, uint32_t parameter) |
438 | | { |
439 | | FLAC__uint32 uval; |
440 | | |
441 | | FLAC__ASSERT(parameter < 32); |
442 | | |
443 | | /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */ |
444 | | uval = val; |
445 | | uval <<= 1; |
446 | | uval ^= (val>>31); |
447 | | |
448 | | return 1 + parameter + (uval >> parameter); |
449 | | } |
450 | | |
451 | | uint32_t FLAC__bitwriter_golomb_bits_signed(int val, uint32_t parameter) |
452 | | { |
453 | | uint32_t bits, msbs, uval; |
454 | | uint32_t k; |
455 | | |
456 | | FLAC__ASSERT(parameter > 0); |
457 | | |
458 | | /* fold signed to uint32_t */ |
459 | | if(val < 0) |
460 | | uval = (uint32_t)(((-(++val)) << 1) + 1); |
461 | | else |
462 | | uval = (uint32_t)(val << 1); |
463 | | |
464 | | k = FLAC__bitmath_ilog2(parameter); |
465 | | if(parameter == 1u<<k) { |
466 | | FLAC__ASSERT(k <= 30); |
467 | | |
468 | | msbs = uval >> k; |
469 | | bits = 1 + k + msbs; |
470 | | } |
471 | | else { |
472 | | uint32_t q, r, d; |
473 | | |
474 | | d = (1 << (k+1)) - parameter; |
475 | | q = uval / parameter; |
476 | | r = uval - (q * parameter); |
477 | | |
478 | | bits = 1 + q + k; |
479 | | if(r >= d) |
480 | | bits++; |
481 | | } |
482 | | return bits; |
483 | | } |
484 | | |
485 | | uint32_t FLAC__bitwriter_golomb_bits_unsigned(uint32_t uval, uint32_t parameter) |
486 | | { |
487 | | uint32_t bits, msbs; |
488 | | uint32_t k; |
489 | | |
490 | | FLAC__ASSERT(parameter > 0); |
491 | | |
492 | | k = FLAC__bitmath_ilog2(parameter); |
493 | | if(parameter == 1u<<k) { |
494 | | FLAC__ASSERT(k <= 30); |
495 | | |
496 | | msbs = uval >> k; |
497 | | bits = 1 + k + msbs; |
498 | | } |
499 | | else { |
500 | | uint32_t q, r, d; |
501 | | |
502 | | d = (1 << (k+1)) - parameter; |
503 | | q = uval / parameter; |
504 | | r = uval - (q * parameter); |
505 | | |
506 | | bits = 1 + q + k; |
507 | | if(r >= d) |
508 | | bits++; |
509 | | } |
510 | | return bits; |
511 | | } |
512 | | |
513 | | FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t parameter) |
514 | | { |
515 | | uint32_t total_bits, interesting_bits, msbs; |
516 | | FLAC__uint32 uval, pattern; |
517 | | |
518 | | FLAC__ASSERT(0 != bw); |
519 | | FLAC__ASSERT(0 != bw->buffer); |
520 | | FLAC__ASSERT(parameter < 32); |
521 | | |
522 | | /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */ |
523 | | uval = val; |
524 | | uval <<= 1; |
525 | | uval ^= (val>>31); |
526 | | |
527 | | msbs = uval >> parameter; |
528 | | interesting_bits = 1 + parameter; |
529 | | total_bits = interesting_bits + msbs; |
530 | | pattern = 1 << parameter; /* the unary end bit */ |
531 | | pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */ |
532 | | |
533 | | if(total_bits <= 32) |
534 | | return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits); |
535 | | else |
536 | | return |
537 | | FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */ |
538 | | FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */ |
539 | | } |
540 | | #endif /* UNUSED */ |
541 | | |
542 | | #if (ENABLE_64_BIT_WORDS == 0) |
543 | | |
544 | | #define WIDE_ACCUM_TO_BW { \ |
545 | | bw->accum = wide_accum >> FLAC__HALF_TEMP_BITS; \ |
546 | | bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); \ |
547 | | wide_accum <<= FLAC__HALF_TEMP_BITS; \ |
548 | | bitpointer += FLAC__HALF_TEMP_BITS; \ |
549 | | } |
550 | | |
551 | | #else |
552 | | |
553 | 94.5M | #define WIDE_ACCUM_TO_BW { \ |
554 | 94.5M | FLAC__ASSERT(bw->bits % FLAC__HALF_TEMP_BITS == 0); \ |
555 | 94.5M | if(bw->bits == 0) { \ |
556 | 47.2M | bw->accum = wide_accum >> FLAC__HALF_TEMP_BITS; \ |
557 | 47.2M | wide_accum <<= FLAC__HALF_TEMP_BITS; \ |
558 | 47.2M | bw->bits = FLAC__HALF_TEMP_BITS; \ |
559 | 47.2M | } \ |
560 | 94.5M | else { \ |
561 | 47.2M | bw->accum <<= FLAC__HALF_TEMP_BITS; \ |
562 | 47.2M | bw->accum += wide_accum >> FLAC__HALF_TEMP_BITS; \ |
563 | 47.2M | bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); \ |
564 | 47.2M | wide_accum <<= FLAC__HALF_TEMP_BITS; \ |
565 | 47.2M | bw->bits = 0; \ |
566 | 47.2M | } \ |
567 | 94.5M | bitpointer += FLAC__HALF_TEMP_BITS; \ |
568 | 94.5M | } |
569 | | |
570 | | #endif |
571 | | |
572 | | FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, uint32_t nvals, uint32_t parameter) |
573 | 1.18M | { |
574 | 1.18M | const FLAC__uint32 mask1 = (FLAC__uint32)0xffffffff << parameter; /* we val|=mask1 to set the stop bit above it... */ |
575 | 1.18M | const FLAC__uint32 mask2 = (FLAC__uint32)0xffffffff >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2 */ |
576 | 1.18M | FLAC__uint32 uval; |
577 | 1.18M | const uint32_t lsbits = 1 + parameter; |
578 | 1.18M | uint32_t msbits, total_bits; |
579 | 1.18M | FLAC__bwtemp wide_accum = 0; |
580 | 1.18M | FLAC__uint32 bitpointer = FLAC__TEMP_BITS; |
581 | | |
582 | 1.18M | FLAC__ASSERT(0 != bw); |
583 | 1.18M | FLAC__ASSERT(0 != bw->buffer); |
584 | 1.18M | FLAC__ASSERT(parameter < 31); |
585 | | /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ |
586 | 1.18M | FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); |
587 | | #if (ENABLE_64_BIT_WORDS == 0) |
588 | | if(bw->bits > 0) { |
589 | | bitpointer -= bw->bits; |
590 | | wide_accum = (FLAC__bwtemp)(bw->accum) << bitpointer; |
591 | | bw->bits = 0; |
592 | | } |
593 | | #else |
594 | 1.18M | if(bw->bits > 0 && bw->bits < FLAC__HALF_TEMP_BITS) { |
595 | 452k | bitpointer -= bw->bits; |
596 | 452k | wide_accum = bw->accum << bitpointer; |
597 | 452k | bw->bits = 0; |
598 | 452k | } |
599 | 733k | else if(bw->bits > FLAC__HALF_TEMP_BITS) { |
600 | 477k | bitpointer -= (bw->bits - FLAC__HALF_TEMP_BITS); |
601 | 477k | wide_accum = bw->accum << bitpointer; |
602 | 477k | bw->accum >>= (bw->bits - FLAC__HALF_TEMP_BITS); |
603 | 477k | bw->bits = FLAC__HALF_TEMP_BITS; |
604 | 477k | } |
605 | 1.18M | #endif |
606 | | |
607 | | /* Reserve one FLAC__TEMP_BITS per symbol, so checks for space are only necessary when very large symbols are encountered |
608 | | * this might be considered wasteful, but is only at most 8kB more than necessary for a blocksize of 4096 */ |
609 | 1.18M | if(bw->capacity * FLAC__BITS_PER_WORD <= bw->words * FLAC__BITS_PER_WORD + nvals * FLAC__TEMP_BITS + bw->bits && !bitwriter_grow_(bw, nvals * FLAC__TEMP_BITS)) |
610 | 15 | return false; |
611 | | |
612 | 44.1M | while(nvals) { |
613 | | /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */ |
614 | 42.9M | uval = *vals; |
615 | 42.9M | uval <<= 1; |
616 | 42.9M | uval ^= (*vals>>31); |
617 | | |
618 | 42.9M | msbits = uval >> parameter; |
619 | 42.9M | total_bits = lsbits + msbits; |
620 | | |
621 | 42.9M | uval |= mask1; /* set stop bit */ |
622 | 42.9M | uval &= mask2; /* mask off unused top bits */ |
623 | | |
624 | | |
625 | 42.9M | if(total_bits <= bitpointer) { |
626 | | /* There is room enough to store the symbol whole at once */ |
627 | 42.8M | wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - total_bits); |
628 | 42.8M | bitpointer -= total_bits; |
629 | 42.8M | if(bitpointer <= FLAC__HALF_TEMP_BITS) { |
630 | | /* A word is finished, copy the upper 32 bits of the wide_accum */ |
631 | 10.7M | WIDE_ACCUM_TO_BW |
632 | 10.7M | } |
633 | 42.8M | } |
634 | 108k | else { |
635 | | /* The symbol needs to be split. This code isn't used often */ |
636 | | /* First check for space in the bitwriter */ |
637 | 108k | if(total_bits > FLAC__TEMP_BITS) { |
638 | 74.0k | FLAC__uint32 oversize_in_bits = total_bits - FLAC__TEMP_BITS; |
639 | 74.0k | FLAC__uint32 capacity_needed = bw->words * FLAC__BITS_PER_WORD + bw->bits + nvals * FLAC__TEMP_BITS + oversize_in_bits; |
640 | 74.0k | if(bw->capacity * FLAC__BITS_PER_WORD <= capacity_needed && !bitwriter_grow_(bw, nvals * FLAC__TEMP_BITS + oversize_in_bits)) |
641 | 15 | return false; |
642 | 74.0k | } |
643 | 108k | if(msbits > bitpointer) { |
644 | | /* We have a lot of 0 bits to write, first align with bitwriter word */ |
645 | 84.5k | msbits -= bitpointer - FLAC__HALF_TEMP_BITS; |
646 | 84.5k | bitpointer = FLAC__HALF_TEMP_BITS; |
647 | 84.5k | WIDE_ACCUM_TO_BW |
648 | 83.5M | while(msbits > bitpointer) { |
649 | | /* As the accumulator is already zero, we only need to |
650 | | * assign zeroes to the bitbuffer */ |
651 | 83.5M | WIDE_ACCUM_TO_BW |
652 | 83.5M | bitpointer -= FLAC__HALF_TEMP_BITS; |
653 | 83.5M | msbits -= FLAC__HALF_TEMP_BITS; |
654 | 83.5M | } |
655 | | /* The remaining bits are zero, and the accumulator already is zero, |
656 | | * so just subtract the number of bits from bitpointer. When storing, |
657 | | * we can also just store 0 */ |
658 | 84.5k | bitpointer -= msbits; |
659 | 84.5k | if(bitpointer <= FLAC__HALF_TEMP_BITS) |
660 | 84.5k | WIDE_ACCUM_TO_BW |
661 | 84.5k | } |
662 | 24.4k | else { |
663 | 24.4k | bitpointer -= msbits; |
664 | 24.4k | if(bitpointer <= FLAC__HALF_TEMP_BITS) |
665 | 25.1k | WIDE_ACCUM_TO_BW |
666 | 24.4k | } |
667 | | /* The lsbs + stop bit always fit 32 bit, so this code mirrors the code above */ |
668 | 108k | wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - lsbits); |
669 | 108k | bitpointer -= lsbits; |
670 | 108k | if(bitpointer <= FLAC__HALF_TEMP_BITS) { |
671 | | /* A word is finished, copy the upper 32 bits of the wide_accum */ |
672 | 44.1k | WIDE_ACCUM_TO_BW |
673 | 44.1k | } |
674 | 108k | } |
675 | 42.9M | vals++; |
676 | 42.9M | nvals--; |
677 | 42.9M | } |
678 | | /* Now fixup remainder of wide_accum */ |
679 | | #if (ENABLE_64_BIT_WORDS == 0) |
680 | | if(bitpointer < FLAC__TEMP_BITS) { |
681 | | bw->accum = wide_accum >> bitpointer; |
682 | | bw->bits = FLAC__TEMP_BITS - bitpointer; |
683 | | } |
684 | | #else |
685 | 1.18M | if(bitpointer < FLAC__TEMP_BITS) { |
686 | 1.13M | if(bw->bits == 0) { |
687 | 535k | bw->accum = wide_accum >> bitpointer; |
688 | 535k | bw->bits = FLAC__TEMP_BITS - bitpointer; |
689 | 535k | } |
690 | 604k | else if (bw->bits == FLAC__HALF_TEMP_BITS) { |
691 | 604k | bw->accum <<= FLAC__TEMP_BITS - bitpointer; |
692 | 604k | bw->accum |= (wide_accum >> bitpointer); |
693 | 604k | bw->bits = FLAC__HALF_TEMP_BITS + FLAC__TEMP_BITS - bitpointer; |
694 | 604k | } |
695 | 18.4E | else { |
696 | 18.4E | FLAC__ASSERT(0); |
697 | 18.4E | } |
698 | 1.13M | } |
699 | 1.18M | #endif |
700 | | |
701 | | |
702 | 1.18M | return true; |
703 | 1.18M | } |
704 | | |
705 | | #if 0 /* UNUSED */ |
706 | | FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, uint32_t parameter) |
707 | | { |
708 | | uint32_t total_bits, msbs, uval; |
709 | | uint32_t k; |
710 | | |
711 | | FLAC__ASSERT(0 != bw); |
712 | | FLAC__ASSERT(0 != bw->buffer); |
713 | | FLAC__ASSERT(parameter > 0); |
714 | | |
715 | | /* fold signed to uint32_t */ |
716 | | if(val < 0) |
717 | | uval = (uint32_t)(((-(++val)) << 1) + 1); |
718 | | else |
719 | | uval = (uint32_t)(val << 1); |
720 | | |
721 | | k = FLAC__bitmath_ilog2(parameter); |
722 | | if(parameter == 1u<<k) { |
723 | | uint32_t pattern; |
724 | | |
725 | | FLAC__ASSERT(k <= 30); |
726 | | |
727 | | msbs = uval >> k; |
728 | | total_bits = 1 + k + msbs; |
729 | | pattern = 1 << k; /* the unary end bit */ |
730 | | pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */ |
731 | | |
732 | | if(total_bits <= 32) { |
733 | | if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits)) |
734 | | return false; |
735 | | } |
736 | | else { |
737 | | /* write the unary MSBs */ |
738 | | if(!FLAC__bitwriter_write_zeroes(bw, msbs)) |
739 | | return false; |
740 | | /* write the unary end bit and binary LSBs */ |
741 | | if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1)) |
742 | | return false; |
743 | | } |
744 | | } |
745 | | else { |
746 | | uint32_t q, r, d; |
747 | | |
748 | | d = (1 << (k+1)) - parameter; |
749 | | q = uval / parameter; |
750 | | r = uval - (q * parameter); |
751 | | /* write the unary MSBs */ |
752 | | if(!FLAC__bitwriter_write_zeroes(bw, q)) |
753 | | return false; |
754 | | /* write the unary end bit */ |
755 | | if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1)) |
756 | | return false; |
757 | | /* write the binary LSBs */ |
758 | | if(r >= d) { |
759 | | if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1)) |
760 | | return false; |
761 | | } |
762 | | else { |
763 | | if(!FLAC__bitwriter_write_raw_uint32(bw, r, k)) |
764 | | return false; |
765 | | } |
766 | | } |
767 | | return true; |
768 | | } |
769 | | |
770 | | FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, uint32_t uval, uint32_t parameter) |
771 | | { |
772 | | uint32_t total_bits, msbs; |
773 | | uint32_t k; |
774 | | |
775 | | FLAC__ASSERT(0 != bw); |
776 | | FLAC__ASSERT(0 != bw->buffer); |
777 | | FLAC__ASSERT(parameter > 0); |
778 | | |
779 | | k = FLAC__bitmath_ilog2(parameter); |
780 | | if(parameter == 1u<<k) { |
781 | | uint32_t pattern; |
782 | | |
783 | | FLAC__ASSERT(k <= 30); |
784 | | |
785 | | msbs = uval >> k; |
786 | | total_bits = 1 + k + msbs; |
787 | | pattern = 1 << k; /* the unary end bit */ |
788 | | pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */ |
789 | | |
790 | | if(total_bits <= 32) { |
791 | | if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits)) |
792 | | return false; |
793 | | } |
794 | | else { |
795 | | /* write the unary MSBs */ |
796 | | if(!FLAC__bitwriter_write_zeroes(bw, msbs)) |
797 | | return false; |
798 | | /* write the unary end bit and binary LSBs */ |
799 | | if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1)) |
800 | | return false; |
801 | | } |
802 | | } |
803 | | else { |
804 | | uint32_t q, r, d; |
805 | | |
806 | | d = (1 << (k+1)) - parameter; |
807 | | q = uval / parameter; |
808 | | r = uval - (q * parameter); |
809 | | /* write the unary MSBs */ |
810 | | if(!FLAC__bitwriter_write_zeroes(bw, q)) |
811 | | return false; |
812 | | /* write the unary end bit */ |
813 | | if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1)) |
814 | | return false; |
815 | | /* write the binary LSBs */ |
816 | | if(r >= d) { |
817 | | if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1)) |
818 | | return false; |
819 | | } |
820 | | else { |
821 | | if(!FLAC__bitwriter_write_raw_uint32(bw, r, k)) |
822 | | return false; |
823 | | } |
824 | | } |
825 | | return true; |
826 | | } |
827 | | #endif /* UNUSED */ |
828 | | |
829 | | FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val) |
830 | 1.98M | { |
831 | 1.98M | FLAC__bool ok = 1; |
832 | | |
833 | 1.98M | FLAC__ASSERT(0 != bw); |
834 | 1.98M | FLAC__ASSERT(0 != bw->buffer); |
835 | | |
836 | 1.98M | if((val & 0x80000000) != 0) /* this version only handles 31 bits */ |
837 | 0 | return false; |
838 | | |
839 | 1.98M | if(val < 0x80) { |
840 | 101k | return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, 8); |
841 | 101k | } |
842 | 1.88M | else if(val < 0x800) { |
843 | 317k | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (val>>6), 8); |
844 | 317k | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8); |
845 | 317k | } |
846 | 1.56M | else if(val < 0x10000) { |
847 | 1.56M | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (val>>12), 8); |
848 | 1.56M | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8); |
849 | 1.56M | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8); |
850 | 1.56M | } |
851 | 18.4E | else if(val < 0x200000) { |
852 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (val>>18), 8); |
853 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8); |
854 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8); |
855 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8); |
856 | 0 | } |
857 | 18.4E | else if(val < 0x4000000) { |
858 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (val>>24), 8); |
859 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8); |
860 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8); |
861 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8); |
862 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8); |
863 | 0 | } |
864 | 18.4E | else { |
865 | 18.4E | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (val>>30), 8); |
866 | 18.4E | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>24)&0x3F), 8); |
867 | 18.4E | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8); |
868 | 18.4E | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8); |
869 | 18.4E | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8); |
870 | 18.4E | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8); |
871 | 18.4E | } |
872 | | |
873 | 1.88M | return ok; |
874 | 1.98M | } |
875 | | |
876 | | FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val) |
877 | 0 | { |
878 | 0 | FLAC__bool ok = 1; |
879 | |
|
880 | 0 | FLAC__ASSERT(0 != bw); |
881 | 0 | FLAC__ASSERT(0 != bw->buffer); |
882 | |
|
883 | 0 | if((val & FLAC__U64L(0xFFFFFFF000000000)) != 0) /* this version only handles 36 bits */ |
884 | 0 | return false; |
885 | | |
886 | 0 | if(val < 0x80) { |
887 | 0 | return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 8); |
888 | 0 | } |
889 | 0 | else if(val < 0x800) { |
890 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (FLAC__uint32)(val>>6), 8); |
891 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8); |
892 | 0 | } |
893 | 0 | else if(val < 0x10000) { |
894 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (FLAC__uint32)(val>>12), 8); |
895 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); |
896 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8); |
897 | 0 | } |
898 | 0 | else if(val < 0x200000) { |
899 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (FLAC__uint32)(val>>18), 8); |
900 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); |
901 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); |
902 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8); |
903 | 0 | } |
904 | 0 | else if(val < 0x4000000) { |
905 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (FLAC__uint32)(val>>24), 8); |
906 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8); |
907 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); |
908 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); |
909 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8); |
910 | 0 | } |
911 | 0 | else if(val < 0x80000000) { |
912 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (FLAC__uint32)(val>>30), 8); |
913 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8); |
914 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8); |
915 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); |
916 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); |
917 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8); |
918 | 0 | } |
919 | 0 | else { |
920 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFE, 8); |
921 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8); |
922 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8); |
923 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8); |
924 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8); |
925 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8); |
926 | 0 | ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8); |
927 | 0 | } |
928 | | |
929 | 0 | return ok; |
930 | 0 | } |
931 | | |
932 | | FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw) |
933 | 1.97M | { |
934 | | /* 0-pad to byte boundary */ |
935 | 1.97M | if(bw->bits & 7u) |
936 | 802k | return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u)); |
937 | 1.17M | else |
938 | 1.17M | return true; |
939 | 1.97M | } |
940 | | |
941 | | /* These functions are declared inline in this file but are also callable as |
942 | | * externs from elsewhere. |
943 | | * According to the C99 spec, section 6.7.4, simply providing a function |
944 | | * prototype in a header file without 'inline' and making the function inline |
945 | | * in this file should be sufficient. |
946 | | * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To |
947 | | * fix that we add extern declarations here. |
948 | | */ |
949 | | extern FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits); |
950 | | extern FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits); |
951 | | extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits); |
952 | | extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits); |
953 | | extern FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits); |
954 | | extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val); |
955 | | extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals); |