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 *
|
|
30 * mpglib is part of mpg123, which can be found in its original form at:
|
|
31 * http://www.mpg123.de/
|
|
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);
|
|
64
|
|
65 static const char *extensions_mpglib[] = { "MP3", NULL };
|
|
66 const Sound_DecoderFunctions __Sound_DecoderFunctions_MPGLIB =
|
|
67 {
|
|
68 {
|
|
69 extensions_mpglib,
|
|
70 "MP3 decoding via internal mpglib",
|
|
71 "Ryan C. Gordon <icculus@clutteredmind.org>",
|
|
72 "http://www.icculus.org/SDL_sound/"
|
|
73 },
|
|
74
|
|
75 MPGLIB_init, /* init() method */
|
|
76 MPGLIB_quit, /* quit() method */
|
|
77 MPGLIB_open, /* open() method */
|
|
78 MPGLIB_close, /* close() method */
|
|
79 MPGLIB_read, /* read() method */
|
|
80 MPGLIB_rewind /* rewind() method */
|
|
81 };
|
|
82
|
|
83
|
|
84 /* this is what we store in our internal->decoder_private field... */
|
|
85 typedef struct
|
|
86 {
|
|
87 struct mpstr mp;
|
|
88 Uint8 inbuf[16384];
|
|
89 Uint8 outbuf[8192];
|
|
90 int outleft;
|
|
91 int outpos;
|
|
92 } mpglib_t;
|
|
93
|
|
94
|
|
95
|
|
96 static int MPGLIB_init(void)
|
|
97 {
|
|
98 return(1); /* always succeeds. */
|
|
99 } /* MPGLIB_init */
|
|
100
|
|
101
|
|
102 static void MPGLIB_quit(void)
|
|
103 {
|
|
104 /* it's a no-op. */
|
|
105 } /* MPGLIB_quit */
|
|
106
|
|
107
|
|
108 static int MPGLIB_open(Sound_Sample *sample, const char *ext)
|
|
109 {
|
|
110 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
|
|
111 mpglib_t *mpg = NULL;
|
|
112
|
|
113 /*
|
|
114 * If I understand things correctly, MP3 files don't really have any
|
|
115 * magic header we can check for. The MP3 player is expected to just
|
|
116 * pick the first thing that looks like a valid frame and start
|
|
117 * playing from there.
|
|
118 *
|
|
119 * So here's what we do: If the caller insists that this is really
|
|
120 * MP3 we'll take his word for it. Otherwise, use the same test as
|
|
121 * SDL_mixer does and check if the stream starts with something that
|
|
122 * looks like a frame.
|
|
123 *
|
|
124 * A frame begins with 11 bits of frame sync (all bits must be set),
|
|
125 * followed by a two-bit MPEG Audio version ID:
|
|
126 *
|
|
127 * 00 - MPEG Version 2.5 (later extension of MPEG 2)
|
|
128 * 01 - reserved
|
|
129 * 10 - MPEG Version 2 (ISO/IEC 13818-3)
|
|
130 * 11 - MPEG Version 1 (ISO/IEC 11172-3)
|
|
131 *
|
|
132 * Apparently we don't handle MPEG Version 2.5.
|
|
133 */
|
|
134 if (__Sound_strcasecmp(ext, "MP3") != 0)
|
|
135 {
|
|
136 Uint8 mp3_magic[2];
|
|
137
|
|
138 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1)
|
|
139 {
|
|
140 Sound_SetError("MP3: Could not read MP3 magic.");
|
|
141 return(0);
|
|
142 } /*if */
|
|
143
|
|
144 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0)
|
|
145 {
|
|
146 Sound_SetError("MP3: Not an MP3 stream.");
|
|
147 return(0);
|
|
148 } /* if */
|
|
149
|
|
150 /* !!! FIXME: If the seek fails, we'll probably miss a frame */
|
|
151 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR);
|
|
152 } /* if */
|
|
153
|
|
154 mpg = (mpglib_t *) malloc(sizeof (mpglib_t));
|
|
155 BAIL_IF_MACRO(mpg == NULL, ERR_OUT_OF_MEMORY, 0);
|
|
156 mpg->outpos = mpg->outleft = 0;
|
|
157 InitMP3(&mpg->mp);
|
|
158
|
|
159 SNDDBG(("MPGLIB: Accepting data stream.\n"));
|
|
160
|
|
161 internal->decoder_private = mpg;
|
|
162 sample->actual.rate = 44100;
|
|
163 sample->actual.channels = 2;
|
|
164 sample->actual.format = AUDIO_S16LSB;
|
|
165 sample->flags = SOUND_SAMPLEFLAG_NONE;
|
|
166
|
|
167 return(1); /* we'll handle this data. */
|
|
168 } /* MPGLIB_open */
|
|
169
|
|
170
|
|
171 static void MPGLIB_close(Sound_Sample *sample)
|
|
172 {
|
|
173 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
|
|
174 mpglib_t *mpg = ((mpglib_t *) internal->decoder_private);
|
|
175 ExitMP3(&mpg->mp);
|
|
176 free(mpg);
|
|
177 } /* MPGLIB_close */
|
|
178
|
|
179
|
|
180 static Uint32 MPGLIB_read(Sound_Sample *sample)
|
|
181 {
|
|
182 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
|
|
183 mpglib_t *mpg = ((mpglib_t *) internal->decoder_private);
|
|
184 Uint32 bw = 0;
|
|
185 int rc;
|
|
186
|
|
187 while (bw < internal->buffer_size)
|
|
188 {
|
|
189 if (mpg->outleft > 0)
|
|
190 {
|
|
191 Uint16 cpysize = internal->buffer_size - bw;
|
|
192 if (cpysize > mpg->outleft)
|
|
193 cpysize = mpg->outleft;
|
|
194 memcpy(((Uint8 *) internal->buffer) + bw,
|
|
195 mpg->outbuf + mpg->outpos, cpysize);
|
|
196 bw += cpysize;
|
|
197 mpg->outpos += cpysize;
|
|
198 mpg->outleft -= cpysize;
|
|
199 continue;
|
|
200 } /* if */
|
|
201
|
|
202 /* need to decode more from the MP3 stream... */
|
|
203 mpg->outpos = 0;
|
|
204 rc = decodeMP3(&mpg->mp, NULL, 0, mpg->outbuf,
|
|
205 sizeof (mpg->outbuf), &mpg->outleft);
|
|
206 if (rc == MP3_ERR)
|
|
207 {
|
|
208 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
|
|
209 return(bw);
|
|
210 } /* if */
|
|
211
|
|
212 else if (rc == MP3_NEED_MORE)
|
|
213 {
|
|
214 rc = SDL_RWread(internal->rw, mpg->inbuf, 1, sizeof (mpg->inbuf));
|
|
215 if (rc == -1)
|
|
216 {
|
|
217 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
|
|
218 return(bw);
|
|
219 } /* if */
|
|
220 else if (rc == 0)
|
|
221 {
|
|
222 sample->flags |= SOUND_SAMPLEFLAG_EOF;
|
|
223 return(bw);
|
|
224 } /* else if */
|
|
225
|
263
|
226 rc = decodeMP3(&mpg->mp, mpg->inbuf, rc,
|
261
|
227 mpg->outbuf, sizeof (mpg->outbuf), &mpg->outleft);
|
|
228 if (rc == MP3_ERR)
|
|
229 {
|
|
230 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
|
|
231 return(bw);
|
|
232 } /* if */
|
|
233 } /* else if */
|
|
234 } /* while */
|
|
235
|
|
236 return(bw);
|
|
237 } /* MPGLIB_read */
|
|
238
|
|
239
|
|
240 static int MPGLIB_rewind(Sound_Sample *sample)
|
|
241 {
|
|
242 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
|
|
243 mpglib_t *mpg = ((mpglib_t *) internal->decoder_private);
|
|
244 BAIL_IF_MACRO(SDL_RWseek(internal->rw, 0, SEEK_SET) != 0, ERR_IO_ERROR, 0);
|
|
245
|
|
246 /* this is just resetting some fields in a structure; it's very fast. */
|
|
247 ExitMP3(&mpg->mp);
|
|
248 InitMP3(&mpg->mp);
|
|
249 mpg->outpos = mpg->outleft = 0;
|
|
250 return(1);
|
|
251 } /* MPGLIB_rewind */
|
|
252
|
|
253
|
|
254 #endif /* SOUND_SUPPORTS_MPGLIB */
|
|
255
|
|
256
|
|
257 /* end of mpglib.c ... */
|
|
258
|