Coverage Report

Created: 2025-12-31 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/speex/libspeex/bits.c
Line
Count
Source
1
/* Copyright (C) 2002 Jean-Marc Valin
2
   File: speex_bits.c
3
4
   Handles bit packing/unpacking
5
6
   Redistribution and use in source and binary forms, with or without
7
   modification, are permitted provided that the following conditions
8
   are met:
9
10
   - Redistributions of source code must retain the above copyright
11
   notice, this list of conditions and the following disclaimer.
12
13
   - Redistributions in binary form must reproduce the above copyright
14
   notice, this list of conditions and the following disclaimer in the
15
   documentation and/or other materials provided with the distribution.
16
17
   - Neither the name of the Xiph.org Foundation nor the names of its
18
   contributors may be used to endorse or promote products derived from
19
   this software without specific prior written permission.
20
21
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
25
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
*/
34
35
#ifdef HAVE_CONFIG_H
36
#include "config.h"
37
#endif
38
39
#include "speex/speex_bits.h"
40
#include "arch.h"
41
#include "os_support.h"
42
43
/* Maximum size of the bit-stream (for fixed-size allocation) */
44
#ifndef MAX_CHARS_PER_FRAME
45
19.6k
#define MAX_CHARS_PER_FRAME (2000/BYTES_PER_CHAR)
46
#endif
47
48
EXPORT void speex_bits_init(SpeexBits *bits)
49
9.82k
{
50
9.82k
   bits->chars = (char*)speex_alloc(MAX_CHARS_PER_FRAME);
51
9.82k
   if (!bits->chars)
52
0
      return;
53
54
9.82k
   bits->buf_size = MAX_CHARS_PER_FRAME;
55
56
9.82k
   bits->owner=1;
57
58
9.82k
   speex_bits_reset(bits);
59
9.82k
}
60
61
EXPORT void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size)
62
0
{
63
0
   bits->chars = (char*)buff;
64
0
   bits->buf_size = buf_size;
65
66
0
   bits->owner=0;
67
68
0
   speex_bits_reset(bits);
69
0
}
70
71
EXPORT void speex_bits_set_bit_buffer(SpeexBits *bits, void *buff, int buf_size)
72
0
{
73
0
   bits->chars = (char*)buff;
74
0
   bits->buf_size = buf_size;
75
76
0
   bits->owner=0;
77
78
0
   bits->nbBits=buf_size<<LOG2_BITS_PER_CHAR;
79
0
   bits->charPtr=0;
80
0
   bits->bitPtr=0;
81
0
   bits->overflow=0;
82
83
0
}
84
85
EXPORT void speex_bits_destroy(SpeexBits *bits)
86
9.82k
{
87
9.82k
   if (bits->owner)
88
9.82k
      speex_free(bits->chars);
89
   /* Will do something once the allocation is dynamic */
90
9.82k
}
91
92
EXPORT void speex_bits_reset(SpeexBits *bits)
93
37.5k
{
94
   /* We only need to clear the first byte now */
95
37.5k
   bits->chars[0]=0;
96
37.5k
   bits->nbBits=0;
97
37.5k
   bits->charPtr=0;
98
37.5k
   bits->bitPtr=0;
99
37.5k
   bits->overflow=0;
100
37.5k
}
101
102
EXPORT void speex_bits_rewind(SpeexBits *bits)
103
0
{
104
0
   bits->charPtr=0;
105
0
   bits->bitPtr=0;
106
0
   bits->overflow=0;
107
0
}
108
109
EXPORT void speex_bits_read_from(SpeexBits *bits, const char *chars, int len)
110
46.0k
{
111
46.0k
   int i;
112
46.0k
   int nchars = len / BYTES_PER_CHAR;
113
46.0k
   if (nchars > bits->buf_size)
114
30
   {
115
30
      speex_notify("Packet is larger than allocated buffer");
116
30
      if (bits->owner)
117
30
      {
118
30
         char *tmp = (char*)speex_realloc(bits->chars, nchars);
119
30
         if (tmp)
120
30
         {
121
30
            bits->buf_size=nchars;
122
30
            bits->chars=tmp;
123
30
         } else {
124
0
            nchars=bits->buf_size;
125
0
            speex_warning("Could not resize input buffer: truncating input");
126
0
         }
127
30
      } else {
128
0
         speex_warning("Do not own input buffer: truncating oversize input");
129
0
         nchars=bits->buf_size;
130
0
      }
131
30
   }
132
#if (BYTES_PER_CHAR==2)
133
/* Swap bytes to proper endian order (could be done externally) */
134
#define HTOLS(A) ((((A) >> 8)&0xff)|(((A) & 0xff)<<8))
135
#else
136
941k
#define HTOLS(A) (A)
137
46.0k
#endif
138
454k
   for (i=0;i<nchars;i++)
139
408k
      bits->chars[i]=HTOLS(chars[i]);
140
141
46.0k
   bits->nbBits=nchars<<LOG2_BITS_PER_CHAR;
142
46.0k
   bits->charPtr=0;
143
46.0k
   bits->bitPtr=0;
144
46.0k
   bits->overflow=0;
145
46.0k
}
146
147
static void speex_bits_flush(SpeexBits *bits)
148
0
{
149
0
   int nchars = ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
150
0
   if (bits->charPtr>0)
151
0
      SPEEX_MOVE(bits->chars, &bits->chars[bits->charPtr], nchars-bits->charPtr);
152
0
   bits->nbBits -= bits->charPtr<<LOG2_BITS_PER_CHAR;
153
0
   bits->charPtr=0;
154
0
}
155
156
EXPORT void speex_bits_read_whole_bytes(SpeexBits *bits, const char *chars, int nbytes)
157
0
{
158
0
   int i,pos;
159
0
   int nchars = nbytes/BYTES_PER_CHAR;
160
161
0
   if (((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR)+nchars > bits->buf_size)
162
0
   {
163
      /* Packet is larger than allocated buffer */
164
0
      if (bits->owner)
165
0
      {
166
0
         char *tmp = (char*)speex_realloc(bits->chars, (bits->nbBits>>LOG2_BITS_PER_CHAR)+nchars+1);
167
0
         if (tmp)
168
0
         {
169
0
            bits->buf_size=(bits->nbBits>>LOG2_BITS_PER_CHAR)+nchars+1;
170
0
            bits->chars=tmp;
171
0
         } else {
172
0
            nchars=bits->buf_size-(bits->nbBits>>LOG2_BITS_PER_CHAR)-1;
173
0
            speex_warning("Could not resize input buffer: truncating oversize input");
174
0
         }
175
0
      } else {
176
0
         speex_warning("Do not own input buffer: truncating oversize input");
177
0
         nchars=bits->buf_size;
178
0
      }
179
0
   }
180
181
0
   speex_bits_flush(bits);
182
0
   pos=bits->nbBits>>LOG2_BITS_PER_CHAR;
183
0
   for (i=0;i<nchars;i++)
184
0
      bits->chars[pos+i]=HTOLS(chars[i]);
185
0
   bits->nbBits+=nchars<<LOG2_BITS_PER_CHAR;
186
0
}
187
188
EXPORT int speex_bits_write(SpeexBits *bits, char *chars, int max_nbytes)
189
27.7k
{
190
27.7k
   int i;
191
27.7k
   int max_nchars = max_nbytes/BYTES_PER_CHAR;
192
27.7k
   int charPtr, bitPtr, nbBits;
193
194
   /* Insert terminator, but save the data so we can put it back after */
195
27.7k
   bitPtr=bits->bitPtr;
196
27.7k
   charPtr=bits->charPtr;
197
27.7k
   nbBits=bits->nbBits;
198
27.7k
   speex_bits_insert_terminator(bits);
199
27.7k
   bits->bitPtr=bitPtr;
200
27.7k
   bits->charPtr=charPtr;
201
27.7k
   bits->nbBits=nbBits;
202
203
27.7k
   if (max_nchars > ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR))
204
27.7k
      max_nchars = ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
205
206
560k
   for (i=0;i<max_nchars;i++)
207
533k
      chars[i]=HTOLS(bits->chars[i]);
208
27.7k
   return max_nchars*BYTES_PER_CHAR;
209
27.7k
}
210
211
EXPORT int speex_bits_write_whole_bytes(SpeexBits *bits, char *chars, int max_nbytes)
212
0
{
213
0
   int max_nchars = max_nbytes/BYTES_PER_CHAR;
214
0
   int i;
215
0
   if (max_nchars > ((bits->nbBits)>>LOG2_BITS_PER_CHAR))
216
0
      max_nchars = ((bits->nbBits)>>LOG2_BITS_PER_CHAR);
217
0
   for (i=0;i<max_nchars;i++)
218
0
      chars[i]=HTOLS(bits->chars[i]);
219
220
0
   if (bits->bitPtr>0)
221
0
      bits->chars[0]=bits->chars[max_nchars];
222
0
   else
223
0
      bits->chars[0]=0;
224
0
   bits->charPtr=0;
225
0
   bits->nbBits &= (BITS_PER_CHAR-1);
226
0
   return max_nchars*BYTES_PER_CHAR;
227
0
}
228
229
EXPORT void speex_bits_pack(SpeexBits *bits, int data, int nbBits)
230
863k
{
231
863k
   unsigned int d=data;
232
233
863k
   if (bits->charPtr+((nbBits+bits->bitPtr)>>LOG2_BITS_PER_CHAR) >= bits->buf_size)
234
0
   {
235
0
      speex_notify("Buffer too small to pack bits");
236
0
      if (bits->owner)
237
0
      {
238
0
         int new_nchars = ((bits->buf_size+5)*3)>>1;
239
0
         char *tmp = (char*)speex_realloc(bits->chars, new_nchars);
240
0
         if (tmp)
241
0
         {
242
0
            bits->buf_size=new_nchars;
243
0
            bits->chars=tmp;
244
0
         } else {
245
0
            speex_warning("Could not resize input buffer: not packing");
246
0
            return;
247
0
         }
248
0
      } else {
249
0
         speex_warning("Do not own input buffer: not packing");
250
0
         return;
251
0
      }
252
0
   }
253
254
5.12M
   while(nbBits)
255
4.26M
   {
256
4.26M
      int bit;
257
4.26M
      bit = (d>>(nbBits-1))&1;
258
4.26M
      bits->chars[bits->charPtr] |= bit<<(BITS_PER_CHAR-1-bits->bitPtr);
259
4.26M
      bits->bitPtr++;
260
261
4.26M
      if (bits->bitPtr==BITS_PER_CHAR)
262
533k
      {
263
533k
         bits->bitPtr=0;
264
533k
         bits->charPtr++;
265
533k
         bits->chars[bits->charPtr] = 0;
266
533k
      }
267
4.26M
      bits->nbBits++;
268
4.26M
      nbBits--;
269
4.26M
   }
270
863k
}
271
272
EXPORT int speex_bits_unpack_signed(SpeexBits *bits, int nbBits)
273
0
{
274
0
   unsigned int d=speex_bits_unpack_unsigned(bits,nbBits);
275
   /* If number is negative */
276
0
   if (d>>(nbBits-1))
277
0
   {
278
0
      d |= (~0u)<<nbBits;
279
0
   }
280
0
   return d;
281
0
}
282
283
EXPORT unsigned int speex_bits_unpack_unsigned(SpeexBits *bits, int nbBits)
284
1.10M
{
285
1.10M
   unsigned int d=0;
286
1.10M
   if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+nbBits>bits->nbBits)
287
683k
      bits->overflow=1;
288
1.10M
   if (bits->overflow)
289
795k
      return 0;
290
1.50M
   while(nbBits)
291
1.19M
   {
292
1.19M
      d<<=1;
293
1.19M
      d |= (bits->chars[bits->charPtr]>>(BITS_PER_CHAR-1 - bits->bitPtr))&1;
294
1.19M
      bits->bitPtr++;
295
1.19M
      if (bits->bitPtr==BITS_PER_CHAR)
296
127k
      {
297
127k
         bits->bitPtr=0;
298
127k
         bits->charPtr++;
299
127k
      }
300
1.19M
      nbBits--;
301
1.19M
   }
302
309k
   return d;
303
1.10M
}
304
305
EXPORT unsigned int speex_bits_peek_unsigned(SpeexBits *bits, int nbBits)
306
0
{
307
0
   unsigned int d=0;
308
0
   int bitPtr, charPtr;
309
0
   char *chars;
310
311
0
   if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+nbBits>bits->nbBits)
312
0
     bits->overflow=1;
313
0
   if (bits->overflow)
314
0
      return 0;
315
316
0
   bitPtr=bits->bitPtr;
317
0
   charPtr=bits->charPtr;
318
0
   chars = bits->chars;
319
0
   while(nbBits)
320
0
   {
321
0
      d<<=1;
322
0
      d |= (chars[charPtr]>>(BITS_PER_CHAR-1 - bitPtr))&1;
323
0
      bitPtr++;
324
0
      if (bitPtr==BITS_PER_CHAR)
325
0
      {
326
0
         bitPtr=0;
327
0
         charPtr++;
328
0
      }
329
0
      nbBits--;
330
0
   }
331
0
   return d;
332
0
}
333
334
EXPORT int speex_bits_peek(SpeexBits *bits)
335
31.9k
{
336
31.9k
   if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+1>bits->nbBits)
337
0
      bits->overflow=1;
338
31.9k
   if (bits->overflow)
339
0
      return 0;
340
31.9k
   return (bits->chars[bits->charPtr]>>(BITS_PER_CHAR-1 - bits->bitPtr))&1;
341
31.9k
}
342
343
EXPORT void speex_bits_advance(SpeexBits *bits, int n)
344
9.66k
{
345
9.66k
    if (((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+n>bits->nbBits) || bits->overflow){
346
4.36k
      bits->overflow=1;
347
4.36k
      return;
348
4.36k
    }
349
5.29k
   bits->charPtr += (bits->bitPtr+n) >> LOG2_BITS_PER_CHAR; /* divide by BITS_PER_CHAR */
350
5.29k
   bits->bitPtr = (bits->bitPtr+n) & (BITS_PER_CHAR-1);       /* modulo by BITS_PER_CHAR */
351
5.29k
}
352
353
EXPORT int speex_bits_remaining(SpeexBits *bits)
354
228k
{
355
228k
   if (bits->overflow)
356
55.0k
      return -1;
357
173k
   else
358
173k
      return bits->nbBits-((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr);
359
228k
}
360
361
EXPORT int speex_bits_nbytes(SpeexBits *bits)
362
27.7k
{
363
27.7k
   return ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
364
27.7k
}
365
366
EXPORT void speex_bits_insert_terminator(SpeexBits *bits)
367
55.4k
{
368
55.4k
   if (bits->bitPtr)
369
24.4k
      speex_bits_pack(bits, 0, 1);
370
126k
   while (bits->bitPtr)
371
71.1k
      speex_bits_pack(bits, 1, 1);
372
55.4k
}