/proc/self/cwd/libfaad/bits.c
Line | Count | Source (jump to first uncovered line) |
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 | 1.75k | { |
40 | 1.75k | uint8_t* m8 = (uint8_t*)mem; |
41 | 1.75k | switch (n) |
42 | 1.75k | { |
43 | 56 | case 3: |
44 | 56 | return ((uint32_t)m8[2] << 8) | ((uint32_t)m8[1] << 16) | ((uint32_t)m8[0] << 24); |
45 | 45 | case 2: |
46 | 45 | return ((uint32_t)m8[1] << 16) | ((uint32_t)m8[0] << 24); |
47 | 60 | case 1: |
48 | 60 | return (uint32_t)m8[0] << 24; |
49 | 1.59k | default: |
50 | 1.59k | return 0; |
51 | 1.75k | } |
52 | 1.75k | } |
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 | 333 | { |
57 | 333 | uint32_t tmp; |
58 | | |
59 | 333 | if (ld == NULL) |
60 | 0 | return; |
61 | | |
62 | 333 | if (buffer_size == 0 || _buffer == NULL) |
63 | 14 | { |
64 | 14 | ld->error = 1; |
65 | 14 | return; |
66 | 14 | } |
67 | | |
68 | 319 | ld->buffer = _buffer; |
69 | | |
70 | 319 | ld->buffer_size = buffer_size; |
71 | 319 | ld->bytes_left = buffer_size; |
72 | | |
73 | 319 | if (ld->bytes_left >= 4) |
74 | 250 | { |
75 | 250 | tmp = getdword((uint32_t*)ld->buffer); |
76 | 250 | ld->bytes_left -= 4; |
77 | 250 | } else { |
78 | 69 | tmp = getdword_n((uint32_t*)ld->buffer, ld->bytes_left); |
79 | 69 | ld->bytes_left = 0; |
80 | 69 | } |
81 | 319 | ld->bufa = tmp; |
82 | | |
83 | 319 | if (ld->bytes_left >= 4) |
84 | 155 | { |
85 | 155 | tmp = getdword((uint32_t*)ld->buffer + 1); |
86 | 155 | ld->bytes_left -= 4; |
87 | 164 | } else { |
88 | 164 | tmp = getdword_n((uint32_t*)ld->buffer + 1, ld->bytes_left); |
89 | 164 | ld->bytes_left = 0; |
90 | 164 | } |
91 | 319 | ld->bufb = tmp; |
92 | | |
93 | 319 | ld->start = (uint32_t*)ld->buffer; |
94 | 319 | ld->tail = ((uint32_t*)ld->buffer + 2); |
95 | | |
96 | 319 | ld->bits_left = 32; |
97 | | |
98 | 319 | ld->error = 0; |
99 | 319 | } |
100 | | |
101 | | void faad_endbits(bitfile *ld) |
102 | 604 | { |
103 | 604 | (void)ld; |
104 | 604 | } |
105 | | |
106 | | uint32_t faad_get_processed_bits(bitfile *ld) |
107 | 604 | { |
108 | 604 | return (uint32_t)(8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left)); |
109 | 604 | } |
110 | | |
111 | | uint8_t faad_byte_align(bitfile *ld) |
112 | 198 | { |
113 | 198 | int remainder = (32 - ld->bits_left) & 0x7; |
114 | | |
115 | 198 | if (remainder) |
116 | 183 | { |
117 | 183 | faad_flushbits(ld, 8 - remainder); |
118 | 183 | return (uint8_t)(8 - remainder); |
119 | 183 | } |
120 | 15 | return 0; |
121 | 198 | } |
122 | | |
123 | | void faad_flushbits_ex(bitfile *ld, uint32_t bits) |
124 | 3.49k | { |
125 | 3.49k | uint32_t tmp; |
126 | | |
127 | 3.49k | ld->bufa = ld->bufb; |
128 | 3.49k | if (ld->bytes_left >= 4) |
129 | 1.96k | { |
130 | 1.96k | tmp = getdword(ld->tail); |
131 | 1.96k | ld->bytes_left -= 4; |
132 | 1.96k | } else { |
133 | 1.52k | tmp = getdword_n(ld->tail, ld->bytes_left); |
134 | 1.52k | ld->bytes_left = 0; |
135 | 1.52k | } |
136 | 3.49k | ld->bufb = tmp; |
137 | 3.49k | ld->tail++; |
138 | 3.49k | 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 | 3.49k | } |
145 | | |
146 | | #ifdef DRM |
147 | | /* rewind to beginning */ |
148 | | void faad_rewindbits(bitfile *ld) |
149 | | { |
150 | | uint32_t tmp; |
151 | | |
152 | | ld->bytes_left = ld->buffer_size; |
153 | | |
154 | | if (ld->bytes_left >= 4) |
155 | | { |
156 | | tmp = getdword((uint32_t*)&ld->start[0]); |
157 | | ld->bytes_left -= 4; |
158 | | } else { |
159 | | tmp = getdword_n((uint32_t*)&ld->start[0], ld->bytes_left); |
160 | | ld->bytes_left = 0; |
161 | | } |
162 | | ld->bufa = tmp; |
163 | | |
164 | | if (ld->bytes_left >= 4) |
165 | | { |
166 | | tmp = getdword((uint32_t*)&ld->start[1]); |
167 | | ld->bytes_left -= 4; |
168 | | } else { |
169 | | tmp = getdword_n((uint32_t*)&ld->start[1], ld->bytes_left); |
170 | | ld->bytes_left = 0; |
171 | | } |
172 | | ld->bufb = tmp; |
173 | | |
174 | | ld->bits_left = 32; |
175 | | ld->tail = &ld->start[2]; |
176 | | } |
177 | | #endif |
178 | | |
179 | | /* reset to a certain point */ |
180 | | void faad_resetbits(bitfile *ld, uint32_t bits) |
181 | 0 | { |
182 | 0 | uint32_t tmp; |
183 | 0 | uint32_t words = bits >> 5; |
184 | 0 | uint32_t remainder = bits & 0x1F; |
185 | |
|
186 | 0 | if (ld->buffer_size < words * 4) |
187 | 0 | ld->bytes_left = 0; |
188 | 0 | else |
189 | 0 | ld->bytes_left = ld->buffer_size - words*4; |
190 | |
|
191 | 0 | if (ld->bytes_left >= 4) |
192 | 0 | { |
193 | 0 | tmp = getdword(&ld->start[words]); |
194 | 0 | ld->bytes_left -= 4; |
195 | 0 | } else { |
196 | 0 | tmp = getdword_n(&ld->start[words], ld->bytes_left); |
197 | 0 | ld->bytes_left = 0; |
198 | 0 | } |
199 | 0 | ld->bufa = tmp; |
200 | |
|
201 | 0 | if (ld->bytes_left >= 4) |
202 | 0 | { |
203 | 0 | tmp = getdword(&ld->start[words+1]); |
204 | 0 | ld->bytes_left -= 4; |
205 | 0 | } else { |
206 | 0 | tmp = getdword_n(&ld->start[words+1], ld->bytes_left); |
207 | 0 | ld->bytes_left = 0; |
208 | 0 | } |
209 | 0 | ld->bufb = tmp; |
210 | |
|
211 | 0 | ld->bits_left = 32 - remainder; |
212 | 0 | ld->tail = &ld->start[words+2]; |
213 | | |
214 | | /* recheck for reading too many bytes */ |
215 | 0 | 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 | 0 | } |
221 | | |
222 | | uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits |
223 | | DEBUGDEC) |
224 | 0 | { |
225 | 0 | int i; |
226 | 0 | unsigned int temp; |
227 | 0 | int bytes = bits >> 3; |
228 | 0 | int remainder = bits & 0x7; |
229 | |
|
230 | 0 | uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t)); |
231 | |
|
232 | 0 | for (i = 0; i < bytes; i++) |
233 | 0 | { |
234 | 0 | buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg)); |
235 | 0 | } |
236 | |
|
237 | 0 | if (remainder) |
238 | 0 | { |
239 | 0 | temp = faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder); |
240 | |
|
241 | 0 | buffer[bytes] = (uint8_t)temp; |
242 | 0 | } |
243 | |
|
244 | 0 | return buffer; |
245 | 0 | } |
246 | | |
247 | | #ifdef DRM |
248 | | /* return the original data buffer */ |
249 | | void *faad_origbitbuffer(bitfile *ld) |
250 | | { |
251 | | return (void*)ld->start; |
252 | | } |
253 | | |
254 | | /* return the original data buffer size */ |
255 | | uint32_t faad_origbitbuffer_size(bitfile *ld) |
256 | | { |
257 | | return ld->buffer_size; |
258 | | } |
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 */ |