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.

1005 lines
34KB

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