Mercurial > SDL_sound_CoreAudio
annotate decoders/ogg.c @ 59:cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
chatter enabled.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 24 Sep 2001 15:29:27 +0000 |
parents | ea58bc3b15d7 |
children | 9669aa13d3e0 |
rev | line source |
---|---|
24 | 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 in the source's root directory. | |
31 * | |
32 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
33 */ | |
34 | |
35 #include <stdio.h> | |
36 #include <stdlib.h> | |
37 #include <string.h> | |
38 #include <math.h> | |
39 #include <assert.h> | |
40 #include "vorbis/codec.h" | |
41 #include "vorbis/vorbisfile.h" | |
42 | |
43 #include "SDL_sound.h" | |
44 | |
45 #define __SDL_SOUND_INTERNAL__ | |
46 #include "SDL_sound_internal.h" | |
47 | |
48 #if (!defined SOUND_SUPPORTS_OGG) | |
49 #error SOUND_SUPPORTS_OGG must be defined. | |
50 #endif | |
51 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
52 static int OGG_init(void); |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
53 static void OGG_quit(void); |
24 | 54 static int OGG_open(Sound_Sample *sample, const char *ext); |
55 static void OGG_close(Sound_Sample *sample); | |
56 static Uint32 OGG_read(Sound_Sample *sample); | |
57 | |
58 const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG = | |
59 { | |
60 { | |
61 "OGG", | |
62 "Ogg Vorbis audio through VorbisFile", | |
63 "Ryan C. Gordon <icculus@clutteredmind.org>", | |
64 "http://www.icculus.org/SDL_sound/" | |
65 }, | |
66 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
67 OGG_init, /* init() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
68 OGG_quit, /* quit() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
69 OGG_open, /* open() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
70 OGG_close, /* close() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
71 OGG_read /* read() method */ |
24 | 72 }; |
73 | |
74 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
75 static int OGG_init(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
76 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
77 return(1); /* always succeeds. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
78 } /* OGG_init */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
79 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
80 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
81 static void OGG_quit(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
82 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
83 /* it's a no-op. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
84 } /* OGG_quit */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
85 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
86 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset
|
87 |
24 | 88 /* |
89 * These are callbacks from vorbisfile that let them read data from | |
90 * a RWops... | |
91 */ | |
92 | |
93 size_t RWops_ogg_read(void *ptr, size_t size, size_t nmemb, void *datasource) | |
94 { | |
95 return((size_t) SDL_RWread((SDL_RWops *) datasource, ptr, size, nmemb)); | |
96 } /* RWops_ogg_read */ | |
97 | |
98 int RWops_ogg_seek(void *datasource, int64_t offset, int whence) | |
99 { | |
100 return(SDL_RWseek((SDL_RWops *) datasource, offset, whence)); | |
101 } /* RWops_ogg_seek */ | |
102 | |
103 int RWops_ogg_close(void *datasource) | |
104 { | |
105 /* do nothing; SDL_sound will delete the RWops at a higher level. */ | |
106 return(0); /* this is success in fclose(), so I guess that's okay. */ | |
107 } /* RWops_ogg_close */ | |
108 | |
109 long RWops_ogg_tell(void *datasource) | |
110 { | |
111 return((long) SDL_RWtell((SDL_RWops *) datasource)); | |
112 } /* RWops_ogg_tell */ | |
113 | |
114 static const ov_callbacks RWops_ogg_callbacks = | |
115 { | |
116 RWops_ogg_read, | |
117 RWops_ogg_seek, | |
118 RWops_ogg_close, | |
119 RWops_ogg_tell | |
120 }; | |
121 | |
122 | |
123 /* Return a human readable version of an VorbisFile error code... */ | |
59
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
124 #if (defined DEBUG_CHATTER) |
24 | 125 static const char *ogg_error(int errnum) |
126 { | |
127 switch(errnum) | |
128 { | |
129 case OV_EREAD: | |
130 return("i/o error"); | |
131 case OV_ENOTVORBIS: | |
132 return("not a vorbis file"); | |
133 case OV_EVERSION: | |
134 return("Vorbis version mismatch"); | |
135 case OV_EBADHEADER: | |
136 return("invalid Vorbis bitstream header"); | |
137 case OV_EFAULT: | |
138 return("internal logic fault in Vorbis library"); | |
139 } /* switch */ | |
140 | |
141 return("unknown error"); | |
142 } /* ogg_error */ | |
59
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
143 #endif |
24 | 144 |
145 static __inline__ void output_ogg_comments(OggVorbis_File *vf) | |
146 { | |
147 #if (defined DEBUG_CHATTER) | |
148 int i; | |
149 vorbis_comment *vc = ov_comment(vf, -1); | |
150 | |
151 if (vc == NULL) | |
152 return; | |
153 | |
59
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
154 SNDDBG(("OGG: vendor == [%s].\n", vc->vendor)); |
24 | 155 for (i = 0; i < vc->comments; i++) |
156 { | |
59
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
157 SNDDBG(("OGG: user comment [%s].\n", vc->user_comments[i])); |
24 | 158 } /* for */ |
159 #endif | |
160 } /* output_ogg_comments */ | |
161 | |
162 | |
163 static int OGG_open(Sound_Sample *sample, const char *ext) | |
164 { | |
165 int rc; | |
166 OggVorbis_File *vf; | |
167 vorbis_info *info; | |
168 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
169 | |
170 vf = (OggVorbis_File *) malloc(sizeof (OggVorbis_File)); | |
171 BAIL_IF_MACRO(vf == NULL, ERR_OUT_OF_MEMORY, 0); | |
172 | |
173 rc = ov_open_callbacks(internal->rw, vf, NULL, 0, RWops_ogg_callbacks); | |
174 if (rc != 0) | |
175 { | |
59
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
176 SNDDBG(("OGG: can't grok data. reason: [%s].\n", ogg_error(rc))); |
24 | 177 free(vf); |
178 BAIL_MACRO("OGG: Not valid Ogg Vorbis data.", 0); | |
179 } /* if */ | |
180 | |
181 info = ov_info(vf, -1); | |
182 if (info == NULL) | |
183 { | |
184 ov_clear(vf); | |
185 free(vf); | |
186 BAIL_MACRO("OGG: failed to retrieve bitstream info", 0); | |
187 } /* if */ | |
188 | |
59
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
189 SNDDBG(("OGG: Accepting data stream.\n")); |
24 | 190 |
191 output_ogg_comments(vf); | |
59
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
192 SNDDBG(("OGG: bitstream version == (%d).\n", info->version)); |
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
193 SNDDBG(("OGG: bitstream channels == (%d).\n", info->channels)); |
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
194 SNDDBG(("OGG: bitstream sampling rate == (%ld).\n", info->rate)); |
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
195 SNDDBG(("OGG: seekable == {%s}.\n", ov_seekable(vf) ? "TRUE" : "FALSE")); |
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
196 SNDDBG(("OGG: number of logical bitstreams == (%ld).\n", ov_streams(vf))); |
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
197 SNDDBG(("OGG: serial number == (%ld).\n", ov_serialnumber(vf, -1))); |
cd91e1857b42
Changed _D macro to SNDDBG, and fixed a warning if compiling without debug
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
198 SNDDBG(("OGG: total seconds of sample == (%f).\n", ov_time_total(vf, -1))); |
24 | 199 |
200 internal->decoder_private = vf; | |
201 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
202 sample->actual.rate = (Uint32) info->rate; | |
203 sample->actual.channels = (Uint8) info->channels; | |
204 | |
205 /* | |
206 * Since we might have more than one logical bitstream in the OGG file, | |
207 * and these bitstreams may be in different formats, we might be | |
208 * converting two or three times: once in vorbisfile, once again in | |
209 * SDL_sound, and perhaps a third time to get it to the sound device's | |
210 * format. That's wickedly inefficient. | |
211 * | |
212 * To combat this a little, if the user specified a desired format, we | |
213 * claim that to be the "actual" format of the collection of logical | |
214 * bitstreams. This means that VorbisFile will do a conversion as | |
215 * necessary, and SDL_sound will not. If the user didn't specify a | |
216 * desired format, then we pretend the "actual" format is something that | |
217 * OGG files are apparently commonly encoded in. | |
218 */ | |
219 sample->actual.format = (sample->desired.format == 0) ? | |
220 AUDIO_S16LSB : sample->desired.format; | |
221 return(1); | |
222 } /* OGG_open */ | |
223 | |
224 | |
225 static void OGG_close(Sound_Sample *sample) | |
226 { | |
227 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
228 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private; | |
229 ov_clear(vf); | |
230 free(vf); | |
231 } /* OGG_close */ | |
232 | |
233 | |
234 static Uint32 OGG_read(Sound_Sample *sample) | |
235 { | |
236 int rc; | |
237 int bitstream; | |
238 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
239 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private; | |
240 | |
241 rc = ov_read(vf, internal->buffer, internal->buffer_size, | |
242 ((sample->actual.format & 0x1000) ? 1 : 0), /* bigendian? */ | |
243 ((sample->actual.format & 0xFF) / 8), /* bytes per sample point */ | |
244 ((sample->actual.format & 0x8000) ? 1 : 0), /* signed data? */ | |
245 &bitstream); | |
246 | |
247 /* Make sure the read went smoothly... */ | |
248 if (rc == 0) | |
249 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
250 | |
251 else if (rc < 0) | |
252 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
253 | |
254 /* (next call this EAGAIN may turn into an EOF or error.) */ | |
255 else if ((Uint32) rc < internal->buffer_size) | |
256 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
257 | |
258 return((Uint32) rc); | |
259 } /* OGG_read */ | |
260 | |
261 /* end of ogg.c ... */ | |
262 |