-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwuopus.cpp
441 lines (396 loc) · 14.4 KB
/
wuopus.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
//---------------------------------------------------------------------------
// Ogg Opus plugin for TSS ( stands for TVP Sound System )
// This is released under TVP(Kirikiri)'s license.
// See details for TVP source distribution.
//---------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include "opusfile/include/opusfile.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
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch(reason){
case DLL_PROCESS_ATTACH: break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
case DLL_PROCESS_DETACH: break;
}
return TRUE;
}
//---------------------------------------------------------------------------
ITSSStorageProvider *StorageProvider = NULL;
//---------------------------------------------------------------------------
class OpusModule : public ITSSModule // module interface
{
ULONG RefCount; // reference count
public:
OpusModule();
virtual ~OpusModule();
public:
// IUnknown
STDMETHODIMP QueryInterface(REFIID iid, void ** ppvObject);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
// ITSSModule
STDMETHODIMP GetModuleCopyright(LPWSTR buffer, unsigned long buflen );
STDMETHODIMP GetModuleDescription(LPWSTR buffer, unsigned long buflen );
STDMETHODIMP GetSupportExts(unsigned long index, LPWSTR mediashortname, LPWSTR buf, unsigned long buflen );
STDMETHODIMP GetMediaInfo(LPWSTR url, ITSSMediaBaseInfo ** info );
STDMETHODIMP GetMediaSupport(LPWSTR url );
STDMETHODIMP GetMediaInstance(LPWSTR url, IUnknown ** instance );
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
class OpusWaveDecoder : public ITSSWaveDecoder // decoder interface
{
ULONG RefCount; // refernce count
OggOpusFile *InputFile; // OggVorbis_File instance
IStream *InputStream; // input stream
TSSWaveFormat Format; // output PCM format
public:
OpusWaveDecoder();
virtual ~OpusWaveDecoder();
public:
public:
// IUnkown
STDMETHODIMP QueryInterface(REFIID iid, void ** ppvObject);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
// ITSSWaveDecoder
STDMETHODIMP GetFormat(TSSWaveFormat *format);
STDMETHODIMP Render(void *buf, unsigned long bufsamplelen, unsigned long *rendered, unsigned long *status);
STDMETHODIMP SetPosition(unsigned __int64 samplepos);
// others
HRESULT SetStream(IStream *stream, LPWSTR url);
private:
static int read_func(void *_stream,unsigned char *_ptr,int _nbytes);
static int seek_func(void *_stream,opus_int64 _offset,int _whence);
static opus_int64 tell_func(void *_stream);
static int close_func(void *_stream);
};
//---------------------------------------------------------------------------
// OpusModule implementation ################################################
//---------------------------------------------------------------------------
OpusModule::OpusModule() : RefCount(1)
{
// VorbisModule constructor
}
//---------------------------------------------------------------------------
OpusModule::~OpusModule()
{
// VorbisModule destructor
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusModule::QueryInterface(REFIID iid, void ** ppvObject)
{
// IUnknown::QueryInterface
if(ppvObject == NULL) return E_INVALIDARG;
*ppvObject = NULL;
if(IsEqualIID(iid,IID_IUnknown)){ *ppvObject = static_cast<IUnknown *>(this); }
else if(IsEqualIID(iid,IID_ITSSModule)){ *ppvObject = static_cast<ITSSModule * >(this); }
if(*ppvObject != NULL){ AddRef(); return S_OK; }
return E_NOINTERFACE;
}
//---------------------------------------------------------------------------
STDMETHODIMP_(ULONG) OpusModule::AddRef()
{
return InterlockedIncrement(&RefCount);
}
//---------------------------------------------------------------------------
STDMETHODIMP_(ULONG) OpusModule::Release()
{
long Ref = InterlockedDecrement(&RefCount);
if(Ref <= 0){ if(Ref != 0){ InterlockedExchange(&RefCount,0); } delete this; return 0; }
return Ref;
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusModule::GetModuleCopyright(LPWSTR buffer, unsigned long buflen)
{
// return module copyright information
wcscpy_s(buffer,buflen,L"Opus Plug-in for TVP Sound System (C) 2014 Shiku Otomiya <[email protected]>");
return S_OK;
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusModule::GetModuleDescription(LPWSTR buffer, unsigned long buflen )
{
// return module description
wcscpy_s(buffer,buflen,L"Opus (*.opus) decoder");
return S_OK;
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusModule::GetSupportExts(unsigned long index, LPWSTR mediashortname, LPWSTR buf, unsigned long buflen )
{
// return supported file extensios
switch(index){
case 0: wcscpy(mediashortname,L"Opus Stream Format"); wcscpy_s(buf,buflen,L".opus"); break;
case 1: wcscpy(mediashortname,L"Ogg Audio Stream Format"); wcscpy_s(buf,buflen,L".oga"); break;
default: return S_FALSE;
}
return S_OK;
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusModule::GetMediaInfo(LPWSTR url, ITSSMediaBaseInfo ** info )
{
// return media information interface
return E_NOTIMPL; // not implemented
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusModule::GetMediaSupport(LPWSTR url )
{
// return media support interface
return E_NOTIMPL; // not implemented
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusModule::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
OpusWaveDecoder *decoder = new OpusWaveDecoder();
hr = decoder->SetStream(stream, url);
if(FAILED(hr))
{
// error; stream may not be a vorbis stream
delete decoder;
stream->Release();
return hr;
}
*instance = static_cast<IUnknown *>(decoder); // return as IUnknown
stream->Release(); // release stream because the decoder already holds it
return S_OK;
}
//---------------------------------------------------------------------------
// OpusWaveDecoder implementation ###########################################
//---------------------------------------------------------------------------
OpusWaveDecoder::OpusWaveDecoder() : RefCount(1), InputFile(NULL), InputStream(NULL)
{
// OpusWaveDecoder constructor
}
//---------------------------------------------------------------------------
OpusWaveDecoder::~OpusWaveDecoder()
{
// OpusWaveDecoder destructor
if(InputFile != NULL)
{
op_free(InputFile);
InputFile = NULL;
}
if(InputStream != NULL)
{
InputStream->Release();
InputStream = NULL;
}
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusWaveDecoder::QueryInterface(REFIID iid, void ** ppvObject)
{
// IUnknown::QueryInterface
if(ppvObject == NULL) return E_INVALIDARG;
*ppvObject = NULL;
if(IsEqualIID(iid,IID_IUnknown)){ *ppvObject = static_cast<IUnknown *>(this); }
else if(IsEqualIID(iid,IID_ITSSWaveDecoder)){ *ppvObject = static_cast<ITSSWaveDecoder * >(this); }
if(*ppvObject != NULL){ AddRef(); return S_OK; }
return E_NOINTERFACE;
}
//---------------------------------------------------------------------------
STDMETHODIMP_(ULONG) OpusWaveDecoder::AddRef()
{
return InterlockedIncrement(&RefCount);
}
//---------------------------------------------------------------------------
STDMETHODIMP_(ULONG) OpusWaveDecoder::Release()
{
long Ref = InterlockedDecrement(&RefCount);
if(Ref <= 0){ if(Ref != 0){ InterlockedExchange(&RefCount,0); } delete this; return 0; }
return Ref;
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusWaveDecoder::GetFormat(TSSWaveFormat *format)
{
// return PCM format
if(InputFile == NULL) return E_FAIL;
*format = Format;
return S_OK;
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusWaveDecoder::Render(void *buf, unsigned long bufsamplelen, unsigned long *rendered, unsigned long *status)
{
// render output PCM
if(InputFile == NULL) return E_FAIL; // InputFile is yet not inited
int res,pos = 0,remain = bufsamplelen * Format.dwChannels;
while(remain > 0){
if(FloatExtraction){
res = op_read_float(InputFile,(float *)buf + pos,remain,NULL);
// decode via ov_read (IEEE 32-bit float)
}
else{
res = op_read(InputFile,(opus_int16 *)buf + pos,remain,NULL);
// decode via ov_read (16bit integer)
}
if(res == 0) break;
res *= Format.dwChannels; pos += res; remain -= res;
}
pos /= Format.dwChannels;
if(status != NULL){
*status = ((unsigned int)pos < bufsamplelen) ? 0 : 1;
// *status will be 0 if the decoding is ended
}
if(rendered != NULL){
*rendered = pos;
// return renderd PCM samples
}
return S_OK;
}
//---------------------------------------------------------------------------
STDMETHODIMP OpusWaveDecoder::SetPosition(unsigned __int64 samplepos)
{
// set PCM position (seek)
if(InputFile == NULL) return E_FAIL;
if(op_pcm_seek(InputFile,samplepos) != 0){ return E_FAIL; }
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT OpusWaveDecoder::SetStream(IStream *stream, LPWSTR url)
{
// set input stream
InputStream = stream; InputStream->AddRef(); // add-ref
OpusFileCallbacks callbacks = {read_func, seek_func, tell_func, close_func};
// callback functions
// open input stream via op_open_callbacks
int err;
InputFile = op_open_callbacks(this,&callbacks,NULL,0,&err);
if(InputFile == NULL || err < 0)
{
// error!
InputStream->Release();
InputStream = NULL;
return E_FAIL;
}
// retrieve PCM information
const OpusHead *head = op_head(InputFile, -1);
if(head == NULL){ return E_FAIL; }
// set Format up
ZeroMemory(&Format, sizeof(Format));
Format.dwSamplesPerSec = 48000;
Format.dwChannels = head->channel_count;
Format.dwBitsPerSample = FloatExtraction ? (0x10000 + 32) : 16;
Format.dwSeekable = 2;
__int64 pcmtotal = op_pcm_total(InputFile, -1); // PCM total samples
if(pcmtotal < 0) pcmtotal = 0;
Format.ui64TotalSamples = pcmtotal;
Format.dwTotalTime = (unsigned long)(pcmtotal / (48000 / 1000));
return S_OK;
}
//---------------------------------------------------------------------------
int _cdecl OpusWaveDecoder::read_func(void *_stream,unsigned char *_ptr,int _nbytes)
{
// read function (wrapper for IStream)
OpusWaveDecoder *decoder = (OpusWaveDecoder *)_stream;
if(decoder->InputStream == NULL) return 0;
ULONG bytesread;
if(FAILED(decoder->InputStream->Read(_ptr, _nbytes, &bytesread))) return -1;
return bytesread;
}
//---------------------------------------------------------------------------
int OpusWaveDecoder::seek_func(void *_stream,opus_int64 _offset,int _whence)
{
// seek function (wrapper for IStream)
OpusWaveDecoder *decoder = (OpusWaveDecoder *)_stream;
if(decoder->InputStream == NULL) return -1;
LARGE_INTEGER newpos; ULARGE_INTEGER result; int seek_type; newpos.QuadPart = _offset;
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;
default: return -1;
}
if(FAILED(decoder->InputStream->Seek(newpos, seek_type, &result))) return -1;
return 0;
}
//---------------------------------------------------------------------------
opus_int64 OpusWaveDecoder::tell_func(void *_stream)
{
// tell function (wrapper for IStream)
OpusWaveDecoder *decoder = (OpusWaveDecoder *)_stream;
if(decoder->InputStream == NULL) return EOF;
LARGE_INTEGER newpos; ULARGE_INTEGER result; newpos.QuadPart = 0;
if(FAILED(decoder->InputStream->Seek(newpos, STREAM_SEEK_CUR, &result))) return EOF;
return result.QuadPart;
}
//---------------------------------------------------------------------------
int OpusWaveDecoder::close_func(void *_stream)
{
// close function (wrapper for IStream)
OpusWaveDecoder *decoder = (OpusWaveDecoder *)_stream;
if(decoder->InputStream == NULL) return EOF;
decoder->InputStream->Release();
decoder->InputStream = NULL;
return 0;
}
//---------------------------------------------------------------------------
// ##########################################################################
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) STDMETHODIMP GetModuleInstance(ITSSModule **out, ITSSStorageProvider *provider, IStream * config, HWND mainwin)
{
// GetModuleInstance function (exported)
StorageProvider = provider;
*out = new OpusModule();
return S_OK;
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) STDMETHODIMP V2Link(iTVPFunctionExporter *exporter)
{
#ifndef NOT_HAVE_TP_STUB
// exported function, only called by kirikiri ver 2+
TVPAlive = true;
TVPInitImportStub(exporter);
tTJSVariant val;
if(TVPGetCommandLine(TJS_W("-opus_pcm_format"), &val))
{
ttstr sval(val);
if(sval == TJS_W("f32"))
{
FloatExtraction = true;
TVPAddLog(TJS_W("wuopus: IEEE 32bit float output enabled."));
}
}
#endif
return S_OK;
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) STDMETHODIMP V2Unlink(void)
{
#ifndef NOT_HAVE_TP_STUB
TVPUninitImportStub();
#endif
return S_OK;
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) STDMETHODIMP_(const wchar_t *) GetOptionDesc(void)
{
if(GetACP() == 932) // 932 == Japan
{
return
L"Opusデコーダ:出力形式;Opus デコーダが出力する PCM 形式の設定です。|"
L"opus_pcm_format|select,*i16;16bit 整数 PCM,f32;32bit 浮動小数点数 PCM\n";
}
else
{
return
L"Opus decoder:Output format;Specify PCM format that the Opus decoder outputs.|"
L"opus_pcm_format|select,*i16;16bit integer PCM,f32;32bit floating-point PCM\n";
}
}