+#define STREAM_BUFFER_SIZE (128 * 1024)
+
+// Note: it must be able to contain enough samples at 48 KHz (max speed)
+// to fill STREAM_BUFFER_SIZE bytes of samples at 8 KHz (min speed)
+// TODO: dynamically allocate this buffer depending on the shm and min sound speeds
+static qbyte resampling_buffer [STREAM_BUFFER_SIZE * (48000 / 8000)];
+
+
+// Per-sfx data structure
+typedef struct
+{
+ qbyte *file;
+ size_t filesize;
+} ogg_stream_persfx_t;
+
+// Per-channel data structure
+typedef struct
+{
+ OggVorbis_File vf;
+ ov_decode_t ov_decode;
+ int bs;
+ snd_format_t format;
+ sfxbuffer_t sb; // must be at the end due to its dynamically allocated size
+} ogg_stream_perchannel_t;
+
+
+static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
+
+/*
+====================
+OGG_FetchSound
+====================
+*/
+static const sfxbuffer_t* OGG_FetchSound (channel_t* ch, unsigned int start, unsigned int nbsamples)
+{
+ ogg_stream_perchannel_t* per_ch;
+ sfxbuffer_t* sb;
+ int newlength, done, ret, bigendian;
+ unsigned int factor;
+
+ per_ch = ch->fetcher_data;
+
+ // If there's no fetcher structure attached to the channel yet
+ if (per_ch == NULL)
+ {
+ sfx_t* sfx;
+ vorbis_info *vi;
+ ogg_stream_persfx_t* per_sfx;
+
+ sfx = ch->sfx;
+ per_ch = Mem_Alloc (sfx->mempool, sizeof (*per_ch) - sizeof (per_ch->sb.data) + STREAM_BUFFER_SIZE);
+ per_sfx = sfx->fetcher_data;
+
+ // Open it with the VorbisFile API
+ per_ch->ov_decode.buffer = per_sfx->file;
+ per_ch->ov_decode.ind = 0;
+ per_ch->ov_decode.buffsize = per_sfx->filesize;
+ if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
+ {
+ Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", sfx->name);
+ Mem_Free (per_ch);
+ return NULL;
+ }
+
+ // Get the stream information
+ vi = qov_info (&per_ch->vf, -1);
+ per_ch->format.speed = vi->rate;
+ per_ch->format.width = sfx->format.width;
+ per_ch->format.channels = sfx->format.channels;
+
+ per_ch->sb.offset = 0;
+ per_ch->sb.length = 0;
+ per_ch->bs = 0;
+
+ ch->fetcher_data = per_ch;
+ }
+
+ sb = &per_ch->sb;
+
+ // If the data we need has already been decompressed in the sfxbuffer, just return it
+ if (sb->offset <= start && sb->offset + sb->length >= start + nbsamples)
+ return sb;
+
+ newlength = sb->offset + sb->length - start;
+ factor = per_ch->format.width * per_ch->format.channels;
+
+ // If we need to skip some data before decompressing the rest, or if the stream has looped
+ if (newlength < 0 || sb->offset > start)
+ {
+ if (qov_pcm_seek (&per_ch->vf, (ogg_int64_t)start) != 0)
+ return NULL;
+
+ sb->offset = start;
+ sb->length = 0;
+ newlength = 0;
+ }
+ // Else, move forward the samples we need to keep in the sfxbuffer
+ else
+ {
+ memmove (sb->data, sb->data + (start - sb->offset) * factor, newlength * factor);
+ sb->offset = start;
+ sb->length = newlength;
+ }
+
+ // How many free bytes do we have in the sfxbuffer now?
+ newlength = STREAM_BUFFER_SIZE - (newlength * factor);
+
+ // Decompress in the resampling_buffer to get STREAM_BUFFER_SIZE samples after resampling
+#if BYTE_ORDER == LITTLE_ENDIAN
+ bigendian = 0;
+#else
+ bigendian = 1;
+#endif
+ done = 0;
+ while ((ret = qov_read (&per_ch->vf, &resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
+ done += ret;
+
+ // Resample in the sfxbuffer
+ newlength = ResampleSfx (resampling_buffer, (size_t)done / factor, &per_ch->format, sb->data + sb->length * factor, ch->sfx->name);
+ sb->length += newlength;
+
+ return sb;
+}
+
+
+/*
+====================
+OGG_FetchEnd
+====================
+*/
+static void OGG_FetchEnd (channel_t* ch)
+{
+ ogg_stream_perchannel_t* per_ch;
+
+ per_ch = ch->fetcher_data;
+ if (per_ch != NULL)
+ {
+ // Free the ogg vorbis decoder
+ qov_clear (&per_ch->vf);
+
+ Mem_Free (per_ch);
+ ch->fetcher_data = NULL;
+ }
+}
+
+static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd };
+extern snd_fetcher_t wav_fetcher;
+
+