38 DEFINE_GUID(ff_DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
39 DEFINE_GUID(ff_DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
40 DEFINE_GUID(ff_DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
41 DEFINE_GUID(ff_DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
42 DEFINE_GUID(ff_DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
43 DEFINE_GUID(ff_DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
44 DEFINE_GUID(ff_DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
45 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main, 0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
46 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
47 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_Profile0,0x463707f8,0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
48 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_10bit_Profile2,0xa4c749ef,0x6ecf,0x48aa,0x84,0x48,0x50,0xa7,0xa1,0x16,0x5f,0xf7);
49 DEFINE_GUID(ff_DXVA2_ModeAV1_VLD_Profile0,0xb8be4ccb,0xcf53,0x46ba,0x8d,0x59,0xd6,0xb8,0xa6,0xda,0x5d,0x2a);
50 DEFINE_GUID(ff_DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
51 DEFINE_GUID(ff_GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
52 DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
112 const void *cfg_list,
116 unsigned i, best_score = 0;
119 for (
i = 0;
i < cfg_count;
i++) {
121 UINT ConfigBitstreamRaw = 0;
122 GUID guidConfigBitstreamEncryption;
126 D3D11_VIDEO_DECODER_CONFIG *cfg = &((D3D11_VIDEO_DECODER_CONFIG *)cfg_list)[
i];
127 ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
128 guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
133 DXVA2_ConfigPictureDecode *cfg = &((DXVA2_ConfigPictureDecode *)cfg_list)[
i];
134 ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
135 guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
139 if (ConfigBitstreamRaw == 1)
145 if (IsEqualGUID(&guidConfigBitstreamEncryption, &ff_DXVA2_NoEncrypt))
147 if (score > best_score) {
162 static int d3d11va_validate_output(
void *service, GUID guid,
const void *surface_format)
166 hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice *)service,
168 *(DXGI_FORMAT *)surface_format,
175 static int dxva2_validate_output(
void *decoder_service, GUID guid,
const void *surface_format)
179 unsigned j, target_count;
180 D3DFORMAT *target_list;
181 hr = IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService *)decoder_service, &guid, &target_count, &target_list);
183 for (j = 0; j < target_count; j++) {
184 const D3DFORMAT
format = target_list[j];
185 if (
format == *(D3DFORMAT *)surface_format) {
190 CoTaskMemFree(target_list);
217 unsigned guid_count,
const GUID *guid_list)
224 for (
i = 0;
i < guid_count;
i++) {
225 const GUID *guid = &guid_list[
i];
228 "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
229 (
unsigned) guid->Data1, guid->Data2, guid->Data3,
230 guid->Data4[0], guid->Data4[1],
231 guid->Data4[2], guid->Data4[3],
232 guid->Data4[4], guid->Data4[5],
233 guid->Data4[6], guid->Data4[7]);
241 if (d3d11va_validate_output(service, *guid, &
format))
249 MKTAG(
'P',
'0',
'1',
'0')};
252 if (dxva2_validate_output(service, *guid, &
formats[
i]))
262 unsigned guid_count,
const GUID *guid_list, GUID *decoder_guid)
269 *decoder_guid = ff_GUID_NULL;
276 for (j = 0; j < guid_count; j++) {
277 if (IsEqualGUID(
mode->guid, &guid_list[j]))
285 validate = d3d11va_validate_output(service, *
mode->guid, surface_format);
289 validate = dxva2_validate_output(service, *
mode->guid, surface_format);
292 *decoder_guid = *
mode->guid;
297 if (IsEqualGUID(decoder_guid, &ff_GUID_NULL)) {
302 if (IsEqualGUID(decoder_guid, &ff_DXVADDI_Intel_ModeH264_E))
310 IUnknown_Release((IUnknown *)opaque);
320 static int dxva2_get_decoder_configuration(
AVCodecContext *avctx,
const GUID *device_guid,
321 const DXVA2_VideoDesc *
desc,
322 DXVA2_ConfigPictureDecode *
config)
326 DXVA2_ConfigPictureDecode *cfg_list;
330 hr = IDirectXVideoDecoderService_GetDecoderConfigurations(sctx->dxva2_service, device_guid,
desc,
NULL, &cfg_count, &cfg_list);
339 CoTaskMemFree(cfg_list);
350 MKTAG(
'P',
'0',
'1',
'0') :
MKTAG(
'N',
'V',
'1',
'2');
351 DXVA2_VideoDesc
desc = { 0 };
352 DXVA2_ConfigPictureDecode
config;
355 HANDLE device_handle;
360 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
367 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
368 &ff_IID_IDirectXVideoDecoderService,
369 (
void **)&sctx->dxva2_service);
370 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
376 hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(sctx->dxva2_service, &guid_count, &guid_list);
383 guid_count, guid_list, &device_guid);
384 CoTaskMemFree(guid_list);
391 desc.Format = surface_format;
393 ret = dxva2_get_decoder_configuration(avctx, &device_guid, &
desc, &
config);
398 hr = IDirectXVideoDecoderService_CreateVideoDecoder(sctx->dxva2_service, &device_guid,
400 frames_hwctx->nb_surfaces, &sctx->dxva2_decoder);
406 sctx->dxva2_config =
config;
421 static int d3d11va_get_decoder_configuration(
AVCodecContext *avctx,
422 ID3D11VideoDevice *video_device,
423 const D3D11_VIDEO_DECODER_DESC *
desc,
424 D3D11_VIDEO_DECODER_CONFIG *
config)
426 unsigned cfg_count = 0;
427 D3D11_VIDEO_DECODER_CONFIG *cfg_list =
NULL;
431 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(video_device,
desc, &cfg_count);
437 cfg_list =
av_malloc_array(cfg_count,
sizeof(D3D11_VIDEO_DECODER_CONFIG));
438 if (cfg_list ==
NULL)
440 for (
i = 0;
i < cfg_count;
i++) {
441 hr = ID3D11VideoDevice_GetVideoDecoderConfig(video_device,
desc,
i, &cfg_list[
i]);
443 av_log(avctx,
AV_LOG_ERROR,
"Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr);
462 default:
return DXGI_FORMAT_UNKNOWN;
470 unsigned guid_count,
i;
472 D3D11_VIDEO_DECODER_DESC
desc = { 0 };
473 D3D11_VIDEO_DECODER_CONFIG
config;
477 DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(frames_ctx->
sw_format);
478 D3D11_TEXTURE2D_DESC texdesc;
482 if (!frames_hwctx->texture) {
486 ID3D11Texture2D_GetDesc(frames_hwctx->texture, &texdesc);
488 guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device);
490 if (guid_list ==
NULL || guid_count == 0) {
495 for (
i = 0;
i < guid_count;
i++) {
496 hr = ID3D11VideoDevice_GetVideoDecoderProfile(device_hwctx->video_device,
i, &guid_list[
i]);
505 guid_count, guid_list, &decoder_guid);
512 desc.OutputFormat = surface_format;
513 desc.Guid = decoder_guid;
515 ret = d3d11va_get_decoder_configuration(avctx, device_hwctx->video_device, &
desc, &
config);
519 sctx->d3d11_views =
av_calloc(texdesc.ArraySize,
sizeof(sctx->d3d11_views[0]));
520 if (!sctx->d3d11_views)
522 sctx->nb_d3d11_views = texdesc.ArraySize;
524 for (
i = 0;
i < sctx->nb_d3d11_views;
i++) {
525 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = {
526 .DecodeProfile = decoder_guid,
527 .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D,
532 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView(device_hwctx->video_device,
533 (ID3D11Resource*) frames_hwctx->texture,
535 (ID3D11VideoDecoderOutputView**) &sctx->d3d11_views[
i]);
542 hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &
desc,
543 &
config, &sctx->d3d11_decoder);
549 sctx->d3d11_config =
config;
550 sctx->d3d11_texture = frames_hwctx->texture;
598 int surface_alignment, num_surfaces;
611 surface_alignment = 32;
615 surface_alignment = 128;
617 surface_alignment = 16;
641 frames_hwctx->
surface_type = DXVA2_VideoDecoderRenderTarget;
649 frames_hwctx->
BindFlags |= D3D11_BIND_DECODER;
690 ret = d3d11va_create_decoder(avctx);
695 d3d11_ctx->
decoder = sctx->d3d11_decoder;
697 d3d11_ctx->
cfg = &sctx->d3d11_config;
699 d3d11_ctx->
surface = sctx->d3d11_views;
711 ret = dxva2_create_decoder(avctx);
716 dxva_ctx->
decoder = sctx->dxva2_decoder;
717 dxva_ctx->
cfg = &sctx->dxva2_config;
739 for (
i = 0;
i < sctx->nb_d3d11_views;
i++) {
740 if (sctx->d3d11_views[
i])
741 ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[
i]);
747 if (sctx->dxva2_service)
748 IDirectXVideoDecoderService_Release(sctx->dxva2_service);
760 if (index < 0 || index >= sctx->nb_d3d11_views ||
761 sctx->d3d11_texture != (ID3D11Texture2D *)
frame->data[0]) {
765 return sctx->d3d11_views[
index];
768 return frame->data[3];
785 return (intptr_t)
frame->data[1];
787 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
788 ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*)
surface, &viewDesc);
789 return viewDesc.Texture2D.ArraySlice;
809 void *dxva_data =
NULL;
819 &dxva_size, &dxva_data);
824 &dxva_data, &dxva_size);
831 if (dxva_data &&
size <= dxva_size) {
836 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
837 memset(dsc11, 0,
sizeof(*dsc11));
838 dsc11->BufferType =
type;
839 dsc11->DataSize =
size;
840 dsc11->NumMBsInBuffer = mb_count;
845 DXVA2_DecodeBufferDesc *dsc2 = dsc;
846 memset(dsc2, 0,
sizeof(*dsc2));
847 dsc2->CompressedBufferType =
type;
848 dsc2->DataSize =
size;
849 dsc2->NumMBsInBuffer = mb_count;
869 "Failed to release buffer type %u: 0x%x\n",
893 const void *pp,
unsigned pp_size,
894 const void *qm,
unsigned qm_size,
900 unsigned buffer_count = 0;
902 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
905 DXVA2_DecodeBufferDesc buffer2[4];
933 if (hr != E_PENDING || ++runs > 50)
947 buffer = &buffer11[buffer_count];
948 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
953 buffer = &buffer2[buffer_count];
954 type = DXVA2_PictureParametersBufferType;
962 "Failed to add picture parameter buffer\n");
970 buffer = &buffer11[buffer_count];
971 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
976 buffer = &buffer2[buffer_count];
977 type = DXVA2_InverseQuantizationMatrixBufferType;
985 "Failed to add inverse quantization matrix buffer\n");
993 buffer = &buffer11[buffer_count + 0];
994 buffer_slice = &buffer11[buffer_count + 1];
999 buffer = &buffer2[buffer_count + 0];
1000 buffer_slice = &buffer2[buffer_count + 1];
1004 result = commit_bs_si(avctx,
1009 "Failed to add bitstream or slice control buffer\n");
1016 av_assert0(buffer_count == 1 + (qm_size > 0) + 2);
1022 buffer_count, buffer11);
1026 DXVA2_DecodeExecuteParams exec = {
1027 .NumCompBuffers = buffer_count,
1028 .pCompressedBuffers = buffer2,
1029 .pExtensionData =
NULL,