blob: 727e047ea1168cf1d363200978dfdd22afd9e1f5
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 | #include <stdlib.h> |
31 | #include "common.h" |
32 | #include "structs.h" |
33 | |
34 | #include "bits.h" |
35 | |
36 | /* initialize buffer, call once before first getbits or showbits */ |
37 | void faad_initbits(bitfile *ld, const void *_buffer, const uint32_t buffer_size) |
38 | { |
39 | uint32_t tmp; |
40 | |
41 | if (ld == NULL) { |
42 | return; |
43 | } |
44 | |
45 | // useless |
46 | //memset(ld, 0, sizeof(bitfile)); |
47 | |
48 | if (buffer_size == 0 || _buffer == NULL) { |
49 | ld->error = 1; |
50 | return; |
51 | } |
52 | |
53 | ld->buffer = _buffer; |
54 | |
55 | ld->buffer_size = buffer_size; |
56 | ld->bytes_left = buffer_size; |
57 | |
58 | if (ld->bytes_left >= 4) { |
59 | tmp = getdword((uint32_t*)ld->buffer); |
60 | ld->bytes_left -= 4; |
61 | } else { |
62 | tmp = getdword_n((uint32_t*)ld->buffer, ld->bytes_left); |
63 | ld->bytes_left = 0; |
64 | } |
65 | ld->bufa = tmp; |
66 | |
67 | if (ld->bytes_left >= 4) { |
68 | tmp = getdword((uint32_t*)ld->buffer + 1); |
69 | ld->bytes_left -= 4; |
70 | } else { |
71 | tmp = getdword_n((uint32_t*)ld->buffer + 1, ld->bytes_left); |
72 | ld->bytes_left = 0; |
73 | } |
74 | ld->bufb = tmp; |
75 | |
76 | ld->start = (uint32_t*)ld->buffer; |
77 | ld->tail = ((uint32_t*)ld->buffer + 2); |
78 | |
79 | ld->bits_left = 32; |
80 | |
81 | ld->error = 0; |
82 | } |
83 | |
84 | void faad_endbits(bitfile *ld) |
85 | { |
86 | // void |
87 | } |
88 | |
89 | uint32_t faad_get_processed_bits(bitfile *ld) |
90 | { |
91 | return (uint32_t)(8 * (4 * (ld->tail - ld->start) - 4) - (ld->bits_left)); |
92 | } |
93 | |
94 | uint8_t faad_byte_align(bitfile *ld) |
95 | { |
96 | int remainder = (32 - ld->bits_left) & 0x7; |
97 | |
98 | if (remainder) { |
99 | faad_flushbits(ld, 8 - remainder); |
100 | return (uint8_t)(8 - remainder); |
101 | } |
102 | return 0; |
103 | } |
104 | |
105 | void faad_flushbits_ex(bitfile *ld, uint32_t bits) |
106 | { |
107 | uint32_t tmp; |
108 | |
109 | ld->bufa = ld->bufb; |
110 | if (ld->bytes_left >= 4) { |
111 | tmp = getdword(ld->tail); |
112 | ld->bytes_left -= 4; |
113 | } else { |
114 | tmp = getdword_n(ld->tail, ld->bytes_left); |
115 | ld->bytes_left = 0; |
116 | } |
117 | ld->bufb = tmp; |
118 | ld->tail++; |
119 | ld->bits_left += (32 - bits); |
120 | //ld->bytes_left -= 4; |
121 | // if (ld->bytes_left == 0) |
122 | // ld->no_more_reading = 1; |
123 | // if (ld->bytes_left < 0) |
124 | // ld->error = 1; |
125 | } |
126 | |
127 | /* rewind to beginning */ |
128 | void faad_rewindbits(bitfile *ld) |
129 | { |
130 | uint32_t tmp; |
131 | |
132 | ld->bytes_left = ld->buffer_size; |
133 | |
134 | if (ld->bytes_left >= 4) { |
135 | tmp = getdword((uint32_t*)&ld->start[0]); |
136 | ld->bytes_left -= 4; |
137 | } else { |
138 | tmp = getdword_n((uint32_t*)&ld->start[0], ld->bytes_left); |
139 | ld->bytes_left = 0; |
140 | } |
141 | ld->bufa = tmp; |
142 | |
143 | if (ld->bytes_left >= 4) { |
144 | tmp = getdword((uint32_t*)&ld->start[1]); |
145 | ld->bytes_left -= 4; |
146 | } else { |
147 | tmp = getdword_n((uint32_t*)&ld->start[1], ld->bytes_left); |
148 | ld->bytes_left = 0; |
149 | } |
150 | ld->bufb = tmp; |
151 | |
152 | ld->bits_left = 32; |
153 | ld->tail = &ld->start[2]; |
154 | } |
155 | |
156 | /* reset to a certain point */ |
157 | void faad_resetbits(bitfile *ld, int bits) |
158 | { |
159 | uint32_t tmp; |
160 | int words = bits >> 5; |
161 | int remainder = bits & 0x1F; |
162 | |
163 | ld->bytes_left = ld->buffer_size - words * 4; |
164 | |
165 | if (ld->bytes_left >= 4) { |
166 | tmp = getdword(&ld->start[words]); |
167 | ld->bytes_left -= 4; |
168 | } else { |
169 | tmp = getdword_n(&ld->start[words], ld->bytes_left); |
170 | ld->bytes_left = 0; |
171 | } |
172 | ld->bufa = tmp; |
173 | |
174 | if (ld->bytes_left >= 4) { |
175 | tmp = getdword(&ld->start[words + 1]); |
176 | ld->bytes_left -= 4; |
177 | } else { |
178 | tmp = getdword_n(&ld->start[words + 1], ld->bytes_left); |
179 | ld->bytes_left = 0; |
180 | } |
181 | ld->bufb = tmp; |
182 | |
183 | ld->bits_left = 32 - remainder; |
184 | ld->tail = &ld->start[words + 2]; |
185 | |
186 | /* recheck for reading too many bytes */ |
187 | ld->error = 0; |
188 | // if (ld->bytes_left == 0) |
189 | // ld->no_more_reading = 1; |
190 | // if (ld->bytes_left < 0) |
191 | // ld->error = 1; |
192 | } |
193 | |
194 | uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits |
195 | DEBUGDEC) |
196 | { |
197 | int i; |
198 | unsigned int temp; |
199 | int bytes = bits >> 3; |
200 | int remainder = bits & 0x7; |
201 | |
202 | uint8_t *buffer = (uint8_t*)faad_malloc((bytes + 1) * sizeof(uint8_t)); |
203 | |
204 | for (i = 0; i < bytes; i++) { |
205 | buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print, var, dbg)); |
206 | } |
207 | |
208 | if (remainder) { |
209 | temp = faad_getbits(ld, remainder DEBUGVAR(print, var, dbg)) << (8 - remainder); |
210 | |
211 | buffer[bytes] = (uint8_t)temp; |
212 | } |
213 | |
214 | return buffer; |
215 | } |
216 | |
217 | #ifdef DRM |
218 | /* return the original data buffer */ |
219 | void *faad_origbitbuffer(bitfile *ld) |
220 | { |
221 | return (void*)ld->start; |
222 | } |
223 | |
224 | /* return the original data buffer size */ |
225 | uint32_t faad_origbitbuffer_size(bitfile *ld) |
226 | { |
227 | return ld->buffer_size; |
228 | } |
229 | #endif |
230 | |
231 | /* reversed bit reading routines, used for RVLC and HCR */ |
232 | void faad_initbits_rev(bitfile *ld, void *buffer, |
233 | uint32_t bits_in_buffer) |
234 | { |
235 | uint32_t tmp; |
236 | int32_t index; |
237 | |
238 | ld->buffer_size = bit2byte(bits_in_buffer); |
239 | |
240 | index = (bits_in_buffer + 31) / 32 - 1; |
241 | |
242 | ld->start = (uint32_t*)buffer + index - 2; |
243 | |
244 | tmp = getdword((uint32_t*)buffer + index); |
245 | ld->bufa = tmp; |
246 | |
247 | tmp = getdword((uint32_t*)buffer + index - 1); |
248 | ld->bufb = tmp; |
249 | |
250 | ld->tail = (uint32_t*)buffer + index; |
251 | |
252 | ld->bits_left = bits_in_buffer % 32; |
253 | if (ld->bits_left == 0) { |
254 | ld->bits_left = 32; |
255 | } |
256 | |
257 | ld->bytes_left = ld->buffer_size; |
258 | ld->error = 0; |
259 | } |
260 | |
261 | /* EOF */ |
262 |