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.

526 lines
16KB

  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. #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
  20. #undef _WIN32_WINNT
  21. #define _WIN32_WINNT 0x0600
  22. #endif
  23. #define DXVA2API_USE_BITFIELDS
  24. #define COBJMACROS
  25. #include <d3d9.h>
  26. #include <dxva2api.h>
  27. #include <initguid.h>
  28. #include "common.h"
  29. #include "hwcontext.h"
  30. #include "hwcontext_dxva2.h"
  31. #include "hwcontext_internal.h"
  32. #include "imgutils.h"
  33. #include "pixdesc.h"
  34. #include "pixfmt.h"
  35. typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
  36. typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
  37. typedef struct DXVA2Mapping {
  38. uint32_t palette_dummy[256];
  39. } DXVA2Mapping;
  40. typedef struct DXVA2FramesContext {
  41. IDirect3DSurface9 **surfaces_internal;
  42. int nb_surfaces_used;
  43. HANDLE device_handle;
  44. IDirectXVideoAccelerationService *service;
  45. D3DFORMAT format;
  46. } DXVA2FramesContext;
  47. typedef struct DXVA2DevicePriv {
  48. HMODULE d3dlib;
  49. HMODULE dxva2lib;
  50. HANDLE device_handle;
  51. IDirect3D9 *d3d9;
  52. IDirect3DDevice9 *d3d9device;
  53. } DXVA2DevicePriv;
  54. static const struct {
  55. D3DFORMAT d3d_format;
  56. enum AVPixelFormat pix_fmt;
  57. } supported_formats[] = {
  58. { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
  59. { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
  60. { D3DFMT_P8, AV_PIX_FMT_PAL8 },
  61. };
  62. DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
  63. DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
  64. static void dxva2_frames_uninit(AVHWFramesContext *ctx)
  65. {
  66. AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  67. AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
  68. DXVA2FramesContext *s = ctx->internal->priv;
  69. int i;
  70. if (frames_hwctx->decoder_to_release)
  71. IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
  72. if (s->surfaces_internal) {
  73. for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
  74. if (s->surfaces_internal[i])
  75. IDirect3DSurface9_Release(s->surfaces_internal[i]);
  76. }
  77. }
  78. av_freep(&s->surfaces_internal);
  79. if (s->service) {
  80. IDirectXVideoAccelerationService_Release(s->service);
  81. s->service = NULL;
  82. }
  83. if (s->device_handle != INVALID_HANDLE_VALUE) {
  84. IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
  85. s->device_handle = INVALID_HANDLE_VALUE;
  86. }
  87. }
  88. static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
  89. {
  90. AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
  91. DXVA2FramesContext *s = ctx->internal->priv;
  92. AVDXVA2FramesContext *hwctx = ctx->hwctx;
  93. if (s->nb_surfaces_used < hwctx->nb_surfaces) {
  94. s->nb_surfaces_used++;
  95. return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
  96. sizeof(*hwctx->surfaces), NULL, 0, 0);
  97. }
  98. return NULL;
  99. }
  100. static int dxva2_init_pool(AVHWFramesContext *ctx)
  101. {
  102. AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
  103. AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  104. DXVA2FramesContext *s = ctx->internal->priv;
  105. int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
  106. int i;
  107. HRESULT hr;
  108. if (ctx->initial_pool_size <= 0)
  109. return 0;
  110. hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
  111. if (FAILED(hr)) {
  112. av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
  113. return AVERROR_UNKNOWN;
  114. }
  115. hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
  116. s->device_handle,
  117. decode ? &video_decoder_service : &video_processor_service,
  118. (void **)&s->service);
  119. if (FAILED(hr)) {
  120. av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
  121. return AVERROR_UNKNOWN;
  122. }
  123. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
  124. if (ctx->sw_format == supported_formats[i].pix_fmt) {
  125. s->format = supported_formats[i].d3d_format;
  126. break;
  127. }
  128. }
  129. if (i == FF_ARRAY_ELEMS(supported_formats)) {
  130. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
  131. av_get_pix_fmt_name(ctx->sw_format));
  132. return AVERROR(EINVAL);
  133. }
  134. s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
  135. sizeof(*s->surfaces_internal));
  136. if (!s->surfaces_internal)
  137. return AVERROR(ENOMEM);
  138. hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
  139. ctx->width, ctx->height,
  140. ctx->initial_pool_size - 1,
  141. s->format, D3DPOOL_DEFAULT, 0,
  142. frames_hwctx->surface_type,
  143. s->surfaces_internal, NULL);
  144. if (FAILED(hr)) {
  145. av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
  146. return AVERROR_UNKNOWN;
  147. }
  148. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
  149. ctx, dxva2_pool_alloc, NULL);
  150. if (!ctx->internal->pool_internal)
  151. return AVERROR(ENOMEM);
  152. frames_hwctx->surfaces = s->surfaces_internal;
  153. frames_hwctx->nb_surfaces = ctx->initial_pool_size;
  154. return 0;
  155. }
  156. static int dxva2_frames_init(AVHWFramesContext *ctx)
  157. {
  158. AVDXVA2FramesContext *hwctx = ctx->hwctx;
  159. DXVA2FramesContext *s = ctx->internal->priv;
  160. int ret;
  161. if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
  162. hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
  163. av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
  164. hwctx->surface_type);
  165. return AVERROR(EINVAL);
  166. }
  167. s->device_handle = INVALID_HANDLE_VALUE;
  168. /* init the frame pool if the caller didn't provide one */
  169. if (!ctx->pool) {
  170. ret = dxva2_init_pool(ctx);
  171. if (ret < 0) {
  172. av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
  173. return ret;
  174. }
  175. }
  176. return 0;
  177. }
  178. static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  179. {
  180. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  181. if (!frame->buf[0])
  182. return AVERROR(ENOMEM);
  183. frame->data[3] = frame->buf[0]->data;
  184. frame->format = AV_PIX_FMT_DXVA2_VLD;
  185. frame->width = ctx->width;
  186. frame->height = ctx->height;
  187. return 0;
  188. }
  189. static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
  190. enum AVHWFrameTransferDirection dir,
  191. enum AVPixelFormat **formats)
  192. {
  193. enum AVPixelFormat *fmts;
  194. fmts = av_malloc_array(2, sizeof(*fmts));
  195. if (!fmts)
  196. return AVERROR(ENOMEM);
  197. fmts[0] = ctx->sw_format;
  198. fmts[1] = AV_PIX_FMT_NONE;
  199. *formats = fmts;
  200. return 0;
  201. }
  202. static void dxva2_unmap_frame(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
  203. {
  204. IDirect3DSurface9 *surface = (IDirect3DSurface9*)hwmap->source->data[3];
  205. IDirect3DSurface9_UnlockRect(surface);
  206. av_freep(&hwmap->priv);
  207. }
  208. static int dxva2_map_frame(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src,
  209. int flags)
  210. {
  211. IDirect3DSurface9 *surface = (IDirect3DSurface9*)src->data[3];
  212. DXVA2Mapping *map;
  213. D3DSURFACE_DESC surfaceDesc;
  214. D3DLOCKED_RECT LockedRect;
  215. HRESULT hr;
  216. int i, err, nb_planes;
  217. int lock_flags = 0;
  218. nb_planes = av_pix_fmt_count_planes(dst->format);
  219. hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
  220. if (FAILED(hr)) {
  221. av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
  222. return AVERROR_UNKNOWN;
  223. }
  224. if (!(flags & AV_HWFRAME_MAP_WRITE))
  225. lock_flags |= D3DLOCK_READONLY;
  226. if (flags & AV_HWFRAME_MAP_OVERWRITE)
  227. lock_flags |= D3DLOCK_DISCARD;
  228. hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, lock_flags);
  229. if (FAILED(hr)) {
  230. av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
  231. return AVERROR_UNKNOWN;
  232. }
  233. map = av_mallocz(sizeof(*map));
  234. if (!map)
  235. goto fail;
  236. err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
  237. dxva2_unmap_frame, map);
  238. if (err < 0) {
  239. av_freep(&map);
  240. goto fail;
  241. }
  242. for (i = 0; i < nb_planes; i++)
  243. dst->linesize[i] = LockedRect.Pitch;
  244. av_image_fill_pointers(dst->data, dst->format, surfaceDesc.Height,
  245. (uint8_t*)LockedRect.pBits, dst->linesize);
  246. if (dst->format == AV_PIX_FMT_PAL8)
  247. dst->data[1] = (uint8_t*)map->palette_dummy;
  248. return 0;
  249. fail:
  250. IDirect3DSurface9_UnlockRect(surface);
  251. return err;
  252. }
  253. static int dxva2_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
  254. const AVFrame *src)
  255. {
  256. AVFrame *map;
  257. int ret;
  258. if (src->format != ctx->sw_format)
  259. return AVERROR(ENOSYS);
  260. map = av_frame_alloc();
  261. if (!map)
  262. return AVERROR(ENOMEM);
  263. map->format = dst->format;
  264. ret = dxva2_map_frame(ctx, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
  265. if (ret < 0)
  266. goto fail;
  267. av_image_copy(map->data, map->linesize, src->data, src->linesize,
  268. ctx->sw_format, src->width, src->height);
  269. fail:
  270. av_frame_free(&map);
  271. return ret;
  272. }
  273. static int dxva2_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
  274. const AVFrame *src)
  275. {
  276. AVFrame *map;
  277. ptrdiff_t src_linesize[4], dst_linesize[4];
  278. int ret, i;
  279. if (dst->format != ctx->sw_format)
  280. return AVERROR(ENOSYS);
  281. map = av_frame_alloc();
  282. if (!map)
  283. return AVERROR(ENOMEM);
  284. map->format = dst->format;
  285. ret = dxva2_map_frame(ctx, map, src, AV_HWFRAME_MAP_READ);
  286. if (ret < 0)
  287. goto fail;
  288. for (i = 0; i < 4; i++) {
  289. dst_linesize[i] = dst->linesize[i];
  290. src_linesize[i] = map->linesize[i];
  291. }
  292. av_image_copy_uc_from(dst->data, dst_linesize, map->data, src_linesize,
  293. ctx->sw_format, src->width, src->height);
  294. fail:
  295. av_frame_free(&map);
  296. return ret;
  297. }
  298. static int dxva2_map_from(AVHWFramesContext *ctx,
  299. AVFrame *dst, const AVFrame *src, int flags)
  300. {
  301. int err;
  302. if (dst->format != AV_PIX_FMT_NONE && dst->format != ctx->sw_format)
  303. return AVERROR(ENOSYS);
  304. dst->format = ctx->sw_format;
  305. err = dxva2_map_frame(ctx, dst, src, flags);
  306. if (err < 0)
  307. return err;
  308. err = av_frame_copy_props(dst, src);
  309. if (err < 0)
  310. return err;
  311. return 0;
  312. }
  313. static void dxva2_device_free(AVHWDeviceContext *ctx)
  314. {
  315. AVDXVA2DeviceContext *hwctx = ctx->hwctx;
  316. DXVA2DevicePriv *priv = ctx->user_opaque;
  317. if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
  318. IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
  319. if (hwctx->devmgr)
  320. IDirect3DDeviceManager9_Release(hwctx->devmgr);
  321. if (priv->d3d9device)
  322. IDirect3DDevice9_Release(priv->d3d9device);
  323. if (priv->d3d9)
  324. IDirect3D9_Release(priv->d3d9);
  325. if (priv->d3dlib)
  326. FreeLibrary(priv->d3dlib);
  327. if (priv->dxva2lib)
  328. FreeLibrary(priv->dxva2lib);
  329. av_freep(&ctx->user_opaque);
  330. }
  331. static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
  332. AVDictionary *opts, int flags)
  333. {
  334. AVDXVA2DeviceContext *hwctx = ctx->hwctx;
  335. DXVA2DevicePriv *priv;
  336. pDirect3DCreate9 *createD3D = NULL;
  337. pCreateDeviceManager9 *createDeviceManager = NULL;
  338. D3DPRESENT_PARAMETERS d3dpp = {0};
  339. D3DDISPLAYMODE d3ddm;
  340. unsigned resetToken = 0;
  341. UINT adapter = D3DADAPTER_DEFAULT;
  342. HRESULT hr;
  343. if (device)
  344. adapter = atoi(device);
  345. priv = av_mallocz(sizeof(*priv));
  346. if (!priv)
  347. return AVERROR(ENOMEM);
  348. ctx->user_opaque = priv;
  349. ctx->free = dxva2_device_free;
  350. priv->device_handle = INVALID_HANDLE_VALUE;
  351. priv->d3dlib = LoadLibrary("d3d9.dll");
  352. if (!priv->d3dlib) {
  353. av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
  354. return AVERROR_UNKNOWN;
  355. }
  356. priv->dxva2lib = LoadLibrary("dxva2.dll");
  357. if (!priv->dxva2lib) {
  358. av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
  359. return AVERROR_UNKNOWN;
  360. }
  361. createD3D = (pDirect3DCreate9 *)GetProcAddress(priv->d3dlib, "Direct3DCreate9");
  362. if (!createD3D) {
  363. av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
  364. return AVERROR_UNKNOWN;
  365. }
  366. createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(priv->dxva2lib,
  367. "DXVA2CreateDirect3DDeviceManager9");
  368. if (!createDeviceManager) {
  369. av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
  370. return AVERROR_UNKNOWN;
  371. }
  372. priv->d3d9 = createD3D(D3D_SDK_VERSION);
  373. if (!priv->d3d9) {
  374. av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
  375. return AVERROR_UNKNOWN;
  376. }
  377. IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
  378. d3dpp.Windowed = TRUE;
  379. d3dpp.BackBufferWidth = 640;
  380. d3dpp.BackBufferHeight = 480;
  381. d3dpp.BackBufferCount = 0;
  382. d3dpp.BackBufferFormat = d3ddm.Format;
  383. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  384. d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
  385. hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
  386. D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
  387. &d3dpp, &priv->d3d9device);
  388. if (FAILED(hr)) {
  389. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
  390. return AVERROR_UNKNOWN;
  391. }
  392. hr = createDeviceManager(&resetToken, &hwctx->devmgr);
  393. if (FAILED(hr)) {
  394. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
  395. return AVERROR_UNKNOWN;
  396. }
  397. hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
  398. if (FAILED(hr)) {
  399. av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
  400. return AVERROR_UNKNOWN;
  401. }
  402. hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
  403. if (FAILED(hr)) {
  404. av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
  405. return AVERROR_UNKNOWN;
  406. }
  407. return 0;
  408. }
  409. const HWContextType ff_hwcontext_type_dxva2 = {
  410. .type = AV_HWDEVICE_TYPE_DXVA2,
  411. .name = "DXVA2",
  412. .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
  413. .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
  414. .frames_priv_size = sizeof(DXVA2FramesContext),
  415. .device_create = dxva2_device_create,
  416. .frames_init = dxva2_frames_init,
  417. .frames_uninit = dxva2_frames_uninit,
  418. .frames_get_buffer = dxva2_get_buffer,
  419. .transfer_get_formats = dxva2_transfer_get_formats,
  420. .transfer_data_to = dxva2_transfer_data_to,
  421. .transfer_data_from = dxva2_transfer_data_from,
  422. .map_from = dxva2_map_from,
  423. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },
  424. };