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: frame.c,v 1.29 2004/02/04 22:59:19 rob Exp $
39 unsigned long const bitrate_table[5][15] = {
41 { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */
42 256000, 288000, 320000, 352000, 384000, 416000, 448000 },
43 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */
44 128000, 160000, 192000, 224000, 256000, 320000, 384000 },
45 { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */
46 112000, 128000, 160000, 192000, 224000, 256000, 320000 },
49 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */
50 128000, 144000, 160000, 176000, 192000, 224000, 256000 },
51 { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */
52 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */
56 unsigned int const samplerate_table[3] = { 44100, 48000, 32000 };
59 int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = {
66 * NAME: header->init()
67 * DESCRIPTION: initialize header struct
69 void mad_header_init(struct mad_header *header)
73 header->mode_extension = 0;
77 header->samplerate = 0;
79 header->crc_check = 0;
80 header->crc_target = 0;
83 header->private_bits = 0;
85 header->duration = mad_timer_zero;
90 * DESCRIPTION: initialize frame struct
92 void mad_frame_init(struct mad_frame *frame)
94 mad_header_init(&frame->header);
99 mad_frame_mute(frame);
103 * NAME: frame->finish()
104 * DESCRIPTION: deallocate any dynamic memory associated with frame
106 void mad_frame_finish(struct mad_frame *frame)
108 mad_header_finish(&frame->header);
110 if (frame->overlap) {
111 free(frame->overlap);
117 * NAME: decode_header()
118 * DESCRIPTION: read header data and following CRC word
121 int decode_header(struct mad_header *header, struct mad_stream *stream)
126 header->private_bits = 0;
131 mad_bit_skip(&stream->ptr, 11);
133 /* MPEG 2.5 indicator (really part of syncword) */
134 if (mad_bit_read(&stream->ptr, 1) == 0)
135 header->flags |= MAD_FLAG_MPEG_2_5_EXT;
138 if (mad_bit_read(&stream->ptr, 1) == 0)
139 header->flags |= MAD_FLAG_LSF_EXT;
140 else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) {
141 stream->error = MAD_ERROR_LOSTSYNC;
146 header->layer = 4 - mad_bit_read(&stream->ptr, 2);
148 if (header->layer == 4) {
149 stream->error = MAD_ERROR_BADLAYER;
154 if (mad_bit_read(&stream->ptr, 1) == 0) {
155 header->flags |= MAD_FLAG_PROTECTION;
156 header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff);
160 index = mad_bit_read(&stream->ptr, 4);
163 stream->error = MAD_ERROR_BADBITRATE;
167 if (header->flags & MAD_FLAG_LSF_EXT)
168 header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
170 header->bitrate = bitrate_table[header->layer - 1][index];
172 /* sampling_frequency */
173 index = mad_bit_read(&stream->ptr, 2);
176 stream->error = MAD_ERROR_BADSAMPLERATE;
180 header->samplerate = samplerate_table[index];
182 if (header->flags & MAD_FLAG_LSF_EXT) {
183 header->samplerate /= 2;
185 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
186 header->samplerate /= 2;
190 if (mad_bit_read(&stream->ptr, 1))
191 header->flags |= MAD_FLAG_PADDING;
194 if (mad_bit_read(&stream->ptr, 1))
195 header->private_bits |= MAD_PRIVATE_HEADER;
198 header->mode = 3 - mad_bit_read(&stream->ptr, 2);
201 header->mode_extension = mad_bit_read(&stream->ptr, 2);
204 if (mad_bit_read(&stream->ptr, 1))
205 header->flags |= MAD_FLAG_COPYRIGHT;
208 if (mad_bit_read(&stream->ptr, 1))
209 header->flags |= MAD_FLAG_ORIGINAL;
212 header->emphasis = mad_bit_read(&stream->ptr, 2);
214 # if defined(OPT_STRICT)
216 * ISO/IEC 11172-3 says this is a reserved emphasis value, but
217 * streams exist which use it anyway. Since the value is not important
218 * to the decoder proper, we allow it unless OPT_STRICT is defined.
220 if (header->emphasis == MAD_EMPHASIS_RESERVED) {
221 stream->error = MAD_ERROR_BADEMPHASIS;
229 if (header->flags & MAD_FLAG_PROTECTION)
230 header->crc_target = mad_bit_read(&stream->ptr, 16);
236 * NAME: free_bitrate()
237 * DESCRIPTION: attempt to discover the bitstream's free bitrate
240 int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
242 struct mad_bitptr keep_ptr;
243 unsigned long rate = 0;
244 unsigned int pad_slot, slots_per_frame;
245 unsigned char const *ptr = 0;
247 keep_ptr = stream->ptr;
249 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
250 slots_per_frame = (header->layer == MAD_LAYER_III &&
251 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
253 while (mad_stream_sync(stream) == 0) {
254 struct mad_stream peek_stream;
255 struct mad_header peek_header;
257 peek_stream = *stream;
258 peek_header = *header;
260 if (decode_header(&peek_header, &peek_stream) == 0 &&
261 peek_header.layer == header->layer &&
262 peek_header.samplerate == header->samplerate) {
265 ptr = mad_bit_nextbyte(&stream->ptr);
267 N = ptr - stream->this_frame;
269 if (header->layer == MAD_LAYER_I) {
270 rate = (unsigned long) header->samplerate *
271 (N - 4 * pad_slot + 4) / 48 / 1000;
274 rate = (unsigned long) header->samplerate *
275 (N - pad_slot + 1) / slots_per_frame / 1000;
282 mad_bit_skip(&stream->ptr, 8);
285 stream->ptr = keep_ptr;
287 if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
288 stream->error = MAD_ERROR_LOSTSYNC;
292 stream->freerate = rate * 1000;
298 * NAME: header->decode()
299 * DESCRIPTION: read the next frame header from the stream
301 int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
303 register unsigned char const *ptr, *end;
304 unsigned int pad_slot, N;
306 ptr = stream->next_frame;
307 end = stream->bufend;
310 stream->error = MAD_ERROR_BUFPTR;
315 if (stream->skiplen) {
317 ptr = stream->this_frame;
319 if (end - ptr < stream->skiplen) {
320 stream->skiplen -= end - ptr;
321 stream->next_frame = end;
323 stream->error = MAD_ERROR_BUFLEN;
327 ptr += stream->skiplen;
336 if (end - ptr < MAD_BUFFER_GUARD) {
337 stream->next_frame = ptr;
339 stream->error = MAD_ERROR_BUFLEN;
342 else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
343 /* mark point where frame sync word was expected */
344 stream->this_frame = ptr;
345 stream->next_frame = ptr + 1;
347 stream->error = MAD_ERROR_LOSTSYNC;
352 mad_bit_init(&stream->ptr, ptr);
354 if (mad_stream_sync(stream) == -1) {
355 if (end - stream->next_frame >= MAD_BUFFER_GUARD)
356 stream->next_frame = end - MAD_BUFFER_GUARD;
358 stream->error = MAD_ERROR_BUFLEN;
362 ptr = mad_bit_nextbyte(&stream->ptr);
365 /* begin processing */
366 stream->this_frame = ptr;
367 stream->next_frame = ptr + 1; /* possibly bogus sync word */
369 mad_bit_init(&stream->ptr, stream->this_frame);
371 if (decode_header(header, stream) == -1)
374 /* calculate frame duration */
375 mad_timer_set(&header->duration, 0,
376 32 * MAD_NSBSAMPLES(header), header->samplerate);
378 /* calculate free bit rate */
379 if (header->bitrate == 0) {
380 if ((stream->freerate == 0 || !stream->sync ||
381 (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
382 free_bitrate(stream, header) == -1)
385 header->bitrate = stream->freerate;
386 header->flags |= MAD_FLAG_FREEFORMAT;
389 /* calculate beginning of next frame */
390 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
392 if (header->layer == MAD_LAYER_I)
393 N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
395 unsigned int slots_per_frame;
397 slots_per_frame = (header->layer == MAD_LAYER_III &&
398 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
400 N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
403 /* verify there is enough data left in buffer to decode this frame */
404 if (N + MAD_BUFFER_GUARD > end - stream->this_frame) {
405 stream->next_frame = stream->this_frame;
407 stream->error = MAD_ERROR_BUFLEN;
411 stream->next_frame = stream->this_frame + N;
414 /* check that a valid frame header follows this frame */
416 ptr = stream->next_frame;
417 if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
418 ptr = stream->next_frame = stream->this_frame + 1;
425 header->flags |= MAD_FLAG_INCOMPLETE;
436 * NAME: frame->decode()
437 * DESCRIPTION: decode a single frame from a bitstream
439 int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
441 frame->options = stream->options;
446 if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
447 mad_header_decode(&frame->header, stream) == -1)
452 frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
454 if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
455 if (!MAD_RECOVERABLE(stream->error))
456 stream->next_frame = stream->this_frame;
461 /* ancillary_data() */
463 if (frame->header.layer != MAD_LAYER_III) {
464 struct mad_bitptr next_frame;
466 mad_bit_init(&next_frame, stream->next_frame);
468 stream->anc_ptr = stream->ptr;
469 stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
471 mad_bit_finish(&next_frame);
477 stream->anc_bitlen = 0;
482 * NAME: frame->mute()
483 * DESCRIPTION: zero all subband values so the frame becomes silent
485 void mad_frame_mute(struct mad_frame *frame)
489 for (s = 0; s < 36; ++s) {
490 for (sb = 0; sb < 32; ++sb) {
491 frame->sbsample[0][s][sb] =
492 frame->sbsample[1][s][sb] = 0;
496 if (frame->overlap) {
497 for (s = 0; s < 18; ++s) {
498 for (sb = 0; sb < 32; ++sb) {
499 (*frame->overlap)[0][sb][s] =
500 (*frame->overlap)[1][sb][s] = 0;