2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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_P010, MFX_FOURCC_P010 },
104 { AV_PIX_FMT_PAL8, MFX_FOURCC_P8 },
107 static uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
110 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
111 if (supported_pixel_formats[i].pix_fmt == pix_fmt)
112 return supported_pixel_formats[i].fourcc;
117 static int qsv_device_init(AVHWDeviceContext *ctx)
119 AVQSVDeviceContext *hwctx = ctx->hwctx;
120 QSVDeviceContext *s = ctx->internal->priv;
125 for (i = 0; supported_handle_types[i].handle_type; i++) {
126 err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
128 if (err == MFX_ERR_NONE) {
129 s->handle_type = supported_handle_types[i].handle_type;
130 s->child_device_type = supported_handle_types[i].device_type;
131 s->child_pix_fmt = supported_handle_types[i].pix_fmt;
136 av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
137 "from the session\n");
140 err = MFXQueryIMPL(hwctx->session, &s->impl);
141 if (err == MFX_ERR_NONE)
142 err = MFXQueryVersion(hwctx->session, &s->ver);
143 if (err != MFX_ERR_NONE) {
144 av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
145 return AVERROR_UNKNOWN;
151 static void qsv_frames_uninit(AVHWFramesContext *ctx)
153 QSVFramesContext *s = ctx->internal->priv;
155 if (s->session_download) {
156 MFXVideoVPP_Close(s->session_download);
157 MFXClose(s->session_download);
159 s->session_download = NULL;
160 s->session_download_init = 0;
162 if (s->session_upload) {
163 MFXVideoVPP_Close(s->session_upload);
164 MFXClose(s->session_upload);
166 s->session_upload = NULL;
167 s->session_upload_init = 0;
170 pthread_mutex_destroy(&s->session_lock);
171 pthread_cond_destroy(&s->session_cond);
174 av_freep(&s->mem_ids);
175 av_freep(&s->surface_ptrs);
176 av_freep(&s->surfaces_internal);
177 av_buffer_unref(&s->child_frames_ref);
180 static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
184 static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
186 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
187 QSVFramesContext *s = ctx->internal->priv;
188 AVQSVFramesContext *hwctx = ctx->hwctx;
190 if (s->nb_surfaces_used < hwctx->nb_surfaces) {
191 s->nb_surfaces_used++;
192 return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
193 sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
199 static int qsv_init_child_ctx(AVHWFramesContext *ctx)
201 AVQSVFramesContext *hwctx = ctx->hwctx;
202 QSVFramesContext *s = ctx->internal->priv;
203 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
205 AVBufferRef *child_device_ref = NULL;
206 AVBufferRef *child_frames_ref = NULL;
208 AVHWDeviceContext *child_device_ctx;
209 AVHWFramesContext *child_frames_ctx;
213 if (!device_priv->handle) {
214 av_log(ctx, AV_LOG_ERROR,
215 "Cannot create a non-opaque internal surface pool without "
216 "a hardware handle\n");
217 return AVERROR(EINVAL);
220 child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
221 if (!child_device_ref)
222 return AVERROR(ENOMEM);
223 child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
226 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
227 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
228 child_device_hwctx->display = (VADisplay)device_priv->handle;
232 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
233 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
234 child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
238 ret = av_hwdevice_ctx_init(child_device_ref);
240 av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
244 child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
245 if (!child_frames_ref) {
246 ret = AVERROR(ENOMEM);
249 child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
251 child_frames_ctx->format = device_priv->child_pix_fmt;
252 child_frames_ctx->sw_format = ctx->sw_format;
253 child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
254 child_frames_ctx->width = FFALIGN(ctx->width, 16);
255 child_frames_ctx->height = FFALIGN(ctx->height, 16);
258 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
259 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
260 if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
261 child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
263 child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
267 ret = av_hwframe_ctx_init(child_frames_ref);
269 av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
274 if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
275 AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
276 for (i = 0; i < ctx->initial_pool_size; i++)
277 s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
278 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
282 if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
283 AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
284 for (i = 0; i < ctx->initial_pool_size; i++)
285 s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
286 if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
287 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
289 hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
293 s->child_frames_ref = child_frames_ref;
294 child_frames_ref = NULL;
297 av_buffer_unref(&child_device_ref);
298 av_buffer_unref(&child_frames_ref);
302 static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
304 const AVPixFmtDescriptor *desc;
307 desc = av_pix_fmt_desc_get(ctx->sw_format);
309 return AVERROR(EINVAL);
311 fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
313 return AVERROR(EINVAL);
315 surf->Info.BitDepthLuma = desc->comp[0].depth;
316 surf->Info.BitDepthChroma = desc->comp[0].depth;
317 surf->Info.Shift = desc->comp[0].depth > 8;
319 if (desc->log2_chroma_w && desc->log2_chroma_h)
320 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
321 else if (desc->log2_chroma_w)
322 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
324 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
326 surf->Info.FourCC = fourcc;
327 surf->Info.Width = FFALIGN(ctx->width, 16);
328 surf->Info.CropW = ctx->width;
329 surf->Info.Height = FFALIGN(ctx->height, 16);
330 surf->Info.CropH = ctx->height;
331 surf->Info.FrameRateExtN = 25;
332 surf->Info.FrameRateExtD = 1;
333 surf->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
338 static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
340 QSVFramesContext *s = ctx->internal->priv;
341 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
345 if (ctx->initial_pool_size <= 0) {
346 av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
347 return AVERROR(EINVAL);
350 s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
351 sizeof(*s->surfaces_internal));
352 if (!s->surfaces_internal)
353 return AVERROR(ENOMEM);
355 for (i = 0; i < ctx->initial_pool_size; i++) {
356 ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
361 if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
362 ret = qsv_init_child_ctx(ctx);
367 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
368 ctx, qsv_pool_alloc, NULL);
369 if (!ctx->internal->pool_internal)
370 return AVERROR(ENOMEM);
372 frames_hwctx->surfaces = s->surfaces_internal;
373 frames_hwctx->nb_surfaces = ctx->initial_pool_size;
378 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
379 mfxFrameAllocResponse *resp)
381 AVHWFramesContext *ctx = pthis;
382 QSVFramesContext *s = ctx->internal->priv;
383 AVQSVFramesContext *hwctx = ctx->hwctx;
384 mfxFrameInfo *i = &req->Info;
385 mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
387 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
388 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
389 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
390 return MFX_ERR_UNSUPPORTED;
391 if (i->Width != i1->Width || i->Height != i1->Height ||
392 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
393 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
394 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
395 i->Width, i->Height, i->FourCC, i->ChromaFormat,
396 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
397 return MFX_ERR_UNSUPPORTED;
400 resp->mids = s->mem_ids;
401 resp->NumFrameActual = hwctx->nb_surfaces;
406 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
411 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
413 return MFX_ERR_UNSUPPORTED;
416 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
418 return MFX_ERR_UNSUPPORTED;
421 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
427 static int qsv_init_internal_session(AVHWFramesContext *ctx,
428 mfxSession *session, int upload)
430 QSVFramesContext *s = ctx->internal->priv;
431 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
432 QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
433 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
435 mfxFrameAllocator frame_allocator = {
437 .Alloc = frame_alloc,
439 .Unlock = frame_unlock,
440 .GetHDL = frame_get_hdl,
447 err = MFXInit(device_priv->impl, &device_priv->ver, session);
448 if (err != MFX_ERR_NONE) {
449 av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
450 return AVERROR_UNKNOWN;
453 if (device_priv->handle) {
454 err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
455 device_priv->handle);
456 if (err != MFX_ERR_NONE)
457 return AVERROR_UNKNOWN;
461 err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
462 if (err != MFX_ERR_NONE)
463 return AVERROR_UNKNOWN;
466 memset(&par, 0, sizeof(par));
469 par.ExtParam = s->ext_buffers;
470 par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
471 par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
472 MFX_IOPATTERN_IN_OPAQUE_MEMORY;
474 par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
475 MFX_IOPATTERN_IN_VIDEO_MEMORY;
478 par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
479 MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
482 par.vpp.In = frames_hwctx->surfaces[0].Info;
484 /* Apparently VPP requires the frame rate to be set to some value, otherwise
485 * init will fail (probably for the framerate conversion filter). Since we
486 * are only doing data upload/download here, we just invent an arbitrary
488 par.vpp.In.FrameRateExtN = 25;
489 par.vpp.In.FrameRateExtD = 1;
490 par.vpp.Out = par.vpp.In;
492 err = MFXVideoVPP_Init(*session, &par);
493 if (err != MFX_ERR_NONE) {
494 av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
495 "Surface upload/download will not be possible\n");
503 static int qsv_frames_init(AVHWFramesContext *ctx)
505 QSVFramesContext *s = ctx->internal->priv;
506 AVQSVFramesContext *frames_hwctx = ctx->hwctx;
508 int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
513 fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
515 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
516 return AVERROR(ENOSYS);
520 ret = qsv_init_pool(ctx, fourcc);
522 av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
528 s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
529 sizeof(*s->surface_ptrs));
530 if (!s->surface_ptrs)
531 return AVERROR(ENOMEM);
533 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
534 s->surface_ptrs[i] = frames_hwctx->surfaces + i;
536 s->opaque_alloc.In.Surfaces = s->surface_ptrs;
537 s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
538 s->opaque_alloc.In.Type = frames_hwctx->frame_type;
540 s->opaque_alloc.Out = s->opaque_alloc.In;
542 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
543 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
545 s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
547 s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
549 return AVERROR(ENOMEM);
551 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
552 s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
555 s->session_download = NULL;
556 s->session_upload = NULL;
558 s->session_download_init = 0;
559 s->session_upload_init = 0;
562 pthread_mutex_init(&s->session_lock, NULL);
563 pthread_cond_init(&s->session_cond, NULL);
569 static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
571 frame->buf[0] = av_buffer_pool_get(ctx->pool);
573 return AVERROR(ENOMEM);
575 frame->data[3] = frame->buf[0]->data;
576 frame->format = AV_PIX_FMT_QSV;
577 frame->width = ctx->width;
578 frame->height = ctx->height;
583 static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
584 enum AVHWFrameTransferDirection dir,
585 enum AVPixelFormat **formats)
587 enum AVPixelFormat *fmts;
589 fmts = av_malloc_array(2, sizeof(*fmts));
591 return AVERROR(ENOMEM);
593 fmts[0] = ctx->sw_format;
594 fmts[1] = AV_PIX_FMT_NONE;
601 static int qsv_frames_derive_from(AVHWFramesContext *dst_ctx,
602 AVHWFramesContext *src_ctx, int flags)
604 AVQSVFramesContext *src_hwctx = src_ctx->hwctx;
607 switch (dst_ctx->device_ctx->type) {
609 case AV_HWDEVICE_TYPE_VAAPI:
611 AVVAAPIFramesContext *dst_hwctx = dst_ctx->hwctx;
612 dst_hwctx->surface_ids = av_mallocz_array(src_hwctx->nb_surfaces,
613 sizeof(*dst_hwctx->surface_ids));
614 if (!dst_hwctx->surface_ids)
615 return AVERROR(ENOMEM);
616 for (i = 0; i < src_hwctx->nb_surfaces; i++)
617 dst_hwctx->surface_ids[i] =
618 *(VASurfaceID*)src_hwctx->surfaces[i].Data.MemId;
619 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
624 case AV_HWDEVICE_TYPE_DXVA2:
626 AVDXVA2FramesContext *dst_hwctx = dst_ctx->hwctx;
627 dst_hwctx->surfaces = av_mallocz_array(src_hwctx->nb_surfaces,
628 sizeof(*dst_hwctx->surfaces));
629 if (!dst_hwctx->surfaces)
630 return AVERROR(ENOMEM);
631 for (i = 0; i < src_hwctx->nb_surfaces; i++)
632 dst_hwctx->surfaces[i] =
633 (IDirect3DSurface9*)src_hwctx->surfaces[i].Data.MemId;
634 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
635 if (src_hwctx->frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
636 dst_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
638 dst_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
643 return AVERROR(ENOSYS);
649 static int qsv_map_from(AVHWFramesContext *ctx,
650 AVFrame *dst, const AVFrame *src, int flags)
652 QSVFramesContext *s = ctx->internal->priv;
653 mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
654 AVHWFramesContext *child_frames_ctx;
655 const AVPixFmtDescriptor *desc;
660 if (!s->child_frames_ref)
661 return AVERROR(ENOSYS);
662 child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
664 switch (child_frames_ctx->device_ctx->type) {
666 case AV_HWDEVICE_TYPE_VAAPI:
667 child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)surf->Data.MemId;
671 case AV_HWDEVICE_TYPE_DXVA2:
672 child_data = surf->Data.MemId;
676 return AVERROR(ENOSYS);
679 if (dst->format == child_frames_ctx->format) {
680 ret = ff_hwframe_map_create(s->child_frames_ref,
681 dst, src, NULL, NULL);
685 dst->width = src->width;
686 dst->height = src->height;
687 dst->data[3] = child_data;
692 desc = av_pix_fmt_desc_get(dst->format);
693 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
694 // This only supports mapping to software.
695 return AVERROR(ENOSYS);
698 dummy = av_frame_alloc();
700 return AVERROR(ENOMEM);
702 dummy->buf[0] = av_buffer_ref(src->buf[0]);
703 dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
704 if (!dummy->buf[0] || !dummy->hw_frames_ctx)
707 dummy->format = child_frames_ctx->format;
708 dummy->width = src->width;
709 dummy->height = src->height;
710 dummy->data[3] = child_data;
712 ret = av_hwframe_map(dst, dummy, flags);
715 av_frame_free(&dummy);
720 static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst,
723 QSVFramesContext *s = ctx->internal->priv;
724 AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
725 int download = !!src->hw_frames_ctx;
726 mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
731 dummy = av_frame_alloc();
733 return AVERROR(ENOMEM);
735 dummy->format = child_frames_ctx->format;
736 dummy->width = src->width;
737 dummy->height = src->height;
738 dummy->buf[0] = download ? src->buf[0] : dst->buf[0];
739 dummy->data[3] = surf->Data.MemId;
740 dummy->hw_frames_ctx = s->child_frames_ref;
742 ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
743 av_hwframe_transfer_data(dummy, src, 0);
745 dummy->buf[0] = NULL;
746 dummy->data[3] = NULL;
747 dummy->hw_frames_ctx = NULL;
749 av_frame_free(&dummy);
754 static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
757 QSVFramesContext *s = ctx->internal->priv;
758 mfxFrameSurface1 out = {{ 0 }};
759 mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
761 mfxSyncPoint sync = NULL;
765 while (!s->session_download_init && !s->session_download && !ret) {
767 if (pthread_mutex_trylock(&s->session_lock) == 0) {
769 if (!s->session_download_init) {
770 ret = qsv_init_internal_session(ctx, &s->session_download, 0);
771 if (s->session_download)
772 s->session_download_init = 1;
775 pthread_mutex_unlock(&s->session_lock);
776 pthread_cond_signal(&s->session_cond);
778 pthread_mutex_lock(&s->session_lock);
779 while (!s->session_download_init && !s->session_download) {
780 pthread_cond_wait(&s->session_cond, &s->session_lock);
782 pthread_mutex_unlock(&s->session_lock);
790 if (!s->session_download) {
791 if (s->child_frames_ref)
792 return qsv_transfer_data_child(ctx, dst, src);
794 av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
795 return AVERROR(ENOSYS);
799 out.Data.PitchLow = dst->linesize[0];
800 out.Data.Y = dst->data[0];
801 out.Data.U = dst->data[1];
802 out.Data.V = dst->data[2];
803 out.Data.A = dst->data[3];
806 err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, 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 downloading the surface\n");
813 return AVERROR_UNKNOWN;
817 err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
818 } while (err == MFX_WRN_IN_EXECUTION);
820 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
821 return AVERROR_UNKNOWN;
827 static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
830 QSVFramesContext *s = ctx->internal->priv;
831 mfxFrameSurface1 in = {{ 0 }};
832 mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
834 mfxSyncPoint sync = NULL;
838 while (!s->session_upload_init && !s->session_upload && !ret) {
840 if (pthread_mutex_trylock(&s->session_lock) == 0) {
842 if (!s->session_upload_init) {
843 ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
844 if (s->session_upload)
845 s->session_upload_init = 1;
848 pthread_mutex_unlock(&s->session_lock);
849 pthread_cond_signal(&s->session_cond);
851 pthread_mutex_lock(&s->session_lock);
852 while (!s->session_upload_init && !s->session_upload) {
853 pthread_cond_wait(&s->session_cond, &s->session_lock);
855 pthread_mutex_unlock(&s->session_lock);
862 if (!s->session_upload) {
863 if (s->child_frames_ref)
864 return qsv_transfer_data_child(ctx, dst, src);
866 av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
867 return AVERROR(ENOSYS);
871 in.Data.PitchLow = src->linesize[0];
872 in.Data.Y = src->data[0];
873 in.Data.U = src->data[1];
874 in.Data.V = src->data[2];
875 in.Data.A = src->data[3];
878 err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
879 if (err == MFX_WRN_DEVICE_BUSY)
881 } while (err == MFX_WRN_DEVICE_BUSY);
883 if (err < 0 || !sync) {
884 av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
885 return AVERROR_UNKNOWN;
889 err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
890 } while (err == MFX_WRN_IN_EXECUTION);
892 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
893 return AVERROR_UNKNOWN;
899 static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx,
900 AVHWFramesContext *src_ctx, int flags)
902 QSVFramesContext *s = dst_ctx->internal->priv;
903 AVQSVFramesContext *dst_hwctx = dst_ctx->hwctx;
906 switch (src_ctx->device_ctx->type) {
908 case AV_HWDEVICE_TYPE_VAAPI:
910 AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
911 s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
912 sizeof(*s->surfaces_internal));
913 if (!s->surfaces_internal)
914 return AVERROR(ENOMEM);
915 for (i = 0; i < src_hwctx->nb_surfaces; i++) {
916 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
917 s->surfaces_internal[i].Data.MemId = src_hwctx->surface_ids + i;
919 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
920 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
925 case AV_HWDEVICE_TYPE_DXVA2:
927 AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
928 s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
929 sizeof(*s->surfaces_internal));
930 if (!s->surfaces_internal)
931 return AVERROR(ENOMEM);
932 for (i = 0; i < src_hwctx->nb_surfaces; i++) {
933 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
934 s->surfaces_internal[i].Data.MemId = (mfxMemId)src_hwctx->surfaces[i];
936 dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
937 if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
938 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
940 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
945 return AVERROR(ENOSYS);
948 dst_hwctx->surfaces = s->surfaces_internal;
953 static int qsv_map_to(AVHWFramesContext *dst_ctx,
954 AVFrame *dst, const AVFrame *src, int flags)
956 AVQSVFramesContext *hwctx = dst_ctx->hwctx;
959 for (i = 0; i < hwctx->nb_surfaces; i++) {
961 if (*(VASurfaceID*)hwctx->surfaces[i].Data.MemId ==
962 (VASurfaceID)(uintptr_t)src->data[3])
966 if ((IDirect3DSurface9*)hwctx->surfaces[i].Data.MemId ==
967 (IDirect3DSurface9*)(uintptr_t)src->data[3])
971 if (i >= hwctx->nb_surfaces) {
972 av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
973 "is not in the mapped frames context.\n");
974 return AVERROR(EINVAL);
977 err = ff_hwframe_map_create(dst->hw_frames_ctx,
978 dst, src, NULL, NULL);
982 dst->width = src->width;
983 dst->height = src->height;
984 dst->data[3] = (uint8_t*)&hwctx->surfaces[i];
989 static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
990 const void *hwconfig,
991 AVHWFramesConstraints *constraints)
995 constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
996 sizeof(*constraints->valid_sw_formats));
997 if (!constraints->valid_sw_formats)
998 return AVERROR(ENOMEM);
1000 for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
1001 constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
1002 constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
1004 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
1005 if (!constraints->valid_hw_formats)
1006 return AVERROR(ENOMEM);
1008 constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
1009 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
1014 static void qsv_device_free(AVHWDeviceContext *ctx)
1016 AVQSVDeviceContext *hwctx = ctx->hwctx;
1017 QSVDevicePriv *priv = ctx->user_opaque;
1020 MFXClose(hwctx->session);
1022 av_buffer_unref(&priv->child_device_ctx);
1026 static mfxIMPL choose_implementation(const char *device)
1028 static const struct {
1032 { "auto", MFX_IMPL_AUTO },
1033 { "sw", MFX_IMPL_SOFTWARE },
1034 { "hw", MFX_IMPL_HARDWARE },
1035 { "auto_any", MFX_IMPL_AUTO_ANY },
1036 { "hw_any", MFX_IMPL_HARDWARE_ANY },
1037 { "hw2", MFX_IMPL_HARDWARE2 },
1038 { "hw3", MFX_IMPL_HARDWARE3 },
1039 { "hw4", MFX_IMPL_HARDWARE4 },
1042 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
1046 for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
1047 if (!strcmp(device, impl_map[i].name)) {
1048 impl = impl_map[i].impl;
1051 if (i == FF_ARRAY_ELEMS(impl_map))
1052 impl = strtol(device, NULL, 0);
1058 static int qsv_device_derive_from_child(AVHWDeviceContext *ctx,
1059 mfxIMPL implementation,
1060 AVHWDeviceContext *child_device_ctx,
1063 AVQSVDeviceContext *hwctx = ctx->hwctx;
1065 mfxVersion ver = { { 3, 1 } };
1067 mfxHandleType handle_type;
1071 switch (child_device_ctx->type) {
1073 case AV_HWDEVICE_TYPE_VAAPI:
1075 AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1076 handle_type = MFX_HANDLE_VA_DISPLAY;
1077 handle = (mfxHDL)child_device_hwctx->display;
1082 case AV_HWDEVICE_TYPE_DXVA2:
1084 AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1085 handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
1086 handle = (mfxHDL)child_device_hwctx->devmgr;
1091 ret = AVERROR(ENOSYS);
1095 err = MFXInit(implementation, &ver, &hwctx->session);
1096 if (err != MFX_ERR_NONE) {
1097 av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
1099 ret = AVERROR_UNKNOWN;
1103 err = MFXQueryVersion(hwctx->session, &ver);
1104 if (err != MFX_ERR_NONE) {
1105 av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: %d.\n", err);
1106 ret = AVERROR_UNKNOWN;
1110 av_log(ctx, AV_LOG_VERBOSE,
1111 "Initialize MFX session: API version is %d.%d, implementation version is %d.%d\n",
1112 MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
1114 MFXClose(hwctx->session);
1116 err = MFXInit(implementation, &ver, &hwctx->session);
1117 if (err != MFX_ERR_NONE) {
1118 av_log(ctx, AV_LOG_ERROR,
1119 "Error initializing an MFX session: %d.\n", err);
1120 ret = AVERROR_UNKNOWN;
1124 err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
1125 if (err != MFX_ERR_NONE) {
1126 av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
1128 ret = AVERROR_UNKNOWN;
1132 ret = MFXQueryVersion(hwctx->session,&ver);
1133 if (ret == MFX_ERR_NONE) {
1134 av_log(ctx, AV_LOG_VERBOSE, "MFX compile/runtime API: %d.%d/%d.%d\n",
1135 MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
1141 MFXClose(hwctx->session);
1145 static int qsv_device_derive(AVHWDeviceContext *ctx,
1146 AVHWDeviceContext *child_device_ctx, int flags)
1148 return qsv_device_derive_from_child(ctx, MFX_IMPL_HARDWARE_ANY,
1149 child_device_ctx, flags);
1152 static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
1153 AVDictionary *opts, int flags)
1155 QSVDevicePriv *priv;
1156 enum AVHWDeviceType child_device_type;
1157 AVHWDeviceContext *child_device;
1158 AVDictionaryEntry *e;
1163 priv = av_mallocz(sizeof(*priv));
1165 return AVERROR(ENOMEM);
1167 ctx->user_opaque = priv;
1168 ctx->free = qsv_device_free;
1170 e = av_dict_get(opts, "child_device", NULL, 0);
1173 child_device_type = AV_HWDEVICE_TYPE_VAAPI;
1174 else if (CONFIG_DXVA2)
1175 child_device_type = AV_HWDEVICE_TYPE_DXVA2;
1177 av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
1178 return AVERROR(ENOSYS);
1181 ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
1182 e ? e->value : NULL, NULL, 0);
1186 child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
1188 impl = choose_implementation(device);
1190 return qsv_device_derive_from_child(ctx, impl, child_device, 0);
1193 const HWContextType ff_hwcontext_type_qsv = {
1194 .type = AV_HWDEVICE_TYPE_QSV,
1197 .device_hwctx_size = sizeof(AVQSVDeviceContext),
1198 .device_priv_size = sizeof(QSVDeviceContext),
1199 .frames_hwctx_size = sizeof(AVQSVFramesContext),
1200 .frames_priv_size = sizeof(QSVFramesContext),
1202 .device_create = qsv_device_create,
1203 .device_derive = qsv_device_derive,
1204 .device_init = qsv_device_init,
1205 .frames_get_constraints = qsv_frames_get_constraints,
1206 .frames_init = qsv_frames_init,
1207 .frames_uninit = qsv_frames_uninit,
1208 .frames_get_buffer = qsv_get_buffer,
1209 .transfer_get_formats = qsv_transfer_get_formats,
1210 .transfer_data_to = qsv_transfer_data_to,
1211 .transfer_data_from = qsv_transfer_data_from,
1212 .map_to = qsv_map_to,
1213 .map_from = qsv_map_from,
1214 .frames_derive_to = qsv_frames_derive_to,
1215 .frames_derive_from = qsv_frames_derive_from,
1217 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },