xref: /plan9/sys/src/games/mp3dec/stream.c (revision f30ccc91ab9e7f92bd5dd82b1eebdeb503fd3465)
1 /*
2  * libmad - MPEG audio decoder library
3  * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4  *
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.
9  *
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.
14  *
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
18  *
19  * $Id: stream.c,v 1.12 2004/02/05 09:02:39 rob Exp $
20  */
21 
22 # ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 # endif
25 
26 # include "global.h"
27 
28 # include "bit.h"
29 # include "stream.h"
30 
31 /*
32  * NAME:	stream->init()
33  * DESCRIPTION:	initialize stream struct
34  */
mad_stream_init(struct mad_stream * stream)35 void mad_stream_init(struct mad_stream *stream)
36 {
37   stream->buffer     = 0;
38   stream->bufend     = 0;
39   stream->skiplen    = 0;
40 
41   stream->sync       = 0;
42   stream->freerate   = 0;
43 
44   stream->this_frame = 0;
45   stream->next_frame = 0;
46   mad_bit_init(&stream->ptr, 0);
47 
48   mad_bit_init(&stream->anc_ptr, 0);
49   stream->anc_bitlen = 0;
50 
51   stream->main_data  = 0;
52   stream->md_len     = 0;
53 
54   stream->options    = 0;
55   stream->error      = MAD_ERROR_NONE;
56 }
57 
58 /*
59  * NAME:	stream->finish()
60  * DESCRIPTION:	deallocate any dynamic memory associated with stream
61  */
mad_stream_finish(struct mad_stream * stream)62 void mad_stream_finish(struct mad_stream *stream)
63 {
64   if (stream->main_data) {
65     free(stream->main_data);
66     stream->main_data = 0;
67   }
68 
69   mad_bit_finish(&stream->anc_ptr);
70   mad_bit_finish(&stream->ptr);
71 }
72 
73 /*
74  * NAME:	stream->buffer()
75  * DESCRIPTION:	set stream buffer pointers
76  */
mad_stream_buffer(struct mad_stream * stream,unsigned char const * buffer,unsigned long length)77 void mad_stream_buffer(struct mad_stream *stream,
78 		       unsigned char const *buffer, unsigned long length)
79 {
80   stream->buffer = buffer;
81   stream->bufend = buffer + length;
82 
83   stream->this_frame = buffer;
84   stream->next_frame = buffer;
85 
86   stream->sync = 1;
87 
88   mad_bit_init(&stream->ptr, buffer);
89 }
90 
91 /*
92  * NAME:	stream->skip()
93  * DESCRIPTION:	arrange to skip bytes before the next frame
94  */
mad_stream_skip(struct mad_stream * stream,unsigned long length)95 void mad_stream_skip(struct mad_stream *stream, unsigned long length)
96 {
97   stream->skiplen += length;
98 }
99 
100 /*
101  * NAME:	stream->sync()
102  * DESCRIPTION:	locate the next stream sync word
103  */
mad_stream_sync(struct mad_stream * stream)104 int mad_stream_sync(struct mad_stream *stream)
105 {
106   register unsigned char const *ptr, *end;
107 
108   ptr = mad_bit_nextbyte(&stream->ptr);
109   end = stream->bufend;
110 
111   while (ptr < end - 1 &&
112 	 !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0))
113     ++ptr;
114 
115   if (end - ptr < MAD_BUFFER_GUARD)
116     return -1;
117 
118   mad_bit_init(&stream->ptr, ptr);
119 
120   return 0;
121 }
122 
123 /*
124  * NAME:	stream->errorstr()
125  * DESCRIPTION:	return a string description of the current error condition
126  */
mad_stream_errorstr(struct mad_stream const * stream)127 char const *mad_stream_errorstr(struct mad_stream const *stream)
128 {
129   switch (stream->error) {
130   case MAD_ERROR_NONE:		 return "no error";
131 
132   case MAD_ERROR_BUFLEN:	 return "input buffer too small (or EOF)";
133   case MAD_ERROR_BUFPTR:	 return "invalid (null) buffer pointer";
134 
135   case MAD_ERROR_NOMEM:		 return "not enough memory";
136 
137   case MAD_ERROR_LOSTSYNC:	 return "lost synchronization";
138   case MAD_ERROR_BADLAYER:	 return "reserved header layer value";
139   case MAD_ERROR_BADBITRATE:	 return "forbidden bitrate value";
140   case MAD_ERROR_BADSAMPLERATE:	 return "reserved sample frequency value";
141   case MAD_ERROR_BADEMPHASIS:	 return "reserved emphasis value";
142 
143   case MAD_ERROR_BADCRC:	 return "CRC check failed";
144   case MAD_ERROR_BADBITALLOC:	 return "forbidden bit allocation value";
145   case MAD_ERROR_BADSCALEFACTOR: return "bad scalefactor index";
146   case MAD_ERROR_BADMODE:	 return "bad bitrate/mode combination";
147   case MAD_ERROR_BADFRAMELEN:	 return "bad frame length";
148   case MAD_ERROR_BADBIGVALUES:	 return "bad big_values count";
149   case MAD_ERROR_BADBLOCKTYPE:	 return "reserved block_type";
150   case MAD_ERROR_BADSCFSI:	 return "bad scalefactor selection info";
151   case MAD_ERROR_BADDATAPTR:	 return "bad main_data_begin pointer";
152   case MAD_ERROR_BADPART3LEN:	 return "bad audio data length";
153   case MAD_ERROR_BADHUFFTABLE:	 return "bad Huffman table select";
154   case MAD_ERROR_BADHUFFDATA:	 return "Huffman data overrun";
155   case MAD_ERROR_BADSTEREO:	 return "incompatible block_type for JS";
156   }
157 
158   return 0;
159 }
160