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