2015-02-12 06:59:27 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2012-2014 Matthew Waters <ystree00@gmail.com>
|
|
|
|
* Copyright (C) 2015 Matthew Waters <matthew@centricular.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., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/gl/gl.h>
|
|
|
|
#include "gstglcolorconvertelement.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_gl_color_convert_element_debug);
|
2019-03-06 12:29:56 +00:00
|
|
|
#define gst_gl_color_convert_element_parent_class parent_class
|
2015-02-12 06:59:27 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_gl_color_convert_element_debug
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLColorConvertElement, gst_gl_color_convert_element,
|
|
|
|
GST_TYPE_GL_BASE_FILTER,
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_color_convert_element_debug,
|
2015-05-06 13:46:49 +00:00
|
|
|
"glconvertelement", 0, "convert");
|
|
|
|
);
|
2015-02-12 06:59:27 +00:00
|
|
|
|
|
|
|
static gboolean gst_gl_color_convert_element_set_caps (GstBaseTransform * bt,
|
|
|
|
GstCaps * in_caps, GstCaps * out_caps);
|
|
|
|
static GstCaps *gst_gl_color_convert_element_transform_caps (GstBaseTransform *
|
|
|
|
bt, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
|
|
|
|
static gboolean gst_gl_color_convert_element_get_unit_size (GstBaseTransform *
|
|
|
|
trans, GstCaps * caps, gsize * size);
|
2015-08-21 00:27:34 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans,
|
|
|
|
GstQuery * query, GType api, const GstStructure * params);
|
2015-02-12 06:59:27 +00:00
|
|
|
static gboolean gst_gl_color_convert_element_decide_allocation (GstBaseTransform
|
|
|
|
* trans, GstQuery * query);
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt,
|
|
|
|
GstBuffer * inbuf, GstBuffer ** outbuf);
|
|
|
|
static GstFlowReturn gst_gl_color_convert_element_transform (GstBaseTransform *
|
|
|
|
bt, GstBuffer * inbuf, GstBuffer * outbuf);
|
2015-04-19 17:16:55 +00:00
|
|
|
static GstCaps *gst_gl_color_convert_element_fixate_caps (GstBaseTransform *
|
|
|
|
bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
|
2019-03-06 12:29:56 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_gl_color_convert_element_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
2015-02-12 06:59:27 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_gl_color_convert_element_src_pad_template =
|
2015-05-06 13:46:49 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
2015-02-12 06:59:27 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2015-10-28 13:44:26 +00:00
|
|
|
GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS));
|
2015-05-06 13:46:49 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_gl_color_convert_element_sink_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2015-10-28 13:44:26 +00:00
|
|
|
GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS));
|
2015-02-12 06:59:27 +00:00
|
|
|
|
2015-04-15 12:49:02 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_color_convert_element_stop (GstBaseTransform * bt)
|
|
|
|
{
|
|
|
|
GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
|
|
|
|
|
|
|
|
if (convert->convert) {
|
|
|
|
gst_object_unref (convert->convert);
|
|
|
|
convert->convert = NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-06 12:29:56 +00:00
|
|
|
return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (bt);
|
2015-04-15 12:49:02 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 06:59:27 +00:00
|
|
|
static void
|
|
|
|
gst_gl_color_convert_element_class_init (GstGLColorConvertElementClass * klass)
|
|
|
|
{
|
|
|
|
GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
bt_class->transform_caps = gst_gl_color_convert_element_transform_caps;
|
|
|
|
bt_class->set_caps = gst_gl_color_convert_element_set_caps;
|
|
|
|
bt_class->get_unit_size = gst_gl_color_convert_element_get_unit_size;
|
2015-08-21 00:27:34 +00:00
|
|
|
bt_class->filter_meta = gst_gl_color_convert_element_filter_meta;
|
2015-02-12 06:59:27 +00:00
|
|
|
bt_class->decide_allocation = gst_gl_color_convert_element_decide_allocation;
|
|
|
|
bt_class->prepare_output_buffer =
|
|
|
|
gst_gl_color_convert_element_prepare_output_buffer;
|
|
|
|
bt_class->transform = gst_gl_color_convert_element_transform;
|
2015-04-15 12:49:02 +00:00
|
|
|
bt_class->stop = gst_gl_color_convert_element_stop;
|
2015-04-19 17:16:55 +00:00
|
|
|
bt_class->fixate_caps = gst_gl_color_convert_element_fixate_caps;
|
2015-02-12 06:59:27 +00:00
|
|
|
|
|
|
|
bt_class->passthrough_on_same_caps = TRUE;
|
|
|
|
|
2019-03-06 12:29:56 +00:00
|
|
|
element_class->change_state = gst_gl_color_convert_element_change_state;
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_gl_color_convert_element_src_pad_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_gl_color_convert_element_sink_pad_template);
|
2015-02-12 06:59:27 +00:00
|
|
|
|
|
|
|
gst_element_class_set_metadata (element_class,
|
|
|
|
"OpenGL color converter", "Filter/Converter/Video",
|
|
|
|
"Converts between color spaces using OpenGL shaders",
|
|
|
|
"Matthew Waters <matthew@centricular.com>");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_color_convert_element_init (GstGLColorConvertElement * convert)
|
|
|
|
{
|
|
|
|
gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (convert),
|
|
|
|
TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gl_color_convert_element_set_caps (GstBaseTransform * bt,
|
|
|
|
GstCaps * in_caps, GstCaps * out_caps)
|
|
|
|
{
|
|
|
|
GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
|
|
|
|
|
2018-05-03 03:59:07 +00:00
|
|
|
if (!gst_gl_color_convert_set_caps (convert->convert, in_caps, out_caps))
|
|
|
|
return FALSE;
|
2015-05-06 13:46:49 +00:00
|
|
|
|
2015-02-12 06:59:27 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
gst_gl_color_convert_element_transform_caps (GstBaseTransform * bt,
|
|
|
|
GstPadDirection direction, GstCaps * caps, GstCaps * filter)
|
|
|
|
{
|
2018-05-03 03:59:07 +00:00
|
|
|
GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
|
|
|
|
GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (bt);
|
|
|
|
GstGLContext *context;
|
|
|
|
|
|
|
|
if (base_filter->display && !gst_gl_base_filter_find_gl_context (base_filter))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
context = GST_GL_BASE_FILTER (bt)->context;
|
|
|
|
|
|
|
|
if (!convert->convert && context)
|
|
|
|
convert->convert = gst_gl_color_convert_new (context);
|
2015-02-12 06:59:27 +00:00
|
|
|
|
2015-05-06 13:46:49 +00:00
|
|
|
return gst_gl_color_convert_transform_caps (context, direction, caps, filter);
|
2015-02-12 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gl_color_convert_element_get_unit_size (GstBaseTransform * trans,
|
|
|
|
GstCaps * caps, gsize * size)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
GstVideoInfo info;
|
|
|
|
|
|
|
|
ret = gst_video_info_from_caps (&info, caps);
|
|
|
|
if (ret)
|
|
|
|
*size = GST_VIDEO_INFO_SIZE (&info);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-08-21 00:27:34 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans,
|
|
|
|
GstQuery * query, GType api, const GstStructure * params)
|
|
|
|
{
|
|
|
|
/* propose all metadata upstream */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-02-12 06:59:27 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_color_convert_element_decide_allocation (GstBaseTransform * trans,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (trans);
|
|
|
|
|
|
|
|
/* get gl context */
|
2019-03-06 12:29:56 +00:00
|
|
|
if (!GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
|
2015-02-12 06:59:27 +00:00
|
|
|
query))
|
|
|
|
return FALSE;
|
|
|
|
|
2016-01-06 05:25:38 +00:00
|
|
|
if (!gst_gl_color_convert_decide_allocation (convert->convert, query))
|
|
|
|
return FALSE;
|
|
|
|
|
2015-02-12 06:59:27 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt,
|
|
|
|
GstBuffer * inbuf, GstBuffer ** outbuf)
|
|
|
|
{
|
|
|
|
GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
|
2015-08-24 23:28:10 +00:00
|
|
|
GstBaseTransformClass *bclass;
|
|
|
|
|
|
|
|
bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
|
2015-02-12 06:59:27 +00:00
|
|
|
|
|
|
|
if (gst_base_transform_is_passthrough (bt)) {
|
|
|
|
*outbuf = inbuf;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-06 13:46:49 +00:00
|
|
|
if (!convert->convert)
|
2015-02-12 06:59:27 +00:00
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
|
2015-05-06 13:46:49 +00:00
|
|
|
*outbuf = gst_gl_color_convert_perform (convert->convert, inbuf);
|
2015-07-17 07:47:37 +00:00
|
|
|
if (!*outbuf) {
|
|
|
|
GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
|
|
|
|
("%s", "Failed to convert video buffer"), (NULL));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2015-02-12 06:59:27 +00:00
|
|
|
|
2015-03-14 10:39:06 +00:00
|
|
|
/* basetransform doesn't unref if they're the same */
|
|
|
|
if (inbuf == *outbuf)
|
|
|
|
gst_buffer_unref (*outbuf);
|
2015-08-24 23:28:10 +00:00
|
|
|
else
|
|
|
|
bclass->copy_metadata (bt, inbuf, *outbuf);
|
2015-02-12 06:59:27 +00:00
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_gl_color_convert_element_transform (GstBaseTransform * bt,
|
|
|
|
GstBuffer * inbuf, GstBuffer * outbuf)
|
|
|
|
{
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2015-04-19 17:16:55 +00:00
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
gst_gl_color_convert_element_fixate_caps (GstBaseTransform *
|
|
|
|
bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
|
|
|
|
{
|
2015-10-29 02:04:31 +00:00
|
|
|
GstGLContext *context = GST_GL_BASE_FILTER (bt)->context;
|
2015-04-19 17:16:55 +00:00
|
|
|
|
2015-10-29 02:04:31 +00:00
|
|
|
return gst_gl_color_convert_fixate_caps (context, direction, caps, othercaps);
|
2015-04-19 17:16:55 +00:00
|
|
|
}
|
2019-03-06 12:29:56 +00:00
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_gl_color_convert_element_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (element);
|
|
|
|
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (convert, "changing state: %s => %s",
|
|
|
|
gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
|
|
|
|
gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
if (convert->convert) {
|
|
|
|
gst_object_unref (convert->convert);
|
|
|
|
convert->convert = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|