2 * DXVA2 HW acceleration.
4 * copyright (c) 2010 Laurent Aimar
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/common.h"
28 #include "libavutil/log.h"
29 #include "libavutil/time.h"
33 #include "dxva2_internal.h"
35 /* define all the GUIDs used directly here,
36 to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
37 DEFINE_GUID(ff_DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
38 DEFINE_GUID(ff_DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
39 DEFINE_GUID(ff_DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
40 DEFINE_GUID(ff_DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
41 DEFINE_GUID(ff_DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
42 DEFINE_GUID(ff_DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
43 DEFINE_GUID(ff_DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
44 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main, 0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
45 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
46 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_Profile0,0x463707f8,0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
47 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_10bit_Profile2,0xa4c749ef,0x6ecf,0x48aa,0x84,0x48,0x50,0xa7,0xa1,0x16,0x5f,0xf7);
48 DEFINE_GUID(ff_DXVA2_ModeAV1_VLD_Profile0,0xb8be4ccb,0xcf53,0x46ba,0x8d,0x59,0xd6,0xb8,0xa6,0xda,0x5d,0x2a);
49 DEFINE_GUID(ff_DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
50 DEFINE_GUID(ff_GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
51 DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
53 typedef struct dxva_mode {
56 // List of supported profiles, terminated by a FF_PROFILE_UNKNOWN entry.
57 // If NULL, don't check profile.
61 static const int prof_mpeg2_main[] = {FF_PROFILE_MPEG2_SIMPLE,
62 FF_PROFILE_MPEG2_MAIN,
64 static const int prof_h264_high[] = {FF_PROFILE_H264_CONSTRAINED_BASELINE,
68 static const int prof_hevc_main[] = {FF_PROFILE_HEVC_MAIN,
70 static const int prof_hevc_main10[] = {FF_PROFILE_HEVC_MAIN_10,
72 static const int prof_vp9_profile0[] = {FF_PROFILE_VP9_0,
74 static const int prof_vp9_profile2[] = {FF_PROFILE_VP9_2,
76 static const int prof_av1_profile0[] = {FF_PROFILE_AV1_MAIN,
79 static const dxva_mode dxva_modes[] = {
81 { &ff_DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
82 { &ff_DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
85 { &ff_DXVA2_ModeH264_F, AV_CODEC_ID_H264, prof_h264_high },
86 { &ff_DXVA2_ModeH264_E, AV_CODEC_ID_H264, prof_h264_high },
87 /* Intel specific H.264 mode */
88 { &ff_DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264, prof_h264_high },
91 { &ff_DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
92 { &ff_DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
93 { &ff_DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
94 { &ff_DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
97 { &ff_DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC, prof_hevc_main10 },
98 { &ff_DXVA2_ModeHEVC_VLD_Main, AV_CODEC_ID_HEVC, prof_hevc_main },
101 { &ff_DXVA2_ModeVP9_VLD_Profile0, AV_CODEC_ID_VP9, prof_vp9_profile0 },
102 { &ff_DXVA2_ModeVP9_VLD_10bit_Profile2, AV_CODEC_ID_VP9, prof_vp9_profile2 },
105 { &ff_DXVA2_ModeAV1_VLD_Profile0, AV_CODEC_ID_AV1, prof_av1_profile0 },
110 static int dxva_get_decoder_configuration(AVCodecContext *avctx,
111 const void *cfg_list,
114 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
115 unsigned i, best_score = 0;
118 for (i = 0; i < cfg_count; i++) {
120 UINT ConfigBitstreamRaw;
121 GUID guidConfigBitstreamEncryption;
124 if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
125 D3D11_VIDEO_DECODER_CONFIG *cfg = &((D3D11_VIDEO_DECODER_CONFIG *)cfg_list)[i];
126 ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
127 guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
131 if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
132 DXVA2_ConfigPictureDecode *cfg = &((DXVA2_ConfigPictureDecode *)cfg_list)[i];
133 ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
134 guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
138 if (ConfigBitstreamRaw == 1)
140 else if (avctx->codec_id == AV_CODEC_ID_H264 && ConfigBitstreamRaw == 2)
144 if (IsEqualGUID(&guidConfigBitstreamEncryption, &ff_DXVA2_NoEncrypt))
146 if (score > best_score) {
153 av_log(avctx, AV_LOG_VERBOSE, "No valid decoder configuration available\n");
154 return AVERROR(EINVAL);
161 static int d3d11va_validate_output(void *service, GUID guid, const void *surface_format)
164 BOOL is_supported = FALSE;
165 hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice *)service,
167 *(DXGI_FORMAT *)surface_format,
169 return SUCCEEDED(hr) && is_supported;
174 static int dxva2_validate_output(void *decoder_service, GUID guid, const void *surface_format)
178 unsigned j, target_count;
179 D3DFORMAT *target_list;
180 hr = IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService *)decoder_service, &guid, &target_count, &target_list);
182 for (j = 0; j < target_count; j++) {
183 const D3DFORMAT format = target_list[j];
184 if (format == *(D3DFORMAT *)surface_format) {
189 CoTaskMemFree(target_list);
195 static int dxva_check_codec_compatibility(AVCodecContext *avctx, const dxva_mode *mode)
197 if (mode->codec != avctx->codec_id)
200 if (mode->profiles && !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
202 for (i = 0; mode->profiles[i] != FF_PROFILE_UNKNOWN; i++) {
203 if (avctx->profile == mode->profiles[i]) {
215 static void dxva_list_guids_debug(AVCodecContext *avctx, void *service,
216 unsigned guid_count, const GUID *guid_list)
218 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
221 av_log(avctx, AV_LOG_VERBOSE, "Decoder GUIDs reported as supported:\n");
223 for (i = 0; i < guid_count; i++) {
224 const GUID *guid = &guid_list[i];
226 av_log(avctx, AV_LOG_VERBOSE,
227 "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
228 (unsigned) guid->Data1, guid->Data2, guid->Data3,
229 guid->Data4[0], guid->Data4[1],
230 guid->Data4[2], guid->Data4[3],
231 guid->Data4[4], guid->Data4[5],
232 guid->Data4[6], guid->Data4[7]);
235 if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
237 // We don't know the maximum valid DXGI_FORMAT, so use 200 as
238 // arbitrary upper bound (that could become outdated).
239 for (format = 0; format < 200; format++) {
240 if (d3d11va_validate_output(service, *guid, &format))
241 av_log(avctx, AV_LOG_VERBOSE, " %d", (int)format);
246 if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
247 const D3DFORMAT formats[] = {MKTAG('N', 'V', '1', '2'),
248 MKTAG('P', '0', '1', '0')};
250 for (i = 0; i < FF_ARRAY_ELEMS(formats); i++) {
251 if (dxva2_validate_output(service, *guid, &formats[i]))
252 av_log(avctx, AV_LOG_VERBOSE, " %d", i);
256 av_log(avctx, AV_LOG_VERBOSE, "\n");
260 static int dxva_get_decoder_guid(AVCodecContext *avctx, void *service, void *surface_format,
261 unsigned guid_count, const GUID *guid_list, GUID *decoder_guid)
263 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
266 dxva_list_guids_debug(avctx, service, guid_count, guid_list);
268 *decoder_guid = ff_GUID_NULL;
269 for (i = 0; dxva_modes[i].guid; i++) {
270 const dxva_mode *mode = &dxva_modes[i];
272 if (!dxva_check_codec_compatibility(avctx, mode))
275 for (j = 0; j < guid_count; j++) {
276 if (IsEqualGUID(mode->guid, &guid_list[j]))
283 if (sctx->pix_fmt == AV_PIX_FMT_D3D11)
284 validate = d3d11va_validate_output(service, *mode->guid, surface_format);
287 if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
288 validate = dxva2_validate_output(service, *mode->guid, surface_format);
291 *decoder_guid = *mode->guid;
296 if (IsEqualGUID(decoder_guid, &ff_GUID_NULL)) {
297 av_log(avctx, AV_LOG_VERBOSE, "No decoder device for codec found\n");
298 return AVERROR(EINVAL);
301 if (IsEqualGUID(decoder_guid, &ff_DXVADDI_Intel_ModeH264_E))
302 sctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
307 static void bufref_free_interface(void *opaque, uint8_t *data)
309 IUnknown_Release((IUnknown *)opaque);
312 static AVBufferRef *bufref_wrap_interface(IUnknown *iface)
314 return av_buffer_create((uint8_t*)iface, 1, bufref_free_interface, iface, 0);
319 static int dxva2_get_decoder_configuration(AVCodecContext *avctx, const GUID *device_guid,
320 const DXVA2_VideoDesc *desc,
321 DXVA2_ConfigPictureDecode *config)
323 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
325 DXVA2_ConfigPictureDecode *cfg_list;
329 hr = IDirectXVideoDecoderService_GetDecoderConfigurations(sctx->dxva2_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
331 av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
332 return AVERROR(EINVAL);
335 ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
337 *config = cfg_list[ret];
338 CoTaskMemFree(cfg_list);
342 static int dxva2_create_decoder(AVCodecContext *avctx)
344 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
348 D3DFORMAT surface_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
349 MKTAG('P', '0', '1', '0') : MKTAG('N', 'V', '1', '2');
350 DXVA2_VideoDesc desc = { 0 };
351 DXVA2_ConfigPictureDecode config;
354 HANDLE device_handle;
355 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
356 AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
357 AVDXVA2DeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
359 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
362 av_log(avctx, AV_LOG_ERROR, "Failed to open a device handle\n");
366 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
367 &ff_IID_IDirectXVideoDecoderService,
368 (void **)&sctx->dxva2_service);
369 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
371 av_log(avctx, AV_LOG_ERROR, "Failed to create IDirectXVideoDecoderService\n");
375 hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(sctx->dxva2_service, &guid_count, &guid_list);
377 av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder device GUIDs\n");
381 ret = dxva_get_decoder_guid(avctx, sctx->dxva2_service, &surface_format,
382 guid_count, guid_list, &device_guid);
383 CoTaskMemFree(guid_list);
388 desc.SampleWidth = avctx->coded_width;
389 desc.SampleHeight = avctx->coded_height;
390 desc.Format = surface_format;
392 ret = dxva2_get_decoder_configuration(avctx, &device_guid, &desc, &config);
397 hr = IDirectXVideoDecoderService_CreateVideoDecoder(sctx->dxva2_service, &device_guid,
398 &desc, &config, frames_hwctx->surfaces,
399 frames_hwctx->nb_surfaces, &sctx->dxva2_decoder);
401 av_log(avctx, AV_LOG_ERROR, "Failed to create DXVA2 video decoder\n");
405 sctx->dxva2_config = config;
407 sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->dxva2_decoder);
408 if (!sctx->decoder_ref)
409 return AVERROR(ENOMEM);
413 return AVERROR(EINVAL);
420 static int d3d11va_get_decoder_configuration(AVCodecContext *avctx,
421 ID3D11VideoDevice *video_device,
422 const D3D11_VIDEO_DECODER_DESC *desc,
423 D3D11_VIDEO_DECODER_CONFIG *config)
425 unsigned cfg_count = 0;
426 D3D11_VIDEO_DECODER_CONFIG *cfg_list = NULL;
430 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(video_device, desc, &cfg_count);
432 av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
433 return AVERROR(EINVAL);
436 cfg_list = av_malloc_array(cfg_count, sizeof(D3D11_VIDEO_DECODER_CONFIG));
437 if (cfg_list == NULL)
438 return AVERROR(ENOMEM);
439 for (i = 0; i < cfg_count; i++) {
440 hr = ID3D11VideoDevice_GetVideoDecoderConfig(video_device, desc, i, &cfg_list[i]);
442 av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr);
444 return AVERROR(EINVAL);
448 ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
450 *config = cfg_list[ret];
455 static DXGI_FORMAT d3d11va_map_sw_to_hw_format(enum AVPixelFormat pix_fmt)
458 case AV_PIX_FMT_NV12: return DXGI_FORMAT_NV12;
459 case AV_PIX_FMT_P010: return DXGI_FORMAT_P010;
460 case AV_PIX_FMT_YUV420P: return DXGI_FORMAT_420_OPAQUE;
461 default: return DXGI_FORMAT_UNKNOWN;
465 static int d3d11va_create_decoder(AVCodecContext *avctx)
467 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
469 unsigned guid_count, i;
471 D3D11_VIDEO_DECODER_DESC desc = { 0 };
472 D3D11_VIDEO_DECODER_CONFIG config;
473 AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
474 AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
475 AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
476 DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(frames_ctx->sw_format);
477 D3D11_TEXTURE2D_DESC texdesc;
481 if (!frames_hwctx->texture) {
482 av_log(avctx, AV_LOG_ERROR, "AVD3D11VAFramesContext.texture not set.\n");
483 return AVERROR(EINVAL);
485 ID3D11Texture2D_GetDesc(frames_hwctx->texture, &texdesc);
487 guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device);
488 guid_list = av_malloc_array(guid_count, sizeof(*guid_list));
489 if (guid_list == NULL || guid_count == 0) {
490 av_log(avctx, AV_LOG_ERROR, "Failed to get the decoder GUIDs\n");
492 return AVERROR(EINVAL);
494 for (i = 0; i < guid_count; i++) {
495 hr = ID3D11VideoDevice_GetVideoDecoderProfile(device_hwctx->video_device, i, &guid_list[i]);
497 av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder GUID %d\n", i);
499 return AVERROR(EINVAL);
503 ret = dxva_get_decoder_guid(avctx, device_hwctx->video_device, &surface_format,
504 guid_count, guid_list, &decoder_guid);
507 return AVERROR(EINVAL);
509 desc.SampleWidth = avctx->coded_width;
510 desc.SampleHeight = avctx->coded_height;
511 desc.OutputFormat = surface_format;
512 desc.Guid = decoder_guid;
514 ret = d3d11va_get_decoder_configuration(avctx, device_hwctx->video_device, &desc, &config);
516 return AVERROR(EINVAL);
518 sctx->d3d11_views = av_mallocz_array(texdesc.ArraySize, sizeof(sctx->d3d11_views[0]));
519 if (!sctx->d3d11_views)
520 return AVERROR(ENOMEM);
521 sctx->nb_d3d11_views = texdesc.ArraySize;
523 for (i = 0; i < sctx->nb_d3d11_views; i++) {
524 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = {
525 .DecodeProfile = decoder_guid,
526 .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D,
531 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView(device_hwctx->video_device,
532 (ID3D11Resource*) frames_hwctx->texture,
534 (ID3D11VideoDecoderOutputView**) &sctx->d3d11_views[i]);
536 av_log(avctx, AV_LOG_ERROR, "Could not create the decoder output view %d\n", i);
537 return AVERROR_UNKNOWN;
541 hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &desc,
542 &config, &sctx->d3d11_decoder);
544 av_log(avctx, AV_LOG_ERROR, "Failed to create D3D11VA video decoder\n");
545 return AVERROR(EINVAL);
548 sctx->d3d11_config = config;
549 sctx->d3d11_texture = frames_hwctx->texture;
551 sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->d3d11_decoder);
552 if (!sctx->decoder_ref)
553 return AVERROR(ENOMEM);
560 static void ff_dxva2_lock(AVCodecContext *avctx)
563 if (ff_dxva2_is_d3d11(avctx)) {
564 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
565 AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
566 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
567 WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
568 if (sctx->device_ctx) {
569 AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
570 hwctx->lock(hwctx->lock_ctx);
576 static void ff_dxva2_unlock(AVCodecContext *avctx)
579 if (ff_dxva2_is_d3d11(avctx)) {
580 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
581 AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
582 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
583 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
584 if (sctx->device_ctx) {
585 AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
586 hwctx->unlock(hwctx->lock_ctx);
592 int ff_dxva2_common_frame_params(AVCodecContext *avctx,
593 AVBufferRef *hw_frames_ctx)
595 AVHWFramesContext *frames_ctx = (AVHWFramesContext *)hw_frames_ctx->data;
596 AVHWDeviceContext *device_ctx = frames_ctx->device_ctx;
597 int surface_alignment, num_surfaces;
599 if (device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
600 frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
601 } else if (device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
602 frames_ctx->format = AV_PIX_FMT_D3D11;
604 return AVERROR(EINVAL);
607 /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
608 but it causes issues for H.264 on certain AMD GPUs..... */
609 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
610 surface_alignment = 32;
611 /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
612 all coding features have enough room to work with */
613 else if (avctx->codec_id == AV_CODEC_ID_HEVC || avctx->codec_id == AV_CODEC_ID_AV1)
614 surface_alignment = 128;
616 surface_alignment = 16;
618 /* 1 base work surface */
621 /* add surfaces based on number of possible refs */
622 if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC)
624 else if (avctx->codec_id == AV_CODEC_ID_VP9 || avctx->codec_id == AV_CODEC_ID_AV1)
629 frames_ctx->sw_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
630 AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
631 frames_ctx->width = FFALIGN(avctx->coded_width, surface_alignment);
632 frames_ctx->height = FFALIGN(avctx->coded_height, surface_alignment);
633 frames_ctx->initial_pool_size = num_surfaces;
637 if (frames_ctx->format == AV_PIX_FMT_DXVA2_VLD) {
638 AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
640 frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
645 if (frames_ctx->format == AV_PIX_FMT_D3D11) {
646 AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
648 frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
655 int ff_dxva2_decode_init(AVCodecContext *avctx)
657 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
658 AVHWFramesContext *frames_ctx;
659 enum AVHWDeviceType dev_type = avctx->hwaccel->pix_fmt == AV_PIX_FMT_DXVA2_VLD
660 ? AV_HWDEVICE_TYPE_DXVA2 : AV_HWDEVICE_TYPE_D3D11VA;
664 if (avctx->hwaccel_context)
667 // (avctx->pix_fmt is not updated yet at this point)
668 sctx->pix_fmt = avctx->hwaccel->pix_fmt;
670 ret = ff_decode_get_hw_frames_ctx(avctx, dev_type);
674 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
675 sctx->device_ctx = frames_ctx->device_ctx;
677 if (frames_ctx->format != sctx->pix_fmt) {
678 av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
679 ret = AVERROR(EINVAL);
684 if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
685 AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
686 AVD3D11VAContext *d3d11_ctx = &sctx->ctx.d3d11va;
688 ff_dxva2_lock(avctx);
689 ret = d3d11va_create_decoder(avctx);
690 ff_dxva2_unlock(avctx);
694 d3d11_ctx->decoder = sctx->d3d11_decoder;
695 d3d11_ctx->video_context = device_hwctx->video_context;
696 d3d11_ctx->cfg = &sctx->d3d11_config;
697 d3d11_ctx->surface_count = sctx->nb_d3d11_views;
698 d3d11_ctx->surface = sctx->d3d11_views;
699 d3d11_ctx->workaround = sctx->workaround;
700 d3d11_ctx->context_mutex = INVALID_HANDLE_VALUE;
705 if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
706 AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
707 struct dxva_context *dxva_ctx = &sctx->ctx.dxva2;
709 ff_dxva2_lock(avctx);
710 ret = dxva2_create_decoder(avctx);
711 ff_dxva2_unlock(avctx);
715 dxva_ctx->decoder = sctx->dxva2_decoder;
716 dxva_ctx->cfg = &sctx->dxva2_config;
717 dxva_ctx->surface = frames_hwctx->surfaces;
718 dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
719 dxva_ctx->workaround = sctx->workaround;
726 ff_dxva2_decode_uninit(avctx);
730 int ff_dxva2_decode_uninit(AVCodecContext *avctx)
732 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
735 av_buffer_unref(&sctx->decoder_ref);
738 for (i = 0; i < sctx->nb_d3d11_views; i++) {
739 if (sctx->d3d11_views[i])
740 ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[i]);
742 av_freep(&sctx->d3d11_views);
746 if (sctx->dxva2_service)
747 IDirectXVideoDecoderService_Release(sctx->dxva2_service);
753 static void *get_surface(const AVCodecContext *avctx, const AVFrame *frame)
756 if (frame->format == AV_PIX_FMT_D3D11) {
757 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
758 intptr_t index = (intptr_t)frame->data[1];
759 if (index < 0 || index >= sctx->nb_d3d11_views ||
760 sctx->d3d11_texture != (ID3D11Texture2D *)frame->data[0]) {
761 av_log((void *)avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
764 return sctx->d3d11_views[index];
767 return frame->data[3];
770 unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
771 const AVDXVAContext *ctx,
772 const AVFrame *frame)
774 void *surface = get_surface(avctx, frame);
778 if (avctx->pix_fmt == AV_PIX_FMT_D3D11)
779 return (intptr_t)frame->data[1];
780 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
781 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
782 ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*) surface, &viewDesc);
783 return viewDesc.Texture2D.ArraySlice;
787 for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++) {
788 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
797 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
799 DECODER_BUFFER_DESC *dsc,
800 unsigned type, const void *data, unsigned size,
809 if (ff_dxva2_is_d3d11(avctx))
810 hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
811 D3D11VA_CONTEXT(ctx)->decoder,
813 &dxva_size, &dxva_data);
816 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
817 hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
818 &dxva_data, &dxva_size);
821 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%x\n",
825 if (size <= dxva_size) {
826 memcpy(dxva_data, data, size);
829 if (ff_dxva2_is_d3d11(avctx)) {
830 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
831 memset(dsc11, 0, sizeof(*dsc11));
832 dsc11->BufferType = type;
833 dsc11->DataSize = size;
834 dsc11->NumMBsInBuffer = mb_count;
838 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
839 DXVA2_DecodeBufferDesc *dsc2 = dsc;
840 memset(dsc2, 0, sizeof(*dsc2));
841 dsc2->CompressedBufferType = type;
842 dsc2->DataSize = size;
843 dsc2->NumMBsInBuffer = mb_count;
849 av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
854 if (ff_dxva2_is_d3d11(avctx))
855 hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
858 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
859 hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
862 av_log(avctx, AV_LOG_ERROR,
863 "Failed to release buffer type %u: 0x%x\n",
870 static int frame_add_buf(AVFrame *frame, AVBufferRef *ref)
874 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
875 if (!frame->buf[i]) {
876 frame->buf[i] = av_buffer_ref(ref);
877 return frame->buf[i] ? 0 : AVERROR(ENOMEM);
881 // For now we expect that the caller does not use more than
882 // AV_NUM_DATA_POINTERS-1 buffers if the user uses a custom pool.
883 return AVERROR(EINVAL);
886 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
887 const void *pp, unsigned pp_size,
888 const void *qm, unsigned qm_size,
889 int (*commit_bs_si)(AVCodecContext *,
890 DECODER_BUFFER_DESC *bs,
891 DECODER_BUFFER_DESC *slice))
893 AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
894 unsigned buffer_count = 0;
896 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
899 DXVA2_DecodeBufferDesc buffer2[4];
901 DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
902 int result, runs = 0;
905 FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
907 if (sctx->decoder_ref) {
908 result = frame_add_buf(frame, sctx->decoder_ref);
914 ff_dxva2_lock(avctx);
916 if (ff_dxva2_is_d3d11(avctx))
917 hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
918 get_surface(avctx, frame),
922 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
923 hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
924 get_surface(avctx, frame),
927 if (hr != E_PENDING || ++runs > 50)
929 ff_dxva2_unlock(avctx);
934 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", (unsigned)hr);
935 ff_dxva2_unlock(avctx);
940 if (ff_dxva2_is_d3d11(avctx)) {
941 buffer = &buffer11[buffer_count];
942 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
946 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
947 buffer = &buffer2[buffer_count];
948 type = DXVA2_PictureParametersBufferType;
951 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
955 av_log(avctx, AV_LOG_ERROR,
956 "Failed to add picture parameter buffer\n");
963 if (ff_dxva2_is_d3d11(avctx)) {
964 buffer = &buffer11[buffer_count];
965 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
969 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
970 buffer = &buffer2[buffer_count];
971 type = DXVA2_InverseQuantizationMatrixBufferType;
974 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
978 av_log(avctx, AV_LOG_ERROR,
979 "Failed to add inverse quantization matrix buffer\n");
986 if (ff_dxva2_is_d3d11(avctx)) {
987 buffer = &buffer11[buffer_count + 0];
988 buffer_slice = &buffer11[buffer_count + 1];
992 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
993 buffer = &buffer2[buffer_count + 0];
994 buffer_slice = &buffer2[buffer_count + 1];
998 result = commit_bs_si(avctx,
1002 av_log(avctx, AV_LOG_ERROR,
1003 "Failed to add bitstream or slice control buffer\n");
1008 /* TODO Film Grain when possible */
1010 assert(buffer_count == 1 + (qm_size > 0) + 2);
1013 if (ff_dxva2_is_d3d11(avctx))
1014 hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
1015 D3D11VA_CONTEXT(ctx)->decoder,
1016 buffer_count, buffer11);
1019 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
1020 DXVA2_DecodeExecuteParams exec = {
1021 .NumCompBuffers = buffer_count,
1022 .pCompressedBuffers = buffer2,
1023 .pExtensionData = NULL,
1025 hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
1029 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", (unsigned)hr);
1035 if (ff_dxva2_is_d3d11(avctx))
1036 hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
1039 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
1040 hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
1042 ff_dxva2_unlock(avctx);
1044 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", (unsigned)hr);
1051 int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
1054 return avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD ||
1055 avctx->pix_fmt == AV_PIX_FMT_D3D11;