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.

1003 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. unsigned cfg_count = 0;
  317. D3D11_VIDEO_DECODER_CONFIG *cfg_list = NULL;
  318. HRESULT hr;
  319. int i, ret;
  320. hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(video_device, desc, &cfg_count);
  321. if (FAILED(hr)) {
  322. av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
  323. return AVERROR(EINVAL);
  324. }
  325. cfg_list = av_malloc_array(cfg_count, sizeof(D3D11_VIDEO_DECODER_CONFIG));
  326. if (cfg_list == NULL)
  327. return AVERROR(ENOMEM);
  328. for (i = 0; i < cfg_count; i++) {
  329. hr = ID3D11VideoDevice_GetVideoDecoderConfig(video_device, desc, i, &cfg_list[i]);
  330. if (FAILED(hr)) {
  331. av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr);
  332. av_free(cfg_list);
  333. return AVERROR(EINVAL);
  334. }
  335. }
  336. ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
  337. if (ret >= 0)
  338. *config = cfg_list[ret];
  339. av_free(cfg_list);
  340. return ret;
  341. }
  342. static int d3d11va_create_decoder(AVCodecContext *avctx)
  343. {
  344. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  345. GUID *guid_list;
  346. unsigned guid_count, i;
  347. GUID decoder_guid;
  348. DXGI_FORMAT surface_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
  349. DXGI_FORMAT_P010 : DXGI_FORMAT_NV12;
  350. D3D11_VIDEO_DECODER_DESC desc = { 0 };
  351. D3D11_VIDEO_DECODER_CONFIG config;
  352. AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
  353. AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
  354. AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
  355. D3D11_TEXTURE2D_DESC texdesc;
  356. HRESULT hr;
  357. int ret;
  358. if (!frames_hwctx->texture) {
  359. av_log(avctx, AV_LOG_ERROR, "AVD3D11VAFramesContext.texture not set.\n");
  360. return AVERROR(EINVAL);
  361. }
  362. ID3D11Texture2D_GetDesc(frames_hwctx->texture, &texdesc);
  363. guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device);
  364. guid_list = av_malloc_array(guid_count, sizeof(*guid_list));
  365. if (guid_list == NULL || guid_count == 0) {
  366. av_log(avctx, AV_LOG_ERROR, "Failed to get the decoder GUIDs\n");
  367. av_free(guid_list);
  368. return AVERROR(EINVAL);
  369. }
  370. for (i = 0; i < guid_count; i++) {
  371. hr = ID3D11VideoDevice_GetVideoDecoderProfile(device_hwctx->video_device, i, &guid_list[i]);
  372. if (FAILED(hr)) {
  373. av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder GUID %d\n", i);
  374. av_free(guid_list);
  375. return AVERROR(EINVAL);
  376. }
  377. }
  378. ret = dxva_get_decoder_guid(avctx, device_hwctx->video_device, &surface_format,
  379. guid_count, guid_list, &decoder_guid);
  380. av_free(guid_list);
  381. if (ret < 0)
  382. return AVERROR(EINVAL);
  383. desc.SampleWidth = avctx->coded_width;
  384. desc.SampleHeight = avctx->coded_height;
  385. desc.OutputFormat = surface_format;
  386. desc.Guid = decoder_guid;
  387. ret = d3d11va_get_decoder_configuration(avctx, device_hwctx->video_device, &desc, &config);
  388. if (ret < 0)
  389. return AVERROR(EINVAL);
  390. sctx->d3d11_views = av_mallocz_array(texdesc.ArraySize, sizeof(sctx->d3d11_views[0]));
  391. if (!sctx->d3d11_views)
  392. return AVERROR(ENOMEM);
  393. sctx->nb_d3d11_views = texdesc.ArraySize;
  394. for (i = 0; i < sctx->nb_d3d11_views; i++) {
  395. D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = {
  396. .DecodeProfile = decoder_guid,
  397. .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D,
  398. .Texture2D = {
  399. .ArraySlice = i,
  400. }
  401. };
  402. hr = ID3D11VideoDevice_CreateVideoDecoderOutputView(device_hwctx->video_device,
  403. (ID3D11Resource*) frames_hwctx->texture,
  404. &viewDesc,
  405. (ID3D11VideoDecoderOutputView**) &sctx->d3d11_views[i]);
  406. if (FAILED(hr)) {
  407. av_log(avctx, AV_LOG_ERROR, "Could not create the decoder output view %d\n", i);
  408. return AVERROR_UNKNOWN;
  409. }
  410. }
  411. hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &desc,
  412. &config, &sctx->d3d11_decoder);
  413. if (FAILED(hr)) {
  414. av_log(avctx, AV_LOG_ERROR, "Failed to create D3D11VA video decoder\n");
  415. return AVERROR(EINVAL);
  416. }
  417. sctx->d3d11_config = config;
  418. sctx->d3d11_texture = frames_hwctx->texture;
  419. sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->d3d11_decoder);
  420. if (!sctx->decoder_ref)
  421. return AVERROR(ENOMEM);
  422. return 0;
  423. }
  424. #endif
  425. static void ff_dxva2_lock(AVCodecContext *avctx)
  426. {
  427. #if CONFIG_D3D11VA
  428. if (ff_dxva2_is_d3d11(avctx)) {
  429. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  430. AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
  431. if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
  432. WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
  433. if (sctx->device_ctx) {
  434. AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
  435. hwctx->lock(hwctx->lock_ctx);
  436. }
  437. }
  438. #endif
  439. }
  440. static void ff_dxva2_unlock(AVCodecContext *avctx)
  441. {
  442. #if CONFIG_D3D11VA
  443. if (ff_dxva2_is_d3d11(avctx)) {
  444. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  445. AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
  446. if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
  447. ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
  448. if (sctx->device_ctx) {
  449. AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
  450. hwctx->unlock(hwctx->lock_ctx);
  451. }
  452. }
  453. #endif
  454. }
  455. // This must work before the decoder is created.
  456. // This somehow needs to be exported to the user.
  457. static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frames_ctx)
  458. {
  459. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  460. int surface_alignment, num_surfaces;
  461. frames_ctx->format = sctx->pix_fmt;
  462. /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
  463. but it causes issues for H.264 on certain AMD GPUs..... */
  464. if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  465. surface_alignment = 32;
  466. /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
  467. all coding features have enough room to work with */
  468. else if (avctx->codec_id == AV_CODEC_ID_HEVC)
  469. surface_alignment = 128;
  470. else
  471. surface_alignment = 16;
  472. /* 4 base work surfaces */
  473. num_surfaces = 4;
  474. /* add surfaces based on number of possible refs */
  475. if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC)
  476. num_surfaces += 16;
  477. else if (avctx->codec_id == AV_CODEC_ID_VP9)
  478. num_surfaces += 8;
  479. else
  480. num_surfaces += 2;
  481. /* add extra surfaces for frame threading */
  482. if (avctx->active_thread_type & FF_THREAD_FRAME)
  483. num_surfaces += avctx->thread_count;
  484. frames_ctx->sw_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
  485. AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
  486. frames_ctx->width = FFALIGN(avctx->coded_width, surface_alignment);
  487. frames_ctx->height = FFALIGN(avctx->coded_height, surface_alignment);
  488. frames_ctx->initial_pool_size = num_surfaces;
  489. #if CONFIG_DXVA2
  490. if (frames_ctx->format == AV_PIX_FMT_DXVA2_VLD) {
  491. AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
  492. frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
  493. }
  494. #endif
  495. #if CONFIG_D3D11VA
  496. if (frames_ctx->format == AV_PIX_FMT_D3D11) {
  497. AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
  498. frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
  499. }
  500. #endif
  501. }
  502. int ff_dxva2_decode_init(AVCodecContext *avctx)
  503. {
  504. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  505. AVHWFramesContext *frames_ctx = NULL;
  506. int ret = 0;
  507. // Old API.
  508. if (avctx->hwaccel_context)
  509. return 0;
  510. // (avctx->pix_fmt is not updated yet at this point)
  511. sctx->pix_fmt = avctx->hwaccel->pix_fmt;
  512. if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
  513. av_log(avctx, AV_LOG_ERROR, "Either a hw_frames_ctx or a hw_device_ctx needs to be set for hardware decoding.\n");
  514. return AVERROR(EINVAL);
  515. }
  516. if (avctx->hw_frames_ctx) {
  517. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  518. } else {
  519. avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
  520. if (!avctx->hw_frames_ctx)
  521. return AVERROR(ENOMEM);
  522. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  523. dxva_adjust_hwframes(avctx, frames_ctx);
  524. ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  525. if (ret < 0)
  526. goto fail;
  527. }
  528. sctx->device_ctx = frames_ctx->device_ctx;
  529. if (frames_ctx->format != sctx->pix_fmt ||
  530. !((sctx->pix_fmt == AV_PIX_FMT_D3D11 && CONFIG_D3D11VA) ||
  531. (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && CONFIG_DXVA2))) {
  532. av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
  533. ret = AVERROR(EINVAL);
  534. goto fail;
  535. }
  536. #if CONFIG_D3D11VA
  537. if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
  538. AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
  539. AVD3D11VAContext *d3d11_ctx = &sctx->ctx.d3d11va;
  540. ff_dxva2_lock(avctx);
  541. ret = d3d11va_create_decoder(avctx);
  542. ff_dxva2_unlock(avctx);
  543. if (ret < 0)
  544. goto fail;
  545. d3d11_ctx->decoder = sctx->d3d11_decoder;
  546. d3d11_ctx->video_context = device_hwctx->video_context;
  547. d3d11_ctx->cfg = &sctx->d3d11_config;
  548. d3d11_ctx->surface_count = sctx->nb_d3d11_views;
  549. d3d11_ctx->surface = sctx->d3d11_views;
  550. d3d11_ctx->workaround = sctx->workaround;
  551. d3d11_ctx->context_mutex = INVALID_HANDLE_VALUE;
  552. }
  553. #endif
  554. #if CONFIG_DXVA2
  555. if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  556. AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
  557. struct dxva_context *dxva_ctx = &sctx->ctx.dxva2;
  558. ff_dxva2_lock(avctx);
  559. ret = dxva2_create_decoder(avctx);
  560. ff_dxva2_unlock(avctx);
  561. if (ret < 0)
  562. goto fail;
  563. dxva_ctx->decoder = sctx->dxva2_decoder;
  564. dxva_ctx->cfg = &sctx->dxva2_config;
  565. dxva_ctx->surface = frames_hwctx->surfaces;
  566. dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
  567. dxva_ctx->workaround = sctx->workaround;
  568. }
  569. #endif
  570. return 0;
  571. fail:
  572. ff_dxva2_decode_uninit(avctx);
  573. return ret;
  574. }
  575. int ff_dxva2_decode_uninit(AVCodecContext *avctx)
  576. {
  577. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  578. int i;
  579. av_buffer_unref(&sctx->decoder_ref);
  580. #if CONFIG_D3D11VA
  581. for (i = 0; i < sctx->nb_d3d11_views; i++) {
  582. if (sctx->d3d11_views[i])
  583. ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[i]);
  584. }
  585. av_freep(&sctx->d3d11_views);
  586. #endif
  587. #if CONFIG_DXVA2
  588. if (sctx->dxva2_service)
  589. IDirectXVideoDecoderService_Release(sctx->dxva2_service);
  590. #endif
  591. return 0;
  592. }
  593. static void *get_surface(const AVCodecContext *avctx, const AVFrame *frame)
  594. {
  595. #if CONFIG_D3D11VA
  596. if (frame->format == AV_PIX_FMT_D3D11) {
  597. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  598. intptr_t index = (intptr_t)frame->data[1];
  599. if (index < 0 || index >= sctx->nb_d3d11_views ||
  600. sctx->d3d11_texture != (ID3D11Texture2D *)frame->data[0]) {
  601. av_log((void *)avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
  602. return NULL;
  603. }
  604. return sctx->d3d11_views[index];
  605. }
  606. #endif
  607. return frame->data[3];
  608. }
  609. unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
  610. const AVDXVAContext *ctx,
  611. const AVFrame *frame)
  612. {
  613. void *surface = get_surface(avctx, frame);
  614. unsigned i;
  615. #if CONFIG_D3D11VA
  616. if (avctx->pix_fmt == AV_PIX_FMT_D3D11)
  617. return (intptr_t)frame->data[1];
  618. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  619. D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
  620. ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*) surface, &viewDesc);
  621. return viewDesc.Texture2D.ArraySlice;
  622. }
  623. #endif
  624. #if CONFIG_DXVA2
  625. for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++) {
  626. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
  627. return i;
  628. }
  629. #endif
  630. assert(0);
  631. return 0;
  632. }
  633. int ff_dxva2_commit_buffer(AVCodecContext *avctx,
  634. AVDXVAContext *ctx,
  635. DECODER_BUFFER_DESC *dsc,
  636. unsigned type, const void *data, unsigned size,
  637. unsigned mb_count)
  638. {
  639. void *dxva_data;
  640. unsigned dxva_size;
  641. int result;
  642. HRESULT hr = 0;
  643. #if CONFIG_D3D11VA
  644. if (ff_dxva2_is_d3d11(avctx))
  645. hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
  646. D3D11VA_CONTEXT(ctx)->decoder,
  647. type,
  648. &dxva_size, &dxva_data);
  649. #endif
  650. #if CONFIG_DXVA2
  651. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  652. hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
  653. &dxva_data, &dxva_size);
  654. #endif
  655. if (FAILED(hr)) {
  656. av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%x\n",
  657. type, (unsigned)hr);
  658. return -1;
  659. }
  660. if (size <= dxva_size) {
  661. memcpy(dxva_data, data, size);
  662. #if CONFIG_D3D11VA
  663. if (ff_dxva2_is_d3d11(avctx)) {
  664. D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
  665. memset(dsc11, 0, sizeof(*dsc11));
  666. dsc11->BufferType = type;
  667. dsc11->DataSize = size;
  668. dsc11->NumMBsInBuffer = mb_count;
  669. }
  670. #endif
  671. #if CONFIG_DXVA2
  672. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  673. DXVA2_DecodeBufferDesc *dsc2 = dsc;
  674. memset(dsc2, 0, sizeof(*dsc2));
  675. dsc2->CompressedBufferType = type;
  676. dsc2->DataSize = size;
  677. dsc2->NumMBsInBuffer = mb_count;
  678. }
  679. #endif
  680. result = 0;
  681. } else {
  682. av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
  683. result = -1;
  684. }
  685. #if CONFIG_D3D11VA
  686. if (ff_dxva2_is_d3d11(avctx))
  687. hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
  688. #endif
  689. #if CONFIG_DXVA2
  690. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  691. hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
  692. #endif
  693. if (FAILED(hr)) {
  694. av_log(avctx, AV_LOG_ERROR,
  695. "Failed to release buffer type %u: 0x%x\n",
  696. type, (unsigned)hr);
  697. result = -1;
  698. }
  699. return result;
  700. }
  701. static int frame_add_buf(AVFrame *frame, AVBufferRef *ref)
  702. {
  703. int i;
  704. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  705. if (!frame->buf[i]) {
  706. frame->buf[i] = av_buffer_ref(ref);
  707. return frame->buf[i] ? 0 : AVERROR(ENOMEM);
  708. }
  709. }
  710. // For now we expect that the caller does not use more than
  711. // AV_NUM_DATA_POINTERS-1 buffers if the user uses a custom pool.
  712. return AVERROR(EINVAL);
  713. }
  714. int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  715. const void *pp, unsigned pp_size,
  716. const void *qm, unsigned qm_size,
  717. int (*commit_bs_si)(AVCodecContext *,
  718. DECODER_BUFFER_DESC *bs,
  719. DECODER_BUFFER_DESC *slice))
  720. {
  721. AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
  722. unsigned buffer_count = 0;
  723. #if CONFIG_D3D11VA
  724. D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
  725. #endif
  726. #if CONFIG_DXVA2
  727. DXVA2_DecodeBufferDesc buffer2[4];
  728. #endif
  729. DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
  730. int result, runs = 0;
  731. HRESULT hr;
  732. unsigned type;
  733. FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
  734. if (sctx->decoder_ref) {
  735. result = frame_add_buf(frame, sctx->decoder_ref);
  736. if (result < 0)
  737. return result;
  738. }
  739. do {
  740. ff_dxva2_lock(avctx);
  741. #if CONFIG_D3D11VA
  742. if (ff_dxva2_is_d3d11(avctx))
  743. hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
  744. get_surface(avctx, frame),
  745. 0, NULL);
  746. #endif
  747. #if CONFIG_DXVA2
  748. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  749. hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
  750. get_surface(avctx, frame),
  751. NULL);
  752. #endif
  753. if (hr != E_PENDING || ++runs > 50)
  754. break;
  755. ff_dxva2_unlock(avctx);
  756. av_usleep(2000);
  757. } while(1);
  758. if (FAILED(hr)) {
  759. av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", (unsigned)hr);
  760. ff_dxva2_unlock(avctx);
  761. return -1;
  762. }
  763. #if CONFIG_D3D11VA
  764. if (ff_dxva2_is_d3d11(avctx)) {
  765. buffer = &buffer11[buffer_count];
  766. type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
  767. }
  768. #endif
  769. #if CONFIG_DXVA2
  770. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  771. buffer = &buffer2[buffer_count];
  772. type = DXVA2_PictureParametersBufferType;
  773. }
  774. #endif
  775. result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
  776. type,
  777. pp, pp_size, 0);
  778. if (result) {
  779. av_log(avctx, AV_LOG_ERROR,
  780. "Failed to add picture parameter buffer\n");
  781. goto end;
  782. }
  783. buffer_count++;
  784. if (qm_size > 0) {
  785. #if CONFIG_D3D11VA
  786. if (ff_dxva2_is_d3d11(avctx)) {
  787. buffer = &buffer11[buffer_count];
  788. type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
  789. }
  790. #endif
  791. #if CONFIG_DXVA2
  792. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  793. buffer = &buffer2[buffer_count];
  794. type = DXVA2_InverseQuantizationMatrixBufferType;
  795. }
  796. #endif
  797. result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
  798. type,
  799. qm, qm_size, 0);
  800. if (result) {
  801. av_log(avctx, AV_LOG_ERROR,
  802. "Failed to add inverse quantization matrix buffer\n");
  803. goto end;
  804. }
  805. buffer_count++;
  806. }
  807. #if CONFIG_D3D11VA
  808. if (ff_dxva2_is_d3d11(avctx)) {
  809. buffer = &buffer11[buffer_count + 0];
  810. buffer_slice = &buffer11[buffer_count + 1];
  811. }
  812. #endif
  813. #if CONFIG_DXVA2
  814. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  815. buffer = &buffer2[buffer_count + 0];
  816. buffer_slice = &buffer2[buffer_count + 1];
  817. }
  818. #endif
  819. result = commit_bs_si(avctx,
  820. buffer,
  821. buffer_slice);
  822. if (result) {
  823. av_log(avctx, AV_LOG_ERROR,
  824. "Failed to add bitstream or slice control buffer\n");
  825. goto end;
  826. }
  827. buffer_count += 2;
  828. /* TODO Film Grain when possible */
  829. assert(buffer_count == 1 + (qm_size > 0) + 2);
  830. #if CONFIG_D3D11VA
  831. if (ff_dxva2_is_d3d11(avctx))
  832. hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
  833. D3D11VA_CONTEXT(ctx)->decoder,
  834. buffer_count, buffer11);
  835. #endif
  836. #if CONFIG_DXVA2
  837. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  838. DXVA2_DecodeExecuteParams exec = {
  839. .NumCompBuffers = buffer_count,
  840. .pCompressedBuffers = buffer2,
  841. .pExtensionData = NULL,
  842. };
  843. hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
  844. }
  845. #endif
  846. if (FAILED(hr)) {
  847. av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", (unsigned)hr);
  848. result = -1;
  849. }
  850. end:
  851. #if CONFIG_D3D11VA
  852. if (ff_dxva2_is_d3d11(avctx))
  853. hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
  854. #endif
  855. #if CONFIG_DXVA2
  856. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  857. hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
  858. #endif
  859. ff_dxva2_unlock(avctx);
  860. if (FAILED(hr)) {
  861. av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", (unsigned)hr);
  862. result = -1;
  863. }
  864. return result;
  865. }
  866. int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
  867. {
  868. if (CONFIG_D3D11VA)
  869. return avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD ||
  870. avctx->pix_fmt == AV_PIX_FMT_D3D11;
  871. else
  872. return 0;
  873. }