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 * ## Example launch line
52 *
53 * |[
54 * gst-launch-1.0 videotestsrc ! videoconvert ! opencvtextoverlay text="Opencv Text Overlay " ! videoconvert ! xvimagesink
55 * ]|
56 */
57
58 #ifdef HAVE_CONFIG_H
59 # include <config.h>
60 #endif
61
62 #include "gsttextoverlay.h"
63
64 GST_DEBUG_CATEGORY_STATIC (gst_opencv_text_overlay_debug);
65 #define GST_CAT_DEFAULT gst_opencv_opencv_text_overlay_debug
66
67 #define DEFAULT_PROP_TEXT "Opencv Text Overlay"
68 #define DEFAULT_PROP_WIDTH 1
69 #define DEFAULT_PROP_HEIGHT 1
70 #define DEFAULT_PROP_XPOS 50
71 #define DEFAULT_PROP_YPOS 50
72 #define DEFAULT_PROP_THICKNESS 2
73 #define DEFAULT_PROP_COLOR 0
74
75
76 /* Filter signals and args */
77 enum
78 {
79 /* FILL ME */
80 LAST_SIGNAL
81 };
82 #define DEFAULT_WIDTH 1.0
83 #define DEFAULT_HEIGHT 1.0
84 enum
85 {
86 PROP_0,
87 PROP_XPOS,
88 PROP_YPOS,
89 PROP_THICKNESS,
90 PROP_COLOR_R,
91 PROP_COLOR_G,
92 PROP_COLOR_B,
93 PROP_TEXT,
94 PROP_HEIGHT,
95 PROP_WIDTH
96 };
97
98 /* the capabilities of the inputs and outputs.
99 *
100 * describe the real formats here.
101 */
102 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
103 GST_PAD_SINK,
104 GST_PAD_ALWAYS,
105 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
106 );
107
108 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
109 GST_PAD_SRC,
110 GST_PAD_ALWAYS,
111 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
112 );
113
114 G_DEFINE_TYPE_WITH_CODE (GstOpencvTextOverlay, gst_opencv_text_overlay,
115 GST_TYPE_OPENCV_VIDEO_FILTER,
116 GST_DEBUG_CATEGORY_INIT (gst_opencv_text_overlay_debug, "opencvtextoverlay",
117 0, "Template opencvtextoverlay");
118 );
119 GST_ELEMENT_REGISTER_DEFINE (opencvtextoverlay, "opencvtextoverlay",
120 GST_RANK_NONE, GST_TYPE_OPENCV_TEXT_OVERLAY);
121
122 static void gst_opencv_text_overlay_set_property (GObject * object,
123 guint prop_id, const GValue * value, GParamSpec * pspec);
124 static void gst_opencv_text_overlay_get_property (GObject * object,
125 guint prop_id, GValue * value, GParamSpec * pspec);
126
127 static GstFlowReturn gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter
128 * filter, GstBuffer * buf, cv::Mat img);
129
130 /* Clean up */
131 static void
gst_opencv_text_overlay_finalize(GObject * obj)132 gst_opencv_text_overlay_finalize (GObject * obj)
133 {
134 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
135
136 g_free (filter->textbuf);
137
138 G_OBJECT_CLASS (gst_opencv_text_overlay_parent_class)->finalize (obj);
139 }
140
141 /* initialize the opencvtextoverlay's class */
142 static void
gst_opencv_text_overlay_class_init(GstOpencvTextOverlayClass * klass)143 gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
144 {
145 GObjectClass *gobject_class;
146 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
147 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
148
149 gobject_class = (GObjectClass *) klass;
150 gobject_class->finalize =
151 GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
152 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
153
154 gstopencvbasefilter_class->cv_trans_ip_func =
155 gst_opencv_text_overlay_transform_ip;
156
157 gobject_class->set_property = gst_opencv_text_overlay_set_property;
158 gobject_class->get_property = gst_opencv_text_overlay_get_property;
159
160 g_object_class_install_property (gobject_class, PROP_TEXT,
161 g_param_spec_string ("text", "text",
162 "Text to be display.", DEFAULT_PROP_TEXT,
163 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
164
165 g_object_class_install_property (gobject_class, PROP_XPOS,
166 g_param_spec_int ("xpos", "horizontal position",
167 "Sets the Horizontal position", 0, G_MAXINT,
168 DEFAULT_PROP_XPOS,
169 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
170
171 g_object_class_install_property (gobject_class, PROP_YPOS,
172 g_param_spec_int ("ypos", "vertical position",
173 "Sets the Vertical position", 0, G_MAXINT,
174 DEFAULT_PROP_YPOS,
175 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
176
177 g_object_class_install_property (gobject_class, PROP_THICKNESS,
178 g_param_spec_int ("thickness", "font thickness",
179 "Sets the Thickness of Font", 0, G_MAXINT,
180 DEFAULT_PROP_THICKNESS,
181 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
182
183 g_object_class_install_property (gobject_class, PROP_COLOR_R,
184 g_param_spec_int ("colorR", "color -Red ",
185 "Sets the color -R", 0, 255,
186 DEFAULT_PROP_COLOR,
187 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
188
189 g_object_class_install_property (gobject_class, PROP_COLOR_G,
190 g_param_spec_int ("colorG", "color -Green",
191 "Sets the color -G", 0, 255,
192 DEFAULT_PROP_COLOR,
193 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
194
195 g_object_class_install_property (gobject_class, PROP_COLOR_B,
196 g_param_spec_int ("colorB", "color -Blue",
197 "Sets the color -B", 0, 255,
198 DEFAULT_PROP_COLOR,
199 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
200
201 g_object_class_install_property (gobject_class, PROP_HEIGHT,
202 g_param_spec_double ("height", "Height",
203 "Sets the height of fonts", 1.0, 5.0,
204 DEFAULT_HEIGHT,
205 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
206
207 g_object_class_install_property (gobject_class, PROP_WIDTH,
208 g_param_spec_double ("width", "Width",
209 "Sets the width of fonts", 1.0, 5.0,
210 DEFAULT_WIDTH,
211 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
212
213 gst_element_class_set_static_metadata (element_class,
214 "opencvtextoverlay",
215 "Filter/Effect/Video",
216 "Write text on the top of video", "sreerenj<bsreerenj@gmail.com>");
217
218 gst_element_class_add_static_pad_template (element_class, &src_factory);
219 gst_element_class_add_static_pad_template (element_class, &sink_factory);
220
221 }
222
223 /* initialize the new element
224 * instantiate pads and add them to element
225 * set pad callback functions
226 * initialize instance structure
227 */
228 static void
gst_opencv_text_overlay_init(GstOpencvTextOverlay * filter)229 gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter)
230 {
231 filter->textbuf = g_strdup (DEFAULT_PROP_TEXT);
232 filter->width = DEFAULT_PROP_WIDTH;
233 filter->height = DEFAULT_PROP_HEIGHT;
234 filter->xpos = DEFAULT_PROP_XPOS;
235 filter->ypos = DEFAULT_PROP_YPOS;
236 filter->thickness = DEFAULT_PROP_THICKNESS;
237 filter->colorR = DEFAULT_PROP_COLOR;
238 filter->colorG = DEFAULT_PROP_COLOR;
239 filter->colorB = DEFAULT_PROP_COLOR;
240
241 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
242 TRUE);
243 }
244
245 static void
gst_opencv_text_overlay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)246 gst_opencv_text_overlay_set_property (GObject * object, guint prop_id,
247 const GValue * value, GParamSpec * pspec)
248 {
249 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
250
251 switch (prop_id) {
252 case PROP_TEXT:
253 g_free (filter->textbuf);
254 filter->textbuf = g_value_dup_string (value);
255 break;
256 case PROP_XPOS:
257 filter->xpos = g_value_get_int (value);
258 break;
259 case PROP_YPOS:
260 filter->ypos = g_value_get_int (value);
261 break;
262 case PROP_THICKNESS:
263 filter->thickness = g_value_get_int (value);
264 break;
265
266 case PROP_COLOR_R:
267 filter->colorR = g_value_get_int (value);
268 break;
269 case PROP_COLOR_G:
270 filter->colorG = g_value_get_int (value);
271 break;
272 case PROP_COLOR_B:
273 filter->colorB = g_value_get_int (value);
274 break;
275
276 case PROP_HEIGHT:
277 filter->height = g_value_get_double (value);
278 break;
279 case PROP_WIDTH:
280 filter->width = g_value_get_double (value);
281 break;
282 default:
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 break;
285 }
286 }
287
288 static void
gst_opencv_text_overlay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)289 gst_opencv_text_overlay_get_property (GObject * object, guint prop_id,
290 GValue * value, GParamSpec * pspec)
291 {
292 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
293
294 switch (prop_id) {
295 case PROP_TEXT:
296 g_value_set_string (value, filter->textbuf);
297 break;
298 case PROP_XPOS:
299 g_value_set_int (value, filter->xpos);
300 break;
301 case PROP_YPOS:
302 g_value_set_int (value, filter->ypos);
303 break;
304 case PROP_THICKNESS:
305 g_value_set_int (value, filter->thickness);
306 break;
307 case PROP_COLOR_R:
308 g_value_set_int (value, filter->colorR);
309 break;
310 case PROP_COLOR_G:
311 g_value_set_int (value, filter->colorG);
312 break;
313 case PROP_COLOR_B:
314 g_value_set_int (value, filter->colorB);
315 break;
316 case PROP_HEIGHT:
317 g_value_set_double (value, filter->height);
318 break;
319 case PROP_WIDTH:
320 g_value_set_double (value, filter->width);
321 break;
322 default:
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324 break;
325 }
326 }
327
328 /* chain function
329 * this function does the actual processing
330 */
331 static GstFlowReturn
gst_opencv_text_overlay_transform_ip(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img)332 gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter * base,
333 GstBuffer * buf, cv::Mat img)
334 {
335 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (base);
336
337 cv::putText (img, filter->textbuf, cv::Point (filter->xpos,
338 filter->ypos), cv::FONT_HERSHEY_SIMPLEX,
339 (filter->width + filter->height) * 0.5, cv::Scalar (filter->colorR,
340 filter->colorG, filter->colorB), filter->thickness);
341
342 return GST_FLOW_OK;
343 }
344