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
|
/*
* Copyright (C) 2010-2013 Hendrik Leppkes
* http://www.1f0.de
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Initial Design and Concept taken from MPC-HC, licensed under GPLv2
*/
#include "stdafx.h"
#include "decoders/dxva2dec.h"
#include "DXVA2SurfaceAllocator.h"
#include "moreuuids.h"
#include <evr.h>
#include <Mferror.h>
CDXVA2Sample::CDXVA2Sample(CDXVA2SurfaceAllocator *pAlloc, HRESULT *phr)
: CMediaSample(NAME("CDXVA2Sample"), (CBaseAllocator*)pAlloc, phr, nullptr, 0)
{
}
CDXVA2Sample::~CDXVA2Sample()
{
SafeRelease(&m_pSurface);
}
//Note: CMediaSample does not derive from CUnknown, so we cannot use the
// DECLARE_IUNKNOWN macro that is used by most of the filter classes.
STDMETHODIMP CDXVA2Sample::QueryInterface(REFIID riid, __deref_out void **ppv)
{
CheckPointer(ppv,E_POINTER);
ValidateReadWritePtr(ppv,sizeof(PVOID));
if (riid == __uuidof(IMFGetService)) {
return GetInterface((IMFGetService*) this, ppv);
}
if (riid == __uuidof(ILAVDXVA2Sample)) {
return GetInterface((ILAVDXVA2Sample*) this, ppv);
} else {
return CMediaSample::QueryInterface(riid, ppv);
}
}
STDMETHODIMP_(ULONG) CDXVA2Sample::AddRef()
{
return __super::AddRef();
}
STDMETHODIMP_(ULONG) CDXVA2Sample::Release()
{
// Return a temporary variable for thread safety.
ULONG cRef = __super::Release();
return cRef;
}
// IMFGetService::GetService
STDMETHODIMP CDXVA2Sample::GetService(REFGUID guidService, REFIID riid, LPVOID *ppv)
{
if (guidService != MR_BUFFER_SERVICE) {
return MF_E_UNSUPPORTED_SERVICE;
} else if (m_pSurface == nullptr) {
return E_NOINTERFACE;
} else {
return m_pSurface->QueryInterface(riid, ppv);
}
}
// Override GetPointer because this class does not manage a system memory buffer.
// The EVR uses the MR_BUFFER_SERVICE service to get the Direct3D surface.
STDMETHODIMP CDXVA2Sample::GetPointer(BYTE ** ppBuffer)
{
return E_NOTIMPL;
}
// Sets the pointer to the Direct3D surface.
void CDXVA2Sample::SetSurface(DWORD surfaceId, IDirect3DSurface9 *pSurf)
{
SafeRelease(&m_pSurface);
m_pSurface = pSurf;
m_dwSurfaceId = surfaceId;
if (m_pSurface)
m_pSurface->AddRef();
}
STDMETHODIMP_(int) CDXVA2Sample::GetDXSurfaceId()
{
return m_dwSurfaceId;
}
CDXVA2SurfaceAllocator::CDXVA2SurfaceAllocator(CDecDXVA2 *m_pDXVA2Dec, HRESULT* phr)
: CBaseAllocator(NAME("CDXVA2SurfaceAllocator"), nullptr, phr)
, m_pDec(m_pDXVA2Dec)
{
}
CDXVA2SurfaceAllocator::~CDXVA2SurfaceAllocator(void)
{
if (m_pDec && m_pDec->m_pDXVA2Allocator == this)
m_pDec->m_pDXVA2Allocator = nullptr;
}
// IUnknown
STDMETHODIMP CDXVA2SurfaceAllocator::NonDelegatingQueryInterface(REFIID riid, void** ppv)
{
CheckPointer(ppv, E_POINTER);
*ppv = nullptr;
return
QI(ILAVDXVA2SurfaceAllocator)
__super::NonDelegatingQueryInterface(riid, ppv);
}
HRESULT CDXVA2SurfaceAllocator::Alloc()
{
DbgLog((LOG_TRACE, 10, L"CDXVA2SurfaceAllocator::Alloc()"));
HRESULT hr = S_OK;
IDirectXVideoDecoderService *pDXVA2Service = nullptr;
if (!m_pDec)
return E_FAIL;
CheckPointer(m_pDec->m_pD3DDevMngr, E_UNEXPECTED);
hr = m_pDec->m_pD3DDevMngr->GetVideoService (m_pDec->m_hDevice, IID_IDirectXVideoDecoderService, (void**)&pDXVA2Service);
CheckPointer (pDXVA2Service, E_UNEXPECTED);
CAutoLock lock(this);
hr = __super::Alloc();
if (SUCCEEDED(hr)) {
DbgLog((LOG_TRACE, 10, L"-> Releasing old resources"));
// Free the old resources.
m_pDec->FlushFromAllocator();
Free();
m_nSurfaceArrayCount = m_lCount;
// Allocate a new array of pointers.
m_ppRTSurfaceArray = new IDirect3DSurface9*[m_lCount];
if (m_ppRTSurfaceArray == nullptr) {
hr = E_OUTOFMEMORY;
} else {
ZeroMemory(m_ppRTSurfaceArray, sizeof(IDirect3DSurface9*) * m_lCount);
}
}
// Allocate the surfaces.
D3DFORMAT d3dFormat = (D3DFORMAT)FOURCC_NV12;
if (SUCCEEDED(hr)) {
DbgLog((LOG_TRACE, 10, L"-> Allocating surfaces"));
hr = pDXVA2Service->CreateSurface(
m_pDec->m_dwSurfaceWidth,
m_pDec->m_dwSurfaceHeight,
m_lCount - 1,
d3dFormat,
D3DPOOL_DEFAULT,
0,
DXVA2_VideoDecoderRenderTarget,
m_ppRTSurfaceArray,
nullptr
);
}
if (SUCCEEDED(hr)) {
DbgLog((LOG_TRACE, 10, L"-> Creating samples"));
// Important : create samples in reverse order !
for (int i = m_lCount-1; i >= 0; i--) {
CDXVA2Sample *pSample = new CDXVA2Sample(this, &hr);
if (pSample == nullptr) {
hr = E_OUTOFMEMORY;
break;
}
if (FAILED(hr)) {
break;
}
// Assign the Direct3D surface pointer and the index.
pSample->SetSurface(i, m_ppRTSurfaceArray[i]);
// Add to the sample list.
m_lFree.Add(pSample);
}
hr = m_pDec->CreateDXVA2Decoder(m_lCount, m_ppRTSurfaceArray);
if (FAILED (hr)) {
Free();
}
}
m_lAllocated = m_lCount;
if (SUCCEEDED(hr)) {
m_bChanged = FALSE;
}
SafeRelease(&pDXVA2Service);
return hr;
}
void CDXVA2SurfaceAllocator::Free()
{
DbgLog((LOG_TRACE, 10, L"CDXVA2SurfaceAllocator::Free()"));
CMediaSample *pSample = nullptr;
CAutoLock lock(this);
do {
pSample = m_lFree.RemoveHead();
if (pSample) {
delete pSample;
}
} while (pSample);
if (m_ppRTSurfaceArray) {
for (UINT i = 0; i < m_nSurfaceArrayCount; i++) {
if (m_ppRTSurfaceArray[i] != nullptr) {
m_ppRTSurfaceArray[i]->Release();
}
}
delete [] m_ppRTSurfaceArray;
m_ppRTSurfaceArray = nullptr;
}
m_lAllocated = 0;
m_nSurfaceArrayCount = 0;
}
|