hwcontext_dxva2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. #define DXVA2API_USE_BITFIELDS
  20. #define COBJMACROS
  21. #include <d3d9.h>
  22. #include <dxva2api.h>
  23. #include <initguid.h>
  24. #include "avassert.h"
  25. #include "common.h"
  26. #include "hwcontext.h"
  27. #include "hwcontext_dxva2.h"
  28. #include "hwcontext_internal.h"
  29. #include "imgutils.h"
  30. #include "pixdesc.h"
  31. #include "pixfmt.h"
  32. #include "compat/w32dlfcn.h"
  33. typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
  34. typedef HRESULT WINAPI pDirect3DCreate9Ex(UINT, IDirect3D9Ex **);
  35. typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
  36. #define FF_D3DCREATE_FLAGS (D3DCREATE_SOFTWARE_VERTEXPROCESSING | \
  37. D3DCREATE_MULTITHREADED | \
  38. D3DCREATE_FPU_PRESERVE)
  39. static const D3DPRESENT_PARAMETERS dxva2_present_params = {
  40. .Windowed = TRUE,
  41. .BackBufferWidth = 640,
  42. .BackBufferHeight = 480,
  43. .BackBufferCount = 0,
  44. .SwapEffect = D3DSWAPEFFECT_DISCARD,
  45. .Flags = D3DPRESENTFLAG_VIDEO,
  46. };
  47. typedef struct DXVA2Mapping {
  48. uint32_t palette_dummy[256];
  49. } DXVA2Mapping;
  50. typedef struct DXVA2FramesContext {
  51. IDirect3DSurface9 **surfaces_internal;
  52. int nb_surfaces_used;
  53. HANDLE device_handle;
  54. IDirectXVideoAccelerationService *service;
  55. D3DFORMAT format;
  56. } DXVA2FramesContext;
  57. typedef struct DXVA2DevicePriv {
  58. HMODULE d3dlib;
  59. HMODULE dxva2lib;
  60. HANDLE device_handle;
  61. IDirect3D9 *d3d9;
  62. IDirect3DDevice9 *d3d9device;
  63. } DXVA2DevicePriv;
  64. static const struct {
  65. D3DFORMAT d3d_format;
  66. enum AVPixelFormat pix_fmt;
  67. } supported_formats[] = {
  68. { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
  69. { MKTAG('P', '0', '1', '0'), AV_PIX_FMT_P010 },
  70. { D3DFMT_P8, AV_PIX_FMT_PAL8 },
  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 void dxva2_pool_release_dummy(void *opaque, uint8_t *data)
  99. {
  100. // important not to free anything here--data is a surface object
  101. // associated with the call to CreateSurface(), and these surfaces are
  102. // released in dxva2_frames_uninit()
  103. }
  104. static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
  105. {
  106. AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
  107. DXVA2FramesContext *s = ctx->internal->priv;
  108. AVDXVA2FramesContext *hwctx = ctx->hwctx;
  109. if (s->nb_surfaces_used < hwctx->nb_surfaces) {
  110. s->nb_surfaces_used++;
  111. return av_buffer_create((uint8_t*)s->surfaces_internal[s->nb_surfaces_used - 1],
  112. sizeof(*hwctx->surfaces), dxva2_pool_release_dummy, 0, 0);
  113. }
  114. return NULL;
  115. }
  116. static int dxva2_init_pool(AVHWFramesContext *ctx)
  117. {
  118. AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
  119. AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  120. DXVA2FramesContext *s = ctx->internal->priv;
  121. int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
  122. int i;
  123. HRESULT hr;
  124. if (ctx->initial_pool_size <= 0)
  125. return 0;
  126. hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
  127. if (FAILED(hr)) {
  128. av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
  129. return AVERROR_UNKNOWN;
  130. }
  131. hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
  132. s->device_handle,
  133. decode ? &video_decoder_service : &video_processor_service,
  134. (void **)&s->service);
  135. if (FAILED(hr)) {
  136. av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
  137. return AVERROR_UNKNOWN;
  138. }
  139. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
  140. if (ctx->sw_format == supported_formats[i].pix_fmt) {
  141. s->format = supported_formats[i].d3d_format;
  142. break;
  143. }
  144. }
  145. if (i == FF_ARRAY_ELEMS(supported_formats)) {
  146. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
  147. av_get_pix_fmt_name(ctx->sw_format));
  148. return AVERROR(EINVAL);
  149. }
  150. s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
  151. sizeof(*s->surfaces_internal));
  152. if (!s->surfaces_internal)
  153. return AVERROR(ENOMEM);
  154. hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
  155. ctx->width, ctx->height,
  156. ctx->initial_pool_size - 1,
  157. s->format, D3DPOOL_DEFAULT, 0,
  158. frames_hwctx->surface_type,
  159. s->surfaces_internal, NULL);
  160. if (FAILED(hr)) {
  161. av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
  162. return AVERROR_UNKNOWN;
  163. }
  164. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
  165. ctx, dxva2_pool_alloc, NULL);
  166. if (!ctx->internal->pool_internal)
  167. return AVERROR(ENOMEM);
  168. frames_hwctx->surfaces = s->surfaces_internal;
  169. frames_hwctx->nb_surfaces = ctx->initial_pool_size;
  170. return 0;
  171. }
  172. static int dxva2_frames_init(AVHWFramesContext *ctx)
  173. {
  174. AVDXVA2FramesContext *hwctx = ctx->hwctx;
  175. DXVA2FramesContext *s = ctx->internal->priv;
  176. int ret;
  177. if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
  178. hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
  179. av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
  180. hwctx->surface_type);
  181. return AVERROR(EINVAL);
  182. }
  183. s->device_handle = INVALID_HANDLE_VALUE;
  184. /* init the frame pool if the caller didn't provide one */
  185. if (!ctx->pool) {
  186. ret = dxva2_init_pool(ctx);
  187. if (ret < 0) {
  188. av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
  189. return ret;
  190. }
  191. }
  192. return 0;
  193. }
  194. static int dxva2_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  195. {
  196. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  197. if (!frame->buf[0])
  198. return AVERROR(ENOMEM);
  199. frame->data[3] = frame->buf[0]->data;
  200. frame->format = AV_PIX_FMT_DXVA2_VLD;
  201. frame->width = ctx->width;
  202. frame->height = ctx->height;
  203. return 0;
  204. }
  205. static int dxva2_transfer_get_formats(AVHWFramesContext *ctx,
  206. enum AVHWFrameTransferDirection dir,
  207. enum AVPixelFormat **formats)
  208. {
  209. enum AVPixelFormat *fmts;
  210. fmts = av_malloc_array(2, sizeof(*fmts));
  211. if (!fmts)
  212. return AVERROR(ENOMEM);
  213. fmts[0] = ctx->sw_format;
  214. fmts[1] = AV_PIX_FMT_NONE;
  215. *formats = fmts;
  216. return 0;
  217. }
  218. static void dxva2_unmap_frame(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
  219. {
  220. IDirect3DSurface9 *surface = (IDirect3DSurface9*)hwmap->source->data[3];
  221. IDirect3DSurface9_UnlockRect(surface);
  222. av_freep(&hwmap->priv);
  223. }
  224. static int dxva2_map_frame(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src,
  225. int flags)
  226. {
  227. IDirect3DSurface9 *surface = (IDirect3DSurface9*)src->data[3];
  228. DXVA2Mapping *map;
  229. D3DSURFACE_DESC surfaceDesc;
  230. D3DLOCKED_RECT LockedRect;
  231. HRESULT hr;
  232. int i, err, nb_planes;
  233. int lock_flags = 0;
  234. nb_planes = av_pix_fmt_count_planes(dst->format);
  235. hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
  236. if (FAILED(hr)) {
  237. av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
  238. return AVERROR_UNKNOWN;
  239. }
  240. if (!(flags & AV_HWFRAME_MAP_WRITE))
  241. lock_flags |= D3DLOCK_READONLY;
  242. if (flags & AV_HWFRAME_MAP_OVERWRITE)
  243. lock_flags |= D3DLOCK_DISCARD;
  244. hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, lock_flags);
  245. if (FAILED(hr)) {
  246. av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
  247. return AVERROR_UNKNOWN;
  248. }
  249. map = av_mallocz(sizeof(*map));
  250. if (!map) {
  251. err = AVERROR(ENOMEM);
  252. goto fail;
  253. }
  254. err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
  255. dxva2_unmap_frame, map);
  256. if (err < 0) {
  257. av_freep(&map);
  258. goto fail;
  259. }
  260. for (i = 0; i < nb_planes; i++)
  261. dst->linesize[i] = LockedRect.Pitch;
  262. av_image_fill_pointers(dst->data, dst->format, surfaceDesc.Height,
  263. (uint8_t*)LockedRect.pBits, dst->linesize);
  264. if (dst->format == AV_PIX_FMT_PAL8)
  265. dst->data[1] = (uint8_t*)map->palette_dummy;
  266. return 0;
  267. fail:
  268. IDirect3DSurface9_UnlockRect(surface);
  269. return err;
  270. }
  271. static int dxva2_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
  272. const AVFrame *src)
  273. {
  274. AVFrame *map;
  275. int ret;
  276. if (src->format != ctx->sw_format)
  277. return AVERROR(ENOSYS);
  278. map = av_frame_alloc();
  279. if (!map)
  280. return AVERROR(ENOMEM);
  281. map->format = dst->format;
  282. ret = dxva2_map_frame(ctx, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
  283. if (ret < 0)
  284. goto fail;
  285. av_image_copy(map->data, map->linesize, (const uint8_t **)src->data, src->linesize,
  286. ctx->sw_format, src->width, src->height);
  287. fail:
  288. av_frame_free(&map);
  289. return ret;
  290. }
  291. static int dxva2_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
  292. const AVFrame *src)
  293. {
  294. AVFrame *map;
  295. ptrdiff_t src_linesize[4], dst_linesize[4];
  296. int ret, i;
  297. if (dst->format != ctx->sw_format)
  298. return AVERROR(ENOSYS);
  299. map = av_frame_alloc();
  300. if (!map)
  301. return AVERROR(ENOMEM);
  302. map->format = dst->format;
  303. ret = dxva2_map_frame(ctx, map, src, AV_HWFRAME_MAP_READ);
  304. if (ret < 0)
  305. goto fail;
  306. for (i = 0; i < 4; i++) {
  307. dst_linesize[i] = dst->linesize[i];
  308. src_linesize[i] = map->linesize[i];
  309. }
  310. av_image_copy_uc_from(dst->data, dst_linesize, (const uint8_t **)map->data, src_linesize,
  311. ctx->sw_format, src->width, src->height);
  312. fail:
  313. av_frame_free(&map);
  314. return ret;
  315. }
  316. static int dxva2_map_from(AVHWFramesContext *ctx,
  317. AVFrame *dst, const AVFrame *src, int flags)
  318. {
  319. int err;
  320. if (dst->format != AV_PIX_FMT_NONE && dst->format != ctx->sw_format)
  321. return AVERROR(ENOSYS);
  322. dst->format = ctx->sw_format;
  323. err = dxva2_map_frame(ctx, dst, src, flags);
  324. if (err < 0)
  325. return err;
  326. err = av_frame_copy_props(dst, src);
  327. if (err < 0)
  328. return err;
  329. return 0;
  330. }
  331. static void dxva2_device_free(AVHWDeviceContext *ctx)
  332. {
  333. AVDXVA2DeviceContext *hwctx = ctx->hwctx;
  334. DXVA2DevicePriv *priv = ctx->user_opaque;
  335. if (hwctx->devmgr && priv->device_handle != INVALID_HANDLE_VALUE)
  336. IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->device_handle);
  337. if (hwctx->devmgr)
  338. IDirect3DDeviceManager9_Release(hwctx->devmgr);
  339. if (priv->d3d9device)
  340. IDirect3DDevice9_Release(priv->d3d9device);
  341. if (priv->d3d9)
  342. IDirect3D9_Release(priv->d3d9);
  343. if (priv->d3dlib)
  344. dlclose(priv->d3dlib);
  345. if (priv->dxva2lib)
  346. dlclose(priv->dxva2lib);
  347. av_freep(&ctx->user_opaque);
  348. }
  349. static int dxva2_device_create9(AVHWDeviceContext *ctx, UINT adapter)
  350. {
  351. DXVA2DevicePriv *priv = ctx->user_opaque;
  352. D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
  353. D3DDISPLAYMODE d3ddm;
  354. HRESULT hr;
  355. pDirect3DCreate9 *createD3D = (pDirect3DCreate9 *)dlsym(priv->d3dlib, "Direct3DCreate9");
  356. if (!createD3D) {
  357. av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
  358. return AVERROR_UNKNOWN;
  359. }
  360. priv->d3d9 = createD3D(D3D_SDK_VERSION);
  361. if (!priv->d3d9) {
  362. av_log(ctx, AV_LOG_ERROR, "Failed to create IDirect3D object\n");
  363. return AVERROR_UNKNOWN;
  364. }
  365. IDirect3D9_GetAdapterDisplayMode(priv->d3d9, adapter, &d3ddm);
  366. d3dpp.BackBufferFormat = d3ddm.Format;
  367. hr = IDirect3D9_CreateDevice(priv->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
  368. FF_D3DCREATE_FLAGS,
  369. &d3dpp, &priv->d3d9device);
  370. if (FAILED(hr)) {
  371. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device\n");
  372. return AVERROR_UNKNOWN;
  373. }
  374. return 0;
  375. }
  376. static int dxva2_device_create9ex(AVHWDeviceContext *ctx, UINT adapter)
  377. {
  378. DXVA2DevicePriv *priv = ctx->user_opaque;
  379. D3DPRESENT_PARAMETERS d3dpp = dxva2_present_params;
  380. D3DDISPLAYMODEEX modeex = {0};
  381. IDirect3D9Ex *d3d9ex = NULL;
  382. IDirect3DDevice9Ex *exdev = NULL;
  383. HRESULT hr;
  384. pDirect3DCreate9Ex *createD3DEx = (pDirect3DCreate9Ex *)dlsym(priv->d3dlib, "Direct3DCreate9Ex");
  385. if (!createD3DEx)
  386. return AVERROR(ENOSYS);
  387. hr = createD3DEx(D3D_SDK_VERSION, &d3d9ex);
  388. if (FAILED(hr))
  389. return AVERROR_UNKNOWN;
  390. modeex.Size = sizeof(D3DDISPLAYMODEEX);
  391. hr = IDirect3D9Ex_GetAdapterDisplayModeEx(d3d9ex, adapter, &modeex, NULL);
  392. if (FAILED(hr)) {
  393. IDirect3D9Ex_Release(d3d9ex);
  394. return AVERROR_UNKNOWN;
  395. }
  396. d3dpp.BackBufferFormat = modeex.Format;
  397. hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
  398. FF_D3DCREATE_FLAGS,
  399. &d3dpp, NULL, &exdev);
  400. if (FAILED(hr)) {
  401. IDirect3D9Ex_Release(d3d9ex);
  402. return AVERROR_UNKNOWN;
  403. }
  404. av_log(ctx, AV_LOG_VERBOSE, "Using D3D9Ex device.\n");
  405. priv->d3d9 = (IDirect3D9 *)d3d9ex;
  406. priv->d3d9device = (IDirect3DDevice9 *)exdev;
  407. return 0;
  408. }
  409. static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
  410. AVDictionary *opts, int flags)
  411. {
  412. AVDXVA2DeviceContext *hwctx = ctx->hwctx;
  413. DXVA2DevicePriv *priv;
  414. pCreateDeviceManager9 *createDeviceManager = NULL;
  415. unsigned resetToken = 0;
  416. UINT adapter = D3DADAPTER_DEFAULT;
  417. HRESULT hr;
  418. int err;
  419. if (device)
  420. adapter = atoi(device);
  421. priv = av_mallocz(sizeof(*priv));
  422. if (!priv)
  423. return AVERROR(ENOMEM);
  424. ctx->user_opaque = priv;
  425. ctx->free = dxva2_device_free;
  426. priv->device_handle = INVALID_HANDLE_VALUE;
  427. priv->d3dlib = dlopen("d3d9.dll", 0);
  428. if (!priv->d3dlib) {
  429. av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
  430. return AVERROR_UNKNOWN;
  431. }
  432. priv->dxva2lib = dlopen("dxva2.dll", 0);
  433. if (!priv->dxva2lib) {
  434. av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
  435. return AVERROR_UNKNOWN;
  436. }
  437. createDeviceManager = (pCreateDeviceManager9 *)dlsym(priv->dxva2lib,
  438. "DXVA2CreateDirect3DDeviceManager9");
  439. if (!createDeviceManager) {
  440. av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
  441. return AVERROR_UNKNOWN;
  442. }
  443. if (dxva2_device_create9ex(ctx, adapter) < 0) {
  444. // Retry with "classic" d3d9
  445. err = dxva2_device_create9(ctx, adapter);
  446. if (err < 0)
  447. return err;
  448. }
  449. hr = createDeviceManager(&resetToken, &hwctx->devmgr);
  450. if (FAILED(hr)) {
  451. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device manager\n");
  452. return AVERROR_UNKNOWN;
  453. }
  454. hr = IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, priv->d3d9device, resetToken);
  455. if (FAILED(hr)) {
  456. av_log(ctx, AV_LOG_ERROR, "Failed to bind Direct3D device to device manager\n");
  457. return AVERROR_UNKNOWN;
  458. }
  459. hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &priv->device_handle);
  460. if (FAILED(hr)) {
  461. av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
  462. return AVERROR_UNKNOWN;
  463. }
  464. return 0;
  465. }
  466. const HWContextType ff_hwcontext_type_dxva2 = {
  467. .type = AV_HWDEVICE_TYPE_DXVA2,
  468. .name = "DXVA2",
  469. .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
  470. .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
  471. .frames_priv_size = sizeof(DXVA2FramesContext),
  472. .device_create = dxva2_device_create,
  473. .frames_init = dxva2_frames_init,
  474. .frames_uninit = dxva2_frames_uninit,
  475. .frames_get_buffer = dxva2_get_buffer,
  476. .transfer_get_formats = dxva2_transfer_get_formats,
  477. .transfer_data_to = dxva2_transfer_data_to,
  478. .transfer_data_from = dxva2_transfer_data_from,
  479. .map_from = dxva2_map_from,
  480. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },
  481. };