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 },
94 { AV_PIX_FMT_PAL8, MFX_FOURCC_P8 },
97 static uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
100 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
101 if (supported_pixel_formats[i].pix_fmt == pix_fmt)
102 return supported_pixel_formats[i].fourcc;
107 static int qsv_device_init(AVHWDeviceContext *ctx)
109 AVQSVDeviceContext *hwctx = ctx->hwctx;
110 QSVDeviceContext *s = ctx->internal->priv;
115 for (i = 0; supported_handle_types[i].handle_type; i++) {
116 err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
118 if (err == MFX_ERR_NONE) {
119 s->handle_type = supported_handle_types[i].handle_type;
120 s->child_device_type = supported_handle_types[i].device_type;
121 s->child_pix_fmt = supported_handle_types[i].pix_fmt;
126 av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
127 "from the session\n");
130 err = MFXQueryIMPL(hwctx->session, &s->impl);
131 if (err == MFX_ERR_NONE)
132 err = MFXQueryVersion(hwctx->session, &s->ver);
133 if (err != MFX_ERR_NONE) {
134 av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
135 return AVERROR_UNKNOWN;
141 static void qsv_frames_uninit(AVHWFramesContext *ctx)
143 QSVFramesContext *s = ctx->internal->priv;
145 if (s->session_download) {
146 MFXVideoVPP_Close(s->session_download);
147 MFXClose(s->session_download);
149 s->session_download = NULL;
151 if (s->session_upload) {
152 MFXVideoVPP_Close(s->session_upload);
153 MFXClose(s->session_upload);
155 s->session_upload = NULL;
157 av_freep(&s->mem_ids);
158 av_freep(&s->surface_ptrs);
159 av_freep(&s->surfaces_internal);
160 av_buffer_unref(&s->child_frames_ref);
163 static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
167 static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
169 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
170 QSVFramesContext *s = ctx->internal->priv;
171 AVQSVFramesContext *hwctx = ctx->hwctx;
173 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
174 s->nb_surfaces_used++;
175 return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
176 sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
182 static int qsv_init_child_ctx(AVHWFramesContext *ctx)
184 AVQSVFramesContext *hwctx = ctx->hwctx;
185 QSVFramesContext *s = ctx->internal->priv;
186 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
188 AVBufferRef *child_device_ref = NULL;
189 AVBufferRef *child_frames_ref = NULL;
191 AVHWDeviceContext *child_device_ctx;
192 AVHWFramesContext *child_frames_ctx;
196 if (!device_priv->handle) {
197 av_log(ctx, AV_LOG_ERROR,
198 "Cannot create a non-opaque internal surface pool without "
199 "a hardware handle\n");
200 return AVERROR(EINVAL);
203 child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
204 if (!child_device_ref)
205 return AVERROR(ENOMEM);
206 child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
209 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
210 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
211 child_device_hwctx->display = (VADisplay)device_priv->handle;
215 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
216 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
217 child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
221 ret = av_hwdevice_ctx_init(child_device_ref);
223 av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
227 child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
228 if (!child_frames_ref) {
229 ret = AVERROR(ENOMEM);
232 child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
234 child_frames_ctx->format = device_priv->child_pix_fmt;
235 child_frames_ctx->sw_format = ctx->sw_format;
236 child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
237 child_frames_ctx->width = ctx->width;
238 child_frames_ctx->height = ctx->height;
241 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
242 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
243 if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
244 child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
246 child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
250 ret = av_hwframe_ctx_init(child_frames_ref);
252 av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
257 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
258 AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
259 for (i = 0; i < ctx->initial_pool_size; i++)
260 s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
261 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
265 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
266 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
267 for (i = 0; i < ctx->initial_pool_size; i++)
268 s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
269 if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
270 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
272 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
276 s->child_frames_ref = child_frames_ref;
277 child_frames_ref = NULL;
280 av_buffer_unref(&child_device_ref);
281 av_buffer_unref(&child_frames_ref);
285 static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
287 const AVPixFmtDescriptor *desc;
290 desc = av_pix_fmt_desc_get(ctx->sw_format);
292 return AVERROR(EINVAL);
294 fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
296 return AVERROR(EINVAL);
298 surf->Info.BitDepthLuma = desc->comp[0].depth;
299 surf->Info.BitDepthChroma = desc->comp[0].depth;
300 surf->Info.Shift = desc->comp[0].depth > 8;
302 if (desc->log2_chroma_w && desc->log2_chroma_h)
303 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
304 else if (desc->log2_chroma_w)
305 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
307 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
309 surf->Info.FourCC = fourcc;
310 surf->Info.Width = ctx->width;
311 surf->Info.CropW = ctx->width;
312 surf->Info.Height = ctx->height;
313 surf->Info.CropH = ctx->height;
314 surf->Info.FrameRateExtN = 25;
315 surf->Info.FrameRateExtD = 1;
320 static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
322 QSVFramesContext *s = ctx->internal->priv;
323 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
327 if (ctx->initial_pool_size <= 0) {
328 av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
329 return AVERROR(EINVAL);
332 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
333 sizeof(*s->surfaces_internal));
334 if (!s->surfaces_internal)
335 return AVERROR(ENOMEM);
337 for (i = 0; i < ctx->initial_pool_size; i++) {
338 ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
343 if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
344 ret = qsv_init_child_ctx(ctx);
349 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
350 ctx, qsv_pool_alloc, NULL);
351 if (!ctx->internal->pool_internal)
352 return AVERROR(ENOMEM);
354 frames_hwctx->surfaces = s->surfaces_internal;
355 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
360 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
361 mfxFrameAllocResponse *resp)
363 AVHWFramesContext *ctx = pthis;
364 QSVFramesContext *s = ctx->internal->priv;
365 AVQSVFramesContext *hwctx = ctx->hwctx;
366 mfxFrameInfo *i = &req->Info;
367 mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
369 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
370 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
371 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
372 return MFX_ERR_UNSUPPORTED;
373 if (i->Width != i1->Width || i->Height != i1->Height ||
374 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
375 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
376 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
377 i->Width, i->Height, i->FourCC, i->ChromaFormat,
378 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
379 return MFX_ERR_UNSUPPORTED;
382 resp->mids = s->mem_ids;
383 resp->NumFrameActual = hwctx->nb_surfaces;
388 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
393 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
395 return MFX_ERR_UNSUPPORTED;
398 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
400 return MFX_ERR_UNSUPPORTED;
403 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
409 static int qsv_init_internal_session(AVHWFramesContext *ctx,
410 mfxSession *session, int upload)
412 QSVFramesContext *s = ctx->internal->priv;
413 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
414 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
415 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
417 mfxFrameAllocator frame_allocator = {
419 .Alloc = frame_alloc,
421 .Unlock = frame_unlock,
422 .GetHDL = frame_get_hdl,
429 err = MFXInit(device_priv->impl, &device_priv->ver, session);
430 if (err != MFX_ERR_NONE) {
431 av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
432 return AVERROR_UNKNOWN;
435 if (device_priv->handle) {
436 err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
437 device_priv->handle);
438 if (err != MFX_ERR_NONE)
439 return AVERROR_UNKNOWN;
443 err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
444 if (err != MFX_ERR_NONE)
445 return AVERROR_UNKNOWN;
448 memset(&par, 0, sizeof(par));
451 par.ExtParam = s->ext_buffers;
452 par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
453 par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
454 MFX_IOPATTERN_IN_OPAQUE_MEMORY;
456 par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
457 MFX_IOPATTERN_IN_VIDEO_MEMORY;
460 par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
461 MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
464 par.vpp.In = frames_hwctx->surfaces[0].Info;
466 /* Apparently VPP requires the frame rate to be set to some value, otherwise
467 * init will fail (probably for the framerate conversion filter). Since we
468 * are only doing data upload/download here, we just invent an arbitrary
470 par.vpp.In.FrameRateExtN = 25;
471 par.vpp.In.FrameRateExtD = 1;
472 par.vpp.Out = par.vpp.In;
474 err = MFXVideoVPP_Init(*session, &par);
475 if (err != MFX_ERR_NONE) {
476 av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
477 "Surface upload/download will not be possible\n");
485 static int qsv_frames_init(AVHWFramesContext *ctx)
487 QSVFramesContext *s = ctx->internal->priv;
488 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
490 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
495 fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
497 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
498 return AVERROR(ENOSYS);
502 ret = qsv_init_pool(ctx, fourcc);
504 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
510 s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
511 sizeof(*s->surface_ptrs));
512 if (!s->surface_ptrs)
513 return AVERROR(ENOMEM);
515 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
516 s->surface_ptrs[i] = frames_hwctx->surfaces + i;
518 s->opaque_alloc.In.Surfaces = s->surface_ptrs;
519 s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
520 s->opaque_alloc.In.Type = frames_hwctx->frame_type;
522 s->opaque_alloc.Out = s->opaque_alloc.In;
524 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
525 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
527 s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
529 s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
531 return AVERROR(ENOMEM);
533 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
534 s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
537 ret = qsv_init_internal_session(ctx, &s->session_download, 0);
541 ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
548 static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
550 frame->buf[0] = av_buffer_pool_get(ctx->pool);
552 return AVERROR(ENOMEM);
554 frame->data[3] = frame->buf[0]->data;
555 frame->format = AV_PIX_FMT_QSV;
556 frame->width = ctx->width;
557 frame->height = ctx->height;
562 static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
563 enum AVHWFrameTransferDirection dir,
564 enum AVPixelFormat **formats)
566 enum AVPixelFormat *fmts;
568 fmts = av_malloc_array(2, sizeof(*fmts));
570 return AVERROR(ENOMEM);
572 fmts[0] = ctx->sw_format;
573 fmts[1] = AV_PIX_FMT_NONE;
580 static int qsv_frames_derive_from(AVHWFramesContext *dst_ctx,
581 AVHWFramesContext *src_ctx, int flags)
583 AVQSVFramesContext *src_hwctx = src_ctx->hwctx;
586 switch (dst_ctx->device_ctx->type) {
588 case AV_HWDEVICE_TYPE_VAAPI:
590 AVVAAPIFramesContext *dst_hwctx = dst_ctx->hwctx;
591 dst_hwctx->surface_ids = av_mallocz_array(src_hwctx->nb_surfaces,
592 sizeof(*dst_hwctx->surface_ids));
593 if (!dst_hwctx->surface_ids)
594 return AVERROR(ENOMEM);
595 for (i = 0; i < src_hwctx->nb_surfaces; i++)
596 dst_hwctx->surface_ids[i] =
597 *(VASurfaceID*)src_hwctx->surfaces[i].Data.MemId;
598 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
603 case AV_HWDEVICE_TYPE_DXVA2:
605 AVDXVA2FramesContext *dst_hwctx = dst_ctx->hwctx;
606 dst_hwctx->surfaces = av_mallocz_array(src_hwctx->nb_surfaces,
607 sizeof(*dst_hwctx->surfaces));
608 if (!dst_hwctx->surfaces)
609 return AVERROR(ENOMEM);
610 for (i = 0; i < src_hwctx->nb_surfaces; i++)
611 dst_hwctx->surfaces[i] =
612 (IDirect3DSurface9*)src_hwctx->surfaces[i].Data.MemId;
613 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
614 if (src_hwctx->frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
615 dst_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
617 dst_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
622 return AVERROR(ENOSYS);
628 static int qsv_map_from(AVHWFramesContext *ctx,
629 AVFrame *dst, const AVFrame *src, int flags)
631 QSVFramesContext *s = ctx->internal->priv;
632 mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
633 AVHWFramesContext *child_frames_ctx;
634 const AVPixFmtDescriptor *desc;
639 if (!s->child_frames_ref)
640 return AVERROR(ENOSYS);
641 child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
643 switch (child_frames_ctx->device_ctx->type) {
645 case AV_HWDEVICE_TYPE_VAAPI:
646 child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)surf->Data.MemId;
650 case AV_HWDEVICE_TYPE_DXVA2:
651 child_data = surf->Data.MemId;
655 return AVERROR(ENOSYS);
658 if (dst->format == child_frames_ctx->format) {
659 ret = ff_hwframe_map_create(s->child_frames_ref,
660 dst, src, NULL, NULL);
664 dst->width = src->width;
665 dst->height = src->height;
666 dst->data[3] = child_data;
671 desc = av_pix_fmt_desc_get(dst->format);
672 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
673 // This only supports mapping to software.
674 return AVERROR(ENOSYS);
677 dummy = av_frame_alloc();
679 return AVERROR(ENOMEM);
681 dummy->buf[0] = av_buffer_ref(src->buf[0]);
682 dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
683 if (!dummy->buf[0] || !dummy->hw_frames_ctx)
686 dummy->format = child_frames_ctx->format;
687 dummy->width = src->width;
688 dummy->height = src->height;
689 dummy->data[3] = child_data;
691 ret = av_hwframe_map(dst, dummy, flags);
694 av_frame_free(&dummy);
699 static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst,
702 QSVFramesContext *s = ctx->internal->priv;
703 AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
704 int download = !!src->hw_frames_ctx;
705 mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
710 dummy = av_frame_alloc();
712 return AVERROR(ENOMEM);
714 dummy->format = child_frames_ctx->format;
715 dummy->width = src->width;
716 dummy->height = src->height;
717 dummy->buf[0] = download ? src->buf[0] : dst->buf[0];
718 dummy->data[3] = surf->Data.MemId;
719 dummy->hw_frames_ctx = s->child_frames_ref;
721 ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
722 av_hwframe_transfer_data(dummy, src, 0);
724 dummy->buf[0] = NULL;
725 dummy->data[3] = NULL;
726 dummy->hw_frames_ctx = NULL;
728 av_frame_free(&dummy);
733 static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
736 QSVFramesContext *s = ctx->internal->priv;
737 mfxFrameSurface1 out = {{ 0 }};
738 mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
740 mfxSyncPoint sync = NULL;
743 if (!s->session_download) {
744 if (s->child_frames_ref)
745 return qsv_transfer_data_child(ctx, dst, src);
747 av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
748 return AVERROR(ENOSYS);
752 out.Data.PitchLow = dst->linesize[0];
753 out.Data.Y = dst->data[0];
754 out.Data.U = dst->data[1];
755 out.Data.V = dst->data[2];
756 out.Data.A = dst->data[3];
759 err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
760 if (err == MFX_WRN_DEVICE_BUSY)
762 } while (err == MFX_WRN_DEVICE_BUSY);
764 if (err < 0 || !sync) {
765 av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
766 return AVERROR_UNKNOWN;
770 err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
771 } while (err == MFX_WRN_IN_EXECUTION);
773 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
774 return AVERROR_UNKNOWN;
780 static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
783 QSVFramesContext *s = ctx->internal->priv;
784 mfxFrameSurface1 in = {{ 0 }};
785 mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
787 mfxSyncPoint sync = NULL;
790 if (!s->session_upload) {
791 if (s->child_frames_ref)
792 return qsv_transfer_data_child(ctx, dst, src);
794 av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
795 return AVERROR(ENOSYS);
799 in.Data.PitchLow = src->linesize[0];
800 in.Data.Y = src->data[0];
801 in.Data.U = src->data[1];
802 in.Data.V = src->data[2];
803 in.Data.A = src->data[3];
806 err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
807 if (err == MFX_WRN_DEVICE_BUSY)
809 } while (err == MFX_WRN_DEVICE_BUSY);
811 if (err < 0 || !sync) {
812 av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
813 return AVERROR_UNKNOWN;
817 err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
818 } while (err == MFX_WRN_IN_EXECUTION);
820 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
821 return AVERROR_UNKNOWN;
827 static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx,
828 AVHWFramesContext *src_ctx, int flags)
830 QSVFramesContext *s = dst_ctx->internal->priv;
831 AVQSVFramesContext *dst_hwctx = dst_ctx->hwctx;
834 switch (src_ctx->device_ctx->type) {
836 case AV_HWDEVICE_TYPE_VAAPI:
838 AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
839 s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
840 sizeof(*s->surfaces_internal));
841 if (!s->surfaces_internal)
842 return AVERROR(ENOMEM);
843 for (i = 0; i < src_hwctx->nb_surfaces; i++) {
844 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
845 s->surfaces_internal[i].Data.MemId = src_hwctx->surface_ids + i;
847 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
848 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
853 case AV_HWDEVICE_TYPE_DXVA2:
855 AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
856 s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
857 sizeof(*s->surfaces_internal));
858 if (!s->surfaces_internal)
859 return AVERROR(ENOMEM);
860 for (i = 0; i < src_hwctx->nb_surfaces; i++) {
861 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
862 s->surfaces_internal[i].Data.MemId = (mfxMemId)src_hwctx->surfaces[i];
864 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
865 if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
866 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
868 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
873 return AVERROR(ENOSYS);
876 dst_hwctx->surfaces = s->surfaces_internal;
881 static int qsv_map_to(AVHWFramesContext *dst_ctx,
882 AVFrame *dst, const AVFrame *src, int flags)
884 AVQSVFramesContext *hwctx = dst_ctx->hwctx;
887 for (i = 0; i < hwctx->nb_surfaces; i++) {
889 if (*(VASurfaceID*)hwctx->surfaces[i].Data.MemId ==
890 (VASurfaceID)(uintptr_t)src->data[3])
894 if ((IDirect3DSurface9*)hwctx->surfaces[i].Data.MemId ==
895 (IDirect3DSurface9*)(uintptr_t)src->data[3])
899 if (i >= hwctx->nb_surfaces) {
900 av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
901 "is not in the mapped frames context.\n");
902 return AVERROR(EINVAL);
905 err = ff_hwframe_map_create(dst->hw_frames_ctx,
906 dst, src, NULL, NULL);
910 dst->width = src->width;
911 dst->height = src->height;
912 dst->data[3] = (uint8_t*)&hwctx->surfaces[i];
917 static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
918 const void *hwconfig,
919 AVHWFramesConstraints *constraints)
923 constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
924 sizeof(*constraints->valid_sw_formats));
925 if (!constraints->valid_sw_formats)
926 return AVERROR(ENOMEM);
928 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
929 constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
930 constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
932 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
933 if (!constraints->valid_hw_formats)
934 return AVERROR(ENOMEM);
936 constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
937 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
942 static void qsv_device_free(AVHWDeviceContext *ctx)
944 AVQSVDeviceContext *hwctx = ctx->hwctx;
945 QSVDevicePriv *priv = ctx->user_opaque;
948 MFXClose(hwctx->session);
950 av_buffer_unref(&priv->child_device_ctx);
954 static mfxIMPL choose_implementation(const char *device)
956 static const struct {
960 { "auto", MFX_IMPL_AUTO },
961 { "sw", MFX_IMPL_SOFTWARE },
962 { "hw", MFX_IMPL_HARDWARE },
963 { "auto_any", MFX_IMPL_AUTO_ANY },
964 { "hw_any", MFX_IMPL_HARDWARE_ANY },
965 { "hw2", MFX_IMPL_HARDWARE2 },
966 { "hw3", MFX_IMPL_HARDWARE3 },
967 { "hw4", MFX_IMPL_HARDWARE4 },
970 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
974 for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
975 if (!strcmp(device, impl_map[i].name)) {
976 impl = impl_map[i].impl;
979 if (i == FF_ARRAY_ELEMS(impl_map))
980 impl = strtol(device, NULL, 0);
986 static int qsv_device_derive_from_child(AVHWDeviceContext *ctx,
987 mfxIMPL implementation,
988 AVHWDeviceContext *child_device_ctx,
991 AVQSVDeviceContext *hwctx = ctx->hwctx;
992 QSVDeviceContext *s = ctx->internal->priv;
994 mfxVersion ver = { { 3, 1 } };
996 mfxHandleType handle_type;
1000 switch (child_device_ctx->type) {
1002 case AV_HWDEVICE_TYPE_VAAPI:
1004 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1005 handle_type = MFX_HANDLE_VA_DISPLAY;
1006 handle = (mfxHDL)child_device_hwctx->display;
1011 case AV_HWDEVICE_TYPE_DXVA2:
1013 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1014 handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
1015 handle = (mfxHDL)child_device_hwctx->devmgr;
1020 ret = AVERROR(ENOSYS);
1024 err = MFXInit(implementation, &ver, &hwctx->session);
1025 if (err != MFX_ERR_NONE) {
1026 av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
1028 ret = AVERROR_UNKNOWN;
1032 err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
1033 if (err != MFX_ERR_NONE) {
1034 av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
1036 ret = AVERROR_UNKNOWN;
1044 MFXClose(hwctx->session);
1048 static int qsv_device_derive(AVHWDeviceContext *ctx,
1049 AVHWDeviceContext *child_device_ctx, int flags)
1051 return qsv_device_derive_from_child(ctx, MFX_IMPL_HARDWARE_ANY,
1052 child_device_ctx, flags);
1055 static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
1056 AVDictionary *opts, int flags)
1058 QSVDevicePriv *priv;
1059 enum AVHWDeviceType child_device_type;
1060 AVHWDeviceContext *child_device;
1061 AVDictionaryEntry *e;
1066 priv = av_mallocz(sizeof(*priv));
1068 return AVERROR(ENOMEM);
1070 ctx->user_opaque = priv;
1071 ctx->free = qsv_device_free;
1073 e = av_dict_get(opts, "child_device", NULL, 0);
1076 child_device_type = AV_HWDEVICE_TYPE_VAAPI;
1077 else if (CONFIG_DXVA2)
1078 child_device_type = AV_HWDEVICE_TYPE_DXVA2;
1080 av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
1081 return AVERROR(ENOSYS);
1084 ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
1085 e ? e->value : NULL, NULL, 0);
1089 child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
1091 impl = choose_implementation(device);
1093 return qsv_device_derive_from_child(ctx, impl, child_device, 0);
1096 const HWContextType ff_hwcontext_type_qsv = {
1097 .type = AV_HWDEVICE_TYPE_QSV,
1100 .device_hwctx_size = sizeof(AVQSVDeviceContext),
1101 .device_priv_size = sizeof(QSVDeviceContext),
1102 .frames_hwctx_size = sizeof(AVQSVFramesContext),
1103 .frames_priv_size = sizeof(QSVFramesContext),
1105 .device_create = qsv_device_create,
1106 .device_derive = qsv_device_derive,
1107 .device_init = qsv_device_init,
1108 .frames_get_constraints = qsv_frames_get_constraints,
1109 .frames_init = qsv_frames_init,
1110 .frames_uninit = qsv_frames_uninit,
1111 .frames_get_buffer = qsv_get_buffer,
1112 .transfer_get_formats = qsv_transfer_get_formats,
1113 .transfer_data_to = qsv_transfer_data_to,
1114 .transfer_data_from = qsv_transfer_data_from,
1115 .map_to = qsv_map_to,
1116 .map_from = qsv_map_from,
1117 .frames_derive_to = qsv_frames_derive_to,
1118 .frames_derive_from = qsv_frames_derive_from,
1120 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },