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.

1041 lines
35KB

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