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>
31 #include "hwcontext_vaapi.h"
34 #include "hwcontext_dxva2.h"
39 #include "hwcontext.h"
40 #include "hwcontext_internal.h"
41 #include "hwcontext_qsv.h"
47 typedef struct QSVDevicePriv {
48 AVBufferRef *child_device_ctx;
51 typedef struct QSVDeviceContext {
53 mfxHandleType handle_type;
57 enum AVHWDeviceType child_device_type;
58 enum AVPixelFormat child_pix_fmt;
61 typedef struct QSVFramesContext {
62 mfxSession session_download;
63 int session_download_init;
64 mfxSession session_upload;
65 int session_upload_init;
67 pthread_mutex_t session_lock;
68 pthread_cond_t session_cond;
71 AVBufferRef *child_frames_ref;
72 mfxFrameSurface1 *surfaces_internal;
75 // used in the frame allocator for non-opaque surfaces
77 // used in the opaque alloc request for opaque surfaces
78 mfxFrameSurface1 **surface_ptrs;
80 mfxExtOpaqueSurfaceAlloc opaque_alloc;
81 mfxExtBuffer *ext_buffers[1];
85 mfxHandleType handle_type;
86 enum AVHWDeviceType device_type;
87 enum AVPixelFormat pix_fmt;
88 } supported_handle_types[] = {
90 { MFX_HANDLE_VA_DISPLAY, AV_HWDEVICE_TYPE_VAAPI, AV_PIX_FMT_VAAPI },
93 { MFX_HANDLE_D3D9_DEVICE_MANAGER, AV_HWDEVICE_TYPE_DXVA2, AV_PIX_FMT_DXVA2_VLD },
99 enum AVPixelFormat pix_fmt;
101 } supported_pixel_formats[] = {
102 { AV_PIX_FMT_NV12, MFX_FOURCC_NV12 },
103 { AV_PIX_FMT_BGRA, MFX_FOURCC_RGB4 },
104 { AV_PIX_FMT_P010, MFX_FOURCC_P010 },
105 { AV_PIX_FMT_PAL8, MFX_FOURCC_P8 },
108 static uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
111 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
112 if (supported_pixel_formats[i].pix_fmt == pix_fmt)
113 return supported_pixel_formats[i].fourcc;
118 static int qsv_device_init(AVHWDeviceContext *ctx)
120 AVQSVDeviceContext *hwctx = ctx->hwctx;
121 QSVDeviceContext *s = ctx->internal->priv;
126 for (i = 0; supported_handle_types[i].handle_type; i++) {
127 err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
129 if (err == MFX_ERR_NONE) {
130 s->handle_type = supported_handle_types[i].handle_type;
131 s->child_device_type = supported_handle_types[i].device_type;
132 s->child_pix_fmt = supported_handle_types[i].pix_fmt;
137 av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
138 "from the session\n");
141 err = MFXQueryIMPL(hwctx->session, &s->impl);
142 if (err == MFX_ERR_NONE)
143 err = MFXQueryVersion(hwctx->session, &s->ver);
144 if (err != MFX_ERR_NONE) {
145 av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
146 return AVERROR_UNKNOWN;
152 static void qsv_frames_uninit(AVHWFramesContext *ctx)
154 QSVFramesContext *s = ctx->internal->priv;
156 if (s->session_download) {
157 MFXVideoVPP_Close(s->session_download);
158 MFXClose(s->session_download);
160 s->session_download = NULL;
161 s->session_download_init = 0;
163 if (s->session_upload) {
164 MFXVideoVPP_Close(s->session_upload);
165 MFXClose(s->session_upload);
167 s->session_upload = NULL;
168 s->session_upload_init = 0;
171 pthread_mutex_destroy(&s->session_lock);
172 pthread_cond_destroy(&s->session_cond);
175 av_freep(&s->mem_ids);
176 av_freep(&s->surface_ptrs);
177 av_freep(&s->surfaces_internal);
178 av_buffer_unref(&s->child_frames_ref);
181 static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
185 static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
187 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
188 QSVFramesContext *s = ctx->internal->priv;
189 AVQSVFramesContext *hwctx = ctx->hwctx;
191 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
192 s->nb_surfaces_used++;
193 return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
194 sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
200 static int qsv_init_child_ctx(AVHWFramesContext *ctx)
202 AVQSVFramesContext *hwctx = ctx->hwctx;
203 QSVFramesContext *s = ctx->internal->priv;
204 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
206 AVBufferRef *child_device_ref = NULL;
207 AVBufferRef *child_frames_ref = NULL;
209 AVHWDeviceContext *child_device_ctx;
210 AVHWFramesContext *child_frames_ctx;
214 if (!device_priv->handle) {
215 av_log(ctx, AV_LOG_ERROR,
216 "Cannot create a non-opaque internal surface pool without "
217 "a hardware handle\n");
218 return AVERROR(EINVAL);
221 child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
222 if (!child_device_ref)
223 return AVERROR(ENOMEM);
224 child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
227 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
228 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
229 child_device_hwctx->display = (VADisplay)device_priv->handle;
233 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
234 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
235 child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
239 ret = av_hwdevice_ctx_init(child_device_ref);
241 av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
245 child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
246 if (!child_frames_ref) {
247 ret = AVERROR(ENOMEM);
250 child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
252 child_frames_ctx->format = device_priv->child_pix_fmt;
253 child_frames_ctx->sw_format = ctx->sw_format;
254 child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
255 child_frames_ctx->width = FFALIGN(ctx->width, 16);
256 child_frames_ctx->height = FFALIGN(ctx->height, 16);
259 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
260 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
261 if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
262 child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
264 child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
268 ret = av_hwframe_ctx_init(child_frames_ref);
270 av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
275 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
276 AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
277 for (i = 0; i < ctx->initial_pool_size; i++)
278 s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
279 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
283 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
284 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
285 for (i = 0; i < ctx->initial_pool_size; i++)
286 s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
287 if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
288 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
290 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
294 s->child_frames_ref = child_frames_ref;
295 child_frames_ref = NULL;
298 av_buffer_unref(&child_device_ref);
299 av_buffer_unref(&child_frames_ref);
303 static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
305 const AVPixFmtDescriptor *desc;
308 desc = av_pix_fmt_desc_get(ctx->sw_format);
310 return AVERROR(EINVAL);
312 fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
314 return AVERROR(EINVAL);
316 surf->Info.BitDepthLuma = desc->comp[0].depth;
317 surf->Info.BitDepthChroma = desc->comp[0].depth;
318 surf->Info.Shift = desc->comp[0].depth > 8;
320 if (desc->log2_chroma_w && desc->log2_chroma_h)
321 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
322 else if (desc->log2_chroma_w)
323 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
325 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
327 surf->Info.FourCC = fourcc;
328 surf->Info.Width = FFALIGN(ctx->width, 16);
329 surf->Info.CropW = ctx->width;
330 surf->Info.Height = FFALIGN(ctx->height, 16);
331 surf->Info.CropH = ctx->height;
332 surf->Info.FrameRateExtN = 25;
333 surf->Info.FrameRateExtD = 1;
334 surf->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
339 static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
341 QSVFramesContext *s = ctx->internal->priv;
342 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
346 if (ctx->initial_pool_size <= 0) {
347 av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
348 return AVERROR(EINVAL);
351 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
352 sizeof(*s->surfaces_internal));
353 if (!s->surfaces_internal)
354 return AVERROR(ENOMEM);
356 for (i = 0; i < ctx->initial_pool_size; i++) {
357 ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
362 if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
363 ret = qsv_init_child_ctx(ctx);
368 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
369 ctx, qsv_pool_alloc, NULL);
370 if (!ctx->internal->pool_internal)
371 return AVERROR(ENOMEM);
373 frames_hwctx->surfaces = s->surfaces_internal;
374 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
379 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
380 mfxFrameAllocResponse *resp)
382 AVHWFramesContext *ctx = pthis;
383 QSVFramesContext *s = ctx->internal->priv;
384 AVQSVFramesContext *hwctx = ctx->hwctx;
385 mfxFrameInfo *i = &req->Info;
386 mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
388 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
389 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
390 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
391 return MFX_ERR_UNSUPPORTED;
392 if (i->Width > i1->Width || i->Height > i1->Height ||
393 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
394 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
395 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
396 i->Width, i->Height, i->FourCC, i->ChromaFormat,
397 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
398 return MFX_ERR_UNSUPPORTED;
401 resp->mids = s->mem_ids;
402 resp->NumFrameActual = hwctx->nb_surfaces;
407 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
412 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
414 return MFX_ERR_UNSUPPORTED;
417 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
419 return MFX_ERR_UNSUPPORTED;
422 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
428 static int qsv_init_internal_session(AVHWFramesContext *ctx,
429 mfxSession *session, int upload)
431 QSVFramesContext *s = ctx->internal->priv;
432 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
433 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
434 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
436 mfxFrameAllocator frame_allocator = {
438 .Alloc = frame_alloc,
440 .Unlock = frame_unlock,
441 .GetHDL = frame_get_hdl,
448 err = MFXInit(device_priv->impl, &device_priv->ver, session);
449 if (err != MFX_ERR_NONE) {
450 av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
451 return AVERROR_UNKNOWN;
454 if (device_priv->handle) {
455 err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
456 device_priv->handle);
457 if (err != MFX_ERR_NONE)
458 return AVERROR_UNKNOWN;
462 err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
463 if (err != MFX_ERR_NONE)
464 return AVERROR_UNKNOWN;
467 memset(&par, 0, sizeof(par));
470 par.ExtParam = s->ext_buffers;
471 par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
472 par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
473 MFX_IOPATTERN_IN_OPAQUE_MEMORY;
475 par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
476 MFX_IOPATTERN_IN_VIDEO_MEMORY;
479 par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
480 MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
483 par.vpp.In = frames_hwctx->surfaces[0].Info;
485 /* Apparently VPP requires the frame rate to be set to some value, otherwise
486 * init will fail (probably for the framerate conversion filter). Since we
487 * are only doing data upload/download here, we just invent an arbitrary
489 par.vpp.In.FrameRateExtN = 25;
490 par.vpp.In.FrameRateExtD = 1;
491 par.vpp.Out = par.vpp.In;
493 err = MFXVideoVPP_Init(*session, &par);
494 if (err != MFX_ERR_NONE) {
495 av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
496 "Surface upload/download will not be possible\n");
504 static int qsv_frames_init(AVHWFramesContext *ctx)
506 QSVFramesContext *s = ctx->internal->priv;
507 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
509 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
514 fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
516 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
517 return AVERROR(ENOSYS);
521 ret = qsv_init_pool(ctx, fourcc);
523 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
529 s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
530 sizeof(*s->surface_ptrs));
531 if (!s->surface_ptrs)
532 return AVERROR(ENOMEM);
534 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
535 s->surface_ptrs[i] = frames_hwctx->surfaces + i;
537 s->opaque_alloc.In.Surfaces = s->surface_ptrs;
538 s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
539 s->opaque_alloc.In.Type = frames_hwctx->frame_type;
541 s->opaque_alloc.Out = s->opaque_alloc.In;
543 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
544 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
546 s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
548 s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
550 return AVERROR(ENOMEM);
552 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
553 s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
556 s->session_download = NULL;
557 s->session_upload = NULL;
559 s->session_download_init = 0;
560 s->session_upload_init = 0;
563 pthread_mutex_init(&s->session_lock, NULL);
564 pthread_cond_init(&s->session_cond, NULL);
570 static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
572 frame->buf[0] = av_buffer_pool_get(ctx->pool);
574 return AVERROR(ENOMEM);
576 frame->data[3] = frame->buf[0]->data;
577 frame->format = AV_PIX_FMT_QSV;
578 frame->width = ctx->width;
579 frame->height = ctx->height;
584 static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
585 enum AVHWFrameTransferDirection dir,
586 enum AVPixelFormat **formats)
588 enum AVPixelFormat *fmts;
590 fmts = av_malloc_array(2, sizeof(*fmts));
592 return AVERROR(ENOMEM);
594 fmts[0] = ctx->sw_format;
595 fmts[1] = AV_PIX_FMT_NONE;
602 static int qsv_frames_derive_from(AVHWFramesContext *dst_ctx,
603 AVHWFramesContext *src_ctx, int flags)
605 AVQSVFramesContext *src_hwctx = src_ctx->hwctx;
608 switch (dst_ctx->device_ctx->type) {
610 case AV_HWDEVICE_TYPE_VAAPI:
612 AVVAAPIFramesContext *dst_hwctx = dst_ctx->hwctx;
613 dst_hwctx->surface_ids = av_mallocz_array(src_hwctx->nb_surfaces,
614 sizeof(*dst_hwctx->surface_ids));
615 if (!dst_hwctx->surface_ids)
616 return AVERROR(ENOMEM);
617 for (i = 0; i < src_hwctx->nb_surfaces; i++)
618 dst_hwctx->surface_ids[i] =
619 *(VASurfaceID*)src_hwctx->surfaces[i].Data.MemId;
620 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
625 case AV_HWDEVICE_TYPE_DXVA2:
627 AVDXVA2FramesContext *dst_hwctx = dst_ctx->hwctx;
628 dst_hwctx->surfaces = av_mallocz_array(src_hwctx->nb_surfaces,
629 sizeof(*dst_hwctx->surfaces));
630 if (!dst_hwctx->surfaces)
631 return AVERROR(ENOMEM);
632 for (i = 0; i < src_hwctx->nb_surfaces; i++)
633 dst_hwctx->surfaces[i] =
634 (IDirect3DSurface9*)src_hwctx->surfaces[i].Data.MemId;
635 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
636 if (src_hwctx->frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
637 dst_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
639 dst_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
644 return AVERROR(ENOSYS);
650 static int qsv_map_from(AVHWFramesContext *ctx,
651 AVFrame *dst, const AVFrame *src, int flags)
653 QSVFramesContext *s = ctx->internal->priv;
654 mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
655 AVHWFramesContext *child_frames_ctx;
656 const AVPixFmtDescriptor *desc;
661 if (!s->child_frames_ref)
662 return AVERROR(ENOSYS);
663 child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
665 switch (child_frames_ctx->device_ctx->type) {
667 case AV_HWDEVICE_TYPE_VAAPI:
668 child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)surf->Data.MemId;
672 case AV_HWDEVICE_TYPE_DXVA2:
673 child_data = surf->Data.MemId;
677 return AVERROR(ENOSYS);
680 if (dst->format == child_frames_ctx->format) {
681 ret = ff_hwframe_map_create(s->child_frames_ref,
682 dst, src, NULL, NULL);
686 dst->width = src->width;
687 dst->height = src->height;
688 dst->data[3] = child_data;
693 desc = av_pix_fmt_desc_get(dst->format);
694 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
695 // This only supports mapping to software.
696 return AVERROR(ENOSYS);
699 dummy = av_frame_alloc();
701 return AVERROR(ENOMEM);
703 dummy->buf[0] = av_buffer_ref(src->buf[0]);
704 dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
705 if (!dummy->buf[0] || !dummy->hw_frames_ctx)
708 dummy->format = child_frames_ctx->format;
709 dummy->width = src->width;
710 dummy->height = src->height;
711 dummy->data[3] = child_data;
713 ret = av_hwframe_map(dst, dummy, flags);
716 av_frame_free(&dummy);
721 static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst,
724 QSVFramesContext *s = ctx->internal->priv;
725 AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
726 int download = !!src->hw_frames_ctx;
727 mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
732 dummy = av_frame_alloc();
734 return AVERROR(ENOMEM);
736 dummy->format = child_frames_ctx->format;
737 dummy->width = src->width;
738 dummy->height = src->height;
739 dummy->buf[0] = download ? src->buf[0] : dst->buf[0];
740 dummy->data[3] = surf->Data.MemId;
741 dummy->hw_frames_ctx = s->child_frames_ref;
743 ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
744 av_hwframe_transfer_data(dummy, src, 0);
746 dummy->buf[0] = NULL;
747 dummy->data[3] = NULL;
748 dummy->hw_frames_ctx = NULL;
750 av_frame_free(&dummy);
755 static int map_frame_to_surface(const AVFrame *frame, mfxFrameSurface1 *surface)
757 switch (frame->format) {
758 case AV_PIX_FMT_NV12:
759 case AV_PIX_FMT_P010:
760 surface->Data.Y = frame->data[0];
761 surface->Data.UV = frame->data[1];
764 case AV_PIX_FMT_YUV420P:
765 surface->Data.Y = frame->data[0];
766 surface->Data.U = frame->data[1];
767 surface->Data.V = frame->data[2];
770 case AV_PIX_FMT_BGRA:
771 surface->Data.B = frame->data[0];
772 surface->Data.G = frame->data[0] + 1;
773 surface->Data.R = frame->data[0] + 2;
774 surface->Data.A = frame->data[0] + 3;
778 return MFX_ERR_UNSUPPORTED;
780 surface->Data.Pitch = frame->linesize[0];
781 surface->Data.TimeStamp = frame->pts;
786 static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
789 QSVFramesContext *s = ctx->internal->priv;
790 mfxFrameSurface1 out = {{ 0 }};
791 mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
793 mfxSyncPoint sync = NULL;
797 while (!s->session_download_init && !s->session_download && !ret) {
799 if (pthread_mutex_trylock(&s->session_lock) == 0) {
801 if (!s->session_download_init) {
802 ret = qsv_init_internal_session(ctx, &s->session_download, 0);
803 if (s->session_download)
804 s->session_download_init = 1;
807 pthread_mutex_unlock(&s->session_lock);
808 pthread_cond_signal(&s->session_cond);
810 pthread_mutex_lock(&s->session_lock);
811 while (!s->session_download_init && !s->session_download) {
812 pthread_cond_wait(&s->session_cond, &s->session_lock);
814 pthread_mutex_unlock(&s->session_lock);
822 if (!s->session_download) {
823 if (s->child_frames_ref)
824 return qsv_transfer_data_child(ctx, dst, src);
826 av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
827 return AVERROR(ENOSYS);
831 map_frame_to_surface(dst, &out);
834 err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
835 if (err == MFX_WRN_DEVICE_BUSY)
837 } while (err == MFX_WRN_DEVICE_BUSY);
839 if (err < 0 || !sync) {
840 av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
841 return AVERROR_UNKNOWN;
845 err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
846 } while (err == MFX_WRN_IN_EXECUTION);
848 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
849 return AVERROR_UNKNOWN;
855 static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
858 QSVFramesContext *s = ctx->internal->priv;
859 mfxFrameSurface1 in = {{ 0 }};
860 mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
862 mfxSyncPoint sync = NULL;
865 /* make a copy if the input is not padded as libmfx requires */
867 const AVFrame *src_frame;
871 while (!s->session_upload_init && !s->session_upload && !ret) {
873 if (pthread_mutex_trylock(&s->session_lock) == 0) {
875 if (!s->session_upload_init) {
876 ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
877 if (s->session_upload)
878 s->session_upload_init = 1;
881 pthread_mutex_unlock(&s->session_lock);
882 pthread_cond_signal(&s->session_cond);
884 pthread_mutex_lock(&s->session_lock);
885 while (!s->session_upload_init && !s->session_upload) {
886 pthread_cond_wait(&s->session_cond, &s->session_lock);
888 pthread_mutex_unlock(&s->session_lock);
895 if (src->height & 15 || src->linesize[0] & 15) {
897 memset(&tmp_frame, 0, sizeof(tmp_frame));
898 tmp_frame.format = src->format;
899 tmp_frame.width = FFALIGN(src->width, 16);
900 tmp_frame.height = FFALIGN(src->height, 16);
901 ret = av_frame_get_buffer(&tmp_frame, 32);
905 ret = av_frame_copy(&tmp_frame, src);
907 av_frame_unref(&tmp_frame);
912 src_frame = realigned ? &tmp_frame : src;
914 if (!s->session_upload) {
915 if (s->child_frames_ref)
916 return qsv_transfer_data_child(ctx, dst, src_frame);
918 av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
919 return AVERROR(ENOSYS);
923 map_frame_to_surface(src_frame, &in);
926 err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
927 if (err == MFX_WRN_DEVICE_BUSY)
929 } while (err == MFX_WRN_DEVICE_BUSY);
931 if (err < 0 || !sync) {
932 av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
933 return AVERROR_UNKNOWN;
937 err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
938 } while (err == MFX_WRN_IN_EXECUTION);
940 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
941 return AVERROR_UNKNOWN;
945 av_frame_unref(&tmp_frame);
950 static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx,
951 AVHWFramesContext *src_ctx, int flags)
953 QSVFramesContext *s = dst_ctx->internal->priv;
954 AVQSVFramesContext *dst_hwctx = dst_ctx->hwctx;
957 switch (src_ctx->device_ctx->type) {
959 case AV_HWDEVICE_TYPE_VAAPI:
961 AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
962 s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
963 sizeof(*s->surfaces_internal));
964 if (!s->surfaces_internal)
965 return AVERROR(ENOMEM);
966 for (i = 0; i < src_hwctx->nb_surfaces; i++) {
967 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
968 s->surfaces_internal[i].Data.MemId = src_hwctx->surface_ids + i;
970 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
971 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
976 case AV_HWDEVICE_TYPE_DXVA2:
978 AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
979 s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
980 sizeof(*s->surfaces_internal));
981 if (!s->surfaces_internal)
982 return AVERROR(ENOMEM);
983 for (i = 0; i < src_hwctx->nb_surfaces; i++) {
984 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
985 s->surfaces_internal[i].Data.MemId = (mfxMemId)src_hwctx->surfaces[i];
987 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
988 if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
989 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
991 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
996 return AVERROR(ENOSYS);
999 dst_hwctx->surfaces = s->surfaces_internal;
1004 static int qsv_map_to(AVHWFramesContext *dst_ctx,
1005 AVFrame *dst, const AVFrame *src, int flags)
1007 AVQSVFramesContext *hwctx = dst_ctx->hwctx;
1010 for (i = 0; i < hwctx->nb_surfaces; i++) {
1012 if (*(VASurfaceID*)hwctx->surfaces[i].Data.MemId ==
1013 (VASurfaceID)(uintptr_t)src->data[3])
1017 if ((IDirect3DSurface9*)hwctx->surfaces[i].Data.MemId ==
1018 (IDirect3DSurface9*)(uintptr_t)src->data[3])
1022 if (i >= hwctx->nb_surfaces) {
1023 av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
1024 "is not in the mapped frames context.\n");
1025 return AVERROR(EINVAL);
1028 err = ff_hwframe_map_create(dst->hw_frames_ctx,
1029 dst, src, NULL, NULL);
1033 dst->width = src->width;
1034 dst->height = src->height;
1035 dst->data[3] = (uint8_t*)&hwctx->surfaces[i];
1040 static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
1041 const void *hwconfig,
1042 AVHWFramesConstraints *constraints)
1046 constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
1047 sizeof(*constraints->valid_sw_formats));
1048 if (!constraints->valid_sw_formats)
1049 return AVERROR(ENOMEM);
1051 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
1052 constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
1053 constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
1055 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
1056 if (!constraints->valid_hw_formats)
1057 return AVERROR(ENOMEM);
1059 constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
1060 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
1065 static void qsv_device_free(AVHWDeviceContext *ctx)
1067 AVQSVDeviceContext *hwctx = ctx->hwctx;
1068 QSVDevicePriv *priv = ctx->user_opaque;
1071 MFXClose(hwctx->session);
1073 av_buffer_unref(&priv->child_device_ctx);
1077 static mfxIMPL choose_implementation(const char *device)
1079 static const struct {
1083 { "auto", MFX_IMPL_AUTO },
1084 { "sw", MFX_IMPL_SOFTWARE },
1085 { "hw", MFX_IMPL_HARDWARE },
1086 { "auto_any", MFX_IMPL_AUTO_ANY },
1087 { "hw_any", MFX_IMPL_HARDWARE_ANY },
1088 { "hw2", MFX_IMPL_HARDWARE2 },
1089 { "hw3", MFX_IMPL_HARDWARE3 },
1090 { "hw4", MFX_IMPL_HARDWARE4 },
1093 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
1097 for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
1098 if (!strcmp(device, impl_map[i].name)) {
1099 impl = impl_map[i].impl;
1102 if (i == FF_ARRAY_ELEMS(impl_map))
1103 impl = strtol(device, NULL, 0);
1109 static int qsv_device_derive_from_child(AVHWDeviceContext *ctx,
1110 mfxIMPL implementation,
1111 AVHWDeviceContext *child_device_ctx,
1114 AVQSVDeviceContext *hwctx = ctx->hwctx;
1116 mfxVersion ver = { { 3, 1 } };
1118 mfxHandleType handle_type;
1122 switch (child_device_ctx->type) {
1124 case AV_HWDEVICE_TYPE_VAAPI:
1126 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1127 handle_type = MFX_HANDLE_VA_DISPLAY;
1128 handle = (mfxHDL)child_device_hwctx->display;
1133 case AV_HWDEVICE_TYPE_DXVA2:
1135 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1136 handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
1137 handle = (mfxHDL)child_device_hwctx->devmgr;
1142 ret = AVERROR(ENOSYS);
1146 err = MFXInit(implementation, &ver, &hwctx->session);
1147 if (err != MFX_ERR_NONE) {
1148 av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
1150 ret = AVERROR_UNKNOWN;
1154 err = MFXQueryVersion(hwctx->session, &ver);
1155 if (err != MFX_ERR_NONE) {
1156 av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: %d.\n", err);
1157 ret = AVERROR_UNKNOWN;
1161 av_log(ctx, AV_LOG_VERBOSE,
1162 "Initialize MFX session: API version is %d.%d, implementation version is %d.%d\n",
1163 MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
1165 MFXClose(hwctx->session);
1167 err = MFXInit(implementation, &ver, &hwctx->session);
1168 if (err != MFX_ERR_NONE) {
1169 av_log(ctx, AV_LOG_ERROR,
1170 "Error initializing an MFX session: %d.\n", err);
1171 ret = AVERROR_UNKNOWN;
1175 err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
1176 if (err != MFX_ERR_NONE) {
1177 av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
1179 ret = AVERROR_UNKNOWN;
1187 MFXClose(hwctx->session);
1191 static int qsv_device_derive(AVHWDeviceContext *ctx,
1192 AVHWDeviceContext *child_device_ctx, int flags)
1194 return qsv_device_derive_from_child(ctx, MFX_IMPL_HARDWARE_ANY,
1195 child_device_ctx, flags);
1198 static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
1199 AVDictionary *opts, int flags)
1201 QSVDevicePriv *priv;
1202 enum AVHWDeviceType child_device_type;
1203 AVHWDeviceContext *child_device;
1204 AVDictionary *child_device_opts;
1205 AVDictionaryEntry *e;
1210 priv = av_mallocz(sizeof(*priv));
1212 return AVERROR(ENOMEM);
1214 ctx->user_opaque = priv;
1215 ctx->free = qsv_device_free;
1217 e = av_dict_get(opts, "child_device", NULL, 0);
1219 child_device_opts = NULL;
1221 child_device_type = AV_HWDEVICE_TYPE_VAAPI;
1222 // libmfx does not actually implement VAAPI properly, rather it
1223 // depends on the specific behaviour of a matching iHD driver when
1224 // used on recent Intel hardware. Set options to the VAAPI device
1225 // creation so that we should pick a usable setup by default if
1226 // possible, even when multiple devices and drivers are available.
1227 av_dict_set(&child_device_opts, "kernel_driver", "i915", 0);
1228 av_dict_set(&child_device_opts, "driver", "iHD", 0);
1229 } else if (CONFIG_DXVA2)
1230 child_device_type = AV_HWDEVICE_TYPE_DXVA2;
1232 av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
1233 return AVERROR(ENOSYS);
1236 ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
1237 e ? e->value : NULL, child_device_opts, 0);
1239 av_dict_free(&child_device_opts);
1243 child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
1245 impl = choose_implementation(device);
1247 return qsv_device_derive_from_child(ctx, impl, child_device, 0);
1250 const HWContextType ff_hwcontext_type_qsv = {
1251 .type = AV_HWDEVICE_TYPE_QSV,
1254 .device_hwctx_size = sizeof(AVQSVDeviceContext),
1255 .device_priv_size = sizeof(QSVDeviceContext),
1256 .frames_hwctx_size = sizeof(AVQSVFramesContext),
1257 .frames_priv_size = sizeof(QSVFramesContext),
1259 .device_create = qsv_device_create,
1260 .device_derive = qsv_device_derive,
1261 .device_init = qsv_device_init,
1262 .frames_get_constraints = qsv_frames_get_constraints,
1263 .frames_init = qsv_frames_init,
1264 .frames_uninit = qsv_frames_uninit,
1265 .frames_get_buffer = qsv_get_buffer,
1266 .transfer_get_formats = qsv_transfer_get_formats,
1267 .transfer_data_to = qsv_transfer_data_to,
1268 .transfer_data_from = qsv_transfer_data_from,
1269 .map_to = qsv_map_to,
1270 .map_from = qsv_map_from,
1271 .frames_derive_to = qsv_frames_derive_to,
1272 .frames_derive_from = qsv_frames_derive_from,
1274 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },