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
|
#include "stdafx.h"
#include "PcmReader.h"
#include <mfapi.h>
#include <Mferror.h>
#include "mfUtils.h"
namespace Whisper
{
__interface iSampleHandler
{
void copyChunk( PcmMonoChunk* pMono, const AudioBuffer& rsi, size_t sourceOffset, PcmStereoChunk* pStereo ) const;
void moveBufferData( AudioBuffer& rdi, size_t amount ) const;
void appendPcm( AudioBuffer& rdi, const float* rsi, size_t countFloats ) const;
void copyChunk( PcmMonoChunk* pMono, const AudioBuffer& rsi, size_t sourceOffset, size_t samples, PcmStereoChunk* pStereo ) const;
uint32_t readerChannelsCount() const;
};
}
namespace
{
using namespace Whisper;
__forceinline void copyMono( PcmMonoChunk* rdi, const AudioBuffer& rsi, size_t sourceOffset, size_t samples )
{
assert( sourceOffset + samples <= rsi.mono.size() );
memcpy( rdi->mono.data(), &rsi.mono[ sourceOffset ], samples * 4 );
if( samples < FFT_STEP )
memset( rdi->mono.data() + samples, 0, ( FFT_STEP - samples ) * 4 );
}
__forceinline void copyStereo( PcmStereoChunk* rdi, const AudioBuffer& rsi, size_t sourceOffset, size_t samples )
{
memcpy( rdi->stereo.data(), &rsi.stereo[ sourceOffset * 2 ], samples * 8 );
if( samples < FFT_STEP )
memset( rdi->stereo.data() + samples * 2, 0, ( FFT_STEP - samples ) * 8 );
}
struct HandlerMono : iSampleHandler
{
void appendPcm( AudioBuffer& rdi, const float* rsi, size_t countFloats ) const override
{
rdi.appendMono( rsi, countFloats );
}
void copyChunk( PcmMonoChunk* pMono, const AudioBuffer& rsi, size_t sourceOffset, PcmStereoChunk* pStereo ) const override final
{
copyMono( pMono, rsi, sourceOffset, FFT_STEP );
}
void copyChunk( PcmMonoChunk* pMono, const AudioBuffer& rsi, size_t sourceOffset, size_t samples, PcmStereoChunk* pStereo ) const override final
{
copyMono( pMono, rsi, sourceOffset, samples );
}
void moveBufferData( AudioBuffer& rdi, size_t amount ) const override final
{
const size_t len = rdi.mono.size();
assert( amount <= len );
if( amount < len )
{
const size_t block = len - amount;
memmove( rdi.mono.data(), rdi.mono.data() + amount, block * 4 );
rdi.mono.resize( block );
}
else
rdi.mono.clear();
}
uint32_t readerChannelsCount() const override { return 1; }
};
struct HandlerDownmixedStereo : HandlerMono
{
void appendPcm( AudioBuffer& rdi, const float* rsi, size_t countFloats ) const override final
{
rdi.appendDownmixedStereo( rsi, countFloats );
}
uint32_t readerChannelsCount() const override final { return 2; }
};
struct HandlerStereo : iSampleHandler
{
void appendPcm( AudioBuffer& rdi, const float* rsi, size_t countFloats ) const override final
{
rdi.appendStereo( rsi, countFloats );
}
void copyChunk( PcmMonoChunk* pMono, const AudioBuffer& rsi, size_t sourceOffset, PcmStereoChunk* pStereo ) const override final
{
copyMono( pMono, rsi, sourceOffset, FFT_STEP );
copyStereo( pStereo, rsi, sourceOffset, FFT_STEP );
}
void copyChunk( PcmMonoChunk* pMono, const AudioBuffer& rsi, size_t sourceOffset, size_t samples, PcmStereoChunk* pStereo ) const override final
{
copyMono( pMono, rsi, sourceOffset, samples );
copyStereo( pStereo, rsi, sourceOffset, samples );
}
void moveBufferData( AudioBuffer& rdi, size_t amount ) const override final
{
const size_t len = rdi.mono.size();
assert( amount <= len );
if( amount < len )
{
const size_t block = len - amount;
memmove( rdi.mono.data(), rdi.mono.data() + amount, block * 4 );
rdi.mono.resize( block );
memmove( rdi.stereo.data(), rdi.stereo.data() + amount * 2, block * 8 );
rdi.stereo.resize( block * 2 );
}
else
{
rdi.mono.clear();
rdi.stereo.clear();
}
}
uint32_t readerChannelsCount() const override final { return 2; }
};
static const HandlerMono s_mono;
static const HandlerDownmixedStereo s_downmix;
static const HandlerStereo s_stereo;
__forceinline __m128i load( const GUID& guid )
{
return _mm_loadu_si128( ( const __m128i* )( &guid ) );
}
// Find audio decoder MFT, query MF_MT_SUBTYPE attribute of the current input media type of that MFT
HRESULT getDecoderInputSubtype( IMFSourceReader* reader, __m128i& rdi )
{
store16( &rdi, _mm_setzero_si128() );
CComPtr<IMFSourceReaderEx> readerEx;
CHECK( reader->QueryInterface( &readerEx ) );
constexpr uint32_t stream = MF_SOURCE_READER_FIRST_AUDIO_STREAM;
const __m128i decGuid = load( MFT_CATEGORY_AUDIO_DECODER );
alignas( 16 ) GUID category;
for( DWORD i = 0; true; i++ )
{
CComPtr<IMFTransform> mft;
HRESULT hr = readerEx->GetTransformForStream( stream, i, &category, &mft );
if( hr == MF_E_INVALIDINDEX )
{
// This happens for *.wav input files
// They don't have any MFT_CATEGORY_AUDIO_DECODER MFTs in the source reader, and it's not an error
return S_FALSE;
}
if( FAILED( hr ) )
return hr;
const __m128i cat = _mm_load_si128( ( const __m128i* ) & category );
if( !vectorEqual( decGuid, cat ) )
continue;
CComPtr<IMFMediaType> mt;
CHECK( mft->GetInputCurrentType( 0, &mt ) );
CHECK( mt->GetGUID( MF_MT_SUBTYPE, (GUID*)&rdi ) );
return S_OK;
}
}
// S_OK when the reader has an MP3 decoder for the first audio stream, S_FALSE otherwise
HRESULT isMp3Decoder( IMFSourceReader* reader )
{
__m128i subtype;
CHECK( getDecoderInputSubtype( reader, subtype ) );
const bool res = vectorEqual( subtype, load( MFAudioFormat_MP3 ) );
return res ? S_OK : S_FALSE;
}
// Workaround for a Microsoft's bug in Media Foundation MP3 decoder: https://github.com/Const-me/Whisper/issues/4
// Media Foundation is reporting incorrect media duration = 12.54. Windows Media Player does the same.
// Winamp and Media Player Classic are reporting 12:35, VLC reports 12:36.
HRESULT getPreciseDuration( IMFSourceReader* reader, size_t& length, bool mono, const iAudioReader* iar )
{
size_t samples = 0;
// Decode the complete stream, counting samples
while( true )
{
DWORD dwFlags = 0;
CComPtr<IMFSample> sample;
// Read the next sample
HRESULT hr = reader->ReadSample( (DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, nullptr, &dwFlags, nullptr, &sample );
if( FAILED( hr ) )
{
logErrorHr( hr, u8"IMFSourceReader.ReadSample" );
return hr;
}
if( dwFlags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED )
{
// logError( u8"Media type changes ain’t supported by the library." );
// return E_UNEXPECTED;
// This happens for some video files at the very start of the reading, with Dolby AC3 audio track.
// Instead of failing the transcribe process, verify the important attributes (FP32 samples, sample rate, count of channels) haven’t changed.
CHECK( validateCurrentMediaType( reader, mono ? 1 : 2 ) );
}
if( dwFlags & MF_SOURCE_READERF_ENDOFSTREAM )
break;
if( !sample )
{
// printf( "No sample\n" );
continue;
}
// Get a pointer to the audio data in the sample.
CComPtr<IMFMediaBuffer> buffer;
hr = sample->ConvertToContiguousBuffer( &buffer );
if( FAILED( hr ) )
return hr;
const float* pAudioData = nullptr;
DWORD cbBuffer;
hr = buffer->Lock( (BYTE**)&pAudioData, nullptr, &cbBuffer );
if( FAILED( hr ) )
return hr;
assert( 0 == ( cbBuffer % sizeof( float ) ) );
const size_t countFloats = cbBuffer / sizeof( float );
if( mono )
samples += countFloats;
else
{
assert( 0 == countFloats % 2 );
samples += countFloats / 2;
}
// Unlock the buffer
hr = buffer->Unlock();
if( FAILED( hr ) )
return hr;
}
// Rewind the stream to beginning
PROPVARIANT pv;
PropVariantInit( &pv );
pv.vt = VT_I8;
pv.hVal.QuadPart = 0;
CHECK( reader->SetCurrentPosition( GUID_NULL, pv ) );
// Make the output value
length = samples / FFT_STEP;
// Store the actual samples count in the reader
// This way the iAudioReader.getDuration() API returns correct value to the user
setPreciseSamplesCount( iar, samples );
return S_OK;
}
HRESULT getDuration( IMFSourceReader* reader, size_t& length, bool mono, const iAudioReader* iar )
{
HRESULT hr = isMp3Decoder( reader );
if( SUCCEEDED( hr ) )
{
if( S_OK == hr )
{
return getPreciseDuration( reader, length, mono, iar );
}
}
else
logWarningHr( hr, u8"isMp3Decoder" );
// Find out the length
int64_t durationTicks;
CHECK( getStreamDuration( reader, durationTicks ) );
// Convert length to chunks
// Seconds = Ticks / 10^7
// Samples = Seconds * SAMPLE_RATE = Ticks * SAMPLE_RATE / 10^7
// Chunks = Samples / FFT_STEP = Ticks * SAMPLE_RATE / ( FFT_STEP * 10^7 ), and we want that integer rounded down
constexpr __int64 mul = SAMPLE_RATE;
constexpr __int64 div = (__int64)FFT_STEP * 10'000'000;
length = (size_t)MFllMulDiv( durationTicks, mul, div, 0 );
return S_OK;
}
}
PcmReader::PcmReader( const iAudioReader* iar )
{
if( nullptr == iar )
throw E_POINTER;
check( iar->getReader( &reader ) );
const bool stereo = iar->requestedStereo() == S_OK;
// Set up media type, and figure out sample handler
check( reader->SetStreamSelection( MF_SOURCE_READER_ALL_STREAMS, FALSE ) );
check( reader->SetStreamSelection( MF_SOURCE_READER_FIRST_AUDIO_STREAM, TRUE ) );
CComPtr<IMFMediaType> mtNative;
check( reader->GetNativeMediaType( MF_SOURCE_READER_FIRST_AUDIO_STREAM, MF_SOURCE_READER_CURRENT_TYPE_INDEX, &mtNative ) );
UINT32 numChannels;
check( mtNative->GetUINT32( MF_MT_AUDIO_NUM_CHANNELS, &numChannels ) );
const bool sourceMono = numChannels < 2;
if( sourceMono )
sampleHandler = &s_mono;
else if( !stereo )
sampleHandler = &s_downmix;
else
{
sampleHandler = &s_stereo;
m_stereoOutput = true;
}
CComPtr<IMFMediaType> mt;
check( createMediaType( !sourceMono, &mt ) );
check( reader->SetCurrentMediaType( MF_SOURCE_READER_FIRST_AUDIO_STREAM, nullptr, mt ) );
// Find out the length.
// Sadly, broken Microsoft's MP3 decoder MFT made this much harder than necessary:
// https://github.com/Const-me/Whisper/issues/4
check( getDuration( reader, m_length, sourceMono, iar ) );
}
HRESULT PcmReader::readNextSample()
{
const size_t off = bufferReadOffset;
const size_t availableSamples = pcm.mono.size() - off;
// If needed, move the remaining PCM data to the start of these vectors
if( availableSamples > 0 )
{
if( 0 != off )
sampleHandler->moveBufferData( pcm, off );
}
else
pcm.clear();
bufferReadOffset = 0;
while( true )
{
DWORD dwFlags = 0;
CComPtr<IMFSample> sample;
// Read the next sample
HRESULT hr = reader->ReadSample( (DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, nullptr, &dwFlags, nullptr, &sample );
if( FAILED( hr ) )
{
logErrorHr( hr, u8"IMFSourceReader.ReadSample" );
return hr;
}
if( dwFlags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED )
{
// logError( u8"Media type changes ain’t supported by the library." );
// return E_UNEXPECTED;
// This happens for some video files at the very start of the reading, with Dolby AC3 audio track.
// Instead of failing the transcribe process, verify the important attributes (FP32 samples, sample rate, count of channels) haven’t changed.
CHECK( validateCurrentMediaType( reader, sampleHandler->readerChannelsCount() ) );
}
if( dwFlags & MF_SOURCE_READERF_ENDOFSTREAM )
return E_EOF;
if( !sample )
{
// printf( "No sample\n" );
continue;
}
// Get a pointer to the audio data in the sample.
CComPtr<IMFMediaBuffer> buffer;
hr = sample->ConvertToContiguousBuffer( &buffer );
if( FAILED( hr ) )
return hr;
const float* pAudioData = nullptr;
DWORD cbBuffer;
hr = buffer->Lock( (BYTE**)&pAudioData, nullptr, &cbBuffer );
if( FAILED( hr ) )
return hr;
try
{
assert( 0 == ( cbBuffer % sizeof( float ) ) );
const size_t countFloats = cbBuffer / sizeof( float );
sampleHandler->appendPcm( pcm, pAudioData, countFloats );
}
catch( const std::bad_alloc& )
{
buffer->Unlock();
return E_OUTOFMEMORY;
}
// Unlock the buffer
hr = buffer->Unlock();
if( FAILED( hr ) )
return hr;
return S_OK;
}
}
HRESULT PcmReader::readChunk( PcmMonoChunk& mono, PcmStereoChunk* stereo )
{
while( true )
{
const size_t off = bufferReadOffset;
const size_t availableSamples = pcm.mono.size() - off;
if( availableSamples >= FFT_STEP )
{
// We have enough data in the buffer
sampleHandler->copyChunk( &mono, pcm, off, stereo );
bufferReadOffset = off + FFT_STEP;
return S_OK;
}
if( !m_readerEndOfFile )
{
// We don't have enough data, but the stream has not ended yet, can load moar samples from the reader
HRESULT hr = readNextSample();
if( SUCCEEDED( hr ) )
continue;
if( hr != E_EOF )
return hr;
m_readerEndOfFile = true;
}
if( availableSamples > 0 )
{
// We have reached the end of stream of the reader, but the buffer still has a few samples.
// Return the final incomplete chunk padded with zeros
sampleHandler->copyChunk( &mono, pcm, off, availableSamples, stereo );
bufferReadOffset = off + availableSamples;
return S_OK;
}
return E_EOF;
}
}
|