Coverage Report

Created: 2026-02-14 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/bits.c
Line
Count
Source
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: bits.c,v 1.44 2007/11/01 12:33:29 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include <stdlib.h>
35
#include "bits.h"
36
37
/* reads only n bytes from the stream instead of the standard 4 */
38
static /*INLINE*/ uint32_t getdword_n(void *mem, int n)
39
7.50M
{
40
7.50M
    uint8_t* m8 = (uint8_t*)mem;
41
7.50M
    switch (n)
42
7.50M
    {
43
12.6k
    case 3:
44
12.6k
        return ((uint32_t)m8[2] << 8) | ((uint32_t)m8[1] << 16) | ((uint32_t)m8[0] << 24);
45
12.9k
    case 2:
46
12.9k
        return ((uint32_t)m8[1] << 16) | ((uint32_t)m8[0] << 24);
47
25.5k
    case 1:
48
25.5k
        return (uint32_t)m8[0] << 24;
49
7.45M
    default:
50
7.45M
        return 0;
51
7.50M
    }
52
7.50M
}
53
54
/* initialize buffer, call once before first getbits or showbits */
55
void faad_initbits(bitfile *ld, const void *_buffer, const uint32_t buffer_size)
56
67.2k
{
57
67.2k
    uint32_t tmp;
58
59
67.2k
    if (ld == NULL)
60
0
        return;
61
62
67.2k
    if (buffer_size == 0 || _buffer == NULL)
63
15.7k
    {
64
15.7k
        ld->error = 1;
65
15.7k
        return;
66
15.7k
    }
67
68
51.5k
    ld->buffer = _buffer;
69
70
51.5k
    ld->buffer_size = buffer_size;
71
51.5k
    ld->bytes_left  = buffer_size;
72
73
51.5k
    if (ld->bytes_left >= 4)
74
31.0k
    {
75
31.0k
        tmp = getdword((uint32_t*)ld->buffer);
76
31.0k
        ld->bytes_left -= 4;
77
31.0k
    } else {
78
20.4k
        tmp = getdword_n((uint32_t*)ld->buffer, ld->bytes_left);
79
20.4k
        ld->bytes_left = 0;
80
20.4k
    }
81
51.5k
    ld->bufa = tmp;
82
83
51.5k
    if (ld->bytes_left >= 4)
84
22.0k
    {
85
22.0k
        tmp = getdword((uint32_t*)ld->buffer + 1);
86
22.0k
        ld->bytes_left -= 4;
87
29.4k
    } else {
88
29.4k
        tmp = getdword_n((uint32_t*)ld->buffer + 1, ld->bytes_left);
89
29.4k
        ld->bytes_left = 0;
90
29.4k
    }
91
51.5k
    ld->bufb = tmp;
92
93
51.5k
    ld->start = (uint32_t*)ld->buffer;
94
51.5k
    ld->tail = ((uint32_t*)ld->buffer + 2);
95
96
51.5k
    ld->bits_left = 32;
97
98
51.5k
    ld->error = 0;
99
51.5k
}
100
101
void faad_endbits(bitfile *ld)
102
59.1k
{
103
59.1k
    (void)ld;
104
59.1k
}
105
106
uint32_t faad_get_processed_bits(bitfile *ld)
107
87.6M
{
108
87.6M
    return (uint32_t)(8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left));
109
87.6M
}
110
111
uint8_t faad_byte_align(bitfile *ld)
112
1.29M
{
113
1.29M
    int remainder = (32 - ld->bits_left) & 0x7;
114
115
1.29M
    if (remainder)
116
39.1k
    {
117
39.1k
        faad_flushbits(ld, 8 - remainder);
118
39.1k
        return (uint8_t)(8 - remainder);
119
39.1k
    }
120
1.25M
    return 0;
121
1.29M
}
122
123
void faad_flushbits_ex(bitfile *ld, uint32_t bits)
124
24.5M
{
125
24.5M
    uint32_t tmp;
126
127
24.5M
    ld->bufa = ld->bufb;
128
24.5M
    if (ld->bytes_left >= 4)
129
17.0M
    {
130
17.0M
        tmp = getdword(ld->tail);
131
17.0M
        ld->bytes_left -= 4;
132
17.0M
    } else {
133
7.45M
        tmp = getdword_n(ld->tail, ld->bytes_left);
134
7.45M
        ld->bytes_left = 0;
135
7.45M
    }
136
24.5M
    ld->bufb = tmp;
137
24.5M
    ld->tail++;
138
24.5M
    ld->bits_left += (32 - bits);
139
    //ld->bytes_left -= 4;
140
//    if (ld->bytes_left == 0)
141
//        ld->no_more_reading = 1;
142
//    if (ld->bytes_left < 0)
143
//        ld->error = 1;
144
24.5M
}
145
146
#ifdef DRM
147
/* rewind to beginning */
148
void faad_rewindbits(bitfile *ld)
149
896
{
150
896
    uint32_t tmp;
151
152
896
    ld->bytes_left = ld->buffer_size;
153
154
896
    if (ld->bytes_left >= 4)
155
841
    {
156
841
        tmp = getdword((uint32_t*)&ld->start[0]);
157
841
        ld->bytes_left -= 4;
158
841
    } else {
159
55
        tmp = getdword_n((uint32_t*)&ld->start[0], ld->bytes_left);
160
55
        ld->bytes_left = 0;
161
55
    }
162
896
    ld->bufa = tmp;
163
164
896
    if (ld->bytes_left >= 4)
165
728
    {
166
728
        tmp = getdword((uint32_t*)&ld->start[1]);
167
728
        ld->bytes_left -= 4;
168
728
    } else {
169
168
        tmp = getdword_n((uint32_t*)&ld->start[1], ld->bytes_left);
170
168
        ld->bytes_left = 0;
171
168
    }
172
896
    ld->bufb = tmp;
173
174
896
    ld->bits_left = 32;
175
896
    ld->tail = &ld->start[2];
176
896
}
177
#endif
178
179
/* reset to a certain point */
180
void faad_resetbits(bitfile *ld, uint32_t bits)
181
45.6k
{
182
45.6k
    uint32_t tmp;
183
45.6k
    uint32_t words = bits >> 5;
184
45.6k
    uint32_t remainder = bits & 0x1F;
185
186
45.6k
    if (ld->buffer_size < words * 4)
187
339
        ld->bytes_left = 0;
188
45.3k
    else
189
45.3k
        ld->bytes_left = ld->buffer_size - words*4;
190
191
45.6k
    if (ld->bytes_left >= 4)
192
44.3k
    {
193
44.3k
        tmp = getdword(&ld->start[words]);
194
44.3k
        ld->bytes_left -= 4;
195
44.3k
    } else {
196
1.31k
        tmp = getdword_n(&ld->start[words], ld->bytes_left);
197
1.31k
        ld->bytes_left = 0;
198
1.31k
    }
199
45.6k
    ld->bufa = tmp;
200
201
45.6k
    if (ld->bytes_left >= 4)
202
43.0k
    {
203
43.0k
        tmp = getdword(&ld->start[words+1]);
204
43.0k
        ld->bytes_left -= 4;
205
43.0k
    } else {
206
2.64k
        tmp = getdword_n(&ld->start[words+1], ld->bytes_left);
207
2.64k
        ld->bytes_left = 0;
208
2.64k
    }
209
45.6k
    ld->bufb = tmp;
210
211
45.6k
    ld->bits_left = 32 - remainder;
212
45.6k
    ld->tail = &ld->start[words+2];
213
214
    /* recheck for reading too many bytes */
215
45.6k
    ld->error = 0;
216
//    if (ld->bytes_left == 0)
217
//        ld->no_more_reading = 1;
218
//    if (ld->bytes_left < 0)
219
//        ld->error = 1;
220
45.6k
}
221
222
uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
223
                       DEBUGDEC)
224
9.52k
{
225
9.52k
    int i;
226
9.52k
    unsigned int temp;
227
9.52k
    int bytes = bits >> 3;
228
9.52k
    int remainder = bits & 0x7;
229
230
9.52k
    uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t));
231
232
613k
    for (i = 0; i < bytes; i++)
233
603k
    {
234
603k
        buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
235
603k
    }
236
237
9.52k
    if (remainder)
238
6.51k
    {
239
6.51k
        temp = faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
240
241
6.51k
        buffer[bytes] = (uint8_t)temp;
242
6.51k
    }
243
244
9.52k
    return buffer;
245
9.52k
}
246
247
#ifdef DRM
248
/* return the original data buffer */
249
void *faad_origbitbuffer(bitfile *ld)
250
482
{
251
482
    return (void*)ld->start;
252
482
}
253
254
/* return the original data buffer size */
255
uint32_t faad_origbitbuffer_size(bitfile *ld)
256
482
{
257
482
    return ld->buffer_size;
258
482
}
259
#endif
260
261
#if 0
262
/* reversed bit reading routines, used for RVLC and HCR */
263
void faad_initbits_rev(bitfile *ld, void *buffer,
264
                       uint32_t bits_in_buffer)
265
{
266
    uint32_t tmp;
267
    int32_t index;
268
269
    ld->buffer_size = bit2byte(bits_in_buffer);
270
271
    index = (bits_in_buffer+31)/32 - 1;
272
273
    ld->start = (uint32_t*)buffer + index - 2;
274
275
    tmp = getdword((uint32_t*)buffer + index);
276
    ld->bufa = tmp;
277
278
    tmp = getdword((uint32_t*)buffer + index - 1);
279
    ld->bufb = tmp;
280
281
    ld->tail = (uint32_t*)buffer + index;
282
283
    ld->bits_left = bits_in_buffer % 32;
284
    if (ld->bits_left == 0)
285
        ld->bits_left = 32;
286
287
    ld->bytes_left = ld->buffer_size;
288
    ld->error = 0;
289
}
290
#endif
291
292
/* EOF */