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
|
/**************************************************************************
AVStream Filter-Centric Sample
Copyright (c) 1999 - 2001, Microsoft Corporation
File:
wave.cpp
Abstract:
Wave object implementation.
History:
Created 6/28/01
**************************************************************************/
#include "avssamp.h"
/**************************************************************************
PAGED CODE
**************************************************************************/
#ifdef ALLOC_PRAGMA
#pragma code_seg("PAGE")
#endif // ALLOC_PRAGMA
CWaveObject::
~CWaveObject (
)
/*++
Routine Description:
Destroy a wave object.
Arguments:
None
Return Value:
None
--*/
{
PAGED_CODE();
if (m_WaveData) {
ExFreePool (m_WaveData);
}
}
/*************************************************/
NTSTATUS
CWaveObject::
ParseForBlock (
IN HANDLE FileHandle,
IN ULONG BlockHeader,
IN OUT PLARGE_INTEGER BlockPosition,
OUT PULONG BlockSize
)
/*++
Routine Description:
Given that BlockPosition points to the offset of the start of a RIFF block,
continue parsing the specified file until a block with the header of
BlockHeader is found. Return the position of the block data and the size
of the block.
Arguments:
FileHandle -
Handle to the file to parse
BlockHeader -
The block header to scan for
BlockPosition -
INPUT : Points to the block header to start at
OUTPUT: If successful, points to the block data for the sought block
If unsuccessful, unchanged
BlockSize -
On output, if successful -- the size of the sought block will be
placed here
Return Value:
Success / Failure of the search
--*/
{
PAGED_CODE();
NTSTATUS Status;
ULONG FmtBlockSize = 0;
LARGE_INTEGER ReadPos = *BlockPosition;
IO_STATUS_BLOCK iosb;
while (1) {
ULONG BlockHeaderData [2];
Status = ZwReadFile (
FileHandle,
NULL,
NULL,
NULL,
&iosb,
BlockHeaderData,
sizeof (BlockHeaderData),
&ReadPos,
NULL
);
if (NT_SUCCESS (Status)) {
if (BlockHeaderData [0] == BlockHeader) {
FmtBlockSize = BlockHeaderData [1];
ReadPos.QuadPart += 0x8;
break;
} else {
//
// This isn't a format block. Just ignore it. All we
// care about is the format block and the PCM data.
//
ReadPos.QuadPart += BlockHeaderData [1] + 0x8;
}
} else {
break;
}
}
if (FmtBlockSize == 0) {
Status = STATUS_NOT_FOUND;
} else {
*BlockPosition = ReadPos;
*BlockSize = FmtBlockSize;
}
return Status;
}
/*************************************************/
NTSTATUS
CWaveObject::
ParseAndRead (
)
/*++
Routine Description:
Parse the wave file and read the data into an internally allocated
buffer. This prepares to synthesize audio data from the wave
object.
Arguments:
None
Return Value:
Success / Failure
If the wave is unrecognized, unparsable, or insufficient memory
exists to allocate the internal buffer, an error code will
be returned and the object will be incapable of synthesizing
audio data based on the wave.
--*/
{
PAGED_CODE();
IO_STATUS_BLOCK iosb;
UNICODE_STRING FileName;
OBJECT_ATTRIBUTES ObjectAttributes;
NTSTATUS Status;
HANDLE FileHandle = NULL;
FILE_OBJECT *FileObj;
RtlInitUnicodeString (&FileName, m_FileName);
InitializeObjectAttributes (
&ObjectAttributes,
&FileName,
(OBJ_CASE_INSENSITIVE |
OBJ_KERNEL_HANDLE),
NULL,
NULL
);
Status = ZwCreateFile (
&FileHandle,
GENERIC_READ | SYNCHRONIZE,
&ObjectAttributes,
&iosb,
0,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0
);
if (NT_SUCCESS (Status)) {
ULONG RiffWaveHeader [3];
//
// Read the header: RIFF size WAVE
//
Status = ZwReadFile (
FileHandle,
NULL,
NULL,
NULL,
&iosb,
RiffWaveHeader,
sizeof (RiffWaveHeader),
NULL,
NULL
);
//
// Ensure that this is a RIFF file and it's a WAVE.
//
if (NT_SUCCESS (Status)) {
if (RiffWaveHeader [0] != 'FFIR' ||
RiffWaveHeader [2] != 'EVAW') {
Status = STATUS_INVALID_PARAMETER;
}
}
}
//
// Find the wave format block and ensure it's WAVEFORMATEX and PCM
// data. Otherwise, this can't parse the wave.
//
LARGE_INTEGER ReadPos;
ReadPos.QuadPart = 0xc;
ULONG FmtBlockSize = 0;
if (NT_SUCCESS (Status)) {
Status = ParseForBlock (FileHandle, ' tmf', &ReadPos, &FmtBlockSize);
}
//
// If the format block was not found, the file cannot be parsed. If the
// format block is unrecognized, the file cannot be parsed.
//
if (FmtBlockSize >= sizeof (m_WaveFormat)) {
Status = STATUS_INVALID_PARAMETER;
}
if (NT_SUCCESS (Status)) {
Status = ZwReadFile (
FileHandle,
NULL,
NULL,
NULL,
&iosb,
&m_WaveFormat,
FmtBlockSize,
&ReadPos,
NULL
);
}
if (NT_SUCCESS (Status)) {
if (m_WaveFormat.wFormatTag != WAVE_FORMAT_PCM) {
Status = STATUS_INVALID_PARAMETER;
}
}
ReadPos.QuadPart += FmtBlockSize;
//
// Find the data block and read it in.
//
ULONG DataBlockSize;
if (NT_SUCCESS (Status)) {
Status = ParseForBlock (FileHandle, 'atad', &ReadPos, &DataBlockSize);
}
//
// Perform a slight validation.
//
if (NT_SUCCESS (Status) &&
(DataBlockSize == 0 ||
(DataBlockSize & (m_WaveFormat.nBlockAlign - 1)))) {
Status = STATUS_INVALID_PARAMETER;
}
//
// If we're okay so far, allocate memory for the wave data.
//
if (NT_SUCCESS (Status)) {
m_WaveData = reinterpret_cast <PUCHAR> (
ExAllocatePoolZero (NonPagedPool, DataBlockSize, AVSSMP_POOLTAG)
);
if (!m_WaveData) {
Status = STATUS_INSUFFICIENT_RESOURCES;
}
}
//
// Read the wave data in.
//
if (NT_SUCCESS (Status)) {
Status = ZwReadFile (
FileHandle,
NULL,
NULL,
NULL,
&iosb,
m_WaveData,
DataBlockSize,
&ReadPos,
NULL
);
m_WaveSize = DataBlockSize;
}
//
// If we failed, clean up.
//
if (!NT_SUCCESS (Status)) {
if (m_WaveData) {
ExFreePool (m_WaveData);
m_WaveData = NULL;
}
}
if (FileHandle) {
ZwClose (FileHandle);
}
return Status;
}
/*************************************************/
void
CWaveObject::
WriteRange (
OUT PKSDATARANGE_AUDIO DataRange
)
/*++
Routine Description:
Fill out the extended portion of the audio data range at DataRange. This
includes the channel, bps, and frequency fields.
Arguments:
DataRange -
The data range to fill out
Return Value:
None
--*/
{
PAGED_CODE();
DataRange -> MaximumChannels = m_WaveFormat.nChannels;
DataRange -> MinimumBitsPerSample =
DataRange -> MaximumBitsPerSample =
m_WaveFormat.wBitsPerSample;
DataRange -> MinimumSampleFrequency =
DataRange -> MaximumSampleFrequency =
m_WaveFormat.nSamplesPerSec;
}
/**************************************************************************
LOCKED CODE
**************************************************************************/
#ifdef ALLOC_PRAGMA
#pragma code_seg()
#endif // ALLOC_PRAGMA
void
CWaveObject::
SkipFixed (
IN LONGLONG TimeDelta
)
/*++
Routine Description:
Skip ahead a specific time delta within the wave.
Arguments:
TimeDelta -
The amount of time to skip ahead.
--*/
{
if (TimeDelta > 0) {
//
// Compute the number of bytes of audio data necessary to move the
// stream forward TimeDelta time. Remember that TimeDelta is in
// units of 100nS.
//
ULONG Samples = (ULONG)(
(m_WaveFormat.nSamplesPerSec * TimeDelta) / 10000000
);
ULONG Bytes = Samples * (m_WaveFormat.wBitsPerSample / 8) *
m_WaveFormat.nChannels;
m_WavePointer = (m_WavePointer + Bytes) % m_WaveSize;
m_SynthesisTime += TimeDelta;
}
}
ULONG
CWaveObject::
SynthesizeFixed (
IN LONGLONG TimeDelta,
IN PVOID Buffer,
IN ULONG BufferSize
)
/*++
Routine Description:
Copy wave data from our wave block in order to synthesize forward in time
TimeDelta (in 100nS units).
Arguments:
TimeDelta -
The amount of time to move the stream (in 100nS increments)
Buffer -
The buffer to synthesize into
BufferSize -
The size of the buffer
Return Value:
Number of bytes synthesized.
--*/
{
//
// If there is no time delta, return 0.
//
if (TimeDelta < 0)
return 0;
//
// Compute the number of bytes of audio data necessary to move the stream
// forward TimeDelta time. Remember that TimeDelta is in units of 100nS.
//
ULONG Samples = (ULONG)(
(m_WaveFormat.nSamplesPerSec * TimeDelta) / 10000000
);
ULONG Bytes = Samples * (m_WaveFormat.wBitsPerSample / 8) *
m_WaveFormat.nChannels;
//
// Now that we have a specified number of bytes, we determine how many
// to really copy based on the Size of the buffer.
//
if (Bytes > BufferSize) Bytes = BufferSize;
//
// Because the buffer is looping, this may multiple distinct copies. For
// large wave files, this may be two chunks. For small wave files, this
// may be MANY distinct chunks.
//
ULONG BytesRemaining = Bytes;
PUCHAR DataCopy = reinterpret_cast <PUCHAR> (Buffer);
while (BytesRemaining) {
ULONG ChunkCount = m_WaveSize - m_WavePointer;
if (ChunkCount > BytesRemaining) ChunkCount = BytesRemaining;
RtlCopyMemory (
DataCopy,
m_WaveData + m_WavePointer,
ChunkCount
);
m_WavePointer += ChunkCount;
if (m_WavePointer >= m_WaveSize) m_WavePointer -= m_WaveSize;
BytesRemaining -= ChunkCount;
DataCopy += ChunkCount;
}
//
// Consider that we have synthesized up to the specified time. If the
// buffer was not large enough to do this, we'll end up falling behind
// the synthesis time. This does not skip samples.
//
m_SynthesisTime += TimeDelta;
return Bytes;
}
ULONG
CWaveObject::
SynthesizeTo (
IN LONGLONG StreamTime,
IN PVOID Buffer,
IN ULONG BufferSize
)
/*++
Routine Description:
Copy wave data from our wave block in order to synthesize the stream
up to the specified stream time. If the buffers are not large enough,
this will fall behind on synthesis.
Arguments:
StreamTime -
The time to synthesize up to
Buffer -
The buffer to copy synthesized wave data into
BufferSize -
The size of the buffer
Return Value:
The number of bytes used.
--*/
{
LONGLONG TimeDelta = StreamTime - m_SynthesisTime;
return SynthesizeFixed (TimeDelta, Buffer, BufferSize);
}
|