-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grayscale.cpp
1506 lines (1196 loc) · 39.1 KB
/
Grayscale.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
#include "pch.h"
#include <wrl\module.h>
#include "Grayscale.h"
#include "VideoBufferLock.h"
#pragma comment(lib, "d2d1")
using namespace Microsoft::WRL;
ActivatableClass(CGrayscale);
/*
This sample implements a video effect as a Media Foundation transform (MFT).
NOTES ON THE MFT IMPLEMENTATION
1. The MFT has fixed streams: One input stream and one output stream.
2. The MFT supports the following formats: UYVY, YUY2, NV12.
3. If the MFT is holding an input sample, SetInputType and SetOutputType both fail.
4. The input and output types must be identical.
5. If both types are set, no type can be set until the current type is cleared.
6. Preferred input types:
(a) If the output type is set, that's the preferred type.
(b) Otherwise, the preferred types are partial types, constructed from the
list of supported subtypes.
7. Preferred output types: As above.
8. Streaming:
The private BeingStreaming() method is called in response to the
MFT_MESSAGE_NOTIFY_BEGIN_STREAMING message.
If the client does not send MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, the MFT calls
BeginStreaming inside the first call to ProcessInput or ProcessOutput.
This is a good approach for allocating resources that your MFT requires for
streaming.
9. The configuration attributes are applied in the BeginStreaming method. If the
client changes the attributes during streaming, the change is ignored until
streaming is stopped (either by changing the media types or by sending the
MFT_MESSAGE_NOTIFY_END_STREAMING message) and then restarted.
*/
// Video FOURCC codes.
const DWORD FOURCC_YUY2 = '2YUY';
const DWORD FOURCC_UYVY = 'YVYU';
const DWORD FOURCC_NV12 = '21VN';
// Static array of media types (preferred and accepted).
const GUID g_MediaSubtypes[] =
{
MFVideoFormat_NV12,
MFVideoFormat_YUY2,
MFVideoFormat_UYVY
};
DWORD GetImageSize(DWORD fcc, UINT32 width, UINT32 height);
LONG GetDefaultStride(IMFMediaType *pType);
template <typename T>
inline T clamp(const T &val, const T &minVal, const T &maxVal)
{
return (val < minVal ? minVal : (val > maxVal ? maxVal : val));
}
//-------------------------------------------------------------------
// Functions to convert a YUV images to grayscale.
//
// In all cases, the same transformation is applied to the 8-bit
// chroma values, but the pixel layout in memory differs.
//
// The image conversion functions take the following parameters:
//
// mat Transfomation matrix for chroma values.
// rcDest Destination rectangle.
// pDest Pointer to the destination buffer.
// lDestStride Stride of the destination buffer, in bytes.
// pSrc Pointer to the source buffer.
// lSrcStride Stride of the source buffer, in bytes.
// dwWidthInPixels Frame width in pixels.
// dwHeightInPixels Frame height, in pixels.
//-------------------------------------------------------------------
// Convert UYVY image.
void TransformImage_UYVY(
const D2D_RECT_U &rcDest,
_Inout_updates_(_Inexpressible_(lDestStride * dwHeightInPixels)) BYTE *pDest,
_In_ LONG lDestStride,
_In_reads_(_Inexpressible_(lSrcStride * dwHeightInPixels)) const BYTE *pSrc,
_In_ LONG lSrcStride,
_In_ DWORD dwWidthInPixels,
_In_ DWORD dwHeightInPixels)
{
DWORD y = 0;
// Round down to the even value.
const UINT32 left = rcDest.left & ~(1);
const UINT32 right = rcDest.right & ~(1);
const DWORD y0 = min(rcDest.bottom, dwHeightInPixels);
// Lines above the destination rectangle.
for ( ; y < rcDest.top; y++)
{
CopyMemory(pDest, pSrc, dwWidthInPixels * 2);
pSrc += lSrcStride;
pDest += lDestStride;
}
// Lines within the destination rectangle.
for ( ; y < y0; y++)
{
const WORD *pSrc_Pixel = reinterpret_cast<const WORD*>(pSrc);
WORD *pDest_Pixel = reinterpret_cast<WORD*>(pDest);
CopyMemory(pDest, pSrc, left * 2);
for (DWORD x = left; (x + 1) < right; x += 2)
{
// Byte order is Y0 U0 Y1 V0
// Each WORD is a byte pair (Y, U/V)
// Windows is little-endian so the order appears reversed.
DWORD tmp = *reinterpret_cast<const DWORD*>(&pSrc_Pixel[x]);
*reinterpret_cast<DWORD*>(&pDest_Pixel[x]) = (tmp & 0xFF00FF00) | 0x00800080;
}
CopyMemory(pDest + (right * 2), pSrc + (right * 2), (dwWidthInPixels - right) * 2);
pDest += lDestStride;
pSrc += lSrcStride;
}
// Lines below the destination rectangle.
for ( ; y < dwHeightInPixels; y++)
{
CopyMemory(pDest, pSrc, dwWidthInPixels * 2);
pSrc += lSrcStride;
pDest += lDestStride;
}
}
// Convert YUY2 image.
void TransformImage_YUY2(
const D2D_RECT_U &rcDest,
_Inout_updates_(_Inexpressible_(lDestStride * dwHeightInPixels)) BYTE *pDest,
_In_ LONG lDestStride,
_In_reads_(_Inexpressible_(lSrcStride * dwHeightInPixels)) const BYTE *pSrc,
_In_ LONG lSrcStride,
_In_ DWORD dwWidthInPixels,
_In_ DWORD dwHeightInPixels)
{
DWORD y = 0;
// Round down to the even value.
const UINT32 left = rcDest.left & ~(1);
const UINT32 right = rcDest.right & ~(1);
const DWORD y0 = min(rcDest.bottom, dwHeightInPixels);
// Lines above the destination rectangle.
for ( ; y < rcDest.top; y++)
{
CopyMemory(pDest, pSrc, dwWidthInPixels * 2);
pSrc += lSrcStride;
pDest += lDestStride;
}
// Lines within the destination rectangle.
for ( ; y < y0; y++)
{
const WORD *pSrc_Pixel = reinterpret_cast<const WORD*>(pSrc);
WORD *pDest_Pixel = reinterpret_cast<WORD*>(pDest);
CopyMemory(pDest, pSrc, left * 2);
for (DWORD x = left; (x + 1) < right; x += 2)
{
// Byte order is Y0 U0 Y1 V0
// Each WORD is a byte pair (Y, U/V)
// Windows is little-endian so the order appears reversed.
DWORD tmp = *reinterpret_cast<const DWORD*>(&pSrc_Pixel[x]);
*reinterpret_cast<DWORD*>(&pDest_Pixel[x]) = (tmp & 0x00FF00FF) | 0x80008000;
}
CopyMemory(pDest + (right * 2), pSrc + (right * 2), (dwWidthInPixels - right) * 2);
pDest += lDestStride;
pSrc += lSrcStride;
}
// Lines below the destination rectangle.
for ( ; y < dwHeightInPixels; y++)
{
CopyMemory(pDest, pSrc, dwWidthInPixels * 2);
pSrc += lSrcStride;
pDest += lDestStride;
}
}
// Convert NV12 image
void TransformImage_NV12(
const D2D_RECT_U &rcDest,
_Inout_updates_(_Inexpressible_(2 * lDestStride * dwHeightInPixels)) BYTE *pDest,
_In_ LONG lDestStride,
_In_reads_(_Inexpressible_(2 * lSrcStride * dwHeightInPixels)) const BYTE *pSrc,
_In_ LONG lSrcStride,
_In_ DWORD dwWidthInPixels,
_In_ DWORD dwHeightInPixels)
{
// NV12 is planar: Y plane, followed by packed U-V plane.
// Y plane
for (DWORD y = 0; y < dwHeightInPixels; y++)
{
CopyMemory(pDest, pSrc, dwWidthInPixels);
pDest += lDestStride;
pSrc += lSrcStride;
}
// U-V plane
// NOTE: The U-V plane has 1/2 the number of lines as the Y plane.
// Lines above the destination rectangle.
DWORD y = 0;
const DWORD y0 = min(rcDest.bottom, dwHeightInPixels);
for ( ; y < rcDest.top/2; y++)
{
CopyMemory(pDest, pSrc, dwWidthInPixels);
pSrc += lSrcStride;
pDest += lDestStride;
}
// Lines within the destination rectangle.
for ( ; y < y0/2; y++)
{
CopyMemory(pDest, pSrc, rcDest.left);
FillMemory(pDest + rcDest.left, rcDest.right - rcDest.left, 128);
CopyMemory(pDest + rcDest.right, pSrc + rcDest.right, dwWidthInPixels - rcDest.right);
pDest += lDestStride;
pSrc += lSrcStride;
}
// Lines below the destination rectangle.
for ( ; y < dwHeightInPixels/2; y++)
{
CopyMemory(pDest, pSrc, dwWidthInPixels);
pSrc += lSrcStride;
pDest += lDestStride;
}
}
CGrayscale::CGrayscale()
: m_pTransformFn(nullptr)
, m_imageWidthInPixels(0)
, m_imageHeightInPixels(0)
, m_cbImageSize(0)
, m_rcDest(D2D1::RectU())
, m_fStreamingInitialized(false)
{
}
CGrayscale::~CGrayscale()
{
}
// Initialize the instance.
STDMETHODIMP CGrayscale::RuntimeClassInitialize()
{
// Create the attribute store.
return MFCreateAttributes(&m_spAttributes, 3);
}
// IMediaExtension methods
//-------------------------------------------------------------------
// SetProperties
// Sets the configuration of the effect
//-------------------------------------------------------------------
HRESULT CGrayscale::SetProperties(ABI::Windows::Foundation::Collections::IPropertySet *pConfiguration)
{
return S_OK;
}
// IMFTransform methods. Refer to the Media Foundation SDK documentation for details.
//-------------------------------------------------------------------
// GetStreamLimits
// Returns the minimum and maximum number of streams.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetStreamLimits(
DWORD *pdwInputMinimum,
DWORD *pdwInputMaximum,
DWORD *pdwOutputMinimum,
DWORD *pdwOutputMaximum
)
{
if ((pdwInputMinimum == nullptr) ||
(pdwInputMaximum == nullptr) ||
(pdwOutputMinimum == nullptr) ||
(pdwOutputMaximum == nullptr))
{
return E_POINTER;
}
// This MFT has a fixed number of streams.
*pdwInputMinimum = 1;
*pdwInputMaximum = 1;
*pdwOutputMinimum = 1;
*pdwOutputMaximum = 1;
return S_OK;
}
//-------------------------------------------------------------------
// GetStreamCount
// Returns the actual number of streams.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetStreamCount(
DWORD *pcInputStreams,
DWORD *pcOutputStreams
)
{
if ((pcInputStreams == nullptr) || (pcOutputStreams == nullptr))
{
return E_POINTER;
}
// This MFT has a fixed number of streams.
*pcInputStreams = 1;
*pcOutputStreams = 1;
return S_OK;
}
//-------------------------------------------------------------------
// GetStreamIDs
// Returns stream IDs for the input and output streams.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetStreamIDs(
DWORD dwInputIDArraySize,
DWORD *pdwInputIDs,
DWORD dwOutputIDArraySize,
DWORD *pdwOutputIDs
)
{
// It is not required to implement this method if the MFT has a fixed number of
// streams AND the stream IDs are numbered sequentially from zero (that is, the
// stream IDs match the stream indexes).
// In that case, it is OK to return E_NOTIMPL.
return E_NOTIMPL;
}
//-------------------------------------------------------------------
// GetInputStreamInfo
// Returns information about an input stream.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetInputStreamInfo(
DWORD dwInputStreamID,
MFT_INPUT_STREAM_INFO * pStreamInfo
)
{
if (pStreamInfo == nullptr)
{
return E_POINTER;
}
AutoLock lock(m_critSec);
if (!IsValidInputStream(dwInputStreamID))
{
return MF_E_INVALIDSTREAMNUMBER;
}
// NOTE: This method should succeed even when there is no media type on the
// stream. If there is no media type, we only need to fill in the dwFlags
// member of MFT_INPUT_STREAM_INFO. The other members depend on having a
// a valid media type.
pStreamInfo->hnsMaxLatency = 0;
pStreamInfo->dwFlags = MFT_INPUT_STREAM_WHOLE_SAMPLES | MFT_INPUT_STREAM_SINGLE_SAMPLE_PER_BUFFER;
if (m_spInputType == nullptr)
{
pStreamInfo->cbSize = 0;
}
else
{
pStreamInfo->cbSize = m_cbImageSize;
}
pStreamInfo->cbMaxLookahead = 0;
pStreamInfo->cbAlignment = 0;
return S_OK;
}
//-------------------------------------------------------------------
// GetOutputStreamInfo
// Returns information about an output stream.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetOutputStreamInfo(
DWORD dwOutputStreamID,
MFT_OUTPUT_STREAM_INFO * pStreamInfo
)
{
if (pStreamInfo == nullptr)
{
return E_POINTER;
}
AutoLock lock(m_critSec);
if (!IsValidOutputStream(dwOutputStreamID))
{
return MF_E_INVALIDSTREAMNUMBER;
}
// NOTE: This method should succeed even when there is no media type on the
// stream. If there is no media type, we only need to fill in the dwFlags
// member of MFT_OUTPUT_STREAM_INFO. The other members depend on having a
// a valid media type.
pStreamInfo->dwFlags =
MFT_OUTPUT_STREAM_WHOLE_SAMPLES |
MFT_OUTPUT_STREAM_SINGLE_SAMPLE_PER_BUFFER |
MFT_OUTPUT_STREAM_FIXED_SAMPLE_SIZE ;
if (m_spOutputType == nullptr)
{
pStreamInfo->cbSize = 0;
}
else
{
pStreamInfo->cbSize = m_cbImageSize;
}
pStreamInfo->cbAlignment = 0;
return S_OK;
}
//-------------------------------------------------------------------
// GetAttributes
// Returns the attributes for the MFT.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetAttributes(IMFAttributes **ppAttributes)
{
if (ppAttributes == nullptr)
{
return E_POINTER;
}
AutoLock lock(m_critSec);
*ppAttributes = m_spAttributes.Get();
(*ppAttributes)->AddRef();
return S_OK;
}
//-------------------------------------------------------------------
// GetInputStreamAttributes
// Returns stream-level attributes for an input stream.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetInputStreamAttributes(
DWORD dwInputStreamID,
IMFAttributes **ppAttributes
)
{
// This MFT does not support any stream-level attributes, so the method is not implemented.
return E_NOTIMPL;
}
//-------------------------------------------------------------------
// GetOutputStreamAttributes
// Returns stream-level attributes for an output stream.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetOutputStreamAttributes(
DWORD dwOutputStreamID,
IMFAttributes **ppAttributes
)
{
// This MFT does not support any stream-level attributes, so the method is not implemented.
return E_NOTIMPL;
}
//-------------------------------------------------------------------
// DeleteInputStream
//-------------------------------------------------------------------
HRESULT CGrayscale::DeleteInputStream(DWORD dwStreamID)
{
// This MFT has a fixed number of input streams, so the method is not supported.
return E_NOTIMPL;
}
//-------------------------------------------------------------------
// AddInputStreams
//-------------------------------------------------------------------
HRESULT CGrayscale::AddInputStreams(
DWORD cStreams,
DWORD *adwStreamIDs
)
{
// This MFT has a fixed number of output streams, so the method is not supported.
return E_NOTIMPL;
}
//-------------------------------------------------------------------
// GetInputAvailableType
// Returns a preferred input type.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetInputAvailableType(
DWORD dwInputStreamID,
DWORD dwTypeIndex, // 0-based
IMFMediaType **ppType
)
{
HRESULT hr = S_OK;
try
{
if (ppType == nullptr)
{
throw ref new InvalidArgumentException();
}
AutoLock lock(m_critSec);
if (!IsValidInputStream(dwInputStreamID))
{
ThrowException(MF_E_INVALIDSTREAMNUMBER);
}
// If the output type is set, return that type as our preferred input type.
if (m_spOutputType == nullptr)
{
// The output type is not set. Create a partial media type.
*ppType = OnGetPartialType(dwTypeIndex).Detach();
}
else if (dwTypeIndex > 0)
{
return MF_E_NO_MORE_TYPES;
}
else
{
*ppType = m_spOutputType.Get();
(*ppType)->AddRef();
}
}
catch(Exception ^exc)
{
hr = exc->HResult;
}
return hr;
}
//-------------------------------------------------------------------
// GetOutputAvailableType
// Returns a preferred output type.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetOutputAvailableType(
DWORD dwOutputStreamID,
DWORD dwTypeIndex, // 0-based
IMFMediaType **ppType
)
{
HRESULT hr = S_OK;
try
{
if (ppType == nullptr)
{
throw ref new InvalidArgumentException();
}
AutoLock lock(m_critSec);
if (!IsValidOutputStream(dwOutputStreamID))
{
return MF_E_INVALIDSTREAMNUMBER;
}
if (m_spInputType == nullptr)
{
// The input type is not set. Create a partial media type.
*ppType = OnGetPartialType(dwTypeIndex).Detach();
}
else if (dwTypeIndex > 0)
{
return MF_E_NO_MORE_TYPES;
}
else
{
*ppType = m_spInputType.Get();
(*ppType)->AddRef();
}
}
catch(Exception ^exc)
{
hr = exc->HResult;
}
return hr;
}
//-------------------------------------------------------------------
// SetInputType
//-------------------------------------------------------------------
HRESULT CGrayscale::SetInputType(
DWORD dwInputStreamID,
IMFMediaType *pType, // Can be nullptr to clear the input type.
DWORD dwFlags
)
{
HRESULT hr = S_OK;
try
{
// Validate flags.
if (dwFlags & ~MFT_SET_TYPE_TEST_ONLY)
{
throw ref new InvalidArgumentException();
}
AutoLock lock(m_critSec);
if (!IsValidInputStream(dwInputStreamID))
{
ThrowException(MF_E_INVALIDSTREAMNUMBER);
}
// Does the caller want us to set the type, or just test it?
bool fReallySet = ((dwFlags & MFT_SET_TYPE_TEST_ONLY) == 0);
// If we have an input sample, the client cannot change the type now.
if (HasPendingOutput())
{
ThrowException(MF_E_TRANSFORM_CANNOT_CHANGE_MEDIATYPE_WHILE_PROCESSING);
}
// Validate the type, if non-nullptr.
if (pType != nullptr)
{
OnCheckInputType(pType);
}
// The type is OK. Set the type, unless the caller was just testing.
if (fReallySet)
{
OnSetInputType(pType);
// When the type changes, end streaming.
EndStreaming();
}
}
catch(Exception ^exc)
{
hr = exc->HResult;
}
return hr;
}
//-------------------------------------------------------------------
// SetOutputType
//-------------------------------------------------------------------
HRESULT CGrayscale::SetOutputType(
DWORD dwOutputStreamID,
IMFMediaType *pType, // Can be nullptr to clear the output type.
DWORD dwFlags
)
{
HRESULT hr = S_OK;
try
{
if (!IsValidOutputStream(dwOutputStreamID))
{
return MF_E_INVALIDSTREAMNUMBER;
}
// Validate flags.
if (dwFlags & ~MFT_SET_TYPE_TEST_ONLY)
{
return E_INVALIDARG;
}
AutoLock lock(m_critSec);
// Does the caller want us to set the type, or just test it?
bool fReallySet = ((dwFlags & MFT_SET_TYPE_TEST_ONLY) == 0);
// If we have an input sample, the client cannot change the type now.
if (HasPendingOutput())
{
ThrowException(MF_E_TRANSFORM_CANNOT_CHANGE_MEDIATYPE_WHILE_PROCESSING);
}
// Validate the type, if non-nullptr.
if (pType != nullptr)
{
OnCheckOutputType(pType);
}
if (fReallySet)
{
// The type is OK.
// Set the type, unless the caller was just testing.
OnSetOutputType(pType);
EndStreaming();
}
}
catch (Exception ^exc)
{
hr = exc->HResult;
}
return hr;
}
//-------------------------------------------------------------------
// GetInputCurrentType
// Returns the current input type.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetInputCurrentType(
DWORD dwInputStreamID,
IMFMediaType **ppType
)
{
if (ppType == nullptr)
{
return E_POINTER;
}
HRESULT hr = S_OK;
AutoLock lock(m_critSec);
if (!IsValidInputStream(dwInputStreamID))
{
hr = MF_E_INVALIDSTREAMNUMBER;
}
else if (!m_spInputType)
{
hr = MF_E_TRANSFORM_TYPE_NOT_SET;
}
else
{
*ppType = m_spInputType.Get();
(*ppType)->AddRef();
}
return hr;
}
//-------------------------------------------------------------------
// GetOutputCurrentType
// Returns the current output type.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetOutputCurrentType(
DWORD dwOutputStreamID,
IMFMediaType **ppType
)
{
if (ppType == nullptr)
{
return E_POINTER;
}
HRESULT hr = S_OK;
AutoLock lock(m_critSec);
if (!IsValidOutputStream(dwOutputStreamID))
{
hr = MF_E_INVALIDSTREAMNUMBER;
}
else if (!m_spOutputType)
{
hr = MF_E_TRANSFORM_TYPE_NOT_SET;
}
else
{
*ppType = m_spOutputType.Get();
(*ppType)->AddRef();
}
return hr;
}
//-------------------------------------------------------------------
// GetInputStatus
// Query if the MFT is accepting more input.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetInputStatus(
DWORD dwInputStreamID,
DWORD *pdwFlags
)
{
if (pdwFlags == nullptr)
{
return E_POINTER;
}
AutoLock lock(m_critSec);
if (!IsValidInputStream(dwInputStreamID))
{
return MF_E_INVALIDSTREAMNUMBER;
}
// If an input sample is already queued, do not accept another sample until the
// client calls ProcessOutput or Flush.
// NOTE: It is possible for an MFT to accept more than one input sample. For
// example, this might be required in a video decoder if the frames do not
// arrive in temporal order. In the case, the decoder must hold a queue of
// samples. For the video effect, each sample is transformed independently, so
// there is no reason to queue multiple input samples.
if (m_spSample == nullptr)
{
*pdwFlags = MFT_INPUT_STATUS_ACCEPT_DATA;
}
else
{
*pdwFlags = 0;
}
return S_OK;
}
//-------------------------------------------------------------------
// GetOutputStatus
// Query if the MFT can produce output.
//-------------------------------------------------------------------
HRESULT CGrayscale::GetOutputStatus(DWORD *pdwFlags)
{
if (pdwFlags == nullptr)
{
return E_POINTER;
}
AutoLock lock(m_critSec);
// The MFT can produce an output sample if (and only if) there an input sample.
if (m_spSample != nullptr)
{
*pdwFlags = MFT_OUTPUT_STATUS_SAMPLE_READY;
}
else
{
*pdwFlags = 0;
}
return S_OK;
}
//-------------------------------------------------------------------
// SetOutputBounds
// Sets the range of time stamps that the MFT will output.
//-------------------------------------------------------------------
HRESULT CGrayscale::SetOutputBounds(
LONGLONG hnsLowerBound,
LONGLONG hnsUpperBound
)
{
// Implementation of this method is optional.
return E_NOTIMPL;
}
//-------------------------------------------------------------------
// ProcessEvent
// Sends an event to an input stream.
//-------------------------------------------------------------------
HRESULT CGrayscale::ProcessEvent(
DWORD dwInputStreamID,
IMFMediaEvent *pEvent
)
{
// This MFT does not handle any stream events, so the method can
// return E_NOTIMPL. This tells the pipeline that it can stop
// sending any more events to this MFT.
return E_NOTIMPL;
}
//-------------------------------------------------------------------
// ProcessMessage
//-------------------------------------------------------------------
HRESULT CGrayscale::ProcessMessage(
MFT_MESSAGE_TYPE eMessage,
ULONG_PTR ulParam
)
{
AutoLock lock(m_critSec);
HRESULT hr = S_OK;
try
{
switch (eMessage)
{
case MFT_MESSAGE_COMMAND_FLUSH:
// Flush the MFT.
OnFlush();
break;
case MFT_MESSAGE_COMMAND_DRAIN:
// Drain: Tells the MFT to reject further input until all pending samples are
// processed. That is our default behavior already, so there is nothing to do.
//
// For a decoder that accepts a queue of samples, the MFT might need to drain
// the queue in response to this command.
break;
case MFT_MESSAGE_SET_D3D_MANAGER:
// Sets a pointer to the IDirect3DDeviceManager9 interface.
// The pipeline should never send this message unless the MFT sets the MF_SA_D3D_AWARE
// attribute set to TRUE. Because this MFT does not set MF_SA_D3D_AWARE, it is an error
// to send the MFT_MESSAGE_SET_D3D_MANAGER message to the MFT. Return an error code in
// this case.
// NOTE: If this MFT were D3D-enabled, it would cache the IMFDXGIDeviceManager
// pointer for use during streaming.
ThrowException(E_NOTIMPL);
break;
case MFT_MESSAGE_NOTIFY_BEGIN_STREAMING:
BeginStreaming();
break;
case MFT_MESSAGE_NOTIFY_END_STREAMING:
EndStreaming();
break;
// The next two messages do not require any action from this MFT.