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