LOOP_START tag in vorbis files now gives the sample number to loop from
[divverent/darkplaces.git] / snd_ogg.c
1 /*
2         Copyright (C) 2003-2005  Mathieu Olivier
3
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.
8
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.
12
13         See the GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to:
17
18                 Free Software Foundation, Inc.
19                 59 Temple Place - Suite 330
20                 Boston, MA  02111-1307, USA
21
22 */
23
24
25 #include "quakedef.h"
26 #include "snd_main.h"
27 #include "snd_ogg.h"
28 #include "snd_wav.h"
29
30
31 /*
32 =================================================================
33
34   Minimal set of definitions from the Ogg Vorbis lib
35   (C) COPYRIGHT 1994-2001 by the XIPHOPHORUS Company
36   http://www.xiph.org/
37
38   WARNING: for a matter of simplicity, several pointer types are
39   casted to "void*", and most enumerated values are not included
40
41 =================================================================
42 */
43
44 #ifdef _MSC_VER
45 typedef __int64 ogg_int64_t;
46 #else
47 typedef long long ogg_int64_t;
48 #endif
49
50 typedef struct
51 {
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);
56 } ov_callbacks;
57
58 typedef struct
59 {
60         unsigned char   *data;
61         int                             storage;
62         int                             fill;
63         int                             returned;
64         int                             unsynced;
65         int                             headerbytes;
66         int                             bodybytes;
67 } ogg_sync_state;
68
69 typedef struct
70 {
71         int             version;
72         int             channels;
73         long    rate;
74         long    bitrate_upper;
75         long    bitrate_nominal;
76         long    bitrate_lower;
77         long    bitrate_window;
78         void    *codec_setup;
79 } vorbis_info;
80
81 typedef struct
82 {
83         unsigned char   *body_data;
84         long                    body_storage;
85         long                    body_fill;
86         long                    body_returned;
87         int                             *lacing_vals;
88         ogg_int64_t             *granule_vals;
89         long                    lacing_storage;
90         long                    lacing_fill;
91         long                    lacing_packet;
92         long                    lacing_returned;
93         unsigned char   header[282];
94         int                             header_fill;
95         int                             e_o_s;
96         int                             b_o_s;
97         long                    serialno;
98         long                    pageno;
99         ogg_int64_t             packetno;
100         ogg_int64_t             granulepos;
101 } ogg_stream_state;
102
103 typedef struct
104 {
105         int                     analysisp;
106         vorbis_info     *vi;
107         float           **pcm;
108         float           **pcmret;
109         int                     pcm_storage;
110         int                     pcm_current;
111         int                     pcm_returned;
112         int                     preextrapolate;
113         int                     eofflag;
114         long            lW;
115         long            W;
116         long            nW;
117         long            centerW;
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;
124         void            *backend_state;
125 } vorbis_dsp_state;
126
127 typedef struct
128 {
129         long                    endbyte;
130         int                             endbit;
131         unsigned char   *buffer;
132         unsigned char   *ptr;
133         long                    storage;
134 } oggpack_buffer;
135
136 typedef struct
137 {
138         float                           **pcm;
139         oggpack_buffer          opb;
140         long                            lW;
141         long                            W;
142         long                            nW;
143         int                                     pcmend;
144         int                                     mode;
145         int                                     eofflag;
146         ogg_int64_t                     granulepos;
147         ogg_int64_t                     sequence;
148         vorbis_dsp_state        *vd;
149         void                            *localstore;
150         long                            localtop;
151         long                            localalloc;
152         long                            totaluse;
153         void                            *reap;  // VOIDED POINTER
154         long                            glue_bits;
155         long                            time_bits;
156         long                            floor_bits;
157         long                            res_bits;
158         void                            *internal;
159 } vorbis_block;
160
161 typedef struct
162 {
163         char **user_comments;
164         int   *comment_lengths;
165         int    comments;
166         char  *vendor;
167 } vorbis_comment;
168
169 typedef struct
170 {
171         void                            *datasource;
172         int                                     seekable;
173         ogg_int64_t                     offset;
174         ogg_int64_t                     end;
175         ogg_sync_state          oy;
176         int                                     links;
177         ogg_int64_t                     *offsets;
178         ogg_int64_t                     *dataoffsets;
179         long                            *serialnos;
180         ogg_int64_t                     *pcmlengths;
181         vorbis_info                     *vi;
182         vorbis_comment          *vc;
183         ogg_int64_t                     pcm_offset;
184         int                                     ready_state;
185         long                            current_serialno;
186         int                                     current_link;
187         double                          bittrack;
188         double                          samptrack;
189         ogg_stream_state        os;
190         vorbis_dsp_state        vd;
191         vorbis_block            vb;
192         ov_callbacks            callbacks;
193 } OggVorbis_File;
194
195
196 /*
197 =================================================================
198
199   DarkPlaces definitions
200
201 =================================================================
202 */
203
204 // Functions exported from the vorbisfile library
205 static int (*qov_clear) (OggVorbis_File *vf);
206 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
207 static vorbis_comment* (*qov_comment) (OggVorbis_File *vf,int link);
208 static char * (*qvorbis_comment_query) (vorbis_comment *vc, char *tag, int count);
209 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
210                                                                   char *initial, long ibytes,
211                                                                   ov_callbacks callbacks);
212 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
213 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
214 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
215                                                  int bigendianp,int word,int sgned,int *bitstream);
216
217 static dllfunction_t oggvorbisfuncs[] =
218 {
219         {"ov_clear",                            (void **) &qov_clear},
220         {"ov_info",                                     (void **) &qov_info},
221         {"ov_comment",                          (void **) &qov_comment},
222         {"vorbis_comment_query",        (void **) &qvorbis_comment_query},
223         {"ov_open_callbacks",           (void **) &qov_open_callbacks},
224         {"ov_pcm_seek",                         (void **) &qov_pcm_seek},
225         {"ov_pcm_total",                        (void **) &qov_pcm_total},
226         {"ov_read",                                     (void **) &qov_read},
227         {NULL, NULL}
228 };
229
230 // Handles for the Vorbis and Vorbisfile DLLs
231 static dllhandle_t vo_dll = NULL;
232 static dllhandle_t vf_dll = NULL;
233
234 typedef struct
235 {
236         unsigned char *buffer;
237         ogg_int64_t ind, buffsize;
238 } ov_decode_t;
239
240
241 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
242 {
243         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
244         size_t remain, len;
245
246         remain = ov_decode->buffsize - ov_decode->ind;
247         len = size * nb;
248         if (remain < len)
249                 len = remain - remain % size;
250
251         memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
252         ov_decode->ind += len;
253
254         return len / size;
255 }
256
257 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
258 {
259         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
260
261         switch (whence)
262         {
263                 case SEEK_SET:
264                         break;
265                 case SEEK_CUR:
266                         offset += ov_decode->ind;
267                         break;
268                 case SEEK_END:
269                         offset += ov_decode->buffsize;
270                         break;
271                 default:
272                         return -1;
273         }
274         if (offset < 0 || offset > ov_decode->buffsize)
275                 return -1;
276
277         ov_decode->ind = offset;
278         return 0;
279 }
280
281 static int ovcb_close (void *ov_decode)
282 {
283         return 0;
284 }
285
286 static long ovcb_tell (void *ov_decode)
287 {
288         return ((ov_decode_t*)ov_decode)->ind;
289 }
290
291
292 /*
293 =================================================================
294
295   DLL load & unload
296
297 =================================================================
298 */
299
300 /*
301 ====================
302 OGG_OpenLibrary
303
304 Try to load the VorbisFile DLL
305 ====================
306 */
307 qboolean OGG_OpenLibrary (void)
308 {
309         const char* dllnames_vo [] =
310         {
311 #if defined(WIN64)
312                 "libvorbis64.dll",
313 #elif defined(WIN32)
314                 "libvorbis.dll",
315                 "vorbis.dll",
316 #elif defined(MACOSX)
317                 "libvorbis.dylib",
318 #else
319                 "libvorbis.so.0",
320                 "libvorbis.so",
321 #endif
322                 NULL
323         };
324         const char* dllnames_vf [] =
325         {
326 #if defined(WIN64)
327                 "libvorbisfile64.dll",
328 #elif defined(WIN32)
329                 "libvorbisfile.dll",
330                 "vorbisfile.dll",
331 #elif defined(MACOSX)
332                 "libvorbisfile.dylib",
333 #else
334                 "libvorbisfile.so.3",
335                 "libvorbisfile.so",
336 #endif
337                 NULL
338         };
339
340         // Already loaded?
341         if (vf_dll)
342                 return true;
343
344 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
345         if (COM_CheckParm("-novorbis"))
346                 return false;
347
348         // Load the DLLs
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         if (! Sys_LoadLibrary (dllnames_vo, &vo_dll, NULL) ||
352                 ! Sys_LoadLibrary (dllnames_vf, &vf_dll, oggvorbisfuncs))
353         {
354                 Sys_UnloadLibrary (&vo_dll);
355                 Con_Printf ("Ogg Vorbis support disabled\n");
356                 return false;
357         }
358
359         Con_Printf ("Ogg Vorbis support enabled\n");
360         return true;
361 }
362
363
364 /*
365 ====================
366 OGG_CloseLibrary
367
368 Unload the VorbisFile DLL
369 ====================
370 */
371 void OGG_CloseLibrary (void)
372 {
373         Sys_UnloadLibrary (&vf_dll);
374         Sys_UnloadLibrary (&vo_dll);
375 }
376
377
378 /*
379 =================================================================
380
381         Ogg Vorbis decoding
382
383 =================================================================
384 */
385
386 #define STREAM_BUFFER_DURATION 1.5f     // 1.5 sec
387 #define STREAM_BUFFER_SIZE(format_ptr) ((int)(ceil (STREAM_BUFFER_DURATION * ((format_ptr)->speed * (format_ptr)->width * (format_ptr)->channels))))
388
389 // We work with 1 sec sequences, so this buffer must be able to contain
390 // 1 sec of sound of the highest quality (48 KHz, 16 bit samples, stereo)
391 static unsigned char resampling_buffer [48000 * 2 * 2];
392
393
394 // Per-sfx data structure
395 typedef struct
396 {
397         unsigned char   *file;
398         size_t                  filesize;
399         snd_format_t    format;
400 } ogg_stream_persfx_t;
401
402 // Per-channel data structure
403 typedef struct
404 {
405         OggVorbis_File  vf;
406         ov_decode_t             ov_decode;
407         unsigned int    sb_offset;
408         int                             bs;
409         snd_buffer_t    sb;             // must be at the end due to its dynamically allocated size
410 } ogg_stream_perchannel_t;
411
412
413 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
414
415 /*
416 ====================
417 OGG_FetchSound
418 ====================
419 */
420 static const snd_buffer_t* OGG_FetchSound (channel_t* ch, unsigned int* start, unsigned int nbsampleframes)
421 {
422         ogg_stream_perchannel_t* per_ch;
423         sfx_t* sfx;
424         ogg_stream_persfx_t* per_sfx;
425         snd_buffer_t* sb;
426         int newlength, done, ret, bigendian;
427         unsigned int real_start;
428         unsigned int factor;
429
430         per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
431         sfx = ch->sfx;
432         per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
433
434         // If there's no fetcher structure attached to the channel yet
435         if (per_ch == NULL)
436         {
437                 size_t buff_len, memsize;
438                 snd_format_t sb_format;
439
440                 sb_format.speed = snd_renderbuffer->format.speed;
441                 sb_format.width = per_sfx->format.width;
442                 sb_format.channels = per_sfx->format.channels;
443
444                 buff_len = STREAM_BUFFER_SIZE(&sb_format);
445                 memsize = sizeof (*per_ch) - sizeof (per_ch->sb.samples) + buff_len;
446                 per_ch = (ogg_stream_perchannel_t *)Mem_Alloc (snd_mempool, memsize);
447                 sfx->memsize += memsize;
448
449                 // Open it with the VorbisFile API
450                 per_ch->ov_decode.buffer = per_sfx->file;
451                 per_ch->ov_decode.ind = 0;
452                 per_ch->ov_decode.buffsize = per_sfx->filesize;
453                 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
454                 {
455                         Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", sfx->name);
456                         Mem_Free (per_ch);
457                         return NULL;
458                 }
459                 per_ch->bs = 0;
460
461                 per_ch->sb_offset = 0;
462                 per_ch->sb.format = sb_format;
463                 per_ch->sb.nbframes = 0;
464                 per_ch->sb.maxframes = buff_len / (per_ch->sb.format.channels * per_ch->sb.format.width);
465
466                 ch->fetcher_data = per_ch;
467         }
468
469         real_start = *start;
470
471         sb = &per_ch->sb;
472         factor = per_sfx->format.width * per_sfx->format.channels;
473
474         // If the stream buffer can't contain that much samples anyway
475         if (nbsampleframes > sb->maxframes)
476         {
477                 Con_Printf ("OGG_FetchSound: stream buffer too small (%u sample frames required)\n", nbsampleframes);
478                 return NULL;
479         }
480
481         // If the data we need has already been decompressed in the sfxbuffer, just return it
482         if (per_ch->sb_offset <= real_start && per_ch->sb_offset + sb->nbframes >= real_start + nbsampleframes)
483         {
484                 *start = per_ch->sb_offset;
485                 return sb;
486         }
487
488         newlength = (int)(per_ch->sb_offset + sb->nbframes) - real_start;
489
490         // If we need to skip some data before decompressing the rest, or if the stream has looped
491         if (newlength < 0 || per_ch->sb_offset > real_start)
492         {
493                 unsigned int time_start;
494                 ogg_int64_t ogg_start;
495                 int err;
496
497                 if (real_start > (unsigned int)sfx->total_length)
498                 {
499                         Con_Printf ("OGG_FetchSound: asked for a start position after the end of the sfx! (%u > %u)\n",
500                                                 real_start, sfx->total_length);
501                         return NULL;
502                 }
503
504                 // We work with 200ms (1/5 sec) steps to avoid rounding errors
505                 time_start = real_start * 5 / snd_renderbuffer->format.speed;
506                 ogg_start = time_start * (per_sfx->format.speed / 5);
507                 err = qov_pcm_seek (&per_ch->vf, ogg_start);
508                 if (err != 0)
509                 {
510                         Con_Printf ("OGG_FetchSound: qov_pcm_seek(..., %d) returned %d\n",
511                                                 real_start, err);
512                         return NULL;
513                 }
514                 sb->nbframes = 0;
515
516                 real_start = (float)ogg_start / per_sfx->format.speed * snd_renderbuffer->format.speed;
517                 if (*start - real_start + nbsampleframes > sb->maxframes)
518                 {
519                         Con_Printf ("OGG_FetchSound: stream buffer too small after seek (%u sample frames required)\n",
520                                                 *start - real_start + nbsampleframes);
521                         per_ch->sb_offset = real_start;
522                         return NULL;
523                 }
524         }
525         // Else, move forward the samples we need to keep in the sound buffer
526         else
527         {
528                 memmove (sb->samples, sb->samples + (real_start - per_ch->sb_offset) * factor, newlength * factor);
529                 sb->nbframes = newlength;
530         }
531
532         per_ch->sb_offset = real_start;
533
534         // We add exactly 1 sec of sound to the buffer:
535         // 1- to ensure we won't lose any sample during the resampling process
536         // 2- to force one call to OGG_FetchSound per second to regulate the workload
537         if (sb->format.speed + sb->nbframes > sb->maxframes)
538         {
539                 Con_Printf ("OGG_FetchSound: stream buffer overflow (%u sample frames / %u)\n",
540                                         sb->format.speed + sb->nbframes, sb->maxframes);
541                 return NULL;
542         }
543         newlength = per_sfx->format.speed * factor;  // -> 1 sec of sound before resampling
544
545         // Decompress in the resampling_buffer
546 #if BYTE_ORDER == BIG_ENDIAN
547         bigendian = 1;
548 #else
549         bigendian = 0;
550 #endif
551         done = 0;
552         while ((ret = qov_read (&per_ch->vf, (char *)&resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
553                 done += ret;
554
555         Snd_AppendToSndBuffer (sb, resampling_buffer, (size_t)done / (size_t)factor, &per_sfx->format);
556
557         *start = per_ch->sb_offset;
558         return sb;
559 }
560
561
562 /*
563 ====================
564 OGG_FetchEnd
565 ====================
566 */
567 static void OGG_FetchEnd (channel_t* ch)
568 {
569         ogg_stream_perchannel_t* per_ch;
570
571         per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
572         if (per_ch != NULL)
573         {
574                 size_t buff_len;
575
576                 // Free the ogg vorbis decoder
577                 qov_clear (&per_ch->vf);
578
579                 buff_len = per_ch->sb.maxframes * per_ch->sb.format.channels * per_ch->sb.format.width;
580                 ch->sfx->memsize -= sizeof (*per_ch) - sizeof (per_ch->sb.samples) + buff_len;
581
582                 Mem_Free (per_ch);
583                 ch->fetcher_data = NULL;
584         }
585 }
586
587
588 /*
589 ====================
590 OGG_FreeSfx
591 ====================
592 */
593 static void OGG_FreeSfx (sfx_t* sfx)
594 {
595         ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
596
597         // Free the Ogg Vorbis file
598         Mem_Free(per_sfx->file);
599         sfx->memsize -= per_sfx->filesize;
600
601         // Free the stream structure
602         Mem_Free(per_sfx);
603         sfx->memsize -= sizeof (*per_sfx);
604
605         sfx->fetcher_data = NULL;
606         sfx->fetcher = NULL;
607 }
608
609
610 /*
611 ====================
612 OGG_GetFormat
613 ====================
614 */
615 static const snd_format_t* OGG_GetFormat (sfx_t* sfx)
616 {
617         ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
618         return &per_sfx->format;
619 }
620
621 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd, OGG_FreeSfx, OGG_GetFormat };
622
623
624 /*
625 ====================
626 OGG_LoadVorbisFile
627
628 Load an Ogg Vorbis file into memory
629 ====================
630 */
631 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *sfx)
632 {
633         unsigned char *data;
634         const char *loopcomment;
635         fs_offset_t filesize;
636         ov_decode_t ov_decode;
637         OggVorbis_File vf;
638         vorbis_info *vi;
639         vorbis_comment *vc;
640         ogg_int64_t len, buff_len;
641
642         if (!vf_dll)
643                 return false;
644
645         // Already loaded?
646         if (sfx->fetcher != NULL)
647                 return true;
648
649         // Load the file
650         data = FS_LoadFile (filename, snd_mempool, false, &filesize);
651         if (data == NULL)
652                 return false;
653
654         Con_DPrintf ("Loading Ogg Vorbis file \"%s\"\n", filename);
655
656         // Open it with the VorbisFile API
657         ov_decode.buffer = data;
658         ov_decode.ind = 0;
659         ov_decode.buffsize = filesize;
660         if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
661         {
662                 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
663                 Mem_Free(data);
664                 return false;
665         }
666
667         // Get the stream information
668         vi = qov_info (&vf, -1);
669         if (vi->channels < 1 || vi->channels > 2)
670         {
671                 Con_Printf("%s has an unsupported number of channels (%i)\n",
672                                         sfx->name, vi->channels);
673                 qov_clear (&vf);
674                 Mem_Free(data);
675                 return false;
676         }
677
678         len = qov_pcm_total (&vf, -1) * vi->channels * 2;  // 16 bits => "* 2"
679
680         // Decide if we go for a stream or a simple PCM cache
681         buff_len = (int)ceil (STREAM_BUFFER_DURATION * (snd_renderbuffer->format.speed * 2 * vi->channels));
682         if (snd_streaming.integer && len > (ogg_int64_t)filesize + 3 * buff_len)
683         {
684                 ogg_stream_persfx_t* per_sfx;
685
686                 Con_DPrintf ("\"%s\" will be streamed\n", filename);
687                 per_sfx = (ogg_stream_persfx_t *)Mem_Alloc (snd_mempool, sizeof (*per_sfx));
688                 sfx->memsize += sizeof (*per_sfx);
689                 per_sfx->file = data;
690                 per_sfx->filesize = filesize;
691                 sfx->memsize += filesize;
692
693                 per_sfx->format.speed = vi->rate;
694                 per_sfx->format.width = 2;  // We always work with 16 bits samples
695                 per_sfx->format.channels = vi->channels;
696
697                 sfx->fetcher_data = per_sfx;
698                 sfx->fetcher = &ogg_fetcher;
699                 sfx->flags |= SFXFLAG_STREAMED;
700                 sfx->total_length = (int)((size_t)len / (per_sfx->format.channels * 2) * ((double)snd_renderbuffer->format.speed / per_sfx->format.speed));
701                 sfx->loopstart = sfx->total_length;
702                 vc = qov_comment(&vf, -1);
703                 if(vc)
704                 {
705                         loopcomment = qvorbis_comment_query(vc, "LOOP_START", 0);
706                         if(loopcomment)
707                                 sfx->loopstart = bound(0, (unsigned int) (atof(loopcomment) * (double)snd_renderbuffer->format.speed / (double)per_sfx->format.speed), sfx->total_length);
708                 }
709         }
710         else
711         {
712                 char *buff;
713                 ogg_int64_t done;
714                 int bs, bigendian;
715                 long ret;
716                 snd_buffer_t *sb;
717                 snd_format_t ogg_format;
718
719                 Con_DPrintf ("\"%s\" will be cached\n", filename);
720
721                 // Decode it
722                 buff = (char *)Mem_Alloc (snd_mempool, (int)len);
723                 done = 0;
724                 bs = 0;
725 #if BYTE_ORDER == BIG_ENDIAN
726                 bigendian = 1;
727 #else
728                 bigendian = 0;
729 #endif
730                 while ((ret = qov_read (&vf, &buff[done], (int)(len - done), bigendian, 2, 1, &bs)) > 0)
731                         done += ret;
732
733                 // Build the sound buffer
734                 ogg_format.speed = vi->rate;
735                 ogg_format.channels = vi->channels;
736                 ogg_format.width = 2;  // We always work with 16 bits samples
737                 sb = Snd_CreateSndBuffer ((unsigned char *)buff, (size_t)done / (vi->channels * 2), &ogg_format, snd_renderbuffer->format.speed);
738                 if (sb == NULL)
739                 {
740                         qov_clear (&vf);
741                         Mem_Free (data);
742                         Mem_Free (buff);
743                         return false;
744                 }
745
746                 sfx->fetcher = &wav_fetcher;
747                 sfx->fetcher_data = sb;
748
749                 sfx->total_length = sb->nbframes;
750                 sfx->memsize += sb->maxframes * sb->format.channels * sb->format.width + sizeof (*sb) - sizeof (sb->samples);
751
752                 sfx->loopstart = sfx->total_length;
753                 sfx->flags &= ~SFXFLAG_STREAMED;
754                 vc = qov_comment(&vf, -1);
755                 if(vc)
756                 {
757                         loopcomment = qvorbis_comment_query(vc, "LOOP_START", 0);
758                         if(loopcomment)
759                                 sfx->loopstart = bound(0, (unsigned int) (atoi(loopcomment) * (double)snd_renderbuffer->format.speed / (double)sb->format.speed), sfx->total_length);
760                 }
761
762                 qov_clear (&vf);
763                 Mem_Free (data);
764                 Mem_Free (buff);
765         }
766
767         return true;
768 }