buffer: convert to stdatomic
[ffmpeg.git] / libavutil / hwcontext_dxva2.c
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
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 "common.h"
33 #include "hwcontext.h"
34 #include "hwcontext_dxva2.h"
35 #include "hwcontext_internal.h"
36 #include "imgutils.h"
37 #include "pixdesc.h"
38 #include "pixfmt.h"
39
40 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
41 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
42
43 typedef struct DXVA2FramesContext {
44     IDirect3DSurface9 **surfaces_internal;
45     int              nb_surfaces_used;
46
47     HANDLE  device_handle;
48     IDirectXVideoAccelerationService *service;
49
50     D3DFORMAT format;
51 } DXVA2FramesContext;
52
53 typedef struct DXVA2DevicePriv {
54     HMODULE d3dlib;
55     HMODULE dxva2lib;
56
57     HANDLE device_handle;
58
59     IDirect3D9       *d3d9;
60     IDirect3DDevice9 *d3d9device;
61 } DXVA2DevicePriv;
62
63 static const struct {
64     D3DFORMAT d3d_format;
65     enum AVPixelFormat pix_fmt;
66 } supported_formats[] = {
67     { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
68     { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
69 };
70
71 DEFINE_GUID(video_decoder_service,   0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
72 DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
73
74 static void dxva2_frames_uninit(AVHWFramesContext *ctx)
75 {
76     AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
77     AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
78     DXVA2FramesContext *s = ctx->internal->priv;
79     int i;
80
81     if (frames_hwctx->decoder_to_release)
82         IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
83
84     if (s->surfaces_internal) {
85         for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
86             if (s->surfaces_internal[i])
87                 IDirect3DSurface9_Release(s->surfaces_internal[i]);
88         }
89     }
90     av_freep(&s->surfaces_internal);
91
92     if (s->service) {
93         IDirectXVideoAccelerationService_Release(s->service);
94         s->service = NULL;
95     }
96
97     if (s->device_handle != INVALID_HANDLE_VALUE) {
98         IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
99         s->device_handle = INVALID_HANDLE_VALUE;
100     }
101 }
102
103 static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
104 {
105     AVHWFramesContext      *ctx = (AVHWFramesContext*)opaque;
106     DXVA2FramesContext       *s = ctx->internal->priv;
107     AVDXVA2FramesContext *hwctx = ctx->hwctx;
108
109     if (s->nb_surfaces_used < hwctx->nb_surfaces) {
110         s->nb_surfaces_used++;
111         return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
112                                 sizeof(*hwctx->surfaces), NULL, 0, 0);
113     }
114
115     return NULL;
116 }
117
118 static int dxva2_init_pool(AVHWFramesContext *ctx)
119 {
120     AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
121     AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
122     DXVA2FramesContext              *s = ctx->internal->priv;
123     int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
124
125     int i;
126     HRESULT hr;
127
128     if (ctx->initial_pool_size <= 0)
129         return 0;
130
131     hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
132     if (FAILED(hr)) {
133         av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
134         return AVERROR_UNKNOWN;
135     }
136
137     hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
138                                                  s->device_handle,
139                                                  decode ? &video_decoder_service : &video_processor_service,
140                                                  (void **)&s->service);
141     if (FAILED(hr)) {
142         av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
143         return AVERROR_UNKNOWN;
144     }
145
146     for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
147         if (ctx->sw_format == supported_formats[i].pix_fmt) {
148             s->format = supported_formats[i].d3d_format;
149             break;
150         }
151     }
152     if (i == FF_ARRAY_ELEMS(supported_formats)) {
153         av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
154                av_get_pix_fmt_name(ctx->sw_format));
155         return AVERROR(EINVAL);
156     }
157
158     s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
159                                             sizeof(*s->surfaces_internal));
160     if (!s->surfaces_internal)
161         return AVERROR(ENOMEM);
162
163     hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
164                                                         ctx->width, ctx->height,
165                                                         ctx->initial_pool_size - 1,
166                                                         s->format, D3DPOOL_DEFAULT, 0,
167                                                         frames_hwctx->surface_type,
168                                                         s->surfaces_internal, NULL);
169     if (FAILED(hr)) {
170         av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
171         return AVERROR_UNKNOWN;
172     }
173
174     ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
175                                                         ctx, dxva2_pool_alloc, NULL);
176     if (!ctx->internal->pool_internal)
177         return AVERROR(ENOMEM);
178
179     frames_hwctx->surfaces    = s->surfaces_internal;
180     frames_hwctx->nb_surfaces = ctx->initial_pool_size;
181
182     return 0;
183 }
184
185 static int dxva2_frames_init(AVHWFramesContext *ctx)
186 {
187     AVDXVA2FramesContext *hwctx = ctx->hwctx;
188     DXVA2FramesContext       *s = ctx->internal->priv;
189     int ret;
190
191     if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
192         hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
193         av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
194                hwctx->surface_type);
195         return AVERROR(EINVAL);
196     }
197
198     s->device_handle = INVALID_HANDLE_VALUE;
199
200     /* init the frame pool if the caller didn't provide one */
201     if (!ctx->pool) {
202         ret = dxva2_init_pool(ctx);
203         if (ret < 0) {
204             av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
205             return ret;
206         }
207     }
208
209     return 0;
210 }
211
212 static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
213 {
214     frame->buf[0] = av_buffer_pool_get(ctx->pool);
215     if (!frame->buf[0])
216         return AVERROR(ENOMEM);
217
218     frame->data[3] = frame->buf[0]->data;
219     frame->format  = AV_PIX_FMT_DXVA2_VLD;
220     frame->width   = ctx->width;
221     frame->height  = ctx->height;
222
223     return 0;
224 }
225
226 static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
227                                       enum AVHWFrameTransferDirection dir,
228                                       enum AVPixelFormat **formats)
229 {
230     enum AVPixelFormat *fmts;
231
232     fmts = av_malloc_array(2, sizeof(*fmts));
233     if (!fmts)
234         return AVERROR(ENOMEM);
235
236     fmts[0] = ctx->sw_format;
237     fmts[1] = AV_PIX_FMT_NONE;
238
239     *formats = fmts;
240
241     return 0;
242 }
243
244 static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
245                                const AVFrame *src)
246 {
247     IDirect3DSurface9 *surface;
248     D3DSURFACE_DESC    surfaceDesc;
249     D3DLOCKED_RECT     LockedRect;
250     HRESULT            hr;
251
252     uint8_t *surf_data[4]     = { NULL };
253     int      surf_linesize[4] = { 0 };
254     int i;
255
256     int download = !!src->hw_frames_ctx;
257
258     surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
259
260     hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
261     if (FAILED(hr)) {
262         av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
263         return AVERROR_UNKNOWN;
264     }
265
266     hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
267                                     download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
268     if (FAILED(hr)) {
269         av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
270         return AVERROR_UNKNOWN;
271     }
272
273     for (i = 0; download ? dst->data[i] : src->data[i]; i++)
274         surf_linesize[i] = LockedRect.Pitch;
275
276     av_image_fill_pointers(surf_data, ctx->sw_format, surfaceDesc.Height,
277                            (uint8_t*)LockedRect.pBits, surf_linesize);
278
279     if (download) {
280         ptrdiff_t src_linesize1[4], dst_linesize1[4];
281         for (i = 0; i < 4; i++) {
282             dst_linesize1[i] = dst->linesize[i];
283             src_linesize1[i] = surf_linesize[i];
284         }
285         av_image_copy_uc_from(dst->data, dst_linesize1, surf_data, src_linesize1,
286                               ctx->sw_format, src->width, src->height);
287     } else {
288         av_image_copy(surf_data, surf_linesize, src->data, src->linesize,
289                       ctx->sw_format, src->width, src->height);
290     }
291
292     IDirect3DSurface9_UnlockRect(surface);
293
294     return 0;
295 }
296
297 static void dxva2_device_free(AVHWDeviceContext *ctx)
298 {
299     AVDXVA2DeviceContext *hwctx = ctx->hwctx;
300     DXVA2DevicePriv       *priv = ctx->user_opaque;
301
302     if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
303         IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
304
305     if (hwctx->devmgr)
306         IDirect3DDeviceManager9_Release(hwctx->devmgr);
307
308     if (priv->d3d9device)
309         IDirect3DDevice9_Release(priv->d3d9device);
310
311     if (priv->d3d9)
312         IDirect3D9_Release(priv->d3d9);
313
314     if (priv->d3dlib)
315         FreeLibrary(priv->d3dlib);
316
317     if (priv->dxva2lib)
318         FreeLibrary(priv->dxva2lib);
319
320     av_freep(&ctx->user_opaque);
321 }
322
323 static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
324                                AVDictionary *opts, int flags)
325 {
326     AVDXVA2DeviceContext *hwctx = ctx->hwctx;
327     DXVA2DevicePriv *priv;
328
329     pDirect3DCreate9 *createD3D = NULL;
330     pCreateDeviceManager9 *createDeviceManager = NULL;
331     D3DPRESENT_PARAMETERS d3dpp = {0};
332     D3DDISPLAYMODE        d3ddm;
333     unsigned resetToken = 0;
334     UINT adapter = D3DADAPTER_DEFAULT;
335     HRESULT hr;
336
337     if (device)
338         adapter = atoi(device);
339
340     priv = av_mallocz(sizeof(*priv));
341     if (!priv)
342         return AVERROR(ENOMEM);
343
344     ctx->user_opaque = priv;
345     ctx->free        = dxva2_device_free;
346
347     priv->device_handle = INVALID_HANDLE_VALUE;
348
349     priv->d3dlib = LoadLibrary("d3d9.dll");
350     if (!priv->d3dlib) {
351         av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
352         return AVERROR_UNKNOWN;
353     }
354     priv->dxva2lib = LoadLibrary("dxva2.dll");
355     if (!priv->dxva2lib) {
356         av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
357         return AVERROR_UNKNOWN;
358     }
359
360     createD3D = (pDirect3DCreate9 *)GetProcAddress(priv->d3dlib, "Direct3DCreate9");
361     if (!createD3D) {
362         av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
363         return AVERROR_UNKNOWN;
364     }
365     createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(priv->dxva2lib,
366                                                                   "DXVA2CreateDirect3DDeviceManager9");
367     if (!createDeviceManager) {
368         av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
369         return AVERROR_UNKNOWN;
370     }
371
372     priv->d3d9 = createD3D(D3D_SDK_VERSION);
373     if (!priv->d3d9) {
374         av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
375         return AVERROR_UNKNOWN;
376     }
377
378     IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
379     d3dpp.Windowed         = TRUE;
380     d3dpp.BackBufferWidth  = 640;
381     d3dpp.BackBufferHeight = 480;
382     d3dpp.BackBufferCount  = 0;
383     d3dpp.BackBufferFormat = d3ddm.Format;
384     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
385     d3dpp.Flags            = D3DPRESENTFLAG_VIDEO;
386
387     hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
388                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
389                                  &d3dpp, &priv->d3d9device);
390     if (FAILED(hr)) {
391         av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
392         return AVERROR_UNKNOWN;
393     }
394
395     hr = createDeviceManager(&resetToken, &hwctx->devmgr);
396     if (FAILED(hr)) {
397         av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
398         return AVERROR_UNKNOWN;
399     }
400
401     hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
402     if (FAILED(hr)) {
403         av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
404         return AVERROR_UNKNOWN;
405     }
406
407     hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
408     if (FAILED(hr)) {
409         av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
410         return AVERROR_UNKNOWN;
411     }
412
413     return 0;
414 }
415
416 const HWContextType ff_hwcontext_type_dxva2 = {
417     .type                 = AV_HWDEVICE_TYPE_DXVA2,
418     .name                 = "DXVA2",
419
420     .device_hwctx_size    = sizeof(AVDXVA2DeviceContext),
421     .frames_hwctx_size    = sizeof(AVDXVA2FramesContext),
422     .frames_priv_size     = sizeof(DXVA2FramesContext),
423
424     .device_create        = dxva2_device_create,
425     .frames_init          = dxva2_frames_init,
426     .frames_uninit        = dxva2_frames_uninit,
427     .frames_get_buffer    = dxva2_get_buffer,
428     .transfer_get_formats = dxva2_transfer_get_formats,
429     .transfer_data_to     = dxva2_transfer_data,
430     .transfer_data_from   = dxva2_transfer_data,
431
432     .pix_fmts             = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },
433 };