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.

307 lines
10KB

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