2015-06-30 15:59:12 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2015 Lubosz Sarnecki <lubosz.sarnecki@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2016-11-03 01:03:24 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstgloverlaycompositor
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: GstGLOverlayCompositor
|
2016-11-03 01:03:24 +00:00
|
|
|
* @short_description: Composite multiple overlays using OpenGL
|
|
|
|
* @see_also: #GstGLMemory, #GstGLContext
|
|
|
|
*/
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "gstgloverlaycompositor.h"
|
|
|
|
|
2017-07-07 15:15:12 +00:00
|
|
|
#include "gstglcontext.h"
|
|
|
|
#include "gstglfuncs.h"
|
|
|
|
#include "gstglmemory.h"
|
|
|
|
#include "gstglshader.h"
|
|
|
|
#include "gstglslstage.h"
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_gl_overlay_compositor_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_gl_overlay_compositor_debug
|
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* GstGLCompositionOverlay object is internally used by GstGLOverlayCompositor
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#define GST_TYPE_GL_COMPOSITION_OVERLAY (gst_gl_composition_overlay_get_type())
|
|
|
|
#define GST_GL_COMPOSITION_OVERLAY(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GL_COMPOSITION_OVERLAY,\
|
|
|
|
GstGLCompositionOverlay))
|
|
|
|
|
|
|
|
typedef struct _GstGLCompositionOverlay GstGLCompositionOverlay;
|
|
|
|
typedef struct _GstGLCompositionOverlayClass GstGLCompositionOverlayClass;
|
|
|
|
|
|
|
|
static GType gst_gl_composition_overlay_get_type (void);
|
|
|
|
|
2015-08-08 18:45:42 +00:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
const gchar *fragment_shader =
|
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n"
|
|
|
|
"varying vec2 v_texcoord;\n"
|
|
|
|
"uniform sampler2D tex;\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" vec4 t = texture2D(tex, v_texcoord);\n"
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
" gl_FragColor = t.bgra;\n"
|
|
|
|
#else
|
|
|
|
" gl_FragColor = t.gbar;\n"
|
|
|
|
#endif
|
|
|
|
"}";
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
struct _GstGLCompositionOverlay
|
|
|
|
{
|
|
|
|
GstObject parent;
|
|
|
|
GstGLContext *context;
|
|
|
|
|
|
|
|
GLuint vao;
|
|
|
|
GLuint index_buffer;
|
|
|
|
GLuint position_buffer;
|
|
|
|
GLuint texcoord_buffer;
|
|
|
|
GLint position_attrib;
|
|
|
|
GLint texcoord_attrib;
|
|
|
|
|
|
|
|
GLfloat positions[16];
|
|
|
|
|
|
|
|
GLuint texture_id;
|
|
|
|
GstGLMemory *gl_memory;
|
|
|
|
GstVideoOverlayRectangle *rectangle;
|
2018-09-27 03:31:09 +00:00
|
|
|
|
|
|
|
gboolean yinvert;
|
2015-07-22 18:05:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GstGLCompositionOverlayClass
|
|
|
|
{
|
|
|
|
GstObjectClass object_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstGLCompositionOverlay, gst_gl_composition_overlay,
|
|
|
|
GST_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_init_vertex_buffer (GstGLContext * context,
|
|
|
|
gpointer overlay_pointer)
|
|
|
|
{
|
|
|
|
const GstGLFuncs *gl = context->gl_vtable;
|
|
|
|
GstGLCompositionOverlay *overlay =
|
|
|
|
(GstGLCompositionOverlay *) overlay_pointer;
|
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
static const GLfloat texcoords[] = {
|
|
|
|
1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
1.0f, 1.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GLushort indices[] = {
|
|
|
|
0, 1, 2, 0, 2, 3
|
|
|
|
};
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
if (gl->GenVertexArrays) {
|
|
|
|
gl->GenVertexArrays (1, &overlay->vao);
|
|
|
|
gl->BindVertexArray (overlay->vao);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->GenBuffers (1, &overlay->position_buffer);
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, overlay->position_buffer);
|
|
|
|
gl->BufferData (GL_ARRAY_BUFFER, 4 * 4 * sizeof (GLfloat), overlay->positions,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
/* Load the vertex position */
|
|
|
|
gl->VertexAttribPointer (overlay->position_attrib, 4, GL_FLOAT, GL_FALSE,
|
|
|
|
4 * sizeof (GLfloat), NULL);
|
|
|
|
|
|
|
|
gl->GenBuffers (1, &overlay->texcoord_buffer);
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, overlay->texcoord_buffer);
|
|
|
|
gl->BufferData (GL_ARRAY_BUFFER, 4 * 2 * sizeof (GLfloat), texcoords,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
/* Load the texture coordinate */
|
|
|
|
gl->VertexAttribPointer (overlay->texcoord_attrib, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
2 * sizeof (GLfloat), NULL);
|
|
|
|
|
|
|
|
gl->GenBuffers (1, &overlay->index_buffer);
|
|
|
|
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, overlay->index_buffer);
|
|
|
|
gl->BufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (indices), indices,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
gl->EnableVertexAttribArray (overlay->position_attrib);
|
|
|
|
gl->EnableVertexAttribArray (overlay->texcoord_attrib);
|
|
|
|
|
|
|
|
if (gl->GenVertexArrays) {
|
|
|
|
gl->BindVertexArray (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_free_vertex_buffer (GstGLContext * context,
|
|
|
|
gpointer overlay_pointer)
|
|
|
|
{
|
|
|
|
const GstGLFuncs *gl = context->gl_vtable;
|
|
|
|
GstGLCompositionOverlay *overlay =
|
|
|
|
(GstGLCompositionOverlay *) overlay_pointer;
|
|
|
|
if (overlay->vao) {
|
|
|
|
gl->DeleteVertexArrays (1, &overlay->vao);
|
|
|
|
overlay->vao = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlay->position_buffer) {
|
|
|
|
gl->DeleteBuffers (1, &overlay->position_buffer);
|
|
|
|
overlay->position_buffer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlay->texcoord_buffer) {
|
|
|
|
gl->DeleteBuffers (1, &overlay->position_buffer);
|
|
|
|
overlay->position_buffer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlay->index_buffer) {
|
|
|
|
gl->DeleteBuffers (1, &overlay->index_buffer);
|
|
|
|
overlay->index_buffer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_bind_vertex_buffer (GstGLCompositionOverlay *
|
|
|
|
overlay)
|
|
|
|
{
|
|
|
|
const GstGLFuncs *gl = overlay->context->gl_vtable;
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, overlay->position_buffer);
|
|
|
|
gl->VertexAttribPointer (overlay->position_attrib, 4, GL_FLOAT, GL_FALSE,
|
|
|
|
4 * sizeof (GLfloat), NULL);
|
|
|
|
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, overlay->texcoord_buffer);
|
|
|
|
gl->VertexAttribPointer (overlay->texcoord_attrib, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
2 * sizeof (GLfloat), NULL);
|
|
|
|
|
|
|
|
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, overlay->index_buffer);
|
|
|
|
|
|
|
|
gl->EnableVertexAttribArray (overlay->position_attrib);
|
|
|
|
gl->EnableVertexAttribArray (overlay->texcoord_attrib);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstGLCompositionOverlay *overlay;
|
|
|
|
|
|
|
|
overlay = GST_GL_COMPOSITION_OVERLAY (object);
|
|
|
|
|
|
|
|
if (overlay->gl_memory)
|
|
|
|
gst_memory_unref ((GstMemory *) overlay->gl_memory);
|
2015-07-22 19:56:34 +00:00
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
if (overlay->context) {
|
|
|
|
gst_gl_context_thread_add (overlay->context,
|
|
|
|
gst_gl_composition_overlay_free_vertex_buffer, overlay);
|
|
|
|
gst_object_unref (overlay->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_gl_composition_overlay_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_class_init (GstGLCompositionOverlayClass * klass)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (klass)->finalize = gst_gl_composition_overlay_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_init (GstGLCompositionOverlay * overlay)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_add_transformation (GstGLCompositionOverlay *
|
|
|
|
overlay, GstBuffer * video_buffer)
|
|
|
|
{
|
|
|
|
gint comp_x, comp_y;
|
|
|
|
guint comp_width, comp_height;
|
|
|
|
GstVideoMeta *meta;
|
|
|
|
guint width, height;
|
2018-09-27 03:31:09 +00:00
|
|
|
gfloat yswap;
|
2015-07-22 18:05:34 +00:00
|
|
|
|
|
|
|
float rel_x, rel_y, rel_w, rel_h;
|
|
|
|
|
|
|
|
meta = gst_buffer_get_video_meta (video_buffer);
|
|
|
|
|
|
|
|
gst_video_overlay_rectangle_get_render_rectangle (overlay->rectangle,
|
|
|
|
&comp_x, &comp_y, &comp_width, &comp_height);
|
|
|
|
|
|
|
|
width = meta->width;
|
|
|
|
height = meta->height;
|
|
|
|
|
|
|
|
/* calculate relative position */
|
|
|
|
rel_x = (float) comp_x / (float) width;
|
|
|
|
rel_y = (float) comp_y / (float) height;
|
|
|
|
|
|
|
|
rel_w = (float) comp_width / (float) width;
|
|
|
|
rel_h = (float) comp_height / (float) height;
|
|
|
|
|
|
|
|
/* transform from [0,1] to [-1,1], invert y axis */
|
|
|
|
rel_x = rel_x * 2.0 - 1.0;
|
|
|
|
rel_y = (1.0 - rel_y) * 2.0 - 1.0;
|
2018-09-27 03:31:09 +00:00
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
rel_w = rel_w * 2.0;
|
|
|
|
rel_h = rel_h * 2.0;
|
|
|
|
|
2018-09-27 03:31:09 +00:00
|
|
|
yswap = overlay->yinvert ? -1. : 1.;
|
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
/* initialize position array */
|
|
|
|
overlay->positions[0] = rel_x + rel_w;
|
2018-09-27 03:31:09 +00:00
|
|
|
overlay->positions[1] = rel_y * yswap;
|
2015-07-22 18:05:34 +00:00
|
|
|
overlay->positions[2] = 0.0;
|
|
|
|
overlay->positions[3] = 1.0;
|
|
|
|
overlay->positions[4] = rel_x;
|
2018-09-27 03:31:09 +00:00
|
|
|
overlay->positions[5] = rel_y * yswap;
|
2015-07-22 18:05:34 +00:00
|
|
|
overlay->positions[6] = 0.0;
|
|
|
|
overlay->positions[7] = 1.0;
|
|
|
|
overlay->positions[8] = rel_x;
|
2018-09-27 03:31:09 +00:00
|
|
|
overlay->positions[9] = (rel_y - rel_h) * yswap;
|
2015-07-22 18:05:34 +00:00
|
|
|
overlay->positions[10] = 0.0;
|
|
|
|
overlay->positions[11] = 1.0;
|
|
|
|
overlay->positions[12] = rel_x + rel_w;
|
2018-09-27 03:31:09 +00:00
|
|
|
overlay->positions[13] = (rel_y - rel_h) * yswap;
|
2015-07-22 18:05:34 +00:00
|
|
|
overlay->positions[14] = 0.0;
|
|
|
|
overlay->positions[15] = 1.0;
|
|
|
|
|
|
|
|
gst_gl_context_thread_add (overlay->context,
|
|
|
|
gst_gl_composition_overlay_free_vertex_buffer, overlay);
|
|
|
|
|
|
|
|
gst_gl_context_thread_add (overlay->context,
|
|
|
|
gst_gl_composition_overlay_init_vertex_buffer, overlay);
|
|
|
|
|
|
|
|
GST_DEBUG
|
|
|
|
("overlay position: (%d,%d) size: %dx%d video size: %dx%d",
|
|
|
|
comp_x, comp_y, comp_width, comp_height, meta->width, meta->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* helper object API functions */
|
|
|
|
|
|
|
|
static GstGLCompositionOverlay *
|
|
|
|
gst_gl_composition_overlay_new (GstGLContext * context,
|
|
|
|
GstVideoOverlayRectangle * rectangle,
|
|
|
|
GLint position_attrib, GLint texcoord_attrib)
|
|
|
|
{
|
|
|
|
GstGLCompositionOverlay *overlay =
|
|
|
|
g_object_new (GST_TYPE_GL_COMPOSITION_OVERLAY, NULL);
|
|
|
|
|
|
|
|
overlay->gl_memory = NULL;
|
|
|
|
overlay->texture_id = -1;
|
|
|
|
overlay->rectangle = rectangle;
|
|
|
|
overlay->context = gst_object_ref (context);
|
|
|
|
overlay->vao = 0;
|
|
|
|
overlay->position_attrib = position_attrib;
|
|
|
|
overlay->texcoord_attrib = texcoord_attrib;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (overlay, "Created new GstGLCompositionOverlay");
|
|
|
|
|
|
|
|
return overlay;
|
|
|
|
}
|
|
|
|
|
2015-07-22 19:56:34 +00:00
|
|
|
static void
|
|
|
|
_video_frame_unmap_and_free (gpointer user_data)
|
|
|
|
{
|
|
|
|
GstVideoFrame *frame = user_data;
|
|
|
|
|
|
|
|
gst_video_frame_unmap (frame);
|
|
|
|
g_slice_free (GstVideoFrame, frame);
|
|
|
|
}
|
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_upload (GstGLCompositionOverlay * overlay,
|
|
|
|
GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstGLMemory *comp_gl_memory = NULL;
|
|
|
|
GstBuffer *comp_buffer = NULL;
|
|
|
|
GstBuffer *overlay_buffer = NULL;
|
2015-07-22 19:56:34 +00:00
|
|
|
GstVideoInfo vinfo;
|
|
|
|
GstVideoMeta *vmeta;
|
|
|
|
GstVideoFrame *comp_frame;
|
2015-07-22 18:05:34 +00:00
|
|
|
GstVideoFrame gl_frame;
|
2018-10-04 13:02:05 +00:00
|
|
|
GstVideoOverlayFormatFlags flags;
|
|
|
|
GstVideoOverlayFormatFlags alpha_flags;
|
|
|
|
|
|
|
|
flags = gst_video_overlay_rectangle_get_flags (overlay->rectangle);
|
|
|
|
|
|
|
|
if (flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA) {
|
|
|
|
alpha_flags = GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
|
|
|
|
} else if (!overlay->context->gl_vtable->BlendFuncSeparate) {
|
|
|
|
GST_FIXME_OBJECT (overlay, "No separate blend mode function, "
|
|
|
|
"cannot perform correct blending of unmultipled alpha in OpenGL. "
|
|
|
|
"Software converting");
|
|
|
|
alpha_flags = GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
|
|
|
|
} else {
|
|
|
|
alpha_flags = 0;
|
|
|
|
}
|
2015-07-22 18:05:34 +00:00
|
|
|
|
|
|
|
comp_buffer =
|
|
|
|
gst_video_overlay_rectangle_get_pixels_unscaled_argb (overlay->rectangle,
|
2018-10-04 13:02:05 +00:00
|
|
|
alpha_flags);
|
2015-07-22 18:05:34 +00:00
|
|
|
|
2015-07-22 19:56:34 +00:00
|
|
|
comp_frame = g_slice_new (GstVideoFrame);
|
2015-07-22 18:05:34 +00:00
|
|
|
|
2015-07-22 19:56:34 +00:00
|
|
|
vmeta = gst_buffer_get_video_meta (comp_buffer);
|
|
|
|
gst_video_info_set_format (&vinfo, vmeta->format, vmeta->width,
|
|
|
|
vmeta->height);
|
|
|
|
vinfo.stride[0] = vmeta->stride[0];
|
2015-07-22 18:05:34 +00:00
|
|
|
|
2015-07-22 19:56:34 +00:00
|
|
|
if (gst_video_frame_map (comp_frame, &vinfo, comp_buffer, GST_MAP_READ)) {
|
2015-12-16 07:41:06 +00:00
|
|
|
GstGLVideoAllocationParams *params;
|
|
|
|
GstGLBaseMemoryAllocator *mem_allocator;
|
|
|
|
GstAllocator *allocator;
|
|
|
|
|
2015-12-18 02:17:34 +00:00
|
|
|
allocator =
|
|
|
|
GST_ALLOCATOR (gst_gl_memory_allocator_get_default (overlay->context));
|
2015-12-16 07:41:06 +00:00
|
|
|
mem_allocator = GST_GL_BASE_MEMORY_ALLOCATOR (allocator);
|
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
gst_gl_composition_overlay_add_transformation (overlay, buf);
|
|
|
|
|
2015-12-16 07:41:06 +00:00
|
|
|
params = gst_gl_video_allocation_params_new_wrapped_data (overlay->context,
|
|
|
|
NULL, &comp_frame->info, 0, NULL, GST_GL_TEXTURE_TARGET_2D,
|
2017-03-13 03:28:47 +00:00
|
|
|
GST_GL_RGBA, comp_frame->data[0], comp_frame,
|
2016-06-28 03:51:22 +00:00
|
|
|
_video_frame_unmap_and_free);
|
2015-12-16 07:41:06 +00:00
|
|
|
|
2015-07-22 18:05:34 +00:00
|
|
|
comp_gl_memory =
|
2015-12-16 07:41:06 +00:00
|
|
|
(GstGLMemory *) gst_gl_base_memory_alloc (mem_allocator,
|
|
|
|
(GstGLAllocationParams *) params);
|
|
|
|
|
|
|
|
gst_gl_allocation_params_free ((GstGLAllocationParams *) params);
|
|
|
|
gst_object_unref (allocator);
|
2015-07-22 18:05:34 +00:00
|
|
|
|
|
|
|
overlay_buffer = gst_buffer_new ();
|
2015-07-22 19:56:34 +00:00
|
|
|
gst_buffer_append_memory (overlay_buffer, (GstMemory *) comp_gl_memory);
|
2015-07-22 18:05:34 +00:00
|
|
|
|
2015-07-22 19:56:34 +00:00
|
|
|
if (!gst_video_frame_map (&gl_frame, &comp_frame->info, overlay_buffer,
|
2015-07-22 18:05:34 +00:00
|
|
|
GST_MAP_READ | GST_MAP_GL)) {
|
|
|
|
gst_buffer_unref (overlay_buffer);
|
2015-07-22 19:56:34 +00:00
|
|
|
_video_frame_unmap_and_free (comp_frame);
|
2015-07-22 18:05:34 +00:00
|
|
|
GST_WARNING_OBJECT (overlay, "Cannot upload overlay texture");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-22 19:56:34 +00:00
|
|
|
gst_memory_ref ((GstMemory *) comp_gl_memory);
|
|
|
|
overlay->gl_memory = comp_gl_memory;
|
2015-07-22 18:05:34 +00:00
|
|
|
overlay->texture_id = comp_gl_memory->tex_id;
|
2015-07-22 19:56:34 +00:00
|
|
|
|
|
|
|
gst_buffer_unref (overlay_buffer);
|
2015-07-22 18:05:34 +00:00
|
|
|
gst_video_frame_unmap (&gl_frame);
|
|
|
|
|
2015-07-22 19:56:34 +00:00
|
|
|
GST_DEBUG ("uploaded overlay texture %d", overlay->texture_id);
|
|
|
|
} else {
|
|
|
|
g_slice_free (GstVideoFrame, comp_frame);
|
2015-07-22 18:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_composition_overlay_draw (GstGLCompositionOverlay * overlay,
|
|
|
|
GstGLShader * shader)
|
|
|
|
{
|
|
|
|
const GstGLFuncs *gl = overlay->context->gl_vtable;
|
|
|
|
if (gl->GenVertexArrays)
|
|
|
|
gl->BindVertexArray (overlay->vao);
|
|
|
|
else
|
|
|
|
gst_gl_composition_overlay_bind_vertex_buffer (overlay);
|
|
|
|
|
|
|
|
if (overlay->texture_id != -1)
|
|
|
|
gl->BindTexture (GL_TEXTURE_2D, overlay->texture_id);
|
|
|
|
gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:31:09 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gboolean yinvert;
|
|
|
|
} GstGLOverlayCompositorPrivate;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_YINVERT,
|
|
|
|
};
|
2015-07-22 18:05:34 +00:00
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* GstGLOverlayCompositor object, the public helper object to render
|
|
|
|
* GstVideoCompositionOverlayMeta
|
|
|
|
********************************************************************/
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
#define DEBUG_INIT \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_overlay_compositor_debug, \
|
|
|
|
"gloverlaycompositor", 0, "overlaycompositor");
|
|
|
|
|
2018-09-27 03:31:09 +00:00
|
|
|
/* this matches what glimagesink does as this was publicized before being used
|
|
|
|
* in other elements that draw in different orientations */
|
|
|
|
#define DEFAULT_YINVERT FALSE
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLOverlayCompositor, gst_gl_overlay_compositor,
|
2018-10-04 13:02:05 +00:00
|
|
|
GST_TYPE_OBJECT, G_ADD_PRIVATE (GstGLOverlayCompositor); DEBUG_INIT);
|
2015-06-30 15:59:12 +00:00
|
|
|
|
|
|
|
static void gst_gl_overlay_compositor_finalize (GObject * object);
|
2018-09-27 03:31:09 +00:00
|
|
|
static void gst_gl_overlay_compositor_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_gl_overlay_compositor_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
static gboolean _is_rectangle_in_overlays (GList * overlays,
|
|
|
|
GstVideoOverlayRectangle * rectangle);
|
|
|
|
static gboolean _is_overlay_in_rectangles (GstVideoOverlayComposition *
|
|
|
|
composition, GstGLCompositionOverlay * overlay);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_overlay_compositor_class_init (GstGLOverlayCompositorClass * klass)
|
|
|
|
{
|
2018-09-27 03:31:09 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_gl_overlay_compositor_finalize;
|
|
|
|
gobject_class->set_property = gst_gl_overlay_compositor_set_property;
|
|
|
|
gobject_class->get_property = gst_gl_overlay_compositor_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_YINVERT,
|
|
|
|
g_param_spec_boolean ("yinvert",
|
|
|
|
"Y-Invert",
|
|
|
|
"Whether to invert the output across a horizintal axis",
|
|
|
|
DEFAULT_YINVERT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2015-06-30 15:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_overlay_compositor_init (GstGLOverlayCompositor * compositor)
|
|
|
|
{
|
2018-09-27 03:31:09 +00:00
|
|
|
GstGLOverlayCompositorPrivate *priv =
|
|
|
|
gst_gl_overlay_compositor_get_instance_private (compositor);
|
|
|
|
|
|
|
|
priv->yinvert = DEFAULT_YINVERT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_overlay_compositor_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstGLOverlayCompositor *self = GST_GL_OVERLAY_COMPOSITOR (object);
|
|
|
|
GstGLOverlayCompositorPrivate *priv =
|
|
|
|
gst_gl_overlay_compositor_get_instance_private (self);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_YINVERT:
|
|
|
|
/* XXX: invalidiate all current rectangles on a change */
|
|
|
|
priv->yinvert = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_overlay_compositor_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstGLOverlayCompositor *self = GST_GL_OVERLAY_COMPOSITOR (object);
|
|
|
|
GstGLOverlayCompositorPrivate *priv =
|
|
|
|
gst_gl_overlay_compositor_get_instance_private (self);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_YINVERT:
|
|
|
|
g_value_set_boolean (value, priv->yinvert);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2015-06-30 15:59:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 01:27:45 +00:00
|
|
|
static void
|
|
|
|
gst_gl_overlay_compositor_init_gl (GstGLContext * context,
|
|
|
|
gpointer compositor_pointer)
|
|
|
|
{
|
|
|
|
GstGLOverlayCompositor *compositor =
|
|
|
|
(GstGLOverlayCompositor *) compositor_pointer;
|
2015-09-04 06:02:32 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (!(compositor->shader =
|
|
|
|
gst_gl_shader_new_link_with_stages (context, &error,
|
|
|
|
gst_glsl_stage_new_default_vertex (context),
|
|
|
|
gst_glsl_stage_new_with_string (context, GL_FRAGMENT_SHADER,
|
|
|
|
GST_GLSL_VERSION_NONE,
|
|
|
|
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
|
|
|
|
fragment_shader), NULL))) {
|
|
|
|
GST_ERROR_OBJECT (compositor, "could not initialize shader: %s",
|
|
|
|
error->message);
|
|
|
|
return;
|
2015-07-22 01:27:45 +00:00
|
|
|
}
|
2015-09-04 06:02:32 +00:00
|
|
|
|
|
|
|
compositor->position_attrib =
|
|
|
|
gst_gl_shader_get_attribute_location (compositor->shader, "a_position");
|
|
|
|
compositor->texcoord_attrib =
|
|
|
|
gst_gl_shader_get_attribute_location (compositor->shader, "a_texcoord");
|
2015-07-22 01:27:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
GstGLOverlayCompositor *
|
|
|
|
gst_gl_overlay_compositor_new (GstGLContext * context)
|
|
|
|
{
|
|
|
|
GstGLOverlayCompositor *compositor =
|
|
|
|
g_object_new (GST_TYPE_GL_OVERLAY_COMPOSITOR, NULL);
|
|
|
|
|
2017-05-15 17:31:31 +00:00
|
|
|
gst_object_ref_sink (compositor);
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
compositor->context = gst_object_ref (context);
|
|
|
|
|
2015-07-22 01:27:45 +00:00
|
|
|
gst_gl_context_thread_add (compositor->context,
|
|
|
|
gst_gl_overlay_compositor_init_gl, compositor);
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
GST_DEBUG_OBJECT (compositor, "Created new GstGLOverlayCompositor");
|
|
|
|
|
|
|
|
return compositor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_overlay_compositor_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstGLOverlayCompositor *compositor;
|
|
|
|
|
|
|
|
compositor = GST_GL_OVERLAY_COMPOSITOR (object);
|
|
|
|
|
|
|
|
gst_gl_overlay_compositor_free_overlays (compositor);
|
|
|
|
|
|
|
|
if (compositor->context)
|
|
|
|
gst_object_unref (compositor->context);
|
|
|
|
|
2015-07-22 01:27:45 +00:00
|
|
|
if (compositor->shader) {
|
|
|
|
gst_object_unref (compositor->shader);
|
|
|
|
compositor->shader = NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
G_OBJECT_CLASS (gst_gl_overlay_compositor_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_is_rectangle_in_overlays (GList * overlays,
|
|
|
|
GstVideoOverlayRectangle * rectangle)
|
|
|
|
{
|
2015-07-21 12:11:17 +00:00
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = overlays; l != NULL; l = l->next) {
|
2015-06-30 15:59:12 +00:00
|
|
|
GstGLCompositionOverlay *overlay = (GstGLCompositionOverlay *) l->data;
|
|
|
|
if (overlay->rectangle == rectangle)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_is_overlay_in_rectangles (GstVideoOverlayComposition * composition,
|
|
|
|
GstGLCompositionOverlay * overlay)
|
|
|
|
{
|
2015-07-21 12:11:17 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < gst_video_overlay_composition_n_rectangles (composition); i++) {
|
2015-06-30 15:59:12 +00:00
|
|
|
GstVideoOverlayRectangle *rectangle =
|
|
|
|
gst_video_overlay_composition_get_rectangle (composition, i);
|
|
|
|
if (overlay->rectangle == rectangle)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_gl_overlay_compositor_free_overlays (GstGLOverlayCompositor * compositor)
|
|
|
|
{
|
|
|
|
GList *l = compositor->overlays;
|
|
|
|
while (l != NULL) {
|
|
|
|
GList *next = l->next;
|
|
|
|
GstGLCompositionOverlay *overlay = (GstGLCompositionOverlay *) l->data;
|
|
|
|
compositor->overlays = g_list_delete_link (compositor->overlays, l);
|
|
|
|
gst_object_unref (overlay);
|
|
|
|
l = next;
|
|
|
|
}
|
|
|
|
g_list_free (compositor->overlays);
|
|
|
|
compositor->overlays = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_gl_overlay_compositor_upload_overlays (GstGLOverlayCompositor * compositor,
|
2015-07-22 03:48:25 +00:00
|
|
|
GstBuffer * buf)
|
2015-06-30 15:59:12 +00:00
|
|
|
{
|
|
|
|
GstVideoOverlayCompositionMeta *composition_meta;
|
2018-09-27 03:31:09 +00:00
|
|
|
GstGLOverlayCompositorPrivate *priv =
|
|
|
|
gst_gl_overlay_compositor_get_instance_private (compositor);
|
2015-06-30 15:59:12 +00:00
|
|
|
|
|
|
|
composition_meta = gst_buffer_get_video_overlay_composition_meta (buf);
|
|
|
|
if (composition_meta) {
|
|
|
|
GstVideoOverlayComposition *composition = NULL;
|
2015-07-21 12:11:17 +00:00
|
|
|
guint num_overlays, i;
|
2015-06-30 15:59:12 +00:00
|
|
|
GList *l = compositor->overlays;
|
|
|
|
|
|
|
|
GST_DEBUG ("GstVideoOverlayCompositionMeta found.");
|
|
|
|
|
|
|
|
composition = composition_meta->overlay;
|
|
|
|
num_overlays = gst_video_overlay_composition_n_rectangles (composition);
|
|
|
|
|
|
|
|
/* add new overlays to list */
|
2015-07-21 12:11:17 +00:00
|
|
|
for (i = 0; i < num_overlays; i++) {
|
2015-06-30 15:59:12 +00:00
|
|
|
GstVideoOverlayRectangle *rectangle =
|
|
|
|
gst_video_overlay_composition_get_rectangle (composition, i);
|
|
|
|
|
|
|
|
if (!_is_rectangle_in_overlays (compositor->overlays, rectangle)) {
|
|
|
|
GstGLCompositionOverlay *overlay =
|
|
|
|
gst_gl_composition_overlay_new (compositor->context, rectangle,
|
2015-07-22 01:27:45 +00:00
|
|
|
compositor->position_attrib, compositor->texcoord_attrib);
|
2017-05-15 17:31:31 +00:00
|
|
|
gst_object_ref_sink (overlay);
|
2018-09-27 03:31:09 +00:00
|
|
|
overlay->yinvert = priv->yinvert;
|
2015-07-22 01:27:45 +00:00
|
|
|
|
2015-07-22 03:48:25 +00:00
|
|
|
gst_gl_composition_overlay_upload (overlay, buf);
|
2015-06-30 15:59:12 +00:00
|
|
|
|
|
|
|
compositor->overlays = g_list_append (compositor->overlays, overlay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove old overlays from list */
|
|
|
|
while (l != NULL) {
|
|
|
|
GList *next = l->next;
|
|
|
|
GstGLCompositionOverlay *overlay = (GstGLCompositionOverlay *) l->data;
|
|
|
|
if (!_is_overlay_in_rectangles (composition, overlay)) {
|
|
|
|
compositor->overlays = g_list_delete_link (compositor->overlays, l);
|
|
|
|
gst_object_unref (overlay);
|
|
|
|
}
|
|
|
|
l = next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gst_gl_overlay_compositor_free_overlays (compositor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-22 01:27:45 +00:00
|
|
|
gst_gl_overlay_compositor_draw_overlays (GstGLOverlayCompositor * compositor)
|
2015-06-30 15:59:12 +00:00
|
|
|
{
|
|
|
|
const GstGLFuncs *gl = compositor->context->gl_vtable;
|
|
|
|
if (compositor->overlays != NULL) {
|
2015-07-21 12:11:17 +00:00
|
|
|
GList *l;
|
|
|
|
|
2015-07-22 01:27:45 +00:00
|
|
|
gl->Enable (GL_BLEND);
|
|
|
|
|
|
|
|
gst_gl_shader_use (compositor->shader);
|
|
|
|
gl->ActiveTexture (GL_TEXTURE0);
|
|
|
|
gst_gl_shader_set_uniform_1i (compositor->shader, "tex", 0);
|
|
|
|
|
2015-07-21 12:11:17 +00:00
|
|
|
for (l = compositor->overlays; l != NULL; l = l->next) {
|
2015-06-30 15:59:12 +00:00
|
|
|
GstGLCompositionOverlay *overlay = (GstGLCompositionOverlay *) l->data;
|
2018-10-04 13:02:05 +00:00
|
|
|
GstVideoOverlayFormatFlags flags;
|
|
|
|
|
|
|
|
flags = gst_video_overlay_rectangle_get_flags (overlay->rectangle);
|
|
|
|
|
|
|
|
if (flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA
|
|
|
|
|| !gl->BlendFuncSeparate) {
|
|
|
|
gl->BlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
} else {
|
|
|
|
gl->BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE,
|
|
|
|
GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
}
|
2015-07-22 01:27:45 +00:00
|
|
|
gst_gl_composition_overlay_draw (overlay, compositor->shader);
|
2015-06-30 15:59:12 +00:00
|
|
|
}
|
2015-07-22 01:27:45 +00:00
|
|
|
|
2015-06-30 15:59:12 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, 0);
|
2015-08-15 13:02:33 +00:00
|
|
|
gl->Disable (GL_BLEND);
|
2015-06-30 15:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GstCaps *
|
|
|
|
gst_gl_overlay_compositor_add_caps (GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstCaps *composition_caps;
|
2015-07-21 12:11:17 +00:00
|
|
|
int i;
|
2015-06-30 15:59:12 +00:00
|
|
|
|
|
|
|
composition_caps = gst_caps_copy (caps);
|
|
|
|
|
2015-07-21 12:11:17 +00:00
|
|
|
for (i = 0; i < gst_caps_get_size (composition_caps); i++) {
|
2015-06-30 15:59:12 +00:00
|
|
|
GstCapsFeatures *f = gst_caps_get_features (composition_caps, i);
|
2018-09-27 03:30:35 +00:00
|
|
|
if (!gst_caps_features_is_any (f))
|
|
|
|
gst_caps_features_add (f,
|
|
|
|
GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
|
2015-06-30 15:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
caps = gst_caps_merge (composition_caps, caps);
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|