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
30 #ifdef LINK_TO_LIBVORBIS
31 #define OV_EXCLUDE_STATIC_CALLBACKS
33 #include <vorbis/vorbisfile.h>
35 #define qov_clear ov_clear
36 #define qov_info ov_info
37 #define qov_comment ov_comment
38 #define qov_open_callbacks ov_open_callbacks
39 #define qov_pcm_seek ov_pcm_seek
40 #define qov_pcm_total ov_pcm_total
41 #define qov_read ov_read
42 #define qvorbis_comment_query vorbis_comment_query
44 qboolean OGG_OpenLibrary (void) {return true;}
45 void OGG_CloseLibrary (void) {}
49 =================================================================
51 Minimal set of definitions from the Ogg Vorbis lib
52 (C) COPYRIGHT 1994-2001 by the XIPHOPHORUS Company
55 WARNING: for a matter of simplicity, several pointer types are
56 casted to "void*", and most enumerated values are not included
58 =================================================================
62 typedef __int64 ogg_int64_t;
64 typedef long long ogg_int64_t;
69 size_t (*read_func) (void *ptr, size_t size, size_t nmemb, void *datasource);
70 int (*seek_func) (void *datasource, ogg_int64_t offset, int whence);
71 int (*close_func) (void *datasource);
72 long (*tell_func) (void *datasource);
100 unsigned char *body_data;
105 ogg_int64_t *granule_vals;
109 long lacing_returned;
110 unsigned char header[282];
116 ogg_int64_t packetno;
117 ogg_int64_t granulepos;
135 ogg_int64_t granulepos;
136 ogg_int64_t sequence;
137 ogg_int64_t glue_bits;
138 ogg_int64_t time_bits;
139 ogg_int64_t floor_bits;
140 ogg_int64_t res_bits;
148 unsigned char *buffer;
163 ogg_int64_t granulepos;
164 ogg_int64_t sequence;
165 vorbis_dsp_state *vd;
170 void *reap; // VOIDED POINTER
180 char **user_comments;
181 int *comment_lengths;
194 ogg_int64_t *offsets;
195 ogg_int64_t *dataoffsets;
197 ogg_int64_t *pcmlengths;
200 ogg_int64_t pcm_offset;
202 long current_serialno;
209 ov_callbacks callbacks;
214 =================================================================
216 DarkPlaces definitions
218 =================================================================
221 // Functions exported from the vorbisfile library
222 static int (*qov_clear) (OggVorbis_File *vf);
223 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
224 static vorbis_comment* (*qov_comment) (OggVorbis_File *vf,int link);
225 static char * (*qvorbis_comment_query) (vorbis_comment *vc, const char *tag, int count);
226 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
227 char *initial, long ibytes,
228 ov_callbacks callbacks);
229 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
230 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
231 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
232 int bigendianp,int word,int sgned,int *bitstream);
234 static dllfunction_t vorbisfilefuncs[] =
236 {"ov_clear", (void **) &qov_clear},
237 {"ov_info", (void **) &qov_info},
238 {"ov_comment", (void **) &qov_comment},
239 {"ov_open_callbacks", (void **) &qov_open_callbacks},
240 {"ov_pcm_seek", (void **) &qov_pcm_seek},
241 {"ov_pcm_total", (void **) &qov_pcm_total},
242 {"ov_read", (void **) &qov_read},
246 static dllfunction_t vorbisfuncs[] =
248 {"vorbis_comment_query", (void **) &qvorbis_comment_query},
252 // Handles for the Vorbis and Vorbisfile DLLs
253 static dllhandle_t vo_dll = NULL;
254 static dllhandle_t vf_dll = NULL;
258 =================================================================
262 =================================================================
269 Try to load the VorbisFile DLL
272 qboolean OGG_OpenLibrary (void)
274 const char* dllnames_vo [] =
280 #elif defined(MACOSX)
288 const char* dllnames_vf [] =
291 "libvorbisfile-3.dll",
294 #elif defined(MACOSX)
295 "libvorbisfile.dylib",
297 "libvorbisfile.so.3",
307 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
308 if (COM_CheckParm("-novorbis"))
312 // We need to load both by hand because some OSes seem to not load
313 // the vorbis DLL automatically when loading the VorbisFile DLL
314 return Sys_LoadLibrary (dllnames_vo, &vo_dll, vorbisfuncs) && Sys_LoadLibrary (dllnames_vf, &vf_dll, vorbisfilefuncs);
322 Unload the VorbisFile DLL
325 void OGG_CloseLibrary (void)
327 Sys_UnloadLibrary (&vf_dll);
328 Sys_UnloadLibrary (&vo_dll);
334 =================================================================
338 =================================================================
343 unsigned char *buffer;
344 ogg_int64_t ind, buffsize;
347 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
349 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
352 remain = ov_decode->buffsize - ov_decode->ind;
355 len = remain - remain % size;
357 memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
358 ov_decode->ind += len;
363 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
365 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
372 offset += ov_decode->ind;
375 offset += ov_decode->buffsize;
380 if (offset < 0 || offset > ov_decode->buffsize)
383 ov_decode->ind = offset;
387 static int ovcb_close (void *ov_decode)
392 static long ovcb_tell (void *ov_decode)
394 return ((ov_decode_t*)ov_decode)->ind;
397 // Per-sfx data structure
402 } ogg_stream_persfx_t;
404 // Per-channel data structure
408 ov_decode_t ov_decode;
410 int buffer_firstframe;
411 int buffer_numframes;
412 unsigned char buffer[STREAM_BUFFERSIZE*4];
413 } ogg_stream_perchannel_t;
416 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
423 static void OGG_GetSamplesFloat (channel_t *ch, sfx_t *sfx, int firstsampleframe, int numsampleframes, float *outsamplesfloat)
425 ogg_stream_perchannel_t *per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
426 ogg_stream_persfx_t *per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
427 int f = sfx->format.width * sfx->format.channels; // bytes per frame in the buffer
430 int newlength, done, ret;
432 // if this channel does not yet have a channel fetcher, make one
435 // allocate a struct to keep track of our file position and buffer
436 per_ch = (ogg_stream_perchannel_t *)Mem_Alloc(snd_mempool, sizeof(*per_ch));
437 // begin decoding the file
438 per_ch->ov_decode.buffer = per_sfx->file;
439 per_ch->ov_decode.ind = 0;
440 per_ch->ov_decode.buffsize = per_sfx->filesize;
441 if (qov_open_callbacks(&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
443 // this never happens - this function succeeded earlier on the same data
448 per_ch->buffer_firstframe = 0;
449 per_ch->buffer_numframes = 0;
450 // attach the struct to our channel
451 ch->fetcher_data = (void *)per_ch;
454 // if the request is too large for our buffer, loop...
455 while (numsampleframes * f > (int)sizeof(per_ch->buffer))
457 done = sizeof(per_ch->buffer) / f;
458 OGG_GetSamplesFloat(ch, sfx, firstsampleframe, done, outsamplesfloat);
459 firstsampleframe += done;
460 numsampleframes -= done;
461 outsamplesfloat += done * sfx->format.channels;
464 // seek if the request is before the current buffer (loop back)
465 // seek if the request starts beyond the current buffer by at least one frame (channel was zero volume for a while)
466 // do not seek if the request overlaps the buffer end at all (expected behavior)
467 if (per_ch->buffer_firstframe > firstsampleframe || per_ch->buffer_firstframe + per_ch->buffer_numframes < firstsampleframe)
469 // we expect to decode forward from here so this will be our new buffer start
470 per_ch->buffer_firstframe = firstsampleframe;
471 per_ch->buffer_numframes = 0;
472 ret = qov_pcm_seek(&per_ch->vf, (ogg_int64_t)firstsampleframe);
475 // LadyHavoc: we can't Con_Printf here, not thread safe...
476 //Con_Printf("OGG_FetchSound: qov_pcm_seek(..., %d) returned %d\n", firstsampleframe, ret);
481 // decompress the file as needed
482 if (firstsampleframe + numsampleframes > per_ch->buffer_firstframe + per_ch->buffer_numframes)
484 // first slide the buffer back, discarding any data preceding the range we care about
485 int offset = firstsampleframe - per_ch->buffer_firstframe;
486 int keeplength = per_ch->buffer_numframes - offset;
488 memmove(per_ch->buffer, per_ch->buffer + offset * sfx->format.width * sfx->format.channels, keeplength * sfx->format.width * sfx->format.channels);
489 per_ch->buffer_firstframe = firstsampleframe;
490 per_ch->buffer_numframes -= offset;
491 // decompress as much as we can fit in the buffer
492 newlength = sizeof(per_ch->buffer) - per_ch->buffer_numframes * f;
494 while (newlength > done && (ret = qov_read(&per_ch->vf, (char *)per_ch->buffer + per_ch->buffer_numframes * f + done, (int)(newlength - done), mem_bigendian, 2, 1, &per_ch->bs)) > 0)
496 // clear the missing space if any
497 if (done < newlength)
498 memset(per_ch->buffer + done, 0, newlength - done);
499 // we now have more data in the buffer
500 per_ch->buffer_numframes += done / f;
503 // convert the sample format for the caller
504 buf = (short *)((char *)per_ch->buffer + (firstsampleframe - per_ch->buffer_firstframe) * f);
505 len = numsampleframes * sfx->format.channels;
506 for (i = 0;i < len;i++)
507 outsamplesfloat[i] = buf[i] * (1.0f / 32768.0f);
516 static void OGG_StopChannel(channel_t *ch)
518 ogg_stream_perchannel_t *per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
521 // release the vorbis decompressor
522 qov_clear(&per_ch->vf);
533 static void OGG_FreeSfx(sfx_t *sfx)
535 ogg_stream_persfx_t *per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
536 // free the complete file we were keeping around
537 Mem_Free(per_sfx->file);
538 // free the file information structure
543 static const snd_fetcher_t ogg_fetcher = {OGG_GetSamplesFloat, OGG_StopChannel, OGG_FreeSfx};
545 static void OGG_DecodeTags(vorbis_comment *vc, unsigned int *start, unsigned int *length, unsigned int numsamples, double *peak, double *gaindb)
547 const char *startcomment = NULL, *lengthcomment = NULL, *endcomment = NULL, *thiscomment = NULL;
550 *length = numsamples;
557 thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_PEAK", 0);
559 *peak = atof(thiscomment);
560 thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_GAIN", 0);
562 *gaindb = atof(thiscomment);
564 startcomment = qvorbis_comment_query(vc, "LOOP_START", 0); // DarkPlaces, and some Japanese app
567 endcomment = qvorbis_comment_query(vc, "LOOP_END", 0);
569 lengthcomment = qvorbis_comment_query(vc, "LOOP_LENGTH", 0);
573 startcomment = qvorbis_comment_query(vc, "LOOPSTART", 0); // RPG Maker VX
576 lengthcomment = qvorbis_comment_query(vc, "LOOPLENGTH", 0);
578 endcomment = qvorbis_comment_query(vc, "LOOPEND", 0);
582 startcomment = qvorbis_comment_query(vc, "LOOPPOINT", 0); // Sonic Robo Blast 2
588 *start = (unsigned int) bound(0, atof(startcomment), numsamples);
590 *length = (unsigned int) bound(0, atof(endcomment), numsamples);
591 else if(lengthcomment)
592 *length = (unsigned int) bound(0, *start + atof(lengthcomment), numsamples);
600 Load an Ogg Vorbis file into memory
603 qboolean OGG_LoadVorbisFile(const char *filename, sfx_t *sfx)
606 fs_offset_t filesize;
607 ov_decode_t ov_decode;
613 #ifndef LINK_TO_LIBVORBIS
618 // Return if already loaded
619 if (sfx->fetcher != NULL)
622 // Load the file completely
623 data = FS_LoadFile(filename, snd_mempool, false, &filesize);
627 if (developer_loading.integer >= 2)
628 Con_Printf("Loading Ogg Vorbis file \"%s\"\n", filename);
630 // Open it with the VorbisFile API
631 ov_decode.buffer = data;
633 ov_decode.buffsize = filesize;
634 if (qov_open_callbacks(&ov_decode, &vf, NULL, 0, callbacks) < 0)
636 Con_Printf("error while opening Ogg Vorbis file \"%s\"\n", filename);
641 // Get the stream information
642 vi = qov_info(&vf, -1);
643 if (vi->channels < 1 || vi->channels > 2)
645 Con_Printf("%s has an unsupported number of channels (%i)\n",
646 sfx->name, vi->channels);
652 sfx->format.speed = vi->rate;
653 sfx->format.channels = vi->channels;
654 sfx->format.width = 2; // We always work with 16 bits samples
656 sfx->total_length = qov_pcm_total(&vf, -1);
658 if (snd_streaming.integer && (snd_streaming.integer >= 2 || sfx->total_length > max(sizeof(ogg_stream_perchannel_t), snd_streaming_length.value * sfx->format.speed)))
660 // large sounds use the OGG fetcher to decode the file on demand (but the entire file is held in memory)
661 ogg_stream_persfx_t* per_sfx;
662 if (developer_loading.integer >= 2)
663 Con_Printf("Ogg sound file \"%s\" will be streamed\n", filename);
664 per_sfx = (ogg_stream_persfx_t *)Mem_Alloc(snd_mempool, sizeof(*per_sfx));
665 sfx->memsize += sizeof (*per_sfx);
666 per_sfx->file = data;
667 per_sfx->filesize = filesize;
668 sfx->memsize += filesize;
669 sfx->fetcher_data = per_sfx;
670 sfx->fetcher = &ogg_fetcher;
671 sfx->flags |= SFXFLAG_STREAMED;
672 vc = qov_comment(&vf, -1);
673 OGG_DecodeTags(vc, &sfx->loopstart, &sfx->total_length, sfx->total_length, &peak, &gaindb);
678 // small sounds are entirely loaded and use the PCM fetcher
684 if (developer_loading.integer >= 2)
685 Con_Printf ("Ogg sound file \"%s\" will be cached\n", filename);
686 len = sfx->total_length * sfx->format.channels * sfx->format.width;
687 sfx->flags &= ~SFXFLAG_STREAMED;
689 sfx->fetcher = &wav_fetcher;
690 sfx->fetcher_data = Mem_Alloc(snd_mempool, (size_t)len);
691 buff = (char *)sfx->fetcher_data;
694 while ((ret = qov_read(&vf, &buff[done], (int)(len - done), mem_bigendian, 2, 1, &bs)) > 0)
696 vc = qov_comment(&vf, -1);
697 OGG_DecodeTags(vc, &sfx->loopstart, &sfx->total_length, sfx->total_length, &peak, &gaindb);
704 sfx->volume_mult = min(1.0f / peak, exp(gaindb * 0.05f * log(10.0f)));
705 sfx->volume_peak = peak;
706 if (developer_loading.integer >= 2)
707 Con_Printf ("Ogg sound file \"%s\" uses ReplayGain (gain %f, peak %f)\n", filename, sfx->volume_mult, sfx->volume_peak);
711 sfx->volume_mult = min(1.0f / peak, exp(gaindb * 0.05f * log(10.0f)));
712 sfx->volume_peak = 1.0; // if peak is not defined, we won't trust it
713 if (developer_loading.integer >= 2)
714 Con_Printf ("Ogg sound file \"%s\" uses ReplayGain (gain %f, peak not defined and assumed to be %f)\n", filename, sfx->volume_mult, sfx->volume_peak);