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 int download = !!src->hw_frames_ctx;
255 int bytes_per_component;
257 switch (ctx->sw_format) {
258 case AV_PIX_FMT_NV12:
259 bytes_per_component = 1;
261 case AV_PIX_FMT_P010:
262 bytes_per_component = 2;
268 surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
270 hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
272 av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
273 return AVERROR_UNKNOWN;
276 hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
277 download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
279 av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
280 return AVERROR_UNKNOWN;
284 av_image_copy_plane(dst->data[0], dst->linesize[0],
285 (uint8_t*)LockedRect.pBits, LockedRect.Pitch,
286 src->width * bytes_per_component, src->height);
287 av_image_copy_plane(dst->data[1], dst->linesize[1],
288 (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
289 LockedRect.Pitch, src->width * bytes_per_component, src->height / 2);
291 av_image_copy_plane((uint8_t*)LockedRect.pBits, LockedRect.Pitch,
292 dst->data[0], dst->linesize[0],
293 src->width * bytes_per_component, src->height);
294 av_image_copy_plane((uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
295 LockedRect.Pitch, dst->data[1], dst->linesize[1],
296 src->width * bytes_per_component, src->height / 2);
299 IDirect3DSurface9_UnlockRect(surface);
304 static void dxva2_device_free(AVHWDeviceContext *ctx)
306 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
307 DXVA2DevicePriv *priv = ctx->user_opaque;
309 if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
310 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
313 IDirect3DDeviceManager9_Release(hwctx->devmgr);
315 if (priv->d3d9device)
316 IDirect3DDevice9_Release(priv->d3d9device);
319 IDirect3D9_Release(priv->d3d9);
322 dlclose(priv->d3dlib);
325 dlclose(priv->dxva2lib);
327 av_freep(&ctx->user_opaque);
330 static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
331 AVDictionary *opts, int flags)
333 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
334 DXVA2DevicePriv *priv;
336 pDirect3DCreate9 *createD3D = NULL;
337 pCreateDeviceManager9 *createDeviceManager = NULL;
338 D3DPRESENT_PARAMETERS d3dpp = {0};
339 D3DDISPLAYMODE d3ddm;
340 unsigned resetToken = 0;
341 UINT adapter = D3DADAPTER_DEFAULT;
345 adapter = atoi(device);
347 priv = av_mallocz(sizeof(*priv));
349 return AVERROR(ENOMEM);
351 ctx->user_opaque = priv;
352 ctx->free = dxva2_device_free;
354 priv->device_handle = INVALID_HANDLE_VALUE;
356 priv->d3dlib = dlopen("d3d9.dll", 0);
358 av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
359 return AVERROR_UNKNOWN;
361 priv->dxva2lib = dlopen("dxva2.dll", 0);
362 if (!priv->dxva2lib) {
363 av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
364 return AVERROR_UNKNOWN;
367 createD3D = (pDirect3DCreate9 *)dlsym(priv->d3dlib, "Direct3DCreate9");
369 av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
370 return AVERROR_UNKNOWN;
372 createDeviceManager = (pCreateDeviceManager9 *)dlsym(priv->dxva2lib,
373 "DXVA2CreateDirect3DDeviceManager9");
374 if (!createDeviceManager) {
375 av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
376 return AVERROR_UNKNOWN;
379 priv->d3d9 = createD3D(D3D_SDK_VERSION);
381 av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
382 return AVERROR_UNKNOWN;
385 IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
386 d3dpp.Windowed = TRUE;
387 d3dpp.BackBufferWidth = 640;
388 d3dpp.BackBufferHeight = 480;
389 d3dpp.BackBufferCount = 0;
390 d3dpp.BackBufferFormat = d3ddm.Format;
391 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
392 d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
394 hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
395 D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
396 &d3dpp, &priv->d3d9device);
398 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
399 return AVERROR_UNKNOWN;
402 hr = createDeviceManager(&resetToken, &hwctx->devmgr);
404 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
405 return AVERROR_UNKNOWN;
408 hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
410 av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
411 return AVERROR_UNKNOWN;
414 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
416 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
417 return AVERROR_UNKNOWN;
423 const HWContextType ff_hwcontext_type_dxva2 = {
424 .type = AV_HWDEVICE_TYPE_DXVA2,
427 .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
428 .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
429 .frames_priv_size = sizeof(DXVA2FramesContext),
431 .device_create = dxva2_device_create,
432 .frames_init = dxva2_frames_init,
433 .frames_uninit = dxva2_frames_uninit,
434 .frames_get_buffer = dxva2_get_buffer,
435 .transfer_get_formats = dxva2_transfer_get_formats,
436 .transfer_data_to = dxva2_transfer_data,
437 .transfer_data_from = dxva2_transfer_data,
439 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },