hwcontext_qsv.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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 uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
  83. {
  84. int i;
  85. for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
  86. if (supported_pixel_formats[i].pix_fmt == pix_fmt)
  87. return supported_pixel_formats[i].fourcc;
  88. }
  89. return 0;
  90. }
  91. static int qsv_device_init(AVHWDeviceContext *ctx)
  92. {
  93. AVQSVDeviceContext *hwctx = ctx->hwctx;
  94. QSVDeviceContext *s = ctx->internal->priv;
  95. mfxStatus err;
  96. int i;
  97. for (i = 0; supported_handle_types[i].handle_type; i++) {
  98. err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
  99. &s->handle);
  100. if (err == MFX_ERR_NONE) {
  101. s->handle_type = supported_handle_types[i].handle_type;
  102. s->child_device_type = supported_handle_types[i].device_type;
  103. s->child_pix_fmt = supported_handle_types[i].pix_fmt;
  104. break;
  105. }
  106. }
  107. if (!s->handle) {
  108. av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
  109. "from the session\n");
  110. }
  111. err = MFXQueryIMPL(hwctx->session, &s->impl);
  112. if (err == MFX_ERR_NONE)
  113. err = MFXQueryVersion(hwctx->session, &s->ver);
  114. if (err != MFX_ERR_NONE) {
  115. av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
  116. return AVERROR_UNKNOWN;
  117. }
  118. return 0;
  119. }
  120. static void qsv_frames_uninit(AVHWFramesContext *ctx)
  121. {
  122. QSVFramesContext *s = ctx->internal->priv;
  123. if (s->session_download) {
  124. MFXVideoVPP_Close(s->session_download);
  125. MFXClose(s->session_download);
  126. }
  127. s->session_download = NULL;
  128. if (s->session_upload) {
  129. MFXVideoVPP_Close(s->session_upload);
  130. MFXClose(s->session_upload);
  131. }
  132. s->session_upload = NULL;
  133. av_freep(&s->mem_ids);
  134. av_freep(&s->surface_ptrs);
  135. av_freep(&s->surfaces_internal);
  136. av_buffer_unref(&s->child_frames_ref);
  137. }
  138. static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
  139. {
  140. }
  141. static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
  142. {
  143. AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
  144. QSVFramesContext *s = ctx->internal->priv;
  145. AVQSVFramesContext *hwctx = ctx->hwctx;
  146. if (s->nb_surfaces_used < hwctx->nb_surfaces) {
  147. s->nb_surfaces_used++;
  148. return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
  149. sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
  150. }
  151. return NULL;
  152. }
  153. static int qsv_init_child_ctx(AVHWFramesContext *ctx)
  154. {
  155. AVQSVFramesContext *hwctx = ctx->hwctx;
  156. QSVFramesContext *s = ctx->internal->priv;
  157. QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
  158. AVBufferRef *child_device_ref = NULL;
  159. AVBufferRef *child_frames_ref = NULL;
  160. AVHWDeviceContext *child_device_ctx;
  161. AVHWFramesContext *child_frames_ctx;
  162. int i, ret = 0;
  163. if (!device_priv->handle) {
  164. av_log(ctx, AV_LOG_ERROR,
  165. "Cannot create a non-opaque internal surface pool without "
  166. "a hardware handle\n");
  167. return AVERROR(EINVAL);
  168. }
  169. child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
  170. if (!child_device_ref)
  171. return AVERROR(ENOMEM);
  172. child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
  173. #if CONFIG_VAAPI
  174. if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
  175. AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  176. child_device_hwctx->display = (VADisplay)device_priv->handle;
  177. }
  178. #endif
  179. #if CONFIG_DXVA2
  180. if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
  181. AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  182. child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
  183. }
  184. #endif
  185. ret = av_hwdevice_ctx_init(child_device_ref);
  186. if (ret < 0) {
  187. av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
  188. goto fail;
  189. }
  190. child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
  191. if (!child_frames_ref) {
  192. ret = AVERROR(ENOMEM);
  193. goto fail;
  194. }
  195. child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
  196. child_frames_ctx->format = device_priv->child_pix_fmt;
  197. child_frames_ctx->sw_format = ctx->sw_format;
  198. child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
  199. child_frames_ctx->width = FFALIGN(ctx->width, 16);
  200. child_frames_ctx->height = FFALIGN(ctx->height, 16);
  201. #if CONFIG_DXVA2
  202. if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
  203. AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
  204. if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
  205. child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
  206. else
  207. child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
  208. }
  209. #endif
  210. ret = av_hwframe_ctx_init(child_frames_ref);
  211. if (ret < 0) {
  212. av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
  213. goto fail;
  214. }
  215. #if CONFIG_VAAPI
  216. if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
  217. AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
  218. for (i = 0; i < ctx->initial_pool_size; i++)
  219. s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
  220. hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  221. }
  222. #endif
  223. #if CONFIG_DXVA2
  224. if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
  225. AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
  226. for (i = 0; i < ctx->initial_pool_size; i++)
  227. s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
  228. if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
  229. hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
  230. else
  231. hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  232. }
  233. #endif
  234. s->child_frames_ref = child_frames_ref;
  235. child_frames_ref = NULL;
  236. fail:
  237. av_buffer_unref(&child_device_ref);
  238. av_buffer_unref(&child_frames_ref);
  239. return ret;
  240. }
  241. static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
  242. {
  243. const AVPixFmtDescriptor *desc;
  244. uint32_t fourcc;
  245. desc = av_pix_fmt_desc_get(ctx->sw_format);
  246. if (!desc)
  247. return AVERROR(EINVAL);
  248. fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
  249. if (!fourcc)
  250. return AVERROR(EINVAL);
  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 = FFALIGN(ctx->width, 16);
  262. surf->Info.CropW = ctx->width;
  263. surf->Info.Height = FFALIGN(ctx->height, 16);
  264. surf->Info.CropH = ctx->height;
  265. surf->Info.FrameRateExtN = 25;
  266. surf->Info.FrameRateExtD = 1;
  267. surf->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  268. return 0;
  269. }
  270. static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
  271. {
  272. QSVFramesContext *s = ctx->internal->priv;
  273. AVQSVFramesContext *frames_hwctx = ctx->hwctx;
  274. int i, ret = 0;
  275. if (ctx->initial_pool_size <= 0) {
  276. av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
  277. return AVERROR(EINVAL);
  278. }
  279. s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
  280. sizeof(*s->surfaces_internal));
  281. if (!s->surfaces_internal)
  282. return AVERROR(ENOMEM);
  283. for (i = 0; i < ctx->initial_pool_size; i++) {
  284. ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
  285. if (ret < 0)
  286. return ret;
  287. }
  288. if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
  289. ret = qsv_init_child_ctx(ctx);
  290. if (ret < 0)
  291. return ret;
  292. }
  293. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
  294. ctx, qsv_pool_alloc, NULL);
  295. if (!ctx->internal->pool_internal)
  296. return AVERROR(ENOMEM);
  297. frames_hwctx->surfaces = s->surfaces_internal;
  298. frames_hwctx->nb_surfaces = ctx->initial_pool_size;
  299. return 0;
  300. }
  301. static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  302. mfxFrameAllocResponse *resp)
  303. {
  304. AVHWFramesContext *ctx = pthis;
  305. QSVFramesContext *s = ctx->internal->priv;
  306. AVQSVFramesContext *hwctx = ctx->hwctx;
  307. mfxFrameInfo *i = &req->Info;
  308. mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
  309. if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
  310. !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
  311. !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
  312. return MFX_ERR_UNSUPPORTED;
  313. if (i->Width != i1->Width || i->Height != i1->Height ||
  314. i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
  315. av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
  316. "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
  317. i->Width, i->Height, i->FourCC, i->ChromaFormat,
  318. i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
  319. return MFX_ERR_UNSUPPORTED;
  320. }
  321. resp->mids = s->mem_ids;
  322. resp->NumFrameActual = hwctx->nb_surfaces;
  323. return MFX_ERR_NONE;
  324. }
  325. static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  326. {
  327. return MFX_ERR_NONE;
  328. }
  329. static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  330. {
  331. return MFX_ERR_UNSUPPORTED;
  332. }
  333. static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  334. {
  335. return MFX_ERR_UNSUPPORTED;
  336. }
  337. static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  338. {
  339. *hdl = mid;
  340. return MFX_ERR_NONE;
  341. }
  342. static int qsv_init_internal_session(AVHWFramesContext *ctx,
  343. mfxSession *session, int upload)
  344. {
  345. QSVFramesContext *s = ctx->internal->priv;
  346. AVQSVFramesContext *frames_hwctx = ctx->hwctx;
  347. QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
  348. int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
  349. mfxFrameAllocator frame_allocator = {
  350. .pthis = ctx,
  351. .Alloc = frame_alloc,
  352. .Lock = frame_lock,
  353. .Unlock = frame_unlock,
  354. .GetHDL = frame_get_hdl,
  355. .Free = frame_free,
  356. };
  357. mfxVideoParam par;
  358. mfxStatus err;
  359. err = MFXInit(device_priv->impl, &device_priv->ver, session);
  360. if (err != MFX_ERR_NONE) {
  361. av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
  362. return AVERROR_UNKNOWN;
  363. }
  364. if (device_priv->handle) {
  365. err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
  366. device_priv->handle);
  367. if (err != MFX_ERR_NONE)
  368. return AVERROR_UNKNOWN;
  369. }
  370. if (!opaque) {
  371. err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
  372. if (err != MFX_ERR_NONE)
  373. return AVERROR_UNKNOWN;
  374. }
  375. memset(&par, 0, sizeof(par));
  376. if (opaque) {
  377. par.ExtParam = s->ext_buffers;
  378. par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
  379. par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
  380. MFX_IOPATTERN_IN_OPAQUE_MEMORY;
  381. } else {
  382. par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
  383. MFX_IOPATTERN_IN_VIDEO_MEMORY;
  384. }
  385. par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
  386. MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  387. par.AsyncDepth = 1;
  388. par.vpp.In = frames_hwctx->surfaces[0].Info;
  389. /* Apparently VPP requires the frame rate to be set to some value, otherwise
  390. * init will fail (probably for the framerate conversion filter). Since we
  391. * are only doing data upload/download here, we just invent an arbitrary
  392. * value */
  393. par.vpp.In.FrameRateExtN = 25;
  394. par.vpp.In.FrameRateExtD = 1;
  395. par.vpp.Out = par.vpp.In;
  396. err = MFXVideoVPP_Init(*session, &par);
  397. if (err != MFX_ERR_NONE) {
  398. av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
  399. "Surface upload/download will not be possible\n");
  400. MFXClose(*session);
  401. *session = NULL;
  402. }
  403. return 0;
  404. }
  405. static int qsv_frames_init(AVHWFramesContext *ctx)
  406. {
  407. QSVFramesContext *s = ctx->internal->priv;
  408. AVQSVFramesContext *frames_hwctx = ctx->hwctx;
  409. int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
  410. uint32_t fourcc;
  411. int i, ret;
  412. fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
  413. if (!fourcc) {
  414. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  415. return AVERROR(ENOSYS);
  416. }
  417. if (!ctx->pool) {
  418. ret = qsv_init_pool(ctx, fourcc);
  419. if (ret < 0) {
  420. av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
  421. return ret;
  422. }
  423. }
  424. if (opaque) {
  425. s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
  426. sizeof(*s->surface_ptrs));
  427. if (!s->surface_ptrs)
  428. return AVERROR(ENOMEM);
  429. for (i = 0; i < frames_hwctx->nb_surfaces; i++)
  430. s->surface_ptrs[i] = frames_hwctx->surfaces + i;
  431. s->opaque_alloc.In.Surfaces = s->surface_ptrs;
  432. s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
  433. s->opaque_alloc.In.Type = frames_hwctx->frame_type;
  434. s->opaque_alloc.Out = s->opaque_alloc.In;
  435. s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
  436. s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
  437. s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
  438. } else {
  439. s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
  440. if (!s->mem_ids)
  441. return AVERROR(ENOMEM);
  442. for (i = 0; i < frames_hwctx->nb_surfaces; i++)
  443. s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
  444. }
  445. ret = qsv_init_internal_session(ctx, &s->session_download, 0);
  446. if (ret < 0)
  447. return ret;
  448. ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
  449. if (ret < 0)
  450. return ret;
  451. return 0;
  452. }
  453. static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  454. {
  455. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  456. if (!frame->buf[0])
  457. return AVERROR(ENOMEM);
  458. frame->data[3] = frame->buf[0]->data;
  459. frame->format = AV_PIX_FMT_QSV;
  460. frame->width = ctx->width;
  461. frame->height = ctx->height;
  462. return 0;
  463. }
  464. static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
  465. enum AVHWFrameTransferDirection dir,
  466. enum AVPixelFormat **formats)
  467. {
  468. enum AVPixelFormat *fmts;
  469. fmts = av_malloc_array(2, sizeof(*fmts));
  470. if (!fmts)
  471. return AVERROR(ENOMEM);
  472. fmts[0] = ctx->sw_format;
  473. fmts[1] = AV_PIX_FMT_NONE;
  474. *formats = fmts;
  475. return 0;
  476. }
  477. static int qsv_frames_derive_from(AVHWFramesContext *dst_ctx,
  478. AVHWFramesContext *src_ctx, int flags)
  479. {
  480. AVQSVFramesContext *src_hwctx = src_ctx->hwctx;
  481. int i;
  482. switch (dst_ctx->device_ctx->type) {
  483. #if CONFIG_VAAPI
  484. case AV_HWDEVICE_TYPE_VAAPI:
  485. {
  486. AVVAAPIFramesContext *dst_hwctx = dst_ctx->hwctx;
  487. dst_hwctx->surface_ids = av_mallocz_array(src_hwctx->nb_surfaces,
  488. sizeof(*dst_hwctx->surface_ids));
  489. if (!dst_hwctx->surface_ids)
  490. return AVERROR(ENOMEM);
  491. for (i = 0; i < src_hwctx->nb_surfaces; i++)
  492. dst_hwctx->surface_ids[i] =
  493. *(VASurfaceID*)src_hwctx->surfaces[i].Data.MemId;
  494. dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
  495. }
  496. break;
  497. #endif
  498. #if CONFIG_DXVA2
  499. case AV_HWDEVICE_TYPE_DXVA2:
  500. {
  501. AVDXVA2FramesContext *dst_hwctx = dst_ctx->hwctx;
  502. dst_hwctx->surfaces = av_mallocz_array(src_hwctx->nb_surfaces,
  503. sizeof(*dst_hwctx->surfaces));
  504. if (!dst_hwctx->surfaces)
  505. return AVERROR(ENOMEM);
  506. for (i = 0; i < src_hwctx->nb_surfaces; i++)
  507. dst_hwctx->surfaces[i] =
  508. (IDirect3DSurface9*)src_hwctx->surfaces[i].Data.MemId;
  509. dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
  510. if (src_hwctx->frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
  511. dst_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
  512. else
  513. dst_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
  514. }
  515. break;
  516. #endif
  517. default:
  518. return AVERROR(ENOSYS);
  519. }
  520. return 0;
  521. }
  522. static int qsv_map_from(AVHWFramesContext *ctx,
  523. AVFrame *dst, const AVFrame *src, int flags)
  524. {
  525. QSVFramesContext *s = ctx->internal->priv;
  526. mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
  527. AVHWFramesContext *child_frames_ctx;
  528. const AVPixFmtDescriptor *desc;
  529. uint8_t *child_data;
  530. AVFrame *dummy;
  531. int ret = 0;
  532. if (!s->child_frames_ref)
  533. return AVERROR(ENOSYS);
  534. child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
  535. switch (child_frames_ctx->device_ctx->type) {
  536. #if CONFIG_VAAPI
  537. case AV_HWDEVICE_TYPE_VAAPI:
  538. child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)surf->Data.MemId;
  539. break;
  540. #endif
  541. #if CONFIG_DXVA2
  542. case AV_HWDEVICE_TYPE_DXVA2:
  543. child_data = surf->Data.MemId;
  544. break;
  545. #endif
  546. default:
  547. return AVERROR(ENOSYS);
  548. }
  549. if (dst->format == child_frames_ctx->format) {
  550. ret = ff_hwframe_map_create(s->child_frames_ref,
  551. dst, src, NULL, NULL);
  552. if (ret < 0)
  553. return ret;
  554. dst->width = src->width;
  555. dst->height = src->height;
  556. dst->data[3] = child_data;
  557. return 0;
  558. }
  559. desc = av_pix_fmt_desc_get(dst->format);
  560. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
  561. // This only supports mapping to software.
  562. return AVERROR(ENOSYS);
  563. }
  564. dummy = av_frame_alloc();
  565. if (!dummy)
  566. return AVERROR(ENOMEM);
  567. dummy->buf[0] = av_buffer_ref(src->buf[0]);
  568. dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
  569. if (!dummy->buf[0] || !dummy->hw_frames_ctx)
  570. goto fail;
  571. dummy->format = child_frames_ctx->format;
  572. dummy->width = src->width;
  573. dummy->height = src->height;
  574. dummy->data[3] = child_data;
  575. ret = av_hwframe_map(dst, dummy, flags);
  576. fail:
  577. av_frame_free(&dummy);
  578. return ret;
  579. }
  580. static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst,
  581. const AVFrame *src)
  582. {
  583. QSVFramesContext *s = ctx->internal->priv;
  584. AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
  585. int download = !!src->hw_frames_ctx;
  586. mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
  587. AVFrame *dummy;
  588. int ret;
  589. dummy = av_frame_alloc();
  590. if (!dummy)
  591. return AVERROR(ENOMEM);
  592. dummy->format = child_frames_ctx->format;
  593. dummy->width = src->width;
  594. dummy->height = src->height;
  595. dummy->buf[0] = download ? src->buf[0] : dst->buf[0];
  596. dummy->data[3] = surf->Data.MemId;
  597. dummy->hw_frames_ctx = s->child_frames_ref;
  598. ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
  599. av_hwframe_transfer_data(dummy, src, 0);
  600. dummy->buf[0] = NULL;
  601. dummy->data[3] = NULL;
  602. dummy->hw_frames_ctx = NULL;
  603. av_frame_free(&dummy);
  604. return ret;
  605. }
  606. static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
  607. const AVFrame *src)
  608. {
  609. QSVFramesContext *s = ctx->internal->priv;
  610. mfxFrameSurface1 out = {{ 0 }};
  611. mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
  612. mfxSyncPoint sync = NULL;
  613. mfxStatus err;
  614. if (!s->session_download) {
  615. if (s->child_frames_ref)
  616. return qsv_transfer_data_child(ctx, dst, src);
  617. av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
  618. return AVERROR(ENOSYS);
  619. }
  620. out.Info = in->Info;
  621. out.Data.PitchLow = dst->linesize[0];
  622. out.Data.Y = dst->data[0];
  623. out.Data.U = dst->data[1];
  624. out.Data.V = dst->data[2];
  625. out.Data.A = dst->data[3];
  626. do {
  627. err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
  628. if (err == MFX_WRN_DEVICE_BUSY)
  629. av_usleep(1);
  630. } while (err == MFX_WRN_DEVICE_BUSY);
  631. if (err < 0 || !sync) {
  632. av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
  633. return AVERROR_UNKNOWN;
  634. }
  635. do {
  636. err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
  637. } while (err == MFX_WRN_IN_EXECUTION);
  638. if (err < 0) {
  639. av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
  640. return AVERROR_UNKNOWN;
  641. }
  642. return 0;
  643. }
  644. static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
  645. const AVFrame *src)
  646. {
  647. QSVFramesContext *s = ctx->internal->priv;
  648. mfxFrameSurface1 in = {{ 0 }};
  649. mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
  650. mfxSyncPoint sync = NULL;
  651. mfxStatus err;
  652. if (!s->session_upload) {
  653. if (s->child_frames_ref)
  654. return qsv_transfer_data_child(ctx, dst, src);
  655. av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
  656. return AVERROR(ENOSYS);
  657. }
  658. in.Info = out->Info;
  659. in.Data.PitchLow = src->linesize[0];
  660. in.Data.Y = src->data[0];
  661. in.Data.U = src->data[1];
  662. in.Data.V = src->data[2];
  663. in.Data.A = src->data[3];
  664. do {
  665. err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
  666. if (err == MFX_WRN_DEVICE_BUSY)
  667. av_usleep(1);
  668. } while (err == MFX_WRN_DEVICE_BUSY);
  669. if (err < 0 || !sync) {
  670. av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
  671. return AVERROR_UNKNOWN;
  672. }
  673. do {
  674. err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
  675. } while (err == MFX_WRN_IN_EXECUTION);
  676. if (err < 0) {
  677. av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
  678. return AVERROR_UNKNOWN;
  679. }
  680. return 0;
  681. }
  682. static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx,
  683. AVHWFramesContext *src_ctx, int flags)
  684. {
  685. QSVFramesContext *s = dst_ctx->internal->priv;
  686. AVQSVFramesContext *dst_hwctx = dst_ctx->hwctx;
  687. int i;
  688. switch (src_ctx->device_ctx->type) {
  689. #if CONFIG_VAAPI
  690. case AV_HWDEVICE_TYPE_VAAPI:
  691. {
  692. AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
  693. s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
  694. sizeof(*s->surfaces_internal));
  695. if (!s->surfaces_internal)
  696. return AVERROR(ENOMEM);
  697. for (i = 0; i < src_hwctx->nb_surfaces; i++) {
  698. qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
  699. s->surfaces_internal[i].Data.MemId = src_hwctx->surface_ids + i;
  700. }
  701. dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
  702. dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  703. }
  704. break;
  705. #endif
  706. #if CONFIG_DXVA2
  707. case AV_HWDEVICE_TYPE_DXVA2:
  708. {
  709. AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
  710. s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
  711. sizeof(*s->surfaces_internal));
  712. if (!s->surfaces_internal)
  713. return AVERROR(ENOMEM);
  714. for (i = 0; i < src_hwctx->nb_surfaces; i++) {
  715. qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
  716. s->surfaces_internal[i].Data.MemId = (mfxMemId)src_hwctx->surfaces[i];
  717. }
  718. dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
  719. if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
  720. dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
  721. else
  722. dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  723. }
  724. break;
  725. #endif
  726. default:
  727. return AVERROR(ENOSYS);
  728. }
  729. dst_hwctx->surfaces = s->surfaces_internal;
  730. return 0;
  731. }
  732. static int qsv_map_to(AVHWFramesContext *dst_ctx,
  733. AVFrame *dst, const AVFrame *src, int flags)
  734. {
  735. AVQSVFramesContext *hwctx = dst_ctx->hwctx;
  736. int i, err;
  737. for (i = 0; i < hwctx->nb_surfaces; i++) {
  738. #if CONFIG_VAAPI
  739. if (*(VASurfaceID*)hwctx->surfaces[i].Data.MemId ==
  740. (VASurfaceID)(uintptr_t)src->data[3])
  741. break;
  742. #endif
  743. #if CONFIG_DXVA2
  744. if ((IDirect3DSurface9*)hwctx->surfaces[i].Data.MemId ==
  745. (IDirect3DSurface9*)(uintptr_t)src->data[3])
  746. break;
  747. #endif
  748. }
  749. if (i >= hwctx->nb_surfaces) {
  750. av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
  751. "is not in the mapped frames context.\n");
  752. return AVERROR(EINVAL);
  753. }
  754. err = ff_hwframe_map_create(dst->hw_frames_ctx,
  755. dst, src, NULL, NULL);
  756. if (err)
  757. return err;
  758. dst->width = src->width;
  759. dst->height = src->height;
  760. dst->data[3] = (uint8_t*)&hwctx->surfaces[i];
  761. return 0;
  762. }
  763. static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
  764. const void *hwconfig,
  765. AVHWFramesConstraints *constraints)
  766. {
  767. int i;
  768. constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
  769. sizeof(*constraints->valid_sw_formats));
  770. if (!constraints->valid_sw_formats)
  771. return AVERROR(ENOMEM);
  772. for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
  773. constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
  774. constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
  775. constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
  776. if (!constraints->valid_hw_formats)
  777. return AVERROR(ENOMEM);
  778. constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
  779. constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
  780. return 0;
  781. }
  782. static void qsv_device_free(AVHWDeviceContext *ctx)
  783. {
  784. AVQSVDeviceContext *hwctx = ctx->hwctx;
  785. QSVDevicePriv *priv = ctx->user_opaque;
  786. if (hwctx->session)
  787. MFXClose(hwctx->session);
  788. av_buffer_unref(&priv->child_device_ctx);
  789. av_freep(&priv);
  790. }
  791. static mfxIMPL choose_implementation(const char *device)
  792. {
  793. static const struct {
  794. const char *name;
  795. mfxIMPL impl;
  796. } impl_map[] = {
  797. { "auto", MFX_IMPL_AUTO },
  798. { "sw", MFX_IMPL_SOFTWARE },
  799. { "hw", MFX_IMPL_HARDWARE },
  800. { "auto_any", MFX_IMPL_AUTO_ANY },
  801. { "hw_any", MFX_IMPL_HARDWARE_ANY },
  802. { "hw2", MFX_IMPL_HARDWARE2 },
  803. { "hw3", MFX_IMPL_HARDWARE3 },
  804. { "hw4", MFX_IMPL_HARDWARE4 },
  805. };
  806. mfxIMPL impl = MFX_IMPL_AUTO_ANY;
  807. int i;
  808. if (device) {
  809. for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
  810. if (!strcmp(device, impl_map[i].name)) {
  811. impl = impl_map[i].impl;
  812. break;
  813. }
  814. if (i == FF_ARRAY_ELEMS(impl_map))
  815. impl = strtol(device, NULL, 0);
  816. }
  817. return impl;
  818. }
  819. static int qsv_device_derive_from_child(AVHWDeviceContext *ctx,
  820. mfxIMPL implementation,
  821. AVHWDeviceContext *child_device_ctx,
  822. int flags)
  823. {
  824. AVQSVDeviceContext *hwctx = ctx->hwctx;
  825. mfxVersion ver = { { 3, 1 } };
  826. mfxHDL handle;
  827. mfxHandleType handle_type;
  828. mfxStatus err;
  829. int ret;
  830. switch (child_device_ctx->type) {
  831. #if CONFIG_VAAPI
  832. case AV_HWDEVICE_TYPE_VAAPI:
  833. {
  834. AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  835. handle_type = MFX_HANDLE_VA_DISPLAY;
  836. handle = (mfxHDL)child_device_hwctx->display;
  837. }
  838. break;
  839. #endif
  840. #if CONFIG_DXVA2
  841. case AV_HWDEVICE_TYPE_DXVA2:
  842. {
  843. AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
  844. handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
  845. handle = (mfxHDL)child_device_hwctx->devmgr;
  846. }
  847. break;
  848. #endif
  849. default:
  850. ret = AVERROR(ENOSYS);
  851. goto fail;
  852. }
  853. err = MFXInit(implementation, &ver, &hwctx->session);
  854. if (err != MFX_ERR_NONE) {
  855. av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
  856. "%d.\n", err);
  857. ret = AVERROR_UNKNOWN;
  858. goto fail;
  859. }
  860. err = MFXQueryVersion(hwctx->session, &ver);
  861. if (err != MFX_ERR_NONE) {
  862. av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: %d.\n", err);
  863. ret = AVERROR_UNKNOWN;
  864. goto fail;
  865. }
  866. av_log(ctx, AV_LOG_VERBOSE,
  867. "Initialize MFX session: API version is %d.%d, implementation version is %d.%d\n",
  868. MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
  869. MFXClose(hwctx->session);
  870. err = MFXInit(implementation, &ver, &hwctx->session);
  871. if (err != MFX_ERR_NONE) {
  872. av_log(ctx, AV_LOG_ERROR,
  873. "Error initializing an MFX session: %d.\n", err);
  874. ret = AVERROR_UNKNOWN;
  875. goto fail;
  876. }
  877. err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
  878. if (err != MFX_ERR_NONE) {
  879. av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
  880. "%d\n", err);
  881. ret = AVERROR_UNKNOWN;
  882. goto fail;
  883. }
  884. ret = MFXQueryVersion(hwctx->session,&ver);
  885. if (ret == MFX_ERR_NONE) {
  886. av_log(ctx, AV_LOG_VERBOSE, "MFX compile/runtime API: %d.%d/%d.%d\n",
  887. MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
  888. }
  889. return 0;
  890. fail:
  891. if (hwctx->session)
  892. MFXClose(hwctx->session);
  893. return ret;
  894. }
  895. static int qsv_device_derive(AVHWDeviceContext *ctx,
  896. AVHWDeviceContext *child_device_ctx, int flags)
  897. {
  898. return qsv_device_derive_from_child(ctx, MFX_IMPL_HARDWARE_ANY,
  899. child_device_ctx, flags);
  900. }
  901. static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
  902. AVDictionary *opts, int flags)
  903. {
  904. QSVDevicePriv *priv;
  905. enum AVHWDeviceType child_device_type;
  906. AVHWDeviceContext *child_device;
  907. AVDictionaryEntry *e;
  908. mfxIMPL impl;
  909. int ret;
  910. priv = av_mallocz(sizeof(*priv));
  911. if (!priv)
  912. return AVERROR(ENOMEM);
  913. ctx->user_opaque = priv;
  914. ctx->free = qsv_device_free;
  915. e = av_dict_get(opts, "child_device", NULL, 0);
  916. if (CONFIG_VAAPI)
  917. child_device_type = AV_HWDEVICE_TYPE_VAAPI;
  918. else if (CONFIG_DXVA2)
  919. child_device_type = AV_HWDEVICE_TYPE_DXVA2;
  920. else {
  921. av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
  922. return AVERROR(ENOSYS);
  923. }
  924. ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
  925. e ? e->value : NULL, NULL, 0);
  926. if (ret < 0)
  927. return ret;
  928. child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
  929. impl = choose_implementation(device);
  930. return qsv_device_derive_from_child(ctx, impl, child_device, 0);
  931. }
  932. const HWContextType ff_hwcontext_type_qsv = {
  933. .type = AV_HWDEVICE_TYPE_QSV,
  934. .name = "QSV",
  935. .device_hwctx_size = sizeof(AVQSVDeviceContext),
  936. .device_priv_size = sizeof(QSVDeviceContext),
  937. .frames_hwctx_size = sizeof(AVQSVFramesContext),
  938. .frames_priv_size = sizeof(QSVFramesContext),
  939. .device_create = qsv_device_create,
  940. .device_derive = qsv_device_derive,
  941. .device_init = qsv_device_init,
  942. .frames_get_constraints = qsv_frames_get_constraints,
  943. .frames_init = qsv_frames_init,
  944. .frames_uninit = qsv_frames_uninit,
  945. .frames_get_buffer = qsv_get_buffer,
  946. .transfer_get_formats = qsv_transfer_get_formats,
  947. .transfer_data_to = qsv_transfer_data_to,
  948. .transfer_data_from = qsv_transfer_data_from,
  949. .map_to = qsv_map_to,
  950. .map_from = qsv_map_from,
  951. .frames_derive_to = qsv_frames_derive_to,
  952. .frames_derive_from = qsv_frames_derive_from,
  953. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },
  954. };