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
37KB

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