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.

429 lines
14KB

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