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
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
162 char **user_comments;
163 int *comment_lengths;
176 ogg_int64_t *offsets;
177 ogg_int64_t *dataoffsets;
179 ogg_int64_t *pcmlengths;
182 ogg_int64_t pcm_offset;
184 long current_serialno;
191 ov_callbacks callbacks;
196 =================================================================
198 DarkPlaces definitions
200 =================================================================
203 // Functions exported from the vorbisfile library
204 static int (*qov_clear) (OggVorbis_File *vf);
205 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
206 static vorbis_comment* (*qov_comment) (OggVorbis_File *vf,int link);
207 static char * (*qvorbis_comment_query) (vorbis_comment *vc, char *tag, int count);
208 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
209 char *initial, long ibytes,
210 ov_callbacks callbacks);
211 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
212 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
213 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
214 int bigendianp,int word,int sgned,int *bitstream);
216 static dllfunction_t vorbisfilefuncs[] =
218 {"ov_clear", (void **) &qov_clear},
219 {"ov_info", (void **) &qov_info},
220 {"ov_comment", (void **) &qov_comment},
221 {"ov_open_callbacks", (void **) &qov_open_callbacks},
222 {"ov_pcm_seek", (void **) &qov_pcm_seek},
223 {"ov_pcm_total", (void **) &qov_pcm_total},
224 {"ov_read", (void **) &qov_read},
228 static dllfunction_t vorbisfuncs[] =
230 {"vorbis_comment_query", (void **) &qvorbis_comment_query},
234 // Handles for the Vorbis and Vorbisfile DLLs
235 static dllhandle_t vo_dll = NULL;
236 static dllhandle_t vf_dll = NULL;
240 unsigned char *buffer;
241 ogg_int64_t ind, buffsize;
245 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
247 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
250 remain = ov_decode->buffsize - ov_decode->ind;
253 len = remain - remain % size;
255 memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
256 ov_decode->ind += len;
261 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
263 ov_decode_t *ov_decode = (ov_decode_t*)datasource;
270 offset += ov_decode->ind;
273 offset += ov_decode->buffsize;
278 if (offset < 0 || offset > ov_decode->buffsize)
281 ov_decode->ind = offset;
285 static int ovcb_close (void *ov_decode)
290 static long ovcb_tell (void *ov_decode)
292 return ((ov_decode_t*)ov_decode)->ind;
297 =================================================================
301 =================================================================
308 Try to load the VorbisFile DLL
311 qboolean OGG_OpenLibrary (void)
313 const char* dllnames_vo [] =
318 #elif defined(MACOSX)
326 const char* dllnames_vf [] =
331 #elif defined(MACOSX)
332 "libvorbisfile.dylib",
334 "libvorbisfile.so.3",
344 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
345 if (COM_CheckParm("-novorbis"))
349 // We need to load both by hand because some OSes seem to not load
350 // the vorbis DLL automatically when loading the VorbisFile DLL
351 return Sys_LoadLibrary (dllnames_vo, &vo_dll, vorbisfuncs) && Sys_LoadLibrary (dllnames_vf, &vf_dll, vorbisfilefuncs);
359 Unload the VorbisFile DLL
362 void OGG_CloseLibrary (void)
364 Sys_UnloadLibrary (&vf_dll);
365 Sys_UnloadLibrary (&vo_dll);
370 =================================================================
374 =================================================================
377 // Per-sfx data structure
383 unsigned int total_length;
385 } ogg_stream_persfx_t;
387 // Per-channel data structure
391 ov_decode_t ov_decode;
392 unsigned int sb_offset;
394 snd_buffer_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 snd_buffer_t* OGG_FetchSound (void *sfxfetcher, void **chfetcherpointer, unsigned int *start, unsigned int nbsampleframes)
407 ogg_stream_perchannel_t* per_ch = (ogg_stream_perchannel_t *)*chfetcherpointer;
408 ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfxfetcher;
410 int newlength, done, ret;
411 unsigned int real_start;
414 // If there's no fetcher structure attached to the channel yet
417 size_t buff_len, memsize;
418 snd_format_t sb_format;
420 sb_format.speed = snd_renderbuffer->format.speed;
421 sb_format.width = per_sfx->format.width;
422 sb_format.channels = per_sfx->format.channels;
424 buff_len = STREAM_BUFFER_SIZE(&sb_format);
425 memsize = sizeof (*per_ch) - sizeof (per_ch->sb.samples) + buff_len;
426 per_ch = (ogg_stream_perchannel_t *)Mem_Alloc (snd_mempool, memsize);
428 // Open it with the VorbisFile API
429 per_ch->ov_decode.buffer = per_sfx->file;
430 per_ch->ov_decode.ind = 0;
431 per_ch->ov_decode.buffsize = per_sfx->filesize;
432 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
434 Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", per_sfx->name);
440 per_ch->sb_offset = 0;
441 per_ch->sb.format = sb_format;
442 per_ch->sb.nbframes = 0;
443 per_ch->sb.maxframes = buff_len / (per_ch->sb.format.channels * per_ch->sb.format.width);
445 *chfetcherpointer = per_ch;
451 factor = per_sfx->format.width * per_sfx->format.channels;
453 // If the stream buffer can't contain that much samples anyway
454 if (nbsampleframes > sb->maxframes)
456 Con_Printf ("OGG_FetchSound: stream buffer too small (%u sample frames required)\n", nbsampleframes);
460 // If the data we need has already been decompressed in the sfxbuffer, just return it
461 if (per_ch->sb_offset <= real_start && per_ch->sb_offset + sb->nbframes >= real_start + nbsampleframes)
463 *start = per_ch->sb_offset;
467 newlength = (int)(per_ch->sb_offset + sb->nbframes) - real_start;
469 // If we need to skip some data before decompressing the rest, or if the stream has looped
470 if (newlength < 0 || per_ch->sb_offset > real_start)
472 unsigned int time_start;
473 ogg_int64_t ogg_start;
476 if (real_start > (unsigned int)per_sfx->total_length)
478 Con_Printf ("OGG_FetchSound: asked for a start position after the end of the sfx! (%u > %u)\n",
479 real_start, per_sfx->total_length);
483 // We work with 200ms (1/5 sec) steps to avoid rounding errors
484 time_start = real_start * 5 / snd_renderbuffer->format.speed;
485 ogg_start = time_start * (per_sfx->format.speed / 5);
486 err = qov_pcm_seek (&per_ch->vf, ogg_start);
489 Con_Printf ("OGG_FetchSound: qov_pcm_seek(..., %d) returned %d\n",
495 real_start = (unsigned int) ((float)ogg_start / per_sfx->format.speed * snd_renderbuffer->format.speed);
496 if (*start - real_start + nbsampleframes > sb->maxframes)
498 Con_Printf ("OGG_FetchSound: stream buffer too small after seek (%u sample frames required)\n",
499 *start - real_start + nbsampleframes);
500 per_ch->sb_offset = real_start;
504 // Else, move forward the samples we need to keep in the sound buffer
507 memmove (sb->samples, sb->samples + (real_start - per_ch->sb_offset) * factor, newlength * factor);
508 sb->nbframes = newlength;
511 per_ch->sb_offset = real_start;
513 // We add more than one frame of sound to the buffer:
514 // 1- to ensure we won't lose many samples during the resampling process
515 // 2- to reduce calls to OGG_FetchSound to regulate workload
516 newlength = (int)(per_sfx->format.speed*STREAM_BUFFER_FILL);
517 if (newlength + sb->nbframes > sb->maxframes)
519 Con_Printf ("OGG_FetchSound: stream buffer overflow (%u sample frames / %u)\n",
520 sb->format.speed + sb->nbframes, sb->maxframes);
523 newlength *= factor; // convert from sample frames to bytes
524 if(newlength > (int)sizeof(resampling_buffer))
525 newlength = sizeof(resampling_buffer);
527 // Decompress in the resampling_buffer
529 while ((ret = qov_read (&per_ch->vf, (char *)&resampling_buffer[done], (int)(newlength - done), mem_bigendian, 2, 1, &per_ch->bs)) > 0)
532 Snd_AppendToSndBuffer (sb, resampling_buffer, (size_t)done / (size_t)factor, &per_sfx->format);
534 *start = per_ch->sb_offset;
544 static void OGG_FetchEnd (void *chfetcherdata)
546 ogg_stream_perchannel_t* per_ch = (ogg_stream_perchannel_t *)chfetcherdata;
550 // Free the ogg vorbis decoder
551 qov_clear (&per_ch->vf);
563 static void OGG_FreeSfx (void *sfxfetcherdata)
565 ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfxfetcherdata;
567 // Free the Ogg Vorbis file
568 Mem_Free(per_sfx->file);
570 // Free the stream structure
580 static const snd_format_t* OGG_GetFormat (sfx_t* sfx)
582 ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
583 return &per_sfx->format;
586 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd, OGG_FreeSfx, OGG_GetFormat };
588 static void OGG_DecodeTags(vorbis_comment *vc, unsigned int *start, unsigned int *length, double samplesfactor, unsigned int numsamples, double *peak, double *gaindb)
590 const char *startcomment = NULL, *lengthcomment = NULL, *endcomment = NULL, *thiscomment = NULL;
593 *length = numsamples;
600 thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_PEAK", 0);
602 *peak = atof(thiscomment);
603 thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_GAIN", 0);
605 *gaindb = atof(thiscomment);
607 startcomment = qvorbis_comment_query(vc, "LOOP_START", 0); // DarkPlaces, and some Japanese app
610 endcomment = qvorbis_comment_query(vc, "LOOP_END", 0);
612 lengthcomment = qvorbis_comment_query(vc, "LOOP_LENGTH", 0);
616 startcomment = qvorbis_comment_query(vc, "LOOPSTART", 0); // RPG Maker VX
619 lengthcomment = qvorbis_comment_query(vc, "LOOPLENGTH", 0);
621 endcomment = qvorbis_comment_query(vc, "LOOPEND", 0);
625 startcomment = qvorbis_comment_query(vc, "LOOPPOINT", 0); // Sonic Robo Blast 2
631 *start = (unsigned int) bound(0, atof(startcomment) * samplesfactor, numsamples);
633 *length = (unsigned int) bound(0, atof(endcomment) * samplesfactor, numsamples);
634 else if(lengthcomment)
635 *length = (unsigned int) bound(0, *start + atof(lengthcomment) * samplesfactor, numsamples);
643 Load an Ogg Vorbis file into memory
646 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *sfx)
649 fs_offset_t filesize;
650 ov_decode_t ov_decode;
652 ogg_stream_persfx_t* per_sfx;
662 if (sfx->fetcher != NULL)
666 data = FS_LoadFile (filename, snd_mempool, false, &filesize);
670 if (developer_loading.integer >= 2)
671 Con_Printf ("Loading Ogg Vorbis file \"%s\"\n", filename);
673 // Open it with the VorbisFile API
674 ov_decode.buffer = data;
676 ov_decode.buffsize = filesize;
677 if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
679 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
684 // Get the stream information
685 vi = qov_info (&vf, -1);
686 if (vi->channels < 1 || vi->channels > 2)
688 Con_Printf("%s has an unsupported number of channels (%i)\n",
689 sfx->name, vi->channels);
695 len = qov_pcm_total (&vf, -1) * vi->channels * 2; // 16 bits => "* 2"
697 if (developer_loading.integer >= 2)
698 Con_Printf ("Ogg sound file \"%s\" will be streamed\n", filename);
699 per_sfx = (ogg_stream_persfx_t *)Mem_Alloc (snd_mempool, sizeof (*per_sfx));
700 strlcpy(per_sfx->name, sfx->name, sizeof(per_sfx->name));
701 sfx->memsize += sizeof (*per_sfx);
702 per_sfx->file = data;
703 per_sfx->filesize = filesize;
704 sfx->memsize += filesize;
706 per_sfx->format.speed = vi->rate;
707 per_sfx->format.width = 2; // We always work with 16 bits samples
708 per_sfx->format.channels = vi->channels;
710 sfx->fetcher_data = per_sfx;
711 sfx->fetcher = &ogg_fetcher;
712 sfx->flags |= SFXFLAG_STREAMED;
713 sfx->total_length = (int)((size_t)len / (per_sfx->format.channels * 2) * ((double)snd_renderbuffer->format.speed / per_sfx->format.speed));
714 vc = qov_comment(&vf, -1);
715 OGG_DecodeTags(vc, &sfx->loopstart, &sfx->total_length, (double)snd_renderbuffer->format.speed / (double)per_sfx->format.speed, sfx->total_length, &peak, &gaindb);
716 per_sfx->total_length = sfx->total_length;
721 sfx->volume_mult = min(1.0f / peak, exp(gaindb * 0.05f * log(10.0f)));
722 sfx->volume_peak = peak;
723 if (developer_loading.integer >= 2)
724 Con_Printf ("Ogg sound file \"%s\" uses ReplayGain (gain %f, peak %f)\n", filename, sfx->volume_mult, sfx->volume_peak);