vaapiplugin: fix gst_vaapi_ensure_display() to use system defaults.
[platform/upstream/gstreamer-vaapi.git] / gst / vaapi / gstvaapipluginutil.c
1 /*
2  *  gstvaapipluginutil.h - VA-API plugin helpers
3  *
4  *  Copyright (C) 2011 Intel Corporation
5  *  Copyright (C) 2011 Collabora
6  *    Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public License
10  *  as published by the Free Software Foundation; either version 2.1
11  *  of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free
20  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  *  Boston, MA 02110-1301 USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "gstvaapipluginutil.h"
29
30 #include <string.h>
31
32 #ifdef USE_VAAPI_GLX
33 #include <gst/vaapi/gstvaapidisplay_glx.h>
34 #else
35 #include <gst/vaapi/gstvaapidisplay_x11.h>
36 #endif
37
38 /* Preferred first */
39 static const char *display_types[] = {
40   "gst-vaapi-display",
41   "vaapi-display",
42   "x11-display",
43   "x11-display-name",
44   NULL
45 };
46
47 gboolean
48 gst_vaapi_ensure_display (gpointer element, GstVaapiDisplay **display)
49 {
50   GstVideoContext *context;
51
52   g_return_val_if_fail (GST_IS_VIDEO_CONTEXT (element), FALSE);
53   g_return_val_if_fail (display != NULL, FALSE);
54
55   /* Already exist ? */
56   if (*display)
57     return TRUE;
58
59   context = GST_VIDEO_CONTEXT (element);
60   gst_video_context_prepare (context, display_types);
61
62   /* If no neighboor, or application not interested, use system default */
63   if (!*display)
64 #if USE_VAAPI_GLX
65     *display = gst_vaapi_display_glx_new (NULL);
66 #else
67     *display = gst_vaapi_display_x11_new (NULL);
68 #endif
69
70   /* FIXME allocator should return NULL in case of failure */
71   if (*display && !gst_vaapi_display_get_display(*display)) {
72     g_object_unref (*display);
73     *display = NULL;
74   }
75
76   return (*display != NULL);
77 }
78
79 void
80 gst_vaapi_set_display (const gchar *type,
81     const GValue *value,
82     GstVaapiDisplay **display)
83 {
84   GstVaapiDisplay *dpy = NULL;
85
86   if (!strcmp (type, "x11-display-name")) {
87     g_return_if_fail (G_VALUE_HOLDS_STRING (value));
88 #if USE_VAAPI_GLX
89     dpy = gst_vaapi_display_glx_new (g_value_get_string (value));
90 #else
91     dpy = gst_vaapi_display_x11_new (g_value_get_string (value));
92 #endif
93   } else if (!strcmp (type, "x11-display")) {
94     g_return_if_fail (G_VALUE_HOLDS_POINTER (value));
95 #if USE_VAAPI_GLX
96     dpy = gst_vaapi_display_glx_new_with_display (g_value_get_pointer (value));
97 #else
98     dpy = gst_vaapi_display_x11_new_with_display (g_value_get_pointer (value));
99 #endif
100   } else if (!strcmp (type, "vaapi-display")) {
101     g_return_if_fail (G_VALUE_HOLDS_POINTER (value));
102     dpy = gst_vaapi_display_new_with_display (g_value_get_pointer (value));
103   } else if (!strcmp (type, "gst-vaapi-display")) {
104     g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
105     dpy = g_value_dup_object (value);
106   }
107
108   if (dpy) {
109     if (*display)
110       g_object_unref (*display);
111     *display = dpy;
112   }
113 }
114
115 gboolean
116 gst_vaapi_reply_to_query (GstQuery *query, GstVaapiDisplay *display)
117 {
118   const gchar **types;
119   const gchar *type;
120   gint i;
121   gboolean res = FALSE;
122
123   if (!display)
124     return FALSE;
125
126   types = gst_video_context_query_get_supported_types (query);
127
128   if (!types)
129     return FALSE;
130
131   for (i = 0; types[i]; i++) {
132     type = types[i];
133
134     if (!strcmp (type, "gst-vaapi-display")) {
135       gst_video_context_query_set_object (query, type, G_OBJECT (display));
136
137     } else if (!strcmp (type, "vaapi-display")) {
138       VADisplay vadpy = gst_vaapi_display_get_display(display);
139       gst_video_context_query_set_pointer (query, type, vadpy);
140
141     } else if (!strcmp (type, "x11-display") &&
142         GST_VAAPI_IS_DISPLAY_X11(display)) {
143       GstVaapiDisplayX11 *xvadpy = GST_VAAPI_DISPLAY_X11 (display);
144       Display *x11dpy = gst_vaapi_display_x11_get_display (xvadpy);
145       gst_video_context_query_set_pointer (query, type, x11dpy);
146
147     } else if (!strcmp (type, "x11-display-name") &&
148         GST_VAAPI_IS_DISPLAY_X11(display)) {
149       GstVaapiDisplayX11 *xvadpy = GST_VAAPI_DISPLAY_X11 (display);
150       Display *x11dpy = gst_vaapi_display_x11_get_display (xvadpy);
151       gst_video_context_query_set_string (query, type, DisplayString(x11dpy));
152
153     } else {
154       continue;
155     }
156
157     res = TRUE;
158     break;
159   }
160
161   return res;
162 }
163
164 gboolean
165 gst_vaapi_append_surface_caps (GstCaps *out_caps, GstCaps *in_caps)
166 {
167   GstStructure *structure;
168   const GValue *v_width, *v_height, *v_framerate, *v_par;
169   guint i, n_structures;
170
171   structure   = gst_caps_get_structure (in_caps, 0);
172   v_width     = gst_structure_get_value (structure, "width");
173   v_height    = gst_structure_get_value (structure, "height");
174   v_framerate = gst_structure_get_value (structure, "framerate");
175   v_par       = gst_structure_get_value (structure, "pixel-aspect-ratio");
176   if (!v_width || !v_height)
177     return FALSE;
178
179   n_structures = gst_caps_get_size (out_caps);
180   for (i = 0; i < n_structures; i++) {
181     structure = gst_caps_get_structure (out_caps, i);
182     gst_structure_set_value (structure, "width", v_width);
183     gst_structure_set_value (structure, "height", v_height);
184     if (v_framerate)
185       gst_structure_set_value (structure, "framerate", v_framerate);
186     if (v_par)
187       gst_structure_set_value (structure, "pixel-aspect-ratio", v_par);
188   }
189   return TRUE;
190 }