f5e0cf8d32d81da110be08de6d25f3bb4a235db2
[kugel-rb.git] / apps / codecs / libmad / frame.c
blobf5e0cf8d32d81da110be08de6d25f3bb4a235db2
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
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.
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.
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
19 * $Id$
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
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"
36 static
37 unsigned 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 },
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 */
53 static
54 unsigned int const samplerate_table[3] = { 44100, 48000, 32000 };
56 static
57 int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = {
58 mad_layer_I,
59 mad_layer_II,
60 mad_layer_III
64 * NAME: header->init()
65 * DESCRIPTION: initialize header struct
67 void mad_header_init(struct mad_header *header)
69 header->layer = 0;
70 header->mode = 0;
71 header->mode_extension = 0;
72 header->emphasis = 0;
74 header->bitrate = 0;
75 header->samplerate = 0;
77 header->crc_check = 0;
78 header->crc_target = 0;
80 header->flags = 0;
81 header->private_bits = 0;
83 header->duration = mad_timer_zero;
87 * NAME: frame->init()
88 * DESCRIPTION: initialize frame struct
90 void mad_frame_init(struct mad_frame *frame)
92 mad_header_init(&frame->header);
94 frame->options = 0;
95 /* rockbox: comment this to proper zero this array in mad_frame_mute(). overlap
96 * is linked to an array in rockbox' apps/codecs/mpa.c before calling this.
97 frame->overlap = 0;
99 mad_frame_mute(frame);
103 * NAME: frame->finish()
104 * DESCRIPTION: deallocate any dynamic memory associated with frame
106 /* rockbox: unused
107 void mad_frame_finish(struct mad_frame *frame)
109 mad_header_finish(&frame->header);
111 if (frame->overlap) {
112 free(frame->overlap);
113 frame->overlap = 0;
119 * NAME: decode_header()
120 * DESCRIPTION: read header data and following CRC word
122 static
123 int decode_header(struct mad_header *header, struct mad_stream *stream)
125 unsigned int index;
127 header->flags = 0;
128 header->private_bits = 0;
130 /* header() */
132 /* syncword */
133 mad_bit_skip(&stream->ptr, 11);
135 /* MPEG 2.5 indicator (really part of syncword) */
136 if (mad_bit_read(&stream->ptr, 1) == 0)
137 header->flags |= MAD_FLAG_MPEG_2_5_EXT;
139 /* ID */
140 if (mad_bit_read(&stream->ptr, 1) == 0)
141 header->flags |= MAD_FLAG_LSF_EXT;
142 else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) {
143 stream->error = MAD_ERROR_LOSTSYNC;
144 return -1;
147 /* layer */
148 header->layer = 4 - mad_bit_read(&stream->ptr, 2);
150 if (header->layer == 4) {
151 stream->error = MAD_ERROR_BADLAYER;
152 return -1;
155 /* protection_bit */
156 if (mad_bit_read(&stream->ptr, 1) == 0) {
157 header->flags |= MAD_FLAG_PROTECTION;
158 header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff);
161 /* bitrate_index */
162 index = mad_bit_read(&stream->ptr, 4);
164 if (index == 15) {
165 stream->error = MAD_ERROR_BADBITRATE;
166 return -1;
169 if (header->flags & MAD_FLAG_LSF_EXT)
170 header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
171 else
172 header->bitrate = bitrate_table[header->layer - 1][index];
174 /* sampling_frequency */
175 index = mad_bit_read(&stream->ptr, 2);
177 if (index == 3) {
178 stream->error = MAD_ERROR_BADSAMPLERATE;
179 return -1;
182 header->samplerate = samplerate_table[index];
184 if (header->flags & MAD_FLAG_LSF_EXT) {
185 header->samplerate /= 2;
187 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
188 header->samplerate /= 2;
191 /* padding_bit */
192 if (mad_bit_read(&stream->ptr, 1))
193 header->flags |= MAD_FLAG_PADDING;
195 /* private_bit */
196 if (mad_bit_read(&stream->ptr, 1))
197 header->private_bits |= MAD_PRIVATE_HEADER;
199 /* mode */
200 header->mode = 3 - mad_bit_read(&stream->ptr, 2);
202 /* mode_extension */
203 header->mode_extension = mad_bit_read(&stream->ptr, 2);
205 /* copyright */
206 if (mad_bit_read(&stream->ptr, 1))
207 header->flags |= MAD_FLAG_COPYRIGHT;
209 /* original/copy */
210 if (mad_bit_read(&stream->ptr, 1))
211 header->flags |= MAD_FLAG_ORIGINAL;
213 /* emphasis */
214 header->emphasis = mad_bit_read(&stream->ptr, 2);
216 # if defined(OPT_STRICT)
218 * ISO/IEC 11172-3 says this is a reserved emphasis value, but
219 * streams exist which use it anyway. Since the value is not important
220 * to the decoder proper, we allow it unless OPT_STRICT is defined.
222 if (header->emphasis == MAD_EMPHASIS_RESERVED) {
223 stream->error = MAD_ERROR_BADEMPHASIS;
224 return -1;
226 # endif
228 /* error_check() */
230 /* crc_check */
231 if (header->flags & MAD_FLAG_PROTECTION)
232 header->crc_target = mad_bit_read(&stream->ptr, 16);
234 return 0;
238 * NAME: free_bitrate()
239 * DESCRIPTION: attempt to discover the bitstream's free bitrate
241 static
242 int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
244 struct mad_bitptr keep_ptr;
245 unsigned long rate = 0;
246 unsigned int pad_slot, slots_per_frame;
247 unsigned char const *ptr = 0;
249 keep_ptr = stream->ptr;
251 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
252 slots_per_frame = (header->layer == MAD_LAYER_III &&
253 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
255 while (mad_stream_sync(stream) == 0) {
256 struct mad_stream peek_stream;
257 struct mad_header peek_header;
259 peek_stream = *stream;
260 peek_header = *header;
262 if (decode_header(&peek_header, &peek_stream) == 0 &&
263 peek_header.layer == header->layer &&
264 peek_header.samplerate == header->samplerate) {
265 unsigned int N;
267 ptr = mad_bit_nextbyte(&stream->ptr);
269 N = ptr - stream->this_frame;
271 if (header->layer == MAD_LAYER_I) {
272 rate = (unsigned long) header->samplerate *
273 (N - 4 * pad_slot + 4) / 48 / 1000;
275 else {
276 rate = (unsigned long) header->samplerate *
277 (N - pad_slot + 1) / slots_per_frame / 1000;
280 if (rate >= 8)
281 break;
284 mad_bit_skip(&stream->ptr, 8);
287 stream->ptr = keep_ptr;
289 if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
290 stream->error = MAD_ERROR_LOSTSYNC;
291 return -1;
294 stream->freerate = rate * 1000;
296 return 0;
300 * NAME: header->decode()
301 * DESCRIPTION: read the next frame header from the stream
303 int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
305 register unsigned char const *ptr, *end;
306 unsigned int pad_slot, N;
308 ptr = stream->next_frame;
309 end = stream->bufend;
311 if (ptr == 0) {
312 stream->error = MAD_ERROR_BUFPTR;
313 goto fail;
316 /* stream skip */
317 if (stream->skiplen) {
318 if (!stream->sync)
319 ptr = stream->this_frame;
321 if (end - ptr < (long) stream->skiplen) {
322 stream->skiplen -= end - ptr;
323 stream->next_frame = end;
325 stream->error = MAD_ERROR_BUFLEN;
326 goto fail;
329 ptr += stream->skiplen;
330 stream->skiplen = 0;
332 stream->sync = 1;
335 sync:
336 /* synchronize */
337 if (stream->sync) {
338 if (end - ptr < MAD_BUFFER_GUARD) {
339 stream->next_frame = ptr;
341 stream->error = MAD_ERROR_BUFLEN;
342 goto fail;
344 else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
345 /* mark point where frame sync word was expected */
346 stream->this_frame = ptr;
347 stream->next_frame = ptr + 1;
349 stream->error = MAD_ERROR_LOSTSYNC;
350 goto fail;
353 else {
354 mad_bit_init(&stream->ptr, ptr);
356 if (mad_stream_sync(stream) == -1) {
357 if (end - stream->next_frame >= MAD_BUFFER_GUARD)
358 stream->next_frame = end - MAD_BUFFER_GUARD;
360 stream->error = MAD_ERROR_BUFLEN;
361 goto fail;
364 ptr = mad_bit_nextbyte(&stream->ptr);
367 /* begin processing */
368 stream->this_frame = ptr;
369 stream->next_frame = ptr + 1; /* possibly bogus sync word */
371 mad_bit_init(&stream->ptr, stream->this_frame);
373 if (decode_header(header, stream) == -1)
374 goto fail;
376 /* calculate frame duration */
377 mad_timer_set(&header->duration, 0,
378 32 * MAD_NSBSAMPLES(header), header->samplerate);
380 /* calculate free bit rate */
381 if (header->bitrate == 0) {
382 if ((stream->freerate == 0 || !stream->sync ||
383 (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
384 free_bitrate(stream, header) == -1)
385 goto fail;
387 header->bitrate = stream->freerate;
388 header->flags |= MAD_FLAG_FREEFORMAT;
391 /* calculate beginning of next frame */
392 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
394 if (header->layer == MAD_LAYER_I)
395 N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
396 else {
397 unsigned int slots_per_frame;
399 slots_per_frame = (header->layer == MAD_LAYER_III &&
400 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
402 N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
405 /* verify there is enough data left in buffer to decode this frame */
406 if ((long)(N + MAD_BUFFER_GUARD) > end - stream->this_frame) {
407 stream->next_frame = stream->this_frame;
409 stream->error = MAD_ERROR_BUFLEN;
410 goto fail;
413 stream->next_frame = stream->this_frame + N;
415 if (!stream->sync) {
416 /* check that a valid frame header follows this frame */
418 ptr = stream->next_frame;
419 if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
420 ptr = stream->next_frame = stream->this_frame + 1;
421 goto sync;
424 stream->sync = 1;
427 header->flags |= MAD_FLAG_INCOMPLETE;
429 return 0;
431 fail:
432 stream->sync = 0;
434 return -1;
438 * NAME: frame->decode()
439 * DESCRIPTION: decode a single frame from a bitstream
441 int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
443 frame->options = stream->options;
445 /* header() */
446 /* error_check() */
448 if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
449 mad_header_decode(&frame->header, stream) == -1)
450 goto fail;
452 /* audio_data() */
454 frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
456 if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
457 if (!MAD_RECOVERABLE(stream->error))
458 stream->next_frame = stream->this_frame;
460 goto fail;
463 /* ancillary_data() */
465 if (frame->header.layer != MAD_LAYER_III) {
466 struct mad_bitptr next_frame;
468 mad_bit_init(&next_frame, stream->next_frame);
470 stream->anc_ptr = stream->ptr;
471 stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
473 mad_bit_finish(&next_frame);
478 return 0;
480 fail:
481 stream->anc_bitlen = 0;
482 return -1;
486 * NAME: frame->mute()
487 * DESCRIPTION: zero all subband values so the frame becomes silent
489 void mad_frame_mute(struct mad_frame *frame)
491 memset((*frame->sbsample_prev), 0, sizeof(*frame->sbsample_prev));
492 memset((*frame->sbsample) , 0, sizeof(*frame->sbsample));
493 memset((*frame->overlap) , 0, sizeof(*frame->overlap));