2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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
33 #include "hwcontext.h"
34 #include "hwcontext_dxva2.h"
35 #include "hwcontext_internal.h"
40 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
41 typedef HRESULT WINAPI pDirect3DCreate9Ex(UINT, IDirect3D9Ex **);
42 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
44 #define FF_D3DCREATE_FLAGS (D3DCREATE_SOFTWARE_VERTEXPROCESSING | \
45 D3DCREATE_MULTITHREADED | \
46 D3DCREATE_FPU_PRESERVE)
48 static const D3DPRESENT_PARAMETERS dxva2_present_params = {
50 .BackBufferWidth = 640,
51 .BackBufferHeight = 480,
53 .SwapEffect = D3DSWAPEFFECT_DISCARD,
54 .Flags = D3DPRESENTFLAG_VIDEO,
57 typedef struct DXVA2Mapping {
58 uint32_t palette_dummy[256];
61 typedef struct DXVA2FramesContext {
62 IDirect3DSurface9 **surfaces_internal;
66 IDirectXVideoAccelerationService *service;
71 typedef struct DXVA2DevicePriv {
78 IDirect3DDevice9 *d3d9device;
83 enum AVPixelFormat pix_fmt;
84 } supported_formats[] = {
85 { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
86 { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
87 { D3DFMT_P8, AV_PIX_FMT_PAL8 },
90 DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
91 DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
93 static void dxva2_frames_uninit(AVHWFramesContext *ctx)
95 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
96 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
97 DXVA2FramesContext *s = ctx->internal->priv;
100 if (frames_hwctx->decoder_to_release)
101 IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
103 if (s->surfaces_internal) {
104 for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
105 if (s->surfaces_internal[i])
106 IDirect3DSurface9_Release(s->surfaces_internal[i]);
109 av_freep(&s->surfaces_internal);
112 IDirectXVideoAccelerationService_Release(s->service);
116 if (s->device_handle != INVALID_HANDLE_VALUE) {
117 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
118 s->device_handle = INVALID_HANDLE_VALUE;
122 static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
124 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
125 DXVA2FramesContext *s = ctx->internal->priv;
126 AVDXVA2FramesContext *hwctx = ctx->hwctx;
128 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
129 s->nb_surfaces_used++;
130 return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
131 sizeof(*hwctx->surfaces), NULL, 0, 0);
137 static int dxva2_init_pool(AVHWFramesContext *ctx)
139 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
140 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
141 DXVA2FramesContext *s = ctx->internal->priv;
142 int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
147 if (ctx->initial_pool_size <= 0)
150 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
152 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
153 return AVERROR_UNKNOWN;
156 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
158 decode ? &video_decoder_service : &video_processor_service,
159 (void **)&s->service);
161 av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
162 return AVERROR_UNKNOWN;
165 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
166 if (ctx->sw_format == supported_formats[i].pix_fmt) {
167 s->format = supported_formats[i].d3d_format;
171 if (i == FF_ARRAY_ELEMS(supported_formats)) {
172 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
173 av_get_pix_fmt_name(ctx->sw_format));
174 return AVERROR(EINVAL);
177 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
178 sizeof(*s->surfaces_internal));
179 if (!s->surfaces_internal)
180 return AVERROR(ENOMEM);
182 hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
183 ctx->width, ctx->height,
184 ctx->initial_pool_size - 1,
185 s->format, D3DPOOL_DEFAULT, 0,
186 frames_hwctx->surface_type,
187 s->surfaces_internal, NULL);
189 av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
190 return AVERROR_UNKNOWN;
193 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
194 ctx, dxva2_pool_alloc, NULL);
195 if (!ctx->internal->pool_internal)
196 return AVERROR(ENOMEM);
198 frames_hwctx->surfaces = s->surfaces_internal;
199 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
204 static int dxva2_frames_init(AVHWFramesContext *ctx)
206 AVDXVA2FramesContext *hwctx = ctx->hwctx;
207 DXVA2FramesContext *s = ctx->internal->priv;
210 if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
211 hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
212 av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
213 hwctx->surface_type);
214 return AVERROR(EINVAL);
217 s->device_handle = INVALID_HANDLE_VALUE;
219 /* init the frame pool if the caller didn't provide one */
221 ret = dxva2_init_pool(ctx);
223 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
231 static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
233 frame->buf[0] = av_buffer_pool_get(ctx->pool);
235 return AVERROR(ENOMEM);
237 frame->data[3] = frame->buf[0]->data;
238 frame->format = AV_PIX_FMT_DXVA2_VLD;
239 frame->width = ctx->width;
240 frame->height = ctx->height;
245 static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
246 enum AVHWFrameTransferDirection dir,
247 enum AVPixelFormat **formats)
249 enum AVPixelFormat *fmts;
251 fmts = av_malloc_array(2, sizeof(*fmts));
253 return AVERROR(ENOMEM);
255 fmts[0] = ctx->sw_format;
256 fmts[1] = AV_PIX_FMT_NONE;
263 static void dxva2_unmap_frame(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
265 IDirect3DSurface9 *surface = (IDirect3DSurface9*)hwmap->source->data[3];
266 IDirect3DSurface9_UnlockRect(surface);
267 av_freep(&hwmap->priv);
270 static int dxva2_map_frame(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src,
273 IDirect3DSurface9 *surface = (IDirect3DSurface9*)src->data[3];
275 D3DSURFACE_DESC surfaceDesc;
276 D3DLOCKED_RECT LockedRect;
278 int i, err, nb_planes;
281 nb_planes = av_pix_fmt_count_planes(dst->format);
283 hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
285 av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
286 return AVERROR_UNKNOWN;
289 if (!(flags & AV_HWFRAME_MAP_WRITE))
290 lock_flags |= D3DLOCK_READONLY;
291 if (flags & AV_HWFRAME_MAP_OVERWRITE)
292 lock_flags |= D3DLOCK_DISCARD;
294 hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, lock_flags);
296 av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
297 return AVERROR_UNKNOWN;
300 map = av_mallocz(sizeof(*map));
304 err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
305 dxva2_unmap_frame, map);
311 for (i = 0; i < nb_planes; i++)
312 dst->linesize[i] = LockedRect.Pitch;
314 av_image_fill_pointers(dst->data, dst->format, surfaceDesc.Height,
315 (uint8_t*)LockedRect.pBits, dst->linesize);
317 if (dst->format == AV_PIX_FMT_PAL8)
318 dst->data[1] = (uint8_t*)map->palette_dummy;
322 IDirect3DSurface9_UnlockRect(surface);
326 static int dxva2_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
332 if (src->format != ctx->sw_format)
333 return AVERROR(ENOSYS);
335 map = av_frame_alloc();
337 return AVERROR(ENOMEM);
338 map->format = dst->format;
340 ret = dxva2_map_frame(ctx, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
344 av_image_copy(map->data, map->linesize, src->data, src->linesize,
345 ctx->sw_format, src->width, src->height);
352 static int dxva2_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
356 ptrdiff_t src_linesize[4], dst_linesize[4];
359 if (dst->format != ctx->sw_format)
360 return AVERROR(ENOSYS);
362 map = av_frame_alloc();
364 return AVERROR(ENOMEM);
365 map->format = dst->format;
367 ret = dxva2_map_frame(ctx, map, src, AV_HWFRAME_MAP_READ);
371 for (i = 0; i < 4; i++) {
372 dst_linesize[i] = dst->linesize[i];
373 src_linesize[i] = map->linesize[i];
375 av_image_copy_uc_from(dst->data, dst_linesize, map->data, src_linesize,
376 ctx->sw_format, src->width, src->height);
382 static int dxva2_map_from(AVHWFramesContext *ctx,
383 AVFrame *dst, const AVFrame *src, int flags)
387 if (dst->format != AV_PIX_FMT_NONE && dst->format != ctx->sw_format)
388 return AVERROR(ENOSYS);
389 dst->format = ctx->sw_format;
391 err = dxva2_map_frame(ctx, dst, src, flags);
395 err = av_frame_copy_props(dst, src);
402 static void dxva2_device_free(AVHWDeviceContext *ctx)
404 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
405 DXVA2DevicePriv *priv = ctx->user_opaque;
407 if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
408 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
411 IDirect3DDeviceManager9_Release(hwctx->devmgr);
413 if (priv->d3d9device)
414 IDirect3DDevice9_Release(priv->d3d9device);
417 IDirect3D9_Release(priv->d3d9);
420 FreeLibrary(priv->d3dlib);
423 FreeLibrary(priv->dxva2lib);
425 av_freep(&ctx->user_opaque);
428 static int dxva2_device_create9(AVHWDeviceContext *ctx, UINT adapter)
430 DXVA2DevicePriv *priv = ctx->user_opaque;
431 D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
432 D3DDISPLAYMODE d3ddm;
434 pDirect3DCreate9 *createD3D = (pDirect3DCreate9 *)GetProcAddress(priv->d3dlib, "Direct3DCreate9");
436 av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
437 return AVERROR_UNKNOWN;
440 priv->d3d9 = createD3D(D3D_SDK_VERSION);
442 av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
443 return AVERROR_UNKNOWN;
446 IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
448 d3dpp.BackBufferFormat = d3ddm.Format;
450 hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
452 &d3dpp, &priv->d3d9device);
454 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
455 return AVERROR_UNKNOWN;
461 static int dxva2_device_create9ex(AVHWDeviceContext *ctx, UINT adapter)
463 DXVA2DevicePriv *priv = ctx->user_opaque;
464 D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
465 D3DDISPLAYMODEEX modeex = {0};
466 IDirect3D9Ex *d3d9ex = NULL;
467 IDirect3DDevice9Ex *exdev = NULL;
469 pDirect3DCreate9Ex *createD3DEx = (pDirect3DCreate9Ex *)GetProcAddress(priv->d3dlib, "Direct3DCreate9Ex");
471 return AVERROR(ENOSYS);
473 hr = createD3DEx(D3D_SDK_VERSION, &d3d9ex);
475 return AVERROR_UNKNOWN;
477 IDirect3D9Ex_GetAdapterDisplayModeEx(d3d9ex, adapter, &modeex, NULL);
479 d3dpp.BackBufferFormat = modeex.Format;
481 hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
483 &d3dpp, NULL, &exdev);
485 IDirect3D9Ex_Release(d3d9ex);
486 return AVERROR_UNKNOWN;
489 av_log(ctx, AV_LOG_VERBOSE, "Using D3D9Ex device.\n");
490 priv->d3d9 = (IDirect3D9 *)d3d9ex;
491 priv->d3d9device = (IDirect3DDevice9 *)exdev;
495 static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
496 AVDictionary *opts, int flags)
498 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
499 DXVA2DevicePriv *priv;
500 pCreateDeviceManager9 *createDeviceManager = NULL;
501 unsigned resetToken = 0;
502 UINT adapter = D3DADAPTER_DEFAULT;
507 adapter = atoi(device);
509 priv = av_mallocz(sizeof(*priv));
511 return AVERROR(ENOMEM);
513 ctx->user_opaque = priv;
514 ctx->free = dxva2_device_free;
516 priv->device_handle = INVALID_HANDLE_VALUE;
518 priv->d3dlib = LoadLibrary("d3d9.dll");
520 av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
521 return AVERROR_UNKNOWN;
523 priv->dxva2lib = LoadLibrary("dxva2.dll");
524 if (!priv->dxva2lib) {
525 av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
526 return AVERROR_UNKNOWN;
529 createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(priv->dxva2lib,
530 "DXVA2CreateDirect3DDeviceManager9");
531 if (!createDeviceManager) {
532 av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
533 return AVERROR_UNKNOWN;
536 if (dxva2_device_create9ex(ctx, adapter) < 0) {
537 // Retry with "classic" d3d9
538 err = dxva2_device_create9(ctx, adapter);
543 hr = createDeviceManager(&resetToken, &hwctx->devmgr);
545 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
546 return AVERROR_UNKNOWN;
549 hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
551 av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
552 return AVERROR_UNKNOWN;
555 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
557 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
558 return AVERROR_UNKNOWN;
564 const HWContextType ff_hwcontext_type_dxva2 = {
565 .type = AV_HWDEVICE_TYPE_DXVA2,
568 .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
569 .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
570 .frames_priv_size = sizeof(DXVA2FramesContext),
572 .device_create = dxva2_device_create,
573 .frames_init = dxva2_frames_init,
574 .frames_uninit = dxva2_frames_uninit,
575 .frames_get_buffer = dxva2_get_buffer,
576 .transfer_get_formats = dxva2_transfer_get_formats,
577 .transfer_data_to = dxva2_transfer_data_to,
578 .transfer_data_from = dxva2_transfer_data_from,
579 .map_from = dxva2_map_from,
581 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },