1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
5 * Author: Sreerenj Balachaandran <sreerenj.balachandran@intel.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
17 * 3. Neither the name of the copyright holder nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "gstmsdkvpp.h"
41 #include "gstmsdkbufferpool.h"
42 #include "gstmsdkvideomemory.h"
43 #include "gstmsdksystemmemory.h"
44 #include "gstmsdkcontextutil.h"
45 #include "gstmsdkvpputil.h"
46 #include "msdk-enums.h"
48 GST_DEBUG_CATEGORY_EXTERN (gst_msdkvpp_debug);
49 #define GST_CAT_DEFAULT gst_msdkvpp_debug
51 static GstStaticPadTemplate gst_msdkvpp_sink_factory =
52 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ NV12, I420, YUY2, UYVY, BGRA }")
56 ", " "interlace-mode = (string){ progressive, interleaved, mixed }"));
58 static GstStaticPadTemplate gst_msdkvpp_src_factory =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ NV12, BGRA }") ", "
63 "interlace-mode = (string){ progressive, interleaved, mixed }"));
75 #define PROP_HARDWARE_DEFAULT TRUE
76 #define PROP_ASYNC_DEPTH_DEFAULT 1
77 #define PROP_DENOISE_DEFAULT 0
78 #define PROP_ROTATION_DEFAULT MFX_ANGLE_0
80 #define gst_msdkvpp_parent_class parent_class
81 G_DEFINE_TYPE (GstMsdkVPP, gst_msdkvpp, GST_TYPE_BASE_TRANSFORM);
85 mfxFrameSurface1 *surface;
90 free_msdk_surface (MsdkSurface * surface)
93 gst_buffer_unref (surface->buf);
94 g_slice_free (MsdkSurface, surface);
98 gst_msdkvpp_add_extra_param (GstMsdkVPP * thiz, mfxExtBuffer * param)
100 if (thiz->num_extra_params < MAX_EXTRA_PARAMS) {
101 thiz->extra_params[thiz->num_extra_params] = param;
102 thiz->num_extra_params++;
107 ensure_context (GstBaseTransform * trans)
109 GstMsdkVPP *thiz = GST_MSDKVPP (trans);
111 if (gst_msdk_context_prepare (GST_ELEMENT_CAST (thiz), &thiz->context)) {
112 GST_INFO_OBJECT (thiz, "Found context from neighbour %" GST_PTR_FORMAT,
115 if (gst_msdk_context_get_job_type (thiz->context) & GST_MSDK_JOB_VPP) {
116 GstMsdkContext *parent_context;
118 parent_context = thiz->context;
119 thiz->context = gst_msdk_context_new_with_parent (parent_context);
120 gst_object_unref (parent_context);
122 GST_INFO_OBJECT (thiz,
123 "Creating new context %" GST_PTR_FORMAT " with joined session",
126 gst_msdk_context_add_job_type (thiz->context, GST_MSDK_JOB_VPP);
129 if (!gst_msdk_context_ensure_context (GST_ELEMENT_CAST (thiz),
130 thiz->hardware, GST_MSDK_JOB_VPP))
132 GST_INFO_OBJECT (thiz, "Creating new context %" GST_PTR_FORMAT,
136 gst_msdk_context_add_shared_async_depth (thiz->context, thiz->async_depth);
142 create_output_buffer (GstMsdkVPP * thiz)
146 GstBufferPool *pool = thiz->srcpad_buffer_pool;
148 g_return_val_if_fail (pool != NULL, NULL);
150 if (!gst_buffer_pool_is_active (pool) &&
151 !gst_buffer_pool_set_active (pool, TRUE))
152 goto error_activate_pool;
155 ret = gst_buffer_pool_acquire_buffer (pool, &outbuf, NULL);
156 if (ret != GST_FLOW_OK || !outbuf)
157 goto error_create_buffer;
164 GST_ERROR_OBJECT (thiz, "failed to activate output video buffer pool");
169 GST_ERROR_OBJECT (thiz, "failed to create output video buffer");
175 gst_msdkvpp_prepare_output_buffer (GstBaseTransform * trans,
176 GstBuffer * inbuf, GstBuffer ** outbuf_ptr)
178 GstMsdkVPP *thiz = GST_MSDKVPP (trans);
180 if (gst_base_transform_is_passthrough (trans)) {
185 *outbuf_ptr = create_output_buffer (thiz);
186 return *outbuf_ptr ? GST_FLOW_OK : GST_FLOW_ERROR;
189 static GstBufferPool *
190 gst_msdkvpp_create_buffer_pool (GstMsdkVPP * thiz, GstPadDirection direction,
191 GstCaps * caps, guint min_num_buffers)
193 GstBufferPool *pool = NULL;
194 GstStructure *config;
195 GstAllocator *allocator = NULL;
197 GstVideoInfo *pool_info = NULL;
198 GstVideoAlignment align;
199 GstAllocationParams params = { 0, 31, 0, 0, };
200 mfxFrameAllocResponse *alloc_resp = NULL;
202 if (direction == GST_PAD_SINK) {
203 alloc_resp = &thiz->in_alloc_resp;
204 pool_info = &thiz->sinkpad_buffer_pool_info;
205 } else if (direction == GST_PAD_SRC) {
206 alloc_resp = &thiz->out_alloc_resp;
207 pool_info = &thiz->srcpad_buffer_pool_info;
210 pool = gst_msdk_buffer_pool_new (thiz->context, alloc_resp);
214 if (!gst_video_info_from_caps (&info, caps))
215 goto error_no_video_info;
217 gst_msdk_set_video_alignment (&info, &align);
218 gst_video_info_align (&info, &align);
220 if (thiz->use_video_memory)
221 allocator = gst_msdk_video_allocator_new (thiz->context, &info, alloc_resp);
223 allocator = gst_msdk_system_allocator_new (&info);
226 goto error_no_allocator;
228 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
229 gst_buffer_pool_config_set_params (config, caps, info.size, min_num_buffers,
232 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
233 gst_buffer_pool_config_add_option (config,
234 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
235 if (thiz->use_video_memory)
236 gst_buffer_pool_config_add_option (config,
237 GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY);
239 gst_buffer_pool_config_set_video_alignment (config, &align);
240 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
241 gst_object_unref (allocator);
243 if (!gst_buffer_pool_set_config (pool, config))
244 goto error_pool_config;
246 /* Updating pool_info with algined info of allocator */
253 GST_INFO_OBJECT (thiz, "Failed to create bufferpool");
258 GST_INFO_OBJECT (thiz, "Failed to get Video info from caps");
263 GST_INFO_OBJECT (thiz, "Failed to create allocator");
265 gst_object_unref (pool);
270 GST_INFO_OBJECT (thiz, "Failed to set config");
272 gst_object_unref (pool);
274 gst_object_unref (allocator);
280 gst_msdkvpp_decide_allocation (GstBaseTransform * trans, GstQuery * query)
282 GstMsdkVPP *thiz = GST_MSDKVPP (trans);
284 GstBufferPool *pool = NULL;
285 GstStructure *config = NULL;
287 guint size = 0, min_buffers = 0, max_buffers = 0;
288 GstAllocator *allocator = NULL;
289 GstAllocationParams params;
290 gboolean update_pool = FALSE;
292 gst_query_parse_allocation (query, &caps, NULL);
294 GST_ERROR_OBJECT (thiz, "Failed to parse the decide_allocation caps");
297 if (!gst_video_info_from_caps (&info, caps)) {
298 GST_ERROR_OBJECT (thiz, "Failed to get video info");
302 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL))
303 thiz->add_video_meta = TRUE;
305 thiz->add_video_meta = FALSE;
307 if (gst_query_get_n_allocation_pools (query) > 0) {
308 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min_buffers,
311 size = MAX (size, GST_VIDEO_INFO_SIZE (&info));
313 if (pool && !GST_IS_MSDK_BUFFER_POOL (pool)) {
314 GST_INFO_OBJECT (thiz, "ignoring non-msdk pool: %" GST_PTR_FORMAT, pool);
315 g_clear_object (&pool);
320 gst_object_unref (thiz->srcpad_buffer_pool);
322 gst_msdkvpp_create_buffer_pool (thiz, GST_PAD_SRC, caps, min_buffers);
323 thiz->srcpad_buffer_pool = pool;
325 /* get the configured pool properties inorder to set in query */
326 config = gst_buffer_pool_get_config (pool);
327 gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
329 if (gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
330 gst_query_add_allocation_param (query, allocator, ¶ms);
331 gst_structure_free (config);
335 gst_query_set_nth_allocation_pool (query, 0, pool, size, min_buffers,
338 gst_query_add_allocation_pool (query, pool, size, min_buffers, max_buffers);
340 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
342 /* Fixme if downstream doesn't have videometa support, msdkvpp should
343 * copy the output buffers */
349 gst_msdkvpp_propose_allocation (GstBaseTransform * trans,
350 GstQuery * decide_query, GstQuery * query)
352 GstMsdkVPP *thiz = GST_MSDKVPP (trans);
354 GstBufferPool *pool = NULL;
355 GstAllocator *allocator = NULL;
357 GstStructure *config;
360 GstAllocationParams params;
362 gst_query_parse_allocation (query, &caps, &need_pool);
364 GST_ERROR_OBJECT (thiz, "Failed to parse the allocation caps");
368 if (!gst_video_info_from_caps (&info, caps)) {
369 GST_ERROR_OBJECT (thiz, "Failed to get video info");
373 size = MAX (info.size, GST_VIDEO_INFO_SIZE (&thiz->sinkpad_buffer_pool_info));
375 /* We already created a pool while setting the caps
376 * just to make sure the pipeline works even if there is
377 * no allocation query from upstream (theoratical ??).Provide the
378 * same pool in query if required/possible */
379 if (!gst_video_info_is_equal (&thiz->sinkpad_buffer_pool_info, &info)) {
380 gst_object_unref (thiz->sinkpad_buffer_pool);
381 thiz->sinkpad_buffer_pool =
382 gst_msdkvpp_create_buffer_pool (thiz, GST_PAD_SINK, caps,
383 thiz->in_num_surfaces);
386 pool = thiz->sinkpad_buffer_pool;
388 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
390 gst_buffer_pool_config_get_params (config, NULL, &size, NULL, NULL);
392 if (gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
393 gst_query_add_allocation_param (query, allocator, ¶ms);
394 gst_structure_free (config);
396 /* if upstream does't have a pool requirement, set only
397 * size, min_buffers and max_buffers in query */
401 gst_query_add_allocation_pool (query, pool, size, thiz->in_num_surfaces, 0);
402 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
404 return GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
405 decide_query, query);
409 get_surface_from_pool (GstMsdkVPP * thiz, GstBufferPool * pool,
410 GstBufferPoolAcquireParams * params)
412 GstBuffer *new_buffer;
413 mfxFrameSurface1 *new_surface;
414 MsdkSurface *msdk_surface;
416 if (!gst_buffer_pool_is_active (pool) &&
417 !gst_buffer_pool_set_active (pool, TRUE)) {
418 GST_ERROR_OBJECT (pool, "failed to activate buffer pool");
422 if (gst_buffer_pool_acquire_buffer (pool, &new_buffer, params) != GST_FLOW_OK) {
423 GST_ERROR_OBJECT (pool, "failed to acquire a buffer from pool");
427 if (gst_msdk_is_msdk_buffer (new_buffer))
428 new_surface = gst_msdk_get_surface_from_buffer (new_buffer);
430 GST_ERROR_OBJECT (pool, "the acquired memory is not MSDK memory");
434 msdk_surface = g_slice_new0 (MsdkSurface);
435 msdk_surface->surface = new_surface;
436 msdk_surface->buf = new_buffer;
442 get_msdk_surface_from_input_buffer (GstMsdkVPP * thiz, GstBuffer * inbuf)
444 GstVideoFrame src_frame, out_frame;
445 MsdkSurface *msdk_surface;
447 if (gst_msdk_is_msdk_buffer (inbuf)) {
448 msdk_surface = g_slice_new0 (MsdkSurface);
449 msdk_surface->surface = gst_msdk_get_surface_from_buffer (inbuf);
450 msdk_surface->buf = gst_buffer_ref (inbuf);
454 /* If upstream hasn't accpeted the proposed msdk bufferpool,
455 * just copy frame to msdk buffer and take a surface from it.
458 get_surface_from_pool (thiz, thiz->sinkpad_buffer_pool, NULL)))
461 if (!gst_video_frame_map (&src_frame, &thiz->sinkpad_info, inbuf,
463 GST_ERROR_OBJECT (thiz, "failed to map the frame for source");
467 if (!gst_video_frame_map (&out_frame, &thiz->sinkpad_buffer_pool_info,
468 msdk_surface->buf, GST_MAP_WRITE)) {
469 GST_ERROR_OBJECT (thiz, "failed to map the frame for destination");
470 gst_video_frame_unmap (&src_frame);
474 if (!gst_video_frame_copy (&out_frame, &src_frame)) {
475 GST_ERROR_OBJECT (thiz, "failed to copy frame");
476 gst_video_frame_unmap (&out_frame);
477 gst_video_frame_unmap (&src_frame);
481 gst_video_frame_unmap (&out_frame);
482 gst_video_frame_unmap (&src_frame);
491 gst_msdkvpp_transform (GstBaseTransform * trans, GstBuffer * inbuf,
494 GstMsdkVPP *thiz = GST_MSDKVPP (trans);
496 mfxSyncPoint sync_point = NULL;
498 MsdkSurface *in_surface = NULL;
499 MsdkSurface *out_surface = NULL;
501 in_surface = get_msdk_surface_from_input_buffer (thiz, inbuf);
503 return GST_FLOW_ERROR;
505 if (gst_msdk_is_msdk_buffer (outbuf)) {
506 out_surface = g_slice_new0 (MsdkSurface);
507 out_surface->surface = gst_msdk_get_surface_from_buffer (outbuf);
509 GST_ERROR ("Failed to get msdk outsurface!");
510 return GST_FLOW_ERROR;
513 session = gst_msdk_context_get_session (thiz->context);
516 MFXVideoVPP_RunFrameVPPAsync (session, in_surface->surface,
517 out_surface->surface, NULL, &sync_point);
518 if (status != MFX_WRN_DEVICE_BUSY)
520 /* If device is busy, wait 1ms and retry, as per MSDK's recomendation */
524 if (status != MFX_ERR_NONE && status != MFX_ERR_MORE_DATA
525 && status != MFX_ERR_MORE_SURFACE)
528 /* No output generated */
529 if (status == MFX_ERR_MORE_DATA)
530 goto error_more_data;
532 MFXVideoCORE_SyncOperation (session, sync_point, 10000);
534 /* More than one output buffers are generated */
535 if (status == MFX_ERR_MORE_SURFACE)
536 status = MFX_ERR_NONE;
538 gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
540 free_msdk_surface (in_surface);
544 GST_ERROR_OBJECT (thiz, "MSDK Failed to do VPP");
545 free_msdk_surface (in_surface);
546 free_msdk_surface (out_surface);
547 return GST_FLOW_ERROR;
550 GST_WARNING_OBJECT (thiz,
551 "MSDK Requries additional input for processing, "
552 "Retruning FLOW_DROPPED since no output buffer was generated");
553 free_msdk_surface (in_surface);
554 return GST_BASE_TRANSFORM_FLOW_DROPPED;
558 gst_msdkvpp_close (GstMsdkVPP * thiz)
565 GST_DEBUG_OBJECT (thiz, "Closing VPP 0x%p", thiz->context);
566 status = MFXVideoVPP_Close (gst_msdk_context_get_session (thiz->context));
567 if (status != MFX_ERR_NONE && status != MFX_ERR_NOT_INITIALIZED) {
568 GST_WARNING_OBJECT (thiz, "Encoder close failed (%s)",
569 msdk_status_to_string (status));
573 gst_object_replace ((GstObject **) & thiz->context, NULL);
575 memset (&thiz->param, 0, sizeof (thiz->param));
577 if (thiz->sinkpad_buffer_pool)
578 gst_object_unref (thiz->sinkpad_buffer_pool);
579 thiz->sinkpad_buffer_pool = NULL;
580 if (thiz->srcpad_buffer_pool)
581 gst_object_unref (thiz->srcpad_buffer_pool);
582 thiz->srcpad_buffer_pool = NULL;
584 gst_video_info_init (&thiz->sinkpad_info);
585 gst_video_info_init (&thiz->srcpad_info);
589 ensure_filters (GstMsdkVPP * thiz)
594 if (thiz->flags & GST_MSDK_FLAG_DENOISE) {
595 mfxExtVPPDenoise *mfx_denoise = &thiz->mfx_denoise;
596 mfx_denoise->Header.BufferId = MFX_EXTBUFF_VPP_DENOISE;
597 mfx_denoise->Header.BufferSz = sizeof (mfxExtVPPDenoise);
598 mfx_denoise->DenoiseFactor = thiz->denoise_factor;
599 gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_denoise);
600 thiz->max_filter_algorithms[n_filters] = MFX_EXTBUFF_VPP_DENOISE;
605 if (thiz->flags & GST_MSDK_FLAG_ROTATION) {
606 mfxExtVPPRotation *mfx_rotation = &thiz->mfx_rotation;
607 mfx_rotation->Header.BufferId = MFX_EXTBUFF_VPP_ROTATION;
608 mfx_rotation->Header.BufferSz = sizeof (mfxExtVPPRotation);
609 mfx_rotation->Angle = thiz->rotation;
610 gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_rotation);
611 thiz->max_filter_algorithms[n_filters] = MFX_EXTBUFF_VPP_ROTATION;
617 mfxExtVPPDoUse *mfx_vpp_douse = &thiz->mfx_vpp_douse;
618 mfx_vpp_douse->Header.BufferId = MFX_EXTBUFF_VPP_DOUSE;
619 mfx_vpp_douse->Header.BufferSz = sizeof (mfxExtVPPDoUse);
620 mfx_vpp_douse->NumAlg = n_filters;
621 mfx_vpp_douse->AlgList = thiz->max_filter_algorithms;
622 gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_vpp_douse);
627 gst_msdkvpp_set_passthrough (GstMsdkVPP * thiz)
629 gboolean passthrough = TRUE;
631 /* no passthrough if any of the filter algorithm is enabled */
635 /* no passthrough if there is change in out width,height or format */
636 if (GST_VIDEO_INFO_WIDTH (&thiz->sinkpad_info) !=
637 GST_VIDEO_INFO_WIDTH (&thiz->srcpad_info)
638 || GST_VIDEO_INFO_HEIGHT (&thiz->sinkpad_info) !=
639 GST_VIDEO_INFO_HEIGHT (&thiz->srcpad_info)
640 || GST_VIDEO_INFO_FORMAT (&thiz->sinkpad_info) !=
641 GST_VIDEO_INFO_FORMAT (&thiz->srcpad_info))
644 GST_OBJECT_UNLOCK (thiz);
645 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (thiz), passthrough);
646 GST_OBJECT_LOCK (thiz);
650 gst_msdkvpp_initialize (GstMsdkVPP * thiz)
654 mfxFrameAllocRequest request[2];
656 if (!thiz->context) {
657 GST_WARNING_OBJECT (thiz, "No MSDK Context");
661 GST_OBJECT_LOCK (thiz);
662 session = gst_msdk_context_get_session (thiz->context);
664 if (thiz->use_video_memory) {
665 gst_msdk_set_frame_allocator (thiz->context);
666 thiz->param.IOPattern =
667 MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY;
669 thiz->param.IOPattern =
670 MFX_IOPATTERN_IN_SYSTEM_MEMORY | MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
673 /* update input video attributes */
674 gst_msdk_set_mfx_frame_info_from_video_info (&thiz->param.vpp.In,
675 &thiz->sinkpad_info);
677 /* update output video attributes, only CSC and Scaling are supported for now */
678 gst_msdk_set_mfx_frame_info_from_video_info (&thiz->param.vpp.Out,
680 thiz->param.vpp.Out.FrameRateExtN =
681 GST_VIDEO_INFO_FPS_N (&thiz->sinkpad_info);
682 thiz->param.vpp.Out.FrameRateExtD =
683 GST_VIDEO_INFO_FPS_D (&thiz->sinkpad_info);
685 /* validate parameters and allow the Media SDK to make adjustments */
686 status = MFXVideoVPP_Query (session, &thiz->param, &thiz->param);
687 if (status < MFX_ERR_NONE) {
688 GST_ERROR_OBJECT (thiz, "Video VPP Query failed (%s)",
689 msdk_status_to_string (status));
691 } else if (status > MFX_ERR_NONE) {
692 GST_WARNING_OBJECT (thiz, "Video VPP Query returned: %s",
693 msdk_status_to_string (status));
696 /* Enable the required filters */
697 ensure_filters (thiz);
699 /* set passthrough according to filter operation change */
700 gst_msdkvpp_set_passthrough (thiz);
702 /* Add exteneded buffers */
703 if (thiz->num_extra_params) {
704 thiz->param.NumExtParam = thiz->num_extra_params;
705 thiz->param.ExtParam = thiz->extra_params;
708 status = MFXVideoVPP_QueryIOSurf (session, &thiz->param, request);
709 if (status < MFX_ERR_NONE) {
710 GST_ERROR_OBJECT (thiz, "VPP Query IO surfaces failed (%s)",
711 msdk_status_to_string (status));
713 } else if (status > MFX_ERR_NONE) {
714 GST_WARNING_OBJECT (thiz, "VPP Query IO surfaces returned: %s",
715 msdk_status_to_string (status));
718 if (thiz->use_video_memory) {
719 /* Input surface pool pre-allocation */
720 gst_msdk_frame_alloc (thiz->context, &(request[0]), &thiz->in_alloc_resp);
721 /* Output surface pool pre-allocation */
722 gst_msdk_frame_alloc (thiz->context, &(request[1]), &thiz->out_alloc_resp);
725 thiz->in_num_surfaces = request[0].NumFrameSuggested;
726 thiz->out_num_surfaces = request[1].NumFrameSuggested;
729 status = MFXVideoVPP_Init (session, &thiz->param);
730 if (status < MFX_ERR_NONE) {
731 GST_ERROR_OBJECT (thiz, "Init failed (%s)", msdk_status_to_string (status));
733 } else if (status > MFX_ERR_NONE) {
734 GST_WARNING_OBJECT (thiz, "Init returned: %s",
735 msdk_status_to_string (status));
738 GST_OBJECT_UNLOCK (thiz);
742 GST_OBJECT_UNLOCK (thiz);
744 gst_object_replace ((GstObject **) & thiz->context, NULL);
749 gst_msdkvpp_set_caps (GstBaseTransform * trans, GstCaps * caps,
752 GstMsdkVPP *thiz = GST_MSDKVPP (trans);
753 GstVideoInfo in_info, out_info;
754 gboolean sinkpad_info_changed = FALSE;
755 gboolean srcpad_info_changed = FALSE;
757 gst_video_info_from_caps (&in_info, caps);
758 gst_video_info_from_caps (&out_info, out_caps);
760 if (!gst_video_info_is_equal (&in_info, &thiz->sinkpad_info))
761 sinkpad_info_changed = TRUE;
762 if (!gst_video_info_is_equal (&out_info, &thiz->srcpad_info))
763 srcpad_info_changed = TRUE;
765 thiz->sinkpad_info = in_info;
766 thiz->srcpad_info = out_info;
768 thiz->use_video_memory = TRUE;
770 thiz->use_video_memory = FALSE;
773 if (!sinkpad_info_changed && !srcpad_info_changed)
776 if (!gst_msdkvpp_initialize (thiz))
779 /* Ensure sinkpad buffer pool */
780 thiz->sinkpad_buffer_pool =
781 gst_msdkvpp_create_buffer_pool (thiz, GST_PAD_SINK, caps,
782 thiz->in_num_surfaces);
783 if (!thiz->sinkpad_buffer_pool) {
784 GST_ERROR_OBJECT (thiz, "Failed to ensure the sinkpad buffer pool");
787 /* Ensure a srcpad buffer pool */
788 thiz->srcpad_buffer_pool =
789 gst_msdkvpp_create_buffer_pool (thiz, GST_PAD_SRC, out_caps,
790 thiz->out_num_surfaces);
791 if (!thiz->srcpad_buffer_pool) {
792 GST_ERROR_OBJECT (thiz, "Failed to ensure the srcpad buffer pool");
800 gst_msdkvpp_fixate_caps (GstBaseTransform * trans,
801 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
803 GstMsdkVPP *thiz = GST_MSDKVPP (trans);
804 GstCaps *result = NULL;
806 if (direction == GST_PAD_SRC)
807 result = gst_caps_fixate (result);
809 result = gst_msdkvpp_fixate_srccaps (thiz, caps, othercaps);
812 GST_DEBUG_OBJECT (trans, "fixated to %" GST_PTR_FORMAT, result);
813 gst_caps_unref (othercaps);
817 /* Generic code for now, requires changes in future when we
818 * add hardware query for supported formats, Framerate control etc */
820 gst_msdkvpp_transform_caps (GstBaseTransform * trans,
821 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
825 GST_DEBUG_OBJECT (trans,
826 "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
827 (direction == GST_PAD_SINK) ? "sink" : "src");
829 if (direction == GST_PAD_SRC)
830 out_caps = gst_static_pad_template_get_caps (&gst_msdkvpp_sink_factory);
832 out_caps = gst_static_pad_template_get_caps (&gst_msdkvpp_src_factory);
834 if (out_caps && filter) {
835 GstCaps *intersection;
837 intersection = gst_caps_intersect_full (out_caps, filter,
838 GST_CAPS_INTERSECT_FIRST);
839 gst_caps_unref (out_caps);
840 out_caps = intersection;
843 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, out_caps);
848 gst_msdkvpp_start (GstBaseTransform * trans)
850 if (!ensure_context (trans))
856 gst_msdkvpp_stop (GstBaseTransform * trans)
858 gst_msdkvpp_close (GST_MSDKVPP (trans));
863 gst_msdkvpp_set_property (GObject * object, guint prop_id,
864 const GValue * value, GParamSpec * pspec)
866 GstMsdkVPP *thiz = GST_MSDKVPP (object);
870 thiz->hardware = g_value_get_boolean (value);
872 case PROP_ASYNC_DEPTH:
873 thiz->async_depth = g_value_get_uint (value);
876 thiz->denoise_factor = g_value_get_uint (value);
877 thiz->flags |= GST_MSDK_FLAG_DENOISE;
880 thiz->rotation = g_value_get_enum (value);
881 thiz->flags |= GST_MSDK_FLAG_ROTATION;
884 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
890 gst_msdkvpp_get_property (GObject * object, guint prop_id,
891 GValue * value, GParamSpec * pspec)
893 GstMsdkVPP *thiz = GST_MSDKVPP (object);
897 g_value_set_boolean (value, thiz->hardware);
899 case PROP_ASYNC_DEPTH:
900 g_value_set_uint (value, thiz->async_depth);
903 g_value_set_uint (value, thiz->denoise_factor);
906 g_value_set_enum (value, thiz->rotation);
909 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
915 gst_msdkvpp_finalize (GObject * object)
917 G_OBJECT_CLASS (parent_class)->finalize (object);
921 gst_msdkvpp_set_context (GstElement * element, GstContext * context)
923 GstMsdkContext *msdk_context = NULL;
924 GstMsdkVPP *thiz = GST_MSDKVPP (element);
926 if (gst_msdk_context_get_context (context, &msdk_context)) {
927 gst_object_replace ((GstObject **) & thiz->context,
928 (GstObject *) msdk_context);
929 gst_object_unref (msdk_context);
932 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
936 gst_msdkvpp_class_init (GstMsdkVPPClass * klass)
938 GObjectClass *gobject_class;
939 GstElementClass *element_class;
940 GstBaseTransformClass *trans_class;
941 GParamSpec *obj_properties[PROP_N] = { NULL, };
943 gobject_class = G_OBJECT_CLASS (klass);
944 element_class = GST_ELEMENT_CLASS (klass);
945 trans_class = GST_BASE_TRANSFORM_CLASS (klass);
947 gobject_class->set_property = gst_msdkvpp_set_property;
948 gobject_class->get_property = gst_msdkvpp_get_property;
949 gobject_class->finalize = gst_msdkvpp_finalize;
951 element_class->set_context = gst_msdkvpp_set_context;
953 gst_element_class_add_static_pad_template (element_class,
954 &gst_msdkvpp_src_factory);
955 gst_element_class_add_static_pad_template (element_class,
956 &gst_msdkvpp_sink_factory);
958 gst_element_class_set_static_metadata (element_class,
959 "MSDK Video Postprocessor",
960 "Filter/Converter/Video;Filter/Converter/Video/Scaler;"
961 "Filter/Effect/Video;Filter/Effect/Video/Deinterlace",
962 "A MediaSDK Video Postprocessing Filter",
963 "Sreerenj Balachandrn <sreerenj.balachandran@intel.com>");
965 trans_class->start = GST_DEBUG_FUNCPTR (gst_msdkvpp_start);
966 trans_class->stop = GST_DEBUG_FUNCPTR (gst_msdkvpp_stop);
967 trans_class->transform_caps = GST_DEBUG_FUNCPTR (gst_msdkvpp_transform_caps);
968 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_msdkvpp_fixate_caps);
969 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_msdkvpp_set_caps);
970 trans_class->transform = GST_DEBUG_FUNCPTR (gst_msdkvpp_transform);
971 trans_class->propose_allocation =
972 GST_DEBUG_FUNCPTR (gst_msdkvpp_propose_allocation);
973 trans_class->decide_allocation =
974 GST_DEBUG_FUNCPTR (gst_msdkvpp_decide_allocation);
975 trans_class->prepare_output_buffer =
976 GST_DEBUG_FUNCPTR (gst_msdkvpp_prepare_output_buffer);
978 obj_properties[PROP_HARDWARE] =
979 g_param_spec_boolean ("hardware", "Hardware", "Enable hardware VPP",
980 PROP_HARDWARE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
982 obj_properties[PROP_ASYNC_DEPTH] =
983 g_param_spec_uint ("async-depth", "Async Depth",
984 "Depth of asynchronous pipeline",
985 1, 1, PROP_ASYNC_DEPTH_DEFAULT,
986 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
988 obj_properties[PROP_DENOISE] =
989 g_param_spec_uint ("denoise", "Denoising factor",
991 0, 100, PROP_DENOISE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
993 obj_properties[PROP_ROTATION] =
994 g_param_spec_enum ("rotation", "Rotation",
995 "Rotation Angle", gst_msdkvpp_rotation_get_type (),
996 PROP_ROTATION_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
998 g_object_class_install_properties (gobject_class, PROP_N, obj_properties);
1002 gst_msdkvpp_init (GstMsdkVPP * thiz)
1004 thiz->hardware = PROP_HARDWARE_DEFAULT;
1005 thiz->async_depth = PROP_ASYNC_DEPTH_DEFAULT;
1006 thiz->denoise_factor = PROP_DENOISE_DEFAULT;
1007 thiz->rotation = PROP_ROTATION_DEFAULT;
1008 gst_video_info_init (&thiz->sinkpad_info);
1009 gst_video_info_init (&thiz->srcpad_info);