mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-12-24 17:20:36 +00:00
cdf55c7c7a
Remove obsolete code. Remove the BlendInfo structure, we can do this better with GstVideoFrame Use GstVideoFrame in the API Prefix functions with gst_
371 lines
10 KiB
C
371 lines
10 KiB
C
/* Gstreamer video blending utility functions
|
|
*
|
|
* Copied/pasted from gst/videoconvert/videoconvert.c
|
|
* Copyright (C) 2010 David Schleef <ds@schleef.org>
|
|
* Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
|
*
|
|
* Copyright (C) <2011> Intel Corporation
|
|
* Copyright (C) <2011> Collabora Ltd.
|
|
* Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Library General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2 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
|
|
* Library General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Library General Public
|
|
* License along with this library; if not, write to the
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
* Boston, MA 02111-1307, USA.
|
|
*/
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "config.h"
|
|
#endif
|
|
|
|
#include "video-blend.h"
|
|
#include "videoblendorc.h"
|
|
|
|
#include <string.h>
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
#define GST_CAT_DEFAULT ensure_debug_category()
|
|
|
|
static GstDebugCategory *
|
|
ensure_debug_category (void)
|
|
{
|
|
static gsize cat_gonce = 0;
|
|
|
|
if (g_once_init_enter (&cat_gonce)) {
|
|
gsize cat_done;
|
|
|
|
cat_done = (gsize) _gst_debug_category_new ("video-blending", 0,
|
|
"video blending");
|
|
|
|
g_once_init_leave (&cat_gonce, cat_done);
|
|
}
|
|
|
|
return (GstDebugCategory *) cat_gonce;
|
|
}
|
|
|
|
#else
|
|
|
|
#define ensure_debug_category() /* NOOP */
|
|
|
|
#endif /* GST_DISABLE_GST_DEBUG */
|
|
|
|
static void
|
|
matrix_identity (guint8 * tmpline, guint width)
|
|
{
|
|
}
|
|
|
|
static void
|
|
matrix_prea_rgb_to_yuv (guint8 * tmpline, guint width)
|
|
{
|
|
int i;
|
|
int a, r, g, b;
|
|
int y, u, v;
|
|
|
|
for (i = 0; i < width; i++) {
|
|
a = tmpline[i * 4 + 0];
|
|
r = tmpline[i * 4 + 1];
|
|
g = tmpline[i * 4 + 2];
|
|
b = tmpline[i * 4 + 3];
|
|
if (a) {
|
|
r = (r * 255 + a / 2) / a;
|
|
g = (g * 255 + a / 2) / a;
|
|
b = (b * 255 + a / 2) / a;
|
|
}
|
|
|
|
y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
|
|
u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
|
|
v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
|
|
|
|
tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
|
|
tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
|
|
tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
|
|
}
|
|
}
|
|
|
|
static void
|
|
matrix_rgb_to_yuv (guint8 * tmpline, guint width)
|
|
{
|
|
int i;
|
|
int r, g, b;
|
|
int y, u, v;
|
|
|
|
for (i = 0; i < width; i++) {
|
|
r = tmpline[i * 4 + 1];
|
|
g = tmpline[i * 4 + 2];
|
|
b = tmpline[i * 4 + 3];
|
|
|
|
y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
|
|
u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
|
|
v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
|
|
|
|
tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
|
|
tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
|
|
tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
|
|
}
|
|
}
|
|
|
|
static void
|
|
matrix_yuv_to_rgb (guint8 * tmpline, guint width)
|
|
{
|
|
int i;
|
|
int r, g, b;
|
|
int y, u, v;
|
|
|
|
for (i = 0; i < width; i++) {
|
|
y = tmpline[i * 4 + 1];
|
|
u = tmpline[i * 4 + 2];
|
|
v = tmpline[i * 4 + 3];
|
|
|
|
r = (298 * y + 459 * v - 63514) >> 8;
|
|
g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
|
|
b = (298 * y + 541 * u - 73988) >> 8;
|
|
|
|
tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
|
|
tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
|
|
tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
|
|
}
|
|
}
|
|
|
|
#define BLEND00(ret, alpha, v0, v1) \
|
|
G_STMT_START { \
|
|
ret = (v0 * alpha + v1 * (255 - alpha)) / 255; \
|
|
} G_STMT_END
|
|
|
|
#define BLEND10(ret, alpha, v0, v1) \
|
|
G_STMT_START { \
|
|
ret = v0 + (v1 * (255 - alpha)) / 255; \
|
|
} G_STMT_END
|
|
|
|
/* returns newly-allocated pixels in src->pixels, which caller must g_free() */
|
|
void
|
|
gst_video_blend_scale_linear_RGBA (GstVideoFrame * src,
|
|
gint dest_height, gint dest_width)
|
|
{
|
|
const guint8 *src_pixels;
|
|
int acc;
|
|
int y_increment;
|
|
int x_increment;
|
|
int y1;
|
|
int i;
|
|
int j;
|
|
int x;
|
|
int dest_size;
|
|
guint dest_stride = dest_width * 4;
|
|
guint src_stride = src->width * 4;
|
|
|
|
guint8 *tmpbuf = g_malloc (dest_width * 8 * 4);
|
|
guint8 *dest_pixels =
|
|
g_malloc (gst_video_format_get_size (src->fmt, dest_height,
|
|
dest_width));
|
|
|
|
if (dest_height == 1)
|
|
y_increment = 0;
|
|
else
|
|
y_increment = ((src->height - 1) << 16) / (dest_height - 1) - 1;
|
|
|
|
if (dest_width == 1)
|
|
x_increment = 0;
|
|
else
|
|
x_increment = ((src->width - 1) << 16) / (dest_width - 1) - 1;
|
|
|
|
dest_size = dest_width * 4;
|
|
|
|
#define LINE(x) ((tmpbuf) + (dest_size)*((x)&1))
|
|
|
|
src_pixels = src->pixels;
|
|
|
|
acc = 0;
|
|
orc_resample_bilinear_u32 (LINE (0), src_pixels, 0, x_increment, dest_width);
|
|
y1 = 0;
|
|
for (i = 0; i < dest_height; i++) {
|
|
j = acc >> 16;
|
|
x = acc & 0xffff;
|
|
|
|
if (x == 0) {
|
|
memcpy (dest_pixels + i * dest_stride, LINE (j), dest_size);
|
|
} else {
|
|
if (j > y1) {
|
|
orc_resample_bilinear_u32 (LINE (j),
|
|
src_pixels + j * src_stride, 0, x_increment, dest_width);
|
|
y1++;
|
|
}
|
|
if (j >= y1) {
|
|
orc_resample_bilinear_u32 (LINE (j + 1),
|
|
src_pixels + (j + 1) * src_stride, 0, x_increment, dest_width);
|
|
y1++;
|
|
}
|
|
orc_merge_linear_u8 (dest_pixels + i * dest_stride,
|
|
LINE (j), LINE (j + 1), (x >> 8), dest_width * 4);
|
|
}
|
|
|
|
acc += y_increment;
|
|
}
|
|
|
|
/* Update src, our reference to the old src->pixels is lost */
|
|
video_blend_format_info_init (src, dest_pixels, dest_height, dest_width,
|
|
src->fmt, src->premultiplied_alpha);
|
|
|
|
g_free (tmpbuf);
|
|
}
|
|
|
|
/* video_blend:
|
|
* @dest: The #GstBlendVideoFormatInfo where to blend @src in
|
|
* @src: the #GstBlendVideoFormatInfo that we want to blend into
|
|
* @dest
|
|
* @x: The x offset in pixel where the @src image should be blended
|
|
* @y: the y offset in pixel where the @src image should be blended
|
|
* @global_alpha: the global_alpha each per-pixel alpha value is multiplied
|
|
* with
|
|
*
|
|
* Lets you blend the @src image into the @dest image
|
|
*/
|
|
gboolean
|
|
gst_video_blend (GstVideoFrame * dest,
|
|
GstVideoFrame * src, guint x, guint y, gfloat global_alpha)
|
|
{
|
|
guint i, j, global_alpha_val, src_width, src_height;
|
|
GetPutLine getputdest, getputsrc;
|
|
gint src_stride;
|
|
guint8 *tmpdestline = NULL, *tmpsrcline = NULL;
|
|
gboolean src_premultiplied_alpha;
|
|
|
|
g_assert (dest != NULL);
|
|
g_assert (src != NULL);
|
|
|
|
global_alpha_val = 256.0 * global_alpha;
|
|
|
|
/* we do no support writing to premultiplied alpha, though that should
|
|
just be a matter of adding blenders below (BLEND01 and BLEND11) */
|
|
g_return_val_if_fail (!dest->premultiplied_alpha, FALSE);
|
|
src_premultiplied_alpha = src->premultiplied_alpha;
|
|
|
|
src_stride = src->width * 4;
|
|
tmpdestline = g_malloc (sizeof (guint8) * (dest->width + 8) * 4);
|
|
tmpsrcline = g_malloc (sizeof (guint8) * (dest->width + 8) * 4);
|
|
|
|
ensure_debug_category ();
|
|
|
|
|
|
if (!lookup_getput (&getputdest, dest->fmt))
|
|
goto failed;
|
|
|
|
if (!lookup_getput (&getputsrc, src->fmt))
|
|
goto failed;
|
|
|
|
if (gst_video_format_is_rgb (src->fmt) != gst_video_format_is_rgb (dest->fmt)) {
|
|
if (gst_video_format_is_rgb (src->fmt)) {
|
|
if (src_premultiplied_alpha) {
|
|
getputsrc.matrix = matrix_prea_rgb_to_yuv;
|
|
src_premultiplied_alpha = FALSE;
|
|
} else {
|
|
getputsrc.matrix = matrix_rgb_to_yuv;
|
|
}
|
|
} else {
|
|
getputsrc.matrix = matrix_yuv_to_rgb;
|
|
}
|
|
}
|
|
|
|
/* adjust src pointers for negative sizes */
|
|
if (x < 0) {
|
|
src += -x * 4;
|
|
src->width -= -x;
|
|
x = 0;
|
|
}
|
|
|
|
if (y < 0) {
|
|
src += -y * src_stride;
|
|
src->height -= -y;
|
|
y = 0;
|
|
}
|
|
|
|
/* adjust width/height if the src is bigger than dest */
|
|
if (x + src->width > dest->width)
|
|
src->width = dest->width - x;
|
|
|
|
if (y + src->height > dest->height)
|
|
src->height = dest->height - y;
|
|
|
|
src_width = src->width;
|
|
src_height = src->height;
|
|
|
|
/* Mainloop doing the needed conversions, and blending */
|
|
for (i = y; i < y + src_height; i++) {
|
|
|
|
getputdest.getline (tmpdestline, dest, x, i);
|
|
getputsrc.getline (tmpsrcline, src, 0, (i - y));
|
|
|
|
getputsrc.matrix (tmpsrcline, src_width);
|
|
|
|
/* Here dest and src are both either in AYUV or ARGB
|
|
* TODO: Make the orc version working properly*/
|
|
#define BLENDLOOP(blender,alpha_val,alpha_scale) \
|
|
do { \
|
|
for (j = 0; j < src_width * 4; j += 4) { \
|
|
guint8 alpha; \
|
|
\
|
|
alpha = (tmpsrcline[j] * alpha_val) / alpha_scale; \
|
|
\
|
|
blender (tmpdestline[j + 1], alpha, tmpsrcline[j + 1], tmpdestline[j + 1]); \
|
|
blender (tmpdestline[j + 2], alpha, tmpsrcline[j + 2], tmpdestline[j + 2]); \
|
|
blender (tmpdestline[j + 3], alpha, tmpsrcline[j + 3], tmpdestline[j + 3]); \
|
|
} \
|
|
} while(0)
|
|
|
|
if (G_LIKELY (global_alpha == 1.0)) {
|
|
if (src_premultiplied_alpha && dest->premultiplied_alpha) {
|
|
/* BLENDLOOP (BLEND11, 1, 1); */
|
|
} else if (!src_premultiplied_alpha && dest->premultiplied_alpha) {
|
|
/* BLENDLOOP (BLEND01, 1, 1); */
|
|
} else if (src_premultiplied_alpha && !dest->premultiplied_alpha) {
|
|
BLENDLOOP (BLEND10, 1, 1);
|
|
} else {
|
|
BLENDLOOP (BLEND00, 1, 1);
|
|
}
|
|
} else {
|
|
if (src_premultiplied_alpha && dest->premultiplied_alpha) {
|
|
/* BLENDLOOP (BLEND11, global_alpha_val, 256); */
|
|
} else if (!src_premultiplied_alpha && dest->premultiplied_alpha) {
|
|
/* BLENDLOOP (BLEND01, global_alpha_val, 256); */
|
|
} else if (src_premultiplied_alpha && !dest->premultiplied_alpha) {
|
|
BLENDLOOP (BLEND10, global_alpha_val, 256);
|
|
} else {
|
|
BLENDLOOP (BLEND00, global_alpha_val, 256);
|
|
}
|
|
}
|
|
|
|
#undef BLENDLOOP
|
|
|
|
/* FIXME
|
|
* #if G_BYTE_ORDER == LITTLE_ENDIAN
|
|
* orc_blend_little (tmpdestline, tmpsrcline, dest->width);
|
|
* #else
|
|
* orc_blend_big (tmpdestline, tmpsrcline, src->width);
|
|
* #endif
|
|
*/
|
|
|
|
getputdest.putline (dest, src, tmpdestline, x, i);
|
|
|
|
}
|
|
|
|
g_free (tmpdestline);
|
|
g_free (tmpsrcline);
|
|
|
|
return TRUE;
|
|
|
|
failed:
|
|
GST_WARNING ("Could not do the blending");
|
|
g_free (tmpdestline);
|
|
g_free (tmpsrcline);
|
|
|
|
return FALSE;
|
|
}
|