1 /*
2  * GStreamer
3  * Copyright (C) <2010-2015> Luis de Bethencourt <luis@debethencourt.com>
4  *
5  * Burn - curve adjustment 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-burn
49  * @title: burn
50  *
51  * Burn adjusts the colors of a video stream in realtime.
52  *
53  * ## Example launch line
54  * |[
55  * gst-launch-1.0 -v videotestsrc ! burn ! videoconvert ! autovideosink
56  * ]| This pipeline shows the effect of burn 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 "gstplugin.h"
68 #include "gstburn.h"
69 
70 #include "gstgaudieffectsorc.h"
71 
72 #define gst_burn_parent_class parent_class
73 G_DEFINE_TYPE (GstBurn, gst_burn, GST_TYPE_VIDEO_FILTER);
74 
75 GST_DEBUG_CATEGORY_STATIC (gst_burn_debug);
76 #define GST_CAT_DEFAULT gst_burn_debug
77 
78 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
79 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  BGRx, RGBx }")
80 #else
81 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  xBGR, xRGB }")
82 #endif
83 
84 /* Filter signals and args. */
85 enum
86 {
87   LAST_SIGNAL
88 };
89 
90 enum
91 {
92   PROP_0 = 0,
93   PROP_ADJUSTMENT,
94 };
95 
96 /* Initializations */
97 
98 #define DEFAULT_ADJUSTMENT 175
99 
100 /* The capabilities of the inputs and outputs. */
101 
102 static GstStaticPadTemplate gst_burn_sink_template =
103 GST_STATIC_PAD_TEMPLATE ("sink",
104     GST_PAD_SINK,
105     GST_PAD_ALWAYS,
106     GST_STATIC_CAPS (CAPS_STR)
107     );
108 
109 static GstStaticPadTemplate gst_burn_src_template =
110 GST_STATIC_PAD_TEMPLATE ("src",
111     GST_PAD_SRC,
112     GST_PAD_ALWAYS,
113     GST_STATIC_CAPS (CAPS_STR)
114     );
115 
116 static void gst_burn_set_property (GObject * object, guint prop_id,
117     const GValue * value, GParamSpec * pspec);
118 static void gst_burn_get_property (GObject * object, guint prop_id,
119     GValue * value, GParamSpec * pspec);
120 
121 static void gst_burn_finalize (GObject * object);
122 
123 static GstFlowReturn gst_burn_transform_frame (GstVideoFilter * vfilter,
124     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
125 
126 /* GObject vmethod implementations */
127 
128 /* Initialize the burn's class. */
129 static void
gst_burn_class_init(GstBurnClass * klass)130 gst_burn_class_init (GstBurnClass * klass)
131 {
132   GObjectClass *gobject_class = (GObjectClass *) klass;
133   GstElementClass *gstelement_class = (GstElementClass *) klass;
134   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
135 
136   gst_element_class_set_static_metadata (gstelement_class, "Burn",
137       "Filter/Effect/Video",
138       "Burn adjusts the colors in the video signal.",
139       "Luis de Bethencourt <luis@debethencourt.com>");
140 
141   gst_element_class_add_static_pad_template (gstelement_class,
142       &gst_burn_sink_template);
143   gst_element_class_add_static_pad_template (gstelement_class,
144       &gst_burn_src_template);
145 
146   gobject_class->set_property = gst_burn_set_property;
147   gobject_class->get_property = gst_burn_get_property;
148   gobject_class->finalize = gst_burn_finalize;
149 
150   g_object_class_install_property (gobject_class, PROP_ADJUSTMENT,
151       g_param_spec_uint ("adjustment", "Adjustment",
152           "Adjustment parameter", 0, 256, DEFAULT_ADJUSTMENT,
153           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
154 
155   vfilter_class->transform_frame = GST_DEBUG_FUNCPTR (gst_burn_transform_frame);
156 }
157 
158 /* Initialize the element,
159  * instantiate pads and add them to element,
160  * set pad calback functions, and
161  * initialize instance structure.
162  */
163 static void
gst_burn_init(GstBurn * filter)164 gst_burn_init (GstBurn * filter)
165 {
166   filter->adjustment = DEFAULT_ADJUSTMENT;
167 }
168 
169 static void
gst_burn_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)170 gst_burn_set_property (GObject * object, guint prop_id,
171     const GValue * value, GParamSpec * pspec)
172 {
173   GstBurn *filter = GST_BURN (object);
174 
175   switch (prop_id) {
176     case PROP_ADJUSTMENT:
177       filter->adjustment = g_value_get_uint (value);
178       break;
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182   }
183 }
184 
185 static void
gst_burn_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)186 gst_burn_get_property (GObject * object, guint prop_id,
187     GValue * value, GParamSpec * pspec)
188 {
189   GstBurn *filter = GST_BURN (object);
190 
191   GST_OBJECT_LOCK (filter);
192   switch (prop_id) {
193     case PROP_ADJUSTMENT:
194       g_value_set_uint (value, filter->adjustment);
195       break;
196     default:
197       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198       break;
199   }
200   GST_OBJECT_UNLOCK (filter);
201 }
202 
203 static void
gst_burn_finalize(GObject * object)204 gst_burn_finalize (GObject * object)
205 {
206   G_OBJECT_CLASS (parent_class)->finalize (object);
207 }
208 
209 /* GstElement vmethod implementations */
210 
211 /* Actual processing. */
212 static GstFlowReturn
gst_burn_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)213 gst_burn_transform_frame (GstVideoFilter * vfilter,
214     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
215 {
216   GstBurn *filter = GST_BURN (vfilter);
217   gint video_size, adjustment;
218   guint32 *src, *dest;
219   GstClockTime timestamp;
220   gint64 stream_time;
221 
222   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
223   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
224 
225   video_size = GST_VIDEO_FRAME_WIDTH (in_frame) *
226       GST_VIDEO_FRAME_HEIGHT (in_frame);
227 
228   /* GstController: update the properties */
229   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
230   stream_time =
231       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
232       GST_FORMAT_TIME, timestamp);
233 
234   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
235       GST_TIME_ARGS (timestamp));
236 
237   if (GST_CLOCK_TIME_IS_VALID (stream_time))
238     gst_object_sync_values (GST_OBJECT (filter), stream_time);
239 
240   GST_OBJECT_LOCK (filter);
241   adjustment = filter->adjustment;
242   GST_OBJECT_UNLOCK (filter);
243 
244   /*** Now the image processing work.... ***/
245   gaudi_orc_burn (dest, src, adjustment, video_size);
246 
247   return GST_FLOW_OK;
248 }
249 
250 /* Entry point to initialize the plug-in.
251  * Register the element factories and other features. */
252 gboolean
gst_burn_plugin_init(GstPlugin * burn)253 gst_burn_plugin_init (GstPlugin * burn)
254 {
255   /* debug category for fltering log messages */
256   GST_DEBUG_CATEGORY_INIT (gst_burn_debug, "burn", 0, "Template burn");
257 
258   return gst_element_register (burn, "burn", GST_RANK_NONE, GST_TYPE_BURN);
259 }
260