3 * Copyright (C) <2010-2012> Luis de Bethencourt <luis@debethencourt.com>
5 * Solarize - curve adjustment video effect.
6 * Based on Pete Warden's FreeFrame plugin with the same name.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * Alternatively, the contents of this file may be used under the
27 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28 * which case the following provisions apply instead of the ones
31 * This library is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU Library General Public
33 * License as published by the Free Software Foundation; either
34 * version 2 of the License, or (at your option) any later version.
36 * This library is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * Library General Public License for more details.
41 * You should have received a copy of the GNU Library General Public
42 * License along with this library; if not, write to the
43 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
44 * Boston, MA 02111-1307, USA.
48 * SECTION:element-solarize
50 * Solarize does a smart inverse in a video stream in realtime.
53 * <title>Example launch line</title>
55 * gst-launch -v videotestsrc ! solarize ! videoconvert ! autovideosink
56 * ]| This pipeline shows the effect of solarize on a test stream
67 #include "gstplugin.h"
68 #include "gstsolarize.h"
70 GST_DEBUG_CATEGORY_STATIC (gst_solarize_debug);
71 #define GST_CAT_DEFAULT gst_solarize_debug
73 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
74 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
76 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
79 /* Filter signals and args. */
96 #define DEFAULT_THRESHOLD 127
97 #define DEFAULT_START 50
98 #define DEFAULT_END 185
100 static void transform (guint32 * src, guint32 * dest, gint video_area,
101 gint threshold, gint start, gint end);
103 /* The capabilities of the inputs and outputs. */
105 static GstStaticPadTemplate gst_solarize_sink_template =
106 GST_STATIC_PAD_TEMPLATE ("sink",
109 GST_STATIC_CAPS (CAPS_STR)
112 static GstStaticPadTemplate gst_solarize_src_template =
113 GST_STATIC_PAD_TEMPLATE ("src",
116 GST_STATIC_CAPS (CAPS_STR)
119 #define gst_solarize_parent_class parent_class
120 G_DEFINE_TYPE (GstSolarize, gst_solarize, GST_TYPE_VIDEO_FILTER);
122 static void gst_solarize_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_solarize_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
126 static void gst_solarize_finalize (GObject * object);
128 static GstFlowReturn gst_solarize_transform_frame (GstVideoFilter * vfilter,
129 GstVideoFrame * in_frame, GstVideoFrame * out_frame);
131 /* GObject vmethod implementations */
133 /* Initialize the solarize's class. */
135 gst_solarize_class_init (GstSolarizeClass * klass)
137 GObjectClass *gobject_class = (GObjectClass *) klass;
138 GstElementClass *gstelement_class = (GstElementClass *) klass;
139 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
141 gst_element_class_set_details_simple (gstelement_class,
143 "Filter/Effect/Video",
144 "Solarize tunable inverse in the video signal.",
145 "Luis de Bethencourt <luis@debethencourt.com>");
147 gst_element_class_add_pad_template (gstelement_class,
148 gst_static_pad_template_get (&gst_solarize_sink_template));
149 gst_element_class_add_pad_template (gstelement_class,
150 gst_static_pad_template_get (&gst_solarize_src_template));
152 gobject_class->set_property = gst_solarize_set_property;
153 gobject_class->get_property = gst_solarize_get_property;
154 gobject_class->finalize = gst_solarize_finalize;
156 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
157 g_param_spec_uint ("threshold", "Threshold",
158 "Threshold parameter", 0, 256, DEFAULT_THRESHOLD,
159 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
161 g_object_class_install_property (gobject_class, PROP_START,
162 g_param_spec_uint ("start", "Start",
163 "Start parameter", 0, 256, DEFAULT_START,
164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
166 g_object_class_install_property (gobject_class, PROP_END,
167 g_param_spec_uint ("end", "End",
168 "End parameter", 0, 256, DEFAULT_END,
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
171 g_object_class_install_property (gobject_class, PROP_SILENT,
172 g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
173 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 vfilter_class->transform_frame =
176 GST_DEBUG_FUNCPTR (gst_solarize_transform_frame);
179 /* Initialize the element,
180 * instantiate pads and add them to element,
181 * set pad calback functions, and
182 * initialize instance structure.
185 gst_solarize_init (GstSolarize * filter)
187 filter->threshold = DEFAULT_THRESHOLD;
188 filter->start = DEFAULT_START;
189 filter->end = DEFAULT_END;
190 filter->silent = FALSE;
194 gst_solarize_set_property (GObject * object, guint prop_id,
195 const GValue * value, GParamSpec * pspec)
197 GstSolarize *filter = GST_SOLARIZE (object);
201 filter->silent = g_value_get_boolean (value);
204 filter->threshold = g_value_get_uint (value);
207 filter->start = g_value_get_uint (value);
210 filter->end = g_value_get_uint (value);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 gst_solarize_get_property (GObject * object, guint prop_id,
220 GValue * value, GParamSpec * pspec)
222 GstSolarize *filter = GST_SOLARIZE (object);
224 GST_OBJECT_LOCK (filter);
227 g_value_set_boolean (value, filter->silent);
230 g_value_set_uint (value, filter->threshold);
233 g_value_set_uint (value, filter->start);
236 g_value_set_uint (value, filter->end);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 GST_OBJECT_UNLOCK (filter);
246 gst_solarize_finalize (GObject * object)
248 G_OBJECT_CLASS (parent_class)->finalize (object);
251 /* GstElement vmethod implementations */
253 /* Actual processing. */
255 gst_solarize_transform_frame (GstVideoFilter * vfilter,
256 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
258 GstSolarize *filter = GST_SOLARIZE (vfilter);
259 gint video_size, threshold, start, end, width, height;
261 GstClockTime timestamp;
264 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
265 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
267 width = GST_VIDEO_FRAME_WIDTH (in_frame);
268 height = GST_VIDEO_FRAME_HEIGHT (in_frame);
270 /* GstController: update the properties */
271 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
273 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
274 GST_FORMAT_TIME, timestamp);
276 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
277 GST_TIME_ARGS (timestamp));
279 if (GST_CLOCK_TIME_IS_VALID (stream_time))
280 gst_object_sync_values (GST_OBJECT (filter), stream_time);
282 GST_OBJECT_LOCK (filter);
283 threshold = filter->threshold;
284 start = filter->start;
286 GST_OBJECT_UNLOCK (filter);
288 video_size = width * height;
289 transform (src, dest, video_size, threshold, start, end);
294 /* Entry point to initialize the plug-in.
295 * Register the element factories and other features. */
297 gst_solarize_plugin_init (GstPlugin * solarize)
299 /* debug category for fltering log messages */
300 GST_DEBUG_CATEGORY_INIT (gst_solarize_debug, "solarize",
301 0, "Template solarize");
303 return gst_element_register (solarize, "solarize", GST_RANK_NONE,
307 /*** Now the image processing work.... ***/
309 /* Transform processes each frame. */
311 transform (guint32 * src, guint32 * dest, gint video_area,
312 gint threshold, gint start, gint end)
320 gint period, up_length, down_length, height_scale, param;
322 period = end - start;
327 up_length = threshold - start;
328 if (up_length == 0) {
332 down_length = end - threshold;
333 if (down_length == 0) {
337 height_scale = ceiling - floor;
339 /* Loop through pixels. */
340 for (x = 0; x < video_area; x++) {
343 color[0] = (in >> 16) & 0xff;
344 color[1] = (in >> 8) & 0xff;
345 color[2] = (in) & 0xff;
348 /* Loop through colors. */
349 for (c = 0; c < 3; c++) {
355 if (param < up_length) {
356 color[c] = param * height_scale;
357 color[c] /= up_length;
360 color[c] = down_length - (param - up_length);
361 color[c] *= height_scale;
362 color[c] /= down_length;
367 color[0] = CLAMP (color[0], 0, 255);
368 color[1] = CLAMP (color[1], 0, 255);
369 color[2] = CLAMP (color[2], 0, 255);
371 *dest++ = (color[0] << 16) | (color[1] << 8) | color[2];