Mercurial > SDL_sound_CoreAudio
comparison decoders/mp3.c @ 96:25e515dfa18d
Fix for MP3 detection.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 02 Oct 2001 17:46:12 +0000 |
parents | 40006625142a |
children | 6d9fdec2f708 |
comparison
equal
deleted
inserted
replaced
95:4436f4587c9a | 96:25e515dfa18d |
---|---|
99 SMPEG *smpeg; | 99 SMPEG *smpeg; |
100 SMPEG_Info smpeg_info; | 100 SMPEG_Info smpeg_info; |
101 SDL_AudioSpec spec; | 101 SDL_AudioSpec spec; |
102 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | 102 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
103 SDL_RWops *refCounter; | 103 SDL_RWops *refCounter; |
104 Uint8 mp3_magic[2]; | |
105 | 104 |
106 output_version(); | 105 output_version(); |
107 | 106 |
108 /* | 107 /* |
109 * SMPEG appears to be far too greedy about what it accepts as input. | 108 * If I understand things correctly, MP3 files don't really have any |
110 * This test was adapted from SDL_mixer. | 109 * magic header we can check for. The MP3 player is expected to just |
110 * pick the first thing that looks like a valid frame and start | |
111 * playing from there. | |
112 * | |
113 * So here's what we do: If the caller insists that this is really | |
114 * MP3 we'll take his word for it. Otherwise, use the same test as | |
115 * SDL_mixer does and check if the stream starts with something that | |
116 * looks like a frame. | |
117 * | |
118 * A frame begins with 11 bits of frame sync (all bits must be set), | |
119 * followed by a two-bit MPEG Audio version ID: | |
120 * | |
121 * 00 - MPEG Version 2.5 (later extension of MPEG 2) | |
122 * 01 - reserved | |
123 * 10 - MPEG Version 2 (ISO/IEC 13818-3) | |
124 * 11 - MPEG Version 1 (ISO/IEC 11172-3) | |
125 * | |
126 * Apparently we don't handle MPEG Version 2.5. | |
111 */ | 127 */ |
112 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1) | 128 if (__Sound_strcasecmp(ext, "MP3") != 0) |
113 { | 129 { |
114 Sound_SetError("MP3: Could not read MP3 magic."); | 130 Uint8 mp3_magic[2]; |
115 return(0); | 131 |
116 } | 132 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1) |
117 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0) | 133 { |
118 { | 134 Sound_SetError("MP3: Could not read MP3 magic."); |
119 Sound_SetError("MP3: Not an MP3 stream."); | 135 return(0); |
120 return(0); | 136 } /*if */ |
121 } | 137 |
122 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR); | 138 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0) |
139 { | |
140 Sound_SetError("MP3: Not an MP3 stream."); | |
141 return(0); | |
142 } /* if */ | |
143 | |
144 /* !!! FIXME: If the seek fails, we'll probably miss a frame */ | |
145 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR); | |
146 } /* if */ | |
123 | 147 |
124 refCounter = RWops_RWRefCounter_new(internal->rw); | 148 refCounter = RWops_RWRefCounter_new(internal->rw); |
125 if (refCounter == NULL) | 149 if (refCounter == NULL) |
126 { | 150 { |
127 SNDDBG(("MP3: Failed to create reference counting RWops.\n")); | 151 SNDDBG(("MP3: Failed to create reference counting RWops.\n")); |
169 SMPEG_enablevideo(smpeg, 0); | 193 SMPEG_enablevideo(smpeg, 0); |
170 SMPEG_enableaudio(smpeg, 1); | 194 SMPEG_enableaudio(smpeg, 1); |
171 SMPEG_loop(smpeg, 0); | 195 SMPEG_loop(smpeg, 0); |
172 | 196 |
173 SMPEG_wantedSpec(smpeg, &spec); | 197 SMPEG_wantedSpec(smpeg, &spec); |
198 | |
174 /* | 199 /* |
175 * One of the MP3:s I tried wouldn't work unless I added this line | 200 * One of the MP3s I tried wouldn't work unless I added this line |
176 * to tell SMPEG that yes, it may have the spec it wants. | 201 * to tell SMPEG that yes, it may have the spec it wants. |
177 */ | 202 */ |
178 SMPEG_actualSpec(smpeg, &spec); | 203 SMPEG_actualSpec(smpeg, &spec); |
179 sample->actual.format = spec.format; | 204 sample->actual.format = spec.format; |
180 sample->actual.rate = spec.freq; | 205 sample->actual.rate = spec.freq; |