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 struct DXVA2FramesContext {
42 IDirect3DSurface9 **surfaces_internal;
46 IDirectXVideoAccelerationService *service;
53 enum AVPixelFormat pix_fmt;
54 } supported_formats[] = {
55 { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
56 { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
59 DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
60 DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
62 static void dxva2_frames_uninit(AVHWFramesContext *ctx)
64 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
65 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
66 DXVA2FramesContext *s = ctx->internal->priv;
69 if (frames_hwctx->decoder_to_release)
70 IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
72 if (s->surfaces_internal) {
73 for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
74 if (s->surfaces_internal[i])
75 IDirect3DSurface9_Release(s->surfaces_internal[i]);
78 av_freep(&s->surfaces_internal);
81 IDirectXVideoAccelerationService_Release(s->service);
85 if (s->device_handle != INVALID_HANDLE_VALUE) {
86 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
87 s->device_handle = INVALID_HANDLE_VALUE;
91 static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
93 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
94 DXVA2FramesContext *s = ctx->internal->priv;
95 AVDXVA2FramesContext *hwctx = ctx->hwctx;
97 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
98 s->nb_surfaces_used++;
99 return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
100 sizeof(*hwctx->surfaces), NULL, 0, 0);
106 static int dxva2_init_pool(AVHWFramesContext *ctx)
108 AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
109 AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
110 DXVA2FramesContext *s = ctx->internal->priv;
111 int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
116 if (ctx->initial_pool_size <= 0)
119 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
121 av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
122 return AVERROR_UNKNOWN;
125 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
127 decode ? &video_decoder_service : &video_processor_service,
128 (void **)&s->service);
130 av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
131 return AVERROR_UNKNOWN;
134 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
135 if (ctx->sw_format == supported_formats[i].pix_fmt) {
136 s->format = supported_formats[i].d3d_format;
140 if (i == FF_ARRAY_ELEMS(supported_formats)) {
141 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
142 av_get_pix_fmt_name(ctx->sw_format));
143 return AVERROR(EINVAL);
146 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
147 sizeof(*s->surfaces_internal));
148 if (!s->surfaces_internal)
149 return AVERROR(ENOMEM);
151 hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
152 ctx->width, ctx->height,
153 ctx->initial_pool_size - 1,
154 s->format, D3DPOOL_DEFAULT, 0,
155 frames_hwctx->surface_type,
156 s->surfaces_internal, NULL);
158 av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
159 return AVERROR_UNKNOWN;
162 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
163 ctx, dxva2_pool_alloc, NULL);
164 if (!ctx->internal->pool_internal)
165 return AVERROR(ENOMEM);
167 frames_hwctx->surfaces = s->surfaces_internal;
168 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
173 static int dxva2_frames_init(AVHWFramesContext *ctx)
175 AVDXVA2FramesContext *hwctx = ctx->hwctx;
176 DXVA2FramesContext *s = ctx->internal->priv;
179 if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
180 hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
181 av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
182 hwctx->surface_type);
183 return AVERROR(EINVAL);
186 s->device_handle = INVALID_HANDLE_VALUE;
188 /* init the frame pool if the caller didn't provide one */
190 ret = dxva2_init_pool(ctx);
192 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
200 static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
202 frame->buf[0] = av_buffer_pool_get(ctx->pool);
204 return AVERROR(ENOMEM);
206 frame->data[3] = frame->buf[0]->data;
207 frame->format = AV_PIX_FMT_DXVA2_VLD;
208 frame->width = ctx->width;
209 frame->height = ctx->height;
214 static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
215 enum AVHWFrameTransferDirection dir,
216 enum AVPixelFormat **formats)
218 enum AVPixelFormat *fmts;
220 fmts = av_malloc_array(2, sizeof(*fmts));
222 return AVERROR(ENOMEM);
224 fmts[0] = ctx->sw_format;
225 fmts[1] = AV_PIX_FMT_NONE;
232 static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
235 IDirect3DSurface9 *surface;
236 D3DSURFACE_DESC surfaceDesc;
237 D3DLOCKED_RECT LockedRect;
240 int download = !!src->hw_frames_ctx;
241 int bytes_per_component;
243 switch (ctx->sw_format) {
244 case AV_PIX_FMT_NV12:
245 bytes_per_component = 1;
247 case AV_PIX_FMT_P010:
248 bytes_per_component = 2;
254 surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
256 hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
258 av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
259 return AVERROR_UNKNOWN;
262 hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
263 download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
265 av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
266 return AVERROR_UNKNOWN;
270 av_image_copy_plane(dst->data[0], dst->linesize[0],
271 (uint8_t*)LockedRect.pBits, LockedRect.Pitch,
272 src->width * bytes_per_component, src->height);
273 av_image_copy_plane(dst->data[1], dst->linesize[1],
274 (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
275 LockedRect.Pitch, src->width * bytes_per_component, src->height / 2);
277 av_image_copy_plane((uint8_t*)LockedRect.pBits, LockedRect.Pitch,
278 dst->data[0], dst->linesize[0],
279 src->width * bytes_per_component, src->height);
280 av_image_copy_plane((uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
281 LockedRect.Pitch, dst->data[1], dst->linesize[1],
282 src->width * bytes_per_component, src->height / 2);
285 IDirect3DSurface9_UnlockRect(surface);
290 const HWContextType ff_hwcontext_type_dxva2 = {
291 .type = AV_HWDEVICE_TYPE_DXVA2,
294 .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
295 .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
296 .frames_priv_size = sizeof(DXVA2FramesContext),
298 .frames_init = dxva2_frames_init,
299 .frames_uninit = dxva2_frames_uninit,
300 .frames_get_buffer = dxva2_get_buffer,
301 .transfer_get_formats = dxva2_transfer_get_formats,
302 .transfer_data_to = dxva2_transfer_data,
303 .transfer_data_from = dxva2_transfer_data,
305 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },