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.

883 lines
27KB

  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 <stdint.h>
  19. #include <string.h>
  20. #include <mfx/mfxvideo.h>
  21. #include "config.h"
  22. #if CONFIG_VAAPI
  23. #include "hwcontext_vaapi.h"
  24. #endif
  25. #if CONFIG_DXVA2
  26. #include "hwcontext_dxva2.h"
  27. #endif
  28. #include "buffer.h"
  29. #include "common.h"
  30. #include "hwcontext.h"
  31. #include "hwcontext_internal.h"
  32. #include "hwcontext_qsv.h"
  33. #include "mem.h"
  34. #include "pixfmt.h"
  35. #include "pixdesc.h"
  36. #include "time.h"
  37. typedef struct QSVDevicePriv {
  38. AVBufferRef *child_device_ctx;
  39. } QSVDevicePriv;
  40. typedef struct QSVDeviceContext {
  41. mfxHDL handle;
  42. mfxHandleType handle_type;
  43. mfxVersion ver;
  44. mfxIMPL impl;
  45. enum AVHWDeviceType child_device_type;
  46. enum AVPixelFormat child_pix_fmt;
  47. } QSVDeviceContext;
  48. typedef struct QSVFramesContext {
  49. mfxSession session_download;
  50. mfxSession session_upload;
  51. AVBufferRef *child_frames_ref;
  52. mfxFrameSurface1 *surfaces_internal;
  53. int nb_surfaces_used;
  54. // used in the frame allocator for non-opaque surfaces
  55. mfxMemId *mem_ids;
  56. // used in the opaque alloc request for opaque surfaces
  57. mfxFrameSurface1 **surface_ptrs;
  58. mfxExtOpaqueSurfaceAlloc opaque_alloc;
  59. mfxExtBuffer *ext_buffers[1];
  60. } QSVFramesContext;
  61. static const struct {
  62. mfxHandleType handle_type;
  63. enum AVHWDeviceType device_type;
  64. enum AVPixelFormat pix_fmt;
  65. } supported_handle_types[] = {
  66. #if CONFIG_VAAPI
  67. { MFX_HANDLE_VA_DISPLAY, AV_HWDEVICE_TYPE_VAAPI, AV_PIX_FMT_VAAPI },
  68. #endif
  69. #if CONFIG_DXVA2
  70. { MFX_HANDLE_D3D9_DEVICE_MANAGER, AV_HWDEVICE_TYPE_DXVA2, AV_PIX_FMT_DXVA2_VLD },
  71. #endif
  72. { 0 },
  73. };
  74. static const struct {
  75. enum AVPixelFormat pix_fmt;
  76. uint32_t fourcc;
  77. } supported_pixel_formats[] = {
  78. { AV_PIX_FMT_NV12, MFX_FOURCC_NV12 },
  79. { AV_PIX_FMT_P010, MFX_FOURCC_P010 },
  80. { AV_PIX_FMT_PAL8, MFX_FOURCC_P8 },
  81. };
  82. static int qsv_device_init(AVHWDeviceContext *ctx)
  83. {
  84. AVQSVDeviceContext *hwctx = ctx->hwctx;
  85. QSVDeviceContext *s = ctx->internal->priv;
  86. mfxStatus err;
  87. int i;
  88. for (i = 0; supported_handle_types[i].handle_type; i++) {
  89. err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
  90. &s->handle);
  91. if (err == MFX_ERR_NONE) {
  92. s->handle_type = supported_handle_types[i].handle_type;
  93. s->child_device_type = supported_handle_types[i].device_type;
  94. s->child_pix_fmt = supported_handle_types[i].pix_fmt;
  95. break;
  96. }
  97. }
  98. if (!s->handle) {
  99. av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
  100. "from the session\n");
  101. }
  102. err = MFXQueryIMPL(hwctx->session, &s->impl);
  103. if (err == MFX_ERR_NONE)
  104. err = MFXQueryVersion(hwctx->session, &s->ver);
  105. if (err != MFX_ERR_NONE) {
  106. av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
  107. return AVERROR_UNKNOWN;
  108. }
  109. return 0;
  110. }
  111. static void qsv_frames_uninit(AVHWFramesContext *ctx)
  112. {
  113. QSVFramesContext *s = ctx->internal->priv;
  114. if (s->session_download) {
  115. MFXVideoVPP_Close(s->session_download);
  116. MFXClose(s->session_download);
  117. }
  118. s->session_download = NULL;
  119. if (s->session_upload) {
  120. MFXVideoVPP_Close(s->session_upload);
  121. MFXClose(s->session_upload);
  122. }
  123. s->session_upload = NULL;
  124. av_freep(&s->mem_ids);
  125. av_freep(&s->surface_ptrs);
  126. av_freep(&s->surfaces_internal);
  127. av_buffer_unref(&s->child_frames_ref);
  128. }
  129. static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
  130. {
  131. }
  132. static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
  133. {
  134. AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
  135. QSVFramesContext *s = ctx->internal->priv;
  136. AVQSVFramesContext *hwctx = ctx->hwctx;
  137. if (s->nb_surfaces_used < hwctx->nb_surfaces) {
  138. s->nb_surfaces_used++;
  139. return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
  140. sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
  141. }
  142. return NULL;
  143. }
  144. static int qsv_init_child_ctx(AVHWFramesContext *ctx)
  145. {
  146. AVQSVFramesContext *hwctx = ctx->hwctx;
  147. QSVFramesContext *s = ctx->internal->priv;
  148. QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
  149. AVBufferRef *child_device_ref = NULL;
  150. AVBufferRef *child_frames_ref = NULL;
  151. AVHWDeviceContext *child_device_ctx;
  152. AVHWFramesContext *child_frames_ctx;
  153. int i, ret = 0;
  154. if (!device_priv->handle) {
  155. av_log(ctx, AV_LOG_ERROR,
  156. "Cannot create a non-opaque internal surface pool without "
  157. "a hardware handle\n");
  158. return AVERROR(EINVAL);
  159. }
  160. child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
  161. if (!child_device_ref)
  162. return AVERROR(ENOMEM);
  163. child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
  164. #if CONFIG_VAAPI
  165. if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
  166. AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  167. child_device_hwctx->display = (VADisplay)device_priv->handle;
  168. }
  169. #endif
  170. #if CONFIG_DXVA2
  171. if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
  172. AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  173. child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
  174. }
  175. #endif
  176. ret = av_hwdevice_ctx_init(child_device_ref);
  177. if (ret < 0) {
  178. av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
  179. goto fail;
  180. }
  181. child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
  182. if (!child_frames_ref) {
  183. ret = AVERROR(ENOMEM);
  184. goto fail;
  185. }
  186. child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
  187. child_frames_ctx->format = device_priv->child_pix_fmt;
  188. child_frames_ctx->sw_format = ctx->sw_format;
  189. child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
  190. child_frames_ctx->width = ctx->width;
  191. child_frames_ctx->height = ctx->height;
  192. #if CONFIG_DXVA2
  193. if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
  194. AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
  195. if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
  196. child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
  197. else
  198. child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
  199. }
  200. #endif
  201. ret = av_hwframe_ctx_init(child_frames_ref);
  202. if (ret < 0) {
  203. av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
  204. goto fail;
  205. }
  206. #if CONFIG_VAAPI
  207. if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
  208. AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
  209. for (i = 0; i < ctx->initial_pool_size; i++)
  210. s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
  211. hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  212. }
  213. #endif
  214. #if CONFIG_DXVA2
  215. if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
  216. AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
  217. for (i = 0; i < ctx->initial_pool_size; i++)
  218. s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
  219. if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
  220. hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
  221. else
  222. hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  223. }
  224. #endif
  225. s->child_frames_ref = child_frames_ref;
  226. child_frames_ref = NULL;
  227. fail:
  228. av_buffer_unref(&child_device_ref);
  229. av_buffer_unref(&child_frames_ref);
  230. return ret;
  231. }
  232. static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
  233. {
  234. QSVFramesContext *s = ctx->internal->priv;
  235. AVQSVFramesContext *frames_hwctx = ctx->hwctx;
  236. const AVPixFmtDescriptor *desc;
  237. int i, ret = 0;
  238. desc = av_pix_fmt_desc_get(ctx->sw_format);
  239. if (!desc)
  240. return AVERROR_BUG;
  241. if (ctx->initial_pool_size <= 0) {
  242. av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
  243. return AVERROR(EINVAL);
  244. }
  245. s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
  246. sizeof(*s->surfaces_internal));
  247. if (!s->surfaces_internal)
  248. return AVERROR(ENOMEM);
  249. for (i = 0; i < ctx->initial_pool_size; i++) {
  250. mfxFrameSurface1 *surf = &s->surfaces_internal[i];
  251. surf->Info.BitDepthLuma = desc->comp[0].depth;
  252. surf->Info.BitDepthChroma = desc->comp[0].depth;
  253. surf->Info.Shift = desc->comp[0].depth > 8;
  254. if (desc->log2_chroma_w && desc->log2_chroma_h)
  255. surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  256. else if (desc->log2_chroma_w)
  257. surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
  258. else
  259. surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
  260. surf->Info.FourCC = fourcc;
  261. surf->Info.Width = ctx->width;
  262. surf->Info.CropW = ctx->width;
  263. surf->Info.Height = ctx->height;
  264. surf->Info.CropH = ctx->height;
  265. surf->Info.FrameRateExtN = 25;
  266. surf->Info.FrameRateExtD = 1;
  267. }
  268. if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
  269. ret = qsv_init_child_ctx(ctx);
  270. if (ret < 0)
  271. return ret;
  272. }
  273. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
  274. ctx, qsv_pool_alloc, NULL);
  275. if (!ctx->internal->pool_internal)
  276. return AVERROR(ENOMEM);
  277. frames_hwctx->surfaces = s->surfaces_internal;
  278. frames_hwctx->nb_surfaces = ctx->initial_pool_size;
  279. return 0;
  280. }
  281. static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  282. mfxFrameAllocResponse *resp)
  283. {
  284. AVHWFramesContext *ctx = pthis;
  285. QSVFramesContext *s = ctx->internal->priv;
  286. AVQSVFramesContext *hwctx = ctx->hwctx;
  287. mfxFrameInfo *i = &req->Info;
  288. mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
  289. if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
  290. !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
  291. !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
  292. return MFX_ERR_UNSUPPORTED;
  293. if (i->Width != i1->Width || i->Height != i1->Height ||
  294. i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
  295. av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
  296. "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
  297. i->Width, i->Height, i->FourCC, i->ChromaFormat,
  298. i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
  299. return MFX_ERR_UNSUPPORTED;
  300. }
  301. resp->mids = s->mem_ids;
  302. resp->NumFrameActual = hwctx->nb_surfaces;
  303. return MFX_ERR_NONE;
  304. }
  305. static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  306. {
  307. return MFX_ERR_NONE;
  308. }
  309. static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  310. {
  311. return MFX_ERR_UNSUPPORTED;
  312. }
  313. static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  314. {
  315. return MFX_ERR_UNSUPPORTED;
  316. }
  317. static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  318. {
  319. *hdl = mid;
  320. return MFX_ERR_NONE;
  321. }
  322. static int qsv_init_internal_session(AVHWFramesContext *ctx,
  323. mfxSession *session, int upload)
  324. {
  325. QSVFramesContext *s = ctx->internal->priv;
  326. AVQSVFramesContext *frames_hwctx = ctx->hwctx;
  327. QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
  328. int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
  329. mfxFrameAllocator frame_allocator = {
  330. .pthis = ctx,
  331. .Alloc = frame_alloc,
  332. .Lock = frame_lock,
  333. .Unlock = frame_unlock,
  334. .GetHDL = frame_get_hdl,
  335. .Free = frame_free,
  336. };
  337. mfxVideoParam par;
  338. mfxStatus err;
  339. err = MFXInit(device_priv->impl, &device_priv->ver, session);
  340. if (err != MFX_ERR_NONE) {
  341. av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
  342. return AVERROR_UNKNOWN;
  343. }
  344. if (device_priv->handle) {
  345. err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
  346. device_priv->handle);
  347. if (err != MFX_ERR_NONE)
  348. return AVERROR_UNKNOWN;
  349. }
  350. if (!opaque) {
  351. err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
  352. if (err != MFX_ERR_NONE)
  353. return AVERROR_UNKNOWN;
  354. }
  355. memset(&par, 0, sizeof(par));
  356. if (opaque) {
  357. par.ExtParam = s->ext_buffers;
  358. par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
  359. par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
  360. MFX_IOPATTERN_IN_OPAQUE_MEMORY;
  361. } else {
  362. par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
  363. MFX_IOPATTERN_IN_VIDEO_MEMORY;
  364. }
  365. par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
  366. MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  367. par.AsyncDepth = 1;
  368. par.vpp.In = frames_hwctx->surfaces[0].Info;
  369. /* Apparently VPP requires the frame rate to be set to some value, otherwise
  370. * init will fail (probably for the framerate conversion filter). Since we
  371. * are only doing data upload/download here, we just invent an arbitrary
  372. * value */
  373. par.vpp.In.FrameRateExtN = 25;
  374. par.vpp.In.FrameRateExtD = 1;
  375. par.vpp.Out = par.vpp.In;
  376. err = MFXVideoVPP_Init(*session, &par);
  377. if (err != MFX_ERR_NONE) {
  378. av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
  379. "Surface upload/download will not be possible\n");
  380. MFXClose(*session);
  381. *session = NULL;
  382. }
  383. return 0;
  384. }
  385. static int qsv_frames_init(AVHWFramesContext *ctx)
  386. {
  387. QSVFramesContext *s = ctx->internal->priv;
  388. AVQSVFramesContext *frames_hwctx = ctx->hwctx;
  389. int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
  390. uint32_t fourcc = 0;
  391. int i, ret;
  392. for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
  393. if (supported_pixel_formats[i].pix_fmt == ctx->sw_format) {
  394. fourcc = supported_pixel_formats[i].fourcc;
  395. break;
  396. }
  397. }
  398. if (!fourcc) {
  399. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  400. return AVERROR(ENOSYS);
  401. }
  402. if (!ctx->pool) {
  403. ret = qsv_init_pool(ctx, fourcc);
  404. if (ret < 0) {
  405. av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
  406. return ret;
  407. }
  408. }
  409. if (opaque) {
  410. s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
  411. sizeof(*s->surface_ptrs));
  412. if (!s->surface_ptrs)
  413. return AVERROR(ENOMEM);
  414. for (i = 0; i < frames_hwctx->nb_surfaces; i++)
  415. s->surface_ptrs[i] = frames_hwctx->surfaces + i;
  416. s->opaque_alloc.In.Surfaces = s->surface_ptrs;
  417. s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
  418. s->opaque_alloc.In.Type = frames_hwctx->frame_type;
  419. s->opaque_alloc.Out = s->opaque_alloc.In;
  420. s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
  421. s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
  422. s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
  423. } else {
  424. s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
  425. if (!s->mem_ids)
  426. return AVERROR(ENOMEM);
  427. for (i = 0; i < frames_hwctx->nb_surfaces; i++)
  428. s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
  429. }
  430. ret = qsv_init_internal_session(ctx, &s->session_download, 0);
  431. if (ret < 0)
  432. return ret;
  433. ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
  434. if (ret < 0)
  435. return ret;
  436. return 0;
  437. }
  438. static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  439. {
  440. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  441. if (!frame->buf[0])
  442. return AVERROR(ENOMEM);
  443. frame->data[3] = frame->buf[0]->data;
  444. frame->format = AV_PIX_FMT_QSV;
  445. frame->width = ctx->width;
  446. frame->height = ctx->height;
  447. return 0;
  448. }
  449. static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
  450. enum AVHWFrameTransferDirection dir,
  451. enum AVPixelFormat **formats)
  452. {
  453. enum AVPixelFormat *fmts;
  454. fmts = av_malloc_array(2, sizeof(*fmts));
  455. if (!fmts)
  456. return AVERROR(ENOMEM);
  457. fmts[0] = ctx->sw_format;
  458. fmts[1] = AV_PIX_FMT_NONE;
  459. *formats = fmts;
  460. return 0;
  461. }
  462. static int qsv_map_from(AVHWFramesContext *ctx,
  463. AVFrame *dst, const AVFrame *src, int flags)
  464. {
  465. QSVFramesContext *s = ctx->internal->priv;
  466. mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
  467. AVHWFramesContext *child_frames_ctx;
  468. AVFrame *dummy;
  469. int ret = 0;
  470. if (!s->child_frames_ref)
  471. return AVERROR(ENOSYS);
  472. child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
  473. dummy = av_frame_alloc();
  474. if (!dummy)
  475. return AVERROR(ENOMEM);
  476. dummy->buf[0] = av_buffer_ref(src->buf[0]);
  477. dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
  478. if (!dummy->buf[0] || !dummy->hw_frames_ctx)
  479. goto fail;
  480. dummy->format = child_frames_ctx->format;
  481. dummy->width = src->width;
  482. dummy->height = src->height;
  483. dummy->data[3] = surf->Data.MemId;
  484. ret = av_hwframe_map(dst, dummy, flags);
  485. fail:
  486. av_frame_free(&dummy);
  487. return ret;
  488. }
  489. static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst,
  490. const AVFrame *src)
  491. {
  492. QSVFramesContext *s = ctx->internal->priv;
  493. AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
  494. int download = !!src->hw_frames_ctx;
  495. mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
  496. AVFrame *dummy;
  497. int ret;
  498. dummy = av_frame_alloc();
  499. if (!dummy)
  500. return AVERROR(ENOMEM);
  501. dummy->format = child_frames_ctx->format;
  502. dummy->width = src->width;
  503. dummy->height = src->height;
  504. dummy->buf[0] = download ? src->buf[0] : dst->buf[0];
  505. dummy->data[3] = surf->Data.MemId;
  506. dummy->hw_frames_ctx = s->child_frames_ref;
  507. ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
  508. av_hwframe_transfer_data(dummy, src, 0);
  509. dummy->buf[0] = NULL;
  510. dummy->data[3] = NULL;
  511. dummy->hw_frames_ctx = NULL;
  512. av_frame_free(&dummy);
  513. return ret;
  514. }
  515. static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
  516. const AVFrame *src)
  517. {
  518. QSVFramesContext *s = ctx->internal->priv;
  519. mfxFrameSurface1 out = {{ 0 }};
  520. mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
  521. mfxSyncPoint sync = NULL;
  522. mfxStatus err;
  523. if (!s->session_download) {
  524. if (s->child_frames_ref)
  525. return qsv_transfer_data_child(ctx, dst, src);
  526. av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
  527. return AVERROR(ENOSYS);
  528. }
  529. out.Info = in->Info;
  530. out.Data.PitchLow = dst->linesize[0];
  531. out.Data.Y = dst->data[0];
  532. out.Data.U = dst->data[1];
  533. out.Data.V = dst->data[2];
  534. out.Data.A = dst->data[3];
  535. do {
  536. err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
  537. if (err == MFX_WRN_DEVICE_BUSY)
  538. av_usleep(1);
  539. } while (err == MFX_WRN_DEVICE_BUSY);
  540. if (err < 0 || !sync) {
  541. av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
  542. return AVERROR_UNKNOWN;
  543. }
  544. do {
  545. err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
  546. } while (err == MFX_WRN_IN_EXECUTION);
  547. if (err < 0) {
  548. av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
  549. return AVERROR_UNKNOWN;
  550. }
  551. return 0;
  552. }
  553. static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
  554. const AVFrame *src)
  555. {
  556. QSVFramesContext *s = ctx->internal->priv;
  557. mfxFrameSurface1 in = {{ 0 }};
  558. mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
  559. mfxSyncPoint sync = NULL;
  560. mfxStatus err;
  561. if (!s->session_upload) {
  562. if (s->child_frames_ref)
  563. return qsv_transfer_data_child(ctx, dst, src);
  564. av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
  565. return AVERROR(ENOSYS);
  566. }
  567. in.Info = out->Info;
  568. in.Data.PitchLow = src->linesize[0];
  569. in.Data.Y = src->data[0];
  570. in.Data.U = src->data[1];
  571. in.Data.V = src->data[2];
  572. in.Data.A = src->data[3];
  573. do {
  574. err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
  575. if (err == MFX_WRN_DEVICE_BUSY)
  576. av_usleep(1);
  577. } while (err == MFX_WRN_DEVICE_BUSY);
  578. if (err < 0 || !sync) {
  579. av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
  580. return AVERROR_UNKNOWN;
  581. }
  582. do {
  583. err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
  584. } while (err == MFX_WRN_IN_EXECUTION);
  585. if (err < 0) {
  586. av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
  587. return AVERROR_UNKNOWN;
  588. }
  589. return 0;
  590. }
  591. static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
  592. const void *hwconfig,
  593. AVHWFramesConstraints *constraints)
  594. {
  595. int i;
  596. constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
  597. sizeof(*constraints->valid_sw_formats));
  598. if (!constraints->valid_sw_formats)
  599. return AVERROR(ENOMEM);
  600. for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
  601. constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
  602. constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
  603. constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
  604. if (!constraints->valid_hw_formats)
  605. return AVERROR(ENOMEM);
  606. constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
  607. constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
  608. return 0;
  609. }
  610. static void qsv_device_free(AVHWDeviceContext *ctx)
  611. {
  612. AVQSVDeviceContext *hwctx = ctx->hwctx;
  613. QSVDevicePriv *priv = ctx->user_opaque;
  614. if (hwctx->session)
  615. MFXClose(hwctx->session);
  616. av_buffer_unref(&priv->child_device_ctx);
  617. av_freep(&priv);
  618. }
  619. static mfxIMPL choose_implementation(const char *device)
  620. {
  621. static const struct {
  622. const char *name;
  623. mfxIMPL impl;
  624. } impl_map[] = {
  625. { "auto", MFX_IMPL_AUTO },
  626. { "sw", MFX_IMPL_SOFTWARE },
  627. { "hw", MFX_IMPL_HARDWARE },
  628. { "auto_any", MFX_IMPL_AUTO_ANY },
  629. { "hw_any", MFX_IMPL_HARDWARE_ANY },
  630. { "hw2", MFX_IMPL_HARDWARE2 },
  631. { "hw3", MFX_IMPL_HARDWARE3 },
  632. { "hw4", MFX_IMPL_HARDWARE4 },
  633. };
  634. mfxIMPL impl = MFX_IMPL_AUTO_ANY;
  635. int i;
  636. if (device) {
  637. for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
  638. if (!strcmp(device, impl_map[i].name)) {
  639. impl = impl_map[i].impl;
  640. break;
  641. }
  642. if (i == FF_ARRAY_ELEMS(impl_map))
  643. impl = strtol(device, NULL, 0);
  644. }
  645. return impl;
  646. }
  647. static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
  648. AVDictionary *opts, int flags)
  649. {
  650. AVQSVDeviceContext *hwctx = ctx->hwctx;
  651. QSVDevicePriv *priv;
  652. enum AVHWDeviceType child_device_type;
  653. AVDictionaryEntry *e;
  654. mfxVersion ver = { { 3, 1 } };
  655. mfxIMPL impl;
  656. mfxHDL handle;
  657. mfxHandleType handle_type;
  658. mfxStatus err;
  659. int ret;
  660. priv = av_mallocz(sizeof(*priv));
  661. if (!priv)
  662. return AVERROR(ENOMEM);
  663. ctx->user_opaque = priv;
  664. ctx->free = qsv_device_free;
  665. e = av_dict_get(opts, "child_device", NULL, 0);
  666. if (CONFIG_VAAPI)
  667. child_device_type = AV_HWDEVICE_TYPE_VAAPI;
  668. else if (CONFIG_DXVA2)
  669. child_device_type = AV_HWDEVICE_TYPE_DXVA2;
  670. else {
  671. av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
  672. return AVERROR(ENOSYS);
  673. }
  674. ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
  675. e ? e->value : NULL, NULL, 0);
  676. if (ret < 0)
  677. return ret;
  678. {
  679. AVHWDeviceContext *child_device_ctx = (AVHWDeviceContext*)priv->child_device_ctx->data;
  680. #if CONFIG_VAAPI
  681. AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  682. handle_type = MFX_HANDLE_VA_DISPLAY;
  683. handle = (mfxHDL)child_device_hwctx->display;
  684. #elif CONFIG_DXVA2
  685. AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  686. handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
  687. handle = (mfxHDL)child_device_hwctx->devmgr;
  688. #endif
  689. }
  690. impl = choose_implementation(device);
  691. err = MFXInit(impl, &ver, &hwctx->session);
  692. if (err != MFX_ERR_NONE) {
  693. av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
  694. return AVERROR_UNKNOWN;
  695. }
  696. err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
  697. if (err != MFX_ERR_NONE)
  698. return AVERROR_UNKNOWN;
  699. return 0;
  700. }
  701. const HWContextType ff_hwcontext_type_qsv = {
  702. .type = AV_HWDEVICE_TYPE_QSV,
  703. .name = "QSV",
  704. .device_hwctx_size = sizeof(AVQSVDeviceContext),
  705. .device_priv_size = sizeof(QSVDeviceContext),
  706. .frames_hwctx_size = sizeof(AVQSVFramesContext),
  707. .frames_priv_size = sizeof(QSVFramesContext),
  708. .device_create = qsv_device_create,
  709. .device_init = qsv_device_init,
  710. .frames_get_constraints = qsv_frames_get_constraints,
  711. .frames_init = qsv_frames_init,
  712. .frames_uninit = qsv_frames_uninit,
  713. .frames_get_buffer = qsv_get_buffer,
  714. .transfer_get_formats = qsv_transfer_get_formats,
  715. .transfer_data_to = qsv_transfer_data_to,
  716. .transfer_data_from = qsv_transfer_data_from,
  717. .map_from = qsv_map_from,
  718. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },
  719. };