2 Copyright (C) 2003-2005 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
32 =================================================================
34 Minimal set of definitions from the Ogg Vorbis lib
35 (C) COPYRIGHT 1994-2001 by the XIPHOPHORUS Company
38 WARNING: for a matter of simplicity, several pointer types are
39 casted to "void*", and most enumerated values are not included
41 =================================================================
45 typedef __int64 ogg_int64_t;
47 typedef long long ogg_int64_t;
52 size_t (*read_func) (void *ptr, size_t size, size_t nmemb, void *datasource);
53 int (*seek_func) (void *datasource, ogg_int64_t offset, int whence);
54 int (*close_func) (void *datasource);
55 long (*tell_func) (void *datasource);
83 unsigned char *body_data;
88 ogg_int64_t *granule_vals;
93 unsigned char header[282];
100 ogg_int64_t granulepos;
118 ogg_int64_t granulepos;
119 ogg_int64_t sequence;
120 ogg_int64_t glue_bits;
121 ogg_int64_t time_bits;
122 ogg_int64_t floor_bits;
123 ogg_int64_t res_bits;
131 unsigned char *buffer;
146 ogg_int64_t granulepos;
147 ogg_int64_t sequence;
148 vorbis_dsp_state *vd;
153 void *reap; // VOIDED POINTER
169 ogg_int64_t *offsets;
170 ogg_int64_t *dataoffsets;
172 ogg_int64_t *pcmlengths;
174 void *vc; // VOIDED POINTER
175 ogg_int64_t pcm_offset;
177 long current_serialno;
184 ov_callbacks callbacks;
189 =================================================================
191 DarkPlaces definitions
193 =================================================================
196 // Functions exported from the vorbisfile library
197 static int (*qov_clear) (OggVorbis_File *vf);
198 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
199 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
200 char *initial, long ibytes,
201 ov_callbacks callbacks);
202 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
203 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
204 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
205 int bigendianp,int word,int sgned,int *bitstream);
207 static dllfunction_t oggvorbisfuncs[] =
209 {"ov_clear", (void **) &qov_clear},
210 {"ov_info", (void **) &qov_info},
211 {"ov_open_callbacks", (void **) &qov_open_callbacks},
212 {"ov_pcm_seek", (void **) &qov_pcm_seek},
213 {"ov_pcm_total", (void **) &qov_pcm_total},
214 {"ov_read", (void **) &qov_read},
218 // Handles for the Vorbis and Vorbisfile DLLs
219 static dllhandle_t vo_dll = NULL;
220 static dllhandle_t vf_dll = NULL;
224 unsigned char *buffer;
225 ogg_int64_t ind, buffsize;
229 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
231 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
234 remain = ov_decode->buffsize - ov_decode->ind;
237 len = remain - remain % size;
239 memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
240 ov_decode->ind += len;
245 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
247 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
254 offset += ov_decode->ind;
257 offset += ov_decode->buffsize;
262 if (offset < 0 || offset > ov_decode->buffsize)
265 ov_decode->ind = offset;
269 static int ovcb_close (void *ov_decode)
274 static long ovcb_tell (void *ov_decode)
276 return ((ov_decode_t*)ov_decode)->ind;
281 =================================================================
285 =================================================================
292 Try to load the VorbisFile DLL
295 qboolean OGG_OpenLibrary (void)
297 const char* dllnames_vo [] =
303 #elif defined(MACOSX)
311 const char* dllnames_vf [] =
317 #elif defined(MACOSX)
318 "libvorbisfile.dylib",
320 "libvorbisfile.so.3",
330 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
331 if (COM_CheckParm("-novorbis"))
335 // We need to load both by hand because some OSes seem to not load
336 // the vorbis DLL automatically when loading the VorbisFile DLL
337 if (! Sys_LoadLibrary (dllnames_vo, &vo_dll, NULL) ||
338 ! Sys_LoadLibrary (dllnames_vf, &vf_dll, oggvorbisfuncs))
340 Sys_UnloadLibrary (&vo_dll);
341 Con_Printf ("Ogg Vorbis support disabled\n");
345 Con_Printf ("Ogg Vorbis support enabled\n");
354 Unload the VorbisFile DLL
357 void OGG_CloseLibrary (void)
359 Sys_UnloadLibrary (&vf_dll);
360 Sys_UnloadLibrary (&vo_dll);
365 =================================================================
369 =================================================================
372 #define STREAM_BUFFER_DURATION 1.5f // 1.5 sec
373 #define STREAM_BUFFER_SIZE(format_ptr) (ceil (STREAM_BUFFER_DURATION * ((format_ptr)->speed * (format_ptr)->width * (format_ptr)->channels)))
375 // We work with 1 sec sequences, so this buffer must be able to contain
376 // 1 sec of sound of the highest quality (48 KHz, 16 bit samples, stereo)
377 static unsigned char resampling_buffer [48000 * 2 * 2];
380 // Per-sfx data structure
386 } ogg_stream_persfx_t;
388 // Per-channel data structure
392 ov_decode_t ov_decode;
394 sfxbuffer_t sb; // must be at the end due to its dynamically allocated size
395 } ogg_stream_perchannel_t;
398 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
405 static const sfxbuffer_t* OGG_FetchSound (channel_t* ch, unsigned int start, unsigned int nbsamples)
407 ogg_stream_perchannel_t* per_ch;
410 snd_format_t* format;
411 ogg_stream_persfx_t* per_sfx;
412 int newlength, done, ret, bigendian;
416 per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
418 per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
419 format = &sfx->format;
420 buff_len = STREAM_BUFFER_SIZE(format);
422 // If there's no fetcher structure attached to the channel yet
426 ogg_stream_persfx_t* per_sfx;
428 memsize = sizeof (*per_ch) - sizeof (per_ch->sb.data) + buff_len;
429 per_ch = (ogg_stream_perchannel_t *)Mem_Alloc (snd_mempool, memsize);
430 sfx->memsize += memsize;
431 per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
433 // Open it with the VorbisFile API
434 per_ch->ov_decode.buffer = per_sfx->file;
435 per_ch->ov_decode.ind = 0;
436 per_ch->ov_decode.buffsize = per_sfx->filesize;
437 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
439 Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", sfx->name);
444 per_ch->sb.offset = 0;
445 per_ch->sb.length = 0;
448 ch->fetcher_data = per_ch;
452 factor = per_sfx->format.width * per_sfx->format.channels;
454 // If the stream buffer can't contain that much samples anyway
455 if (nbsamples * factor > buff_len)
457 Con_Printf ("OGG_FetchSound: stream buffer too small (%u bytes required)\n", nbsamples * factor);
461 // If the data we need has already been decompressed in the sfxbuffer, just return it
462 if (sb->offset <= start && sb->offset + sb->length >= start + nbsamples)
465 newlength = (int)(sb->offset + sb->length) - start;
467 // If we need to skip some data before decompressing the rest, or if the stream has looped
468 if (newlength < 0 || sb->offset > start)
470 if (qov_pcm_seek (&per_ch->vf, (ogg_int64_t)start) != 0)
474 // Else, move forward the samples we need to keep in the sfxbuffer
477 memmove (sb->data, sb->data + (start - sb->offset) * factor, newlength * factor);
478 sb->length = newlength;
483 // We add exactly 1 sec of sound to the buffer:
484 // 1- to ensure we won't lose any sample during the resampling process
485 // 2- to force one call to OGG_FetchSound per second to regulate the workload
486 if ((sfx->format.speed + sb->length) * factor > buff_len)
488 Con_Printf ("OGG_FetchSound: stream buffer overflow (%u bytes / %u)\n",
489 (sfx->format.speed + sb->length) * factor, buff_len);
492 newlength = per_sfx->format.speed * factor; // -> 1 sec of sound before resampling
494 // Decompress in the resampling_buffer
495 #if BYTE_ORDER == BIG_ENDIAN
501 while ((ret = qov_read (&per_ch->vf, (char *)&resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
504 // Resample in the sfxbuffer
505 newlength = (int)ResampleSfx (resampling_buffer, (size_t)done / (size_t)factor, &per_sfx->format, sb->data + sb->length * (size_t)factor, sfx->name);
506 sb->length += newlength;
517 static void OGG_FetchEnd (channel_t* ch)
519 ogg_stream_perchannel_t* per_ch;
521 per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
525 snd_format_t* format;
527 // Free the ogg vorbis decoder
528 qov_clear (&per_ch->vf);
531 ch->fetcher_data = NULL;
533 format = &ch->sfx->format;
534 buff_len = STREAM_BUFFER_SIZE(format);
535 ch->sfx->memsize -= sizeof (*per_ch) - sizeof (per_ch->sb.data) + buff_len;
545 static void OGG_FreeSfx (sfx_t* sfx)
547 ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
549 // Free the Ogg Vorbis file
550 Mem_Free(per_sfx->file);
551 sfx->memsize -= per_sfx->filesize;
553 // Free the stream structure
555 sfx->memsize -= sizeof (*per_sfx);
557 sfx->fetcher_data = NULL;
561 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd, OGG_FreeSfx };
568 Load an Ogg Vorbis file into memory
571 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *s)
574 fs_offset_t filesize;
575 ov_decode_t ov_decode;
578 ogg_int64_t len, buff_len;
584 if (s->fetcher != NULL)
588 data = FS_LoadFile (filename, snd_mempool, false, &filesize);
592 Con_DPrintf ("Loading Ogg Vorbis file \"%s\"\n", filename);
594 // Open it with the VorbisFile API
595 ov_decode.buffer = data;
597 ov_decode.buffsize = filesize;
598 if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
600 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
605 // Get the stream information
606 vi = qov_info (&vf, -1);
607 if (vi->channels < 1 || vi->channels > 2)
609 Con_Printf("%s has an unsupported number of channels (%i)\n",
610 s->name, vi->channels);
616 len = qov_pcm_total (&vf, -1) * vi->channels * 2; // 16 bits => "* 2"
618 // Decide if we go for a stream or a simple PCM cache
619 buff_len = ceil (STREAM_BUFFER_DURATION * (shm->format.speed * 2 * vi->channels));
620 if (snd_streaming.integer && len > (ogg_int64_t)filesize + 3 * buff_len)
622 ogg_stream_persfx_t* per_sfx;
624 Con_DPrintf ("\"%s\" will be streamed\n", filename);
625 per_sfx = (ogg_stream_persfx_t *)Mem_Alloc (snd_mempool, sizeof (*per_sfx));
626 s->memsize += sizeof (*per_sfx);
627 per_sfx->file = data;
628 per_sfx->filesize = filesize;
629 s->memsize += filesize;
631 per_sfx->format.speed = vi->rate;
632 per_sfx->format.width = 2; // We always work with 16 bits samples
633 per_sfx->format.channels = vi->channels;
634 s->format.speed = shm->format.speed;
635 s->format.width = per_sfx->format.width;
636 s->format.channels = per_sfx->format.channels;
638 s->fetcher_data = per_sfx;
639 s->fetcher = &ogg_fetcher;
641 s->flags |= SFXFLAG_STREAMED;
642 s->total_length = (size_t)len / per_sfx->format.channels / 2 * ((float)s->format.speed / per_sfx->format.speed);
653 Con_DPrintf ("\"%s\" will be cached\n", filename);
656 buff = (char *)Mem_Alloc (snd_mempool, (int)len);
659 #if BYTE_ORDER == LITTLE_ENDIAN
664 while ((ret = qov_read (&vf, &buff[done], (int)(len - done), bigendian, 2, 1, &bs)) > 0)
667 // Calculate resampled length
668 len = (double)done * (double)shm->format.speed / (double)vi->rate;
671 memsize = (size_t)len + sizeof (*sb) - sizeof (sb->data);
672 sb = (sfxbuffer_t *)Mem_Alloc (snd_mempool, memsize);
673 s->memsize += memsize;
674 s->fetcher_data = sb;
675 s->fetcher = &wav_fetcher;
676 s->format.speed = vi->rate;
677 s->format.width = 2; // We always work with 16 bits samples
678 s->format.channels = vi->channels;
680 s->flags &= ~SFXFLAG_STREAMED;
682 sb->length = (unsigned int)ResampleSfx ((unsigned char *)buff, (size_t)done / (vi->channels * 2), &s->format, sb->data, s->name);
683 s->format.speed = shm->format.speed;
684 s->total_length = sb->length;