2 * gstvaapidisplay.c - VA display abstraction
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6 * Copyright (C) 2011-2013 Intel Corporation
7 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
26 * SECTION:gstvaapidisplay
27 * @short_description: VA display abstraction
32 #include "gstvaapiutils.h"
33 #include "gstvaapivalue.h"
34 #include "gstvaapidisplay.h"
35 #include "gstvaapidisplay_priv.h"
36 #include "gstvaapiworkarounds.h"
37 #include "gstvaapiversion.h"
40 #include "gstvaapidebug.h"
42 GST_DEBUG_CATEGORY (gst_debug_vaapi);
44 /* Ensure those symbols are actually defined in the resulting libraries */
45 #undef gst_vaapi_display_ref
46 #undef gst_vaapi_display_unref
47 #undef gst_vaapi_display_replace
49 typedef struct _GstVaapiConfig GstVaapiConfig;
50 struct _GstVaapiConfig
52 GstVaapiProfile profile;
53 GstVaapiEntrypoint entrypoint;
56 typedef struct _GstVaapiProperty GstVaapiProperty;
57 struct _GstVaapiProperty
60 VADisplayAttribute attribute;
64 typedef struct _GstVaapiFormatInfo GstVaapiFormatInfo;
65 struct _GstVaapiFormatInfo
67 GstVideoFormat format;
71 #define DEFAULT_RENDER_MODE GST_VAAPI_RENDER_MODE_TEXTURE
72 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
88 static GstVaapiDisplayCache *g_display_cache;
89 static GMutex g_display_cache_lock;
91 static GParamSpec *g_properties[N_PROPERTIES] = { NULL, };
93 static void gst_vaapi_display_properties_init (void);
96 get_attribute (GstVaapiDisplay * display, VADisplayAttribType type,
100 set_attribute (GstVaapiDisplay * display, VADisplayAttribType type, gint value);
103 get_color_balance (GstVaapiDisplay * display, guint prop_id, gfloat * v);
106 set_color_balance (GstVaapiDisplay * display, guint prop_id, gfloat v);
109 libgstvaapi_init_once (void)
111 static gsize g_once = FALSE;
113 if (!g_once_init_enter (&g_once))
116 GST_DEBUG_CATEGORY_INIT (gst_debug_vaapi, "vaapi", 0, "VA-API helper");
118 /* Dump gstreamer-vaapi version for debugging purposes */
119 GST_INFO ("gstreamer-vaapi version %s", GST_VAAPI_VERSION_ID);
121 gst_vaapi_display_properties_init ();
123 g_once_init_leave (&g_once, TRUE);
126 static GstVaapiDisplayCache *
127 get_display_cache (void)
129 GstVaapiDisplayCache *cache = NULL;
131 g_mutex_lock (&g_display_cache_lock);
132 if (!g_display_cache)
133 g_display_cache = gst_vaapi_display_cache_new ();
135 cache = gst_vaapi_display_cache_ref (g_display_cache);
136 g_mutex_unlock (&g_display_cache_lock);
141 free_display_cache (void)
143 g_mutex_lock (&g_display_cache_lock);
144 if (g_display_cache && gst_vaapi_display_cache_is_empty (g_display_cache))
145 gst_vaapi_display_cache_replace (&g_display_cache, NULL);
146 g_mutex_unlock (&g_display_cache_lock);
149 /* GstVaapiDisplayType enumerations */
151 gst_vaapi_display_type_get_type (void)
153 static GType g_type = 0;
155 static const GEnumValue display_types[] = {
156 {GST_VAAPI_DISPLAY_TYPE_ANY,
157 "Auto detection", "any"},
159 {GST_VAAPI_DISPLAY_TYPE_X11,
160 "VA/X11 display", "x11"},
163 {GST_VAAPI_DISPLAY_TYPE_GLX,
164 "VA/GLX display", "glx"},
167 {GST_VAAPI_DISPLAY_TYPE_WAYLAND,
168 "VA/Wayland display", "wayland"},
171 {GST_VAAPI_DISPLAY_TYPE_DRM,
172 "VA/DRM display", "drm"},
178 g_type = g_enum_register_static ("GstVaapiDisplayType", display_types);
183 * gst_vaapi_display_type_is_compatible:
184 * @type1: the #GstVaapiDisplayType to test
185 * @type2: the reference #GstVaapiDisplayType
187 * Compares whether #GstVaapiDisplay @type1 is compatible with @type2.
188 * That is, if @type2 is in "any" category, or derived from @type1.
190 * Returns: %TRUE if @type1 is compatible with @type2, %FALSE otherwise.
193 gst_vaapi_display_type_is_compatible (GstVaapiDisplayType type1,
194 GstVaapiDisplayType type2)
200 case GST_VAAPI_DISPLAY_TYPE_GLX:
201 if (type2 == GST_VAAPI_DISPLAY_TYPE_X11)
207 return type2 == GST_VAAPI_DISPLAY_TYPE_ANY;
210 /* Append GstVideoFormat to formats array */
212 append_format (GArray * formats, GstVideoFormat format, guint flags)
214 GstVaapiFormatInfo fi;
218 g_array_append_val (formats, fi);
221 /* Append VAImageFormats to formats array */
223 append_formats (GArray * formats, const VAImageFormat * va_formats,
224 guint * flags, guint n)
226 GstVideoFormat format;
227 const GstVaapiFormatInfo *YV12_fip = NULL;
228 const GstVaapiFormatInfo *I420_fip = NULL;
231 for (i = 0; i < n; i++) {
232 const VAImageFormat *const va_format = &va_formats[i];
233 const GstVaapiFormatInfo **fipp;
235 format = gst_vaapi_video_format_from_va_format (va_format);
236 if (format == GST_VIDEO_FORMAT_UNKNOWN) {
237 GST_DEBUG ("unsupported format %" GST_FOURCC_FORMAT,
238 GST_FOURCC_ARGS (va_format->fourcc));
241 append_format (formats, format, flags ? flags[i] : 0);
244 case GST_VIDEO_FORMAT_YV12:
247 case GST_VIDEO_FORMAT_I420:
255 *fipp = &g_array_index (formats, GstVaapiFormatInfo, formats->len - 1);
258 /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
259 supported by the underlying driver */
260 if (YV12_fip && !I420_fip)
261 append_format (formats, GST_VIDEO_FORMAT_I420, YV12_fip->flags);
262 else if (I420_fip && !YV12_fip)
263 append_format (formats, GST_VIDEO_FORMAT_YV12, I420_fip->flags);
266 /* Sort image formats. Prefer YUV formats first */
268 compare_yuv_formats (gconstpointer a, gconstpointer b)
270 const GstVideoFormat fmt1 = ((GstVaapiFormatInfo *) a)->format;
271 const GstVideoFormat fmt2 = ((GstVaapiFormatInfo *) b)->format;
273 const gboolean is_fmt1_yuv = gst_vaapi_video_format_is_yuv (fmt1);
274 const gboolean is_fmt2_yuv = gst_vaapi_video_format_is_yuv (fmt2);
276 if (is_fmt1_yuv != is_fmt2_yuv)
277 return is_fmt1_yuv ? -1 : 1;
279 return ((gint) gst_vaapi_video_format_get_score (fmt1) -
280 (gint) gst_vaapi_video_format_get_score (fmt2));
283 /* Sort subpicture formats. Prefer RGB formats first */
285 compare_rgb_formats (gconstpointer a, gconstpointer b)
287 const GstVideoFormat fmt1 = ((GstVaapiFormatInfo *) a)->format;
288 const GstVideoFormat fmt2 = ((GstVaapiFormatInfo *) b)->format;
290 const gboolean is_fmt1_rgb = gst_vaapi_video_format_is_rgb (fmt1);
291 const gboolean is_fmt2_rgb = gst_vaapi_video_format_is_rgb (fmt2);
293 if (is_fmt1_rgb != is_fmt2_rgb)
294 return is_fmt1_rgb ? -1 : 1;
296 return ((gint) gst_vaapi_video_format_get_score (fmt1) -
297 (gint) gst_vaapi_video_format_get_score (fmt2));
300 /* Check if configs array contains profile at entrypoint */
301 static inline gboolean
302 find_config (GArray * configs,
303 GstVaapiProfile profile, GstVaapiEntrypoint entrypoint)
305 GstVaapiConfig *config;
311 for (i = 0; i < configs->len; i++) {
312 config = &g_array_index (configs, GstVaapiConfig, i);
313 if (config->profile == profile && config->entrypoint == entrypoint)
319 /* HACK: append H.263 Baseline profile if MPEG-4:2 Simple profile is supported */
321 append_h263_config (GArray * configs)
323 GstVaapiConfig *config, tmp_config;
324 GstVaapiConfig *mpeg4_simple_config = NULL;
325 GstVaapiConfig *h263_baseline_config = NULL;
328 if (!WORKAROUND_H263_BASELINE_DECODE_PROFILE)
334 for (i = 0; i < configs->len; i++) {
335 config = &g_array_index (configs, GstVaapiConfig, i);
336 if (config->profile == GST_VAAPI_PROFILE_MPEG4_SIMPLE)
337 mpeg4_simple_config = config;
338 else if (config->profile == GST_VAAPI_PROFILE_H263_BASELINE)
339 h263_baseline_config = config;
342 if (mpeg4_simple_config && !h263_baseline_config) {
343 tmp_config = *mpeg4_simple_config;
344 tmp_config.profile = GST_VAAPI_PROFILE_H263_BASELINE;
345 g_array_append_val (configs, tmp_config);
349 /* Sort profiles. Group per codec */
351 compare_profiles (gconstpointer a, gconstpointer b)
353 const GstVaapiConfig *const config1 = (GstVaapiConfig *) a;
354 const GstVaapiConfig *const config2 = (GstVaapiConfig *) b;
356 if (config1->profile == config2->profile)
357 return config1->entrypoint - config2->entrypoint;
359 return config1->profile - config2->profile;
362 /* Convert configs array to profiles as GstCaps */
364 get_profiles (GArray * configs)
366 GstVaapiConfig *config;
367 GArray *out_profiles;
373 out_profiles = g_array_new (FALSE, FALSE, sizeof (GstVaapiProfile));
377 for (i = 0; i < configs->len; i++) {
378 config = &g_array_index (configs, GstVaapiConfig, i);
379 g_array_append_val (out_profiles, config->profile);
384 /* Find format info */
385 static const GstVaapiFormatInfo *
386 find_format_info (GArray * formats, GstVideoFormat format)
388 const GstVaapiFormatInfo *fip;
391 for (i = 0; i < formats->len; i++) {
392 fip = &g_array_index (formats, GstVaapiFormatInfo, i);
393 if (fip->format == format)
399 /* Check if formats array contains format */
400 static inline gboolean
401 find_format (GArray * formats, GstVideoFormat format)
403 return find_format_info (formats, format) != NULL;
406 /* Convert formats array to GstCaps */
408 get_formats (GArray * formats)
410 const GstVaapiFormatInfo *fip;
414 out_formats = g_array_new (FALSE, FALSE, sizeof (GstVideoFormat));
418 for (i = 0; i < formats->len; i++) {
419 fip = &g_array_index (formats, GstVaapiFormatInfo, i);
420 g_array_append_val (out_formats, fip->format);
425 /* Find display attribute */
426 static const GstVaapiProperty *
427 find_property (GArray * properties, const gchar * name)
429 GstVaapiProperty *prop;
435 for (i = 0; i < properties->len; i++) {
436 prop = &g_array_index (properties, GstVaapiProperty, i);
437 if (strcmp (prop->name, name) == 0)
444 static const GstVaapiProperty *
445 find_property_by_type (GArray * properties, VADisplayAttribType type)
447 GstVaapiProperty *prop;
450 for (i = 0; i < properties->len; i++) {
451 prop = &g_array_index (properties, GstVaapiProperty, i);
452 if (prop->attribute.type == type)
459 static inline const GstVaapiProperty *
460 find_property_by_pspec (GstVaapiDisplay * display, GParamSpec * pspec)
462 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
464 return find_property (priv->properties, pspec->name);
468 find_property_id (const gchar * name)
476 static const property_map g_property_map[] = {
477 {GST_VAAPI_DISPLAY_PROP_RENDER_MODE, PROP_RENDER_MODE},
478 {GST_VAAPI_DISPLAY_PROP_ROTATION, PROP_ROTATION},
479 {GST_VAAPI_DISPLAY_PROP_HUE, PROP_HUE},
480 {GST_VAAPI_DISPLAY_PROP_SATURATION, PROP_SATURATION},
481 {GST_VAAPI_DISPLAY_PROP_BRIGHTNESS, PROP_BRIGHTNESS},
482 {GST_VAAPI_DISPLAY_PROP_CONTRAST, PROP_CONTRAST},
486 const property_map *m;
487 for (m = g_property_map; m->name != NULL; m++) {
488 if (strcmp (m->name, name) == 0)
494 /* Initialize VA profiles (decoders, encoders) */
496 ensure_profiles (GstVaapiDisplay * display)
498 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
499 VAProfile *profiles = NULL;
500 VAEntrypoint *entrypoints = NULL;
501 gint i, j, n, num_entrypoints;
503 gboolean success = FALSE;
505 if (priv->has_profiles)
508 priv->decoders = g_array_new (FALSE, FALSE, sizeof (GstVaapiConfig));
511 priv->encoders = g_array_new (FALSE, FALSE, sizeof (GstVaapiConfig));
514 priv->has_profiles = TRUE;
517 profiles = g_new (VAProfile, vaMaxNumProfiles (priv->display));
520 entrypoints = g_new (VAEntrypoint, vaMaxNumEntrypoints (priv->display));
525 status = vaQueryConfigProfiles (priv->display, profiles, &n);
526 if (!vaapi_check_status (status, "vaQueryConfigProfiles()"))
529 GST_DEBUG ("%d profiles", n);
530 for (i = 0; i < n; i++) {
531 #if VA_CHECK_VERSION(0,34,0)
532 /* Introduced in VA/VPP API */
533 if (profiles[i] == VAProfileNone)
536 GST_DEBUG (" %s", string_of_VAProfile (profiles[i]));
539 for (i = 0; i < n; i++) {
540 GstVaapiConfig config;
542 config.profile = gst_vaapi_profile (profiles[i]);
546 status = vaQueryConfigEntrypoints (priv->display,
547 profiles[i], entrypoints, &num_entrypoints);
548 if (!vaapi_check_status (status, "vaQueryConfigEntrypoints()"))
551 for (j = 0; j < num_entrypoints; j++) {
552 config.entrypoint = gst_vaapi_entrypoint (entrypoints[j]);
553 switch (config.entrypoint) {
554 case GST_VAAPI_ENTRYPOINT_VLD:
555 case GST_VAAPI_ENTRYPOINT_IDCT:
556 case GST_VAAPI_ENTRYPOINT_MOCO:
557 g_array_append_val (priv->decoders, config);
559 case GST_VAAPI_ENTRYPOINT_SLICE_ENCODE:
560 g_array_append_val (priv->encoders, config);
565 append_h263_config (priv->decoders);
567 g_array_sort (priv->decoders, compare_profiles);
568 g_array_sort (priv->encoders, compare_profiles);
570 /* Video processing API */
572 status = vaQueryConfigEntrypoints (priv->display, VAProfileNone,
573 entrypoints, &num_entrypoints);
574 if (vaapi_check_status (status, "vaQueryEntrypoints() [VAProfileNone]")) {
575 for (j = 0; j < num_entrypoints; j++) {
576 if (entrypoints[j] == VAEntrypointVideoProc)
577 priv->has_vpp = TRUE;
585 g_free (entrypoints);
589 /* Initialize VA display attributes */
591 ensure_properties (GstVaapiDisplay * display)
593 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
594 VADisplayAttribute *display_attrs = NULL;
597 gboolean success = FALSE;
599 if (priv->properties)
602 priv->properties = g_array_new (FALSE, FALSE, sizeof (GstVaapiProperty));
603 if (!priv->properties)
606 /* VA display attributes */
608 g_new (VADisplayAttribute, vaMaxNumDisplayAttributes (priv->display));
613 status = vaQueryDisplayAttributes (priv->display, display_attrs, &n);
614 if (!vaapi_check_status (status, "vaQueryDisplayAttributes()"))
617 GST_DEBUG ("%d display attributes", n);
618 for (i = 0; i < n; i++) {
619 VADisplayAttribute *const attr = &display_attrs[i];
620 GstVaapiProperty prop;
623 GST_DEBUG (" %s", string_of_VADisplayAttributeType (attr->type));
625 switch (attr->type) {
626 #if !VA_CHECK_VERSION(0,34,0)
627 case VADisplayAttribDirectSurface:
628 prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
631 case VADisplayAttribRenderMode:
632 prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
634 case VADisplayAttribRotation:
635 prop.name = GST_VAAPI_DISPLAY_PROP_ROTATION;
637 case VADisplayAttribHue:
638 prop.name = GST_VAAPI_DISPLAY_PROP_HUE;
640 case VADisplayAttribSaturation:
641 prop.name = GST_VAAPI_DISPLAY_PROP_SATURATION;
643 case VADisplayAttribBrightness:
644 prop.name = GST_VAAPI_DISPLAY_PROP_BRIGHTNESS;
646 case VADisplayAttribContrast:
647 prop.name = GST_VAAPI_DISPLAY_PROP_CONTRAST;
656 /* Assume the attribute is really supported if we can get the
657 * actual and current value */
658 if (!get_attribute (display, attr->type, &value))
661 /* Some drivers (e.g. EMGD) have completely random initial
663 if (value < attr->min_value || value > attr->max_value)
666 prop.attribute = *attr;
667 prop.old_value = value;
668 g_array_append_val (priv->properties, prop);
673 g_free (display_attrs);
677 /* Initialize VA image formats */
679 ensure_image_formats (GstVaapiDisplay * display)
681 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
682 VAImageFormat *formats = NULL;
685 gboolean success = FALSE;
687 if (priv->image_formats)
690 priv->image_formats = g_array_new (FALSE, FALSE, sizeof (GstVaapiFormatInfo));
691 if (!priv->image_formats)
694 /* VA image formats */
695 formats = g_new (VAImageFormat, vaMaxNumImageFormats (priv->display));
700 status = vaQueryImageFormats (priv->display, formats, &n);
701 if (!vaapi_check_status (status, "vaQueryImageFormats()"))
704 GST_DEBUG ("%d image formats", n);
705 for (i = 0; i < n; i++)
706 GST_DEBUG (" %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (formats[i].fourcc));
708 append_formats (priv->image_formats, formats, NULL, n);
709 g_array_sort (priv->image_formats, compare_yuv_formats);
717 /* Initialize VA subpicture formats */
719 ensure_subpicture_formats (GstVaapiDisplay * display)
721 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
722 VAImageFormat *formats = NULL;
723 unsigned int *flags = NULL;
726 gboolean success = FALSE;
728 if (priv->subpicture_formats)
731 priv->subpicture_formats =
732 g_array_new (FALSE, FALSE, sizeof (GstVaapiFormatInfo));
733 if (!priv->subpicture_formats)
736 /* VA subpicture formats */
737 n = vaMaxNumSubpictureFormats (priv->display);
738 formats = g_new (VAImageFormat, n);
741 flags = g_new (guint, n);
746 status = vaQuerySubpictureFormats (priv->display, formats, flags, &n);
747 if (!vaapi_check_status (status, "vaQuerySubpictureFormats()"))
750 GST_DEBUG ("%d subpicture formats", n);
751 for (i = 0; i < n; i++) {
752 GST_DEBUG (" %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (formats[i].fourcc));
753 flags[i] = to_GstVaapiSubpictureFlags (flags[i]);
756 append_formats (priv->subpicture_formats, formats, flags, n);
757 g_array_sort (priv->subpicture_formats, compare_rgb_formats);
767 gst_vaapi_display_calculate_pixel_aspect_ratio (GstVaapiDisplay * display)
769 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
770 gdouble ratio, delta;
771 gint i, j, index, windex;
773 static const gint par[][2] = {
774 {1, 1}, /* regular screen */
775 {16, 15}, /* PAL TV */
776 {11, 10}, /* 525 line Rec.601 video */
777 {54, 59}, /* 625 line Rec.601 video */
778 {64, 45}, /* 1280x1024 on 16:9 display */
779 {5, 3}, /* 1280x1024 on 4:3 display */
780 {4, 3} /* 800x600 on 16:9 display */
783 /* First, calculate the "real" ratio based on the X values;
784 * which is the "physical" w/h divided by the w/h in pixels of the
786 if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
789 ratio = (gdouble) (priv->width_mm * priv->height) /
790 (priv->height_mm * priv->width);
791 GST_DEBUG ("calculated pixel aspect ratio: %f", ratio);
793 /* Now, find the one from par[][2] with the lowest delta to the real one */
794 #define DELTA(idx, w) (ABS(ratio - ((gdouble)par[idx][w] / par[idx][!(w)])))
795 delta = DELTA (0, 0);
799 for (i = 1; i < G_N_ELEMENTS (par); i++) {
800 for (j = 0; j < 2; j++) {
801 const gdouble this_delta = DELTA (i, j);
802 if (this_delta < delta) {
811 priv->par_n = par[index][windex];
812 priv->par_d = par[index][windex ^ 1];
816 gst_vaapi_display_destroy (GstVaapiDisplay * display)
818 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
820 if (priv->decoders) {
821 g_array_free (priv->decoders, TRUE);
822 priv->decoders = NULL;
825 if (priv->encoders) {
826 g_array_free (priv->encoders, TRUE);
827 priv->encoders = NULL;
830 if (priv->image_formats) {
831 g_array_free (priv->image_formats, TRUE);
832 priv->image_formats = NULL;
835 if (priv->subpicture_formats) {
836 g_array_free (priv->subpicture_formats, TRUE);
837 priv->subpicture_formats = NULL;
840 if (priv->properties) {
841 g_array_free (priv->properties, TRUE);
842 priv->properties = NULL;
847 vaTerminate (priv->display);
848 priv->display = NULL;
851 if (!priv->use_foreign_display) {
852 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS (display);
853 if (klass->close_display)
854 klass->close_display (display);
857 g_free (priv->vendor_string);
858 priv->vendor_string = NULL;
860 gst_vaapi_display_replace_internal (&priv->parent, NULL);
862 g_mutex_lock (&g_display_cache_lock);
864 gst_vaapi_display_cache_remove (priv->cache, display);
865 g_mutex_unlock (&g_display_cache_lock);
866 gst_vaapi_display_cache_replace (&priv->cache, NULL);
867 free_display_cache ();
871 gst_vaapi_display_create_unlocked (GstVaapiDisplay * display,
872 GstVaapiDisplayInitType init_type, gpointer init_value)
874 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
875 const GstVaapiDisplayClass *const klass =
876 GST_VAAPI_DISPLAY_GET_CLASS (display);
877 gint major_version, minor_version;
879 GstVaapiDisplayInfo info;
880 const GstVaapiDisplayInfo *cached_info = NULL;
882 memset (&info, 0, sizeof (info));
883 info.display = display;
884 info.display_type = priv->display_type;
887 case GST_VAAPI_DISPLAY_INIT_FROM_VA_DISPLAY:
888 info.va_display = init_value;
889 priv->display = init_value;
890 priv->use_foreign_display = TRUE;
892 case GST_VAAPI_DISPLAY_INIT_FROM_DISPLAY_NAME:
893 if (klass->open_display && !klass->open_display (display, init_value))
896 case GST_VAAPI_DISPLAY_INIT_FROM_NATIVE_DISPLAY:
897 if (klass->bind_display && !klass->bind_display (display, init_value))
901 if (!klass->get_display || !klass->get_display (display, &info))
903 priv->display = info.va_display;
904 priv->display_type = info.display_type;
906 klass->get_size (display, &priv->width, &priv->height);
907 if (klass->get_size_mm)
908 klass->get_size_mm (display, &priv->width_mm, &priv->height_mm);
909 gst_vaapi_display_calculate_pixel_aspect_ratio (display);
915 cached_info = gst_vaapi_display_cache_lookup_by_va_display (priv->cache,
918 gst_vaapi_display_replace_internal (&priv->parent, cached_info->display);
919 priv->display_type = cached_info->display_type;
923 status = vaInitialize (priv->display, &major_version, &minor_version);
924 if (!vaapi_check_status (status, "vaInitialize()"))
926 GST_DEBUG ("VA-API version %d.%d", major_version, minor_version);
930 if (!gst_vaapi_display_cache_add (priv->cache, &info))
937 gst_vaapi_display_create (GstVaapiDisplay * display,
938 GstVaapiDisplayInitType init_type, gpointer init_value)
940 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
941 GstVaapiDisplayCache *cache;
944 cache = get_display_cache ();
947 gst_vaapi_display_cache_replace (&priv->cache, cache);
948 gst_vaapi_display_cache_unref (cache);
950 g_mutex_lock (&g_display_cache_lock);
951 success = gst_vaapi_display_create_unlocked (display, init_type, init_value);
952 g_mutex_unlock (&g_display_cache_lock);
957 gst_vaapi_display_lock_default (GstVaapiDisplay * display)
959 GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
962 priv = GST_VAAPI_DISPLAY_GET_PRIVATE (priv->parent);
963 g_rec_mutex_lock (&priv->mutex);
967 gst_vaapi_display_unlock_default (GstVaapiDisplay * display)
969 GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
972 priv = GST_VAAPI_DISPLAY_GET_PRIVATE (priv->parent);
973 g_rec_mutex_unlock (&priv->mutex);
977 gst_vaapi_display_init (GstVaapiDisplay * display)
979 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
980 const GstVaapiDisplayClass *const dpy_class =
981 GST_VAAPI_DISPLAY_GET_CLASS (display);
983 priv->display_type = GST_VAAPI_DISPLAY_TYPE_ANY;
987 g_rec_mutex_init (&priv->mutex);
990 dpy_class->init (display);
994 gst_vaapi_display_finalize (GstVaapiDisplay * display)
996 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
998 gst_vaapi_display_destroy (display);
999 g_rec_mutex_clear (&priv->mutex);
1003 gst_vaapi_display_class_init (GstVaapiDisplayClass * klass)
1005 GstVaapiMiniObjectClass *const object_class =
1006 GST_VAAPI_MINI_OBJECT_CLASS (klass);
1007 GstVaapiDisplayClass *const dpy_class = GST_VAAPI_DISPLAY_CLASS (klass);
1009 libgstvaapi_init_once ();
1011 object_class->size = sizeof (GstVaapiDisplay);
1012 object_class->finalize = (GDestroyNotify) gst_vaapi_display_finalize;
1013 dpy_class->lock = gst_vaapi_display_lock_default;
1014 dpy_class->unlock = gst_vaapi_display_unlock_default;
1018 gst_vaapi_display_properties_init (void)
1021 * GstVaapiDisplay:render-mode:
1023 * The VA display rendering mode, expressed as a #GstVaapiRenderMode.
1025 g_properties[PROP_RENDER_MODE] =
1026 g_param_spec_enum (GST_VAAPI_DISPLAY_PROP_RENDER_MODE,
1028 "The display rendering mode",
1029 GST_VAAPI_TYPE_RENDER_MODE, DEFAULT_RENDER_MODE, G_PARAM_READWRITE);
1032 * GstVaapiDisplay:rotation:
1034 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1036 g_properties[PROP_ROTATION] =
1037 g_param_spec_enum (GST_VAAPI_DISPLAY_PROP_ROTATION,
1039 "The display rotation mode",
1040 GST_VAAPI_TYPE_ROTATION, DEFAULT_ROTATION, G_PARAM_READWRITE);
1043 * GstVaapiDisplay:hue:
1045 * The VA display hue, expressed as a float value. Range is -180.0
1046 * to 180.0. Default value is 0.0 and represents no modification.
1048 g_properties[PROP_HUE] =
1049 g_param_spec_float (GST_VAAPI_DISPLAY_PROP_HUE,
1050 "hue", "The display hue value", -180.0, 180.0, 0.0, G_PARAM_READWRITE);
1053 * GstVaapiDisplay:saturation:
1055 * The VA display saturation, expressed as a float value. Range is
1056 * 0.0 to 2.0. Default value is 1.0 and represents no modification.
1058 g_properties[PROP_SATURATION] =
1059 g_param_spec_float (GST_VAAPI_DISPLAY_PROP_SATURATION,
1061 "The display saturation value", 0.0, 2.0, 1.0, G_PARAM_READWRITE);
1064 * GstVaapiDisplay:brightness:
1066 * The VA display brightness, expressed as a float value. Range is
1067 * -1.0 to 1.0. Default value is 0.0 and represents no modification.
1069 g_properties[PROP_BRIGHTNESS] =
1070 g_param_spec_float (GST_VAAPI_DISPLAY_PROP_BRIGHTNESS,
1072 "The display brightness value", -1.0, 1.0, 0.0, G_PARAM_READWRITE);
1075 * GstVaapiDisplay:contrast:
1077 * The VA display contrast, expressed as a float value. Range is 0.0
1078 * to 2.0. Default value is 1.0 and represents no modification.
1080 g_properties[PROP_CONTRAST] =
1081 g_param_spec_float (GST_VAAPI_DISPLAY_PROP_CONTRAST,
1083 "The display contrast value", 0.0, 2.0, 1.0, G_PARAM_READWRITE);
1086 static inline const GstVaapiDisplayClass *
1087 gst_vaapi_display_class (void)
1089 static GstVaapiDisplayClass g_class;
1090 static gsize g_class_init = FALSE;
1092 if (g_once_init_enter (&g_class_init)) {
1093 gst_vaapi_display_class_init (&g_class);
1094 g_once_init_leave (&g_class_init, TRUE);
1100 gst_vaapi_display_new (const GstVaapiDisplayClass * klass,
1101 GstVaapiDisplayInitType init_type, gpointer init_value)
1103 GstVaapiDisplay *display;
1105 display = (GstVaapiDisplay *)
1106 gst_vaapi_mini_object_new0 (GST_VAAPI_MINI_OBJECT_CLASS (klass));
1110 gst_vaapi_display_init (display);
1111 if (!gst_vaapi_display_create (display, init_type, init_value))
1116 gst_vaapi_display_unref_internal (display);
1121 * gst_vaapi_display_new_with_display:
1122 * @va_display: a #VADisplay
1124 * Creates a new #GstVaapiDisplay, using @va_display as the VA
1127 * Return value: the newly created #GstVaapiDisplay object
1130 gst_vaapi_display_new_with_display (VADisplay va_display)
1132 GstVaapiDisplayCache *const cache = get_display_cache ();
1133 const GstVaapiDisplayInfo *info;
1135 g_return_val_if_fail (va_display != NULL, NULL);
1136 g_return_val_if_fail (cache != NULL, NULL);
1138 info = gst_vaapi_display_cache_lookup_by_va_display (cache, va_display);
1140 return gst_vaapi_display_ref_internal (info->display);
1142 return gst_vaapi_display_new (gst_vaapi_display_class (),
1143 GST_VAAPI_DISPLAY_INIT_FROM_VA_DISPLAY, va_display);
1147 * gst_vaapi_display_ref:
1148 * @display: a #GstVaapiDisplay
1150 * Atomically increases the reference count of the given @display by one.
1152 * Returns: The same @display argument
1155 gst_vaapi_display_ref (GstVaapiDisplay * display)
1157 return gst_vaapi_display_ref_internal (display);
1161 * gst_vaapi_display_unref:
1162 * @display: a #GstVaapiDisplay
1164 * Atomically decreases the reference count of the @display by one. If
1165 * the reference count reaches zero, the display will be free'd.
1168 gst_vaapi_display_unref (GstVaapiDisplay * display)
1170 gst_vaapi_display_unref_internal (display);
1174 * gst_vaapi_display_replace:
1175 * @old_display_ptr: a pointer to a #GstVaapiDisplay
1176 * @new_display: a #GstVaapiDisplay
1178 * Atomically replaces the display display held in @old_display_ptr
1179 * with @new_display. This means that @old_display_ptr shall reference
1180 * a valid display. However, @new_display can be NULL.
1183 gst_vaapi_display_replace (GstVaapiDisplay ** old_display_ptr,
1184 GstVaapiDisplay * new_display)
1186 gst_vaapi_display_replace_internal (old_display_ptr, new_display);
1190 * gst_vaapi_display_lock:
1191 * @display: a #GstVaapiDisplay
1193 * Locks @display. If @display is already locked by another thread,
1194 * the current thread will block until @display is unlocked by the
1198 gst_vaapi_display_lock (GstVaapiDisplay * display)
1200 GstVaapiDisplayClass *klass;
1202 g_return_if_fail (display != NULL);
1204 klass = GST_VAAPI_DISPLAY_GET_CLASS (display);
1206 klass->lock (display);
1210 * gst_vaapi_display_unlock:
1211 * @display: a #GstVaapiDisplay
1213 * Unlocks @display. If another thread is blocked in a
1214 * gst_vaapi_display_lock() call for @display, it will be woken and
1215 * can lock @display itself.
1218 gst_vaapi_display_unlock (GstVaapiDisplay * display)
1220 GstVaapiDisplayClass *klass;
1222 g_return_if_fail (display != NULL);
1224 klass = GST_VAAPI_DISPLAY_GET_CLASS (display);
1226 klass->unlock (display);
1230 * gst_vaapi_display_sync:
1231 * @display: a #GstVaapiDisplay
1233 * Flushes any requests queued for the windowing system and waits until
1234 * all requests have been handled. This is often used for making sure
1235 * that the display is synchronized with the current state of the program.
1237 * This is most useful for X11. On windowing systems where requests are
1238 * handled synchronously, this function will do nothing.
1241 gst_vaapi_display_sync (GstVaapiDisplay * display)
1243 GstVaapiDisplayClass *klass;
1245 g_return_if_fail (display != NULL);
1247 klass = GST_VAAPI_DISPLAY_GET_CLASS (display);
1249 klass->sync (display);
1250 else if (klass->flush)
1251 klass->flush (display);
1255 * gst_vaapi_display_flush:
1256 * @display: a #GstVaapiDisplay
1258 * Flushes any requests queued for the windowing system.
1260 * This is most useful for X11. On windowing systems where requests
1261 * are handled synchronously, this function will do nothing.
1264 gst_vaapi_display_flush (GstVaapiDisplay * display)
1266 GstVaapiDisplayClass *klass;
1268 g_return_if_fail (display != NULL);
1270 klass = GST_VAAPI_DISPLAY_GET_CLASS (display);
1272 klass->flush (display);
1276 * gst_vaapi_display_get_display:
1277 * @display: a #GstVaapiDisplay
1279 * Returns the #GstVaapiDisplayType bound to @display.
1281 * Return value: the #GstVaapiDisplayType
1284 gst_vaapi_display_get_display_type (GstVaapiDisplay * display)
1286 g_return_val_if_fail (display != NULL, GST_VAAPI_DISPLAY_TYPE_ANY);
1288 return GST_VAAPI_DISPLAY_GET_PRIVATE (display)->display_type;
1292 * gst_vaapi_display_get_display:
1293 * @display: a #GstVaapiDisplay
1295 * Returns the #VADisplay bound to @display.
1297 * Return value: the #VADisplay
1300 gst_vaapi_display_get_display (GstVaapiDisplay * display)
1302 g_return_val_if_fail (display != NULL, NULL);
1304 return GST_VAAPI_DISPLAY_GET_PRIVATE (display)->display;
1308 * gst_vaapi_display_get_width:
1309 * @display: a #GstVaapiDisplay
1311 * Retrieves the width of a #GstVaapiDisplay.
1313 * Return value: the width of the @display, in pixels
1316 gst_vaapi_display_get_width (GstVaapiDisplay * display)
1318 g_return_val_if_fail (display != NULL, 0);
1320 return GST_VAAPI_DISPLAY_GET_PRIVATE (display)->width;
1324 * gst_vaapi_display_get_height:
1325 * @display: a #GstVaapiDisplay
1327 * Retrieves the height of a #GstVaapiDisplay
1329 * Return value: the height of the @display, in pixels
1332 gst_vaapi_display_get_height (GstVaapiDisplay * display)
1334 g_return_val_if_fail (display != NULL, 0);
1336 return GST_VAAPI_DISPLAY_GET_PRIVATE (display)->height;
1340 * gst_vaapi_display_get_size:
1341 * @display: a #GstVaapiDisplay
1342 * @pwidth: return location for the width, or %NULL
1343 * @pheight: return location for the height, or %NULL
1345 * Retrieves the dimensions of a #GstVaapiDisplay.
1348 gst_vaapi_display_get_size (GstVaapiDisplay * display, guint * pwidth,
1351 g_return_if_fail (GST_VAAPI_DISPLAY (display));
1354 *pwidth = GST_VAAPI_DISPLAY_GET_PRIVATE (display)->width;
1357 *pheight = GST_VAAPI_DISPLAY_GET_PRIVATE (display)->height;
1361 * gst_vaapi_display_get_pixel_aspect_ratio:
1362 * @display: a #GstVaapiDisplay
1363 * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
1364 * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
1366 * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
1369 gst_vaapi_display_get_pixel_aspect_ratio (GstVaapiDisplay * display,
1370 guint * par_n, guint * par_d)
1372 g_return_if_fail (display != NULL);
1375 *par_n = GST_VAAPI_DISPLAY_GET_PRIVATE (display)->par_n;
1378 *par_d = GST_VAAPI_DISPLAY_GET_PRIVATE (display)->par_d;
1382 * gst_vaapi_display_has_video_processing:
1383 * @display: a #GstVaapiDisplay
1385 * Checks whether the underlying VA driver implementation supports
1386 * video processing (VPP) acceleration.
1388 * Returns: %TRUE if some VPP features are available
1391 gst_vaapi_display_has_video_processing (GstVaapiDisplay * display)
1393 g_return_val_if_fail (display != NULL, FALSE);
1395 if (!ensure_profiles (display))
1397 return GST_VAAPI_DISPLAY_GET_PRIVATE (display)->has_vpp;
1401 * gst_vaapi_display_get_decode_profiles:
1402 * @display: a #GstVaapiDisplay
1404 * Gets the supported profiles for decoding. The caller owns an extra
1405 * reference to the resulting array of #GstVaapiProfile elements, so
1406 * it shall be released with g_array_unref() after usage.
1408 * Return value: a newly allocated #GArray, or %NULL or error or if
1409 * decoding is not supported at all
1412 gst_vaapi_display_get_decode_profiles (GstVaapiDisplay * display)
1414 g_return_val_if_fail (display != NULL, NULL);
1416 if (!ensure_profiles (display))
1418 return get_profiles (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->decoders);
1422 * gst_vaapi_display_has_decoder:
1423 * @display: a #GstVaapiDisplay
1424 * @profile: a #VAProfile
1425 * @entrypoint: a #GstVaaiEntrypoint
1427 * Returns whether VA @display supports @profile for decoding at the
1428 * specified @entrypoint.
1430 * Return value: %TRUE if VA @display supports @profile for decoding.
1433 gst_vaapi_display_has_decoder (GstVaapiDisplay * display,
1434 GstVaapiProfile profile, GstVaapiEntrypoint entrypoint)
1436 g_return_val_if_fail (display != NULL, FALSE);
1438 if (!ensure_profiles (display))
1440 return find_config (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->decoders,
1441 profile, entrypoint);
1445 * gst_vaapi_display_get_encode_profiles:
1446 * @display: a #GstVaapiDisplay
1448 * Gets the supported profiles for encoding. The caller owns an extra
1449 * reference to the resulting array of #GstVaapiProfile elements, so
1450 * it shall be released with g_array_unref() after usage.
1452 * Return value: a newly allocated #GArray, or %NULL or error or if
1453 * encoding is not supported at all
1456 gst_vaapi_display_get_encode_profiles (GstVaapiDisplay * display)
1458 g_return_val_if_fail (display != NULL, NULL);
1460 if (!ensure_profiles (display))
1462 return get_profiles (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->encoders);
1466 * gst_vaapi_display_has_encoder:
1467 * @display: a #GstVaapiDisplay
1468 * @profile: a #VAProfile
1469 * @entrypoint: a #GstVaapiEntrypoint
1471 * Returns whether VA @display supports @profile for encoding at the
1472 * specified @entrypoint.
1474 * Return value: %TRUE if VA @display supports @profile for encoding.
1477 gst_vaapi_display_has_encoder (GstVaapiDisplay * display,
1478 GstVaapiProfile profile, GstVaapiEntrypoint entrypoint)
1480 g_return_val_if_fail (display != NULL, FALSE);
1482 if (!ensure_profiles (display))
1484 return find_config (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->encoders,
1485 profile, entrypoint);
1489 * gst_vaapi_display_get_image_formats:
1490 * @display: a #GstVaapiDisplay
1492 * Gets the supported image formats for gst_vaapi_surface_get_image()
1493 * or gst_vaapi_surface_put_image().
1495 * Note that this method does not necessarily map image formats
1496 * returned by vaQueryImageFormats(). The set of capabilities can be
1497 * stripped down, if gstreamer-vaapi does not support the format, or
1498 * expanded to cover compatible formats not exposed by the underlying
1499 * driver. e.g. I420 can be supported even if the driver only exposes
1502 * Note: the caller owns an extra reference to the resulting array of
1503 * #GstVideoFormat elements, so it shall be released with
1504 * g_array_unref() after usage.
1506 * Return value: a newly allocated #GArray, or %NULL on error or if
1510 gst_vaapi_display_get_image_formats (GstVaapiDisplay * display)
1512 g_return_val_if_fail (display != NULL, NULL);
1514 if (!ensure_image_formats (display))
1516 return get_formats (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->image_formats);
1520 * gst_vaapi_display_has_image_format:
1521 * @display: a #GstVaapiDisplay
1522 * @format: a #GstVideoFormat
1524 * Returns whether VA @display supports @format image format.
1526 * Return value: %TRUE if VA @display supports @format image format
1529 gst_vaapi_display_has_image_format (GstVaapiDisplay * display,
1530 GstVideoFormat format)
1532 GstVaapiDisplayPrivate *priv;
1534 g_return_val_if_fail (display != NULL, FALSE);
1535 g_return_val_if_fail (format, FALSE);
1537 priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
1539 if (!ensure_image_formats (display))
1541 if (find_format (priv->image_formats, format))
1544 /* XXX: try subpicture formats since some drivers could report a
1545 * set of VA image formats that is not a superset of the set of VA
1546 * subpicture formats
1548 if (!ensure_subpicture_formats (display))
1550 return find_format (priv->subpicture_formats, format);
1554 * gst_vaapi_display_get_subpicture_formats:
1555 * @display: a #GstVaapiDisplay
1557 * Gets the supported subpicture formats.
1559 * Note that this method does not necessarily map subpicture formats
1560 * returned by vaQuerySubpictureFormats(). The set of capabilities can
1561 * be stripped down if gstreamer-vaapi does not support the
1562 * format. e.g. this is the case for paletted formats like IA44.
1564 * Note: the caller owns an extra reference to the resulting array of
1565 * #GstVideoFormat elements, so it shall be released with
1566 * g_array_unref() after usage.
1568 * Return value: a newly allocated #GArray, or %NULL on error of if
1572 gst_vaapi_display_get_subpicture_formats (GstVaapiDisplay * display)
1574 g_return_val_if_fail (display != NULL, NULL);
1576 if (!ensure_subpicture_formats (display))
1578 return get_formats (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->
1579 subpicture_formats);
1583 * gst_vaapi_display_has_subpicture_format:
1584 * @display: a #GstVaapiDisplay
1585 * @format: a #GstVideoFormat
1586 * @flags_ptr: pointer to #GstVaapiSubpictureFlags, or zero
1588 * Returns whether VA @display supports @format subpicture format with
1589 * the supplied @flags.
1591 * Return value: %TRUE if VA @display supports @format subpicture format
1594 gst_vaapi_display_has_subpicture_format (GstVaapiDisplay * display,
1595 GstVideoFormat format, guint * flags_ptr)
1597 GstVaapiDisplayPrivate *priv;
1598 const GstVaapiFormatInfo *fip;
1600 g_return_val_if_fail (display != NULL, FALSE);
1601 g_return_val_if_fail (format, FALSE);
1603 priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
1605 if (!ensure_subpicture_formats (display))
1608 fip = find_format_info (priv->subpicture_formats, format);
1613 *flags_ptr = fip->flags;
1618 * gst_vaapi_display_has_property:
1619 * @display: a #GstVaapiDisplay
1620 * @name: the property name to check
1622 * Returns whether VA @display supports the requested property. The
1623 * check is performed against the property @name. So, the client
1624 * application may perform this check only once and cache this
1627 * Return value: %TRUE if VA @display supports property @name
1630 gst_vaapi_display_has_property (GstVaapiDisplay * display, const gchar * name)
1632 g_return_val_if_fail (display != NULL, FALSE);
1633 g_return_val_if_fail (name, FALSE);
1635 if (!ensure_properties (display))
1637 return find_property (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->properties,
1642 gst_vaapi_display_get_property (GstVaapiDisplay * display, const gchar * name,
1645 const GstVaapiProperty *prop;
1647 g_return_val_if_fail (display != NULL, FALSE);
1648 g_return_val_if_fail (name != NULL, FALSE);
1649 g_return_val_if_fail (out_value != NULL, FALSE);
1651 if (!ensure_properties (display))
1655 find_property (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->properties, name);
1659 switch (prop->attribute.type) {
1660 case VADisplayAttribRenderMode:{
1661 GstVaapiRenderMode mode;
1662 if (!gst_vaapi_display_get_render_mode (display, &mode))
1664 g_value_init (out_value, GST_VAAPI_TYPE_RENDER_MODE);
1665 g_value_set_enum (out_value, mode);
1668 case VADisplayAttribRotation:{
1669 GstVaapiRotation rotation;
1670 rotation = gst_vaapi_display_get_rotation (display);
1671 g_value_init (out_value, GST_VAAPI_TYPE_ROTATION);
1672 g_value_set_enum (out_value, rotation);
1675 case VADisplayAttribHue:
1676 case VADisplayAttribSaturation:
1677 case VADisplayAttribBrightness:
1678 case VADisplayAttribContrast:{
1680 if (!get_color_balance (display, find_property_id (name), &value))
1682 g_value_init (out_value, G_TYPE_FLOAT);
1683 g_value_set_float (out_value, value);
1687 GST_WARNING ("unsupported property '%s'", name);
1694 gst_vaapi_display_set_property (GstVaapiDisplay * display, const gchar * name,
1695 const GValue * value)
1697 const GstVaapiProperty *prop;
1699 g_return_val_if_fail (display != NULL, FALSE);
1700 g_return_val_if_fail (name != NULL, FALSE);
1701 g_return_val_if_fail (value != NULL, FALSE);
1703 if (!ensure_properties (display))
1707 find_property (GST_VAAPI_DISPLAY_GET_PRIVATE (display)->properties, name);
1711 switch (prop->attribute.type) {
1712 case VADisplayAttribRenderMode:{
1713 GstVaapiRenderMode mode;
1714 if (!G_VALUE_HOLDS (value, GST_VAAPI_TYPE_RENDER_MODE))
1716 mode = g_value_get_enum (value);
1717 return gst_vaapi_display_set_render_mode (display, mode);
1719 case VADisplayAttribRotation:{
1720 GstVaapiRotation rotation;
1721 if (!G_VALUE_HOLDS (value, GST_VAAPI_TYPE_ROTATION))
1723 rotation = g_value_get_enum (value);
1724 return gst_vaapi_display_set_rotation (display, rotation);
1726 case VADisplayAttribHue:
1727 case VADisplayAttribSaturation:
1728 case VADisplayAttribBrightness:
1729 case VADisplayAttribContrast:{
1731 if (!G_VALUE_HOLDS (value, G_TYPE_FLOAT))
1733 v = g_value_get_float (value);
1734 return set_color_balance (display, find_property_id (name), v);
1740 GST_WARNING ("unsupported property '%s'", name);
1745 get_attribute (GstVaapiDisplay * display, VADisplayAttribType type,
1748 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
1749 VADisplayAttribute attr;
1753 attr.flags = VA_DISPLAY_ATTRIB_GETTABLE;
1754 status = vaGetDisplayAttributes (priv->display, &attr, 1);
1755 if (!vaapi_check_status (status, "vaGetDisplayAttributes()"))
1757 *value = attr.value;
1762 set_attribute (GstVaapiDisplay * display, VADisplayAttribType type, gint value)
1764 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
1765 VADisplayAttribute attr;
1770 attr.flags = VA_DISPLAY_ATTRIB_SETTABLE;
1771 status = vaSetDisplayAttributes (priv->display, &attr, 1);
1772 if (!vaapi_check_status (status, "vaSetDisplayAttributes()"))
1778 get_render_mode_VADisplayAttribRenderMode (GstVaapiDisplay * display,
1779 GstVaapiRenderMode * pmode)
1781 gint modes, devices;
1783 if (!get_attribute (display, VADisplayAttribRenderDevice, &devices))
1787 if (!get_attribute (display, VADisplayAttribRenderMode, &modes))
1790 /* Favor "overlay" mode since it is the most restrictive one */
1791 if (modes & (VA_RENDER_MODE_LOCAL_OVERLAY | VA_RENDER_MODE_EXTERNAL_OVERLAY))
1792 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1794 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1799 get_render_mode_VADisplayAttribDirectSurface (GstVaapiDisplay * display,
1800 GstVaapiRenderMode * pmode)
1802 #if VA_CHECK_VERSION(0,34,0)
1803 /* VADisplayAttribDirectsurface was removed in VA-API >= 0.34.0 */
1806 gint direct_surface;
1808 if (!get_attribute (display, VADisplayAttribDirectSurface, &direct_surface))
1811 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1813 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1819 get_render_mode_default (GstVaapiDisplay * display, GstVaapiRenderMode * pmode)
1821 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
1823 switch (priv->display_type) {
1825 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1826 /* wl_buffer mapped from VA surface through vaGetSurfaceBufferWl() */
1827 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1831 case GST_VAAPI_DISPLAY_TYPE_DRM:
1832 /* vaGetSurfaceBufferDRM() returns the underlying DRM buffer handle */
1833 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1837 /* This includes VA/X11 and VA/GLX modes */
1838 *pmode = DEFAULT_RENDER_MODE;
1845 * gst_vaapi_display_get_render_mode:
1846 * @display: a #GstVaapiDisplay
1847 * @pmode: return location for the VA @display rendering mode
1849 * Returns the current VA @display rendering mode.
1851 * Return value: %TRUE if VA @display rendering mode could be determined
1854 gst_vaapi_display_get_render_mode (GstVaapiDisplay * display,
1855 GstVaapiRenderMode * pmode)
1857 g_return_val_if_fail (display != NULL, FALSE);
1859 /* Try with render-mode attribute */
1860 if (get_render_mode_VADisplayAttribRenderMode (display, pmode))
1863 /* Try with direct-surface attribute */
1864 if (get_render_mode_VADisplayAttribDirectSurface (display, pmode))
1867 /* Default: determine from the display type */
1868 return get_render_mode_default (display, pmode);
1872 * gst_vaapi_display_set_render_mode:
1873 * @display: a #GstVaapiDisplay
1874 * @mode: the #GstVaapiRenderMode to set
1876 * Sets the VA @display rendering mode to the supplied @mode. This
1877 * function returns %FALSE if the rendering mode could not be set,
1878 * e.g. run-time switching rendering mode is not supported.
1880 * Return value: %TRUE if VA @display rendering @mode could be changed
1881 * to the requested value
1884 gst_vaapi_display_set_render_mode (GstVaapiDisplay * display,
1885 GstVaapiRenderMode mode)
1887 gint modes, devices;
1889 g_return_val_if_fail (display != NULL, FALSE);
1891 if (!get_attribute (display, VADisplayAttribRenderDevice, &devices))
1896 case GST_VAAPI_RENDER_MODE_OVERLAY:
1897 if (devices & VA_RENDER_DEVICE_LOCAL)
1898 modes |= VA_RENDER_MODE_LOCAL_OVERLAY;
1899 if (devices & VA_RENDER_DEVICE_EXTERNAL)
1900 modes |= VA_RENDER_MODE_EXTERNAL_OVERLAY;
1902 case GST_VAAPI_RENDER_MODE_TEXTURE:
1903 if (devices & VA_RENDER_DEVICE_LOCAL)
1904 modes |= VA_RENDER_MODE_LOCAL_GPU;
1905 if (devices & VA_RENDER_DEVICE_EXTERNAL)
1906 modes |= VA_RENDER_MODE_EXTERNAL_GPU;
1911 if (!set_attribute (display, VADisplayAttribRenderMode, modes))
1917 * gst_vaapi_display_get_rotation:
1918 * @display: a #GstVaapiDisplay
1920 * Returns the current VA @display rotation angle. If the VA driver
1921 * does not support "rotation" display attribute, then the display is
1922 * assumed to be un-rotated.
1924 * Return value: the current #GstVaapiRotation value
1927 gst_vaapi_display_get_rotation (GstVaapiDisplay * display)
1931 g_return_val_if_fail (display != NULL, DEFAULT_ROTATION);
1933 if (!get_attribute (display, VADisplayAttribRotation, &value))
1934 value = VA_ROTATION_NONE;
1935 return to_GstVaapiRotation (value);
1939 * gst_vaapi_display_set_rotation:
1940 * @display: a #GstVaapiDisplay
1941 * @rotation: the #GstVaapiRotation value to set
1943 * Sets the VA @display rotation angle to the supplied @rotation
1944 * value. This function returns %FALSE if the rotation angle could not
1945 * be set, e.g. the VA driver does not allow to change the display
1948 * Return value: %TRUE if VA @display rotation angle could be changed
1949 * to the requested value
1952 gst_vaapi_display_set_rotation (GstVaapiDisplay * display,
1953 GstVaapiRotation rotation)
1957 g_return_val_if_fail (display != NULL, FALSE);
1959 value = from_GstVaapiRotation (rotation);
1960 if (!set_attribute (display, VADisplayAttribRotation, value))
1965 /* Get color balance attributes */
1967 get_color_balance (GstVaapiDisplay * display, guint prop_id, gfloat * v)
1969 GParamSpecFloat *const pspec = G_PARAM_SPEC_FLOAT (g_properties[prop_id]);
1970 const GstVaapiProperty *prop;
1971 const VADisplayAttribute *attr;
1975 if (!ensure_properties (display))
1981 prop = find_property_by_pspec (display, &pspec->parent_instance);
1984 attr = &prop->attribute;
1986 if (!get_attribute (display, attr->type, &value))
1989 /* Scale wrt. the medium ("default") value */
1990 out_value = pspec->default_value;
1991 if (value > attr->value)
1992 out_value += ((gfloat) (value - attr->value) /
1993 (attr->max_value - attr->value) *
1994 (pspec->maximum - pspec->default_value));
1995 else if (value < attr->value)
1996 out_value -= ((gfloat) (attr->value - value) /
1997 (attr->value - attr->min_value) *
1998 (pspec->default_value - pspec->minimum));
2003 /* Set color balance attribute */
2005 set_color_balance (GstVaapiDisplay * display, guint prop_id, gfloat v)
2007 GParamSpecFloat *const pspec = G_PARAM_SPEC_FLOAT (g_properties[prop_id]);
2008 const GstVaapiProperty *prop;
2009 const VADisplayAttribute *attr;
2012 if (!ensure_properties (display))
2018 prop = find_property_by_pspec (display, &pspec->parent_instance);
2021 attr = &prop->attribute;
2023 /* Scale wrt. the medium ("default") value */
2024 value = attr->value;
2025 if (v > pspec->default_value)
2026 value += ((v - pspec->default_value) /
2027 (pspec->maximum - pspec->default_value) *
2028 (attr->max_value - attr->value));
2029 else if (v < pspec->default_value)
2030 value -= ((pspec->default_value - v) /
2031 (pspec->default_value - pspec->minimum) *
2032 (attr->value - attr->min_value));
2033 if (!set_attribute (display, attr->type, value))
2038 /* Ensures the VA driver vendor string was copied */
2040 ensure_vendor_string (GstVaapiDisplay * display)
2042 GstVaapiDisplayPrivate *const priv = GST_VAAPI_DISPLAY_GET_PRIVATE (display);
2043 const gchar *vendor_string;
2045 GST_VAAPI_DISPLAY_LOCK (display);
2046 if (!priv->vendor_string) {
2047 vendor_string = vaQueryVendorString (priv->display);
2049 priv->vendor_string = g_strdup (vendor_string);
2051 GST_VAAPI_DISPLAY_UNLOCK (display);
2052 return priv->vendor_string != NULL;
2056 * gst_vaapi_display_get_vendor_string:
2057 * @display: a #GstVaapiDisplay
2059 * Returns the VA driver vendor string attached to the supplied VA @display.
2060 * The @display owns the vendor string, do *not* de-allocate it.
2062 * This function is thread safe.
2064 * Return value: the current #GstVaapiRotation value
2067 gst_vaapi_display_get_vendor_string (GstVaapiDisplay * display)
2069 g_return_val_if_fail (display != NULL, NULL);
2071 if (!ensure_vendor_string (display))
2073 return display->priv.vendor_string;