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