1 /*
2 * GStreamer
3 * Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
4 * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5 * Copyright (C) 2010 Sreerenj Balachandran <bsreerenj@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Alternatively, the contents of this file may be used under the
26 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27 * which case the following provisions apply instead of the ones
28 * mentioned above:
29 *
30 * This library is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU Library General Public
32 * License as published by the Free Software Foundation; either
33 * version 2 of the License, or (at your option) any later version.
34 *
35 * This library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * Library General Public License for more details.
39 *
40 * You should have received a copy of the GNU Library General Public
41 * License along with this library; if not, write to the
42 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
43 * Boston, MA 02110-1301, USA.
44 */
45
46 /**
47 * SECTION:element-opencvtextoverlay
48 *
49 * opencvtextoverlay renders the text on top of the video frames
50 *
51 * <refsect2>
52 * <title>Example launch line</title>
53 * |[
54 * gst-launch-1.0 videotestsrc ! videoconvert ! opencvtextoverlay text="Opencv Text Overlay " ! videoconvert ! xvimagesink
55 * ]|
56 * </refsect2>
57 */
58
59 #ifdef HAVE_CONFIG_H
60 # include <config.h>
61 #endif
62
63 #include "gsttextoverlay.h"
64
65 GST_DEBUG_CATEGORY_STATIC (gst_opencv_text_overlay_debug);
66 #define GST_CAT_DEFAULT gst_opencv_opencv_text_overlay_debug
67
68 #define DEFAULT_PROP_TEXT "Opencv Text Overlay"
69 #define DEFAULT_PROP_WIDTH 1
70 #define DEFAULT_PROP_HEIGHT 1
71 #define DEFAULT_PROP_XPOS 50
72 #define DEFAULT_PROP_YPOS 50
73 #define DEFAULT_PROP_THICKNESS 2
74 #define DEFAULT_PROP_COLOR 0
75
76
77 /* Filter signals and args */
78 enum
79 {
80 /* FILL ME */
81 LAST_SIGNAL
82 };
83 #define DEFAULT_WIDTH 1.0
84 #define DEFAULT_HEIGHT 1.0
85 enum
86 {
87 PROP_0,
88 PROP_XPOS,
89 PROP_YPOS,
90 PROP_THICKNESS,
91 PROP_COLOR_R,
92 PROP_COLOR_G,
93 PROP_COLOR_B,
94 PROP_TEXT,
95 PROP_HEIGHT,
96 PROP_WIDTH
97 };
98
99 /* the capabilities of the inputs and outputs.
100 *
101 * describe the real formats here.
102 */
103 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
104 GST_PAD_SINK,
105 GST_PAD_ALWAYS,
106 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
107 );
108
109 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
110 GST_PAD_SRC,
111 GST_PAD_ALWAYS,
112 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
113 );
114
115 G_DEFINE_TYPE (GstOpencvTextOverlay, gst_opencv_text_overlay,
116 GST_TYPE_OPENCV_VIDEO_FILTER);
117
118 static void gst_opencv_text_overlay_set_property (GObject * object,
119 guint prop_id, const GValue * value, GParamSpec * pspec);
120 static void gst_opencv_text_overlay_get_property (GObject * object,
121 guint prop_id, GValue * value, GParamSpec * pspec);
122
123 static GstFlowReturn gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter
124 * filter, GstBuffer * buf, cv::Mat img);
125
126 /* Clean up */
127 static void
gst_opencv_text_overlay_finalize(GObject * obj)128 gst_opencv_text_overlay_finalize (GObject * obj)
129 {
130 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
131
132 g_free (filter->textbuf);
133
134 G_OBJECT_CLASS (gst_opencv_text_overlay_parent_class)->finalize (obj);
135 }
136
137 /* initialize the opencvtextoverlay's class */
138 static void
gst_opencv_text_overlay_class_init(GstOpencvTextOverlayClass * klass)139 gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
140 {
141 GObjectClass *gobject_class;
142 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
143 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
144
145 gobject_class = (GObjectClass *) klass;
146 gobject_class->finalize =
147 GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
148 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
149
150 gstopencvbasefilter_class->cv_trans_ip_func =
151 gst_opencv_text_overlay_transform_ip;
152
153 gobject_class->set_property = gst_opencv_text_overlay_set_property;
154 gobject_class->get_property = gst_opencv_text_overlay_get_property;
155
156 g_object_class_install_property (gobject_class, PROP_TEXT,
157 g_param_spec_string ("text", "text",
158 "Text to be display.", DEFAULT_PROP_TEXT,
159 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
160
161 g_object_class_install_property (gobject_class, PROP_XPOS,
162 g_param_spec_int ("xpos", "horizontal position",
163 "Sets the Horizontal position", 0, G_MAXINT,
164 DEFAULT_PROP_XPOS,
165 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
166
167 g_object_class_install_property (gobject_class, PROP_YPOS,
168 g_param_spec_int ("ypos", "vertical position",
169 "Sets the Vertical position", 0, G_MAXINT,
170 DEFAULT_PROP_YPOS,
171 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
172
173 g_object_class_install_property (gobject_class, PROP_THICKNESS,
174 g_param_spec_int ("thickness", "font thickness",
175 "Sets the Thickness of Font", 0, G_MAXINT,
176 DEFAULT_PROP_THICKNESS,
177 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
178
179 g_object_class_install_property (gobject_class, PROP_COLOR_R,
180 g_param_spec_int ("colorR", "color -Red ",
181 "Sets the color -R", 0, 255,
182 DEFAULT_PROP_COLOR,
183 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
184
185 g_object_class_install_property (gobject_class, PROP_COLOR_G,
186 g_param_spec_int ("colorG", "color -Green",
187 "Sets the color -G", 0, 255,
188 DEFAULT_PROP_COLOR,
189 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
190
191 g_object_class_install_property (gobject_class, PROP_COLOR_B,
192 g_param_spec_int ("colorB", "color -Blue",
193 "Sets the color -B", 0, 255,
194 DEFAULT_PROP_COLOR,
195 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
196
197 g_object_class_install_property (gobject_class, PROP_HEIGHT,
198 g_param_spec_double ("height", "Height",
199 "Sets the height of fonts", 1.0, 5.0,
200 DEFAULT_HEIGHT,
201 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
202
203 g_object_class_install_property (gobject_class, PROP_WIDTH,
204 g_param_spec_double ("width", "Width",
205 "Sets the width of fonts", 1.0, 5.0,
206 DEFAULT_WIDTH,
207 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
208
209 gst_element_class_set_static_metadata (element_class,
210 "opencvtextoverlay",
211 "Filter/Effect/Video",
212 "Write text on the top of video", "sreerenj<bsreerenj@gmail.com>");
213
214 gst_element_class_add_static_pad_template (element_class, &src_factory);
215 gst_element_class_add_static_pad_template (element_class, &sink_factory);
216
217 }
218
219 /* initialize the new element
220 * instantiate pads and add them to element
221 * set pad calback functions
222 * initialize instance structure
223 */
224 static void
gst_opencv_text_overlay_init(GstOpencvTextOverlay * filter)225 gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter)
226 {
227 filter->textbuf = g_strdup (DEFAULT_PROP_TEXT);
228 filter->width = DEFAULT_PROP_WIDTH;
229 filter->height = DEFAULT_PROP_HEIGHT;
230 filter->xpos = DEFAULT_PROP_XPOS;
231 filter->ypos = DEFAULT_PROP_YPOS;
232 filter->thickness = DEFAULT_PROP_THICKNESS;
233 filter->colorR = DEFAULT_PROP_COLOR;
234 filter->colorG = DEFAULT_PROP_COLOR;
235 filter->colorB = DEFAULT_PROP_COLOR;
236
237 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
238 TRUE);
239 }
240
241 static void
gst_opencv_text_overlay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)242 gst_opencv_text_overlay_set_property (GObject * object, guint prop_id,
243 const GValue * value, GParamSpec * pspec)
244 {
245 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
246
247 switch (prop_id) {
248 case PROP_TEXT:
249 g_free (filter->textbuf);
250 filter->textbuf = g_value_dup_string (value);
251 break;
252 case PROP_XPOS:
253 filter->xpos = g_value_get_int (value);
254 break;
255 case PROP_YPOS:
256 filter->ypos = g_value_get_int (value);
257 break;
258 case PROP_THICKNESS:
259 filter->thickness = g_value_get_int (value);
260 break;
261
262 case PROP_COLOR_R:
263 filter->colorR = g_value_get_int (value);
264 break;
265 case PROP_COLOR_G:
266 filter->colorG = g_value_get_int (value);
267 break;
268 case PROP_COLOR_B:
269 filter->colorB = g_value_get_int (value);
270 break;
271
272 case PROP_HEIGHT:
273 filter->height = g_value_get_double (value);
274 break;
275 case PROP_WIDTH:
276 filter->width = g_value_get_double (value);
277 break;
278 default:
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 break;
281 }
282 }
283
284 static void
gst_opencv_text_overlay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)285 gst_opencv_text_overlay_get_property (GObject * object, guint prop_id,
286 GValue * value, GParamSpec * pspec)
287 {
288 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
289
290 switch (prop_id) {
291 case PROP_TEXT:
292 g_value_set_string (value, filter->textbuf);
293 break;
294 case PROP_XPOS:
295 g_value_set_int (value, filter->xpos);
296 break;
297 case PROP_YPOS:
298 g_value_set_int (value, filter->ypos);
299 break;
300 case PROP_THICKNESS:
301 g_value_set_int (value, filter->thickness);
302 break;
303 case PROP_COLOR_R:
304 g_value_set_int (value, filter->colorR);
305 break;
306 case PROP_COLOR_G:
307 g_value_set_int (value, filter->colorG);
308 break;
309 case PROP_COLOR_B:
310 g_value_set_int (value, filter->colorB);
311 break;
312 case PROP_HEIGHT:
313 g_value_set_double (value, filter->height);
314 break;
315 case PROP_WIDTH:
316 g_value_set_double (value, filter->width);
317 break;
318 default:
319 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320 break;
321 }
322 }
323
324 /* chain function
325 * this function does the actual processing
326 */
327 static GstFlowReturn
gst_opencv_text_overlay_transform_ip(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img)328 gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter * base,
329 GstBuffer * buf, cv::Mat img)
330 {
331 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (base);
332
333 cv::putText (img, filter->textbuf, cv::Point (filter->xpos,
334 filter->ypos), cv::FONT_HERSHEY_SIMPLEX,
335 (filter->width + filter->height) * 0.5, cv::Scalar (filter->colorR,
336 filter->colorG, filter->colorB), filter->thickness);
337
338 return GST_FLOW_OK;
339 }
340
341
342 /* entry point to initialize the plug-in
343 * initialize the plug-in itself
344 * register the element factories and other features
345 */
346 gboolean
gst_opencv_text_overlay_plugin_init(GstPlugin * plugin)347 gst_opencv_text_overlay_plugin_init (GstPlugin * plugin)
348 {
349 /* debug category for fltering log messages
350 *
351 * exchange the string 'Template opencvtextoverlay' with your description
352 */
353 GST_DEBUG_CATEGORY_INIT (gst_opencv_text_overlay_debug, "opencvtextoverlay",
354 0, "Template opencvtextoverlay");
355
356 return gst_element_register (plugin, "opencvtextoverlay", GST_RANK_NONE,
357 GST_TYPE_OPENCV_TEXT_OVERLAY);
358 }
359