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
30 =================================================================
32 Minimal set of definitions from the Ogg Vorbis lib
33 (C) COPYRIGHT 1994-2001 by the XIPHOPHORUS Company
36 WARNING: for a matter of simplicity, several pointer types are
37 casted to "void*", and most enumerated values are not included
39 =================================================================
43 typedef __int64 ogg_int64_t;
45 typedef long long ogg_int64_t;
50 size_t (*read_func) (void *ptr, size_t size, size_t nmemb, void *datasource);
51 int (*seek_func) (void *datasource, ogg_int64_t offset, int whence);
52 int (*close_func) (void *datasource);
53 long (*tell_func) (void *datasource);
81 unsigned char *body_data;
86 ogg_int64_t *granule_vals;
91 unsigned char header[282];
98 ogg_int64_t granulepos;
116 ogg_int64_t granulepos;
117 ogg_int64_t sequence;
118 ogg_int64_t glue_bits;
119 ogg_int64_t time_bits;
120 ogg_int64_t floor_bits;
121 ogg_int64_t res_bits;
129 unsigned char *buffer;
144 ogg_int64_t granulepos;
145 ogg_int64_t sequence;
146 vorbis_dsp_state *vd;
151 void *reap; // VOIDED POINTER
167 ogg_int64_t *offsets;
168 ogg_int64_t *dataoffsets;
170 ogg_int64_t *pcmlengths;
172 void *vc; // VOIDED POINTER
173 ogg_int64_t pcm_offset;
175 long current_serialno;
182 ov_callbacks callbacks;
187 =================================================================
189 DarkPlaces definitions
191 =================================================================
194 // Functions exported from the vorbisfile library
195 static int (*qov_clear) (OggVorbis_File *vf);
196 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
197 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
198 char *initial, long ibytes,
199 ov_callbacks callbacks);
200 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
201 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
202 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
203 int bigendianp,int word,int sgned,int *bitstream);
205 static dllfunction_t oggvorbisfuncs[] =
207 {"ov_clear", (void **) &qov_clear},
208 {"ov_info", (void **) &qov_info},
209 {"ov_open_callbacks", (void **) &qov_open_callbacks},
210 {"ov_pcm_seek", (void **) &qov_pcm_seek},
211 {"ov_pcm_total", (void **) &qov_pcm_total},
212 {"ov_read", (void **) &qov_read},
216 // Handle for the Vorbisfile DLL
217 static dllhandle_t vf_dll = NULL;
222 ogg_int64_t ind, buffsize;
226 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
228 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
231 remain = ov_decode->buffsize - ov_decode->ind;
234 len = remain - remain % size;
236 memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
237 ov_decode->ind += len;
242 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
244 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
251 offset += ov_decode->ind;
254 offset += ov_decode->buffsize;
259 if (offset < 0 || offset > ov_decode->buffsize)
262 ov_decode->ind = offset;
266 static int ovcb_close (void *ov_decode)
271 static long ovcb_tell (void *ov_decode)
273 return ((ov_decode_t*)ov_decode)->ind;
278 =================================================================
282 =================================================================
289 Try to load the VorbisFile DLL
292 qboolean OGG_OpenLibrary (void)
295 const dllfunction_t *func;
302 dllname = "vorbisfile.dll";
304 dllname = "libvorbisfile.so";
308 for (func = oggvorbisfuncs; func && func->name != NULL; func++)
309 *func->funcvariable = NULL;
312 if (! (vf_dll = Sys_LoadLibrary (dllname)))
314 Con_DPrintf("Can't find %s. Ogg Vorbis support disabled\n", dllname);
318 // Get the function adresses
319 for (func = oggvorbisfuncs; func && func->name != NULL; func++)
320 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (vf_dll, func->name)))
322 Con_Printf("missing function \"%s\" - broken Ogg Vorbis library!\n", func->name);
327 Con_DPrintf("%s loaded. Ogg Vorbis support enabled\n", dllname);
336 Unload the VorbisFile DLL
339 void OGG_CloseLibrary (void)
344 Sys_UnloadLibrary (vf_dll);
350 =================================================================
354 =================================================================
357 #define STREAM_BUFFER_SIZE (128 * 1024)
359 // Note: it must be able to contain enough samples at 48 KHz (max speed)
360 // to fill STREAM_BUFFER_SIZE bytes of samples at 8 KHz (min speed)
361 // TODO: dynamically allocate this buffer depending on the shm and min sound speeds
362 static qbyte resampling_buffer [STREAM_BUFFER_SIZE * (48000 / 8000)];
365 // Per-sfx data structure
370 } ogg_stream_persfx_t;
372 // Per-channel data structure
376 ov_decode_t ov_decode;
379 sfxbuffer_t sb; // must be at the end due to its dynamically allocated size
380 } ogg_stream_perchannel_t;
383 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
390 static const sfxbuffer_t* OGG_FetchSound (channel_t* ch, unsigned int start, unsigned int nbsamples)
392 ogg_stream_perchannel_t* per_ch;
394 int newlength, done, ret, bigendian;
397 per_ch = ch->fetcher_data;
399 // If there's no fetcher structure attached to the channel yet
404 ogg_stream_persfx_t* per_sfx;
407 per_ch = Mem_Alloc (sfx->mempool, sizeof (*per_ch) - sizeof (per_ch->sb.data) + STREAM_BUFFER_SIZE);
408 per_sfx = sfx->fetcher_data;
410 // Open it with the VorbisFile API
411 per_ch->ov_decode.buffer = per_sfx->file;
412 per_ch->ov_decode.ind = 0;
413 per_ch->ov_decode.buffsize = per_sfx->filesize;
414 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
416 Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", sfx->name);
421 // Get the stream information
422 vi = qov_info (&per_ch->vf, -1);
423 per_ch->format.speed = vi->rate;
424 per_ch->format.width = sfx->format.width;
425 per_ch->format.channels = sfx->format.channels;
427 per_ch->sb.offset = 0;
428 per_ch->sb.length = 0;
431 ch->fetcher_data = per_ch;
436 // If the data we need has already been decompressed in the sfxbuffer, just return it
437 if (sb->offset <= start && sb->offset + sb->length >= start + nbsamples)
440 newlength = sb->offset + sb->length - start;
441 factor = per_ch->format.width * per_ch->format.channels;
443 // If we need to skip some data before decompressing the rest, or if the stream has looped
444 if (newlength < 0 || sb->offset > start)
446 if (qov_pcm_seek (&per_ch->vf, (ogg_int64_t)start) != 0)
453 // Else, move forward the samples we need to keep in the sfxbuffer
456 memmove (sb->data, sb->data + (start - sb->offset) * factor, newlength * factor);
458 sb->length = newlength;
461 // How many free bytes do we have in the sfxbuffer now?
462 newlength = STREAM_BUFFER_SIZE - (newlength * factor);
464 // Decompress in the resampling_buffer to get STREAM_BUFFER_SIZE samples after resampling
465 #if BYTE_ORDER == LITTLE_ENDIAN
471 while ((ret = qov_read (&per_ch->vf, &resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
474 // Resample in the sfxbuffer
475 newlength = ResampleSfx (resampling_buffer, (size_t)done / factor, &per_ch->format, sb->data + sb->length * factor, ch->sfx->name);
476 sb->length += newlength;
487 static void OGG_FetchEnd (channel_t* ch)
489 ogg_stream_perchannel_t* per_ch;
491 per_ch = ch->fetcher_data;
494 // Free the ogg vorbis decoder
495 qov_clear (&per_ch->vf);
498 ch->fetcher_data = NULL;
502 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd };
503 extern snd_fetcher_t wav_fetcher;
510 Load an Ogg Vorbis file into memory
513 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *s)
516 ov_decode_t ov_decode;
524 Mem_FreePool (&s->mempool);
525 s->mempool = Mem_AllocPool (s->name);
528 data = FS_LoadFile (filename, s->mempool, false);
531 Mem_FreePool (&s->mempool);
535 Con_DPrintf ("Loading Ogg Vorbis file \"%s\"\n", filename);
537 // Open it with the VorbisFile API
538 ov_decode.buffer = data;
540 ov_decode.buffsize = fs_filesize;
541 if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
543 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
544 Mem_FreePool (&s->mempool);
548 // Get the stream information
549 vi = qov_info (&vf, -1);
550 if (vi->channels < 1 || vi->channels > 2)
552 Con_Printf("%s has an unsupported number of channels (%i)\n",
553 s->name, vi->channels);
555 Mem_FreePool (&s->mempool);
559 len = qov_pcm_total (&vf, -1) * vi->channels * 2; // 16 bits => "* 2"
561 // Decide if we go for a stream or a simple PCM cache
562 if (snd_streaming.integer && len > fs_filesize + 3 * STREAM_BUFFER_SIZE)
564 ogg_stream_persfx_t* per_sfx;
566 Con_DPrintf ("\"%s\" will be streamed\n", filename);
567 per_sfx = Mem_Alloc (s->mempool, sizeof (*per_sfx));
568 per_sfx->file = data;
569 per_sfx->filesize = fs_filesize;
570 s->fetcher_data = per_sfx;
571 s->fetcher = &ogg_fetcher;
572 s->format.speed = shm->format.speed;
573 s->format.width = 2; // We always work with 16 bits samples
574 s->format.channels = vi->channels;
576 s->total_length = (size_t)len / (vi->channels * 2) * (float)(shm->format.speed / vi->rate);
586 Con_DPrintf ("\"%s\" will be streamed\n", filename);
589 buff = Mem_Alloc (s->mempool, (int)len);
592 #if BYTE_ORDER == LITTLE_ENDIAN
597 while ((ret = qov_read (&vf, &buff[done], (int)(len - done), bigendian, 2, 1, &bs)) > 0)
600 // Calculate resampled length
601 len = (double)done * (double)shm->format.speed / (double)vi->rate;
604 sb = Mem_Alloc (s->mempool, (size_t)len + sizeof (*sb) - sizeof (sb->data));
605 s->fetcher_data = sb;
606 s->fetcher = &wav_fetcher;
607 s->format.speed = vi->rate;
608 s->format.width = 2; // We always work with 16 bits samples
609 s->format.channels = vi->channels;
612 sb->length = ResampleSfx (buff, (size_t)done / (vi->channels * 2), &s->format, sb->data, s->name);
613 s->format.speed = shm->format.speed;
614 s->total_length = sb->length;