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