2 * image.c - Image utilities for the tests
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6 * Copyright (C) 2013 Intel Corporation
7 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
39 image_draw_color_rectangles(
43 const guint32 colors[4],
47 const guint w = width / 2;
48 const guint h = height / 2;
50 if (!image_draw_rectangle(image, 0, 0, w, h, colors[0], flags))
52 if (!image_draw_rectangle(image, w, 0, w, h, colors[1], flags))
54 if (!image_draw_rectangle(image, 0, h, w, h, colors[2], flags))
56 if (!image_draw_rectangle(image, w, h, w, h, colors[3], flags))
63 GstVaapiDisplay *display,
64 GstVideoFormat format,
69 return image_generate_full(display, format, width, height, 0);
74 GstVaapiDisplay *display,
75 GstVideoFormat format,
83 static const guint32 rgb_colors[4] =
84 { 0xffff0000, 0xff00ff00, 0xff0000ff, 0xff000000 };
85 static const guint32 bgr_colors[4] =
86 { 0xff000000, 0xff0000ff, 0xff00ff00, 0xffff0000 };
87 static const guint32 inv_colors[4] =
88 { 0xffdeadc0, 0xffdeadc0, 0xffdeadc0, 0xffdeadc0 };
90 image = gst_vaapi_image_new(display, format, width, height);
95 if (!image_draw_color_rectangles(image, width, height,
96 ((flags & GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD) ?
97 rgb_colors : inv_colors),
98 GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD))
101 if (!image_draw_color_rectangles(image, width, height,
102 ((flags & GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD) ?
103 bgr_colors : inv_colors),
104 GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD))
107 else if (!image_draw_color_rectangles(image, width, height, rgb_colors, 0))
112 gst_vaapi_object_unref(image);
116 typedef void (*DrawRectFunc)(
126 static void draw_rect_ARGB(
138 color = GUINT32_TO_BE(color);
140 for (j = 0; j < height; j++) {
141 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
142 for (i = 0; i < width; i++)
147 static void draw_rect_BGRA(
157 // Converts ARGB color to BGRA
158 color = GUINT32_SWAP_LE_BE(color);
160 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
163 static void draw_rect_RGBA(
173 // Converts ARGB color to RGBA
174 color = ((color >> 24) & 0xff) | ((color & 0xffffff) << 8);
176 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
179 static void draw_rect_ABGR(
189 // Converts ARGB color to ABGR
190 color = ((color & 0xff00ff00) |
191 ((color >> 16) & 0xff) |
192 ((color & 0xff) << 16));
194 draw_rect_ARGB(pixels, stride, x, y, width, height, color);
197 static void draw_rect_NV12( // Y, UV planes
207 const guchar Y = color >> 16;
208 const guchar Cb = color >> 8;
209 const guchar Cr = color;
213 dst = pixels[0] + y * stride[0] + x;
214 for (j = 0; j < height; j++, dst += stride[0])
215 for (i = 0; i < width; i++)
223 dst = pixels[1] + y * stride[1] + x * 2;
224 for (j = 0; j < height; j++, dst += stride[1])
225 for (i = 0; i < width; i++) {
231 static void draw_rect_YV12( // Y, V, U planes
241 const guchar Y = color >> 16;
242 const guchar Cb = color >> 8;
243 const guchar Cr = color;
244 guchar *pY, *pU, *pV;
247 pY = pixels[0] + y * stride[0] + x;
248 for (j = 0; j < height; j++, pY += stride[0])
249 for (i = 0; i < width; i++)
257 pV = pixels[1] + y * stride[1] + x;
258 pU = pixels[2] + y * stride[2] + x;
259 for (j = 0; j < height; j++, pU += stride[1], pV += stride[2])
260 for (i = 0; i < width; i++) {
266 static void draw_rect_I420( // Y, U, V planes
276 guchar *new_pixels[3] = { pixels[0], pixels[2], pixels[1] };
277 guint new_stride[3] = { stride[0], stride[2], stride[1] };
279 draw_rect_YV12(new_pixels, new_stride, x, y, width, height, color);
282 static void draw_rect_YUV422(guchar *pixels[3], guint stride[3],
283 gint x, gint y, guint width, guint height, guint32 color)
288 for (j = 0; j < height; j++) {
289 guint32 * const p = (guint32 *)
290 (pixels[0] + (y + j) * stride[0] + x * 2);
291 for (i = 0; i < width; i++)
296 static void draw_rect_YUY2(guchar *pixels[3], guint stride[3],
297 gint x, gint y, guint width, guint height, guint32 color)
299 const guchar Y = color >> 16;
300 const guchar Cb = color >> 8;
301 const guchar Cr = color;
303 color = (Y << 24) | (Cb << 16) | (Y << 8) | Cr;
304 draw_rect_YUV422(pixels, stride, x, y, width, height, GUINT32_TO_BE(color));
307 static void draw_rect_UYVY(guchar *pixels[3], guint stride[3],
308 gint x, gint y, guint width, guint height, guint32 color)
310 const guchar Y = color >> 16;
311 const guchar Cb = color >> 8;
312 const guchar Cr = color;
314 color = (Cb << 24) | (Y << 16) | (Cr << 8) | Y;
315 draw_rect_YUV422(pixels, stride, x, y, width, height, GUINT32_TO_BE(color));
318 static void draw_rect_AYUV(
330 color = color | 0xff000000;
332 for (j = 0; j < height; j++) {
333 guint32 *p = (guint32 *)(pixels[0] + (y + j) * stride[0] + x * 4);
334 for (i = 0; i < width; i++)
339 static inline guint32 argb2yuv(guint32 color)
341 const gint32 r = (color >> 16) & 0xff;
342 const gint32 g = (color >> 8) & 0xff;
343 const gint32 b = (color ) & 0xff;
345 const guint32 y = (( 306 * r + 601 * g + 116 * b) >> 10);
346 const guint32 u = ((-172 * r - 339 * g + 512 * b) >> 10) + 128;
347 const guint32 v = (( 512 * r - 428 * g - 83 * b) >> 10) + 128;
349 return (y << 16) | (u << 8) | v;
353 image_draw_rectangle(
354 GstVaapiImage *image,
363 const GstVideoFormat image_format = gst_vaapi_image_get_format(image);
364 const guint image_width = gst_vaapi_image_get_width(image);
365 const guint image_height = gst_vaapi_image_get_height(image);
366 GstVaapiDisplay *display;
369 DrawRectFunc draw_rect = NULL;
372 static const struct {
373 GstVideoFormat format;
374 DrawRectFunc draw_rect;
377 #define _(FORMAT) { GST_VIDEO_FORMAT_##FORMAT, draw_rect_##FORMAT }
392 for (i = 0; !draw_rect && map[i].format; i++)
393 if (map[i].format == image_format)
394 draw_rect = map[i].draw_rect;
402 if (width > image_width - x)
403 width = image_width - x;
404 if (height > image_height - y)
405 height = image_height - y;
407 display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(image));
411 if (!gst_vaapi_image_map(image))
414 for (i = 0; i < gst_vaapi_image_get_plane_count(image); i++) {
415 pixels[i] = gst_vaapi_image_get_plane(image, i);
416 stride[i] = gst_vaapi_image_get_pitch(image, i);
418 case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
419 pixels[i] += stride[i];
421 case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
430 if (gst_vaapi_video_format_is_yuv(image_format))
431 color = argb2yuv(color);
433 draw_rect(pixels, stride, x, y, width, height, color);
434 return gst_vaapi_image_unmap(image);
438 image_upload(GstVaapiImage *image, GstVaapiSurface *surface)
440 GstVaapiDisplay *display;
441 GstVideoFormat format;
442 GstVaapiImage *surface_image;
443 GstVaapiSubpicture *subpicture;
446 display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(surface));
450 format = gst_vaapi_image_get_format(image);
454 if (gst_vaapi_surface_put_image(surface, image))
457 surface_image = gst_vaapi_surface_derive_image(surface);
459 success = gst_vaapi_image_copy(surface_image, image);
460 gst_vaapi_object_unref(surface_image);
465 g_print("could not upload %s image to surface\n",
466 gst_vaapi_video_format_to_string(format));
468 if (!gst_vaapi_display_has_subpicture_format(display, format, NULL))
471 g_print("trying as a subpicture\n");
473 subpicture = gst_vaapi_subpicture_new(image, 0);
475 g_error("could not create VA subpicture");
477 if (!gst_vaapi_surface_associate_subpicture(surface, subpicture,
479 g_error("could not associate subpicture to surface");
481 /* The surface holds a reference to the subpicture. This is safe */
482 gst_vaapi_object_unref(subpicture);