ffmpeg_dxva2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <windows.h>
  19. #ifdef _WIN32_WINNT
  20. #undef _WIN32_WINNT
  21. #endif
  22. #define _WIN32_WINNT 0x0600
  23. #define DXVA2API_USE_BITFIELDS
  24. #define COBJMACROS
  25. #include <stdint.h>
  26. #include <d3d9.h>
  27. #include <dxva2api.h>
  28. #include "ffmpeg.h"
  29. #include "libavcodec/dxva2.h"
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/buffer.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/pixfmt.h"
  35. #include "libavutil/hwcontext.h"
  36. #include "libavutil/hwcontext_dxva2.h"
  37. /* define all the GUIDs used directly here,
  38. to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
  39. #include <initguid.h>
  40. DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
  41. DEFINE_GUID(DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
  42. DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
  43. DEFINE_GUID(DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  44. DEFINE_GUID(DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  45. DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
  46. DEFINE_GUID(DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  47. DEFINE_GUID(DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  48. DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main, 0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
  49. DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
  50. DEFINE_GUID(DXVA2_ModeVP9_VLD_Profile0, 0x463707f8, 0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
  51. DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  52. DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
  53. typedef struct dxva2_mode {
  54. const GUID *guid;
  55. enum AVCodecID codec;
  56. } dxva2_mode;
  57. static const dxva2_mode dxva2_modes[] = {
  58. /* MPEG-2 */
  59. { &DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO },
  60. { &DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO },
  61. /* H.264 */
  62. { &DXVA2_ModeH264_F, AV_CODEC_ID_H264 },
  63. { &DXVA2_ModeH264_E, AV_CODEC_ID_H264 },
  64. /* Intel specific H.264 mode */
  65. { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
  66. /* VC-1 / WMV3 */
  67. { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
  68. { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
  69. { &DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
  70. { &DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
  71. /* HEVC/H.265 */
  72. { &DXVA2_ModeHEVC_VLD_Main, AV_CODEC_ID_HEVC },
  73. { &DXVA2_ModeHEVC_VLD_Main10,AV_CODEC_ID_HEVC },
  74. /* VP8/9 */
  75. { &DXVA2_ModeVP9_VLD_Profile0, AV_CODEC_ID_VP9 },
  76. { NULL, 0 },
  77. };
  78. typedef struct DXVA2Context {
  79. IDirectXVideoDecoder *decoder;
  80. GUID decoder_guid;
  81. DXVA2_ConfigPictureDecode decoder_config;
  82. IDirectXVideoDecoderService *decoder_service;
  83. AVFrame *tmp_frame;
  84. AVBufferRef *hw_device_ctx;
  85. AVBufferRef *hw_frames_ctx;
  86. } DXVA2Context;
  87. static void dxva2_uninit(AVCodecContext *s)
  88. {
  89. InputStream *ist = s->opaque;
  90. DXVA2Context *ctx = ist->hwaccel_ctx;
  91. ist->hwaccel_uninit = NULL;
  92. ist->hwaccel_get_buffer = NULL;
  93. ist->hwaccel_retrieve_data = NULL;
  94. if (ctx->decoder_service)
  95. IDirectXVideoDecoderService_Release(ctx->decoder_service);
  96. av_buffer_unref(&ctx->hw_frames_ctx);
  97. av_buffer_unref(&ctx->hw_device_ctx);
  98. av_frame_free(&ctx->tmp_frame);
  99. av_freep(&ist->hwaccel_ctx);
  100. av_freep(&s->hwaccel_context);
  101. }
  102. static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  103. {
  104. InputStream *ist = s->opaque;
  105. DXVA2Context *ctx = ist->hwaccel_ctx;
  106. return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
  107. }
  108. static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
  109. {
  110. InputStream *ist = s->opaque;
  111. DXVA2Context *ctx = ist->hwaccel_ctx;
  112. int ret;
  113. ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
  114. if (ret < 0)
  115. return ret;
  116. ret = av_frame_copy_props(ctx->tmp_frame, frame);
  117. if (ret < 0) {
  118. av_frame_unref(ctx->tmp_frame);
  119. return ret;
  120. }
  121. av_frame_unref(frame);
  122. av_frame_move_ref(frame, ctx->tmp_frame);
  123. return 0;
  124. }
  125. static int dxva2_alloc(AVCodecContext *s)
  126. {
  127. InputStream *ist = s->opaque;
  128. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  129. DXVA2Context *ctx;
  130. HANDLE device_handle;
  131. HRESULT hr;
  132. AVHWDeviceContext *device_ctx;
  133. AVDXVA2DeviceContext *device_hwctx;
  134. int ret;
  135. ctx = av_mallocz(sizeof(*ctx));
  136. if (!ctx)
  137. return AVERROR(ENOMEM);
  138. ist->hwaccel_ctx = ctx;
  139. ist->hwaccel_uninit = dxva2_uninit;
  140. ist->hwaccel_get_buffer = dxva2_get_buffer;
  141. ist->hwaccel_retrieve_data = dxva2_retrieve_data;
  142. ret = av_hwdevice_ctx_create(&ctx->hw_device_ctx, AV_HWDEVICE_TYPE_DXVA2,
  143. ist->hwaccel_device, NULL, 0);
  144. if (ret < 0)
  145. goto fail;
  146. device_ctx = (AVHWDeviceContext*)ctx->hw_device_ctx->data;
  147. device_hwctx = device_ctx->hwctx;
  148. hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
  149. &device_handle);
  150. if (FAILED(hr)) {
  151. av_log(NULL, loglevel, "Failed to open a device handle\n");
  152. goto fail;
  153. }
  154. hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
  155. &IID_IDirectXVideoDecoderService,
  156. (void **)&ctx->decoder_service);
  157. IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
  158. if (FAILED(hr)) {
  159. av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
  160. goto fail;
  161. }
  162. ctx->tmp_frame = av_frame_alloc();
  163. if (!ctx->tmp_frame)
  164. goto fail;
  165. s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
  166. if (!s->hwaccel_context)
  167. goto fail;
  168. return 0;
  169. fail:
  170. dxva2_uninit(s);
  171. return AVERROR(EINVAL);
  172. }
  173. static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
  174. const DXVA2_VideoDesc *desc,
  175. DXVA2_ConfigPictureDecode *config)
  176. {
  177. InputStream *ist = s->opaque;
  178. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  179. DXVA2Context *ctx = ist->hwaccel_ctx;
  180. unsigned cfg_count = 0, best_score = 0;
  181. DXVA2_ConfigPictureDecode *cfg_list = NULL;
  182. DXVA2_ConfigPictureDecode best_cfg = {{0}};
  183. HRESULT hr;
  184. int i;
  185. hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
  186. if (FAILED(hr)) {
  187. av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
  188. return AVERROR(EINVAL);
  189. }
  190. for (i = 0; i < cfg_count; i++) {
  191. DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
  192. unsigned score;
  193. if (cfg->ConfigBitstreamRaw == 1)
  194. score = 1;
  195. else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
  196. score = 2;
  197. else
  198. continue;
  199. if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
  200. score += 16;
  201. if (score > best_score) {
  202. best_score = score;
  203. best_cfg = *cfg;
  204. }
  205. }
  206. CoTaskMemFree(cfg_list);
  207. if (!best_score) {
  208. av_log(NULL, loglevel, "No valid decoder configuration available\n");
  209. return AVERROR(EINVAL);
  210. }
  211. *config = best_cfg;
  212. return 0;
  213. }
  214. static int dxva2_create_decoder(AVCodecContext *s)
  215. {
  216. InputStream *ist = s->opaque;
  217. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  218. DXVA2Context *ctx = ist->hwaccel_ctx;
  219. struct dxva_context *dxva_ctx = s->hwaccel_context;
  220. GUID *guid_list = NULL;
  221. unsigned guid_count = 0, i, j;
  222. GUID device_guid = GUID_NULL;
  223. const D3DFORMAT surface_format = (s->sw_pix_fmt == AV_PIX_FMT_YUV420P10) ? MKTAG('P','0','1','0') : MKTAG('N','V','1','2');
  224. D3DFORMAT target_format = 0;
  225. DXVA2_VideoDesc desc = { 0 };
  226. DXVA2_ConfigPictureDecode config;
  227. HRESULT hr;
  228. int surface_alignment, num_surfaces;
  229. int ret;
  230. AVDXVA2FramesContext *frames_hwctx;
  231. AVHWFramesContext *frames_ctx;
  232. hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
  233. if (FAILED(hr)) {
  234. av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
  235. goto fail;
  236. }
  237. for (i = 0; dxva2_modes[i].guid; i++) {
  238. D3DFORMAT *target_list = NULL;
  239. unsigned target_count = 0;
  240. const dxva2_mode *mode = &dxva2_modes[i];
  241. if (mode->codec != s->codec_id)
  242. continue;
  243. for (j = 0; j < guid_count; j++) {
  244. if (IsEqualGUID(mode->guid, &guid_list[j]))
  245. break;
  246. }
  247. if (j == guid_count)
  248. continue;
  249. hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
  250. if (FAILED(hr)) {
  251. continue;
  252. }
  253. for (j = 0; j < target_count; j++) {
  254. const D3DFORMAT format = target_list[j];
  255. if (format == surface_format) {
  256. target_format = format;
  257. break;
  258. }
  259. }
  260. CoTaskMemFree(target_list);
  261. if (target_format) {
  262. device_guid = *mode->guid;
  263. break;
  264. }
  265. }
  266. CoTaskMemFree(guid_list);
  267. if (IsEqualGUID(&device_guid, &GUID_NULL)) {
  268. av_log(NULL, loglevel, "No decoder device for codec found\n");
  269. goto fail;
  270. }
  271. desc.SampleWidth = s->coded_width;
  272. desc.SampleHeight = s->coded_height;
  273. desc.Format = target_format;
  274. ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
  275. if (ret < 0) {
  276. goto fail;
  277. }
  278. /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
  279. but it causes issues for H.264 on certain AMD GPUs..... */
  280. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  281. surface_alignment = 32;
  282. /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
  283. all coding features have enough room to work with */
  284. else if (s->codec_id == AV_CODEC_ID_HEVC)
  285. surface_alignment = 128;
  286. else
  287. surface_alignment = 16;
  288. /* 4 base work surfaces */
  289. num_surfaces = 4;
  290. /* add surfaces based on number of possible refs */
  291. if (s->codec_id == AV_CODEC_ID_H264 || s->codec_id == AV_CODEC_ID_HEVC)
  292. num_surfaces += 16;
  293. else if (s->codec_id == AV_CODEC_ID_VP9)
  294. num_surfaces += 8;
  295. else
  296. num_surfaces += 2;
  297. /* add extra surfaces for frame threading */
  298. if (s->active_thread_type & FF_THREAD_FRAME)
  299. num_surfaces += s->thread_count;
  300. ctx->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->hw_device_ctx);
  301. if (!ctx->hw_frames_ctx)
  302. goto fail;
  303. frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  304. frames_hwctx = frames_ctx->hwctx;
  305. frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
  306. frames_ctx->sw_format = (target_format == MKTAG('P','0','1','0') ? AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
  307. frames_ctx->width = FFALIGN(s->coded_width, surface_alignment);
  308. frames_ctx->height = FFALIGN(s->coded_height, surface_alignment);
  309. frames_ctx->initial_pool_size = num_surfaces;
  310. frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
  311. ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
  312. if (ret < 0) {
  313. av_log(NULL, loglevel, "Failed to initialize the HW frames context\n");
  314. goto fail;
  315. }
  316. hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
  317. &desc, &config, frames_hwctx->surfaces,
  318. frames_hwctx->nb_surfaces, &frames_hwctx->decoder_to_release);
  319. if (FAILED(hr)) {
  320. av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
  321. goto fail;
  322. }
  323. ctx->decoder_guid = device_guid;
  324. ctx->decoder_config = config;
  325. dxva_ctx->cfg = &ctx->decoder_config;
  326. dxva_ctx->decoder = frames_hwctx->decoder_to_release;
  327. dxva_ctx->surface = frames_hwctx->surfaces;
  328. dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
  329. if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
  330. dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
  331. return 0;
  332. fail:
  333. av_buffer_unref(&ctx->hw_frames_ctx);
  334. return AVERROR(EINVAL);
  335. }
  336. int dxva2_init(AVCodecContext *s)
  337. {
  338. InputStream *ist = s->opaque;
  339. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  340. DXVA2Context *ctx;
  341. int ret;
  342. if (!ist->hwaccel_ctx) {
  343. ret = dxva2_alloc(s);
  344. if (ret < 0)
  345. return ret;
  346. }
  347. ctx = ist->hwaccel_ctx;
  348. if (s->codec_id == AV_CODEC_ID_H264 &&
  349. (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
  350. av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
  351. return AVERROR(EINVAL);
  352. }
  353. if (s->codec_id == AV_CODEC_ID_HEVC &&
  354. s->profile != FF_PROFILE_HEVC_MAIN && s->profile != FF_PROFILE_HEVC_MAIN_10) {
  355. av_log(NULL, loglevel, "Unsupported HEVC profile for DXVA2 HWAccel: %d\n", s->profile);
  356. return AVERROR(EINVAL);
  357. }
  358. av_buffer_unref(&ctx->hw_frames_ctx);
  359. ret = dxva2_create_decoder(s);
  360. if (ret < 0) {
  361. av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
  362. return ret;
  363. }
  364. return 0;
  365. }