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_ARGB(
54 color = GUINT32_TO_BE(color);
56 for (j = 0; j < height; j++) {
57 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
58 for (i = 0; i < width; i++)
63 static void draw_rect_BGRA(
73 // Converts ARGB color to BGRA
74 color = GUINT32_SWAP_LE_BE(color);
76 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
79 static void draw_rect_RGBA(
89 // Converts ARGB color to RGBA
90 color = ((color >> 24) & 0xff) | ((color & 0xffffff) << 8);
92 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
95 static void draw_rect_ABGR(
105 // Converts ARGB color to ABGR
106 color = ((color & 0xff00ff00) |
107 ((color >> 16) & 0xff) |
108 ((color & 0xff) << 16));
110 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
113 static void draw_rect_NV12( // Y, UV planes
123 const guchar Y = color >> 16;
124 const guchar Cb = color >> 8;
125 const guchar Cr = color;
129 dst = pixels[0] + y * stride[0] + x;
130 for (j = 0; j < height; j++, dst += stride[0])
131 for (i = 0; i < width; i++)
139 dst = pixels[1] + y * stride[1] + x * 2;
140 for (j = 0; j < height; j++, dst += stride[1])
141 for (i = 0; i < width; i++) {
147 static void draw_rect_YV12( // Y, U, V planes
157 const guchar Y = color >> 16;
158 const guchar Cb = color >> 8;
159 const guchar Cr = color;
160 guchar *pY, *pU, *pV;
163 pY = pixels[0] + y * stride[0] + x;
164 for (j = 0; j < height; j++, pY += stride[0])
165 for (i = 0; i < width; i++)
173 pU = pixels[1] + y * stride[1] + x;
174 pV = pixels[2] + y * stride[2] + x;
175 for (j = 0; j < height; j++, pU += stride[1], pV += stride[2])
176 for (i = 0; i < width; i++) {
182 static void draw_rect_AYUV(
194 color = color | 0xff000000;
196 for (j = 0; j < height; j++) {
197 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
198 for (i = 0; i < width; i++)
203 static gboolean draw_rgb_rects(GstVaapiImage *image)
205 GstVaapiImageFormat format = GST_VAAPI_IMAGE_FORMAT(image);
206 guint w = GST_VAAPI_IMAGE_WIDTH(image);
207 guint h = GST_VAAPI_IMAGE_HEIGHT(image);
210 guint32 red_color, green_color, blue_color, black_color;
211 DrawRectFunc draw_rect;
213 if (!gst_vaapi_image_map(image))
217 case GST_VAAPI_IMAGE_ARGB:
218 draw_rect = draw_rect_ARGB;
220 case GST_VAAPI_IMAGE_BGRA:
221 draw_rect = draw_rect_BGRA;
223 case GST_VAAPI_IMAGE_RGBA:
224 draw_rect = draw_rect_RGBA;
226 case GST_VAAPI_IMAGE_ABGR:
227 draw_rect = draw_rect_ABGR;
229 pixels[0] = gst_vaapi_image_get_plane(image, 0);
230 stride[0] = gst_vaapi_image_get_pitch(image, 0);
231 red_color = 0xffff0000;
232 green_color = 0xff00ff00;
233 blue_color = 0xff0000ff;
234 black_color = 0xff000000;
236 case GST_VAAPI_IMAGE_NV12:
237 draw_rect = draw_rect_NV12;
238 pixels[0] = gst_vaapi_image_get_plane(image, 0);
239 stride[0] = gst_vaapi_image_get_pitch(image, 0);
240 pixels[1] = gst_vaapi_image_get_plane(image, 1);
241 stride[1] = gst_vaapi_image_get_pitch(image, 1);
243 case GST_VAAPI_IMAGE_YV12:
244 draw_rect = draw_rect_YV12;
245 pixels[0] = gst_vaapi_image_get_plane(image, 0);
246 stride[0] = gst_vaapi_image_get_pitch(image, 0);
247 pixels[1] = gst_vaapi_image_get_plane(image, 2);
248 stride[1] = gst_vaapi_image_get_pitch(image, 2);
249 pixels[2] = gst_vaapi_image_get_plane(image, 1);
250 stride[2] = gst_vaapi_image_get_pitch(image, 1);
252 case GST_VAAPI_IMAGE_I420:
253 draw_rect = draw_rect_YV12;
254 pixels[0] = gst_vaapi_image_get_plane(image, 0);
255 stride[0] = gst_vaapi_image_get_pitch(image, 0);
256 pixels[1] = gst_vaapi_image_get_plane(image, 1);
257 stride[1] = gst_vaapi_image_get_pitch(image, 1);
258 pixels[2] = gst_vaapi_image_get_plane(image, 2);
259 stride[2] = gst_vaapi_image_get_pitch(image, 2);
261 case GST_VAAPI_IMAGE_AYUV:
262 draw_rect = draw_rect_AYUV;
263 pixels[0] = gst_vaapi_image_get_plane(image, 0);
264 stride[0] = gst_vaapi_image_get_pitch(image, 0);
266 red_color = 0x515af0;
267 green_color = 0x913622;
268 blue_color = 0x29f06e;
269 black_color = 0x108080;
272 gst_vaapi_image_unmap(image);
276 draw_rect(pixels, stride, 0, 0, w/2, h/2, red_color);
277 draw_rect(pixels, stride, w/2, 0, w/2, h/2, green_color);
278 draw_rect(pixels, stride, 0, h/2, w/2, h/2, blue_color);
279 draw_rect(pixels, stride, w/2, h/2, w/2, h/2, black_color);
281 if (!gst_vaapi_image_unmap(image))
288 upload_image(GstVaapiSurface *surface, GstVaapiImage *image)
290 GstVaapiDisplay *display;
291 GstVaapiImageFormat format;
292 GstVaapiSubpicture *subpicture;
294 display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(surface));
298 format = gst_vaapi_image_get_format(image);
302 if (gst_vaapi_surface_put_image(surface, image))
305 g_print("could not upload %" GST_FOURCC_FORMAT" image to surface\n",
306 GST_FOURCC_ARGS(format));
308 if (!gst_vaapi_display_has_subpicture_format(display, format))
311 g_print("trying as a subpicture\n");
313 subpicture = gst_vaapi_subpicture_new(image);
315 g_error("could not create Gst/VA subpicture");
317 if (!gst_vaapi_surface_associate_subpicture(surface, subpicture,
319 g_error("could not associate subpicture to surface");
321 /* The surface holds a reference to the subpicture. This is safe */
322 g_object_unref(subpicture);
327 main(int argc, char *argv[])
329 GstVaapiDisplay *display;
330 GstVaapiWindow *window;
331 GstVaapiSurface *surface;
332 GstVaapiImage *image = NULL;
333 guint flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
336 static const GstVaapiImageFormat image_formats[] = {
337 GST_VAAPI_IMAGE_NV12,
338 GST_VAAPI_IMAGE_YV12,
339 GST_VAAPI_IMAGE_I420,
340 GST_VAAPI_IMAGE_AYUV,
341 GST_VAAPI_IMAGE_ARGB,
342 GST_VAAPI_IMAGE_BGRA,
343 GST_VAAPI_IMAGE_RGBA,
344 GST_VAAPI_IMAGE_ABGR,
348 static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
349 static const guint width = 320;
350 static const guint height = 240;
351 static const guint win_width = 640;
352 static const guint win_height = 480;
354 gst_init(&argc, &argv);
356 display = gst_vaapi_display_x11_new(NULL);
358 g_error("could not create Gst/VA display");
360 surface = gst_vaapi_surface_new(display, chroma_type, width, height);
362 g_error("could not create Gst/VA surface");
364 for (i = 0; image_formats[i]; i++) {
365 const GstVaapiImageFormat format = image_formats[i];
367 image = gst_vaapi_image_new(display, format, width, height);
371 if (!draw_rgb_rects(image))
372 g_error("could not draw RGB rectangles");
374 if (upload_image(surface, image))
378 g_error("could not create Gst/VA image");
380 if (!gst_vaapi_surface_sync(surface))
381 g_error("could not complete image upload");
384 g_print("# Create window with gst_vaapi_window_x11_new()\n");
387 window = gst_vaapi_window_x11_new(display, win_width, win_height);
389 g_error("could not create window");
391 gst_vaapi_window_show(window);
393 if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
394 g_error("could not render surface");
397 g_object_unref(window);
401 g_print("# Create window with gst_vaapi_window_x11_new_with_xid()\n");
404 Display * const dpy = GST_VAAPI_DISPLAY_XDISPLAY(display);
407 unsigned long white_pixel, black_pixel;
409 screen = DefaultScreen(dpy);
410 rootwin = RootWindow(dpy, screen);
411 white_pixel = WhitePixel(dpy, screen);
412 black_pixel = BlackPixel(dpy, screen);
414 win = XCreateSimpleWindow(
417 0, 0, win_width, win_height,
422 g_error("could not create X window");
424 window = gst_vaapi_window_x11_new_with_xid(display, win);
426 g_error("could not create window");
428 gst_vaapi_window_show(window);
430 if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
431 g_error("could not render surface");
434 g_object_unref(window);
435 XUnmapWindow(dpy, win);
436 XDestroyWindow(dpy, win);
439 g_object_unref(image);
440 g_object_unref(surface);
441 g_object_unref(display);