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.

556 lines
17KB

  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. nb_planes = av_pix_fmt_count_planes(dst->format);
  232. hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
  233. if (FAILED(hr)) {
  234. av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
  235. return AVERROR_UNKNOWN;
  236. }
  237. hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
  238. flags & AV_HWFRAME_MAP_READ ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
  239. if (FAILED(hr)) {
  240. av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
  241. return AVERROR_UNKNOWN;
  242. }
  243. map = av_mallocz(sizeof(*map));
  244. if (!map)
  245. goto fail;
  246. err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
  247. dxva2_unmap_frame, map);
  248. if (err < 0) {
  249. av_freep(&map);
  250. goto fail;
  251. }
  252. for (i = 0; i < nb_planes; i++)
  253. dst->linesize[i] = LockedRect.Pitch;
  254. av_image_fill_pointers(dst->data, dst->format, surfaceDesc.Height,
  255. (uint8_t*)LockedRect.pBits, dst->linesize);
  256. if (dst->format == AV_PIX_FMT_PAL8)
  257. dst->data[1] = (uint8_t*)map->palette_dummy;
  258. return 0;
  259. fail:
  260. IDirect3DSurface9_UnlockRect(surface);
  261. return err;
  262. }
  263. static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
  264. const AVFrame *src)
  265. {
  266. int download = !!src->hw_frames_ctx;
  267. AVFrame *map;
  268. int ret, i;
  269. map = av_frame_alloc();
  270. if (!map)
  271. return AVERROR(ENOMEM);
  272. map->format = dst->format;
  273. ret = dxva2_map_frame(ctx, map, download ? src : dst,
  274. download ? AV_HWFRAME_MAP_READ : AV_HWFRAME_MAP_WRITE);
  275. if (ret < 0)
  276. goto fail;
  277. if (download) {
  278. ptrdiff_t src_linesize[4], dst_linesize[4];
  279. for (i = 0; i < 4; i++) {
  280. dst_linesize[i] = dst->linesize[i];
  281. src_linesize[i] = map->linesize[i];
  282. }
  283. av_image_copy_uc_from(dst->data, dst_linesize, map->data, src_linesize,
  284. ctx->sw_format, src->width, src->height);
  285. } else {
  286. av_image_copy(map->data, map->linesize, src->data, src->linesize,
  287. ctx->sw_format, src->width, src->height);
  288. }
  289. fail:
  290. av_frame_free(&map);
  291. return ret;
  292. }
  293. static int dxva2_map_from(AVHWFramesContext *ctx,
  294. AVFrame *dst, const AVFrame *src, int flags)
  295. {
  296. int err;
  297. err = dxva2_map_frame(ctx, dst, src, flags);
  298. if (err < 0)
  299. return err;
  300. err = av_frame_copy_props(dst, src);
  301. if (err < 0)
  302. return err;
  303. return 0;
  304. }
  305. static void dxva2_device_free(AVHWDeviceContext *ctx)
  306. {
  307. AVDXVA2DeviceContext *hwctx = ctx->hwctx;
  308. DXVA2DevicePriv *priv = ctx->user_opaque;
  309. if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
  310. IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
  311. if (hwctx->devmgr)
  312. IDirect3DDeviceManager9_Release(hwctx->devmgr);
  313. if (priv->d3d9device)
  314. IDirect3DDevice9_Release(priv->d3d9device);
  315. if (priv->d3d9)
  316. IDirect3D9_Release(priv->d3d9);
  317. if (priv->d3dlib)
  318. dlclose(priv->d3dlib);
  319. if (priv->dxva2lib)
  320. dlclose(priv->dxva2lib);
  321. av_freep(&ctx->user_opaque);
  322. }
  323. static int dxva2_device_create9(AVHWDeviceContext *ctx, UINT adapter)
  324. {
  325. DXVA2DevicePriv *priv = ctx->user_opaque;
  326. D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
  327. D3DDISPLAYMODE d3ddm;
  328. HRESULT hr;
  329. pDirect3DCreate9 *createD3D = (pDirect3DCreate9 *)dlsym(priv->d3dlib, "Direct3DCreate9");
  330. if (!createD3D) {
  331. av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
  332. return AVERROR_UNKNOWN;
  333. }
  334. priv->d3d9 = createD3D(D3D_SDK_VERSION);
  335. if (!priv->d3d9) {
  336. av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
  337. return AVERROR_UNKNOWN;
  338. }
  339. IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
  340. d3dpp.BackBufferFormat = d3ddm.Format;
  341. hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
  342. FF_D3DCREATE_FLAGS,
  343. &d3dpp, &priv->d3d9device);
  344. if (FAILED(hr)) {
  345. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
  346. return AVERROR_UNKNOWN;
  347. }
  348. return 0;
  349. }
  350. static int dxva2_device_create9ex(AVHWDeviceContext *ctx, UINT adapter)
  351. {
  352. DXVA2DevicePriv *priv = ctx->user_opaque;
  353. D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
  354. D3DDISPLAYMODEEX modeex = {0};
  355. IDirect3D9Ex *d3d9ex = NULL;
  356. IDirect3DDevice9Ex *exdev = NULL;
  357. HRESULT hr;
  358. pDirect3DCreate9Ex *createD3DEx = (pDirect3DCreate9Ex *)dlsym(priv->d3dlib, "Direct3DCreate9Ex");
  359. if (!createD3DEx)
  360. return AVERROR(ENOSYS);
  361. hr = createD3DEx(D3D_SDK_VERSION, &d3d9ex);
  362. if (FAILED(hr))
  363. return AVERROR_UNKNOWN;
  364. IDirect3D9Ex_GetAdapterDisplayModeEx(d3d9ex, adapter, &modeex, NULL);
  365. d3dpp.BackBufferFormat = modeex.Format;
  366. hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
  367. FF_D3DCREATE_FLAGS,
  368. &d3dpp, NULL, &exdev);
  369. if (FAILED(hr)) {
  370. IDirect3D9Ex_Release(d3d9ex);
  371. return AVERROR_UNKNOWN;
  372. }
  373. av_log(ctx, AV_LOG_VERBOSE, "Using D3D9Ex device.\n");
  374. priv->d3d9 = (IDirect3D9 *)d3d9ex;
  375. priv->d3d9device = (IDirect3DDevice9 *)exdev;
  376. return 0;
  377. }
  378. static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
  379. AVDictionary *opts, int flags)
  380. {
  381. AVDXVA2DeviceContext *hwctx = ctx->hwctx;
  382. DXVA2DevicePriv *priv;
  383. pCreateDeviceManager9 *createDeviceManager = NULL;
  384. unsigned resetToken = 0;
  385. UINT adapter = D3DADAPTER_DEFAULT;
  386. HRESULT hr;
  387. int err;
  388. if (device)
  389. adapter = atoi(device);
  390. priv = av_mallocz(sizeof(*priv));
  391. if (!priv)
  392. return AVERROR(ENOMEM);
  393. ctx->user_opaque = priv;
  394. ctx->free = dxva2_device_free;
  395. priv->device_handle = INVALID_HANDLE_VALUE;
  396. priv->d3dlib = dlopen("d3d9.dll", 0);
  397. if (!priv->d3dlib) {
  398. av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
  399. return AVERROR_UNKNOWN;
  400. }
  401. priv->dxva2lib = dlopen("dxva2.dll", 0);
  402. if (!priv->dxva2lib) {
  403. av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
  404. return AVERROR_UNKNOWN;
  405. }
  406. createDeviceManager = (pCreateDeviceManager9 *)dlsym(priv->dxva2lib,
  407. "DXVA2CreateDirect3DDeviceManager9");
  408. if (!createDeviceManager) {
  409. av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
  410. return AVERROR_UNKNOWN;
  411. }
  412. if (dxva2_device_create9ex(ctx, adapter) < 0) {
  413. // Retry with "classic" d3d9
  414. err = dxva2_device_create9(ctx, adapter);
  415. if (err < 0)
  416. return err;
  417. }
  418. hr = createDeviceManager(&resetToken, &hwctx->devmgr);
  419. if (FAILED(hr)) {
  420. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
  421. return AVERROR_UNKNOWN;
  422. }
  423. hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
  424. if (FAILED(hr)) {
  425. av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
  426. return AVERROR_UNKNOWN;
  427. }
  428. hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
  429. if (FAILED(hr)) {
  430. av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
  431. return AVERROR_UNKNOWN;
  432. }
  433. return 0;
  434. }
  435. const HWContextType ff_hwcontext_type_dxva2 = {
  436. .type = AV_HWDEVICE_TYPE_DXVA2,
  437. .name = "DXVA2",
  438. .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
  439. .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
  440. .frames_priv_size = sizeof(DXVA2FramesContext),
  441. .device_create = dxva2_device_create,
  442. .frames_init = dxva2_frames_init,
  443. .frames_uninit = dxva2_frames_uninit,
  444. .frames_get_buffer = dxva2_get_buffer,
  445. .transfer_get_formats = dxva2_transfer_get_formats,
  446. .transfer_data_to = dxva2_transfer_data,
  447. .transfer_data_from = dxva2_transfer_data,
  448. .map_from = dxva2_map_from,
  449. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },
  450. };