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