Comment further unused code in libmad.
[kugel-rb.git] / apps / codecs / libmad / frame.c
blob0db5c5aa4440f4b28dc220ee910bd7961ac57947
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;
82 /* rockbox: not used
83 header->duration = mad_timer_zero;
88 * NAME: frame->init()
89 * DESCRIPTION: initialize frame struct
91 void mad_frame_init(struct mad_frame *frame)
93 mad_header_init(&frame->header);
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 rockbox' apps/codecs/mpa.c before calling this.
98 frame->overlap = 0;
100 mad_frame_mute(frame);
104 * NAME: frame->finish()
105 * DESCRIPTION: deallocate any dynamic memory associated with frame
107 /* rockbox: unused
108 void mad_frame_finish(struct mad_frame *frame)
110 mad_header_finish(&frame->header);
112 if (frame->overlap) {
113 free(frame->overlap);
114 frame->overlap = 0;
120 * NAME: decode_header()
121 * DESCRIPTION: read header data and following CRC word
123 static
124 int decode_header(struct mad_header *header, struct mad_stream *stream)
126 unsigned int index;
128 header->flags = 0;
129 header->private_bits = 0;
131 /* header() */
133 /* syncword */
134 mad_bit_skip(&stream->ptr, 11);
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;
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;
148 /* layer */
149 header->layer = 4 - mad_bit_read(&stream->ptr, 2);
151 if (header->layer == 4) {
152 stream->error = MAD_ERROR_BADLAYER;
153 return -1;
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);
162 /* bitrate_index */
163 index = mad_bit_read(&stream->ptr, 4);
165 if (index == 15) {
166 stream->error = MAD_ERROR_BADBITRATE;
167 return -1;
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];
175 /* sampling_frequency */
176 index = mad_bit_read(&stream->ptr, 2);
178 if (index == 3) {
179 stream->error = MAD_ERROR_BADSAMPLERATE;
180 return -1;
183 header->samplerate = samplerate_table[index];
185 if (header->flags & MAD_FLAG_LSF_EXT) {
186 header->samplerate /= 2;
188 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
189 header->samplerate /= 2;
192 /* padding_bit */
193 if (mad_bit_read(&stream->ptr, 1))
194 header->flags |= MAD_FLAG_PADDING;
196 /* private_bit */
197 if (mad_bit_read(&stream->ptr, 1))
198 header->private_bits |= MAD_PRIVATE_HEADER;
200 /* mode */
201 header->mode = 3 - mad_bit_read(&stream->ptr, 2);
203 /* mode_extension */
204 header->mode_extension = mad_bit_read(&stream->ptr, 2);
206 /* copyright */
207 if (mad_bit_read(&stream->ptr, 1))
208 header->flags |= MAD_FLAG_COPYRIGHT;
210 /* original/copy */
211 if (mad_bit_read(&stream->ptr, 1))
212 header->flags |= MAD_FLAG_ORIGINAL;
214 /* emphasis */
215 header->emphasis = mad_bit_read(&stream->ptr, 2);
217 # if defined(OPT_STRICT)
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.
223 if (header->emphasis == MAD_EMPHASIS_RESERVED) {
224 stream->error = MAD_ERROR_BADEMPHASIS;
225 return -1;
227 # endif
229 /* error_check() */
231 /* crc_check */
232 if (header->flags & MAD_FLAG_PROTECTION)
233 header->crc_target = mad_bit_read(&stream->ptr, 16);
235 return 0;
239 * NAME: free_bitrate()
240 * DESCRIPTION: attempt to discover the bitstream's free bitrate
242 static
243 int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
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;
250 keep_ptr = stream->ptr;
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;
256 while (mad_stream_sync(stream) == 0) {
257 struct mad_stream peek_stream;
258 struct mad_header peek_header;
260 peek_stream = *stream;
261 peek_header = *header;
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;
268 ptr = mad_bit_nextbyte(&stream->ptr);
270 N = ptr - stream->this_frame;
272 if (header->layer == MAD_LAYER_I) {
273 rate = (unsigned long) header->samplerate *
274 (N - 4 * pad_slot + 4) / 48 / 1000;
276 else {
277 rate = (unsigned long) header->samplerate *
278 (N - pad_slot + 1) / slots_per_frame / 1000;
281 if (rate >= 8)
282 break;
285 mad_bit_skip(&stream->ptr, 8);
288 stream->ptr = keep_ptr;
290 if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
291 stream->error = MAD_ERROR_LOSTSYNC;
292 return -1;
295 stream->freerate = rate * 1000;
297 return 0;
301 * NAME: header->decode()
302 * DESCRIPTION: read the next frame header from the stream
304 int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
306 register unsigned char const *ptr, *end;
307 unsigned int pad_slot, N;
309 ptr = stream->next_frame;
310 end = stream->bufend;
312 if (ptr == 0) {
313 stream->error = MAD_ERROR_BUFPTR;
314 goto fail;
317 /* stream skip */
318 if (stream->skiplen) {
319 if (!stream->sync)
320 ptr = stream->this_frame;
322 if (end - ptr < (long) stream->skiplen) {
323 stream->skiplen -= end - ptr;
324 stream->next_frame = end;
326 stream->error = MAD_ERROR_BUFLEN;
327 goto fail;
330 ptr += stream->skiplen;
331 stream->skiplen = 0;
333 stream->sync = 1;
336 sync:
337 /* synchronize */
338 if (stream->sync) {
339 if (end - ptr < MAD_BUFFER_GUARD) {
340 stream->next_frame = ptr;
342 stream->error = MAD_ERROR_BUFLEN;
343 goto fail;
345 else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
346 /* mark point where frame sync word was expected */
347 stream->this_frame = ptr;
348 stream->next_frame = ptr + 1;
350 stream->error = MAD_ERROR_LOSTSYNC;
351 goto fail;
354 else {
355 mad_bit_init(&stream->ptr, ptr);
357 if (mad_stream_sync(stream) == -1) {
358 if (end - stream->next_frame >= MAD_BUFFER_GUARD)
359 stream->next_frame = end - MAD_BUFFER_GUARD;
361 stream->error = MAD_ERROR_BUFLEN;
362 goto fail;
365 ptr = mad_bit_nextbyte(&stream->ptr);
368 /* begin processing */
369 stream->this_frame = ptr;
370 stream->next_frame = ptr + 1; /* possibly bogus sync word */
372 mad_bit_init(&stream->ptr, stream->this_frame);
374 if (decode_header(header, stream) == -1)
375 goto fail;
377 /* calculate frame duration */
378 /* rockbox: not used
379 mad_timer_set(&header->duration, 0,
380 32 * MAD_NSBSAMPLES(header), header->samplerate);
383 /* calculate free bit rate */
384 if (header->bitrate == 0) {
385 if ((stream->freerate == 0 || !stream->sync ||
386 (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
387 free_bitrate(stream, header) == -1)
388 goto fail;
390 header->bitrate = stream->freerate;
391 header->flags |= MAD_FLAG_FREEFORMAT;
394 /* calculate beginning of next frame */
395 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
397 if (header->layer == MAD_LAYER_I)
398 N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
399 else {
400 unsigned int slots_per_frame;
402 slots_per_frame = (header->layer == MAD_LAYER_III &&
403 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
405 N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
408 /* verify there is enough data left in buffer to decode this frame */
409 if ((long)(N + MAD_BUFFER_GUARD) > end - stream->this_frame) {
410 stream->next_frame = stream->this_frame;
412 stream->error = MAD_ERROR_BUFLEN;
413 goto fail;
416 stream->next_frame = stream->this_frame + N;
418 if (!stream->sync) {
419 /* check that a valid frame header follows this frame */
421 ptr = stream->next_frame;
422 if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
423 ptr = stream->next_frame = stream->this_frame + 1;
424 goto sync;
427 stream->sync = 1;
430 header->flags |= MAD_FLAG_INCOMPLETE;
432 return 0;
434 fail:
435 stream->sync = 0;
437 return -1;
441 * NAME: frame->decode()
442 * DESCRIPTION: decode a single frame from a bitstream
444 int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
446 frame->options = stream->options;
448 /* header() */
449 /* error_check() */
451 if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
452 mad_header_decode(&frame->header, stream) == -1)
453 goto fail;
455 /* audio_data() */
457 frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
459 if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
460 if (!MAD_RECOVERABLE(stream->error))
461 stream->next_frame = stream->this_frame;
463 goto fail;
466 /* ancillary_data() */
468 if (frame->header.layer != MAD_LAYER_III) {
469 struct mad_bitptr next_frame;
471 mad_bit_init(&next_frame, stream->next_frame);
473 stream->anc_ptr = stream->ptr;
474 stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
476 mad_bit_finish(&next_frame);
481 return 0;
483 fail:
484 stream->anc_bitlen = 0;
485 return -1;
489 * NAME: frame->mute()
490 * DESCRIPTION: zero all subband values so the frame becomes silent
492 void mad_frame_mute(struct mad_frame *frame)
494 memset((*frame->sbsample_prev), 0, sizeof(*frame->sbsample_prev));
495 memset((*frame->sbsample) , 0, sizeof(*frame->sbsample));
496 memset((*frame->overlap) , 0, sizeof(*frame->overlap));