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 [] =
320 #elif defined(MACOSX)
328 const char* dllnames_vf [] =
331 "libvorbisfile64.dll",
335 #elif defined(MACOSX)
336 "libvorbisfile.dylib",
338 "libvorbisfile.so.3",
348 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
349 if (COM_CheckParm("-novorbis"))
353 // We need to load both by hand because some OSes seem to not load
354 // the vorbis DLL automatically when loading the VorbisFile DLL
355 return Sys_LoadLibrary (dllnames_vo, &vo_dll, vorbisfuncs) && Sys_LoadLibrary (dllnames_vf, &vf_dll, vorbisfilefuncs);
363 Unload the VorbisFile DLL
366 void OGG_CloseLibrary (void)
368 Sys_UnloadLibrary (&vf_dll);
369 Sys_UnloadLibrary (&vo_dll);
374 =================================================================
378 =================================================================
381 // Per-sfx data structure
387 unsigned int total_length;
389 } ogg_stream_persfx_t;
391 // Per-channel data structure
395 ov_decode_t ov_decode;
396 unsigned int sb_offset;
398 snd_buffer_t sb; // must be at the end due to its dynamically allocated size
399 } ogg_stream_perchannel_t;
402 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
409 static const snd_buffer_t* OGG_FetchSound (void *sfxfetcher, void **chfetcherpointer, unsigned int *start, unsigned int nbsampleframes)
411 ogg_stream_perchannel_t* per_ch = (ogg_stream_perchannel_t *)*chfetcherpointer;
412 ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfxfetcher;
414 int newlength, done, ret;
415 unsigned int real_start;
418 // If there's no fetcher structure attached to the channel yet
421 size_t buff_len, memsize;
422 snd_format_t sb_format;
424 sb_format.speed = snd_renderbuffer->format.speed;
425 sb_format.width = per_sfx->format.width;
426 sb_format.channels = per_sfx->format.channels;
428 buff_len = STREAM_BUFFER_SIZE(&sb_format);
429 memsize = sizeof (*per_ch) - sizeof (per_ch->sb.samples) + buff_len;
430 per_ch = (ogg_stream_perchannel_t *)Mem_Alloc (snd_mempool, memsize);
432 // Open it with the VorbisFile API
433 per_ch->ov_decode.buffer = per_sfx->file;
434 per_ch->ov_decode.ind = 0;
435 per_ch->ov_decode.buffsize = per_sfx->filesize;
436 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
438 Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", per_sfx->name);
444 per_ch->sb_offset = 0;
445 per_ch->sb.format = sb_format;
446 per_ch->sb.nbframes = 0;
447 per_ch->sb.maxframes = buff_len / (per_ch->sb.format.channels * per_ch->sb.format.width);
449 *chfetcherpointer = per_ch;
455 factor = per_sfx->format.width * per_sfx->format.channels;
457 // If the stream buffer can't contain that much samples anyway
458 if (nbsampleframes > sb->maxframes)
460 Con_Printf ("OGG_FetchSound: stream buffer too small (%u sample frames required)\n", nbsampleframes);
464 // If the data we need has already been decompressed in the sfxbuffer, just return it
465 if (per_ch->sb_offset <= real_start && per_ch->sb_offset + sb->nbframes >= real_start + nbsampleframes)
467 *start = per_ch->sb_offset;
471 newlength = (int)(per_ch->sb_offset + sb->nbframes) - real_start;
473 // If we need to skip some data before decompressing the rest, or if the stream has looped
474 if (newlength < 0 || per_ch->sb_offset > real_start)
476 unsigned int time_start;
477 ogg_int64_t ogg_start;
480 if (real_start > (unsigned int)per_sfx->total_length)
482 Con_Printf ("OGG_FetchSound: asked for a start position after the end of the sfx! (%u > %u)\n",
483 real_start, per_sfx->total_length);
487 // We work with 200ms (1/5 sec) steps to avoid rounding errors
488 time_start = real_start * 5 / snd_renderbuffer->format.speed;
489 ogg_start = time_start * (per_sfx->format.speed / 5);
490 err = qov_pcm_seek (&per_ch->vf, ogg_start);
493 Con_Printf ("OGG_FetchSound: qov_pcm_seek(..., %d) returned %d\n",
499 real_start = (unsigned int) ((float)ogg_start / per_sfx->format.speed * snd_renderbuffer->format.speed);
500 if (*start - real_start + nbsampleframes > sb->maxframes)
502 Con_Printf ("OGG_FetchSound: stream buffer too small after seek (%u sample frames required)\n",
503 *start - real_start + nbsampleframes);
504 per_ch->sb_offset = real_start;
508 // Else, move forward the samples we need to keep in the sound buffer
511 memmove (sb->samples, sb->samples + (real_start - per_ch->sb_offset) * factor, newlength * factor);
512 sb->nbframes = newlength;
515 per_ch->sb_offset = real_start;
517 // We add more than one frame of sound to the buffer:
518 // 1- to ensure we won't lose many samples during the resampling process
519 // 2- to reduce calls to OGG_FetchSound to regulate workload
520 newlength = (int)(per_sfx->format.speed*STREAM_BUFFER_FILL);
521 if (newlength + sb->nbframes > sb->maxframes)
523 Con_Printf ("OGG_FetchSound: stream buffer overflow (%u sample frames / %u)\n",
524 sb->format.speed + sb->nbframes, sb->maxframes);
527 newlength *= factor; // convert from sample frames to bytes
528 if(newlength > (int)sizeof(resampling_buffer))
529 newlength = sizeof(resampling_buffer);
531 // Decompress in the resampling_buffer
533 while ((ret = qov_read (&per_ch->vf, (char *)&resampling_buffer[done], (int)(newlength - done), mem_bigendian, 2, 1, &per_ch->bs)) > 0)
536 Snd_AppendToSndBuffer (sb, resampling_buffer, (size_t)done / (size_t)factor, &per_sfx->format);
538 *start = per_ch->sb_offset;
548 static void OGG_FetchEnd (void *chfetcherdata)
550 ogg_stream_perchannel_t* per_ch = (ogg_stream_perchannel_t *)chfetcherdata;
554 // Free the ogg vorbis decoder
555 qov_clear (&per_ch->vf);
567 static void OGG_FreeSfx (void *sfxfetcherdata)
569 ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfxfetcherdata;
571 // Free the Ogg Vorbis file
572 Mem_Free(per_sfx->file);
574 // Free the stream structure
584 static const snd_format_t* OGG_GetFormat (sfx_t* sfx)
586 ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
587 return &per_sfx->format;
590 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd, OGG_FreeSfx, OGG_GetFormat };
592 static void OGG_DecodeTags(vorbis_comment *vc, unsigned int *start, unsigned int *length, double samplesfactor, unsigned int numsamples, double *peak, double *gaindb)
594 const char *startcomment = NULL, *lengthcomment = NULL, *endcomment = NULL, *thiscomment = NULL;
597 *length = numsamples;
604 thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_PEAK", 0);
606 *peak = atof(thiscomment);
607 thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_GAIN", 0);
609 *gaindb = atof(thiscomment);
611 startcomment = qvorbis_comment_query(vc, "LOOP_START", 0); // DarkPlaces, and some Japanese app
614 endcomment = qvorbis_comment_query(vc, "LOOP_END", 0);
616 lengthcomment = qvorbis_comment_query(vc, "LOOP_LENGTH", 0);
620 startcomment = qvorbis_comment_query(vc, "LOOPSTART", 0); // RPG Maker VX
623 lengthcomment = qvorbis_comment_query(vc, "LOOPLENGTH", 0);
625 endcomment = qvorbis_comment_query(vc, "LOOPEND", 0);
629 startcomment = qvorbis_comment_query(vc, "LOOPPOINT", 0); // Sonic Robo Blast 2
635 *start = (unsigned int) bound(0, atof(startcomment) * samplesfactor, numsamples);
637 *length = (unsigned int) bound(0, atof(endcomment) * samplesfactor, numsamples);
638 else if(lengthcomment)
639 *length = (unsigned int) bound(0, *start + atof(lengthcomment) * samplesfactor, numsamples);
647 Load an Ogg Vorbis file into memory
650 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *sfx)
653 fs_offset_t filesize;
654 ov_decode_t ov_decode;
656 ogg_stream_persfx_t* per_sfx;
666 if (sfx->fetcher != NULL)
670 data = FS_LoadFile (filename, snd_mempool, false, &filesize);
674 if (developer_loading.integer >= 2)
675 Con_Printf ("Loading Ogg Vorbis file \"%s\"\n", filename);
677 // Open it with the VorbisFile API
678 ov_decode.buffer = data;
680 ov_decode.buffsize = filesize;
681 if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
683 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
688 // Get the stream information
689 vi = qov_info (&vf, -1);
690 if (vi->channels < 1 || vi->channels > 2)
692 Con_Printf("%s has an unsupported number of channels (%i)\n",
693 sfx->name, vi->channels);
699 len = qov_pcm_total (&vf, -1) * vi->channels * 2; // 16 bits => "* 2"
701 if (developer_loading.integer >= 2)
702 Con_Printf ("Ogg sound file \"%s\" will be streamed\n", filename);
703 per_sfx = (ogg_stream_persfx_t *)Mem_Alloc (snd_mempool, sizeof (*per_sfx));
704 strlcpy(per_sfx->name, sfx->name, sizeof(per_sfx->name));
705 sfx->memsize += sizeof (*per_sfx);
706 per_sfx->file = data;
707 per_sfx->filesize = filesize;
708 sfx->memsize += filesize;
710 per_sfx->format.speed = vi->rate;
711 per_sfx->format.width = 2; // We always work with 16 bits samples
712 per_sfx->format.channels = vi->channels;
714 sfx->fetcher_data = per_sfx;
715 sfx->fetcher = &ogg_fetcher;
716 sfx->flags |= SFXFLAG_STREAMED;
717 sfx->total_length = (int)((size_t)len / (per_sfx->format.channels * 2) * ((double)snd_renderbuffer->format.speed / per_sfx->format.speed));
718 vc = qov_comment(&vf, -1);
719 OGG_DecodeTags(vc, &sfx->loopstart, &sfx->total_length, (double)snd_renderbuffer->format.speed / (double)per_sfx->format.speed, sfx->total_length, &peak, &gaindb);
720 per_sfx->total_length = sfx->total_length;
725 sfx->volume_mult = min(1.0f / peak, exp(gaindb * 0.05f * log(10.0f)));
726 sfx->volume_peak = peak;
727 if (developer_loading.integer >= 2)
728 Con_Printf ("Ogg sound file \"%s\" uses ReplayGain (gain %f, peak %f)\n", filename, sfx->volume_mult, sfx->volume_peak);