3 * Copyright (C) 2010 Luis de Bethencourt <luis@debethencourt.com>
5 * Burn - 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-burn
50 * Burn adjusts the colors of a video stream in realtime.
53 * <title>Example launch line</title>
55 * gst-launch -v videotestsrc ! burn ! ffmpegcolorspace ! autovideosink
56 * ]| This pipeline shows the effect of burn on a test stream
67 #include "gstplugin.h"
70 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
73 GST_DEBUG_CATEGORY_STATIC (gst_burn_debug);
74 #define GST_CAT_DEFAULT gst_burn_debug
76 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
77 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
79 #define CAPS_STR GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR
82 /* Filter signals and args. */
97 #define DEFAULT_ADJUSTMENT 175
99 static void transform (guint32 * src, guint32 * dest, gint video_area,
102 /* The capabilities of the inputs and outputs. */
104 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
107 GST_STATIC_CAPS (CAPS_STR)
110 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
113 GST_STATIC_CAPS (CAPS_STR)
116 GST_BOILERPLATE (GstBurn, gst_burn, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
118 static void gst_burn_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec);
120 static void gst_burn_get_property (GObject * object, guint prop_id,
121 GValue * value, GParamSpec * pspec);
123 static gboolean gst_burn_set_caps (GstBaseTransform * btrans,
124 GstCaps * incaps, GstCaps * outcaps);
125 static GstFlowReturn gst_burn_transform (GstBaseTransform * btrans,
126 GstBuffer * in_buf, GstBuffer * out_buf);
128 /* GObject vmethod implementations */
131 gst_burn_base_init (gpointer gclass)
133 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
135 gst_element_class_set_details_simple (element_class,
137 "Filter/Effect/Video",
138 "Burn adjusts the colors in the video signal.",
139 "Luis de Bethencourt <luis@debethencourt.com>");
141 gst_element_class_add_static_pad_template (element_class, &src_factory);
142 gst_element_class_add_static_pad_template (element_class, &sink_factory);
145 /* Initialize the burn's class. */
147 gst_burn_class_init (GstBurnClass * klass)
149 GObjectClass *gobject_class = (GObjectClass *) klass;
150 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
152 gobject_class->set_property = gst_burn_set_property;
153 gobject_class->get_property = gst_burn_get_property;
155 g_object_class_install_property (gobject_class, PROP_ADJUSTMENT,
156 g_param_spec_uint ("adjustment", "Adjustment",
157 "Adjustment parameter", 0, 256, DEFAULT_ADJUSTMENT,
158 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
160 g_object_class_install_property (gobject_class, PROP_SILENT,
161 g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
162 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_burn_set_caps);
165 trans_class->transform = GST_DEBUG_FUNCPTR (gst_burn_transform);
168 /* Initialize the element,
169 * instantiate pads and add them to element,
170 * set pad calback functions, and
171 * initialize instance structure.
174 gst_burn_init (GstBurn * filter, GstBurnClass * gclass)
176 filter->adjustment = DEFAULT_ADJUSTMENT;
177 filter->silent = FALSE;
181 gst_burn_set_property (GObject * object, guint prop_id,
182 const GValue * value, GParamSpec * pspec)
184 GstBurn *filter = GST_BURN (object);
188 filter->silent = g_value_get_boolean (value);
190 case PROP_ADJUSTMENT:
191 filter->adjustment = g_value_get_uint (value);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gst_burn_get_property (GObject * object, guint prop_id,
201 GValue * value, GParamSpec * pspec)
203 GstBurn *filter = GST_BURN (object);
205 GST_OBJECT_LOCK (filter);
208 g_value_set_boolean (value, filter->silent);
210 case PROP_ADJUSTMENT:
211 g_value_set_uint (value, filter->adjustment);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 GST_OBJECT_UNLOCK (filter);
220 /* GstElement vmethod implementations */
222 /* Handle the link with other elements. */
224 gst_burn_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
227 GstBurn *filter = GST_BURN (btrans);
228 GstStructure *structure;
229 gboolean ret = FALSE;
231 structure = gst_caps_get_structure (incaps, 0);
233 GST_OBJECT_LOCK (filter);
234 if (gst_structure_get_int (structure, "width", &filter->width) &&
235 gst_structure_get_int (structure, "height", &filter->height)) {
238 GST_OBJECT_UNLOCK (filter);
243 /* Actual processing. */
245 gst_burn_transform (GstBaseTransform * btrans,
246 GstBuffer * in_buf, GstBuffer * out_buf)
248 GstBurn *filter = GST_BURN (btrans);
249 gint video_size, adjustment;
250 guint32 *src = (guint32 *) GST_BUFFER_DATA (in_buf);
251 guint32 *dest = (guint32 *) GST_BUFFER_DATA (out_buf);
252 GstClockTime timestamp;
255 video_size = filter->width * filter->height;
257 /* GstController: update the properties */
258 timestamp = GST_BUFFER_TIMESTAMP (in_buf);
260 gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
262 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
263 GST_TIME_ARGS (timestamp));
265 if (GST_CLOCK_TIME_IS_VALID (stream_time))
266 gst_object_sync_values (G_OBJECT (filter), stream_time);
268 GST_OBJECT_LOCK (filter);
269 adjustment = filter->adjustment;
270 GST_OBJECT_UNLOCK (filter);
272 transform (src, dest, video_size, adjustment);
277 /* Entry point to initialize the plug-in.
278 * Register the element factories and other features. */
280 gst_burn_plugin_init (GstPlugin * burn)
282 /* debug category for fltering log messages */
283 GST_DEBUG_CATEGORY_INIT (gst_burn_debug, "burn", 0, "Template burn");
285 return gst_element_register (burn, "burn", GST_RANK_NONE, GST_TYPE_BURN);
288 /*** Now the image processing work.... ***/
290 /* Transform processes each frame. */
292 transform (guint32 * src, guint32 * dest, gint video_area, gint adjustment)
295 gint red, green, blue, c;
298 for (x = 0; x < video_area; x++) {
301 red = (in >> 16) & 0xff;
302 green = (in >> 8) & 0xff;
305 c = (red + adjustment);
306 red = c ? (256 - (256 * (255 - red) / c)) : 0;
307 c = (green + adjustment);
308 green = c ? (256 - (256 * (255 - green) / c)) : 0;
309 c = (blue + adjustment);
310 blue = c ? (256 - (256 * (255 - blue) / c)) : 0;
312 red = CLAMP (red, 0, 255);
313 green = CLAMP (green, 0, 255);
314 blue = CLAMP (blue, 0, 255);
316 *dest++ = (red << 16) | (green << 8) | blue;