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.

426 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. };
  57. DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
  58. DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
  59. static void dxva2_frames_uninit(AVHWFramesContext *ctx)
  60. {
  61. AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  62. AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
  63. DXVA2FramesContext *s = ctx->internal->priv;
  64. int i;
  65. if (frames_hwctx->decoder_to_release)
  66. IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
  67. if (s->surfaces_internal) {
  68. for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
  69. if (s->surfaces_internal[i])
  70. IDirect3DSurface9_Release(s->surfaces_internal[i]);
  71. }
  72. }
  73. av_freep(&s->surfaces_internal);
  74. if (s->service) {
  75. IDirectXVideoAccelerationService_Release(s->service);
  76. s->service = NULL;
  77. }
  78. if (s->device_handle != INVALID_HANDLE_VALUE) {
  79. IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
  80. s->device_handle = INVALID_HANDLE_VALUE;
  81. }
  82. }
  83. static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
  84. {
  85. AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
  86. DXVA2FramesContext *s = ctx->internal->priv;
  87. AVDXVA2FramesContext *hwctx = ctx->hwctx;
  88. if (s->nb_surfaces_used < hwctx->nb_surfaces) {
  89. s->nb_surfaces_used++;
  90. return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
  91. sizeof(*hwctx->surfaces), NULL, 0, 0);
  92. }
  93. return NULL;
  94. }
  95. static int dxva2_init_pool(AVHWFramesContext *ctx)
  96. {
  97. AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
  98. AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  99. DXVA2FramesContext *s = ctx->internal->priv;
  100. int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
  101. int i;
  102. HRESULT hr;
  103. if (ctx->initial_pool_size <= 0)
  104. return 0;
  105. hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
  106. if (FAILED(hr)) {
  107. av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
  108. return AVERROR_UNKNOWN;
  109. }
  110. hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
  111. s->device_handle,
  112. decode ? &video_decoder_service : &video_processor_service,
  113. (void **)&s->service);
  114. if (FAILED(hr)) {
  115. av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
  116. return AVERROR_UNKNOWN;
  117. }
  118. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
  119. if (ctx->sw_format == supported_formats[i].pix_fmt) {
  120. s->format = supported_formats[i].d3d_format;
  121. break;
  122. }
  123. }
  124. if (i == FF_ARRAY_ELEMS(supported_formats)) {
  125. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
  126. av_get_pix_fmt_name(ctx->sw_format));
  127. return AVERROR(EINVAL);
  128. }
  129. s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
  130. sizeof(*s->surfaces_internal));
  131. if (!s->surfaces_internal)
  132. return AVERROR(ENOMEM);
  133. hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
  134. ctx->width, ctx->height,
  135. ctx->initial_pool_size - 1,
  136. s->format, D3DPOOL_DEFAULT, 0,
  137. frames_hwctx->surface_type,
  138. s->surfaces_internal, NULL);
  139. if (FAILED(hr)) {
  140. av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
  141. return AVERROR_UNKNOWN;
  142. }
  143. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
  144. ctx, dxva2_pool_alloc, NULL);
  145. if (!ctx->internal->pool_internal)
  146. return AVERROR(ENOMEM);
  147. frames_hwctx->surfaces = s->surfaces_internal;
  148. frames_hwctx->nb_surfaces = ctx->initial_pool_size;
  149. return 0;
  150. }
  151. static int dxva2_frames_init(AVHWFramesContext *ctx)
  152. {
  153. AVDXVA2FramesContext *hwctx = ctx->hwctx;
  154. DXVA2FramesContext *s = ctx->internal->priv;
  155. int ret;
  156. if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
  157. hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
  158. av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
  159. hwctx->surface_type);
  160. return AVERROR(EINVAL);
  161. }
  162. s->device_handle = INVALID_HANDLE_VALUE;
  163. /* init the frame pool if the caller didn't provide one */
  164. if (!ctx->pool) {
  165. ret = dxva2_init_pool(ctx);
  166. if (ret < 0) {
  167. av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
  168. return ret;
  169. }
  170. }
  171. return 0;
  172. }
  173. static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  174. {
  175. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  176. if (!frame->buf[0])
  177. return AVERROR(ENOMEM);
  178. frame->data[3] = frame->buf[0]->data;
  179. frame->format = AV_PIX_FMT_DXVA2_VLD;
  180. frame->width = ctx->width;
  181. frame->height = ctx->height;
  182. return 0;
  183. }
  184. static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
  185. enum AVHWFrameTransferDirection dir,
  186. enum AVPixelFormat **formats)
  187. {
  188. enum AVPixelFormat *fmts;
  189. fmts = av_malloc_array(2, sizeof(*fmts));
  190. if (!fmts)
  191. return AVERROR(ENOMEM);
  192. fmts[0] = ctx->sw_format;
  193. fmts[1] = AV_PIX_FMT_NONE;
  194. *formats = fmts;
  195. return 0;
  196. }
  197. static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
  198. const AVFrame *src)
  199. {
  200. IDirect3DSurface9 *surface;
  201. D3DSURFACE_DESC surfaceDesc;
  202. D3DLOCKED_RECT LockedRect;
  203. HRESULT hr;
  204. int download = !!src->hw_frames_ctx;
  205. surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
  206. hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
  207. if (FAILED(hr)) {
  208. av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
  209. return AVERROR_UNKNOWN;
  210. }
  211. hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
  212. download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
  213. if (FAILED(hr)) {
  214. av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
  215. return AVERROR_UNKNOWN;
  216. }
  217. if (download) {
  218. av_image_copy_plane(dst->data[0], dst->linesize[0],
  219. (uint8_t*)LockedRect.pBits, LockedRect.Pitch,
  220. src->width, src->height);
  221. av_image_copy_plane(dst->data[1], dst->linesize[1],
  222. (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
  223. LockedRect.Pitch, src->width, src->height / 2);
  224. } else {
  225. av_image_copy_plane((uint8_t*)LockedRect.pBits, LockedRect.Pitch,
  226. dst->data[0], dst->linesize[0],
  227. src->width, src->height);
  228. av_image_copy_plane((uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
  229. LockedRect.Pitch, dst->data[1], dst->linesize[1],
  230. src->width, src->height / 2);
  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. };