Chris@0: /* Chris@0: * libmad - MPEG audio decoder library Chris@0: * Copyright (C) 2000-2004 Underbit Technologies, Inc. Chris@0: * Chris@0: * This program is free software; you can redistribute it and/or modify Chris@0: * it under the terms of the GNU General Public License as published by Chris@0: * the Free Software Foundation; either version 2 of the License, or Chris@0: * (at your option) any later version. Chris@0: * Chris@0: * This program is distributed in the hope that it will be useful, Chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: * GNU General Public License for more details. Chris@0: * Chris@0: * You should have received a copy of the GNU General Public License Chris@0: * along with this program; if not, write to the Free Software Chris@0: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: * Chris@0: * $Id: frame.c,v 1.29 2004/02/04 22:59:19 rob Exp $ Chris@0: */ Chris@0: Chris@0: # ifdef HAVE_CONFIG_H Chris@0: # include "config.h" Chris@0: # endif Chris@0: Chris@0: # include "global.h" Chris@0: Chris@0: # include Chris@0: Chris@0: # include "bit.h" Chris@0: # include "stream.h" Chris@0: # include "frame.h" Chris@0: # include "timer.h" Chris@0: # include "layer12.h" Chris@0: # include "layer3.h" Chris@0: Chris@0: static Chris@0: unsigned long const bitrate_table[5][15] = { Chris@0: /* MPEG-1 */ Chris@0: { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */ Chris@0: 256000, 288000, 320000, 352000, 384000, 416000, 448000 }, Chris@0: { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */ Chris@0: 128000, 160000, 192000, 224000, 256000, 320000, 384000 }, Chris@0: { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */ Chris@0: 112000, 128000, 160000, 192000, 224000, 256000, 320000 }, Chris@0: Chris@0: /* MPEG-2 LSF */ Chris@0: { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */ Chris@0: 128000, 144000, 160000, 176000, 192000, 224000, 256000 }, Chris@0: { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */ Chris@0: 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */ Chris@0: }; Chris@0: Chris@0: static Chris@0: unsigned int const samplerate_table[3] = { 44100, 48000, 32000 }; Chris@0: Chris@0: static Chris@0: int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = { Chris@0: mad_layer_I, Chris@0: mad_layer_II, Chris@0: mad_layer_III Chris@0: }; Chris@0: Chris@0: /* Chris@0: * NAME: header->init() Chris@0: * DESCRIPTION: initialize header struct Chris@0: */ Chris@0: void mad_header_init(struct mad_header *header) Chris@0: { Chris@0: header->layer = 0; Chris@0: header->mode = 0; Chris@0: header->mode_extension = 0; Chris@0: header->emphasis = 0; Chris@0: Chris@0: header->bitrate = 0; Chris@0: header->samplerate = 0; Chris@0: Chris@0: header->crc_check = 0; Chris@0: header->crc_target = 0; Chris@0: Chris@0: header->flags = 0; Chris@0: header->private_bits = 0; Chris@0: Chris@0: header->duration = mad_timer_zero; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: frame->init() Chris@0: * DESCRIPTION: initialize frame struct Chris@0: */ Chris@0: void mad_frame_init(struct mad_frame *frame) Chris@0: { Chris@0: mad_header_init(&frame->header); Chris@0: Chris@0: frame->options = 0; Chris@0: Chris@0: frame->overlap = 0; Chris@0: mad_frame_mute(frame); Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: frame->finish() Chris@0: * DESCRIPTION: deallocate any dynamic memory associated with frame Chris@0: */ Chris@0: void mad_frame_finish(struct mad_frame *frame) Chris@0: { Chris@0: mad_header_finish(&frame->header); Chris@0: Chris@0: if (frame->overlap) { Chris@0: free(frame->overlap); Chris@0: frame->overlap = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: decode_header() Chris@0: * DESCRIPTION: read header data and following CRC word Chris@0: */ Chris@0: static Chris@0: int decode_header(struct mad_header *header, struct mad_stream *stream) Chris@0: { Chris@0: unsigned int index; Chris@0: Chris@0: header->flags = 0; Chris@0: header->private_bits = 0; Chris@0: Chris@0: /* header() */ Chris@0: Chris@0: /* syncword */ Chris@0: mad_bit_skip(&stream->ptr, 11); Chris@0: Chris@0: /* MPEG 2.5 indicator (really part of syncword) */ Chris@0: if (mad_bit_read(&stream->ptr, 1) == 0) Chris@0: header->flags |= MAD_FLAG_MPEG_2_5_EXT; Chris@0: Chris@0: /* ID */ Chris@0: if (mad_bit_read(&stream->ptr, 1) == 0) Chris@0: header->flags |= MAD_FLAG_LSF_EXT; Chris@0: else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) { Chris@0: stream->error = MAD_ERROR_LOSTSYNC; Chris@0: return -1; Chris@0: } Chris@0: Chris@0: /* layer */ Chris@0: header->layer = 4 - mad_bit_read(&stream->ptr, 2); Chris@0: Chris@0: if (header->layer == 4) { Chris@0: stream->error = MAD_ERROR_BADLAYER; Chris@0: return -1; Chris@0: } Chris@0: Chris@0: /* protection_bit */ Chris@0: if (mad_bit_read(&stream->ptr, 1) == 0) { Chris@0: header->flags |= MAD_FLAG_PROTECTION; Chris@0: header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff); Chris@0: } Chris@0: Chris@0: /* bitrate_index */ Chris@0: index = mad_bit_read(&stream->ptr, 4); Chris@0: Chris@0: if (index == 15) { Chris@0: stream->error = MAD_ERROR_BADBITRATE; Chris@0: return -1; Chris@0: } Chris@0: Chris@0: if (header->flags & MAD_FLAG_LSF_EXT) Chris@0: header->bitrate = bitrate_table[3 + (header->layer >> 1)][index]; Chris@0: else Chris@0: header->bitrate = bitrate_table[header->layer - 1][index]; Chris@0: Chris@0: /* sampling_frequency */ Chris@0: index = mad_bit_read(&stream->ptr, 2); Chris@0: Chris@0: if (index == 3) { Chris@0: stream->error = MAD_ERROR_BADSAMPLERATE; Chris@0: return -1; Chris@0: } Chris@0: Chris@0: header->samplerate = samplerate_table[index]; Chris@0: Chris@0: if (header->flags & MAD_FLAG_LSF_EXT) { Chris@0: header->samplerate /= 2; Chris@0: Chris@0: if (header->flags & MAD_FLAG_MPEG_2_5_EXT) Chris@0: header->samplerate /= 2; Chris@0: } Chris@0: Chris@0: /* padding_bit */ Chris@0: if (mad_bit_read(&stream->ptr, 1)) Chris@0: header->flags |= MAD_FLAG_PADDING; Chris@0: Chris@0: /* private_bit */ Chris@0: if (mad_bit_read(&stream->ptr, 1)) Chris@0: header->private_bits |= MAD_PRIVATE_HEADER; Chris@0: Chris@0: /* mode */ Chris@0: header->mode = 3 - mad_bit_read(&stream->ptr, 2); Chris@0: Chris@0: /* mode_extension */ Chris@0: header->mode_extension = mad_bit_read(&stream->ptr, 2); Chris@0: Chris@0: /* copyright */ Chris@0: if (mad_bit_read(&stream->ptr, 1)) Chris@0: header->flags |= MAD_FLAG_COPYRIGHT; Chris@0: Chris@0: /* original/copy */ Chris@0: if (mad_bit_read(&stream->ptr, 1)) Chris@0: header->flags |= MAD_FLAG_ORIGINAL; Chris@0: Chris@0: /* emphasis */ Chris@0: header->emphasis = mad_bit_read(&stream->ptr, 2); Chris@0: Chris@0: # if defined(OPT_STRICT) Chris@0: /* Chris@0: * ISO/IEC 11172-3 says this is a reserved emphasis value, but Chris@0: * streams exist which use it anyway. Since the value is not important Chris@0: * to the decoder proper, we allow it unless OPT_STRICT is defined. Chris@0: */ Chris@0: if (header->emphasis == MAD_EMPHASIS_RESERVED) { Chris@0: stream->error = MAD_ERROR_BADEMPHASIS; Chris@0: return -1; Chris@0: } Chris@0: # endif Chris@0: Chris@0: /* error_check() */ Chris@0: Chris@0: /* crc_check */ Chris@0: if (header->flags & MAD_FLAG_PROTECTION) Chris@0: header->crc_target = mad_bit_read(&stream->ptr, 16); Chris@0: Chris@0: return 0; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: free_bitrate() Chris@0: * DESCRIPTION: attempt to discover the bitstream's free bitrate Chris@0: */ Chris@0: static Chris@0: int free_bitrate(struct mad_stream *stream, struct mad_header const *header) Chris@0: { Chris@0: struct mad_bitptr keep_ptr; Chris@0: unsigned long rate = 0; Chris@0: unsigned int pad_slot, slots_per_frame; Chris@0: unsigned char const *ptr = 0; Chris@0: Chris@0: keep_ptr = stream->ptr; Chris@0: Chris@0: pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; Chris@0: slots_per_frame = (header->layer == MAD_LAYER_III && Chris@0: (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; Chris@0: Chris@0: while (mad_stream_sync(stream) == 0) { Chris@0: struct mad_stream peek_stream; Chris@0: struct mad_header peek_header; Chris@0: Chris@0: peek_stream = *stream; Chris@0: peek_header = *header; Chris@0: Chris@0: if (decode_header(&peek_header, &peek_stream) == 0 && Chris@0: peek_header.layer == header->layer && Chris@0: peek_header.samplerate == header->samplerate) { Chris@0: unsigned int N; Chris@0: Chris@0: ptr = mad_bit_nextbyte(&stream->ptr); Chris@0: Chris@0: N = ptr - stream->this_frame; Chris@0: Chris@0: if (header->layer == MAD_LAYER_I) { Chris@0: rate = (unsigned long) header->samplerate * Chris@0: (N - 4 * pad_slot + 4) / 48 / 1000; Chris@0: } Chris@0: else { Chris@0: rate = (unsigned long) header->samplerate * Chris@0: (N - pad_slot + 1) / slots_per_frame / 1000; Chris@0: } Chris@0: Chris@0: if (rate >= 8) Chris@0: break; Chris@0: } Chris@0: Chris@0: mad_bit_skip(&stream->ptr, 8); Chris@0: } Chris@0: Chris@0: stream->ptr = keep_ptr; Chris@0: Chris@0: if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) { Chris@0: stream->error = MAD_ERROR_LOSTSYNC; Chris@0: return -1; Chris@0: } Chris@0: Chris@0: stream->freerate = rate * 1000; Chris@0: Chris@0: return 0; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: header->decode() Chris@0: * DESCRIPTION: read the next frame header from the stream Chris@0: */ Chris@0: int mad_header_decode(struct mad_header *header, struct mad_stream *stream) Chris@0: { Chris@0: register unsigned char const *ptr, *end; Chris@0: unsigned int pad_slot, N; Chris@0: Chris@0: ptr = stream->next_frame; Chris@0: end = stream->bufend; Chris@0: Chris@0: if (ptr == 0) { Chris@0: stream->error = MAD_ERROR_BUFPTR; Chris@0: goto fail; Chris@0: } Chris@0: Chris@0: /* stream skip */ Chris@0: if (stream->skiplen) { Chris@0: if (!stream->sync) Chris@0: ptr = stream->this_frame; Chris@0: Chris@0: if (end - ptr < stream->skiplen) { Chris@0: stream->skiplen -= end - ptr; Chris@0: stream->next_frame = end; Chris@0: Chris@0: stream->error = MAD_ERROR_BUFLEN; Chris@0: goto fail; Chris@0: } Chris@0: Chris@0: ptr += stream->skiplen; Chris@0: stream->skiplen = 0; Chris@0: Chris@0: stream->sync = 1; Chris@0: } Chris@0: Chris@0: sync: Chris@0: /* synchronize */ Chris@0: if (stream->sync) { Chris@0: if (end - ptr < MAD_BUFFER_GUARD) { Chris@0: stream->next_frame = ptr; Chris@0: Chris@0: stream->error = MAD_ERROR_BUFLEN; Chris@0: goto fail; Chris@0: } Chris@0: else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { Chris@0: /* mark point where frame sync word was expected */ Chris@0: stream->this_frame = ptr; Chris@0: stream->next_frame = ptr + 1; Chris@0: Chris@0: stream->error = MAD_ERROR_LOSTSYNC; Chris@0: goto fail; Chris@0: } Chris@0: } Chris@0: else { Chris@0: mad_bit_init(&stream->ptr, ptr); Chris@0: Chris@0: if (mad_stream_sync(stream) == -1) { Chris@0: if (end - stream->next_frame >= MAD_BUFFER_GUARD) Chris@0: stream->next_frame = end - MAD_BUFFER_GUARD; Chris@0: Chris@0: stream->error = MAD_ERROR_BUFLEN; Chris@0: goto fail; Chris@0: } Chris@0: Chris@0: ptr = mad_bit_nextbyte(&stream->ptr); Chris@0: } Chris@0: Chris@0: /* begin processing */ Chris@0: stream->this_frame = ptr; Chris@0: stream->next_frame = ptr + 1; /* possibly bogus sync word */ Chris@0: Chris@0: mad_bit_init(&stream->ptr, stream->this_frame); Chris@0: Chris@0: if (decode_header(header, stream) == -1) Chris@0: goto fail; Chris@0: Chris@0: /* calculate frame duration */ Chris@0: mad_timer_set(&header->duration, 0, Chris@0: 32 * MAD_NSBSAMPLES(header), header->samplerate); Chris@0: Chris@0: /* calculate free bit rate */ Chris@0: if (header->bitrate == 0) { Chris@0: if ((stream->freerate == 0 || !stream->sync || Chris@0: (header->layer == MAD_LAYER_III && stream->freerate > 640000)) && Chris@0: free_bitrate(stream, header) == -1) Chris@0: goto fail; Chris@0: Chris@0: header->bitrate = stream->freerate; Chris@0: header->flags |= MAD_FLAG_FREEFORMAT; Chris@0: } Chris@0: Chris@0: /* calculate beginning of next frame */ Chris@0: pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; Chris@0: Chris@0: if (header->layer == MAD_LAYER_I) Chris@0: N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4; Chris@0: else { Chris@0: unsigned int slots_per_frame; Chris@0: Chris@0: slots_per_frame = (header->layer == MAD_LAYER_III && Chris@0: (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; Chris@0: Chris@0: N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot; Chris@0: } Chris@0: Chris@0: /* verify there is enough data left in buffer to decode this frame */ Chris@0: if (N + MAD_BUFFER_GUARD > end - stream->this_frame) { Chris@0: stream->next_frame = stream->this_frame; Chris@0: Chris@0: stream->error = MAD_ERROR_BUFLEN; Chris@0: goto fail; Chris@0: } Chris@0: Chris@0: stream->next_frame = stream->this_frame + N; Chris@0: Chris@0: if (!stream->sync) { Chris@0: /* check that a valid frame header follows this frame */ Chris@0: Chris@0: ptr = stream->next_frame; Chris@0: if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { Chris@0: ptr = stream->next_frame = stream->this_frame + 1; Chris@0: goto sync; Chris@0: } Chris@0: Chris@0: stream->sync = 1; Chris@0: } Chris@0: Chris@0: header->flags |= MAD_FLAG_INCOMPLETE; Chris@0: Chris@0: return 0; Chris@0: Chris@0: fail: Chris@0: stream->sync = 0; Chris@0: Chris@0: return -1; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: frame->decode() Chris@0: * DESCRIPTION: decode a single frame from a bitstream Chris@0: */ Chris@0: int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream) Chris@0: { Chris@0: frame->options = stream->options; Chris@0: Chris@0: /* header() */ Chris@0: /* error_check() */ Chris@0: Chris@0: if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) && Chris@0: mad_header_decode(&frame->header, stream) == -1) Chris@0: goto fail; Chris@0: Chris@0: /* audio_data() */ Chris@0: Chris@0: frame->header.flags &= ~MAD_FLAG_INCOMPLETE; Chris@0: Chris@0: if (decoder_table[frame->header.layer - 1](stream, frame) == -1) { Chris@0: if (!MAD_RECOVERABLE(stream->error)) Chris@0: stream->next_frame = stream->this_frame; Chris@0: Chris@0: goto fail; Chris@0: } Chris@0: Chris@0: /* ancillary_data() */ Chris@0: Chris@0: if (frame->header.layer != MAD_LAYER_III) { Chris@0: struct mad_bitptr next_frame; Chris@0: Chris@0: mad_bit_init(&next_frame, stream->next_frame); Chris@0: Chris@0: stream->anc_ptr = stream->ptr; Chris@0: stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame); Chris@0: Chris@0: mad_bit_finish(&next_frame); Chris@0: } Chris@0: Chris@0: return 0; Chris@0: Chris@0: fail: Chris@0: stream->anc_bitlen = 0; Chris@0: return -1; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: frame->mute() Chris@0: * DESCRIPTION: zero all subband values so the frame becomes silent Chris@0: */ Chris@0: void mad_frame_mute(struct mad_frame *frame) Chris@0: { Chris@0: unsigned int s, sb; Chris@0: Chris@0: for (s = 0; s < 36; ++s) { Chris@0: for (sb = 0; sb < 32; ++sb) { Chris@0: frame->sbsample[0][s][sb] = Chris@0: frame->sbsample[1][s][sb] = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: if (frame->overlap) { Chris@0: for (s = 0; s < 18; ++s) { Chris@0: for (sb = 0; sb < 32; ++sb) { Chris@0: (*frame->overlap)[0][sb][s] = Chris@0: (*frame->overlap)[1][sb][s] = 0; Chris@0: } Chris@0: } Chris@0: } Chris@0: }