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: stream.c,v 1.12 2004/02/05 09:02:39 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: Chris@0: /* Chris@0: * NAME: stream->init() Chris@0: * DESCRIPTION: initialize stream struct Chris@0: */ Chris@0: void mad_stream_init(struct mad_stream *stream) Chris@0: { Chris@0: stream->buffer = 0; Chris@0: stream->bufend = 0; Chris@0: stream->skiplen = 0; Chris@0: Chris@0: stream->sync = 0; Chris@0: stream->freerate = 0; Chris@0: Chris@0: stream->this_frame = 0; Chris@0: stream->next_frame = 0; Chris@0: mad_bit_init(&stream->ptr, 0); Chris@0: Chris@0: mad_bit_init(&stream->anc_ptr, 0); Chris@0: stream->anc_bitlen = 0; Chris@0: Chris@0: stream->main_data = 0; Chris@0: stream->md_len = 0; Chris@0: Chris@0: stream->options = 0; Chris@0: stream->error = MAD_ERROR_NONE; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: stream->finish() Chris@0: * DESCRIPTION: deallocate any dynamic memory associated with stream Chris@0: */ Chris@0: void mad_stream_finish(struct mad_stream *stream) Chris@0: { Chris@0: if (stream->main_data) { Chris@0: free(stream->main_data); Chris@0: stream->main_data = 0; Chris@0: } Chris@0: Chris@0: mad_bit_finish(&stream->anc_ptr); Chris@0: mad_bit_finish(&stream->ptr); Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: stream->buffer() Chris@0: * DESCRIPTION: set stream buffer pointers Chris@0: */ Chris@0: void mad_stream_buffer(struct mad_stream *stream, Chris@0: unsigned char const *buffer, unsigned long length) Chris@0: { Chris@0: stream->buffer = buffer; Chris@0: stream->bufend = buffer + length; Chris@0: Chris@0: stream->this_frame = buffer; Chris@0: stream->next_frame = buffer; Chris@0: Chris@0: stream->sync = 1; Chris@0: Chris@0: mad_bit_init(&stream->ptr, buffer); Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: stream->skip() Chris@0: * DESCRIPTION: arrange to skip bytes before the next frame Chris@0: */ Chris@0: void mad_stream_skip(struct mad_stream *stream, unsigned long length) Chris@0: { Chris@0: stream->skiplen += length; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: stream->sync() Chris@0: * DESCRIPTION: locate the next stream sync word Chris@0: */ Chris@0: int mad_stream_sync(struct mad_stream *stream) Chris@0: { Chris@0: register unsigned char const *ptr, *end; Chris@0: Chris@0: ptr = mad_bit_nextbyte(&stream->ptr); Chris@0: end = stream->bufend; Chris@0: Chris@0: while (ptr < end - 1 && Chris@0: !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) Chris@0: ++ptr; Chris@0: Chris@0: if (end - ptr < MAD_BUFFER_GUARD) Chris@0: return -1; Chris@0: Chris@0: mad_bit_init(&stream->ptr, ptr); Chris@0: Chris@0: return 0; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: stream->errorstr() Chris@0: * DESCRIPTION: return a string description of the current error condition Chris@0: */ Chris@0: char const *mad_stream_errorstr(struct mad_stream const *stream) Chris@0: { Chris@0: switch (stream->error) { Chris@0: case MAD_ERROR_NONE: return "no error"; Chris@0: Chris@0: case MAD_ERROR_BUFLEN: return "input buffer too small (or EOF)"; Chris@0: case MAD_ERROR_BUFPTR: return "invalid (null) buffer pointer"; Chris@0: Chris@0: case MAD_ERROR_NOMEM: return "not enough memory"; Chris@0: Chris@0: case MAD_ERROR_LOSTSYNC: return "lost synchronization"; Chris@0: case MAD_ERROR_BADLAYER: return "reserved header layer value"; Chris@0: case MAD_ERROR_BADBITRATE: return "forbidden bitrate value"; Chris@0: case MAD_ERROR_BADSAMPLERATE: return "reserved sample frequency value"; Chris@0: case MAD_ERROR_BADEMPHASIS: return "reserved emphasis value"; Chris@0: Chris@0: case MAD_ERROR_BADCRC: return "CRC check failed"; Chris@0: case MAD_ERROR_BADBITALLOC: return "forbidden bit allocation value"; Chris@0: case MAD_ERROR_BADSCALEFACTOR: return "bad scalefactor index"; Chris@0: case MAD_ERROR_BADMODE: return "bad bitrate/mode combination"; Chris@0: case MAD_ERROR_BADFRAMELEN: return "bad frame length"; Chris@0: case MAD_ERROR_BADBIGVALUES: return "bad big_values count"; Chris@0: case MAD_ERROR_BADBLOCKTYPE: return "reserved block_type"; Chris@0: case MAD_ERROR_BADSCFSI: return "bad scalefactor selection info"; Chris@0: case MAD_ERROR_BADDATAPTR: return "bad main_data_begin pointer"; Chris@0: case MAD_ERROR_BADPART3LEN: return "bad audio data length"; Chris@0: case MAD_ERROR_BADHUFFTABLE: return "bad Huffman table select"; Chris@0: case MAD_ERROR_BADHUFFDATA: return "Huffman data overrun"; Chris@0: case MAD_ERROR_BADSTEREO: return "incompatible block_type for JS"; Chris@0: } Chris@0: Chris@0: return 0; Chris@0: }