1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "config.h"
20
21 #include <windows.h>
22
24
25 #include <initguid.h>
26 #include <d3d11.h>
27 #include <dxgi1_2.h>
28
29 #if HAVE_DXGIDEBUG_H
30 #include <dxgidebug.h>
31 #endif
32
43
45
47
50
52 {
53 #if !HAVE_UWP
54 // We let these "leak" - this is fine, as unloading has no great benefit, and
55 // Windows will mark a DLL as loaded forever if its internal refcount overflows
56 // from too many LoadLibrary calls.
57 HANDLE d3dlib, dxgilib;
58
59 d3dlib = dlopen("d3d11.dll", 0);
60 dxgilib = dlopen("dxgi.dll", 0);
61 if (!d3dlib || !dxgilib)
62 return;
63
64 mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE) GetProcAddress(d3dlib,
"D3D11CreateDevice");
66 #else
67 // In UWP (which lacks LoadLibrary), CreateDXGIFactory isn't available,
68 // only CreateDXGIFactory1
71 #endif
72 }
73
77
79
82
83 static const struct {
92 // Special opaque formats. The pix_fmt is merely a place holder, as the
93 // opaque format cannot be accessed directly.
95 };
96
98 {
99 WaitForSingleObjectEx(
ctx, INFINITE, FALSE);
100 }
101
103 {
105 }
106
108 {
111
113 ID3D11Texture2D_Release(frames_hwctx->
texture);
115
116 if (
s->staging_texture)
117 ID3D11Texture2D_Release(
s->staging_texture);
118 s->staging_texture =
NULL;
119
121 }
122
124 const void *hwconfig,
126 {
128 int nb_sw_formats = 0;
129 HRESULT hr;
131
136
138 UINT format_support = 0;
140 if (SUCCEEDED(hr) && (format_support & D3D11_FORMAT_SUPPORT_TEXTURE2D))
142 }
144
148
151
152 return 0;
153 }
154
156 {
157 ID3D11Texture2D_Release((ID3D11Texture2D *)opaque);
159 }
160
162 {
168 ID3D11Texture2D_Release(tex);
170 }
171
172 if (
s->nb_surfaces <=
s->nb_surfaces_used) {
174 s->nb_surfaces_used + 1,
177 ID3D11Texture2D_Release(tex);
179 }
180 s->nb_surfaces =
s->nb_surfaces_used + 1;
181 }
182
185 s->nb_surfaces_used++;
186
189
191 if (!buf) {
192 ID3D11Texture2D_Release(tex);
195 }
196
197 return buf;
198 }
199
201 {
205 HRESULT hr;
206 ID3D11Texture2D *tex;
207 D3D11_TEXTURE2D_DESC texDesc = {
209 .Height =
ctx->height,
210 .MipLevels = 1,
212 .SampleDesc = { .Count = 1 },
213 .ArraySize = 1,
214 .Usage = D3D11_USAGE_DEFAULT,
217 };
218
219 hr = ID3D11Device_CreateTexture2D(device_hwctx->
device, &texDesc,
NULL, &tex);
220 if (FAILED(hr)) {
223 }
224
226 }
227
229 {
233 D3D11_TEXTURE2D_DESC texDesc;
234
237
238 ID3D11Texture2D_GetDesc(hwctx->
texture, &texDesc);
239
240 if (
s->nb_surfaces_used >= texDesc.ArraySize) {
243 }
244
245 ID3D11Texture2D_AddRef(hwctx->
texture);
247 }
248
250 {
254
256 HRESULT hr;
257 D3D11_TEXTURE2D_DESC texDesc;
258
262 break;
263 }
264 }
269 }
270
271 texDesc = (D3D11_TEXTURE2D_DESC){
273 .Height =
ctx->height,
274 .MipLevels = 1,
276 .SampleDesc = { .Count = 1 },
277 .ArraySize =
ctx->initial_pool_size,
278 .Usage = D3D11_USAGE_DEFAULT,
281 };
282
284 D3D11_TEXTURE2D_DESC texDesc2;
285 ID3D11Texture2D_GetDesc(hwctx->
texture, &texDesc2);
286
287 if (texDesc.Width != texDesc2.Width ||
288 texDesc.Height != texDesc2.Height ||
289 texDesc.Format != texDesc2.Format) {
292 }
293
294 ctx->initial_pool_size = texDesc2.ArraySize;
297 } else if (!(texDesc.BindFlags & D3D11_BIND_RENDER_TARGET) && texDesc.ArraySize > 0) {
298 hr = ID3D11Device_CreateTexture2D(device_hwctx->
device, &texDesc,
NULL, &hwctx->
texture);
299 if (FAILED(hr)) {
302 }
303 }
304
308 s->nb_surfaces =
ctx->initial_pool_size;
309
312 if (!
ctx->internal->pool_internal)
314
315 return 0;
316 }
317
319 {
321
325
327
328 frame->data[0] = (uint8_t *)
desc->texture;
333
334 return 0;
335 }
336
340 {
343
345 if (!fmts)
347
348 fmts[0] =
ctx->sw_format;
350
351 // Don't signal support for opaque formats. Actual access would fail.
352 if (
s->format == DXGI_FORMAT_420_OPAQUE)
354
356
357 return 0;
358 }
359
361 {
364 HRESULT hr;
365 D3D11_TEXTURE2D_DESC texDesc = {
367 .Height =
ctx->height,
368 .MipLevels = 1,
370 .SampleDesc = { .Count = 1 },
371 .ArraySize = 1,
372 .Usage = D3D11_USAGE_STAGING,
373 .CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE,
374 };
375
376 hr = ID3D11Device_CreateTexture2D(device_hwctx->
device, &texDesc,
NULL, &
s->staging_texture);
377 if (FAILED(hr)) {
380 }
381
382 return 0;
383 }
384
387 D3D11_TEXTURE2D_DESC *
desc,
388 D3D11_MAPPED_SUBRESOURCE *
map)
389 {
391
392 for (
i = 0;
i < 4;
i++)
393 linesize[
i] =
map->RowPitch;
394
396 (uint8_t*)
map->pData, linesize);
397 }
398
401 {
407 // (The interface types are compatible.)
408 ID3D11Resource *texture = (ID3D11Resource *)(ID3D11Texture2D *)
frame->data[0];
410 ID3D11Resource *staging;
413 uint8_t *map_data[4];
414 int map_linesize[4];
415 D3D11_TEXTURE2D_DESC
desc;
416 D3D11_MAPPED_SUBRESOURCE
map;
417 HRESULT hr;
418 int res;
419
420 if (
frame->hw_frames_ctx->data != (uint8_t *)
ctx || other->
format !=
ctx->sw_format)
422
424
425 if (!
s->staging_texture) {
426 ID3D11Texture2D_GetDesc((ID3D11Texture2D *)texture, &
desc);
428 if (res < 0)
429 return res;
430 }
431
432 staging = (ID3D11Resource *)
s->staging_texture;
433
434 ID3D11Texture2D_GetDesc(
s->staging_texture, &
desc);
435
436 if (download) {
437 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->
device_context,
438 staging, 0, 0, 0, 0,
440
442 staging, 0, D3D11_MAP_READ, 0, &
map);
443 if (FAILED(hr))
444 goto map_failed;
445
447
449 ctx->sw_format,
w,
h);
450
451 ID3D11DeviceContext_Unmap(device_hwctx->
device_context, staging, 0);
452 } else {
454 staging, 0, D3D11_MAP_WRITE, 0, &
map);
455 if (FAILED(hr))
456 goto map_failed;
457
459
461 ctx->sw_format,
w,
h);
462
463 ID3D11DeviceContext_Unmap(device_hwctx->
device_context, staging, 0);
464
465 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->
device_context,
466 texture,
index, 0, 0, 0,
468 }
469
471 return 0;
472
473 map_failed:
477 }
478
480 {
482 HRESULT hr;
483
484 if (!device_hwctx->
lock) {
486 if (device_hwctx->
lock_ctx == INVALID_HANDLE_VALUE) {
489 }
492 }
493
498 }
499
501 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->
device, &IID_ID3D11VideoDevice,
503 if (FAILED(hr))
505 }
506
508 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->
device_context, &IID_ID3D11VideoContext,
510 if (FAILED(hr))
512 }
513
514 return 0;
515 }
516
518 {
520
521 if (device_hwctx->
device) {
522 ID3D11Device_Release(device_hwctx->
device);
524 }
525
529 }
530
534 }
535
539 }
540
542 CloseHandle(device_hwctx->
lock_ctx);
543 device_hwctx->
lock_ctx = INVALID_HANDLE_VALUE;
545 }
546 }
547
550 {
552
553 HRESULT hr;
554 IDXGIAdapter *pAdapter =
NULL;
555 ID3D10Multithread *pMultithread;
556 UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
559
560 // (On UWP we can't check this.)
561 #if !HAVE_UWP
562 if (!LoadLibrary("d3d11_1sdklayers.dll"))
563 is_debug = 0;
564 #endif
565
566 if (is_debug)
567 creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
568
574 }
575
576 if (device) {
577 IDXGIFactory2 *pDXGIFactory;
579 if (SUCCEEDED(hr)) {
580 int adapter = atoi(device);
581 if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
583 IDXGIFactory2_Release(pDXGIFactory);
584 }
585 }
586
587 if (pAdapter) {
588 DXGI_ADAPTER_DESC
desc;
589 hr = IDXGIAdapter2_GetDesc(pAdapter, &
desc);
590 if (!FAILED(hr)) {
593 }
594 }
595
596 hr =
mD3D11CreateDevice(pAdapter, pAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE,
NULL, creationFlags,
NULL, 0,
598 if (pAdapter)
599 IDXGIAdapter_Release(pAdapter);
600 if (FAILED(hr)) {
603 }
604
605 hr = ID3D11Device_QueryInterface(device_hwctx->
device, &IID_ID3D10Multithread, (
void **)&pMultithread);
606 if (SUCCEEDED(hr)) {
607 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
608 ID3D10Multithread_Release(pMultithread);
609 }
610
611 #if !HAVE_UWP && HAVE_DXGIDEBUG_H
612 if (is_debug) {
613 HANDLE dxgidebug_dll = LoadLibrary("dxgidebug.dll");
614 if (dxgidebug_dll) {
615 HRESULT (WINAPI * pf_DXGIGetDebugInterface)(const GUID *riid, void **ppDebug)
616 = (void *)GetProcAddress(dxgidebug_dll, "DXGIGetDebugInterface");
617 if (pf_DXGIGetDebugInterface) {
618 IDXGIDebug *dxgi_debug =
NULL;
619 hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (void**)&dxgi_debug);
620 if (SUCCEEDED(hr) && dxgi_debug)
621 IDXGIDebug_ReportLiveObjects(dxgi_debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
622 }
623 }
624 }
625 #endif
626
627 return 0;
628 }
629
632 .name = "D3D11VA",
633
637
648
650 };