• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) <2010-2015> Luis de Bethencourt <luis@debethencourt.com>
4  *
5  * Dodge - saturation video effect.
6  * Based on Pete Warden's FreeFrame plugin with the same name.
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
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.
25  *
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
29  * mentioned above:
30  *
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.
35  *
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.
40  *
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., 51 Franklin St, Fifth Floor,
44  * Boston, MA 02110-1301, USA.
45  */
46 
47 /**
48  * SECTION:element-dodge
49  * @title: dodge
50  *
51  * Dodge saturates the colors of a video stream in realtime.
52  *
53  * ## Example launch line
54  * |[
55  * gst-launch-1.0 -v videotestsrc ! dodge ! videoconvert ! autovideosink
56  * ]| This pipeline shows the effect of dodge on a test stream
57  *
58  */
59 
60 #ifdef HAVE_CONFIG_H
61 #  include <config.h>
62 #endif
63 
64 #include <gst/gst.h>
65 #include <math.h>
66 
67 #include "gstdodge.h"
68 
69 GST_DEBUG_CATEGORY_STATIC (gst_dodge_debug);
70 #define GST_CAT_DEFAULT gst_dodge_debug
71 
72 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
73 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  BGRx, RGBx }")
74 #else
75 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  xBGR, xRGB }")
76 #endif
77 
78 #define gst_dodge_parent_class parent_class
79 G_DEFINE_TYPE (GstDodge, gst_dodge, GST_TYPE_VIDEO_FILTER);
80 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (dodge, "dodge", GST_RANK_NONE,
81     GST_TYPE_DODGE, GST_DEBUG_CATEGORY_INIT (gst_dodge_debug, "dodge", 0,
82         "Template dodge"));
83 
84 /* Filter signals and args. */
85 enum
86 {
87   LAST_SIGNAL
88 };
89 
90 enum
91 {
92   PROP_0,
93 };
94 
95 /* Initializations */
96 
97 static void transform (guint32 * src, guint32 * dest, gint video_area);
98 
99 /* The capabilities of the inputs and outputs. */
100 
101 static GstStaticPadTemplate gst_dodge_sink_template =
102 GST_STATIC_PAD_TEMPLATE ("sink",
103     GST_PAD_SINK,
104     GST_PAD_ALWAYS,
105     GST_STATIC_CAPS (CAPS_STR)
106     );
107 
108 static GstStaticPadTemplate gst_dodge_src_template =
109 GST_STATIC_PAD_TEMPLATE ("src",
110     GST_PAD_SRC,
111     GST_PAD_ALWAYS,
112     GST_STATIC_CAPS (CAPS_STR)
113     );
114 
115 static void gst_dodge_set_property (GObject * object, guint prop_id,
116     const GValue * value, GParamSpec * pspec);
117 static void gst_dodge_get_property (GObject * object, guint prop_id,
118     GValue * value, GParamSpec * pspec);
119 static void gst_dodge_finalize (GObject * object);
120 
121 static GstFlowReturn gst_dodge_transform_frame (GstVideoFilter * vfilter,
122     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
123 
124 /* GObject vmethod implementations */
125 
126 /* Initialize the dodge's class. */
127 static void
gst_dodge_class_init(GstDodgeClass * klass)128 gst_dodge_class_init (GstDodgeClass * klass)
129 {
130   GObjectClass *gobject_class = (GObjectClass *) klass;
131   GstElementClass *gstelement_class = (GstElementClass *) klass;
132   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
133 
134   gst_element_class_set_static_metadata (gstelement_class,
135       "Dodge",
136       "Filter/Effect/Video",
137       "Dodge saturates the colors in the video signal.",
138       "Luis de Bethencourt <luis@debethencourt.com>");
139 
140   gst_element_class_add_static_pad_template (gstelement_class,
141       &gst_dodge_sink_template);
142   gst_element_class_add_static_pad_template (gstelement_class,
143       &gst_dodge_src_template);
144 
145   gobject_class->set_property = gst_dodge_set_property;
146   gobject_class->get_property = gst_dodge_get_property;
147   gobject_class->finalize = gst_dodge_finalize;
148 
149   vfilter_class->transform_frame =
150       GST_DEBUG_FUNCPTR (gst_dodge_transform_frame);
151 }
152 
153 /* Initialize the element,
154  * instantiate pads and add them to element,
155  * set pad callback functions, and
156  * initialize instance structure.
157  */
158 static void
gst_dodge_init(GstDodge * filter)159 gst_dodge_init (GstDodge * filter)
160 {
161 }
162 
163 static void
gst_dodge_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)164 gst_dodge_set_property (GObject * object, guint prop_id,
165     const GValue * value, GParamSpec * pspec)
166 {
167   switch (prop_id) {
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170       break;
171   }
172 }
173 
174 static void
gst_dodge_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)175 gst_dodge_get_property (GObject * object, guint prop_id,
176     GValue * value, GParamSpec * pspec)
177 {
178   switch (prop_id) {
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182   }
183 }
184 
185 static void
gst_dodge_finalize(GObject * object)186 gst_dodge_finalize (GObject * object)
187 {
188   G_OBJECT_CLASS (parent_class)->finalize (object);
189 }
190 
191 /* GstElement vmethod implementations */
192 
193 /* Actual processing. */
194 static GstFlowReturn
gst_dodge_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)195 gst_dodge_transform_frame (GstVideoFilter * vfilter,
196     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
197 {
198   GstDodge *filter = GST_DODGE (vfilter);
199   guint32 *src, *dest;
200   gint video_size;
201 
202   GstClockTime timestamp;
203   gint64 stream_time;
204 
205   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
206   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
207 
208   /* GstController: update the properties */
209   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
210   stream_time =
211       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
212       GST_FORMAT_TIME, timestamp);
213 
214   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
215       GST_TIME_ARGS (timestamp));
216 
217   if (GST_CLOCK_TIME_IS_VALID (stream_time))
218     gst_object_sync_values (GST_OBJECT (filter), stream_time);
219 
220   video_size = GST_VIDEO_FRAME_WIDTH (in_frame) *
221       GST_VIDEO_FRAME_HEIGHT (in_frame);
222 
223   transform (src, dest, video_size);
224 
225   return GST_FLOW_OK;
226 }
227 
228 /*** Now the image processing work.... ***/
229 
230 /* Transform processes each frame. */
231 static void
transform(guint32 * src,guint32 * dest,gint video_area)232 transform (guint32 * src, guint32 * dest, gint video_area)
233 {
234   guint32 in;
235   gint x, red, green, blue;
236 
237   for (x = 0; x < video_area; x++) {
238     in = *src++;
239 
240     red = (in >> 16) & 0xff;
241     green = (in >> 8) & 0xff;
242     blue = (in) & 0xff;
243 
244     red = (256 * red) / (256 - red);
245     green = (256 * green) / (256 - green);
246     blue = (256 * blue) / (256 - blue);
247 
248     red = CLAMP (red, 0, 255);
249     green = CLAMP (green, 0, 255);
250     blue = CLAMP (blue, 0, 255);
251 
252     *dest++ = (red << 16) | (green << 8) | blue;
253   }
254 }
255