Mercurial > almixer_isolated
comparison Isolated/LGPL/oggvorbis.c @ 62:7a4a8459f0c1
Ogg Vorbis decoder for SoundDecoder directly adapted from SDL_sound's code. Thanks to Johnson Lin for providing this!
Instead of trying to backport my Tremor decoder which originated from the SDL_sound Vorbis decoder, I told Johnson it would be better to just start at the source and avoid all the original gotchas I hit in the subtle differences between Tremor and Vorbis.
Since this derives directly from Ryan Gordon's SDL_sound implementation, I am putting this under the LGPL subdirectory.
Johnson Lin < arch . jslin - at - gmail . com >
author | Eric Wing <ewing . public |-at-| gmail . com> |
---|---|
date | Tue, 19 Jun 2012 00:31:12 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
61:72570fcd30e8 | 62:7a4a8459f0c1 |
---|---|
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 * Ogg Vorbis decoder for SDL_sound. | |
22 * | |
23 * This driver handles .OGG audio files, and depends on libvorbisfile to | |
24 * do the actual decoding work. libvorbisfile is part of libvorbis, which | |
25 * is part of the Ogg Vorbis project. | |
26 * | |
27 * Ogg Vorbis: http://www.xiph.org/ogg/vorbis/ | |
28 * vorbisfile documentation: http://www.xiph.org/ogg/vorbis/doc/vorbisfile/ | |
29 * | |
30 * Please see the file LICENSE.txt in the source's root directory. | |
31 * | |
32 * This file written by Ryan C. Gordon. (icculus@icculus.org) | |
33 */ | |
34 | |
35 #if HAVE_CONFIG_H | |
36 # include <config.h> | |
37 #endif | |
38 | |
39 #ifdef SOUND_SUPPORTS_OGG | |
40 | |
41 #include <stdio.h> | |
42 #include <stdlib.h> | |
43 #include <string.h> | |
44 #include <math.h> | |
45 | |
46 #include "SoundDecoder.h" | |
47 | |
48 #include "SoundDecoder_Internal.h" | |
49 #include "SDL_endian_minimal.h" | |
50 #include "ALmixer_RWops.h" | |
51 | |
52 //#include <vorbis/codec.h> | |
53 #ifdef _WIN32 | |
54 #include <vorbis/vorbisfile.h> | |
55 #else | |
56 #include <ivorbisfile.h> | |
57 #endif //_WIN32 | |
58 | |
59 #define ERR_IO_ERROR "I/O error" | |
60 | |
61 | |
62 static int OGG_init(void); | |
63 static void OGG_quit(void); | |
64 static int OGG_open(Sound_Sample *sample, const char *ext); | |
65 static void OGG_close(Sound_Sample *sample); | |
66 static uint32_t OGG_read(Sound_Sample *sample); | |
67 static int OGG_rewind(Sound_Sample *sample); | |
68 static int OGG_seek(Sound_Sample *sample, uint32_t ms); | |
69 | |
70 static const char *extensions_ogg[] = { "OGG", NULL }; | |
71 const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG = | |
72 { | |
73 { | |
74 extensions_ogg, | |
75 "Ogg Vorbis audio through VorbisFile", | |
76 "Ryan C. Gordon <icculus@icculus.org>", | |
77 "http://www.icculus.org/SDL_sound/" | |
78 }, | |
79 | |
80 OGG_init, /* init() method */ | |
81 OGG_quit, /* quit() method */ | |
82 OGG_open, /* open() method */ | |
83 OGG_close, /* close() method */ | |
84 OGG_read, /* read() method */ | |
85 OGG_rewind, /* rewind() method */ | |
86 OGG_seek /* seek() method */ | |
87 }; | |
88 | |
89 | |
90 static int OGG_init(void) | |
91 { | |
92 return(1); /* always succeeds. */ | |
93 } /* OGG_init */ | |
94 | |
95 | |
96 static void OGG_quit(void) | |
97 { | |
98 /* it's a no-op. */ | |
99 } /* OGG_quit */ | |
100 | |
101 | |
102 | |
103 /* | |
104 * These are callbacks from vorbisfile that let them read data from | |
105 * a RWops... | |
106 */ | |
107 | |
108 static size_t RWops_ogg_read(void *ptr, size_t size, size_t nmemb, void *datasource) | |
109 { | |
110 return((size_t) ALmixer_RWread((ALmixer_RWops *) datasource, ptr, size, nmemb)); | |
111 } /* RWops_ogg_read */ | |
112 | |
113 static int RWops_ogg_seek(void *datasource, ogg_int64_t offset, int whence) | |
114 { | |
115 return(ALmixer_RWseek((ALmixer_RWops *) datasource, offset, whence)); | |
116 } /* RWops_ogg_seek */ | |
117 | |
118 static int RWops_ogg_close(void *datasource) | |
119 { | |
120 /* do nothing; SDL_sound will delete the RWops at a higher level. */ | |
121 return(0); /* this is success in fclose(), so I guess that's okay. */ | |
122 } /* RWops_ogg_close */ | |
123 | |
124 static long RWops_ogg_tell(void *datasource) | |
125 { | |
126 return((long) ALmixer_RWtell((ALmixer_RWops *) datasource)); | |
127 } /* RWops_ogg_tell */ | |
128 | |
129 static const ov_callbacks RWops_ogg_callbacks = | |
130 { | |
131 RWops_ogg_read, | |
132 RWops_ogg_seek, | |
133 RWops_ogg_close, | |
134 RWops_ogg_tell | |
135 }; | |
136 | |
137 | |
138 /* Return a human readable version of an VorbisFile error code... */ | |
139 #if (defined DEBUG_CHATTER) | |
140 static const char *ogg_error(int errnum) | |
141 { | |
142 switch(errnum) | |
143 { | |
144 case OV_EREAD: | |
145 return("i/o error"); | |
146 case OV_ENOTVORBIS: | |
147 return("not a vorbis file"); | |
148 case OV_EVERSION: | |
149 return("Vorbis version mismatch"); | |
150 case OV_EBADHEADER: | |
151 return("invalid Vorbis bitstream header"); | |
152 case OV_EFAULT: | |
153 return("internal logic fault in Vorbis library"); | |
154 } /* switch */ | |
155 | |
156 return("unknown error"); | |
157 } /* ogg_error */ | |
158 #endif | |
159 | |
160 static __inline__ void output_ogg_comments(OggVorbis_File *vf) | |
161 { | |
162 #if (defined DEBUG_CHATTER) | |
163 int i; | |
164 vorbis_comment *vc = ov_comment(vf, -1); | |
165 | |
166 if (vc == NULL) | |
167 return; | |
168 | |
169 SNDDBG(("OGG: vendor == [%s].\n", vc->vendor)); | |
170 for (i = 0; i < vc->comments; i++) | |
171 { | |
172 SNDDBG(("OGG: user comment [%s].\n", vc->user_comments[i])); | |
173 } /* for */ | |
174 #endif | |
175 } /* output_ogg_comments */ | |
176 | |
177 | |
178 static int OGG_open(Sound_Sample *sample, const char *ext) | |
179 { | |
180 int rc; | |
181 double total_time; | |
182 OggVorbis_File *vf; | |
183 vorbis_info *info; | |
184 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
185 | |
186 vf = (OggVorbis_File *) malloc(sizeof (OggVorbis_File)); | |
187 BAIL_IF_MACRO(vf == NULL, ERR_OUT_OF_MEMORY, 0); | |
188 | |
189 rc = ov_open_callbacks(internal->rw, vf, NULL, 0, RWops_ogg_callbacks); | |
190 if (rc != 0) | |
191 { | |
192 #if (defined DEBUG_CHATTER) | |
193 SNDDBG(("OGG: can't grok data. reason: [%s].\n", ogg_error(rc))); | |
194 #endif | |
195 free(vf); | |
196 BAIL_MACRO("OGG: Not valid Ogg Vorbis data.", 0); | |
197 } /* if */ | |
198 | |
199 info = ov_info(vf, -1); | |
200 if (info == NULL) | |
201 { | |
202 ov_clear(vf); | |
203 free(vf); | |
204 BAIL_MACRO("OGG: failed to retrieve bitstream info", 0); | |
205 } /* if */ | |
206 | |
207 SNDDBG(("OGG: Accepting data stream.\n")); | |
208 | |
209 output_ogg_comments(vf); | |
210 SNDDBG(("OGG: bitstream version == (%d).\n", info->version)); | |
211 SNDDBG(("OGG: bitstream channels == (%d).\n", info->channels)); | |
212 SNDDBG(("OGG: bitstream sampling rate == (%ld).\n", info->rate)); | |
213 SNDDBG(("OGG: seekable == {%s}.\n", ov_seekable(vf) ? "TRUE" : "FALSE")); | |
214 SNDDBG(("OGG: number of logical bitstreams == (%ld).\n", ov_streams(vf))); | |
215 SNDDBG(("OGG: serial number == (%ld).\n", ov_serialnumber(vf, -1))); | |
216 SNDDBG(("OGG: total seconds of sample == (%f).\n", ov_time_total(vf, -1))); | |
217 | |
218 internal->decoder_private = vf; | |
219 sample->flags = SOUND_SAMPLEFLAG_CANSEEK; | |
220 sample->actual.rate = (uint32_t) info->rate; | |
221 sample->actual.channels = (uint8_t) info->channels; | |
222 total_time = ov_time_total(vf, -1); | |
223 if (OV_EINVAL == total_time) | |
224 internal->total_time = -1; | |
225 else | |
226 internal->total_time = (int32_t)(total_time * 1000.0 + 0.5); | |
227 | |
228 /* | |
229 * Since we might have more than one logical bitstream in the OGG file, | |
230 * and these bitstreams may be in different formats, we might be | |
231 * converting two or three times: once in vorbisfile, once again in | |
232 * SDL_sound, and perhaps a third time to get it to the sound device's | |
233 * format. That's wickedly inefficient. | |
234 * | |
235 * To combat this a little, if the user specified a desired format, we | |
236 * claim that to be the "actual" format of the collection of logical | |
237 * bitstreams. This means that VorbisFile will do a conversion as | |
238 * necessary, and SDL_sound will not. If the user didn't specify a | |
239 * desired format, then we pretend the "actual" format is something that | |
240 * OGG files are apparently commonly encoded in. | |
241 */ | |
242 sample->actual.format = (sample->desired.format == 0) ? | |
243 AUDIO_S16LSB : sample->desired.format; | |
244 return(1); | |
245 } /* OGG_open */ | |
246 | |
247 | |
248 static void OGG_close(Sound_Sample *sample) | |
249 { | |
250 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
251 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private; | |
252 ov_clear(vf); | |
253 free(vf); | |
254 } /* OGG_close */ | |
255 | |
256 /* Note: According to the Vorbis documentation: | |
257 * "ov_read() will decode at most one vorbis packet per invocation, | |
258 * so the value returned will generally be less than length." | |
259 * Due to this, for buffer sizes like 16384, SDL_Sound was always getting | |
260 * an underfilled buffer and always setting the EAGAIN flag. | |
261 * Since the SDL_Sound API implies that the entire buffer | |
262 * should be filled unless EOF, additional code has been added | |
263 * to this function to call ov_read() until the buffer is filled. | |
264 * However, there may still be some corner cases where the buffer | |
265 * cannot be entirely filled. So be aware. | |
266 */ | |
267 static uint32_t OGG_read(Sound_Sample *sample) | |
268 { | |
269 int rc; | |
270 int bitstream; | |
271 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
272 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private; | |
273 | |
274 rc = ov_read(vf, internal->buffer, internal->buffer_size, | |
275 ((sample->actual.format & 0x1000) ? 1 : 0), /* bigendian? */ | |
276 ((sample->actual.format & 0xFF) / 8), /* bytes per sample point */ | |
277 ((sample->actual.format & 0x8000) ? 1 : 0), /* signed data? */ | |
278 &bitstream); | |
279 | |
280 /* Make sure the read went smoothly... */ | |
281 if (rc == 0) | |
282 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
283 | |
284 else if (rc < 0) | |
285 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
286 | |
287 /* If the buffer isn't filled, keep trying to fill it | |
288 * until no more data can be grabbed */ | |
289 else if ((uint32_t) rc < internal->buffer_size) | |
290 { | |
291 /* Creating a pointer to the buffer that denotes where to start | |
292 * writing new data. */ | |
293 uint8_t lala; | |
294 char* buffer_start_point = NULL; | |
295 int total_bytes_read = rc; | |
296 int bytes_remaining = internal->buffer_size - rc; | |
297 | |
298 /* Keep grabbing data until something prevents | |
299 * us from getting more. (Could be EOF, | |
300 * packets are too large to fit in remaining | |
301 * space, or an error.) | |
302 */ | |
303 while( (rc > 0) && (bytes_remaining > 0) ) | |
304 { | |
305 /* Set buffer pointer to end of last write */ | |
306 /* All the messiness is to get rid of the warning for | |
307 * dereferencing a void* | |
308 */ | |
309 buffer_start_point = &(((char*)internal->buffer)[total_bytes_read]); | |
310 rc = ov_read(vf, buffer_start_point, bytes_remaining, | |
311 ((sample->actual.format & 0x1000) ? 1 : 0), /* bigendian? */ | |
312 ((sample->actual.format & 0xFF) / 8), /* bytes per sample point */ | |
313 ((sample->actual.format & 0x8000) ? 1 : 0), /* signed data? */ | |
314 &bitstream); | |
315 /* Make sure rc > 0 because we don't accidently want | |
316 * to change the counters if there was an error | |
317 */ | |
318 if(rc > 0) | |
319 { | |
320 total_bytes_read += rc; | |
321 bytes_remaining = bytes_remaining - rc; | |
322 } | |
323 } | |
324 /* I think the minimum read size is 2, though I'm | |
325 * not sure about this. (I've hit cases where I | |
326 * couldn't read less than 4.) What I don't want to do is | |
327 * accidently claim we hit EOF when the reason rc == 0 | |
328 * is because the requested amount of data was smaller | |
329 * than the minimum packet size. | |
330 * For now, I will be conservative | |
331 * and not set the EOF flag, and let the next call to | |
332 * this function figure it out. | |
333 * I think the ERROR flag is safe to set because | |
334 * it looks like OGG simply returns 0 if the | |
335 * read size is too small. | |
336 * And in most cases for sensible buffer sizes, | |
337 * this fix will fill the buffer, | |
338 * so we can set the EAGAIN flag without worrying | |
339 * that it will always be set. | |
340 */ | |
341 if(rc < 0) | |
342 { | |
343 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
344 } | |
345 else if(rc == 0) | |
346 { | |
347 /* Do nothing for now until there is a better solution */ | |
348 /* sample->flags |= SOUND_SAMPLEFLAG_EOF; */ | |
349 } | |
350 | |
351 /* Test for a buffer underrun. It should occur less frequently | |
352 * now, but it still may happen and not necessarily mean | |
353 * anything useful. */ | |
354 if ((uint32_t) total_bytes_read < internal->buffer_size) | |
355 { | |
356 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
357 } | |
358 /* change rc to the total bytes read so function | |
359 * can return the correct value. | |
360 */ | |
361 rc = total_bytes_read; | |
362 } | |
363 | |
364 return((uint32_t) rc); | |
365 } /* OGG_read */ | |
366 | |
367 | |
368 static int OGG_rewind(Sound_Sample *sample) | |
369 { | |
370 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
371 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private; | |
372 | |
373 BAIL_IF_MACRO(ov_raw_seek(vf, 0) < 0, ERR_IO_ERROR, 0); | |
374 return(1); | |
375 } /* OGG_rewind */ | |
376 | |
377 | |
378 static int OGG_seek(Sound_Sample *sample, uint32_t ms) | |
379 { | |
380 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
381 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private; | |
382 double timepos = (((double) ms) / 1000.0); | |
383 BAIL_IF_MACRO(ov_time_seek(vf, timepos) < 0, ERR_IO_ERROR, 0); | |
384 return(1); | |
385 } /* OGG_seek */ | |
386 | |
387 #endif /* SOUND_SUPPORTS_OGG */ | |
388 | |
389 | |
390 /* end of ogg.c ... */ | |
391 |