hwcontext_dxva2.c 15 KB

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