-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathd3d_allocator.cpp
More file actions
401 lines (343 loc) · 11.1 KB
/
d3d_allocator.cpp
File metadata and controls
401 lines (343 loc) · 11.1 KB
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
/*
* Copyright (c) 2013, INTEL CORPORATION
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* Neither the name of INTEL CORPORATION nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
#include "QuickSync_defs.h"
#include "d3d_allocator.h"
static const D3DFORMAT D3DFMT_NV12 = (D3DFORMAT)MAKEFOURCC('N','V','1','2');
static const D3DFORMAT D3DFMT_YV12 = (D3DFORMAT)MAKEFOURCC('Y','V','1','2');
D3DFORMAT ConvertMfxFourccToD3dFormat(mfxU32 fourcc)
{
switch (fourcc)
{
case MFX_FOURCC_NV12:
return D3DFMT_NV12;
case MFX_FOURCC_YV12:
return D3DFMT_YV12;
case MFX_FOURCC_YUY2:
return D3DFMT_YUY2;
case MFX_FOURCC_RGB3:
return D3DFMT_R8G8B8;
case MFX_FOURCC_RGB4:
return D3DFMT_A8R8G8B8;
case MFX_FOURCC_P8:
return D3DFMT_P8;
default:
return D3DFMT_UNKNOWN;
}
}
class DeviceHandle
{
public:
DeviceHandle(IDirect3DDeviceManager9* manager)
: m_manager(manager)
, m_handle(0)
{
if (manager)
{
HRESULT hr = manager->OpenDeviceHandle(&m_handle);
if (FAILED(hr))
m_manager = 0;
}
}
~DeviceHandle()
{
if (m_manager && m_handle)
m_manager->CloseDeviceHandle(m_handle);
}
HANDLE Detach()
{
HANDLE tmp = m_handle;
m_manager = 0;
m_handle = 0;
return tmp;
}
operator HANDLE()
{
return m_handle;
}
bool operator !() const
{
return m_handle == 0;
}
protected:
CComPtr<IDirect3DDeviceManager9> m_manager;
HANDLE m_handle;
};
D3DFrameAllocator::D3DFrameAllocator() :
m_manager(0),
m_decoderService(0),
m_processorService(0),
m_hDecoder(0),
m_hProcessor(0),
m_surfaceUsage(0)
{
}
D3DFrameAllocator::~D3DFrameAllocator()
{
Close();
}
mfxStatus D3DFrameAllocator::Init(mfxAllocatorParams *pParams)
{
D3DAllocatorParams *pd3dParams = 0;
pd3dParams = dynamic_cast<D3DAllocatorParams *>(pParams);
if (!pd3dParams)
return MFX_ERR_NOT_INITIALIZED;
m_manager = pd3dParams->pManager;
m_surfaceUsage = pd3dParams->surfaceUsage;
return MFX_ERR_NONE;
}
mfxStatus D3DFrameAllocator::Close()
{
if (m_manager && m_hDecoder)
{
m_manager->CloseDeviceHandle(m_hDecoder);
m_manager = 0;
m_hDecoder = 0;
}
if (m_manager && m_hProcessor)
{
m_manager->CloseDeviceHandle(m_hProcessor);
m_manager = 0;
m_hProcessor = 0;
}
return BaseFrameAllocator::Close();
}
mfxStatus D3DFrameAllocator::LockFrame(mfxMemId mid, mfxFrameData *ptr)
{
IDirect3DSurface9 *pSurface = (IDirect3DSurface9 *)mid;
if (pSurface == 0)
return MFX_ERR_INVALID_HANDLE;
if (ptr == 0)
return MFX_ERR_LOCK_MEMORY;
D3DSURFACE_DESC desc;
HRESULT hr = pSurface->GetDesc(&desc);
if (FAILED(hr))
return MFX_ERR_LOCK_MEMORY;
if (desc.Format != D3DFMT_NV12 &&
desc.Format != D3DFMT_YV12 &&
desc.Format != D3DFMT_YUY2 &&
desc.Format != D3DFMT_R8G8B8 &&
desc.Format != D3DFMT_A8R8G8B8 &&
desc.Format != D3DFMT_P8)
return MFX_ERR_LOCK_MEMORY;
D3DLOCKED_RECT locked;
hr = pSurface->LockRect(&locked, 0, D3DLOCK_NOSYSLOCK);
if (FAILED(hr))
return MFX_ERR_LOCK_MEMORY;
switch ((DWORD)desc.Format)
{
case D3DFMT_NV12:
ptr->Pitch = (mfxU16)locked.Pitch;
ptr->Y = (mfxU8 *)locked.pBits;
ptr->U = (mfxU8 *)locked.pBits + desc.Height * locked.Pitch;
ptr->V = ptr->U + 1;
break;
case D3DFMT_YV12:
ptr->Pitch = (mfxU16)locked.Pitch;
ptr->Y = (mfxU8 *)locked.pBits;
ptr->V = ptr->Y + desc.Height * locked.Pitch;
ptr->U = ptr->V + (desc.Height * locked.Pitch) / 4;
break;
case D3DFMT_YUY2:
ptr->Pitch = (mfxU16)locked.Pitch;
ptr->Y = (mfxU8 *)locked.pBits;
ptr->U = ptr->Y + 1;
ptr->V = ptr->Y + 3;
break;
case D3DFMT_R8G8B8:
ptr->Pitch = (mfxU16)locked.Pitch;
ptr->B = (mfxU8 *)locked.pBits;
ptr->G = ptr->B + 1;
ptr->R = ptr->B + 2;
break;
case D3DFMT_A8R8G8B8:
ptr->Pitch = (mfxU16)locked.Pitch;
ptr->B = (mfxU8 *)locked.pBits;
ptr->G = ptr->B + 1;
ptr->R = ptr->B + 2;
ptr->A = ptr->B + 3;
break;
case D3DFMT_P8:
ptr->Pitch = (mfxU16)locked.Pitch;
ptr->Y = (mfxU8 *)locked.pBits;
ptr->U = 0;
ptr->V = 0;
break;
}
return MFX_ERR_NONE;
}
mfxStatus D3DFrameAllocator::UnlockFrame(mfxMemId mid, mfxFrameData *ptr)
{
IDirect3DSurface9 *pSurface = (IDirect3DSurface9 *)mid;
if (pSurface == 0)
return MFX_ERR_INVALID_HANDLE;
pSurface->UnlockRect();
if (NULL != ptr)
{
ptr->Pitch = 0;
ptr->Y = 0;
ptr->U = 0;
ptr->V = 0;
}
return MFX_ERR_NONE;
}
mfxStatus D3DFrameAllocator::GetFrameHDL(mfxMemId mid, mfxHDL *handle)
{
if (handle == 0)
return MFX_ERR_INVALID_HANDLE;
*handle = mid;
return MFX_ERR_NONE;
}
mfxStatus D3DFrameAllocator::CheckRequestType(mfxFrameAllocRequest *request)
{
mfxStatus sts = BaseFrameAllocator::CheckRequestType(request);
if (MSDK_FAILED(sts))
return sts;
if ((request->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) != 0)
return MFX_ERR_NONE;
else
return MFX_ERR_UNSUPPORTED;
}
mfxStatus D3DFrameAllocator::ReleaseResponse(mfxFrameAllocResponse *response)
{
if (!response)
return MFX_ERR_NULL_PTR;
mfxStatus sts = MFX_ERR_NONE;
if (response->mids)
{
for (mfxU32 i = 0; i < response->NumFrameActual; i++)
{
if (response->mids[i])
{
IDirect3DSurface9* handle = 0;
sts = GetFrameHDL(response->mids[i], (mfxHDL *)&handle);
if (MSDK_FAILED(sts))
return sts;
handle->Release();
}
}
}
MSDK_SAFE_DELETE_ARRAY(response->mids);
return sts;
}
mfxStatus D3DFrameAllocator::AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response)
{
HRESULT hr;
D3DFORMAT format = ConvertMfxFourccToD3dFormat(request->Info.FourCC);
if (format == D3DFMT_UNKNOWN)
return MFX_ERR_UNSUPPORTED;
safe_array<mfxMemId> mids(new mfxMemId[request->NumFrameSuggested]);
if (!mids.get())
return MFX_ERR_MEMORY_ALLOC;
DWORD target;
if (MFX_MEMTYPE_DXVA2_DECODER_TARGET & request->Type)
{
target = DXVA2_VideoDecoderRenderTarget;
}
else if (MFX_MEMTYPE_DXVA2_PROCESSOR_TARGET & request->Type)
{
target = DXVA2_VideoProcessorRenderTarget;
}
else
return MFX_ERR_UNSUPPORTED;
// VPP may require at input/output surfaces with color format other than NV12 (in case of color conversion)
// VideoProcessorService must be used to create such surfaces
if (target == DXVA2_VideoProcessorRenderTarget)
{
if (!m_hProcessor)
{
DeviceHandle device = DeviceHandle(m_manager);
if (!device)
return MFX_ERR_MEMORY_ALLOC;
CComPtr<IDirectXVideoProcessorService> service = 0;
hr = m_manager->GetVideoService(device, IID_IDirectXVideoProcessorService, (void**)&service);
if (FAILED(hr))
return MFX_ERR_MEMORY_ALLOC;
m_processorService = service;
m_hProcessor = device.Detach();
}
hr = m_processorService->CreateSurface(
request->Info.Width,
request->Info.Height,
request->NumFrameSuggested - 1,
format,
D3DPOOL_DEFAULT,
m_surfaceUsage,
target,
(IDirect3DSurface9 **)mids.get(),
NULL);
}
else
{
if (!m_hDecoder)
{
DeviceHandle device = DeviceHandle(m_manager);
if (!device)
return MFX_ERR_MEMORY_ALLOC;
CComPtr<IDirectXVideoDecoderService> service = 0;
hr = m_manager->GetVideoService(device, IID_IDirectXVideoDecoderService, (void**)&service);
if (FAILED(hr))
return MFX_ERR_MEMORY_ALLOC;
m_decoderService = service;
m_hDecoder = device.Detach();
}
hr = m_decoderService->CreateSurface(
request->Info.Width,
request->Info.Height,
request->NumFrameSuggested - 1,
format,
D3DPOOL_DEFAULT,
m_surfaceUsage,
target,
(IDirect3DSurface9 **)mids.get(),
NULL);
}
if (FAILED(hr))
{
return MFX_ERR_MEMORY_ALLOC;
}
#if 0 // optional, may be done on application level if needed
//zeroing created surfaces
for (mfxU32 i = 0; i < request->NumFrameSuggested; i++)
{
mfxFrameData pData;
if (MSDK_SUCCEEDED(LockFrame(mids.get()[i], &pData)))
{
if (format == D3DFMT_NV12 ||
format == D3DFMT_YV12)
{
memset(pData.Y, 0, (pData.Pitch * request->Info.Height * 3)/2 );
}
UnlockFrame(mids.get()[i], &pData);
}
}
#endif
response->mids = mids.release();
response->NumFrameActual = request->NumFrameSuggested;
return MFX_ERR_NONE;
}