Add display size accessors.
[profile/ivi/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidisplay.c
1 /*
2  *  gstvaapidisplay.c - VA display abstraction
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * SECTION:gst-vaapi-display
23  * @short_description:
24  */
25
26 #include "config.h"
27 #include "gstvaapiutils.h"
28 #include "gstvaapidisplay.h"
29 #include <va/va_backend.h>
30
31 #define DEBUG 1
32 #include "gstvaapidebug.h"
33
34 GST_DEBUG_CATEGORY(gst_debug_vaapi);
35
36 G_DEFINE_TYPE(GstVaapiDisplay, gst_vaapi_display, G_TYPE_OBJECT);
37
38 #define GST_VAAPI_DISPLAY_GET_PRIVATE(obj)                      \
39     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
40                                  GST_VAAPI_TYPE_DISPLAY,        \
41                                  GstVaapiDisplayPrivate))
42
43 struct _GstVaapiDisplayPrivate {
44     GStaticMutex        mutex;
45     VADisplay           display;
46     guint               width;
47     guint               height;
48     gboolean            create_display;
49     GArray             *profiles;
50     GArray             *image_formats;
51     GArray             *subpicture_formats;
52 };
53
54 enum {
55     PROP_0,
56
57     PROP_DISPLAY,
58     PROP_WIDTH,
59     PROP_HEIGHT
60 };
61
62 /* Append GstVaapiImageFormat to formats array */
63 static inline void
64 append_format(GArray *formats, GstVaapiImageFormat format)
65 {
66     g_array_append_val(formats, format);
67 }
68
69 /* Append VAImageFormats to formats array */
70 static void
71 append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
72 {
73     GstVaapiImageFormat format;
74     gboolean has_YV12 = FALSE;
75     gboolean has_I420 = FALSE;
76     guint i;
77
78     for (i = 0; i < n; i++) {
79         const VAImageFormat * const va_format = &va_formats[i];
80
81         format = gst_vaapi_image_format(va_format);
82         if (!format) {
83             GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
84                       GST_FOURCC_ARGS(va_format->fourcc));
85             continue;
86         }
87
88         switch (format) {
89         case GST_VAAPI_IMAGE_YV12:
90             has_YV12 = TRUE;
91             break;
92         case GST_VAAPI_IMAGE_I420:
93             has_I420 = TRUE;
94             break;
95         default:
96             break;
97         }
98         append_format(formats, format);
99     }
100
101     /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
102        supported by the underlying driver */
103     if (has_YV12 && !has_I420)
104         append_format(formats, GST_VAAPI_IMAGE_I420);
105     else if (has_I420 && !has_YV12)
106         append_format(formats, GST_VAAPI_IMAGE_YV12);
107 }
108
109 /* Sort image formats. Prefer YUV formats first */
110 static gint
111 compare_yuv_formats(gconstpointer a, gconstpointer b)
112 {
113     const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
114     const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
115
116     const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
117     const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);
118
119     if (is_fmt1_yuv != is_fmt2_yuv)
120         return is_fmt1_yuv ? -1 : 1;
121
122     return ((gint)gst_vaapi_image_format_get_score(fmt1) -
123             (gint)gst_vaapi_image_format_get_score(fmt2));
124 }
125
126 /* Sort subpicture formats. Prefer RGB formats first */
127 static gint
128 compare_rgb_formats(gconstpointer a, gconstpointer b)
129 {
130     const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
131     const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
132
133     const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
134     const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);
135
136     if (is_fmt1_rgb != is_fmt2_rgb)
137         return is_fmt1_rgb ? -1 : 1;
138
139     return ((gint)gst_vaapi_image_format_get_score(fmt1) -
140             (gint)gst_vaapi_image_format_get_score(fmt2));
141 }
142
143 /* Check if profiles array contains profile */
144 static inline gboolean
145 find_profile(GArray *profiles, VAProfile profile)
146 {
147     guint i;
148
149     for (i = 0; i < profiles->len; i++)
150         if (g_array_index(profiles, VAProfile, i) == profile)
151             return TRUE;
152     return FALSE;
153 }
154
155 /* Check if formats array contains format */
156 static inline gboolean
157 find_format(GArray *formats, GstVaapiImageFormat format)
158 {
159     guint i;
160
161     for (i = 0; i < formats->len; i++)
162         if (g_array_index(formats, GstVaapiImageFormat, i) == format)
163             return TRUE;
164     return FALSE;
165 }
166
167 /* Convert formats array to GstCaps */
168 static GstCaps *
169 get_caps(GArray *formats)
170 {
171     GstVaapiImageFormat format;
172     GstCaps *out_caps, *caps;
173     guint i;
174
175     out_caps = gst_caps_new_empty();
176     if (!out_caps)
177         return NULL;
178
179     for (i = 0; i < formats->len; i++) {
180         format = g_array_index(formats, GstVaapiImageFormat, i);
181         caps   = gst_vaapi_image_format_get_caps(format);
182         if (caps)
183             gst_caps_append(out_caps, caps);
184     }
185     return out_caps;
186 }
187
188 static void
189 gst_vaapi_display_destroy(GstVaapiDisplay *display)
190 {
191     GstVaapiDisplayPrivate * const priv = display->priv;
192
193     if (priv->profiles) {
194         g_array_free(priv->profiles, TRUE);
195         priv->profiles = NULL;
196     }
197
198     if (priv->image_formats) {
199         g_array_free(priv->image_formats, TRUE);
200         priv->image_formats = NULL;
201     }
202
203     if (priv->subpicture_formats) {
204         g_array_free(priv->subpicture_formats, TRUE);
205         priv->subpicture_formats = NULL;
206     }
207
208     if (priv->display) {
209         vaTerminate(priv->display);
210         priv->display = NULL;
211     }
212
213     if (priv->create_display) {
214         GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
215         if (klass->close_display)
216             klass->close_display(display);
217     }
218 }
219
220 static gboolean
221 gst_vaapi_display_create(GstVaapiDisplay *display)
222 {
223     GstVaapiDisplayPrivate * const priv = display->priv;
224     gboolean            has_errors      = TRUE;
225     VAProfile          *profiles        = NULL;
226     VAImageFormat      *formats         = NULL;
227     unsigned int       *flags           = NULL;
228     gint                i, n, major_version, minor_version;
229     VAStatus            status;
230
231     if (!priv->display && priv->create_display) {
232         GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
233         if (klass->open_display && !klass->open_display(display))
234             return FALSE;
235         if (klass->get_display)
236             priv->display = klass->get_display(display);
237         if (klass->get_size)
238             klass->get_size(display, &priv->width, &priv->height);
239     }
240     if (!priv->display)
241         return FALSE;
242
243     status = vaInitialize(priv->display, &major_version, &minor_version);
244     if (!vaapi_check_status(status, "vaInitialize()"))
245         goto end;
246     GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
247
248     /* VA profiles */
249     profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
250     if (!profiles)
251         goto end;
252     status = vaQueryConfigProfiles(priv->display, profiles, &n);
253     if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
254         goto end;
255
256     GST_DEBUG("%d profiles", n);
257     for (i = 0; i < n; i++)
258         GST_DEBUG("  %s", string_of_VAProfile(profiles[i]));
259
260     priv->profiles = g_array_new(FALSE, FALSE, sizeof(VAProfile));
261     if (!priv->profiles)
262         goto end;
263     g_array_append_vals(priv->profiles, profiles, n);
264
265     /* VA image formats */
266     formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
267     if (!formats)
268         goto end;
269     status = vaQueryImageFormats(priv->display, formats, &n);
270     if (!vaapi_check_status(status, "vaQueryImageFormats()"))
271         goto end;
272
273     GST_DEBUG("%d image formats", n);
274     for (i = 0; i < n; i++)
275         GST_DEBUG("  %s", string_of_FOURCC(formats[i].fourcc));
276
277     priv->image_formats =
278         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
279     if (!priv->image_formats)
280         goto end;
281     append_formats(priv->image_formats, formats, n);
282     g_array_sort(priv->image_formats, compare_yuv_formats);
283
284     /* VA subpicture formats */
285     n = vaMaxNumSubpictureFormats(priv->display);
286     formats = g_renew(VAImageFormat, formats, n);
287     flags   = g_new(guint, n);
288     if (!formats || !flags)
289         goto end;
290     status = vaQuerySubpictureFormats(priv->display, formats, flags, &n);
291     if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
292         goto end;
293
294     GST_DEBUG("%d subpicture formats", n);
295     for (i = 0; i < n; i++)
296         GST_DEBUG("  %s", string_of_FOURCC(formats[i].fourcc));
297
298     priv->subpicture_formats =
299         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
300     if (!priv->subpicture_formats)
301         goto end;
302     append_formats(priv->subpicture_formats, formats, n);
303     g_array_sort(priv->subpicture_formats, compare_rgb_formats);
304
305     has_errors = FALSE;
306 end:
307     g_free(profiles);
308     g_free(formats);
309     g_free(flags);
310     return !has_errors;
311 }
312
313 static void
314 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
315 {
316     g_static_mutex_lock(&display->priv->mutex);
317 }
318
319 static void
320 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
321 {
322     g_static_mutex_unlock(&display->priv->mutex);
323 }
324
325 static void
326 gst_vaapi_display_finalize(GObject *object)
327 {
328     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
329
330     gst_vaapi_display_destroy(display);
331
332     G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
333 }
334
335 static void
336 gst_vaapi_display_set_property(
337     GObject      *object,
338     guint         prop_id,
339     const GValue *value,
340     GParamSpec   *pspec
341 )
342 {
343     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
344
345     switch (prop_id) {
346     case PROP_DISPLAY:
347         display->priv->display = g_value_get_pointer(value);
348         break;
349     default:
350         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
351         break;
352     }
353 }
354
355 static void
356 gst_vaapi_display_get_property(
357     GObject    *object,
358     guint       prop_id,
359     GValue     *value,
360     GParamSpec *pspec
361 )
362 {
363     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
364
365     switch (prop_id) {
366     case PROP_DISPLAY:
367         g_value_set_pointer(value, gst_vaapi_display_get_display(display));
368         break;
369     case PROP_WIDTH:
370         g_value_set_uint(value, gst_vaapi_display_get_width(display));
371         break;
372     case PROP_HEIGHT:
373         g_value_set_uint(value, gst_vaapi_display_get_height(display));
374         break;
375     default:
376         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
377         break;
378     }
379 }
380
381 static void
382 gst_vaapi_display_constructed(GObject *object)
383 {
384     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
385     GObjectClass *parent_class;
386
387     display->priv->create_display = display->priv->display == NULL;
388     gst_vaapi_display_create(display);
389
390     parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
391     if (parent_class->constructed)
392         parent_class->constructed(object);
393 }
394
395 static void
396 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
397 {
398     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
399     GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
400
401     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
402
403     g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
404
405     object_class->finalize      = gst_vaapi_display_finalize;
406     object_class->set_property  = gst_vaapi_display_set_property;
407     object_class->get_property  = gst_vaapi_display_get_property;
408     object_class->constructed   = gst_vaapi_display_constructed;
409
410     dpy_class->lock_display     = gst_vaapi_display_lock_default;
411     dpy_class->unlock_display   = gst_vaapi_display_unlock_default;
412
413     g_object_class_install_property
414         (object_class,
415          PROP_DISPLAY,
416          g_param_spec_pointer("display",
417                               "VA display",
418                               "VA display",
419                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
420
421     g_object_class_install_property
422         (object_class,
423          PROP_WIDTH,
424          g_param_spec_uint("width",
425                            "Width",
426                            "The display width",
427                            1, G_MAXUINT32, 1,
428                            G_PARAM_READABLE));
429
430     g_object_class_install_property
431         (object_class,
432          PROP_HEIGHT,
433          g_param_spec_uint("height",
434                            "height",
435                            "The display height",
436                            1, G_MAXUINT32, 1,
437                            G_PARAM_READABLE));
438 }
439
440 static void
441 gst_vaapi_display_init(GstVaapiDisplay *display)
442 {
443     GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
444
445     display->priv               = priv;
446     priv->display               = NULL;
447     priv->width                 = 0;
448     priv->height                = 0;
449     priv->create_display        = TRUE;
450     priv->profiles              = NULL;
451     priv->image_formats         = NULL;
452     priv->subpicture_formats    = NULL;
453
454     g_static_mutex_init(&priv->mutex);
455 }
456
457 /**
458  * gst_vaapi_display_new_with_display:
459  * @va_display: a #VADisplay
460  *
461  * Creates a new #GstVaapiDisplay, using @va_display as the VA
462  * display.
463  *
464  * Return value: the newly created #GstVaapiDisplay object
465  */
466 GstVaapiDisplay *
467 gst_vaapi_display_new_with_display(VADisplay va_display)
468 {
469     return g_object_new(GST_VAAPI_TYPE_DISPLAY,
470                         "display", va_display,
471                         NULL);
472 }
473
474 /**
475  * gst_vaapi_display_lock:
476  * @display: a #GstVaapiDisplay
477  *
478  * Locks @display. If @display is already locked by another thread,
479  * the current thread will block until @display is unlocked by the
480  * other thread.
481  */
482 void
483 gst_vaapi_display_lock(GstVaapiDisplay *display)
484 {
485     GstVaapiDisplayClass *klass;
486
487     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
488
489     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
490     if (klass->lock_display)
491         klass->lock_display(display);
492 }
493
494 /**
495  * gst_vaapi_display_unlock:
496  * @display: a #GstVaapiDisplay
497  *
498  * Unlocks @display. If another thread is blocked in a
499  * gst_vaapi_display_lock() call for @display, it will be woken and
500  * can lock @display itself.
501  */
502 void
503 gst_vaapi_display_unlock(GstVaapiDisplay *display)
504 {
505     GstVaapiDisplayClass *klass;
506
507     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
508
509     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
510     if (klass->unlock_display)
511         klass->unlock_display(display);
512 }
513
514 /**
515  * gst_vaapi_display_get_display:
516  * @display: a #GstVaapiDisplay
517  *
518  * Returns the #VADisplay bound to @display.
519  *
520  * Return value: the #VADisplay
521  */
522 VADisplay
523 gst_vaapi_display_get_display(GstVaapiDisplay *display)
524 {
525     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
526
527     return display->priv->display;
528 }
529
530 /**
531  * gst_vaapi_display_get_width:
532  * @display: a #GstVaapiDisplay
533  *
534  * Retrieves the width of a #GstVaapiDisplay.
535  *
536  * Return value: the width of the @display, in pixels
537  */
538 guint
539 gst_vaapi_display_get_width(GstVaapiDisplay *display)
540 {
541     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
542
543     return display->priv->width;
544 }
545
546 /**
547  * gst_vaapi_display_get_height:
548  * @display: a #GstVaapiDisplay
549  *
550  * Retrieves the height of a #GstVaapiDisplay
551  *
552  * Return value: the height of the @display, in pixels
553  */
554 guint
555 gst_vaapi_display_get_height(GstVaapiDisplay *display)
556 {
557     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
558
559     return display->priv->height;
560 }
561
562 /**
563  * gst_vaapi_display_get_size:
564  * @display: a #GstVaapiDisplay
565  * @pwidth: (out) (allow-none): return location for the width, or %NULL
566  * @pheight: (out) (allow-none): return location for the height, or %NULL
567  *
568  * Retrieves the dimensions of a #GstVaapiDisplay.
569  */
570 void
571 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
572 {
573     g_return_if_fail(GST_VAAPI_DISPLAY(display));
574
575     if (pwidth)
576         *pwidth = display->priv->width;
577
578     if (pheight)
579         *pheight = display->priv->height;
580 }
581
582 /**
583  * gst_vaapi_display_has_profile:
584  * @display: a #GstVaapiDisplay
585  * @profile: a #VAProfile
586  *
587  * Returns whether VA @display supports @profile.
588  *
589  * Return value: %TRUE if VA @display supports @profile
590  */
591 gboolean
592 gst_vaapi_display_has_profile(GstVaapiDisplay *display, VAProfile profile)
593 {
594     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
595
596     return find_profile(display->priv->profiles, profile);
597 }
598
599 /**
600  * gst_vaapi_display_get_image_caps:
601  * @display: a #GstVaapiDisplay
602  *
603  * Gets the supported image formats for gst_vaapi_surface_get_image()
604  * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
605  *
606  * Note that this method does not necessarily map image formats
607  * returned by vaQueryImageFormats(). The set of capabilities can be
608  * stripped down, if gstreamer-vaapi does not support the format, or
609  * expanded to cover compatible formats not exposed by the underlying
610  * driver. e.g. I420 can be supported even if the driver only exposes
611  * YV12.
612  *
613  * Return value: a newly allocated #GstCaps object, possibly empty
614  */
615 GstCaps *
616 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
617 {
618     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
619
620     return get_caps(display->priv->image_formats);
621 }
622
623 /**
624  * gst_vaapi_display_has_image_format:
625  * @display: a #GstVaapiDisplay
626  * @format: a #GstVaapiFormat
627  *
628  * Returns whether VA @display supports @format image format.
629  *
630  * Return value: %TRUE if VA @display supports @format image format
631  */
632 gboolean
633 gst_vaapi_display_has_image_format(
634     GstVaapiDisplay    *display,
635     GstVaapiImageFormat format
636 )
637 {
638     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
639     g_return_val_if_fail(format, FALSE);
640
641     return find_format(display->priv->image_formats, format);
642 }
643
644 /**
645  * gst_vaapi_display_get_subpicture_caps:
646  * @display: a #GstVaapiDisplay
647  *
648  * Gets the supported subpicture formats as #GstCaps capabilities.
649  *
650  * Note that this method does not necessarily map subpicture formats
651  * returned by vaQuerySubpictureFormats(). The set of capabilities can
652  * be stripped down if gstreamer-vaapi does not support the
653  * format. e.g. this is the case for paletted formats like IA44.
654  *
655  * Return value: a newly allocated #GstCaps object, possibly empty
656  */
657 GstCaps *
658 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
659 {
660     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
661
662     return get_caps(display->priv->subpicture_formats);
663 }
664
665 /**
666  * gst_vaapi_display_has_subpicture_format:
667  * @display: a #GstVaapiDisplay
668  * @format: a #GstVaapiFormat
669  *
670  * Returns whether VA @display supports @format subpicture format.
671  *
672  * Return value: %TRUE if VA @display supports @format subpicture format
673  */
674 gboolean
675 gst_vaapi_display_has_subpicture_format(
676     GstVaapiDisplay    *display,
677     GstVaapiImageFormat format
678 )
679 {
680     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
681     g_return_val_if_fail(format, FALSE);
682
683     return find_format(display->priv->subpicture_formats, format);
684 }