2 * image.c - Image utilities for the tests
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
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.
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.
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
26 GstVaapiDisplay *display,
27 GstVaapiImageFormat format,
32 const guint w = width;
33 const guint h = height;
36 image = gst_vaapi_image_new(display, format, w, h);
40 if (image_draw_rectangle(image, 0, 0, w/2, h/2, 0xffff0000) &&
41 image_draw_rectangle(image, w/2, 0, w/2, h/2, 0xff00ff00) &&
42 image_draw_rectangle(image, 0, h/2, w/2, h/2, 0xff0000ff) &&
43 image_draw_rectangle(image, w/2, h/2, w/2, h/2, 0xff000000))
46 g_object_unref(image);
50 typedef void (*DrawRectFunc)(
60 static void draw_rect_ARGB(
72 color = GUINT32_TO_BE(color);
74 for (j = 0; j < height; j++) {
75 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
76 for (i = 0; i < width; i++)
81 static void draw_rect_BGRA(
91 // Converts ARGB color to BGRA
92 color = GUINT32_SWAP_LE_BE(color);
94 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
97 static void draw_rect_RGBA(
107 // Converts ARGB color to RGBA
108 color = ((color >> 24) & 0xff) | ((color & 0xffffff) << 8);
110 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
113 static void draw_rect_ABGR(
123 // Converts ARGB color to ABGR
124 color = ((color & 0xff00ff00) |
125 ((color >> 16) & 0xff) |
126 ((color & 0xff) << 16));
128 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
131 static void draw_rect_NV12( // Y, UV planes
141 const guchar Y = color >> 16;
142 const guchar Cb = color >> 8;
143 const guchar Cr = color;
147 dst = pixels[0] + y * stride[0] + x;
148 for (j = 0; j < height; j++, dst += stride[0])
149 for (i = 0; i < width; i++)
157 dst = pixels[1] + y * stride[1] + x * 2;
158 for (j = 0; j < height; j++, dst += stride[1])
159 for (i = 0; i < width; i++) {
165 static void draw_rect_YV12( // Y, V, U planes
175 const guchar Y = color >> 16;
176 const guchar Cb = color >> 8;
177 const guchar Cr = color;
178 guchar *pY, *pU, *pV;
181 pY = pixels[0] + y * stride[0] + x;
182 for (j = 0; j < height; j++, pY += stride[0])
183 for (i = 0; i < width; i++)
191 pU = pixels[1] + y * stride[1] + x;
192 pV = pixels[2] + y * stride[2] + x;
193 for (j = 0; j < height; j++, pU += stride[1], pV += stride[2])
194 for (i = 0; i < width; i++) {
200 static void draw_rect_I420( // Y, U, V planes
210 guchar *new_pixels[3] = { pixels[0], pixels[2], pixels[1] };
211 guint new_stride[3] = { stride[0], stride[2], stride[1] };
213 draw_rect_YV12(new_pixels, new_stride, x, y, width, height, color);
216 static void draw_rect_AYUV(
228 color = color | 0xff000000;
230 for (j = 0; j < height; j++) {
231 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
232 for (i = 0; i < width; i++)
237 static inline guint32 argb2yuv(guint32 color)
239 const gint32 r = (color >> 16) & 0xff;
240 const gint32 g = (color >> 8) & 0xff;
241 const gint32 b = (color ) & 0xff;
243 const guint32 y = (( 263 * r + 516 * g + 100 * b) >> 10) + 16;
244 const guint32 u = ((-152 * r - 298 * g + 450 * b) >> 10) + 128;
245 const guint32 v = (( 450 * r - 376 * g - 73 * b) >> 10) + 128;
247 return (y << 16) | (u << 8) | v;
251 image_draw_rectangle(
252 GstVaapiImage *image,
260 const GstVaapiImageFormat image_format = gst_vaapi_image_get_format(image);
261 const guint image_width = gst_vaapi_image_get_width(image);
262 const guint image_height = gst_vaapi_image_get_height(image);
263 GstVaapiDisplay *display;
266 DrawRectFunc draw_rect = NULL;
269 static const struct {
270 GstVaapiImageFormat format;
271 DrawRectFunc draw_rect;
274 #define _(FORMAT) { GST_VAAPI_IMAGE_##FORMAT, draw_rect_##FORMAT }
287 for (i = 0; !draw_rect && map[i].format; i++)
288 if (map[i].format == image_format)
289 draw_rect = map[i].draw_rect;
293 display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(image));
297 if (!gst_vaapi_image_map(image))
300 for (i = 0; i < gst_vaapi_image_get_plane_count(image); i++) {
301 pixels[i] = gst_vaapi_image_get_plane(image, i);
302 stride[i] = gst_vaapi_image_get_pitch(image, i);
305 if (gst_vaapi_image_format_is_yuv(image_format))
306 color = argb2yuv(color);
312 if (width > image_width - x)
313 width = image_width - x;
314 if (height > image_height - y)
315 height = image_height - y;
317 gst_vaapi_display_lock(display);
318 draw_rect(pixels, stride, x, y, width, height, color);
319 gst_vaapi_display_unlock(display);
320 return gst_vaapi_image_unmap(image);
324 image_upload(GstVaapiImage *image, GstVaapiSurface *surface)
326 GstVaapiDisplay *display;
327 GstVaapiImageFormat format;
328 GstVaapiSubpicture *subpicture;
330 display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(surface));
334 format = gst_vaapi_image_get_format(image);
338 if (gst_vaapi_surface_put_image(surface, image))
341 g_print("could not upload %" GST_FOURCC_FORMAT" image to surface\n",
342 GST_FOURCC_ARGS(format));
344 if (!gst_vaapi_display_has_subpicture_format(display, format, NULL))
347 g_print("trying as a subpicture\n");
349 subpicture = gst_vaapi_subpicture_new(image, 0);
351 g_error("could not create VA subpicture");
353 if (!gst_vaapi_surface_associate_subpicture(surface, subpicture,
355 g_error("could not associate subpicture to surface");
357 /* The surface holds a reference to the subpicture. This is safe */
358 g_object_unref(subpicture);