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.

497 lines
16KB

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