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
|
/*
* (C) 2007-2013 see Authors.txt
*
* This file is part of MPC-HC.
*
* MPC-HC 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 3 of the License, or
* (at your option) any later version.
*
* MPC-HC 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "stdafx.h"
#include "VideoDecDXVAAllocator.h"
#include "MPCVideoDecFilter.h"
#include <Mferror.h>
#include <d3dx9.h>
#include <evr.h>
CDXVA2Sample::CDXVA2Sample(CVideoDecDXVAAllocator* pAlloc, HRESULT* phr)
: CMediaSample(NAME("CDXVA2Sample"), (CBaseAllocator*)pAlloc, phr, NULL, 0)
, m_dwSurfaceId(0)
{
}
//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(IMPCDXVA2Sample)) {
return GetInterface((IMPCDXVA2Sample*) 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 == NULL) {
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)
{
m_pSurface = pSurf;
m_dwSurfaceId = surfaceId;
}
STDMETHODIMP_(int) CDXVA2Sample::GetDXSurfaceId()
{
return m_dwSurfaceId;
}
CVideoDecDXVAAllocator::CVideoDecDXVAAllocator(CMPCVideoDecFilter* pVideoDecFilter, HRESULT* phr)
: CBaseAllocator(NAME("CVideoDecDXVAAllocator"), NULL, phr)
, m_pVideoDecFilter(pVideoDecFilter)
, m_ppRTSurfaceArray(NULL)
, m_nSurfaceArrayCount(0)
{
}
CVideoDecDXVAAllocator::~CVideoDecDXVAAllocator()
{
Free();
}
HRESULT CVideoDecDXVAAllocator::Alloc()
{
HRESULT hr;
CComPtr<IDirectXVideoDecoderService> pDXVA2Service;
CheckPointer(m_pVideoDecFilter->m_pDeviceManager, E_UNEXPECTED);
hr = m_pVideoDecFilter->m_pDeviceManager->GetVideoService(m_pVideoDecFilter->m_hDevice, IID_IDirectXVideoDecoderService, (void**)&pDXVA2Service);
CheckPointer(pDXVA2Service, E_UNEXPECTED);
CAutoLock lock(this);
hr = __super::Alloc();
if (SUCCEEDED(hr)) {
// Free the old resources.
Free();
m_nSurfaceArrayCount = m_lCount;
// Allocate a new array of pointers.
m_ppRTSurfaceArray = DEBUG_NEW IDirect3DSurface9 * [m_lCount];
if (m_ppRTSurfaceArray == NULL) {
hr = E_OUTOFMEMORY;
} else {
ZeroMemory(m_ppRTSurfaceArray, sizeof(IDirect3DSurface9*) * m_lCount);
}
}
// Allocate the surfaces.
D3DFORMAT m_dwFormat = m_pVideoDecFilter->m_VideoDesc.Format;
if (SUCCEEDED(hr)) {
hr = pDXVA2Service->CreateSurface(
m_pVideoDecFilter->PictWidthRounded(),
m_pVideoDecFilter->PictHeightRounded(),
m_lCount - 1,
(D3DFORMAT)m_dwFormat,
D3DPOOL_DEFAULT,
0,
DXVA2_VideoDecoderRenderTarget,
m_ppRTSurfaceArray,
NULL
);
}
if (SUCCEEDED(hr)) {
// Important : create samples in reverse order !
for (m_lAllocated = m_lCount - 1; m_lAllocated >= 0; m_lAllocated--) {
CDXVA2Sample* pSample = DEBUG_NEW CDXVA2Sample(this, &hr);
if (pSample == NULL) {
hr = E_OUTOFMEMORY;
break;
}
if (FAILED(hr)) {
break;
}
// Assign the Direct3D surface pointer and the index.
pSample->SetSurface(m_lAllocated, m_ppRTSurfaceArray[m_lAllocated]);
// Add to the sample list.
m_lFree.Add(pSample);
}
hr = m_pVideoDecFilter->CreateDXVA2Decoder(m_lCount, m_ppRTSurfaceArray);
if (FAILED(hr)) {
Free();
}
}
if (SUCCEEDED(hr)) {
m_bChanged = FALSE;
}
return hr;
}
void CVideoDecDXVAAllocator::Free()
{
CMediaSample* pSample = NULL;
m_pVideoDecFilter->FlushDXVADecoder();
// m_FreeSurface.RemoveAll();
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] != NULL) {
m_ppRTSurfaceArray[i]->Release();
}
}
delete [] m_ppRTSurfaceArray;
m_ppRTSurfaceArray = NULL;
}
m_lAllocated = 0;
m_nSurfaceArrayCount = 0;
}
|