diff options
author | Daniel Stenberg <daniel@haxx.se> | 2003-03-04 15:52:56 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2003-03-04 15:52:56 +0000 |
commit | 3aeb55f3858a1eca0a3b9ba8f212e889d19c0194 (patch) | |
tree | e8477cc3c1d17c36067a14eab398ae27563b5759 /uisimulator/common/libmad/frame.c | |
parent | 2e638cf8d57d5d253925b55e58b5b019446b6bcc (diff) | |
download | rockbox-3aeb55f3858a1eca0a3b9ba8f212e889d19c0194.tar.gz rockbox-3aeb55f3858a1eca0a3b9ba8f212e889d19c0194.zip |
no one ever uses libmad, remove it from our CVS
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@3382 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'uisimulator/common/libmad/frame.c')
-rw-r--r-- | uisimulator/common/libmad/frame.c | 499 |
1 files changed, 0 insertions, 499 deletions
diff --git a/uisimulator/common/libmad/frame.c b/uisimulator/common/libmad/frame.c deleted file mode 100644 index 2f513c61c4..0000000000 --- a/uisimulator/common/libmad/frame.c +++ /dev/null | |||
@@ -1,499 +0,0 @@ | |||
1 | /* | ||
2 | * libmad - MPEG audio decoder library | ||
3 | * Copyright (C) 2000-2001 Robert Leslie | ||
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 | * $Id$ | ||
20 | */ | ||
21 | |||
22 | # ifdef HAVE_CONFIG_H | ||
23 | # include "madconfig.h" | ||
24 | # endif | ||
25 | |||
26 | # include "global.h" | ||
27 | |||
28 | # include <stdlib.h> | ||
29 | |||
30 | # include "bit.h" | ||
31 | # include "stream.h" | ||
32 | # include "frame.h" | ||
33 | # include "timer.h" | ||
34 | # include "layer12.h" | ||
35 | # include "layer3.h" | ||
36 | |||
37 | static | ||
38 | unsigned long const bitrate_table[5][15] = { | ||
39 | /* MPEG-1 */ | ||
40 | { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */ | ||
41 | 256000, 288000, 320000, 352000, 384000, 416000, 448000 }, | ||
42 | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */ | ||
43 | 128000, 160000, 192000, 224000, 256000, 320000, 384000 }, | ||
44 | { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */ | ||
45 | 112000, 128000, 160000, 192000, 224000, 256000, 320000 }, | ||
46 | |||
47 | /* MPEG-2 LSF */ | ||
48 | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */ | ||
49 | 128000, 144000, 160000, 176000, 192000, 224000, 256000 }, | ||
50 | { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */ | ||
51 | 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */ | ||
52 | }; | ||
53 | |||
54 | static | ||
55 | unsigned int const samplerate_table[3] = { 44100, 48000, 32000 }; | ||
56 | |||
57 | static | ||
58 | int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = { | ||
59 | mad_layer_I, | ||
60 | mad_layer_II, | ||
61 | mad_layer_III | ||
62 | }; | ||
63 | |||
64 | /* | ||
65 | * NAME: header->init() | ||
66 | * DESCRIPTION: initialize header struct | ||
67 | */ | ||
68 | void mad_header_init(struct mad_header *header) | ||
69 | { | ||
70 | header->layer = 0; | ||
71 | header->mode = 0; | ||
72 | header->mode_extension = 0; | ||
73 | header->emphasis = 0; | ||
74 | |||
75 | header->bitrate = 0; | ||
76 | header->samplerate = 0; | ||
77 | |||
78 | header->crc_check = 0; | ||
79 | header->crc_target = 0; | ||
80 | |||
81 | header->flags = 0; | ||
82 | header->private_bits = 0; | ||
83 | |||
84 | header->duration = mad_timer_zero; | ||
85 | } | ||
86 | |||
87 | /* | ||
88 | * NAME: frame->init() | ||
89 | * DESCRIPTION: initialize frame struct | ||
90 | */ | ||
91 | void mad_frame_init(struct mad_frame *frame) | ||
92 | { | ||
93 | mad_header_init(&frame->header); | ||
94 | |||
95 | frame->options = 0; | ||
96 | |||
97 | frame->overlap = 0; | ||
98 | mad_frame_mute(frame); | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | * NAME: frame->finish() | ||
103 | * DESCRIPTION: deallocate any dynamic memory associated with frame | ||
104 | */ | ||
105 | void mad_frame_finish(struct mad_frame *frame) | ||
106 | { | ||
107 | mad_header_finish(&frame->header); | ||
108 | |||
109 | if (frame->overlap) { | ||
110 | free(frame->overlap); | ||
111 | frame->overlap = 0; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | /* | ||
116 | * NAME: decode_header() | ||
117 | * DESCRIPTION: read header data and following CRC word | ||
118 | */ | ||
119 | static | ||
120 | int decode_header(struct mad_header *header, struct mad_stream *stream) | ||
121 | { | ||
122 | unsigned int index; | ||
123 | |||
124 | header->flags = 0; | ||
125 | header->private_bits = 0; | ||
126 | |||
127 | /* header() */ | ||
128 | |||
129 | /* syncword */ | ||
130 | mad_bit_skip(&stream->ptr, 11); | ||
131 | |||
132 | /* MPEG 2.5 indicator (really part of syncword) */ | ||
133 | if (mad_bit_read(&stream->ptr, 1) == 0) | ||
134 | header->flags |= MAD_FLAG_MPEG_2_5_EXT; | ||
135 | |||
136 | /* ID */ | ||
137 | if (mad_bit_read(&stream->ptr, 1) == 0) | ||
138 | header->flags |= MAD_FLAG_LSF_EXT; | ||
139 | else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) { | ||
140 | stream->error = MAD_ERROR_LOSTSYNC; | ||
141 | return -1; | ||
142 | } | ||
143 | |||
144 | /* layer */ | ||
145 | header->layer = 4 - mad_bit_read(&stream->ptr, 2); | ||
146 | |||
147 | if (header->layer == 4) { | ||
148 | stream->error = MAD_ERROR_BADLAYER; | ||
149 | return -1; | ||
150 | } | ||
151 | |||
152 | /* protection_bit */ | ||
153 | if (mad_bit_read(&stream->ptr, 1) == 0) { | ||
154 | header->flags |= MAD_FLAG_PROTECTION; | ||
155 | header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff); | ||
156 | } | ||
157 | |||
158 | /* bitrate_index */ | ||
159 | index = mad_bit_read(&stream->ptr, 4); | ||
160 | |||
161 | if (index == 15) { | ||
162 | stream->error = MAD_ERROR_BADBITRATE; | ||
163 | return -1; | ||
164 | } | ||
165 | |||
166 | if (header->flags & MAD_FLAG_LSF_EXT) | ||
167 | header->bitrate = bitrate_table[3 + (header->layer >> 1)][index]; | ||
168 | else | ||
169 | header->bitrate = bitrate_table[header->layer - 1][index]; | ||
170 | |||
171 | /* sampling_frequency */ | ||
172 | index = mad_bit_read(&stream->ptr, 2); | ||
173 | |||
174 | if (index == 3) { | ||
175 | stream->error = MAD_ERROR_BADSAMPLERATE; | ||
176 | return -1; | ||
177 | } | ||
178 | |||
179 | header->samplerate = samplerate_table[index]; | ||
180 | |||
181 | if (header->flags & MAD_FLAG_LSF_EXT) { | ||
182 | header->samplerate /= 2; | ||
183 | |||
184 | if (header->flags & MAD_FLAG_MPEG_2_5_EXT) | ||
185 | header->samplerate /= 2; | ||
186 | } | ||
187 | |||
188 | /* padding_bit */ | ||
189 | if (mad_bit_read(&stream->ptr, 1)) | ||
190 | header->flags |= MAD_FLAG_PADDING; | ||
191 | |||
192 | /* private_bit */ | ||
193 | if (mad_bit_read(&stream->ptr, 1)) | ||
194 | header->private_bits |= MAD_PRIVATE_HEADER; | ||
195 | |||
196 | /* mode */ | ||
197 | header->mode = 3 - mad_bit_read(&stream->ptr, 2); | ||
198 | |||
199 | /* mode_extension */ | ||
200 | header->mode_extension = mad_bit_read(&stream->ptr, 2); | ||
201 | |||
202 | /* copyright */ | ||
203 | if (mad_bit_read(&stream->ptr, 1)) | ||
204 | header->flags |= MAD_FLAG_COPYRIGHT; | ||
205 | |||
206 | /* original/copy */ | ||
207 | if (mad_bit_read(&stream->ptr, 1)) | ||
208 | header->flags |= MAD_FLAG_ORIGINAL; | ||
209 | |||
210 | /* emphasis */ | ||
211 | header->emphasis = mad_bit_read(&stream->ptr, 2); | ||
212 | |||
213 | if (header->emphasis == 2) { | ||
214 | stream->error = MAD_ERROR_BADEMPHASIS; | ||
215 | return -1; | ||
216 | } | ||
217 | |||
218 | /* error_check() */ | ||
219 | |||
220 | /* crc_check */ | ||
221 | if (header->flags & MAD_FLAG_PROTECTION) | ||
222 | header->crc_target = mad_bit_read(&stream->ptr, 16); | ||
223 | |||
224 | return 0; | ||
225 | } | ||
226 | |||
227 | /* | ||
228 | * NAME: free_bitrate() | ||
229 | * DESCRIPTION: attempt to discover the bitstream's free bitrate | ||
230 | */ | ||
231 | static | ||
232 | int free_bitrate(struct mad_stream *stream, struct mad_header const *header) | ||
233 | { | ||
234 | struct mad_bitptr keep_ptr; | ||
235 | unsigned long rate = 0; | ||
236 | unsigned int pad_slot, slots_per_frame; | ||
237 | unsigned char const *ptr = 0; | ||
238 | |||
239 | keep_ptr = stream->ptr; | ||
240 | |||
241 | pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; | ||
242 | slots_per_frame = (header->layer == MAD_LAYER_III && | ||
243 | (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; | ||
244 | |||
245 | while (mad_stream_sync(stream) == 0) { | ||
246 | struct mad_stream peek_stream; | ||
247 | struct mad_header peek_header; | ||
248 | |||
249 | peek_stream = *stream; | ||
250 | peek_header = *header; | ||
251 | |||
252 | if (decode_header(&peek_header, &peek_stream) == 0 && | ||
253 | peek_header.layer == header->layer && | ||
254 | peek_header.samplerate == header->samplerate) { | ||
255 | unsigned int N; | ||
256 | |||
257 | ptr = mad_bit_nextbyte(&stream->ptr); | ||
258 | |||
259 | N = ptr - stream->this_frame; | ||
260 | |||
261 | if (header->layer == MAD_LAYER_I) { | ||
262 | rate = (unsigned long) header->samplerate * | ||
263 | (N - 4 * pad_slot + 4) / 48 / 1000; | ||
264 | } | ||
265 | else { | ||
266 | rate = (unsigned long) header->samplerate * | ||
267 | (N - pad_slot + 1) / slots_per_frame / 1000; | ||
268 | } | ||
269 | |||
270 | if (rate >= 8) | ||
271 | break; | ||
272 | } | ||
273 | |||
274 | mad_bit_skip(&stream->ptr, 8); | ||
275 | } | ||
276 | |||
277 | stream->ptr = keep_ptr; | ||
278 | |||
279 | if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) { | ||
280 | stream->error = MAD_ERROR_LOSTSYNC; | ||
281 | return -1; | ||
282 | } | ||
283 | |||
284 | stream->freerate = rate * 1000; | ||
285 | |||
286 | # if 0 && defined(DEBUG) | ||
287 | fprintf(stderr, "free bitrate == %lu\n", stream->freerate); | ||
288 | # endif | ||
289 | |||
290 | return 0; | ||
291 | } | ||
292 | |||
293 | /* | ||
294 | * NAME: header->decode() | ||
295 | * DESCRIPTION: read the next frame header from the stream | ||
296 | */ | ||
297 | int mad_header_decode(struct mad_header *header, struct mad_stream *stream) | ||
298 | { | ||
299 | register unsigned char const *ptr, *end; | ||
300 | unsigned int pad_slot, N; | ||
301 | |||
302 | ptr = stream->next_frame; | ||
303 | end = stream->bufend; | ||
304 | |||
305 | if (ptr == 0) { | ||
306 | stream->error = MAD_ERROR_BUFPTR; | ||
307 | goto fail; | ||
308 | } | ||
309 | |||
310 | /* stream skip */ | ||
311 | if (stream->skiplen) { | ||
312 | if (!stream->sync) | ||
313 | ptr = stream->this_frame; | ||
314 | |||
315 | if (end - ptr < stream->skiplen) { | ||
316 | stream->skiplen -= end - ptr; | ||
317 | stream->next_frame = end; | ||
318 | |||
319 | stream->error = MAD_ERROR_BUFLEN; | ||
320 | goto fail; | ||
321 | } | ||
322 | |||
323 | ptr += stream->skiplen; | ||
324 | stream->skiplen = 0; | ||
325 | |||
326 | stream->sync = 1; | ||
327 | } | ||
328 | |||
329 | sync: | ||
330 | /* synchronize */ | ||
331 | if (stream->sync) { | ||
332 | if (end - ptr < MAD_BUFFER_GUARD) { | ||
333 | stream->next_frame = ptr; | ||
334 | |||
335 | stream->error = MAD_ERROR_BUFLEN; | ||
336 | goto fail; | ||
337 | } | ||
338 | else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { | ||
339 | /* mark point where frame sync word was expected */ | ||
340 | stream->this_frame = ptr; | ||
341 | stream->next_frame = ptr + 1; | ||
342 | |||
343 | stream->error = MAD_ERROR_LOSTSYNC; | ||
344 | goto fail; | ||
345 | } | ||
346 | } | ||
347 | else { | ||
348 | mad_bit_init(&stream->ptr, ptr); | ||
349 | |||
350 | if (mad_stream_sync(stream) == -1) { | ||
351 | if (end - stream->next_frame >= MAD_BUFFER_GUARD) | ||
352 | stream->next_frame = end - MAD_BUFFER_GUARD; | ||
353 | |||
354 | stream->error = MAD_ERROR_BUFLEN; | ||
355 | goto fail; | ||
356 | } | ||
357 | |||
358 | ptr = mad_bit_nextbyte(&stream->ptr); | ||
359 | } | ||
360 | |||
361 | /* begin processing */ | ||
362 | stream->this_frame = ptr; | ||
363 | stream->next_frame = ptr + 1; /* possibly bogus sync word */ | ||
364 | |||
365 | mad_bit_init(&stream->ptr, stream->this_frame); | ||
366 | |||
367 | if (decode_header(header, stream) == -1) | ||
368 | goto fail; | ||
369 | |||
370 | /* calculate frame duration */ | ||
371 | mad_timer_set(&header->duration, 0, | ||
372 | 32 * MAD_NSBSAMPLES(header), header->samplerate); | ||
373 | |||
374 | /* calculate free bit rate */ | ||
375 | if (header->bitrate == 0) { | ||
376 | if ((stream->freerate == 0 || !stream->sync) && | ||
377 | free_bitrate(stream, header) == -1) | ||
378 | goto fail; | ||
379 | |||
380 | header->bitrate = stream->freerate; | ||
381 | header->flags |= MAD_FLAG_FREEFORMAT; | ||
382 | } | ||
383 | |||
384 | /* calculate beginning of next frame */ | ||
385 | pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; | ||
386 | |||
387 | if (header->layer == MAD_LAYER_I) | ||
388 | N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4; | ||
389 | else { | ||
390 | unsigned int slots_per_frame; | ||
391 | |||
392 | slots_per_frame = (header->layer == MAD_LAYER_III && | ||
393 | (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; | ||
394 | |||
395 | N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot; | ||
396 | } | ||
397 | |||
398 | /* verify there is enough data left in buffer to decode this frame */ | ||
399 | if (N + MAD_BUFFER_GUARD > end - stream->this_frame) { | ||
400 | stream->next_frame = stream->this_frame; | ||
401 | |||
402 | stream->error = MAD_ERROR_BUFLEN; | ||
403 | goto fail; | ||
404 | } | ||
405 | |||
406 | stream->next_frame = stream->this_frame + N; | ||
407 | |||
408 | if (!stream->sync) { | ||
409 | /* check that a valid frame header follows this frame */ | ||
410 | |||
411 | ptr = stream->next_frame; | ||
412 | if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { | ||
413 | ptr = stream->next_frame = stream->this_frame + 1; | ||
414 | goto sync; | ||
415 | } | ||
416 | |||
417 | stream->sync = 1; | ||
418 | } | ||
419 | |||
420 | header->flags |= MAD_FLAG_INCOMPLETE; | ||
421 | |||
422 | return 0; | ||
423 | |||
424 | fail: | ||
425 | stream->sync = 0; | ||
426 | |||
427 | return -1; | ||
428 | } | ||
429 | |||
430 | /* | ||
431 | * NAME: frame->decode() | ||
432 | * DESCRIPTION: decode a single frame from a bitstream | ||
433 | */ | ||
434 | int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream) | ||
435 | { | ||
436 | frame->options = stream->options; | ||
437 | |||
438 | /* header() */ | ||
439 | /* error_check() */ | ||
440 | |||
441 | if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) && | ||
442 | mad_header_decode(&frame->header, stream) == -1) | ||
443 | goto fail; | ||
444 | |||
445 | /* audio_data() */ | ||
446 | |||
447 | frame->header.flags &= ~MAD_FLAG_INCOMPLETE; | ||
448 | |||
449 | if (decoder_table[frame->header.layer - 1](stream, frame) == -1) { | ||
450 | if (!MAD_RECOVERABLE(stream->error)) | ||
451 | stream->next_frame = stream->this_frame; | ||
452 | |||
453 | goto fail; | ||
454 | } | ||
455 | |||
456 | /* ancillary_data() */ | ||
457 | |||
458 | if (frame->header.layer != MAD_LAYER_III) { | ||
459 | struct mad_bitptr next_frame; | ||
460 | |||
461 | mad_bit_init(&next_frame, stream->next_frame); | ||
462 | |||
463 | stream->anc_ptr = stream->ptr; | ||
464 | stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame); | ||
465 | |||
466 | mad_bit_finish(&next_frame); | ||
467 | } | ||
468 | |||
469 | return 0; | ||
470 | |||
471 | fail: | ||
472 | stream->anc_bitlen = 0; | ||
473 | return -1; | ||
474 | } | ||
475 | |||
476 | /* | ||
477 | * NAME: frame->mute() | ||
478 | * DESCRIPTION: zero all subband values so the frame becomes silent | ||
479 | */ | ||
480 | void mad_frame_mute(struct mad_frame *frame) | ||
481 | { | ||
482 | unsigned int s, sb; | ||
483 | |||
484 | for (s = 0; s < 36; ++s) { | ||
485 | for (sb = 0; sb < 32; ++sb) { | ||
486 | frame->sbsample[0][s][sb] = | ||
487 | frame->sbsample[1][s][sb] = 0; | ||
488 | } | ||
489 | } | ||
490 | |||
491 | if (frame->overlap) { | ||
492 | for (s = 0; s < 18; ++s) { | ||
493 | for (sb = 0; sb < 32; ++sb) { | ||
494 | (*frame->overlap)[0][sb][s] = | ||
495 | (*frame->overlap)[1][sb][s] = 0; | ||
496 | } | ||
497 | } | ||
498 | } | ||
499 | } | ||