Fix seek issue
[profile/ivi/gstreamer-vaapi.git] / tests / test-display.c
1 /*
2  *  test-display.c - Test GstVaapiDisplayX11
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23 #include <gst/video/video.h>
24 #if USE_DRM
25 # include <gst/vaapi/gstvaapidisplay_drm.h>
26 # include <va/va_drm.h>
27 # include <fcntl.h>
28 # include <unistd.h>
29 # ifndef DRM_DEVICE_PATH
30 # define DRM_DEVICE_PATH "/dev/dri/card0"
31 # endif
32 #endif
33 #if USE_X11
34 # include <gst/vaapi/gstvaapidisplay_x11.h>
35 #endif
36 #if USE_GLX
37 # include <gst/vaapi/gstvaapidisplay_glx.h>
38 #endif
39 #if USE_WAYLAND
40 # include <gst/vaapi/gstvaapidisplay_wayland.h>
41 #endif
42
43 #ifdef HAVE_VA_VA_GLX_H
44 # include <va/va_glx.h>
45 #endif
46
47 static void
48 print_value(const GValue *value, const gchar *name)
49 {
50     gchar *value_string;
51
52     value_string = g_strdup_value_contents(value);
53     if (!value_string)
54         return;
55     g_print("  %s: %s\n", name, value_string);
56     g_free(value_string);
57 }
58
59 static void
60 print_profile_caps(GstCaps *caps, const gchar *name)
61 {
62     guint i, n_caps = gst_caps_get_size(caps);
63     gint version;
64     const gchar *profile;
65     gboolean has_version;
66
67     g_print("%u %s caps\n", n_caps, name);
68
69     for (i = 0; i < gst_caps_get_size(caps); i++) {
70         GstStructure * const structure = gst_caps_get_structure(caps, i);
71         if (!structure)
72             g_error("could not get caps structure %d", i);
73
74         has_version = (
75             gst_structure_get_int(structure, "version", &version) ||
76             gst_structure_get_int(structure, "mpegversion", &version)
77         );
78
79         g_print("  %s", gst_structure_get_name(structure));
80         if (has_version)
81             g_print("%d", version);
82
83         profile = gst_structure_get_string(structure, "profile");
84         if (!profile)
85             g_error("could not get structure profile");
86         g_print(": %s profile\n", profile);
87     }
88 }
89
90 static void
91 print_format_caps(GstCaps *caps, const gchar *name)
92 {
93     guint i, n_caps = gst_caps_get_size(caps);
94
95     g_print("%u %s caps\n", n_caps, name);
96
97     for (i = 0; i < gst_caps_get_size(caps); i++) {
98         GstStructure * const structure = gst_caps_get_structure(caps, i);
99         if (!structure)
100             g_error("could not get caps structure %d", i);
101
102         g_print("  %s:", gst_structure_get_name(structure));
103
104         if (gst_structure_has_name(structure, "video/x-raw-yuv")) {
105             guint32 fourcc;
106
107             gst_structure_get_fourcc(structure, "format", &fourcc);
108
109             g_print(" fourcc '%c%c%c%c'",
110                     fourcc & 0xff,
111                     (fourcc >> 8) & 0xff,
112                     (fourcc >> 16) & 0xff,
113                     (fourcc >> 24) & 0xff);
114         }
115         else {
116             gint bpp, endian, rmask, gmask, bmask, amask;
117             gboolean has_alpha;
118
119             gst_structure_get_int(structure, "bpp",         &bpp);
120             gst_structure_get_int(structure, "endianness",  &endian);
121             gst_structure_get_int(structure, "red_mask",    &rmask);
122             gst_structure_get_int(structure, "blue_mask",   &bmask);
123             gst_structure_get_int(structure, "green_mask",  &gmask);
124             has_alpha = gst_structure_get_int(structure, "alpha_mask", &amask);
125
126             g_print(" %d bits per pixel, %s endian,",
127                     bpp, endian == G_BIG_ENDIAN ? "big" : "little");
128             g_print(" %s masks", has_alpha ? "rgba" : "rgb");
129             g_print(" 0x%08x 0x%08x 0x%08x", rmask, gmask, bmask);
130             if (has_alpha)
131                 g_print(" 0x%08x", amask);
132         }
133         g_print("\n");
134     }
135 }
136
137 typedef struct _GstVaapiDisplayProperty GstVaapiDisplayProperty;
138 struct _GstVaapiDisplayProperty {
139     const gchar *name;
140     GValue       value;
141 };
142
143 static void
144 gst_vaapi_display_property_free(GstVaapiDisplayProperty *prop)
145 {
146     if (!prop)
147         return;
148     g_value_unset(&prop->value);
149     g_slice_free(GstVaapiDisplayProperty, prop);
150 }
151
152 static GstVaapiDisplayProperty *
153 gst_vaapi_display_property_new(const gchar *name)
154 {
155     GstVaapiDisplayProperty *prop;
156
157     prop = g_slice_new0(GstVaapiDisplayProperty);
158     if (!prop)
159         return NULL;
160     prop->name = name;
161     return prop;
162 }
163
164 static void
165 free_property_cb(gpointer data, gpointer user_data)
166 {
167     gst_vaapi_display_property_free(data);
168 }
169
170 static inline GParamSpec *
171 get_display_property(GstVaapiDisplay *display, const gchar *name)
172 {
173     GObjectClass *klass;
174
175     klass = G_OBJECT_CLASS(GST_VAAPI_DISPLAY_GET_CLASS(display));
176     if (!klass)
177         return NULL;
178     return g_object_class_find_property(klass, name);
179 }
180
181 static void
182 dump_properties(GstVaapiDisplay *display)
183 {
184     GstVaapiDisplayProperty *prop;
185     GPtrArray *properties;
186     guint i;
187
188     static const gchar *g_properties[] = {
189         GST_VAAPI_DISPLAY_PROP_RENDER_MODE,
190         GST_VAAPI_DISPLAY_PROP_ROTATION,
191         GST_VAAPI_DISPLAY_PROP_HUE,
192         GST_VAAPI_DISPLAY_PROP_SATURATION,
193         GST_VAAPI_DISPLAY_PROP_BRIGHTNESS,
194         GST_VAAPI_DISPLAY_PROP_CONTRAST,
195         NULL
196     };
197
198     properties = g_ptr_array_new();
199     if (!properties)
200         return;
201
202     for (i = 0; g_properties[i] != NULL; i++) {
203         GParamSpec *pspec = get_display_property(display, g_properties[i]);
204
205         if (!pspec) {
206             GST_ERROR("failed to find GstVaapiDisplay property '%s'",
207                       g_properties[i]);
208             goto end;
209         }
210
211         if (!gst_vaapi_display_has_property(display, pspec->name))
212             continue;
213             
214         prop = gst_vaapi_display_property_new(pspec->name);
215         if (!prop) {
216             GST_ERROR("failed to allocate GstVaapiDisplayProperty");
217             goto end;
218         }
219
220         g_value_init(&prop->value, pspec->value_type);
221         g_object_get_property(G_OBJECT(display), pspec->name, &prop->value);
222         g_ptr_array_add(properties, prop);
223     }
224
225     g_print("%u properties\n", properties->len);
226     for (i = 0; i < properties->len; i++) {
227         prop = g_ptr_array_index(properties, i);
228         print_value(&prop->value, prop->name);
229     }
230
231 end:
232     if (properties) {
233         g_ptr_array_foreach(properties, free_property_cb, NULL);
234         g_ptr_array_free(properties, TRUE);
235     }
236 }
237
238 static void
239 dump_info(GstVaapiDisplay *display)
240 {
241     GstCaps *caps;
242
243     caps = gst_vaapi_display_get_decode_caps(display);
244     if (!caps)
245         g_error("could not get VA decode caps");
246
247     print_profile_caps(caps, "decoders");
248     gst_caps_unref(caps);
249
250     caps = gst_vaapi_display_get_encode_caps(display);
251     if (!caps)
252         g_error("could not get VA encode caps");
253
254     print_profile_caps(caps, "encoders");
255     gst_caps_unref(caps);
256
257     caps = gst_vaapi_display_get_image_caps(display);
258     if (!caps)
259         g_error("could not get VA image caps");
260
261     print_format_caps(caps, "image");
262     gst_caps_unref(caps);
263
264     caps = gst_vaapi_display_get_subpicture_caps(display);
265     if (!caps)
266         g_error("could not get VA subpicture caps");
267
268     print_format_caps(caps, "subpicture");
269     gst_caps_unref(caps);
270
271     dump_properties(display);
272 }
273
274 int
275 main(int argc, char *argv[])
276 {
277     GstVaapiDisplay *display;
278     guint width, height, par_n, par_d;
279
280     gst_init(&argc, &argv);
281
282 #if USE_DRM
283     g_print("#\n");
284     g_print("# Create display with gst_vaapi_display_drm_new()\n");
285     g_print("#\n");
286     {
287         display = gst_vaapi_display_drm_new(NULL);
288         if (!display)
289             g_error("could not create Gst/VA display");
290
291         dump_info(display);
292         g_object_unref(display);
293     }
294     g_print("\n");
295
296     g_print("#\n");
297     g_print("# Create display with gst_vaapi_display_drm_new_with_device()\n");
298     g_print("#\n");
299     {
300         int drm_device;
301
302         drm_device = open(DRM_DEVICE_PATH, O_RDWR|O_CLOEXEC);
303         if (drm_device < 0)
304             g_error("could not open DRM device");
305
306         display = gst_vaapi_display_drm_new_with_device(drm_device);
307         if (!display)
308             g_error("could not create Gst/VA display");
309
310         dump_info(display);
311         g_object_unref(display);
312         close(drm_device);
313     }
314     g_print("\n");
315
316     g_print("#\n");
317     g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayDRM()]\n");
318     g_print("#\n");
319     {
320         int drm_device;
321         VADisplay va_display;
322
323         drm_device = open(DRM_DEVICE_PATH, O_RDWR|O_CLOEXEC);
324         if (drm_device < 0)
325             g_error("could not open DRM device");
326
327         va_display = vaGetDisplayDRM(drm_device);
328         if (!va_display)
329             g_error("could not create VA display");
330
331         display = gst_vaapi_display_new_with_display(va_display);
332         if (!display)
333             g_error("could not create Gst/VA display");
334
335         dump_info(display);
336         g_object_unref(display);
337         close(drm_device);
338     }
339     g_print("\n");
340 #endif
341
342 #if USE_X11
343     g_print("#\n");
344     g_print("# Create display with gst_vaapi_display_x11_new()\n");
345     g_print("#\n");
346     {
347         display = gst_vaapi_display_x11_new(NULL);
348         if (!display)
349             g_error("could not create Gst/VA display");
350
351         gst_vaapi_display_get_size(display, &width, &height);
352         g_print("Display size: %ux%u\n", width, height);
353
354         gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
355         g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);
356
357         dump_info(display);
358         g_object_unref(display);
359     }
360     g_print("\n");
361
362     g_print("#\n");
363     g_print("# Create display with gst_vaapi_display_x11_new_with_display()\n");
364     g_print("#\n");
365     {
366         Display *x11_display;
367
368         x11_display = XOpenDisplay(NULL);
369         if (!x11_display)
370             g_error("could not create X11 display");
371
372         display = gst_vaapi_display_x11_new_with_display(x11_display);
373         if (!display)
374             g_error("could not create Gst/VA display");
375
376         dump_info(display);
377         g_object_unref(display);
378         XCloseDisplay(x11_display);
379     }
380     g_print("\n");
381
382     g_print("#\n");
383     g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplay()]\n");
384     g_print("#\n");
385     {
386         Display *x11_display;
387         VADisplay va_display;
388
389         x11_display = XOpenDisplay(NULL);
390         if (!x11_display)
391             g_error("could not create X11 display");
392
393         va_display = vaGetDisplay(x11_display);
394         if (!va_display)
395             g_error("could not create VA display");
396
397         display = gst_vaapi_display_new_with_display(va_display);
398         if (!display)
399             g_error("could not create Gst/VA display");
400
401         dump_info(display);
402         g_object_unref(display);
403         XCloseDisplay(x11_display);
404     }
405     g_print("\n");
406 #endif
407
408 #if USE_GLX
409     g_print("#\n");
410     g_print("# Create display with gst_vaapi_display_glx_new()\n");
411     g_print("#\n");
412     {
413         display = gst_vaapi_display_glx_new(NULL);
414         if (!display)
415             g_error("could not create Gst/VA display");
416
417         gst_vaapi_display_get_size(display, &width, &height);
418         g_print("Display size: %ux%u\n", width, height);
419
420         gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
421         g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);
422
423         dump_info(display);
424         g_object_unref(display);
425     }
426     g_print("\n");
427
428     g_print("#\n");
429     g_print("# Create display with gst_vaapi_display_glx_new_with_display()\n");
430     g_print("#\n");
431     {
432         Display *x11_display;
433
434         x11_display = XOpenDisplay(NULL);
435         if (!x11_display)
436             g_error("could not create X11 display");
437
438         display = gst_vaapi_display_glx_new_with_display(x11_display);
439         if (!display)
440             g_error("could not create Gst/VA display");
441
442         dump_info(display);
443         g_object_unref(display);
444         XCloseDisplay(x11_display);
445     }
446     g_print("\n");
447
448 #ifdef HAVE_VA_VA_GLX_H
449     g_print("#\n");
450     g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayGLX()]\n");
451     g_print("#\n");
452     {
453         Display *x11_display;
454         VADisplay va_display;
455
456         x11_display = XOpenDisplay(NULL);
457         if (!x11_display)
458             g_error("could not create X11 display");
459
460         va_display = vaGetDisplayGLX(x11_display);
461         if (!va_display)
462             g_error("could not create VA display");
463
464         display = gst_vaapi_display_new_with_display(va_display);
465         if (!display)
466             g_error("could not create Gst/VA display");
467
468         dump_info(display);
469         g_object_unref(display);
470         XCloseDisplay(x11_display);
471     }
472     g_print("\n");
473 #endif
474 #endif
475
476 #if USE_WAYLAND
477     g_print("#\n");
478     g_print("# Create display with gst_vaapi_display_wayland_new()\n");
479     g_print("#\n");
480     {
481         display = gst_vaapi_display_wayland_new(NULL);
482         if (!display)
483             g_error("could not create Gst/VA display");
484
485         gst_vaapi_display_get_size(display, &width, &height);
486         g_print("Display size: %ux%u\n", width, height);
487
488         gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
489         g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);
490
491         dump_info(display);
492         g_object_unref(display);
493     }
494     g_print("\n");
495 #endif
496
497     gst_deinit();
498     return 0;
499 }