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>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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
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.
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.
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., 59 Temple Place - Suite 330,
43 * Boston, MA 02111-1307, USA.
47 * SECTION:element-opencvtextoverlay
49 * opencvtextoverlay renders the text on top of the video frames
52 * <title>Example launch line</title>
54 * gst-launch-0.10 videotestsrc ! ffmpegcolorspace ! opencvtextoverlay text="Opencv Text Overlay " ! ffmpegcolorspace ! xvimagesink
65 #include "gstopencvutils.h"
66 #include "gsttextoverlay.h"
68 GST_DEBUG_CATEGORY_STATIC (gst_opencv_text_overlay_debug);
69 #define GST_CAT_DEFAULT gst_opencv_opencv_text_overlay_debug
71 #define DEFAULT_PROP_TEXT ""
72 #define DEFAULT_PROP_WIDTH 1
73 #define DEFAULT_PROP_HEIGHT 1
74 #define DEFAULT_PROP_XPOS 50
75 #define DEFAULT_PROP_YPOS 50
76 #define DEFAULT_PROP_THICKNESS 2
77 #define DEFAULT_PROP_COLOR 0
80 /* Filter signals and args */
86 #define DEFAULT_WIDTH 1.0
87 #define DEFAULT_HEIGHT 1.0
102 /* the capabilities of the inputs and outputs.
104 * describe the real formats here.
106 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
109 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
112 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
115 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
118 GST_BOILERPLATE (GstOpencvTextOverlay, gst_opencv_text_overlay, GstElement,
121 static void gst_opencv_text_overlay_set_property (GObject * object,
122 guint prop_id, const GValue * value, GParamSpec * pspec);
123 static void gst_opencv_text_overlay_get_property (GObject * object,
124 guint prop_id, GValue * value, GParamSpec * pspec);
126 static gboolean gst_opencv_text_overlay_set_caps (GstPad * pad, GstCaps * caps);
127 static GstFlowReturn gst_opencv_text_overlay_chain (GstPad * pad,
134 gst_opencv_text_overlay_finalize (GObject * obj)
136 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
138 if (filter->cvImage) {
139 cvReleaseImage (&filter->cvImage);
142 G_OBJECT_CLASS (parent_class)->finalize (obj);
147 /* GObject vmethod implementations */
150 gst_opencv_text_overlay_base_init (gpointer gclass)
152 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
154 gst_element_class_set_details_simple (element_class,
156 "Filter/Effect/Video",
157 "Write text on the top of video", "sreerenj<bsreerenj@gmail.com>");
159 gst_element_class_add_static_pad_template (element_class, &src_factory);
160 gst_element_class_add_static_pad_template (element_class, &sink_factory);
163 /* initialize the opencvtextoverlay's class */
165 gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
167 GObjectClass *gobject_class;
169 gobject_class = (GObjectClass *) klass;
171 parent_class = g_type_class_peek_parent (klass);
172 gobject_class->finalize =
173 GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
175 gobject_class->set_property = gst_opencv_text_overlay_set_property;
176 gobject_class->get_property = gst_opencv_text_overlay_get_property;
179 g_object_class_install_property (gobject_class, PROP_TEXT,
180 g_param_spec_string ("text", "text",
181 "Text to be display.", DEFAULT_PROP_TEXT,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class, PROP_XPOS,
185 g_param_spec_int ("xpos", "horizontal position",
186 "Sets the Horizontal position", 0, G_MAXINT,
187 DEFAULT_PROP_XPOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (gobject_class, PROP_YPOS,
190 g_param_spec_int ("ypos", "vertical position",
191 "Sets the Vertical position", 0, G_MAXINT,
192 DEFAULT_PROP_YPOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (gobject_class, PROP_THICKNESS,
195 g_param_spec_int ("thickness", "font thickness",
196 "Sets the Thickness of Font", 0, G_MAXINT,
197 DEFAULT_PROP_THICKNESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (gobject_class, PROP_COLOR_R,
200 g_param_spec_int ("colorR", "color -Red ",
201 "Sets the color -R", 0, 255,
202 DEFAULT_PROP_COLOR, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class, PROP_COLOR_G,
205 g_param_spec_int ("colorG", "color -Green",
206 "Sets the color -G", 0, 255,
207 DEFAULT_PROP_COLOR, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (gobject_class, PROP_COLOR_B,
210 g_param_spec_int ("colorB", "color -Blue",
211 "Sets the color -B", 0, 255,
212 DEFAULT_PROP_COLOR, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class, PROP_HEIGHT,
215 g_param_spec_double ("height", "Height",
216 "Sets the height of fonts", 1.0, 5.0,
217 DEFAULT_HEIGHT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_WIDTH,
220 g_param_spec_double ("width", "Width",
221 "Sets the width of fonts", 1.0, 5.0,
222 DEFAULT_WIDTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 /* initialize the new element
227 * instantiate pads and add them to element
228 * set pad calback functions
229 * initialize instance structure
232 gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter,
233 GstOpencvTextOverlayClass * gclass)
235 filter->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
236 gst_pad_set_setcaps_function (filter->sinkpad,
237 GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_set_caps));
238 gst_pad_set_getcaps_function (filter->sinkpad,
239 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
240 gst_pad_set_chain_function (filter->sinkpad,
241 GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_chain));
243 filter->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
244 gst_pad_set_getcaps_function (filter->srcpad,
245 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
247 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
248 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
249 filter->textbuf = g_strdup (DEFAULT_PROP_TEXT);
250 filter->width = DEFAULT_PROP_WIDTH;
251 filter->height = DEFAULT_PROP_HEIGHT;
252 filter->xpos = DEFAULT_PROP_XPOS;
253 filter->ypos = DEFAULT_PROP_YPOS;
254 filter->thickness = DEFAULT_PROP_THICKNESS;
255 filter->colorR = DEFAULT_PROP_COLOR;
256 filter->colorG = DEFAULT_PROP_COLOR;
257 filter->colorB = DEFAULT_PROP_COLOR;
262 gst_opencv_text_overlay_set_property (GObject * object, guint prop_id,
263 const GValue * value, GParamSpec * pspec)
265 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
269 g_free (filter->textbuf);
270 filter->textbuf = g_value_dup_string (value);
273 filter->xpos = g_value_get_int (value);
276 filter->ypos = g_value_get_int (value);
279 filter->thickness = g_value_get_int (value);
283 filter->colorR = g_value_get_int (value);
286 filter->colorG = g_value_get_int (value);
289 filter->colorB = g_value_get_int (value);
293 filter->height = g_value_get_double (value);
296 filter->width = g_value_get_double (value);
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 gst_opencv_text_overlay_get_property (GObject * object, guint prop_id,
306 GValue * value, GParamSpec * pspec)
308 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
312 g_value_set_string (value, filter->textbuf);
315 g_value_set_int (value, filter->xpos);
318 g_value_set_int (value, filter->ypos);
321 g_value_set_int (value, filter->thickness);
324 g_value_set_int (value, filter->colorR);
327 g_value_set_int (value, filter->colorG);
330 g_value_set_int (value, filter->colorB);
333 g_value_set_double (value, filter->height);
336 g_value_set_double (value, filter->width);
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 /* GstElement vmethod implementations */
346 /* this function handles the link with other elements */
348 gst_opencv_text_overlay_set_caps (GstPad * pad, GstCaps * caps)
350 GstOpencvTextOverlay *filter;
354 GstStructure *structure;
356 filter = GST_OPENCV_TEXT_OVERLAY (gst_pad_get_parent (pad));
358 structure = gst_caps_get_structure (caps, 0);
359 gst_structure_get_int (structure, "width", &width);
360 gst_structure_get_int (structure, "height", &height);
362 filter->cvImage = cvCreateImage (cvSize (width, height), IPL_DEPTH_8U, 3);
363 filter->cvStorage = cvCreateMemStorage (0);
365 otherpad = (pad == filter->srcpad) ? filter->sinkpad : filter->srcpad;
366 gst_object_unref (filter);
368 return gst_pad_set_caps (otherpad, caps);
372 * this function does the actual processing
375 gst_opencv_text_overlay_chain (GstPad * pad, GstBuffer * buf)
377 GstOpencvTextOverlay *filter;
379 filter = GST_OPENCV_TEXT_OVERLAY (GST_OBJECT_PARENT (pad));
381 filter->cvImage->imageData = (char *) GST_BUFFER_DATA (buf);
383 cvInitFont (&(filter->font), CV_FONT_VECTOR0, filter->width, filter->height,
384 0, filter->thickness, 0);
386 buf = gst_buffer_make_writable (buf);
387 cvPutText (filter->cvImage, filter->textbuf, cvPoint (filter->xpos,
388 filter->ypos), &(filter->font), cvScalar (filter->colorR,
389 filter->colorG, filter->colorB, 0));
391 return gst_pad_push (filter->srcpad, buf);
395 /* entry point to initialize the plug-in
396 * initialize the plug-in itself
397 * register the element factories and other features
400 gst_opencv_text_overlay_plugin_init (GstPlugin * plugin)
402 /* debug category for fltering log messages
404 * exchange the string 'Template opencvtextoverlay' with your description
406 GST_DEBUG_CATEGORY_INIT (gst_opencv_text_overlay_debug, "opencvtextoverlay",
407 0, "Template opencvtextoverlay");
409 return gst_element_register (plugin, "opencvtextoverlay", GST_RANK_NONE,
410 GST_TYPE_OPENCV_TEXT_OVERLAY);