avconv_dxva2: use the hwcontext API
[libav.git] / avconv_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 #ifdef _WIN32_WINNT
22 #undef _WIN32_WINNT
23 #endif
24 #define _WIN32_WINNT 0x0600
25 #define DXVA2API_USE_BITFIELDS
26 #define COBJMACROS
27
28 #include <stdint.h>
29
30 #include <d3d9.h>
31 #include <dxva2api.h>
32
33 #include "avconv.h"
34
35 #include "libavcodec/dxva2.h"
36
37 #include "libavutil/avassert.h"
38 #include "libavutil/buffer.h"
39 #include "libavutil/frame.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/pixfmt.h"
42
43 #include "libavutil/hwcontext.h"
44 #include "libavutil/hwcontext_dxva2.h"
45
46 /* define all the GUIDs used directly here,
47 to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
48 #include <initguid.h>
49 DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
50
51 DEFINE_GUID(DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
52 DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
53 DEFINE_GUID(DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
54 DEFINE_GUID(DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
55 DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
56 DEFINE_GUID(DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
57 DEFINE_GUID(DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
58 DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main, 0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
59 DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
60 DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
61
62 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
63 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
64
65 typedef struct dxva2_mode {
66 const GUID *guid;
67 enum AVCodecID codec;
68 } dxva2_mode;
69
70 static const dxva2_mode dxva2_modes[] = {
71 /* MPEG-2 */
72 { &DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO },
73 { &DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO },
74
75 /* H.264 */
76 { &DXVA2_ModeH264_F, AV_CODEC_ID_H264 },
77 { &DXVA2_ModeH264_E, AV_CODEC_ID_H264 },
78 /* Intel specific H.264 mode */
79 { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
80
81 /* VC-1 / WMV3 */
82 { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
83 { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
84 { &DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
85 { &DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
86
87 /* HEVC/H.265 */
88 { &DXVA2_ModeHEVC_VLD_Main, AV_CODEC_ID_HEVC },
89
90 { NULL, 0 },
91 };
92
93 typedef struct DXVA2DevicePriv {
94 HMODULE d3dlib;
95 HMODULE dxva2lib;
96
97 HANDLE deviceHandle;
98
99 IDirect3D9 *d3d9;
100 IDirect3DDevice9 *d3d9device;
101 } DXVA2DevicePriv;
102
103 typedef struct DXVA2Context {
104 IDirectXVideoDecoder *decoder;
105
106 GUID decoder_guid;
107 DXVA2_ConfigPictureDecode decoder_config;
108 IDirectXVideoDecoderService *decoder_service;
109
110 AVFrame *tmp_frame;
111
112 AVBufferRef *hw_device_ctx;
113 AVBufferRef *hw_frames_ctx;
114 } DXVA2Context;
115
116 static void dxva2_device_uninit(AVHWDeviceContext *ctx)
117 {
118 AVDXVA2DeviceContext *hwctx = ctx->hwctx;
119 DXVA2DevicePriv *priv = ctx->user_opaque;
120
121 if (hwctx->devmgr && priv->deviceHandle != INVALID_HANDLE_VALUE)
122 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->deviceHandle);
123
124 if (hwctx->devmgr)
125 IDirect3DDeviceManager9_Release(hwctx->devmgr);
126
127 if (priv->d3d9device)
128 IDirect3DDevice9_Release(priv->d3d9device);
129
130 if (priv->d3d9)
131 IDirect3D9_Release(priv->d3d9);
132
133 if (priv->d3dlib)
134 FreeLibrary(priv->d3dlib);
135
136 if (priv->dxva2lib)
137 FreeLibrary(priv->dxva2lib);
138
139 av_freep(&ctx->user_opaque);
140 }
141
142 static void dxva2_uninit(AVCodecContext *s)
143 {
144 InputStream *ist = s->opaque;
145 DXVA2Context *ctx = ist->hwaccel_ctx;
146
147 ist->hwaccel_uninit = NULL;
148 ist->hwaccel_get_buffer = NULL;
149 ist->hwaccel_retrieve_data = NULL;
150
151 if (ctx->decoder_service)
152 IDirectXVideoDecoderService_Release(ctx->decoder_service);
153
154 av_buffer_unref(&ctx->hw_frames_ctx);
155 av_buffer_unref(&ctx->hw_device_ctx);
156
157 av_frame_free(&ctx->tmp_frame);
158
159 av_freep(&ist->hwaccel_ctx);
160 av_freep(&s->hwaccel_context);
161 }
162
163 static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
164 {
165 InputStream *ist = s->opaque;
166 DXVA2Context *ctx = ist->hwaccel_ctx;
167
168 return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
169 }
170
171 static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
172 {
173 InputStream *ist = s->opaque;
174 DXVA2Context *ctx = ist->hwaccel_ctx;
175 int ret;
176
177 ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
178 if (ret < 0)
179 return ret;
180
181 ret = av_frame_copy_props(ctx->tmp_frame, frame);
182 if (ret < 0) {
183 av_frame_unref(ctx->tmp_frame);
184 return ret;
185 }
186
187 av_frame_unref(frame);
188 av_frame_move_ref(frame, ctx->tmp_frame);
189
190 return 0;
191 }
192
193 static int dxva2_alloc(AVCodecContext *s)
194 {
195 InputStream *ist = s->opaque;
196 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
197 DXVA2Context *ctx;
198 pDirect3DCreate9 *createD3D = NULL;
199 pCreateDeviceManager9 *createDeviceManager = NULL;
200 HRESULT hr;
201 D3DPRESENT_PARAMETERS d3dpp = {0};
202 D3DDISPLAYMODE d3ddm;
203 unsigned resetToken = 0;
204 UINT adapter = D3DADAPTER_DEFAULT;
205
206 AVHWDeviceContext *device_ctx;
207 AVDXVA2DeviceContext *device_hwctx;
208 DXVA2DevicePriv *device_priv;
209 int ret;
210
211 ctx = av_mallocz(sizeof(*ctx));
212 if (!ctx)
213 return AVERROR(ENOMEM);
214
215 ist->hwaccel_ctx = ctx;
216 ist->hwaccel_uninit = dxva2_uninit;
217 ist->hwaccel_get_buffer = dxva2_get_buffer;
218 ist->hwaccel_retrieve_data = dxva2_retrieve_data;
219
220 ctx->hw_device_ctx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DXVA2);
221 if (!ctx->hw_device_ctx)
222 goto fail;
223
224 device_ctx = (AVHWDeviceContext*)ctx->hw_device_ctx->data;
225 device_hwctx = device_ctx->hwctx;
226
227 device_priv = av_mallocz(sizeof(*device_priv));
228 if (!device_priv)
229 goto fail;
230
231 device_ctx->user_opaque = device_priv;
232 device_ctx->free = dxva2_device_uninit;
233
234 device_priv->deviceHandle = INVALID_HANDLE_VALUE;
235
236 device_priv->d3dlib = LoadLibrary("d3d9.dll");
237 if (!device_priv->d3dlib) {
238 av_log(NULL, loglevel, "Failed to load D3D9 library\n");
239 goto fail;
240 }
241 device_priv->dxva2lib = LoadLibrary("dxva2.dll");
242 if (!device_priv->dxva2lib) {
243 av_log(NULL, loglevel, "Failed to load DXVA2 library\n");
244 goto fail;
245 }
246
247 createD3D = (pDirect3DCreate9 *)GetProcAddress(device_priv->d3dlib, "Direct3DCreate9");
248 if (!createD3D) {
249 av_log(NULL, loglevel, "Failed to locate Direct3DCreate9\n");
250 goto fail;
251 }
252 createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(device_priv->dxva2lib, "DXVA2CreateDirect3DDeviceManager9");
253 if (!createDeviceManager) {
254 av_log(NULL, loglevel, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
255 goto fail;
256 }
257
258 device_priv->d3d9 = createD3D(D3D_SDK_VERSION);
259 if (!device_priv->d3d9) {
260 av_log(NULL, loglevel, "Failed to create IDirect3D object\n");
261 goto fail;
262 }
263
264 if (ist->hwaccel_device) {
265 adapter = atoi(ist->hwaccel_device);
266 av_log(NULL, AV_LOG_INFO, "Using HWAccel device %d\n", adapter);
267 }
268
269 IDirect3D9_GetAdapterDisplayMode(device_priv->d3d9, adapter, &d3ddm);
270 d3dpp.Windowed = TRUE;
271 d3dpp.BackBufferWidth = 640;
272 d3dpp.BackBufferHeight = 480;
273 d3dpp.BackBufferCount = 0;
274 d3dpp.BackBufferFormat = d3ddm.Format;
275 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
276 d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
277
278 hr = IDirect3D9_CreateDevice(device_priv->d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
279 D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
280 &d3dpp, &device_priv->d3d9device);
281 if (FAILED(hr)) {
282 av_log(NULL, loglevel, "Failed to create Direct3D device\n");
283 goto fail;
284 }
285
286 hr = createDeviceManager(&resetToken, &device_hwctx->devmgr);
287 if (FAILED(hr)) {
288 av_log(NULL, loglevel, "Failed to create Direct3D device manager\n");
289 goto fail;
290 }
291
292 hr = IDirect3DDeviceManager9_ResetDevice(device_hwctx->devmgr, device_priv->d3d9device, resetToken);
293 if (FAILED(hr)) {
294 av_log(NULL, loglevel, "Failed to bind Direct3D device to device manager\n");
295 goto fail;
296 }
297
298 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &device_priv->deviceHandle);
299 if (FAILED(hr)) {
300 av_log(NULL, loglevel, "Failed to open device handle\n");
301 goto fail;
302 }
303
304 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_priv->deviceHandle, &IID_IDirectXVideoDecoderService, (void **)&ctx->decoder_service);
305 if (FAILED(hr)) {
306 av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
307 goto fail;
308 }
309
310 ret = av_hwdevice_ctx_init(ctx->hw_device_ctx);
311 if (ret < 0) {
312 av_log(NULL, loglevel, "Failed to initialize the HW device context\n");
313 goto fail;
314 }
315
316 ctx->tmp_frame = av_frame_alloc();
317 if (!ctx->tmp_frame)
318 goto fail;
319
320 s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
321 if (!s->hwaccel_context)
322 goto fail;
323
324 return 0;
325 fail:
326 dxva2_uninit(s);
327 return AVERROR(EINVAL);
328 }
329
330 static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
331 const DXVA2_VideoDesc *desc,
332 DXVA2_ConfigPictureDecode *config)
333 {
334 InputStream *ist = s->opaque;
335 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
336 DXVA2Context *ctx = ist->hwaccel_ctx;
337 unsigned cfg_count = 0, best_score = 0;
338 DXVA2_ConfigPictureDecode *cfg_list = NULL;
339 DXVA2_ConfigPictureDecode best_cfg = {{0}};
340 HRESULT hr;
341 int i;
342
343 hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
344 if (FAILED(hr)) {
345 av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
346 return AVERROR(EINVAL);
347 }
348
349 for (i = 0; i < cfg_count; i++) {
350 DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
351
352 unsigned score;
353 if (cfg->ConfigBitstreamRaw == 1)
354 score = 1;
355 else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
356 score = 2;
357 else
358 continue;
359 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
360 score += 16;
361 if (score > best_score) {
362 best_score = score;
363 best_cfg = *cfg;
364 }
365 }
366 CoTaskMemFree(cfg_list);
367
368 if (!best_score) {
369 av_log(NULL, loglevel, "No valid decoder configuration available\n");
370 return AVERROR(EINVAL);
371 }
372
373 *config = best_cfg;
374 return 0;
375 }
376
377 static int dxva2_create_decoder(AVCodecContext *s)
378 {
379 InputStream *ist = s->opaque;
380 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
381 DXVA2Context *ctx = ist->hwaccel_ctx;
382 struct dxva_context *dxva_ctx = s->hwaccel_context;
383 GUID *guid_list = NULL;
384 unsigned guid_count = 0, i, j;
385 GUID device_guid = GUID_NULL;
386 D3DFORMAT target_format = 0;
387 DXVA2_VideoDesc desc = { 0 };
388 DXVA2_ConfigPictureDecode config;
389 HRESULT hr;
390 int surface_alignment, num_surfaces;
391 int ret;
392
393 AVDXVA2FramesContext *frames_hwctx;
394 AVHWFramesContext *frames_ctx;
395
396 hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
397 if (FAILED(hr)) {
398 av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
399 goto fail;
400 }
401
402 for (i = 0; dxva2_modes[i].guid; i++) {
403 D3DFORMAT *target_list = NULL;
404 unsigned target_count = 0;
405 const dxva2_mode *mode = &dxva2_modes[i];
406 if (mode->codec != s->codec_id)
407 continue;
408
409 for (j = 0; j < guid_count; j++) {
410 if (IsEqualGUID(mode->guid, &guid_list[j]))
411 break;
412 }
413 if (j == guid_count)
414 continue;
415
416 hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
417 if (FAILED(hr)) {
418 continue;
419 }
420 for (j = 0; j < target_count; j++) {
421 const D3DFORMAT format = target_list[j];
422 if (format == MKTAG('N','V','1','2')) {
423 target_format = format;
424 break;
425 }
426 }
427 CoTaskMemFree(target_list);
428 if (target_format) {
429 device_guid = *mode->guid;
430 break;
431 }
432 }
433 CoTaskMemFree(guid_list);
434
435 if (IsEqualGUID(&device_guid, &GUID_NULL)) {
436 av_log(NULL, loglevel, "No decoder device for codec found\n");
437 goto fail;
438 }
439
440 desc.SampleWidth = s->coded_width;
441 desc.SampleHeight = s->coded_height;
442 desc.Format = target_format;
443
444 ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
445 if (ret < 0) {
446 goto fail;
447 }
448
449 /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
450 but it causes issues for H.264 on certain AMD GPUs..... */
451 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
452 surface_alignment = 32;
453 /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
454 all coding features have enough room to work with */
455 else if (s->codec_id == AV_CODEC_ID_HEVC)
456 surface_alignment = 128;
457 else
458 surface_alignment = 16;
459
460 /* 4 base work surfaces */
461 num_surfaces = 4;
462
463 /* add surfaces based on number of possible refs */
464 if (s->codec_id == AV_CODEC_ID_H264 || s->codec_id == AV_CODEC_ID_HEVC)
465 num_surfaces += 16;
466 else
467 num_surfaces += 2;
468
469 /* add extra surfaces for frame threading */
470 if (s->active_thread_type & FF_THREAD_FRAME)
471 num_surfaces += s->thread_count;
472
473 ctx->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->hw_device_ctx);
474 if (!ctx->hw_frames_ctx)
475 goto fail;
476 frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
477 frames_hwctx = frames_ctx->hwctx;
478
479 frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
480 frames_ctx->sw_format = AV_PIX_FMT_NV12;
481 frames_ctx->width = FFALIGN(s->coded_width, surface_alignment);
482 frames_ctx->height = FFALIGN(s->coded_height, surface_alignment);
483 frames_ctx->initial_pool_size = num_surfaces;
484
485 frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
486
487 ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
488 if (ret < 0) {
489 av_log(NULL, loglevel, "Failed to initialize the HW frames context\n");
490 goto fail;
491 }
492
493 hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
494 &desc, &config, frames_hwctx->surfaces,
495 frames_hwctx->nb_surfaces, &frames_hwctx->decoder_to_release);
496 if (FAILED(hr)) {
497 av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
498 goto fail;
499 }
500
501 ctx->decoder_guid = device_guid;
502 ctx->decoder_config = config;
503
504 dxva_ctx->cfg = &ctx->decoder_config;
505 dxva_ctx->decoder = frames_hwctx->decoder_to_release;
506 dxva_ctx->surface = frames_hwctx->surfaces;
507 dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
508
509 if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
510 dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
511
512 return 0;
513 fail:
514 av_buffer_unref(&ctx->hw_frames_ctx);
515 return AVERROR(EINVAL);
516 }
517
518 int dxva2_init(AVCodecContext *s)
519 {
520 InputStream *ist = s->opaque;
521 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
522 DXVA2Context *ctx;
523 int ret;
524
525 if (!ist->hwaccel_ctx) {
526 ret = dxva2_alloc(s);
527 if (ret < 0)
528 return ret;
529 }
530 ctx = ist->hwaccel_ctx;
531
532 if (s->codec_id == AV_CODEC_ID_H264 &&
533 (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
534 av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
535 return AVERROR(EINVAL);
536 }
537
538 av_buffer_unref(&ctx->hw_frames_ctx);
539
540 ret = dxva2_create_decoder(s);
541 if (ret < 0) {
542 av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
543 return ret;
544 }
545
546 return 0;
547 }