00001 /* 00002 * libmad - MPEG audio decoder library 00003 * Copyright (C) 2000-2008 Underbit Technologies, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 * 00019 * $Id: stream.c 88 2008-06-09 11:59:11Z mich $ 00020 */ 00026 # ifdef HAVE_CONFIG_H 00027 # include "config.h" 00028 # endif 00029 00030 # include "global.h" 00031 00032 # include <stdlib.h> 00033 00034 # include "bit.h" 00035 # include "stream.h" 00036 00040 void mad_stream_init(struct mad_stream *stream) 00041 { 00042 stream->buffer = 0; 00043 stream->bufend = 0; 00044 stream->skiplen = 0; 00045 00046 stream->sync = 0; 00047 stream->freerate = 0; 00048 00049 stream->this_frame = 0; 00050 stream->next_frame = 0; 00051 mad_bit_init(&stream->ptr, 0); 00052 00053 mad_bit_init(&stream->anc_ptr, 0); 00054 stream->anc_bitlen = 0; 00055 00056 stream->main_data = 0; 00057 stream->md_len = 0; 00058 00059 stream->options = 0; 00060 stream->error = MAD_ERROR_NONE; 00061 } 00062 00066 void mad_stream_finish(struct mad_stream *stream) 00067 { 00068 if (stream->main_data) { 00069 free(stream->main_data); 00070 stream->main_data = 0; 00071 } 00072 00073 mad_bit_finish(&stream->anc_ptr); 00074 mad_bit_finish(&stream->ptr); 00075 } 00076 00080 void mad_stream_buffer(struct mad_stream *stream, 00081 unsigned char const *buffer, unsigned long length) 00082 { 00083 stream->buffer = buffer; 00084 stream->bufend = buffer + length; 00085 00086 stream->this_frame = buffer; 00087 stream->next_frame = buffer; 00088 00089 stream->sync = 1; 00090 00091 mad_bit_init(&stream->ptr, buffer); 00092 } 00093 00097 void mad_stream_skip(struct mad_stream *stream, unsigned long length) 00098 { 00099 stream->skiplen += length; 00100 } 00101 00105 int mad_stream_sync(struct mad_stream *stream) 00106 { 00107 register unsigned char const *ptr, *end; 00108 00109 ptr = mad_bit_nextbyte(&stream->ptr); 00110 end = stream->bufend; 00111 00112 while (ptr < end - 1 && 00113 !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) 00114 ++ptr; 00115 00116 if (end - ptr < MAD_BUFFER_GUARD) 00117 return -1; 00118 00119 mad_bit_init(&stream->ptr, ptr); 00120 00121 return 0; 00122 } 00123 00127 char const *mad_stream_errorstr(struct mad_stream const *stream) 00128 { 00129 switch (stream->error) { 00130 case MAD_ERROR_NONE: return "no error"; 00131 00132 case MAD_ERROR_BUFLEN: return "input buffer too small (or EOF)"; 00133 case MAD_ERROR_BUFPTR: return "invalid (null) buffer pointer"; 00134 00135 case MAD_ERROR_NOMEM: return "not enough memory"; 00136 00137 case MAD_ERROR_LOSTSYNC: return "lost synchronization"; 00138 case MAD_ERROR_BADLAYER: return "reserved header layer value"; 00139 case MAD_ERROR_BADBITRATE: return "forbidden bitrate value"; 00140 case MAD_ERROR_BADSAMPLERATE: return "reserved sample frequency value"; 00141 case MAD_ERROR_BADEMPHASIS: return "reserved emphasis value"; 00142 00143 case MAD_ERROR_BADCRC: return "CRC check failed"; 00144 case MAD_ERROR_BADBITALLOC: return "forbidden bit allocation value"; 00145 case MAD_ERROR_BADSCALEFACTOR: return "bad scalefactor index"; 00146 case MAD_ERROR_BADMODE: return "bad bitrate/mode combination"; 00147 case MAD_ERROR_BADFRAMELEN: return "bad frame length"; 00148 case MAD_ERROR_BADBIGVALUES: return "bad big_values count"; 00149 case MAD_ERROR_BADBLOCKTYPE: return "reserved block_type"; 00150 case MAD_ERROR_BADSCFSI: return "bad scalefactor selection info"; 00151 case MAD_ERROR_BADDATAPTR: return "bad main_data_begin pointer"; 00152 case MAD_ERROR_BADPART3LEN: return "bad audio data length"; 00153 case MAD_ERROR_BADHUFFTABLE: return "bad Huffman table select"; 00154 case MAD_ERROR_BADHUFFDATA: return "Huffman data overrun"; 00155 case MAD_ERROR_BADSTEREO: return "incompatible block_type for JS"; 00156 } 00157 00158 return 0; 00159 }