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.

583 lines
18KB

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