2 * This file is part of FFmpeg.
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.
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.
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
21 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
23 #define _WIN32_WINNT 0x0600
25 #define DXVA2API_USE_BITFIELDS
34 #include "hwcontext.h"
35 #include "hwcontext_dxva2.h"
36 #include "hwcontext_internal.h"
40 #include "compat/w32dlfcn.h"
42 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
43 typedef HRESULT WINAPI pDirect3DCreate9Ex(UINT, IDirect3D9Ex **);
44 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
46 #define FF_D3DCREATE_FLAGS (D3DCREATE_SOFTWARE_VERTEXPROCESSING | \
47 D3DCREATE_MULTITHREADED | \
48 D3DCREATE_FPU_PRESERVE)
50 static const D3DPRESENT_PARAMETERS dxva2_present_params = {
52 .BackBufferWidth = 640,
53 .BackBufferHeight = 480,
55 .SwapEffect = D3DSWAPEFFECT_DISCARD,
56 .Flags = D3DPRESENTFLAG_VIDEO,
59 typedef struct DXVA2Mapping {
60 uint32_t palette_dummy[256];
63 typedef struct DXVA2FramesContext {
64 IDirect3DSurface9 **surfaces_internal;
68 IDirectXVideoAccelerationService *service;
73 typedef struct DXVA2DevicePriv {
80 IDirect3DDevice9 *d3d9device;
85 enum AVPixelFormat pix_fmt;
86 } supported_formats[] = {
87 { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
88 { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
89 { D3DFMT_P8, AV_PIX_FMT_PAL8 },
92 DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
93 DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
95 static void dxva2_frames_uninit(AVHWFramesContext *ctx)
97 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
98 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
99 DXVA2FramesContext *s = ctx->internal->priv;
102 if (frames_hwctx->decoder_to_release)
103 IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
105 if (s->surfaces_internal) {
106 for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
107 if (s->surfaces_internal[i])
108 IDirect3DSurface9_Release(s->surfaces_internal[i]);
111 av_freep(&s->surfaces_internal);
114 IDirectXVideoAccelerationService_Release(s->service);
118 if (s->device_handle != INVALID_HANDLE_VALUE) {
119 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
120 s->device_handle = INVALID_HANDLE_VALUE;
124 static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
126 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
127 DXVA2FramesContext *s = ctx->internal->priv;
128 AVDXVA2FramesContext *hwctx = ctx->hwctx;
130 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
131 s->nb_surfaces_used++;
132 return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
133 sizeof(*hwctx->surfaces), NULL, 0, 0);
139 static int dxva2_init_pool(AVHWFramesContext *ctx)
141 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
142 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
143 DXVA2FramesContext *s = ctx->internal->priv;
144 int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
149 if (ctx->initial_pool_size <= 0)
152 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
154 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
155 return AVERROR_UNKNOWN;
158 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
160 decode ? &video_decoder_service : &video_processor_service,
161 (void **)&s->service);
163 av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
164 return AVERROR_UNKNOWN;
167 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
168 if (ctx->sw_format == supported_formats[i].pix_fmt) {
169 s->format = supported_formats[i].d3d_format;
173 if (i == FF_ARRAY_ELEMS(supported_formats)) {
174 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
175 av_get_pix_fmt_name(ctx->sw_format));
176 return AVERROR(EINVAL);
179 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
180 sizeof(*s->surfaces_internal));
181 if (!s->surfaces_internal)
182 return AVERROR(ENOMEM);
184 hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
185 ctx->width, ctx->height,
186 ctx->initial_pool_size - 1,
187 s->format, D3DPOOL_DEFAULT, 0,
188 frames_hwctx->surface_type,
189 s->surfaces_internal, NULL);
191 av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
192 return AVERROR_UNKNOWN;
195 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
196 ctx, dxva2_pool_alloc, NULL);
197 if (!ctx->internal->pool_internal)
198 return AVERROR(ENOMEM);
200 frames_hwctx->surfaces = s->surfaces_internal;
201 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
206 static int dxva2_frames_init(AVHWFramesContext *ctx)
208 AVDXVA2FramesContext *hwctx = ctx->hwctx;
209 DXVA2FramesContext *s = ctx->internal->priv;
212 if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
213 hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
214 av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
215 hwctx->surface_type);
216 return AVERROR(EINVAL);
219 s->device_handle = INVALID_HANDLE_VALUE;
221 /* init the frame pool if the caller didn't provide one */
223 ret = dxva2_init_pool(ctx);
225 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
233 static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
235 frame->buf[0] = av_buffer_pool_get(ctx->pool);
237 return AVERROR(ENOMEM);
239 frame->data[3] = frame->buf[0]->data;
240 frame->format = AV_PIX_FMT_DXVA2_VLD;
241 frame->width = ctx->width;
242 frame->height = ctx->height;
247 static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
248 enum AVHWFrameTransferDirection dir,
249 enum AVPixelFormat **formats)
251 enum AVPixelFormat *fmts;
253 fmts = av_malloc_array(2, sizeof(*fmts));
255 return AVERROR(ENOMEM);
257 fmts[0] = ctx->sw_format;
258 fmts[1] = AV_PIX_FMT_NONE;
265 static void dxva2_unmap_frame(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
267 IDirect3DSurface9 *surface = (IDirect3DSurface9*)hwmap->source->data[3];
268 IDirect3DSurface9_UnlockRect(surface);
269 av_freep(&hwmap->priv);
272 static int dxva2_map_frame(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src,
275 IDirect3DSurface9 *surface = (IDirect3DSurface9*)src->data[3];
277 D3DSURFACE_DESC surfaceDesc;
278 D3DLOCKED_RECT LockedRect;
280 int i, err, nb_planes;
282 nb_planes = av_pix_fmt_count_planes(dst->format);
284 hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
286 av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
287 return AVERROR_UNKNOWN;
290 hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
291 flags & AV_HWFRAME_MAP_READ ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
293 av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
294 return AVERROR_UNKNOWN;
297 map = av_mallocz(sizeof(*map));
301 err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
302 dxva2_unmap_frame, map);
308 for (i = 0; i < nb_planes; i++)
309 dst->linesize[i] = LockedRect.Pitch;
311 av_image_fill_pointers(dst->data, dst->format, surfaceDesc.Height,
312 (uint8_t*)LockedRect.pBits, dst->linesize);
314 if (dst->format == AV_PIX_FMT_PAL8)
315 dst->data[1] = (uint8_t*)map->palette_dummy;
319 IDirect3DSurface9_UnlockRect(surface);
323 static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
326 int download = !!src->hw_frames_ctx;
331 map = av_frame_alloc();
333 return AVERROR(ENOMEM);
334 map->format = dst->format;
336 ret = dxva2_map_frame(ctx, map, download ? src : dst,
337 download ? AV_HWFRAME_MAP_READ : AV_HWFRAME_MAP_WRITE);
342 ptrdiff_t src_linesize[4], dst_linesize[4];
343 for (i = 0; i < 4; i++) {
344 dst_linesize[i] = dst->linesize[i];
345 src_linesize[i] = map->linesize[i];
347 av_image_copy_uc_from(dst->data, dst_linesize, map->data, src_linesize,
348 ctx->sw_format, src->width, src->height);
350 av_image_copy(map->data, map->linesize, src->data, src->linesize,
351 ctx->sw_format, src->width, src->height);
359 static int dxva2_map_from(AVHWFramesContext *ctx,
360 AVFrame *dst, const AVFrame *src, int flags)
364 err = dxva2_map_frame(ctx, dst, src, flags);
368 err = av_frame_copy_props(dst, src);
375 static void dxva2_device_free(AVHWDeviceContext *ctx)
377 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
378 DXVA2DevicePriv *priv = ctx->user_opaque;
380 if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
381 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
384 IDirect3DDeviceManager9_Release(hwctx->devmgr);
386 if (priv->d3d9device)
387 IDirect3DDevice9_Release(priv->d3d9device);
390 IDirect3D9_Release(priv->d3d9);
393 dlclose(priv->d3dlib);
396 dlclose(priv->dxva2lib);
398 av_freep(&ctx->user_opaque);
401 static int dxva2_device_create9(AVHWDeviceContext *ctx, UINT adapter)
403 DXVA2DevicePriv *priv = ctx->user_opaque;
404 D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
405 D3DDISPLAYMODE d3ddm;
407 pDirect3DCreate9 *createD3D = (pDirect3DCreate9 *)dlsym(priv->d3dlib, "Direct3DCreate9");
409 av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
410 return AVERROR_UNKNOWN;
413 priv->d3d9 = createD3D(D3D_SDK_VERSION);
415 av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
416 return AVERROR_UNKNOWN;
419 IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
421 d3dpp.BackBufferFormat = d3ddm.Format;
423 hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
425 &d3dpp, &priv->d3d9device);
427 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
428 return AVERROR_UNKNOWN;
434 static int dxva2_device_create9ex(AVHWDeviceContext *ctx, UINT adapter)
436 DXVA2DevicePriv *priv = ctx->user_opaque;
437 D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
438 D3DDISPLAYMODEEX modeex = {0};
439 IDirect3D9Ex *d3d9ex = NULL;
440 IDirect3DDevice9Ex *exdev = NULL;
442 pDirect3DCreate9Ex *createD3DEx = (pDirect3DCreate9Ex *)dlsym(priv->d3dlib, "Direct3DCreate9Ex");
444 return AVERROR(ENOSYS);
446 hr = createD3DEx(D3D_SDK_VERSION, &d3d9ex);
448 return AVERROR_UNKNOWN;
450 IDirect3D9Ex_GetAdapterDisplayModeEx(d3d9ex, adapter, &modeex, NULL);
452 d3dpp.BackBufferFormat = modeex.Format;
454 hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
456 &d3dpp, NULL, &exdev);
458 IDirect3D9Ex_Release(d3d9ex);
459 return AVERROR_UNKNOWN;
462 av_log(ctx, AV_LOG_VERBOSE, "Using D3D9Ex device.\n");
463 priv->d3d9 = (IDirect3D9 *)d3d9ex;
464 priv->d3d9device = (IDirect3DDevice9 *)exdev;
468 static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
469 AVDictionary *opts, int flags)
471 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
472 DXVA2DevicePriv *priv;
473 pCreateDeviceManager9 *createDeviceManager = NULL;
474 unsigned resetToken = 0;
475 UINT adapter = D3DADAPTER_DEFAULT;
480 adapter = atoi(device);
482 priv = av_mallocz(sizeof(*priv));
484 return AVERROR(ENOMEM);
486 ctx->user_opaque = priv;
487 ctx->free = dxva2_device_free;
489 priv->device_handle = INVALID_HANDLE_VALUE;
491 priv->d3dlib = dlopen("d3d9.dll", 0);
493 av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
494 return AVERROR_UNKNOWN;
496 priv->dxva2lib = dlopen("dxva2.dll", 0);
497 if (!priv->dxva2lib) {
498 av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
499 return AVERROR_UNKNOWN;
502 createDeviceManager = (pCreateDeviceManager9 *)dlsym(priv->dxva2lib,
503 "DXVA2CreateDirect3DDeviceManager9");
504 if (!createDeviceManager) {
505 av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
506 return AVERROR_UNKNOWN;
509 if (dxva2_device_create9ex(ctx, adapter) < 0) {
510 // Retry with "classic" d3d9
511 err = dxva2_device_create9(ctx, adapter);
516 hr = createDeviceManager(&resetToken, &hwctx->devmgr);
518 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
519 return AVERROR_UNKNOWN;
522 hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
524 av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
525 return AVERROR_UNKNOWN;
528 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
530 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
531 return AVERROR_UNKNOWN;
537 const HWContextType ff_hwcontext_type_dxva2 = {
538 .type = AV_HWDEVICE_TYPE_DXVA2,
541 .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
542 .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
543 .frames_priv_size = sizeof(DXVA2FramesContext),
545 .device_create = dxva2_device_create,
546 .frames_init = dxva2_frames_init,
547 .frames_uninit = dxva2_frames_uninit,
548 .frames_get_buffer = dxva2_get_buffer,
549 .transfer_get_formats = dxva2_transfer_get_formats,
550 .transfer_data_to = dxva2_transfer_data,
551 .transfer_data_from = dxva2_transfer_data,
552 .map_from = dxva2_map_from,
554 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },