comparison decoders/ogg.c @ 24:0fc0f91b1b84

Initial add.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 19 Sep 2001 14:02:56 +0000
parents
children ea58bc3b15d7
comparison
equal deleted inserted replaced
23:06a009a1ec9e 24:0fc0f91b1b84
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
52
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
66 OGG_open, /* open() method */
67 OGG_close, /* close() method */
68 OGG_read /* read() method */
69 };
70
71
72 /*
73 * These are callbacks from vorbisfile that let them read data from
74 * a RWops...
75 */
76
77 size_t RWops_ogg_read(void *ptr, size_t size, size_t nmemb, void *datasource)
78 {
79 return((size_t) SDL_RWread((SDL_RWops *) datasource, ptr, size, nmemb));
80 } /* RWops_ogg_read */
81
82 int RWops_ogg_seek(void *datasource, int64_t offset, int whence)
83 {
84 return(SDL_RWseek((SDL_RWops *) datasource, offset, whence));
85 } /* RWops_ogg_seek */
86
87 int RWops_ogg_close(void *datasource)
88 {
89 /* do nothing; SDL_sound will delete the RWops at a higher level. */
90 return(0); /* this is success in fclose(), so I guess that's okay. */
91 } /* RWops_ogg_close */
92
93 long RWops_ogg_tell(void *datasource)
94 {
95 return((long) SDL_RWtell((SDL_RWops *) datasource));
96 } /* RWops_ogg_tell */
97
98 static const ov_callbacks RWops_ogg_callbacks =
99 {
100 RWops_ogg_read,
101 RWops_ogg_seek,
102 RWops_ogg_close,
103 RWops_ogg_tell
104 };
105
106
107 /* Return a human readable version of an VorbisFile error code... */
108 static const char *ogg_error(int errnum)
109 {
110 switch(errnum)
111 {
112 case OV_EREAD:
113 return("i/o error");
114 case OV_ENOTVORBIS:
115 return("not a vorbis file");
116 case OV_EVERSION:
117 return("Vorbis version mismatch");
118 case OV_EBADHEADER:
119 return("invalid Vorbis bitstream header");
120 case OV_EFAULT:
121 return("internal logic fault in Vorbis library");
122 } /* switch */
123
124 return("unknown error");
125 } /* ogg_error */
126
127
128 static __inline__ void output_ogg_comments(OggVorbis_File *vf)
129 {
130 #if (defined DEBUG_CHATTER)
131 int i;
132 vorbis_comment *vc = ov_comment(vf, -1);
133
134 if (vc == NULL)
135 return;
136
137 _D(("OGG: vendor == [%s].\n", vc->vendor));
138 for (i = 0; i < vc->comments; i++)
139 {
140 _D(("OGG: user comment [%s].\n", vc->user_comments[i]));
141 } /* for */
142 #endif
143 } /* output_ogg_comments */
144
145
146 static int OGG_open(Sound_Sample *sample, const char *ext)
147 {
148 int rc;
149 OggVorbis_File *vf;
150 vorbis_info *info;
151 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
152
153 vf = (OggVorbis_File *) malloc(sizeof (OggVorbis_File));
154 BAIL_IF_MACRO(vf == NULL, ERR_OUT_OF_MEMORY, 0);
155
156 rc = ov_open_callbacks(internal->rw, vf, NULL, 0, RWops_ogg_callbacks);
157 if (rc != 0)
158 {
159 _D(("OGG: can't grok data. reason: [%s].\n", ogg_error(rc)));
160 free(vf);
161 BAIL_MACRO("OGG: Not valid Ogg Vorbis data.", 0);
162 } /* if */
163
164 info = ov_info(vf, -1);
165 if (info == NULL)
166 {
167 ov_clear(vf);
168 free(vf);
169 BAIL_MACRO("OGG: failed to retrieve bitstream info", 0);
170 } /* if */
171
172 _D(("OGG: Accepting data stream.\n"));
173
174 output_ogg_comments(vf);
175 _D(("OGG: bitstream version == (%d).\n", info->version));
176 _D(("OGG: bitstream channels == (%d).\n", info->channels));
177 _D(("OGG: bitstream sampling rate == (%ld).\n", info->rate));
178 _D(("OGG: seekable == {%s}.\n", ov_seekable(vf) ? "TRUE" : "FALSE"));
179 _D(("OGG: number of logical bitstreams == (%ld).\n", ov_streams(vf)));
180 _D(("OGG: serial number == (%ld).\n", ov_serialnumber(vf, -1)));
181 _D(("OGG: total seconds of sample == (%f).\n", ov_time_total(vf, -1)));
182
183 internal->decoder_private = vf;
184 sample->flags = SOUND_SAMPLEFLAG_NONE;
185 sample->actual.rate = (Uint32) info->rate;
186 sample->actual.channels = (Uint8) info->channels;
187
188 /*
189 * Since we might have more than one logical bitstream in the OGG file,
190 * and these bitstreams may be in different formats, we might be
191 * converting two or three times: once in vorbisfile, once again in
192 * SDL_sound, and perhaps a third time to get it to the sound device's
193 * format. That's wickedly inefficient.
194 *
195 * To combat this a little, if the user specified a desired format, we
196 * claim that to be the "actual" format of the collection of logical
197 * bitstreams. This means that VorbisFile will do a conversion as
198 * necessary, and SDL_sound will not. If the user didn't specify a
199 * desired format, then we pretend the "actual" format is something that
200 * OGG files are apparently commonly encoded in.
201 */
202 sample->actual.format = (sample->desired.format == 0) ?
203 AUDIO_S16LSB : sample->desired.format;
204 return(1);
205 } /* OGG_open */
206
207
208 static void OGG_close(Sound_Sample *sample)
209 {
210 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
211 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private;
212 ov_clear(vf);
213 free(vf);
214 } /* OGG_close */
215
216
217 static Uint32 OGG_read(Sound_Sample *sample)
218 {
219 int rc;
220 int bitstream;
221 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
222 OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private;
223
224 rc = ov_read(vf, internal->buffer, internal->buffer_size,
225 ((sample->actual.format & 0x1000) ? 1 : 0), /* bigendian? */
226 ((sample->actual.format & 0xFF) / 8), /* bytes per sample point */
227 ((sample->actual.format & 0x8000) ? 1 : 0), /* signed data? */
228 &bitstream);
229
230 /* Make sure the read went smoothly... */
231 if (rc == 0)
232 sample->flags |= SOUND_SAMPLEFLAG_EOF;
233
234 else if (rc < 0)
235 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
236
237 /* (next call this EAGAIN may turn into an EOF or error.) */
238 else if ((Uint32) rc < internal->buffer_size)
239 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
240
241 return((Uint32) rc);
242 } /* OGG_read */
243
244 /* end of ogg.c ... */
245