1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/*
* image.c - Image utilities for the tests
*
* Copyright (C) 2010-2011 Splitted-Desktop Systems
* Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
* Copyright (C) 2013 Intel Corporation
* Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "image.h"
static gboolean
image_draw_rectangle (GstVaapiImage * image,
gint x, gint y, guint width, guint height, guint32 color, guint32 flags);
static gboolean
image_draw_color_rectangles (GstVaapiImage * image,
guint width, guint height, const guint32 colors[4], guint32 flags)
{
const guint w = width / 2;
const guint h = height / 2;
if (!image_draw_rectangle (image, 0, 0, w, h, colors[0], flags))
return FALSE;
if (!image_draw_rectangle (image, w, 0, w, h, colors[1], flags))
return FALSE;
if (!image_draw_rectangle (image, 0, h, w, h, colors[2], flags))
return FALSE;
if (!image_draw_rectangle (image, w, h, w, h, colors[3], flags))
return FALSE;
return TRUE;
}
GstVaapiImage *
image_generate (GstVaapiDisplay * display,
GstVideoFormat format, guint width, guint height)
{
return image_generate_full (display, format, width, height, 0);
}
GstVaapiImage *
image_generate_full (GstVaapiDisplay * display,
GstVideoFormat format, guint width, guint height, guint32 flags)
{
GstVaapiImage *image;
static const guint32 rgb_colors[4] =
{ 0xffff0000, 0xff00ff00, 0xff0000ff, 0xff000000 };
static const guint32 bgr_colors[4] =
{ 0xff000000, 0xff0000ff, 0xff00ff00, 0xffff0000 };
static const guint32 inv_colors[4] =
{ 0xffdeadc0, 0xffdeadc0, 0xffdeadc0, 0xffdeadc0 };
image = gst_vaapi_image_new (display, format, width, height);
if (!image)
return NULL;
if (flags) {
if (!image_draw_color_rectangles (image, width, height,
((flags & GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD) ?
rgb_colors : inv_colors),
GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD))
goto error;
if (!image_draw_color_rectangles (image, width, height,
((flags & GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD) ?
bgr_colors : inv_colors),
GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD))
goto error;
} else if (!image_draw_color_rectangles (image, width, height, rgb_colors, 0))
goto error;
return image;
error:
gst_vaapi_image_unref (image);
return NULL;
}
typedef void (*DrawRectFunc) (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color);
static void
draw_rect_ARGB (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
guint i, j;
color = GUINT32_TO_BE (color);
for (j = 0; j < height; j++) {
guint32 *p = (guint32 *) (pixels[0] + (y + j) * stride[0] + x * 4);
for (i = 0; i < width; i++)
p[i] = color;
}
}
static void
draw_rect_BGRA (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
// Converts ARGB color to BGRA
color = GUINT32_SWAP_LE_BE (color);
draw_rect_ARGB (pixels, stride, x, y, width, height, color);
}
static void
draw_rect_RGBA (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
// Converts ARGB color to RGBA
color = ((color >> 24) & 0xff) | ((color & 0xffffff) << 8);
draw_rect_ARGB (pixels, stride, x, y, width, height, color);
}
static void
draw_rect_ABGR (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
// Converts ARGB color to ABGR
color = ((color & 0xff00ff00) |
((color >> 16) & 0xff) | ((color & 0xff) << 16));
draw_rect_ARGB (pixels, stride, x, y, width, height, color);
}
static void
draw_rect_NV12 ( // Y, UV planes
guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
const guchar Y = color >> 16;
const guchar Cb = color >> 8;
const guchar Cr = color;
guchar *dst;
guint i, j;
dst = pixels[0] + y * stride[0] + x;
for (j = 0; j < height; j++, dst += stride[0])
for (i = 0; i < width; i++)
dst[i] = Y;
x /= 2;
y /= 2;
width /= 2;
height /= 2;
dst = pixels[1] + y * stride[1] + x * 2;
for (j = 0; j < height; j++, dst += stride[1])
for (i = 0; i < width; i++) {
dst[2 * i + 0] = Cb;
dst[2 * i + 1] = Cr;
}
}
static void
draw_rect_YV12 ( // Y, V, U planes
guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
const guchar Y = color >> 16;
const guchar Cb = color >> 8;
const guchar Cr = color;
guchar *pY, *pU, *pV;
guint i, j;
pY = pixels[0] + y * stride[0] + x;
for (j = 0; j < height; j++, pY += stride[0])
for (i = 0; i < width; i++)
pY[i] = Y;
x /= 2;
y /= 2;
width /= 2;
height /= 2;
pV = pixels[1] + y * stride[1] + x;
pU = pixels[2] + y * stride[2] + x;
for (j = 0; j < height; j++, pU += stride[1], pV += stride[2])
for (i = 0; i < width; i++) {
pU[i] = Cb;
pV[i] = Cr;
}
}
static void
draw_rect_I420 ( // Y, U, V planes
guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
guchar *new_pixels[3] = { pixels[0], pixels[2], pixels[1] };
guint new_stride[3] = { stride[0], stride[2], stride[1] };
draw_rect_YV12 (new_pixels, new_stride, x, y, width, height, color);
}
static void
draw_rect_YUV422 (guchar * pixels[3], guint stride[3],
gint x, gint y, guint width, guint height, guint32 color)
{
guint i, j;
width /= 2;
for (j = 0; j < height; j++) {
guint32 *const p = (guint32 *)
(pixels[0] + (y + j) * stride[0] + x * 2);
for (i = 0; i < width; i++)
p[i] = color;
}
}
static void
draw_rect_YUY2 (guchar * pixels[3], guint stride[3],
gint x, gint y, guint width, guint height, guint32 color)
{
const guchar Y = color >> 16;
const guchar Cb = color >> 8;
const guchar Cr = color;
color = (Y << 24) | (Cb << 16) | (Y << 8) | Cr;
draw_rect_YUV422 (pixels, stride, x, y, width, height, GUINT32_TO_BE (color));
}
static void
draw_rect_UYVY (guchar * pixels[3], guint stride[3],
gint x, gint y, guint width, guint height, guint32 color)
{
const guchar Y = color >> 16;
const guchar Cb = color >> 8;
const guchar Cr = color;
color = (Cb << 24) | (Y << 16) | (Cr << 8) | Y;
draw_rect_YUV422 (pixels, stride, x, y, width, height, GUINT32_TO_BE (color));
}
static void
draw_rect_AYUV (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
{
guint i, j;
color = color | 0xff000000;
for (j = 0; j < height; j++) {
guint32 *p = (guint32 *) (pixels[0] + (y + j) * stride[0] + x * 4);
for (i = 0; i < width; i++)
p[i] = color;
}
}
static inline guint32
argb2yuv (guint32 color)
{
const gint32 r = (color >> 16) & 0xff;
const gint32 g = (color >> 8) & 0xff;
const gint32 b = (color) & 0xff;
const guint32 y = ((306 * r + 601 * g + 116 * b) >> 10);
const guint32 u = ((-172 * r - 339 * g + 512 * b) >> 10) + 128;
const guint32 v = ((512 * r - 428 * g - 83 * b) >> 10) + 128;
return (y << 16) | (u << 8) | v;
}
gboolean
image_draw_rectangle (GstVaapiImage * image,
gint x, gint y, guint width, guint height, guint32 color, guint32 flags)
{
const GstVideoFormat image_format = gst_vaapi_image_get_format (image);
const guint image_width = gst_vaapi_image_get_width (image);
const guint image_height = gst_vaapi_image_get_height (image);
GstVaapiDisplay *display;
guchar *pixels[3];
guint stride[3];
DrawRectFunc draw_rect = NULL;
guint i;
static const struct
{
GstVideoFormat format;
DrawRectFunc draw_rect;
}
map[] = {
#define _(FORMAT) { GST_VIDEO_FORMAT_##FORMAT, draw_rect_##FORMAT }
_(ARGB),
_(BGRA),
_(RGBA), _(ABGR), _(NV12), _(YV12), _(I420), _(YUY2), _(UYVY), _(AYUV),
#undef _
{
0,}
};
for (i = 0; !draw_rect && map[i].format; i++)
if (map[i].format == image_format)
draw_rect = map[i].draw_rect;
if (!draw_rect)
return FALSE;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (width > image_width - x)
width = image_width - x;
if (height > image_height - y)
height = image_height - y;
display = gst_vaapi_image_get_display (image);
if (!display)
return FALSE;
if (!gst_vaapi_image_map (image))
return FALSE;
for (i = 0; i < gst_vaapi_image_get_plane_count (image); i++) {
pixels[i] = gst_vaapi_image_get_plane (image, i);
stride[i] = gst_vaapi_image_get_pitch (image, i);
switch (flags) {
case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
pixels[i] += stride[i];
// fall-through
case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
stride[i] *= 2;
break;
}
}
if (flags)
y /= 2, height /= 2;
if (gst_vaapi_video_format_is_yuv (image_format))
color = argb2yuv (color);
draw_rect (pixels, stride, x, y, width, height, color);
return gst_vaapi_image_unmap (image);
}
gboolean
image_upload (GstVaapiImage * image, GstVaapiSurface * surface)
{
GstVaapiDisplay *display;
GstVideoFormat format;
GstVaapiImage *surface_image;
GstVaapiSubpicture *subpicture;
gboolean success;
display = gst_vaapi_surface_get_display (surface);
if (!display)
return FALSE;
format = gst_vaapi_image_get_format (image);
if (!format)
return FALSE;
if (gst_vaapi_surface_put_image (surface, image))
return TRUE;
surface_image = gst_vaapi_surface_derive_image (surface);
if (surface_image) {
success = gst_vaapi_image_copy (surface_image, image);
gst_vaapi_image_unref (surface_image);
if (success)
return TRUE;
}
g_print ("could not upload %s image to surface\n",
gst_vaapi_video_format_to_string (format));
if (!gst_vaapi_display_has_subpicture_format (display, format, NULL))
return FALSE;
g_print ("trying as a subpicture\n");
subpicture = gst_vaapi_subpicture_new (image, 0);
if (!subpicture)
g_error ("could not create VA subpicture");
if (!gst_vaapi_surface_associate_subpicture (surface, subpicture, NULL, NULL))
g_error ("could not associate subpicture to surface");
/* The surface holds a reference to the subpicture. This is safe */
gst_vaapi_subpicture_unref (subpicture);
return TRUE;
}
|