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.

431 lines
14KB

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