7da4b1593dc84e9effd9b9fef5e47abf8177ab22
[flac.git] / src / libFLAC / bitwriter.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #if HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include "private/bitwriter.h"
39 #include "private/crc.h"
40 #include "FLAC/assert.h"
41 #include "share/alloc.h"
42 #include "share/endswap.h"
43
44 /* Things should be fastest when this matches the machine word size */
45 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
46 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
47 #define FLAC__BYTES_PER_WORD 4
48 #define FLAC__BITS_PER_WORD 32
49 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
50 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
51 #if WORDS_BIGENDIAN
52 #define SWAP_BE_WORD_TO_HOST(x) (x)
53 #else
54 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
55 #endif
56
57 /*
58  * The default capacity here doesn't matter too much.  The buffer always grows
59  * to hold whatever is written to it.  Usually the encoder will stop adding at
60  * a frame or metadata block, then write that out and clear the buffer for the
61  * next one.
62  */
63 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(uint32_t); /* size in words */
64 /* When growing, increment 4K at a time */
65 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(uint32_t); /* size in words */
66
67 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
68 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
69
70 #ifdef min
71 #undef min
72 #endif
73 #define min(x,y) ((x)<(y)?(x):(y))
74
75 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
76 #ifdef _MSC_VER
77 #define FLAC__U64L(x) x
78 #else
79 #define FLAC__U64L(x) x##LLU
80 #endif
81
82 #ifndef FLaC__INLINE
83 #define FLaC__INLINE
84 #endif
85
86 struct FLAC__BitWriter {
87         uint32_t *buffer;
88         uint32_t accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
89         unsigned capacity; /* capacity of buffer in words */
90         unsigned words; /* # of complete words in buffer */
91         unsigned bits; /* # of used bits in accum */
92 };
93
94 /* * WATCHOUT: The current implementation only grows the buffer. */
95 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
96 {
97         unsigned new_capacity;
98         uint32_t *new_buffer;
99
100         FLAC__ASSERT(0 != bw);
101         FLAC__ASSERT(0 != bw->buffer);
102
103         /* calculate total words needed to store 'bits_to_add' additional bits */
104         new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
105
106         /* it's possible (due to pessimism in the growth estimation that
107          * leads to this call) that we don't actually need to grow
108          */
109         if(bw->capacity >= new_capacity)
110                 return true;
111
112         /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
113         if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
114                 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
115         /* make sure we got everything right */
116         FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
117         FLAC__ASSERT(new_capacity > bw->capacity);
118         FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
119
120         new_buffer = safe_realloc_mul_2op_(bw->buffer, sizeof(uint32_t), /*times*/new_capacity);
121         if(new_buffer == 0)
122                 return false;
123         bw->buffer = new_buffer;
124         bw->capacity = new_capacity;
125         return true;
126 }
127
128
129 /***********************************************************************
130  *
131  * Class constructor/destructor
132  *
133  ***********************************************************************/
134
135 FLAC__BitWriter *FLAC__bitwriter_new(void)
136 {
137         FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
138         /* note that calloc() sets all members to 0 for us */
139         return bw;
140 }
141
142 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
143 {
144         FLAC__ASSERT(0 != bw);
145
146         FLAC__bitwriter_free(bw);
147         free(bw);
148 }
149
150 /***********************************************************************
151  *
152  * Public class methods
153  *
154  ***********************************************************************/
155
156 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
157 {
158         FLAC__ASSERT(0 != bw);
159
160         bw->words = bw->bits = 0;
161         bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
162         bw->buffer = malloc(sizeof(uint32_t) * bw->capacity);
163         if(bw->buffer == 0)
164                 return false;
165
166         return true;
167 }
168
169 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
170 {
171         FLAC__ASSERT(0 != bw);
172
173         if(0 != bw->buffer)
174                 free(bw->buffer);
175         bw->buffer = 0;
176         bw->capacity = 0;
177         bw->words = bw->bits = 0;
178 }
179
180 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
181 {
182         bw->words = bw->bits = 0;
183 }
184
185 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
186 {
187         unsigned i, j;
188         if(bw == 0) {
189                 fprintf(out, "bitwriter is NULL\n");
190         }
191         else {
192                 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
193
194                 for(i = 0; i < bw->words; i++) {
195                         fprintf(out, "%08X: ", i);
196                         for(j = 0; j < FLAC__BITS_PER_WORD; j++)
197                                 fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
198                         fprintf(out, "\n");
199                 }
200                 if(bw->bits > 0) {
201                         fprintf(out, "%08X: ", i);
202                         for(j = 0; j < bw->bits; j++)
203                                 fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
204                         fprintf(out, "\n");
205                 }
206         }
207 }
208
209 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
210 {
211         const FLAC__byte *buffer;
212         size_t bytes;
213
214         FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
215
216         if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
217                 return false;
218
219         *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
220         FLAC__bitwriter_release_buffer(bw);
221         return true;
222 }
223
224 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
225 {
226         const FLAC__byte *buffer;
227         size_t bytes;
228
229         FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
230
231         if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
232                 return false;
233
234         *crc = FLAC__crc8(buffer, bytes);
235         FLAC__bitwriter_release_buffer(bw);
236         return true;
237 }
238
239 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
240 {
241         return ((bw->bits & 7) == 0);
242 }
243
244 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
245 {
246         return FLAC__TOTAL_BITS(bw);
247 }
248
249 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
250 {
251         FLAC__ASSERT((bw->bits & 7) == 0);
252         /* double protection */
253         if(bw->bits & 7)
254                 return false;
255         /* if we have bits in the accumulator we have to flush those to the buffer first */
256         if(bw->bits) {
257                 FLAC__ASSERT(bw->words <= bw->capacity);
258                 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
259                         return false;
260                 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
261                 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
262         }
263         /* now we can just return what we have */
264         *buffer = (FLAC__byte*)bw->buffer;
265         *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
266         return true;
267 }
268
269 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
270 {
271         /* nothing to do.  in the future, strict checking of a 'writer-is-in-
272          * get-mode' flag could be added everywhere and then cleared here
273          */
274         (void)bw;
275 }
276
277 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
278 {
279         unsigned n;
280
281         FLAC__ASSERT(0 != bw);
282         FLAC__ASSERT(0 != bw->buffer);
283
284         if(bits == 0)
285                 return true;
286         /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
287         if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
288                 return false;
289         /* first part gets to word alignment */
290         if(bw->bits) {
291                 n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
292                 bw->accum <<= n;
293                 bits -= n;
294                 bw->bits += n;
295                 if(bw->bits == FLAC__BITS_PER_WORD) {
296                         bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
297                         bw->bits = 0;
298                 }
299                 else
300                         return true;
301         }
302         /* do whole words */
303         while(bits >= FLAC__BITS_PER_WORD) {
304                 bw->buffer[bw->words++] = 0;
305                 bits -= FLAC__BITS_PER_WORD;
306         }
307         /* do any leftovers */
308         if(bits > 0) {
309                 bw->accum = 0;
310                 bw->bits = bits;
311         }
312         return true;
313 }
314
315 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
316 {
317         register unsigned left;
318
319         /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
320         FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
321
322         FLAC__ASSERT(0 != bw);
323         FLAC__ASSERT(0 != bw->buffer);
324
325         FLAC__ASSERT(bits <= 32);
326         if(bits == 0)
327                 return true;
328
329         /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
330         if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
331                 return false;
332
333         left = FLAC__BITS_PER_WORD - bw->bits;
334         if(bits < left) {
335                 bw->accum <<= bits;
336                 bw->accum |= val;
337                 bw->bits += bits;
338         }
339         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 */
340                 bw->accum <<= left;
341                 bw->accum |= val >> (bw->bits = bits - left);
342                 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
343                 bw->accum = val;
344         }
345         else {
346                 bw->accum = val;
347                 bw->bits = 0;
348                 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
349         }
350
351         return true;
352 }
353
354 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
355 {
356         /* zero-out unused bits */
357         if(bits < 32)
358                 val &= (~(0xffffffff << bits));
359
360         return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
361 }
362
363 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
364 {
365         /* this could be a little faster but it's not used for much */
366         if(bits > 32) {
367                 return
368                         FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
369                         FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
370         }
371         else
372                 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
373 }
374
375 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
376 {
377         /* this doesn't need to be that fast as currently it is only used for vorbis comments */
378
379         if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
380                 return false;
381         if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
382                 return false;
383         if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
384                 return false;
385         if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
386                 return false;
387
388         return true;
389 }
390
391 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
392 {
393         unsigned i;
394
395         /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
396         for(i = 0; i < nvals; i++) {
397                 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
398                         return false;
399         }
400
401         return true;
402 }
403
404 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
405 {
406         if(val < 32)
407                 return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
408         else
409                 return
410                         FLAC__bitwriter_write_zeroes(bw, val) &&
411                         FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
412 }
413
414 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
415 {
416         FLAC__uint32 uval;
417
418         FLAC__ASSERT(parameter < sizeof(unsigned)*8);
419
420         /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
421         uval = (val<<1) ^ (val>>31);
422
423         return 1 + parameter + (uval >> parameter);
424 }
425
426 #if 0 /* UNUSED */
427 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
428 {
429         unsigned bits, msbs, uval;
430         unsigned k;
431
432         FLAC__ASSERT(parameter > 0);
433
434         /* fold signed to unsigned */
435         if(val < 0)
436                 uval = (unsigned)(((-(++val)) << 1) + 1);
437         else
438                 uval = (unsigned)(val << 1);
439
440         k = FLAC__bitmath_ilog2(parameter);
441         if(parameter == 1u<<k) {
442                 FLAC__ASSERT(k <= 30);
443
444                 msbs = uval >> k;
445                 bits = 1 + k + msbs;
446         }
447         else {
448                 unsigned q, r, d;
449
450                 d = (1 << (k+1)) - parameter;
451                 q = uval / parameter;
452                 r = uval - (q * parameter);
453
454                 bits = 1 + q + k;
455                 if(r >= d)
456                         bits++;
457         }
458         return bits;
459 }
460
461 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
462 {
463         unsigned bits, msbs;
464         unsigned k;
465
466         FLAC__ASSERT(parameter > 0);
467
468         k = FLAC__bitmath_ilog2(parameter);
469         if(parameter == 1u<<k) {
470                 FLAC__ASSERT(k <= 30);
471
472                 msbs = uval >> k;
473                 bits = 1 + k + msbs;
474         }
475         else {
476                 unsigned q, r, d;
477
478                 d = (1 << (k+1)) - parameter;
479                 q = uval / parameter;
480                 r = uval - (q * parameter);
481
482                 bits = 1 + q + k;
483                 if(r >= d)
484                         bits++;
485         }
486         return bits;
487 }
488 #endif /* UNUSED */
489
490 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
491 {
492         unsigned total_bits, interesting_bits, msbs;
493         FLAC__uint32 uval, pattern;
494
495         FLAC__ASSERT(0 != bw);
496         FLAC__ASSERT(0 != bw->buffer);
497         FLAC__ASSERT(parameter < 8*sizeof(uval));
498
499         /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
500         uval = (val<<1) ^ (val>>31);
501
502         msbs = uval >> parameter;
503         interesting_bits = 1 + parameter;
504         total_bits = interesting_bits + msbs;
505         pattern = 1 << parameter; /* the unary end bit */
506         pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
507
508         if(total_bits <= 32)
509                 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
510         else
511                 return
512                         FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
513                         FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
514 }
515
516 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
517 {
518         const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
519         const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
520         FLAC__uint32 uval;
521         unsigned left;
522         const unsigned lsbits = 1 + parameter;
523         unsigned msbits;
524
525         FLAC__ASSERT(0 != bw);
526         FLAC__ASSERT(0 != bw->buffer);
527         FLAC__ASSERT(parameter < 8*sizeof(uint32_t)-1);
528         /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
529         FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
530
531         while(nvals) {
532                 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
533                 uval = (*vals<<1) ^ (*vals>>31);
534
535                 msbits = uval >> parameter;
536
537 #if 0 /* OPT: can remove this special case if it doesn't make up for the extra compare (doesn't make a statistically significant difference with msvc or gcc/x86) */
538                 if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
539                         /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free uint32_t to work in */
540                         bw->bits = bw->bits + msbits + lsbits;
541                         uval |= mask1; /* set stop bit */
542                         uval &= mask2; /* mask off unused top bits */
543                         /* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
544                         bw->accum <<= msbits;
545                         bw->accum <<= lsbits;
546                         bw->accum |= uval;
547                         if(bw->bits == FLAC__BITS_PER_WORD) {
548                                 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
549                                 bw->bits = 0;
550                                 /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
551                                 if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
552                                         FLAC__ASSERT(bw->capacity == bw->words);
553                                         return false;
554                                 }
555                         }
556                 }
557                 else {
558 #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
559                 if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
560                         /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free uint32_t to work in */
561                         bw->bits = bw->bits + msbits + lsbits;
562                         uval |= mask1; /* set stop bit */
563                         uval &= mask2; /* mask off unused top bits */
564                         bw->accum <<= msbits + lsbits;
565                         bw->accum |= uval;
566                 }
567                 else {
568 #endif
569                         /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
570                         /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
571                         if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 uint32_t*/ && !bitwriter_grow_(bw, msbits+lsbits))
572                                 return false;
573
574                         if(msbits) {
575                                 /* first part gets to word alignment */
576                                 if(bw->bits) {
577                                         left = FLAC__BITS_PER_WORD - bw->bits;
578                                         if(msbits < left) {
579                                                 bw->accum <<= msbits;
580                                                 bw->bits += msbits;
581                                                 goto break1;
582                                         }
583                                         else {
584                                                 bw->accum <<= left;
585                                                 msbits -= left;
586                                                 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
587                                                 bw->bits = 0;
588                                         }
589                                 }
590                                 /* do whole words */
591                                 while(msbits >= FLAC__BITS_PER_WORD) {
592                                         bw->buffer[bw->words++] = 0;
593                                         msbits -= FLAC__BITS_PER_WORD;
594                                 }
595                                 /* do any leftovers */
596                                 if(msbits > 0) {
597                                         bw->accum = 0;
598                                         bw->bits = msbits;
599                                 }
600                         }
601 break1:
602                         uval |= mask1; /* set stop bit */
603                         uval &= mask2; /* mask off unused top bits */
604
605                         left = FLAC__BITS_PER_WORD - bw->bits;
606                         if(lsbits < left) {
607                                 bw->accum <<= lsbits;
608                                 bw->accum |= uval;
609                                 bw->bits += lsbits;
610                         }
611                         else {
612                                 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
613                                  * be > lsbits (because of previous assertions) so it would have
614                                  * triggered the (lsbits<left) case above.
615                                  */
616                                 FLAC__ASSERT(bw->bits);
617                                 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
618                                 bw->accum <<= left;
619                                 bw->accum |= uval >> (bw->bits = lsbits - left);
620                                 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
621                                 bw->accum = uval;
622                         }
623 #if 1
624                 }
625 #endif
626                 vals++;
627                 nvals--;
628         }
629         return true;
630 }
631
632 #if 0 /* UNUSED */
633 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
634 {
635         unsigned total_bits, msbs, uval;
636         unsigned k;
637
638         FLAC__ASSERT(0 != bw);
639         FLAC__ASSERT(0 != bw->buffer);
640         FLAC__ASSERT(parameter > 0);
641
642         /* fold signed to unsigned */
643         if(val < 0)
644                 uval = (unsigned)(((-(++val)) << 1) + 1);
645         else
646                 uval = (unsigned)(val << 1);
647
648         k = FLAC__bitmath_ilog2(parameter);
649         if(parameter == 1u<<k) {
650                 unsigned pattern;
651
652                 FLAC__ASSERT(k <= 30);
653
654                 msbs = uval >> k;
655                 total_bits = 1 + k + msbs;
656                 pattern = 1 << k; /* the unary end bit */
657                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
658
659                 if(total_bits <= 32) {
660                         if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
661                                 return false;
662                 }
663                 else {
664                         /* write the unary MSBs */
665                         if(!FLAC__bitwriter_write_zeroes(bw, msbs))
666                                 return false;
667                         /* write the unary end bit and binary LSBs */
668                         if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
669                                 return false;
670                 }
671         }
672         else {
673                 unsigned q, r, d;
674
675                 d = (1 << (k+1)) - parameter;
676                 q = uval / parameter;
677                 r = uval - (q * parameter);
678                 /* write the unary MSBs */
679                 if(!FLAC__bitwriter_write_zeroes(bw, q))
680                         return false;
681                 /* write the unary end bit */
682                 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
683                         return false;
684                 /* write the binary LSBs */
685                 if(r >= d) {
686                         if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
687                                 return false;
688                 }
689                 else {
690                         if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
691                                 return false;
692                 }
693         }
694         return true;
695 }
696
697 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
698 {
699         unsigned total_bits, msbs;
700         unsigned k;
701
702         FLAC__ASSERT(0 != bw);
703         FLAC__ASSERT(0 != bw->buffer);
704         FLAC__ASSERT(parameter > 0);
705
706         k = FLAC__bitmath_ilog2(parameter);
707         if(parameter == 1u<<k) {
708                 unsigned pattern;
709
710                 FLAC__ASSERT(k <= 30);
711
712                 msbs = uval >> k;
713                 total_bits = 1 + k + msbs;
714                 pattern = 1 << k; /* the unary end bit */
715                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
716
717                 if(total_bits <= 32) {
718                         if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
719                                 return false;
720                 }
721                 else {
722                         /* write the unary MSBs */
723                         if(!FLAC__bitwriter_write_zeroes(bw, msbs))
724                                 return false;
725                         /* write the unary end bit and binary LSBs */
726                         if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
727                                 return false;
728                 }
729         }
730         else {
731                 unsigned q, r, d;
732
733                 d = (1 << (k+1)) - parameter;
734                 q = uval / parameter;
735                 r = uval - (q * parameter);
736                 /* write the unary MSBs */
737                 if(!FLAC__bitwriter_write_zeroes(bw, q))
738                         return false;
739                 /* write the unary end bit */
740                 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
741                         return false;
742                 /* write the binary LSBs */
743                 if(r >= d) {
744                         if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
745                                 return false;
746                 }
747                 else {
748                         if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
749                                 return false;
750                 }
751         }
752         return true;
753 }
754 #endif /* UNUSED */
755
756 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
757 {
758         FLAC__bool ok = 1;
759
760         FLAC__ASSERT(0 != bw);
761         FLAC__ASSERT(0 != bw->buffer);
762
763         FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
764
765         if(val < 0x80) {
766                 return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
767         }
768         else if(val < 0x800) {
769                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
770                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
771         }
772         else if(val < 0x10000) {
773                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
774                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
775                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
776         }
777         else if(val < 0x200000) {
778                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
779                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
780                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
781                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
782         }
783         else if(val < 0x4000000) {
784                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
785                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
786                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
787                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
788                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
789         }
790         else {
791                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
792                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
793                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
794                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
795                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
796                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
797         }
798
799         return ok;
800 }
801
802 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
803 {
804         FLAC__bool ok = 1;
805
806         FLAC__ASSERT(0 != bw);
807         FLAC__ASSERT(0 != bw->buffer);
808
809         FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
810
811         if(val < 0x80) {
812                 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
813         }
814         else if(val < 0x800) {
815                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
816                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
817         }
818         else if(val < 0x10000) {
819                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
820                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
821                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
822         }
823         else if(val < 0x200000) {
824                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
825                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
826                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
827                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
828         }
829         else if(val < 0x4000000) {
830                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
831                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
832                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
833                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
834                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
835         }
836         else if(val < 0x80000000) {
837                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
838                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
839                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
840                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
841                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
842                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
843         }
844         else {
845                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
846                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
847                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
848                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
849                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
850                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
851                 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
852         }
853
854         return ok;
855 }
856
857 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
858 {
859         /* 0-pad to byte boundary */
860         if(bw->bits & 7u)
861                 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
862         else
863                 return true;
864 }