Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 1 | /* |
| 2 | * GStreamer |
| 3 | * Copyright (C) 2012-2014 Matthew Waters <ystree00@gmail.com> |
| 4 | * Copyright (C) 2015 Matthew Waters <matthew@centricular.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Library General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Library General Public |
| 17 | * License along with this library; if not, write to the |
| 18 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include "config.h" |
| 24 | #endif |
| 25 | |
| 26 | #include <gst/gl/gl.h> |
| 27 | #include "gstglcolorconvertelement.h" |
| 28 | |
| 29 | GST_DEBUG_CATEGORY_STATIC (gst_gl_color_convert_element_debug); |
| 30 | #define GST_CAT_DEFAULT gst_gl_color_convert_element_debug |
| 31 | |
| 32 | G_DEFINE_TYPE_WITH_CODE (GstGLColorConvertElement, gst_gl_color_convert_element, |
| 33 | GST_TYPE_GL_BASE_FILTER, |
| 34 | GST_DEBUG_CATEGORY_INIT (gst_gl_color_convert_element_debug, |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 35 | "glconvertelement", 0, "convert"); |
| 36 | ); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 37 | |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 38 | static gboolean gst_gl_color_convert_element_set_caps (GstBaseTransform * bt, |
| 39 | GstCaps * in_caps, GstCaps * out_caps); |
| 40 | static GstCaps *gst_gl_color_convert_element_transform_caps (GstBaseTransform * |
| 41 | bt, GstPadDirection direction, GstCaps * caps, GstCaps * filter); |
| 42 | static gboolean gst_gl_color_convert_element_get_unit_size (GstBaseTransform * |
| 43 | trans, GstCaps * caps, gsize * size); |
Nicolas Dufresne | 206638c | 2015-08-20 17:27:34 -0700 | [diff] [blame] | 44 | static gboolean |
| 45 | gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans, |
| 46 | GstQuery * query, GType api, const GstStructure * params); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 47 | static gboolean gst_gl_color_convert_element_decide_allocation (GstBaseTransform |
| 48 | * trans, GstQuery * query); |
| 49 | static GstFlowReturn |
| 50 | gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt, |
| 51 | GstBuffer * inbuf, GstBuffer ** outbuf); |
| 52 | static GstFlowReturn gst_gl_color_convert_element_transform (GstBaseTransform * |
| 53 | bt, GstBuffer * inbuf, GstBuffer * outbuf); |
Matthieu Bouron | 76b2cef | 2015-04-19 19:16:55 +0200 | [diff] [blame] | 54 | static GstCaps *gst_gl_color_convert_element_fixate_caps (GstBaseTransform * |
| 55 | bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 56 | |
| 57 | static GstStaticPadTemplate gst_gl_color_convert_element_src_pad_template = |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 58 | GST_STATIC_PAD_TEMPLATE ("src", |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 59 | GST_PAD_SRC, |
| 60 | GST_PAD_ALWAYS, |
Matthew Waters | e61d504 | 2015-10-29 00:44:26 +1100 | [diff] [blame] | 61 | GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS)); |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 62 | |
| 63 | static GstStaticPadTemplate gst_gl_color_convert_element_sink_pad_template = |
| 64 | GST_STATIC_PAD_TEMPLATE ("sink", |
| 65 | GST_PAD_SINK, |
| 66 | GST_PAD_ALWAYS, |
Matthew Waters | e61d504 | 2015-10-29 00:44:26 +1100 | [diff] [blame] | 67 | GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS)); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 68 | |
Guillaume Desmottes | 17446a4 | 2015-04-15 14:49:02 +0200 | [diff] [blame] | 69 | static gboolean |
| 70 | gst_gl_color_convert_element_stop (GstBaseTransform * bt) |
| 71 | { |
| 72 | GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt); |
| 73 | |
| 74 | if (convert->convert) { |
| 75 | gst_object_unref (convert->convert); |
| 76 | convert->convert = NULL; |
| 77 | } |
| 78 | |
Sebastian Dröge | 53ed701 | 2015-04-17 14:03:21 +0200 | [diff] [blame] | 79 | gst_caps_replace (&convert->in_caps, NULL); |
| 80 | gst_caps_replace (&convert->out_caps, NULL); |
| 81 | |
Guillaume Desmottes | 17446a4 | 2015-04-15 14:49:02 +0200 | [diff] [blame] | 82 | return |
| 83 | GST_BASE_TRANSFORM_CLASS (gst_gl_color_convert_element_parent_class)->stop |
| 84 | (bt); |
| 85 | } |
| 86 | |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 87 | static void |
| 88 | gst_gl_color_convert_element_class_init (GstGLColorConvertElementClass * klass) |
| 89 | { |
| 90 | GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass); |
| 91 | GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
| 92 | |
| 93 | bt_class->transform_caps = gst_gl_color_convert_element_transform_caps; |
| 94 | bt_class->set_caps = gst_gl_color_convert_element_set_caps; |
| 95 | bt_class->get_unit_size = gst_gl_color_convert_element_get_unit_size; |
Nicolas Dufresne | 206638c | 2015-08-20 17:27:34 -0700 | [diff] [blame] | 96 | bt_class->filter_meta = gst_gl_color_convert_element_filter_meta; |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 97 | bt_class->decide_allocation = gst_gl_color_convert_element_decide_allocation; |
| 98 | bt_class->prepare_output_buffer = |
| 99 | gst_gl_color_convert_element_prepare_output_buffer; |
| 100 | bt_class->transform = gst_gl_color_convert_element_transform; |
Guillaume Desmottes | 17446a4 | 2015-04-15 14:49:02 +0200 | [diff] [blame] | 101 | bt_class->stop = gst_gl_color_convert_element_stop; |
Matthieu Bouron | 76b2cef | 2015-04-19 19:16:55 +0200 | [diff] [blame] | 102 | bt_class->fixate_caps = gst_gl_color_convert_element_fixate_caps; |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 103 | |
| 104 | bt_class->passthrough_on_same_caps = TRUE; |
| 105 | |
| 106 | gst_element_class_add_pad_template (element_class, |
| 107 | gst_static_pad_template_get |
| 108 | (&gst_gl_color_convert_element_src_pad_template)); |
| 109 | gst_element_class_add_pad_template (element_class, |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 110 | gst_static_pad_template_get |
| 111 | (&gst_gl_color_convert_element_sink_pad_template)); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 112 | |
| 113 | gst_element_class_set_metadata (element_class, |
| 114 | "OpenGL color converter", "Filter/Converter/Video", |
| 115 | "Converts between color spaces using OpenGL shaders", |
| 116 | "Matthew Waters <matthew@centricular.com>"); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void |
| 120 | gst_gl_color_convert_element_init (GstGLColorConvertElement * convert) |
| 121 | { |
| 122 | gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (convert), |
| 123 | TRUE); |
| 124 | } |
| 125 | |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 126 | static gboolean |
| 127 | gst_gl_color_convert_element_set_caps (GstBaseTransform * bt, |
| 128 | GstCaps * in_caps, GstCaps * out_caps) |
| 129 | { |
| 130 | GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt); |
| 131 | |
| 132 | gst_caps_replace (&convert->in_caps, in_caps); |
| 133 | gst_caps_replace (&convert->out_caps, out_caps); |
| 134 | |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 135 | if (convert->convert) |
| 136 | gst_gl_color_convert_set_caps (convert->convert, in_caps, out_caps); |
| 137 | |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 138 | return TRUE; |
| 139 | } |
| 140 | |
| 141 | static GstCaps * |
| 142 | gst_gl_color_convert_element_transform_caps (GstBaseTransform * bt, |
| 143 | GstPadDirection direction, GstCaps * caps, GstCaps * filter) |
| 144 | { |
| 145 | GstGLContext *context = GST_GL_BASE_FILTER (bt)->context; |
| 146 | |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 147 | return gst_gl_color_convert_transform_caps (context, direction, caps, filter); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static gboolean |
| 151 | gst_gl_color_convert_element_get_unit_size (GstBaseTransform * trans, |
| 152 | GstCaps * caps, gsize * size) |
| 153 | { |
| 154 | gboolean ret = FALSE; |
| 155 | GstVideoInfo info; |
| 156 | |
| 157 | ret = gst_video_info_from_caps (&info, caps); |
| 158 | if (ret) |
| 159 | *size = GST_VIDEO_INFO_SIZE (&info); |
| 160 | |
| 161 | return TRUE; |
| 162 | } |
| 163 | |
| 164 | static gboolean |
Nicolas Dufresne | 206638c | 2015-08-20 17:27:34 -0700 | [diff] [blame] | 165 | gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans, |
| 166 | GstQuery * query, GType api, const GstStructure * params) |
| 167 | { |
| 168 | /* propose all metadata upstream */ |
| 169 | return TRUE; |
| 170 | } |
| 171 | |
| 172 | static gboolean |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 173 | gst_gl_color_convert_element_decide_allocation (GstBaseTransform * trans, |
| 174 | GstQuery * query) |
| 175 | { |
| 176 | GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (trans); |
| 177 | GstGLContext *context; |
| 178 | |
| 179 | /* get gl context */ |
| 180 | if (!GST_BASE_TRANSFORM_CLASS |
| 181 | (gst_gl_color_convert_element_parent_class)->decide_allocation (trans, |
| 182 | query)) |
| 183 | return FALSE; |
| 184 | |
| 185 | context = GST_GL_BASE_FILTER (trans)->context; |
| 186 | |
| 187 | if (!convert->convert) |
| 188 | convert->convert = gst_gl_color_convert_new (context); |
| 189 | |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 190 | if (!gst_gl_color_convert_set_caps (convert->convert, convert->in_caps, |
| 191 | convert->out_caps)) |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 192 | return FALSE; |
| 193 | |
Matthew Waters | 5192802 | 2016-01-06 16:25:38 +1100 | [diff] [blame] | 194 | if (!gst_gl_color_convert_decide_allocation (convert->convert, query)) |
| 195 | return FALSE; |
| 196 | |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 197 | return TRUE; |
| 198 | } |
| 199 | |
| 200 | static GstFlowReturn |
| 201 | gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt, |
| 202 | GstBuffer * inbuf, GstBuffer ** outbuf) |
| 203 | { |
| 204 | GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt); |
Nicolas Dufresne | b06fc67 | 2015-08-24 19:28:10 -0400 | [diff] [blame] | 205 | GstBaseTransformClass *bclass; |
| 206 | |
| 207 | bclass = GST_BASE_TRANSFORM_GET_CLASS (bt); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 208 | |
| 209 | if (gst_base_transform_is_passthrough (bt)) { |
| 210 | *outbuf = inbuf; |
| 211 | return GST_FLOW_OK; |
| 212 | } |
| 213 | |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 214 | if (!convert->convert) |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 215 | return GST_FLOW_NOT_NEGOTIATED; |
| 216 | |
Sebastian Dröge | 53c79cb | 2015-05-06 15:46:49 +0200 | [diff] [blame] | 217 | *outbuf = gst_gl_color_convert_perform (convert->convert, inbuf); |
Matthew Waters | 0aaf9db | 2015-07-17 17:47:37 +1000 | [diff] [blame] | 218 | if (!*outbuf) { |
| 219 | GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND, |
| 220 | ("%s", "Failed to convert video buffer"), (NULL)); |
| 221 | return GST_FLOW_ERROR; |
| 222 | } |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 223 | |
Matthew Waters | 0c8ede8 | 2015-03-14 10:39:06 +0000 | [diff] [blame] | 224 | /* basetransform doesn't unref if they're the same */ |
| 225 | if (inbuf == *outbuf) |
| 226 | gst_buffer_unref (*outbuf); |
Nicolas Dufresne | b06fc67 | 2015-08-24 19:28:10 -0400 | [diff] [blame] | 227 | else |
| 228 | bclass->copy_metadata (bt, inbuf, *outbuf); |
Matthew Waters | 3514600 | 2015-02-12 17:59:27 +1100 | [diff] [blame] | 229 | |
| 230 | return GST_FLOW_OK; |
| 231 | } |
| 232 | |
| 233 | static GstFlowReturn |
| 234 | gst_gl_color_convert_element_transform (GstBaseTransform * bt, |
| 235 | GstBuffer * inbuf, GstBuffer * outbuf) |
| 236 | { |
| 237 | return GST_FLOW_OK; |
| 238 | } |
Matthieu Bouron | 76b2cef | 2015-04-19 19:16:55 +0200 | [diff] [blame] | 239 | |
| 240 | static GstCaps * |
| 241 | gst_gl_color_convert_element_fixate_caps (GstBaseTransform * |
| 242 | bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps) |
| 243 | { |
Matthew Waters | ccce217 | 2015-10-29 13:04:31 +1100 | [diff] [blame] | 244 | GstGLContext *context = GST_GL_BASE_FILTER (bt)->context; |
Matthieu Bouron | 76b2cef | 2015-04-19 19:16:55 +0200 | [diff] [blame] | 245 | |
Matthew Waters | ccce217 | 2015-10-29 13:04:31 +1100 | [diff] [blame] | 246 | return gst_gl_color_convert_fixate_caps (context, direction, caps, othercaps); |
Matthieu Bouron | 76b2cef | 2015-04-19 19:16:55 +0200 | [diff] [blame] | 247 | } |