2 * image.c - Image utilities for the tests
4 * gstreamer-vaapi (C) 2010-2011 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
25 GstVaapiDisplay *display,
26 GstVaapiImageFormat format,
31 const guint w = width;
32 const guint h = height;
35 image = gst_vaapi_image_new(display, format, w, h);
39 if (image_draw_rectangle(image, 0, 0, w/2, h/2, 0xffff0000) &&
40 image_draw_rectangle(image, w/2, 0, w/2, h/2, 0xff00ff00) &&
41 image_draw_rectangle(image, 0, h/2, w/2, h/2, 0xff0000ff) &&
42 image_draw_rectangle(image, w/2, h/2, w/2, h/2, 0xff000000))
45 g_object_unref(image);
49 typedef void (*DrawRectFunc)(
59 static void draw_rect_ARGB(
71 color = GUINT32_TO_BE(color);
73 for (j = 0; j < height; j++) {
74 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
75 for (i = 0; i < width; i++)
80 static void draw_rect_BGRA(
90 // Converts ARGB color to BGRA
91 color = GUINT32_SWAP_LE_BE(color);
93 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
96 static void draw_rect_RGBA(
106 // Converts ARGB color to RGBA
107 color = ((color >> 24) & 0xff) | ((color & 0xffffff) << 8);
109 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
112 static void draw_rect_ABGR(
122 // Converts ARGB color to ABGR
123 color = ((color & 0xff00ff00) |
124 ((color >> 16) & 0xff) |
125 ((color & 0xff) << 16));
127 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
130 static void draw_rect_NV12( // Y, UV planes
140 const guchar Y = color >> 16;
141 const guchar Cb = color >> 8;
142 const guchar Cr = color;
146 dst = pixels[0] + y * stride[0] + x;
147 for (j = 0; j < height; j++, dst += stride[0])
148 for (i = 0; i < width; i++)
156 dst = pixels[1] + y * stride[1] + x * 2;
157 for (j = 0; j < height; j++, dst += stride[1])
158 for (i = 0; i < width; i++) {
164 static void draw_rect_YV12( // Y, V, U planes
174 const guchar Y = color >> 16;
175 const guchar Cb = color >> 8;
176 const guchar Cr = color;
177 guchar *pY, *pU, *pV;
180 pY = pixels[0] + y * stride[0] + x;
181 for (j = 0; j < height; j++, pY += stride[0])
182 for (i = 0; i < width; i++)
190 pU = pixels[1] + y * stride[1] + x;
191 pV = pixels[2] + y * stride[2] + x;
192 for (j = 0; j < height; j++, pU += stride[1], pV += stride[2])
193 for (i = 0; i < width; i++) {
199 static void draw_rect_I420( // Y, U, V planes
209 guchar *new_pixels[3] = { pixels[0], pixels[2], pixels[1] };
210 guint new_stride[3] = { stride[0], stride[2], stride[1] };
212 draw_rect_YV12(new_pixels, new_stride, x, y, width, height, color);
215 static void draw_rect_AYUV(
227 color = color | 0xff000000;
229 for (j = 0; j < height; j++) {
230 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
231 for (i = 0; i < width; i++)
236 static inline guint32 argb2yuv(guint32 color)
238 const gint32 r = (color >> 16) & 0xff;
239 const gint32 g = (color >> 8) & 0xff;
240 const gint32 b = (color ) & 0xff;
242 const guint32 y = (( 263 * r + 516 * g + 100 * b) >> 10) + 16;
243 const guint32 u = ((-152 * r - 298 * g + 450 * b) >> 10) + 128;
244 const guint32 v = (( 450 * r - 376 * g - 73 * b) >> 10) + 128;
246 return (y << 16) | (u << 8) | v;
250 image_draw_rectangle(
251 GstVaapiImage *image,
259 const GstVaapiImageFormat image_format = gst_vaapi_image_get_format(image);
260 const guint image_width = gst_vaapi_image_get_width(image);
261 const guint image_height = gst_vaapi_image_get_height(image);
262 GstVaapiDisplay *display;
265 DrawRectFunc draw_rect = NULL;
268 static const struct {
269 GstVaapiImageFormat format;
270 DrawRectFunc draw_rect;
273 #define _(FORMAT) { GST_VAAPI_IMAGE_##FORMAT, draw_rect_##FORMAT }
286 for (i = 0; !draw_rect && map[i].format; i++)
287 if (map[i].format == image_format)
288 draw_rect = map[i].draw_rect;
292 display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(image));
296 if (!gst_vaapi_image_map(image))
299 for (i = 0; i < gst_vaapi_image_get_plane_count(image); i++) {
300 pixels[i] = gst_vaapi_image_get_plane(image, i);
301 stride[i] = gst_vaapi_image_get_pitch(image, i);
304 if (gst_vaapi_image_format_is_yuv(image_format))
305 color = argb2yuv(color);
311 if (width > image_width - x)
312 width = image_width - x;
313 if (height > image_height - y)
314 height = image_height - y;
316 gst_vaapi_display_lock(display);
317 draw_rect(pixels, stride, x, y, width, height, color);
318 gst_vaapi_display_unlock(display);
319 return gst_vaapi_image_unmap(image);
323 image_upload(GstVaapiImage *image, GstVaapiSurface *surface)
325 GstVaapiDisplay *display;
326 GstVaapiImageFormat format;
327 GstVaapiSubpicture *subpicture;
329 display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(surface));
333 format = gst_vaapi_image_get_format(image);
337 if (gst_vaapi_surface_put_image(surface, image))
340 g_print("could not upload %" GST_FOURCC_FORMAT" image to surface\n",
341 GST_FOURCC_ARGS(format));
343 if (!gst_vaapi_display_has_subpicture_format(display, format))
346 g_print("trying as a subpicture\n");
348 subpicture = gst_vaapi_subpicture_new(image);
350 g_error("could not create VA subpicture");
352 if (!gst_vaapi_surface_associate_subpicture(surface, subpicture,
354 g_error("could not associate subpicture to surface");
356 /* The surface holds a reference to the subpicture. This is safe */
357 g_object_unref(subpicture);