Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / ext / opencv / gsttextoverlay.c
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., 59 Temple Place - Suite 330,
43  * Boston, MA 02111-1307, 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-0.10 videotestsrc ! ffmpegcolorspace ! opencvtextoverlay text="Opencv Text Overlay " ! ffmpegcolorspace ! xvimagesink
55  * ]|
56  * </refsect2>
57  */
58
59 #ifdef HAVE_CONFIG_H
60 #  include <config.h>
61 #endif
62
63 #include <gst/gst.h>
64
65 #include "gstopencvutils.h"
66 #include "gsttextoverlay.h"
67
68 GST_DEBUG_CATEGORY_STATIC (gst_opencv_text_overlay_debug);
69 #define GST_CAT_DEFAULT gst_opencv_opencv_text_overlay_debug
70
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
78
79
80 /* Filter signals and args */
81 enum
82 {
83   /* FILL ME */
84   LAST_SIGNAL
85 };
86 #define DEFAULT_WIDTH     1.0
87 #define DEFAULT_HEIGHT    1.0
88 enum
89 {
90   PROP_0,
91   PROP_XPOS,
92   PROP_YPOS,
93   PROP_THICKNESS,
94   PROP_COLOR_R,
95   PROP_COLOR_G,
96   PROP_COLOR_B,
97   PROP_TEXT,
98   PROP_HEIGHT,
99   PROP_WIDTH
100 };
101
102 /* the capabilities of the inputs and outputs.
103  *
104  * describe the real formats here.
105  */
106 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
107     GST_PAD_SINK,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
110     );
111
112 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
113     GST_PAD_SRC,
114     GST_PAD_ALWAYS,
115     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
116     );
117
118 GST_BOILERPLATE (GstOpencvTextOverlay, gst_opencv_text_overlay, GstElement,
119     GST_TYPE_ELEMENT);
120
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);
125
126 static gboolean gst_opencv_text_overlay_set_caps (GstPad * pad, GstCaps * caps);
127 static GstFlowReturn gst_opencv_text_overlay_chain (GstPad * pad,
128     GstBuffer * buf);
129
130
131
132 /* Clean up */
133 static void
134 gst_opencv_text_overlay_finalize (GObject * obj)
135 {
136   GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
137
138   if (filter->cvImage) {
139     cvReleaseImage (&filter->cvImage);
140   }
141
142   G_OBJECT_CLASS (parent_class)->finalize (obj);
143 }
144
145
146
147 /* GObject vmethod implementations */
148
149 static void
150 gst_opencv_text_overlay_base_init (gpointer gclass)
151 {
152   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
153
154   gst_element_class_set_details_simple (element_class,
155       "opencvtextoverlay",
156       "Filter/Effect/Video",
157       "Write text on the top of video", "sreerenj<bsreerenj@gmail.com>");
158
159   gst_element_class_add_static_pad_template (element_class, &src_factory);
160   gst_element_class_add_static_pad_template (element_class, &sink_factory);
161 }
162
163 /* initialize the opencvtextoverlay's class */
164 static void
165 gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
166 {
167   GObjectClass *gobject_class;
168
169   gobject_class = (GObjectClass *) klass;
170
171   parent_class = g_type_class_peek_parent (klass);
172   gobject_class->finalize =
173       GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
174
175   gobject_class->set_property = gst_opencv_text_overlay_set_property;
176   gobject_class->get_property = gst_opencv_text_overlay_get_property;
177
178
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));
183
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));
188
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));
193
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));
198
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));
203
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));
208
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));
213
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));
218
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));
223
224 }
225
226 /* initialize the new element
227  * instantiate pads and add them to element
228  * set pad calback functions
229  * initialize instance structure
230  */
231 static void
232 gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter,
233     GstOpencvTextOverlayClass * gclass)
234 {
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));
242
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));
246
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;
258
259 }
260
261 static void
262 gst_opencv_text_overlay_set_property (GObject * object, guint prop_id,
263     const GValue * value, GParamSpec * pspec)
264 {
265   GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
266
267   switch (prop_id) {
268     case PROP_TEXT:
269       g_free (filter->textbuf);
270       filter->textbuf = g_value_dup_string (value);
271       break;
272     case PROP_XPOS:
273       filter->xpos = g_value_get_int (value);
274       break;
275     case PROP_YPOS:
276       filter->ypos = g_value_get_int (value);
277       break;
278     case PROP_THICKNESS:
279       filter->thickness = g_value_get_int (value);
280       break;
281
282     case PROP_COLOR_R:
283       filter->colorR = g_value_get_int (value);
284       break;
285     case PROP_COLOR_G:
286       filter->colorG = g_value_get_int (value);
287       break;
288     case PROP_COLOR_B:
289       filter->colorB = g_value_get_int (value);
290       break;
291
292     case PROP_HEIGHT:
293       filter->height = g_value_get_double (value);
294       break;
295     case PROP_WIDTH:
296       filter->width = g_value_get_double (value);
297       break;
298     default:
299       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300       break;
301   }
302 }
303
304 static void
305 gst_opencv_text_overlay_get_property (GObject * object, guint prop_id,
306     GValue * value, GParamSpec * pspec)
307 {
308   GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
309
310   switch (prop_id) {
311     case PROP_TEXT:
312       g_value_set_string (value, filter->textbuf);
313       break;
314     case PROP_XPOS:
315       g_value_set_int (value, filter->xpos);
316       break;
317     case PROP_YPOS:
318       g_value_set_int (value, filter->ypos);
319       break;
320     case PROP_THICKNESS:
321       g_value_set_int (value, filter->thickness);
322       break;
323     case PROP_COLOR_R:
324       g_value_set_int (value, filter->colorR);
325       break;
326     case PROP_COLOR_G:
327       g_value_set_int (value, filter->colorG);
328       break;
329     case PROP_COLOR_B:
330       g_value_set_int (value, filter->colorB);
331       break;
332     case PROP_HEIGHT:
333       g_value_set_double (value, filter->height);
334       break;
335     case PROP_WIDTH:
336       g_value_set_double (value, filter->width);
337       break;
338     default:
339       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340       break;
341   }
342 }
343
344 /* GstElement vmethod implementations */
345
346 /* this function handles the link with other elements */
347 static gboolean
348 gst_opencv_text_overlay_set_caps (GstPad * pad, GstCaps * caps)
349 {
350   GstOpencvTextOverlay *filter;
351   GstPad *otherpad;
352
353   gint width, height;
354   GstStructure *structure;
355
356   filter = GST_OPENCV_TEXT_OVERLAY (gst_pad_get_parent (pad));
357
358   structure = gst_caps_get_structure (caps, 0);
359   gst_structure_get_int (structure, "width", &width);
360   gst_structure_get_int (structure, "height", &height);
361
362   filter->cvImage = cvCreateImage (cvSize (width, height), IPL_DEPTH_8U, 3);
363   filter->cvStorage = cvCreateMemStorage (0);
364
365   otherpad = (pad == filter->srcpad) ? filter->sinkpad : filter->srcpad;
366   gst_object_unref (filter);
367
368   return gst_pad_set_caps (otherpad, caps);
369 }
370
371 /* chain function
372  * this function does the actual processing
373  */
374 static GstFlowReturn
375 gst_opencv_text_overlay_chain (GstPad * pad, GstBuffer * buf)
376 {
377   GstOpencvTextOverlay *filter;
378
379   filter = GST_OPENCV_TEXT_OVERLAY (GST_OBJECT_PARENT (pad));
380
381   filter->cvImage->imageData = (char *) GST_BUFFER_DATA (buf);
382
383   cvInitFont (&(filter->font), CV_FONT_VECTOR0, filter->width, filter->height,
384       0, filter->thickness, 0);
385
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));
390
391   return gst_pad_push (filter->srcpad, buf);
392 }
393
394
395 /* entry point to initialize the plug-in
396  * initialize the plug-in itself
397  * register the element factories and other features
398  */
399 gboolean
400 gst_opencv_text_overlay_plugin_init (GstPlugin * plugin)
401 {
402   /* debug category for fltering log messages
403    *
404    * exchange the string 'Template opencvtextoverlay' with your description
405    */
406   GST_DEBUG_CATEGORY_INIT (gst_opencv_text_overlay_debug, "opencvtextoverlay",
407       0, "Template opencvtextoverlay");
408
409   return gst_element_register (plugin, "opencvtextoverlay", GST_RANK_NONE,
410       GST_TYPE_OPENCV_TEXT_OVERLAY);
411 }