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