2011-01-28 00:14:04 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2011> Jon Nordby <jononor@gmail.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-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-01-28 00:14:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-cairooverlay
|
2018-10-22 09:39:24 +00:00
|
|
|
* @title: cairooverlay
|
2011-01-28 00:14:04 +00:00
|
|
|
*
|
|
|
|
* cairooverlay renders an overlay using a application provided render function.
|
|
|
|
*
|
|
|
|
* The full example can be found in tests/examples/cairo/cairo_overlay.c
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
|
|
|
* ## Example code
|
2011-01-28 00:14:04 +00:00
|
|
|
* |[
|
|
|
|
*
|
2021-11-11 19:11:25 +00:00
|
|
|
* #include <gst/gst.h>
|
|
|
|
* #include <gst/video/video.h>
|
2011-01-28 00:14:04 +00:00
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* typedef struct {
|
|
|
|
* gboolean valid;
|
|
|
|
* int width;
|
|
|
|
* int height;
|
|
|
|
* } CairoOverlayState;
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
2011-01-28 00:14:04 +00:00
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* static void
|
|
|
|
* prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
|
|
|
|
* {
|
|
|
|
* CairoOverlayState *state = (CairoOverlayState *)user_data;
|
|
|
|
*
|
2021-11-11 19:11:25 +00:00
|
|
|
* gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
|
|
|
|
* state->valid = TRUE;
|
2011-01-28 00:14:04 +00:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* static void
|
2018-10-22 09:39:24 +00:00
|
|
|
* draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
|
2011-01-28 00:14:04 +00:00
|
|
|
* guint64 duration, gpointer user_data)
|
|
|
|
* {
|
|
|
|
* CairoOverlayState *s = (CairoOverlayState *)user_data;
|
|
|
|
* double scale;
|
|
|
|
*
|
2021-11-11 19:11:25 +00:00
|
|
|
* if (!s->valid)
|
2011-01-28 00:14:04 +00:00
|
|
|
* return;
|
|
|
|
*
|
|
|
|
* scale = 2*(((timestamp/(int)1e7) % 70)+30)/100.0;
|
2021-11-11 19:11:25 +00:00
|
|
|
* cairo_translate(cr, s->width/2, (s->height/2)-30);
|
2011-01-28 00:14:04 +00:00
|
|
|
* cairo_scale (cr, scale, scale);
|
|
|
|
*
|
|
|
|
* cairo_move_to (cr, 0, 0);
|
|
|
|
* cairo_curve_to (cr, 0,-30, -50,-30, -50,0);
|
|
|
|
* cairo_curve_to (cr, -50,30, 0,35, 0,60 );
|
2018-10-22 09:39:24 +00:00
|
|
|
* cairo_curve_to (cr, 0,35, 50,30, 50,0 ); *
|
2011-01-28 00:14:04 +00:00
|
|
|
* cairo_curve_to (cr, 50,-30, 0,-30, 0,0 );
|
|
|
|
* cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
|
|
|
|
* cairo_fill (cr);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
|
|
|
|
*
|
|
|
|
* g_signal_connect (cairo_overlay, "draw", G_CALLBACK (draw_overlay),
|
|
|
|
* overlay_state);
|
2018-10-22 09:39:24 +00:00
|
|
|
* g_signal_connect (cairo_overlay, "caps-changed",
|
2011-01-28 00:14:04 +00:00
|
|
|
* G_CALLBACK (prepare_overlay), overlay_state);
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* ]|
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
2011-01-28 00:14:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstcairooverlay.h"
|
|
|
|
|
|
|
|
#include <gst/video/video.h>
|
|
|
|
|
|
|
|
#include <cairo.h>
|
|
|
|
|
2014-01-31 14:17:54 +00:00
|
|
|
/* RGB16 is native-endianness in GStreamer */
|
2011-01-28 00:14:04 +00:00
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
2014-01-31 14:17:54 +00:00
|
|
|
#define TEMPLATE_CAPS GST_VIDEO_CAPS_MAKE("{ BGRx, BGRA, RGB16 }")
|
2011-01-28 00:14:04 +00:00
|
|
|
#else
|
2014-01-31 14:17:54 +00:00
|
|
|
#define TEMPLATE_CAPS GST_VIDEO_CAPS_MAKE("{ xRGB, ARGB, RGB16 }")
|
2011-01-28 00:14:04 +00:00
|
|
|
#endif
|
|
|
|
|
2021-02-16 16:38:46 +00:00
|
|
|
GST_DEBUG_CATEGORY (cairo_debug);
|
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
static GstStaticPadTemplate gst_cairo_overlay_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-03-02 22:14:36 +00:00
|
|
|
GST_STATIC_CAPS (TEMPLATE_CAPS)
|
2011-01-28 00:14:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_cairo_overlay_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-03-02 22:14:36 +00:00
|
|
|
GST_STATIC_CAPS (TEMPLATE_CAPS)
|
2011-01-28 00:14:04 +00:00
|
|
|
);
|
|
|
|
|
2018-09-25 14:44:15 +00:00
|
|
|
G_DEFINE_TYPE (GstCairoOverlay, gst_cairo_overlay, GST_TYPE_BASE_TRANSFORM);
|
2021-02-16 16:38:46 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (cairooverlay, "cairooverlay",
|
|
|
|
GST_RANK_NONE, GST_TYPE_CAIRO_OVERLAY, GST_DEBUG_CATEGORY_INIT (cairo_debug,
|
|
|
|
"cairo", 0, "Cairo elements"););
|
2018-09-25 12:31:20 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_DRAW_ON_TRANSPARENT_SURFACE,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DEFAULT_DRAW_ON_TRANSPARENT_SURFACE (FALSE)
|
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
SIGNAL_DRAW,
|
|
|
|
SIGNAL_CAPS_CHANGED,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
2011-03-02 22:14:36 +00:00
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
static guint gst_cairo_overlay_signals[N_SIGNALS];
|
|
|
|
|
2018-09-25 12:31:20 +00:00
|
|
|
static void
|
|
|
|
gst_cairo_overlay_set_property (GObject * object, guint property_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (object);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (overlay);
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_DRAW_ON_TRANSPARENT_SURFACE:
|
|
|
|
overlay->draw_on_transparent_surface = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (overlay);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_cairo_overlay_get_property (GObject * object, guint property_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (object);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (overlay);
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_DRAW_ON_TRANSPARENT_SURFACE:
|
|
|
|
g_value_set_boolean (value, overlay->draw_on_transparent_surface);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (overlay);
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:02:26 +00:00
|
|
|
static gboolean
|
|
|
|
gst_cairo_overlay_query (GstBaseTransform * trans, GstPadDirection direction,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (trans);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_ALLOCATION:
|
|
|
|
{
|
|
|
|
/* We're always running in passthrough mode, which means that
|
|
|
|
* basetransform just passes through ALLOCATION queries and
|
|
|
|
* never ever calls BaseTransform::decide_allocation().
|
|
|
|
*
|
|
|
|
* We hook into the query handling for that reason
|
|
|
|
*/
|
|
|
|
overlay->attach_compo_to_buffer = FALSE;
|
|
|
|
|
|
|
|
if (!GST_BASE_TRANSFORM_CLASS (gst_cairo_overlay_parent_class)->query
|
|
|
|
(trans, direction, query)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
overlay->attach_compo_to_buffer = gst_query_find_allocation_meta (query,
|
|
|
|
GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
GST_BASE_TRANSFORM_CLASS (gst_cairo_overlay_parent_class)->query
|
|
|
|
(trans, direction, query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
static gboolean
|
2018-09-25 14:44:15 +00:00
|
|
|
gst_cairo_overlay_set_caps (GstBaseTransform * trans, GstCaps * in_caps,
|
|
|
|
GstCaps * out_caps)
|
2011-01-28 00:14:04 +00:00
|
|
|
{
|
2018-09-25 14:44:15 +00:00
|
|
|
GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (trans);
|
|
|
|
|
|
|
|
if (!gst_video_info_from_caps (&overlay->info, in_caps))
|
|
|
|
return FALSE;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED], 0,
|
2013-01-01 11:52:09 +00:00
|
|
|
in_caps, NULL);
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2013-01-01 11:52:09 +00:00
|
|
|
return TRUE;
|
2011-01-28 00:14:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 12:34:40 +00:00
|
|
|
/* Copy from video-overlay-composition.c */
|
|
|
|
static void
|
|
|
|
gst_video_overlay_rectangle_premultiply_0 (GstVideoFrame * frame)
|
|
|
|
{
|
|
|
|
int i, j;
|
2018-12-07 17:09:30 +00:00
|
|
|
int width = GST_VIDEO_FRAME_WIDTH (frame);
|
|
|
|
int height = GST_VIDEO_FRAME_HEIGHT (frame);
|
|
|
|
int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
|
|
|
|
guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
|
|
|
|
|
|
|
|
for (j = 0; j < height; ++j) {
|
2018-09-25 12:34:40 +00:00
|
|
|
guint8 *line;
|
|
|
|
|
2018-12-07 17:09:30 +00:00
|
|
|
line = data;
|
|
|
|
line += stride * j;
|
|
|
|
for (i = 0; i < width; ++i) {
|
2018-09-25 12:34:40 +00:00
|
|
|
int a = line[0];
|
|
|
|
line[1] = line[1] * a / 255;
|
|
|
|
line[2] = line[2] * a / 255;
|
|
|
|
line[3] = line[3] * a / 255;
|
|
|
|
line += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy from video-overlay-composition.c */
|
|
|
|
static void
|
|
|
|
gst_video_overlay_rectangle_premultiply_3 (GstVideoFrame * frame)
|
|
|
|
{
|
|
|
|
int i, j;
|
2018-12-07 17:09:30 +00:00
|
|
|
int width = GST_VIDEO_FRAME_WIDTH (frame);
|
|
|
|
int height = GST_VIDEO_FRAME_HEIGHT (frame);
|
|
|
|
int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
|
|
|
|
guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
|
|
|
|
|
|
|
|
for (j = 0; j < height; ++j) {
|
2018-09-25 12:34:40 +00:00
|
|
|
guint8 *line;
|
|
|
|
|
2018-12-07 17:09:30 +00:00
|
|
|
line = data;
|
|
|
|
line += stride * j;
|
|
|
|
for (i = 0; i < width; ++i) {
|
2018-09-25 12:34:40 +00:00
|
|
|
int a = line[3];
|
|
|
|
line[0] = line[0] * a / 255;
|
|
|
|
line[1] = line[1] * a / 255;
|
|
|
|
line[2] = line[2] * a / 255;
|
|
|
|
line += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy from video-overlay-composition.c */
|
|
|
|
static void
|
|
|
|
gst_video_overlay_rectangle_premultiply (GstVideoFrame * frame)
|
|
|
|
{
|
|
|
|
gint alpha_offset;
|
|
|
|
|
|
|
|
alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
|
|
|
|
switch (alpha_offset) {
|
|
|
|
case 0:
|
|
|
|
gst_video_overlay_rectangle_premultiply_0 (frame);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
gst_video_overlay_rectangle_premultiply_3 (frame);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy from video-overlay-composition.c */
|
|
|
|
static void
|
|
|
|
gst_video_overlay_rectangle_unpremultiply_0 (GstVideoFrame * frame)
|
|
|
|
{
|
|
|
|
int i, j;
|
2018-12-07 17:09:30 +00:00
|
|
|
int width = GST_VIDEO_FRAME_WIDTH (frame);
|
|
|
|
int height = GST_VIDEO_FRAME_HEIGHT (frame);
|
|
|
|
int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
|
|
|
|
guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
|
|
|
|
|
|
|
|
for (j = 0; j < height; ++j) {
|
2018-09-25 12:34:40 +00:00
|
|
|
guint8 *line;
|
|
|
|
|
2018-12-07 17:09:30 +00:00
|
|
|
line = data;
|
|
|
|
line += stride * j;
|
|
|
|
for (i = 0; i < width; ++i) {
|
2018-09-25 12:34:40 +00:00
|
|
|
int a = line[0];
|
|
|
|
if (a) {
|
|
|
|
line[1] = MIN ((line[1] * 255 + a / 2) / a, 255);
|
|
|
|
line[2] = MIN ((line[2] * 255 + a / 2) / a, 255);
|
|
|
|
line[3] = MIN ((line[3] * 255 + a / 2) / a, 255);
|
|
|
|
}
|
|
|
|
line += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy from video-overlay-composition.c */
|
|
|
|
static void
|
|
|
|
gst_video_overlay_rectangle_unpremultiply_3 (GstVideoFrame * frame)
|
|
|
|
{
|
|
|
|
int i, j;
|
2018-12-07 17:09:30 +00:00
|
|
|
int width = GST_VIDEO_FRAME_WIDTH (frame);
|
|
|
|
int height = GST_VIDEO_FRAME_HEIGHT (frame);
|
|
|
|
int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
|
|
|
|
guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
|
|
|
|
|
|
|
|
for (j = 0; j < height; ++j) {
|
2018-09-25 12:34:40 +00:00
|
|
|
guint8 *line;
|
|
|
|
|
2018-12-07 17:09:30 +00:00
|
|
|
line = data;
|
|
|
|
line += stride * j;
|
|
|
|
for (i = 0; i < width; ++i) {
|
2018-09-25 12:34:40 +00:00
|
|
|
int a = line[3];
|
|
|
|
if (a) {
|
|
|
|
line[0] = MIN ((line[0] * 255 + a / 2) / a, 255);
|
|
|
|
line[1] = MIN ((line[1] * 255 + a / 2) / a, 255);
|
|
|
|
line[2] = MIN ((line[2] * 255 + a / 2) / a, 255);
|
|
|
|
}
|
|
|
|
line += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy from video-overlay-composition.c */
|
|
|
|
static void
|
|
|
|
gst_video_overlay_rectangle_unpremultiply (GstVideoFrame * frame)
|
|
|
|
{
|
|
|
|
gint alpha_offset;
|
|
|
|
|
|
|
|
alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
|
|
|
|
switch (alpha_offset) {
|
|
|
|
case 0:
|
|
|
|
gst_video_overlay_rectangle_unpremultiply_0 (frame);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
gst_video_overlay_rectangle_unpremultiply_3 (frame);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
static GstFlowReturn
|
2018-09-25 14:44:15 +00:00
|
|
|
gst_cairo_overlay_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
|
2011-01-28 00:14:04 +00:00
|
|
|
{
|
2018-09-25 14:44:15 +00:00
|
|
|
GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (trans);
|
|
|
|
GstVideoFrame frame;
|
2011-01-28 00:14:04 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
cairo_format_t format;
|
2018-09-25 12:31:20 +00:00
|
|
|
gboolean draw_on_transparent_surface = overlay->draw_on_transparent_surface;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2018-09-25 14:44:15 +00:00
|
|
|
switch (GST_VIDEO_INFO_FORMAT (&overlay->info)) {
|
2014-01-31 14:17:54 +00:00
|
|
|
case GST_VIDEO_FORMAT_ARGB:
|
|
|
|
case GST_VIDEO_FORMAT_BGRA:
|
|
|
|
format = CAIRO_FORMAT_ARGB32;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_xRGB:
|
|
|
|
case GST_VIDEO_FORMAT_BGRx:
|
|
|
|
format = CAIRO_FORMAT_RGB24;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_RGB16:
|
|
|
|
format = CAIRO_FORMAT_RGB16_565;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
GST_WARNING ("No matching cairo format for %s",
|
2018-09-25 14:44:15 +00:00
|
|
|
gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (&overlay->info)));
|
2014-01-31 14:17:54 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2018-09-25 14:44:15 +00:00
|
|
|
/* If we need to map the buffer writable, do so */
|
|
|
|
if (!draw_on_transparent_surface || !overlay->attach_compo_to_buffer) {
|
|
|
|
if (!gst_video_frame_map (&frame, &overlay->info, buf, GST_MAP_READWRITE)) {
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
frame.buffer = NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-25 12:31:20 +00:00
|
|
|
if (draw_on_transparent_surface) {
|
|
|
|
surface =
|
|
|
|
cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
2018-09-25 14:44:15 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&overlay->info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&overlay->info));
|
2018-09-25 12:31:20 +00:00
|
|
|
} else {
|
2018-09-25 12:34:40 +00:00
|
|
|
if (format == CAIRO_FORMAT_ARGB32)
|
2018-09-25 14:44:15 +00:00
|
|
|
gst_video_overlay_rectangle_premultiply (&frame);
|
2018-09-25 12:34:40 +00:00
|
|
|
|
2018-09-25 12:31:20 +00:00
|
|
|
surface =
|
2018-09-25 14:44:15 +00:00
|
|
|
cairo_image_surface_create_for_data (GST_VIDEO_FRAME_PLANE_DATA (&frame,
|
|
|
|
0), format, GST_VIDEO_FRAME_WIDTH (&frame),
|
|
|
|
GST_VIDEO_FRAME_HEIGHT (&frame), GST_VIDEO_FRAME_PLANE_STRIDE (&frame,
|
2018-09-25 12:31:20 +00:00
|
|
|
0));
|
|
|
|
}
|
2013-01-01 11:52:09 +00:00
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
if (G_UNLIKELY (!surface))
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
if (G_UNLIKELY (!cr)) {
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_DRAW], 0,
|
2018-09-25 14:44:15 +00:00
|
|
|
cr, GST_BUFFER_PTS (buf), GST_BUFFER_DURATION (buf), NULL);
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
cairo_destroy (cr);
|
2018-09-25 12:31:20 +00:00
|
|
|
|
|
|
|
if (draw_on_transparent_surface) {
|
|
|
|
guint size;
|
|
|
|
GstBuffer *surface_buffer;
|
|
|
|
GstVideoOverlayRectangle *rect;
|
|
|
|
GstVideoOverlayComposition *composition;
|
|
|
|
gsize offset[GST_VIDEO_MAX_PLANES] = { 0, };
|
|
|
|
gint stride[GST_VIDEO_MAX_PLANES] = { 0, };
|
|
|
|
|
|
|
|
size =
|
|
|
|
cairo_image_surface_get_height (surface) *
|
|
|
|
cairo_image_surface_get_stride (surface);
|
|
|
|
stride[0] = cairo_image_surface_get_stride (surface);
|
|
|
|
|
|
|
|
/* Create a GstVideoOverlayComposition for blending, this handles
|
|
|
|
* pre-multiplied alpha correctly */
|
|
|
|
surface_buffer =
|
|
|
|
gst_buffer_new_wrapped_full (0, cairo_image_surface_get_data (surface),
|
|
|
|
size, 0, size, surface, (GDestroyNotify) cairo_surface_destroy);
|
|
|
|
gst_buffer_add_video_meta_full (surface_buffer, GST_VIDEO_FRAME_FLAG_NONE,
|
|
|
|
(G_BYTE_ORDER ==
|
|
|
|
G_LITTLE_ENDIAN ? GST_VIDEO_FORMAT_BGRA : GST_VIDEO_FORMAT_ARGB),
|
2018-09-25 14:44:15 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&overlay->info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&overlay->info), 1, offset, stride);
|
2018-09-25 12:31:20 +00:00
|
|
|
rect =
|
|
|
|
gst_video_overlay_rectangle_new_raw (surface_buffer, 0, 0,
|
2018-09-25 14:44:15 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&overlay->info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&overlay->info),
|
2018-09-25 12:31:20 +00:00
|
|
|
GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
|
|
|
|
gst_buffer_unref (surface_buffer);
|
|
|
|
|
2018-09-25 14:02:26 +00:00
|
|
|
if (overlay->attach_compo_to_buffer) {
|
|
|
|
GstVideoOverlayCompositionMeta *composition_meta;
|
|
|
|
|
2018-09-25 14:44:15 +00:00
|
|
|
composition_meta = gst_buffer_get_video_overlay_composition_meta (buf);
|
2018-09-25 14:02:26 +00:00
|
|
|
if (composition_meta) {
|
|
|
|
GstVideoOverlayComposition *merged_composition =
|
|
|
|
gst_video_overlay_composition_copy (composition_meta->overlay);
|
|
|
|
gst_video_overlay_composition_add_rectangle (merged_composition, rect);
|
|
|
|
gst_video_overlay_composition_unref (composition_meta->overlay);
|
|
|
|
composition_meta->overlay = merged_composition;
|
|
|
|
gst_video_overlay_rectangle_unref (rect);
|
|
|
|
} else {
|
|
|
|
composition = gst_video_overlay_composition_new (rect);
|
|
|
|
gst_video_overlay_rectangle_unref (rect);
|
2018-09-25 14:44:15 +00:00
|
|
|
gst_buffer_add_video_overlay_composition_meta (buf, composition);
|
2018-09-25 14:02:26 +00:00
|
|
|
gst_video_overlay_composition_unref (composition);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
composition = gst_video_overlay_composition_new (rect);
|
|
|
|
gst_video_overlay_rectangle_unref (rect);
|
2018-09-25 14:44:15 +00:00
|
|
|
gst_video_overlay_composition_blend (composition, &frame);
|
2018-09-25 14:02:26 +00:00
|
|
|
gst_video_overlay_composition_unref (composition);
|
|
|
|
}
|
2018-09-25 12:31:20 +00:00
|
|
|
} else {
|
|
|
|
cairo_surface_destroy (surface);
|
2018-09-25 12:34:40 +00:00
|
|
|
if (format == CAIRO_FORMAT_ARGB32)
|
2018-09-25 14:44:15 +00:00
|
|
|
gst_video_overlay_rectangle_unpremultiply (&frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.buffer) {
|
|
|
|
gst_video_frame_unmap (&frame);
|
2018-09-25 12:31:20 +00:00
|
|
|
}
|
2011-03-02 22:14:36 +00:00
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_cairo_overlay_class_init (GstCairoOverlayClass * klass)
|
|
|
|
{
|
2018-09-25 14:02:26 +00:00
|
|
|
GstBaseTransformClass *btrans_class;
|
2013-01-01 11:52:09 +00:00
|
|
|
GstElementClass *element_class;
|
2018-09-25 12:31:20 +00:00
|
|
|
GObjectClass *gobject_class;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2018-09-25 14:02:26 +00:00
|
|
|
btrans_class = (GstBaseTransformClass *) klass;
|
2013-01-01 11:52:09 +00:00
|
|
|
element_class = (GstElementClass *) klass;
|
2018-09-25 12:31:20 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2018-09-25 14:44:15 +00:00
|
|
|
btrans_class->set_caps = gst_cairo_overlay_set_caps;
|
|
|
|
btrans_class->transform_ip = gst_cairo_overlay_transform_ip;
|
2018-09-25 14:02:26 +00:00
|
|
|
btrans_class->query = gst_cairo_overlay_query;
|
|
|
|
|
2018-09-25 12:31:20 +00:00
|
|
|
gobject_class->set_property = gst_cairo_overlay_set_property;
|
|
|
|
gobject_class->get_property = gst_cairo_overlay_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_DRAW_ON_TRANSPARENT_SURFACE,
|
|
|
|
g_param_spec_boolean ("draw-on-transparent-surface",
|
|
|
|
"Draw on transparent surface",
|
|
|
|
"Let the draw signal work on a transparent surface "
|
|
|
|
"and blend the results with the video at a later time",
|
|
|
|
DEFAULT_DRAW_ON_TRANSPARENT_SURFACE,
|
|
|
|
GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
|
|
|
|
| G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
/**
|
|
|
|
* GstCairoOverlay::draw:
|
|
|
|
* @overlay: Overlay element emitting the signal.
|
|
|
|
* @cr: Cairo context to draw to.
|
2011-03-02 22:14:36 +00:00
|
|
|
* @timestamp: Timestamp (see #GstClockTime) of the current buffer.
|
|
|
|
* @duration: Duration (see #GstClockTime) of the current buffer.
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
2011-01-28 00:14:04 +00:00
|
|
|
* This signal is emitted when the overlay should be drawn.
|
|
|
|
*/
|
|
|
|
gst_cairo_overlay_signals[SIGNAL_DRAW] =
|
|
|
|
g_signal_new ("draw",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
2019-08-26 06:03:24 +00:00
|
|
|
0, 0, NULL, NULL, NULL,
|
2011-01-28 00:14:04 +00:00
|
|
|
G_TYPE_NONE, 3, CAIRO_GOBJECT_TYPE_CONTEXT, G_TYPE_UINT64, G_TYPE_UINT64);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GstCairoOverlay::caps-changed:
|
|
|
|
* @overlay: Overlay element emitting the signal.
|
2011-03-02 22:14:36 +00:00
|
|
|
* @caps: The #GstCaps of the element.
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
2011-01-28 00:14:04 +00:00
|
|
|
* This signal is emitted when the caps of the element has changed.
|
|
|
|
*/
|
|
|
|
gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED] =
|
|
|
|
g_signal_new ("caps-changed",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
2019-08-26 06:03:24 +00:00
|
|
|
0, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_CAPS);
|
2013-01-01 11:52:09 +00:00
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (element_class, "Cairo overlay",
|
|
|
|
"Filter/Editor/Video",
|
|
|
|
"Render overlay on a video stream using Cairo",
|
|
|
|
"Jon Nordby <jononor@gmail.com>");
|
|
|
|
|
2016-03-04 01:30:12 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_cairo_overlay_sink_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_cairo_overlay_src_template);
|
2011-01-28 00:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-01 11:52:09 +00:00
|
|
|
gst_cairo_overlay_init (GstCairoOverlay * overlay)
|
2011-01-28 00:14:04 +00:00
|
|
|
{
|
2013-01-01 11:52:09 +00:00
|
|
|
/* nothing to do */
|
2011-01-28 00:14:04 +00:00
|
|
|
}
|