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 pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
45 typedef struct DXVA2FramesContext {
46 IDirect3DSurface9 **surfaces_internal;
50 IDirectXVideoAccelerationService *service;
55 typedef struct DXVA2DevicePriv {
62 IDirect3DDevice9 *d3d9device;
67 enum AVPixelFormat pix_fmt;
68 } supported_formats[] = {
69 { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
70 { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
73 DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
74 DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
76 static void dxva2_frames_uninit(AVHWFramesContext *ctx)
78 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
79 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
80 DXVA2FramesContext *s = ctx->internal->priv;
83 if (frames_hwctx->decoder_to_release)
84 IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
86 if (s->surfaces_internal) {
87 for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
88 if (s->surfaces_internal[i])
89 IDirect3DSurface9_Release(s->surfaces_internal[i]);
92 av_freep(&s->surfaces_internal);
95 IDirectXVideoAccelerationService_Release(s->service);
99 if (s->device_handle != INVALID_HANDLE_VALUE) {
100 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
101 s->device_handle = INVALID_HANDLE_VALUE;
105 static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
107 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
108 DXVA2FramesContext *s = ctx->internal->priv;
109 AVDXVA2FramesContext *hwctx = ctx->hwctx;
111 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
112 s->nb_surfaces_used++;
113 return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
114 sizeof(*hwctx->surfaces), NULL, 0, 0);
120 static int dxva2_init_pool(AVHWFramesContext *ctx)
122 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
123 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
124 DXVA2FramesContext *s = ctx->internal->priv;
125 int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
130 if (ctx->initial_pool_size <= 0)
133 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
135 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
136 return AVERROR_UNKNOWN;
139 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
141 decode ? &video_decoder_service : &video_processor_service,
142 (void **)&s->service);
144 av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
145 return AVERROR_UNKNOWN;
148 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
149 if (ctx->sw_format == supported_formats[i].pix_fmt) {
150 s->format = supported_formats[i].d3d_format;
154 if (i == FF_ARRAY_ELEMS(supported_formats)) {
155 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
156 av_get_pix_fmt_name(ctx->sw_format));
157 return AVERROR(EINVAL);
160 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
161 sizeof(*s->surfaces_internal));
162 if (!s->surfaces_internal)
163 return AVERROR(ENOMEM);
165 hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
166 ctx->width, ctx->height,
167 ctx->initial_pool_size - 1,
168 s->format, D3DPOOL_DEFAULT, 0,
169 frames_hwctx->surface_type,
170 s->surfaces_internal, NULL);
172 av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
173 return AVERROR_UNKNOWN;
176 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
177 ctx, dxva2_pool_alloc, NULL);
178 if (!ctx->internal->pool_internal)
179 return AVERROR(ENOMEM);
181 frames_hwctx->surfaces = s->surfaces_internal;
182 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
187 static int dxva2_frames_init(AVHWFramesContext *ctx)
189 AVDXVA2FramesContext *hwctx = ctx->hwctx;
190 DXVA2FramesContext *s = ctx->internal->priv;
193 if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
194 hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
195 av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
196 hwctx->surface_type);
197 return AVERROR(EINVAL);
200 s->device_handle = INVALID_HANDLE_VALUE;
202 /* init the frame pool if the caller didn't provide one */
204 ret = dxva2_init_pool(ctx);
206 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
214 static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
216 frame->buf[0] = av_buffer_pool_get(ctx->pool);
218 return AVERROR(ENOMEM);
220 frame->data[3] = frame->buf[0]->data;
221 frame->format = AV_PIX_FMT_DXVA2_VLD;
222 frame->width = ctx->width;
223 frame->height = ctx->height;
228 static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
229 enum AVHWFrameTransferDirection dir,
230 enum AVPixelFormat **formats)
232 enum AVPixelFormat *fmts;
234 fmts = av_malloc_array(2, sizeof(*fmts));
236 return AVERROR(ENOMEM);
238 fmts[0] = ctx->sw_format;
239 fmts[1] = AV_PIX_FMT_NONE;
246 static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
249 IDirect3DSurface9 *surface;
250 D3DSURFACE_DESC surfaceDesc;
251 D3DLOCKED_RECT LockedRect;
254 uint8_t *surf_data[4] = { NULL };
255 int surf_linesize[4] = { 0 };
258 int download = !!src->hw_frames_ctx;
260 surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
262 hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
264 av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
265 return AVERROR_UNKNOWN;
268 hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
269 download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
271 av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
272 return AVERROR_UNKNOWN;
275 for (i = 0; download ? dst->data[i] : src->data[i]; i++)
276 surf_linesize[i] = LockedRect.Pitch;
278 av_image_fill_pointers(surf_data, ctx->sw_format, surfaceDesc.Height,
279 (uint8_t*)LockedRect.pBits, surf_linesize);
282 av_image_copy(dst->data, dst->linesize, surf_data, surf_linesize,
283 ctx->sw_format, src->width, src->height);
285 av_image_copy(surf_data, surf_linesize, src->data, src->linesize,
286 ctx->sw_format, src->width, src->height);
289 IDirect3DSurface9_UnlockRect(surface);
294 static void dxva2_device_free(AVHWDeviceContext *ctx)
296 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
297 DXVA2DevicePriv *priv = ctx->user_opaque;
299 if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
300 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
303 IDirect3DDeviceManager9_Release(hwctx->devmgr);
305 if (priv->d3d9device)
306 IDirect3DDevice9_Release(priv->d3d9device);
309 IDirect3D9_Release(priv->d3d9);
312 dlclose(priv->d3dlib);
315 dlclose(priv->dxva2lib);
317 av_freep(&ctx->user_opaque);
320 static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
321 AVDictionary *opts, int flags)
323 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
324 DXVA2DevicePriv *priv;
326 pDirect3DCreate9 *createD3D = NULL;
327 pCreateDeviceManager9 *createDeviceManager = NULL;
328 D3DPRESENT_PARAMETERS d3dpp = {0};
329 D3DDISPLAYMODE d3ddm;
330 unsigned resetToken = 0;
331 UINT adapter = D3DADAPTER_DEFAULT;
335 adapter = atoi(device);
337 priv = av_mallocz(sizeof(*priv));
339 return AVERROR(ENOMEM);
341 ctx->user_opaque = priv;
342 ctx->free = dxva2_device_free;
344 priv->device_handle = INVALID_HANDLE_VALUE;
346 priv->d3dlib = dlopen("d3d9.dll", 0);
348 av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
349 return AVERROR_UNKNOWN;
351 priv->dxva2lib = dlopen("dxva2.dll", 0);
352 if (!priv->dxva2lib) {
353 av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
354 return AVERROR_UNKNOWN;
357 createD3D = (pDirect3DCreate9 *)dlsym(priv->d3dlib, "Direct3DCreate9");
359 av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
360 return AVERROR_UNKNOWN;
362 createDeviceManager = (pCreateDeviceManager9 *)dlsym(priv->dxva2lib,
363 "DXVA2CreateDirect3DDeviceManager9");
364 if (!createDeviceManager) {
365 av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
366 return AVERROR_UNKNOWN;
369 priv->d3d9 = createD3D(D3D_SDK_VERSION);
371 av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
372 return AVERROR_UNKNOWN;
375 IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
376 d3dpp.Windowed = TRUE;
377 d3dpp.BackBufferWidth = 640;
378 d3dpp.BackBufferHeight = 480;
379 d3dpp.BackBufferCount = 0;
380 d3dpp.BackBufferFormat = d3ddm.Format;
381 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
382 d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
384 hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
385 D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
386 &d3dpp, &priv->d3d9device);
388 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
389 return AVERROR_UNKNOWN;
392 hr = createDeviceManager(&resetToken, &hwctx->devmgr);
394 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
395 return AVERROR_UNKNOWN;
398 hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
400 av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
401 return AVERROR_UNKNOWN;
404 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
406 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
407 return AVERROR_UNKNOWN;
413 const HWContextType ff_hwcontext_type_dxva2 = {
414 .type = AV_HWDEVICE_TYPE_DXVA2,
417 .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
418 .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
419 .frames_priv_size = sizeof(DXVA2FramesContext),
421 .device_create = dxva2_device_create,
422 .frames_init = dxva2_frames_init,
423 .frames_uninit = dxva2_frames_uninit,
424 .frames_get_buffer = dxva2_get_buffer,
425 .transfer_get_formats = dxva2_transfer_get_formats,
426 .transfer_data_to = dxva2_transfer_data,
427 .transfer_data_from = dxva2_transfer_data,
429 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },