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