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