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.

441 lines
15KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 "avconv.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_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  51. DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
  52. typedef struct dxva2_mode {
  53. const GUID *guid;
  54. enum AVCodecID codec;
  55. } dxva2_mode;
  56. static const dxva2_mode dxva2_modes[] = {
  57. /* MPEG-2 */
  58. { &DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO },
  59. { &DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO },
  60. /* H.264 */
  61. { &DXVA2_ModeH264_F, AV_CODEC_ID_H264 },
  62. { &DXVA2_ModeH264_E, AV_CODEC_ID_H264 },
  63. /* Intel specific H.264 mode */
  64. { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
  65. /* VC-1 / WMV3 */
  66. { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
  67. { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
  68. { &DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
  69. { &DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
  70. /* HEVC/H.265 */
  71. { &DXVA2_ModeHEVC_VLD_Main, AV_CODEC_ID_HEVC },
  72. { &DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC },
  73. { NULL, 0 },
  74. };
  75. typedef struct DXVA2Context {
  76. IDirectXVideoDecoder *decoder;
  77. GUID decoder_guid;
  78. DXVA2_ConfigPictureDecode decoder_config;
  79. IDirectXVideoDecoderService *decoder_service;
  80. AVFrame *tmp_frame;
  81. AVBufferRef *hw_device_ctx;
  82. AVBufferRef *hw_frames_ctx;
  83. } DXVA2Context;
  84. static void dxva2_uninit(AVCodecContext *s)
  85. {
  86. InputStream *ist = s->opaque;
  87. DXVA2Context *ctx = ist->hwaccel_ctx;
  88. ist->hwaccel_uninit = NULL;
  89. ist->hwaccel_get_buffer = NULL;
  90. ist->hwaccel_retrieve_data = NULL;
  91. if (ctx->decoder_service)
  92. IDirectXVideoDecoderService_Release(ctx->decoder_service);
  93. av_buffer_unref(&ctx->hw_frames_ctx);
  94. av_buffer_unref(&ctx->hw_device_ctx);
  95. av_frame_free(&ctx->tmp_frame);
  96. av_freep(&ist->hwaccel_ctx);
  97. av_freep(&s->hwaccel_context);
  98. }
  99. static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  100. {
  101. InputStream *ist = s->opaque;
  102. DXVA2Context *ctx = ist->hwaccel_ctx;
  103. return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
  104. }
  105. static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
  106. {
  107. InputStream *ist = s->opaque;
  108. DXVA2Context *ctx = ist->hwaccel_ctx;
  109. int ret;
  110. ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
  111. if (ret < 0)
  112. return ret;
  113. ret = av_frame_copy_props(ctx->tmp_frame, frame);
  114. if (ret < 0) {
  115. av_frame_unref(ctx->tmp_frame);
  116. return ret;
  117. }
  118. av_frame_unref(frame);
  119. av_frame_move_ref(frame, ctx->tmp_frame);
  120. return 0;
  121. }
  122. static int dxva2_alloc(AVCodecContext *s)
  123. {
  124. InputStream *ist = s->opaque;
  125. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  126. DXVA2Context *ctx;
  127. HANDLE device_handle;
  128. HRESULT hr;
  129. AVHWDeviceContext *device_ctx;
  130. AVDXVA2DeviceContext *device_hwctx;
  131. int ret;
  132. ctx = av_mallocz(sizeof(*ctx));
  133. if (!ctx)
  134. return AVERROR(ENOMEM);
  135. ist->hwaccel_ctx = ctx;
  136. ist->hwaccel_uninit = dxva2_uninit;
  137. ist->hwaccel_get_buffer = dxva2_get_buffer;
  138. ist->hwaccel_retrieve_data = dxva2_retrieve_data;
  139. ret = av_hwdevice_ctx_create(&ctx->hw_device_ctx, AV_HWDEVICE_TYPE_DXVA2,
  140. ist->hwaccel_device, NULL, 0);
  141. if (ret < 0)
  142. goto fail;
  143. device_ctx = (AVHWDeviceContext*)ctx->hw_device_ctx->data;
  144. device_hwctx = device_ctx->hwctx;
  145. hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
  146. &device_handle);
  147. if (FAILED(hr)) {
  148. av_log(NULL, loglevel, "Failed to open a device handle\n");
  149. goto fail;
  150. }
  151. hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
  152. &IID_IDirectXVideoDecoderService,
  153. (void **)&ctx->decoder_service);
  154. IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
  155. if (FAILED(hr)) {
  156. av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
  157. goto fail;
  158. }
  159. ctx->tmp_frame = av_frame_alloc();
  160. if (!ctx->tmp_frame)
  161. goto fail;
  162. s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
  163. if (!s->hwaccel_context)
  164. goto fail;
  165. return 0;
  166. fail:
  167. dxva2_uninit(s);
  168. return AVERROR(EINVAL);
  169. }
  170. static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
  171. const DXVA2_VideoDesc *desc,
  172. DXVA2_ConfigPictureDecode *config)
  173. {
  174. InputStream *ist = s->opaque;
  175. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  176. DXVA2Context *ctx = ist->hwaccel_ctx;
  177. unsigned cfg_count = 0, best_score = 0;
  178. DXVA2_ConfigPictureDecode *cfg_list = NULL;
  179. DXVA2_ConfigPictureDecode best_cfg = {{0}};
  180. HRESULT hr;
  181. int i;
  182. hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
  183. if (FAILED(hr)) {
  184. av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
  185. return AVERROR(EINVAL);
  186. }
  187. for (i = 0; i < cfg_count; i++) {
  188. DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
  189. unsigned score;
  190. if (cfg->ConfigBitstreamRaw == 1)
  191. score = 1;
  192. else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
  193. score = 2;
  194. else
  195. continue;
  196. if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
  197. score += 16;
  198. if (score > best_score) {
  199. best_score = score;
  200. best_cfg = *cfg;
  201. }
  202. }
  203. CoTaskMemFree(cfg_list);
  204. if (!best_score) {
  205. av_log(NULL, loglevel, "No valid decoder configuration available\n");
  206. return AVERROR(EINVAL);
  207. }
  208. *config = best_cfg;
  209. return 0;
  210. }
  211. static int dxva2_create_decoder(AVCodecContext *s)
  212. {
  213. InputStream *ist = s->opaque;
  214. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  215. DXVA2Context *ctx = ist->hwaccel_ctx;
  216. struct dxva_context *dxva_ctx = s->hwaccel_context;
  217. GUID *guid_list = NULL;
  218. unsigned guid_count = 0, i, j;
  219. GUID device_guid = GUID_NULL;
  220. const D3DFORMAT surface_format = s->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
  221. MKTAG('P', '0', '1', '0') : MKTAG('N', 'V', '1', '2');
  222. D3DFORMAT target_format = 0;
  223. DXVA2_VideoDesc desc = { 0 };
  224. DXVA2_ConfigPictureDecode config;
  225. HRESULT hr;
  226. int surface_alignment, num_surfaces;
  227. int ret;
  228. AVDXVA2FramesContext *frames_hwctx;
  229. AVHWFramesContext *frames_ctx;
  230. hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
  231. if (FAILED(hr)) {
  232. av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
  233. goto fail;
  234. }
  235. for (i = 0; dxva2_modes[i].guid; i++) {
  236. D3DFORMAT *target_list = NULL;
  237. unsigned target_count = 0;
  238. const dxva2_mode *mode = &dxva2_modes[i];
  239. if (mode->codec != s->codec_id)
  240. continue;
  241. for (j = 0; j < guid_count; j++) {
  242. if (IsEqualGUID(mode->guid, &guid_list[j]))
  243. break;
  244. }
  245. if (j == guid_count)
  246. continue;
  247. hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
  248. if (FAILED(hr)) {
  249. continue;
  250. }
  251. for (j = 0; j < target_count; j++) {
  252. const D3DFORMAT format = target_list[j];
  253. if (format == surface_format) {
  254. target_format = format;
  255. break;
  256. }
  257. }
  258. CoTaskMemFree(target_list);
  259. if (target_format) {
  260. device_guid = *mode->guid;
  261. break;
  262. }
  263. }
  264. CoTaskMemFree(guid_list);
  265. if (IsEqualGUID(&device_guid, &GUID_NULL)) {
  266. av_log(NULL, loglevel, "No decoder device for codec found\n");
  267. goto fail;
  268. }
  269. desc.SampleWidth = s->coded_width;
  270. desc.SampleHeight = s->coded_height;
  271. desc.Format = target_format;
  272. ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
  273. if (ret < 0) {
  274. goto fail;
  275. }
  276. /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
  277. but it causes issues for H.264 on certain AMD GPUs..... */
  278. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  279. surface_alignment = 32;
  280. /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
  281. all coding features have enough room to work with */
  282. else if (s->codec_id == AV_CODEC_ID_HEVC)
  283. surface_alignment = 128;
  284. else
  285. surface_alignment = 16;
  286. /* 4 base work surfaces */
  287. num_surfaces = 4;
  288. /* add surfaces based on number of possible refs */
  289. if (s->codec_id == AV_CODEC_ID_H264 || s->codec_id == AV_CODEC_ID_HEVC)
  290. num_surfaces += 16;
  291. else
  292. num_surfaces += 2;
  293. /* add extra surfaces for frame threading */
  294. if (s->active_thread_type & FF_THREAD_FRAME)
  295. num_surfaces += s->thread_count;
  296. ctx->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->hw_device_ctx);
  297. if (!ctx->hw_frames_ctx)
  298. goto fail;
  299. frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  300. frames_hwctx = frames_ctx->hwctx;
  301. frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
  302. frames_ctx->sw_format = s->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
  303. AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
  304. frames_ctx->width = FFALIGN(s->coded_width, surface_alignment);
  305. frames_ctx->height = FFALIGN(s->coded_height, surface_alignment);
  306. frames_ctx->initial_pool_size = num_surfaces;
  307. frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
  308. ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
  309. if (ret < 0) {
  310. av_log(NULL, loglevel, "Failed to initialize the HW frames context\n");
  311. goto fail;
  312. }
  313. hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
  314. &desc, &config, frames_hwctx->surfaces,
  315. frames_hwctx->nb_surfaces, &frames_hwctx->decoder_to_release);
  316. if (FAILED(hr)) {
  317. av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
  318. goto fail;
  319. }
  320. ctx->decoder_guid = device_guid;
  321. ctx->decoder_config = config;
  322. dxva_ctx->cfg = &ctx->decoder_config;
  323. dxva_ctx->decoder = frames_hwctx->decoder_to_release;
  324. dxva_ctx->surface = frames_hwctx->surfaces;
  325. dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
  326. if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
  327. dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
  328. return 0;
  329. fail:
  330. av_buffer_unref(&ctx->hw_frames_ctx);
  331. return AVERROR(EINVAL);
  332. }
  333. int dxva2_init(AVCodecContext *s)
  334. {
  335. InputStream *ist = s->opaque;
  336. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  337. DXVA2Context *ctx;
  338. int ret;
  339. if (!ist->hwaccel_ctx) {
  340. ret = dxva2_alloc(s);
  341. if (ret < 0)
  342. return ret;
  343. }
  344. ctx = ist->hwaccel_ctx;
  345. if (s->codec_id == AV_CODEC_ID_H264 &&
  346. (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
  347. av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
  348. return AVERROR(EINVAL);
  349. }
  350. if (s->codec_id == AV_CODEC_ID_HEVC &&
  351. s->profile != FF_PROFILE_HEVC_MAIN && s->profile != FF_PROFILE_HEVC_MAIN_10) {
  352. av_log(NULL, loglevel, "Unsupported HEVC profile for DXVA2 HWAccel: %d\n", s->profile);
  353. return AVERROR(EINVAL);
  354. }
  355. av_buffer_unref(&ctx->hw_frames_ctx);
  356. ret = dxva2_create_decoder(s);
  357. if (ret < 0) {
  358. av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
  359. return ret;
  360. }
  361. return 0;
  362. }