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