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"
41 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
42 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
44 typedef struct DXVA2FramesContext {
45 IDirect3DSurface9 **surfaces_internal;
49 IDirectXVideoAccelerationService *service;
54 typedef struct DXVA2DevicePriv {
61 IDirect3DDevice9 *d3d9device;
66 enum AVPixelFormat pix_fmt;
67 } supported_formats[] = {
68 { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
69 { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
72 DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
73 DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
75 static void dxva2_frames_uninit(AVHWFramesContext *ctx)
77 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
78 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
79 DXVA2FramesContext *s = ctx->internal->priv;
82 if (frames_hwctx->decoder_to_release)
83 IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
85 if (s->surfaces_internal) {
86 for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
87 if (s->surfaces_internal[i])
88 IDirect3DSurface9_Release(s->surfaces_internal[i]);
91 av_freep(&s->surfaces_internal);
94 IDirectXVideoAccelerationService_Release(s->service);
98 if (s->device_handle != INVALID_HANDLE_VALUE) {
99 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
100 s->device_handle = INVALID_HANDLE_VALUE;
104 static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
106 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
107 DXVA2FramesContext *s = ctx->internal->priv;
108 AVDXVA2FramesContext *hwctx = ctx->hwctx;
110 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
111 s->nb_surfaces_used++;
112 return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
113 sizeof(*hwctx->surfaces), NULL, 0, 0);
119 static int dxva2_init_pool(AVHWFramesContext *ctx)
121 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
122 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
123 DXVA2FramesContext *s = ctx->internal->priv;
124 int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
129 if (ctx->initial_pool_size <= 0)
132 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
134 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
135 return AVERROR_UNKNOWN;
138 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
140 decode ? &video_decoder_service : &video_processor_service,
141 (void **)&s->service);
143 av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
144 return AVERROR_UNKNOWN;
147 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
148 if (ctx->sw_format == supported_formats[i].pix_fmt) {
149 s->format = supported_formats[i].d3d_format;
153 if (i == FF_ARRAY_ELEMS(supported_formats)) {
154 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
155 av_get_pix_fmt_name(ctx->sw_format));
156 return AVERROR(EINVAL);
159 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
160 sizeof(*s->surfaces_internal));
161 if (!s->surfaces_internal)
162 return AVERROR(ENOMEM);
164 hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
165 ctx->width, ctx->height,
166 ctx->initial_pool_size - 1,
167 s->format, D3DPOOL_DEFAULT, 0,
168 frames_hwctx->surface_type,
169 s->surfaces_internal, NULL);
171 av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
172 return AVERROR_UNKNOWN;
175 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
176 ctx, dxva2_pool_alloc, NULL);
177 if (!ctx->internal->pool_internal)
178 return AVERROR(ENOMEM);
180 frames_hwctx->surfaces = s->surfaces_internal;
181 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
186 static int dxva2_frames_init(AVHWFramesContext *ctx)
188 AVDXVA2FramesContext *hwctx = ctx->hwctx;
189 DXVA2FramesContext *s = ctx->internal->priv;
192 if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
193 hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
194 av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
195 hwctx->surface_type);
196 return AVERROR(EINVAL);
199 s->device_handle = INVALID_HANDLE_VALUE;
201 /* init the frame pool if the caller didn't provide one */
203 ret = dxva2_init_pool(ctx);
205 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
213 static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
215 frame->buf[0] = av_buffer_pool_get(ctx->pool);
217 return AVERROR(ENOMEM);
219 frame->data[3] = frame->buf[0]->data;
220 frame->format = AV_PIX_FMT_DXVA2_VLD;
221 frame->width = ctx->width;
222 frame->height = ctx->height;
227 static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
228 enum AVHWFrameTransferDirection dir,
229 enum AVPixelFormat **formats)
231 enum AVPixelFormat *fmts;
233 fmts = av_malloc_array(2, sizeof(*fmts));
235 return AVERROR(ENOMEM);
237 fmts[0] = ctx->sw_format;
238 fmts[1] = AV_PIX_FMT_NONE;
245 static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
248 IDirect3DSurface9 *surface;
249 D3DSURFACE_DESC surfaceDesc;
250 D3DLOCKED_RECT LockedRect;
253 int download = !!src->hw_frames_ctx;
254 int bytes_per_component;
256 switch (ctx->sw_format) {
257 case AV_PIX_FMT_NV12:
258 bytes_per_component = 1;
260 case AV_PIX_FMT_P010:
261 bytes_per_component = 2;
267 surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
269 hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
271 av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
272 return AVERROR_UNKNOWN;
275 hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
276 download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
278 av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
279 return AVERROR_UNKNOWN;
283 av_image_copy_plane(dst->data[0], dst->linesize[0],
284 (uint8_t*)LockedRect.pBits, LockedRect.Pitch,
285 src->width * bytes_per_component, src->height);
286 av_image_copy_plane(dst->data[1], dst->linesize[1],
287 (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
288 LockedRect.Pitch, src->width * bytes_per_component, src->height / 2);
290 av_image_copy_plane((uint8_t*)LockedRect.pBits, LockedRect.Pitch,
291 dst->data[0], dst->linesize[0],
292 src->width * bytes_per_component, src->height);
293 av_image_copy_plane((uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
294 LockedRect.Pitch, dst->data[1], dst->linesize[1],
295 src->width * bytes_per_component, src->height / 2);
298 IDirect3DSurface9_UnlockRect(surface);
303 static void dxva2_device_free(AVHWDeviceContext *ctx)
305 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
306 DXVA2DevicePriv *priv = ctx->user_opaque;
308 if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
309 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
312 IDirect3DDeviceManager9_Release(hwctx->devmgr);
314 if (priv->d3d9device)
315 IDirect3DDevice9_Release(priv->d3d9device);
318 IDirect3D9_Release(priv->d3d9);
321 FreeLibrary(priv->d3dlib);
324 FreeLibrary(priv->dxva2lib);
326 av_freep(&ctx->user_opaque);
329 static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
330 AVDictionary *opts, int flags)
332 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
333 DXVA2DevicePriv *priv;
335 pDirect3DCreate9 *createD3D = NULL;
336 pCreateDeviceManager9 *createDeviceManager = NULL;
337 D3DPRESENT_PARAMETERS d3dpp = {0};
338 D3DDISPLAYMODE d3ddm;
339 unsigned resetToken = 0;
340 UINT adapter = D3DADAPTER_DEFAULT;
344 adapter = atoi(device);
346 priv = av_mallocz(sizeof(*priv));
348 return AVERROR(ENOMEM);
350 ctx->user_opaque = priv;
351 ctx->free = dxva2_device_free;
353 priv->device_handle = INVALID_HANDLE_VALUE;
355 priv->d3dlib = LoadLibrary("d3d9.dll");
357 av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
358 return AVERROR_UNKNOWN;
360 priv->dxva2lib = LoadLibrary("dxva2.dll");
361 if (!priv->dxva2lib) {
362 av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
363 return AVERROR_UNKNOWN;
366 createD3D = (pDirect3DCreate9 *)GetProcAddress(priv->d3dlib, "Direct3DCreate9");
368 av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
369 return AVERROR_UNKNOWN;
371 createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(priv->dxva2lib,
372 "DXVA2CreateDirect3DDeviceManager9");
373 if (!createDeviceManager) {
374 av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
375 return AVERROR_UNKNOWN;
378 priv->d3d9 = createD3D(D3D_SDK_VERSION);
380 av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
381 return AVERROR_UNKNOWN;
384 IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
385 d3dpp.Windowed = TRUE;
386 d3dpp.BackBufferWidth = 640;
387 d3dpp.BackBufferHeight = 480;
388 d3dpp.BackBufferCount = 0;
389 d3dpp.BackBufferFormat = d3ddm.Format;
390 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
391 d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
393 hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
394 D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
395 &d3dpp, &priv->d3d9device);
397 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
398 return AVERROR_UNKNOWN;
401 hr = createDeviceManager(&resetToken, &hwctx->devmgr);
403 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
404 return AVERROR_UNKNOWN;
407 hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
409 av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
410 return AVERROR_UNKNOWN;
413 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
415 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
416 return AVERROR_UNKNOWN;
422 const HWContextType ff_hwcontext_type_dxva2 = {
423 .type = AV_HWDEVICE_TYPE_DXVA2,
426 .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
427 .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
428 .frames_priv_size = sizeof(DXVA2FramesContext),
430 .device_create = dxva2_device_create,
431 .frames_init = dxva2_frames_init,
432 .frames_uninit = dxva2_frames_uninit,
433 .frames_get_buffer = dxva2_get_buffer,
434 .transfer_get_formats = dxva2_transfer_get_formats,
435 .transfer_data_to = dxva2_transfer_data,
436 .transfer_data_from = dxva2_transfer_data,
438 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },