• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2018, Intel Corporation
3  * All rights reserved.
4  *
5  * Author: Sreerenj Balachaandran <sreerenj.balachandran@intel.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 /**
35  * SECTION: element-msdkvpp
36  * @title: msdkvpp
37  * @short_description: MSDK Video Postprocessor
38  *
39  * A MediaSDK Video Postprocessing Filter
40  *
41  * ## Example launch line
42  * ```
43  * gst-launch-1.0 videotestsrc ! msdkvpp ! glimagesink
44  * ```
45  *
46  * Since: 1.16
47  *
48  */
49 
50 #ifdef HAVE_CONFIG_H
51 #  include <config.h>
52 #endif
53 
54 #include <stdlib.h>
55 
56 #include "gstmsdkvpp.h"
57 #include "gstmsdkbufferpool.h"
58 #include "gstmsdkvideomemory.h"
59 #include "gstmsdksystemmemory.h"
60 #include "gstmsdkcontextutil.h"
61 #include "gstmsdkvpputil.h"
62 
63 #define EXT_FORMATS     ""
64 
65 #ifndef _WIN32
66 #include "gstmsdkallocator_libva.h"
67 #if VA_CHECK_VERSION(1, 4, 1)
68 #undef EXT_FORMATS
69 #define EXT_FORMATS     ", BGR10A2_LE"
70 #endif
71 #endif
72 
73 #if (MFX_VERSION >= 2004)
74 #define EXT_SINK_FORMATS        ", RGB16, Y410, Y210, P012_LE, Y212_LE, Y412_LE"
75 #define EXT_SRC_FORMATS         ", YV12, Y410, Y210, RGBP, BGRP, P012_LE, Y212_LE, Y412_LE"
76 #elif (MFX_VERSION >= 1032)
77 #define EXT_SINK_FORMATS        ", RGB16, Y410, Y210, P012_LE, Y212_LE, Y412_LE"
78 #define EXT_SRC_FORMATS         ", YV12, Y410, Y210, P012_LE, Y212_LE, Y412_LE"
79 #elif (MFX_VERSION >= 1031)
80 #define EXT_SINK_FORMATS        ", RGB16, Y410, Y210, P012_LE, Y212_LE, Y412_LE"
81 #define EXT_SRC_FORMATS         ", Y410, Y210, P012_LE, Y212_LE, Y412_LE"
82 #elif (MFX_VERSION >= 1028)
83 #define EXT_SINK_FORMATS        ", RGB16, Y410, Y210"
84 #define EXT_SRC_FORMATS         ", Y410, Y210"
85 #elif (MFX_VERSION >= 1027)
86 #define EXT_SINK_FORMATS        ", Y410, Y210"
87 #define EXT_SRC_FORMATS         ", Y410, Y210"
88 #else
89 #define EXT_SINK_FORMATS        ""
90 #define EXT_SRC_FORMATS         ""
91 #endif
92 
93 GST_DEBUG_CATEGORY_EXTERN (gst_msdkvpp_debug);
94 #define GST_CAT_DEFAULT gst_msdkvpp_debug
95 
96 #define SUPPORTED_SYSTEM_FORMAT \
97     "{ NV12, YV12, I420, YUY2, UYVY, VUYA, BGRA, BGRx, P010_10LE" EXT_SINK_FORMATS "}"
98 #define SUPPORTED_DMABUF_FORMAT \
99     "{ NV12, BGRA, YUY2, UYVY, VUYA, P010_10LE" EXT_SINK_FORMATS "}"
100 #define SRC_SYSTEM_FORMAT \
101     "{ NV12, BGRA, YUY2, UYVY, VUYA, BGRx, P010_10LE" EXT_FORMATS EXT_SRC_FORMATS "}"
102 #define SRC_DMABUF_FORMAT       \
103     "{ NV12, BGRA, YUY2, UYVY, VUYA, BGRx, P010_10LE" EXT_FORMATS EXT_SRC_FORMATS "}"
104 
105 #ifndef _WIN32
106 #define DMABUF_SINK_CAPS_STR \
107   GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF, \
108       SUPPORTED_DMABUF_FORMAT)
109 #else
110 #define DMABUF_SINK_CAPS_STR ""
111 #endif
112 
113 #ifndef _WIN32
114 #define DMABUF_SRC_CAPS_STR \
115   GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF, \
116       SRC_DMABUF_FORMAT) ";"
117 #else
118 #define DMABUF_SRC_CAPS_STR ""
119 #endif
120 
121 
122 static GstStaticPadTemplate gst_msdkvpp_sink_factory =
123     GST_STATIC_PAD_TEMPLATE ("sink",
124     GST_PAD_SINK,
125     GST_PAD_ALWAYS,
126     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (SUPPORTED_SYSTEM_FORMAT)
127         ", " "interlace-mode = (string){ progressive, interleaved, mixed }" ";"
128         DMABUF_SINK_CAPS_STR));
129 
130 static GstStaticPadTemplate gst_msdkvpp_src_factory =
131     GST_STATIC_PAD_TEMPLATE ("src",
132     GST_PAD_SRC,
133     GST_PAD_ALWAYS,
134     GST_STATIC_CAPS (DMABUF_SRC_CAPS_STR
135         GST_VIDEO_CAPS_MAKE (SRC_SYSTEM_FORMAT) ", "
136         "interlace-mode = (string){ progressive, interleaved, mixed }" ";"));
137 
138 enum
139 {
140   PROP_0,
141   PROP_HARDWARE,
142   PROP_ASYNC_DEPTH,
143   PROP_DENOISE,
144 #ifndef GST_REMOVE_DEPRECATED
145   PROP_ROTATION,
146 #endif
147   PROP_DEINTERLACE_MODE,
148   PROP_DEINTERLACE_METHOD,
149   PROP_HUE,
150   PROP_SATURATION,
151   PROP_BRIGHTNESS,
152   PROP_CONTRAST,
153   PROP_DETAIL,
154 #ifndef GST_REMOVE_DEPRECATED
155   PROP_MIRRORING,
156 #endif
157   PROP_SCALING_MODE,
158   PROP_FORCE_ASPECT_RATIO,
159   PROP_FRC_ALGORITHM,
160   PROP_VIDEO_DIRECTION,
161   PROP_CROP_LEFT,
162   PROP_CROP_RIGHT,
163   PROP_CROP_TOP,
164   PROP_CROP_BOTTOM,
165   PROP_N,
166 };
167 
168 #define PROP_HARDWARE_DEFAULT            TRUE
169 #define PROP_ASYNC_DEPTH_DEFAULT         1
170 #define PROP_DENOISE_DEFAULT             0
171 #ifndef GST_REMOVE_DEPRECATED
172 #define PROP_ROTATION_DEFAULT            MFX_ANGLE_0
173 #define PROP_MIRRORING_DEFAULT           MFX_MIRRORING_DISABLED
174 #endif
175 #define PROP_DEINTERLACE_MODE_DEFAULT    GST_MSDKVPP_DEINTERLACE_MODE_AUTO
176 #define PROP_DEINTERLACE_METHOD_DEFAULT  MFX_DEINTERLACING_BOB
177 #define PROP_HUE_DEFAULT                 0
178 #define PROP_SATURATION_DEFAULT          1
179 #define PROP_BRIGHTNESS_DEFAULT          0
180 #define PROP_CONTRAST_DEFAULT            1
181 #define PROP_DETAIL_DEFAULT              0
182 #define PROP_SCALING_MODE_DEFAULT        MFX_SCALING_MODE_DEFAULT
183 #define PROP_FORCE_ASPECT_RATIO_DEFAULT  TRUE
184 #define PROP_FRC_ALGORITHM_DEFAULT       _MFX_FRC_ALGORITHM_NONE
185 #define PROP_VIDEO_DIRECTION_DEFAULT     GST_VIDEO_ORIENTATION_IDENTITY
186 #define PROP_CROP_LEFT_DEFAULT           0
187 #define PROP_CROP_RIGHT_DEFAULT          0
188 #define PROP_CROP_TOP_DEFAULT            0
189 #define PROP_CROP_BOTTOM_DEFAULT         0
190 
191 /* 8 should enough for a normal encoder */
192 #define SRC_POOL_SIZE_DEFAULT            8
193 
194 #define gst_msdkvpp_parent_class parent_class
195 G_DEFINE_TYPE (GstMsdkVPP, gst_msdkvpp, GST_TYPE_BASE_TRANSFORM);
196 
197 typedef struct
198 {
199   mfxFrameSurface1 *surface;
200   GstBuffer *buf;
201 } MsdkSurface;
202 
203 static void
free_msdk_surface(gpointer p)204 free_msdk_surface (gpointer p)
205 {
206   MsdkSurface *surface = (MsdkSurface *) p;
207   if (surface->buf)
208     gst_buffer_unref (surface->buf);
209   g_slice_free (MsdkSurface, surface);
210 }
211 
212 static void
release_msdk_surface(GstMsdkVPP * thiz,MsdkSurface * surface,GList ** list)213 release_msdk_surface (GstMsdkVPP * thiz, MsdkSurface * surface, GList ** list)
214 {
215   if (surface->surface) {
216     if (surface->surface->Data.Locked) {
217       *list = g_list_append (*list, surface);
218     } else {
219       free_msdk_surface (surface);
220     }
221   }
222 }
223 
224 static void
release_in_surface(GstMsdkVPP * thiz,MsdkSurface * surface,gboolean locked_by_others)225 release_in_surface (GstMsdkVPP * thiz, MsdkSurface * surface,
226     gboolean locked_by_others)
227 {
228   if (locked_by_others) {
229     /* mfxFrameSurface1 locked by others, others will hold the surface->buf reference */
230     /* we are good to release it here */
231     free_msdk_surface (surface);
232   } else {
233     release_msdk_surface (thiz, surface, &thiz->locked_in_surfaces);
234   }
235 }
236 
237 static void
release_out_surface(GstMsdkVPP * thiz,MsdkSurface * surface)238 release_out_surface (GstMsdkVPP * thiz, MsdkSurface * surface)
239 {
240   release_msdk_surface (thiz, surface, &thiz->locked_out_surfaces);
241 }
242 
243 static void
free_unlocked_msdk_surfaces_from_list(GstMsdkVPP * thiz,GList ** list)244 free_unlocked_msdk_surfaces_from_list (GstMsdkVPP * thiz, GList ** list)
245 {
246   GList *l;
247   MsdkSurface *surface;
248 
249   for (l = *list; l;) {
250     GList *next = l->next;
251     surface = l->data;
252     if (surface->surface->Data.Locked == 0) {
253       free_msdk_surface (surface);
254       *list = g_list_delete_link (*list, l);
255     }
256     l = next;
257   }
258 }
259 
260 static void
free_unlocked_msdk_surfaces(GstMsdkVPP * thiz)261 free_unlocked_msdk_surfaces (GstMsdkVPP * thiz)
262 {
263   free_unlocked_msdk_surfaces_from_list (thiz, &thiz->locked_in_surfaces);
264   free_unlocked_msdk_surfaces_from_list (thiz, &thiz->locked_out_surfaces);
265 }
266 
267 static void
free_all_msdk_surfaces(GstMsdkVPP * thiz)268 free_all_msdk_surfaces (GstMsdkVPP * thiz)
269 {
270   g_list_free_full (thiz->locked_in_surfaces, free_msdk_surface);
271   thiz->locked_in_surfaces = NULL;
272   g_list_free_full (thiz->locked_out_surfaces, free_msdk_surface);
273   thiz->locked_out_surfaces = NULL;
274 }
275 
276 static void
gst_msdkvpp_add_extra_param(GstMsdkVPP * thiz,mfxExtBuffer * param)277 gst_msdkvpp_add_extra_param (GstMsdkVPP * thiz, mfxExtBuffer * param)
278 {
279   if (thiz->num_extra_params < MAX_EXTRA_PARAMS) {
280     thiz->extra_params[thiz->num_extra_params] = param;
281     thiz->num_extra_params++;
282   }
283 }
284 
285 static gboolean
gst_msdkvpp_context_prepare(GstMsdkVPP * thiz)286 gst_msdkvpp_context_prepare (GstMsdkVPP * thiz)
287 {
288   /* Try to find an existing context from the pipeline. This may (indirectly)
289    * invoke gst_msdkvpp_set_context, which will set thiz->context. */
290   if (!gst_msdk_context_find (GST_ELEMENT_CAST (thiz), &thiz->context))
291     return FALSE;
292 
293   if (thiz->context == thiz->old_context) {
294     GST_INFO_OBJECT (thiz, "Found old context %" GST_PTR_FORMAT
295         ", reusing as-is", thiz->context);
296     return TRUE;
297   }
298 
299   GST_INFO_OBJECT (thiz, "Found context %" GST_PTR_FORMAT " from neighbour",
300       thiz->context);
301 
302   /* Check GST_MSDK_JOB_VPP and GST_MSDK_JOB_ENCODER together to avoid sharing context
303    * between VPP and ENCODER
304    * Example:
305    * gst-launch-1.0 videotestsrc ! msdkvpp ! video/x-raw,format=YUY2 ! msdkh264enc ! fakesink
306    */
307   if (!(gst_msdk_context_get_job_type (thiz->context) & (GST_MSDK_JOB_VPP |
308               GST_MSDK_JOB_ENCODER))) {
309     gst_msdk_context_add_job_type (thiz->context, GST_MSDK_JOB_VPP);
310     return TRUE;
311   }
312 
313   /* Found an existing context that's already being used as VPP, so clone the
314    * MFX session inside it to create a new one */
315   {
316     GstMsdkContext *parent_context, *msdk_context;
317 
318     GST_INFO_OBJECT (thiz, "Creating new context %" GST_PTR_FORMAT " with "
319         "joined session", thiz->context);
320     parent_context = thiz->context;
321     msdk_context = gst_msdk_context_new_with_parent (parent_context);
322 
323     if (!msdk_context) {
324       GST_ERROR_OBJECT (thiz, "Failed to create a context with parent context "
325           "as %" GST_PTR_FORMAT, parent_context);
326       return FALSE;
327     }
328 
329     thiz->context = msdk_context;
330     gst_object_unref (parent_context);
331   }
332 
333   return TRUE;
334 }
335 
336 static gboolean
ensure_context(GstBaseTransform * trans)337 ensure_context (GstBaseTransform * trans)
338 {
339   GstMsdkVPP *thiz = GST_MSDKVPP (trans);
340 
341   if (!gst_msdkvpp_context_prepare (thiz)) {
342     if (!gst_msdk_context_ensure_context (GST_ELEMENT_CAST (thiz),
343             thiz->hardware, GST_MSDK_JOB_VPP))
344       return FALSE;
345     GST_INFO_OBJECT (thiz, "Creating new context %" GST_PTR_FORMAT,
346         thiz->context);
347   }
348 
349   /* Save the current context in a separate field so that we know whether it
350    * has changed between calls to _start() */
351   gst_object_replace ((GstObject **) & thiz->old_context,
352       (GstObject *) thiz->context);
353 
354   gst_msdk_context_add_shared_async_depth (thiz->context, thiz->async_depth);
355 
356   return TRUE;
357 }
358 
359 static GstBuffer *
create_output_buffer(GstMsdkVPP * thiz)360 create_output_buffer (GstMsdkVPP * thiz)
361 {
362   GstBuffer *outbuf;
363   GstFlowReturn ret;
364   GstBufferPool *pool = thiz->srcpad_buffer_pool;
365 
366   g_return_val_if_fail (pool != NULL, NULL);
367 
368   if (!gst_buffer_pool_is_active (pool) &&
369       !gst_buffer_pool_set_active (pool, TRUE))
370     goto error_activate_pool;
371 
372   outbuf = NULL;
373   ret = gst_buffer_pool_acquire_buffer (pool, &outbuf, NULL);
374   if (ret != GST_FLOW_OK || !outbuf)
375     goto error_create_buffer;
376 
377   return outbuf;
378 
379   /* ERRORS */
380 error_activate_pool:
381   {
382     GST_ERROR_OBJECT (thiz, "failed to activate output video buffer pool");
383     return NULL;
384   }
385 error_create_buffer:
386   {
387     GST_ERROR_OBJECT (thiz, "failed to create output video buffer");
388     return NULL;
389   }
390 }
391 
392 static GstFlowReturn
gst_msdkvpp_prepare_output_buffer(GstBaseTransform * trans,GstBuffer * inbuf,GstBuffer ** outbuf_ptr)393 gst_msdkvpp_prepare_output_buffer (GstBaseTransform * trans,
394     GstBuffer * inbuf, GstBuffer ** outbuf_ptr)
395 {
396   GstMsdkVPP *thiz = GST_MSDKVPP (trans);
397 
398   if (gst_base_transform_is_passthrough (trans)) {
399     *outbuf_ptr = inbuf;
400     return GST_FLOW_OK;
401   }
402 
403   *outbuf_ptr = create_output_buffer (thiz);
404   return *outbuf_ptr ? GST_FLOW_OK : GST_FLOW_ERROR;
405 }
406 
407 static GstBufferPool *
gst_msdkvpp_create_buffer_pool(GstMsdkVPP * thiz,GstPadDirection direction,GstCaps * caps,guint min_num_buffers)408 gst_msdkvpp_create_buffer_pool (GstMsdkVPP * thiz, GstPadDirection direction,
409     GstCaps * caps, guint min_num_buffers)
410 {
411   GstBufferPool *pool = NULL;
412   GstStructure *config;
413   GstAllocator *allocator = NULL;
414   GstVideoInfo info;
415   GstVideoInfo *pool_info = NULL;
416   GstVideoAlignment align;
417   GstAllocationParams params = { 0, 31, 0, 0, };
418   mfxFrameAllocResponse *alloc_resp = NULL;
419   gboolean use_dmabuf = FALSE;
420 
421   if (direction == GST_PAD_SINK) {
422     alloc_resp = &thiz->in_alloc_resp;
423     pool_info = &thiz->sinkpad_buffer_pool_info;
424     use_dmabuf = thiz->use_sinkpad_dmabuf;
425   } else if (direction == GST_PAD_SRC) {
426     alloc_resp = &thiz->out_alloc_resp;
427     pool_info = &thiz->srcpad_buffer_pool_info;
428     use_dmabuf = thiz->use_srcpad_dmabuf;
429   }
430 
431   pool = gst_msdk_buffer_pool_new (thiz->context, alloc_resp);
432   if (!pool)
433     goto error_no_pool;
434 
435   if (!gst_video_info_from_caps (&info, caps))
436     goto error_no_video_info;
437 
438   gst_msdk_set_video_alignment (&info, 0, 0, &align);
439   gst_video_info_align (&info, &align);
440 
441   if (use_dmabuf)
442     allocator =
443         gst_msdk_dmabuf_allocator_new (thiz->context, &info, alloc_resp);
444   else if (thiz->use_video_memory)
445     allocator = gst_msdk_video_allocator_new (thiz->context, &info, alloc_resp);
446   else
447     allocator = gst_msdk_system_allocator_new (&info);
448 
449   if (!allocator)
450     goto error_no_allocator;
451 
452   config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
453   /* we do not support dynamic buffer count change */
454   gst_buffer_pool_config_set_params (config, caps, info.size, min_num_buffers,
455       min_num_buffers);
456 
457   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
458   gst_buffer_pool_config_add_option (config,
459       GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
460   if (thiz->use_video_memory) {
461     gst_buffer_pool_config_add_option (config,
462         GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY);
463     if (use_dmabuf)
464       gst_buffer_pool_config_add_option (config,
465           GST_BUFFER_POOL_OPTION_MSDK_USE_DMABUF);
466   }
467 
468   gst_buffer_pool_config_set_video_alignment (config, &align);
469   gst_buffer_pool_config_set_allocator (config, allocator, &params);
470   gst_object_unref (allocator);
471 
472   if (!gst_buffer_pool_set_config (pool, config))
473     goto error_pool_config;
474 
475   /* Updating pool_info with aligned info of allocator */
476   *pool_info = info;
477 
478   return pool;
479 
480 error_no_pool:
481   {
482     GST_INFO_OBJECT (thiz, "Failed to create bufferpool");
483     return NULL;
484   }
485 error_no_video_info:
486   {
487     GST_INFO_OBJECT (thiz, "Failed to get Video info from caps");
488     gst_object_unref (pool);
489     return NULL;
490   }
491 error_no_allocator:
492   {
493     GST_INFO_OBJECT (thiz, "Failed to create allocator");
494     gst_object_unref (pool);
495     return NULL;
496   }
497 error_pool_config:
498   {
499     GST_INFO_OBJECT (thiz, "Failed to set config");
500     gst_object_unref (pool);
501     gst_object_unref (allocator);
502     return NULL;
503   }
504 }
505 
506 static gboolean
_gst_caps_has_feature(const GstCaps * caps,const gchar * feature)507 _gst_caps_has_feature (const GstCaps * caps, const gchar * feature)
508 {
509   guint i;
510 
511   for (i = 0; i < gst_caps_get_size (caps); i++) {
512     GstCapsFeatures *const features = gst_caps_get_features (caps, i);
513     /* Skip ANY features, we need an exact match for correct evaluation */
514     if (gst_caps_features_is_any (features))
515       continue;
516     if (gst_caps_features_contains (features, feature))
517       return TRUE;
518   }
519   return FALSE;
520 }
521 
522 static GstBufferPool *
create_src_pool(GstMsdkVPP * thiz,GstQuery * query,GstCaps * caps)523 create_src_pool (GstMsdkVPP * thiz, GstQuery * query, GstCaps * caps)
524 {
525   GstBufferPool *pool = NULL;
526   guint size = 0, min_buffers = 0, max_buffers = 0;
527   gboolean update_pool = FALSE;
528   GstAllocator *allocator = NULL;
529   GstAllocationParams params;
530   mfxFrameAllocRequest request;
531 
532   /* Check whether the query has pool */
533   if (gst_query_get_n_allocation_pools (query) > 0) {
534     update_pool = TRUE;
535     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
536   }
537   if (pool) {
538     GstStructure *config = NULL;
539     /* get the configured pool properties inorder to set in query */
540     config = gst_buffer_pool_get_config (pool);
541     gst_object_unref (pool);
542 
543     gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
544         &max_buffers);
545     if (gst_buffer_pool_config_get_allocator (config, &allocator, &params))
546       gst_query_add_allocation_param (query, allocator, &params);
547     gst_structure_free (config);
548   } else {
549     /* if we have tee after msdkvpp, we will not have pool for src pad,
550        we need assign size for the internal pool
551        gst-launch-1.0 -v videotestsrc  ! msdkvpp ! tee ! msdkh264enc ! fakesink silent=false
552      */
553     min_buffers = SRC_POOL_SIZE_DEFAULT;
554   }
555 
556   /* Always create a pool for vpp out buffers. Each of the msdk element
557    * has to create it's own mfxsurfacepool which is an msdk constraint.
558    * For eg: Each Msdk component (vpp, dec and enc) will invoke the external
559    * Frame allocator for video-memory usage.So sharing the pool between
560    * gst-msdk elements might not be a good idea, rather each element
561    * can check the buffer type (whether it is from msdk-buffer pool)
562    * to make sure there is no copy. Since we share the context between
563    * msdk elements, using buffers from one sdk's framealloator in another
564    * sdk-components is perfectly fine */
565   gst_msdk_frame_free (thiz->context, &thiz->out_alloc_resp);
566 
567   request = thiz->request[1];
568   min_buffers += thiz->async_depth + request.NumFrameSuggested;
569   request.NumFrameSuggested = min_buffers;
570   gst_msdk_frame_alloc (thiz->context, &request, &thiz->out_alloc_resp);
571 
572   pool = gst_msdkvpp_create_buffer_pool (thiz, GST_PAD_SRC, caps, min_buffers);
573   if (!pool)
574     return NULL;
575   /* we do not support dynamic buffer count change */
576   max_buffers = min_buffers;
577   if (update_pool)
578     gst_query_set_nth_allocation_pool (query, 0, pool, size, min_buffers,
579         max_buffers);
580   else
581     gst_query_add_allocation_pool (query, pool, size, min_buffers, max_buffers);
582 
583   return pool;
584 }
585 
586 static gboolean
gst_msdkvpp_decide_allocation(GstBaseTransform * trans,GstQuery * query)587 gst_msdkvpp_decide_allocation (GstBaseTransform * trans, GstQuery * query)
588 {
589   GstMsdkVPP *thiz = GST_MSDKVPP (trans);
590   GstVideoInfo info;
591   GstCaps *caps;
592 
593   gst_query_parse_allocation (query, &caps, NULL);
594   if (!caps) {
595     GST_ERROR_OBJECT (thiz, "Failed to parse the decide_allocation caps");
596     return FALSE;
597   }
598   if (!gst_video_info_from_caps (&info, caps)) {
599     GST_ERROR_OBJECT (thiz, "Failed to get video info");
600     return FALSE;
601   }
602   /* if downstream allocation query supports dmabuf-capsfeatures,
603    * we do allocate dmabuf backed memory */
604   if (_gst_caps_has_feature (caps, GST_CAPS_FEATURE_MEMORY_DMABUF)) {
605     GST_INFO_OBJECT (thiz, "MSDK VPP srcpad uses DMABuf memory");
606     thiz->use_srcpad_dmabuf = TRUE;
607   }
608 
609   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL))
610     thiz->add_video_meta = TRUE;
611   else
612     thiz->add_video_meta = FALSE;
613 
614   gst_clear_object (&thiz->srcpad_buffer_pool);
615   thiz->srcpad_buffer_pool = create_src_pool (thiz, query, caps);
616   if (!thiz->srcpad_buffer_pool)
617     return FALSE;
618 
619   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
620 
621   /* Fixme if downstream doesn't have videometa support, msdkvpp should
622    * copy the output buffers */
623 
624   return TRUE;
625 }
626 
627 static gboolean
gst_msdkvpp_propose_allocation(GstBaseTransform * trans,GstQuery * decide_query,GstQuery * query)628 gst_msdkvpp_propose_allocation (GstBaseTransform * trans,
629     GstQuery * decide_query, GstQuery * query)
630 {
631   GstMsdkVPP *thiz = GST_MSDKVPP (trans);
632   GstVideoInfo info;
633   GstBufferPool *pool = NULL;
634   GstAllocator *allocator = NULL;
635   GstCaps *caps;
636   GstStructure *config;
637   gboolean need_pool;
638   GstAllocationParams params;
639   guint size;
640   guint min_buffers = thiz->async_depth + 1;
641 
642   gst_query_parse_allocation (query, &caps, &need_pool);
643   if (!caps) {
644     GST_ERROR_OBJECT (thiz, "Failed to parse the allocation caps");
645     return FALSE;
646   }
647 
648   if (!gst_video_info_from_caps (&info, caps)) {
649     GST_ERROR_OBJECT (thiz, "Failed to get video info");
650     return FALSE;
651   }
652 
653   /* if upstream allocation query supports dmabuf-capsfeatures,
654    * we do allocate dmabuf backed memory */
655   if (_gst_caps_has_feature (caps, GST_CAPS_FEATURE_MEMORY_DMABUF)) {
656     GST_INFO_OBJECT (thiz, "MSDK VPP srcpad uses DMABuf memory");
657     thiz->use_sinkpad_dmabuf = TRUE;
658   }
659 
660   if (need_pool) {
661     /* alwys provide a new pool for upstream to help re-negotiation
662      * more info here: https://bugzilla.gnome.org/show_bug.cgi?id=748344 */
663     pool = gst_msdkvpp_create_buffer_pool (thiz, GST_PAD_SINK, caps,
664         min_buffers);
665   }
666 
667   /* Update the internal pool if any allocation attribute changed */
668   if (!gst_video_info_is_equal (&thiz->sinkpad_buffer_pool_info, &info)) {
669     gst_object_unref (thiz->sinkpad_buffer_pool);
670     thiz->sinkpad_buffer_pool = gst_msdkvpp_create_buffer_pool (thiz,
671         GST_PAD_SINK, caps, min_buffers);
672   }
673 
674   /* get the size and allocator params from configured pool and set it in query */
675   if (!need_pool)
676     pool = gst_object_ref (thiz->sinkpad_buffer_pool);
677   config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
678   gst_buffer_pool_config_get_params (config, NULL, &size, NULL, NULL);
679   if (gst_buffer_pool_config_get_allocator (config, &allocator, &params))
680     gst_query_add_allocation_param (query, allocator, &params);
681   gst_structure_free (config);
682 
683   /* if upstream doesn't have a pool requirement, set only
684    *  size, min_buffers and max_buffers in query */
685   gst_query_add_allocation_pool (query, need_pool ? pool : NULL, size,
686       min_buffers, 0);
687   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
688 
689   gst_object_unref (pool);
690 
691   return GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
692       decide_query, query);
693 }
694 
695 static MsdkSurface *
get_surface_from_pool(GstMsdkVPP * thiz,GstBufferPool * pool,GstBufferPoolAcquireParams * params)696 get_surface_from_pool (GstMsdkVPP * thiz, GstBufferPool * pool,
697     GstBufferPoolAcquireParams * params)
698 {
699   GstBuffer *new_buffer;
700   mfxFrameSurface1 *new_surface;
701   MsdkSurface *msdk_surface;
702 
703   if (!gst_buffer_pool_is_active (pool) &&
704       !gst_buffer_pool_set_active (pool, TRUE)) {
705     GST_ERROR_OBJECT (pool, "failed to activate buffer pool");
706     return NULL;
707   }
708 
709   if (gst_buffer_pool_acquire_buffer (pool, &new_buffer, params) != GST_FLOW_OK) {
710     GST_ERROR_OBJECT (pool, "failed to acquire a buffer from pool");
711     return NULL;
712   }
713 
714   if (gst_msdk_is_msdk_buffer (new_buffer))
715     new_surface = gst_msdk_get_surface_from_buffer (new_buffer);
716   else {
717     GST_ERROR_OBJECT (pool, "the acquired memory is not MSDK memory");
718     return NULL;
719   }
720 
721   msdk_surface = g_slice_new0 (MsdkSurface);
722   msdk_surface->surface = new_surface;
723   msdk_surface->buf = new_buffer;
724 
725   return msdk_surface;
726 }
727 
728 #ifndef _WIN32
729 static gboolean
import_dmabuf_to_msdk_surface(GstMsdkVPP * thiz,GstBuffer * buf,MsdkSurface * msdk_surface)730 import_dmabuf_to_msdk_surface (GstMsdkVPP * thiz, GstBuffer * buf,
731     MsdkSurface * msdk_surface)
732 {
733   GstMemory *mem = NULL;
734   GstVideoInfo vinfo;
735   GstVideoMeta *vmeta;
736   GstMsdkMemoryID *msdk_mid = NULL;
737   mfxFrameSurface1 *mfx_surface = NULL;
738   gint fd, i;
739 
740   mem = gst_buffer_peek_memory (buf, 0);
741   fd = gst_dmabuf_memory_get_fd (mem);
742   if (fd < 0)
743     return FALSE;
744 
745   vinfo = thiz->sinkpad_info;
746 
747   /* Update offset/stride/size if there is VideoMeta attached to
748    * the buffer */
749   vmeta = gst_buffer_get_video_meta (buf);
750   if (vmeta) {
751     if (GST_VIDEO_INFO_FORMAT (&vinfo) != vmeta->format ||
752         GST_VIDEO_INFO_WIDTH (&vinfo) != vmeta->width ||
753         GST_VIDEO_INFO_HEIGHT (&vinfo) != vmeta->height ||
754         GST_VIDEO_INFO_N_PLANES (&vinfo) != vmeta->n_planes) {
755       GST_ERROR_OBJECT (thiz, "VideoMeta attached to buffer is not matching"
756           "the negotiated width/height/format");
757       return FALSE;
758     }
759     for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&vinfo); ++i) {
760       GST_VIDEO_INFO_PLANE_OFFSET (&vinfo, i) = vmeta->offset[i];
761       GST_VIDEO_INFO_PLANE_STRIDE (&vinfo, i) = vmeta->stride[i];
762     }
763     GST_VIDEO_INFO_SIZE (&vinfo) = gst_buffer_get_size (buf);
764   }
765 
766   /* Upstream neither accepted the msdk pool nor the msdk buffer size restrictions.
767    * Current media-driver and GMMLib will fail due to strict memory size restrictions.
768    * Ideally, media-driver should accept what ever memory coming from other drivers
769    * in case of dmabuf-import and this is how the intel-vaapi-driver works.
770    * For now, in order to avoid any crash we check the buffer size and fallback
771    * to copy frame method.
772    *
773    * See this: https://github.com/intel/media-driver/issues/169
774    * */
775   if (GST_VIDEO_INFO_SIZE (&vinfo) <
776       GST_VIDEO_INFO_SIZE (&thiz->sinkpad_buffer_pool_info))
777     return FALSE;
778 
779   mfx_surface = msdk_surface->surface;
780   msdk_mid = (GstMsdkMemoryID *) mfx_surface->Data.MemId;
781 
782   /* release the internal memory storage of associated mfxSurface */
783   gst_msdk_replace_mfx_memid (thiz->context, mfx_surface, VA_INVALID_ID);
784 
785   /* export dmabuf to vasurface */
786   if (!gst_msdk_export_dmabuf_to_vasurface (thiz->context, &vinfo, fd,
787           msdk_mid->surface))
788     return FALSE;
789 
790   return TRUE;
791 }
792 #endif
793 
794 static MsdkSurface *
get_msdk_surface_from_input_buffer(GstMsdkVPP * thiz,GstBuffer * inbuf)795 get_msdk_surface_from_input_buffer (GstMsdkVPP * thiz, GstBuffer * inbuf)
796 {
797   GstVideoFrame src_frame, out_frame;
798   MsdkSurface *msdk_surface;
799 #ifndef _WIN32
800   GstMemory *mem = NULL;
801 #endif
802 
803   if (gst_msdk_is_msdk_buffer (inbuf)) {
804     msdk_surface = g_slice_new0 (MsdkSurface);
805     msdk_surface->surface = gst_msdk_get_surface_from_buffer (inbuf);
806     msdk_surface->buf = gst_buffer_ref (inbuf);
807     return msdk_surface;
808   }
809 
810   /* If upstream hasn't accpeted the proposed msdk bufferpool,
811    * just copy frame (if not dmabuf backed) to msdk buffer and
812    * take a surface from it.   */
813   if (!(msdk_surface =
814           get_surface_from_pool (thiz, thiz->sinkpad_buffer_pool, NULL)))
815     goto error;
816 
817 #ifndef _WIN32
818   /************ dmabuf-import ************* */
819   /* if upstream provided a dmabuf backed memory, but not an msdk
820    * buffer, we could export the dmabuf to underlined vasurface */
821   mem = gst_buffer_peek_memory (inbuf, 0);
822   if (gst_is_dmabuf_memory (mem)) {
823     if (import_dmabuf_to_msdk_surface (thiz, inbuf, msdk_surface))
824       return msdk_surface;
825     else
826       GST_INFO_OBJECT (thiz, "Upstream dmabuf-backed memory is not imported"
827           "to the msdk surface, fall back to the copy input frame method");
828   }
829 #endif
830 
831   if (!gst_video_frame_map (&src_frame, &thiz->sinkpad_info, inbuf,
832           GST_MAP_READ)) {
833     GST_ERROR_OBJECT (thiz, "failed to map the frame for source");
834     goto error;
835   }
836 
837   if (!gst_video_frame_map (&out_frame, &thiz->sinkpad_buffer_pool_info,
838           msdk_surface->buf, GST_MAP_WRITE)) {
839     GST_ERROR_OBJECT (thiz, "failed to map the frame for destination");
840     gst_video_frame_unmap (&src_frame);
841     goto error;
842   }
843 
844   if (!gst_video_frame_copy (&out_frame, &src_frame)) {
845     GST_ERROR_OBJECT (thiz, "failed to copy frame");
846     gst_video_frame_unmap (&out_frame);
847     gst_video_frame_unmap (&src_frame);
848     goto error;
849   }
850 
851   gst_video_frame_unmap (&out_frame);
852   gst_video_frame_unmap (&src_frame);
853 
854   return msdk_surface;
855 
856 error:
857   return NULL;
858 }
859 
860 static GstFlowReturn
gst_msdkvpp_transform(GstBaseTransform * trans,GstBuffer * inbuf,GstBuffer * outbuf)861 gst_msdkvpp_transform (GstBaseTransform * trans, GstBuffer * inbuf,
862     GstBuffer * outbuf)
863 {
864   GstMsdkVPP *thiz = GST_MSDKVPP (trans);
865   GstClockTime timestamp;
866   GstFlowReturn ret = GST_FLOW_OK;
867   mfxSession session;
868   mfxSyncPoint sync_point = NULL;
869   mfxStatus status;
870   mfxFrameInfo *in_info = NULL;
871   MsdkSurface *in_surface = NULL;
872   MsdkSurface *out_surface = NULL;
873   GstBuffer *outbuf_new = NULL;
874   gboolean locked_by_others;
875   gboolean create_new_surface = FALSE;
876 
877   free_unlocked_msdk_surfaces (thiz);
878 
879   in_surface = get_msdk_surface_from_input_buffer (thiz, inbuf);
880   if (!in_surface)
881     return GST_FLOW_ERROR;
882 
883   if (!in_surface->surface) {
884     GST_ERROR_OBJECT (thiz, "mfx surface is NULL for the current input buffer");
885     free_msdk_surface (in_surface);
886     return GST_FLOW_ERROR;
887   }
888   locked_by_others = ! !in_surface->surface->Data.Locked;
889 
890   /* always convert timestamp of input surface as msdk timestamp */
891   if (inbuf->pts == GST_CLOCK_TIME_NONE)
892     in_surface->surface->Data.TimeStamp = MFX_TIMESTAMP_UNKNOWN;
893   else
894     in_surface->surface->Data.TimeStamp =
895         gst_util_uint64_scale_round (inbuf->pts, 90000, GST_SECOND);
896 
897   if (gst_msdk_is_msdk_buffer (outbuf)) {
898     out_surface = g_slice_new0 (MsdkSurface);
899     out_surface->surface = gst_msdk_get_surface_from_buffer (outbuf);
900   } else {
901     GST_ERROR_OBJECT (thiz, "Failed to get msdk outsurface!");
902     free_msdk_surface (in_surface);
903     return GST_FLOW_ERROR;
904   }
905 
906   /* update surface crop info (NOTE: msdk min frame size is 2x2) */
907   in_info = &in_surface->surface->Info;
908   if ((thiz->crop_left + thiz->crop_right >= in_info->CropW - 1)
909       || (thiz->crop_top + thiz->crop_bottom >= in_info->CropH - 1)) {
910     GST_WARNING_OBJECT (thiz, "ignoring crop... cropping too much!");
911   } else {
912     in_info->CropX = thiz->crop_left;
913     in_info->CropY = thiz->crop_top;
914     in_info->CropW -= thiz->crop_left + thiz->crop_right;
915     in_info->CropH -= thiz->crop_top + thiz->crop_bottom;
916   }
917 
918   session = gst_msdk_context_get_session (thiz->context);
919 
920   /* outer loop is for handling FrameRate Control and deinterlace use cases */
921   do {
922     for (;;) {
923       status =
924           MFXVideoVPP_RunFrameVPPAsync (session, in_surface->surface,
925           out_surface->surface, NULL, &sync_point);
926       timestamp = out_surface->surface->Data.TimeStamp;
927 
928       if (status != MFX_WRN_DEVICE_BUSY)
929         break;
930       /* If device is busy, wait 1ms and retry, as per MSDK's recommendation */
931       g_usleep (1000);
932     }
933 
934     if (timestamp == MFX_TIMESTAMP_UNKNOWN)
935       timestamp = GST_CLOCK_TIME_NONE;
936     else
937       timestamp = gst_util_uint64_scale_round (timestamp, GST_SECOND, 90000);
938 
939     if (status == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM)
940       GST_WARNING_OBJECT (thiz, "VPP returned: %s",
941           msdk_status_to_string (status));
942     else if (status != MFX_ERR_NONE && status != MFX_ERR_MORE_DATA
943         && status != MFX_ERR_MORE_SURFACE)
944       goto vpp_error;
945 
946     /* No output generated */
947     if (status == MFX_ERR_MORE_DATA)
948       goto error_more_data;
949 
950     /* Wait for vpp operation to complete, the magic number 300000 below
951      * is used in MSDK samples
952      * #define MSDK_VPP_WAIT_INTERVAL 300000
953      */
954     if (sync_point &&
955         MFXVideoCORE_SyncOperation (session, sync_point,
956             300000) != MFX_ERR_NONE)
957       GST_WARNING_OBJECT (thiz, "failed to do sync operation");
958     /* push new output buffer forward after sync operation */
959     if (create_new_surface) {
960       create_new_surface = FALSE;
961       ret = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (trans), outbuf_new);
962       if (ret != GST_FLOW_OK)
963         goto error_push_buffer;
964     }
965 
966     /* More than one output buffers are generated */
967     if (status == MFX_ERR_MORE_SURFACE) {
968       outbuf_new = create_output_buffer (thiz);
969       GST_BUFFER_TIMESTAMP (outbuf_new) = timestamp;
970       GST_BUFFER_DURATION (outbuf_new) = thiz->buffer_duration;
971 
972       if (gst_msdk_is_msdk_buffer (outbuf_new)) {
973         release_out_surface (thiz, out_surface);
974         out_surface = g_slice_new0 (MsdkSurface);
975         out_surface->surface = gst_msdk_get_surface_from_buffer (outbuf_new);
976         create_new_surface = TRUE;
977       } else {
978         GST_ERROR_OBJECT (thiz, "Failed to get msdk outsurface!");
979         goto vpp_error;
980       }
981     } else {
982       GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
983       GST_BUFFER_DURATION (outbuf) = thiz->buffer_duration;
984     }
985   } while (status == MFX_ERR_MORE_SURFACE);
986 
987   goto transform_end;
988 
989 vpp_error:
990   GST_ERROR_OBJECT (thiz, "MSDK Failed to do VPP");
991   ret = GST_FLOW_ERROR;
992   goto transform_end;
993 
994 error_more_data:
995   GST_WARNING_OBJECT (thiz,
996       "MSDK Requires additional input for processing, "
997       "Retruning FLOW_DROPPED since no output buffer was generated");
998   ret = GST_BASE_TRANSFORM_FLOW_DROPPED;
999   goto transform_end;
1000 
1001 error_push_buffer:
1002   GST_DEBUG_OBJECT (thiz, "failed to push output buffer: %s",
1003       gst_flow_get_name (ret));
1004 
1005 transform_end:
1006   release_in_surface (thiz, in_surface, locked_by_others);
1007   release_out_surface (thiz, out_surface);
1008 
1009   return ret;
1010 }
1011 
1012 static void
gst_msdkvpp_close(GstMsdkVPP * thiz)1013 gst_msdkvpp_close (GstMsdkVPP * thiz)
1014 {
1015   mfxStatus status;
1016 
1017   if (!thiz->context)
1018     return;
1019 
1020   if (thiz->use_video_memory) {
1021     gst_msdk_frame_free (thiz->context, &thiz->in_alloc_resp);
1022     gst_msdk_frame_free (thiz->context, &thiz->out_alloc_resp);
1023   }
1024 
1025   GST_DEBUG_OBJECT (thiz, "Closing VPP 0x%p", thiz->context);
1026   status = MFXVideoVPP_Close (gst_msdk_context_get_session (thiz->context));
1027   if (status != MFX_ERR_NONE && status != MFX_ERR_NOT_INITIALIZED) {
1028     GST_WARNING_OBJECT (thiz, "VPP close failed (%s)",
1029         msdk_status_to_string (status));
1030   }
1031   free_all_msdk_surfaces (thiz);
1032 
1033   gst_clear_object (&thiz->context);
1034 
1035   memset (&thiz->param, 0, sizeof (thiz->param));
1036 
1037   gst_clear_object (&thiz->sinkpad_buffer_pool);
1038   gst_clear_object (&thiz->srcpad_buffer_pool);
1039 
1040   thiz->buffer_duration = GST_CLOCK_TIME_NONE;
1041   gst_video_info_init (&thiz->sinkpad_info);
1042   gst_video_info_init (&thiz->srcpad_info);
1043 }
1044 
1045 static void
ensure_filters(GstMsdkVPP * thiz)1046 ensure_filters (GstMsdkVPP * thiz)
1047 {
1048   /* Denoise */
1049   if (thiz->flags & GST_MSDK_FLAG_DENOISE) {
1050     mfxExtVPPDenoise *mfx_denoise = &thiz->mfx_denoise;
1051     mfx_denoise->Header.BufferId = MFX_EXTBUFF_VPP_DENOISE;
1052     mfx_denoise->Header.BufferSz = sizeof (mfxExtVPPDenoise);
1053     mfx_denoise->DenoiseFactor = thiz->denoise_factor;
1054     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_denoise);
1055   }
1056 
1057   /* Rotation */
1058   if (thiz->rotation != MFX_ANGLE_0) {
1059     mfxExtVPPRotation *mfx_rotation = &thiz->mfx_rotation;
1060     mfx_rotation->Header.BufferId = MFX_EXTBUFF_VPP_ROTATION;
1061     mfx_rotation->Header.BufferSz = sizeof (mfxExtVPPRotation);
1062     mfx_rotation->Angle = thiz->rotation;
1063     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_rotation);
1064   }
1065 
1066   /* Deinterlace */
1067   if (thiz->flags & GST_MSDK_FLAG_DEINTERLACE) {
1068     mfxExtVPPDeinterlacing *mfx_deinterlace = &thiz->mfx_deinterlace;
1069     mfx_deinterlace->Header.BufferId = MFX_EXTBUFF_VPP_DEINTERLACING;
1070     mfx_deinterlace->Header.BufferSz = sizeof (mfxExtVPPDeinterlacing);
1071     mfx_deinterlace->Mode = thiz->deinterlace_method;
1072     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_deinterlace);
1073   }
1074 
1075   /* Colorbalance(ProcAmp) */
1076   if (thiz->flags & (GST_MSDK_FLAG_HUE | GST_MSDK_FLAG_SATURATION |
1077           GST_MSDK_FLAG_BRIGHTNESS | GST_MSDK_FLAG_CONTRAST)) {
1078     mfxExtVPPProcAmp *mfx_procamp = &thiz->mfx_procamp;
1079     mfx_procamp->Header.BufferId = MFX_EXTBUFF_VPP_PROCAMP;
1080     mfx_procamp->Header.BufferSz = sizeof (mfxExtVPPProcAmp);
1081     mfx_procamp->Hue = thiz->hue;
1082     mfx_procamp->Saturation = thiz->saturation;
1083     mfx_procamp->Brightness = thiz->brightness;
1084     mfx_procamp->Contrast = thiz->contrast;
1085     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_procamp);
1086   }
1087 
1088   /* Detail/Edge enhancement */
1089   if (thiz->flags & GST_MSDK_FLAG_DETAIL) {
1090     mfxExtVPPDetail *mfx_detail = &thiz->mfx_detail;
1091     mfx_detail->Header.BufferId = MFX_EXTBUFF_VPP_DETAIL;
1092     mfx_detail->Header.BufferSz = sizeof (mfxExtVPPDetail);
1093     mfx_detail->DetailFactor = thiz->detail;
1094     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_detail);
1095   }
1096 
1097   /* Mirroring */
1098   if (thiz->mirroring != MFX_MIRRORING_DISABLED) {
1099     mfxExtVPPMirroring *mfx_mirroring = &thiz->mfx_mirroring;
1100     mfx_mirroring->Header.BufferId = MFX_EXTBUFF_VPP_MIRRORING;
1101     mfx_mirroring->Header.BufferSz = sizeof (mfxExtVPPMirroring);
1102     mfx_mirroring->Type = thiz->mirroring;
1103     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_mirroring);
1104   }
1105 
1106   /* Scaling Mode */
1107   if (thiz->flags & GST_MSDK_FLAG_SCALING_MODE) {
1108     mfxExtVPPScaling *mfx_scaling = &thiz->mfx_scaling;
1109     mfx_scaling->Header.BufferId = MFX_EXTBUFF_VPP_SCALING;
1110     mfx_scaling->Header.BufferSz = sizeof (mfxExtVPPScaling);
1111     mfx_scaling->ScalingMode = thiz->scaling_mode;
1112     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_scaling);
1113   }
1114 
1115   /* FRC */
1116   if (thiz->flags & GST_MSDK_FLAG_FRC) {
1117     mfxExtVPPFrameRateConversion *mfx_frc = &thiz->mfx_frc;
1118     mfx_frc->Header.BufferId = MFX_EXTBUFF_VPP_FRAME_RATE_CONVERSION;
1119     mfx_frc->Header.BufferSz = sizeof (mfxExtVPPFrameRateConversion);
1120     mfx_frc->Algorithm = thiz->frc_algm;
1121     gst_msdkvpp_add_extra_param (thiz, (mfxExtBuffer *) mfx_frc);
1122   }
1123 }
1124 
1125 static void
gst_msdkvpp_set_passthrough(GstMsdkVPP * thiz)1126 gst_msdkvpp_set_passthrough (GstMsdkVPP * thiz)
1127 {
1128   gboolean passthrough = TRUE;
1129 
1130   /* no passthrough if any of the filter algorithm is enabled */
1131   if (thiz->flags)
1132     passthrough = FALSE;
1133 
1134   /* vpp could be needed in some specific circumstances, for eg:
1135    * input surface is dmabuf and output must be videomemory. So far
1136    * the underline iHD driver doesn't seems to support dmabuf mapping,
1137    * so we could explicitly ask msdkvpp to provide non-dambuf videomemory
1138    * surfaces as output thourgh capsfileters */
1139   if (thiz->need_vpp)
1140     passthrough = FALSE;
1141 
1142   /* no passthrough if there is change in out width,height or format */
1143   if (GST_VIDEO_INFO_WIDTH (&thiz->sinkpad_info) !=
1144       GST_VIDEO_INFO_WIDTH (&thiz->srcpad_info)
1145       || GST_VIDEO_INFO_HEIGHT (&thiz->sinkpad_info) !=
1146       GST_VIDEO_INFO_HEIGHT (&thiz->srcpad_info)
1147       || GST_VIDEO_INFO_FORMAT (&thiz->sinkpad_info) !=
1148       GST_VIDEO_INFO_FORMAT (&thiz->srcpad_info))
1149     passthrough = FALSE;
1150 
1151   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (thiz), passthrough);
1152 }
1153 
1154 static gboolean
gst_msdkvpp_initialize(GstMsdkVPP * thiz)1155 gst_msdkvpp_initialize (GstMsdkVPP * thiz)
1156 {
1157   mfxSession session;
1158   mfxStatus status;
1159   mfxFrameAllocRequest *request = &thiz->request[0];
1160 
1161   if (!thiz->context) {
1162     GST_WARNING_OBJECT (thiz, "No MSDK Context");
1163     return FALSE;
1164   }
1165 
1166   GST_OBJECT_LOCK (thiz);
1167   session = gst_msdk_context_get_session (thiz->context);
1168 
1169   /* Close the current session if the session has been initialized,
1170    * otherwise the subsequent function call of MFXVideoVPP_Init() will
1171    * fail
1172    */
1173   if (thiz->initialized) {
1174     if (thiz->use_video_memory) {
1175       gst_msdk_frame_free (thiz->context, &thiz->in_alloc_resp);
1176     }
1177 
1178     MFXVideoVPP_Close (session);
1179 
1180     memset (&thiz->param, 0, sizeof (thiz->param));
1181     memset (&thiz->extra_params, 0, sizeof (thiz->extra_params));
1182     thiz->num_extra_params = 0;
1183   }
1184 
1185   if (thiz->use_video_memory) {
1186     gst_msdk_set_frame_allocator (thiz->context);
1187     thiz->param.IOPattern =
1188         MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY;
1189   } else {
1190     thiz->param.IOPattern =
1191         MFX_IOPATTERN_IN_SYSTEM_MEMORY | MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
1192   }
1193 
1194   /* update input video attributes */
1195   gst_msdk_set_mfx_frame_info_from_video_info (&thiz->param.vpp.In,
1196       &thiz->sinkpad_info);
1197 
1198   /* update output video attributes, only CSC and Scaling are supported for now */
1199   gst_msdk_set_mfx_frame_info_from_video_info (&thiz->param.vpp.Out,
1200       &thiz->srcpad_info);
1201 
1202   /* use msdk frame rarte control if there is a mismatch in In & OUt fps  */
1203   if (GST_VIDEO_INFO_FPS_N (&thiz->srcpad_info) &&
1204       (GST_VIDEO_INFO_FPS_N (&thiz->sinkpad_info) !=
1205           GST_VIDEO_INFO_FPS_N (&thiz->srcpad_info)
1206           || GST_VIDEO_INFO_FPS_D (&thiz->sinkpad_info) !=
1207           GST_VIDEO_INFO_FPS_D (&thiz->srcpad_info))) {
1208     thiz->flags |= GST_MSDK_FLAG_FRC;
1209     /* manually set distributed timestamp as frc algorithm
1210      * as it is more resonable for framerate conversion
1211      */
1212     thiz->frc_algm = MFX_FRCALGM_DISTRIBUTED_TIMESTAMP;
1213   }
1214 
1215   /* work-around to avoid zero fps in msdk structure */
1216   if (!thiz->param.vpp.In.FrameRateExtN)
1217     thiz->param.vpp.In.FrameRateExtN = 30;
1218   if (!thiz->param.vpp.Out.FrameRateExtN)
1219     thiz->param.vpp.Out.FrameRateExtN = thiz->param.vpp.In.FrameRateExtN;
1220 
1221   /* set vpp out picstruct as progressive if deinterlacing enabled */
1222   if (thiz->flags & GST_MSDK_FLAG_DEINTERLACE)
1223     thiz->param.vpp.Out.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
1224 
1225   /* Enable the required filters */
1226   ensure_filters (thiz);
1227 
1228   /* Add extended buffers */
1229   if (thiz->num_extra_params) {
1230     thiz->param.NumExtParam = thiz->num_extra_params;
1231     thiz->param.ExtParam = thiz->extra_params;
1232   }
1233 
1234   /* validate parameters and allow MFX to make adjustments */
1235   status = MFXVideoVPP_Query (session, &thiz->param, &thiz->param);
1236   if (status < MFX_ERR_NONE) {
1237     GST_ERROR_OBJECT (thiz, "Video VPP Query failed (%s)",
1238         msdk_status_to_string (status));
1239     goto no_vpp;
1240   } else if (status > MFX_ERR_NONE) {
1241     GST_WARNING_OBJECT (thiz, "Video VPP Query returned: %s",
1242         msdk_status_to_string (status));
1243   }
1244 
1245   status = MFXVideoVPP_QueryIOSurf (session, &thiz->param, request);
1246   if (status < MFX_ERR_NONE) {
1247     GST_ERROR_OBJECT (thiz, "VPP Query IO surfaces failed (%s)",
1248         msdk_status_to_string (status));
1249     goto no_vpp;
1250   } else if (status > MFX_ERR_NONE) {
1251     GST_WARNING_OBJECT (thiz, "VPP Query IO surfaces returned: %s",
1252         msdk_status_to_string (status));
1253   }
1254 
1255   if (thiz->use_video_memory) {
1256     /* Input surface pool pre-allocation */
1257     request[0].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
1258     if (thiz->use_sinkpad_dmabuf)
1259       request[0].Type |= MFX_MEMTYPE_EXPORT_FRAME;
1260     gst_msdk_frame_alloc (thiz->context, &(request[0]), &thiz->in_alloc_resp);
1261 
1262     /* Output surface pool pre-allocation */
1263     request[1].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
1264     if (thiz->use_srcpad_dmabuf)
1265       request[1].Type |= MFX_MEMTYPE_EXPORT_FRAME;
1266   }
1267 
1268   thiz->in_num_surfaces = request[0].NumFrameSuggested;
1269 
1270   status = MFXVideoVPP_Init (session, &thiz->param);
1271   if (status < MFX_ERR_NONE) {
1272     GST_ERROR_OBJECT (thiz, "Init failed (%s)", msdk_status_to_string (status));
1273     goto no_vpp;
1274   } else if (status > MFX_ERR_NONE) {
1275     GST_WARNING_OBJECT (thiz, "Init returned: %s",
1276         msdk_status_to_string (status));
1277   }
1278 
1279   thiz->initialized = TRUE;
1280   GST_OBJECT_UNLOCK (thiz);
1281   return TRUE;
1282 
1283 no_vpp:
1284   GST_OBJECT_UNLOCK (thiz);
1285   gst_clear_object (&thiz->context);
1286   return FALSE;
1287 }
1288 
1289 static gboolean
gst_msdkvpp_set_caps(GstBaseTransform * trans,GstCaps * caps,GstCaps * out_caps)1290 gst_msdkvpp_set_caps (GstBaseTransform * trans, GstCaps * caps,
1291     GstCaps * out_caps)
1292 {
1293   GstMsdkVPP *thiz = GST_MSDKVPP (trans);
1294   GstVideoInfo in_info, out_info;
1295   gboolean sinkpad_info_changed = FALSE;
1296   gboolean srcpad_info_changed = FALSE;
1297   gboolean deinterlace;
1298 
1299   if (!gst_caps_features_is_equal (gst_caps_get_features (caps, 0),
1300           gst_caps_get_features (out_caps, 0)))
1301     thiz->need_vpp = 1;
1302 
1303   gst_video_info_from_caps (&in_info, caps);
1304   gst_video_info_from_caps (&out_info, out_caps);
1305 
1306   if (!gst_video_info_is_equal (&in_info, &thiz->sinkpad_info))
1307     sinkpad_info_changed = TRUE;
1308   if (!gst_video_info_is_equal (&out_info, &thiz->srcpad_info))
1309     srcpad_info_changed = TRUE;
1310 
1311   if (!sinkpad_info_changed && !srcpad_info_changed && thiz->initialized)
1312     return TRUE;
1313 
1314   thiz->sinkpad_info = in_info;
1315   thiz->srcpad_info = out_info;
1316 #ifndef _WIN32
1317   thiz->use_video_memory = TRUE;
1318 #else
1319   thiz->use_video_memory = FALSE;
1320 #endif
1321 
1322   /* check for deinterlace requirement */
1323   deinterlace = gst_msdkvpp_is_deinterlace_enabled (thiz, &in_info);
1324   if (deinterlace)
1325     thiz->flags |= GST_MSDK_FLAG_DEINTERLACE;
1326 
1327   thiz->buffer_duration = GST_VIDEO_INFO_FPS_N (&out_info) > 0 ?
1328       gst_util_uint64_scale (GST_SECOND, GST_VIDEO_INFO_FPS_D (&out_info),
1329       GST_VIDEO_INFO_FPS_N (&out_info)) : 0;
1330 
1331   if (!gst_msdkvpp_initialize (thiz))
1332     return FALSE;
1333 
1334   /* set passthrough according to filter operation change */
1335   gst_msdkvpp_set_passthrough (thiz);
1336 
1337   /* Ensure sinkpad buffer pool */
1338   if (thiz->sinkpad_buffer_pool)
1339     gst_object_unref (thiz->sinkpad_buffer_pool);
1340 
1341   thiz->sinkpad_buffer_pool =
1342       gst_msdkvpp_create_buffer_pool (thiz, GST_PAD_SINK, caps,
1343       thiz->in_num_surfaces);
1344   if (!thiz->sinkpad_buffer_pool) {
1345     GST_ERROR_OBJECT (thiz, "Failed to ensure the sinkpad buffer pool");
1346     return FALSE;
1347   }
1348 
1349   return TRUE;
1350 }
1351 
1352 static gboolean
pad_can_dmabuf(GstMsdkVPP * thiz,GstPadDirection direction,GstCaps * filter)1353 pad_can_dmabuf (GstMsdkVPP * thiz, GstPadDirection direction, GstCaps * filter)
1354 {
1355   gboolean ret = FALSE;
1356   GstCaps *caps, *out_caps;
1357   GstPad *pad;
1358   GstBaseTransform *trans = GST_BASE_TRANSFORM (thiz);
1359 
1360   if (direction == GST_PAD_SRC)
1361     pad = GST_BASE_TRANSFORM_SRC_PAD (trans);
1362   else
1363     pad = GST_BASE_TRANSFORM_SINK_PAD (trans);
1364 
1365   /* make a copy of filter caps since we need to alter the structure
1366    * by adding dmabuf-capsfeatures */
1367   caps = gst_caps_copy (filter);
1368   gst_caps_set_features (caps, 0,
1369       gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_DMABUF));
1370 
1371   out_caps = gst_pad_peer_query_caps (pad, caps);
1372   if (!out_caps)
1373     goto done;
1374 
1375   if (gst_caps_is_any (out_caps) || gst_caps_is_empty (out_caps)
1376       || out_caps == caps)
1377     goto done;
1378 
1379   if (_gst_caps_has_feature (out_caps, GST_CAPS_FEATURE_MEMORY_DMABUF))
1380     ret = TRUE;
1381 done:
1382   if (caps)
1383     gst_caps_unref (caps);
1384   if (out_caps)
1385     gst_caps_unref (out_caps);
1386   return ret;
1387 }
1388 
1389 static GstCaps *
gst_msdkvpp_fixate_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * othercaps)1390 gst_msdkvpp_fixate_caps (GstBaseTransform * trans,
1391     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
1392 {
1393   GstMsdkVPP *thiz = GST_MSDKVPP (trans);
1394   GstCaps *result = NULL;
1395   gboolean *use_dmabuf;
1396 
1397   if (direction == GST_PAD_SRC) {
1398     result = gst_caps_fixate (result);
1399     use_dmabuf = &thiz->use_sinkpad_dmabuf;
1400   } else {
1401     /*
1402      * Override mirroring & rotation properties once video-direction
1403      * is set explicitly
1404      */
1405     if (thiz->flags & GST_MSDK_FLAG_VIDEO_DIRECTION)
1406       gst_msdk_get_mfx_video_orientation_from_video_direction
1407           (thiz->video_direction, &thiz->mirroring, &thiz->rotation);
1408 
1409     result = gst_msdkvpp_fixate_srccaps (thiz, caps, othercaps);
1410     use_dmabuf = &thiz->use_srcpad_dmabuf;
1411   }
1412 
1413   GST_DEBUG_OBJECT (trans, "fixated to %" GST_PTR_FORMAT, result);
1414   gst_caps_unref (othercaps);
1415 
1416   if (pad_can_dmabuf (thiz,
1417           direction == GST_PAD_SRC ? GST_PAD_SINK : GST_PAD_SRC, result)) {
1418     gst_caps_set_features (result, 0,
1419         gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_DMABUF, NULL));
1420     *use_dmabuf = TRUE;
1421   }
1422 
1423   return result;
1424 }
1425 
1426 /* Generic code for now, requires changes in future when we
1427  * add hardware query for supported formats, Framerate control etc */
1428 static GstCaps *
gst_msdkvpp_transform_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * filter)1429 gst_msdkvpp_transform_caps (GstBaseTransform * trans,
1430     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
1431 {
1432   GstCaps *out_caps;
1433 
1434   GST_DEBUG_OBJECT (trans,
1435       "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
1436       (direction == GST_PAD_SINK) ? "sink" : "src");
1437 
1438   if (direction == GST_PAD_SRC)
1439     out_caps = gst_static_pad_template_get_caps (&gst_msdkvpp_sink_factory);
1440   else
1441     out_caps = gst_static_pad_template_get_caps (&gst_msdkvpp_src_factory);
1442 
1443   if (out_caps && filter) {
1444     GstCaps *intersection;
1445 
1446     intersection = gst_caps_intersect_full (out_caps, filter,
1447         GST_CAPS_INTERSECT_FIRST);
1448     gst_caps_unref (out_caps);
1449     out_caps = intersection;
1450   }
1451 
1452   GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, out_caps);
1453   return out_caps;
1454 }
1455 
1456 static gboolean
gst_msdkvpp_start(GstBaseTransform * trans)1457 gst_msdkvpp_start (GstBaseTransform * trans)
1458 {
1459   if (!ensure_context (trans))
1460     return FALSE;
1461   return TRUE;
1462 }
1463 
1464 static gboolean
gst_msdkvpp_stop(GstBaseTransform * trans)1465 gst_msdkvpp_stop (GstBaseTransform * trans)
1466 {
1467   gst_msdkvpp_close (GST_MSDKVPP (trans));
1468   return TRUE;
1469 }
1470 
1471 static void
gst_msdkvpp_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1472 gst_msdkvpp_set_property (GObject * object, guint prop_id,
1473     const GValue * value, GParamSpec * pspec)
1474 {
1475   GstMsdkVPP *thiz = GST_MSDKVPP (object);
1476 
1477   switch (prop_id) {
1478     case PROP_HARDWARE:
1479       thiz->hardware = g_value_get_boolean (value);
1480       break;
1481     case PROP_ASYNC_DEPTH:
1482       thiz->async_depth = g_value_get_uint (value);
1483       break;
1484     case PROP_DENOISE:
1485       thiz->denoise_factor = g_value_get_uint (value);
1486       thiz->flags |= GST_MSDK_FLAG_DENOISE;
1487       break;
1488 #ifndef GST_REMOVE_DEPRECATED
1489     case PROP_ROTATION:
1490       thiz->rotation = g_value_get_enum (value);
1491       thiz->flags |= GST_MSDK_FLAG_ROTATION;
1492       break;
1493     case PROP_MIRRORING:
1494       thiz->mirroring = g_value_get_enum (value);
1495       thiz->flags |= GST_MSDK_FLAG_MIRRORING;
1496       break;
1497 #endif
1498     case PROP_DEINTERLACE_MODE:
1499       thiz->deinterlace_mode = g_value_get_enum (value);
1500       break;
1501     case PROP_DEINTERLACE_METHOD:
1502       thiz->deinterlace_method = g_value_get_enum (value);
1503       break;
1504     case PROP_HUE:
1505       thiz->hue = g_value_get_float (value);
1506       thiz->flags |= GST_MSDK_FLAG_HUE;
1507       break;
1508     case PROP_SATURATION:
1509       thiz->saturation = g_value_get_float (value);
1510       thiz->flags |= GST_MSDK_FLAG_SATURATION;
1511       break;
1512     case PROP_BRIGHTNESS:
1513       thiz->brightness = g_value_get_float (value);
1514       thiz->flags |= GST_MSDK_FLAG_BRIGHTNESS;
1515       break;
1516     case PROP_CONTRAST:
1517       thiz->contrast = g_value_get_float (value);
1518       thiz->flags |= GST_MSDK_FLAG_CONTRAST;
1519       break;
1520     case PROP_DETAIL:
1521       thiz->detail = g_value_get_uint (value);
1522       thiz->flags |= GST_MSDK_FLAG_DETAIL;
1523       break;
1524     case PROP_SCALING_MODE:
1525       thiz->scaling_mode = g_value_get_enum (value);
1526       thiz->flags |= GST_MSDK_FLAG_SCALING_MODE;
1527       break;
1528     case PROP_FORCE_ASPECT_RATIO:
1529       thiz->keep_aspect = g_value_get_boolean (value);
1530       break;
1531     case PROP_FRC_ALGORITHM:
1532       thiz->frc_algm = g_value_get_enum (value);
1533       break;
1534     case PROP_VIDEO_DIRECTION:
1535       thiz->video_direction = g_value_get_enum (value);
1536       thiz->flags |= GST_MSDK_FLAG_VIDEO_DIRECTION;
1537       break;
1538     case PROP_CROP_LEFT:
1539       thiz->crop_left = g_value_get_uint (value);
1540       break;
1541     case PROP_CROP_RIGHT:
1542       thiz->crop_right = g_value_get_uint (value);
1543       break;
1544     case PROP_CROP_TOP:
1545       thiz->crop_top = g_value_get_uint (value);
1546       break;
1547     case PROP_CROP_BOTTOM:
1548       thiz->crop_bottom = g_value_get_uint (value);
1549       break;
1550     default:
1551       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1552       break;
1553   }
1554 }
1555 
1556 static void
gst_msdkvpp_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1557 gst_msdkvpp_get_property (GObject * object, guint prop_id,
1558     GValue * value, GParamSpec * pspec)
1559 {
1560   GstMsdkVPP *thiz = GST_MSDKVPP (object);
1561 
1562   switch (prop_id) {
1563     case PROP_HARDWARE:
1564       g_value_set_boolean (value, thiz->hardware);
1565       break;
1566     case PROP_ASYNC_DEPTH:
1567       g_value_set_uint (value, thiz->async_depth);
1568       break;
1569     case PROP_DENOISE:
1570       g_value_set_uint (value, thiz->denoise_factor);
1571       break;
1572 #ifndef GST_REMOVE_DEPRECATED
1573     case PROP_ROTATION:
1574       g_value_set_enum (value, thiz->rotation);
1575       break;
1576     case PROP_MIRRORING:
1577       g_value_set_enum (value, thiz->mirroring);
1578       break;
1579 #endif
1580     case PROP_DEINTERLACE_MODE:
1581       g_value_set_enum (value, thiz->deinterlace_mode);
1582       break;
1583     case PROP_DEINTERLACE_METHOD:
1584       g_value_set_enum (value, thiz->deinterlace_method);
1585       break;
1586     case PROP_HUE:
1587       g_value_set_float (value, thiz->hue);
1588       break;
1589     case PROP_SATURATION:
1590       g_value_set_float (value, thiz->saturation);
1591       break;
1592     case PROP_BRIGHTNESS:
1593       g_value_set_float (value, thiz->brightness);
1594       break;
1595     case PROP_CONTRAST:
1596       g_value_set_float (value, thiz->contrast);
1597       break;
1598     case PROP_DETAIL:
1599       g_value_set_uint (value, thiz->detail);
1600       break;
1601     case PROP_SCALING_MODE:
1602       g_value_set_enum (value, thiz->scaling_mode);
1603       break;
1604     case PROP_FORCE_ASPECT_RATIO:
1605       g_value_set_boolean (value, thiz->keep_aspect);
1606       break;
1607     case PROP_FRC_ALGORITHM:
1608       g_value_set_enum (value, thiz->frc_algm);
1609       break;
1610     case PROP_VIDEO_DIRECTION:
1611       g_value_set_enum (value, thiz->video_direction);
1612       break;
1613     case PROP_CROP_LEFT:
1614       g_value_set_uint (value, thiz->crop_left);
1615       break;
1616     case PROP_CROP_RIGHT:
1617       g_value_set_uint (value, thiz->crop_right);
1618       break;
1619     case PROP_CROP_TOP:
1620       g_value_set_uint (value, thiz->crop_top);
1621       break;
1622     case PROP_CROP_BOTTOM:
1623       g_value_set_uint (value, thiz->crop_bottom);
1624       break;
1625     default:
1626       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1627       break;
1628   }
1629 }
1630 
1631 static void
gst_msdkvpp_dispose(GObject * object)1632 gst_msdkvpp_dispose (GObject * object)
1633 {
1634   GstMsdkVPP *thiz = GST_MSDKVPP (object);
1635 
1636   gst_clear_object (&thiz->old_context);
1637 
1638   G_OBJECT_CLASS (parent_class)->dispose (object);
1639 }
1640 
1641 static void
gst_msdkvpp_set_context(GstElement * element,GstContext * context)1642 gst_msdkvpp_set_context (GstElement * element, GstContext * context)
1643 {
1644   GstMsdkContext *msdk_context = NULL;
1645   GstMsdkVPP *thiz = GST_MSDKVPP (element);
1646 
1647   if (gst_msdk_context_get_context (context, &msdk_context)) {
1648     gst_object_replace ((GstObject **) & thiz->context,
1649         (GstObject *) msdk_context);
1650     gst_object_unref (msdk_context);
1651   }
1652 
1653   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
1654 }
1655 
1656 static void
gst_msdkvpp_class_init(GstMsdkVPPClass * klass)1657 gst_msdkvpp_class_init (GstMsdkVPPClass * klass)
1658 {
1659   GObjectClass *gobject_class;
1660   GstElementClass *element_class;
1661   GstBaseTransformClass *trans_class;
1662   GParamSpec *obj_properties[PROP_N] = { NULL, };
1663 
1664   gobject_class = G_OBJECT_CLASS (klass);
1665   element_class = GST_ELEMENT_CLASS (klass);
1666   trans_class = GST_BASE_TRANSFORM_CLASS (klass);
1667 
1668   gobject_class->set_property = gst_msdkvpp_set_property;
1669   gobject_class->get_property = gst_msdkvpp_get_property;
1670   gobject_class->dispose = gst_msdkvpp_dispose;
1671 
1672   element_class->set_context = gst_msdkvpp_set_context;
1673 
1674   gst_element_class_add_static_pad_template (element_class,
1675       &gst_msdkvpp_src_factory);
1676   gst_element_class_add_static_pad_template (element_class,
1677       &gst_msdkvpp_sink_factory);
1678 
1679   gst_element_class_set_static_metadata (element_class,
1680       "Intel MSDK Video Postprocessor",
1681       "Filter/Converter/Video;Filter/Converter/Video/Scaler;"
1682       "Filter/Effect/Video;Filter/Effect/Video/Deinterlace",
1683       "Video Postprocessing Filter based on " MFX_API_SDK,
1684       "Sreerenj Balachandrn <sreerenj.balachandran@intel.com>");
1685 
1686   trans_class->start = GST_DEBUG_FUNCPTR (gst_msdkvpp_start);
1687   trans_class->stop = GST_DEBUG_FUNCPTR (gst_msdkvpp_stop);
1688   trans_class->transform_caps = GST_DEBUG_FUNCPTR (gst_msdkvpp_transform_caps);
1689   trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_msdkvpp_fixate_caps);
1690   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_msdkvpp_set_caps);
1691   trans_class->transform = GST_DEBUG_FUNCPTR (gst_msdkvpp_transform);
1692   trans_class->propose_allocation =
1693       GST_DEBUG_FUNCPTR (gst_msdkvpp_propose_allocation);
1694   trans_class->decide_allocation =
1695       GST_DEBUG_FUNCPTR (gst_msdkvpp_decide_allocation);
1696   trans_class->prepare_output_buffer =
1697       GST_DEBUG_FUNCPTR (gst_msdkvpp_prepare_output_buffer);
1698 
1699   obj_properties[PROP_HARDWARE] =
1700       g_param_spec_boolean ("hardware", "Hardware", "Enable hardware VPP",
1701       PROP_HARDWARE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1702 
1703   obj_properties[PROP_ASYNC_DEPTH] =
1704       g_param_spec_uint ("async-depth", "Async Depth",
1705       "Depth of asynchronous pipeline",
1706       1, 1, PROP_ASYNC_DEPTH_DEFAULT,
1707       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1708 
1709   obj_properties[PROP_DENOISE] =
1710       g_param_spec_uint ("denoise", "Denoising factor",
1711       "Denoising Factor",
1712       0, 100, PROP_DENOISE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1713 
1714 #ifndef GST_REMOVE_DEPRECATED
1715   obj_properties[PROP_ROTATION] =
1716       g_param_spec_enum ("rotation", "Rotation",
1717       "Rotation Angle (DEPRECATED, use video-direction instead)",
1718       gst_msdkvpp_rotation_get_type (), PROP_ROTATION_DEFAULT,
1719       G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1720 
1721   obj_properties[PROP_MIRRORING] =
1722       g_param_spec_enum ("mirroring", "Mirroring",
1723       "The Mirroring type (DEPRECATED, use video-direction instead)",
1724       gst_msdkvpp_mirroring_get_type (), PROP_MIRRORING_DEFAULT,
1725       G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1726 
1727 #endif
1728 
1729   obj_properties[PROP_DEINTERLACE_MODE] =
1730       g_param_spec_enum ("deinterlace-mode", "Deinterlace Mode",
1731       "Deinterlace mode to use", gst_msdkvpp_deinterlace_mode_get_type (),
1732       PROP_DEINTERLACE_MODE_DEFAULT,
1733       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1734 
1735   obj_properties[PROP_DEINTERLACE_METHOD] =
1736       g_param_spec_enum ("deinterlace-method", "Deinterlace Method",
1737       "Deinterlace method to use", gst_msdkvpp_deinterlace_method_get_type (),
1738       PROP_DEINTERLACE_METHOD_DEFAULT,
1739       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1740 
1741   obj_properties[PROP_HUE] =
1742       g_param_spec_float ("hue", "Hue",
1743       "The hue of the video",
1744       -180, 180, PROP_HUE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1745 
1746   obj_properties[PROP_SATURATION] =
1747       g_param_spec_float ("saturation", "Saturation",
1748       "The Saturation of the video",
1749       0, 10, PROP_SATURATION_DEFAULT,
1750       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1751 
1752   obj_properties[PROP_BRIGHTNESS] =
1753       g_param_spec_float ("brightness", "Brightness",
1754       "The Brightness of the video",
1755       -100, 100, PROP_BRIGHTNESS_DEFAULT,
1756       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1757 
1758   obj_properties[PROP_CONTRAST] =
1759       g_param_spec_float ("contrast", "Contrast",
1760       "The Contrast of the video",
1761       0, 10, PROP_CONTRAST_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1762 
1763   obj_properties[PROP_DETAIL] =
1764       g_param_spec_uint ("detail", "Detail",
1765       "The factor of detail/edge enhancement filter algorithm",
1766       0, 100, PROP_DETAIL_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1767 
1768   obj_properties[PROP_SCALING_MODE] =
1769       g_param_spec_enum ("scaling-mode", "Scaling Mode",
1770       "The Scaling mode to use", gst_msdkvpp_scaling_mode_get_type (),
1771       PROP_SCALING_MODE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1772 
1773   obj_properties[PROP_FORCE_ASPECT_RATIO] =
1774       g_param_spec_boolean ("force-aspect-ratio", "Force Aspect Ratio",
1775       "When enabled, scaling will respect original aspect ratio",
1776       PROP_FORCE_ASPECT_RATIO_DEFAULT,
1777       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1778 
1779   obj_properties[PROP_FRC_ALGORITHM] =
1780       g_param_spec_enum ("frc-algorithm", "FrameRateControl Algorithm",
1781       "The Framerate Control Alogorithm to use",
1782       gst_msdkvpp_frc_algorithm_get_type (), PROP_FRC_ALGORITHM_DEFAULT,
1783       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1784 
1785   /*
1786    * The video-direction to use, expressed as an enum value. See
1787    * #GstVideoOrientationMethod.
1788    */
1789   obj_properties[PROP_VIDEO_DIRECTION] = g_param_spec_enum ("video-direction",
1790       "Video Direction", "Video direction: rotation and flipping"
1791 #ifndef GST_REMOVE_DEPRECATED
1792       ", it will override both mirroring & rotation properties if set explicitly"
1793 #endif
1794       ,
1795       GST_TYPE_VIDEO_ORIENTATION_METHOD,
1796       PROP_VIDEO_DIRECTION_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1797 
1798   obj_properties[PROP_CROP_LEFT] = g_param_spec_uint ("crop-left",
1799       "Crop Left", "Pixels to crop at left",
1800       0, G_MAXUINT16, PROP_CROP_LEFT_DEFAULT,
1801       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1802 
1803   obj_properties[PROP_CROP_RIGHT] = g_param_spec_uint ("crop-right",
1804       "Crop Right", "Pixels to crop at right",
1805       0, G_MAXUINT16, PROP_CROP_RIGHT_DEFAULT,
1806       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1807 
1808   obj_properties[PROP_CROP_TOP] = g_param_spec_uint ("crop-top",
1809       "Crop Top", "Pixels to crop at top",
1810       0, G_MAXUINT16, PROP_CROP_TOP_DEFAULT,
1811       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1812 
1813   obj_properties[PROP_CROP_BOTTOM] = g_param_spec_uint ("crop-bottom",
1814       "Crop Bottom", "Pixels to crop at bottom",
1815       0, G_MAXUINT16, PROP_CROP_BOTTOM_DEFAULT,
1816       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1817 
1818   g_object_class_install_properties (gobject_class, PROP_N, obj_properties);
1819 }
1820 
1821 static void
gst_msdkvpp_init(GstMsdkVPP * thiz)1822 gst_msdkvpp_init (GstMsdkVPP * thiz)
1823 {
1824   thiz->initialized = FALSE;
1825   thiz->hardware = PROP_HARDWARE_DEFAULT;
1826   thiz->async_depth = PROP_ASYNC_DEPTH_DEFAULT;
1827   thiz->denoise_factor = PROP_DENOISE_DEFAULT;
1828 #ifndef GST_REMOVE_DEPRECATED
1829   thiz->rotation = PROP_ROTATION_DEFAULT;
1830   thiz->mirroring = PROP_MIRRORING_DEFAULT;
1831 #else
1832   thiz->rotation = MFX_ANGLE_0;
1833   thiz->mirroring = MFX_MIRRORING_DISABLED;
1834 #endif
1835   thiz->deinterlace_mode = PROP_DEINTERLACE_MODE_DEFAULT;
1836   thiz->deinterlace_method = PROP_DEINTERLACE_METHOD_DEFAULT;
1837   thiz->buffer_duration = GST_CLOCK_TIME_NONE;
1838   thiz->hue = PROP_HUE_DEFAULT;
1839   thiz->saturation = PROP_SATURATION_DEFAULT;
1840   thiz->brightness = PROP_BRIGHTNESS_DEFAULT;
1841   thiz->contrast = PROP_CONTRAST_DEFAULT;
1842   thiz->detail = PROP_DETAIL_DEFAULT;
1843   thiz->scaling_mode = PROP_SCALING_MODE_DEFAULT;
1844   thiz->keep_aspect = PROP_FORCE_ASPECT_RATIO_DEFAULT;
1845   thiz->frc_algm = PROP_FRC_ALGORITHM_DEFAULT;
1846   thiz->video_direction = PROP_VIDEO_DIRECTION_DEFAULT;
1847   thiz->crop_left = PROP_CROP_LEFT_DEFAULT;
1848   thiz->crop_right = PROP_CROP_RIGHT_DEFAULT;
1849   thiz->crop_top = PROP_CROP_TOP_DEFAULT;
1850   thiz->crop_bottom = PROP_CROP_BOTTOM_DEFAULT;
1851 
1852   gst_video_info_init (&thiz->sinkpad_info);
1853   gst_video_info_init (&thiz->srcpad_info);
1854 }
1855