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.

1052 lines
36KB

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