2011-10-17 15:25:11 +00:00
|
|
|
/* 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
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-10-17 15:25:11 +00:00
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "video-blend.h"
|
2012-07-23 14:52:33 +00:00
|
|
|
#include "video-orc.h"
|
2011-10-17 15:25:11 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-14 14:14:47 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 15:25:11 +00:00
|
|
|
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;
|
|
|
|
|
2011-12-14 16:34:39 +00:00
|
|
|
tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
|
|
|
|
tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
|
|
|
|
tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
|
2011-10-17 15:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-01 14:28:55 +00:00
|
|
|
/**
|
|
|
|
* gst_video_blend_scale_linear_RGBA:
|
|
|
|
* @src: the #GstVideoInfo describing the video data in @src_buffer
|
|
|
|
* @src_buffer: the source buffer containing video pixels to scale
|
|
|
|
* @dest_height: the height in pixels to scale the video data in @src_buffer to
|
|
|
|
* @dest_width: the width in pixels to scale the video data in @src_buffer to
|
|
|
|
* @dest: (out): pointer to a #GstVideoInfo structure that will be filled in
|
|
|
|
* with the details for @dest_buffer
|
|
|
|
* @dest_buffer: (out): a pointer to a #GstBuffer variable, which will be
|
|
|
|
* set to a newly-allocated buffer containing the scaled pixels.
|
|
|
|
*
|
|
|
|
* Scales a buffer containing RGBA (or AYUV) video. This is an internal
|
|
|
|
* helper function which is used to scale subtitle overlays, and may be
|
|
|
|
* deprecated in the near future. Use #GstVideoScaler to scale video buffers
|
|
|
|
* instead.
|
|
|
|
*/
|
2012-06-28 16:16:20 +00:00
|
|
|
/* returns newly-allocated buffer, which caller must unref */
|
2011-10-17 15:25:11 +00:00
|
|
|
void
|
2012-06-28 16:16:20 +00:00
|
|
|
gst_video_blend_scale_linear_RGBA (GstVideoInfo * src, GstBuffer * src_buffer,
|
|
|
|
gint dest_height, gint dest_width, GstVideoInfo * dest,
|
|
|
|
GstBuffer ** dest_buffer)
|
2011-10-17 15:25:11 +00:00
|
|
|
{
|
2012-03-25 00:31:41 +00:00
|
|
|
const guint8 *src_pixels;
|
2011-10-17 15:25:11 +00:00
|
|
|
int acc;
|
|
|
|
int y_increment;
|
|
|
|
int x_increment;
|
|
|
|
int y1;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int x;
|
|
|
|
int dest_size;
|
2012-07-16 14:25:42 +00:00
|
|
|
guint dest_stride;
|
|
|
|
guint src_stride;
|
2012-06-28 16:16:20 +00:00
|
|
|
guint8 *dest_pixels;
|
2012-11-02 19:09:21 +00:00
|
|
|
guint8 *tmpbuf;
|
2012-06-28 16:16:20 +00:00
|
|
|
GstVideoFrame src_frame, dest_frame;
|
|
|
|
|
|
|
|
g_return_if_fail (dest_buffer != NULL);
|
|
|
|
|
2012-11-02 19:09:21 +00:00
|
|
|
tmpbuf = g_malloc (dest_width * 8 * 4);
|
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
gst_video_info_init (dest);
|
|
|
|
gst_video_info_set_format (dest, GST_VIDEO_INFO_FORMAT (src),
|
|
|
|
dest_width, dest_height);
|
|
|
|
|
|
|
|
*dest_buffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (dest));
|
|
|
|
|
|
|
|
gst_video_frame_map (&src_frame, src, src_buffer, GST_MAP_READ);
|
|
|
|
gst_video_frame_map (&dest_frame, dest, *dest_buffer, GST_MAP_WRITE);
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2015-11-12 22:01:03 +00:00
|
|
|
if (dest_height == 1 || src->height == 1)
|
2011-10-17 15:25:11 +00:00
|
|
|
y_increment = 0;
|
|
|
|
else
|
|
|
|
y_increment = ((src->height - 1) << 16) / (dest_height - 1) - 1;
|
|
|
|
|
2015-11-12 22:01:03 +00:00
|
|
|
if (dest_width == 1 || src->width == 1)
|
2011-10-17 15:25:11 +00:00
|
|
|
x_increment = 0;
|
|
|
|
else
|
|
|
|
x_increment = ((src->width - 1) << 16) / (dest_width - 1) - 1;
|
|
|
|
|
2012-07-16 14:25:42 +00:00
|
|
|
dest_size = dest_stride = dest_width * 4;
|
|
|
|
src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (&src_frame, 0);
|
2011-10-17 15:25:11 +00:00
|
|
|
|
|
|
|
#define LINE(x) ((tmpbuf) + (dest_size)*((x)&1))
|
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
dest_pixels = GST_VIDEO_FRAME_PLANE_DATA (&dest_frame, 0);
|
|
|
|
src_pixels = GST_VIDEO_FRAME_PLANE_DATA (&src_frame, 0);
|
2012-03-25 00:31:41 +00:00
|
|
|
|
2011-10-17 15:25:11 +00:00
|
|
|
acc = 0;
|
2012-07-23 15:10:08 +00:00
|
|
|
video_orc_resample_bilinear_u32 (LINE (0), src_pixels, 0, x_increment,
|
2012-07-23 14:56:35 +00:00
|
|
|
dest_width);
|
2011-10-17 15:25:11 +00:00
|
|
|
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) {
|
2012-07-23 15:10:08 +00:00
|
|
|
video_orc_resample_bilinear_u32 (LINE (j),
|
2012-03-25 00:31:41 +00:00
|
|
|
src_pixels + j * src_stride, 0, x_increment, dest_width);
|
2011-10-17 15:25:11 +00:00
|
|
|
y1++;
|
|
|
|
}
|
|
|
|
if (j >= y1) {
|
2012-07-23 15:10:08 +00:00
|
|
|
video_orc_resample_bilinear_u32 (LINE (j + 1),
|
2012-03-25 00:31:41 +00:00
|
|
|
src_pixels + (j + 1) * src_stride, 0, x_increment, dest_width);
|
2011-10-17 15:25:11 +00:00
|
|
|
y1++;
|
|
|
|
}
|
2012-07-23 15:10:08 +00:00
|
|
|
video_orc_merge_linear_u8 (dest_pixels + i * dest_stride,
|
2011-10-17 15:25:11 +00:00
|
|
|
LINE (j), LINE (j + 1), (x >> 8), dest_width * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
acc += y_increment;
|
|
|
|
}
|
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
gst_video_frame_unmap (&src_frame);
|
|
|
|
gst_video_frame_unmap (&dest_frame);
|
2011-10-17 15:25:11 +00:00
|
|
|
|
|
|
|
g_free (tmpbuf);
|
|
|
|
}
|
|
|
|
|
2013-11-30 00:59:55 +00:00
|
|
|
/*
|
|
|
|
* A OVER B alpha compositing operation, with:
|
|
|
|
* alphaG: global alpha to apply on the source color
|
|
|
|
* -> only needed for premultiplied source
|
|
|
|
* alphaA: source pixel alpha
|
|
|
|
* colorA: source pixel color
|
|
|
|
* alphaB: destination pixel alpha
|
|
|
|
* colorB: destination pixel color
|
|
|
|
* alphaD: blended pixel alpha
|
|
|
|
* -> only needed for premultiplied destination
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Source non-premultiplied, Destination non-premultiplied */
|
|
|
|
#define OVER00(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
|
|
|
|
((colorA * alphaA + colorB * alphaB * (255 - alphaA) / 255) / alphaD)
|
|
|
|
|
|
|
|
/* Source premultiplied, Destination non-premultiplied */
|
|
|
|
#define OVER10(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
|
|
|
|
((colorA * alphaG + colorB * alphaB * (255 - alphaA) / 255) / alphaD)
|
|
|
|
|
|
|
|
/* Source non-premultiplied, Destination premultiplied */
|
|
|
|
#define OVER01(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
|
|
|
|
((colorA * alphaA + colorB * (255 - alphaA)) / 255)
|
|
|
|
|
|
|
|
/* Source premultiplied, Destination premultiplied */
|
|
|
|
#define OVER11(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
|
|
|
|
((colorA * alphaG + colorB * (255 - alphaA)) / 255)
|
|
|
|
|
|
|
|
#define BLENDC(op, global_alpha, aa, ca, ab, cb, dest_alpha) \
|
|
|
|
G_STMT_START { \
|
|
|
|
int c = op(global_alpha, aa, ca, ab, cb, dest_alpha); \
|
|
|
|
cb = MIN(c, 255); \
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
|
|
|
2014-11-01 14:28:55 +00:00
|
|
|
/**
|
|
|
|
* gst_video_blend:
|
2012-07-11 13:27:11 +00:00
|
|
|
* @dest: The #GstVideoFrame where to blend @src in
|
|
|
|
* @src: the #GstVideoFrame that we want to blend into
|
2011-10-17 15:25:11 +00:00
|
|
|
* @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
|
2012-03-24 19:31:29 +00:00
|
|
|
* @global_alpha: the global_alpha each per-pixel alpha value is multiplied
|
|
|
|
* with
|
2011-10-17 15:25:11 +00:00
|
|
|
*
|
|
|
|
* Lets you blend the @src image into the @dest image
|
|
|
|
*/
|
|
|
|
gboolean
|
2012-05-30 07:25:12 +00:00
|
|
|
gst_video_blend (GstVideoFrame * dest,
|
2012-06-20 08:35:04 +00:00
|
|
|
GstVideoFrame * src, gint x, gint y, gfloat global_alpha)
|
2011-10-17 15:25:11 +00:00
|
|
|
{
|
2014-11-10 11:06:35 +00:00
|
|
|
gint i, j, global_alpha_val, src_width, src_height, dest_width, dest_height;
|
|
|
|
gint src_xoff = 0, src_yoff = 0;
|
2011-12-14 14:14:47 +00:00
|
|
|
guint8 *tmpdestline = NULL, *tmpsrcline = NULL;
|
2012-06-28 16:16:20 +00:00
|
|
|
gboolean src_premultiplied_alpha, dest_premultiplied_alpha;
|
|
|
|
void (*matrix) (guint8 * tmpline, guint width);
|
2012-11-07 11:17:14 +00:00
|
|
|
const GstVideoFormatInfo *sinfo, *dinfo, *dunpackinfo, *sunpackinfo;
|
2011-12-14 14:14:47 +00:00
|
|
|
|
2012-03-25 00:22:29 +00:00
|
|
|
g_assert (dest != NULL);
|
|
|
|
g_assert (src != NULL);
|
|
|
|
|
2013-11-30 00:59:55 +00:00
|
|
|
global_alpha_val = 255.0 * global_alpha;
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
dest_premultiplied_alpha =
|
|
|
|
GST_VIDEO_INFO_FLAGS (&dest->info) & GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
|
|
|
|
src_premultiplied_alpha =
|
|
|
|
GST_VIDEO_INFO_FLAGS (&src->info) & GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
|
|
|
|
|
|
|
|
src_width = GST_VIDEO_FRAME_WIDTH (src);
|
|
|
|
src_height = GST_VIDEO_FRAME_HEIGHT (src);
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
dest_width = GST_VIDEO_FRAME_WIDTH (dest);
|
|
|
|
dest_height = GST_VIDEO_FRAME_HEIGHT (dest);
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
ensure_debug_category ();
|
|
|
|
|
2014-11-16 16:34:31 +00:00
|
|
|
GST_LOG ("blend src %dx%d onto dest %dx%d @ %d,%d", src_width, src_height,
|
|
|
|
dest_width, dest_height, x, y);
|
|
|
|
|
2014-11-10 11:06:35 +00:00
|
|
|
/* In case overlay is completely outside the video, dont render */
|
|
|
|
if (x + src_width <= 0 || y + src_height <= 0
|
|
|
|
|| x >= dest_width || y >= dest_height) {
|
|
|
|
goto nothing_to_do;
|
|
|
|
}
|
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
dinfo = gst_video_format_get_info (GST_VIDEO_FRAME_FORMAT (dest));
|
|
|
|
sinfo = gst_video_format_get_info (GST_VIDEO_FRAME_FORMAT (src));
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
if (!sinfo || !dinfo)
|
2011-10-17 15:25:11 +00:00
|
|
|
goto failed;
|
|
|
|
|
2012-11-07 11:17:14 +00:00
|
|
|
dunpackinfo = gst_video_format_get_info (dinfo->unpack_format);
|
|
|
|
sunpackinfo = gst_video_format_get_info (sinfo->unpack_format);
|
|
|
|
|
|
|
|
if (dunpackinfo == NULL || sunpackinfo == NULL)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
g_assert (GST_VIDEO_FORMAT_INFO_BITS (sunpackinfo) == 8);
|
|
|
|
|
|
|
|
if (GST_VIDEO_FORMAT_INFO_BITS (dunpackinfo) != 8)
|
|
|
|
goto unpack_format_not_supported;
|
|
|
|
|
|
|
|
tmpdestline = g_malloc (sizeof (guint8) * (dest_width + 8) * 4);
|
2014-11-16 16:31:45 +00:00
|
|
|
tmpsrcline = g_malloc (sizeof (guint8) * (src_width + 8) * 4);
|
2012-11-07 11:17:14 +00:00
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
matrix = matrix_identity;
|
|
|
|
if (GST_VIDEO_INFO_IS_RGB (&src->info) != GST_VIDEO_INFO_IS_RGB (&dest->info)) {
|
|
|
|
if (GST_VIDEO_INFO_IS_RGB (&src->info)) {
|
2011-12-14 14:14:47 +00:00
|
|
|
if (src_premultiplied_alpha) {
|
2012-06-28 16:16:20 +00:00
|
|
|
matrix = matrix_prea_rgb_to_yuv;
|
2011-12-14 14:14:47 +00:00
|
|
|
src_premultiplied_alpha = FALSE;
|
|
|
|
} else {
|
2012-06-28 16:16:20 +00:00
|
|
|
matrix = matrix_rgb_to_yuv;
|
2011-12-14 14:14:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-28 16:16:20 +00:00
|
|
|
matrix = matrix_yuv_to_rgb;
|
2011-12-14 14:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2014-11-10 11:06:35 +00:00
|
|
|
/* If we're here we know that the overlay image fully or
|
|
|
|
* partially overlaps with the video frame */
|
2014-11-16 16:34:31 +00:00
|
|
|
|
2014-11-10 11:06:35 +00:00
|
|
|
/* adjust src image for negative offsets */
|
2011-10-17 15:25:11 +00:00
|
|
|
if (x < 0) {
|
2014-11-10 11:06:35 +00:00
|
|
|
src_xoff = -x;
|
|
|
|
src_width -= src_xoff;
|
2011-10-17 15:25:11 +00:00
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y < 0) {
|
2014-11-10 11:06:35 +00:00
|
|
|
src_yoff = -y;
|
|
|
|
src_height -= src_yoff;
|
2011-10-17 15:25:11 +00:00
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-16 16:34:31 +00:00
|
|
|
/* adjust width/height to render (i.e. clip source image) if the source
|
|
|
|
* image extends beyond the right or bottom border of the video surface */
|
2012-06-28 16:16:20 +00:00
|
|
|
if (x + src_width > dest_width)
|
2014-10-23 09:11:13 +00:00
|
|
|
src_width = dest_width - x;
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
if (y + src_height > dest_height)
|
2014-10-23 09:11:13 +00:00
|
|
|
src_height = dest_height - y;
|
2012-03-25 00:31:41 +00:00
|
|
|
|
2011-10-17 15:25:11 +00:00
|
|
|
/* Mainloop doing the needed conversions, and blending */
|
2014-11-10 11:06:35 +00:00
|
|
|
for (i = y; i < y + src_height; i++, src_yoff++) {
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
dinfo->unpack_func (dinfo, 0, tmpdestline, dest->data, dest->info.stride,
|
|
|
|
0, i, dest_width);
|
|
|
|
sinfo->unpack_func (sinfo, 0, tmpsrcline, src->data, src->info.stride,
|
2014-11-27 15:28:36 +00:00
|
|
|
src_xoff, src_yoff, src_width);
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2014-11-16 16:34:31 +00:00
|
|
|
/* FIXME: use the x parameter of the unpack func once implemented */
|
2012-06-28 16:16:20 +00:00
|
|
|
tmpdestline += 4 * x;
|
2011-10-17 15:25:11 +00:00
|
|
|
|
2014-11-16 23:26:45 +00:00
|
|
|
matrix (tmpsrcline, src_width);
|
|
|
|
|
2013-11-30 00:59:55 +00:00
|
|
|
#define BLENDLOOP(op, alpha_val) \
|
|
|
|
G_STMT_START { \
|
|
|
|
for (j = 0; j < src_width * 4; j += 4) { \
|
|
|
|
guint8 asrc, adst; \
|
|
|
|
gint final_alpha; \
|
|
|
|
\
|
|
|
|
asrc = tmpsrcline[j] * alpha_val / 255; \
|
|
|
|
if (!asrc) \
|
|
|
|
continue; \
|
|
|
|
\
|
|
|
|
adst = tmpdestline[j]; \
|
|
|
|
final_alpha = asrc + adst * (255 - asrc) / 255; \
|
|
|
|
tmpdestline[j] = final_alpha; \
|
|
|
|
if (final_alpha == 0) \
|
|
|
|
final_alpha = 1; \
|
|
|
|
\
|
|
|
|
BLENDC (op, alpha_val, asrc, tmpsrcline[j + 1], adst, tmpdestline[j + 1], final_alpha); \
|
|
|
|
BLENDC (op, alpha_val, asrc, tmpsrcline[j + 2], adst, tmpdestline[j + 2], final_alpha); \
|
|
|
|
BLENDC (op, alpha_val, asrc, tmpsrcline[j + 3], adst, tmpdestline[j + 3], final_alpha); \
|
|
|
|
} \
|
|
|
|
} G_STMT_END
|
2011-12-14 14:14:47 +00:00
|
|
|
|
2012-03-25 00:22:29 +00:00
|
|
|
if (G_LIKELY (global_alpha == 1.0)) {
|
2012-06-28 16:16:20 +00:00
|
|
|
if (src_premultiplied_alpha && dest_premultiplied_alpha) {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER11, 255);
|
2012-06-28 16:16:20 +00:00
|
|
|
} else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER01, 255);
|
2012-06-28 16:16:20 +00:00
|
|
|
} else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER10, 255);
|
2012-03-25 00:22:29 +00:00
|
|
|
} else {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER00, 255);
|
2012-03-25 00:22:29 +00:00
|
|
|
}
|
2011-12-14 14:14:47 +00:00
|
|
|
} else {
|
2012-06-28 16:16:20 +00:00
|
|
|
if (src_premultiplied_alpha && dest_premultiplied_alpha) {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER11, global_alpha_val);
|
2012-06-28 16:16:20 +00:00
|
|
|
} else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER01, global_alpha_val);
|
2012-06-28 16:16:20 +00:00
|
|
|
} else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER10, global_alpha_val);
|
2012-03-25 00:22:29 +00:00
|
|
|
} else {
|
2013-11-30 00:59:55 +00:00
|
|
|
BLENDLOOP (OVER00, global_alpha_val);
|
2012-03-25 00:22:29 +00:00
|
|
|
}
|
2011-10-17 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 14:14:47 +00:00
|
|
|
#undef BLENDLOOP
|
|
|
|
|
2014-11-16 16:34:31 +00:00
|
|
|
/* undo previous pointer adjustments to pass right pointer to g_free */
|
2012-06-28 16:16:20 +00:00
|
|
|
tmpdestline -= 4 * x;
|
|
|
|
|
2011-10-17 15:25:11 +00:00
|
|
|
/* FIXME
|
|
|
|
* #if G_BYTE_ORDER == LITTLE_ENDIAN
|
2012-07-23 15:10:08 +00:00
|
|
|
* video_orc_blend_little (tmpdestline, tmpsrcline, dest->width);
|
2011-10-17 15:25:11 +00:00
|
|
|
* #else
|
2012-07-23 15:10:08 +00:00
|
|
|
* video_orc_blend_big (tmpdestline, tmpsrcline, src->width);
|
2011-10-17 15:25:11 +00:00
|
|
|
* #endif
|
|
|
|
*/
|
|
|
|
|
2012-06-28 16:16:20 +00:00
|
|
|
dinfo->pack_func (dinfo, 0, tmpdestline, dest_width,
|
|
|
|
dest->data, dest->info.stride, dest->info.chroma_site, i, dest_width);
|
2011-10-17 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (tmpdestline);
|
|
|
|
g_free (tmpsrcline);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
2012-11-07 11:17:14 +00:00
|
|
|
{
|
|
|
|
GST_WARNING ("Could not do the blending");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
unpack_format_not_supported:
|
|
|
|
{
|
|
|
|
GST_FIXME ("video format %s not supported yet for blending",
|
|
|
|
gst_video_format_to_string (dinfo->unpack_format));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-11-10 11:06:35 +00:00
|
|
|
nothing_to_do:
|
|
|
|
{
|
|
|
|
GST_LOG
|
|
|
|
("Overlay completely outside the video surface, hence not rendering");
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-10-17 15:25:11 +00:00
|
|
|
}
|