- the Linux sound modules (ALSA and OSS) are now write-based, instead of
[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         void                            *datasource;
164         int                                     seekable;
165         ogg_int64_t                     offset;
166         ogg_int64_t                     end;
167         ogg_sync_state          oy;
168         int                                     links;
169         ogg_int64_t                     *offsets;
170         ogg_int64_t                     *dataoffsets;
171         long                            *serialnos;
172         ogg_int64_t                     *pcmlengths;
173         vorbis_info                     *vi;
174         void                            *vc;  // VOIDED POINTER
175         ogg_int64_t                     pcm_offset;
176         int                                     ready_state;
177         long                            current_serialno;
178         int                                     current_link;
179         double                          bittrack;
180         double                          samptrack;
181         ogg_stream_state        os;
182         vorbis_dsp_state        vd;
183         vorbis_block            vb;
184         ov_callbacks            callbacks;
185 } OggVorbis_File;
186
187
188 /*
189 =================================================================
190
191   DarkPlaces definitions
192
193 =================================================================
194 */
195
196 // Functions exported from the vorbisfile library
197 static int (*qov_clear) (OggVorbis_File *vf);
198 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
199 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
200                                                                   char *initial, long ibytes,
201                                                                   ov_callbacks callbacks);
202 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
203 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
204 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
205                                                  int bigendianp,int word,int sgned,int *bitstream);
206
207 static dllfunction_t oggvorbisfuncs[] =
208 {
209         {"ov_clear",                    (void **) &qov_clear},
210         {"ov_info",                             (void **) &qov_info},
211         {"ov_open_callbacks",   (void **) &qov_open_callbacks},
212         {"ov_pcm_seek",                 (void **) &qov_pcm_seek},
213         {"ov_pcm_total",                (void **) &qov_pcm_total},
214         {"ov_read",                             (void **) &qov_read},
215         {NULL, NULL}
216 };
217
218 // Handles for the Vorbis and Vorbisfile DLLs
219 static dllhandle_t vo_dll = NULL;
220 static dllhandle_t vf_dll = NULL;
221
222 typedef struct
223 {
224         unsigned char *buffer;
225         ogg_int64_t ind, buffsize;
226 } ov_decode_t;
227
228
229 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
230 {
231         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
232         size_t remain, len;
233
234         remain = ov_decode->buffsize - ov_decode->ind;
235         len = size * nb;
236         if (remain < len)
237                 len = remain - remain % size;
238
239         memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
240         ov_decode->ind += len;
241
242         return len / size;
243 }
244
245 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
246 {
247         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
248
249         switch (whence)
250         {
251                 case SEEK_SET:
252                         break;
253                 case SEEK_CUR:
254                         offset += ov_decode->ind;
255                         break;
256                 case SEEK_END:
257                         offset += ov_decode->buffsize;
258                         break;
259                 default:
260                         return -1;
261         }
262         if (offset < 0 || offset > ov_decode->buffsize)
263                 return -1;
264
265         ov_decode->ind = offset;
266         return 0;
267 }
268
269 static int ovcb_close (void *ov_decode)
270 {
271         return 0;
272 }
273
274 static long ovcb_tell (void *ov_decode)
275 {
276         return ((ov_decode_t*)ov_decode)->ind;
277 }
278
279
280 /*
281 =================================================================
282
283   DLL load & unload
284
285 =================================================================
286 */
287
288 /*
289 ====================
290 OGG_OpenLibrary
291
292 Try to load the VorbisFile DLL
293 ====================
294 */
295 qboolean OGG_OpenLibrary (void)
296 {
297         const char* dllnames_vo [] =
298         {
299 #if defined(WIN64)
300                 "libvorbis64.dll",
301 #elif defined(WIN32)
302                 "libvorbis.dll",
303                 "vorbis.dll",
304 #elif defined(MACOSX)
305                 "libvorbis.dylib",
306 #else
307                 "libvorbis.so.0",
308                 "libvorbis.so",
309 #endif
310                 NULL
311         };
312         const char* dllnames_vf [] =
313         {
314 #if defined(WIN64)
315                 "libvorbisfile64.dll",
316 #elif defined(WIN32)
317                 "libvorbisfile.dll",
318                 "vorbisfile.dll",
319 #elif defined(MACOSX)
320                 "libvorbisfile.dylib",
321 #else
322                 "libvorbisfile.so.3",
323                 "libvorbisfile.so",
324 #endif
325                 NULL
326         };
327
328         // Already loaded?
329         if (vf_dll)
330                 return true;
331
332 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
333         if (COM_CheckParm("-novorbis"))
334                 return false;
335
336         // Load the DLLs
337         // We need to load both by hand because some OSes seem to not load
338         // the vorbis DLL automatically when loading the VorbisFile DLL
339         if (! Sys_LoadLibrary (dllnames_vo, &vo_dll, NULL) ||
340                 ! Sys_LoadLibrary (dllnames_vf, &vf_dll, oggvorbisfuncs))
341         {
342                 Sys_UnloadLibrary (&vo_dll);
343                 Con_Printf ("Ogg Vorbis support disabled\n");
344                 return false;
345         }
346
347         Con_Printf ("Ogg Vorbis support enabled\n");
348         return true;
349 }
350
351
352 /*
353 ====================
354 OGG_CloseLibrary
355
356 Unload the VorbisFile DLL
357 ====================
358 */
359 void OGG_CloseLibrary (void)
360 {
361         Sys_UnloadLibrary (&vf_dll);
362         Sys_UnloadLibrary (&vo_dll);
363 }
364
365
366 /*
367 =================================================================
368
369         Ogg Vorbis decoding
370
371 =================================================================
372 */
373
374 #define STREAM_BUFFER_DURATION 1.5f     // 1.5 sec
375 #define STREAM_BUFFER_SIZE(format_ptr) ((int)(ceil (STREAM_BUFFER_DURATION * ((format_ptr)->speed * (format_ptr)->width * (format_ptr)->channels))))
376
377 // We work with 1 sec sequences, so this buffer must be able to contain
378 // 1 sec of sound of the highest quality (48 KHz, 16 bit samples, stereo)
379 static unsigned char resampling_buffer [48000 * 2 * 2];
380
381
382 // Per-sfx data structure
383 typedef struct
384 {
385         unsigned char   *file;
386         size_t                  filesize;
387         snd_format_t    format;
388 } ogg_stream_persfx_t;
389
390 // Per-channel data structure
391 typedef struct
392 {
393         OggVorbis_File  vf;
394         ov_decode_t             ov_decode;
395         unsigned int    sb_offset;
396         int                             bs;
397         snd_buffer_t    sb;             // must be at the end due to its dynamically allocated size
398 } ogg_stream_perchannel_t;
399
400
401 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
402
403 /*
404 ====================
405 OGG_FetchSound
406 ====================
407 */
408 static const snd_buffer_t* OGG_FetchSound (channel_t* ch, unsigned int* start, unsigned int nbsampleframes)
409 {
410         ogg_stream_perchannel_t* per_ch;
411         sfx_t* sfx;
412         ogg_stream_persfx_t* per_sfx;
413         snd_format_t* ogg_format;
414         snd_buffer_t* sb;
415         int newlength, done, ret, bigendian;
416         unsigned int real_start;
417         unsigned int factor;
418
419         per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
420         sfx = ch->sfx;
421         per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
422         ogg_format = &per_sfx->format;
423
424         // If there's no fetcher structure attached to the channel yet
425         if (per_ch == NULL)
426         {
427                 size_t buff_len, memsize;
428
429                 buff_len = STREAM_BUFFER_SIZE(ogg_format);
430                 memsize = sizeof (*per_ch) - sizeof (per_ch->sb.samples) + buff_len;
431                 per_ch = (ogg_stream_perchannel_t *)Mem_Alloc (snd_mempool, memsize);
432                 sfx->memsize += memsize;
433
434                 // Open it with the VorbisFile API
435                 per_ch->ov_decode.buffer = per_sfx->file;
436                 per_ch->ov_decode.ind = 0;
437                 per_ch->ov_decode.buffsize = per_sfx->filesize;
438                 if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
439                 {
440                         Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", sfx->name);
441                         Mem_Free (per_ch);
442                         return NULL;
443                 }
444                 per_ch->bs = 0;
445
446                 per_ch->sb_offset = 0;
447                 per_ch->sb.format.speed = snd_renderbuffer->format.speed;
448                 per_ch->sb.format.width = ogg_format->width;
449                 per_ch->sb.format.channels = ogg_format->channels;
450                 per_ch->sb.nbframes = 0;
451                 per_ch->sb.maxframes = buff_len / (per_ch->sb.format.channels * per_ch->sb.format.width);
452
453                 ch->fetcher_data = per_ch;
454         }
455         
456         real_start = *start;
457
458         sb = &per_ch->sb;
459         factor = per_sfx->format.width * per_sfx->format.channels;
460
461         // If the stream buffer can't contain that much samples anyway
462         if (nbsampleframes > sb->maxframes)
463         {
464                 Con_Printf ("OGG_FetchSound: stream buffer too small (%u sample frames required)\n", nbsampleframes);
465                 return NULL;
466         }
467
468         // If the data we need has already been decompressed in the sfxbuffer, just return it
469         if (per_ch->sb_offset <= real_start && per_ch->sb_offset + sb->nbframes >= real_start + nbsampleframes)
470         {
471                 *start = per_ch->sb_offset;
472                 return sb;
473         }
474
475         newlength = (int)(per_ch->sb_offset + sb->nbframes) - real_start;
476
477         // If we need to skip some data before decompressing the rest, or if the stream has looped
478         if (newlength < 0 || per_ch->sb_offset > real_start)
479         {
480                 unsigned int time_start;
481                 ogg_int64_t ogg_start;
482                 int err;
483                 
484                 if (real_start > sfx->total_length)
485                 {
486                         Con_Printf ("OGG_FetchSound: asked for a start position after the end of the sfx! (%u > %u)\n",
487                                                 real_start, sfx->total_length);
488                         return NULL;
489                 }
490
491                 // We work with 200ms (1/5 sec) steps to avoid rounding errors
492                 time_start = real_start * 5 / snd_renderbuffer->format.speed;
493                 ogg_start = time_start * (per_sfx->format.speed / 5);
494                 err = qov_pcm_seek (&per_ch->vf, ogg_start);
495                 if (err != 0)
496                 {
497                         Con_Printf ("OGG_FetchSound: qov_pcm_seek(..., %d) returned %d\n",
498                                                 real_start, err);
499                         return NULL;
500                 }
501                 sb->nbframes = 0;
502
503                 real_start = (float)ogg_start / per_sfx->format.speed * snd_renderbuffer->format.speed;
504                 if (*start - real_start + nbsampleframes > sb->maxframes)
505                 {
506                         Con_Printf ("OGG_FetchSound: stream buffer too small after seek (%u sample frames required)\n",
507                                                 *start - real_start + nbsampleframes);
508                         per_ch->sb_offset = real_start;
509                         return NULL;
510                 }
511         }
512         // Else, move forward the samples we need to keep in the sound buffer
513         else
514         {
515                 memmove (sb->samples, sb->samples + (real_start - per_ch->sb_offset) * factor, newlength * factor);
516                 sb->nbframes = newlength;
517         }
518
519         per_ch->sb_offset = real_start;
520
521         // We add exactly 1 sec of sound to the buffer:
522         // 1- to ensure we won't lose any sample during the resampling process
523         // 2- to force one call to OGG_FetchSound per second to regulate the workload
524         if (sb->format.speed + sb->nbframes > sb->maxframes)
525         {
526                 Con_Printf ("OGG_FetchSound: stream buffer overflow (%u sample frames / %u)\n",
527                                         sb->format.speed + sb->nbframes, sb->maxframes);
528                 return NULL;
529         }
530         newlength = per_sfx->format.speed * factor;  // -> 1 sec of sound before resampling
531
532         // Decompress in the resampling_buffer
533 #if BYTE_ORDER == BIG_ENDIAN
534         bigendian = 1;
535 #else
536         bigendian = 0;
537 #endif
538         done = 0;
539         while ((ret = qov_read (&per_ch->vf, (char *)&resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
540                 done += ret;
541
542         Snd_AppendToSndBuffer (sb, resampling_buffer, (size_t)done / (size_t)factor, &per_sfx->format);
543
544         *start = per_ch->sb_offset;
545         return sb;
546 }
547
548
549 /*
550 ====================
551 OGG_FetchEnd
552 ====================
553 */
554 static void OGG_FetchEnd (channel_t* ch)
555 {
556         ogg_stream_perchannel_t* per_ch;
557
558         per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
559         if (per_ch != NULL)
560         {
561                 size_t buff_len;
562
563                 // Free the ogg vorbis decoder
564                 qov_clear (&per_ch->vf);
565
566                 buff_len = per_ch->sb.maxframes * per_ch->sb.format.channels * per_ch->sb.format.width;
567                 ch->sfx->memsize -= sizeof (*per_ch) - sizeof (per_ch->sb.samples) + buff_len;
568
569                 Mem_Free (per_ch);
570                 ch->fetcher_data = NULL;
571         }
572 }
573
574
575 /*
576 ====================
577 OGG_FreeSfx
578 ====================
579 */
580 static void OGG_FreeSfx (sfx_t* sfx)
581 {
582         ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
583
584         // Free the Ogg Vorbis file
585         Mem_Free(per_sfx->file);
586         sfx->memsize -= per_sfx->filesize;
587
588         // Free the stream structure
589         Mem_Free(per_sfx);
590         sfx->memsize -= sizeof (*per_sfx);
591
592         sfx->fetcher_data = NULL;
593         sfx->fetcher = NULL;
594 }
595
596
597 /*
598 ====================
599 OGG_GetFormat
600 ====================
601 */
602 static const snd_format_t* OGG_GetFormat (sfx_t* sfx)
603 {
604         ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
605         return &per_sfx->format;
606 }
607
608 static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd, OGG_FreeSfx, OGG_GetFormat };
609
610
611 /*
612 ====================
613 OGG_LoadVorbisFile
614
615 Load an Ogg Vorbis file into memory
616 ====================
617 */
618 qboolean OGG_LoadVorbisFile (const char *filename, sfx_t *sfx)
619 {
620         unsigned char *data;
621         fs_offset_t filesize;
622         ov_decode_t ov_decode;
623         OggVorbis_File vf;
624         vorbis_info *vi;
625         ogg_int64_t len, buff_len;
626
627         if (!vf_dll)
628                 return false;
629
630         // Already loaded?
631         if (sfx->fetcher != NULL)
632                 return true;
633
634         // Load the file
635         data = FS_LoadFile (filename, snd_mempool, false, &filesize);
636         if (data == NULL)
637                 return false;
638
639         Con_DPrintf ("Loading Ogg Vorbis file \"%s\"\n", filename);
640
641         // Open it with the VorbisFile API
642         ov_decode.buffer = data;
643         ov_decode.ind = 0;
644         ov_decode.buffsize = filesize;
645         if (qov_open_callbacks (&ov_decode, &vf, NULL, 0, callbacks) < 0)
646         {
647                 Con_Printf ("error while opening Ogg Vorbis file \"%s\"\n", filename);
648                 Mem_Free(data);
649                 return false;
650         }
651
652         // Get the stream information
653         vi = qov_info (&vf, -1);
654         if (vi->channels < 1 || vi->channels > 2)
655         {
656                 Con_Printf("%s has an unsupported number of channels (%i)\n",
657                                         sfx->name, vi->channels);
658                 qov_clear (&vf);
659                 Mem_Free(data);
660                 return false;
661         }
662
663         len = qov_pcm_total (&vf, -1) * vi->channels * 2;  // 16 bits => "* 2"
664
665         // Decide if we go for a stream or a simple PCM cache
666         buff_len = (int)ceil (STREAM_BUFFER_DURATION * (snd_renderbuffer->format.speed * 2 * vi->channels));
667         if (snd_streaming.integer && len > (ogg_int64_t)filesize + 3 * buff_len)
668         {
669                 ogg_stream_persfx_t* per_sfx;
670
671                 Con_DPrintf ("\"%s\" will be streamed\n", filename);
672                 per_sfx = (ogg_stream_persfx_t *)Mem_Alloc (snd_mempool, sizeof (*per_sfx));
673                 sfx->memsize += sizeof (*per_sfx);
674                 per_sfx->file = data;
675                 per_sfx->filesize = filesize;
676                 sfx->memsize += filesize;
677
678                 per_sfx->format.speed = vi->rate;
679                 per_sfx->format.width = 2;  // We always work with 16 bits samples
680                 per_sfx->format.channels = vi->channels;
681
682                 sfx->fetcher_data = per_sfx;
683                 sfx->fetcher = &ogg_fetcher;
684                 sfx->loopstart = -1;
685                 sfx->flags |= SFXFLAG_STREAMED;
686                 sfx->total_length = (int)((size_t)len / (per_sfx->format.channels * 2) * ((double)snd_renderbuffer->format.speed / per_sfx->format.speed));
687         }
688         else
689         {
690                 char *buff;
691                 ogg_int64_t done;
692                 int bs, bigendian;
693                 long ret;
694                 snd_buffer_t *sb;
695                 snd_format_t ogg_format;
696
697                 Con_DPrintf ("\"%s\" will be cached\n", filename);
698
699                 // Decode it
700                 buff = (char *)Mem_Alloc (snd_mempool, (int)len);
701                 done = 0;
702                 bs = 0;
703 #if BYTE_ORDER == BIG_ENDIAN
704                 bigendian = 1;
705 #else
706                 bigendian = 0;
707 #endif
708                 while ((ret = qov_read (&vf, &buff[done], (int)(len - done), bigendian, 2, 1, &bs)) > 0)
709                         done += ret;
710
711                 // Build the sound buffer
712                 ogg_format.speed = vi->rate;
713                 ogg_format.channels = vi->channels;
714                 ogg_format.width = 2;  // We always work with 16 bits samples
715                 sb = Snd_CreateSndBuffer ((unsigned char *)buff, (size_t)done / (vi->channels * 2), &ogg_format, snd_renderbuffer->format.speed);
716                 if (sb == NULL)
717                 {
718                         qov_clear (&vf);
719                         Mem_Free (data);
720                         Mem_Free (buff);
721                         return false;
722                 }
723
724                 sfx->fetcher = &wav_fetcher;
725                 sfx->fetcher_data = sb;
726
727                 sfx->total_length = sb->nbframes;
728                 sfx->memsize += sb->maxframes * sb->format.channels * sb->format.width + sizeof (*sb) - sizeof (sb->samples);
729
730                 sfx->loopstart = -1;
731                 sfx->flags &= ~SFXFLAG_STREAMED;
732
733                 qov_clear (&vf);
734                 Mem_Free (data);
735                 Mem_Free (buff);
736         }
737
738         return true;
739 }