/
smpeg.c
308 lines (258 loc) · 9.88 KB
/
smpeg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* SDL_sound -- An abstract sound format decoding API.
* Copyright (C) 2001 Ryan C. Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* MPEG-1 Layer 3, or simply, "MP3", decoder for SDL_sound.
*
* This driver handles all those highly compressed songs you stole through
* Napster. :) It depends on the SMPEG library for decoding, which can
* be grabbed from: http://www.lokigames.com/development/smpeg.php3
*
* This should also be able to extract the audio stream from an MPEG movie.
*
* There is an alternative MP3 decoder available, called "mpglib", which
* doesn't depend on external libraries (the decoder itself is part of
* SDL_sound), and may be more efficient, but less flexible than SMPEG. YMMV.
*
* Please see the file COPYING in the source's root directory.
*
* This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef SOUND_SUPPORTS_SMPEG
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL_sound.h"
#define __SDL_SOUND_INTERNAL__
#include "SDL_sound_internal.h"
#include "smpeg.h"
#include "extra_rwops.h"
static int _SMPEG_init(void);
static void _SMPEG_quit(void);
static int _SMPEG_open(Sound_Sample *sample, const char *ext);
static void _SMPEG_close(Sound_Sample *sample);
static Uint32 _SMPEG_read(Sound_Sample *sample);
static int _SMPEG_rewind(Sound_Sample *sample);
static int _SMPEG_seek(Sound_Sample *sample, Uint32 ms);
static const char *extensions_smpeg[] = { "MP3", "MPEG", "MPG", NULL };
const Sound_DecoderFunctions __Sound_DecoderFunctions_SMPEG =
{
{
extensions_smpeg,
"MPEG-1 Layer 3 audio through SMPEG",
"Ryan C. Gordon <icculus@clutteredmind.org>",
"http://icculus.org/smpeg/"
},
_SMPEG_init, /* init() method */
_SMPEG_quit, /* quit() method */
_SMPEG_open, /* open() method */
_SMPEG_close, /* close() method */
_SMPEG_read, /* read() method */
_SMPEG_rewind, /* rewind() method */
_SMPEG_seek /* seek() method */
};
static int _SMPEG_init(void)
{
return(1); /* always succeeds. */
} /* _SMPEG_init */
static void _SMPEG_quit(void)
{
/* it's a no-op. */
} /* _SMPEG_quit */
static __inline__ void output_version(void)
{
static int first_time = 1;
if (first_time)
{
SMPEG_version v;
SMPEG_VERSION(&v);
SNDDBG(("SMPEG: Compiled against SMPEG v%d.%d.%d.\n",
v.major, v.minor, v.patch));
first_time = 0;
} /* if */
} /* output_version */
static int _SMPEG_open(Sound_Sample *sample, const char *ext)
{
SMPEG *smpeg;
SMPEG_Info smpeg_info;
SDL_AudioSpec spec;
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
SDL_RWops *refCounter;
output_version();
/*
* If I understand things correctly, MP3 files don't really have any
* magic header we can check for. The MP3 player is expected to just
* pick the first thing that looks like a valid frame and start
* playing from there.
*
* So here's what we do: If the caller insists that this is really
* MP3 we'll take his word for it. Otherwise, use the same test as
* SDL_mixer does and check if the stream starts with something that
* looks like a frame.
*
* A frame begins with 11 bits of frame sync (all bits must be set),
* followed by a two-bit MPEG Audio version ID:
*
* 00 - MPEG Version 2.5 (later extension of MPEG 2)
* 01 - reserved
* 10 - MPEG Version 2 (ISO/IEC 13818-3)
* 11 - MPEG Version 1 (ISO/IEC 11172-3)
*
* Apparently we don't handle MPEG Version 2.5.
*/
if (__Sound_strcasecmp(ext, "MP3") != 0)
{
Uint8 mp3_magic[2];
if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1)
BAIL_MACRO("SMPEG: Could not read MP3 magic.", 0);
if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0)
BAIL_MACRO("SMPEG: Not an MP3 stream.", 0);
/* If the seek fails, we'll probably miss a frame, but oh well */
SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR);
} /* if */
refCounter = RWops_RWRefCounter_new(internal->rw);
if (refCounter == NULL)
{
SNDDBG(("SMPEG: Failed to create reference counting RWops.\n"));
return(0);
} /* if */
/* replace original RWops. This is safe. Honest. :) */
internal->rw = refCounter;
/*
* increment the refcount, since SMPEG will nuke the RWops if it can't
* accept the contained data...
*/
RWops_RWRefCounter_addRef(refCounter);
smpeg = SMPEG_new_rwops(refCounter, &smpeg_info, 0);
if (SMPEG_error(smpeg))
{
SMPEG_delete(smpeg);
BAIL_MACRO(SMPEG_error(smpeg), 0);
} /* if */
if (!smpeg_info.has_audio)
{
SMPEG_delete(smpeg);
BAIL_MACRO("SMPEG: No audio stream found in data.", 0);
} /* if */
SNDDBG(("SMPEG: Accepting data stream.\n"));
SNDDBG(("SMPEG: has_audio == {%s}.\n", smpeg_info.has_audio ? "TRUE" : "FALSE"));
SNDDBG(("SMPEG: has_video == {%s}.\n", smpeg_info.has_video ? "TRUE" : "FALSE"));
SNDDBG(("SMPEG: width == (%d).\n", smpeg_info.width));
SNDDBG(("SMPEG: height == (%d).\n", smpeg_info.height));
SNDDBG(("SMPEG: current_frame == (%d).\n", smpeg_info.current_frame));
SNDDBG(("SMPEG: current_fps == (%f).\n", smpeg_info.current_fps));
SNDDBG(("SMPEG: audio_string == [%s].\n", smpeg_info.audio_string));
SNDDBG(("SMPEG: audio_current_frame == (%d).\n", smpeg_info.audio_current_frame));
SNDDBG(("SMPEG: current_offset == (%d).\n", smpeg_info.current_offset));
SNDDBG(("SMPEG: total_size == (%d).\n", smpeg_info.total_size));
SNDDBG(("SMPEG: current_time == (%f).\n", smpeg_info.current_time));
SNDDBG(("SMPEG: total_time == (%f).\n", smpeg_info.total_time));
SMPEG_enablevideo(smpeg, 0);
SMPEG_enableaudio(smpeg, 1);
SMPEG_loop(smpeg, 0);
SMPEG_wantedSpec(smpeg, &spec);
/*
* One of the MP3s I tried wouldn't work unless I added this line
* to tell SMPEG that yes, it may have the spec it wants.
*/
SMPEG_actualSpec(smpeg, &spec);
sample->actual.format = spec.format;
sample->actual.rate = spec.freq;
sample->actual.channels = spec.channels;
sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
internal->total_time = smpeg_info.total_time * 1000;
internal->decoder_private = smpeg;
SMPEG_play(smpeg);
return(1);
} /* _SMPEG_open */
static void _SMPEG_close(Sound_Sample *sample)
{
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
SMPEG_delete((SMPEG *) internal->decoder_private);
} /* _SMPEG_close */
static Uint32 _SMPEG_read(Sound_Sample *sample)
{
Uint32 retval;
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
SMPEG *smpeg = (SMPEG *) internal->decoder_private;
/*
* We have to clear the buffer because apparently SMPEG_playAudio()
* will mix the decoded audio with whatever's already in it. Nasty.
*/
memset(internal->buffer, '\0', internal->buffer_size);
retval = SMPEG_playAudio(smpeg, internal->buffer, internal->buffer_size);
if (retval < internal->buffer_size)
{
char *errMsg = SMPEG_error(smpeg);
if (errMsg == NULL)
sample->flags |= SOUND_SAMPLEFLAG_EOF;
else
{
__Sound_SetError(errMsg);
sample->flags |= SOUND_SAMPLEFLAG_ERROR;
} /* else */
} /* if */
return(retval);
} /* _SMPEG_read */
static int _SMPEG_rewind(Sound_Sample *sample)
{
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
SMPEG *smpeg = (SMPEG *) internal->decoder_private;
SMPEGstatus status;
/*
* SMPEG_rewind() really means "stop and rewind", so we may have to
* restart it afterwards.
*/
status = SMPEG_status(smpeg);
SMPEG_rewind(smpeg);
/* EW: I think SMPEG_play() has an independent and unrelated meaning
* to the flag, "SMPEG_PLAYING". This is why the SMPEG_play() call
* is done in the open() function even though the file is not yet
* technically playing. I believe SMPEG_play() must always be active
* because this seems to be what's causing the:
* "Can't rewind after the file has finished playing once" problem,
* because always recalling it here seems to make the problem go away.
*/
/*
if (status == SMPEG_PLAYING)
SMPEG_play(smpeg);
*/
SMPEG_play(smpeg);
return(1);
} /* _SMPEG_rewind */
static int _SMPEG_seek(Sound_Sample *sample, Uint32 ms)
{
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
SMPEG *smpeg = (SMPEG *) internal->decoder_private;
SMPEGstatus status;
/*
* SMPEG_rewind() really means "stop and rewind", so we may have to
* restart it afterwards.
*/
status = SMPEG_status(smpeg);
SMPEG_rewind(smpeg);
SMPEG_skip(smpeg, ((float) ms) / 1000.0);
if (status == SMPEG_PLAYING)
SMPEG_play(smpeg);
return(1);
} /* _SMPEG_seek */
#endif /* SOUND_SUPPORTS_SMPEG */
/* end of smpeg.c ... */