gstreamer/subprojects/gstreamer-vaapi/tests/internal/image.c

401 lines
11 KiB
C
Raw Normal View History

2010-03-26 15:16:01 +00:00
/*
* image.c - Image utilities for the tests
*
* Copyright (C) 2010-2011 Splitted-Desktop Systems
* Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
2013-11-22 05:37:12 +00:00
* Copyright (C) 2013 Intel Corporation
* Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
2010-03-26 15:16:01 +00:00
*
2011-10-18 07:18:20 +00:00
* 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.
2010-03-26 15:16:01 +00:00
*
2011-10-18 07:18:20 +00:00
* This library is distributed in the hope that it will be useful,
2010-03-26 15:16:01 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2011-10-18 07:18:20 +00:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2010-03-26 15:16:01 +00:00
*
2011-10-18 07:18:20 +00:00
* 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
2013-03-20 10:57:57 +00:00
*/
2010-03-26 15:16:01 +00:00
#include "image.h"
static gboolean
2016-04-29 10:48:44 +00:00
image_draw_rectangle (GstVaapiImage * image,
gint x, gint y, guint width, guint height, guint32 color, guint32 flags);
static gboolean
2016-04-29 10:48:44 +00:00
image_draw_color_rectangles (GstVaapiImage * image,
guint width, guint height, const guint32 colors[4], guint32 flags)
{
2016-04-29 10:48:44 +00:00
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;
}
2010-03-26 15:16:01 +00:00
GstVaapiImage *
2016-04-29 10:48:44 +00:00
image_generate (GstVaapiDisplay * display,
GstVideoFormat format, guint width, guint height)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
return image_generate_full (display, format, width, height, 0);
}
GstVaapiImage *
2016-04-29 10:48:44 +00:00
image_generate_full (GstVaapiDisplay * display,
GstVideoFormat format, guint width, guint height, guint32 flags)
{
2016-04-29 10:48:44 +00:00
GstVaapiImage *image;
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
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)
2010-03-26 15:16:01 +00:00
return NULL;
2016-04-29 10:48:44 +00:00
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);
2016-04-29 10:48:44 +00:00
return NULL;
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
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)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
guint i, j;
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
color = GUINT32_TO_BE (color);
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
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;
}
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_BGRA (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
// Converts ARGB color to BGRA
color = GUINT32_SWAP_LE_BE (color);
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
draw_rect_ARGB (pixels, stride, x, y, width, height, color);
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_RGBA (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
// Converts ARGB color to RGBA
color = ((color >> 24) & 0xff) | ((color & 0xffffff) << 8);
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
draw_rect_ARGB (pixels, stride, x, y, width, height, color);
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_ABGR (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
// Converts ARGB color to ABGR
color = ((color & 0xff00ff00) |
((color >> 16) & 0xff) | ((color & 0xff) << 16));
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
draw_rect_ARGB (pixels, stride, x, y, width, height, color);
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_NV12 ( // Y, UV planes
guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
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;
}
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
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)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
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;
}
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
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)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
guchar *new_pixels[3] = { pixels[0], pixels[2], pixels[1] };
guint new_stride[3] = { stride[0], stride[2], stride[1] };
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
draw_rect_YV12 (new_pixels, new_stride, x, y, width, height, color);
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_YUV422 (guchar * pixels[3], guint stride[3],
gint x, gint y, guint width, guint height, guint32 color)
{
2016-04-29 10:48:44 +00:00
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;
}
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_YUY2 (guchar * pixels[3], guint stride[3],
gint x, gint y, guint width, guint height, guint32 color)
{
2016-04-29 10:48:44 +00:00
const guchar Y = color >> 16;
const guchar Cb = color >> 8;
const guchar Cr = color;
2016-04-29 10:48:44 +00:00
color = (Y << 24) | (Cb << 16) | (Y << 8) | Cr;
draw_rect_YUV422 (pixels, stride, x, y, width, height, GUINT32_TO_BE (color));
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_UYVY (guchar * pixels[3], guint stride[3],
gint x, gint y, guint width, guint height, guint32 color)
{
2016-04-29 10:48:44 +00:00
const guchar Y = color >> 16;
const guchar Cb = color >> 8;
const guchar Cr = color;
2016-04-29 10:48:44 +00:00
color = (Cb << 24) | (Y << 16) | (Cr << 8) | Y;
draw_rect_YUV422 (pixels, stride, x, y, width, height, GUINT32_TO_BE (color));
}
2016-04-29 10:48:44 +00:00
static void
draw_rect_AYUV (guchar * pixels[3],
guint stride[3], gint x, gint y, guint width, guint height, guint32 color)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
guint i, j;
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
color = color | 0xff000000;
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
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;
}
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
static inline guint32
argb2yuv (guint32 color)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
const gint32 r = (color >> 16) & 0xff;
const gint32 g = (color >> 8) & 0xff;
const gint32 b = (color) & 0xff;
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
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;
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
return (y << 16) | (u << 8) | v;
2010-03-26 15:16:01 +00:00
}
gboolean
2016-04-29 10:48:44 +00:00
image_draw_rectangle (GstVaapiImage * image,
gint x, gint y, guint width, guint height, guint32 color, guint32 flags)
2010-03-26 15:16:01 +00:00
{
2016-04-29 10:48:44 +00:00
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[] = {
2013-07-09 13:28:31 +00:00
#define _(FORMAT) { GST_VIDEO_FORMAT_##FORMAT, draw_rect_##FORMAT }
2016-04-29 10:48:44 +00:00
_(ARGB),
2010-03-26 15:16:01 +00:00
_(BGRA),
2016-04-29 10:48:44 +00:00
_(RGBA), _(ABGR), _(NV12), _(YV12), _(I420), _(YUY2), _(UYVY), _(AYUV),
2010-03-26 15:16:01 +00:00
#undef _
2016-04-29 10:48:44 +00:00
{
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);
2016-04-29 10:48:44 +00:00
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;
2010-03-26 15:16:01 +00:00
}
2016-04-29 10:48:44 +00:00
}
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
if (flags)
y /= 2, height /= 2;
2016-04-29 10:48:44 +00:00
if (gst_vaapi_video_format_is_yuv (image_format))
color = argb2yuv (color);
2010-03-26 15:16:01 +00:00
2016-04-29 10:48:44 +00:00
draw_rect (pixels, stride, x, y, width, height, color);
return gst_vaapi_image_unmap (image);
2010-03-26 15:16:01 +00:00
}
2010-03-26 17:00:45 +00:00
gboolean
2016-04-29 10:48:44 +00:00
image_upload (GstVaapiImage * image, GstVaapiSurface * surface)
2010-03-26 17:00:45 +00:00
{
2016-04-29 10:48:44 +00:00
GstVaapiDisplay *display;
GstVideoFormat format;
GstVaapiImage *surface_image;
GstVaapiSubpicture *subpicture;
gboolean success;
display = gst_vaapi_surface_get_display (surface);
2016-04-29 10:48:44 +00:00
if (!display)
return FALSE;
2016-04-29 10:48:44 +00:00
format = gst_vaapi_image_get_format (image);
if (!format)
return FALSE;
2010-03-26 17:00:45 +00:00
2016-04-29 10:48:44 +00:00
if (gst_vaapi_surface_put_image (surface, image))
return TRUE;
2010-03-26 17:00:45 +00:00
2016-04-29 10:48:44 +00:00
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);
2016-04-29 10:48:44 +00:00
if (success)
return TRUE;
}
2010-03-26 17:00:45 +00:00
2016-04-29 10:48:44 +00:00
g_print ("could not upload %s image to surface\n",
gst_vaapi_video_format_to_string (format));
2010-03-26 17:00:45 +00:00
2016-04-29 10:48:44 +00:00
if (!gst_vaapi_display_has_subpicture_format (display, format, NULL))
return FALSE;
2010-03-26 17:00:45 +00:00
2016-04-29 10:48:44 +00:00
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);
2016-04-29 10:48:44 +00:00
return TRUE;
2010-03-26 17:00:45 +00:00
}