Debian Codegraph @

code/client/snd_codec_ogg.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2005 Stuart Dalton ([email protected])
5 Copyright (C) 2005-2006 Joerg Dietrich <[email protected]>
6
7 This file is part of Quake III Arena source code.
8
9 Quake III Arena source code is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 Quake III Arena source code is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Quake III Arena source code; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ===========================================================================
23 */
24
25 // OGG support is enabled by this define
26 #ifdef USE_CODEC_VORBIS
27
28 // includes for the Q3 sound system
29 #include "client.h"
30 #include "snd_codec.h"
31
32 // includes for the OGG codec
33 #include <errno.h>
34 #define OV_EXCLUDE_STATIC_CALLBACKS
35 #include <vorbis/vorbisfile.h>
36
37 // The OGG codec can return the samples in a number of different formats,
38 // we use the standard signed short format.
39 #define OGG_SAMPLEWIDTH 2
40
41 // Q3 OGG codec
42 snd_codec_t ogg_codec =
43 {
44 "ogg",
45 S_OGG_CodecLoad,
46 S_OGG_CodecOpenStream,
47 S_OGG_CodecReadStream,
48 S_OGG_CodecCloseStream,
49 NULL
50 };
51
52 // callbacks for vobisfile
53
54 // fread() replacement
55 size_t S_OGG_Callback_read(void *ptr, size_t size, size_t nmemb, void *datasource)
56 {
57 snd_stream_t *stream;
58 int byteSize = 0;
59 int bytesRead = 0;
60 size_t nMembRead = 0;
61
62 // check if input is valid
63 if(!ptr)
64 {
65 errno = EFAULT;
66 return 0;
67 }
68
69 if(!(size && nmemb))
70 {
71 // It's not an error, caller just wants zero bytes!
72 errno = 0;
73 return 0;
74 }
75
76 if(!datasource)
77 {
78 errno = EBADF;
79 return 0;
80 }
81
82 // we use a snd_stream_t in the generic pointer to pass around
83 stream = (snd_stream_t *) datasource;
84
85 // FS_Read does not support multi-byte elements
86 byteSize = nmemb * size;
87
88 // read it with the Q3 function FS_Read()
89 bytesRead = FS_Read(ptr, byteSize, stream->file);
90
91 // update the file position
92 stream->pos += bytesRead;
93
94 // this function returns the number of elements read not the number of bytes
95 nMembRead = bytesRead / size;
96
97 // even if the last member is only read partially
98 // it is counted as a whole in the return value
99 if(bytesRead % size)
100 {
101 nMembRead++;
102 }
103
104 return nMembRead;
105 }
106
107 // fseek() replacement
108 int S_OGG_Callback_seek(void *datasource, ogg_int64_t offset, int whence)
109 {
110 snd_stream_t *stream;
111 int retVal = 0;
112
113 // check if input is valid
114 if(!datasource)
115 {
116 errno = EBADF;
117 return -1;
118 }
119
120 // snd_stream_t in the generic pointer
121 stream = (snd_stream_t *) datasource;
122
123 // we must map the whence to its Q3 counterpart
124 switch(whence)
125 {
126 case SEEK_SET :
127 {
128 // set the file position in the actual file with the Q3 function
129 retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_SET);
130
131 // something has gone wrong, so we return here
132 if(retVal < 0)
133 {
134 return retVal;
135 }
136
137 // keep track of file position
138 stream->pos = (int) offset;
139 break;
140 }
141
142 case SEEK_CUR :
143 {
144 // set the file position in the actual file with the Q3 function
145 retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_CUR);
146
147 // something has gone wrong, so we return here
148 if(retVal < 0)
149 {
150 return retVal;
151 }
152
153 // keep track of file position
154 stream->pos += (int) offset;
155 break;
156 }
157
158 case SEEK_END :
159 {
160 // set the file position in the actual file with the Q3 function
161 retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_END);
162
163 // something has gone wrong, so we return here
164 if(retVal < 0)
165 {
166 return retVal;
167 }
168
169 // keep track of file position
170 stream->pos = stream->length + (int) offset;
171 break;
172 }
173
174 default :
175 {
176 // unknown whence, so we return an error
177 errno = EINVAL;
178 return -1;
179 }
180 }
181
182 // stream->pos shouldn't be smaller than zero or bigger than the filesize
183 stream->pos = (stream->pos < 0) ? 0 : stream->pos;
184 stream->pos = (stream->pos > stream->length) ? stream->length : stream->pos;
185
186 return 0;
187 }
188
189 // fclose() replacement
190 int S_OGG_Callback_close(void *datasource)
191 {
192 // we do nothing here and close all things manually in S_OGG_CodecCloseStream()
193 return 0;
194 }
195
196 // ftell() replacement
197 long S_OGG_Callback_tell(void *datasource)
198 {
199 snd_stream_t *stream;
200
201 // check if input is valid
202 if(!datasource)
203 {
204 errno = EBADF;
205 return -1;
206 }
207
208 // snd_stream_t in the generic pointer
209 stream = (snd_stream_t *) datasource;
210
211 return (long) FS_FTell(stream->file);
212 }
213
214 // the callback structure
215 const ov_callbacks S_OGG_Callbacks =
216 {
217 &S_OGG_Callback_read,
218 &S_OGG_Callback_seek,
219 &S_OGG_Callback_close,
220 &S_OGG_Callback_tell
221 };
222
223 /*
224 =================
225 S_OGG_CodecOpenStream
226 =================
227 */
228 snd_stream_t *S_OGG_CodecOpenStream(const char *filename)
229 {
230 snd_stream_t *stream;
231
232 // OGG codec control structure
233 OggVorbis_File *vf;
234
235 // some variables used to get informations about the OGG
236 vorbis_info *OGGInfo;
237 ogg_int64_t numSamples;
238
239 // check if input is valid
240 if(!filename)
241 {
242 return NULL;
243 }
244
245 // Open the stream
246 stream = S_CodecUtilOpen(filename, &ogg_codec);
247 if(!stream)
248 {
249 return NULL;
250 }
251
252 // alloctate the OggVorbis_File
253 vf = Z_Malloc(sizeof(OggVorbis_File));
254 if(!vf)
255 {
256 S_CodecUtilClose(&stream);
257
258 return NULL;
259 }
260
261 // open the codec with our callbacks and stream as the generic pointer
262 if(ov_open_callbacks(stream, vf, NULL, 0, S_OGG_Callbacks) != 0)
263 {
264 Z_Free(vf);
265
266 S_CodecUtilClose(&stream);
267
268 return NULL;
269 }
270
271 // the stream must be seekable
272 if(!ov_seekable(vf))
273 {
274 ov_clear(vf);
275
276 Z_Free(vf);
277
278 S_CodecUtilClose(&stream);
279
280 return NULL;
281 }
282
283 // we only support OGGs with one substream
284 if(ov_streams(vf) != 1)
285 {
286 ov_clear(vf);
287
288 Z_Free(vf);
289
290 S_CodecUtilClose(&stream);
291
292 return NULL;
293 }
294
295 // get the info about channels and rate
296 OGGInfo = ov_info(vf, 0);
297 if(!OGGInfo)
298 {
299 ov_clear(vf);
300
301 Z_Free(vf);
302
303 S_CodecUtilClose(&stream);
304
305 return NULL;
306 }
307
308 // get the number of sample-frames in the OGG
309 numSamples = ov_pcm_total(vf, 0);
310
311 // fill in the info-structure in the stream
312 stream->info.rate = OGGInfo->rate;
313 stream->info.width = OGG_SAMPLEWIDTH;
314 stream->info.channels = OGGInfo->channels;
315 stream->info.samples = numSamples;
316 stream->info.size = stream->info.samples * stream->info.channels * stream->info.width;
317 stream->info.dataofs = 0;
318
319 // We use stream->pos for the file pointer in the compressed ogg file
320 stream->pos = 0;
321
322 // We use the generic pointer in stream for the OGG codec control structure
323 stream->ptr = vf;
324
325 return stream;
326 }
327
328 /*
329 =================
330 S_OGG_CodecCloseStream
331 =================
332 */
333 void S_OGG_CodecCloseStream(snd_stream_t *stream)
334 {
335 // check if input is valid
336 if(!stream)
337 {
338 return;
339 }
340
341 // let the OGG codec cleanup its stuff
342 ov_clear((OggVorbis_File *) stream->ptr);
343
344 // free the OGG codec control struct
345 Z_Free(stream->ptr);
346
347 // close the stream
348 S_CodecUtilClose(&stream);
349 }
350
351 /*
352 =================
353 S_OGG_CodecReadStream
354 =================
355 */
356 int S_OGG_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer)
357 {
358 // buffer handling
359 int bytesRead, bytesLeft, c;
360 char *bufPtr;
361
362 // Bitstream for the decoder
363 int BS = 0;
364
365 // big endian machines want their samples in big endian order
366 int IsBigEndian = 0;
367
368 # ifdef Q3_BIG_ENDIAN
369 IsBigEndian = 1;
370 # endif // Q3_BIG_ENDIAN
371
372 // check if input is valid
373 if(!(stream && buffer))
374 {
375 return 0;
376 }
377
378 if(bytes <= 0)
379 {
380 return 0;
381 }
382
383 bytesRead = 0;
384 bytesLeft = bytes;
385 bufPtr = buffer;
386
387 // cycle until we have the requested or all available bytes read
388 while(-1)
389 {
390 // read some bytes from the OGG codec
391 c = ov_read((OggVorbis_File *) stream->ptr, bufPtr, bytesLeft, IsBigEndian, OGG_SAMPLEWIDTH, 1, &BS);
392
393 // no more bytes are left
394 if(c <= 0)
395 {
396 break;
397 }
398
399 bytesRead += c;
400 bytesLeft -= c;
401 bufPtr += c;
402
403 // we have enough bytes
404 if(bytesLeft <= 0)
405 {
406 break;
407 }
408 }
409
410 return bytesRead;
411 }
412
413 /*
414 =====================================================================
415 S_OGG_CodecLoad
416
417 We handle S_OGG_CodecLoad as a special case of the streaming functions
418 where we read the whole stream at once.
419 ======================================================================
420 */
421 void *S_OGG_CodecLoad(const char *filename, snd_info_t *info)
422 {
423 snd_stream_t *stream;
424 byte *buffer;
425 int bytesRead;
426
427 // check if input is valid
428 if(!(filename && info))
429 {
430 return NULL;
431 }
432
433 // open the file as a stream
434 stream = S_OGG_CodecOpenStream(filename);
435 if(!stream)
436 {
437 return NULL;
438 }
439
440 // copy over the info
441 info->rate = stream->info.rate;
442 info->width = stream->info.width;
443 info->channels = stream->info.channels;
444 info->samples = stream->info.samples;
445 info->size = stream->info.size;
446 info->dataofs = stream->info.dataofs;
447
448 // allocate a buffer
449 // this buffer must be free-ed by the caller of this function
450 buffer = Hunk_AllocateTempMemory(info->size);
451 if(!buffer)
452 {
453 S_OGG_CodecCloseStream(stream);
454
455 return NULL;
456 }
457
458 // fill the buffer
459 bytesRead = S_OGG_CodecReadStream(stream, info->size, buffer);
460
461 // we don't even have read a single byte
462 if(bytesRead <= 0)
463 {
464 Hunk_FreeTempMemory(buffer);
465 S_OGG_CodecCloseStream(stream);
466
467 return NULL;
468 }
469
470 S_OGG_CodecCloseStream(stream);
471
472 return buffer;
473 }
474
475 #endif // USE_CODEC_VORBIS