2 * This file is part of FFmpeg.
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.
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.
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
22 #include <mfx/mfxvideo.h>
27 #include "hwcontext_vaapi.h"
30 #include "hwcontext_dxva2.h"
35 #include "hwcontext.h"
36 #include "hwcontext_internal.h"
37 #include "hwcontext_qsv.h"
43 typedef struct QSVDevicePriv {
44 AVBufferRef *child_device_ctx;
47 typedef struct QSVDeviceContext {
49 mfxHandleType handle_type;
53 enum AVHWDeviceType child_device_type;
54 enum AVPixelFormat child_pix_fmt;
57 typedef struct QSVFramesContext {
58 mfxSession session_download;
59 mfxSession session_upload;
61 AVBufferRef *child_frames_ref;
62 mfxFrameSurface1 *surfaces_internal;
65 // used in the frame allocator for non-opaque surfaces
67 // used in the opaque alloc request for opaque surfaces
68 mfxFrameSurface1 **surface_ptrs;
70 mfxExtOpaqueSurfaceAlloc opaque_alloc;
71 mfxExtBuffer *ext_buffers[1];
75 mfxHandleType handle_type;
76 enum AVHWDeviceType device_type;
77 enum AVPixelFormat pix_fmt;
78 } supported_handle_types[] = {
80 { MFX_HANDLE_VA_DISPLAY, AV_HWDEVICE_TYPE_VAAPI, AV_PIX_FMT_VAAPI },
83 { MFX_HANDLE_D3D9_DEVICE_MANAGER, AV_HWDEVICE_TYPE_DXVA2, AV_PIX_FMT_DXVA2_VLD },
89 enum AVPixelFormat pix_fmt;
91 } supported_pixel_formats[] = {
92 { AV_PIX_FMT_NV12, MFX_FOURCC_NV12 },
93 { AV_PIX_FMT_P010, MFX_FOURCC_P010 },
96 static int qsv_device_init(AVHWDeviceContext *ctx)
98 AVQSVDeviceContext *hwctx = ctx->hwctx;
99 QSVDeviceContext *s = ctx->internal->priv;
104 for (i = 0; supported_handle_types[i].handle_type; i++) {
105 err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
107 if (err == MFX_ERR_NONE) {
108 s->handle_type = supported_handle_types[i].handle_type;
109 s->child_device_type = supported_handle_types[i].device_type;
110 s->child_pix_fmt = supported_handle_types[i].pix_fmt;
115 av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
116 "from the session\n");
119 err = MFXQueryIMPL(hwctx->session, &s->impl);
120 if (err == MFX_ERR_NONE)
121 err = MFXQueryVersion(hwctx->session, &s->ver);
122 if (err != MFX_ERR_NONE) {
123 av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
124 return AVERROR_UNKNOWN;
130 static void qsv_frames_uninit(AVHWFramesContext *ctx)
132 QSVFramesContext *s = ctx->internal->priv;
134 if (s->session_download) {
135 MFXVideoVPP_Close(s->session_download);
136 MFXClose(s->session_download);
138 s->session_download = NULL;
140 if (s->session_upload) {
141 MFXVideoVPP_Close(s->session_upload);
142 MFXClose(s->session_upload);
144 s->session_upload = NULL;
146 av_freep(&s->mem_ids);
147 av_freep(&s->surface_ptrs);
148 av_freep(&s->surfaces_internal);
149 av_buffer_unref(&s->child_frames_ref);
152 static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
156 static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
158 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
159 QSVFramesContext *s = ctx->internal->priv;
160 AVQSVFramesContext *hwctx = ctx->hwctx;
162 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
163 s->nb_surfaces_used++;
164 return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
165 sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
171 static int qsv_init_child_ctx(AVHWFramesContext *ctx)
173 AVQSVFramesContext *hwctx = ctx->hwctx;
174 QSVFramesContext *s = ctx->internal->priv;
175 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
177 AVBufferRef *child_device_ref = NULL;
178 AVBufferRef *child_frames_ref = NULL;
180 AVHWDeviceContext *child_device_ctx;
181 AVHWFramesContext *child_frames_ctx;
185 if (!device_priv->handle) {
186 av_log(ctx, AV_LOG_ERROR,
187 "Cannot create a non-opaque internal surface pool without "
188 "a hardware handle\n");
189 return AVERROR(EINVAL);
192 child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
193 if (!child_device_ref)
194 return AVERROR(ENOMEM);
195 child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
198 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
199 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
200 child_device_hwctx->display = (VADisplay)device_priv->handle;
204 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
205 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
206 child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
210 ret = av_hwdevice_ctx_init(child_device_ref);
212 av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
216 child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
217 if (!child_frames_ref) {
218 ret = AVERROR(ENOMEM);
221 child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
223 child_frames_ctx->format = device_priv->child_pix_fmt;
224 child_frames_ctx->sw_format = ctx->sw_format;
225 child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
226 child_frames_ctx->width = ctx->width;
227 child_frames_ctx->height = ctx->height;
230 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
231 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
232 if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
233 child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
235 child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
239 ret = av_hwframe_ctx_init(child_frames_ref);
241 av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
246 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
247 AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
248 for (i = 0; i < ctx->initial_pool_size; i++)
249 s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
250 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
254 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
255 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
256 for (i = 0; i < ctx->initial_pool_size; i++)
257 s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
258 if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
259 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
261 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
265 s->child_frames_ref = child_frames_ref;
266 child_frames_ref = NULL;
269 av_buffer_unref(&child_device_ref);
270 av_buffer_unref(&child_frames_ref);
274 static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
276 QSVFramesContext *s = ctx->internal->priv;
277 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
278 const AVPixFmtDescriptor *desc;
282 desc = av_pix_fmt_desc_get(ctx->sw_format);
286 if (ctx->initial_pool_size <= 0) {
287 av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
288 return AVERROR(EINVAL);
291 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
292 sizeof(*s->surfaces_internal));
293 if (!s->surfaces_internal)
294 return AVERROR(ENOMEM);
296 for (i = 0; i < ctx->initial_pool_size; i++) {
297 mfxFrameSurface1 *surf = &s->surfaces_internal[i];
299 surf->Info.BitDepthLuma = desc->comp[0].depth;
300 surf->Info.BitDepthChroma = desc->comp[0].depth;
301 surf->Info.Shift = desc->comp[0].depth > 8;
303 if (desc->log2_chroma_w && desc->log2_chroma_h)
304 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
305 else if (desc->log2_chroma_w)
306 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
308 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
310 surf->Info.FourCC = fourcc;
311 surf->Info.Width = ctx->width;
312 surf->Info.CropW = ctx->width;
313 surf->Info.Height = ctx->height;
314 surf->Info.CropH = ctx->height;
315 surf->Info.FrameRateExtN = 25;
316 surf->Info.FrameRateExtD = 1;
319 if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
320 ret = qsv_init_child_ctx(ctx);
325 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
326 ctx, qsv_pool_alloc, NULL);
327 if (!ctx->internal->pool_internal)
328 return AVERROR(ENOMEM);
330 frames_hwctx->surfaces = s->surfaces_internal;
331 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
336 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
337 mfxFrameAllocResponse *resp)
339 AVHWFramesContext *ctx = pthis;
340 QSVFramesContext *s = ctx->internal->priv;
341 AVQSVFramesContext *hwctx = ctx->hwctx;
342 mfxFrameInfo *i = &req->Info;
343 mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
345 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
346 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
347 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
348 return MFX_ERR_UNSUPPORTED;
349 if (i->Width != i1->Width || i->Height != i1->Height ||
350 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
351 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
352 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
353 i->Width, i->Height, i->FourCC, i->ChromaFormat,
354 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
355 return MFX_ERR_UNSUPPORTED;
358 resp->mids = s->mem_ids;
359 resp->NumFrameActual = hwctx->nb_surfaces;
364 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
369 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
371 return MFX_ERR_UNSUPPORTED;
374 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
376 return MFX_ERR_UNSUPPORTED;
379 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
385 static int qsv_init_internal_session(AVHWFramesContext *ctx,
386 mfxSession *session, int upload)
388 QSVFramesContext *s = ctx->internal->priv;
389 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
390 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
391 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
393 mfxFrameAllocator frame_allocator = {
395 .Alloc = frame_alloc,
397 .Unlock = frame_unlock,
398 .GetHDL = frame_get_hdl,
405 err = MFXInit(device_priv->impl, &device_priv->ver, session);
406 if (err != MFX_ERR_NONE) {
407 av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
408 return AVERROR_UNKNOWN;
411 if (device_priv->handle) {
412 err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
413 device_priv->handle);
414 if (err != MFX_ERR_NONE)
415 return AVERROR_UNKNOWN;
419 err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
420 if (err != MFX_ERR_NONE)
421 return AVERROR_UNKNOWN;
424 memset(&par, 0, sizeof(par));
427 par.ExtParam = s->ext_buffers;
428 par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
429 par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
430 MFX_IOPATTERN_IN_OPAQUE_MEMORY;
432 par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
433 MFX_IOPATTERN_IN_VIDEO_MEMORY;
436 par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
437 MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
440 par.vpp.In = frames_hwctx->surfaces[0].Info;
442 /* Apparently VPP requires the frame rate to be set to some value, otherwise
443 * init will fail (probably for the framerate conversion filter). Since we
444 * are only doing data upload/download here, we just invent an arbitrary
446 par.vpp.In.FrameRateExtN = 25;
447 par.vpp.In.FrameRateExtD = 1;
448 par.vpp.Out = par.vpp.In;
450 err = MFXVideoVPP_Init(*session, &par);
451 if (err != MFX_ERR_NONE) {
452 av_log(ctx, AV_LOG_ERROR, "Error opening the internal VPP session\n");
453 return AVERROR_UNKNOWN;
459 static int qsv_frames_init(AVHWFramesContext *ctx)
461 QSVFramesContext *s = ctx->internal->priv;
462 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
464 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
469 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
470 if (supported_pixel_formats[i].pix_fmt == ctx->sw_format) {
471 fourcc = supported_pixel_formats[i].fourcc;
476 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
477 return AVERROR(ENOSYS);
481 ret = qsv_init_pool(ctx, fourcc);
483 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
489 s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
490 sizeof(*s->surface_ptrs));
491 if (!s->surface_ptrs)
492 return AVERROR(ENOMEM);
494 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
495 s->surface_ptrs[i] = frames_hwctx->surfaces + i;
497 s->opaque_alloc.In.Surfaces = s->surface_ptrs;
498 s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
499 s->opaque_alloc.In.Type = frames_hwctx->frame_type;
501 s->opaque_alloc.Out = s->opaque_alloc.In;
503 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
504 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
506 s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
508 s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
510 return AVERROR(ENOMEM);
512 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
513 s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
516 ret = qsv_init_internal_session(ctx, &s->session_download, 0);
520 ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
527 static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
529 frame->buf[0] = av_buffer_pool_get(ctx->pool);
531 return AVERROR(ENOMEM);
533 frame->data[3] = frame->buf[0]->data;
534 frame->format = AV_PIX_FMT_QSV;
535 frame->width = ctx->width;
536 frame->height = ctx->height;
541 static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
542 enum AVHWFrameTransferDirection dir,
543 enum AVPixelFormat **formats)
545 enum AVPixelFormat *fmts;
547 fmts = av_malloc_array(2, sizeof(*fmts));
549 return AVERROR(ENOMEM);
551 fmts[0] = ctx->sw_format;
552 fmts[1] = AV_PIX_FMT_NONE;
559 static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
562 QSVFramesContext *s = ctx->internal->priv;
563 mfxFrameSurface1 out = {{ 0 }};
564 mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
566 mfxSyncPoint sync = NULL;
570 out.Data.PitchLow = dst->linesize[0];
571 out.Data.Y = dst->data[0];
572 out.Data.U = dst->data[1];
573 out.Data.V = dst->data[2];
574 out.Data.A = dst->data[3];
577 err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
578 if (err == MFX_WRN_DEVICE_BUSY)
580 } while (err == MFX_WRN_DEVICE_BUSY);
582 if (err < 0 || !sync) {
583 av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
584 return AVERROR_UNKNOWN;
588 err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
589 } while (err == MFX_WRN_IN_EXECUTION);
591 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
592 return AVERROR_UNKNOWN;
598 static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
601 QSVFramesContext *s = ctx->internal->priv;
602 mfxFrameSurface1 in = {{ 0 }};
603 mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
605 mfxSyncPoint sync = NULL;
609 in.Data.PitchLow = src->linesize[0];
610 in.Data.Y = src->data[0];
611 in.Data.U = src->data[1];
612 in.Data.V = src->data[2];
613 in.Data.A = src->data[3];
616 err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
617 if (err == MFX_WRN_DEVICE_BUSY)
619 } while (err == MFX_WRN_DEVICE_BUSY);
621 if (err < 0 || !sync) {
622 av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
623 return AVERROR_UNKNOWN;
627 err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
628 } while (err == MFX_WRN_IN_EXECUTION);
630 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
631 return AVERROR_UNKNOWN;
637 static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
638 const void *hwconfig,
639 AVHWFramesConstraints *constraints)
643 constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
644 sizeof(*constraints->valid_sw_formats));
645 if (!constraints->valid_sw_formats)
646 return AVERROR(ENOMEM);
648 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
649 constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
650 constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
652 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
653 if (!constraints->valid_hw_formats)
654 return AVERROR(ENOMEM);
656 constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
657 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
662 static void qsv_device_free(AVHWDeviceContext *ctx)
664 AVQSVDeviceContext *hwctx = ctx->hwctx;
665 QSVDevicePriv *priv = ctx->user_opaque;
668 MFXClose(hwctx->session);
670 av_buffer_unref(&priv->child_device_ctx);
674 static mfxIMPL choose_implementation(const char *device)
676 static const struct {
680 { "auto", MFX_IMPL_AUTO },
681 { "sw", MFX_IMPL_SOFTWARE },
682 { "hw", MFX_IMPL_HARDWARE },
683 { "auto_any", MFX_IMPL_AUTO_ANY },
684 { "hw_any", MFX_IMPL_HARDWARE_ANY },
685 { "hw2", MFX_IMPL_HARDWARE2 },
686 { "hw3", MFX_IMPL_HARDWARE3 },
687 { "hw4", MFX_IMPL_HARDWARE4 },
690 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
694 for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
695 if (!strcmp(device, impl_map[i].name)) {
696 impl = impl_map[i].impl;
699 if (i == FF_ARRAY_ELEMS(impl_map))
700 impl = strtol(device, NULL, 0);
706 static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
707 AVDictionary *opts, int flags)
709 AVQSVDeviceContext *hwctx = ctx->hwctx;
711 enum AVHWDeviceType child_device_type;
712 AVDictionaryEntry *e;
714 mfxVersion ver = { { 3, 1 } };
717 mfxHandleType handle_type;
721 priv = av_mallocz(sizeof(*priv));
723 return AVERROR(ENOMEM);
725 ctx->user_opaque = priv;
726 ctx->free = qsv_device_free;
728 e = av_dict_get(opts, "child_device", NULL, 0);
731 child_device_type = AV_HWDEVICE_TYPE_VAAPI;
732 else if (CONFIG_DXVA2)
733 child_device_type = AV_HWDEVICE_TYPE_DXVA2;
735 av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
736 return AVERROR(ENOSYS);
739 ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
740 e ? e->value : NULL, NULL, 0);
745 AVHWDeviceContext *child_device_ctx = (AVHWDeviceContext*)priv->child_device_ctx->data;
747 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
748 handle_type = MFX_HANDLE_VA_DISPLAY;
749 handle = (mfxHDL)child_device_hwctx->display;
751 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
752 handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
753 handle = (mfxHDL)child_device_hwctx->devmgr;
757 impl = choose_implementation(device);
759 err = MFXInit(impl, &ver, &hwctx->session);
760 if (err != MFX_ERR_NONE) {
761 av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
762 return AVERROR_UNKNOWN;
765 err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
766 if (err != MFX_ERR_NONE)
767 return AVERROR_UNKNOWN;
772 const HWContextType ff_hwcontext_type_qsv = {
773 .type = AV_HWDEVICE_TYPE_QSV,
776 .device_hwctx_size = sizeof(AVQSVDeviceContext),
777 .device_priv_size = sizeof(QSVDeviceContext),
778 .frames_hwctx_size = sizeof(AVQSVFramesContext),
779 .frames_priv_size = sizeof(QSVFramesContext),
781 .device_create = qsv_device_create,
782 .device_init = qsv_device_init,
783 .frames_get_constraints = qsv_frames_get_constraints,
784 .frames_init = qsv_frames_init,
785 .frames_uninit = qsv_frames_uninit,
786 .frames_get_buffer = qsv_get_buffer,
787 .transfer_get_formats = qsv_transfer_get_formats,
788 .transfer_data_to = qsv_transfer_data_to,
789 .transfer_data_from = qsv_transfer_data_from,
791 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },