glib: map deprecated API to glib >= 2.32 equivalents.
[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     g_static_rec_mutex_free(&priv->mutex);
365 }
366
367 static gboolean
368 gst_vaapi_display_create(GstVaapiDisplay *display)
369 {
370     GstVaapiDisplayPrivate * const priv = display->priv;
371     GstVaapiDisplayCache *cache;
372     gboolean            has_errors      = TRUE;
373     VAProfile          *profiles        = NULL;
374     VAEntrypoint       *entrypoints     = NULL;
375     VAImageFormat      *formats         = NULL;
376     unsigned int       *flags           = NULL;
377     gint                i, j, n, num_entrypoints, major_version, minor_version;
378     VAStatus            status;
379     GstVaapiDisplayInfo info;
380     const GstVaapiDisplayInfo *cached_info = NULL;
381
382     memset(&info, 0, sizeof(info));
383     info.display = display;
384
385     if (priv->display)
386         info.va_display = priv->display;
387     else if (priv->create_display) {
388         GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
389         if (klass->open_display && !klass->open_display(display))
390             return FALSE;
391         if (!klass->get_display || !klass->get_display(display, &info))
392             return FALSE;
393         priv->display = info.va_display;
394         if (klass->get_size)
395             klass->get_size(display, &priv->width, &priv->height);
396         if (klass->get_size_mm)
397             klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
398         gst_vaapi_display_calculate_pixel_aspect_ratio(display);
399     }
400     if (!priv->display)
401         return FALSE;
402
403     cache = get_display_cache();
404     if (!cache)
405         return FALSE;
406     cached_info = gst_vaapi_display_cache_lookup_by_va_display(
407         cache,
408         info.va_display
409     );
410     if (cached_info) {
411         if (priv->parent)
412             g_object_unref(priv->parent);
413         priv->parent = g_object_ref(cached_info->display);
414     }
415
416     if (!priv->parent) {
417         status = vaInitialize(priv->display, &major_version, &minor_version);
418         if (!vaapi_check_status(status, "vaInitialize()"))
419             goto end;
420         GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
421     }
422
423     /* VA profiles */
424     profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
425     if (!profiles)
426         goto end;
427     entrypoints = g_new(VAEntrypoint, vaMaxNumEntrypoints(priv->display));
428     if (!entrypoints)
429         goto end;
430     status = vaQueryConfigProfiles(priv->display, profiles, &n);
431     if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
432         goto end;
433
434     GST_DEBUG("%d profiles", n);
435     for (i = 0; i < n; i++)
436         GST_DEBUG("  %s", string_of_VAProfile(profiles[i]));
437
438     priv->decoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
439     if (!priv->decoders)
440         goto end;
441     priv->encoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
442     if (!priv->encoders)
443         goto end;
444
445     for (i = 0; i < n; i++) {
446         GstVaapiConfig config;
447
448         config.profile = gst_vaapi_profile(profiles[i]);
449         if (!config.profile)
450             continue;
451
452         status = vaQueryConfigEntrypoints(
453             priv->display,
454             profiles[i],
455             entrypoints, &num_entrypoints
456         );
457         if (!vaapi_check_status(status, "vaQueryConfigEntrypoints()"))
458             continue;
459
460         for (j = 0; j < num_entrypoints; j++) {
461             config.entrypoint = gst_vaapi_entrypoint(entrypoints[j]);
462             switch (config.entrypoint) {
463             case GST_VAAPI_ENTRYPOINT_VLD:
464             case GST_VAAPI_ENTRYPOINT_IDCT:
465             case GST_VAAPI_ENTRYPOINT_MOCO:
466                 g_array_append_val(priv->decoders, config);
467                 break;
468             case GST_VAAPI_ENTRYPOINT_SLICE_ENCODE:
469                 g_array_append_val(priv->encoders, config);
470                 break;
471             }
472         }
473     }
474     append_h263_config(priv->decoders);
475
476     /* VA image formats */
477     formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
478     if (!formats)
479         goto end;
480     status = vaQueryImageFormats(priv->display, formats, &n);
481     if (!vaapi_check_status(status, "vaQueryImageFormats()"))
482         goto end;
483
484     GST_DEBUG("%d image formats", n);
485     for (i = 0; i < n; i++)
486         GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
487
488     priv->image_formats =
489         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
490     if (!priv->image_formats)
491         goto end;
492     append_formats(priv->image_formats, formats, n);
493     g_array_sort(priv->image_formats, compare_yuv_formats);
494
495     /* VA subpicture formats */
496     n = vaMaxNumSubpictureFormats(priv->display);
497     formats = g_renew(VAImageFormat, formats, n);
498     flags   = g_new(guint, n);
499     if (!formats || !flags)
500         goto end;
501     status = vaQuerySubpictureFormats(priv->display, formats, flags, (guint *)&n);
502     if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
503         goto end;
504
505     GST_DEBUG("%d subpicture formats", n);
506     for (i = 0; i < n; i++)
507         GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
508
509     priv->subpicture_formats =
510         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
511     if (!priv->subpicture_formats)
512         goto end;
513     append_formats(priv->subpicture_formats, formats, n);
514     g_array_sort(priv->subpicture_formats, compare_rgb_formats);
515
516     if (!cached_info) {
517         if (!gst_vaapi_display_cache_add(cache, &info))
518             goto end;
519     }
520
521     has_errors = FALSE;
522 end:
523     g_free(profiles);
524     g_free(entrypoints);
525     g_free(formats);
526     g_free(flags);
527     return !has_errors;
528 }
529
530 static void
531 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
532 {
533     GstVaapiDisplayPrivate *priv = display->priv;
534
535     if (priv->parent)
536         priv = priv->parent->priv;
537     g_static_rec_mutex_lock(&priv->mutex);
538 }
539
540 static void
541 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
542 {
543     GstVaapiDisplayPrivate *priv = display->priv;
544
545     if (priv->parent)
546         priv = priv->parent->priv;
547     g_static_rec_mutex_unlock(&priv->mutex);
548 }
549
550 static void
551 gst_vaapi_display_finalize(GObject *object)
552 {
553     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
554
555     gst_vaapi_display_destroy(display);
556
557     G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
558 }
559
560 static void
561 gst_vaapi_display_set_property(
562     GObject      *object,
563     guint         prop_id,
564     const GValue *value,
565     GParamSpec   *pspec
566 )
567 {
568     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
569
570     switch (prop_id) {
571     case PROP_DISPLAY:
572         display->priv->display = g_value_get_pointer(value);
573         break;
574     default:
575         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
576         break;
577     }
578 }
579
580 static void
581 gst_vaapi_display_get_property(
582     GObject    *object,
583     guint       prop_id,
584     GValue     *value,
585     GParamSpec *pspec
586 )
587 {
588     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
589
590     switch (prop_id) {
591     case PROP_DISPLAY:
592         g_value_set_pointer(value, gst_vaapi_display_get_display(display));
593         break;
594     case PROP_WIDTH:
595         g_value_set_uint(value, gst_vaapi_display_get_width(display));
596         break;
597     case PROP_HEIGHT:
598         g_value_set_uint(value, gst_vaapi_display_get_height(display));
599         break;
600     default:
601         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
602         break;
603     }
604 }
605
606 static void
607 gst_vaapi_display_constructed(GObject *object)
608 {
609     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
610     GObjectClass *parent_class;
611
612     display->priv->create_display = display->priv->display == NULL;
613     if (!gst_vaapi_display_create(display))
614         gst_vaapi_display_destroy(display);
615
616     parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
617     if (parent_class->constructed)
618         parent_class->constructed(object);
619 }
620
621 static void
622 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
623 {
624     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
625     GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
626
627     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
628
629     g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
630
631     object_class->finalize      = gst_vaapi_display_finalize;
632     object_class->set_property  = gst_vaapi_display_set_property;
633     object_class->get_property  = gst_vaapi_display_get_property;
634     object_class->constructed   = gst_vaapi_display_constructed;
635
636     dpy_class->lock             = gst_vaapi_display_lock_default;
637     dpy_class->unlock           = gst_vaapi_display_unlock_default;
638
639     g_object_class_install_property
640         (object_class,
641          PROP_DISPLAY,
642          g_param_spec_pointer("display",
643                               "VA display",
644                               "VA display",
645                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
646
647     g_object_class_install_property
648         (object_class,
649          PROP_WIDTH,
650          g_param_spec_uint("width",
651                            "Width",
652                            "The display width",
653                            1, G_MAXUINT32, 1,
654                            G_PARAM_READABLE));
655
656     g_object_class_install_property
657         (object_class,
658          PROP_HEIGHT,
659          g_param_spec_uint("height",
660                            "height",
661                            "The display height",
662                            1, G_MAXUINT32, 1,
663                            G_PARAM_READABLE));
664 }
665
666 static void
667 gst_vaapi_display_init(GstVaapiDisplay *display)
668 {
669     GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
670
671     display->priv               = priv;
672     priv->parent                = NULL;
673     priv->display               = NULL;
674     priv->width                 = 0;
675     priv->height                = 0;
676     priv->width_mm              = 0;
677     priv->height_mm             = 0;
678     priv->par_n                 = 1;
679     priv->par_d                 = 1;
680     priv->decoders              = NULL;
681     priv->encoders              = NULL;
682     priv->image_formats         = NULL;
683     priv->subpicture_formats    = NULL;
684     priv->create_display        = TRUE;
685
686     g_static_rec_mutex_init(&priv->mutex);
687 }
688
689 /**
690  * gst_vaapi_display_new_with_display:
691  * @va_display: a #VADisplay
692  *
693  * Creates a new #GstVaapiDisplay, using @va_display as the VA
694  * display.
695  *
696  * Return value: the newly created #GstVaapiDisplay object
697  */
698 GstVaapiDisplay *
699 gst_vaapi_display_new_with_display(VADisplay va_display)
700 {
701     GstVaapiDisplayCache * const cache = get_display_cache();
702     const GstVaapiDisplayInfo *info;
703
704     g_return_val_if_fail(va_display != NULL, NULL);
705     g_return_val_if_fail(cache != NULL, NULL);
706
707     info = gst_vaapi_display_cache_lookup_by_va_display(cache, va_display);
708     if (info)
709         return g_object_ref(info->display);
710
711     return g_object_new(GST_VAAPI_TYPE_DISPLAY,
712                         "display", va_display,
713                         NULL);
714 }
715
716 /**
717  * gst_vaapi_display_lock:
718  * @display: a #GstVaapiDisplay
719  *
720  * Locks @display. If @display is already locked by another thread,
721  * the current thread will block until @display is unlocked by the
722  * other thread.
723  */
724 void
725 gst_vaapi_display_lock(GstVaapiDisplay *display)
726 {
727     GstVaapiDisplayClass *klass;
728
729     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
730
731     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
732     if (klass->lock)
733         klass->lock(display);
734 }
735
736 /**
737  * gst_vaapi_display_unlock:
738  * @display: a #GstVaapiDisplay
739  *
740  * Unlocks @display. If another thread is blocked in a
741  * gst_vaapi_display_lock() call for @display, it will be woken and
742  * can lock @display itself.
743  */
744 void
745 gst_vaapi_display_unlock(GstVaapiDisplay *display)
746 {
747     GstVaapiDisplayClass *klass;
748
749     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
750
751     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
752     if (klass->unlock)
753         klass->unlock(display);
754 }
755
756 /**
757  * gst_vaapi_display_sync:
758  * @display: a #GstVaapiDisplay
759  *
760  * Flushes any requests queued for the windowing system and waits until
761  * all requests have been handled. This is often used for making sure
762  * that the display is synchronized with the current state of the program.
763  *
764  * This is most useful for X11. On windowing systems where requests are
765  * handled synchronously, this function will do nothing.
766  */
767 void
768 gst_vaapi_display_sync(GstVaapiDisplay *display)
769 {
770     GstVaapiDisplayClass *klass;
771
772     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
773
774     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
775     if (klass->sync)
776         klass->sync(display);
777     else if (klass->flush)
778         klass->flush(display);
779 }
780
781 /**
782  * gst_vaapi_display_flush:
783  * @display: a #GstVaapiDisplay
784  *
785  * Flushes any requests queued for the windowing system.
786  *
787  * This is most useful for X11. On windowing systems where requests
788  * are handled synchronously, this function will do nothing.
789  */
790 void
791 gst_vaapi_display_flush(GstVaapiDisplay *display)
792 {
793     GstVaapiDisplayClass *klass;
794
795     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
796
797     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
798     if (klass->flush)
799         klass->flush(display);
800 }
801
802 /**
803  * gst_vaapi_display_get_display:
804  * @display: a #GstVaapiDisplay
805  *
806  * Returns the #VADisplay bound to @display.
807  *
808  * Return value: the #VADisplay
809  */
810 VADisplay
811 gst_vaapi_display_get_display(GstVaapiDisplay *display)
812 {
813     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
814
815     return display->priv->display;
816 }
817
818 /**
819  * gst_vaapi_display_get_width:
820  * @display: a #GstVaapiDisplay
821  *
822  * Retrieves the width of a #GstVaapiDisplay.
823  *
824  * Return value: the width of the @display, in pixels
825  */
826 guint
827 gst_vaapi_display_get_width(GstVaapiDisplay *display)
828 {
829     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
830
831     return display->priv->width;
832 }
833
834 /**
835  * gst_vaapi_display_get_height:
836  * @display: a #GstVaapiDisplay
837  *
838  * Retrieves the height of a #GstVaapiDisplay
839  *
840  * Return value: the height of the @display, in pixels
841  */
842 guint
843 gst_vaapi_display_get_height(GstVaapiDisplay *display)
844 {
845     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
846
847     return display->priv->height;
848 }
849
850 /**
851  * gst_vaapi_display_get_size:
852  * @display: a #GstVaapiDisplay
853  * @pwidth: return location for the width, or %NULL
854  * @pheight: return location for the height, or %NULL
855  *
856  * Retrieves the dimensions of a #GstVaapiDisplay.
857  */
858 void
859 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
860 {
861     g_return_if_fail(GST_VAAPI_DISPLAY(display));
862
863     if (pwidth)
864         *pwidth = display->priv->width;
865
866     if (pheight)
867         *pheight = display->priv->height;
868 }
869
870 /**
871  * gst_vaapi_display_get_pixel_aspect_ratio:
872  * @display: a #GstVaapiDisplay
873  * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
874  * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
875  *
876  * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
877  */
878 void
879 gst_vaapi_display_get_pixel_aspect_ratio(
880     GstVaapiDisplay *display,
881     guint           *par_n,
882     guint           *par_d
883 )
884 {
885     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
886
887     if (par_n)
888         *par_n = display->priv->par_n;
889
890     if (par_d)
891         *par_d = display->priv->par_d;
892 }
893
894 /**
895  * gst_vaapi_display_get_decode_caps:
896  * @display: a #GstVaapiDisplay
897  *
898  * Gets the supported profiles for decoding as #GstCaps capabilities.
899  *
900  * Return value: a newly allocated #GstCaps object, possibly empty
901  */
902 GstCaps *
903 gst_vaapi_display_get_decode_caps(GstVaapiDisplay *display)
904 {
905     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
906
907     return get_profile_caps(display->priv->decoders);
908 }
909
910 /**
911  * gst_vaapi_display_has_decoder:
912  * @display: a #GstVaapiDisplay
913  * @profile: a #VAProfile
914  * @entrypoint: a #GstVaaiEntrypoint
915  *
916  * Returns whether VA @display supports @profile for decoding at the
917  * specified @entrypoint.
918  *
919  * Return value: %TRUE if VA @display supports @profile for decoding.
920  */
921 gboolean
922 gst_vaapi_display_has_decoder(
923     GstVaapiDisplay    *display,
924     GstVaapiProfile     profile,
925     GstVaapiEntrypoint  entrypoint
926 )
927 {
928     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
929
930     return find_config(display->priv->decoders, profile, entrypoint);
931 }
932
933 /**
934  * gst_vaapi_display_get_encode_caps:
935  * @display: a #GstVaapiDisplay
936  *
937  * Gets the supported profiles for decoding as #GstCaps capabilities.
938  *
939  * Return value: a newly allocated #GstCaps object, possibly empty
940  */
941 GstCaps *
942 gst_vaapi_display_get_encode_caps(GstVaapiDisplay *display)
943 {
944     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
945
946     return get_profile_caps(display->priv->encoders);
947 }
948
949 /**
950  * gst_vaapi_display_has_encoder:
951  * @display: a #GstVaapiDisplay
952  * @profile: a #VAProfile
953  * @entrypoint: a #GstVaapiEntrypoint
954  *
955  * Returns whether VA @display supports @profile for encoding at the
956  * specified @entrypoint.
957  *
958  * Return value: %TRUE if VA @display supports @profile for encoding.
959  */
960 gboolean
961 gst_vaapi_display_has_encoder(
962     GstVaapiDisplay    *display,
963     GstVaapiProfile     profile,
964     GstVaapiEntrypoint  entrypoint
965 )
966 {
967     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
968
969     return find_config(display->priv->encoders, profile, entrypoint);
970 }
971
972 /**
973  * gst_vaapi_display_get_image_caps:
974  * @display: a #GstVaapiDisplay
975  *
976  * Gets the supported image formats for gst_vaapi_surface_get_image()
977  * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
978  *
979  * Note that this method does not necessarily map image formats
980  * returned by vaQueryImageFormats(). The set of capabilities can be
981  * stripped down, if gstreamer-vaapi does not support the format, or
982  * expanded to cover compatible formats not exposed by the underlying
983  * driver. e.g. I420 can be supported even if the driver only exposes
984  * YV12.
985  *
986  * Return value: a newly allocated #GstCaps object, possibly empty
987  */
988 GstCaps *
989 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
990 {
991     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
992
993     return get_format_caps(display->priv->image_formats);
994 }
995
996 /**
997  * gst_vaapi_display_has_image_format:
998  * @display: a #GstVaapiDisplay
999  * @format: a #GstVaapiFormat
1000  *
1001  * Returns whether VA @display supports @format image format.
1002  *
1003  * Return value: %TRUE if VA @display supports @format image format
1004  */
1005 gboolean
1006 gst_vaapi_display_has_image_format(
1007     GstVaapiDisplay    *display,
1008     GstVaapiImageFormat format
1009 )
1010 {
1011     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1012     g_return_val_if_fail(format, FALSE);
1013
1014     if (find_format(display->priv->image_formats, format))
1015         return TRUE;
1016
1017     /* XXX: try subpicture formats since some drivers could report a
1018      * set of VA image formats that is not a superset of the set of VA
1019      * subpicture formats
1020      */
1021     return find_format(display->priv->subpicture_formats, format);
1022 }
1023
1024 /**
1025  * gst_vaapi_display_get_subpicture_caps:
1026  * @display: a #GstVaapiDisplay
1027  *
1028  * Gets the supported subpicture formats as #GstCaps capabilities.
1029  *
1030  * Note that this method does not necessarily map subpicture formats
1031  * returned by vaQuerySubpictureFormats(). The set of capabilities can
1032  * be stripped down if gstreamer-vaapi does not support the
1033  * format. e.g. this is the case for paletted formats like IA44.
1034  *
1035  * Return value: a newly allocated #GstCaps object, possibly empty
1036  */
1037 GstCaps *
1038 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
1039 {
1040     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1041
1042     return get_format_caps(display->priv->subpicture_formats);
1043 }
1044
1045 /**
1046  * gst_vaapi_display_has_subpicture_format:
1047  * @display: a #GstVaapiDisplay
1048  * @format: a #GstVaapiFormat
1049  *
1050  * Returns whether VA @display supports @format subpicture format.
1051  *
1052  * Return value: %TRUE if VA @display supports @format subpicture format
1053  */
1054 gboolean
1055 gst_vaapi_display_has_subpicture_format(
1056     GstVaapiDisplay    *display,
1057     GstVaapiImageFormat format
1058 )
1059 {
1060     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1061     g_return_val_if_fail(format, FALSE);
1062
1063     return find_format(display->priv->subpicture_formats, format);
1064 }