forked from krkrz/wuvorbis
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WuVorbisMainUnit.cpp
788 lines (697 loc) · 22.3 KB
/
WuVorbisMainUnit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
//---------------------------------------------------------------------------
// Ogg Vorbis plugin for TSS ( stands for TVP Sound System )
// This is released under TVP(Kirikiri)'s license.
// See details for TVP source distribution.
//---------------------------------------------------------------------------
extern "C" {
#include "vorbis/vorbisfile.h"
}
#include <windows.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#ifndef NOT_HAVE_TP_STUB
#include "tp_stub.h"
#endif
#pragma hdrstop
#include "tvpsnd.h" // TSS sound system interface definitions
static bool FloatExtraction = false; // true if output format is IEEE 32-bit float
#ifndef NOT_HAVE_TP_STUB
static bool TVPAlive = false; // true if the DLL is called from TVP
#endif
#define WARN_OLD_VORBIS_DATE 20020717
// warn if the stream is generated by old vorbis encoder earlier than this
#define WARN_PREFER_ENCODER_VERSION "Vorbis encoder 1.0 (dated 2002/07/17) or later is recommended."
static bool OldEncoderWarned = false;
static LONG AllocCount = 0; // memory block allocation count for decoder
static bool Look_Replay_Gain = false; // whether to look replay gain information
static bool Use_Album_Gain = false; // whether to use album gain, otherwise use track gain
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
void strcpy_limit(LPWSTR dest, LPWSTR src, int n)
{
// string copy with limitation
// this will add a null terminater at destination buffer
wcsncpy(dest, src, n-1);
dest[n-1] = '\0';
}
//---------------------------------------------------------------------------
ITSSStorageProvider *StorageProvider = NULL;
//---------------------------------------------------------------------------
class VorbisModule : public ITSSModule // module interface
{
ULONG RefCount; // reference count
public:
VorbisModule();
~VorbisModule();
public:
// IUnknown
HRESULT __stdcall QueryInterface(REFIID iid, void ** ppvObject);
ULONG __stdcall AddRef(void);
ULONG __stdcall Release(void);
// ITSSModule
HRESULT __stdcall GetModuleCopyright(LPWSTR buffer, unsigned long buflen );
HRESULT __stdcall GetModuleDescription(LPWSTR buffer, unsigned long buflen );
HRESULT __stdcall GetSupportExts(unsigned long index, LPWSTR mediashortname, LPWSTR buf, unsigned long buflen );
HRESULT __stdcall GetMediaInfo(LPWSTR url, ITSSMediaBaseInfo ** info );
HRESULT __stdcall GetMediaSupport(LPWSTR url );
HRESULT __stdcall GetMediaInstance(LPWSTR url, IUnknown ** instance );
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
class VorbisWaveDecoder : public ITSSWaveDecoder // decoder interface
{
ULONG RefCount; // refernce count
bool InputFileInit; // whether InputFile is inited
OggVorbis_File InputFile; // OggVorbis_File instance
IStream *InputStream; // input stream
TSSWaveFormat Format; // output PCM format
int CurrentSection; // current section in vorbis stream
public:
VorbisWaveDecoder();
~VorbisWaveDecoder();
public:
// IUnkown
HRESULT __stdcall QueryInterface(REFIID iid, void ** ppvObject);
ULONG __stdcall AddRef(void);
ULONG __stdcall Release(void);
// ITSSWaveDecoder
HRESULT __stdcall GetFormat(TSSWaveFormat *format);
HRESULT __stdcall Render(void *buf, unsigned long bufsamplelen,
unsigned long *rendered, unsigned long *status);
HRESULT __stdcall SetPosition(unsigned __int64 samplepos);
// others
HRESULT SetStream(IStream *stream, LPWSTR url);
private:
size_t static _cdecl read_func(void *ptr, size_t size, size_t nmemb, void *datasource);
int static _cdecl seek_func(void *datasource, __int64 offset, int whence);
int static _cdecl close_func(void *datasource);
long static _cdecl tell_func(void *datasource);
};
//---------------------------------------------------------------------------
// VorbisModule implementation ##############################################
//---------------------------------------------------------------------------
VorbisModule::VorbisModule()
{
// VorbisModule constructor
RefCount = 1;
}
//---------------------------------------------------------------------------
VorbisModule::~VorbisModule()
{
// VorbisModule destructor
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisModule::QueryInterface(REFIID iid, void ** ppvObject)
{
// IUnknown::QueryInterface
if(!ppvObject) return E_INVALIDARG;
*ppvObject=NULL;
if(!memcmp(&iid,&IID_IUnknown,16))
*ppvObject=(IUnknown*)this;
else if(!memcmp(&iid,&IID_ITSSModule,16))
*ppvObject=(ITSSModule*)this;
if(*ppvObject)
{
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
//---------------------------------------------------------------------------
ULONG __stdcall VorbisModule::AddRef()
{
return ++RefCount;
}
//---------------------------------------------------------------------------
ULONG __stdcall VorbisModule::Release()
{
if(RefCount == 1)
{
delete this;
return 0;
}
else
{
return --RefCount;
}
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisModule::GetModuleCopyright(LPWSTR buffer, unsigned long buflen)
{
// return module copyright information
strcpy_limit(buffer, L"OggVorbis Plug-in for TVP Sound System (C) 2000 W.Dee <dee@kikyou.info>",
buflen);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisModule::GetModuleDescription(LPWSTR buffer, unsigned long buflen )
{
// return module description
strcpy_limit(buffer, L"Ogg Vorbis (*.ogg) decoder", buflen);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisModule::GetSupportExts(unsigned long index, LPWSTR mediashortname,
LPWSTR buf, unsigned long buflen )
{
// return supported file extensios
if(index >= 1) return S_FALSE;
wcscpy(mediashortname, L"Ogg Stream Format");
strcpy_limit(buf, L".ogg", buflen);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisModule::GetMediaInfo(LPWSTR url, ITSSMediaBaseInfo ** info )
{
// return media information interface
return E_NOTIMPL; // not implemented
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisModule::GetMediaSupport(LPWSTR url )
{
// return media support interface
return E_NOTIMPL; // not implemented
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisModule::GetMediaInstance(LPWSTR url, IUnknown ** instance )
{
// create a new media interface
HRESULT hr;
// retrieve input stream interface
IStream *stream;
hr = StorageProvider->GetStreamForRead(url, (IUnknown**)&stream);
if(FAILED(hr)) return hr;
// create vorbis decoder
VorbisWaveDecoder * decoder = new VorbisWaveDecoder();
hr = decoder->SetStream(stream, url);
if(FAILED(hr))
{
// error; stream may not be a vorbis stream
delete decoder;
stream->Release();
return hr;
}
*instance = (IUnknown*)decoder; // return as IUnknown
stream->Release(); // release stream because the decoder already holds it
return S_OK;
}
//---------------------------------------------------------------------------
// VorbisWaveDecoder implementation #########################################
//---------------------------------------------------------------------------
VorbisWaveDecoder::VorbisWaveDecoder()
{
// VorbisWaveDecoder constructor
RefCount = 1;
InputFileInit = false;
InputStream = NULL;
CurrentSection = -1;
}
//---------------------------------------------------------------------------
VorbisWaveDecoder::~VorbisWaveDecoder()
{
// VorbisWaveDecoder destructor
if(InputFileInit)
{
ov_clear(&InputFile);
InputFileInit = false;
}
if(InputStream)
{
InputStream->Release();
InputStream = NULL;
}
}
//---------------------------------------------------------------------------
HRESULT VorbisWaveDecoder::QueryInterface(REFIID iid, void ** ppvObject)
{
// IUnknown::QueryInterface
if(!ppvObject) return E_INVALIDARG;
*ppvObject=NULL;
if(!memcmp(&iid,&IID_IUnknown,16))
*ppvObject=(IUnknown*)this;
else if(!memcmp(&iid,&IID_ITSSWaveDecoder,16))
*ppvObject=(ITSSWaveDecoder*)this;
if(*ppvObject)
{
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
//---------------------------------------------------------------------------
ULONG __stdcall VorbisWaveDecoder::AddRef()
{
return ++RefCount;
}
//---------------------------------------------------------------------------
ULONG __stdcall VorbisWaveDecoder::Release()
{
if(RefCount == 1)
{
delete this;
return 0;
}
else
{
return --RefCount;
}
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisWaveDecoder::GetFormat(TSSWaveFormat *format)
{
// return PCM format
if(!InputFileInit)
{
return E_FAIL;
}
*format = Format;
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisWaveDecoder::Render(void *buf, unsigned long bufsamplelen,
unsigned long *rendered, unsigned long *status)
{
// render output PCM
if(!InputFileInit) return E_FAIL; // InputFile is yet not inited
int pcmsize = FloatExtraction?4:2;
int res;
int pos = 0; // decoded PCM (in bytes)
int remain = bufsamplelen * Format.dwChannels * pcmsize; // remaining PCM (in bytes)
while(remain)
{
do
{
res = ov_read(&InputFile, ((char*)buf + pos), remain,
0, pcmsize, 1, &CurrentSection); // decode via ov_read
} while(res<0); // ov_read would return a negative number
// if the decoding is not ready
if(res==0) break;
pos += res;
remain -= res;
}
pos /= (Format.dwChannels * pcmsize); // convert to PCM position
if(status)
{
*status = ((unsigned int)pos < bufsamplelen)?0:1;
// *status will be 0 if the decoding is ended
}
if(rendered)
{
*rendered = pos; // return renderd PCM samples
}
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall VorbisWaveDecoder::SetPosition(unsigned __int64 samplepos)
{
// set PCM position (seek)
if(!InputFileInit) return E_FAIL;
if(0 != ov_pcm_seek(&InputFile, samplepos))
{
return E_FAIL;
}
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT VorbisWaveDecoder::SetStream(IStream *stream, LPWSTR url)
{
// set input stream
InputStream = stream;
InputStream->AddRef(); // add-ref
ov_callbacks callbacks = {read_func, seek_func, close_func, tell_func};
// callback functions
// open input stream via ov_open_callbacks
if(ov_open_callbacks(this, &InputFile, NULL, 0, callbacks) < 0)
{
// error!
InputStream->Release();
InputStream = NULL;
return E_FAIL;
}
InputFileInit = true;
// retrieve PCM information
vorbis_info *vi;
vi = ov_info(&InputFile, -1);
if(!vi)
{
return E_FAIL;
}
// set Format up
ZeroMemory(&Format, sizeof(Format));
Format.dwSamplesPerSec = vi->rate;
Format.dwChannels = vi->channels;
Format.dwBitsPerSample = FloatExtraction ? (0x10000 + 32) : 16;
Format.dwSeekable = 2;
__int64 pcmtotal = ov_pcm_total(&InputFile, -1); // PCM total samples
if(pcmtotal<0) pcmtotal = 0;
Format.ui64TotalSamples = pcmtotal;
double timetotal = ov_time_total(&InputFile, -1); // total time in sec.
if(timetotal<0) Format.dwTotalTime = 0; else Format.dwTotalTime = timetotal * 1000.0;
#ifndef NOT_HAVE_TP_STUB
// check old vorbis stream, which is generated by old encoder.
if(TVPAlive && !OldEncoderWarned)
{
vorbis_comment *vc = ov_comment(&InputFile, -1);
if(vc->vendor)
{
const char * vorbis_date = strstr(vc->vendor, "libVorbis I ");
if(vorbis_date)
{
int date = atoi(vorbis_date + 12);
if(date < WARN_OLD_VORBIS_DATE)
{
OldEncoderWarned = true;
TVPAddLog(TJS_W("wuvorbis: warning: The Vorbis stream \"") + ttstr(url) + TJS_W("\" seems to be generated by old Vorbis encoder [") +
ttstr(vc->vendor) + TJS_W("]. Check that your encoder is new enough, unless you prefer the old encoder. The old stream can still be decoded, but ") + ttstr(WARN_PREFER_ENCODER_VERSION) );
}
}
}
}
#ifndef _M_X64
if(Look_Replay_Gain)
{
for(int i = 0; i < InputFile.links; i++)
{
// for each stream
float gain = 1.0;
vorbis_comment *vc = ov_comment(&InputFile, i);
const char * track = vorbis_comment_query(vc, "replaygain_track_gain", 0);
const char * album = vorbis_comment_query(vc, "replaygain_album_gain", 0);
const char * sel = Use_Album_Gain ? ( album ? album : track ) : track;
if(sel)
{
float db = (float)atof(sel); // in db
gain *= (float)pow(10.0, db / 20.0); // convert db to multiplier
}
vorbis_info_set_global_gain(ov_info(&InputFile, i), gain);
}
}
#endif
#endif
return S_OK;
}
//---------------------------------------------------------------------------
size_t _cdecl VorbisWaveDecoder::read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
{
// read function (wrapper for IStream)
VorbisWaveDecoder * decoder = (VorbisWaveDecoder*)datasource;
if(!decoder->InputStream) return 0;
ULONG bytesread;
if(FAILED(decoder->InputStream->Read(ptr, (ULONG)(size * nmemb), &bytesread)))
{
return 0; // failed
}
return bytesread / size;
}
//---------------------------------------------------------------------------
int _cdecl VorbisWaveDecoder::seek_func(void *datasource, __int64 offset, int whence)
{
// seek function (wrapper for IStream)
VorbisWaveDecoder * decoder = (VorbisWaveDecoder*)datasource;
if(!decoder->InputStream) return -1;
LARGE_INTEGER newpos;
ULARGE_INTEGER result;
newpos.QuadPart = offset;
int seek_type = STREAM_SEEK_SET;
switch(whence)
{
case SEEK_SET:
seek_type = STREAM_SEEK_SET;
break;
case SEEK_CUR:
seek_type = STREAM_SEEK_CUR;
break;
case SEEK_END:
seek_type = STREAM_SEEK_END;
break;
}
if(FAILED(decoder->InputStream->Seek(newpos, seek_type, &result)))
{
return -1;
}
return 0;
}
//---------------------------------------------------------------------------
int _cdecl VorbisWaveDecoder::close_func(void *datasource)
{
// close function (wrapper for IStream)
VorbisWaveDecoder * decoder = (VorbisWaveDecoder*)datasource;
if(!decoder->InputStream) return EOF;
decoder->InputStream->Release();
decoder->InputStream = NULL;
return 0;
}
//---------------------------------------------------------------------------
long _cdecl VorbisWaveDecoder::tell_func(void *datasource)
{
// tell function (wrapper for IStream)
VorbisWaveDecoder * decoder = (VorbisWaveDecoder*)datasource;
if(!decoder->InputStream) return EOF;
LARGE_INTEGER newpos;
ULARGE_INTEGER result;
newpos.QuadPart = 0;
if(FAILED(decoder->InputStream->Seek(newpos, STREAM_SEEK_CUR, &result)))
{
return EOF;
}
return (long)(result.QuadPart); // ?? this will collapse int64 to int32
}
//---------------------------------------------------------------------------
// ##########################################################################
//---------------------------------------------------------------------------
extern "C"
{
int CPU_SSE = 0;
int CPU_MMX = 0;
int CPU_3DN = 0;
void scale_FLOOR1_fromdB_LOOKUP(float fac) ;
void scale_FROMdB2_LOOKUP(float fac) ;
}
//---------------------------------------------------------------------------
HRESULT _stdcall GetModuleInstance(ITSSModule **out, ITSSStorageProvider *provider,
IStream * config, HWND mainwin)
{
// GetModuleInstance function (exported)
StorageProvider = provider;
*out = new VorbisModule();
return S_OK;
}
//---------------------------------------------------------------------------
static void InternalSetCPUType(tjs_uint32 cputype)
{
CPU_SSE = cputype & TVP_CPU_HAS_SSE;
CPU_MMX = cputype & TVP_CPU_HAS_MMX;
CPU_3DN = cputype & TVP_CPU_HAS_3DN;
if(!CPU_MMX) CPU_3DN = 0; // 3D Now! uses MMX registers
if(CPU_SSE && CPU_3DN) CPU_SSE = 0; // Athlon XP; 3DNow! is faster than SSE on this decoder.
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) HRESULT _stdcall V2Link(iTVPFunctionExporter *exporter)
{
#ifndef NOT_HAVE_TP_STUB
// exported function, only called by kirikiri ver 2+
TVPAlive = true;
TVPInitImportStub(exporter);
// retrieve CPU information
tjs_uint32 cputype = TVPGetCPUType();
InternalSetCPUType(cputype);
ttstr debug_str(L"wuvorbis:");
#ifndef _M_X64
if(CPU_SSE) debug_str += L" SSE enabled."; else debug_str += L" SSE disabled.";
if(CPU_MMX) debug_str += L" MMX enabled."; else debug_str += L" MMX disabled.";
if(CPU_3DN) debug_str += L" 3DNow! enabled."; else debug_str += L" 3DNow! disabled.";
#else
debug_str += L" SSE/SSE2 enabled."; // x64 always enabled.
#endif
TVPAddImportantLog(debug_str);
tTJSVariant val;
if(TVPGetCommandLine(TJS_W("-vorbis_gain"), &val))
{
double db = (tTVReal)val;
double fac = pow(10.0, db / 20);
debug_str = TJS_W("wuvorbis: Setting global gain to ");
val = (tTVReal)db;
debug_str += ttstr(val);
debug_str += TJS_W("dB (");
val = (tTVReal)(fac * 100);
debug_str += ttstr(val);
debug_str += TJS_W("%)");
scale_FLOOR1_fromdB_LOOKUP(fac);
scale_FROMdB2_LOOKUP(fac);
TVPAddLog(debug_str);
}
if(TVPGetCommandLine(TJS_W("-vorbis_pcm_format"), &val))
{
ttstr sval(val);
if(sval == TJS_W("f32"))
{
FloatExtraction = true;
scale_FLOOR1_fromdB_LOOKUP(1.0f/32768.0f);
scale_FROMdB2_LOOKUP(1.0f/32768.0f);
TVPAddLog(TJS_W("wuvorbis: IEEE 32bit float output enabled."));
}
}
Look_Replay_Gain = true; // whether to look replay gain information
Use_Album_Gain = false; // whether to use album gain, otherwise use track gain
if(TVPGetCommandLine(TJS_W("-vorbis_rg"), &val))
{
ttstr sval(val);
if(sval == TJS_W("none") || sval == TJS_W("no"))
{
Look_Replay_Gain = false;
}
else if(sval == TJS_W("track"))
{
Use_Album_Gain = false;
}
else if(sval == TJS_W("album"))
{
Use_Album_Gain = true;
}
}
#endif
return S_OK;
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) HRESULT _stdcall V2Unlink()
{
#ifndef NOT_HAVE_TP_STUB
TVPUninitImportStub();
#endif
if(AllocCount != 0)
{
// Not all memory blockes are freed
// MessageBox(NULL, "Memory leak detected in wuvorbis.dll", "DEBUG", MB_OK);
}
return S_OK;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ##########################################################################
//---------------------------------------------------------------------------
// overriding memory allocation functions
struct alloc_record
{
void *org_ptr;
size_t org_size;
};
extern "C"
{
//---------------------------------------------------------------------------
void * dee_ogg_malloc(size_t bytes)
{
bytes += 16;
tjs_int align = 16;
void *ptr = malloc(bytes + align + sizeof(alloc_record));
if(!ptr) return NULL;
void *org_ptr = ptr;
DWORD *iptr =
reinterpret_cast<DWORD *>(&ptr);
*iptr += align + sizeof(alloc_record);
*iptr &= ~(DWORD)(align - 1);
(reinterpret_cast<alloc_record*>(ptr))[-1].org_ptr = org_ptr;
(reinterpret_cast<alloc_record*>(ptr))[-1].org_size = bytes;
InterlockedIncrement(&AllocCount);
return ptr;
}
//---------------------------------------------------------------------------
void * dee_ogg_calloc(size_t num, size_t size)
{
size += 16;
size_t sz = num * size;
void *ptr = dee_ogg_malloc(sz);
memset(ptr, 0, sz);
return ptr;
}
//---------------------------------------------------------------------------
void dee_ogg_free(void *ptr)
{
free((reinterpret_cast<alloc_record*>(ptr))[-1].org_ptr);
InterlockedDecrement(&AllocCount);
}
//---------------------------------------------------------------------------
void * dee_ogg_realloc(void *block, size_t bytes)
{
bytes += 16;
size_t org_size = (reinterpret_cast<alloc_record*>(block))[-1].org_size;
if(org_size == bytes) return block;
void * newptr = dee_ogg_malloc(bytes);
if(!newptr) return NULL;
memcpy(newptr, block, org_size);
dee_ogg_free(block);
return newptr;
}
//---------------------------------------------------------------------------
void * dee_ogg_alloca(size_t bytes)
{
bytes += 16;
tjs_int align = 16;
void *ptr = _alloca(bytes + align + sizeof(alloc_record));
if(!ptr) return NULL;
void *org_ptr = ptr;
DWORD *iptr =
reinterpret_cast<DWORD *>(&ptr);
*iptr += align + sizeof(alloc_record);
*iptr &= ~(DWORD)(align - 1);
(reinterpret_cast<alloc_record*>(ptr))[-1].org_ptr = org_ptr;
(reinterpret_cast<alloc_record*>(ptr))[-1].org_size = bytes;
return ptr;
}
//---------------------------------------------------------------------------
}
//---------------------------------------------------------------------------
// ##########################################################################
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) int _stdcall Query_sizeof_OggVorbis_File()
{
// returns sizeof(OggVorbis_File)
return sizeof(OggVorbis_File);
}
//---------------------------------------------------------------------------
// wuvorbis service functions
//---------------------------------------------------------------------------
extern "C" void SetCPUType(unsigned __int32 type)
{
InternalSetCPUType(type);
}
//---------------------------------------------------------------------------
extern tjs_uint32 TVPCheckCPU();
extern "C" unsigned __int32 TVPCPUType = 0;
extern "C" unsigned __int32 DetectCPU(void)
{
TVPCPUType = TVPCheckCPU();
#ifndef _M_X64
if(TVPCPUType & TVP_CPU_HAS_SSE)
{
// do SSE check
__try
{
_asm { xorps xmm0, xmm0 } // (SSE)
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
// exception had been ocured
// execution of 'xorps' is failed (XMM registers not available)
TVPCPUType &=~ TVP_CPU_HAS_SSE;
TVPCPUType &=~ TVP_CPU_HAS_SSE2;
}
}
#endif
return TVPCPUType;
}
//---------------------------------------------------------------------------
extern "C" void ScaleOutput(float scale)
{
scale_FLOOR1_fromdB_LOOKUP((float)scale) ;
scale_FROMdB2_LOOKUP((float)scale) ;
}
//---------------------------------------------------------------------------
#define IN_WUVORBISMAINUNIT
#include "decapi/include/wuvorbisfile.h"
#include "wuvorbisfile_stub.c"