cosmetics: fix warnings (drop unused variables).
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidisplay.c
1 /*
2  *  gstvaapidisplay.c - VA display abstraction
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 2011-2012 Intel Corporation
6  *
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.
11  *
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.
16  *
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
21  */
22
23 /**
24  * SECTION:gstvaapidisplay
25  * @short_description: VA display abstraction
26  */
27
28 #include "sysdeps.h"
29 #include <string.h>
30 #include "gstvaapiutils.h"
31 #include "gstvaapidisplay.h"
32 #include "gstvaapidisplay_priv.h"
33 #include "gstvaapiworkarounds.h"
34
35 #define DEBUG 1
36 #include "gstvaapidebug.h"
37
38 GST_DEBUG_CATEGORY(gst_debug_vaapi);
39
40 G_DEFINE_TYPE(GstVaapiDisplay, gst_vaapi_display, G_TYPE_OBJECT);
41
42 typedef struct _GstVaapiConfig GstVaapiConfig;
43 struct _GstVaapiConfig {
44     GstVaapiProfile     profile;
45     GstVaapiEntrypoint  entrypoint;
46 };
47
48 enum {
49     PROP_0,
50
51     PROP_DISPLAY,
52     PROP_WIDTH,
53     PROP_HEIGHT
54 };
55
56 static GstVaapiDisplayCache *g_display_cache = NULL;
57
58 static inline GstVaapiDisplayCache *
59 get_display_cache(void)
60 {
61     if (!g_display_cache)
62         g_display_cache = gst_vaapi_display_cache_new();
63     return g_display_cache;
64 }
65
66 GstVaapiDisplayCache *
67 gst_vaapi_display_get_cache(void)
68 {
69     return get_display_cache();
70 }
71
72 static void
73 free_display_cache(void)
74 {
75     if (!g_display_cache)
76         return;
77     if (gst_vaapi_display_cache_get_size(g_display_cache) > 0)
78         return;
79     gst_vaapi_display_cache_free(g_display_cache);
80     g_display_cache = NULL;
81 }
82
83 /* Append GstVaapiImageFormat to formats array */
84 static inline void
85 append_format(GArray *formats, GstVaapiImageFormat format)
86 {
87     g_array_append_val(formats, format);
88 }
89
90 /* Append VAImageFormats to formats array */
91 static void
92 append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
93 {
94     GstVaapiImageFormat format;
95     gboolean has_YV12 = FALSE;
96     gboolean has_I420 = FALSE;
97     guint i;
98
99     for (i = 0; i < n; i++) {
100         const VAImageFormat * const va_format = &va_formats[i];
101
102         format = gst_vaapi_image_format(va_format);
103         if (!format) {
104             GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
105                       GST_FOURCC_ARGS(va_format->fourcc));
106             continue;
107         }
108
109         switch (format) {
110         case GST_VAAPI_IMAGE_YV12:
111             has_YV12 = TRUE;
112             break;
113         case GST_VAAPI_IMAGE_I420:
114             has_I420 = TRUE;
115             break;
116         default:
117             break;
118         }
119         append_format(formats, format);
120     }
121
122     /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
123        supported by the underlying driver */
124     if (has_YV12 && !has_I420)
125         append_format(formats, GST_VAAPI_IMAGE_I420);
126     else if (has_I420 && !has_YV12)
127         append_format(formats, GST_VAAPI_IMAGE_YV12);
128 }
129
130 /* Sort image formats. Prefer YUV formats first */
131 static gint
132 compare_yuv_formats(gconstpointer a, gconstpointer b)
133 {
134     const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
135     const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
136
137     const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
138     const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);
139
140     if (is_fmt1_yuv != is_fmt2_yuv)
141         return is_fmt1_yuv ? -1 : 1;
142
143     return ((gint)gst_vaapi_image_format_get_score(fmt1) -
144             (gint)gst_vaapi_image_format_get_score(fmt2));
145 }
146
147 /* Sort subpicture formats. Prefer RGB formats first */
148 static gint
149 compare_rgb_formats(gconstpointer a, gconstpointer b)
150 {
151     const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
152     const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
153
154     const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
155     const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);
156
157     if (is_fmt1_rgb != is_fmt2_rgb)
158         return is_fmt1_rgb ? -1 : 1;
159
160     return ((gint)gst_vaapi_image_format_get_score(fmt1) -
161             (gint)gst_vaapi_image_format_get_score(fmt2));
162 }
163
164 /* Check if configs array contains profile at entrypoint */
165 static inline gboolean
166 find_config(
167     GArray             *configs,
168     GstVaapiProfile     profile,
169     GstVaapiEntrypoint  entrypoint
170 )
171 {
172     GstVaapiConfig *config;
173     guint i;
174
175     if (!configs)
176         return FALSE;
177
178     for (i = 0; i < configs->len; i++) {
179         config = &g_array_index(configs, GstVaapiConfig, i);
180         if (config->profile == profile && config->entrypoint == entrypoint)
181             return TRUE;
182     }
183     return FALSE;
184 }
185
186 /* HACK: append H.263 Baseline profile if MPEG-4:2 Simple profile is supported */
187 static void
188 append_h263_config(GArray *configs)
189 {
190     GstVaapiConfig *config, tmp_config;
191     GstVaapiConfig *mpeg4_simple_config = NULL;
192     GstVaapiConfig *h263_baseline_config = NULL;
193     guint i;
194
195     if (!WORKAROUND_H263_BASELINE_DECODE_PROFILE)
196         return;
197
198     if (!configs)
199         return;
200
201     for (i = 0; i < configs->len; i++) {
202         config = &g_array_index(configs, GstVaapiConfig, i);
203         if (config->profile == GST_VAAPI_PROFILE_MPEG4_SIMPLE)
204             mpeg4_simple_config = config;
205         else if (config->profile == GST_VAAPI_PROFILE_H263_BASELINE)
206             h263_baseline_config = config;
207     }
208
209     if (mpeg4_simple_config && !h263_baseline_config) {
210         tmp_config = *mpeg4_simple_config;
211         tmp_config.profile = GST_VAAPI_PROFILE_H263_BASELINE;
212         g_array_append_val(configs, tmp_config);
213     }
214 }
215
216 /* Convert configs array to profiles as GstCaps */
217 static GstCaps *
218 get_profile_caps(GArray *configs)
219 {
220     GstVaapiConfig *config;
221     GstCaps *out_caps, *caps;
222     guint i;
223
224     if (!configs)
225         return NULL;
226
227     out_caps = gst_caps_new_empty();
228     if (!out_caps)
229         return NULL;
230
231     for (i = 0; i < configs->len; i++) {
232         config = &g_array_index(configs, GstVaapiConfig, i);
233         caps   = gst_vaapi_profile_get_caps(config->profile);
234         if (caps)
235             gst_caps_merge(out_caps, caps);
236     }
237     return out_caps;
238 }
239
240 /* Check if formats array contains format */
241 static inline gboolean
242 find_format(GArray *formats, GstVaapiImageFormat format)
243 {
244     guint i;
245
246     for (i = 0; i < formats->len; i++)
247         if (g_array_index(formats, GstVaapiImageFormat, i) == format)
248             return TRUE;
249     return FALSE;
250 }
251
252 /* Convert formats array to GstCaps */
253 static GstCaps *
254 get_format_caps(GArray *formats)
255 {
256     GstVaapiImageFormat format;
257     GstCaps *out_caps, *caps;
258     guint i;
259
260     out_caps = gst_caps_new_empty();
261     if (!out_caps)
262         return NULL;
263
264     for (i = 0; i < formats->len; i++) {
265         format = g_array_index(formats, GstVaapiImageFormat, i);
266         caps   = gst_vaapi_image_format_get_caps(format);
267         if (caps)
268             gst_caps_append(out_caps, caps);
269     }
270     return out_caps;
271 }
272
273 static void
274 gst_vaapi_display_calculate_pixel_aspect_ratio(GstVaapiDisplay *display)
275 {
276     GstVaapiDisplayPrivate * const priv = display->priv;
277     gdouble ratio, delta;
278     gint i, index;
279
280     static const gint par[][2] = {
281         {1, 1},         /* regular screen            */
282         {16, 15},       /* PAL TV                    */
283         {11, 10},       /* 525 line Rec.601 video    */
284         {54, 59},       /* 625 line Rec.601 video    */
285         {64, 45},       /* 1280x1024 on 16:9 display */
286         {5, 3},         /* 1280x1024 on  4:3 display */
287         {4, 3}          /*  800x600  on 16:9 display */
288     };
289
290     /* First, calculate the "real" ratio based on the X values;
291      * which is the "physical" w/h divided by the w/h in pixels of the
292      * display */
293     if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
294         ratio = 1.0;
295     else
296         ratio = (gdouble)(priv->width_mm * priv->height) /
297             (priv->height_mm * priv->width);
298     GST_DEBUG("calculated pixel aspect ratio: %f", ratio);
299
300     /* Now, find the one from par[][2] with the lowest delta to the real one */
301 #define DELTA(idx) (ABS(ratio - ((gdouble)par[idx][0] / par[idx][1])))
302     delta = DELTA(0);
303     index = 0;
304
305     for (i = 1; i < G_N_ELEMENTS(par); i++) {
306         const gdouble this_delta = DELTA(i);
307         if (this_delta < delta) {
308             index = i;
309             delta = this_delta;
310         }
311     }
312 #undef DELTA
313
314     priv->par_n = par[index][0];
315     priv->par_d = par[index][1];
316 }
317
318 static void
319 gst_vaapi_display_destroy(GstVaapiDisplay *display)
320 {
321     GstVaapiDisplayPrivate * const priv = display->priv;
322
323     if (priv->decoders) {
324         g_array_free(priv->decoders, TRUE);
325         priv->decoders = NULL;
326     }
327
328     if (priv->encoders) {
329         g_array_free(priv->encoders, TRUE);
330         priv->encoders = NULL;
331     }
332
333     if (priv->image_formats) {
334         g_array_free(priv->image_formats, TRUE);
335         priv->image_formats = NULL;
336     }
337
338     if (priv->subpicture_formats) {
339         g_array_free(priv->subpicture_formats, TRUE);
340         priv->subpicture_formats = NULL;
341     }
342
343     if (priv->display) {
344         if (!priv->parent)
345             vaTerminate(priv->display);
346         priv->display = NULL;
347     }
348
349     if (priv->create_display) {
350         GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
351         if (klass->close_display)
352             klass->close_display(display);
353     }
354
355     if (priv->parent) {
356         g_object_unref(priv->parent);
357         priv->parent = NULL;
358     }
359
360     if (g_display_cache) {
361         gst_vaapi_display_cache_remove(get_display_cache(), display);
362         free_display_cache();
363     }
364 }
365
366 static gboolean
367 gst_vaapi_display_create(GstVaapiDisplay *display)
368 {
369     GstVaapiDisplayPrivate * const priv = display->priv;
370     GstVaapiDisplayCache *cache;
371     gboolean            has_errors      = TRUE;
372     VAProfile          *profiles        = NULL;
373     VAEntrypoint       *entrypoints     = NULL;
374     VAImageFormat      *formats         = NULL;
375     unsigned int       *flags           = NULL;
376     gint                i, j, n, num_entrypoints, major_version, minor_version;
377     VAStatus            status;
378     GstVaapiDisplayInfo info;
379     const GstVaapiDisplayInfo *cached_info = NULL;
380
381     memset(&info, 0, sizeof(info));
382     info.display = display;
383
384     if (priv->display)
385         info.va_display = priv->display;
386     else if (priv->create_display) {
387         GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
388         if (klass->open_display && !klass->open_display(display))
389             return FALSE;
390         if (!klass->get_display || !klass->get_display(display, &info))
391             return FALSE;
392         priv->display = info.va_display;
393         if (klass->get_size)
394             klass->get_size(display, &priv->width, &priv->height);
395         if (klass->get_size_mm)
396             klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
397         gst_vaapi_display_calculate_pixel_aspect_ratio(display);
398     }
399     if (!priv->display)
400         return FALSE;
401
402     cache = get_display_cache();
403     if (!cache)
404         return FALSE;
405     cached_info = gst_vaapi_display_cache_lookup_by_va_display(
406         cache,
407         info.va_display
408     );
409     if (cached_info) {
410         if (priv->parent)
411             g_object_unref(priv->parent);
412         priv->parent = g_object_ref(cached_info->display);
413     }
414
415     if (!priv->parent) {
416         status = vaInitialize(priv->display, &major_version, &minor_version);
417         if (!vaapi_check_status(status, "vaInitialize()"))
418             goto end;
419         GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
420     }
421
422     /* VA profiles */
423     profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
424     if (!profiles)
425         goto end;
426     entrypoints = g_new(VAEntrypoint, vaMaxNumEntrypoints(priv->display));
427     if (!entrypoints)
428         goto end;
429     status = vaQueryConfigProfiles(priv->display, profiles, &n);
430     if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
431         goto end;
432
433     GST_DEBUG("%d profiles", n);
434     for (i = 0; i < n; i++)
435         GST_DEBUG("  %s", string_of_VAProfile(profiles[i]));
436
437     priv->decoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
438     if (!priv->decoders)
439         goto end;
440     priv->encoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
441     if (!priv->encoders)
442         goto end;
443
444     for (i = 0; i < n; i++) {
445         GstVaapiConfig config;
446
447         config.profile = gst_vaapi_profile(profiles[i]);
448         if (!config.profile)
449             continue;
450
451         status = vaQueryConfigEntrypoints(
452             priv->display,
453             profiles[i],
454             entrypoints, &num_entrypoints
455         );
456         if (!vaapi_check_status(status, "vaQueryConfigEntrypoints()"))
457             continue;
458
459         for (j = 0; j < num_entrypoints; j++) {
460             config.entrypoint = gst_vaapi_entrypoint(entrypoints[j]);
461             switch (config.entrypoint) {
462             case GST_VAAPI_ENTRYPOINT_VLD:
463             case GST_VAAPI_ENTRYPOINT_IDCT:
464             case GST_VAAPI_ENTRYPOINT_MOCO:
465                 g_array_append_val(priv->decoders, config);
466                 break;
467             case GST_VAAPI_ENTRYPOINT_SLICE_ENCODE:
468                 g_array_append_val(priv->encoders, config);
469                 break;
470             }
471         }
472     }
473     append_h263_config(priv->decoders);
474
475     /* VA image formats */
476     formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
477     if (!formats)
478         goto end;
479     status = vaQueryImageFormats(priv->display, formats, &n);
480     if (!vaapi_check_status(status, "vaQueryImageFormats()"))
481         goto end;
482
483     GST_DEBUG("%d image formats", n);
484     for (i = 0; i < n; i++)
485         GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
486
487     priv->image_formats =
488         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
489     if (!priv->image_formats)
490         goto end;
491     append_formats(priv->image_formats, formats, n);
492     g_array_sort(priv->image_formats, compare_yuv_formats);
493
494     /* VA subpicture formats */
495     n = vaMaxNumSubpictureFormats(priv->display);
496     formats = g_renew(VAImageFormat, formats, n);
497     flags   = g_new(guint, n);
498     if (!formats || !flags)
499         goto end;
500     status = vaQuerySubpictureFormats(priv->display, formats, flags, (guint *)&n);
501     if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
502         goto end;
503
504     GST_DEBUG("%d subpicture formats", n);
505     for (i = 0; i < n; i++)
506         GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
507
508     priv->subpicture_formats =
509         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
510     if (!priv->subpicture_formats)
511         goto end;
512     append_formats(priv->subpicture_formats, formats, n);
513     g_array_sort(priv->subpicture_formats, compare_rgb_formats);
514
515     if (!cached_info) {
516         if (!gst_vaapi_display_cache_add(cache, &info))
517             goto end;
518     }
519
520     has_errors = FALSE;
521 end:
522     g_free(profiles);
523     g_free(entrypoints);
524     g_free(formats);
525     g_free(flags);
526     return !has_errors;
527 }
528
529 static void
530 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
531 {
532     GstVaapiDisplayPrivate *priv = display->priv;
533
534     if (priv->parent)
535         priv = priv->parent->priv;
536     g_static_rec_mutex_lock(&priv->mutex);
537 }
538
539 static void
540 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
541 {
542     GstVaapiDisplayPrivate *priv = display->priv;
543
544     if (priv->parent)
545         priv = priv->parent->priv;
546     g_static_rec_mutex_unlock(&priv->mutex);
547 }
548
549 static void
550 gst_vaapi_display_finalize(GObject *object)
551 {
552     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
553
554     gst_vaapi_display_destroy(display);
555
556     G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
557 }
558
559 static void
560 gst_vaapi_display_set_property(
561     GObject      *object,
562     guint         prop_id,
563     const GValue *value,
564     GParamSpec   *pspec
565 )
566 {
567     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
568
569     switch (prop_id) {
570     case PROP_DISPLAY:
571         display->priv->display = g_value_get_pointer(value);
572         break;
573     default:
574         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
575         break;
576     }
577 }
578
579 static void
580 gst_vaapi_display_get_property(
581     GObject    *object,
582     guint       prop_id,
583     GValue     *value,
584     GParamSpec *pspec
585 )
586 {
587     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
588
589     switch (prop_id) {
590     case PROP_DISPLAY:
591         g_value_set_pointer(value, gst_vaapi_display_get_display(display));
592         break;
593     case PROP_WIDTH:
594         g_value_set_uint(value, gst_vaapi_display_get_width(display));
595         break;
596     case PROP_HEIGHT:
597         g_value_set_uint(value, gst_vaapi_display_get_height(display));
598         break;
599     default:
600         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
601         break;
602     }
603 }
604
605 static void
606 gst_vaapi_display_constructed(GObject *object)
607 {
608     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
609     GObjectClass *parent_class;
610
611     display->priv->create_display = display->priv->display == NULL;
612     if (!gst_vaapi_display_create(display))
613         gst_vaapi_display_destroy(display);
614
615     parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
616     if (parent_class->constructed)
617         parent_class->constructed(object);
618 }
619
620 static void
621 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
622 {
623     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
624     GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
625
626     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
627
628     g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
629
630     object_class->finalize      = gst_vaapi_display_finalize;
631     object_class->set_property  = gst_vaapi_display_set_property;
632     object_class->get_property  = gst_vaapi_display_get_property;
633     object_class->constructed   = gst_vaapi_display_constructed;
634
635     dpy_class->lock             = gst_vaapi_display_lock_default;
636     dpy_class->unlock           = gst_vaapi_display_unlock_default;
637
638     g_object_class_install_property
639         (object_class,
640          PROP_DISPLAY,
641          g_param_spec_pointer("display",
642                               "VA display",
643                               "VA display",
644                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
645
646     g_object_class_install_property
647         (object_class,
648          PROP_WIDTH,
649          g_param_spec_uint("width",
650                            "Width",
651                            "The display width",
652                            1, G_MAXUINT32, 1,
653                            G_PARAM_READABLE));
654
655     g_object_class_install_property
656         (object_class,
657          PROP_HEIGHT,
658          g_param_spec_uint("height",
659                            "height",
660                            "The display height",
661                            1, G_MAXUINT32, 1,
662                            G_PARAM_READABLE));
663 }
664
665 static void
666 gst_vaapi_display_init(GstVaapiDisplay *display)
667 {
668     GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
669
670     display->priv               = priv;
671     priv->parent                = NULL;
672     priv->display               = NULL;
673     priv->width                 = 0;
674     priv->height                = 0;
675     priv->width_mm              = 0;
676     priv->height_mm             = 0;
677     priv->par_n                 = 1;
678     priv->par_d                 = 1;
679     priv->decoders              = NULL;
680     priv->encoders              = NULL;
681     priv->image_formats         = NULL;
682     priv->subpicture_formats    = NULL;
683     priv->create_display        = TRUE;
684
685     g_static_rec_mutex_init(&priv->mutex);
686 }
687
688 /**
689  * gst_vaapi_display_new_with_display:
690  * @va_display: a #VADisplay
691  *
692  * Creates a new #GstVaapiDisplay, using @va_display as the VA
693  * display.
694  *
695  * Return value: the newly created #GstVaapiDisplay object
696  */
697 GstVaapiDisplay *
698 gst_vaapi_display_new_with_display(VADisplay va_display)
699 {
700     GstVaapiDisplayCache * const cache = get_display_cache();
701     const GstVaapiDisplayInfo *info;
702
703     g_return_val_if_fail(va_display != NULL, NULL);
704     g_return_val_if_fail(cache != NULL, NULL);
705
706     info = gst_vaapi_display_cache_lookup_by_va_display(cache, va_display);
707     if (info)
708         return g_object_ref(info->display);
709
710     return g_object_new(GST_VAAPI_TYPE_DISPLAY,
711                         "display", va_display,
712                         NULL);
713 }
714
715 /**
716  * gst_vaapi_display_lock:
717  * @display: a #GstVaapiDisplay
718  *
719  * Locks @display. If @display is already locked by another thread,
720  * the current thread will block until @display is unlocked by the
721  * other thread.
722  */
723 void
724 gst_vaapi_display_lock(GstVaapiDisplay *display)
725 {
726     GstVaapiDisplayClass *klass;
727
728     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
729
730     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
731     if (klass->lock)
732         klass->lock(display);
733 }
734
735 /**
736  * gst_vaapi_display_unlock:
737  * @display: a #GstVaapiDisplay
738  *
739  * Unlocks @display. If another thread is blocked in a
740  * gst_vaapi_display_lock() call for @display, it will be woken and
741  * can lock @display itself.
742  */
743 void
744 gst_vaapi_display_unlock(GstVaapiDisplay *display)
745 {
746     GstVaapiDisplayClass *klass;
747
748     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
749
750     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
751     if (klass->unlock)
752         klass->unlock(display);
753 }
754
755 /**
756  * gst_vaapi_display_sync:
757  * @display: a #GstVaapiDisplay
758  *
759  * Flushes any requests queued for the windowing system and waits until
760  * all requests have been handled. This is often used for making sure
761  * that the display is synchronized with the current state of the program.
762  *
763  * This is most useful for X11. On windowing systems where requests are
764  * handled synchronously, this function will do nothing.
765  */
766 void
767 gst_vaapi_display_sync(GstVaapiDisplay *display)
768 {
769     GstVaapiDisplayClass *klass;
770
771     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
772
773     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
774     if (klass->sync)
775         klass->sync(display);
776     else if (klass->flush)
777         klass->flush(display);
778 }
779
780 /**
781  * gst_vaapi_display_flush:
782  * @display: a #GstVaapiDisplay
783  *
784  * Flushes any requests queued for the windowing system.
785  *
786  * This is most useful for X11. On windowing systems where requests
787  * are handled synchronously, this function will do nothing.
788  */
789 void
790 gst_vaapi_display_flush(GstVaapiDisplay *display)
791 {
792     GstVaapiDisplayClass *klass;
793
794     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
795
796     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
797     if (klass->flush)
798         klass->flush(display);
799 }
800
801 /**
802  * gst_vaapi_display_get_display:
803  * @display: a #GstVaapiDisplay
804  *
805  * Returns the #VADisplay bound to @display.
806  *
807  * Return value: the #VADisplay
808  */
809 VADisplay
810 gst_vaapi_display_get_display(GstVaapiDisplay *display)
811 {
812     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
813
814     return display->priv->display;
815 }
816
817 /**
818  * gst_vaapi_display_get_width:
819  * @display: a #GstVaapiDisplay
820  *
821  * Retrieves the width of a #GstVaapiDisplay.
822  *
823  * Return value: the width of the @display, in pixels
824  */
825 guint
826 gst_vaapi_display_get_width(GstVaapiDisplay *display)
827 {
828     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
829
830     return display->priv->width;
831 }
832
833 /**
834  * gst_vaapi_display_get_height:
835  * @display: a #GstVaapiDisplay
836  *
837  * Retrieves the height of a #GstVaapiDisplay
838  *
839  * Return value: the height of the @display, in pixels
840  */
841 guint
842 gst_vaapi_display_get_height(GstVaapiDisplay *display)
843 {
844     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
845
846     return display->priv->height;
847 }
848
849 /**
850  * gst_vaapi_display_get_size:
851  * @display: a #GstVaapiDisplay
852  * @pwidth: return location for the width, or %NULL
853  * @pheight: return location for the height, or %NULL
854  *
855  * Retrieves the dimensions of a #GstVaapiDisplay.
856  */
857 void
858 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
859 {
860     g_return_if_fail(GST_VAAPI_DISPLAY(display));
861
862     if (pwidth)
863         *pwidth = display->priv->width;
864
865     if (pheight)
866         *pheight = display->priv->height;
867 }
868
869 /**
870  * gst_vaapi_display_get_pixel_aspect_ratio:
871  * @display: a #GstVaapiDisplay
872  * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
873  * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
874  *
875  * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
876  */
877 void
878 gst_vaapi_display_get_pixel_aspect_ratio(
879     GstVaapiDisplay *display,
880     guint           *par_n,
881     guint           *par_d
882 )
883 {
884     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
885
886     if (par_n)
887         *par_n = display->priv->par_n;
888
889     if (par_d)
890         *par_d = display->priv->par_d;
891 }
892
893 /**
894  * gst_vaapi_display_get_decode_caps:
895  * @display: a #GstVaapiDisplay
896  *
897  * Gets the supported profiles for decoding as #GstCaps capabilities.
898  *
899  * Return value: a newly allocated #GstCaps object, possibly empty
900  */
901 GstCaps *
902 gst_vaapi_display_get_decode_caps(GstVaapiDisplay *display)
903 {
904     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
905
906     return get_profile_caps(display->priv->decoders);
907 }
908
909 /**
910  * gst_vaapi_display_has_decoder:
911  * @display: a #GstVaapiDisplay
912  * @profile: a #VAProfile
913  * @entrypoint: a #GstVaaiEntrypoint
914  *
915  * Returns whether VA @display supports @profile for decoding at the
916  * specified @entrypoint.
917  *
918  * Return value: %TRUE if VA @display supports @profile for decoding.
919  */
920 gboolean
921 gst_vaapi_display_has_decoder(
922     GstVaapiDisplay    *display,
923     GstVaapiProfile     profile,
924     GstVaapiEntrypoint  entrypoint
925 )
926 {
927     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
928
929     return find_config(display->priv->decoders, profile, entrypoint);
930 }
931
932 /**
933  * gst_vaapi_display_get_encode_caps:
934  * @display: a #GstVaapiDisplay
935  *
936  * Gets the supported profiles for decoding as #GstCaps capabilities.
937  *
938  * Return value: a newly allocated #GstCaps object, possibly empty
939  */
940 GstCaps *
941 gst_vaapi_display_get_encode_caps(GstVaapiDisplay *display)
942 {
943     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
944
945     return get_profile_caps(display->priv->encoders);
946 }
947
948 /**
949  * gst_vaapi_display_has_encoder:
950  * @display: a #GstVaapiDisplay
951  * @profile: a #VAProfile
952  * @entrypoint: a #GstVaapiEntrypoint
953  *
954  * Returns whether VA @display supports @profile for encoding at the
955  * specified @entrypoint.
956  *
957  * Return value: %TRUE if VA @display supports @profile for encoding.
958  */
959 gboolean
960 gst_vaapi_display_has_encoder(
961     GstVaapiDisplay    *display,
962     GstVaapiProfile     profile,
963     GstVaapiEntrypoint  entrypoint
964 )
965 {
966     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
967
968     return find_config(display->priv->encoders, profile, entrypoint);
969 }
970
971 /**
972  * gst_vaapi_display_get_image_caps:
973  * @display: a #GstVaapiDisplay
974  *
975  * Gets the supported image formats for gst_vaapi_surface_get_image()
976  * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
977  *
978  * Note that this method does not necessarily map image formats
979  * returned by vaQueryImageFormats(). The set of capabilities can be
980  * stripped down, if gstreamer-vaapi does not support the format, or
981  * expanded to cover compatible formats not exposed by the underlying
982  * driver. e.g. I420 can be supported even if the driver only exposes
983  * YV12.
984  *
985  * Return value: a newly allocated #GstCaps object, possibly empty
986  */
987 GstCaps *
988 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
989 {
990     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
991
992     return get_format_caps(display->priv->image_formats);
993 }
994
995 /**
996  * gst_vaapi_display_has_image_format:
997  * @display: a #GstVaapiDisplay
998  * @format: a #GstVaapiFormat
999  *
1000  * Returns whether VA @display supports @format image format.
1001  *
1002  * Return value: %TRUE if VA @display supports @format image format
1003  */
1004 gboolean
1005 gst_vaapi_display_has_image_format(
1006     GstVaapiDisplay    *display,
1007     GstVaapiImageFormat format
1008 )
1009 {
1010     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1011     g_return_val_if_fail(format, FALSE);
1012
1013     if (find_format(display->priv->image_formats, format))
1014         return TRUE;
1015
1016     /* XXX: try subpicture formats since some drivers could report a
1017      * set of VA image formats that is not a superset of the set of VA
1018      * subpicture formats
1019      */
1020     return find_format(display->priv->subpicture_formats, format);
1021 }
1022
1023 /**
1024  * gst_vaapi_display_get_subpicture_caps:
1025  * @display: a #GstVaapiDisplay
1026  *
1027  * Gets the supported subpicture formats as #GstCaps capabilities.
1028  *
1029  * Note that this method does not necessarily map subpicture formats
1030  * returned by vaQuerySubpictureFormats(). The set of capabilities can
1031  * be stripped down if gstreamer-vaapi does not support the
1032  * format. e.g. this is the case for paletted formats like IA44.
1033  *
1034  * Return value: a newly allocated #GstCaps object, possibly empty
1035  */
1036 GstCaps *
1037 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
1038 {
1039     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1040
1041     return get_format_caps(display->priv->subpicture_formats);
1042 }
1043
1044 /**
1045  * gst_vaapi_display_has_subpicture_format:
1046  * @display: a #GstVaapiDisplay
1047  * @format: a #GstVaapiFormat
1048  *
1049  * Returns whether VA @display supports @format subpicture format.
1050  *
1051  * Return value: %TRUE if VA @display supports @format subpicture format
1052  */
1053 gboolean
1054 gst_vaapi_display_has_subpicture_format(
1055     GstVaapiDisplay    *display,
1056     GstVaapiImageFormat format
1057 )
1058 {
1059     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1060     g_return_val_if_fail(format, FALSE);
1061
1062     return find_format(display->priv->subpicture_formats, format);
1063 }