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.

592 lines
18KB

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