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_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
49 DEFINE_GUID(ff_GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
50 DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
105 const void *cfg_list,
109 unsigned i, best_score = 0;
112 for (
i = 0;
i < cfg_count;
i++) {
114 UINT ConfigBitstreamRaw;
115 GUID guidConfigBitstreamEncryption;
119 D3D11_VIDEO_DECODER_CONFIG *cfg = &((D3D11_VIDEO_DECODER_CONFIG *)cfg_list)[
i];
120 ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
121 guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
126 DXVA2_ConfigPictureDecode *cfg = &((DXVA2_ConfigPictureDecode *)cfg_list)[
i];
127 ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
128 guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
132 if (ConfigBitstreamRaw == 1)
138 if (IsEqualGUID(&guidConfigBitstreamEncryption, &ff_DXVA2_NoEncrypt))
140 if (score > best_score) {
155 static int d3d11va_validate_output(
void *service, GUID guid,
const void *surface_format)
159 hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice *)service,
161 *(DXGI_FORMAT *)surface_format,
168 static int dxva2_validate_output(
void *decoder_service, GUID guid,
const void *surface_format)
172 unsigned j, target_count;
173 D3DFORMAT *target_list;
174 hr = IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService *)decoder_service, &guid, &target_count, &target_list);
176 for (j = 0; j < target_count; j++) {
177 const D3DFORMAT
format = target_list[j];
178 if (
format == *(D3DFORMAT *)surface_format) {
183 CoTaskMemFree(target_list);
210 unsigned guid_count,
const GUID *guid_list)
217 for (
i = 0;
i < guid_count;
i++) {
218 const GUID *guid = &guid_list[
i];
221 "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
222 (
unsigned) guid->Data1, guid->Data2, guid->Data3,
223 guid->Data4[0], guid->Data4[1],
224 guid->Data4[2], guid->Data4[3],
225 guid->Data4[4], guid->Data4[5],
226 guid->Data4[6], guid->Data4[7]);
234 if (d3d11va_validate_output(service, *guid, &
format))
242 MKTAG(
'P',
'0',
'1',
'0')};
245 if (dxva2_validate_output(service, *guid, &
formats[
i]))
255 unsigned guid_count,
const GUID *guid_list, GUID *decoder_guid)
262 *decoder_guid = ff_GUID_NULL;
269 for (j = 0; j < guid_count; j++) {
270 if (IsEqualGUID(
mode->guid, &guid_list[j]))
278 validate = d3d11va_validate_output(service, *
mode->guid, surface_format);
282 validate = dxva2_validate_output(service, *
mode->guid, surface_format);
285 *decoder_guid = *
mode->guid;
290 if (IsEqualGUID(decoder_guid, &ff_GUID_NULL)) {
295 if (IsEqualGUID(decoder_guid, &ff_DXVADDI_Intel_ModeH264_E))
303 IUnknown_Release((IUnknown *)opaque);
313 static int dxva2_get_decoder_configuration(
AVCodecContext *avctx,
const GUID *device_guid,
314 const DXVA2_VideoDesc *
desc,
315 DXVA2_ConfigPictureDecode *config)
319 DXVA2_ConfigPictureDecode *cfg_list;
323 hr = IDirectXVideoDecoderService_GetDecoderConfigurations(sctx->dxva2_service, device_guid,
desc,
NULL, &cfg_count, &cfg_list);
331 *config = cfg_list[
ret];
332 CoTaskMemFree(cfg_list);
343 MKTAG(
'P',
'0',
'1',
'0') :
MKTAG(
'N',
'V',
'1',
'2');
344 DXVA2_VideoDesc
desc = { 0 };
345 DXVA2_ConfigPictureDecode config;
348 HANDLE device_handle;
353 hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
360 hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
361 &ff_IID_IDirectXVideoDecoderService,
362 (
void **)&sctx->dxva2_service);
363 IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
369 hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(sctx->dxva2_service, &guid_count, &guid_list);
376 guid_count, guid_list, &device_guid);
377 CoTaskMemFree(guid_list);
384 desc.Format = surface_format;
386 ret = dxva2_get_decoder_configuration(avctx, &device_guid, &
desc, &config);
391 hr = IDirectXVideoDecoderService_CreateVideoDecoder(sctx->dxva2_service, &device_guid,
392 &
desc, &config, frames_hwctx->surfaces,
393 frames_hwctx->nb_surfaces, &sctx->dxva2_decoder);
399 sctx->dxva2_config = config;
414 static int d3d11va_get_decoder_configuration(
AVCodecContext *avctx,
415 ID3D11VideoDevice *video_device,
416 const D3D11_VIDEO_DECODER_DESC *
desc,
417 D3D11_VIDEO_DECODER_CONFIG *config)
419 unsigned cfg_count = 0;
420 D3D11_VIDEO_DECODER_CONFIG *cfg_list =
NULL;
424 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(video_device,
desc, &cfg_count);
430 cfg_list =
av_malloc_array(cfg_count,
sizeof(D3D11_VIDEO_DECODER_CONFIG));
431 if (cfg_list ==
NULL)
433 for (
i = 0;
i < cfg_count;
i++) {
434 hr = ID3D11VideoDevice_GetVideoDecoderConfig(video_device,
desc,
i, &cfg_list[
i]);
436 av_log(avctx,
AV_LOG_ERROR,
"Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr);
444 *config = cfg_list[
ret];
455 default:
return DXGI_FORMAT_UNKNOWN;
463 unsigned guid_count,
i;
465 D3D11_VIDEO_DECODER_DESC
desc = { 0 };
466 D3D11_VIDEO_DECODER_CONFIG config;
470 DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(frames_ctx->
sw_format);
471 D3D11_TEXTURE2D_DESC texdesc;
475 if (!frames_hwctx->texture) {
479 ID3D11Texture2D_GetDesc(frames_hwctx->texture, &texdesc);
481 guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device);
483 if (guid_list ==
NULL || guid_count == 0) {
488 for (
i = 0;
i < guid_count;
i++) {
489 hr = ID3D11VideoDevice_GetVideoDecoderProfile(device_hwctx->video_device,
i, &guid_list[
i]);
498 guid_count, guid_list, &decoder_guid);
505 desc.OutputFormat = surface_format;
506 desc.Guid = decoder_guid;
508 ret = d3d11va_get_decoder_configuration(avctx, device_hwctx->video_device, &
desc, &config);
512 sctx->d3d11_views =
av_mallocz_array(texdesc.ArraySize,
sizeof(sctx->d3d11_views[0]));
513 if (!sctx->d3d11_views)
515 sctx->nb_d3d11_views = texdesc.ArraySize;
517 for (
i = 0;
i < sctx->nb_d3d11_views;
i++) {
518 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = {
519 .DecodeProfile = decoder_guid,
520 .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D,
525 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView(device_hwctx->video_device,
526 (ID3D11Resource*) frames_hwctx->texture,
528 (ID3D11VideoDecoderOutputView**) &sctx->d3d11_views[
i]);
535 hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &
desc,
536 &config, &sctx->d3d11_decoder);
542 sctx->d3d11_config = config;
543 sctx->d3d11_texture = frames_hwctx->texture;
591 int surface_alignment, num_surfaces;
604 surface_alignment = 32;
608 surface_alignment = 128;
610 surface_alignment = 16;
634 frames_hwctx->
surface_type = DXVA2_VideoDecoderRenderTarget;
642 frames_hwctx->
BindFlags |= D3D11_BIND_DECODER;
683 ret = d3d11va_create_decoder(avctx);
688 d3d11_ctx->
decoder = sctx->d3d11_decoder;
690 d3d11_ctx->
cfg = &sctx->d3d11_config;
692 d3d11_ctx->
surface = sctx->d3d11_views;
704 ret = dxva2_create_decoder(avctx);
709 dxva_ctx->
decoder = sctx->dxva2_decoder;
710 dxva_ctx->
cfg = &sctx->dxva2_config;
732 for (
i = 0;
i < sctx->nb_d3d11_views;
i++) {
733 if (sctx->d3d11_views[
i])
734 ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[
i]);
740 if (sctx->dxva2_service)
741 IDirectXVideoDecoderService_Release(sctx->dxva2_service);
753 if (index < 0 || index >= sctx->nb_d3d11_views ||
754 sctx->d3d11_texture != (ID3D11Texture2D *)
frame->data[0]) {
758 return sctx->d3d11_views[
index];
761 return frame->data[3];
773 return (intptr_t)
frame->data[1];
775 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
776 ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*)
surface, &viewDesc);
777 return viewDesc.Texture2D.ArraySlice;
781 for (
i = 0;
i < DXVA_CONTEXT_COUNT(avctx,
ctx);
i++) {
807 &dxva_size, &dxva_data);
812 &dxva_data, &dxva_size);
819 if (
size <= dxva_size) {
824 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
825 memset(dsc11, 0,
sizeof(*dsc11));
826 dsc11->BufferType =
type;
827 dsc11->DataSize =
size;
828 dsc11->NumMBsInBuffer = mb_count;
833 DXVA2_DecodeBufferDesc *dsc2 = dsc;
834 memset(dsc2, 0,
sizeof(*dsc2));
835 dsc2->CompressedBufferType =
type;
836 dsc2->DataSize =
size;
837 dsc2->NumMBsInBuffer = mb_count;
857 "Failed to release buffer type %u: 0x%x\n",
881 const void *pp,
unsigned pp_size,
882 const void *qm,
unsigned qm_size,
888 unsigned buffer_count = 0;
890 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
893 DXVA2_DecodeBufferDesc buffer2[4];
921 if (hr != E_PENDING || ++runs > 50)
935 buffer = &buffer11[buffer_count];
936 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
941 buffer = &buffer2[buffer_count];
942 type = DXVA2_PictureParametersBufferType;
950 "Failed to add picture parameter buffer\n");
958 buffer = &buffer11[buffer_count];
959 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
964 buffer = &buffer2[buffer_count];
965 type = DXVA2_InverseQuantizationMatrixBufferType;
973 "Failed to add inverse quantization matrix buffer\n");
981 buffer = &buffer11[buffer_count + 0];
982 buffer_slice = &buffer11[buffer_count + 1];
987 buffer = &buffer2[buffer_count + 0];
988 buffer_slice = &buffer2[buffer_count + 1];
992 result = commit_bs_si(avctx,
997 "Failed to add bitstream or slice control buffer\n");
1004 assert(buffer_count == 1 + (qm_size > 0) + 2);
1010 buffer_count, buffer11);
1014 DXVA2_DecodeExecuteParams exec = {
1015 .NumCompBuffers = buffer_count,
1016 .pCompressedBuffers = buffer2,
1017 .pExtensionData =
NULL,