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