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:gst-vaapi-display
27 #include "gstvaapiutils.h"
28 #include "gstvaapidisplay.h"
29 #include <va/va_backend.h>
32 #include "gstvaapidebug.h"
34 GST_DEBUG_CATEGORY(gst_debug_vaapi);
36 G_DEFINE_TYPE(GstVaapiDisplay, gst_vaapi_display, G_TYPE_OBJECT);
38 #define GST_VAAPI_DISPLAY_GET_PRIVATE(obj) \
39 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
40 GST_VAAPI_TYPE_DISPLAY, \
41 GstVaapiDisplayPrivate))
43 struct _GstVaapiDisplayPrivate {
52 gboolean create_display;
54 GArray *image_formats;
55 GArray *subpicture_formats;
66 /* Append GstVaapiImageFormat to formats array */
68 append_format(GArray *formats, GstVaapiImageFormat format)
70 g_array_append_val(formats, format);
73 /* Append VAImageFormats to formats array */
75 append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
77 GstVaapiImageFormat format;
78 gboolean has_YV12 = FALSE;
79 gboolean has_I420 = FALSE;
82 for (i = 0; i < n; i++) {
83 const VAImageFormat * const va_format = &va_formats[i];
85 format = gst_vaapi_image_format(va_format);
87 GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
88 GST_FOURCC_ARGS(va_format->fourcc));
93 case GST_VAAPI_IMAGE_YV12:
96 case GST_VAAPI_IMAGE_I420:
102 append_format(formats, format);
105 /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
106 supported by the underlying driver */
107 if (has_YV12 && !has_I420)
108 append_format(formats, GST_VAAPI_IMAGE_I420);
109 else if (has_I420 && !has_YV12)
110 append_format(formats, GST_VAAPI_IMAGE_YV12);
113 /* Sort image formats. Prefer YUV formats first */
115 compare_yuv_formats(gconstpointer a, gconstpointer b)
117 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
118 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
120 const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
121 const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);
123 if (is_fmt1_yuv != is_fmt2_yuv)
124 return is_fmt1_yuv ? -1 : 1;
126 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
127 (gint)gst_vaapi_image_format_get_score(fmt2));
130 /* Sort subpicture formats. Prefer RGB formats first */
132 compare_rgb_formats(gconstpointer a, gconstpointer b)
134 const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
135 const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
137 const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
138 const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);
140 if (is_fmt1_rgb != is_fmt2_rgb)
141 return is_fmt1_rgb ? -1 : 1;
143 return ((gint)gst_vaapi_image_format_get_score(fmt1) -
144 (gint)gst_vaapi_image_format_get_score(fmt2));
147 /* Check if profiles array contains profile */
148 static inline gboolean
149 find_profile(GArray *profiles, VAProfile profile)
153 for (i = 0; i < profiles->len; i++)
154 if (g_array_index(profiles, VAProfile, i) == profile)
159 /* Check if formats array contains format */
160 static inline gboolean
161 find_format(GArray *formats, GstVaapiImageFormat format)
165 for (i = 0; i < formats->len; i++)
166 if (g_array_index(formats, GstVaapiImageFormat, i) == format)
171 /* Convert formats array to GstCaps */
173 get_caps(GArray *formats)
175 GstVaapiImageFormat format;
176 GstCaps *out_caps, *caps;
179 out_caps = gst_caps_new_empty();
183 for (i = 0; i < formats->len; i++) {
184 format = g_array_index(formats, GstVaapiImageFormat, i);
185 caps = gst_vaapi_image_format_get_caps(format);
187 gst_caps_append(out_caps, caps);
193 gst_vaapi_display_calculate_pixel_aspect_ratio(GstVaapiDisplay *display)
195 GstVaapiDisplayPrivate * const priv = display->priv;
196 gdouble ratio, delta;
199 static const gint par[][2] = {
200 {1, 1}, /* regular screen */
201 {16, 15}, /* PAL TV */
202 {11, 10}, /* 525 line Rec.601 video */
203 {54, 59}, /* 625 line Rec.601 video */
204 {64, 45}, /* 1280x1024 on 16:9 display */
205 {5, 3}, /* 1280x1024 on 4:3 display */
206 {4, 3} /* 800x600 on 16:9 display */
209 /* First, calculate the "real" ratio based on the X values;
210 * which is the "physical" w/h divided by the w/h in pixels of the
212 if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
215 ratio = (gdouble)(priv->width_mm * priv->height) /
216 (priv->height_mm * priv->width);
217 GST_DEBUG("calculated pixel aspect ratio: %f", ratio);
219 /* Now, find the one from par[][2] with the lowest delta to the real one */
220 #define DELTA(idx) (ABS(ratio - ((gdouble)par[idx][0] / par[idx][1])))
224 for (i = 1; i < G_N_ELEMENTS(par); i++) {
225 const gdouble this_delta = DELTA(i);
226 if (this_delta < delta) {
233 priv->par_n = par[index][0];
234 priv->par_d = par[index][1];
238 gst_vaapi_display_destroy(GstVaapiDisplay *display)
240 GstVaapiDisplayPrivate * const priv = display->priv;
242 if (priv->profiles) {
243 g_array_free(priv->profiles, TRUE);
244 priv->profiles = NULL;
247 if (priv->image_formats) {
248 g_array_free(priv->image_formats, TRUE);
249 priv->image_formats = NULL;
252 if (priv->subpicture_formats) {
253 g_array_free(priv->subpicture_formats, TRUE);
254 priv->subpicture_formats = NULL;
258 vaTerminate(priv->display);
259 priv->display = NULL;
262 if (priv->create_display) {
263 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
264 if (klass->close_display)
265 klass->close_display(display);
270 gst_vaapi_display_create(GstVaapiDisplay *display)
272 GstVaapiDisplayPrivate * const priv = display->priv;
273 gboolean has_errors = TRUE;
274 VAProfile *profiles = NULL;
275 VAImageFormat *formats = NULL;
276 unsigned int *flags = NULL;
277 gint i, n, major_version, minor_version;
280 if (!priv->display && priv->create_display) {
281 GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
282 if (klass->open_display && !klass->open_display(display))
284 if (klass->get_display)
285 priv->display = klass->get_display(display);
287 klass->get_size(display, &priv->width, &priv->height);
288 if (klass->get_size_mm)
289 klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
290 gst_vaapi_display_calculate_pixel_aspect_ratio(display);
295 status = vaInitialize(priv->display, &major_version, &minor_version);
296 if (!vaapi_check_status(status, "vaInitialize()"))
298 GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
301 profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
304 status = vaQueryConfigProfiles(priv->display, profiles, &n);
305 if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
308 GST_DEBUG("%d profiles", n);
309 for (i = 0; i < n; i++)
310 GST_DEBUG(" %s", string_of_VAProfile(profiles[i]));
312 priv->profiles = g_array_new(FALSE, FALSE, sizeof(VAProfile));
315 g_array_append_vals(priv->profiles, profiles, n);
317 /* VA image formats */
318 formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
321 status = vaQueryImageFormats(priv->display, formats, &n);
322 if (!vaapi_check_status(status, "vaQueryImageFormats()"))
325 GST_DEBUG("%d image formats", n);
326 for (i = 0; i < n; i++)
327 GST_DEBUG(" %s", string_of_FOURCC(formats[i].fourcc));
329 priv->image_formats =
330 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
331 if (!priv->image_formats)
333 append_formats(priv->image_formats, formats, n);
334 g_array_sort(priv->image_formats, compare_yuv_formats);
336 /* VA subpicture formats */
337 n = vaMaxNumSubpictureFormats(priv->display);
338 formats = g_renew(VAImageFormat, formats, n);
339 flags = g_new(guint, n);
340 if (!formats || !flags)
342 status = vaQuerySubpictureFormats(priv->display, formats, flags, &n);
343 if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
346 GST_DEBUG("%d subpicture formats", n);
347 for (i = 0; i < n; i++)
348 GST_DEBUG(" %s", string_of_FOURCC(formats[i].fourcc));
350 priv->subpicture_formats =
351 g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
352 if (!priv->subpicture_formats)
354 append_formats(priv->subpicture_formats, formats, n);
355 g_array_sort(priv->subpicture_formats, compare_rgb_formats);
366 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
368 g_static_mutex_lock(&display->priv->mutex);
372 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
374 g_static_mutex_unlock(&display->priv->mutex);
378 gst_vaapi_display_finalize(GObject *object)
380 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
382 gst_vaapi_display_destroy(display);
384 G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
388 gst_vaapi_display_set_property(
395 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
399 display->priv->display = g_value_get_pointer(value);
402 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
408 gst_vaapi_display_get_property(
415 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
419 g_value_set_pointer(value, gst_vaapi_display_get_display(display));
422 g_value_set_uint(value, gst_vaapi_display_get_width(display));
425 g_value_set_uint(value, gst_vaapi_display_get_height(display));
428 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
434 gst_vaapi_display_constructed(GObject *object)
436 GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
437 GObjectClass *parent_class;
439 display->priv->create_display = display->priv->display == NULL;
440 gst_vaapi_display_create(display);
442 parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
443 if (parent_class->constructed)
444 parent_class->constructed(object);
448 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
450 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
451 GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
453 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
455 g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
457 object_class->finalize = gst_vaapi_display_finalize;
458 object_class->set_property = gst_vaapi_display_set_property;
459 object_class->get_property = gst_vaapi_display_get_property;
460 object_class->constructed = gst_vaapi_display_constructed;
462 dpy_class->lock_display = gst_vaapi_display_lock_default;
463 dpy_class->unlock_display = gst_vaapi_display_unlock_default;
465 g_object_class_install_property
468 g_param_spec_pointer("display",
471 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
473 g_object_class_install_property
476 g_param_spec_uint("width",
482 g_object_class_install_property
485 g_param_spec_uint("height",
487 "The display height",
493 gst_vaapi_display_init(GstVaapiDisplay *display)
495 GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
497 display->priv = priv;
498 priv->display = NULL;
505 priv->create_display = TRUE;
506 priv->profiles = NULL;
507 priv->image_formats = NULL;
508 priv->subpicture_formats = NULL;
510 g_static_mutex_init(&priv->mutex);
514 * gst_vaapi_display_new_with_display:
515 * @va_display: a #VADisplay
517 * Creates a new #GstVaapiDisplay, using @va_display as the VA
520 * Return value: the newly created #GstVaapiDisplay object
523 gst_vaapi_display_new_with_display(VADisplay va_display)
525 return g_object_new(GST_VAAPI_TYPE_DISPLAY,
526 "display", va_display,
531 * gst_vaapi_display_lock:
532 * @display: a #GstVaapiDisplay
534 * Locks @display. If @display is already locked by another thread,
535 * the current thread will block until @display is unlocked by the
539 gst_vaapi_display_lock(GstVaapiDisplay *display)
541 GstVaapiDisplayClass *klass;
543 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
545 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
546 if (klass->lock_display)
547 klass->lock_display(display);
551 * gst_vaapi_display_unlock:
552 * @display: a #GstVaapiDisplay
554 * Unlocks @display. If another thread is blocked in a
555 * gst_vaapi_display_lock() call for @display, it will be woken and
556 * can lock @display itself.
559 gst_vaapi_display_unlock(GstVaapiDisplay *display)
561 GstVaapiDisplayClass *klass;
563 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
565 klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
566 if (klass->unlock_display)
567 klass->unlock_display(display);
571 * gst_vaapi_display_get_display:
572 * @display: a #GstVaapiDisplay
574 * Returns the #VADisplay bound to @display.
576 * Return value: the #VADisplay
579 gst_vaapi_display_get_display(GstVaapiDisplay *display)
581 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
583 return display->priv->display;
587 * gst_vaapi_display_get_width:
588 * @display: a #GstVaapiDisplay
590 * Retrieves the width of a #GstVaapiDisplay.
592 * Return value: the width of the @display, in pixels
595 gst_vaapi_display_get_width(GstVaapiDisplay *display)
597 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
599 return display->priv->width;
603 * gst_vaapi_display_get_height:
604 * @display: a #GstVaapiDisplay
606 * Retrieves the height of a #GstVaapiDisplay
608 * Return value: the height of the @display, in pixels
611 gst_vaapi_display_get_height(GstVaapiDisplay *display)
613 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
615 return display->priv->height;
619 * gst_vaapi_display_get_size:
620 * @display: a #GstVaapiDisplay
621 * @pwidth: return location for the width, or %NULL
622 * @pheight: return location for the height, or %NULL
624 * Retrieves the dimensions of a #GstVaapiDisplay.
627 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
629 g_return_if_fail(GST_VAAPI_DISPLAY(display));
632 *pwidth = display->priv->width;
635 *pheight = display->priv->height;
639 * gst_vaapi_display_get_pixel_aspect_ratio:
640 * @display: a #GstVaapiDisplay
641 * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
642 * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
644 * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
647 gst_vaapi_display_get_pixel_aspect_ratio(
648 GstVaapiDisplay *display,
653 g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
656 *par_n = display->priv->par_n;
659 *par_d = display->priv->par_d;
663 * gst_vaapi_display_has_profile:
664 * @display: a #GstVaapiDisplay
665 * @profile: a #VAProfile
667 * Returns whether VA @display supports @profile.
669 * Return value: %TRUE if VA @display supports @profile
672 gst_vaapi_display_has_profile(GstVaapiDisplay *display, VAProfile profile)
674 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
676 return find_profile(display->priv->profiles, profile);
680 * gst_vaapi_display_get_image_caps:
681 * @display: a #GstVaapiDisplay
683 * Gets the supported image formats for gst_vaapi_surface_get_image()
684 * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
686 * Note that this method does not necessarily map image formats
687 * returned by vaQueryImageFormats(). The set of capabilities can be
688 * stripped down, if gstreamer-vaapi does not support the format, or
689 * expanded to cover compatible formats not exposed by the underlying
690 * driver. e.g. I420 can be supported even if the driver only exposes
693 * Return value: a newly allocated #GstCaps object, possibly empty
696 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
698 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
700 return get_caps(display->priv->image_formats);
704 * gst_vaapi_display_has_image_format:
705 * @display: a #GstVaapiDisplay
706 * @format: a #GstVaapiFormat
708 * Returns whether VA @display supports @format image format.
710 * Return value: %TRUE if VA @display supports @format image format
713 gst_vaapi_display_has_image_format(
714 GstVaapiDisplay *display,
715 GstVaapiImageFormat format
718 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
719 g_return_val_if_fail(format, FALSE);
721 return find_format(display->priv->image_formats, format);
725 * gst_vaapi_display_get_subpicture_caps:
726 * @display: a #GstVaapiDisplay
728 * Gets the supported subpicture formats as #GstCaps capabilities.
730 * Note that this method does not necessarily map subpicture formats
731 * returned by vaQuerySubpictureFormats(). The set of capabilities can
732 * be stripped down if gstreamer-vaapi does not support the
733 * format. e.g. this is the case for paletted formats like IA44.
735 * Return value: a newly allocated #GstCaps object, possibly empty
738 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
740 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
742 return get_caps(display->priv->subpicture_formats);
746 * gst_vaapi_display_has_subpicture_format:
747 * @display: a #GstVaapiDisplay
748 * @format: a #GstVaapiFormat
750 * Returns whether VA @display supports @format subpicture format.
752 * Return value: %TRUE if VA @display supports @format subpicture format
755 gst_vaapi_display_has_subpicture_format(
756 GstVaapiDisplay *display,
757 GstVaapiImageFormat format
760 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
761 g_return_val_if_fail(format, FALSE);
763 return find_format(display->priv->subpicture_formats, format);