for god's sake
[libogc.git] / libmad / stream.c
blobda58e2cdc06fed4f1d0e412625f659a3fea89815
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2003 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
21 # ifdef HAVE_CONFIG_H
22 # include "config.h"
23 # endif
25 # include "global.h"
27 # include <stdlib.h>
29 # include "bit.h"
30 # include "stream.h"
33 * NAME: stream->init()
34 * DESCRIPTION: initialize stream struct
36 void mad_stream_init(struct mad_stream *stream)
38 stream->buffer = 0;
39 stream->bufend = 0;
40 stream->skiplen = 0;
42 stream->sync = 0;
43 stream->freerate = 0;
45 stream->this_frame = 0;
46 stream->next_frame = 0;
47 mad_bit_init(&stream->ptr, 0);
49 mad_bit_init(&stream->anc_ptr, 0);
50 stream->anc_bitlen = 0;
52 stream->main_data = 0;
53 stream->md_len = 0;
55 stream->options = 0;
56 stream->error = MAD_ERROR_NONE;
60 * NAME: stream->finish()
61 * DESCRIPTION: deallocate any dynamic memory associated with stream
63 void mad_stream_finish(struct mad_stream *stream)
65 if (stream->main_data) {
66 free(stream->main_data);
67 stream->main_data = 0;
70 mad_bit_finish(&stream->anc_ptr);
71 mad_bit_finish(&stream->ptr);
75 * NAME: stream->buffer()
76 * DESCRIPTION: set stream buffer pointers
78 void mad_stream_buffer(struct mad_stream *stream,
79 u8 const *buffer, u32 length)
81 stream->buffer = buffer;
82 stream->bufend = buffer + length;
84 stream->this_frame = buffer;
85 stream->next_frame = buffer;
87 stream->sync = 1;
89 mad_bit_init(&stream->ptr, buffer);
93 * NAME: stream->skip()
94 * DESCRIPTION: arrange to skip bytes before the next frame
96 void mad_stream_skip(struct mad_stream *stream, u32 length)
98 stream->skiplen += length;
102 * NAME: stream->sync()
103 * DESCRIPTION: locate the next stream sync word
105 s32 mad_stream_sync(struct mad_stream *stream)
107 register u8 const *ptr, *end;
109 ptr = mad_bit_nextbyte(&stream->ptr);
110 end = stream->bufend;
112 while (ptr < end - 1 &&
113 !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0))
114 ++ptr;
116 if (end - ptr < MAD_BUFFER_GUARD)
117 return -1;
119 mad_bit_init(&stream->ptr, ptr);
121 return 0;
125 * NAME: stream->errorstr()
126 * DESCRIPTION: return a string description of the current error condition
128 char const *mad_stream_errorstr(struct mad_stream const *stream)
130 switch (stream->error) {
131 case MAD_ERROR_NONE: return "no error";
133 case MAD_ERROR_BUFLEN: return "input buffer too small (or EOF)";
134 case MAD_ERROR_BUFPTR: return "invalid (null) buffer pointer";
136 case MAD_ERROR_NOMEM: return "not enough memory";
138 case MAD_ERROR_LOSTSYNC: return "lost synchronization";
139 case MAD_ERROR_BADLAYER: return "reserved header layer value";
140 case MAD_ERROR_BADBITRATE: return "forbidden bitrate value";
141 case MAD_ERROR_BADSAMPLERATE: return "reserved sample frequency value";
142 case MAD_ERROR_BADEMPHASIS: return "reserved emphasis value";
144 case MAD_ERROR_BADCRC: return "CRC check failed";
145 case MAD_ERROR_BADBITALLOC: return "forbidden bit allocation value";
146 case MAD_ERROR_BADSCALEFACTOR: return "bad scalefactor index";
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";
158 return 0;