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.

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