2 * gstvaapifilter.c - Video processing abstraction
4 * Copyright (C) 2013-2014 Intel Corporation
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1
10 * of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
24 #include "gstvaapicompat.h"
25 #include "gstvaapifilter.h"
26 #include "gstvaapiutils.h"
27 #include "gstvaapivalue.h"
28 #include "gstvaapiminiobject.h"
29 #include "gstvaapidisplay_priv.h"
30 #include "gstvaapisurface_priv.h"
31 #include "gstvaapiutils_core.h"
33 #define GST_VAAPI_FILTER_CAST(obj) \
34 ((GstVaapiFilter *)(obj))
36 typedef struct _GstVaapiFilterOpData GstVaapiFilterOpData;
37 struct _GstVaapiFilterOpData
41 volatile gint ref_count;
52 struct _GstVaapiFilter
55 GstObject parent_instance;
57 GstVaapiDisplay *display;
60 VAContextID va_context;
61 GPtrArray *operations;
62 GstVideoFormat format;
63 GstVaapiScaleMethod scale_method;
64 GstVideoOrientationMethod video_direction;
65 GstVaapiConfigSurfaceAttributes *attribs;
66 GArray *forward_references;
67 GArray *backward_references;
68 GstVaapiRectangle crop_rect;
69 GstVaapiRectangle target_rect;
70 guint use_crop_rect:1;
71 guint use_target_rect:1;
73 guint32 rotation_flags;
76 typedef struct _GstVaapiFilterClass GstVaapiFilterClass;
77 struct _GstVaapiFilterClass
80 GstObjectClass parent_class;
83 /* Debug category for VaapiFilter */
84 GST_DEBUG_CATEGORY (gst_debug_vaapi_filter);
85 #define GST_CAT_DEFAULT gst_debug_vaapi_filter
88 GST_DEBUG_CATEGORY_INIT (gst_debug_vaapi_filter, "vaapifilter", 0, \
91 G_DEFINE_TYPE_WITH_CODE (GstVaapiFilter, gst_vaapi_filter, GST_TYPE_OBJECT,
94 /* ------------------------------------------------------------------------- */
95 /* --- VPP Types --- */
96 /* ------------------------------------------------------------------------- */
99 gst_vaapi_scale_method_get_type (void)
101 static gsize g_type = 0;
103 static const GEnumValue enum_values[] = {
104 {GST_VAAPI_SCALE_METHOD_DEFAULT,
105 "Default scaling mode", "default"},
106 {GST_VAAPI_SCALE_METHOD_FAST,
107 "Fast scaling mode", "fast"},
108 {GST_VAAPI_SCALE_METHOD_HQ,
109 "High quality scaling mode", "hq"},
113 if (g_once_init_enter (&g_type)) {
115 g_enum_register_static ("GstVaapiScaleMethod", enum_values);
116 g_once_init_leave (&g_type, type);
122 gst_vaapi_deinterlace_method_get_type (void)
124 static gsize g_type = 0;
126 static const GEnumValue enum_values[] = {
127 {GST_VAAPI_DEINTERLACE_METHOD_NONE,
128 "Disable deinterlacing", "none"},
129 {GST_VAAPI_DEINTERLACE_METHOD_BOB,
130 "Bob deinterlacing", "bob"},
131 {GST_VAAPI_DEINTERLACE_METHOD_WEAVE,
132 "Weave deinterlacing", "weave"},
133 {GST_VAAPI_DEINTERLACE_METHOD_MOTION_ADAPTIVE,
134 "Motion adaptive deinterlacing", "motion-adaptive"},
135 {GST_VAAPI_DEINTERLACE_METHOD_MOTION_COMPENSATED,
136 "Motion compensated deinterlacing", "motion-compensated"},
140 if (g_once_init_enter (&g_type)) {
142 g_enum_register_static ("GstVaapiDeinterlaceMethod", enum_values);
143 g_once_init_leave (&g_type, type);
149 gst_vaapi_deinterlace_flags_get_type (void)
151 static gsize g_type = 0;
153 static const GEnumValue enum_values[] = {
154 {GST_VAAPI_DEINTERLACE_FLAG_TFF,
155 "Top-field first", "top-field-first"},
156 {GST_VAAPI_DEINTERLACE_FLAG_ONEFIELD,
157 "One field", "one-field"},
158 {GST_VAAPI_DEINTERLACE_FLAG_TOPFIELD,
159 "Top field", "top-field"},
163 if (g_once_init_enter (&g_type)) {
165 g_enum_register_static ("GstVaapiDeinterlaceFlags", enum_values);
166 g_once_init_leave (&g_type, type);
171 /* ------------------------------------------------------------------------- */
172 /* --- VPP Helpers --- */
173 /* ------------------------------------------------------------------------- */
175 static VAProcFilterType *
176 vpp_get_filters_unlocked (GstVaapiFilter * filter, guint * num_filters_ptr)
178 VAProcFilterType *filters = NULL;
179 guint num_filters = 0;
182 num_filters = VAProcFilterCount;
183 filters = g_malloc_n (num_filters, sizeof (*filters));
187 va_status = vaQueryVideoProcFilters (filter->va_display, filter->va_context,
188 filters, &num_filters);
190 // Try to reallocate to the expected number of filters
191 if (va_status == VA_STATUS_ERROR_MAX_NUM_EXCEEDED) {
192 VAProcFilterType *const new_filters =
193 g_try_realloc_n (filters, num_filters, sizeof (*new_filters));
196 filters = new_filters;
198 va_status = vaQueryVideoProcFilters (filter->va_display,
199 filter->va_context, filters, &num_filters);
201 if (!vaapi_check_status (va_status, "vaQueryVideoProcFilters()"))
204 *num_filters_ptr = num_filters;
215 static VAProcFilterType *
216 vpp_get_filters (GstVaapiFilter * filter, guint * num_filters_ptr)
218 VAProcFilterType *filters;
220 GST_VAAPI_DISPLAY_LOCK (filter->display);
221 filters = vpp_get_filters_unlocked (filter, num_filters_ptr);
222 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
227 vpp_get_filter_caps_unlocked (GstVaapiFilter * filter, VAProcFilterType type,
228 guint cap_size, guint * num_caps_ptr)
234 caps = g_malloc (cap_size);
238 va_status = vaQueryVideoProcFilterCaps (filter->va_display,
239 filter->va_context, type, caps, &num_caps);
241 // Try to reallocate to the expected number of filters
242 if (va_status == VA_STATUS_ERROR_MAX_NUM_EXCEEDED) {
243 gpointer const new_caps = g_try_realloc_n (caps, num_caps, cap_size);
248 va_status = vaQueryVideoProcFilterCaps (filter->va_display,
249 filter->va_context, type, caps, &num_caps);
251 if (!vaapi_check_status (va_status, "vaQueryVideoProcFilterCaps()"))
254 *num_caps_ptr = num_caps;
266 vpp_get_filter_caps (GstVaapiFilter * filter, VAProcFilterType type,
267 guint cap_size, guint * num_caps_ptr)
271 GST_VAAPI_DISPLAY_LOCK (filter->display);
272 caps = vpp_get_filter_caps_unlocked (filter, type, cap_size, num_caps_ptr);
273 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
278 vpp_get_pipeline_caps_unlocked (GstVaapiFilter * filter)
280 #if VA_CHECK_VERSION(1,1,0)
281 VAProcPipelineCaps pipeline_caps = { 0, };
283 VAStatus va_status = vaQueryVideoProcPipelineCaps (filter->va_display,
284 filter->va_context, NULL, 0, &pipeline_caps);
286 if (vaapi_check_status (va_status, "vaQueryVideoProcPipelineCaps()")) {
287 filter->mirror_flags = pipeline_caps.mirror_flags;
288 filter->rotation_flags = pipeline_caps.rotation_flags;
293 filter->mirror_flags = 0;
294 filter->rotation_flags = 0;
298 vpp_get_pipeline_caps (GstVaapiFilter * filter)
300 GST_VAAPI_DISPLAY_LOCK (filter->display);
301 vpp_get_pipeline_caps_unlocked (filter);
302 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
305 /* ------------------------------------------------------------------------- */
306 /* --- VPP Operations --- */
307 /* ------------------------------------------------------------------------- */
309 #define DEFAULT_FORMAT GST_VIDEO_FORMAT_UNKNOWN
311 #define OP_DATA_DEFAULT_VALUE(type, op_data) \
312 g_value_get_##type (g_param_spec_get_default_value (op_data->pspec))
314 #define OP_RET_DEFAULT_VALUE(type, filter, op) \
316 g_return_val_if_fail (filter != NULL, FALSE); \
317 return OP_DATA_DEFAULT_VALUE (type, find_operation (filter, op)); \
329 PROP_FORMAT = GST_VAAPI_FILTER_OP_FORMAT,
330 PROP_CROP = GST_VAAPI_FILTER_OP_CROP,
331 PROP_DENOISE = GST_VAAPI_FILTER_OP_DENOISE,
332 PROP_SHARPEN = GST_VAAPI_FILTER_OP_SHARPEN,
333 PROP_HUE = GST_VAAPI_FILTER_OP_HUE,
334 PROP_SATURATION = GST_VAAPI_FILTER_OP_SATURATION,
335 PROP_BRIGHTNESS = GST_VAAPI_FILTER_OP_BRIGHTNESS,
336 PROP_CONTRAST = GST_VAAPI_FILTER_OP_CONTRAST,
337 PROP_DEINTERLACING = GST_VAAPI_FILTER_OP_DEINTERLACING,
338 PROP_SCALING = GST_VAAPI_FILTER_OP_SCALING,
339 PROP_VIDEO_DIRECTION = GST_VAAPI_FILTER_OP_VIDEO_DIRECTION,
340 #ifndef GST_REMOVE_DEPRECATED
341 PROP_SKINTONE = GST_VAAPI_FILTER_OP_SKINTONE,
343 PROP_SKINTONE_LEVEL = GST_VAAPI_FILTER_OP_SKINTONE_LEVEL,
348 static GParamSpec *g_properties[N_PROPERTIES] = { NULL, };
350 static gsize g_properties_initialized = FALSE;
353 init_properties (void)
356 * GstVaapiFilter:format:
358 * The forced output pixel format, expressed as a #GstVideoFormat.
360 g_properties[PROP_FORMAT] = g_param_spec_enum ("format",
362 "The forced output pixel format",
363 GST_TYPE_VIDEO_FORMAT,
364 DEFAULT_FORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
367 * GstVaapiFilter:crop-rect:
369 * The cropping rectangle, expressed as a #GstVaapiRectangle.
371 g_properties[PROP_CROP] = g_param_spec_boxed ("crop-rect",
372 "Cropping Rectangle",
373 "The cropping rectangle",
374 GST_VAAPI_TYPE_RECTANGLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
377 * GstVaapiFilter:denoise:
379 * The level of noise reduction to apply.
381 g_properties[PROP_DENOISE] = g_param_spec_float ("denoise",
383 "The level of denoising to apply",
384 0.0, 1.0, 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
387 * GstVaapiFilter:sharpen:
389 * The level of sharpening to apply for positive values, or the
390 * level of blurring for negative values.
392 g_properties[PROP_SHARPEN] = g_param_spec_float ("sharpen",
394 "The level of sharpening/blurring to apply",
395 -1.0, 1.0, 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
398 * GstVaapiFilter:hue:
400 * The color hue, expressed as a float value. Range is -180.0 to
401 * 180.0. Default value is 0.0 and represents no modification.
403 g_properties[PROP_HUE] = g_param_spec_float ("hue",
405 "The color hue value",
406 -180.0, 180.0, 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
409 * GstVaapiFilter:saturation:
411 * The color saturation, expressed as a float value. Range is 0.0 to
412 * 2.0. Default value is 1.0 and represents no modification.
414 g_properties[PROP_SATURATION] = g_param_spec_float ("saturation",
416 "The color saturation value",
417 0.0, 2.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
420 * GstVaapiFilter:brightness:
422 * The color brightness, expressed as a float value. Range is -1.0
423 * to 1.0. Default value is 0.0 and represents no modification.
425 g_properties[PROP_BRIGHTNESS] = g_param_spec_float ("brightness",
427 "The color brightness value",
428 -1.0, 1.0, 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
431 * GstVaapiFilter:contrast:
433 * The color contrast, expressed as a float value. Range is 0.0 to
434 * 2.0. Default value is 1.0 and represents no modification.
436 g_properties[PROP_CONTRAST] = g_param_spec_float ("contrast",
438 "The color contrast value",
439 0.0, 2.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
442 * GstVaapiFilter:deinterlace-method:
444 * The deinterlacing algorithm to apply, expressed a an enum
445 * value. See #GstVaapiDeinterlaceMethod.
447 g_properties[PROP_DEINTERLACING] = g_param_spec_enum ("deinterlace",
448 "Deinterlacing Method",
449 "Deinterlacing method to apply",
450 GST_VAAPI_TYPE_DEINTERLACE_METHOD,
451 GST_VAAPI_DEINTERLACE_METHOD_NONE,
452 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
455 * GstVaapiFilter:scale-method:
457 * The scaling method to use, expressed as an enum value. See
458 * #GstVaapiScaleMethod.
460 g_properties[PROP_SCALING] = g_param_spec_enum ("scale-method",
462 "Scaling method to use",
463 GST_VAAPI_TYPE_SCALE_METHOD,
464 GST_VAAPI_SCALE_METHOD_DEFAULT,
465 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
468 * GstVaapiFilter:video-direction:
470 * The video-direction to use, expressed as an enum value. See
471 * #GstVideoOrientationMethod.
473 g_properties[PROP_VIDEO_DIRECTION] = g_param_spec_enum ("video-direction",
475 "Video direction: rotation and flipping",
476 GST_TYPE_VIDEO_ORIENTATION_METHOD,
477 GST_VIDEO_ORIENTATION_IDENTITY,
478 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
480 #ifndef GST_REMOVE_DEPRECATED
482 * GstVaapiFilter:skin-tone-enhancement:
484 * Apply the skin tone enhancement algorithm.
486 g_properties[PROP_SKINTONE] = g_param_spec_boolean ("skin-tone-enhancement",
487 "Skin tone enhancement",
488 "Apply the skin tone enhancement algorithm",
489 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
493 * GstVaapiFilter:skin-tone-enhancement-level:
495 * Apply the skin tone enhancement algorithm with specified value.
497 g_properties[PROP_SKINTONE_LEVEL] =
498 g_param_spec_uint ("skin-tone-enhancement-level",
499 "Skin tone enhancement level",
500 "Apply the skin tone enhancement algorithm with specified level", 0, 9, 3,
501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
505 ensure_properties (void)
507 if (g_once_init_enter (&g_properties_initialized)) {
509 g_once_init_leave (&g_properties_initialized, TRUE);
514 op_data_free (GstVaapiFilterOpData * op_data)
516 g_free (op_data->va_caps);
517 g_slice_free (GstVaapiFilterOpData, op_data);
520 static inline gpointer
521 op_data_new (GstVaapiFilterOp op, GParamSpec * pspec)
523 GstVaapiFilterOpData *op_data;
525 op_data = g_slice_new0 (GstVaapiFilterOpData);
530 op_data->pspec = pspec;
531 op_data->ref_count = 1;
532 op_data->va_buffer = VA_INVALID_ID;
535 case GST_VAAPI_FILTER_OP_FORMAT:
536 case GST_VAAPI_FILTER_OP_CROP:
537 case GST_VAAPI_FILTER_OP_SCALING:
538 case GST_VAAPI_FILTER_OP_VIDEO_DIRECTION:
539 op_data->va_type = VAProcFilterNone;
541 case GST_VAAPI_FILTER_OP_DENOISE:
542 op_data->va_type = VAProcFilterNoiseReduction;
543 op_data->va_cap_size = sizeof (VAProcFilterCap);
544 op_data->va_buffer_size = sizeof (VAProcFilterParameterBuffer);
546 case GST_VAAPI_FILTER_OP_SHARPEN:
547 op_data->va_type = VAProcFilterSharpening;
548 op_data->va_cap_size = sizeof (VAProcFilterCap);
549 op_data->va_buffer_size = sizeof (VAProcFilterParameterBuffer);
551 #ifndef GST_REMOVE_DEPRECATED
552 case GST_VAAPI_FILTER_OP_SKINTONE:
554 case GST_VAAPI_FILTER_OP_SKINTONE_LEVEL:
555 op_data->va_type = VAProcFilterSkinToneEnhancement;
556 op_data->va_buffer_size = sizeof (VAProcFilterParameterBuffer);
558 case GST_VAAPI_FILTER_OP_HUE:
559 op_data->va_subtype = VAProcColorBalanceHue;
560 goto op_colorbalance;
561 case GST_VAAPI_FILTER_OP_SATURATION:
562 op_data->va_subtype = VAProcColorBalanceSaturation;
563 goto op_colorbalance;
564 case GST_VAAPI_FILTER_OP_BRIGHTNESS:
565 op_data->va_subtype = VAProcColorBalanceBrightness;
566 goto op_colorbalance;
567 case GST_VAAPI_FILTER_OP_CONTRAST:
568 op_data->va_subtype = VAProcColorBalanceContrast;
570 op_data->va_type = VAProcFilterColorBalance;
571 op_data->va_cap_size = sizeof (VAProcFilterCapColorBalance);
572 op_data->va_buffer_size =
573 sizeof (VAProcFilterParameterBufferColorBalance);
575 case GST_VAAPI_FILTER_OP_DEINTERLACING:
576 op_data->va_type = VAProcFilterDeinterlacing;
577 op_data->va_cap_size = sizeof (VAProcFilterCapDeinterlacing);
578 op_data->va_buffer_size =
579 sizeof (VAProcFilterParameterBufferDeinterlacing);
582 g_assert (0 && "unsupported operation");
590 op_data_free (op_data);
595 static inline gpointer
596 op_data_ref (gpointer data)
598 GstVaapiFilterOpData *const op_data = data;
600 g_return_val_if_fail (op_data != NULL, NULL);
602 g_atomic_int_inc (&op_data->ref_count);
607 op_data_unref (gpointer data)
609 GstVaapiFilterOpData *const op_data = data;
611 g_return_if_fail (op_data != NULL);
612 g_return_if_fail (op_data->ref_count > 0);
614 if (g_atomic_int_dec_and_test (&op_data->ref_count))
615 op_data_free (op_data);
618 /* Ensure capability info is set up for the VA filter we are interested in */
620 op_data_ensure_caps (GstVaapiFilterOpData * op_data, gpointer filter_caps,
621 guint num_filter_caps)
623 guchar *filter_cap = filter_caps;
624 guint i, va_num_caps = num_filter_caps;
626 // Find the VA filter cap matching the op info sub-type
627 if (op_data->va_subtype) {
628 for (i = 0; i < num_filter_caps; i++) {
629 /* XXX: sub-type shall always be the first field */
630 if (op_data->va_subtype == *(guint *) filter_cap) {
634 filter_cap += op_data->va_cap_size;
636 if (i == num_filter_caps)
640 op_data->va_caps = g_memdup (filter_cap, op_data->va_cap_size * va_num_caps);
641 if (!op_data->va_caps)
644 op_data->va_num_caps = va_num_caps;
648 /* Scale the filter value wrt. library spec and VA driver spec */
650 op_data_get_value_float (GstVaapiFilterOpData * op_data,
651 const VAProcFilterValueRange * range, gfloat value, gfloat * out_value_ptr)
653 GParamSpecFloat *const pspec = G_PARAM_SPEC_FLOAT (op_data->pspec);
656 g_return_val_if_fail (range != NULL, FALSE);
657 g_return_val_if_fail (out_value_ptr != NULL, FALSE);
659 if (value < pspec->minimum || value > pspec->maximum)
662 // Scale wrt. the medium ("default") value
663 out_value = range->default_value;
664 if (value > pspec->default_value)
665 out_value += ((value - pspec->default_value) /
666 (pspec->maximum - pspec->default_value) *
667 (range->max_value - range->default_value));
668 else if (value < pspec->default_value)
669 out_value -= ((pspec->default_value - value) /
670 (pspec->default_value - pspec->minimum) *
671 (range->default_value - range->min_value));
673 *out_value_ptr = out_value;
677 /* Get default list of operations supported by the library */
679 get_operations_default (void)
684 ops = g_ptr_array_new_full (N_PROPERTIES, op_data_unref);
688 ensure_properties ();
690 for (i = 0; i < N_PROPERTIES; i++) {
691 GstVaapiFilterOpData *op_data;
692 GParamSpec *const pspec = g_properties[i];
696 op_data = op_data_new (i, pspec);
699 g_ptr_array_add (ops, op_data);
706 g_ptr_array_unref (ops);
711 /* Get the ordered list of operations, based on VA/VPP queries */
713 get_operations_ordered (GstVaapiFilter * filter, GPtrArray * default_ops)
716 VAProcFilterType *filters;
717 gpointer filter_caps = NULL;
718 guint i, j, num_filters, num_filter_caps = 0;
720 ops = g_ptr_array_new_full (default_ops->len, op_data_unref);
724 filters = vpp_get_filters (filter, &num_filters);
728 // Append virtual ops first, i.e. those without an associated VA filter
729 for (i = 0; i < default_ops->len; i++) {
730 GstVaapiFilterOpData *const op_data = g_ptr_array_index (default_ops, i);
731 if (op_data->va_type == VAProcFilterNone)
732 g_ptr_array_add (ops, op_data_ref (op_data));
735 // Append ops, while preserving the VA filters ordering
736 for (i = 0; i < num_filters; i++) {
737 const VAProcFilterType va_type = filters[i];
738 if (va_type == VAProcFilterNone)
741 for (j = 0; j < default_ops->len; j++) {
742 GstVaapiFilterOpData *const op_data = g_ptr_array_index (default_ops, j);
743 if (op_data->va_type != va_type)
746 if (op_data->va_cap_size == 0) { /* no caps, like skintone */
747 g_ptr_array_add (ops, op_data_ref (op_data));
752 filter_caps = vpp_get_filter_caps (filter, va_type,
753 op_data->va_cap_size, &num_filter_caps);
757 if (!op_data_ensure_caps (op_data, filter_caps, num_filter_caps))
759 g_ptr_array_add (ops, op_data_ref (op_data));
765 vpp_get_pipeline_caps (filter);
767 if (filter->operations)
768 g_ptr_array_unref (filter->operations);
769 filter->operations = g_ptr_array_ref (ops);
772 g_ptr_array_unref (default_ops);
778 g_free (filter_caps);
780 g_ptr_array_unref (ops);
781 g_ptr_array_unref (default_ops);
786 /* Determine the set of supported VPP operations by the specific
787 filter, or known to this library if filter is NULL */
789 get_operations (GstVaapiFilter * filter)
793 if (filter && filter->operations)
794 return g_ptr_array_ref (filter->operations);
796 ops = get_operations_default ();
799 return filter ? get_operations_ordered (filter, ops) : ops;
802 /* Ensure the set of supported VPP operations is cached into the
803 GstVaapiFilter::operations member */
804 static inline gboolean
805 ensure_operations (GstVaapiFilter * filter)
812 if (filter->operations)
815 ops = get_operations (filter);
819 g_ptr_array_unref (ops);
823 /* Find whether the VPP operation is supported or not */
824 static GstVaapiFilterOpData *
825 find_operation (GstVaapiFilter * filter, GstVaapiFilterOp op)
829 if (!ensure_operations (filter))
832 for (i = 0; i < filter->operations->len; i++) {
833 GstVaapiFilterOpData *const op_data =
834 g_ptr_array_index (filter->operations, i);
835 if (op_data->op == op)
841 /* Ensure the operation's VA buffer is allocated */
842 static inline gboolean
843 op_ensure_n_elements_buffer (GstVaapiFilter * filter,
844 GstVaapiFilterOpData * op_data, gint op_num)
846 if (G_LIKELY (op_data->va_buffer != VA_INVALID_ID))
848 return vaapi_create_n_elements_buffer (filter->va_display, filter->va_context,
849 VAProcFilterParameterBufferType, op_data->va_buffer_size, NULL,
850 &op_data->va_buffer, NULL, op_num);
853 static inline gboolean
854 op_ensure_buffer (GstVaapiFilter * filter, GstVaapiFilterOpData * op_data)
856 return op_ensure_n_elements_buffer (filter, op_data, 1);
859 /* Update a generic filter (float value) */
861 op_set_generic_unlocked (GstVaapiFilter * filter,
862 GstVaapiFilterOpData * op_data, gfloat value)
864 VAProcFilterParameterBuffer *buf;
865 VAProcFilterCap *filter_cap;
868 if (!op_data || !op_ensure_buffer (filter, op_data))
871 op_data->is_enabled =
872 (value != G_PARAM_SPEC_FLOAT (op_data->pspec)->default_value);
873 if (!op_data->is_enabled)
876 filter_cap = op_data->va_caps;
877 if (!op_data_get_value_float (op_data, &filter_cap->range, value, &va_value))
880 buf = vaapi_map_buffer (filter->va_display, op_data->va_buffer);
884 buf->type = op_data->va_type;
885 buf->value = va_value;
886 vaapi_unmap_buffer (filter->va_display, op_data->va_buffer, NULL);
890 static inline gboolean
891 op_set_generic (GstVaapiFilter * filter, GstVaapiFilterOpData * op_data,
894 gboolean success = FALSE;
896 GST_VAAPI_DISPLAY_LOCK (filter->display);
897 success = op_set_generic_unlocked (filter, op_data, value);
898 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
902 /* Update the color balance filter */
903 #define COLOR_BALANCE_NUM \
904 GST_VAAPI_FILTER_OP_CONTRAST - GST_VAAPI_FILTER_OP_HUE + 1
907 op_set_color_balance_unlocked (GstVaapiFilter * filter,
908 GstVaapiFilterOpData * op_data, gfloat value)
910 VAProcFilterParameterBufferColorBalance *buf;
911 VAProcFilterCapColorBalance *filter_cap;
914 GstVaapiFilterOpData *color_data[COLOR_BALANCE_NUM];
915 GstVaapiFilterOpData *enabled_data = NULL;
921 /* collect all the Color Balance operators and find the first
923 for (i = 0; i < COLOR_BALANCE_NUM; i++) {
924 color_data[i] = find_operation (filter, GST_VAAPI_FILTER_OP_HUE + i);
928 if (!enabled_data && color_data[i]->is_enabled)
929 enabled_data = color_data[i];
932 /* If there's no enabled operators let's enable this one.
934 * HACK: This operator will be the only one with an allocated buffer
935 * which will store all the color balance operators.
938 if (value == G_PARAM_SPEC_FLOAT (op_data->pspec)->default_value)
941 if (!op_ensure_n_elements_buffer (filter, op_data, COLOR_BALANCE_NUM))
944 enabled_data = op_data;
946 buf = vaapi_map_buffer (filter->va_display, enabled_data->va_buffer);
950 /* Write all the color balance operator values in the buffer. --
951 * Use the default value for all the operators except the set
953 for (i = 0; i < COLOR_BALANCE_NUM; i++) {
954 buf[i].type = color_data[i]->va_type;
955 buf[i].attrib = color_data[i]->va_subtype;
957 va_value = G_PARAM_SPEC_FLOAT (color_data[i]->pspec)->default_value;
958 if (color_data[i]->op == op_data->op) {
959 filter_cap = color_data[i]->va_caps;
960 /* fail but ignore current value and set default one */
961 if (!op_data_get_value_float (color_data[i], &filter_cap->range, value,
966 buf[i].value = va_value;
969 enabled_data->is_enabled = 1;
971 /* There's already one operator enabled, *in theory* with a
972 * buffer associated. */
973 if (G_UNLIKELY (enabled_data->va_buffer == VA_INVALID_ID))
976 filter_cap = op_data->va_caps;
977 if (!op_data_get_value_float (op_data, &filter_cap->range, value,
981 buf = vaapi_map_buffer (filter->va_display, enabled_data->va_buffer);
985 buf[op_data->op - GST_VAAPI_FILTER_OP_HUE].value = va_value;
988 vaapi_unmap_buffer (filter->va_display, enabled_data->va_buffer, NULL);
993 static inline gboolean
994 op_set_color_balance (GstVaapiFilter * filter, GstVaapiFilterOpData * op_data,
997 gboolean success = FALSE;
999 GST_VAAPI_DISPLAY_LOCK (filter->display);
1000 success = op_set_color_balance_unlocked (filter, op_data, value);
1001 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
1005 /* Update deinterlace filter */
1007 op_set_deinterlace_unlocked (GstVaapiFilter * filter,
1008 GstVaapiFilterOpData * op_data, GstVaapiDeinterlaceMethod method,
1011 VAProcFilterParameterBufferDeinterlacing *buf;
1012 const VAProcFilterCapDeinterlacing *filter_caps;
1013 VAProcDeinterlacingType algorithm;
1016 if (!op_data || !op_ensure_buffer (filter, op_data))
1019 op_data->is_enabled = (method != GST_VAAPI_DEINTERLACE_METHOD_NONE);
1020 if (!op_data->is_enabled)
1023 algorithm = from_GstVaapiDeinterlaceMethod (method);
1024 for (i = 0, filter_caps = op_data->va_caps; i < op_data->va_num_caps; i++) {
1025 if (filter_caps[i].type == algorithm)
1028 if (i == op_data->va_num_caps)
1031 buf = vaapi_map_buffer (filter->va_display, op_data->va_buffer);
1035 buf->type = op_data->va_type;
1036 buf->algorithm = algorithm;
1037 buf->flags = from_GstVaapiDeinterlaceFlags (flags);
1038 vaapi_unmap_buffer (filter->va_display, op_data->va_buffer, NULL);
1042 static inline gboolean
1043 op_set_deinterlace (GstVaapiFilter * filter, GstVaapiFilterOpData * op_data,
1044 GstVaapiDeinterlaceMethod method, guint flags)
1046 gboolean success = FALSE;
1048 GST_VAAPI_DISPLAY_LOCK (filter->display);
1049 success = op_set_deinterlace_unlocked (filter, op_data, method, flags);
1050 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
1054 /* Update skin tone enhancement level */
1056 op_set_skintone_level_unlocked (GstVaapiFilter * filter,
1057 GstVaapiFilterOpData * op_data, guint value)
1059 VAProcFilterParameterBuffer *buf;
1061 if (!op_data || !op_ensure_buffer (filter, op_data))
1064 op_data->is_enabled = 1;
1066 buf = vaapi_map_buffer (filter->va_display, op_data->va_buffer);
1069 buf->type = op_data->va_type;
1071 vaapi_unmap_buffer (filter->va_display, op_data->va_buffer, NULL);
1075 static inline gboolean
1076 op_set_skintone_level (GstVaapiFilter * filter,
1077 GstVaapiFilterOpData * op_data, guint value)
1079 gboolean success = FALSE;
1081 GST_VAAPI_DISPLAY_LOCK (filter->display);
1082 success = op_set_skintone_level_unlocked (filter, op_data, value);
1083 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
1087 #ifndef GST_REMOVE_DEPRECATED
1088 /* Update skin tone enhancement */
1090 op_set_skintone_unlocked (GstVaapiFilter * filter,
1091 GstVaapiFilterOpData * op_data, gboolean value)
1097 op_data->is_enabled = 0;
1101 return op_set_skintone_level_unlocked (filter, op_data, 3);
1104 static inline gboolean
1105 op_set_skintone (GstVaapiFilter * filter, GstVaapiFilterOpData * op_data,
1108 gboolean success = FALSE;
1110 GST_VAAPI_DISPLAY_LOCK (filter->display);
1111 success = op_set_skintone_unlocked (filter, op_data, enhance);
1112 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
1118 deint_refs_set (GArray * refs, GstVaapiSurface ** surfaces, guint num_surfaces)
1122 if (num_surfaces > 0 && !surfaces)
1125 for (i = 0; i < num_surfaces; i++)
1126 g_array_append_val (refs, GST_VAAPI_OBJECT_ID (surfaces[i]));
1131 deint_refs_clear (GArray * refs)
1134 g_array_remove_range (refs, 0, refs->len);
1138 deint_refs_clear_all (GstVaapiFilter * filter)
1140 deint_refs_clear (filter->forward_references);
1141 deint_refs_clear (filter->backward_references);
1144 /* ------------------------------------------------------------------------- */
1145 /* --- Surface Attribs --- */
1146 /* ------------------------------------------------------------------------- */
1149 ensure_attributes (GstVaapiFilter * filter)
1151 if (G_LIKELY (filter->attribs))
1154 filter->attribs = gst_vaapi_config_surface_attributes_get (filter->display,
1156 return (filter->attribs != NULL);
1159 static inline gboolean
1160 is_special_format (GstVideoFormat format)
1162 return format == GST_VIDEO_FORMAT_UNKNOWN ||
1163 format == GST_VIDEO_FORMAT_ENCODED;
1167 find_format (GstVaapiFilter * filter, GstVideoFormat format)
1172 formats = filter->attribs->formats;
1173 if (is_special_format (format) || !formats)
1176 for (i = 0; i < formats->len; i++) {
1177 if (g_array_index (formats, GstVideoFormat, i) == format)
1183 /* ------------------------------------------------------------------------- */
1184 /* --- Interface --- */
1185 /* ------------------------------------------------------------------------- */
1188 gst_vaapi_filter_init (GstVaapiFilter * filter)
1190 filter->va_config = VA_INVALID_ID;
1191 filter->va_context = VA_INVALID_ID;
1192 filter->format = DEFAULT_FORMAT;
1194 filter->forward_references =
1195 g_array_sized_new (FALSE, FALSE, sizeof (VASurfaceID), 4);
1197 filter->backward_references =
1198 g_array_sized_new (FALSE, FALSE, sizeof (VASurfaceID), 4);
1202 gst_vaapi_filter_initialize (GstVaapiFilter * filter)
1206 if (!filter->display)
1209 va_status = vaCreateConfig (filter->va_display, VAProfileNone,
1210 VAEntrypointVideoProc, NULL, 0, &filter->va_config);
1211 if (!vaapi_check_status (va_status, "vaCreateConfig() [VPP]"))
1214 va_status = vaCreateContext (filter->va_display, filter->va_config, 0, 0, 0,
1215 NULL, 0, &filter->va_context);
1216 if (!vaapi_check_status (va_status, "vaCreateContext() [VPP]"))
1222 gst_vaapi_filter_finalize (GObject * object)
1224 GstVaapiFilter *const filter = GST_VAAPI_FILTER (object);
1227 GST_VAAPI_DISPLAY_LOCK (filter->display);
1228 if (filter->operations) {
1229 for (i = 0; i < filter->operations->len; i++) {
1230 GstVaapiFilterOpData *const op_data =
1231 g_ptr_array_index (filter->operations, i);
1232 vaapi_destroy_buffer (filter->va_display, &op_data->va_buffer);
1234 g_ptr_array_unref (filter->operations);
1235 filter->operations = NULL;
1238 if (filter->va_context != VA_INVALID_ID) {
1239 vaDestroyContext (filter->va_display, filter->va_context);
1240 filter->va_context = VA_INVALID_ID;
1243 if (filter->va_config != VA_INVALID_ID) {
1244 vaDestroyConfig (filter->va_display, filter->va_config);
1245 filter->va_config = VA_INVALID_ID;
1247 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
1248 gst_vaapi_display_replace (&filter->display, NULL);
1250 if (filter->forward_references) {
1251 g_array_unref (filter->forward_references);
1252 filter->forward_references = NULL;
1255 if (filter->backward_references) {
1256 g_array_unref (filter->backward_references);
1257 filter->backward_references = NULL;
1260 if (filter->attribs) {
1261 gst_vaapi_config_surface_attributes_free (filter->attribs);
1262 filter->attribs = NULL;
1265 G_OBJECT_CLASS (gst_vaapi_filter_parent_class)->finalize (object);
1269 gst_vaapi_filter_set_property (GObject * object, guint property_id,
1270 const GValue * value, GParamSpec * pspec)
1272 GstVaapiFilter *const filter = GST_VAAPI_FILTER (object);
1274 switch (property_id) {
1276 GstVaapiDisplay *display = g_value_get_object (value);;
1279 if (GST_VAAPI_DISPLAY_HAS_VPP (display)) {
1280 filter->display = gst_object_ref (display);
1281 filter->va_display = GST_VAAPI_DISPLAY_VADISPLAY (filter->display);
1283 GST_WARNING_OBJECT (filter, "VA display doesn't support VPP");
1289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1294 gst_vaapi_filter_get_property (GObject * object, guint property_id,
1295 GValue * value, GParamSpec * pspec)
1297 GstVaapiFilter *const filter = GST_VAAPI_FILTER (object);
1299 switch (property_id) {
1301 g_value_set_object (value, filter->display);
1304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1309 gst_vaapi_filter_class_init (GstVaapiFilterClass * klass)
1311 GObjectClass *const object_class = G_OBJECT_CLASS (klass);
1313 object_class->set_property = gst_vaapi_filter_set_property;
1314 object_class->get_property = gst_vaapi_filter_get_property;
1315 object_class->finalize = gst_vaapi_filter_finalize;
1318 * GstVaapiFilter:display:
1320 * #GstVaapiDisplay to be used.
1322 g_object_class_install_property (object_class, PROP_DISPLAY,
1323 g_param_spec_object ("display", "Gst VA-API Display",
1324 "The VA-API display object to use", GST_TYPE_VAAPI_DISPLAY,
1325 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME));
1329 * gst_vaapi_filter_new:
1330 * @display: a #GstVaapiDisplay
1332 * Creates a new #GstVaapiFilter set up to operate in "identity"
1333 * mode. This means that no other operation than scaling is performed.
1335 * Return value: the newly created #GstVaapiFilter object
1338 gst_vaapi_filter_new (GstVaapiDisplay * display)
1340 GstVaapiFilter *filter;
1342 filter = g_object_new (GST_TYPE_VAAPI_FILTER, "display", display, NULL);
1343 if (!gst_vaapi_filter_initialize (filter))
1350 gst_object_unref (filter);
1356 * gst_vaapi_filter_replace:
1357 * @old_filter_ptr: a pointer to a #GstVaapiFilter
1358 * @new_filter: a #GstVaapiFilter
1360 * Atomically replaces the filter held in @old_filter_ptr with
1361 * @new_filter. This means that @old_filter_ptr shall reference a
1362 * valid filter. However, @new_filter can be NULL.
1365 gst_vaapi_filter_replace (GstVaapiFilter ** old_filter_ptr,
1366 GstVaapiFilter * new_filter)
1368 g_return_if_fail (old_filter_ptr != NULL);
1370 gst_object_replace ((GstObject **) old_filter_ptr, GST_OBJECT (new_filter));
1374 * gst_vaapi_filter_get_operations:
1375 * @filter: a #GstVaapiFilter, or %NULL
1377 * Determines the set of supported operations for video processing.
1378 * The caller owns an extra reference to the resulting array of
1379 * #GstVaapiFilterOpInfo elements, so it shall be released with
1380 * g_ptr_array_unref() after usage.
1382 * If @filter is %NULL, then this function returns the video
1383 * processing operations supported by this library.
1385 * Return value: the set of supported operations, or %NULL if an error
1389 gst_vaapi_filter_get_operations (GstVaapiFilter * filter)
1391 return get_operations (filter);
1395 * gst_vaapi_filter_has_operation:
1396 * @filter: a #GstVaapiFilter
1397 * @op: a #GstVaapiFilterOp
1399 * Determines whether the underlying VA driver advertises support for
1400 * the supplied operation @op.
1402 * Return value: %TRUE if the specified operation may be supported by
1403 * the underlying hardware, %FALSE otherwise
1406 gst_vaapi_filter_has_operation (GstVaapiFilter * filter, GstVaapiFilterOp op)
1408 g_return_val_if_fail (filter != NULL, FALSE);
1410 return find_operation (filter, op) != NULL;
1414 * gst_vaapi_filter_use_operation:
1415 * @filter: a #GstVaapiFilter
1416 * @op: a #GstVaapiFilterOp
1418 * Determines whether the supplied operation @op was already enabled
1419 * through a prior call to gst_vaapi_filter_set_operation() or any
1420 * other operation-specific function.
1422 * Note: should an operation be set to its default value, this means
1423 * that it is actually not enabled.
1425 * Return value: %TRUE if the specified operation was already enabled,
1429 gst_vaapi_filter_use_operation (GstVaapiFilter * filter, GstVaapiFilterOp op)
1431 GstVaapiFilterOpData *op_data;
1433 g_return_val_if_fail (filter != NULL, FALSE);
1435 op_data = find_operation (filter, op);
1438 return op_data->is_enabled;
1442 * gst_vaapi_filter_set_operation:
1443 * @filter: a #GstVaapiFilter
1444 * @op: a #GstVaapiFilterOp
1445 * @value: the @op settings
1447 * Enable the specified operation @op to be performed during video
1448 * processing, i.e. in gst_vaapi_filter_process(). The @value argument
1449 * specifies the operation settings. e.g. deinterlacing method for
1450 * deinterlacing, denoising level for noise reduction, etc.
1452 * If @value is %NULL, then this function resets the operation
1453 * settings to their default values.
1455 * Return value: %TRUE if the specified operation may be supported,
1459 gst_vaapi_filter_set_operation (GstVaapiFilter * filter, GstVaapiFilterOp op,
1460 const GValue * value)
1462 GstVaapiFilterOpData *op_data;
1464 g_return_val_if_fail (filter != NULL, FALSE);
1466 op_data = find_operation (filter, op);
1470 if (value && !G_VALUE_HOLDS (value, G_PARAM_SPEC_VALUE_TYPE (op_data->pspec)))
1474 case GST_VAAPI_FILTER_OP_FORMAT:
1475 return gst_vaapi_filter_set_format (filter, value ?
1476 g_value_get_enum (value) : DEFAULT_FORMAT);
1477 case GST_VAAPI_FILTER_OP_CROP:
1478 return gst_vaapi_filter_set_cropping_rectangle (filter, value ?
1479 g_value_get_boxed (value) : NULL);
1480 case GST_VAAPI_FILTER_OP_DENOISE:
1481 case GST_VAAPI_FILTER_OP_SHARPEN:
1482 return op_set_generic (filter, op_data,
1483 (value ? g_value_get_float (value) :
1484 G_PARAM_SPEC_FLOAT (op_data->pspec)->default_value));
1485 case GST_VAAPI_FILTER_OP_HUE:
1486 case GST_VAAPI_FILTER_OP_SATURATION:
1487 case GST_VAAPI_FILTER_OP_BRIGHTNESS:
1488 case GST_VAAPI_FILTER_OP_CONTRAST:
1489 return op_set_color_balance (filter, op_data,
1490 (value ? g_value_get_float (value) :
1491 G_PARAM_SPEC_FLOAT (op_data->pspec)->default_value));
1492 case GST_VAAPI_FILTER_OP_DEINTERLACING:
1493 return op_set_deinterlace (filter, op_data,
1494 (value ? g_value_get_enum (value) :
1495 G_PARAM_SPEC_ENUM (op_data->pspec)->default_value), 0);
1497 case GST_VAAPI_FILTER_OP_SCALING:
1498 return gst_vaapi_filter_set_scaling (filter,
1499 (value ? g_value_get_enum (value) :
1500 G_PARAM_SPEC_ENUM (op_data->pspec)->default_value));
1501 #ifndef GST_REMOVE_DEPRECATED
1502 case GST_VAAPI_FILTER_OP_SKINTONE:
1503 return op_set_skintone (filter, op_data,
1504 (value ? g_value_get_boolean (value) :
1505 G_PARAM_SPEC_BOOLEAN (op_data->pspec)->default_value));
1507 case GST_VAAPI_FILTER_OP_SKINTONE_LEVEL:
1508 return op_set_skintone_level (filter, op_data,
1509 (value ? g_value_get_uint (value) :
1510 G_PARAM_SPEC_UINT (op_data->pspec)->default_value));
1511 case GST_VAAPI_FILTER_OP_VIDEO_DIRECTION:
1512 return gst_vaapi_filter_set_video_direction (filter,
1513 (value ? g_value_get_enum (value) :
1514 G_PARAM_SPEC_ENUM (op_data->pspec)->default_value));
1522 * gst_vaapi_filter_process:
1523 * @filter: a #GstVaapiFilter
1524 * @src_surface: the source @GstVaapiSurface
1525 * @dst_surface: the destination @GstVaapiSurface
1526 * @flags: #GstVaapiSurfaceRenderFlags that apply to @src_surface
1528 * Applies the operations currently defined in the @filter to
1529 * @src_surface and return the output in @dst_surface. The order of
1530 * operations is determined in a way that suits best the underlying
1531 * hardware. i.e. the only guarantee held is the generated outcome,
1532 * not any specific order of operations.
1534 * Return value: a #GstVaapiFilterStatus
1536 static GstVaapiFilterStatus
1537 gst_vaapi_filter_process_unlocked (GstVaapiFilter * filter,
1538 GstVaapiSurface * src_surface, GstVaapiSurface * dst_surface, guint flags)
1540 VAProcPipelineParameterBuffer *pipeline_param = NULL;
1541 VABufferID pipeline_param_buf_id = VA_INVALID_ID;
1542 VABufferID filters[N_PROPERTIES];
1543 VAProcPipelineCaps pipeline_caps;
1544 guint i, num_filters = 0;
1546 VARectangle src_rect, dst_rect;
1547 guint va_mirror = 0, va_rotation = 0;
1549 if (!ensure_operations (filter))
1550 return GST_VAAPI_FILTER_STATUS_ERROR_ALLOCATION_FAILED;
1552 /* Build surface region (source) */
1553 if (filter->use_crop_rect) {
1554 const GstVaapiRectangle *const crop_rect = &filter->crop_rect;
1556 if ((crop_rect->x + crop_rect->width >
1557 GST_VAAPI_SURFACE_WIDTH (src_surface)) ||
1558 (crop_rect->y + crop_rect->height >
1559 GST_VAAPI_SURFACE_HEIGHT (src_surface)))
1562 src_rect.x = crop_rect->x;
1563 src_rect.y = crop_rect->y;
1564 src_rect.width = crop_rect->width;
1565 src_rect.height = crop_rect->height;
1569 src_rect.width = GST_VAAPI_SURFACE_WIDTH (src_surface);
1570 src_rect.height = GST_VAAPI_SURFACE_HEIGHT (src_surface);
1573 /* Build output region (target) */
1574 if (filter->use_target_rect) {
1575 const GstVaapiRectangle *const target_rect = &filter->target_rect;
1577 if ((target_rect->x + target_rect->width >
1578 GST_VAAPI_SURFACE_WIDTH (dst_surface)) ||
1579 (target_rect->y + target_rect->height >
1580 GST_VAAPI_SURFACE_HEIGHT (dst_surface)))
1583 dst_rect.x = target_rect->x;
1584 dst_rect.y = target_rect->y;
1585 dst_rect.width = target_rect->width;
1586 dst_rect.height = target_rect->height;
1590 dst_rect.width = GST_VAAPI_SURFACE_WIDTH (dst_surface);
1591 dst_rect.height = GST_VAAPI_SURFACE_HEIGHT (dst_surface);
1594 for (i = 0, num_filters = 0; i < filter->operations->len; i++) {
1595 GstVaapiFilterOpData *const op_data =
1596 g_ptr_array_index (filter->operations, i);
1597 if (!op_data->is_enabled)
1599 if (op_data->va_buffer == VA_INVALID_ID) {
1600 GST_ERROR ("invalid VA buffer for operation %s",
1601 g_param_spec_get_name (op_data->pspec));
1604 filters[num_filters++] = op_data->va_buffer;
1607 /* Validate pipeline caps */
1608 va_status = vaQueryVideoProcPipelineCaps (filter->va_display,
1609 filter->va_context, filters, num_filters, &pipeline_caps);
1610 if (!vaapi_check_status (va_status, "vaQueryVideoProcPipelineCaps()"))
1613 if (!vaapi_create_buffer (filter->va_display, filter->va_context,
1614 VAProcPipelineParameterBufferType, sizeof (*pipeline_param),
1615 NULL, &pipeline_param_buf_id, (gpointer *) & pipeline_param))
1618 memset (pipeline_param, 0, sizeof (*pipeline_param));
1619 pipeline_param->surface = GST_VAAPI_OBJECT_ID (src_surface);
1620 pipeline_param->surface_region = &src_rect;
1621 pipeline_param->surface_color_standard = VAProcColorStandardNone;
1622 pipeline_param->output_region = &dst_rect;
1623 pipeline_param->output_color_standard = VAProcColorStandardNone;
1624 pipeline_param->output_background_color = 0xff000000;
1625 pipeline_param->filter_flags = from_GstVaapiSurfaceRenderFlags (flags) |
1626 from_GstVaapiScaleMethod (filter->scale_method);
1627 pipeline_param->filters = filters;
1628 pipeline_param->num_filters = num_filters;
1630 from_GstVideoOrientationMethod (filter->video_direction, &va_mirror,
1633 #if VA_CHECK_VERSION(1,1,0)
1634 pipeline_param->mirror_state = va_mirror;
1635 pipeline_param->rotation_state = va_rotation;
1638 // Reference frames for advanced deinterlacing
1639 if (filter->forward_references->len > 0) {
1640 pipeline_param->forward_references = (VASurfaceID *)
1641 filter->forward_references->data;
1642 pipeline_param->num_forward_references =
1643 MIN (filter->forward_references->len,
1644 pipeline_caps.num_forward_references);
1646 pipeline_param->forward_references = NULL;
1647 pipeline_param->num_forward_references = 0;
1650 if (filter->backward_references->len > 0) {
1651 pipeline_param->backward_references = (VASurfaceID *)
1652 filter->backward_references->data;
1653 pipeline_param->num_backward_references =
1654 MIN (filter->backward_references->len,
1655 pipeline_caps.num_backward_references);
1657 pipeline_param->backward_references = NULL;
1658 pipeline_param->num_backward_references = 0;
1661 vaapi_unmap_buffer (filter->va_display, pipeline_param_buf_id, NULL);
1663 va_status = vaBeginPicture (filter->va_display, filter->va_context,
1664 GST_VAAPI_OBJECT_ID (dst_surface));
1665 if (!vaapi_check_status (va_status, "vaBeginPicture()"))
1668 va_status = vaRenderPicture (filter->va_display, filter->va_context,
1669 &pipeline_param_buf_id, 1);
1670 if (!vaapi_check_status (va_status, "vaRenderPicture()"))
1673 va_status = vaEndPicture (filter->va_display, filter->va_context);
1674 if (!vaapi_check_status (va_status, "vaEndPicture()"))
1677 deint_refs_clear_all (filter);
1678 vaapi_destroy_buffer (filter->va_display, &pipeline_param_buf_id);
1679 return GST_VAAPI_FILTER_STATUS_SUCCESS;
1684 deint_refs_clear_all (filter);
1685 vaapi_destroy_buffer (filter->va_display, &pipeline_param_buf_id);
1686 return GST_VAAPI_FILTER_STATUS_ERROR_OPERATION_FAILED;
1690 GstVaapiFilterStatus
1691 gst_vaapi_filter_process (GstVaapiFilter * filter,
1692 GstVaapiSurface * src_surface, GstVaapiSurface * dst_surface, guint flags)
1694 GstVaapiFilterStatus status;
1696 g_return_val_if_fail (filter != NULL,
1697 GST_VAAPI_FILTER_STATUS_ERROR_INVALID_PARAMETER);
1698 g_return_val_if_fail (src_surface != NULL,
1699 GST_VAAPI_FILTER_STATUS_ERROR_INVALID_PARAMETER);
1700 g_return_val_if_fail (dst_surface != NULL,
1701 GST_VAAPI_FILTER_STATUS_ERROR_INVALID_PARAMETER);
1703 GST_VAAPI_DISPLAY_LOCK (filter->display);
1704 status = gst_vaapi_filter_process_unlocked (filter,
1705 src_surface, dst_surface, flags);
1706 GST_VAAPI_DISPLAY_UNLOCK (filter->display);
1711 * gst_vaapi_filter_get_formats:
1712 * @filter: a #GstVaapiFilter
1714 * Determines the set of supported source or target formats for video
1715 * processing. The caller owns an extra reference to the resulting
1716 * array of #GstVideoFormat elements, so it shall be released with
1717 * g_array_unref() after usage.
1719 * Return value: the set of supported target formats for video processing.
1722 gst_vaapi_filter_get_formats (GstVaapiFilter * filter)
1724 g_return_val_if_fail (filter != NULL, NULL);
1726 if (!ensure_attributes (filter))
1728 if (filter->attribs->formats)
1729 return g_array_ref (filter->attribs->formats);
1734 * gst_vaapi_filter_set_format:
1735 * @filter: a #GstVaapiFilter
1736 * @format: the target surface format
1738 * Sets the desired pixel format of the resulting video processing
1741 * If @format is #GST_VIDEO_FORMAT_UNKNOWN, the filter will assume iso
1742 * format conversion, i.e. no color conversion at all and the target
1743 * surface format shall match the source surface format.
1745 * If @format is #GST_VIDEO_FORMAT_ENCODED, the filter will use the pixel
1746 * format of the target surface passed to gst_vaapi_filter_process().
1748 * Return value: %TRUE if the color conversion to the specified @format
1749 * may be supported, %FALSE otherwise.
1752 gst_vaapi_filter_set_format (GstVaapiFilter * filter, GstVideoFormat format)
1754 g_return_val_if_fail (filter != NULL, FALSE);
1756 if (!ensure_attributes (filter))
1759 if (!is_special_format (format) && !find_format (filter, format))
1762 filter->format = format;
1767 * gst_vaapi_filter_append_caps:
1768 * @filter: a #GstVaapiFilter
1769 * @structure: a #GstStructure from #GstCaps
1771 * Extracts the config's surface attributes, from @filter's context,
1772 * and transforms it into a caps formats and appended them into
1775 * Returns: %TRUE if the capabilities could be extracted and appended
1776 * into @structure; otherwise %FALSE
1779 gst_vaapi_filter_append_caps (GstVaapiFilter * filter, GstStructure * structure)
1781 GstVaapiConfigSurfaceAttributes *attribs;
1783 g_return_val_if_fail (filter != NULL, FALSE);
1784 g_return_val_if_fail (structure != NULL, FALSE);
1786 if (!ensure_attributes (filter))
1789 attribs = filter->attribs;
1791 if (attribs->min_width >= attribs->max_width ||
1792 attribs->min_height >= attribs->max_height)
1795 gst_structure_set (structure, "width", GST_TYPE_INT_RANGE, attribs->min_width,
1796 attribs->max_width, "height", GST_TYPE_INT_RANGE, attribs->min_height,
1797 attribs->max_height, NULL);
1804 * gst_vaapi_filter_set_cropping_rectangle:
1805 * @filter: a #GstVaapiFilter
1806 * @rect: the cropping region
1808 * Sets the source surface cropping rectangle to use during the video
1809 * processing. If @rect is %NULL, the whole source surface will be used.
1811 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1814 gst_vaapi_filter_set_cropping_rectangle (GstVaapiFilter * filter,
1815 const GstVaapiRectangle * rect)
1817 g_return_val_if_fail (filter != NULL, FALSE);
1819 filter->use_crop_rect = rect != NULL;
1820 if (filter->use_crop_rect)
1821 filter->crop_rect = *rect;
1826 * gst_vaapi_filter_set_target_rectangle:
1827 * @filter: a #GstVaapiFilter
1828 * @rect: the target render region
1830 * Sets the region within the target surface where the source surface
1831 * would be rendered. i.e. where the hardware accelerator would emit
1832 * the outcome of video processing. If @rect is %NULL, the whole
1833 * source surface will be used.
1835 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1838 gst_vaapi_filter_set_target_rectangle (GstVaapiFilter * filter,
1839 const GstVaapiRectangle * rect)
1841 g_return_val_if_fail (filter != NULL, FALSE);
1843 filter->use_target_rect = rect != NULL;
1844 if (filter->use_target_rect)
1845 filter->target_rect = *rect;
1850 * gst_vaapi_filter_set_denoising_level:
1851 * @filter: a #GstVaapiFilter
1852 * @level: the level of noise reduction to apply
1854 * Sets the noise reduction level to apply. If @level is 0.0f, this
1855 * corresponds to disabling the noise reduction algorithm.
1857 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1860 gst_vaapi_filter_set_denoising_level (GstVaapiFilter * filter, gfloat level)
1862 g_return_val_if_fail (filter != NULL, FALSE);
1864 return op_set_generic (filter,
1865 find_operation (filter, GST_VAAPI_FILTER_OP_DENOISE), level);
1869 * gst_vaapi_filter_set_sharpening_level:
1870 * @filter: a #GstVaapiFilter
1871 * @level: the sharpening factor
1873 * Enables noise reduction with the specified factor.
1875 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1878 gst_vaapi_filter_set_sharpening_level (GstVaapiFilter * filter, gfloat level)
1880 g_return_val_if_fail (filter != NULL, FALSE);
1882 return op_set_generic (filter,
1883 find_operation (filter, GST_VAAPI_FILTER_OP_SHARPEN), level);
1887 * gst_vaapi_filter_set_hue:
1888 * @filter: a #GstVaapiFilter
1889 * @value: the color hue value
1891 * Enables color hue adjustment to the specified value.
1893 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1896 gst_vaapi_filter_set_hue (GstVaapiFilter * filter, gfloat value)
1898 g_return_val_if_fail (filter != NULL, FALSE);
1900 return op_set_color_balance (filter,
1901 find_operation (filter, GST_VAAPI_FILTER_OP_HUE), value);
1905 * gst_vaapi_filter_set_saturation:
1906 * @filter: a #GstVaapiFilter
1907 * @value: the color saturation value
1909 * Enables color saturation adjustment to the specified value.
1911 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1914 gst_vaapi_filter_set_saturation (GstVaapiFilter * filter, gfloat value)
1916 g_return_val_if_fail (filter != NULL, FALSE);
1918 return op_set_color_balance (filter,
1919 find_operation (filter, GST_VAAPI_FILTER_OP_SATURATION), value);
1923 * gst_vaapi_filter_set_brightness:
1924 * @filter: a #GstVaapiFilter
1925 * @value: the color brightness value
1927 * Enables color brightness adjustment to the specified value.
1929 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1932 gst_vaapi_filter_set_brightness (GstVaapiFilter * filter, gfloat value)
1934 g_return_val_if_fail (filter != NULL, FALSE);
1936 return op_set_color_balance (filter,
1937 find_operation (filter, GST_VAAPI_FILTER_OP_BRIGHTNESS), value);
1941 * gst_vaapi_filter_set_contrast:
1942 * @filter: a #GstVaapiFilter
1943 * @value: the color contrast value
1945 * Enables color contrast adjustment to the specified value.
1947 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1950 gst_vaapi_filter_set_contrast (GstVaapiFilter * filter, gfloat value)
1952 g_return_val_if_fail (filter != NULL, FALSE);
1954 return op_set_color_balance (filter,
1955 find_operation (filter, GST_VAAPI_FILTER_OP_CONTRAST), value);
1959 * gst_vaapi_filter_set_deinterlacing:
1960 * @filter: a #GstVaapiFilter
1961 * @method: the deinterlacing algorithm (see #GstVaapiDeinterlaceMethod)
1962 * @flags: the additional flags
1964 * Applies deinterlacing to the video processing pipeline. If @method
1965 * is not @GST_VAAPI_DEINTERLACE_METHOD_NONE, then @flags could
1966 * represent the initial picture structure of the source frame.
1968 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
1971 gst_vaapi_filter_set_deinterlacing (GstVaapiFilter * filter,
1972 GstVaapiDeinterlaceMethod method, guint flags)
1974 g_return_val_if_fail (filter != NULL, FALSE);
1976 return op_set_deinterlace (filter,
1977 find_operation (filter, GST_VAAPI_FILTER_OP_DEINTERLACING), method,
1982 * gst_vaapi_filter_set_deinterlacing_references:
1983 * @filter: a #GstVaapiFilter
1984 * @forward_references: the set of #GstVaapiSurface objects used as
1985 * forward references
1986 * @num_forward_references: the number of elements in the
1987 * @forward_references array
1988 * @backward_references: the set of #GstVaapiSurface objects used as
1989 * backward references
1990 * @num_backward_references: the number of elements in the
1991 * @backward_references array
1993 * Specifies the list of surfaces used for forward or backward reference in
1994 * advanced deinterlacing mode. The caller is responsible for maintaining
1995 * the associated surfaces live until gst_vaapi_filter_process() completes.
1996 * e.g. by holding an extra reference to the associated #GstVaapiSurfaceProxy.
1998 * Temporal ordering is maintained as follows: the shorter index in
1999 * either array is, the closest the matching surface is relatively to
2000 * the current source surface to process. e.g. surface in
2001 * @forward_references array index 0 represents the immediately
2002 * preceding surface in display order, surface at index 1 is the one
2003 * preceding surface at index 0, etc.
2005 * The video processing filter will only use the recommended number of
2006 * surfaces for backward and forward references.
2008 * Note: the supplied lists of reference surfaces are not sticky. This
2009 * means that they are only valid for the next gst_vaapi_filter_process()
2010 * call, and thus needs to be submitted again for subsequent calls.
2012 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
2015 gst_vaapi_filter_set_deinterlacing_references (GstVaapiFilter * filter,
2016 GstVaapiSurface ** forward_references, guint num_forward_references,
2017 GstVaapiSurface ** backward_references, guint num_backward_references)
2019 g_return_val_if_fail (filter != NULL, FALSE);
2021 deint_refs_clear_all (filter);
2023 if (!deint_refs_set (filter->forward_references, forward_references,
2024 num_forward_references))
2027 if (!deint_refs_set (filter->backward_references, backward_references,
2028 num_backward_references))
2034 * gst_vaapi_filter_set_scaling:
2035 * @filter: a #GstVaapiFilter
2036 * @method: the scaling algorithm (see #GstVaapiScaleMethod)
2038 * Applies scaling algorithm to the video processing pipeline.
2040 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
2043 gst_vaapi_filter_set_scaling (GstVaapiFilter * filter,
2044 GstVaapiScaleMethod method)
2046 g_return_val_if_fail (filter != NULL, FALSE);
2048 filter->scale_method = method;
2052 #ifndef GST_REMOVE_DEPRECATED
2054 * gst_vaapi_filter_set_skintone:
2055 * @filter: a #GstVaapiFilter
2056 * @enhance: %TRUE if enable the skin tone enhancement algorithm
2058 * Applies the skin tone enhancement algorithm.
2060 * Return value: %TRUE if the operation is supported, %FALSE
2064 gst_vaapi_filter_set_skintone (GstVaapiFilter * filter, gboolean enhance)
2066 g_return_val_if_fail (filter != NULL, FALSE);
2068 return op_set_skintone (filter,
2069 find_operation (filter, GST_VAAPI_FILTER_OP_SKINTONE), enhance);
2074 * gst_vaapi_filter_set_skintone_level:
2075 * @filter: a #GstVaapiFilter
2076 * @value: the value if enable the skin tone enhancement algorithm
2078 * Applies the skin tone enhancement algorithm with specifled value.
2080 * Return value: %TRUE if the operation is supported, %FALSE
2084 gst_vaapi_filter_set_skintone_level (GstVaapiFilter * filter, guint value)
2086 g_return_val_if_fail (filter != NULL, FALSE);
2088 return op_set_skintone_level (filter,
2089 find_operation (filter, GST_VAAPI_FILTER_OP_SKINTONE_LEVEL), value);
2093 * gst_vaapi_filter_set_video_direction:
2094 * @filter: a #GstVaapiFilter
2095 * @method: the video direction (see #GstVideoOrientationMethod)
2097 * Applies mirror/rotation to the video processing pipeline.
2099 * Return value: %TRUE if the operation is supported, %FALSE otherwise.
2102 gst_vaapi_filter_set_video_direction (GstVaapiFilter * filter,
2103 GstVideoOrientationMethod method)
2105 g_return_val_if_fail (filter != NULL, FALSE);
2107 #if VA_CHECK_VERSION(1,1,0)
2109 guint32 va_mirror = VA_MIRROR_NONE;
2110 guint32 va_rotation = VA_ROTATION_NONE;
2112 from_GstVideoOrientationMethod (method, &va_mirror, &va_rotation);
2114 if (va_mirror != VA_MIRROR_NONE && !(filter->mirror_flags & va_mirror))
2117 if (va_rotation != VA_ROTATION_NONE
2118 && !(filter->rotation_flags & (1 << va_rotation)))
2125 filter->video_direction = method;
2130 * gst_vaapi_filter_get_video_direction:
2131 * @filter: a #GstVaapiFilter
2133 * Return value: the currently applied video direction (see #GstVideoOrientationMethod)
2135 GstVideoOrientationMethod
2136 gst_vaapi_filter_get_video_direction (GstVaapiFilter * filter)
2138 g_return_val_if_fail (filter != NULL, GST_VIDEO_ORIENTATION_IDENTITY);
2139 return filter->video_direction;
2142 static inline gfloat
2143 op_get_float_default_value (GstVaapiFilter * filter,
2144 GstVaapiFilterOpData * op_data)
2146 GParamSpecFloat *const pspec = G_PARAM_SPEC_FLOAT (op_data->pspec);
2147 return pspec->default_value;
2151 op_get_enum_default_value (GstVaapiFilter * filter,
2152 GstVaapiFilterOpData * op_data)
2154 GParamSpecEnum *const pspec = G_PARAM_SPEC_ENUM (op_data->pspec);
2155 return pspec->default_value;
2159 op_get_uint_default_value (GstVaapiFilter * filter,
2160 GstVaapiFilterOpData * op_data)
2162 GParamSpecUInt *const pspec = G_PARAM_SPEC_UINT (op_data->pspec);
2163 return pspec->default_value;
2166 static inline gboolean
2167 op_get_bool_default_value (GstVaapiFilter * filter,
2168 GstVaapiFilterOpData * op_data)
2170 GParamSpecBoolean *const pspec = G_PARAM_SPEC_BOOLEAN (op_data->pspec);
2171 return pspec->default_value;
2175 gst_vaapi_filter_get_denoising_level_default (GstVaapiFilter * filter)
2177 OP_RET_DEFAULT_VALUE (float, filter, GST_VAAPI_FILTER_OP_DENOISE);
2181 gst_vaapi_filter_get_sharpening_level_default (GstVaapiFilter * filter)
2183 OP_RET_DEFAULT_VALUE (float, filter, GST_VAAPI_FILTER_OP_SHARPEN);
2187 gst_vaapi_filter_get_hue_default (GstVaapiFilter * filter)
2189 OP_RET_DEFAULT_VALUE (float, filter, GST_VAAPI_FILTER_OP_HUE);
2193 gst_vaapi_filter_get_saturation_default (GstVaapiFilter * filter)
2195 OP_RET_DEFAULT_VALUE (float, filter, GST_VAAPI_FILTER_OP_SATURATION);
2199 gst_vaapi_filter_get_brightness_default (GstVaapiFilter * filter)
2201 OP_RET_DEFAULT_VALUE (float, filter, GST_VAAPI_FILTER_OP_BRIGHTNESS);
2205 gst_vaapi_filter_get_contrast_default (GstVaapiFilter * filter)
2207 OP_RET_DEFAULT_VALUE (float, filter, GST_VAAPI_FILTER_OP_CONTRAST);
2211 gst_vaapi_filter_get_scaling_default (GstVaapiFilter * filter)
2213 OP_RET_DEFAULT_VALUE (enum, filter, GST_VAAPI_FILTER_OP_SCALING);
2216 #ifndef GST_REMOVE_DEPRECATED
2218 gst_vaapi_filter_get_skintone_default (GstVaapiFilter * filter)
2220 OP_RET_DEFAULT_VALUE (boolean, filter, GST_VAAPI_FILTER_OP_SKINTONE);
2225 gst_vaapi_filter_get_skintone_level_default (GstVaapiFilter * filter)
2227 OP_RET_DEFAULT_VALUE (uint, filter, GST_VAAPI_FILTER_OP_SKINTONE_LEVEL);
2230 GstVideoOrientationMethod
2231 gst_vaapi_filter_get_video_direction_default (GstVaapiFilter * filter)
2233 OP_RET_DEFAULT_VALUE (boolean, filter, GST_VAAPI_FILTER_OP_VIDEO_DIRECTION);