comparison decoders/smpeg.c @ 260:44a4730a1e6f

Renamed from mp3.c, some minor changes to avoid symbol clash.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 21 Feb 2002 19:43:05 +0000
parents
children 9828311da44b
comparison
equal deleted inserted replaced
259:b6c47452f4aa 260:44a4730a1e6f
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 * MPEG-1 Layer 3, or simply, "MP3", decoder for SDL_sound.
22 *
23 * This driver handles all those highly compressed songs you stole through
24 * Napster. :) It depends on the SMPEG library for decoding, which can
25 * be grabbed from: http://www.lokigames.com/development/smpeg.php3
26 *
27 * This should also be able to extract the audio stream from an MPEG movie.
28 *
29 * There is an alternative MP3 decoder available, called "mpglib", which
30 * doesn't depend on external libraries (the decoder itself is part of
31 * SDL_sound), and may be more efficient, but less flexible than SMPEG. YMMV.
32 *
33 * Please see the file COPYING in the source's root directory.
34 *
35 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
36 */
37
38 #if HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41
42 #ifdef SOUND_SUPPORTS_SMPEG
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <assert.h>
48
49 #include "SDL_sound.h"
50
51 #define __SDL_SOUND_INTERNAL__
52 #include "SDL_sound_internal.h"
53
54 #include "smpeg.h"
55 #include "extra_rwops.h"
56
57
58 static int _SMPEG_init(void);
59 static void _SMPEG_quit(void);
60 static int _SMPEG_open(Sound_Sample *sample, const char *ext);
61 static void _SMPEG_close(Sound_Sample *sample);
62 static Uint32 _SMPEG_read(Sound_Sample *sample);
63 static int _SMPEG_rewind(Sound_Sample *sample);
64
65 static const char *extensions_smpeg[] = { "MP3", "MPEG", "MPG", NULL };
66 const Sound_DecoderFunctions __Sound_DecoderFunctions_SMPEG =
67 {
68 {
69 extensions_smpeg,
70 "MPEG-1 Layer 3 audio through SMPEG",
71 "Ryan C. Gordon <icculus@clutteredmind.org>",
72 "http://icculus.org/smpeg/"
73 },
74
75 _SMPEG_init, /* init() method */
76 _SMPEG_quit, /* quit() method */
77 _SMPEG_open, /* open() method */
78 _SMPEG_close, /* close() method */
79 _SMPEG_read, /* read() method */
80 _SMPEG_rewind /* rewind() method */
81 };
82
83
84 static int _SMPEG_init(void)
85 {
86 return(1); /* always succeeds. */
87 } /* _SMPEG_init */
88
89
90 static void _SMPEG_quit(void)
91 {
92 /* it's a no-op. */
93 } /* _SMPEG_quit */
94
95
96 static __inline__ void output_version(void)
97 {
98 static int first_time = 1;
99
100 if (first_time)
101 {
102 SMPEG_version v;
103 SMPEG_VERSION(&v);
104 SNDDBG(("SMPEG: Compiled against SMPEG v%d.%d.%d.\n",
105 v.major, v.minor, v.patch));
106 first_time = 0;
107 } /* if */
108 } /* output_version */
109
110
111 static int _SMPEG_open(Sound_Sample *sample, const char *ext)
112 {
113 SMPEG *smpeg;
114 SMPEG_Info smpeg_info;
115 SDL_AudioSpec spec;
116 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
117 SDL_RWops *refCounter;
118
119 output_version();
120
121 /*
122 * If I understand things correctly, MP3 files don't really have any
123 * magic header we can check for. The MP3 player is expected to just
124 * pick the first thing that looks like a valid frame and start
125 * playing from there.
126 *
127 * So here's what we do: If the caller insists that this is really
128 * MP3 we'll take his word for it. Otherwise, use the same test as
129 * SDL_mixer does and check if the stream starts with something that
130 * looks like a frame.
131 *
132 * A frame begins with 11 bits of frame sync (all bits must be set),
133 * followed by a two-bit MPEG Audio version ID:
134 *
135 * 00 - MPEG Version 2.5 (later extension of MPEG 2)
136 * 01 - reserved
137 * 10 - MPEG Version 2 (ISO/IEC 13818-3)
138 * 11 - MPEG Version 1 (ISO/IEC 11172-3)
139 *
140 * Apparently we don't handle MPEG Version 2.5.
141 */
142 if (__Sound_strcasecmp(ext, "MP3") != 0)
143 {
144 Uint8 mp3_magic[2];
145
146 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1)
147 {
148 Sound_SetError("SMPEG: Could not read MP3 magic.");
149 return(0);
150 } /*if */
151
152 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0)
153 {
154 Sound_SetError("SMPEG: Not an MP3 stream.");
155 return(0);
156 } /* if */
157
158 /* !!! FIXME: If the seek fails, we'll probably miss a frame */
159 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR);
160 } /* if */
161
162 refCounter = RWops_RWRefCounter_new(internal->rw);
163 if (refCounter == NULL)
164 {
165 SNDDBG(("SMPEG: Failed to create reference counting RWops.\n"));
166 return(0);
167 } /* if */
168
169 /* replace original RWops. This is safe. Honest. :) */
170 internal->rw = refCounter;
171
172 /*
173 * increment the refcount, since SMPEG will nuke the RWops if it can't
174 * accept the contained data...
175 */
176 RWops_RWRefCounter_addRef(refCounter);
177 smpeg = SMPEG_new_rwops(refCounter, &smpeg_info, 0);
178
179 if (SMPEG_error(smpeg))
180 {
181 Sound_SetError(SMPEG_error(smpeg));
182 SMPEG_delete(smpeg);
183 return(0);
184 } /* if */
185
186 if (!smpeg_info.has_audio)
187 {
188 Sound_SetError("SMPEG: No audio stream found in data.");
189 SMPEG_delete(smpeg);
190 return(0);
191 } /* if */
192
193 SNDDBG(("SMPEG: Accepting data stream.\n"));
194 SNDDBG(("SMPEG: has_audio == {%s}.\n", smpeg_info.has_audio ? "TRUE" : "FALSE"));
195 SNDDBG(("SMPEG: has_video == {%s}.\n", smpeg_info.has_video ? "TRUE" : "FALSE"));
196 SNDDBG(("SMPEG: width == (%d).\n", smpeg_info.width));
197 SNDDBG(("SMPEG: height == (%d).\n", smpeg_info.height));
198 SNDDBG(("SMPEG: current_frame == (%d).\n", smpeg_info.current_frame));
199 SNDDBG(("SMPEG: current_fps == (%f).\n", smpeg_info.current_fps));
200 SNDDBG(("SMPEG: audio_string == [%s].\n", smpeg_info.audio_string));
201 SNDDBG(("SMPEG: audio_current_frame == (%d).\n", smpeg_info.audio_current_frame));
202 SNDDBG(("SMPEG: current_offset == (%d).\n", smpeg_info.current_offset));
203 SNDDBG(("SMPEG: total_size == (%d).\n", smpeg_info.total_size));
204 SNDDBG(("SMPEG: current_time == (%f).\n", smpeg_info.current_time));
205 SNDDBG(("SMPEG: total_time == (%f).\n", smpeg_info.total_time));
206
207 SMPEG_enablevideo(smpeg, 0);
208 SMPEG_enableaudio(smpeg, 1);
209 SMPEG_loop(smpeg, 0);
210
211 SMPEG_wantedSpec(smpeg, &spec);
212
213 /*
214 * One of the MP3s I tried wouldn't work unless I added this line
215 * to tell SMPEG that yes, it may have the spec it wants.
216 */
217 SMPEG_actualSpec(smpeg, &spec);
218 sample->actual.format = spec.format;
219 sample->actual.rate = spec.freq;
220 sample->actual.channels = spec.channels;
221 sample->flags = SOUND_SAMPLEFLAG_NONE;
222 internal->decoder_private = smpeg;
223
224 SMPEG_play(smpeg);
225 return(1);
226 } /* _SMPEG_open */
227
228
229 static void _SMPEG_close(Sound_Sample *sample)
230 {
231 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
232 SMPEG_delete((SMPEG *) internal->decoder_private);
233 } /* _SMPEG_close */
234
235
236 static Uint32 _SMPEG_read(Sound_Sample *sample)
237 {
238 Uint32 retval;
239 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
240 SMPEG *smpeg = (SMPEG *) internal->decoder_private;
241
242 /*
243 * We have to clear the buffer because apparently SMPEG_playAudio()
244 * will mix the decoded audio with whatever's already in it. Nasty.
245 */
246 memset(internal->buffer, '\0', internal->buffer_size);
247 retval = SMPEG_playAudio(smpeg, internal->buffer, internal->buffer_size);
248 if (retval < internal->buffer_size)
249 {
250 char *errMsg = SMPEG_error(smpeg);
251 if (errMsg == NULL)
252 sample->flags |= SOUND_SAMPLEFLAG_EOF;
253 else
254 {
255 Sound_SetError(errMsg);
256 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
257 } /* else */
258 } /* if */
259
260 return(retval);
261 } /* _SMPEG_read */
262
263
264 static int _SMPEG_rewind(Sound_Sample *sample)
265 {
266 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
267 SMPEG *smpeg = (SMPEG *) internal->decoder_private;
268 SMPEGstatus status;
269
270 /*
271 * SMPEG_rewind() really means "stop and rewind", so we may have to
272 * restart it afterwards.
273 */
274 status = SMPEG_status(smpeg);
275 SMPEG_rewind(smpeg);
276 if (status == SMPEG_PLAYING)
277 SMPEG_play(smpeg);
278 return(1);
279 } /* _SMPEG_rewind */
280
281 #endif /* SOUND_SUPPORTS_SMPEG */
282
283 /* end of smpeg.c ... */
284