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.

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