tests: image: fix string representation for GstVideoFormat.
[platform/upstream/gstreamer-vaapi.git] / tests / output.c
1 /*
2  *  output.c - Video output helpers
3  *
4  *  Copyright (C) 2012-2013 Intel Corporation
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 "gst/vaapi/sysdeps.h"
23 #include <string.h>
24 #include <gst/gst.h>
25 #if USE_DRM
26 # include <gst/vaapi/gstvaapidisplay_drm.h>
27 # include <gst/vaapi/gstvaapiwindow_drm.h>
28 #endif
29 #if USE_X11
30 # include <gst/vaapi/gstvaapidisplay_x11.h>
31 # include <gst/vaapi/gstvaapiwindow_x11.h>
32 # include <gst/vaapi/gstvaapipixmap_x11.h>
33 #endif
34 #if USE_GLX
35 # include <gst/vaapi/gstvaapidisplay_glx.h>
36 # include <gst/vaapi/gstvaapiwindow_glx.h>
37 #endif
38 #if USE_WAYLAND
39 # include <gst/vaapi/gstvaapidisplay_wayland.h>
40 # include <gst/vaapi/gstvaapiwindow_wayland.h>
41 #endif
42 #include "output.h"
43
44 static const VideoOutputInfo *g_video_output;
45 static const VideoOutputInfo g_video_outputs[] = {
46     /* Video outputs are sorted in test order for automatic characterisation */
47 #if USE_WAYLAND
48     { "wayland",
49       gst_vaapi_display_wayland_new,
50       gst_vaapi_window_wayland_new
51     },
52 #endif
53 #if USE_X11
54     { "x11",
55       gst_vaapi_display_x11_new,
56       gst_vaapi_window_x11_new,
57       gst_vaapi_pixmap_x11_new
58     },
59 #endif
60 #if USE_GLX
61     { "glx",
62       gst_vaapi_display_glx_new,
63       gst_vaapi_window_glx_new,
64       gst_vaapi_pixmap_x11_new
65     },
66 #endif
67 #if USE_DRM
68     { "drm",
69       gst_vaapi_display_drm_new,
70       gst_vaapi_window_drm_new
71     },
72 #endif
73     { NULL, }
74 };
75
76 static gchar *g_output_name;
77 static gboolean g_list_outputs = FALSE;
78 static gboolean g_fullscreen = FALSE;
79
80 static GOptionEntry g_options[] = {
81     { "list-outputs", 0,
82       0,
83       G_OPTION_ARG_NONE, &g_list_outputs,
84       "list video outputs", NULL },
85     { "output", 'o',
86       0,
87       G_OPTION_ARG_STRING, &g_output_name,
88       "video output name", NULL },
89     { "fullscreen", 'f',
90       0,
91       G_OPTION_ARG_NONE, &g_fullscreen,
92       "fullscreen mode", NULL },
93     { NULL, }
94 };
95
96 static void
97 list_outputs(void)
98 {
99     const VideoOutputInfo *o;
100
101     g_print("Video outputs:");
102     for (o = g_video_outputs; o->name != NULL; o++)
103         g_print(" %s", o->name);
104     g_print("\n");
105 }
106
107 gboolean
108 video_output_init(int *argc, char *argv[], GOptionEntry *options)
109 {
110     GOptionContext *ctx;
111     gboolean success;
112
113 #if !GLIB_CHECK_VERSION(2,31,0)
114     if (!g_thread_supported())
115         g_thread_init(NULL);
116 #endif
117
118     ctx = g_option_context_new("- test options");
119     if (!ctx)
120         return FALSE;
121
122     g_option_context_add_group(ctx, gst_init_get_option_group());
123     g_option_context_add_main_entries(ctx, g_options, NULL);
124     if (options)
125         g_option_context_add_main_entries(ctx, options, NULL);
126     success = g_option_context_parse(ctx, argc, &argv, NULL);
127     g_option_context_free(ctx);
128
129     if (g_list_outputs) {
130         list_outputs();
131         exit(0);
132     }
133     return success;
134 }
135
136 void
137 video_output_exit(void)
138 {
139     g_free(g_output_name);
140     gst_deinit();
141 }
142
143 const VideoOutputInfo *
144 video_output_lookup(const gchar *output_name)
145 {
146     const VideoOutputInfo *o;
147
148     if (!output_name)
149         return NULL;
150
151     for (o = g_video_outputs; o->name != NULL; o++) {
152         if (g_ascii_strcasecmp(o->name, output_name) == 0)
153             return o;
154     }
155     return NULL;
156 }
157
158 GstVaapiDisplay *
159 video_output_create_display(const gchar *display_name)
160 {
161     const VideoOutputInfo *o = g_video_output;
162     GstVaapiDisplay *display = NULL;
163
164     if (!o) {
165         if (g_output_name)
166             o = video_output_lookup(g_output_name);
167         else {
168             for (o = g_video_outputs; o->name != NULL; o++) {
169                 display = o->create_display(display_name);
170                 if (display) {
171                     if (gst_vaapi_display_get_display(display))
172                         break;
173                     gst_vaapi_display_unref(display);
174                     display = NULL;
175                 }
176             }
177         }
178         if (!o || !o->name)
179             return NULL;
180         g_print("Using %s video output\n", o->name);
181         g_video_output = o;
182     }
183
184     if (!display)
185         display = o->create_display(display_name);
186     return display;
187 }
188
189 GstVaapiWindow *
190 video_output_create_window(GstVaapiDisplay *display, guint width, guint height)
191 {
192     GstVaapiWindow *window;
193
194     if (!g_video_output)
195         return NULL;
196
197     window = g_video_output->create_window(display, width, height);
198     if (!window)
199         return NULL;
200
201     /* Force fullscreen mode, should this be requested by the user */
202     if (g_fullscreen)
203         gst_vaapi_window_set_fullscreen(window, TRUE);
204     return window;
205 }
206
207 GstVaapiPixmap *
208 video_output_create_pixmap(GstVaapiDisplay *display, GstVideoFormat format,
209     guint width, guint height)
210 {
211     if (!g_video_output || !g_video_output->create_pixmap)
212         return NULL;
213     return g_video_output->create_pixmap(display, format, width, height);
214 }