2 Copyright (C) 2003-2004 Mathieu Olivier
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
31 =================================================================
33 Minimal set of definitions from the Ogg Vorbis lib
34 (C) COPYRIGHT 1994-2001 by the XIPHOPHORUS Company
37 WARNING: for a matter of simplicity, several pointer types are
38 casted to "void*", and most enumerated values are not included
40 =================================================================
44 typedef __int64 ogg_int64_t;
46 typedef long long ogg_int64_t;
51 size_t (*read_func) (void *ptr, size_t size, size_t nmemb, void *datasource);
52 int (*seek_func) (void *datasource, ogg_int64_t offset, int whence);
53 int (*close_func) (void *datasource);
54 long (*tell_func) (void *datasource);
82 unsigned char *body_data;
87 ogg_int64_t *granule_vals;
92 unsigned char header[282];
99 ogg_int64_t granulepos;
117 ogg_int64_t granulepos;
118 ogg_int64_t sequence;
119 ogg_int64_t glue_bits;
120 ogg_int64_t time_bits;
121 ogg_int64_t floor_bits;
122 ogg_int64_t res_bits;
130 unsigned char *buffer;
145 ogg_int64_t granulepos;
146 ogg_int64_t sequence;
147 vorbis_dsp_state *vd;
152 void *reap; // VOIDED POINTER
168 ogg_int64_t *offsets;
169 ogg_int64_t *dataoffsets;
171 ogg_int64_t *pcmlengths;
173 void *vc; // VOIDED POINTER
174 ogg_int64_t pcm_offset;
176 long current_serialno;
183 ov_callbacks callbacks;
188 =================================================================
190 DarkPlaces definitions
192 =================================================================
195 // Functions exported from the vorbisfile library
196 static int (*qov_clear) (OggVorbis_File *vf);
197 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
198 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
199 char *initial, long ibytes,
200 ov_callbacks callbacks);
201 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
202 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
203 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
204 int bigendianp,int word,int sgned,int *bitstream);
206 static dllfunction_t oggvorbisfuncs[] =
208 {"ov_clear", (void **) &qov_clear},
209 {"ov_info", (void **) &qov_info},
210 {"ov_open_callbacks", (void **) &qov_open_callbacks},
211 {"ov_pcm_seek", (void **) &qov_pcm_seek},
212 {"ov_pcm_total", (void **) &qov_pcm_total},
213 {"ov_read", (void **) &qov_read},
217 // Handle for the Vorbisfile DLL
218 static dllhandle_t vf_dll = NULL;
223 ogg_int64_t ind, buffsize;
227 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
229 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
232 remain = ov_decode->buffsize - ov_decode->ind;
235 len = remain - remain % size;
237 memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
238 ov_decode->ind += len;
243 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
245 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
252 offset += ov_decode->ind;
255 offset += ov_decode->buffsize;
260 if (offset < 0 || offset > ov_decode->buffsize)
263 ov_decode->ind = offset;
267 static int ovcb_close (void *ov_decode)
272 static long ovcb_tell (void *ov_decode)
274 return ((ov_decode_t*)ov_decode)->ind;
279 =================================================================
283 =================================================================
290 Try to load the VorbisFile DLL
293 qboolean OGG_OpenLibrary (void)
302 dllname = "vorbisfile.dll";
304 dllname = "libvorbisfile.so";
308 if (! Sys_LoadLibrary (dllname, &vf_dll, oggvorbisfuncs))
310 Con_Printf ("Ogg Vorbis support disabled\n");
314 Con_Printf ("Ogg Vorbis support enabled\n");
323 Unload the VorbisFile DLL
326 void OGG_CloseLibrary (void)
328 Sys_UnloadLibrary (&vf_dll);
333 =================================================================
337 =================================================================
340 #define STREAM_BUFFER_SIZE (128 * 1024)
342 // Note: it must be able to contain enough samples at 48 KHz (max speed)
343 // to fill STREAM_BUFFER_SIZE bytes of samples at 8 KHz (min speed)
344 // TODO: dynamically allocate this buffer depending on the shm and min sound speeds
345 static qbyte resampling_buffer [STREAM_BUFFER_SIZE * (48000 / 8000)];
348 // Per-sfx data structure
353 } ogg_stream_persfx_t;
355 // Per-channel data structure
359 ov_decode_t ov_decode;
362 sfxbuffer_t sb; // must be at the end due to its dynamically allocated size
363 } ogg_stream_perchannel_t;
366 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
373 static const sfxbuffer_t* OGG_FetchSound (channel_t* ch, unsigned int start, unsigned int nbsamples)
375 ogg_stream_perchannel_t* per_ch;
377 int newlength, done, ret, bigendian;
380 per_ch = ch->fetcher_data;
382 // If there's no fetcher structure attached to the channel yet
387 ogg_stream_persfx_t* per_sfx;
390 per_ch = Mem_Alloc (sfx->mempool, sizeof (*per_ch) - sizeof (per_ch->sb.data) + STREAM_BUFFER_SIZE);
391 per_sfx = sfx->fetcher_data;
393 // Open it with the VorbisFile API
394 per_ch->ov_decode.buffer = per_sfx->file;
395 per_ch->ov_decode.ind = 0;
396 per_ch->ov_decode.buffsize = per_sfx->filesize;
397 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
399 Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", sfx->name);
404 // Get the stream information
405 vi = qov_info (&per_ch->vf, -1);
406 per_ch->format.speed = vi->rate;
407 per_ch->format.width = sfx->format.width;
408 per_ch->format.channels = sfx->format.channels;
410 per_ch->sb.offset = 0;
411 per_ch->sb.length = 0;
414 ch->fetcher_data = per_ch;
418 factor = per_ch->format.width * per_ch->format.channels;
420 // If the stream buffer can't contain that much samples anyway
421 if (nbsamples * factor > STREAM_BUFFER_SIZE)
423 Con_Printf ("OGG_FetchSound: stream buffer too small (%u bytes required)\n", nbsamples * factor);
427 // If the data we need has already been decompressed in the sfxbuffer, just return it
428 if (sb->offset <= start && sb->offset + sb->length >= start + nbsamples)
431 newlength = sb->offset + sb->length - start;
433 // If we need to skip some data before decompressing the rest, or if the stream has looped
434 if (newlength < 0 || sb->offset > start)
436 if (qov_pcm_seek (&per_ch->vf, (ogg_int64_t)start) != 0)
443 // Else, move forward the samples we need to keep in the sfxbuffer
446 memmove (sb->data, sb->data + (start - sb->offset) * factor, newlength * factor);
448 sb->length = newlength;
451 // How many free bytes do we have in the sfxbuffer now?
452 newlength = STREAM_BUFFER_SIZE - (newlength * factor);
454 // Decompress in the resampling_buffer to get STREAM_BUFFER_SIZE samples after resampling
455 #if BYTE_ORDER == LITTLE_ENDIAN
461 while ((ret = qov_read (&per_ch->vf, &resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
464 // Resample in the sfxbuffer
465 newlength = ResampleSfx (resampling_buffer, (size_t)done / factor, &per_ch->format, sb->data + sb->length * factor, ch->sfx->name);
466 sb->length += newlength;
477 static void OGG_FetchEnd (channel_t* ch)
479 ogg_stream_perchannel_t* per_ch;
481 per_ch = ch->fetcher_data;
484 // Free the ogg vorbis decoder
485 qov_clear (&per_ch->vf);
488 ch->fetcher_data = NULL;
492 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd };
499 Load an Ogg Vorbis file into memory
502 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *s)
505 ov_decode_t ov_decode;
513 Mem_FreePool (&s->mempool);
514 s->mempool = Mem_AllocPool (s->name);
517 data = FS_LoadFile (filename, s->mempool, false);
520 Mem_FreePool (&s->mempool);
524 Con_DPrintf ("Loading Ogg Vorbis file \"%s\"\n", filename);
526 // Open it with the VorbisFile API
527 ov_decode.buffer = data;
529 ov_decode.buffsize = fs_filesize;
530 if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
532 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
533 Mem_FreePool (&s->mempool);
537 // Get the stream information
538 vi = qov_info (&vf, -1);
539 if (vi->channels < 1 || vi->channels > 2)
541 Con_Printf("%s has an unsupported number of channels (%i)\n",
542 s->name, vi->channels);
544 Mem_FreePool (&s->mempool);
548 len = qov_pcm_total (&vf, -1) * vi->channels * 2; // 16 bits => "* 2"
550 // Decide if we go for a stream or a simple PCM cache
551 if (snd_streaming.integer && len > fs_filesize + 3 * STREAM_BUFFER_SIZE)
553 ogg_stream_persfx_t* per_sfx;
555 Con_DPrintf ("\"%s\" will be streamed\n", filename);
556 per_sfx = Mem_Alloc (s->mempool, sizeof (*per_sfx));
557 per_sfx->file = data;
558 per_sfx->filesize = fs_filesize;
559 s->fetcher_data = per_sfx;
560 s->fetcher = &ogg_fetcher;
561 s->format.speed = shm->format.speed;
562 s->format.width = 2; // We always work with 16 bits samples
563 s->format.channels = vi->channels;
565 s->flags |= SFXFLAG_STREAMED;
566 s->total_length = (size_t)len / (vi->channels * 2) * (float)(shm->format.speed / vi->rate);
576 Con_DPrintf ("\"%s\" will be streamed\n", filename);
579 buff = Mem_Alloc (s->mempool, (int)len);
582 #if BYTE_ORDER == LITTLE_ENDIAN
587 while ((ret = qov_read (&vf, &buff[done], (int)(len - done), bigendian, 2, 1, &bs)) > 0)
590 // Calculate resampled length
591 len = (double)done * (double)shm->format.speed / (double)vi->rate;
594 sb = Mem_Alloc (s->mempool, (size_t)len + sizeof (*sb) - sizeof (sb->data));
595 s->fetcher_data = sb;
596 s->fetcher = &wav_fetcher;
597 s->format.speed = vi->rate;
598 s->format.width = 2; // We always work with 16 bits samples
599 s->format.channels = vi->channels;
601 s->flags &= ~SFXFLAG_STREAMED;
603 sb->length = ResampleSfx (buff, (size_t)done / (vi->channels * 2), &s->format, sb->data, s->name);
604 s->format.speed = shm->format.speed;
605 s->total_length = sb->length;