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.

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