Mercurial > SDL_sound_CoreAudio
annotate decoders/mpglib.c @ 358:f11c10ffa31a
Fixed some debug messages.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 11 Jun 2002 23:33:02 +0000 |
parents | 069ce624d6cf |
children | cbb15ecf423a |
rev | line source |
---|---|
261 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library 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 GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /* | |
21 * MPGLIB decoder for SDL_sound. This is a very lightweight MP3 decoder, | |
22 * which is included with the SDL_sound source, so that it doesn't rely on | |
23 * unnecessary external libraries. | |
24 * | |
25 * The SMPEG decoder plays back more forms of MPEGs, and may behave better or | |
26 * worse under various conditions. mpglib is (apparently) more efficient than | |
27 * SMPEG, and, again, doesn't need an external library. You should test both | |
28 * decoders and use what you find works best for you. | |
29 * | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
30 * mpglib is an LGPL'd portion of mpg123, which can be found in its original |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
31 * form at: http://www.mpg123.de/ |
261 | 32 * |
33 * Please see the file COPYING in the source's root directory. The included | |
34 * source code for mpglib falls under the LGPL, which is the same license as | |
35 * SDL_sound (so you can consider it a single work). | |
36 * | |
37 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
38 */ | |
39 | |
40 #if HAVE_CONFIG_H | |
41 # include <config.h> | |
42 #endif | |
43 | |
44 #ifdef SOUND_SUPPORTS_MPGLIB | |
45 | |
46 #include <stdio.h> | |
47 #include <stdlib.h> | |
48 #include <string.h> | |
49 #include <assert.h> | |
50 #include "mpglib/mpg123_sdlsound.h" | |
51 #include "mpglib/mpglib_sdlsound.h" | |
52 | |
53 #include "SDL_sound.h" | |
54 | |
55 #define __SDL_SOUND_INTERNAL__ | |
56 #include "SDL_sound_internal.h" | |
57 | |
58 static int MPGLIB_init(void); | |
59 static void MPGLIB_quit(void); | |
60 static int MPGLIB_open(Sound_Sample *sample, const char *ext); | |
61 static void MPGLIB_close(Sound_Sample *sample); | |
62 static Uint32 MPGLIB_read(Sound_Sample *sample); | |
63 static int MPGLIB_rewind(Sound_Sample *sample); | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
64 static int MPGLIB_seek(Sound_Sample *sample, Uint32 ms); |
261 | 65 |
66 static const char *extensions_mpglib[] = { "MP3", NULL }; | |
67 const Sound_DecoderFunctions __Sound_DecoderFunctions_MPGLIB = | |
68 { | |
69 { | |
70 extensions_mpglib, | |
71 "MP3 decoding via internal mpglib", | |
72 "Ryan C. Gordon <icculus@clutteredmind.org>", | |
73 "http://www.icculus.org/SDL_sound/" | |
74 }, | |
75 | |
76 MPGLIB_init, /* init() method */ | |
77 MPGLIB_quit, /* quit() method */ | |
78 MPGLIB_open, /* open() method */ | |
79 MPGLIB_close, /* close() method */ | |
80 MPGLIB_read, /* read() method */ | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
81 MPGLIB_rewind, /* rewind() method */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
82 MPGLIB_seek /* seek() method */ |
261 | 83 }; |
84 | |
85 | |
86 /* this is what we store in our internal->decoder_private field... */ | |
87 typedef struct | |
88 { | |
89 struct mpstr mp; | |
90 Uint8 inbuf[16384]; | |
91 Uint8 outbuf[8192]; | |
92 int outleft; | |
93 int outpos; | |
94 } mpglib_t; | |
95 | |
96 | |
97 | |
98 static int MPGLIB_init(void) | |
99 { | |
100 return(1); /* always succeeds. */ | |
101 } /* MPGLIB_init */ | |
102 | |
103 | |
104 static void MPGLIB_quit(void) | |
105 { | |
106 /* it's a no-op. */ | |
107 } /* MPGLIB_quit */ | |
108 | |
109 | |
110 static int MPGLIB_open(Sound_Sample *sample, const char *ext) | |
111 { | |
112 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
113 mpglib_t *mpg = NULL; | |
293
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
114 int rc; |
261 | 115 |
116 /* | |
117 * If I understand things correctly, MP3 files don't really have any | |
118 * magic header we can check for. The MP3 player is expected to just | |
119 * pick the first thing that looks like a valid frame and start | |
120 * playing from there. | |
121 * | |
122 * So here's what we do: If the caller insists that this is really | |
123 * MP3 we'll take his word for it. Otherwise, use the same test as | |
124 * SDL_mixer does and check if the stream starts with something that | |
125 * looks like a frame. | |
126 * | |
127 * A frame begins with 11 bits of frame sync (all bits must be set), | |
128 * followed by a two-bit MPEG Audio version ID: | |
129 * | |
130 * 00 - MPEG Version 2.5 (later extension of MPEG 2) | |
131 * 01 - reserved | |
132 * 10 - MPEG Version 2 (ISO/IEC 13818-3) | |
133 * 11 - MPEG Version 1 (ISO/IEC 11172-3) | |
134 * | |
135 * Apparently we don't handle MPEG Version 2.5. | |
136 */ | |
137 if (__Sound_strcasecmp(ext, "MP3") != 0) | |
138 { | |
139 Uint8 mp3_magic[2]; | |
140 | |
141 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1) | |
358
f11c10ffa31a
Fixed some debug messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
351
diff
changeset
|
142 BAIL_MACRO("MPGLIB: Could not read MP3 magic.", 0); |
261 | 143 |
144 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0) | |
358
f11c10ffa31a
Fixed some debug messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
351
diff
changeset
|
145 BAIL_MACRO("MPGLIB: Not an MP3 stream.", 0); |
261 | 146 |
351 | 147 /* If the seek fails, we'll probably miss a frame, but oh well. */ |
261 | 148 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR); |
149 } /* if */ | |
150 | |
151 mpg = (mpglib_t *) malloc(sizeof (mpglib_t)); | |
152 BAIL_IF_MACRO(mpg == NULL, ERR_OUT_OF_MEMORY, 0); | |
293
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
153 memset(mpg, '\0', sizeof (mpglib_t)); |
261 | 154 InitMP3(&mpg->mp); |
155 | |
293
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
156 rc = SDL_RWread(internal->rw, mpg->inbuf, 1, sizeof (mpg->inbuf)); |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
157 if (rc <= 0) |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
158 { |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
159 free(mpg); |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
160 BAIL_MACRO("MPGLIB: Failed to read any data at all", 0); |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
161 } /* if */ |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
162 |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
163 if (decodeMP3(&mpg->mp, mpg->inbuf, rc, |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
164 mpg->outbuf, sizeof (mpg->outbuf), |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
165 &mpg->outleft) == MP3_ERR) |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
166 { |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
167 free(mpg); |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
168 BAIL_MACRO("MPGLIB: Not an MP3 stream?", 0); |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
169 } /* if */ |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
170 |
261 | 171 SNDDBG(("MPGLIB: Accepting data stream.\n")); |
172 | |
173 internal->decoder_private = mpg; | |
293
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
174 sample->actual.rate = mpglib_freqs[mpg->mp.fr.sampling_frequency]; |
ee6e1f8bfae9
Can determine audio format correctly (mostly correctly?) now. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
271
diff
changeset
|
175 sample->actual.channels = mpg->mp.fr.stereo; |
351 | 176 sample->actual.format = AUDIO_S16SYS; |
261 | 177 sample->flags = SOUND_SAMPLEFLAG_NONE; |
178 | |
179 return(1); /* we'll handle this data. */ | |
180 } /* MPGLIB_open */ | |
181 | |
182 | |
183 static void MPGLIB_close(Sound_Sample *sample) | |
184 { | |
185 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
186 mpglib_t *mpg = ((mpglib_t *) internal->decoder_private); | |
187 ExitMP3(&mpg->mp); | |
188 free(mpg); | |
189 } /* MPGLIB_close */ | |
190 | |
191 | |
192 static Uint32 MPGLIB_read(Sound_Sample *sample) | |
193 { | |
194 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
195 mpglib_t *mpg = ((mpglib_t *) internal->decoder_private); | |
196 Uint32 bw = 0; | |
197 int rc; | |
198 | |
199 while (bw < internal->buffer_size) | |
200 { | |
201 if (mpg->outleft > 0) | |
202 { | |
203 Uint16 cpysize = internal->buffer_size - bw; | |
204 if (cpysize > mpg->outleft) | |
205 cpysize = mpg->outleft; | |
206 memcpy(((Uint8 *) internal->buffer) + bw, | |
207 mpg->outbuf + mpg->outpos, cpysize); | |
208 bw += cpysize; | |
209 mpg->outpos += cpysize; | |
210 mpg->outleft -= cpysize; | |
211 continue; | |
212 } /* if */ | |
213 | |
214 /* need to decode more from the MP3 stream... */ | |
215 mpg->outpos = 0; | |
216 rc = decodeMP3(&mpg->mp, NULL, 0, mpg->outbuf, | |
217 sizeof (mpg->outbuf), &mpg->outleft); | |
218 if (rc == MP3_ERR) | |
219 { | |
220 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
221 return(bw); | |
222 } /* if */ | |
223 | |
224 else if (rc == MP3_NEED_MORE) | |
225 { | |
226 rc = SDL_RWread(internal->rw, mpg->inbuf, 1, sizeof (mpg->inbuf)); | |
227 if (rc == -1) | |
228 { | |
229 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
230 return(bw); | |
231 } /* if */ | |
268
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
232 |
261 | 233 else if (rc == 0) |
234 { | |
235 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
236 return(bw); | |
237 } /* else if */ | |
238 | |
268
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
239 /* make sure there isn't an ID3 tag. */ |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
240 /* |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
241 * !!! FIXME: This can fail under the following circumstances: |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
242 * First, if there's the sequence "TAG" 128 bytes from the end |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
243 * of a read that isn't the EOF. This is unlikely. |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
244 * Second, if the TAG sequence is split between two reads (ie, |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
245 * the last byte of a read is 'T', and the next read is the |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
246 * final 127 bytes of the stream, being the rest of the ID3 tag). |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
247 * While this is more likely, it's still not very likely at all. |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
248 * Still, something SHOULD be done about this. |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
249 * ID3v2 tags are more complex, too, not to mention LYRICS tags, |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
250 * etc, which aren't handled, either. Hey, this IS meant to be |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
251 * a lightweight decoder. Use SMPEG if you need an all-purpose |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
252 * decoder. mpglib really assumes you control all your assets. |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
253 */ |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
254 if (rc >= 128) |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
255 { |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
256 Uint8 *ptr = &mpg->inbuf[rc - 128]; |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
257 if ((ptr[0] == 'T') && (ptr[1] == 'A') && (ptr[2] == 'G')) |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
258 rc -= 128; /* disregard it. */ |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
259 } /* if */ |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
260 |
263 | 261 rc = decodeMP3(&mpg->mp, mpg->inbuf, rc, |
268
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
262 mpg->outbuf, sizeof (mpg->outbuf), |
9b89bb587f8f
Detect and discard ID3 tags.
Ryan C. Gordon <icculus@icculus.org>
parents:
263
diff
changeset
|
263 &mpg->outleft); |
261 | 264 if (rc == MP3_ERR) |
265 { | |
266 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
267 return(bw); | |
268 } /* if */ | |
269 } /* else if */ | |
270 } /* while */ | |
271 | |
272 return(bw); | |
273 } /* MPGLIB_read */ | |
274 | |
275 | |
276 static int MPGLIB_rewind(Sound_Sample *sample) | |
277 { | |
278 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
279 mpglib_t *mpg = ((mpglib_t *) internal->decoder_private); | |
280 BAIL_IF_MACRO(SDL_RWseek(internal->rw, 0, SEEK_SET) != 0, ERR_IO_ERROR, 0); | |
281 | |
282 /* this is just resetting some fields in a structure; it's very fast. */ | |
283 ExitMP3(&mpg->mp); | |
284 InitMP3(&mpg->mp); | |
285 mpg->outpos = mpg->outleft = 0; | |
286 return(1); | |
287 } /* MPGLIB_rewind */ | |
288 | |
289 | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
290 static int MPGLIB_seek(Sound_Sample *sample, Uint32 ms) |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
291 { |
351 | 292 BAIL_MACRO("MPGLIB: Seeking not implemented", 0); |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
293 } /* MPGLIB_seek */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
293
diff
changeset
|
294 |
261 | 295 #endif /* SOUND_SUPPORTS_MPGLIB */ |
296 | |
297 | |
298 /* end of mpglib.c ... */ | |
299 |