VideoFilter inherits from
[platform/upstream/gstreamer.git] / gst / debug / gstnavigationtest.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstnavigationtest.h"
26 #include <string.h>
27 #include <math.h>
28
29 #include <gst/video/video.h>
30
31 GST_DEBUG_CATEGORY (navigationtest_debug);
32 #define GST_CAT_DEFAULT navigationtest_debug
33
34 static GstElementDetails navigationtest_details =
35 GST_ELEMENT_DETAILS ("Video Navigation test",
36     "Filter/Effect/Video",
37     "Handle navigation events showing a black square following mouse pointer",
38     "David Schleef <ds@schleef.org>");
39
40 static GstStaticPadTemplate gst_navigationtest_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
45     );
46
47 static GstStaticPadTemplate gst_navigationtest_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
49     GST_PAD_SINK,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
52     );
53
54 static GstVideofilterClass *parent_class = NULL;
55
56 static gboolean
57 gst_navigationtest_handle_src_event (GstPad * pad, GstEvent * event)
58 {
59   GstNavigationtest *navtest;
60   const gchar *type;
61
62   navtest = GST_NAVIGATIONTEST (GST_PAD_PARENT (pad));
63
64   switch (GST_EVENT_TYPE (event)) {
65     case GST_EVENT_NAVIGATION:
66     {
67       const GstStructure *s = gst_event_get_structure (event);
68       gint fps_n, fps_d;
69
70       fps_n = gst_value_get_fraction_numerator ((&navtest->framerate));
71       fps_d = gst_value_get_fraction_denominator ((&navtest->framerate));
72
73       type = gst_structure_get_string (s, "event");
74       if (g_str_equal (type, "mouse-move")) {
75         gst_structure_get_double (s, "pointer_x", &navtest->x);
76         gst_structure_get_double (s, "pointer_y", &navtest->y);
77       } else if (g_str_equal (type, "mouse-button-press")) {
78         ButtonClick *click = g_new (ButtonClick, 1);
79
80         gst_structure_get_double (s, "pointer_x", &click->x);
81         gst_structure_get_double (s, "pointer_y", &click->y);
82         click->images_left = (fps_n + fps_d - 1) / fps_d;
83         /* green */
84         click->cy = 150;
85         click->cu = 46;
86         click->cv = 21;
87         navtest->clicks = g_slist_prepend (navtest->clicks, click);
88       } else if (g_str_equal (type, "mouse-button-release")) {
89         ButtonClick *click = g_new (ButtonClick, 1);
90
91         gst_structure_get_double (s, "pointer_x", &click->x);
92         gst_structure_get_double (s, "pointer_y", &click->y);
93         click->images_left = (fps_n + fps_d - 1) / fps_d;
94         /* red */
95         click->cy = 76;
96         click->cu = 85;
97         click->cv = 255;
98         navtest->clicks = g_slist_prepend (navtest->clicks, click);
99       }
100       break;
101     }
102     default:
103       break;
104   }
105   return gst_pad_event_default (pad, event);
106 }
107
108 /* Useful macros */
109 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
110 #define GST_VIDEO_I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
111 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
112
113 #define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
114 #define GST_VIDEO_I420_U_OFFSET(w,h) (GST_VIDEO_I420_Y_OFFSET(w,h)+(GST_VIDEO_I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
115 #define GST_VIDEO_I420_V_OFFSET(w,h) (GST_VIDEO_I420_U_OFFSET(w,h)+(GST_VIDEO_I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
116
117 #define GST_VIDEO_I420_SIZE(w,h)     (GST_VIDEO_I420_V_OFFSET(w,h)+(GST_VIDEO_I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
118
119 static gboolean
120 gst_navigationtest_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
121     guint * size)
122 {
123   GstNavigationtest *navtest;
124   GstStructure *structure;
125   gboolean ret = FALSE;
126   gint width, height;
127
128   navtest = GST_NAVIGATIONTEST (btrans);
129
130   structure = gst_caps_get_structure (caps, 0);
131
132   if (gst_structure_get_int (structure, "width", &width) &&
133       gst_structure_get_int (structure, "height", &height)) {
134     *size = GST_VIDEO_I420_SIZE (width, height);
135     ret = TRUE;
136     GST_DEBUG_OBJECT (navtest, "our frame size is %d bytes (%dx%d)", *size,
137         width, height);
138   }
139
140   return ret;
141 }
142
143 static gboolean
144 gst_navigationtest_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
145     GstCaps * outcaps)
146 {
147   GstNavigationtest *navtest = GST_NAVIGATIONTEST (btrans);
148   gboolean ret = FALSE;
149   GstStructure *structure;
150
151   structure = gst_caps_get_structure (incaps, 0);
152
153   if (gst_structure_get_int (structure, "width", &navtest->width) &&
154       gst_structure_get_int (structure, "height", &navtest->height)) {
155     const GValue *framerate;
156
157     framerate = gst_structure_get_value (structure, "framerate");
158     if (framerate && GST_VALUE_HOLDS_FRACTION (framerate)) {
159       g_value_copy (framerate, &navtest->framerate);
160       ret = TRUE;
161     }
162   }
163
164   return ret;
165 }
166
167 static void
168 draw_box_planar411 (guint8 * dest, int width, int height, int x, int y,
169     guint8 colory, guint8 coloru, guint8 colorv)
170 {
171   int x1, x2, y1, y2;
172
173   if (x < 0 || y < 0 || x >= width || y >= height)
174     return;
175
176   x1 = MAX (x - 5, 0);
177   x2 = MIN (x + 5, width);
178   y1 = MAX (y - 5, 0);
179   y2 = MIN (y + 5, height);
180
181   for (y = y1; y < y2; y++) {
182     for (x = x1; x < x2; x++) {
183       ((guint8 *) dest)[y * width + x] = colory;
184     }
185   }
186
187   dest += height * width;
188   width /= 2;
189   height /= 2;
190   x1 /= 2;
191   x2 /= 2;
192   y1 /= 2;
193   y2 /= 2;
194   for (y = y1; y < y2; y++) {
195     for (x = x1; x < x2; x++) {
196       ((guint8 *) dest)[y * width + x] = coloru;
197     }
198   }
199
200   dest += height * width;
201   for (y = y1; y < y2; y++) {
202     for (x = x1; x < x2; x++) {
203       ((guint8 *) dest)[y * width + x] = colorv;
204     }
205   }
206 }
207
208 static GstFlowReturn
209 gst_navigationtest_transform (GstBaseTransform * trans, GstBuffer * in,
210     GstBuffer * out)
211 {
212   GstNavigationtest *navtest = GST_NAVIGATIONTEST (trans);
213   GSList *walk;
214   GstFlowReturn ret = GST_FLOW_OK;
215
216   /* do something interesting here.  This simply copies the source
217    * to the destination. */
218   gst_buffer_stamp (out, in);
219
220   memcpy (GST_BUFFER_DATA (out), GST_BUFFER_DATA (in),
221       MIN (GST_BUFFER_SIZE (in), GST_BUFFER_SIZE (out)));
222
223   walk = navtest->clicks;
224   while (walk) {
225     ButtonClick *click = walk->data;
226
227     walk = g_slist_next (walk);
228     draw_box_planar411 (GST_BUFFER_DATA (out), navtest->width, navtest->height,
229         rint (click->x), rint (click->y), click->cy, click->cu, click->cv);
230     if (--click->images_left < 1) {
231       navtest->clicks = g_slist_remove (navtest->clicks, click);
232       g_free (click);
233     }
234   }
235   draw_box_planar411 (GST_BUFFER_DATA (out), navtest->width, navtest->height,
236       rint (navtest->x), rint (navtest->y), 0, 128, 128);
237
238   return ret;
239 }
240
241 static GstStateChangeReturn
242 gst_navigationtest_change_state (GstElement * element,
243     GstStateChange transition)
244 {
245   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
246   GstNavigationtest *navtest = GST_NAVIGATIONTEST (element);
247
248   if (GST_ELEMENT_CLASS (parent_class)->change_state)
249     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
250
251   /* downwards state changes */
252   switch (transition) {
253     case GST_STATE_CHANGE_PAUSED_TO_READY:
254     {
255       g_slist_foreach (navtest->clicks, (GFunc) g_free, NULL);
256       g_slist_free (navtest->clicks);
257       navtest->clicks = NULL;
258       break;
259     }
260     default:
261       break;
262   }
263
264   return ret;
265 }
266
267 static void
268 gst_navigationtest_base_init (gpointer g_class)
269 {
270   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
271
272   gst_element_class_set_details (element_class, &navigationtest_details);
273
274   gst_element_class_add_pad_template (element_class,
275       gst_static_pad_template_get (&gst_navigationtest_sink_template));
276   gst_element_class_add_pad_template (element_class,
277       gst_static_pad_template_get (&gst_navigationtest_src_template));
278 }
279
280 static void
281 gst_navigationtest_class_init (gpointer klass, gpointer class_data)
282 {
283   GObjectClass *gobject_class;
284   GstElementClass *element_class;
285   GstBaseTransformClass *trans_class;
286
287   gobject_class = (GObjectClass *) klass;
288   element_class = (GstElementClass *) klass;
289   trans_class = (GstBaseTransformClass *) klass;
290
291   parent_class = g_type_class_peek_parent (klass);
292
293   element_class->change_state =
294       GST_DEBUG_FUNCPTR (gst_navigationtest_change_state);
295
296   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_navigationtest_set_caps);
297   trans_class->get_unit_size =
298       GST_DEBUG_FUNCPTR (gst_navigationtest_get_unit_size);
299   trans_class->transform = GST_DEBUG_FUNCPTR (gst_navigationtest_transform);
300 }
301
302 static void
303 gst_navigationtest_init (GTypeInstance * instance, gpointer g_class)
304 {
305   GstNavigationtest *navtest = GST_NAVIGATIONTEST (instance);
306   GstBaseTransform *btrans = GST_BASE_TRANSFORM (instance);
307
308   gst_pad_set_event_function (btrans->srcpad,
309       GST_DEBUG_FUNCPTR (gst_navigationtest_handle_src_event));
310
311   navtest->x = -1;
312   navtest->y = -1;
313 }
314
315 GType
316 gst_navigationtest_get_type (void)
317 {
318   static GType navigationtest_type = 0;
319
320   if (!navigationtest_type) {
321     static const GTypeInfo navigationtest_info = {
322       sizeof (GstNavigationtestClass),
323       gst_navigationtest_base_init,
324       NULL,
325       gst_navigationtest_class_init,
326       NULL,
327       NULL,
328       sizeof (GstNavigationtest),
329       0,
330       gst_navigationtest_init,
331     };
332
333     navigationtest_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
334         "GstNavigationtest", &navigationtest_info, 0);
335   }
336   return navigationtest_type;
337 }
338
339 static gboolean
340 plugin_init (GstPlugin * plugin)
341 {
342   GST_DEBUG_CATEGORY_INIT (navigationtest_debug, "navigationtest", 0,
343       "navigationtest");
344
345   return gst_element_register (plugin, "navigationtest", GST_RANK_NONE,
346       GST_TYPE_NAVIGATIONTEST);
347 }
348
349 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
350     GST_VERSION_MINOR,
351     "navigationtest",
352     "Template for a video filter",
353     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)