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.

440 lines
14KB

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