forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
163 lines (148 loc) · 4.06 KB
/
common.h
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
///--------------------------------------------------------------------------------------
// Common.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#pragma once
#include <Windows.h>
#include <wrl\implements.h>
#include <mmreg.h>
#include <mfapi.h>
// release and zero out a possible NULL pointer. note this will
// do the release on a temp copy to avoid reentrancy issues that can result from
// callbacks durring the release
template <class T> void SafeRelease( __deref_inout_opt T **ppT )
{
T *pTTemp = *ppT; // temp copy
*ppT = nullptr; // zero the input
if (pTTemp)
{
pTTemp->Release();
}
}
#ifndef SAFE_DELETE
#define SAFE_DELETE(x) { delete x; x = nullptr; }
#endif
#ifndef SAFE_ARRAYDELETE
#define SAFE_ARRAYDELETE(x) { delete[] x; x = nullptr; }
#endif
#ifndef METHODASYNCCALLBACK
#define METHODASYNCCALLBACK(Parent, AsyncCallback, pfnCallback) \
class Callback##AsyncCallback :\
public IMFAsyncCallback \
{ \
public: \
Callback##AsyncCallback() : \
_parent(((Parent*)((BYTE*)this - offsetof(Parent, m_x##AsyncCallback)))), \
_dwQueueID( MFASYNC_CALLBACK_QUEUE_MULTITHREADED ) \
{ \
} \
\
STDMETHOD_( ULONG, AddRef )() \
{ \
return _parent->AddRef(); \
} \
STDMETHOD_( ULONG, Release )() \
{ \
return _parent->Release(); \
} \
STDMETHOD( QueryInterface )( REFIID riid, void **ppvObject ) \
{ \
if (riid == IID_IMFAsyncCallback || riid == IID_IUnknown) \
{ \
(*ppvObject) = this; \
AddRef(); \
return S_OK; \
} \
*ppvObject = NULL; \
return E_NOINTERFACE; \
} \
STDMETHOD( GetParameters )( \
/* [out] */ __RPC__out DWORD *pdwFlags, \
/* [out] */ __RPC__out DWORD *pdwQueue) \
{ \
*pdwFlags = 0; \
*pdwQueue = _dwQueueID; \
return S_OK; \
} \
STDMETHOD( Invoke )( /* [out] */ __RPC__out IMFAsyncResult * pResult ) \
{ \
_parent->pfnCallback( pResult ); \
return S_OK; \
} \
void SetQueueID( DWORD dwQueueID ) { _dwQueueID = dwQueueID; } \
\
protected: \
Parent* _parent; \
DWORD _dwQueueID; \
\
} m_x##AsyncCallback;
#endif
//
// CAsyncState
//
// Used to maintain state during MF Work Item callbacks
class CAsyncState :
public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>, IUnknown>
{
public:
CAsyncState( Platform::Array<int,1>^ data, UINT32 size ) :
m_Data(data),
m_Size(size)
{
};
public:
Platform::Array<int,1>^ m_Data;
UINT32 m_Size;
private:
virtual ~CAsyncState() {};
};
struct RenderBuffer
{
UINT32 BufferSize;
UINT32 BytesFilled;
BYTE *Buffer;
RenderBuffer *Next;
RenderBuffer() :
BufferSize(0),
BytesFilled(0),
Buffer( nullptr ),
Next( nullptr )
{
}
~RenderBuffer()
{
SAFE_ARRAYDELETE( Buffer );
}
};
enum RenderSampleType
{
SampleTypeUnknown,
SampleTypeFloat,
SampleType16BitPCM,
};
//
// CalculateMixFormatType()
//
// Determine IEEE Float or PCM samples based on media type
//
inline RenderSampleType CalculateMixFormatType( WAVEFORMATEX *wfx )
{
if ( (wfx->wFormatTag == WAVE_FORMAT_PCM) ||
( (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
(reinterpret_cast<WAVEFORMATEXTENSIBLE *>(wfx)->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) ) )
{
if (wfx->wBitsPerSample == 16)
{
return RenderSampleType::SampleType16BitPCM;
}
}
else if ( (wfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) ||
( (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
(reinterpret_cast<WAVEFORMATEXTENSIBLE *>(wfx)->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) ) )
{
return RenderSampleType::SampleTypeFloat;
}
return RenderSampleType::SampleTypeUnknown;
}