From 782b343f7df4c83d73b6d0df5352eed0509cbcc0 Mon Sep 17 00:00:00 2001 From: Matthew Waters Date: Thu, 12 Feb 2015 17:59:27 +1100 Subject: [PATCH] gl: add a new glcolorconvert element based on the glcolorconvert library object --- ext/gl/Makefile.am | 2 + ext/gl/gstglcolorconvertelement.c | 205 ++++++++++++++++++++++++++++++++++++++ ext/gl/gstglcolorconvertelement.h | 59 +++++++++++ ext/gl/gstopengl.c | 6 ++ 4 files changed, 272 insertions(+) create mode 100644 ext/gl/gstglcolorconvertelement.c create mode 100644 ext/gl/gstglcolorconvertelement.h diff --git a/ext/gl/Makefile.am b/ext/gl/Makefile.am index 15103f4..0e0b98e 100644 --- a/ext/gl/Makefile.am +++ b/ext/gl/Makefile.am @@ -4,6 +4,7 @@ libgstopengl_la_SOURCES = \ gstopengl.c \ gstglbasemixer.c \ gstgluploadelement.c \ + gstglcolorconvertelement.c \ gstglimagesink.c \ gstglfiltercube.c \ gstgleffects.c \ @@ -20,6 +21,7 @@ libgstopengl_la_SOURCES = \ noinst_HEADERS = \ gstglbasemixer.h \ gstgluploadelement.h \ + gstglcolorconvertelement.h \ gstglimagesink.h \ gstglfiltercube.h \ gstgleffects.h \ diff --git a/ext/gl/gstglcolorconvertelement.c b/ext/gl/gstglcolorconvertelement.c new file mode 100644 index 0000000..ad4c765 --- /dev/null +++ b/ext/gl/gstglcolorconvertelement.c @@ -0,0 +1,205 @@ +/* + * GStreamer + * Copyright (C) 2012-2014 Matthew Waters + * Copyright (C) 2015 Matthew Waters + * + * 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 +#include "gstglcolorconvertelement.h" + +GST_DEBUG_CATEGORY_STATIC (gst_gl_color_convert_element_debug); +#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, + "glconvertelement", 0, "convert"); + ); + +static void gst_gl_color_convert_element_finalize (GObject * object); +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); +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); + +static GstStaticPadTemplate gst_gl_color_convert_element_src_pad_template = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS)); + +static GstStaticPadTemplate gst_gl_color_convert_element_sink_pad_template = +GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS)); + +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; + 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; + + bt_class->passthrough_on_same_caps = TRUE; + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get + (&gst_gl_color_convert_element_src_pad_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get + (&gst_gl_color_convert_element_sink_pad_template)); + + gst_element_class_set_metadata (element_class, + "OpenGL color converter", "Filter/Converter/Video", + "Converts between color spaces using OpenGL shaders", + "Matthew Waters "); + + G_OBJECT_CLASS (klass)->finalize = gst_gl_color_convert_element_finalize; +} + +static void +gst_gl_color_convert_element_init (GstGLColorConvertElement * convert) +{ + gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (convert), + TRUE); +} + +static void +gst_gl_color_convert_element_finalize (GObject * object) +{ + GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (object); + + gst_caps_replace (&convert->in_caps, NULL); + gst_caps_replace (&convert->out_caps, NULL); + + G_OBJECT_CLASS (gst_gl_color_convert_element_parent_class)->finalize (object); +} + +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); + + gst_caps_replace (&convert->in_caps, in_caps); + gst_caps_replace (&convert->out_caps, out_caps); + + if (convert->convert) + gst_gl_color_convert_set_caps (convert->convert, in_caps, out_caps); + + return TRUE; +} + +static GstCaps * +gst_gl_color_convert_element_transform_caps (GstBaseTransform * bt, + GstPadDirection direction, GstCaps * caps, GstCaps * filter) +{ + GstGLContext *context = GST_GL_BASE_FILTER (bt)->context; + + return gst_gl_color_convert_transform_caps (context, direction, caps, filter); +} + +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; +} + +static gboolean +gst_gl_color_convert_element_decide_allocation (GstBaseTransform * trans, + GstQuery * query) +{ + GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (trans); + GstGLContext *context; + + /* get gl context */ + if (!GST_BASE_TRANSFORM_CLASS + (gst_gl_color_convert_element_parent_class)->decide_allocation (trans, + query)) + return FALSE; + + context = GST_GL_BASE_FILTER (trans)->context; + + if (!convert->convert) + convert->convert = gst_gl_color_convert_new (context); + + if (!gst_gl_color_convert_set_caps (convert->convert, convert->in_caps, + convert->out_caps)) + return FALSE; + + 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); + + if (gst_base_transform_is_passthrough (bt)) { + *outbuf = inbuf; + return GST_FLOW_OK; + } + + if (!convert->convert) + return GST_FLOW_NOT_NEGOTIATED; + + *outbuf = gst_gl_color_convert_perform (convert->convert, inbuf); + + if (*outbuf) + gst_buffer_copy_into (*outbuf, inbuf, + GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); + + return GST_FLOW_OK; +} + +static GstFlowReturn +gst_gl_color_convert_element_transform (GstBaseTransform * bt, + GstBuffer * inbuf, GstBuffer * outbuf) +{ + return GST_FLOW_OK; +} diff --git a/ext/gl/gstglcolorconvertelement.h b/ext/gl/gstglcolorconvertelement.h new file mode 100644 index 0000000..2a0dd1d --- /dev/null +++ b/ext/gl/gstglcolorconvertelement.h @@ -0,0 +1,59 @@ +/* + * GStreamer + * Copyright (C) 2012 Matthew Waters + * + * 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. + */ + +#ifndef __GST_GL_COLOR_CONVERT_ELEMENT_H__ +#define __GST_GL_COLOR_CONVERT_ELEMENT_H__ + +#include +#include + +#include + +G_BEGIN_DECLS + +GType gst_gl_color_convert_element_get_type (void); +#define GST_TYPE_GL_COLOR_CONVERT_ELEMENT (gst_gl_color_convert_element_get_type()) +#define GST_GL_COLOR_CONVERT_ELEMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GL_COLOR_CONVERT_ELEMENT,GstGLColorConvertElement)) +#define GST_GL_COLOR_CONVERT_ELEMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_GL_DISPLAY,GstGLColorConvertElementClass)) +#define GST_IS_GL_COLOR_CONVERT_ELEMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GL_COLOR_CONVERT_ELEMENT)) +#define GST_IS_GL_COLOR_CONVERT_ELEMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_GL_COLOR_CONVERT_ELEMENT)) +#define GST_GL_COLOR_CONVERT_ELEMENT_CAST(obj) ((GstGLColorConvertElement*)(obj)) + +typedef struct _GstGLColorConvertElement GstGLColorConvertElement; +typedef struct _GstGLColorConvertElementClass GstGLColorConvertElementClass; +typedef struct _GstGLColorConvertElementPrivate GstGLColorConvertElementPrivate; + +struct _GstGLColorConvertElement +{ + GstGLBaseFilter parent; + + GstGLColorConvert *convert; + GstCaps *in_caps; + GstCaps *out_caps; +}; + +struct _GstGLColorConvertElementClass +{ + GstGLBaseFilterClass object_class; +}; + +G_END_DECLS + +#endif /* __GST_GL_COLOR_CONVERT_ELEMENT_H__ */ diff --git a/ext/gl/gstopengl.c b/ext/gl/gstopengl.c index 59fe7bd..db91369 100644 --- a/ext/gl/gstopengl.c +++ b/ext/gl/gstopengl.c @@ -45,6 +45,7 @@ #include "gstglimagesink.h" #include "gstgluploadelement.h" +#include "gstglcolorconvertelement.h" #include "gstglfiltercube.h" #include "gstgleffects.h" @@ -118,6 +119,11 @@ plugin_init (GstPlugin * plugin) return FALSE; } + if (!gst_element_register (plugin, "glcolorconvert", + GST_RANK_SECONDARY, GST_TYPE_GL_COLOR_CONVERT_ELEMENT)) { + return FALSE; + } + if (!gst_element_register (plugin, "glfiltercube", GST_RANK_NONE, GST_TYPE_GL_FILTER_CUBE)) { return FALSE; -- 2.7.4