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