2 * gstvaapidisplay.c - VA display abstraction
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2012 Intel Corporation
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 * SECTION:gstvaapidisplay
25 * @short_description: VA display abstraction
30 #include "gstvaapiutils.h"
31 #include "gstvaapidisplay.h"
32 #include "gstvaapidisplay_priv.h"
33 #include "gstvaapiworkarounds.h"
36 #include "gstvaapidebug.h"
38 GST_DEBUG_CATEGORY(gst_debug_vaapi);
40 G_DEFINE_TYPE(GstVaapiDisplay, gst_vaapi_display, G_TYPE_OBJECT);
42 typedef struct _GstVaapiConfig GstVaapiConfig;
43 struct _GstVaapiConfig {
44 GstVaapiProfile profile;
45 GstVaapiEntrypoint entrypoint;
48 typedef struct _GstVaapiProperty GstVaapiProperty;
49 struct _GstVaapiProperty {
51 VADisplayAttribute attribute;
54 /* XXX: export property names when the API is stable enough */
55 #define GST_VAAPI_DISPLAY_PROP_RENDER_MODE "render-mode"
56 #define GST_VAAPI_DISPLAY_PROP_ROTATION "rotation"
69 static GstVaapiDisplayCache *g_display_cache = NULL;
71 static GParamSpec *g_properties[N_PROPERTIES] = { NULL, };
73 static inline GstVaapiDisplayCache *
74 get_display_cache(void)
77 g_display_cache = gst_vaapi_display_cache_new();
78 return g_display_cache;
81 GstVaapiDisplayCache *
82 gst_vaapi_display_get_cache(void)
84 return get_display_cache();
88 free_display_cache(void)
92 if (gst_vaapi_display_cache_get_size(g_display_cache) > 0)
94 gst_vaapi_display_cache_free(g_display_cache);
95 g_display_cache = NULL;
98 /* GstVaapiDisplayType enumerations */
100 gst_vaapi_display_type_get_type(void)
102 static GType g_type = 0;
104 static const GEnumValue display_types[] = {
105 { GST_VAAPI_DISPLAY_TYPE_ANY,
106 "Auto detection", "any" },
108 { GST_VAAPI_DISPLAY_TYPE_X11,
109 "VA/X11 display", "x11" },
112 { GST_VAAPI_DISPLAY_TYPE_GLX,
113 "VA/GLX display", "glx" },
116 { GST_VAAPI_DISPLAY_TYPE_WAYLAND,
117 "VA/Wayland display", "wayland" },
120 { GST_VAAPI_DISPLAY_TYPE_DRM,
121 "VA/DRM display", "drm" },
127 g_type = g_enum_register_static("GstVaapiDisplayType", display_types);
131 /* Append GstVaapiImageFormat to formats array */
133 append_format(GArray *formats, GstVaapiImageFormat format)
135 g_array_append_val(formats, format);
138 /* Append VAImageFormats to formats array */
140 append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
142 GstVaapiImageFormat format;
143 gboolean has_YV12 = FALSE;
144 gboolean has_I420 = FALSE;
147 for (i = 0; i < n; i++) {
148 const VAImageFormat * const va_format = &va_formats[i];
150 format = gst_vaapi_image_format(va_format);
152 GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
153 GST_FOURCC_ARGS(va_format->fourcc));
158 case GST_VAAPI_IMAGE_YV12:
161 case GST_VAAPI_IMAGE_I420:
167 append_format(formats, format);
170 /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
171 supported by the underlying driver */
172 if (has_YV12 && !has_I420)
173 append_format(formats, GST_VAAPI_IMAGE_I420);
174 else if (has_I420 && !has_YV12)
175 append_format(formats, GST_VAAPI_IMAGE_YV12);
178 /* Sort image formats. Prefer YUV formats first */
180 compare_yuv_formats(gconstpointer a, gconstpointer b)
182 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
183 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
185 const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
186 const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);
188 if (is_fmt1_yuv != is_fmt2_yuv)
189 return is_fmt1_yuv ? -1 : 1;
191 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
192 (gint)gst_vaapi_image_format_get_score(fmt2));
195 /* Sort subpicture formats. Prefer RGB formats first */
197 compare_rgb_formats(gconstpointer a, gconstpointer b)
199 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
200 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
202 const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
203 const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);
205 if (is_fmt1_rgb != is_fmt2_rgb)
206 return is_fmt1_rgb ? -1 : 1;
208 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
209 (gint)gst_vaapi_image_format_get_score(fmt2));
212 /* Check if configs array contains profile at entrypoint */
213 static inline gboolean
216 GstVaapiProfile profile,
217 GstVaapiEntrypoint entrypoint
220 GstVaapiConfig *config;
226 for (i = 0; i < configs->len; i++) {
227 config = &g_array_index(configs, GstVaapiConfig, i);
228 if (config->profile == profile && config->entrypoint == entrypoint)
234 /* HACK: append H.263 Baseline profile if MPEG-4:2 Simple profile is supported */
236 append_h263_config(GArray *configs)
238 GstVaapiConfig *config, tmp_config;
239 GstVaapiConfig *mpeg4_simple_config = NULL;
240 GstVaapiConfig *h263_baseline_config = NULL;
243 if (!WORKAROUND_H263_BASELINE_DECODE_PROFILE)
249 for (i = 0; i < configs->len; i++) {
250 config = &g_array_index(configs, GstVaapiConfig, i);
251 if (config->profile == GST_VAAPI_PROFILE_MPEG4_SIMPLE)
252 mpeg4_simple_config = config;
253 else if (config->profile == GST_VAAPI_PROFILE_H263_BASELINE)
254 h263_baseline_config = config;
257 if (mpeg4_simple_config && !h263_baseline_config) {
258 tmp_config = *mpeg4_simple_config;
259 tmp_config.profile = GST_VAAPI_PROFILE_H263_BASELINE;
260 g_array_append_val(configs, tmp_config);
264 /* Convert configs array to profiles as GstCaps */
266 get_profile_caps(GArray *configs)
268 GstVaapiConfig *config;
269 GstCaps *out_caps, *caps;
275 out_caps = gst_caps_new_empty();
279 for (i = 0; i < configs->len; i++) {
280 config = &g_array_index(configs, GstVaapiConfig, i);
281 caps = gst_vaapi_profile_get_caps(config->profile);
283 gst_caps_merge(out_caps, caps);
288 /* Check if formats array contains format */
289 static inline gboolean
290 find_format(GArray *formats, GstVaapiImageFormat format)
294 for (i = 0; i < formats->len; i++)
295 if (g_array_index(formats, GstVaapiImageFormat, i) == format)
300 /* Convert formats array to GstCaps */
302 get_format_caps(GArray *formats)
304 GstVaapiImageFormat format;
305 GstCaps *out_caps, *caps;
308 out_caps = gst_caps_new_empty();
312 for (i = 0; i < formats->len; i++) {
313 format = g_array_index(formats, GstVaapiImageFormat, i);
314 caps = gst_vaapi_image_format_get_caps(format);
316 gst_caps_append(out_caps, caps);
321 /* Find display attribute */
322 static const GstVaapiProperty *
323 find_property(GArray *properties, const gchar *name)
325 GstVaapiProperty *prop;
331 for (i = 0; i < properties->len; i++) {
332 prop = &g_array_index(properties, GstVaapiProperty, i);
333 if (strcmp(prop->name, name) == 0)
340 static const GstVaapiProperty *
341 find_property_by_type(GArray *properties, VADisplayAttribType type)
343 GstVaapiProperty *prop;
346 for (i = 0; i < properties->len; i++) {
347 prop = &g_array_index(properties, GstVaapiProperty, i);
348 if (prop->attribute.type == type)
356 gst_vaapi_display_calculate_pixel_aspect_ratio(GstVaapiDisplay *display)
358 GstVaapiDisplayPrivate * const priv = display->priv;
359 gdouble ratio, delta;
362 static const gint par[][2] = {
363 {1, 1}, /* regular screen */
364 {16, 15}, /* PAL TV */
365 {11, 10}, /* 525 line Rec.601 video */
366 {54, 59}, /* 625 line Rec.601 video */
367 {64, 45}, /* 1280x1024 on 16:9 display */
368 {5, 3}, /* 1280x1024 on 4:3 display */
369 {4, 3} /* 800x600 on 16:9 display */
372 /* First, calculate the "real" ratio based on the X values;
373 * which is the "physical" w/h divided by the w/h in pixels of the
375 if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
378 ratio = (gdouble)(priv->width_mm * priv->height) /
379 (priv->height_mm * priv->width);
380 GST_DEBUG("calculated pixel aspect ratio: %f", ratio);
382 /* Now, find the one from par[][2] with the lowest delta to the real one */
383 #define DELTA(idx) (ABS(ratio - ((gdouble)par[idx][0] / par[idx][1])))
387 for (i = 1; i < G_N_ELEMENTS(par); i++) {
388 const gdouble this_delta = DELTA(i);
389 if (this_delta < delta) {
396 priv->par_n = par[index][0];
397 priv->par_d = par[index][1];
401 gst_vaapi_display_destroy(GstVaapiDisplay *display)
403 GstVaapiDisplayPrivate * const priv = display->priv;
405 if (priv->decoders) {
406 g_array_free(priv->decoders, TRUE);
407 priv->decoders = NULL;
410 if (priv->encoders) {
411 g_array_free(priv->encoders, TRUE);
412 priv->encoders = NULL;
415 if (priv->image_formats) {
416 g_array_free(priv->image_formats, TRUE);
417 priv->image_formats = NULL;
420 if (priv->subpicture_formats) {
421 g_array_free(priv->subpicture_formats, TRUE);
422 priv->subpicture_formats = NULL;
425 if (priv->properties) {
426 g_array_free(priv->properties, TRUE);
427 priv->properties = NULL;
432 vaTerminate(priv->display);
433 priv->display = NULL;
436 if (priv->create_display) {
437 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
438 if (klass->close_display)
439 klass->close_display(display);
442 g_clear_object(&priv->parent);
444 if (g_display_cache) {
445 gst_vaapi_display_cache_remove(get_display_cache(), display);
446 free_display_cache();
451 gst_vaapi_display_create(GstVaapiDisplay *display)
453 GstVaapiDisplayPrivate * const priv = display->priv;
454 GstVaapiDisplayCache *cache;
455 gboolean has_errors = TRUE;
456 VADisplayAttribute *display_attrs = NULL;
457 VAProfile *profiles = NULL;
458 VAEntrypoint *entrypoints = NULL;
459 VAImageFormat *formats = NULL;
460 unsigned int *flags = NULL;
461 gint i, j, n, num_entrypoints, major_version, minor_version;
463 GstVaapiDisplayInfo info;
464 const GstVaapiDisplayInfo *cached_info = NULL;
466 memset(&info, 0, sizeof(info));
467 info.display = display;
468 info.display_type = priv->display_type;
471 info.va_display = priv->display;
472 else if (priv->create_display) {
473 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
474 if (klass->open_display && !klass->open_display(display))
476 if (!klass->get_display || !klass->get_display(display, &info))
478 priv->display = info.va_display;
479 priv->display_type = info.display_type;
481 klass->get_size(display, &priv->width, &priv->height);
482 if (klass->get_size_mm)
483 klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
484 gst_vaapi_display_calculate_pixel_aspect_ratio(display);
489 cache = get_display_cache();
492 cached_info = gst_vaapi_display_cache_lookup_by_va_display(
497 g_clear_object(&priv->parent);
498 priv->parent = g_object_ref(cached_info->display);
499 priv->display_type = cached_info->display_type;
503 status = vaInitialize(priv->display, &major_version, &minor_version);
504 if (!vaapi_check_status(status, "vaInitialize()"))
506 GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
510 profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
513 entrypoints = g_new(VAEntrypoint, vaMaxNumEntrypoints(priv->display));
516 status = vaQueryConfigProfiles(priv->display, profiles, &n);
517 if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
520 GST_DEBUG("%d profiles", n);
521 for (i = 0; i < n; i++) {
522 #if VA_CHECK_VERSION(0,34,0)
523 /* Introduced in VA/VPP API */
524 if (profiles[i] == VAProfileNone)
527 GST_DEBUG(" %s", string_of_VAProfile(profiles[i]));
530 priv->decoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
533 priv->encoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
537 for (i = 0; i < n; i++) {
538 GstVaapiConfig config;
540 config.profile = gst_vaapi_profile(profiles[i]);
544 status = vaQueryConfigEntrypoints(
547 entrypoints, &num_entrypoints
549 if (!vaapi_check_status(status, "vaQueryConfigEntrypoints()"))
552 for (j = 0; j < num_entrypoints; j++) {
553 config.entrypoint = gst_vaapi_entrypoint(entrypoints[j]);
554 switch (config.entrypoint) {
555 case GST_VAAPI_ENTRYPOINT_VLD:
556 case GST_VAAPI_ENTRYPOINT_IDCT:
557 case GST_VAAPI_ENTRYPOINT_MOCO:
558 g_array_append_val(priv->decoders, config);
560 case GST_VAAPI_ENTRYPOINT_SLICE_ENCODE:
561 g_array_append_val(priv->encoders, config);
566 append_h263_config(priv->decoders);
568 /* VA display attributes */
570 g_new(VADisplayAttribute, vaMaxNumDisplayAttributes(priv->display));
574 n = 0; /* XXX: workaround old GMA500 bug */
575 status = vaQueryDisplayAttributes(priv->display, display_attrs, &n);
576 if (!vaapi_check_status(status, "vaQueryDisplayAttributes()"))
579 priv->properties = g_array_new(FALSE, FALSE, sizeof(GstVaapiProperty));
580 if (!priv->properties)
583 GST_DEBUG("%d display attributes", n);
584 for (i = 0; i < n; i++) {
585 VADisplayAttribute * const attr = &display_attrs[i];
586 GstVaapiProperty prop;
588 GST_DEBUG(" %s", string_of_VADisplayAttributeType(attr->type));
590 switch (attr->type) {
591 #if !VA_CHECK_VERSION(0,34,0)
592 case VADisplayAttribDirectSurface:
593 prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
596 case VADisplayAttribRenderMode:
597 prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
599 case VADisplayAttribRotation:
600 prop.name = GST_VAAPI_DISPLAY_PROP_ROTATION;
608 prop.attribute = *attr;
609 g_array_append_val(priv->properties, prop);
612 /* VA image formats */
613 formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
616 status = vaQueryImageFormats(priv->display, formats, &n);
617 if (!vaapi_check_status(status, "vaQueryImageFormats()"))
620 GST_DEBUG("%d image formats", n);
621 for (i = 0; i < n; i++)
622 GST_DEBUG(" %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
624 priv->image_formats =
625 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
626 if (!priv->image_formats)
628 append_formats(priv->image_formats, formats, n);
629 g_array_sort(priv->image_formats, compare_yuv_formats);
631 /* VA subpicture formats */
632 n = vaMaxNumSubpictureFormats(priv->display);
633 formats = g_renew(VAImageFormat, formats, n);
634 flags = g_new(guint, n);
635 if (!formats || !flags)
637 status = vaQuerySubpictureFormats(priv->display, formats, flags, (guint *)&n);
638 if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
641 GST_DEBUG("%d subpicture formats", n);
642 for (i = 0; i < n; i++)
643 GST_DEBUG(" %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
645 priv->subpicture_formats =
646 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
647 if (!priv->subpicture_formats)
649 append_formats(priv->subpicture_formats, formats, n);
650 g_array_sort(priv->subpicture_formats, compare_rgb_formats);
653 if (!gst_vaapi_display_cache_add(cache, &info))
659 g_free(display_attrs);
668 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
670 GstVaapiDisplayPrivate *priv = display->priv;
673 priv = priv->parent->priv;
674 g_static_rec_mutex_lock(&priv->mutex);
678 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
680 GstVaapiDisplayPrivate *priv = display->priv;
683 priv = priv->parent->priv;
684 g_static_rec_mutex_unlock(&priv->mutex);
688 gst_vaapi_display_finalize(GObject *object)
690 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
692 gst_vaapi_display_destroy(display);
694 g_static_rec_mutex_free(&display->priv->mutex);
696 G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
700 gst_vaapi_display_set_property(
707 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
711 display->priv->display = g_value_get_pointer(value);
713 case PROP_DISPLAY_TYPE:
714 display->priv->display_type = g_value_get_enum(value);
717 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
723 gst_vaapi_display_get_property(
730 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
734 g_value_set_pointer(value, gst_vaapi_display_get_display(display));
736 case PROP_DISPLAY_TYPE:
737 g_value_set_enum(value, gst_vaapi_display_get_display_type(display));
740 g_value_set_uint(value, gst_vaapi_display_get_width(display));
743 g_value_set_uint(value, gst_vaapi_display_get_height(display));
746 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
752 gst_vaapi_display_constructed(GObject *object)
754 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
755 GObjectClass *parent_class;
757 display->priv->create_display = display->priv->display == NULL;
758 if (!gst_vaapi_display_create(display))
759 gst_vaapi_display_destroy(display);
761 parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
762 if (parent_class->constructed)
763 parent_class->constructed(object);
767 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
769 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
770 GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
772 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
774 g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
776 object_class->finalize = gst_vaapi_display_finalize;
777 object_class->set_property = gst_vaapi_display_set_property;
778 object_class->get_property = gst_vaapi_display_get_property;
779 object_class->constructed = gst_vaapi_display_constructed;
781 dpy_class->lock = gst_vaapi_display_lock_default;
782 dpy_class->unlock = gst_vaapi_display_unlock_default;
784 g_properties[PROP_DISPLAY] =
785 g_param_spec_pointer("display",
788 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY);
790 g_properties[PROP_DISPLAY_TYPE] =
791 g_param_spec_enum("display-type",
794 GST_VAAPI_TYPE_DISPLAY_TYPE,
795 GST_VAAPI_DISPLAY_TYPE_ANY,
796 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY);
798 g_properties[PROP_WIDTH] =
799 g_param_spec_uint("width",
805 g_properties[PROP_HEIGHT] =
806 g_param_spec_uint("height",
808 "The display height",
812 g_object_class_install_properties(object_class, N_PROPERTIES, g_properties);
816 gst_vaapi_display_init(GstVaapiDisplay *display)
818 GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
820 display->priv = priv;
822 priv->display_type = GST_VAAPI_DISPLAY_TYPE_ANY;
823 priv->display = NULL;
830 priv->decoders = NULL;
831 priv->encoders = NULL;
832 priv->image_formats = NULL;
833 priv->subpicture_formats = NULL;
834 priv->properties = NULL;
835 priv->create_display = TRUE;
837 g_static_rec_mutex_init(&priv->mutex);
841 * gst_vaapi_display_new_with_display:
842 * @va_display: a #VADisplay
844 * Creates a new #GstVaapiDisplay, using @va_display as the VA
847 * Return value: the newly created #GstVaapiDisplay object
850 gst_vaapi_display_new_with_display(VADisplay va_display)
852 GstVaapiDisplayCache * const cache = get_display_cache();
853 const GstVaapiDisplayInfo *info;
855 g_return_val_if_fail(va_display != NULL, NULL);
856 g_return_val_if_fail(cache != NULL, NULL);
858 info = gst_vaapi_display_cache_lookup_by_va_display(cache, va_display);
860 return g_object_ref(info->display);
862 return g_object_new(GST_VAAPI_TYPE_DISPLAY,
863 "display", va_display,
868 * gst_vaapi_display_lock:
869 * @display: a #GstVaapiDisplay
871 * Locks @display. If @display is already locked by another thread,
872 * the current thread will block until @display is unlocked by the
876 gst_vaapi_display_lock(GstVaapiDisplay *display)
878 GstVaapiDisplayClass *klass;
880 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
882 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
884 klass->lock(display);
888 * gst_vaapi_display_unlock:
889 * @display: a #GstVaapiDisplay
891 * Unlocks @display. If another thread is blocked in a
892 * gst_vaapi_display_lock() call for @display, it will be woken and
893 * can lock @display itself.
896 gst_vaapi_display_unlock(GstVaapiDisplay *display)
898 GstVaapiDisplayClass *klass;
900 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
902 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
904 klass->unlock(display);
908 * gst_vaapi_display_sync:
909 * @display: a #GstVaapiDisplay
911 * Flushes any requests queued for the windowing system and waits until
912 * all requests have been handled. This is often used for making sure
913 * that the display is synchronized with the current state of the program.
915 * This is most useful for X11. On windowing systems where requests are
916 * handled synchronously, this function will do nothing.
919 gst_vaapi_display_sync(GstVaapiDisplay *display)
921 GstVaapiDisplayClass *klass;
923 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
925 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
927 klass->sync(display);
928 else if (klass->flush)
929 klass->flush(display);
933 * gst_vaapi_display_flush:
934 * @display: a #GstVaapiDisplay
936 * Flushes any requests queued for the windowing system.
938 * This is most useful for X11. On windowing systems where requests
939 * are handled synchronously, this function will do nothing.
942 gst_vaapi_display_flush(GstVaapiDisplay *display)
944 GstVaapiDisplayClass *klass;
946 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
948 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
950 klass->flush(display);
954 * gst_vaapi_display_get_display:
955 * @display: a #GstVaapiDisplay
957 * Returns the #GstVaapiDisplayType bound to @display.
959 * Return value: the #GstVaapiDisplayType
962 gst_vaapi_display_get_display_type(GstVaapiDisplay *display)
964 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display),
965 GST_VAAPI_DISPLAY_TYPE_ANY);
967 return display->priv->display_type;
971 * gst_vaapi_display_get_display:
972 * @display: a #GstVaapiDisplay
974 * Returns the #VADisplay bound to @display.
976 * Return value: the #VADisplay
979 gst_vaapi_display_get_display(GstVaapiDisplay *display)
981 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
983 return display->priv->display;
987 * gst_vaapi_display_get_width:
988 * @display: a #GstVaapiDisplay
990 * Retrieves the width of a #GstVaapiDisplay.
992 * Return value: the width of the @display, in pixels
995 gst_vaapi_display_get_width(GstVaapiDisplay *display)
997 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
999 return display->priv->width;
1003 * gst_vaapi_display_get_height:
1004 * @display: a #GstVaapiDisplay
1006 * Retrieves the height of a #GstVaapiDisplay
1008 * Return value: the height of the @display, in pixels
1011 gst_vaapi_display_get_height(GstVaapiDisplay *display)
1013 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
1015 return display->priv->height;
1019 * gst_vaapi_display_get_size:
1020 * @display: a #GstVaapiDisplay
1021 * @pwidth: return location for the width, or %NULL
1022 * @pheight: return location for the height, or %NULL
1024 * Retrieves the dimensions of a #GstVaapiDisplay.
1027 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
1029 g_return_if_fail(GST_VAAPI_DISPLAY(display));
1032 *pwidth = display->priv->width;
1035 *pheight = display->priv->height;
1039 * gst_vaapi_display_get_pixel_aspect_ratio:
1040 * @display: a #GstVaapiDisplay
1041 * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
1042 * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
1044 * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
1047 gst_vaapi_display_get_pixel_aspect_ratio(
1048 GstVaapiDisplay *display,
1053 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
1056 *par_n = display->priv->par_n;
1059 *par_d = display->priv->par_d;
1063 * gst_vaapi_display_get_decode_caps:
1064 * @display: a #GstVaapiDisplay
1066 * Gets the supported profiles for decoding as #GstCaps capabilities.
1068 * Return value: a newly allocated #GstCaps object, possibly empty
1071 gst_vaapi_display_get_decode_caps(GstVaapiDisplay *display)
1073 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1075 return get_profile_caps(display->priv->decoders);
1079 * gst_vaapi_display_has_decoder:
1080 * @display: a #GstVaapiDisplay
1081 * @profile: a #VAProfile
1082 * @entrypoint: a #GstVaaiEntrypoint
1084 * Returns whether VA @display supports @profile for decoding at the
1085 * specified @entrypoint.
1087 * Return value: %TRUE if VA @display supports @profile for decoding.
1090 gst_vaapi_display_has_decoder(
1091 GstVaapiDisplay *display,
1092 GstVaapiProfile profile,
1093 GstVaapiEntrypoint entrypoint
1096 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1098 return find_config(display->priv->decoders, profile, entrypoint);
1102 * gst_vaapi_display_get_encode_caps:
1103 * @display: a #GstVaapiDisplay
1105 * Gets the supported profiles for decoding as #GstCaps capabilities.
1107 * Return value: a newly allocated #GstCaps object, possibly empty
1110 gst_vaapi_display_get_encode_caps(GstVaapiDisplay *display)
1112 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1114 return get_profile_caps(display->priv->encoders);
1118 * gst_vaapi_display_has_encoder:
1119 * @display: a #GstVaapiDisplay
1120 * @profile: a #VAProfile
1121 * @entrypoint: a #GstVaapiEntrypoint
1123 * Returns whether VA @display supports @profile for encoding at the
1124 * specified @entrypoint.
1126 * Return value: %TRUE if VA @display supports @profile for encoding.
1129 gst_vaapi_display_has_encoder(
1130 GstVaapiDisplay *display,
1131 GstVaapiProfile profile,
1132 GstVaapiEntrypoint entrypoint
1135 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1137 return find_config(display->priv->encoders, profile, entrypoint);
1141 * gst_vaapi_display_get_image_caps:
1142 * @display: a #GstVaapiDisplay
1144 * Gets the supported image formats for gst_vaapi_surface_get_image()
1145 * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
1147 * Note that this method does not necessarily map image formats
1148 * returned by vaQueryImageFormats(). The set of capabilities can be
1149 * stripped down, if gstreamer-vaapi does not support the format, or
1150 * expanded to cover compatible formats not exposed by the underlying
1151 * driver. e.g. I420 can be supported even if the driver only exposes
1154 * Return value: a newly allocated #GstCaps object, possibly empty
1157 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
1159 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1161 return get_format_caps(display->priv->image_formats);
1165 * gst_vaapi_display_has_image_format:
1166 * @display: a #GstVaapiDisplay
1167 * @format: a #GstVaapiFormat
1169 * Returns whether VA @display supports @format image format.
1171 * Return value: %TRUE if VA @display supports @format image format
1174 gst_vaapi_display_has_image_format(
1175 GstVaapiDisplay *display,
1176 GstVaapiImageFormat format
1179 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1180 g_return_val_if_fail(format, FALSE);
1182 if (find_format(display->priv->image_formats, format))
1185 /* XXX: try subpicture formats since some drivers could report a
1186 * set of VA image formats that is not a superset of the set of VA
1187 * subpicture formats
1189 return find_format(display->priv->subpicture_formats, format);
1193 * gst_vaapi_display_get_subpicture_caps:
1194 * @display: a #GstVaapiDisplay
1196 * Gets the supported subpicture formats as #GstCaps capabilities.
1198 * Note that this method does not necessarily map subpicture formats
1199 * returned by vaQuerySubpictureFormats(). The set of capabilities can
1200 * be stripped down if gstreamer-vaapi does not support the
1201 * format. e.g. this is the case for paletted formats like IA44.
1203 * Return value: a newly allocated #GstCaps object, possibly empty
1206 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
1208 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1210 return get_format_caps(display->priv->subpicture_formats);
1214 * gst_vaapi_display_has_subpicture_format:
1215 * @display: a #GstVaapiDisplay
1216 * @format: a #GstVaapiFormat
1218 * Returns whether VA @display supports @format subpicture format.
1220 * Return value: %TRUE if VA @display supports @format subpicture format
1223 gst_vaapi_display_has_subpicture_format(
1224 GstVaapiDisplay *display,
1225 GstVaapiImageFormat format
1228 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1229 g_return_val_if_fail(format, FALSE);
1231 return find_format(display->priv->subpicture_formats, format);
1235 * gst_vaapi_display_has_property:
1236 * @display: a #GstVaapiDisplay
1237 * @name: the property name to check
1239 * Returns whether VA @display supports the requested property. The
1240 * check is performed against the property @name. So, the client
1241 * application may perform this check only once and cache this
1244 * Return value: %TRUE if VA @display supports property @name
1247 gst_vaapi_display_has_property(GstVaapiDisplay *display, const gchar *name)
1249 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1250 g_return_val_if_fail(name, FALSE);
1252 return find_property(display->priv->properties, name) != NULL;
1256 get_attribute(GstVaapiDisplay *display, VADisplayAttribType type, gint *value)
1258 VADisplayAttribute attr;
1262 attr.flags = VA_DISPLAY_ATTRIB_GETTABLE;
1263 status = vaGetDisplayAttributes(display->priv->display, &attr, 1);
1264 if (!vaapi_check_status(status, "vaGetDisplayAttributes()"))
1266 *value = attr.value;
1271 set_attribute(GstVaapiDisplay *display, VADisplayAttribType type, gint value)
1273 VADisplayAttribute attr;
1278 attr.flags = VA_DISPLAY_ATTRIB_SETTABLE;
1279 status = vaSetDisplayAttributes(display->priv->display, &attr, 1);
1280 if (!vaapi_check_status(status, "vaSetDisplayAttributes()"))
1286 get_render_mode_VADisplayAttribRenderMode(
1287 GstVaapiDisplay *display,
1288 GstVaapiRenderMode *pmode
1291 gint modes, devices;
1293 if (!get_attribute(display, VADisplayAttribRenderDevice, &devices))
1297 if (!get_attribute(display, VADisplayAttribRenderMode, &modes))
1300 /* Favor "overlay" mode since it is the most restrictive one */
1301 if (modes & (VA_RENDER_MODE_LOCAL_OVERLAY|VA_RENDER_MODE_EXTERNAL_OVERLAY))
1302 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1304 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1309 get_render_mode_VADisplayAttribDirectSurface(
1310 GstVaapiDisplay *display,
1311 GstVaapiRenderMode *pmode
1314 #if VA_CHECK_VERSION(0,34,0)
1315 /* VADisplayAttribDirectsurface was removed in VA-API >= 0.34.0 */
1318 gint direct_surface;
1320 if (!get_attribute(display, VADisplayAttribDirectSurface, &direct_surface))
1323 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1325 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1331 get_render_mode_default(
1332 GstVaapiDisplay *display,
1333 GstVaapiRenderMode *pmode
1336 switch (display->priv->display_type) {
1338 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1339 /* wl_buffer mapped from VA surface through vaGetSurfaceBufferWl() */
1340 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1344 case GST_VAAPI_DISPLAY_TYPE_DRM:
1345 /* vaGetSurfaceBufferDRM() returns the underlying DRM buffer handle */
1346 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1350 /* This includes VA/X11 and VA/GLX modes */
1351 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1358 * gst_vaapi_display_get_render_mode:
1359 * @display: a #GstVaapiDisplay
1360 * @pmode: return location for the VA @display rendering mode
1362 * Returns the current VA @display rendering mode.
1364 * Return value: %TRUE if VA @display rendering mode could be determined
1367 gst_vaapi_display_get_render_mode(
1368 GstVaapiDisplay *display,
1369 GstVaapiRenderMode *pmode
1372 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1374 /* Try with render-mode attribute */
1375 if (get_render_mode_VADisplayAttribRenderMode(display, pmode))
1378 /* Try with direct-surface attribute */
1379 if (get_render_mode_VADisplayAttribDirectSurface(display, pmode))
1382 /* Default: determine from the display type */
1383 return get_render_mode_default(display, pmode);
1387 * gst_vaapi_display_set_render_mode:
1388 * @display: a #GstVaapiDisplay
1389 * @mode: the #GstVaapiRenderMode to set
1391 * Sets the VA @display rendering mode to the supplied @mode. This
1392 * function returns %FALSE if the rendering mode could not be set,
1393 * e.g. run-time switching rendering mode is not supported.
1395 * Return value: %TRUE if VA @display rendering @mode could be changed
1396 * to the requested value
1399 gst_vaapi_display_set_render_mode(
1400 GstVaapiDisplay *display,
1401 GstVaapiRenderMode mode
1404 gint modes, devices;
1406 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1408 if (!get_attribute(display, VADisplayAttribRenderDevice, &devices))
1413 case GST_VAAPI_RENDER_MODE_OVERLAY:
1414 if (devices & VA_RENDER_DEVICE_LOCAL)
1415 modes |= VA_RENDER_MODE_LOCAL_OVERLAY;
1416 if (devices & VA_RENDER_DEVICE_EXTERNAL)
1417 modes |= VA_RENDER_MODE_EXTERNAL_OVERLAY;
1419 case GST_VAAPI_RENDER_MODE_TEXTURE:
1420 if (devices & VA_RENDER_DEVICE_LOCAL)
1421 modes |= VA_RENDER_MODE_LOCAL_GPU;
1422 if (devices & VA_RENDER_DEVICE_EXTERNAL)
1423 modes |= VA_RENDER_MODE_EXTERNAL_GPU;
1428 if (!set_attribute(display, VADisplayAttribRenderMode, modes))
1434 * gst_vaapi_display_get_rotation:
1435 * @display: a #GstVaapiDisplay
1437 * Returns the current VA @display rotation angle. If the VA driver
1438 * does not support "rotation" display attribute, then the display is
1439 * assumed to be un-rotated.
1441 * Return value: the current #GstVaapiRotation value
1444 gst_vaapi_display_get_rotation(GstVaapiDisplay *display)
1448 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), GST_VAAPI_ROTATION_0);
1450 if (!get_attribute(display, VADisplayAttribRotation, &value))
1451 value = VA_ROTATION_NONE;
1452 return to_GstVaapiRotation(value);
1456 * gst_vaapi_display_set_rotation:
1457 * @display: a #GstVaapiDisplay
1458 * @rotation: the #GstVaapiRotation value to set
1460 * Sets the VA @display rotation angle to the supplied @rotation
1461 * value. This function returns %FALSE if the rotation angle could not
1462 * be set, e.g. the VA driver does not allow to change the display
1465 * Return value: %TRUE if VA @display rotation angle could be changed
1466 * to the requested value
1469 gst_vaapi_display_set_rotation(
1470 GstVaapiDisplay *display,
1471 GstVaapiRotation rotation
1476 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1478 value = from_GstVaapiRotation(rotation);
1479 if (!set_attribute(display, VADisplayAttribRotation, value))