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.

488 lines
15KB

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