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"
67 static GstVaapiDisplayCache *g_display_cache = NULL;
69 static inline GstVaapiDisplayCache *
70 get_display_cache(void)
73 g_display_cache = gst_vaapi_display_cache_new();
74 return g_display_cache;
77 GstVaapiDisplayCache *
78 gst_vaapi_display_get_cache(void)
80 return get_display_cache();
84 free_display_cache(void)
88 if (gst_vaapi_display_cache_get_size(g_display_cache) > 0)
90 gst_vaapi_display_cache_free(g_display_cache);
91 g_display_cache = NULL;
94 /* GstVaapiDisplayType enumerations */
96 gst_vaapi_display_type_get_type(void)
98 static GType g_type = 0;
100 static const GEnumValue display_types[] = {
101 { GST_VAAPI_DISPLAY_TYPE_ANY,
102 "Auto detection", "any" },
104 { GST_VAAPI_DISPLAY_TYPE_X11,
105 "VA/X11 display", "x11" },
108 { GST_VAAPI_DISPLAY_TYPE_GLX,
109 "VA/GLX display", "glx" },
112 { GST_VAAPI_DISPLAY_TYPE_WAYLAND,
113 "VA/Wayland display", "wayland" },
116 { GST_VAAPI_DISPLAY_TYPE_DRM,
117 "VA/DRM display", "drm" },
123 g_type = g_enum_register_static("GstVaapiDisplayType", display_types);
127 /* Append GstVaapiImageFormat to formats array */
129 append_format(GArray *formats, GstVaapiImageFormat format)
131 g_array_append_val(formats, format);
134 /* Append VAImageFormats to formats array */
136 append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
138 GstVaapiImageFormat format;
139 gboolean has_YV12 = FALSE;
140 gboolean has_I420 = FALSE;
143 for (i = 0; i < n; i++) {
144 const VAImageFormat * const va_format = &va_formats[i];
146 format = gst_vaapi_image_format(va_format);
148 GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
149 GST_FOURCC_ARGS(va_format->fourcc));
154 case GST_VAAPI_IMAGE_YV12:
157 case GST_VAAPI_IMAGE_I420:
163 append_format(formats, format);
166 /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
167 supported by the underlying driver */
168 if (has_YV12 && !has_I420)
169 append_format(formats, GST_VAAPI_IMAGE_I420);
170 else if (has_I420 && !has_YV12)
171 append_format(formats, GST_VAAPI_IMAGE_YV12);
174 /* Sort image formats. Prefer YUV formats first */
176 compare_yuv_formats(gconstpointer a, gconstpointer b)
178 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
179 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
181 const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
182 const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);
184 if (is_fmt1_yuv != is_fmt2_yuv)
185 return is_fmt1_yuv ? -1 : 1;
187 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
188 (gint)gst_vaapi_image_format_get_score(fmt2));
191 /* Sort subpicture formats. Prefer RGB formats first */
193 compare_rgb_formats(gconstpointer a, gconstpointer b)
195 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
196 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
198 const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
199 const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);
201 if (is_fmt1_rgb != is_fmt2_rgb)
202 return is_fmt1_rgb ? -1 : 1;
204 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
205 (gint)gst_vaapi_image_format_get_score(fmt2));
208 /* Check if configs array contains profile at entrypoint */
209 static inline gboolean
212 GstVaapiProfile profile,
213 GstVaapiEntrypoint entrypoint
216 GstVaapiConfig *config;
222 for (i = 0; i < configs->len; i++) {
223 config = &g_array_index(configs, GstVaapiConfig, i);
224 if (config->profile == profile && config->entrypoint == entrypoint)
230 /* HACK: append H.263 Baseline profile if MPEG-4:2 Simple profile is supported */
232 append_h263_config(GArray *configs)
234 GstVaapiConfig *config, tmp_config;
235 GstVaapiConfig *mpeg4_simple_config = NULL;
236 GstVaapiConfig *h263_baseline_config = NULL;
239 if (!WORKAROUND_H263_BASELINE_DECODE_PROFILE)
245 for (i = 0; i < configs->len; i++) {
246 config = &g_array_index(configs, GstVaapiConfig, i);
247 if (config->profile == GST_VAAPI_PROFILE_MPEG4_SIMPLE)
248 mpeg4_simple_config = config;
249 else if (config->profile == GST_VAAPI_PROFILE_H263_BASELINE)
250 h263_baseline_config = config;
253 if (mpeg4_simple_config && !h263_baseline_config) {
254 tmp_config = *mpeg4_simple_config;
255 tmp_config.profile = GST_VAAPI_PROFILE_H263_BASELINE;
256 g_array_append_val(configs, tmp_config);
260 /* Convert configs array to profiles as GstCaps */
262 get_profile_caps(GArray *configs)
264 GstVaapiConfig *config;
265 GstCaps *out_caps, *caps;
271 out_caps = gst_caps_new_empty();
275 for (i = 0; i < configs->len; i++) {
276 config = &g_array_index(configs, GstVaapiConfig, i);
277 caps = gst_vaapi_profile_get_caps(config->profile);
279 gst_caps_merge(out_caps, caps);
284 /* Check if formats array contains format */
285 static inline gboolean
286 find_format(GArray *formats, GstVaapiImageFormat format)
290 for (i = 0; i < formats->len; i++)
291 if (g_array_index(formats, GstVaapiImageFormat, i) == format)
296 /* Convert formats array to GstCaps */
298 get_format_caps(GArray *formats)
300 GstVaapiImageFormat format;
301 GstCaps *out_caps, *caps;
304 out_caps = gst_caps_new_empty();
308 for (i = 0; i < formats->len; i++) {
309 format = g_array_index(formats, GstVaapiImageFormat, i);
310 caps = gst_vaapi_image_format_get_caps(format);
312 gst_caps_append(out_caps, caps);
317 /* Find display attribute */
318 static const GstVaapiProperty *
319 find_property(GArray *properties, const gchar *name)
321 GstVaapiProperty *prop;
327 for (i = 0; i < properties->len; i++) {
328 prop = &g_array_index(properties, GstVaapiProperty, i);
329 if (strcmp(prop->name, name) == 0)
336 static const GstVaapiProperty *
337 find_property_by_type(GArray *properties, VADisplayAttribType type)
339 GstVaapiProperty *prop;
342 for (i = 0; i < properties->len; i++) {
343 prop = &g_array_index(properties, GstVaapiProperty, i);
344 if (prop->attribute.type == type)
352 gst_vaapi_display_calculate_pixel_aspect_ratio(GstVaapiDisplay *display)
354 GstVaapiDisplayPrivate * const priv = display->priv;
355 gdouble ratio, delta;
358 static const gint par[][2] = {
359 {1, 1}, /* regular screen */
360 {16, 15}, /* PAL TV */
361 {11, 10}, /* 525 line Rec.601 video */
362 {54, 59}, /* 625 line Rec.601 video */
363 {64, 45}, /* 1280x1024 on 16:9 display */
364 {5, 3}, /* 1280x1024 on 4:3 display */
365 {4, 3} /* 800x600 on 16:9 display */
368 /* First, calculate the "real" ratio based on the X values;
369 * which is the "physical" w/h divided by the w/h in pixels of the
371 if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
374 ratio = (gdouble)(priv->width_mm * priv->height) /
375 (priv->height_mm * priv->width);
376 GST_DEBUG("calculated pixel aspect ratio: %f", ratio);
378 /* Now, find the one from par[][2] with the lowest delta to the real one */
379 #define DELTA(idx) (ABS(ratio - ((gdouble)par[idx][0] / par[idx][1])))
383 for (i = 1; i < G_N_ELEMENTS(par); i++) {
384 const gdouble this_delta = DELTA(i);
385 if (this_delta < delta) {
392 priv->par_n = par[index][0];
393 priv->par_d = par[index][1];
397 gst_vaapi_display_destroy(GstVaapiDisplay *display)
399 GstVaapiDisplayPrivate * const priv = display->priv;
401 if (priv->decoders) {
402 g_array_free(priv->decoders, TRUE);
403 priv->decoders = NULL;
406 if (priv->encoders) {
407 g_array_free(priv->encoders, TRUE);
408 priv->encoders = NULL;
411 if (priv->image_formats) {
412 g_array_free(priv->image_formats, TRUE);
413 priv->image_formats = NULL;
416 if (priv->subpicture_formats) {
417 g_array_free(priv->subpicture_formats, TRUE);
418 priv->subpicture_formats = NULL;
421 if (priv->properties) {
422 g_array_free(priv->properties, TRUE);
423 priv->properties = NULL;
428 vaTerminate(priv->display);
429 priv->display = NULL;
432 if (priv->create_display) {
433 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
434 if (klass->close_display)
435 klass->close_display(display);
438 g_clear_object(&priv->parent);
440 if (g_display_cache) {
441 gst_vaapi_display_cache_remove(get_display_cache(), display);
442 free_display_cache();
447 gst_vaapi_display_create(GstVaapiDisplay *display)
449 GstVaapiDisplayPrivate * const priv = display->priv;
450 GstVaapiDisplayCache *cache;
451 gboolean has_errors = TRUE;
452 VADisplayAttribute *display_attrs = NULL;
453 VAProfile *profiles = NULL;
454 VAEntrypoint *entrypoints = NULL;
455 VAImageFormat *formats = NULL;
456 unsigned int *flags = NULL;
457 gint i, j, n, num_entrypoints, major_version, minor_version;
459 GstVaapiDisplayInfo info;
460 const GstVaapiDisplayInfo *cached_info = NULL;
462 memset(&info, 0, sizeof(info));
463 info.display = display;
464 info.display_type = priv->display_type;
467 info.va_display = priv->display;
468 else if (priv->create_display) {
469 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
470 if (klass->open_display && !klass->open_display(display))
472 if (!klass->get_display || !klass->get_display(display, &info))
474 priv->display = info.va_display;
475 priv->display_type = info.display_type;
477 klass->get_size(display, &priv->width, &priv->height);
478 if (klass->get_size_mm)
479 klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
480 gst_vaapi_display_calculate_pixel_aspect_ratio(display);
485 cache = get_display_cache();
488 cached_info = gst_vaapi_display_cache_lookup_by_va_display(
493 g_clear_object(&priv->parent);
494 priv->parent = g_object_ref(cached_info->display);
495 priv->display_type = cached_info->display_type;
499 status = vaInitialize(priv->display, &major_version, &minor_version);
500 if (!vaapi_check_status(status, "vaInitialize()"))
502 GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
506 profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
509 entrypoints = g_new(VAEntrypoint, vaMaxNumEntrypoints(priv->display));
512 status = vaQueryConfigProfiles(priv->display, profiles, &n);
513 if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
516 GST_DEBUG("%d profiles", n);
517 for (i = 0; i < n; i++) {
518 #if VA_CHECK_VERSION(0,34,0)
519 /* Introduced in VA/VPP API */
520 if (profiles[i] == VAProfileNone)
523 GST_DEBUG(" %s", string_of_VAProfile(profiles[i]));
526 priv->decoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
529 priv->encoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
533 for (i = 0; i < n; i++) {
534 GstVaapiConfig config;
536 config.profile = gst_vaapi_profile(profiles[i]);
540 status = vaQueryConfigEntrypoints(
543 entrypoints, &num_entrypoints
545 if (!vaapi_check_status(status, "vaQueryConfigEntrypoints()"))
548 for (j = 0; j < num_entrypoints; j++) {
549 config.entrypoint = gst_vaapi_entrypoint(entrypoints[j]);
550 switch (config.entrypoint) {
551 case GST_VAAPI_ENTRYPOINT_VLD:
552 case GST_VAAPI_ENTRYPOINT_IDCT:
553 case GST_VAAPI_ENTRYPOINT_MOCO:
554 g_array_append_val(priv->decoders, config);
556 case GST_VAAPI_ENTRYPOINT_SLICE_ENCODE:
557 g_array_append_val(priv->encoders, config);
562 append_h263_config(priv->decoders);
564 /* VA display attributes */
566 g_new(VADisplayAttribute, vaMaxNumDisplayAttributes(priv->display));
570 n = 0; /* XXX: workaround old GMA500 bug */
571 status = vaQueryDisplayAttributes(priv->display, display_attrs, &n);
572 if (!vaapi_check_status(status, "vaQueryDisplayAttributes()"))
575 priv->properties = g_array_new(FALSE, FALSE, sizeof(GstVaapiProperty));
576 if (!priv->properties)
579 GST_DEBUG("%d display attributes", n);
580 for (i = 0; i < n; i++) {
581 VADisplayAttribute * const attr = &display_attrs[i];
582 GstVaapiProperty prop;
584 GST_DEBUG(" %s", string_of_VADisplayAttributeType(attr->type));
586 switch (attr->type) {
587 #if !VA_CHECK_VERSION(0,34,0)
588 case VADisplayAttribDirectSurface:
589 prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
592 case VADisplayAttribRenderMode:
593 prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
595 case VADisplayAttribRotation:
596 prop.name = GST_VAAPI_DISPLAY_PROP_ROTATION;
604 prop.attribute = *attr;
605 g_array_append_val(priv->properties, prop);
608 /* VA image formats */
609 formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
612 status = vaQueryImageFormats(priv->display, formats, &n);
613 if (!vaapi_check_status(status, "vaQueryImageFormats()"))
616 GST_DEBUG("%d image formats", n);
617 for (i = 0; i < n; i++)
618 GST_DEBUG(" %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
620 priv->image_formats =
621 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
622 if (!priv->image_formats)
624 append_formats(priv->image_formats, formats, n);
625 g_array_sort(priv->image_formats, compare_yuv_formats);
627 /* VA subpicture formats */
628 n = vaMaxNumSubpictureFormats(priv->display);
629 formats = g_renew(VAImageFormat, formats, n);
630 flags = g_new(guint, n);
631 if (!formats || !flags)
633 status = vaQuerySubpictureFormats(priv->display, formats, flags, (guint *)&n);
634 if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
637 GST_DEBUG("%d subpicture formats", n);
638 for (i = 0; i < n; i++)
639 GST_DEBUG(" %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
641 priv->subpicture_formats =
642 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
643 if (!priv->subpicture_formats)
645 append_formats(priv->subpicture_formats, formats, n);
646 g_array_sort(priv->subpicture_formats, compare_rgb_formats);
649 if (!gst_vaapi_display_cache_add(cache, &info))
655 g_free(display_attrs);
664 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
666 GstVaapiDisplayPrivate *priv = display->priv;
669 priv = priv->parent->priv;
670 g_static_rec_mutex_lock(&priv->mutex);
674 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
676 GstVaapiDisplayPrivate *priv = display->priv;
679 priv = priv->parent->priv;
680 g_static_rec_mutex_unlock(&priv->mutex);
684 gst_vaapi_display_finalize(GObject *object)
686 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
688 gst_vaapi_display_destroy(display);
690 g_static_rec_mutex_free(&display->priv->mutex);
692 G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
696 gst_vaapi_display_set_property(
703 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
707 display->priv->display = g_value_get_pointer(value);
709 case PROP_DISPLAY_TYPE:
710 display->priv->display_type = g_value_get_enum(value);
713 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
719 gst_vaapi_display_get_property(
726 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
730 g_value_set_pointer(value, gst_vaapi_display_get_display(display));
732 case PROP_DISPLAY_TYPE:
733 g_value_set_enum(value, gst_vaapi_display_get_display_type(display));
736 g_value_set_uint(value, gst_vaapi_display_get_width(display));
739 g_value_set_uint(value, gst_vaapi_display_get_height(display));
742 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
748 gst_vaapi_display_constructed(GObject *object)
750 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
751 GObjectClass *parent_class;
753 display->priv->create_display = display->priv->display == NULL;
754 if (!gst_vaapi_display_create(display))
755 gst_vaapi_display_destroy(display);
757 parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
758 if (parent_class->constructed)
759 parent_class->constructed(object);
763 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
765 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
766 GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
768 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
770 g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
772 object_class->finalize = gst_vaapi_display_finalize;
773 object_class->set_property = gst_vaapi_display_set_property;
774 object_class->get_property = gst_vaapi_display_get_property;
775 object_class->constructed = gst_vaapi_display_constructed;
777 dpy_class->lock = gst_vaapi_display_lock_default;
778 dpy_class->unlock = gst_vaapi_display_unlock_default;
780 g_object_class_install_property
783 g_param_spec_pointer("display",
786 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
788 g_object_class_install_property
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_object_class_install_property
801 g_param_spec_uint("width",
807 g_object_class_install_property
810 g_param_spec_uint("height",
812 "The display height",
818 gst_vaapi_display_init(GstVaapiDisplay *display)
820 GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
822 display->priv = priv;
824 priv->display_type = GST_VAAPI_DISPLAY_TYPE_ANY;
825 priv->display = NULL;
832 priv->decoders = NULL;
833 priv->encoders = NULL;
834 priv->image_formats = NULL;
835 priv->subpicture_formats = NULL;
836 priv->properties = NULL;
837 priv->create_display = TRUE;
839 g_static_rec_mutex_init(&priv->mutex);
843 * gst_vaapi_display_new_with_display:
844 * @va_display: a #VADisplay
846 * Creates a new #GstVaapiDisplay, using @va_display as the VA
849 * Return value: the newly created #GstVaapiDisplay object
852 gst_vaapi_display_new_with_display(VADisplay va_display)
854 GstVaapiDisplayCache * const cache = get_display_cache();
855 const GstVaapiDisplayInfo *info;
857 g_return_val_if_fail(va_display != NULL, NULL);
858 g_return_val_if_fail(cache != NULL, NULL);
860 info = gst_vaapi_display_cache_lookup_by_va_display(cache, va_display);
862 return g_object_ref(info->display);
864 return g_object_new(GST_VAAPI_TYPE_DISPLAY,
865 "display", va_display,
870 * gst_vaapi_display_lock:
871 * @display: a #GstVaapiDisplay
873 * Locks @display. If @display is already locked by another thread,
874 * the current thread will block until @display is unlocked by the
878 gst_vaapi_display_lock(GstVaapiDisplay *display)
880 GstVaapiDisplayClass *klass;
882 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
884 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
886 klass->lock(display);
890 * gst_vaapi_display_unlock:
891 * @display: a #GstVaapiDisplay
893 * Unlocks @display. If another thread is blocked in a
894 * gst_vaapi_display_lock() call for @display, it will be woken and
895 * can lock @display itself.
898 gst_vaapi_display_unlock(GstVaapiDisplay *display)
900 GstVaapiDisplayClass *klass;
902 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
904 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
906 klass->unlock(display);
910 * gst_vaapi_display_sync:
911 * @display: a #GstVaapiDisplay
913 * Flushes any requests queued for the windowing system and waits until
914 * all requests have been handled. This is often used for making sure
915 * that the display is synchronized with the current state of the program.
917 * This is most useful for X11. On windowing systems where requests are
918 * handled synchronously, this function will do nothing.
921 gst_vaapi_display_sync(GstVaapiDisplay *display)
923 GstVaapiDisplayClass *klass;
925 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
927 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
929 klass->sync(display);
930 else if (klass->flush)
931 klass->flush(display);
935 * gst_vaapi_display_flush:
936 * @display: a #GstVaapiDisplay
938 * Flushes any requests queued for the windowing system.
940 * This is most useful for X11. On windowing systems where requests
941 * are handled synchronously, this function will do nothing.
944 gst_vaapi_display_flush(GstVaapiDisplay *display)
946 GstVaapiDisplayClass *klass;
948 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
950 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
952 klass->flush(display);
956 * gst_vaapi_display_get_display:
957 * @display: a #GstVaapiDisplay
959 * Returns the #GstVaapiDisplayType bound to @display.
961 * Return value: the #GstVaapiDisplayType
964 gst_vaapi_display_get_display_type(GstVaapiDisplay *display)
966 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display),
967 GST_VAAPI_DISPLAY_TYPE_ANY);
969 return display->priv->display_type;
973 * gst_vaapi_display_get_display:
974 * @display: a #GstVaapiDisplay
976 * Returns the #VADisplay bound to @display.
978 * Return value: the #VADisplay
981 gst_vaapi_display_get_display(GstVaapiDisplay *display)
983 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
985 return display->priv->display;
989 * gst_vaapi_display_get_width:
990 * @display: a #GstVaapiDisplay
992 * Retrieves the width of a #GstVaapiDisplay.
994 * Return value: the width of the @display, in pixels
997 gst_vaapi_display_get_width(GstVaapiDisplay *display)
999 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
1001 return display->priv->width;
1005 * gst_vaapi_display_get_height:
1006 * @display: a #GstVaapiDisplay
1008 * Retrieves the height of a #GstVaapiDisplay
1010 * Return value: the height of the @display, in pixels
1013 gst_vaapi_display_get_height(GstVaapiDisplay *display)
1015 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
1017 return display->priv->height;
1021 * gst_vaapi_display_get_size:
1022 * @display: a #GstVaapiDisplay
1023 * @pwidth: return location for the width, or %NULL
1024 * @pheight: return location for the height, or %NULL
1026 * Retrieves the dimensions of a #GstVaapiDisplay.
1029 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
1031 g_return_if_fail(GST_VAAPI_DISPLAY(display));
1034 *pwidth = display->priv->width;
1037 *pheight = display->priv->height;
1041 * gst_vaapi_display_get_pixel_aspect_ratio:
1042 * @display: a #GstVaapiDisplay
1043 * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
1044 * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
1046 * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
1049 gst_vaapi_display_get_pixel_aspect_ratio(
1050 GstVaapiDisplay *display,
1055 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
1058 *par_n = display->priv->par_n;
1061 *par_d = display->priv->par_d;
1065 * gst_vaapi_display_get_decode_caps:
1066 * @display: a #GstVaapiDisplay
1068 * Gets the supported profiles for decoding as #GstCaps capabilities.
1070 * Return value: a newly allocated #GstCaps object, possibly empty
1073 gst_vaapi_display_get_decode_caps(GstVaapiDisplay *display)
1075 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1077 return get_profile_caps(display->priv->decoders);
1081 * gst_vaapi_display_has_decoder:
1082 * @display: a #GstVaapiDisplay
1083 * @profile: a #VAProfile
1084 * @entrypoint: a #GstVaaiEntrypoint
1086 * Returns whether VA @display supports @profile for decoding at the
1087 * specified @entrypoint.
1089 * Return value: %TRUE if VA @display supports @profile for decoding.
1092 gst_vaapi_display_has_decoder(
1093 GstVaapiDisplay *display,
1094 GstVaapiProfile profile,
1095 GstVaapiEntrypoint entrypoint
1098 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1100 return find_config(display->priv->decoders, profile, entrypoint);
1104 * gst_vaapi_display_get_encode_caps:
1105 * @display: a #GstVaapiDisplay
1107 * Gets the supported profiles for decoding as #GstCaps capabilities.
1109 * Return value: a newly allocated #GstCaps object, possibly empty
1112 gst_vaapi_display_get_encode_caps(GstVaapiDisplay *display)
1114 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1116 return get_profile_caps(display->priv->encoders);
1120 * gst_vaapi_display_has_encoder:
1121 * @display: a #GstVaapiDisplay
1122 * @profile: a #VAProfile
1123 * @entrypoint: a #GstVaapiEntrypoint
1125 * Returns whether VA @display supports @profile for encoding at the
1126 * specified @entrypoint.
1128 * Return value: %TRUE if VA @display supports @profile for encoding.
1131 gst_vaapi_display_has_encoder(
1132 GstVaapiDisplay *display,
1133 GstVaapiProfile profile,
1134 GstVaapiEntrypoint entrypoint
1137 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1139 return find_config(display->priv->encoders, profile, entrypoint);
1143 * gst_vaapi_display_get_image_caps:
1144 * @display: a #GstVaapiDisplay
1146 * Gets the supported image formats for gst_vaapi_surface_get_image()
1147 * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
1149 * Note that this method does not necessarily map image formats
1150 * returned by vaQueryImageFormats(). The set of capabilities can be
1151 * stripped down, if gstreamer-vaapi does not support the format, or
1152 * expanded to cover compatible formats not exposed by the underlying
1153 * driver. e.g. I420 can be supported even if the driver only exposes
1156 * Return value: a newly allocated #GstCaps object, possibly empty
1159 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
1161 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1163 return get_format_caps(display->priv->image_formats);
1167 * gst_vaapi_display_has_image_format:
1168 * @display: a #GstVaapiDisplay
1169 * @format: a #GstVaapiFormat
1171 * Returns whether VA @display supports @format image format.
1173 * Return value: %TRUE if VA @display supports @format image format
1176 gst_vaapi_display_has_image_format(
1177 GstVaapiDisplay *display,
1178 GstVaapiImageFormat format
1181 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1182 g_return_val_if_fail(format, FALSE);
1184 if (find_format(display->priv->image_formats, format))
1187 /* XXX: try subpicture formats since some drivers could report a
1188 * set of VA image formats that is not a superset of the set of VA
1189 * subpicture formats
1191 return find_format(display->priv->subpicture_formats, format);
1195 * gst_vaapi_display_get_subpicture_caps:
1196 * @display: a #GstVaapiDisplay
1198 * Gets the supported subpicture formats as #GstCaps capabilities.
1200 * Note that this method does not necessarily map subpicture formats
1201 * returned by vaQuerySubpictureFormats(). The set of capabilities can
1202 * be stripped down if gstreamer-vaapi does not support the
1203 * format. e.g. this is the case for paletted formats like IA44.
1205 * Return value: a newly allocated #GstCaps object, possibly empty
1208 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
1210 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1212 return get_format_caps(display->priv->subpicture_formats);
1216 * gst_vaapi_display_has_subpicture_format:
1217 * @display: a #GstVaapiDisplay
1218 * @format: a #GstVaapiFormat
1220 * Returns whether VA @display supports @format subpicture format.
1222 * Return value: %TRUE if VA @display supports @format subpicture format
1225 gst_vaapi_display_has_subpicture_format(
1226 GstVaapiDisplay *display,
1227 GstVaapiImageFormat format
1230 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1231 g_return_val_if_fail(format, FALSE);
1233 return find_format(display->priv->subpicture_formats, format);
1237 * gst_vaapi_display_has_property:
1238 * @display: a #GstVaapiDisplay
1239 * @name: the property name to check
1241 * Returns whether VA @display supports the requested property. The
1242 * check is performed against the property @name. So, the client
1243 * application may perform this check only once and cache this
1246 * Return value: %TRUE if VA @display supports property @name
1249 gst_vaapi_display_has_property(GstVaapiDisplay *display, const gchar *name)
1251 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1252 g_return_val_if_fail(name, FALSE);
1254 return find_property(display->priv->properties, name) != NULL;
1258 get_attribute(GstVaapiDisplay *display, VADisplayAttribType type, gint *value)
1260 VADisplayAttribute attr;
1264 attr.flags = VA_DISPLAY_ATTRIB_GETTABLE;
1265 status = vaGetDisplayAttributes(display->priv->display, &attr, 1);
1266 if (!vaapi_check_status(status, "vaGetDisplayAttributes()"))
1268 *value = attr.value;
1273 set_attribute(GstVaapiDisplay *display, VADisplayAttribType type, gint value)
1275 VADisplayAttribute attr;
1280 attr.flags = VA_DISPLAY_ATTRIB_SETTABLE;
1281 status = vaSetDisplayAttributes(display->priv->display, &attr, 1);
1282 if (!vaapi_check_status(status, "vaSetDisplayAttributes()"))
1288 get_render_mode_VADisplayAttribRenderMode(
1289 GstVaapiDisplay *display,
1290 GstVaapiRenderMode *pmode
1293 gint modes, devices;
1295 if (!get_attribute(display, VADisplayAttribRenderDevice, &devices))
1299 if (!get_attribute(display, VADisplayAttribRenderMode, &modes))
1302 /* Favor "overlay" mode since it is the most restrictive one */
1303 if (modes & (VA_RENDER_MODE_LOCAL_OVERLAY|VA_RENDER_MODE_EXTERNAL_OVERLAY))
1304 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1306 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1311 get_render_mode_VADisplayAttribDirectSurface(
1312 GstVaapiDisplay *display,
1313 GstVaapiRenderMode *pmode
1316 #if VA_CHECK_VERSION(0,34,0)
1317 /* VADisplayAttribDirectsurface was removed in VA-API >= 0.34.0 */
1320 gint direct_surface;
1322 if (!get_attribute(display, VADisplayAttribDirectSurface, &direct_surface))
1325 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1327 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1333 get_render_mode_default(
1334 GstVaapiDisplay *display,
1335 GstVaapiRenderMode *pmode
1338 switch (display->priv->display_type) {
1340 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1341 /* wl_buffer mapped from VA surface through vaGetSurfaceBufferWl() */
1342 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1346 case GST_VAAPI_DISPLAY_TYPE_DRM:
1347 /* vaGetSurfaceBufferDRM() returns the underlying DRM buffer handle */
1348 *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1352 /* This includes VA/X11 and VA/GLX modes */
1353 *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1360 * gst_vaapi_display_get_render_mode:
1361 * @display: a #GstVaapiDisplay
1362 * @pmode: return location for the VA @display rendering mode
1364 * Returns the current VA @display rendering mode.
1366 * Return value: %TRUE if VA @display rendering mode could be determined
1369 gst_vaapi_display_get_render_mode(
1370 GstVaapiDisplay *display,
1371 GstVaapiRenderMode *pmode
1374 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1376 /* Try with render-mode attribute */
1377 if (get_render_mode_VADisplayAttribRenderMode(display, pmode))
1380 /* Try with direct-surface attribute */
1381 if (get_render_mode_VADisplayAttribDirectSurface(display, pmode))
1384 /* Default: determine from the display type */
1385 return get_render_mode_default(display, pmode);
1389 * gst_vaapi_display_set_render_mode:
1390 * @display: a #GstVaapiDisplay
1391 * @mode: the #GstVaapiRenderMode to set
1393 * Sets the VA @display rendering mode to the supplied @mode. This
1394 * function returns %FALSE if the rendering mode could not be set,
1395 * e.g. run-time switching rendering mode is not supported.
1397 * Return value: %TRUE if VA @display rendering @mode could be changed
1398 * to the requested value
1401 gst_vaapi_display_set_render_mode(
1402 GstVaapiDisplay *display,
1403 GstVaapiRenderMode mode
1406 gint modes, devices;
1408 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1410 if (!get_attribute(display, VADisplayAttribRenderDevice, &devices))
1415 case GST_VAAPI_RENDER_MODE_OVERLAY:
1416 if (devices & VA_RENDER_DEVICE_LOCAL)
1417 modes |= VA_RENDER_MODE_LOCAL_OVERLAY;
1418 if (devices & VA_RENDER_DEVICE_EXTERNAL)
1419 modes |= VA_RENDER_MODE_EXTERNAL_OVERLAY;
1421 case GST_VAAPI_RENDER_MODE_TEXTURE:
1422 if (devices & VA_RENDER_DEVICE_LOCAL)
1423 modes |= VA_RENDER_MODE_LOCAL_GPU;
1424 if (devices & VA_RENDER_DEVICE_EXTERNAL)
1425 modes |= VA_RENDER_MODE_EXTERNAL_GPU;
1430 if (!set_attribute(display, VADisplayAttribRenderMode, modes))
1436 * gst_vaapi_display_get_rotation:
1437 * @display: a #GstVaapiDisplay
1439 * Returns the current VA @display rotation angle. If the VA driver
1440 * does not support "rotation" display attribute, then the display is
1441 * assumed to be un-rotated.
1443 * Return value: the current #GstVaapiRotation value
1446 gst_vaapi_display_get_rotation(GstVaapiDisplay *display)
1450 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), GST_VAAPI_ROTATION_0);
1452 if (!get_attribute(display, VADisplayAttribRotation, &value))
1453 value = VA_ROTATION_NONE;
1454 return to_GstVaapiRotation(value);
1458 * gst_vaapi_display_set_rotation:
1459 * @display: a #GstVaapiDisplay
1460 * @rotation: the #GstVaapiRotation value to set
1462 * Sets the VA @display rotation angle to the supplied @rotation
1463 * value. This function returns %FALSE if the rotation angle could not
1464 * be set, e.g. the VA driver does not allow to change the display
1467 * Return value: %TRUE if VA @display rotation angle could be changed
1468 * to the requested value
1471 gst_vaapi_display_set_rotation(
1472 GstVaapiDisplay *display,
1473 GstVaapiRotation rotation
1478 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1480 value = from_GstVaapiRotation(rotation);
1481 if (!set_attribute(display, VADisplayAttribRotation, value))