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