Mercurial > SDL_sound_CoreAudio
annotate decoders/mp3.c @ 106:40de367eb59e
Changing my include structure to do this right.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 03 Oct 2001 18:29:32 +0000 |
parents | 103cfcb3c014 |
children | 1df5c106504e |
rev | line source |
---|---|
14 | 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 * Please see the file LICENSE in the source's root directory. | |
28 * | |
29 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
30 */ | |
31 | |
106
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
32 #if HAVE_CONFIG_H |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
33 # include <config.h> |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
34 #endif |
100
6d9fdec2f708
added config.h, added --enable-debug flag, various other changes to the build system
fingolfin
parents:
96
diff
changeset
|
35 |
104
103cfcb3c014
Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
36 #ifdef SOUND_SUPPORTS_MP3 |
103cfcb3c014
Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
37 |
14 | 38 #include <stdio.h> |
39 #include <stdlib.h> | |
40 #include <string.h> | |
41 #include <assert.h> | |
42 | |
106
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
43 #include "SDL_sound.h" |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
44 |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
45 #define __SDL_SOUND_INTERNAL__ |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
46 #include "SDL_sound_internal.h" |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
47 |
14 | 48 #include "smpeg.h" |
49 #include "extra_rwops.h" | |
50 | |
51 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
52 static int MP3_init(void); |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
53 static void MP3_quit(void); |
14 | 54 static int MP3_open(Sound_Sample *sample, const char *ext); |
55 static void MP3_close(Sound_Sample *sample); | |
56 static Uint32 MP3_read(Sound_Sample *sample); | |
57 | |
58 const Sound_DecoderFunctions __Sound_DecoderFunctions_MP3 = | |
59 { | |
60 { | |
61 "MP3", | |
62 "MPEG-1 Layer 3 audio through SMPEG", | |
63 "Ryan C. Gordon <icculus@clutteredmind.org>", | |
64 "http://www.icculus.org/SDL_sound/" | |
65 }, | |
66 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
67 MP3_init, /* init() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
68 MP3_quit, /* quit() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
69 MP3_open, /* open() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
70 MP3_close, /* close() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
71 MP3_read /* read() method */ |
14 | 72 }; |
73 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
74 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
75 static int MP3_init(void) |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
76 { |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
77 return(1); /* always succeeds. */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
78 } /* MP3_init */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
79 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
80 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
81 static void MP3_quit(void) |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
82 { |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
83 /* it's a no-op. */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
84 } /* MP3_quit */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
85 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
86 |
14 | 87 static __inline__ void output_version(void) |
88 { | |
89 static int first_time = 1; | |
90 | |
91 if (first_time) | |
92 { | |
93 SMPEG_version v; | |
94 SMPEG_VERSION(&v); | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
95 SNDDBG(("MP3: Compiled against SMPEG v%d.%d.%d.\n", |
14 | 96 v.major, v.minor, v.patch)); |
97 first_time = 0; | |
98 } /* if */ | |
99 } /* output_version */ | |
100 | |
101 | |
102 static int MP3_open(Sound_Sample *sample, const char *ext) | |
103 { | |
104 SMPEG *smpeg; | |
105 SMPEG_Info smpeg_info; | |
106 SDL_AudioSpec spec; | |
107 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
108 SDL_RWops *refCounter; | |
109 | |
110 output_version(); | |
111 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
112 /* |
96 | 113 * If I understand things correctly, MP3 files don't really have any |
114 * magic header we can check for. The MP3 player is expected to just | |
115 * pick the first thing that looks like a valid frame and start | |
116 * playing from there. | |
117 * | |
118 * So here's what we do: If the caller insists that this is really | |
119 * MP3 we'll take his word for it. Otherwise, use the same test as | |
120 * SDL_mixer does and check if the stream starts with something that | |
121 * looks like a frame. | |
122 * | |
123 * A frame begins with 11 bits of frame sync (all bits must be set), | |
124 * followed by a two-bit MPEG Audio version ID: | |
125 * | |
126 * 00 - MPEG Version 2.5 (later extension of MPEG 2) | |
127 * 01 - reserved | |
128 * 10 - MPEG Version 2 (ISO/IEC 13818-3) | |
129 * 11 - MPEG Version 1 (ISO/IEC 11172-3) | |
130 * | |
131 * Apparently we don't handle MPEG Version 2.5. | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
132 */ |
96 | 133 if (__Sound_strcasecmp(ext, "MP3") != 0) |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
134 { |
96 | 135 Uint8 mp3_magic[2]; |
136 | |
137 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1) | |
138 { | |
139 Sound_SetError("MP3: Could not read MP3 magic."); | |
140 return(0); | |
141 } /*if */ | |
142 | |
143 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0) | |
144 { | |
145 Sound_SetError("MP3: Not an MP3 stream."); | |
146 return(0); | |
147 } /* if */ | |
148 | |
149 /* !!! FIXME: If the seek fails, we'll probably miss a frame */ | |
150 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR); | |
151 } /* if */ | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
152 |
14 | 153 refCounter = RWops_RWRefCounter_new(internal->rw); |
154 if (refCounter == NULL) | |
155 { | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
156 SNDDBG(("MP3: Failed to create reference counting RWops.\n")); |
14 | 157 return(0); |
158 } /* if */ | |
159 | |
160 /* replace original RWops. This is safe. Honest. :) */ | |
161 internal->rw = refCounter; | |
162 | |
163 /* | |
164 * increment the refcount, since SMPEG will nuke the RWops if it can't | |
165 * accept the contained data... | |
166 */ | |
167 RWops_RWRefCounter_addRef(refCounter); | |
168 smpeg = SMPEG_new_rwops(refCounter, &smpeg_info, 0); | |
169 | |
170 if (SMPEG_error(smpeg)) | |
171 { | |
172 Sound_SetError(SMPEG_error(smpeg)); | |
173 SMPEG_delete(smpeg); | |
174 return(0); | |
175 } /* if */ | |
176 | |
177 if (!smpeg_info.has_audio) | |
178 { | |
179 Sound_SetError("MP3: No audio stream found in data."); | |
180 SMPEG_delete(smpeg); | |
181 return(0); | |
182 } /* if */ | |
183 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
184 SNDDBG(("MP3: Accepting data stream.\n")); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
185 SNDDBG(("MP3: has_audio == {%s}.\n", smpeg_info.has_audio ? "TRUE" : "FALSE")); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
186 SNDDBG(("MP3: has_video == {%s}.\n", smpeg_info.has_video ? "TRUE" : "FALSE")); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
187 SNDDBG(("MP3: width == (%d).\n", smpeg_info.width)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
188 SNDDBG(("MP3: height == (%d).\n", smpeg_info.height)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
189 SNDDBG(("MP3: current_frame == (%d).\n", smpeg_info.current_frame)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
190 SNDDBG(("MP3: current_fps == (%f).\n", smpeg_info.current_fps)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
191 SNDDBG(("MP3: audio_string == [%s].\n", smpeg_info.audio_string)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
192 SNDDBG(("MP3: audio_current_frame == (%d).\n", smpeg_info.audio_current_frame)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
193 SNDDBG(("MP3: current_offset == (%d).\n", smpeg_info.current_offset)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
194 SNDDBG(("MP3: total_size == (%d).\n", smpeg_info.total_size)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
195 SNDDBG(("MP3: current_time == (%f).\n", smpeg_info.current_time)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
196 SNDDBG(("MP3: total_time == (%f).\n", smpeg_info.total_time)); |
14 | 197 |
198 SMPEG_enablevideo(smpeg, 0); | |
199 SMPEG_enableaudio(smpeg, 1); | |
200 SMPEG_loop(smpeg, 0); | |
201 | |
202 SMPEG_wantedSpec(smpeg, &spec); | |
96 | 203 |
54
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
204 /* |
96 | 205 * One of the MP3s I tried wouldn't work unless I added this line |
54
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
206 * to tell SMPEG that yes, it may have the spec it wants. |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
207 */ |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
208 SMPEG_actualSpec(smpeg, &spec); |
14 | 209 sample->actual.format = spec.format; |
210 sample->actual.rate = spec.freq; | |
211 sample->actual.channels = spec.channels; | |
212 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
213 internal->decoder_private = smpeg; | |
214 | |
215 SMPEG_play(smpeg); | |
216 return(1); | |
217 } /* MP3_open */ | |
218 | |
219 | |
220 static void MP3_close(Sound_Sample *sample) | |
221 { | |
222 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
223 SMPEG_delete((SMPEG *) internal->decoder_private); | |
224 } /* MP3_close */ | |
225 | |
226 | |
227 static Uint32 MP3_read(Sound_Sample *sample) | |
228 { | |
229 Uint32 retval; | |
230 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
231 SMPEG *smpeg = (SMPEG *) internal->decoder_private; | |
232 | |
54
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
233 /* |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
234 * We have to clear the buffer because apparently SMPEG_playAudio() |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
235 * will mix the decoded audio with whatever's already in it. Nasty. |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
236 */ |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
237 memset(internal->buffer, 0, internal->buffer_size); |
14 | 238 retval = SMPEG_playAudio(smpeg, internal->buffer, internal->buffer_size); |
239 if (retval < internal->buffer_size) | |
240 { | |
241 char *errMsg = SMPEG_error(smpeg); | |
242 if (errMsg == NULL) | |
243 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
244 else | |
245 { | |
246 Sound_SetError(errMsg); | |
247 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
248 } /* else */ | |
249 } /* if */ | |
250 | |
251 return(retval); | |
252 } /* MP3_read */ | |
253 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
254 #endif /* SOUND_SUPPORTS_MP3 */ |
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
255 |
14 | 256 /* end of mp3.c ... */ |
257 |