1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2022 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 #define FLAC__BYTES_PER_WORD 8 /* sizeof bwword */
73 #define FLAC__BITS_PER_WORD 64
74 #define FLAC__TEMP_BITS 64
75 #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 #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 4K at a time */
93 static const uint32_t FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
94
95 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
96 #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
bitwriter_grow_(FLAC__BitWriter * bw,uint32_t bits_to_add)110 FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add)
111 {
112 uint32_t new_capacity;
113 bwword *new_buffer;
114
115 FLAC__ASSERT(0 != bw);
116 FLAC__ASSERT(0 != bw->buffer);
117
118 /* calculate total words needed to store 'bits_to_add' additional bits */
119 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 if(bw->capacity >= new_capacity)
125 return true;
126
127 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 return false;
133
134 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
135 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
136 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
137 /* make sure we got everything right */
138 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
139 FLAC__ASSERT(new_capacity > bw->capacity);
140 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
141
142 new_buffer = safe_realloc_nofree_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
143 if(new_buffer == 0)
144 return false;
145 bw->buffer = new_buffer;
146 bw->capacity = new_capacity;
147 return true;
148 }
149
150
151 /***********************************************************************
152 *
153 * Class constructor/destructor
154 *
155 ***********************************************************************/
156
FLAC__bitwriter_new(void)157 FLAC__BitWriter *FLAC__bitwriter_new(void)
158 {
159 FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
160 /* note that calloc() sets all members to 0 for us */
161 return bw;
162 }
163
FLAC__bitwriter_delete(FLAC__BitWriter * bw)164 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
165 {
166 FLAC__ASSERT(0 != bw);
167
168 FLAC__bitwriter_free(bw);
169 free(bw);
170 }
171
172 /***********************************************************************
173 *
174 * Public class methods
175 *
176 ***********************************************************************/
177
FLAC__bitwriter_init(FLAC__BitWriter * bw)178 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
179 {
180 FLAC__ASSERT(0 != bw);
181
182 bw->words = bw->bits = 0;
183 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
184 bw->buffer = malloc(sizeof(bwword) * bw->capacity);
185 if(bw->buffer == 0)
186 return false;
187
188 return true;
189 }
190
FLAC__bitwriter_free(FLAC__BitWriter * bw)191 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
192 {
193 FLAC__ASSERT(0 != bw);
194
195 if(0 != bw->buffer)
196 free(bw->buffer);
197 bw->buffer = 0;
198 bw->capacity = 0;
199 bw->words = bw->bits = 0;
200 }
201
FLAC__bitwriter_clear(FLAC__BitWriter * bw)202 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
203 {
204 bw->words = bw->bits = 0;
205 }
206
FLAC__bitwriter_get_write_crc16(FLAC__BitWriter * bw,FLAC__uint16 * crc)207 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
208 {
209 const FLAC__byte *buffer;
210 size_t bytes;
211
212 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
213
214 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
215 return false;
216
217 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
218 FLAC__bitwriter_release_buffer(bw);
219 return true;
220 }
221
FLAC__bitwriter_get_write_crc8(FLAC__BitWriter * bw,FLAC__byte * crc)222 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
223 {
224 const FLAC__byte *buffer;
225 size_t bytes;
226
227 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
228
229 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
230 return false;
231
232 *crc = FLAC__crc8(buffer, bytes);
233 FLAC__bitwriter_release_buffer(bw);
234 return true;
235 }
236
FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter * bw)237 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
238 {
239 return ((bw->bits & 7) == 0);
240 }
241
FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter * bw)242 uint32_t FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
243 {
244 return FLAC__TOTAL_BITS(bw);
245 }
246
FLAC__bitwriter_get_buffer(FLAC__BitWriter * bw,const FLAC__byte ** buffer,size_t * bytes)247 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
248 {
249 FLAC__ASSERT((bw->bits & 7) == 0);
250 /* double protection */
251 if(bw->bits & 7)
252 return false;
253 /* if we have bits in the accumulator we have to flush those to the buffer first */
254 if(bw->bits) {
255 FLAC__ASSERT(bw->words <= bw->capacity);
256 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
257 return false;
258 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
259 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
260 }
261 /* now we can just return what we have */
262 *buffer = (FLAC__byte*)bw->buffer;
263 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
264 return true;
265 }
266
FLAC__bitwriter_release_buffer(FLAC__BitWriter * bw)267 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
268 {
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 (void)bw;
273 }
274
FLAC__bitwriter_write_zeroes(FLAC__BitWriter * bw,uint32_t bits)275 inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits)
276 {
277 uint32_t n;
278
279 FLAC__ASSERT(0 != bw);
280 FLAC__ASSERT(0 != bw->buffer);
281
282 if(bits == 0)
283 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 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
286 return false;
287 /* first part gets to word alignment */
288 if(bw->bits) {
289 n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
290 bw->accum <<= n;
291 bits -= n;
292 bw->bits += n;
293 if(bw->bits == FLAC__BITS_PER_WORD) {
294 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
295 bw->bits = 0;
296 }
297 else
298 return true;
299 }
300 /* do whole words */
301 while(bits >= FLAC__BITS_PER_WORD) {
302 bw->buffer[bw->words++] = 0;
303 bits -= FLAC__BITS_PER_WORD;
304 }
305 /* do any leftovers */
306 if(bits > 0) {
307 bw->accum = 0;
308 bw->bits = bits;
309 }
310 return true;
311 }
312
FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter * bw,FLAC__uint32 val,uint32_t bits)313 static inline FLAC__bool FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
314 {
315 register uint32_t left;
316
317 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
318 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
319
320 if(bw == 0 || bw->buffer == 0)
321 return false;
322
323 if (bits > 32)
324 return false;
325
326 if(bits == 0)
327 return true;
328
329 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 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
333 return false;
334
335 left = FLAC__BITS_PER_WORD - bw->bits;
336 if(bits < left) {
337 bw->accum <<= bits;
338 bw->accum |= val;
339 bw->bits += bits;
340 }
341 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 bw->accum <<= left;
343 bw->accum |= val >> (bw->bits = bits - left);
344 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
345 bw->accum = val; /* unused top bits can contain garbage */
346 }
347 else { /* at this point bits == FLAC__BITS_PER_WORD == 32 and bw->bits == 0 */
348 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST((bwword)val);
349 }
350
351 return true;
352 }
353
FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter * bw,FLAC__uint32 val,uint32_t bits)354 inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
355 {
356 /* check that unused bits are unset */
357 if((bits < 32) && (val>>bits != 0))
358 return false;
359
360 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, bits);
361 }
362
FLAC__bitwriter_write_raw_int32(FLAC__BitWriter * bw,FLAC__int32 val,uint32_t bits)363 inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits)
364 {
365 /* zero-out unused bits */
366 if(bits < 32)
367 val &= (~(0xffffffff << bits));
368
369 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, bits);
370 }
371
FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter * bw,FLAC__uint64 val,uint32_t bits)372 inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits)
373 {
374 /* this could be a little faster but it's not used for much */
375 if(bits > 32) {
376 return
377 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
378 FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 32);
379 }
380 else
381 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
382 }
383
FLAC__bitwriter_write_raw_int64(FLAC__BitWriter * bw,FLAC__int64 val,uint32_t bits)384 inline FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits)
385 {
386 FLAC__uint64 uval = val;
387 /* zero-out unused bits */
388 if(bits < 64)
389 uval &= (~(UINT64_MAX << bits));
390 return FLAC__bitwriter_write_raw_uint64(bw, uval, bits);
391 }
392
FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter * bw,FLAC__uint32 val)393 inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
394 {
395 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
396
397 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val & 0xff, 8))
398 return false;
399 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>8) & 0xff, 8))
400 return false;
401 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>16) & 0xff, 8))
402 return false;
403 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val>>24, 8))
404 return false;
405
406 return true;
407 }
408
FLAC__bitwriter_write_byte_block(FLAC__BitWriter * bw,const FLAC__byte vals[],uint32_t nvals)409 inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals)
410 {
411 uint32_t i;
412
413 /* grow capacity upfront to prevent constant reallocation during writes */
414 if(bw->capacity <= bw->words + nvals / (FLAC__BITS_PER_WORD / 8) + 1 && !bitwriter_grow_(bw, nvals * 8))
415 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 for(i = 0; i < nvals; i++) {
419 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)(vals[i]), 8))
420 return false;
421 }
422
423 return true;
424 }
425
FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter * bw,uint32_t val)426 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, uint32_t val)
427 {
428 if(val < 32)
429 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, ++val);
430 else
431 return
432 FLAC__bitwriter_write_zeroes(bw, val) &&
433 FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, 1);
434 }
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 #define WIDE_ACCUM_TO_BW { \
554 FLAC__ASSERT(bw->bits % FLAC__HALF_TEMP_BITS == 0); \
555 if(bw->bits == 0) { \
556 bw->accum = wide_accum >> FLAC__HALF_TEMP_BITS; \
557 wide_accum <<= FLAC__HALF_TEMP_BITS; \
558 bw->bits = FLAC__HALF_TEMP_BITS; \
559 } \
560 else { \
561 bw->accum <<= FLAC__HALF_TEMP_BITS; \
562 bw->accum += wide_accum >> FLAC__HALF_TEMP_BITS; \
563 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); \
564 wide_accum <<= FLAC__HALF_TEMP_BITS; \
565 bw->bits = 0; \
566 } \
567 bitpointer += FLAC__HALF_TEMP_BITS; \
568 }
569
570 #endif
571
FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter * bw,const FLAC__int32 * vals,uint32_t nvals,uint32_t parameter)572 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, uint32_t nvals, uint32_t parameter)
573 {
574 const FLAC__uint32 mask1 = (FLAC__uint32)0xffffffff << parameter; /* we val|=mask1 to set the stop bit above it... */
575 const FLAC__uint32 mask2 = (FLAC__uint32)0xffffffff >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2 */
576 FLAC__uint32 uval;
577 const uint32_t lsbits = 1 + parameter;
578 uint32_t msbits, total_bits;
579 FLAC__bwtemp wide_accum = 0;
580 FLAC__uint32 bitpointer = FLAC__TEMP_BITS;
581
582 FLAC__ASSERT(0 != bw);
583 FLAC__ASSERT(0 != bw->buffer);
584 FLAC__ASSERT(parameter < 31);
585 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
586 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 if(bw->bits > 0 && bw->bits < FLAC__HALF_TEMP_BITS) {
595 bitpointer -= bw->bits;
596 wide_accum = bw->accum << bitpointer;
597 bw->bits = 0;
598 }
599 else if(bw->bits > FLAC__HALF_TEMP_BITS) {
600 bitpointer -= (bw->bits - FLAC__HALF_TEMP_BITS);
601 wide_accum = bw->accum << bitpointer;
602 bw->accum >>= (bw->bits - FLAC__HALF_TEMP_BITS);
603 bw->bits = FLAC__HALF_TEMP_BITS;
604 }
605 #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 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 return false;
611
612 while(nvals) {
613 /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
614 uval = *vals;
615 uval <<= 1;
616 uval ^= (*vals>>31);
617
618 msbits = uval >> parameter;
619 total_bits = lsbits + msbits;
620
621 uval |= mask1; /* set stop bit */
622 uval &= mask2; /* mask off unused top bits */
623
624
625 if(total_bits <= bitpointer) {
626 /* There is room enough to store the symbol whole at once */
627 wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - total_bits);
628 bitpointer -= total_bits;
629 if(bitpointer <= FLAC__HALF_TEMP_BITS) {
630 /* A word is finished, copy the upper 32 bits of the wide_accum */
631 WIDE_ACCUM_TO_BW
632 }
633 }
634 else {
635 /* The symbol needs to be split. This code isn't used often */
636 /* First check for space in the bitwriter */
637 if(total_bits > FLAC__TEMP_BITS) {
638 FLAC__uint32 oversize_in_bits = total_bits - FLAC__TEMP_BITS;
639 FLAC__uint32 capacity_needed = bw->words * FLAC__BITS_PER_WORD + bw->bits + nvals * FLAC__TEMP_BITS + oversize_in_bits;
640 if(bw->capacity * FLAC__BITS_PER_WORD <= capacity_needed && !bitwriter_grow_(bw, nvals * FLAC__TEMP_BITS + oversize_in_bits))
641 return false;
642 }
643 if(msbits > bitpointer) {
644 /* We have a lot of 0 bits to write, first align with bitwriter word */
645 msbits -= bitpointer - FLAC__HALF_TEMP_BITS;
646 bitpointer = FLAC__HALF_TEMP_BITS;
647 WIDE_ACCUM_TO_BW
648 while(msbits > bitpointer) {
649 /* As the accumulator is already zero, we only need to
650 * assign zeroes to the bitbuffer */
651 WIDE_ACCUM_TO_BW
652 bitpointer -= FLAC__HALF_TEMP_BITS;
653 msbits -= FLAC__HALF_TEMP_BITS;
654 }
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 bitpointer -= msbits;
659 if(bitpointer <= FLAC__HALF_TEMP_BITS)
660 WIDE_ACCUM_TO_BW
661 }
662 else {
663 bitpointer -= msbits;
664 if(bitpointer <= FLAC__HALF_TEMP_BITS)
665 WIDE_ACCUM_TO_BW
666 }
667 /* The lsbs + stop bit always fit 32 bit, so this code mirrors the code above */
668 wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - lsbits);
669 bitpointer -= lsbits;
670 if(bitpointer <= FLAC__HALF_TEMP_BITS) {
671 /* A word is finished, copy the upper 32 bits of the wide_accum */
672 WIDE_ACCUM_TO_BW
673 }
674 }
675 vals++;
676 nvals--;
677 }
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 if(bitpointer < FLAC__TEMP_BITS) {
686 if(bw->bits == 0) {
687 bw->accum = wide_accum >> bitpointer;
688 bw->bits = FLAC__TEMP_BITS - bitpointer;
689 }
690 else if (bw->bits == FLAC__HALF_TEMP_BITS) {
691 bw->accum <<= FLAC__TEMP_BITS - bitpointer;
692 bw->accum |= (wide_accum >> bitpointer);
693 bw->bits = FLAC__HALF_TEMP_BITS + FLAC__TEMP_BITS - bitpointer;
694 }
695 else {
696 FLAC__ASSERT(0);
697 }
698 }
699 #endif
700
701
702 return true;
703 }
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
FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter * bw,FLAC__uint32 val)829 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
830 {
831 FLAC__bool ok = 1;
832
833 FLAC__ASSERT(0 != bw);
834 FLAC__ASSERT(0 != bw->buffer);
835
836 if((val & 0x80000000) != 0) /* this version only handles 31 bits */
837 return false;
838
839 if(val < 0x80) {
840 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, 8);
841 }
842 else if(val < 0x800) {
843 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (val>>6), 8);
844 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
845 }
846 else if(val < 0x10000) {
847 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (val>>12), 8);
848 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
849 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
850 }
851 else if(val < 0x200000) {
852 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (val>>18), 8);
853 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
854 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
855 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
856 }
857 else if(val < 0x4000000) {
858 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (val>>24), 8);
859 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
860 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
861 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
862 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
863 }
864 else {
865 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (val>>30), 8);
866 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>24)&0x3F), 8);
867 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
868 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
869 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
870 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
871 }
872
873 return ok;
874 }
875
FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter * bw,FLAC__uint64 val)876 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
877 {
878 FLAC__bool ok = 1;
879
880 FLAC__ASSERT(0 != bw);
881 FLAC__ASSERT(0 != bw->buffer);
882
883 if((val & FLAC__U64L(0xFFFFFFF000000000)) != 0) /* this version only handles 36 bits */
884 return false;
885
886 if(val < 0x80) {
887 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 8);
888 }
889 else if(val < 0x800) {
890 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
891 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
892 }
893 else if(val < 0x10000) {
894 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
895 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
896 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
897 }
898 else if(val < 0x200000) {
899 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
900 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
901 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
902 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
903 }
904 else if(val < 0x4000000) {
905 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
906 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
907 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
908 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
909 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
910 }
911 else if(val < 0x80000000) {
912 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
913 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
914 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
915 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
916 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
917 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
918 }
919 else {
920 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFE, 8);
921 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
922 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
923 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
924 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
925 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
926 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
927 }
928
929 return ok;
930 }
931
FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter * bw)932 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
933 {
934 /* 0-pad to byte boundary */
935 if(bw->bits & 7u)
936 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
937 else
938 return true;
939 }
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);
956