hwcontext_qsv.c 25 KB

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