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