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