2 * gstvaapidisplay.c - VA display abstraction
4 * gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 * SECTION:gstvaapidisplay
23 * @short_description: VA display abstraction
27 #include "gstvaapiutils.h"
28 #include "gstvaapidisplay.h"
31 #include "gstvaapidebug.h"
33 GST_DEBUG_CATEGORY(gst_debug_vaapi);
35 G_DEFINE_TYPE(GstVaapiDisplay, gst_vaapi_display, G_TYPE_OBJECT);
37 #define GST_VAAPI_DISPLAY_GET_PRIVATE(obj) \
38 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
39 GST_VAAPI_TYPE_DISPLAY, \
40 GstVaapiDisplayPrivate))
42 struct _GstVaapiDisplayPrivate {
51 gboolean create_display;
53 GArray *image_formats;
54 GArray *subpicture_formats;
65 /* Append GstVaapiImageFormat to formats array */
67 append_format(GArray *formats, GstVaapiImageFormat format)
69 g_array_append_val(formats, format);
72 /* Append VAImageFormats to formats array */
74 append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
76 GstVaapiImageFormat format;
77 gboolean has_YV12 = FALSE;
78 gboolean has_I420 = FALSE;
81 for (i = 0; i < n; i++) {
82 const VAImageFormat * const va_format = &va_formats[i];
84 format = gst_vaapi_image_format(va_format);
86 GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
87 GST_FOURCC_ARGS(va_format->fourcc));
92 case GST_VAAPI_IMAGE_YV12:
95 case GST_VAAPI_IMAGE_I420:
101 append_format(formats, format);
104 /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
105 supported by the underlying driver */
106 if (has_YV12 && !has_I420)
107 append_format(formats, GST_VAAPI_IMAGE_I420);
108 else if (has_I420 && !has_YV12)
109 append_format(formats, GST_VAAPI_IMAGE_YV12);
112 /* Sort image formats. Prefer YUV formats first */
114 compare_yuv_formats(gconstpointer a, gconstpointer b)
116 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
117 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
119 const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
120 const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);
122 if (is_fmt1_yuv != is_fmt2_yuv)
123 return is_fmt1_yuv ? -1 : 1;
125 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
126 (gint)gst_vaapi_image_format_get_score(fmt2));
129 /* Sort subpicture formats. Prefer RGB formats first */
131 compare_rgb_formats(gconstpointer a, gconstpointer b)
133 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
134 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
136 const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
137 const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);
139 if (is_fmt1_rgb != is_fmt2_rgb)
140 return is_fmt1_rgb ? -1 : 1;
142 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
143 (gint)gst_vaapi_image_format_get_score(fmt2));
146 /* Check if profiles array contains profile */
147 static inline gboolean
148 find_profile(GArray *profiles, VAProfile profile)
152 for (i = 0; i < profiles->len; i++)
153 if (g_array_index(profiles, VAProfile, i) == profile)
158 /* Check if formats array contains format */
159 static inline gboolean
160 find_format(GArray *formats, GstVaapiImageFormat format)
164 for (i = 0; i < formats->len; i++)
165 if (g_array_index(formats, GstVaapiImageFormat, i) == format)
170 /* Convert formats array to GstCaps */
172 get_caps(GArray *formats)
174 GstVaapiImageFormat format;
175 GstCaps *out_caps, *caps;
178 out_caps = gst_caps_new_empty();
182 for (i = 0; i < formats->len; i++) {
183 format = g_array_index(formats, GstVaapiImageFormat, i);
184 caps = gst_vaapi_image_format_get_caps(format);
186 gst_caps_append(out_caps, caps);
192 gst_vaapi_display_calculate_pixel_aspect_ratio(GstVaapiDisplay *display)
194 GstVaapiDisplayPrivate * const priv = display->priv;
195 gdouble ratio, delta;
198 static const gint par[][2] = {
199 {1, 1}, /* regular screen */
200 {16, 15}, /* PAL TV */
201 {11, 10}, /* 525 line Rec.601 video */
202 {54, 59}, /* 625 line Rec.601 video */
203 {64, 45}, /* 1280x1024 on 16:9 display */
204 {5, 3}, /* 1280x1024 on 4:3 display */
205 {4, 3} /* 800x600 on 16:9 display */
208 /* First, calculate the "real" ratio based on the X values;
209 * which is the "physical" w/h divided by the w/h in pixels of the
211 if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
214 ratio = (gdouble)(priv->width_mm * priv->height) /
215 (priv->height_mm * priv->width);
216 GST_DEBUG("calculated pixel aspect ratio: %f", ratio);
218 /* Now, find the one from par[][2] with the lowest delta to the real one */
219 #define DELTA(idx) (ABS(ratio - ((gdouble)par[idx][0] / par[idx][1])))
223 for (i = 1; i < G_N_ELEMENTS(par); i++) {
224 const gdouble this_delta = DELTA(i);
225 if (this_delta < delta) {
232 priv->par_n = par[index][0];
233 priv->par_d = par[index][1];
237 gst_vaapi_display_destroy(GstVaapiDisplay *display)
239 GstVaapiDisplayPrivate * const priv = display->priv;
241 if (priv->profiles) {
242 g_array_free(priv->profiles, TRUE);
243 priv->profiles = NULL;
246 if (priv->image_formats) {
247 g_array_free(priv->image_formats, TRUE);
248 priv->image_formats = NULL;
251 if (priv->subpicture_formats) {
252 g_array_free(priv->subpicture_formats, TRUE);
253 priv->subpicture_formats = NULL;
257 vaTerminate(priv->display);
258 priv->display = NULL;
261 if (priv->create_display) {
262 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
263 if (klass->close_display)
264 klass->close_display(display);
269 gst_vaapi_display_create(GstVaapiDisplay *display)
271 GstVaapiDisplayPrivate * const priv = display->priv;
272 gboolean has_errors = TRUE;
273 VAProfile *profiles = NULL;
274 VAImageFormat *formats = NULL;
275 unsigned int *flags = NULL;
276 gint i, n, major_version, minor_version;
279 if (!priv->display && priv->create_display) {
280 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
281 if (klass->open_display && !klass->open_display(display))
283 if (klass->get_display) {
284 priv->display = klass->get_display(display);
289 klass->get_size(display, &priv->width, &priv->height);
290 if (klass->get_size_mm)
291 klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
292 gst_vaapi_display_calculate_pixel_aspect_ratio(display);
297 status = vaInitialize(priv->display, &major_version, &minor_version);
298 if (!vaapi_check_status(status, "vaInitialize()"))
300 GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
303 profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
306 status = vaQueryConfigProfiles(priv->display, profiles, &n);
307 if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
310 GST_DEBUG("%d profiles", n);
311 for (i = 0; i < n; i++)
312 GST_DEBUG(" %s", string_of_VAProfile(profiles[i]));
314 priv->profiles = g_array_new(FALSE, FALSE, sizeof(VAProfile));
317 g_array_append_vals(priv->profiles, profiles, n);
319 /* VA image formats */
320 formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
323 status = vaQueryImageFormats(priv->display, formats, &n);
324 if (!vaapi_check_status(status, "vaQueryImageFormats()"))
327 GST_DEBUG("%d image formats", n);
328 for (i = 0; i < n; i++)
329 GST_DEBUG(" %s", string_of_FOURCC(formats[i].fourcc));
331 priv->image_formats =
332 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
333 if (!priv->image_formats)
335 append_formats(priv->image_formats, formats, n);
336 g_array_sort(priv->image_formats, compare_yuv_formats);
338 /* VA subpicture formats */
339 n = vaMaxNumSubpictureFormats(priv->display);
340 formats = g_renew(VAImageFormat, formats, n);
341 flags = g_new(guint, n);
342 if (!formats || !flags)
344 status = vaQuerySubpictureFormats(priv->display, formats, flags, &n);
345 if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
348 GST_DEBUG("%d subpicture formats", n);
349 for (i = 0; i < n; i++)
350 GST_DEBUG(" %s", string_of_FOURCC(formats[i].fourcc));
352 priv->subpicture_formats =
353 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
354 if (!priv->subpicture_formats)
356 append_formats(priv->subpicture_formats, formats, n);
357 g_array_sort(priv->subpicture_formats, compare_rgb_formats);
368 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
370 g_static_mutex_lock(&display->priv->mutex);
374 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
376 g_static_mutex_unlock(&display->priv->mutex);
380 gst_vaapi_display_finalize(GObject *object)
382 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
384 gst_vaapi_display_destroy(display);
386 G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
390 gst_vaapi_display_set_property(
397 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
401 display->priv->display = g_value_get_pointer(value);
404 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
410 gst_vaapi_display_get_property(
417 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
421 g_value_set_pointer(value, gst_vaapi_display_get_display(display));
424 g_value_set_uint(value, gst_vaapi_display_get_width(display));
427 g_value_set_uint(value, gst_vaapi_display_get_height(display));
430 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
436 gst_vaapi_display_constructed(GObject *object)
438 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
439 GObjectClass *parent_class;
441 display->priv->create_display = display->priv->display == NULL;
442 gst_vaapi_display_create(display);
444 parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
445 if (parent_class->constructed)
446 parent_class->constructed(object);
450 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
452 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
453 GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
455 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
457 g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
459 object_class->finalize = gst_vaapi_display_finalize;
460 object_class->set_property = gst_vaapi_display_set_property;
461 object_class->get_property = gst_vaapi_display_get_property;
462 object_class->constructed = gst_vaapi_display_constructed;
464 dpy_class->lock = gst_vaapi_display_lock_default;
465 dpy_class->unlock = gst_vaapi_display_unlock_default;
467 g_object_class_install_property
470 g_param_spec_pointer("display",
473 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
475 g_object_class_install_property
478 g_param_spec_uint("width",
484 g_object_class_install_property
487 g_param_spec_uint("height",
489 "The display height",
495 gst_vaapi_display_init(GstVaapiDisplay *display)
497 GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
499 display->priv = priv;
500 priv->display = NULL;
507 priv->create_display = TRUE;
508 priv->profiles = NULL;
509 priv->image_formats = NULL;
510 priv->subpicture_formats = NULL;
512 g_static_mutex_init(&priv->mutex);
516 * gst_vaapi_display_new_with_display:
517 * @va_display: a #VADisplay
519 * Creates a new #GstVaapiDisplay, using @va_display as the VA
522 * Return value: the newly created #GstVaapiDisplay object
525 gst_vaapi_display_new_with_display(VADisplay va_display)
527 return g_object_new(GST_VAAPI_TYPE_DISPLAY,
528 "display", va_display,
533 * gst_vaapi_display_lock:
534 * @display: a #GstVaapiDisplay
536 * Locks @display. If @display is already locked by another thread,
537 * the current thread will block until @display is unlocked by the
541 gst_vaapi_display_lock(GstVaapiDisplay *display)
543 GstVaapiDisplayClass *klass;
545 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
547 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
549 klass->lock(display);
553 * gst_vaapi_display_unlock:
554 * @display: a #GstVaapiDisplay
556 * Unlocks @display. If another thread is blocked in a
557 * gst_vaapi_display_lock() call for @display, it will be woken and
558 * can lock @display itself.
561 gst_vaapi_display_unlock(GstVaapiDisplay *display)
563 GstVaapiDisplayClass *klass;
565 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
567 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
569 klass->unlock(display);
573 * gst_vaapi_display_get_display:
574 * @display: a #GstVaapiDisplay
576 * Returns the #VADisplay bound to @display.
578 * Return value: the #VADisplay
581 gst_vaapi_display_get_display(GstVaapiDisplay *display)
583 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
585 return display->priv->display;
589 * gst_vaapi_display_get_width:
590 * @display: a #GstVaapiDisplay
592 * Retrieves the width of a #GstVaapiDisplay.
594 * Return value: the width of the @display, in pixels
597 gst_vaapi_display_get_width(GstVaapiDisplay *display)
599 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
601 return display->priv->width;
605 * gst_vaapi_display_get_height:
606 * @display: a #GstVaapiDisplay
608 * Retrieves the height of a #GstVaapiDisplay
610 * Return value: the height of the @display, in pixels
613 gst_vaapi_display_get_height(GstVaapiDisplay *display)
615 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
617 return display->priv->height;
621 * gst_vaapi_display_get_size:
622 * @display: a #GstVaapiDisplay
623 * @pwidth: return location for the width, or %NULL
624 * @pheight: return location for the height, or %NULL
626 * Retrieves the dimensions of a #GstVaapiDisplay.
629 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
631 g_return_if_fail(GST_VAAPI_DISPLAY(display));
634 *pwidth = display->priv->width;
637 *pheight = display->priv->height;
641 * gst_vaapi_display_get_pixel_aspect_ratio:
642 * @display: a #GstVaapiDisplay
643 * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
644 * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
646 * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
649 gst_vaapi_display_get_pixel_aspect_ratio(
650 GstVaapiDisplay *display,
655 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
658 *par_n = display->priv->par_n;
661 *par_d = display->priv->par_d;
665 * gst_vaapi_display_has_profile:
666 * @display: a #GstVaapiDisplay
667 * @profile: a #VAProfile
669 * Returns whether VA @display supports @profile.
671 * Return value: %TRUE if VA @display supports @profile
674 gst_vaapi_display_has_profile(GstVaapiDisplay *display, VAProfile profile)
676 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
678 return find_profile(display->priv->profiles, profile);
682 * gst_vaapi_display_get_image_caps:
683 * @display: a #GstVaapiDisplay
685 * Gets the supported image formats for gst_vaapi_surface_get_image()
686 * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
688 * Note that this method does not necessarily map image formats
689 * returned by vaQueryImageFormats(). The set of capabilities can be
690 * stripped down, if gstreamer-vaapi does not support the format, or
691 * expanded to cover compatible formats not exposed by the underlying
692 * driver. e.g. I420 can be supported even if the driver only exposes
695 * Return value: a newly allocated #GstCaps object, possibly empty
698 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
700 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
702 return get_caps(display->priv->image_formats);
706 * gst_vaapi_display_has_image_format:
707 * @display: a #GstVaapiDisplay
708 * @format: a #GstVaapiFormat
710 * Returns whether VA @display supports @format image format.
712 * Return value: %TRUE if VA @display supports @format image format
715 gst_vaapi_display_has_image_format(
716 GstVaapiDisplay *display,
717 GstVaapiImageFormat format
720 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
721 g_return_val_if_fail(format, FALSE);
723 return find_format(display->priv->image_formats, format);
727 * gst_vaapi_display_get_subpicture_caps:
728 * @display: a #GstVaapiDisplay
730 * Gets the supported subpicture formats as #GstCaps capabilities.
732 * Note that this method does not necessarily map subpicture formats
733 * returned by vaQuerySubpictureFormats(). The set of capabilities can
734 * be stripped down if gstreamer-vaapi does not support the
735 * format. e.g. this is the case for paletted formats like IA44.
737 * Return value: a newly allocated #GstCaps object, possibly empty
740 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
742 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
744 return get_caps(display->priv->subpicture_formats);
748 * gst_vaapi_display_has_subpicture_format:
749 * @display: a #GstVaapiDisplay
750 * @format: a #GstVaapiFormat
752 * Returns whether VA @display supports @format subpicture format.
754 * Return value: %TRUE if VA @display supports @format subpicture format
757 gst_vaapi_display_has_subpicture_format(
758 GstVaapiDisplay *display,
759 GstVaapiImageFormat format
762 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
763 g_return_val_if_fail(format, FALSE);
765 return find_format(display->priv->subpicture_formats, format);