2 * test-windows.c - Test GstVaapiWindow
4 * gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <gst/vaapi/gstvaapidisplay_x11.h>
22 #include <gst/vaapi/gstvaapiwindow_x11.h>
23 #include <gst/vaapi/gstvaapisurface.h>
24 #include <gst/vaapi/gstvaapiimage.h>
26 static inline void pause(void)
28 g_print("Press any key to continue...\n");
32 typedef void (*DrawRectFunc)(
42 static void draw_rect_NV12( // Y, UV planes
52 const guchar Y = color >> 16;
53 const guchar Cb = color >> 8;
54 const guchar Cr = color;
58 dst = pixels[0] + y * stride[0] + x;
59 for (j = 0; j < height; j++, dst += stride[0])
60 for (i = 0; i < width; i++)
68 dst = pixels[1] + y * stride[1] + x * 2;
69 for (j = 0; j < height; j++, dst += stride[1])
70 for (i = 0; i < width; i++) {
76 static void draw_rect_YV12( // Y, U, V planes
86 const guchar Y = color >> 16;
87 const guchar Cb = color >> 8;
88 const guchar Cr = color;
92 pY = pixels[0] + y * stride[0] + x;
93 for (j = 0; j < height; j++, pY += stride[0])
94 for (i = 0; i < width; i++)
102 pU = pixels[1] + y * stride[1] + x;
103 pV = pixels[2] + y * stride[2] + x;
104 for (j = 0; j < height; j++, pU += stride[1], pV += stride[2])
105 for (i = 0; i < width; i++) {
111 static gboolean draw_rgb_rects(GstVaapiImage *image)
113 GstVaapiImageFormat format = GST_VAAPI_IMAGE_FORMAT(image);
114 guint w = GST_VAAPI_IMAGE_WIDTH(image);
115 guint h = GST_VAAPI_IMAGE_HEIGHT(image);
118 guint32 red_color, green_color, blue_color, black_color;
119 DrawRectFunc draw_rect;
121 if (!gst_vaapi_image_map(image))
125 case GST_VAAPI_IMAGE_NV12:
126 draw_rect = draw_rect_NV12;
127 pixels[0] = gst_vaapi_image_get_plane(image, 0);
128 stride[0] = gst_vaapi_image_get_pitch(image, 0);
129 pixels[1] = gst_vaapi_image_get_plane(image, 1);
130 stride[1] = gst_vaapi_image_get_pitch(image, 1);
132 case GST_VAAPI_IMAGE_YV12:
133 draw_rect = draw_rect_YV12;
134 pixels[0] = gst_vaapi_image_get_plane(image, 0);
135 stride[0] = gst_vaapi_image_get_pitch(image, 0);
136 pixels[1] = gst_vaapi_image_get_plane(image, 2);
137 stride[1] = gst_vaapi_image_get_pitch(image, 2);
138 pixels[2] = gst_vaapi_image_get_plane(image, 1);
139 stride[2] = gst_vaapi_image_get_pitch(image, 1);
141 case GST_VAAPI_IMAGE_I420:
142 draw_rect = draw_rect_YV12;
143 pixels[0] = gst_vaapi_image_get_plane(image, 0);
144 stride[0] = gst_vaapi_image_get_pitch(image, 0);
145 pixels[1] = gst_vaapi_image_get_plane(image, 1);
146 stride[1] = gst_vaapi_image_get_pitch(image, 1);
147 pixels[2] = gst_vaapi_image_get_plane(image, 2);
148 stride[2] = gst_vaapi_image_get_pitch(image, 2);
150 red_color = 0x515af0;
151 green_color = 0x913622;
152 blue_color = 0x29f06e;
153 black_color = 0x108080;
156 gst_vaapi_image_unmap(image);
160 draw_rect(pixels, stride, 0, 0, w/2, h/2, red_color);
161 draw_rect(pixels, stride, w/2, 0, w/2, h/2, green_color);
162 draw_rect(pixels, stride, 0, h/2, w/2, h/2, blue_color);
163 draw_rect(pixels, stride, w/2, h/2, w/2, h/2, black_color);
165 if (!gst_vaapi_image_unmap(image))
172 main(int argc, char *argv[])
174 GstVaapiDisplay *display;
175 GstVaapiWindow *window;
176 GstVaapiSurface *surface;
177 GstVaapiImage *image;
178 guint flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
180 static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
181 static const guint width = 320;
182 static const guint height = 240;
183 static const guint win_width = 640;
184 static const guint win_height = 480;
186 gst_init(&argc, &argv);
188 display = gst_vaapi_display_x11_new(NULL);
190 g_error("could not create Gst/VA display");
192 surface = gst_vaapi_surface_new(display, chroma_type, width, height);
194 g_error("could not create Gst/VA surface");
196 image = gst_vaapi_image_new(display, GST_VAAPI_IMAGE_NV12, width, height);
198 g_error("could not create Gst/VA image");
199 if (!draw_rgb_rects(image))
200 g_error("could not draw RGB rectangles");
202 if (!gst_vaapi_surface_put_image(surface, image))
203 g_error("could not upload image");
204 if (!gst_vaapi_surface_sync(surface))
205 g_error("could not complete image upload");
208 g_print("# Create window with gst_vaapi_window_x11_new()\n");
211 window = gst_vaapi_window_x11_new(display, win_width, win_height);
213 g_error("could not create window");
215 gst_vaapi_window_show(window);
217 if (!gst_vaapi_window_put_surface(window, surface, flags))
218 g_error("could not render surface");
221 g_object_unref(window);
225 g_print("# Create window with gst_vaapi_window_x11_new_with_xid()\n");
228 Display * const dpy = GST_VAAPI_DISPLAY_XDISPLAY(display);
231 unsigned long white_pixel, black_pixel;
233 screen = DefaultScreen(dpy);
234 rootwin = RootWindow(dpy, screen);
235 white_pixel = WhitePixel(dpy, screen);
236 black_pixel = BlackPixel(dpy, screen);
238 win = XCreateSimpleWindow(
241 0, 0, win_width, win_height,
246 g_error("could not create X window");
248 window = gst_vaapi_window_x11_new_with_xid(display, win);
250 g_error("could not create window");
252 gst_vaapi_window_show(window);
254 if (!gst_vaapi_window_put_surface(window, surface, flags))
255 g_error("could not render surface");
258 g_object_unref(window);
259 XUnmapWindow(dpy, win);
260 XDestroyWindow(dpy, win);
263 g_object_unref(image);
264 g_object_unref(surface);
265 g_object_unref(display);