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.

293 lines
9.7KB

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