You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1058 lines
36KB

  1. /*
  2. * DXVA2 HW acceleration.
  3. *
  4. * copyright (c) 2010 Laurent Aimar
  5. *
  6. * This file is part of FFmpeg.
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. #include <assert.h>
  23. #include <string.h>
  24. #include <initguid.h>
  25. #include "libavutil/common.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/time.h"
  28. #include "avcodec.h"
  29. #include "dxva2_internal.h"
  30. /* define all the GUIDs used directly here,
  31. to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
  32. DEFINE_GUID(ff_DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
  33. DEFINE_GUID(ff_DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
  34. DEFINE_GUID(ff_DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  35. DEFINE_GUID(ff_DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  36. DEFINE_GUID(ff_DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
  37. DEFINE_GUID(ff_DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  38. DEFINE_GUID(ff_DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  39. DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main, 0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
  40. DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
  41. DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_Profile0,0x463707f8,0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
  42. DEFINE_GUID(ff_DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  43. DEFINE_GUID(ff_GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
  44. DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
  45. typedef struct dxva_mode {
  46. const GUID *guid;
  47. enum AVCodecID codec;
  48. // List of supported profiles, terminated by a FF_PROFILE_UNKNOWN entry.
  49. // If NULL, don't check profile.
  50. const int *profiles;
  51. } dxva_mode;
  52. static const int prof_mpeg2_main[] = {FF_PROFILE_MPEG2_SIMPLE,
  53. FF_PROFILE_MPEG2_MAIN,
  54. FF_PROFILE_UNKNOWN};
  55. static const int prof_h264_high[] = {FF_PROFILE_H264_CONSTRAINED_BASELINE,
  56. FF_PROFILE_H264_MAIN,
  57. FF_PROFILE_H264_HIGH,
  58. FF_PROFILE_UNKNOWN};
  59. static const int prof_hevc_main[] = {FF_PROFILE_HEVC_MAIN,
  60. FF_PROFILE_UNKNOWN};
  61. static const int prof_hevc_main10[] = {FF_PROFILE_HEVC_MAIN_10,
  62. FF_PROFILE_UNKNOWN};
  63. static const dxva_mode dxva_modes[] = {
  64. /* MPEG-2 */
  65. { &ff_DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
  66. { &ff_DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
  67. /* H.264 */
  68. { &ff_DXVA2_ModeH264_F, AV_CODEC_ID_H264, prof_h264_high },
  69. { &ff_DXVA2_ModeH264_E, AV_CODEC_ID_H264, prof_h264_high },
  70. /* Intel specific H.264 mode */
  71. { &ff_DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264, prof_h264_high },
  72. /* VC-1 / WMV3 */
  73. { &ff_DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
  74. { &ff_DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
  75. { &ff_DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
  76. { &ff_DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
  77. /* HEVC/H.265 */
  78. { &ff_DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC, prof_hevc_main10 },
  79. { &ff_DXVA2_ModeHEVC_VLD_Main, AV_CODEC_ID_HEVC, prof_hevc_main },
  80. /* VP8/9 */
  81. { &ff_DXVA2_ModeVP9_VLD_Profile0,AV_CODEC_ID_VP9 },
  82. { NULL, 0 },
  83. };
  84. static int dxva_get_decoder_configuration(AVCodecContext *avctx,
  85. const void *cfg_list,
  86. unsigned cfg_count)
  87. {
  88. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  89. unsigned i, best_score = 0;
  90. int best_cfg = -1;
  91. for (i = 0; i < cfg_count; i++) {
  92. unsigned score;
  93. UINT ConfigBitstreamRaw;
  94. GUID guidConfigBitstreamEncryption;
  95. #if CONFIG_D3D11VA
  96. if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
  97. D3D11_VIDEO_DECODER_CONFIG *cfg = &((D3D11_VIDEO_DECODER_CONFIG *)cfg_list)[i];
  98. ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
  99. guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
  100. }
  101. #endif
  102. #if CONFIG_DXVA2
  103. if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  104. DXVA2_ConfigPictureDecode *cfg = &((DXVA2_ConfigPictureDecode *)cfg_list)[i];
  105. ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
  106. guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
  107. }
  108. #endif
  109. if (ConfigBitstreamRaw == 1)
  110. score = 1;
  111. else if (avctx->codec_id == AV_CODEC_ID_H264 && ConfigBitstreamRaw == 2)
  112. score = 2;
  113. else
  114. continue;
  115. if (IsEqualGUID(&guidConfigBitstreamEncryption, &ff_DXVA2_NoEncrypt))
  116. score += 16;
  117. if (score > best_score) {
  118. best_score = score;
  119. best_cfg = i;
  120. }
  121. }
  122. if (!best_score) {
  123. av_log(avctx, AV_LOG_VERBOSE, "No valid decoder configuration available\n");
  124. return AVERROR(EINVAL);
  125. }
  126. return best_cfg;
  127. }
  128. #if CONFIG_D3D11VA
  129. static int d3d11va_validate_output(void *service, GUID guid, const void *surface_format)
  130. {
  131. HRESULT hr;
  132. BOOL is_supported = FALSE;
  133. hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice *)service,
  134. &guid,
  135. *(DXGI_FORMAT *)surface_format,
  136. &is_supported);
  137. return SUCCEEDED(hr) && is_supported;
  138. }
  139. #endif
  140. #if CONFIG_DXVA2
  141. static int dxva2_validate_output(void *decoder_service, GUID guid, const void *surface_format)
  142. {
  143. HRESULT hr;
  144. int ret = 0;
  145. unsigned j, target_count;
  146. D3DFORMAT *target_list;
  147. hr = IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService *)decoder_service, &guid, &target_count, &target_list);
  148. if (SUCCEEDED(hr)) {
  149. for (j = 0; j < target_count; j++) {
  150. const D3DFORMAT format = target_list[j];
  151. if (format == *(D3DFORMAT *)surface_format) {
  152. ret = 1;
  153. break;
  154. }
  155. }
  156. CoTaskMemFree(target_list);
  157. }
  158. return ret;
  159. }
  160. #endif
  161. static int dxva_check_codec_compatibility(AVCodecContext *avctx, const dxva_mode *mode)
  162. {
  163. if (mode->codec != avctx->codec_id)
  164. return 0;
  165. if (mode->profiles && !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
  166. int i, found = 0;
  167. for (i = 0; mode->profiles[i] != FF_PROFILE_UNKNOWN; i++) {
  168. if (avctx->profile == mode->profiles[i]) {
  169. found = 1;
  170. break;
  171. }
  172. }
  173. if (!found)
  174. return 0;
  175. }
  176. return 1;
  177. }
  178. static void dxva_list_guids_debug(AVCodecContext *avctx, void *service,
  179. unsigned guid_count, const GUID *guid_list)
  180. {
  181. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  182. int i;
  183. av_log(avctx, AV_LOG_VERBOSE, "Decoder GUIDs reported as supported:\n");
  184. for (i = 0; i < guid_count; i++) {
  185. const GUID *guid = &guid_list[i];
  186. av_log(avctx, AV_LOG_VERBOSE,
  187. "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
  188. (unsigned) guid->Data1, guid->Data2, guid->Data3,
  189. guid->Data4[0], guid->Data4[1],
  190. guid->Data4[2], guid->Data4[3],
  191. guid->Data4[4], guid->Data4[5],
  192. guid->Data4[6], guid->Data4[7]);
  193. #if CONFIG_D3D11VA
  194. if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
  195. DXGI_FORMAT format;
  196. // We don't know the maximum valid DXGI_FORMAT, so use 200 as
  197. // arbitrary upper bound (that could become outdated).
  198. for (format = 0; format < 200; format++) {
  199. if (d3d11va_validate_output(service, *guid, &format))
  200. av_log(avctx, AV_LOG_VERBOSE, " %d", (int)format);
  201. }
  202. }
  203. #endif
  204. #if CONFIG_DXVA2
  205. if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  206. const D3DFORMAT formats[] = {MKTAG('N', 'V', '1', '2'),
  207. MKTAG('P', '0', '1', '0')};
  208. int i;
  209. for (i = 0; i < FF_ARRAY_ELEMS(formats); i++) {
  210. if (dxva2_validate_output(service, *guid, &formats[i]))
  211. av_log(avctx, AV_LOG_VERBOSE, " %d", i);
  212. }
  213. }
  214. #endif
  215. av_log(avctx, AV_LOG_VERBOSE, "\n");
  216. }
  217. }
  218. static int dxva_get_decoder_guid(AVCodecContext *avctx, void *service, void *surface_format,
  219. unsigned guid_count, const GUID *guid_list, GUID *decoder_guid)
  220. {
  221. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  222. unsigned i, j;
  223. dxva_list_guids_debug(avctx, service, guid_count, guid_list);
  224. *decoder_guid = ff_GUID_NULL;
  225. for (i = 0; dxva_modes[i].guid; i++) {
  226. const dxva_mode *mode = &dxva_modes[i];
  227. int validate;
  228. if (!dxva_check_codec_compatibility(avctx, mode))
  229. continue;
  230. for (j = 0; j < guid_count; j++) {
  231. if (IsEqualGUID(mode->guid, &guid_list[j]))
  232. break;
  233. }
  234. if (j == guid_count)
  235. continue;
  236. #if CONFIG_D3D11VA
  237. if (sctx->pix_fmt == AV_PIX_FMT_D3D11)
  238. validate = d3d11va_validate_output(service, *mode->guid, surface_format);
  239. #endif
  240. #if CONFIG_DXVA2
  241. if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  242. validate = dxva2_validate_output(service, *mode->guid, surface_format);
  243. #endif
  244. if (validate) {
  245. *decoder_guid = *mode->guid;
  246. break;
  247. }
  248. }
  249. if (IsEqualGUID(decoder_guid, &ff_GUID_NULL)) {
  250. av_log(avctx, AV_LOG_VERBOSE, "No decoder device for codec found\n");
  251. return AVERROR(EINVAL);
  252. }
  253. if (IsEqualGUID(decoder_guid, &ff_DXVADDI_Intel_ModeH264_E))
  254. sctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
  255. return 0;
  256. }
  257. static void bufref_free_interface(void *opaque, uint8_t *data)
  258. {
  259. IUnknown_Release((IUnknown *)opaque);
  260. }
  261. static AVBufferRef *bufref_wrap_interface(IUnknown *iface)
  262. {
  263. return av_buffer_create((uint8_t*)iface, 1, bufref_free_interface, iface, 0);
  264. }
  265. #if CONFIG_DXVA2
  266. static int dxva2_get_decoder_configuration(AVCodecContext *avctx, const GUID *device_guid,
  267. const DXVA2_VideoDesc *desc,
  268. DXVA2_ConfigPictureDecode *config)
  269. {
  270. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  271. unsigned cfg_count;
  272. DXVA2_ConfigPictureDecode *cfg_list;
  273. HRESULT hr;
  274. int ret;
  275. hr = IDirectXVideoDecoderService_GetDecoderConfigurations(sctx->dxva2_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
  276. if (FAILED(hr)) {
  277. av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
  278. return AVERROR(EINVAL);
  279. }
  280. ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
  281. if (ret >= 0)
  282. *config = cfg_list[ret];
  283. CoTaskMemFree(cfg_list);
  284. return ret;
  285. }
  286. static int dxva2_create_decoder(AVCodecContext *avctx)
  287. {
  288. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  289. GUID *guid_list;
  290. unsigned guid_count;
  291. GUID device_guid;
  292. D3DFORMAT surface_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
  293. MKTAG('P', '0', '1', '0') : MKTAG('N', 'V', '1', '2');
  294. DXVA2_VideoDesc desc = { 0 };
  295. DXVA2_ConfigPictureDecode config;
  296. HRESULT hr;
  297. int ret;
  298. HANDLE device_handle;
  299. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  300. AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
  301. AVDXVA2DeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
  302. hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
  303. &device_handle);
  304. if (FAILED(hr)) {
  305. av_log(avctx, AV_LOG_ERROR, "Failed to open a device handle\n");
  306. goto fail;
  307. }
  308. hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
  309. &ff_IID_IDirectXVideoDecoderService,
  310. (void **)&sctx->dxva2_service);
  311. IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
  312. if (FAILED(hr)) {
  313. av_log(avctx, AV_LOG_ERROR, "Failed to create IDirectXVideoDecoderService\n");
  314. goto fail;
  315. }
  316. hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(sctx->dxva2_service, &guid_count, &guid_list);
  317. if (FAILED(hr)) {
  318. av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder device GUIDs\n");
  319. goto fail;
  320. }
  321. ret = dxva_get_decoder_guid(avctx, sctx->dxva2_service, &surface_format,
  322. guid_count, guid_list, &device_guid);
  323. CoTaskMemFree(guid_list);
  324. if (ret < 0) {
  325. goto fail;
  326. }
  327. desc.SampleWidth = avctx->coded_width;
  328. desc.SampleHeight = avctx->coded_height;
  329. desc.Format = surface_format;
  330. ret = dxva2_get_decoder_configuration(avctx, &device_guid, &desc, &config);
  331. if (ret < 0) {
  332. goto fail;
  333. }
  334. hr = IDirectXVideoDecoderService_CreateVideoDecoder(sctx->dxva2_service, &device_guid,
  335. &desc, &config, frames_hwctx->surfaces,
  336. frames_hwctx->nb_surfaces, &sctx->dxva2_decoder);
  337. if (FAILED(hr)) {
  338. av_log(avctx, AV_LOG_ERROR, "Failed to create DXVA2 video decoder\n");
  339. goto fail;
  340. }
  341. sctx->dxva2_config = config;
  342. sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->dxva2_decoder);
  343. if (!sctx->decoder_ref)
  344. return AVERROR(ENOMEM);
  345. return 0;
  346. fail:
  347. return AVERROR(EINVAL);
  348. }
  349. #endif
  350. #if CONFIG_D3D11VA
  351. static int d3d11va_get_decoder_configuration(AVCodecContext *avctx,
  352. ID3D11VideoDevice *video_device,
  353. const D3D11_VIDEO_DECODER_DESC *desc,
  354. D3D11_VIDEO_DECODER_CONFIG *config)
  355. {
  356. unsigned cfg_count = 0;
  357. D3D11_VIDEO_DECODER_CONFIG *cfg_list = NULL;
  358. HRESULT hr;
  359. int i, ret;
  360. hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(video_device, desc, &cfg_count);
  361. if (FAILED(hr)) {
  362. av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
  363. return AVERROR(EINVAL);
  364. }
  365. cfg_list = av_malloc_array(cfg_count, sizeof(D3D11_VIDEO_DECODER_CONFIG));
  366. if (cfg_list == NULL)
  367. return AVERROR(ENOMEM);
  368. for (i = 0; i < cfg_count; i++) {
  369. hr = ID3D11VideoDevice_GetVideoDecoderConfig(video_device, desc, i, &cfg_list[i]);
  370. if (FAILED(hr)) {
  371. av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr);
  372. av_free(cfg_list);
  373. return AVERROR(EINVAL);
  374. }
  375. }
  376. ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
  377. if (ret >= 0)
  378. *config = cfg_list[ret];
  379. av_free(cfg_list);
  380. return ret;
  381. }
  382. static DXGI_FORMAT d3d11va_map_sw_to_hw_format(enum AVPixelFormat pix_fmt)
  383. {
  384. switch (pix_fmt) {
  385. case AV_PIX_FMT_NV12: return DXGI_FORMAT_NV12;
  386. case AV_PIX_FMT_P010: return DXGI_FORMAT_P010;
  387. case AV_PIX_FMT_YUV420P: return DXGI_FORMAT_420_OPAQUE;
  388. default: return DXGI_FORMAT_UNKNOWN;
  389. }
  390. }
  391. static int d3d11va_create_decoder(AVCodecContext *avctx)
  392. {
  393. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  394. GUID *guid_list;
  395. unsigned guid_count, i;
  396. GUID decoder_guid;
  397. D3D11_VIDEO_DECODER_DESC desc = { 0 };
  398. D3D11_VIDEO_DECODER_CONFIG config;
  399. AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
  400. AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
  401. AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
  402. DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(frames_ctx->sw_format);
  403. D3D11_TEXTURE2D_DESC texdesc;
  404. HRESULT hr;
  405. int ret;
  406. if (!frames_hwctx->texture) {
  407. av_log(avctx, AV_LOG_ERROR, "AVD3D11VAFramesContext.texture not set.\n");
  408. return AVERROR(EINVAL);
  409. }
  410. ID3D11Texture2D_GetDesc(frames_hwctx->texture, &texdesc);
  411. guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device);
  412. guid_list = av_malloc_array(guid_count, sizeof(*guid_list));
  413. if (guid_list == NULL || guid_count == 0) {
  414. av_log(avctx, AV_LOG_ERROR, "Failed to get the decoder GUIDs\n");
  415. av_free(guid_list);
  416. return AVERROR(EINVAL);
  417. }
  418. for (i = 0; i < guid_count; i++) {
  419. hr = ID3D11VideoDevice_GetVideoDecoderProfile(device_hwctx->video_device, i, &guid_list[i]);
  420. if (FAILED(hr)) {
  421. av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder GUID %d\n", i);
  422. av_free(guid_list);
  423. return AVERROR(EINVAL);
  424. }
  425. }
  426. ret = dxva_get_decoder_guid(avctx, device_hwctx->video_device, &surface_format,
  427. guid_count, guid_list, &decoder_guid);
  428. av_free(guid_list);
  429. if (ret < 0)
  430. return AVERROR(EINVAL);
  431. desc.SampleWidth = avctx->coded_width;
  432. desc.SampleHeight = avctx->coded_height;
  433. desc.OutputFormat = surface_format;
  434. desc.Guid = decoder_guid;
  435. ret = d3d11va_get_decoder_configuration(avctx, device_hwctx->video_device, &desc, &config);
  436. if (ret < 0)
  437. return AVERROR(EINVAL);
  438. sctx->d3d11_views = av_mallocz_array(texdesc.ArraySize, sizeof(sctx->d3d11_views[0]));
  439. if (!sctx->d3d11_views)
  440. return AVERROR(ENOMEM);
  441. sctx->nb_d3d11_views = texdesc.ArraySize;
  442. for (i = 0; i < sctx->nb_d3d11_views; i++) {
  443. D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = {
  444. .DecodeProfile = decoder_guid,
  445. .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D,
  446. .Texture2D = {
  447. .ArraySlice = i,
  448. }
  449. };
  450. hr = ID3D11VideoDevice_CreateVideoDecoderOutputView(device_hwctx->video_device,
  451. (ID3D11Resource*) frames_hwctx->texture,
  452. &viewDesc,
  453. (ID3D11VideoDecoderOutputView**) &sctx->d3d11_views[i]);
  454. if (FAILED(hr)) {
  455. av_log(avctx, AV_LOG_ERROR, "Could not create the decoder output view %d\n", i);
  456. return AVERROR_UNKNOWN;
  457. }
  458. }
  459. hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &desc,
  460. &config, &sctx->d3d11_decoder);
  461. if (FAILED(hr)) {
  462. av_log(avctx, AV_LOG_ERROR, "Failed to create D3D11VA video decoder\n");
  463. return AVERROR(EINVAL);
  464. }
  465. sctx->d3d11_config = config;
  466. sctx->d3d11_texture = frames_hwctx->texture;
  467. sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->d3d11_decoder);
  468. if (!sctx->decoder_ref)
  469. return AVERROR(ENOMEM);
  470. return 0;
  471. }
  472. #endif
  473. static void ff_dxva2_lock(AVCodecContext *avctx)
  474. {
  475. #if CONFIG_D3D11VA
  476. if (ff_dxva2_is_d3d11(avctx)) {
  477. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  478. AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
  479. if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
  480. WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
  481. if (sctx->device_ctx) {
  482. AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
  483. hwctx->lock(hwctx->lock_ctx);
  484. }
  485. }
  486. #endif
  487. }
  488. static void ff_dxva2_unlock(AVCodecContext *avctx)
  489. {
  490. #if CONFIG_D3D11VA
  491. if (ff_dxva2_is_d3d11(avctx)) {
  492. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  493. AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
  494. if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
  495. ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
  496. if (sctx->device_ctx) {
  497. AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
  498. hwctx->unlock(hwctx->lock_ctx);
  499. }
  500. }
  501. #endif
  502. }
  503. // This must work before the decoder is created.
  504. // This somehow needs to be exported to the user.
  505. static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frames_ctx)
  506. {
  507. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  508. int surface_alignment, num_surfaces;
  509. frames_ctx->format = sctx->pix_fmt;
  510. /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
  511. but it causes issues for H.264 on certain AMD GPUs..... */
  512. if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  513. surface_alignment = 32;
  514. /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
  515. all coding features have enough room to work with */
  516. else if (avctx->codec_id == AV_CODEC_ID_HEVC)
  517. surface_alignment = 128;
  518. else
  519. surface_alignment = 16;
  520. /* 4 base work surfaces */
  521. num_surfaces = 4;
  522. /* add surfaces based on number of possible refs */
  523. if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC)
  524. num_surfaces += 16;
  525. else if (avctx->codec_id == AV_CODEC_ID_VP9)
  526. num_surfaces += 8;
  527. else
  528. num_surfaces += 2;
  529. /* add extra surfaces for frame threading */
  530. if (avctx->active_thread_type & FF_THREAD_FRAME)
  531. num_surfaces += avctx->thread_count;
  532. frames_ctx->sw_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
  533. AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
  534. frames_ctx->width = FFALIGN(avctx->coded_width, surface_alignment);
  535. frames_ctx->height = FFALIGN(avctx->coded_height, surface_alignment);
  536. frames_ctx->initial_pool_size = num_surfaces;
  537. #if CONFIG_DXVA2
  538. if (frames_ctx->format == AV_PIX_FMT_DXVA2_VLD) {
  539. AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
  540. frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
  541. }
  542. #endif
  543. #if CONFIG_D3D11VA
  544. if (frames_ctx->format == AV_PIX_FMT_D3D11) {
  545. AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
  546. frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
  547. }
  548. #endif
  549. }
  550. int ff_dxva2_decode_init(AVCodecContext *avctx)
  551. {
  552. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  553. AVHWFramesContext *frames_ctx = NULL;
  554. int ret = 0;
  555. // Old API.
  556. if (avctx->hwaccel_context)
  557. return 0;
  558. // (avctx->pix_fmt is not updated yet at this point)
  559. sctx->pix_fmt = avctx->hwaccel->pix_fmt;
  560. if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
  561. av_log(avctx, AV_LOG_ERROR, "Either a hw_frames_ctx or a hw_device_ctx needs to be set for hardware decoding.\n");
  562. return AVERROR(EINVAL);
  563. }
  564. if (avctx->hw_frames_ctx) {
  565. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  566. } else {
  567. avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
  568. if (!avctx->hw_frames_ctx)
  569. return AVERROR(ENOMEM);
  570. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  571. dxva_adjust_hwframes(avctx, frames_ctx);
  572. ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  573. if (ret < 0)
  574. goto fail;
  575. }
  576. sctx->device_ctx = frames_ctx->device_ctx;
  577. if (frames_ctx->format != sctx->pix_fmt ||
  578. !((sctx->pix_fmt == AV_PIX_FMT_D3D11 && CONFIG_D3D11VA) ||
  579. (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && CONFIG_DXVA2))) {
  580. av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
  581. ret = AVERROR(EINVAL);
  582. goto fail;
  583. }
  584. #if CONFIG_D3D11VA
  585. if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
  586. AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
  587. AVD3D11VAContext *d3d11_ctx = &sctx->ctx.d3d11va;
  588. ff_dxva2_lock(avctx);
  589. ret = d3d11va_create_decoder(avctx);
  590. ff_dxva2_unlock(avctx);
  591. if (ret < 0)
  592. goto fail;
  593. d3d11_ctx->decoder = sctx->d3d11_decoder;
  594. d3d11_ctx->video_context = device_hwctx->video_context;
  595. d3d11_ctx->cfg = &sctx->d3d11_config;
  596. d3d11_ctx->surface_count = sctx->nb_d3d11_views;
  597. d3d11_ctx->surface = sctx->d3d11_views;
  598. d3d11_ctx->workaround = sctx->workaround;
  599. d3d11_ctx->context_mutex = INVALID_HANDLE_VALUE;
  600. }
  601. #endif
  602. #if CONFIG_DXVA2
  603. if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  604. AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
  605. struct dxva_context *dxva_ctx = &sctx->ctx.dxva2;
  606. ff_dxva2_lock(avctx);
  607. ret = dxva2_create_decoder(avctx);
  608. ff_dxva2_unlock(avctx);
  609. if (ret < 0)
  610. goto fail;
  611. dxva_ctx->decoder = sctx->dxva2_decoder;
  612. dxva_ctx->cfg = &sctx->dxva2_config;
  613. dxva_ctx->surface = frames_hwctx->surfaces;
  614. dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
  615. dxva_ctx->workaround = sctx->workaround;
  616. }
  617. #endif
  618. return 0;
  619. fail:
  620. ff_dxva2_decode_uninit(avctx);
  621. return ret;
  622. }
  623. int ff_dxva2_decode_uninit(AVCodecContext *avctx)
  624. {
  625. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  626. int i;
  627. av_buffer_unref(&sctx->decoder_ref);
  628. #if CONFIG_D3D11VA
  629. for (i = 0; i < sctx->nb_d3d11_views; i++) {
  630. if (sctx->d3d11_views[i])
  631. ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[i]);
  632. }
  633. av_freep(&sctx->d3d11_views);
  634. #endif
  635. #if CONFIG_DXVA2
  636. if (sctx->dxva2_service)
  637. IDirectXVideoDecoderService_Release(sctx->dxva2_service);
  638. #endif
  639. return 0;
  640. }
  641. static void *get_surface(const AVCodecContext *avctx, const AVFrame *frame)
  642. {
  643. #if CONFIG_D3D11VA
  644. if (frame->format == AV_PIX_FMT_D3D11) {
  645. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  646. intptr_t index = (intptr_t)frame->data[1];
  647. if (index < 0 || index >= sctx->nb_d3d11_views ||
  648. sctx->d3d11_texture != (ID3D11Texture2D *)frame->data[0]) {
  649. av_log((void *)avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
  650. return NULL;
  651. }
  652. return sctx->d3d11_views[index];
  653. }
  654. #endif
  655. return frame->data[3];
  656. }
  657. unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
  658. const AVDXVAContext *ctx,
  659. const AVFrame *frame)
  660. {
  661. void *surface = get_surface(avctx, frame);
  662. unsigned i;
  663. #if CONFIG_D3D11VA
  664. if (avctx->pix_fmt == AV_PIX_FMT_D3D11)
  665. return (intptr_t)frame->data[1];
  666. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  667. D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
  668. ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*) surface, &viewDesc);
  669. return viewDesc.Texture2D.ArraySlice;
  670. }
  671. #endif
  672. #if CONFIG_DXVA2
  673. for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++) {
  674. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
  675. return i;
  676. }
  677. #endif
  678. assert(0);
  679. return 0;
  680. }
  681. int ff_dxva2_commit_buffer(AVCodecContext *avctx,
  682. AVDXVAContext *ctx,
  683. DECODER_BUFFER_DESC *dsc,
  684. unsigned type, const void *data, unsigned size,
  685. unsigned mb_count)
  686. {
  687. void *dxva_data;
  688. unsigned dxva_size;
  689. int result;
  690. HRESULT hr = 0;
  691. #if CONFIG_D3D11VA
  692. if (ff_dxva2_is_d3d11(avctx))
  693. hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
  694. D3D11VA_CONTEXT(ctx)->decoder,
  695. type,
  696. &dxva_size, &dxva_data);
  697. #endif
  698. #if CONFIG_DXVA2
  699. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  700. hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
  701. &dxva_data, &dxva_size);
  702. #endif
  703. if (FAILED(hr)) {
  704. av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%x\n",
  705. type, (unsigned)hr);
  706. return -1;
  707. }
  708. if (size <= dxva_size) {
  709. memcpy(dxva_data, data, size);
  710. #if CONFIG_D3D11VA
  711. if (ff_dxva2_is_d3d11(avctx)) {
  712. D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
  713. memset(dsc11, 0, sizeof(*dsc11));
  714. dsc11->BufferType = type;
  715. dsc11->DataSize = size;
  716. dsc11->NumMBsInBuffer = mb_count;
  717. }
  718. #endif
  719. #if CONFIG_DXVA2
  720. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  721. DXVA2_DecodeBufferDesc *dsc2 = dsc;
  722. memset(dsc2, 0, sizeof(*dsc2));
  723. dsc2->CompressedBufferType = type;
  724. dsc2->DataSize = size;
  725. dsc2->NumMBsInBuffer = mb_count;
  726. }
  727. #endif
  728. result = 0;
  729. } else {
  730. av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
  731. result = -1;
  732. }
  733. #if CONFIG_D3D11VA
  734. if (ff_dxva2_is_d3d11(avctx))
  735. hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
  736. #endif
  737. #if CONFIG_DXVA2
  738. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  739. hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
  740. #endif
  741. if (FAILED(hr)) {
  742. av_log(avctx, AV_LOG_ERROR,
  743. "Failed to release buffer type %u: 0x%x\n",
  744. type, (unsigned)hr);
  745. result = -1;
  746. }
  747. return result;
  748. }
  749. static int frame_add_buf(AVFrame *frame, AVBufferRef *ref)
  750. {
  751. int i;
  752. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  753. if (!frame->buf[i]) {
  754. frame->buf[i] = av_buffer_ref(ref);
  755. return frame->buf[i] ? 0 : AVERROR(ENOMEM);
  756. }
  757. }
  758. // For now we expect that the caller does not use more than
  759. // AV_NUM_DATA_POINTERS-1 buffers if the user uses a custom pool.
  760. return AVERROR(EINVAL);
  761. }
  762. int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  763. const void *pp, unsigned pp_size,
  764. const void *qm, unsigned qm_size,
  765. int (*commit_bs_si)(AVCodecContext *,
  766. DECODER_BUFFER_DESC *bs,
  767. DECODER_BUFFER_DESC *slice))
  768. {
  769. AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
  770. unsigned buffer_count = 0;
  771. #if CONFIG_D3D11VA
  772. D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
  773. #endif
  774. #if CONFIG_DXVA2
  775. DXVA2_DecodeBufferDesc buffer2[4];
  776. #endif
  777. DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
  778. int result, runs = 0;
  779. HRESULT hr;
  780. unsigned type;
  781. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  782. if (sctx->decoder_ref) {
  783. result = frame_add_buf(frame, sctx->decoder_ref);
  784. if (result < 0)
  785. return result;
  786. }
  787. do {
  788. ff_dxva2_lock(avctx);
  789. #if CONFIG_D3D11VA
  790. if (ff_dxva2_is_d3d11(avctx))
  791. hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
  792. get_surface(avctx, frame),
  793. 0, NULL);
  794. #endif
  795. #if CONFIG_DXVA2
  796. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  797. hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
  798. get_surface(avctx, frame),
  799. NULL);
  800. #endif
  801. if (hr != E_PENDING || ++runs > 50)
  802. break;
  803. ff_dxva2_unlock(avctx);
  804. av_usleep(2000);
  805. } while(1);
  806. if (FAILED(hr)) {
  807. av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", (unsigned)hr);
  808. ff_dxva2_unlock(avctx);
  809. return -1;
  810. }
  811. #if CONFIG_D3D11VA
  812. if (ff_dxva2_is_d3d11(avctx)) {
  813. buffer = &buffer11[buffer_count];
  814. type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
  815. }
  816. #endif
  817. #if CONFIG_DXVA2
  818. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  819. buffer = &buffer2[buffer_count];
  820. type = DXVA2_PictureParametersBufferType;
  821. }
  822. #endif
  823. result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
  824. type,
  825. pp, pp_size, 0);
  826. if (result) {
  827. av_log(avctx, AV_LOG_ERROR,
  828. "Failed to add picture parameter buffer\n");
  829. goto end;
  830. }
  831. buffer_count++;
  832. if (qm_size > 0) {
  833. #if CONFIG_D3D11VA
  834. if (ff_dxva2_is_d3d11(avctx)) {
  835. buffer = &buffer11[buffer_count];
  836. type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
  837. }
  838. #endif
  839. #if CONFIG_DXVA2
  840. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  841. buffer = &buffer2[buffer_count];
  842. type = DXVA2_InverseQuantizationMatrixBufferType;
  843. }
  844. #endif
  845. result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
  846. type,
  847. qm, qm_size, 0);
  848. if (result) {
  849. av_log(avctx, AV_LOG_ERROR,
  850. "Failed to add inverse quantization matrix buffer\n");
  851. goto end;
  852. }
  853. buffer_count++;
  854. }
  855. #if CONFIG_D3D11VA
  856. if (ff_dxva2_is_d3d11(avctx)) {
  857. buffer = &buffer11[buffer_count + 0];
  858. buffer_slice = &buffer11[buffer_count + 1];
  859. }
  860. #endif
  861. #if CONFIG_DXVA2
  862. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  863. buffer = &buffer2[buffer_count + 0];
  864. buffer_slice = &buffer2[buffer_count + 1];
  865. }
  866. #endif
  867. result = commit_bs_si(avctx,
  868. buffer,
  869. buffer_slice);
  870. if (result) {
  871. av_log(avctx, AV_LOG_ERROR,
  872. "Failed to add bitstream or slice control buffer\n");
  873. goto end;
  874. }
  875. buffer_count += 2;
  876. /* TODO Film Grain when possible */
  877. assert(buffer_count == 1 + (qm_size > 0) + 2);
  878. #if CONFIG_D3D11VA
  879. if (ff_dxva2_is_d3d11(avctx))
  880. hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
  881. D3D11VA_CONTEXT(ctx)->decoder,
  882. buffer_count, buffer11);
  883. #endif
  884. #if CONFIG_DXVA2
  885. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  886. DXVA2_DecodeExecuteParams exec = {
  887. .NumCompBuffers = buffer_count,
  888. .pCompressedBuffers = buffer2,
  889. .pExtensionData = NULL,
  890. };
  891. hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
  892. }
  893. #endif
  894. if (FAILED(hr)) {
  895. av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", (unsigned)hr);
  896. result = -1;
  897. }
  898. end:
  899. #if CONFIG_D3D11VA
  900. if (ff_dxva2_is_d3d11(avctx))
  901. hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
  902. #endif
  903. #if CONFIG_DXVA2
  904. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  905. hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
  906. #endif
  907. ff_dxva2_unlock(avctx);
  908. if (FAILED(hr)) {
  909. av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", (unsigned)hr);
  910. result = -1;
  911. }
  912. return result;
  913. }
  914. int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
  915. {
  916. if (CONFIG_D3D11VA)
  917. return avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD ||
  918. avctx->pix_fmt == AV_PIX_FMT_D3D11;
  919. else
  920. return 0;
  921. }