static GstMfxFilterStatus gst_mfx_filter_start (GstMfxFilter * filter) { mfxStatus sts = MFX_ERR_NONE; gboolean memtype_is_system; mfxFrameAllocRequest *request; /* Get updated video params if modified by peer MFX element*/ gst_mfx_task_update_video_params (filter->vpp[1], &filter->params); memtype_is_system = !(filter->params.IOPattern & MFX_IOPATTERN_OUT_VIDEO_MEMORY); request = gst_mfx_task_get_request (filter->vpp[1]); if (!request) { GST_ERROR ("Unable to retrieve task parameters from VPP allocation request."); return GST_MFX_FILTER_STATUS_ERROR_INVALID_PARAMETER; } memcpy (filter->shared_request[1], request, sizeof(mfxFrameAllocRequest)); if (!memtype_is_system) { gst_mfx_task_use_video_memory (filter->vpp[1]); sts = gst_mfx_task_frame_alloc (filter->vpp[1], filter->shared_request[1], &filter->response[1]); if (MFX_ERR_NONE != sts) return GST_MFX_FILTER_STATUS_ERROR_ALLOCATION_FAILED; } else { gst_mfx_task_ensure_memtype_is_system (filter->vpp[1]); } filter->vpp_pool[1] = gst_mfx_surface_pool_new_with_task (filter->vpp[1]); if (!filter->vpp_pool[1]) return GST_MFX_FILTER_STATUS_ERROR_ALLOCATION_FAILED; sts = MFXVideoVPP_Init (filter->session, &filter->params); if (sts < 0) { GST_ERROR ("Error initializing MFX VPP %d", sts); return GST_MFX_FILTER_STATUS_ERROR_OPERATION_FAILED; } GST_INFO ("Initialized MFX VPP output task using %s memory", memtype_is_system ? "system" : "video"); return GST_MFX_FILTER_STATUS_SUCCESS; }
static gboolean gst_msdkvpp_initialize (GstMsdkVPP * thiz) { mfxSession session; mfxStatus status; mfxFrameAllocRequest request[2]; if (!thiz->context) { GST_WARNING_OBJECT (thiz, "No MSDK Context"); return FALSE; } GST_OBJECT_LOCK (thiz); session = gst_msdk_context_get_session (thiz->context); if (thiz->use_video_memory) { gst_msdk_set_frame_allocator (thiz->context); thiz->param.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY; } else { thiz->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY | MFX_IOPATTERN_OUT_SYSTEM_MEMORY; } /* update input video attributes */ gst_msdk_set_mfx_frame_info_from_video_info (&thiz->param.vpp.In, &thiz->sinkpad_info); /* update output video attributes, only CSC and Scaling are supported for now */ gst_msdk_set_mfx_frame_info_from_video_info (&thiz->param.vpp.Out, &thiz->srcpad_info); /* use msdk frame rarte control if there is a mismatch in In & OUt fps */ if (GST_VIDEO_INFO_FPS_N (&thiz->srcpad_info) && (GST_VIDEO_INFO_FPS_N (&thiz->sinkpad_info) != GST_VIDEO_INFO_FPS_N (&thiz->srcpad_info) || GST_VIDEO_INFO_FPS_D (&thiz->sinkpad_info) != GST_VIDEO_INFO_FPS_D (&thiz->srcpad_info))) { thiz->flags |= GST_MSDK_FLAG_FRC; /* So far this is the only algorithm which is working somewhat good */ thiz->frc_algm = MFX_FRCALGM_PRESERVE_TIMESTAMP; } /* work-around to avoid zero fps in msdk structure */ if (!thiz->param.vpp.In.FrameRateExtN) thiz->param.vpp.In.FrameRateExtN = 30; if (!thiz->param.vpp.Out.FrameRateExtN) thiz->param.vpp.Out.FrameRateExtN = thiz->param.vpp.In.FrameRateExtN; /* set vpp out picstruct as progressive if deinterlacing enabled */ if (thiz->flags & GST_MSDK_FLAG_DEINTERLACE) thiz->param.vpp.Out.PicStruct = MFX_PICSTRUCT_PROGRESSIVE; /* Enable the required filters */ ensure_filters (thiz); /* Add exteneded buffers */ if (thiz->num_extra_params) { thiz->param.NumExtParam = thiz->num_extra_params; thiz->param.ExtParam = thiz->extra_params; } /* validate parameters and allow the Media SDK to make adjustments */ status = MFXVideoVPP_Query (session, &thiz->param, &thiz->param); if (status < MFX_ERR_NONE) { GST_ERROR_OBJECT (thiz, "Video VPP Query failed (%s)", msdk_status_to_string (status)); goto no_vpp; } else if (status > MFX_ERR_NONE) { GST_WARNING_OBJECT (thiz, "Video VPP Query returned: %s", msdk_status_to_string (status)); } status = MFXVideoVPP_QueryIOSurf (session, &thiz->param, request); if (status < MFX_ERR_NONE) { GST_ERROR_OBJECT (thiz, "VPP Query IO surfaces failed (%s)", msdk_status_to_string (status)); goto no_vpp; } else if (status > MFX_ERR_NONE) { GST_WARNING_OBJECT (thiz, "VPP Query IO surfaces returned: %s", msdk_status_to_string (status)); } if (thiz->use_video_memory) { /* Input surface pool pre-allocation */ request[0].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET; if (thiz->use_sinkpad_dmabuf) request[0].Type |= MFX_MEMTYPE_EXPORT_FRAME; gst_msdk_frame_alloc (thiz->context, &(request[0]), &thiz->in_alloc_resp); /* Output surface pool pre-allocation */ request[1].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET; if (thiz->use_srcpad_dmabuf) request[1].Type |= MFX_MEMTYPE_EXPORT_FRAME; gst_msdk_frame_alloc (thiz->context, &(request[1]), &thiz->out_alloc_resp); } thiz->in_num_surfaces = request[0].NumFrameSuggested; thiz->out_num_surfaces = request[1].NumFrameSuggested; status = MFXVideoVPP_Init (session, &thiz->param); if (status < MFX_ERR_NONE) { GST_ERROR_OBJECT (thiz, "Init failed (%s)", msdk_status_to_string (status)); goto no_vpp; } else if (status > MFX_ERR_NONE) { GST_WARNING_OBJECT (thiz, "Init returned: %s", msdk_status_to_string (status)); } thiz->initialized = TRUE; GST_OBJECT_UNLOCK (thiz); return TRUE; no_vpp: GST_OBJECT_UNLOCK (thiz); if (thiz->context) gst_object_replace ((GstObject **) & thiz->context, NULL); return FALSE; }
static int init_out_session(AVFilterContext *ctx) { QSVScaleContext *s = ctx->priv; AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data; AVHWFramesContext *out_frames_ctx = (AVHWFramesContext*)ctx->outputs[0]->hw_frames_ctx->data; AVQSVFramesContext *in_frames_hwctx = in_frames_ctx->hwctx; AVQSVFramesContext *out_frames_hwctx = out_frames_ctx->hwctx; AVQSVDeviceContext *device_hwctx = in_frames_ctx->device_ctx->hwctx; int opaque = !!(in_frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME); mfxHDL handle = NULL; mfxHandleType handle_type; mfxVersion ver; mfxIMPL impl; mfxVideoParam par; mfxStatus err; int i; /* extract the properties of the "master" session given to us */ err = MFXQueryIMPL(device_hwctx->session, &impl); if (err == MFX_ERR_NONE) err = MFXQueryVersion(device_hwctx->session, &ver); if (err != MFX_ERR_NONE) { av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n"); return AVERROR_UNKNOWN; } for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) { err = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle); if (err == MFX_ERR_NONE) { handle_type = handle_types[i]; break; } } /* create a "slave" session with those same properties, to be used for * actual scaling */ err = MFXInit(impl, &ver, &s->session); if (err != MFX_ERR_NONE) { av_log(ctx, AV_LOG_ERROR, "Error initializing a session for scaling\n"); return AVERROR_UNKNOWN; } if (handle) { err = MFXVideoCORE_SetHandle(s->session, handle_type, handle); if (err != MFX_ERR_NONE) return AVERROR_UNKNOWN; } memset(&par, 0, sizeof(par)); if (opaque) { s->surface_ptrs_in = av_mallocz_array(in_frames_hwctx->nb_surfaces, sizeof(*s->surface_ptrs_in)); if (!s->surface_ptrs_in) return AVERROR(ENOMEM); for (i = 0; i < in_frames_hwctx->nb_surfaces; i++) s->surface_ptrs_in[i] = in_frames_hwctx->surfaces + i; s->nb_surface_ptrs_in = in_frames_hwctx->nb_surfaces; s->surface_ptrs_out = av_mallocz_array(out_frames_hwctx->nb_surfaces, sizeof(*s->surface_ptrs_out)); if (!s->surface_ptrs_out) return AVERROR(ENOMEM); for (i = 0; i < out_frames_hwctx->nb_surfaces; i++) s->surface_ptrs_out[i] = out_frames_hwctx->surfaces + i; s->nb_surface_ptrs_out = out_frames_hwctx->nb_surfaces; s->opaque_alloc.In.Surfaces = s->surface_ptrs_in; s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs_in; s->opaque_alloc.In.Type = in_frames_hwctx->frame_type; s->opaque_alloc.Out.Surfaces = s->surface_ptrs_out; s->opaque_alloc.Out.NumSurface = s->nb_surface_ptrs_out; s->opaque_alloc.Out.Type = out_frames_hwctx->frame_type; s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION; s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc); s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc; par.ExtParam = s->ext_buffers; par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers); par.IOPattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY | MFX_IOPATTERN_OUT_OPAQUE_MEMORY; } else { mfxFrameAllocator frame_allocator = { .pthis = ctx, .Alloc = frame_alloc, .Lock = frame_lock, .Unlock = frame_unlock, .GetHDL = frame_get_hdl, .Free = frame_free, }; s->mem_ids_in = av_mallocz_array(in_frames_hwctx->nb_surfaces, sizeof(*s->mem_ids_in)); if (!s->mem_ids_in) return AVERROR(ENOMEM); for (i = 0; i < in_frames_hwctx->nb_surfaces; i++) s->mem_ids_in[i] = in_frames_hwctx->surfaces[i].Data.MemId; s->nb_mem_ids_in = in_frames_hwctx->nb_surfaces; s->mem_ids_out = av_mallocz_array(out_frames_hwctx->nb_surfaces, sizeof(*s->mem_ids_out)); if (!s->mem_ids_out) return AVERROR(ENOMEM); for (i = 0; i < out_frames_hwctx->nb_surfaces; i++) s->mem_ids_out[i] = out_frames_hwctx->surfaces[i].Data.MemId; s->nb_mem_ids_out = out_frames_hwctx->nb_surfaces; err = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator); if (err != MFX_ERR_NONE) return AVERROR_UNKNOWN; par.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY; } par.AsyncDepth = 1; // TODO async par.vpp.In = in_frames_hwctx->surfaces[0].Info; par.vpp.Out = out_frames_hwctx->surfaces[0].Info; /* Apparently VPP requires the frame rate to be set to some value, otherwise * init will fail (probably for the framerate conversion filter). Since we * are only doing scaling here, we just invent an arbitrary * value */ par.vpp.In.FrameRateExtN = 25; par.vpp.In.FrameRateExtD = 1; par.vpp.Out.FrameRateExtN = 25; par.vpp.Out.FrameRateExtD = 1; err = MFXVideoVPP_Init(s->session, &par); if (err != MFX_ERR_NONE) { av_log(ctx, AV_LOG_ERROR, "Error opening the VPP for scaling\n"); return AVERROR_UNKNOWN; } return 0; }
static int qsv_init_internal_session(AVHWFramesContext *ctx, mfxSession *session, int upload) { QSVFramesContext *s = ctx->internal->priv; AVQSVFramesContext *frames_hwctx = ctx->hwctx; QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv; int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME); mfxFrameAllocator frame_allocator = { .pthis = ctx, .Alloc = frame_alloc, .Lock = frame_lock, .Unlock = frame_unlock, .GetHDL = frame_get_hdl, .Free = frame_free, }; mfxVideoParam par; mfxStatus err; err = MFXInit(device_priv->impl, &device_priv->ver, session); if (err != MFX_ERR_NONE) { av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n"); return AVERROR_UNKNOWN; } if (device_priv->handle) { err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type, device_priv->handle); if (err != MFX_ERR_NONE) return AVERROR_UNKNOWN; } if (!opaque) { err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator); if (err != MFX_ERR_NONE) return AVERROR_UNKNOWN; } memset(&par, 0, sizeof(par)); if (opaque) { par.ExtParam = s->ext_buffers; par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers); par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY : MFX_IOPATTERN_IN_OPAQUE_MEMORY; } else { par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY : MFX_IOPATTERN_IN_VIDEO_MEMORY; } par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY : MFX_IOPATTERN_OUT_SYSTEM_MEMORY; par.AsyncDepth = 1; par.vpp.In = frames_hwctx->surfaces[0].Info; /* Apparently VPP requires the frame rate to be set to some value, otherwise * init will fail (probably for the framerate conversion filter). Since we * are only doing data upload/download here, we just invent an arbitrary * value */ par.vpp.In.FrameRateExtN = 25; par.vpp.In.FrameRateExtD = 1; par.vpp.Out = par.vpp.In; err = MFXVideoVPP_Init(*session, &par); if (err != MFX_ERR_NONE) { av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session." "Surface upload/download will not be possible\n"); MFXClose(*session); *session = NULL; } return 0; }