Coverage Report

Created: 2026-12-01 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flac/src/libFLAC/bitwriter.c
Line
Count
Source
1
/* libFLAC - Free Lossless Audio Codec library
2
 * Copyright (C) 2000-2009  Josh Coalson
3
 * Copyright (C) 2011-2025  Xiph.Org Foundation
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 * notice, this list of conditions and the following disclaimer.
11
 *
12
 * - Redistributions in binary form must reproduce the above copyright
13
 * notice, this list of conditions and the following disclaimer in the
14
 * documentation and/or other materials provided with the distribution.
15
 *
16
 * - Neither the name of the Xiph.org Foundation nor the names of its
17
 * contributors may be used to endorse or promote products derived from
18
 * this software without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
 */
32
33
#ifdef HAVE_CONFIG_H
34
#  include <config.h>
35
#endif
36
37
#include <stdlib.h>
38
#include <string.h>
39
#include "private/bitwriter.h"
40
#include "private/crc.h"
41
#include "private/format.h"
42
#include "private/macros.h"
43
#include "private/stream_encoder.h"
44
#include "FLAC/assert.h"
45
#include "share/alloc.h"
46
#include "share/compat.h"
47
#include "share/endswap.h"
48
49
/* Things should be fastest when this matches the machine word size */
50
/* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
51
/* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
52
53
#if (ENABLE_64_BIT_WORDS == 0)
54
55
typedef FLAC__uint32 bwword;
56
typedef FLAC__uint64 FLAC__bwtemp;
57
#define FLAC__BYTES_PER_WORD 4    /* sizeof bwword */
58
#define FLAC__BITS_PER_WORD 32
59
#define FLAC__TEMP_BITS 64
60
#define FLAC__HALF_TEMP_BITS 32
61
/* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
62
#if WORDS_BIGENDIAN
63
#define SWAP_BE_WORD_TO_HOST(x) (x)
64
#else
65
#define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
66
#endif
67
68
#else
69
70
typedef FLAC__uint64 bwword;
71
typedef FLAC__uint64 FLAC__bwtemp;
72
801k
#define FLAC__BYTES_PER_WORD 8    /* sizeof bwword */
73
18.8M
#define FLAC__BITS_PER_WORD 64
74
7.92M
#define FLAC__TEMP_BITS 64
75
83.5M
#define FLAC__HALF_TEMP_BITS 32
76
/* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
77
#if WORDS_BIGENDIAN
78
#define SWAP_BE_WORD_TO_HOST(x) (x)
79
#else
80
11.0M
#define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_64(x)
81
#endif
82
83
#endif
84
85
/*
86
 * The default capacity here doesn't matter too much.  The buffer always grows
87
 * to hold whatever is written to it.  Usually the encoder will stop adding at
88
 * a frame or metadata block, then write that out and clear the buffer for the
89
 * next one.
90
 */
91
static const uint32_t FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
92
/* When growing, increment with 1/4th at a time */
93
static const uint32_t FLAC__BITWRITER_DEFAULT_GROW_FRACTION = 2; /* means grow by >> 2 (1/4th) of current size */
94
95
38.6k
#define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
96
38.6k
#define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
97
98
struct FLAC__BitWriter {
99
  bwword *buffer;
100
  bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
101
  uint32_t capacity; /* capacity of buffer in words */
102
  uint32_t words; /* # of complete words in buffer */
103
  uint32_t bits; /* # of used bits in accum */
104
};
105
106
/* * WATCHOUT: The current implementation only grows the buffer. */
107
#ifndef __SUNPRO_C
108
static
109
#endif
110
FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add)
111
33.4k
{
112
33.4k
  uint32_t new_capacity;
113
33.4k
  bwword *new_buffer;
114
115
  /* Refuse to allocate more words than this amount, based on largest possible metadata chunk size */
116
33.4k
  const uint32_t max_capacity = (((1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1) * 8 + FLAC__STREAM_METADATA_LENGTH_LEN + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD;
117
118
33.4k
  FLAC__ASSERT(0 != bw);
119
33.4k
  FLAC__ASSERT(0 != bw->buffer);
120
121
  /* calculate total words needed to store 'bits_to_add' additional bits */
122
33.4k
  new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
123
124
  /* it's possible (due to pessimism in the growth estimation that
125
   * leads to this call) that we don't actually need to grow
126
   */
127
33.4k
  if(bw->capacity >= new_capacity)
128
31.7k
    return true;
129
130
1.72k
  if(new_capacity > max_capacity)
131
    /* Requested new capacity is larger than the largest possible metadata block,
132
     * which is also larger than the largest sane framesize. That means something
133
     * went very wrong somewhere and previous checks failed.
134
     * To prevent chrashing, give up */
135
0
    return false;
136
137
  /* As reallocation can be quite expensive, grow exponentially */
138
1.72k
  if((new_capacity - bw->capacity) < (bw->capacity >> FLAC__BITWRITER_DEFAULT_GROW_FRACTION))
139
1.05k
    new_capacity = bw->capacity + (bw->capacity >> FLAC__BITWRITER_DEFAULT_GROW_FRACTION);
140
141
  /* make sure we got everything right */
142
1.72k
  FLAC__ASSERT(new_capacity > bw->capacity);
143
1.72k
  FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
144
145
1.72k
  new_buffer = safe_realloc_nofree_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
146
1.72k
  if(new_buffer == 0)
147
0
    return false;
148
1.72k
  bw->buffer = new_buffer;
149
1.72k
  bw->capacity = new_capacity;
150
1.72k
  return true;
151
1.72k
}
152
153
154
/***********************************************************************
155
 *
156
 * Class constructor/destructor
157
 *
158
 ***********************************************************************/
159
160
FLAC__BitWriter *FLAC__bitwriter_new(void)
161
25.4k
{
162
25.4k
  FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
163
  /* note that calloc() sets all members to 0 for us */
164
25.4k
  return bw;
165
25.4k
}
166
167
void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
168
25.4k
{
169
25.4k
  FLAC__ASSERT(0 != bw);
170
171
25.4k
  FLAC__bitwriter_free(bw);
172
25.4k
  free(bw);
173
25.4k
}
174
175
/***********************************************************************
176
 *
177
 * Public class methods
178
 *
179
 ***********************************************************************/
180
181
FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
182
23.0k
{
183
23.0k
  FLAC__ASSERT(0 != bw);
184
185
23.0k
  bw->words = bw->bits = 0;
186
23.0k
  bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
187
23.0k
  bw->buffer = malloc(sizeof(bwword) * bw->capacity);
188
23.0k
  if(bw->buffer == 0)
189
0
    return false;
190
191
23.0k
  return true;
192
23.0k
}
193
194
void FLAC__bitwriter_free(FLAC__BitWriter *bw)
195
25.4k
{
196
25.4k
  FLAC__ASSERT(0 != bw);
197
198
25.4k
  if(0 != bw->buffer)
199
23.0k
    free(bw->buffer);
200
25.4k
  bw->buffer = 0;
201
25.4k
  bw->capacity = 0;
202
25.4k
  bw->words = bw->bits = 0;
203
25.4k
}
204
205
void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
206
287k
{
207
287k
  bw->words = bw->bits = 0;
208
287k
}
209
210
FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
211
257k
{
212
257k
  const FLAC__byte *buffer;
213
257k
  size_t bytes;
214
215
257k
  FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
216
217
257k
  if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
218
0
    return false;
219
220
257k
  *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
221
257k
  FLAC__bitwriter_release_buffer(bw);
222
257k
  return true;
223
257k
}
224
225
FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
226
257k
{
227
257k
  const FLAC__byte *buffer;
228
257k
  size_t bytes;
229
230
257k
  FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
231
232
257k
  if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
233
0
    return false;
234
235
257k
  *crc = FLAC__crc8(buffer, bytes);
236
257k
  FLAC__bitwriter_release_buffer(bw);
237
257k
  return true;
238
257k
}
239
240
FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
241
841k
{
242
841k
  return ((bw->bits & 7) == 0);
243
841k
}
244
245
uint32_t FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
246
38.6k
{
247
38.6k
  return FLAC__TOTAL_BITS(bw);
248
38.6k
}
249
250
FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
251
801k
{
252
801k
  FLAC__ASSERT((bw->bits & 7) == 0);
253
  /* double protection */
254
801k
  if(bw->bits & 7)
255
0
    return false;
256
  /* if we have bits in the accumulator we have to flush those to the buffer first */
257
801k
  if(bw->bits) {
258
669k
    FLAC__ASSERT(bw->words <= bw->capacity);
259
669k
    if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
260
0
      return false;
261
    /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
262
669k
    bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
263
669k
  }
264
  /* now we can just return what we have */
265
801k
  *buffer = (FLAC__byte*)bw->buffer;
266
801k
  *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
267
801k
  return true;
268
801k
}
269
270
void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
271
800k
{
272
  /* nothing to do.  in the future, strict checking of a 'writer-is-in-
273
   * get-mode' flag could be added everywhere and then cleared here
274
   */
275
800k
  (void)bw;
276
800k
}
277
278
FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits)
279
140k
{
280
140k
  uint32_t n;
281
282
140k
  FLAC__ASSERT(0 != bw);
283
140k
  FLAC__ASSERT(0 != bw->buffer);
284
285
140k
  if(bits == 0)
286
0
    return true;
287
  /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
288
140k
  if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
289
0
    return false;
290
  /* first part gets to word alignment */
291
140k
  if(bw->bits) {
292
140k
    n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
293
140k
    bw->accum <<= n;
294
140k
    bits -= n;
295
140k
    bw->bits += n;
296
140k
    if(bw->bits == FLAC__BITS_PER_WORD) {
297
9.11k
      bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
298
9.11k
      bw->bits = 0;
299
9.11k
    }
300
131k
    else
301
131k
      return true;
302
140k
  }
303
  /* do whole words */
304
9.10k
  while(bits >= FLAC__BITS_PER_WORD) {
305
0
    bw->buffer[bw->words++] = 0;
306
0
    bits -= FLAC__BITS_PER_WORD;
307
0
  }
308
  /* do any leftovers */
309
9.10k
  if(bits > 0) {
310
0
    bw->accum = 0;
311
0
    bw->bits = bits;
312
0
  }
313
9.10k
  return true;
314
140k
}
315
316
static inline FLAC__bool FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
317
14.9M
{
318
14.9M
  register uint32_t left;
319
320
  /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
321
14.9M
  FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
322
323
14.9M
  if(bw == 0 || bw->buffer == 0)
324
0
    return false;
325
326
14.9M
  if (bits > 32)
327
0
    return false;
328
329
14.9M
  if(bits == 0)
330
0
    return true;
331
332
14.9M
  FLAC__ASSERT((bits == 32) || (val>>bits == 0));
333
334
  /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
335
14.9M
  if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
336
0
    return false;
337
338
14.9M
  left = FLAC__BITS_PER_WORD - bw->bits;
339
14.9M
  if(bits < left) {
340
11.4M
    bw->accum <<= bits;
341
11.4M
    bw->accum |= val;
342
11.4M
    bw->bits += bits;
343
11.4M
  }
344
3.45M
  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 */
345
3.45M
    bw->accum <<= left;
346
3.45M
    bw->accum |= val >> (bw->bits = bits - left);
347
3.45M
    bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
348
3.45M
    bw->accum = val; /* unused top bits can contain garbage */
349
3.45M
  }
350
18.4E
  else { /* at this point bits == FLAC__BITS_PER_WORD == 32  and  bw->bits == 0 */
351
18.4E
    bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST((bwword)val);
352
18.4E
  }
353
354
14.9M
  return true;
355
14.9M
}
356
357
FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
358
7.74M
{
359
  /* check that unused bits are unset */
360
7.74M
  if((bits < 32) && (val>>bits != 0))
361
0
    return false;
362
363
7.74M
  return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, bits);
364
7.74M
}
365
366
FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits)
367
4.31M
{
368
  /* zero-out unused bits */
369
4.31M
  if(bits < 32)
370
852k
    val &= (~(0xffffffff << bits));
371
372
4.31M
  return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, bits);
373
4.31M
}
374
375
FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits)
376
2.09M
{
377
  /* this could be a little faster but it's not used for much */
378
2.09M
  if(bits > 32) {
379
1.79M
    return
380
1.79M
      FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
381
1.79M
      FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 32);
382
1.79M
  }
383
296k
  else
384
296k
    return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
385
2.09M
}
386
387
FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits)
388
2.08M
{
389
2.08M
  FLAC__uint64 uval = val;
390
  /* zero-out unused bits */
391
2.08M
  if(bits < 64)
392
2.08M
    uval &= (~(UINT64_MAX << bits));
393
2.08M
  return FLAC__bitwriter_write_raw_uint64(bw, uval, bits);
394
2.08M
}
395
396
FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
397
19.3k
{
398
  /* this doesn't need to be that fast as currently it is only used for vorbis comments */
399
400
19.3k
  if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val & 0xff, 8))
401
0
    return false;
402
19.3k
  if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>8) & 0xff, 8))
403
0
    return false;
404
19.3k
  if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>16) & 0xff, 8))
405
0
    return false;
406
19.3k
  if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val>>24, 8))
407
0
    return false;
408
409
19.3k
  return true;
410
19.3k
}
411
412
FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals)
413
19.3k
{
414
19.3k
  uint32_t i;
415
416
  /* grow capacity upfront to prevent constant reallocation during writes */
417
19.3k
  if(bw->capacity <= bw->words + nvals / (FLAC__BITS_PER_WORD / 8) + 1 && !bitwriter_grow_(bw, nvals * 8))
418
0
    return false;
419
420
  /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
421
551k
  for(i = 0; i < nvals; i++) {
422
531k
    if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)(vals[i]), 8))
423
0
      return false;
424
531k
  }
425
426
19.3k
  return true;
427
19.3k
}
428
429
FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, uint32_t val)
430
23.9k
{
431
23.9k
  if(val < 32)
432
23.9k
    return FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, ++val);
433
0
  else
434
0
    return
435
0
      FLAC__bitwriter_write_zeroes(bw, val) &&
436
0
      FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, 1);
437
23.9k
}
438
439
#if 0 /* UNUSED */
440
uint32_t FLAC__bitwriter_rice_bits(FLAC__int32 val, uint32_t parameter)
441
{
442
  FLAC__uint32 uval;
443
444
  FLAC__ASSERT(parameter < 32);
445
446
  /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
447
  uval = val;
448
  uval <<= 1;
449
  uval ^= (val>>31);
450
451
  return 1 + parameter + (uval >> parameter);
452
}
453
454
uint32_t FLAC__bitwriter_golomb_bits_signed(int val, uint32_t parameter)
455
{
456
  uint32_t bits, msbs, uval;
457
  uint32_t k;
458
459
  FLAC__ASSERT(parameter > 0);
460
461
  /* fold signed to uint32_t */
462
  if(val < 0)
463
    uval = (uint32_t)(((-(++val)) << 1) + 1);
464
  else
465
    uval = (uint32_t)(val << 1);
466
467
  k = FLAC__bitmath_ilog2(parameter);
468
  if(parameter == 1u<<k) {
469
    FLAC__ASSERT(k <= 30);
470
471
    msbs = uval >> k;
472
    bits = 1 + k + msbs;
473
  }
474
  else {
475
    uint32_t q, r, d;
476
477
    d = (1 << (k+1)) - parameter;
478
    q = uval / parameter;
479
    r = uval - (q * parameter);
480
481
    bits = 1 + q + k;
482
    if(r >= d)
483
      bits++;
484
  }
485
  return bits;
486
}
487
488
uint32_t FLAC__bitwriter_golomb_bits_unsigned(uint32_t uval, uint32_t parameter)
489
{
490
  uint32_t bits, msbs;
491
  uint32_t k;
492
493
  FLAC__ASSERT(parameter > 0);
494
495
  k = FLAC__bitmath_ilog2(parameter);
496
  if(parameter == 1u<<k) {
497
    FLAC__ASSERT(k <= 30);
498
499
    msbs = uval >> k;
500
    bits = 1 + k + msbs;
501
  }
502
  else {
503
    uint32_t q, r, d;
504
505
    d = (1 << (k+1)) - parameter;
506
    q = uval / parameter;
507
    r = uval - (q * parameter);
508
509
    bits = 1 + q + k;
510
    if(r >= d)
511
      bits++;
512
  }
513
  return bits;
514
}
515
516
FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t parameter)
517
{
518
  uint32_t 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 < 32);
524
525
  /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
526
  uval = val;
527
  uval <<= 1;
528
  uval ^= (val>>31);
529
530
  msbs = uval >> parameter;
531
  interesting_bits = 1 + parameter;
532
  total_bits = interesting_bits + msbs;
533
  pattern = 1 << parameter; /* the unary end bit */
534
  pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
535
536
  if(total_bits <= 32)
537
    return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
538
  else
539
    return
540
      FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
541
      FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
542
}
543
#endif /* UNUSED */
544
545
#if (ENABLE_64_BIT_WORDS == 0)
546
547
#define WIDE_ACCUM_TO_BW {  \
548
  bw->accum = wide_accum >> FLAC__HALF_TEMP_BITS;  \
549
  bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); \
550
  wide_accum <<= FLAC__HALF_TEMP_BITS;  \
551
  bitpointer += FLAC__HALF_TEMP_BITS;  \
552
}
553
554
#else
555
556
13.8M
#define WIDE_ACCUM_TO_BW {  \
557
13.8M
  FLAC__ASSERT(bw->bits % FLAC__HALF_TEMP_BITS == 0);  \
558
13.8M
  if(bw->bits == 0) {  \
559
6.87M
    bw->accum = wide_accum >> FLAC__HALF_TEMP_BITS;  \
560
6.87M
    wide_accum <<= FLAC__HALF_TEMP_BITS;  \
561
6.87M
    bw->bits = FLAC__HALF_TEMP_BITS;  \
562
6.87M
  }  \
563
13.8M
  else {  \
564
6.92M
    bw->accum <<= FLAC__HALF_TEMP_BITS;  \
565
6.92M
    bw->accum += wide_accum >> FLAC__HALF_TEMP_BITS;  \
566
6.92M
    bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);  \
567
6.92M
    wide_accum <<= FLAC__HALF_TEMP_BITS;  \
568
6.92M
    bw->bits = 0;  \
569
6.92M
  }  \
570
13.8M
  bitpointer += FLAC__HALF_TEMP_BITS;  \
571
13.8M
}
572
573
#endif
574
575
FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, uint32_t nvals, uint32_t parameter)
576
1.74M
{
577
1.74M
  const FLAC__uint32 mask1 = (FLAC__uint32)0xffffffff << parameter; /* we val|=mask1 to set the stop bit above it... */
578
1.74M
  const FLAC__uint32 mask2 = (FLAC__uint32)0xffffffff >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2 */
579
1.74M
  FLAC__uint32 uval;
580
1.74M
  const uint32_t lsbits = 1 + parameter;
581
1.74M
  uint32_t msbits, total_bits;
582
1.74M
  FLAC__bwtemp wide_accum = 0;
583
1.74M
  FLAC__uint32 bitpointer = FLAC__TEMP_BITS;
584
585
1.74M
  FLAC__ASSERT(0 != bw);
586
1.74M
  FLAC__ASSERT(0 != bw->buffer);
587
1.74M
  FLAC__ASSERT(parameter < 31);
588
  /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
589
1.74M
  FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
590
#if (ENABLE_64_BIT_WORDS == 0)
591
  if(bw->bits > 0) {
592
    bitpointer -= bw->bits;
593
    wide_accum = (FLAC__bwtemp)(bw->accum) << bitpointer;
594
    bw->bits = 0;
595
  }
596
#else
597
1.74M
  if(bw->bits > 0 && bw->bits < FLAC__HALF_TEMP_BITS) {
598
862k
    bitpointer -= bw->bits;
599
862k
    wide_accum = bw->accum << bitpointer;
600
862k
    bw->bits = 0;
601
862k
  }
602
887k
  else if(bw->bits > FLAC__HALF_TEMP_BITS) {
603
845k
    bitpointer -= (bw->bits - FLAC__HALF_TEMP_BITS);
604
845k
    wide_accum = bw->accum << bitpointer;
605
845k
    bw->accum >>= (bw->bits - FLAC__HALF_TEMP_BITS);
606
845k
    bw->bits = FLAC__HALF_TEMP_BITS;
607
845k
  }
608
1.74M
#endif
609
610
  /* Reserve one FLAC__TEMP_BITS per symbol, so checks for space are only necessary when very large symbols are encountered
611
   * this might be considered wasteful, but is only at most 8kB more than necessary for a blocksize of 4096 */
612
1.74M
  if(bw->capacity * FLAC__BITS_PER_WORD <= bw->words * FLAC__BITS_PER_WORD + nvals * FLAC__TEMP_BITS + bw->bits && !bitwriter_grow_(bw, nvals * FLAC__TEMP_BITS))
613
0
    return false;
614
615
22.7M
  while(nvals) {
616
    /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
617
20.9M
    uval = *vals;
618
20.9M
    uval <<= 1;
619
20.9M
    uval ^= (*vals>>31);
620
621
20.9M
    msbits = uval >> parameter;
622
20.9M
    total_bits = lsbits + msbits;
623
624
20.9M
    uval |= mask1; /* set stop bit */
625
20.9M
    uval &= mask2; /* mask off unused top bits */
626
627
628
20.9M
    if(total_bits <= bitpointer) {
629
      /* There is room enough to store the symbol whole at once */
630
20.8M
      wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - total_bits);
631
20.8M
      bitpointer -= total_bits;
632
20.8M
      if(bitpointer <= FLAC__HALF_TEMP_BITS) {
633
        /* A word is finished, copy the upper 32 bits of the wide_accum */
634
13.3M
        WIDE_ACCUM_TO_BW
635
13.3M
      }
636
20.8M
    }
637
102k
    else {
638
      /* The symbol needs to be split. This code isn't used often */
639
      /* First check for space in the bitwriter */
640
102k
      if(total_bits > FLAC__TEMP_BITS) {
641
28.0k
        FLAC__uint32 oversize_in_bits = total_bits - FLAC__TEMP_BITS;
642
28.0k
        FLAC__uint32 capacity_needed = bw->words * FLAC__BITS_PER_WORD + bw->bits + nvals * FLAC__TEMP_BITS + oversize_in_bits;
643
28.0k
        if(bw->capacity * FLAC__BITS_PER_WORD <= capacity_needed && !bitwriter_grow_(bw, nvals * FLAC__TEMP_BITS + oversize_in_bits))
644
0
          return false;
645
28.0k
      }
646
102k
      if(msbits > bitpointer) {
647
        /* We have a lot of 0 bits to write, first align with bitwriter word */
648
26.2k
        msbits -= bitpointer - FLAC__HALF_TEMP_BITS;
649
26.2k
        bitpointer = FLAC__HALF_TEMP_BITS;
650
26.2k
        WIDE_ACCUM_TO_BW
651
244k
        while(msbits > bitpointer) {
652
          /* As the accumulator is already zero, we only need to
653
           * assign zeroes to the bitbuffer */
654
218k
          WIDE_ACCUM_TO_BW
655
218k
          bitpointer -= FLAC__HALF_TEMP_BITS;
656
218k
          msbits -= FLAC__HALF_TEMP_BITS;
657
218k
        }
658
        /* The remaining bits are zero, and the accumulator already is zero,
659
         * so just subtract the number of bits from bitpointer. When storing,
660
         * we can also just store 0 */
661
26.2k
        bitpointer -= msbits;
662
26.2k
        if(bitpointer <= FLAC__HALF_TEMP_BITS)
663
26.2k
          WIDE_ACCUM_TO_BW
664
26.2k
      }
665
76.2k
      else {
666
76.2k
        bitpointer -= msbits;
667
76.2k
        if(bitpointer <= FLAC__HALF_TEMP_BITS)
668
76.3k
          WIDE_ACCUM_TO_BW
669
76.2k
      }
670
      /* The lsbs + stop bit always fit 32 bit, so this code mirrors the code above */
671
102k
                        wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - lsbits);
672
102k
                        bitpointer -= lsbits;
673
102k
                        if(bitpointer <= FLAC__HALF_TEMP_BITS) {
674
                                /* A word is finished, copy the upper 32 bits of the wide_accum */
675
91.7k
                                WIDE_ACCUM_TO_BW
676
91.7k
                        }
677
102k
    }
678
20.9M
    vals++;
679
20.9M
    nvals--;
680
20.9M
  }
681
  /* Now fixup remainder of wide_accum */
682
#if (ENABLE_64_BIT_WORDS == 0)
683
  if(bitpointer < FLAC__TEMP_BITS) {
684
    bw->accum = wide_accum >> bitpointer;
685
    bw->bits = FLAC__TEMP_BITS - bitpointer;
686
  }
687
#else
688
1.74M
  if(bitpointer < FLAC__TEMP_BITS) {
689
1.71M
    if(bw->bits == 0) {
690
920k
      bw->accum = wide_accum >> bitpointer;
691
920k
      bw->bits = FLAC__TEMP_BITS - bitpointer;
692
920k
    }
693
796k
    else if (bw->bits == FLAC__HALF_TEMP_BITS) {
694
796k
      bw->accum <<= FLAC__TEMP_BITS - bitpointer;
695
796k
      bw->accum |= (wide_accum >> bitpointer);
696
796k
      bw->bits = FLAC__HALF_TEMP_BITS + FLAC__TEMP_BITS - bitpointer;
697
796k
    }
698
18.4E
    else {
699
18.4E
      FLAC__ASSERT(0);
700
18.4E
    }
701
1.71M
  }
702
1.74M
#endif
703
704
705
1.74M
  return true;
706
1.74M
}
707
708
#if 0 /* UNUSED */
709
FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, uint32_t parameter)
710
{
711
  uint32_t total_bits, msbs, uval;
712
  uint32_t k;
713
714
  FLAC__ASSERT(0 != bw);
715
  FLAC__ASSERT(0 != bw->buffer);
716
  FLAC__ASSERT(parameter > 0);
717
718
  /* fold signed to uint32_t */
719
  if(val < 0)
720
    uval = (uint32_t)(((-(++val)) << 1) + 1);
721
  else
722
    uval = (uint32_t)(val << 1);
723
724
  k = FLAC__bitmath_ilog2(parameter);
725
  if(parameter == 1u<<k) {
726
    uint32_t pattern;
727
728
    FLAC__ASSERT(k <= 30);
729
730
    msbs = uval >> k;
731
    total_bits = 1 + k + msbs;
732
    pattern = 1 << k; /* the unary end bit */
733
    pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
734
735
    if(total_bits <= 32) {
736
      if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
737
        return false;
738
    }
739
    else {
740
      /* write the unary MSBs */
741
      if(!FLAC__bitwriter_write_zeroes(bw, msbs))
742
        return false;
743
      /* write the unary end bit and binary LSBs */
744
      if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
745
        return false;
746
    }
747
  }
748
  else {
749
    uint32_t q, r, d;
750
751
    d = (1 << (k+1)) - parameter;
752
    q = uval / parameter;
753
    r = uval - (q * parameter);
754
    /* write the unary MSBs */
755
    if(!FLAC__bitwriter_write_zeroes(bw, q))
756
      return false;
757
    /* write the unary end bit */
758
    if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
759
      return false;
760
    /* write the binary LSBs */
761
    if(r >= d) {
762
      if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
763
        return false;
764
    }
765
    else {
766
      if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
767
        return false;
768
    }
769
  }
770
  return true;
771
}
772
773
FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, uint32_t uval, uint32_t parameter)
774
{
775
  uint32_t total_bits, msbs;
776
  uint32_t k;
777
778
  FLAC__ASSERT(0 != bw);
779
  FLAC__ASSERT(0 != bw->buffer);
780
  FLAC__ASSERT(parameter > 0);
781
782
  k = FLAC__bitmath_ilog2(parameter);
783
  if(parameter == 1u<<k) {
784
    uint32_t pattern;
785
786
    FLAC__ASSERT(k <= 30);
787
788
    msbs = uval >> k;
789
    total_bits = 1 + k + msbs;
790
    pattern = 1 << k; /* the unary end bit */
791
    pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
792
793
    if(total_bits <= 32) {
794
      if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
795
        return false;
796
    }
797
    else {
798
      /* write the unary MSBs */
799
      if(!FLAC__bitwriter_write_zeroes(bw, msbs))
800
        return false;
801
      /* write the unary end bit and binary LSBs */
802
      if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
803
        return false;
804
    }
805
  }
806
  else {
807
    uint32_t q, r, d;
808
809
    d = (1 << (k+1)) - parameter;
810
    q = uval / parameter;
811
    r = uval - (q * parameter);
812
    /* write the unary MSBs */
813
    if(!FLAC__bitwriter_write_zeroes(bw, q))
814
      return false;
815
    /* write the unary end bit */
816
    if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
817
      return false;
818
    /* write the binary LSBs */
819
    if(r >= d) {
820
      if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
821
        return false;
822
    }
823
    else {
824
      if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
825
        return false;
826
    }
827
  }
828
  return true;
829
}
830
#endif /* UNUSED */
831
832
FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
833
257k
{
834
257k
  FLAC__bool ok = 1;
835
836
257k
  FLAC__ASSERT(0 != bw);
837
257k
  FLAC__ASSERT(0 != bw->buffer);
838
839
257k
  if((val & 0x80000000) != 0) /* this version only handles 31 bits */
840
0
    return false;
841
842
257k
  if(val < 0x80) {
843
84.1k
    return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, 8);
844
84.1k
  }
845
173k
  else if(val < 0x800) {
846
140k
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (val>>6), 8);
847
140k
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
848
140k
  }
849
33.3k
  else if(val < 0x10000) {
850
33.2k
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (val>>12), 8);
851
33.2k
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
852
33.2k
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
853
33.2k
  }
854
55
  else if(val < 0x200000) {
855
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (val>>18), 8);
856
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
857
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
858
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
859
0
  }
860
55
  else if(val < 0x4000000) {
861
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (val>>24), 8);
862
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
863
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
864
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
865
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
866
0
  }
867
55
  else {
868
55
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (val>>30), 8);
869
55
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>24)&0x3F), 8);
870
55
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
871
55
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
872
55
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
873
55
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
874
55
  }
875
876
173k
  return ok;
877
257k
}
878
879
FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
880
0
{
881
0
  FLAC__bool ok = 1;
882
883
0
  FLAC__ASSERT(0 != bw);
884
0
  FLAC__ASSERT(0 != bw->buffer);
885
886
0
  if((val & FLAC__U64L(0xFFFFFFF000000000)) != 0) /* this version only handles 36 bits */
887
0
    return false;
888
889
0
  if(val < 0x80) {
890
0
    return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 8);
891
0
  }
892
0
  else if(val < 0x800) {
893
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
894
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
895
0
  }
896
0
  else if(val < 0x10000) {
897
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
898
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
899
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
900
0
  }
901
0
  else if(val < 0x200000) {
902
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
903
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
904
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
905
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
906
0
  }
907
0
  else if(val < 0x4000000) {
908
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
909
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
910
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
911
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
912
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
913
0
  }
914
0
  else if(val < 0x80000000) {
915
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
916
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
917
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
918
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
919
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
920
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
921
0
  }
922
0
  else {
923
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFE, 8);
924
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
925
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
926
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
927
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
928
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
929
0
    ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
930
0
  }
931
932
0
  return ok;
933
0
}
934
935
FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
936
257k
{
937
  /* 0-pad to byte boundary */
938
257k
  if(bw->bits & 7u)
939
140k
    return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
940
117k
  else
941
117k
    return true;
942
257k
}