revised commandline option comments for better categorization when sorted
[divverent/darkplaces.git] / snd_ogg.c
1 /*
2         Copyright (C) 2003-2004  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_ogg.h"
27 #include "snd_wav.h"
28
29
30 /*
31 =================================================================
32
33   Minimal set of definitions from the Ogg Vorbis lib
34   (C) COPYRIGHT 1994-2001 by the XIPHOPHORUS Company
35   http://www.xiph.org/
36
37   WARNING: for a matter of simplicity, several pointer types are
38   casted to "void*", and most enumerated values are not included
39
40 =================================================================
41 */
42
43 #ifdef _MSC_VER
44 typedef __int64 ogg_int64_t;
45 #else
46 typedef long long ogg_int64_t;
47 #endif
48
49 typedef struct
50 {
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);
55 } ov_callbacks;
56
57 typedef struct
58 {
59         unsigned char   *data;
60         int                             storage;
61         int                             fill;
62         int                             returned;
63         int                             unsynced;
64         int                             headerbytes;
65         int                             bodybytes;
66 } ogg_sync_state;
67
68 typedef struct
69 {
70         int             version;
71         int             channels;
72         long    rate;
73         long    bitrate_upper;
74         long    bitrate_nominal;
75         long    bitrate_lower;
76         long    bitrate_window;
77         void    *codec_setup;
78 } vorbis_info;
79
80 typedef struct
81 {
82         unsigned char   *body_data;
83         long                    body_storage;
84         long                    body_fill;
85         long                    body_returned;
86         int                             *lacing_vals;
87         ogg_int64_t             *granule_vals;
88         long                    lacing_storage;
89         long                    lacing_fill;
90         long                    lacing_packet;
91         long                    lacing_returned;
92         unsigned char   header[282];
93         int                             header_fill;
94         int                             e_o_s;
95         int                             b_o_s;
96         long                    serialno;
97         long                    pageno;
98         ogg_int64_t             packetno;
99         ogg_int64_t             granulepos;
100 } ogg_stream_state;
101
102 typedef struct
103 {
104         int                     analysisp;
105         vorbis_info     *vi;
106         float           **pcm;
107         float           **pcmret;
108         int                     pcm_storage;
109         int                     pcm_current;
110         int                     pcm_returned;
111         int                     preextrapolate;
112         int                     eofflag;
113         long            lW;
114         long            W;
115         long            nW;
116         long            centerW;
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;
123         void            *backend_state;
124 } vorbis_dsp_state;
125
126 typedef struct
127 {
128         long                    endbyte;
129         int                             endbit;
130         unsigned char   *buffer;
131         unsigned char   *ptr;
132         long                    storage;
133 } oggpack_buffer;
134
135 typedef struct
136 {
137         float                           **pcm;
138         oggpack_buffer          opb;
139         long                            lW;
140         long                            W;
141         long                            nW;
142         int                                     pcmend;
143         int                                     mode;
144         int                                     eofflag;
145         ogg_int64_t                     granulepos;
146         ogg_int64_t                     sequence;
147         vorbis_dsp_state        *vd;
148         void                            *localstore;
149         long                            localtop;
150         long                            localalloc;
151         long                            totaluse;
152         void                            *reap;  // VOIDED POINTER
153         long                            glue_bits;
154         long                            time_bits;
155         long                            floor_bits;
156         long                            res_bits;
157         void                            *internal;
158 } vorbis_block;
159
160 typedef struct
161 {
162         void                            *datasource;
163         int                                     seekable;
164         ogg_int64_t                     offset;
165         ogg_int64_t                     end;
166         ogg_sync_state          oy;
167         int                                     links;
168         ogg_int64_t                     *offsets;
169         ogg_int64_t                     *dataoffsets;
170         long                            *serialnos;
171         ogg_int64_t                     *pcmlengths;
172         vorbis_info                     *vi;
173         void                            *vc;  // VOIDED POINTER
174         ogg_int64_t                     pcm_offset;
175         int                                     ready_state;
176         long                            current_serialno;
177         int                                     current_link;
178         double                          bittrack;
179         double                          samptrack;
180         ogg_stream_state        os;
181         vorbis_dsp_state        vd;
182         vorbis_block            vb;
183         ov_callbacks            callbacks;
184 } OggVorbis_File;
185
186
187 /*
188 =================================================================
189
190   DarkPlaces definitions
191
192 =================================================================
193 */
194
195 // Functions exported from the vorbisfile library
196 static int (*qov_clear) (OggVorbis_File *vf);
197 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
198 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
199                                                                   char *initial, long ibytes,
200                                                                   ov_callbacks callbacks);
201 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
202 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
203 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
204                                                  int bigendianp,int word,int sgned,int *bitstream);
205
206 static dllfunction_t oggvorbisfuncs[] =
207 {
208         {"ov_clear",                    (void **) &qov_clear},
209         {"ov_info",                             (void **) &qov_info},
210         {"ov_open_callbacks",   (void **) &qov_open_callbacks},
211         {"ov_pcm_seek",                 (void **) &qov_pcm_seek},
212         {"ov_pcm_total",                (void **) &qov_pcm_total},
213         {"ov_read",                             (void **) &qov_read},
214         {NULL, NULL}
215 };
216
217 // Handle for the Vorbisfile DLL
218 static dllhandle_t vf_dll = NULL;
219
220 typedef struct
221 {
222         qbyte *buffer;
223         ogg_int64_t ind, buffsize;
224 } ov_decode_t;
225
226
227 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
228 {
229         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
230         size_t remain, len;
231
232         remain = ov_decode->buffsize - ov_decode->ind;
233         len = size * nb;
234         if (remain < len)
235                 len = remain - remain % size;
236
237         memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
238         ov_decode->ind += len;
239
240         return len / size;
241 }
242
243 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
244 {
245         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
246
247         switch (whence)
248         {
249                 case SEEK_SET:
250                         break;
251                 case SEEK_CUR:
252                         offset += ov_decode->ind;
253                         break;
254                 case SEEK_END:
255                         offset += ov_decode->buffsize;
256                         break;
257                 default:
258                         return -1;
259         }
260         if (offset < 0 || offset > ov_decode->buffsize)
261                 return -1;
262
263         ov_decode->ind = offset;
264         return 0;
265 }
266
267 static int ovcb_close (void *ov_decode)
268 {
269         return 0;
270 }
271
272 static long ovcb_tell (void *ov_decode)
273 {
274         return ((ov_decode_t*)ov_decode)->ind;
275 }
276
277
278 /*
279 =================================================================
280
281   DLL load & unload
282
283 =================================================================
284 */
285
286 /*
287 ====================
288 OGG_OpenLibrary
289
290 Try to load the VorbisFile DLL
291 ====================
292 */
293 qboolean OGG_OpenLibrary (void)
294 {
295         const char* dllname;
296
297         // Already loaded?
298         if (vf_dll)
299                 return true;
300
301 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
302         if (COM_CheckParm("-novorbis"))
303                 return false;
304
305 #ifdef WIN32
306         dllname = "vorbisfile.dll";
307 #else
308         dllname = "libvorbisfile.so";
309 #endif
310
311         // Load the DLL
312         if (! Sys_LoadLibrary (dllname, &vf_dll, oggvorbisfuncs))
313         {
314                 Con_Printf ("Ogg Vorbis support disabled\n");
315                 return false;
316         }
317
318         Con_Printf ("Ogg Vorbis support enabled\n");
319         return true;
320 }
321
322
323 /*
324 ====================
325 OGG_CloseLibrary
326
327 Unload the VorbisFile DLL
328 ====================
329 */
330 void OGG_CloseLibrary (void)
331 {
332         Sys_UnloadLibrary (&vf_dll);
333 }
334
335
336 /*
337 =================================================================
338
339         Ogg Vorbis decoding
340
341 =================================================================
342 */
343
344 #define STREAM_BUFFER_DURATION 1.5f     // 1.5 sec
345
346 // We work with 1 sec sequences, so this buffer must be able to contain
347 // 1 sec of sound of the highest quality (48 KHz, 16 bit samples, stereo)
348 static qbyte resampling_buffer [48000 * 2 * 2];
349
350
351 // Per-sfx data structure
352 typedef struct
353 {
354         qbyte                   *file;
355         size_t                  filesize;
356         snd_format_t    format;
357 } ogg_stream_persfx_t;
358
359 // Per-channel data structure
360 typedef struct
361 {
362         OggVorbis_File  vf;
363         ov_decode_t             ov_decode;
364         int                             bs;
365         sfxbuffer_t             sb;             // must be at the end due to its dynamically allocated size
366 } ogg_stream_perchannel_t;
367
368
369 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
370
371 /*
372 ====================
373 OGG_FetchSound
374 ====================
375 */
376 static const sfxbuffer_t* OGG_FetchSound (channel_t* ch, unsigned int start, unsigned int nbsamples)
377 {
378         ogg_stream_perchannel_t* per_ch;
379         sfxbuffer_t* sb;
380         sfx_t* sfx;
381         ogg_stream_persfx_t* per_sfx;
382         int newlength, done, ret, bigendian;
383         unsigned int factor;
384         size_t buff_len;
385
386         per_ch = ch->fetcher_data;
387         sfx = ch->sfx;
388         per_sfx = sfx->fetcher_data;
389         buff_len = ceil (STREAM_BUFFER_DURATION * (sfx->format.speed * sfx->format.width * sfx->format.channels));
390
391         // If there's no fetcher structure attached to the channel yet
392         if (per_ch == NULL)
393         {
394                 ogg_stream_persfx_t* per_sfx;
395
396                 per_ch = Mem_Alloc (sfx->mempool, sizeof (*per_ch) - sizeof (per_ch->sb.data) + buff_len);
397                 per_sfx = sfx->fetcher_data;
398
399                 // Open it with the VorbisFile API
400                 per_ch->ov_decode.buffer = per_sfx->file;
401                 per_ch->ov_decode.ind = 0;
402                 per_ch->ov_decode.buffsize = per_sfx->filesize;
403                 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
404                 {
405                         Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", sfx->name);
406                         Mem_Free (per_ch);
407                         return NULL;
408                 }
409
410                 per_ch->sb.offset = 0;
411                 per_ch->sb.length = 0;
412                 per_ch->bs = 0;
413
414                 ch->fetcher_data = per_ch;
415         }
416
417         sb = &per_ch->sb;
418         factor = per_sfx->format.width * per_sfx->format.channels;
419
420         // If the stream buffer can't contain that much samples anyway
421         if (nbsamples * factor > buff_len)
422         {
423                 Con_Printf ("OGG_FetchSound: stream buffer too small (%u bytes required)\n", nbsamples * factor);
424                 return NULL;
425         }
426
427         // If the data we need has already been decompressed in the sfxbuffer, just return it
428         if (sb->offset <= start && sb->offset + sb->length >= start + nbsamples)
429                 return sb;
430
431         newlength = sb->offset + sb->length - start;
432
433         // If we need to skip some data before decompressing the rest, or if the stream has looped
434         if (newlength < 0 || sb->offset > start)
435         {
436                 if (qov_pcm_seek (&per_ch->vf, (ogg_int64_t)start) != 0)
437                         return NULL;
438
439                 sb->offset = start;
440                 sb->length = 0;
441                 newlength = 0;
442         }
443         // Else, move forward the samples we need to keep in the sfxbuffer
444         else
445         {
446                 memmove (sb->data, sb->data + (start - sb->offset) * factor, newlength * factor);
447                 sb->offset = start;
448                 sb->length = newlength;
449         }
450
451         // We add exactly 1 sec of sound to the buffer:
452         // 1- to ensure we won't lose any sample during the resampling process
453         // 2- to force one call to OGG_FetchSound per second to regulate the workload
454         if ((sfx->format.speed + sb->length) * factor > buff_len)
455         {
456                 Con_Printf ("OGG_FetchSound: stream buffer overflow (%u bytes / %u)\n",
457                                         (sfx->format.speed + sb->length) * factor, buff_len);
458                 return NULL;
459         }
460         newlength = per_sfx->format.speed * factor;  // 1 sec of sound before resampling
461
462         // Decompress in the resampling_buffer
463 #if BYTE_ORDER == LITTLE_ENDIAN
464         bigendian = 0;
465 #else
466         bigendian = 1;
467 #endif
468         done = 0;
469         while ((ret = qov_read (&per_ch->vf, &resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
470                 done += ret;
471
472         // Resample in the sfxbuffer
473         newlength = ResampleSfx (resampling_buffer, (size_t)done / factor, &per_sfx->format, sb->data + sb->length * factor, sfx->name);
474         sb->length += newlength;
475
476         return sb;
477 }
478
479
480 /*
481 ====================
482 OGG_FetchEnd
483 ====================
484 */
485 static void OGG_FetchEnd (channel_t* ch)
486 {
487         ogg_stream_perchannel_t* per_ch;
488
489         per_ch = ch->fetcher_data;
490         if (per_ch != NULL)
491         {
492                 // Free the ogg vorbis decoder
493                 qov_clear (&per_ch->vf);
494
495                 Mem_Free (per_ch);
496                 ch->fetcher_data = NULL;
497         }
498 }
499
500 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd };
501
502
503 /*
504 ====================
505 OGG_LoadVorbisFile
506
507 Load an Ogg Vorbis file into memory
508 ====================
509 */
510 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *s)
511 {
512         qbyte *data;
513         ov_decode_t ov_decode;
514         OggVorbis_File vf;
515         vorbis_info *vi;
516         ogg_int64_t len, buff_len;
517
518         if (!vf_dll)
519                 return false;
520
521         Mem_FreePool (&s->mempool);
522         s->mempool = Mem_AllocPool (s->name, 0, NULL);
523
524         // Load the file
525         data = FS_LoadFile (filename, s->mempool, false);
526         if (data == NULL)
527         {
528                 Mem_FreePool (&s->mempool);
529                 return false;
530         }
531
532         Con_DPrintf ("Loading Ogg Vorbis file \"%s\"\n", filename);
533
534         // Open it with the VorbisFile API
535         ov_decode.buffer = data;
536         ov_decode.ind = 0;
537         ov_decode.buffsize = fs_filesize;
538         if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
539         {
540                 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
541                 Mem_FreePool (&s->mempool);
542                 return false;
543         }
544
545         // Get the stream information
546         vi = qov_info (&vf, -1);
547         if (vi->channels < 1 || vi->channels > 2)
548         {
549                 Con_Printf("%s has an unsupported number of channels (%i)\n",
550                                         s->name, vi->channels);
551                 qov_clear (&vf);
552                 Mem_FreePool (&s->mempool);
553                 return false;
554         }
555
556         len = qov_pcm_total (&vf, -1) * vi->channels * 2;  // 16 bits => "* 2"
557
558         // Decide if we go for a stream or a simple PCM cache
559         buff_len = ceil (STREAM_BUFFER_DURATION * (shm->format.speed * 2 * vi->channels));
560         if (snd_streaming.integer && len > fs_filesize + 3 * buff_len)
561         {
562                 ogg_stream_persfx_t* per_sfx;
563
564                 Con_DPrintf ("\"%s\" will be streamed\n", filename);
565                 per_sfx = Mem_Alloc (s->mempool, sizeof (*per_sfx));
566                 per_sfx->file = data;
567                 per_sfx->filesize = fs_filesize;
568
569                 per_sfx->format.speed = vi->rate;
570                 per_sfx->format.width = 2;  // We always work with 16 bits samples
571                 per_sfx->format.channels = vi->channels;
572                 s->format.speed = shm->format.speed;
573                 s->format.width = per_sfx->format.width;
574                 s->format.channels = per_sfx->format.channels;
575
576                 s->fetcher_data = per_sfx;
577                 s->fetcher = &ogg_fetcher;
578                 s->loopstart = -1;
579                 s->flags |= SFXFLAG_STREAMED;
580                 s->total_length = (size_t)len / per_sfx->format.channels / 2 * ((float)s->format.speed / per_sfx->format.speed);
581         }
582         else
583         {
584                 char *buff;
585                 ogg_int64_t done;
586                 int bs, bigendian;
587                 long ret;
588                 sfxbuffer_t *sb;
589
590                 Con_DPrintf ("\"%s\" will be streamed\n", filename);
591
592                 // Decode it
593                 buff = Mem_Alloc (s->mempool, (int)len);
594                 done = 0;
595                 bs = 0;
596 #if BYTE_ORDER == LITTLE_ENDIAN
597                 bigendian = 0;
598 #else
599                 bigendian = 1;
600 #endif
601                 while ((ret = qov_read (&vf, &buff[done], (int)(len - done), bigendian, 2, 1, &bs)) > 0)
602                         done += ret;
603
604                 // Calculate resampled length
605                 len = (double)done * (double)shm->format.speed / (double)vi->rate;
606
607                 // Resample it
608                 sb = Mem_Alloc (s->mempool, (size_t)len + sizeof (*sb) - sizeof (sb->data));
609                 s->fetcher_data = sb;
610                 s->fetcher = &wav_fetcher;
611                 s->format.speed = vi->rate;
612                 s->format.width = 2;  // We always work with 16 bits samples
613                 s->format.channels = vi->channels;
614                 s->loopstart = -1;
615                 s->flags &= ~SFXFLAG_STREAMED;
616
617                 sb->length = ResampleSfx (buff, (size_t)done / (vi->channels * 2), &s->format, sb->data, s->name);
618                 s->format.speed = shm->format.speed;
619                 s->total_length = sb->length;
620                 sb->offset = 0;
621
622                 qov_clear (&vf);
623                 Mem_Free (data);
624                 Mem_Free (buff);
625         }
626
627         return true;
628 }