ccee287ee654a307407db734f9d5b44726d14b6e
[ffmpeg.git] / libavutil / hwcontext_dxva2.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <windows.h>
20
21 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
22 #undef _WIN32_WINNT
23 #define _WIN32_WINNT 0x0600
24 #endif
25 #define DXVA2API_USE_BITFIELDS
26 #define COBJMACROS
27
28 #include <d3d9.h>
29 #include <dxva2api.h>
30 #include <initguid.h>
31
32 #include "avassert.h"
33 #include "common.h"
34 #include "hwcontext.h"
35 #include "hwcontext_dxva2.h"
36 #include "hwcontext_internal.h"
37 #include "imgutils.h"
38 #include "pixdesc.h"
39 #include "pixfmt.h"
40
41 typedef struct DXVA2FramesContext {
42     IDirect3DSurface9 **surfaces_internal;
43     int              nb_surfaces_used;
44
45     HANDLE  device_handle;
46     IDirectXVideoAccelerationService *service;
47
48     D3DFORMAT format;
49 } DXVA2FramesContext;
50
51 static const struct {
52     D3DFORMAT d3d_format;
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 },
57 };
58
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);
61
62 static void dxva2_frames_uninit(AVHWFramesContext *ctx)
63 {
64     AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
65     AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
66     DXVA2FramesContext *s = ctx->internal->priv;
67     int i;
68
69     if (frames_hwctx->decoder_to_release)
70         IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
71
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]);
76         }
77     }
78     av_freep(&s->surfaces_internal);
79
80     if (s->service) {
81         IDirectXVideoAccelerationService_Release(s->service);
82         s->service = NULL;
83     }
84
85     if (s->device_handle != INVALID_HANDLE_VALUE) {
86         IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
87         s->device_handle = INVALID_HANDLE_VALUE;
88     }
89 }
90
91 static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
92 {
93     AVHWFramesContext      *ctx = (AVHWFramesContext*)opaque;
94     DXVA2FramesContext       *s = ctx->internal->priv;
95     AVDXVA2FramesContext *hwctx = ctx->hwctx;
96
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);
101     }
102
103     return NULL;
104 }
105
106 static int dxva2_init_pool(AVHWFramesContext *ctx)
107 {
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);
112
113     int i;
114     HRESULT hr;
115
116     if (ctx->initial_pool_size <= 0)
117         return 0;
118
119     hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
120     if (FAILED(hr)) {
121         av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
122         return AVERROR_UNKNOWN;
123     }
124
125     hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
126                                                  s->device_handle,
127                                                  decode ? &video_decoder_service : &video_processor_service,
128                                                  (void **)&s->service);
129     if (FAILED(hr)) {
130         av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
131         return AVERROR_UNKNOWN;
132     }
133
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;
137             break;
138         }
139     }
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);
144     }
145
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);
150
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);
157     if (FAILED(hr)) {
158         av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
159         return AVERROR_UNKNOWN;
160     }
161
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);
166
167     frames_hwctx->surfaces    = s->surfaces_internal;
168     frames_hwctx->nb_surfaces = ctx->initial_pool_size;
169
170     return 0;
171 }
172
173 static int dxva2_frames_init(AVHWFramesContext *ctx)
174 {
175     AVDXVA2FramesContext *hwctx = ctx->hwctx;
176     DXVA2FramesContext       *s = ctx->internal->priv;
177     int ret;
178
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);
184     }
185
186     s->device_handle = INVALID_HANDLE_VALUE;
187
188     /* init the frame pool if the caller didn't provide one */
189     if (!ctx->pool) {
190         ret = dxva2_init_pool(ctx);
191         if (ret < 0) {
192             av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
193             return ret;
194         }
195     }
196
197     return 0;
198 }
199
200 static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
201 {
202     frame->buf[0] = av_buffer_pool_get(ctx->pool);
203     if (!frame->buf[0])
204         return AVERROR(ENOMEM);
205
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;
210
211     return 0;
212 }
213
214 static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
215                                       enum AVHWFrameTransferDirection dir,
216                                       enum AVPixelFormat **formats)
217 {
218     enum AVPixelFormat *fmts;
219
220     fmts = av_malloc_array(2, sizeof(*fmts));
221     if (!fmts)
222         return AVERROR(ENOMEM);
223
224     fmts[0] = ctx->sw_format;
225     fmts[1] = AV_PIX_FMT_NONE;
226
227     *formats = fmts;
228
229     return 0;
230 }
231
232 static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
233                                const AVFrame *src)
234 {
235     IDirect3DSurface9 *surface;
236     D3DSURFACE_DESC    surfaceDesc;
237     D3DLOCKED_RECT     LockedRect;
238     HRESULT            hr;
239
240     int download = !!src->hw_frames_ctx;
241     int bytes_per_component;
242
243     switch (ctx->sw_format) {
244         case AV_PIX_FMT_NV12:
245             bytes_per_component = 1;
246             break;
247         case AV_PIX_FMT_P010:
248             bytes_per_component = 2;
249             break;
250         default:
251             av_assert0(0);
252     }
253
254     surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
255
256     hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
257     if (FAILED(hr)) {
258         av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
259         return AVERROR_UNKNOWN;
260     }
261
262     hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
263                                     download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
264     if (FAILED(hr)) {
265         av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
266         return AVERROR_UNKNOWN;
267     }
268
269     if (download) {
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);
276     } else {
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);
283     }
284
285     IDirect3DSurface9_UnlockRect(surface);
286
287     return 0;
288 }
289
290 const HWContextType ff_hwcontext_type_dxva2 = {
291     .type                 = AV_HWDEVICE_TYPE_DXVA2,
292     .name                 = "DXVA2",
293
294     .device_hwctx_size    = sizeof(AVDXVA2DeviceContext),
295     .frames_hwctx_size    = sizeof(AVDXVA2FramesContext),
296     .frames_priv_size     = sizeof(DXVA2FramesContext),
297
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,
304
305     .pix_fmts             = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },
306 };