220947a88178b8c23ea405b574a3798b06ca7f68
[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 /*
22  * This file was (probably) generated from gstnavigationtest.c,
23  * gstnavigationtest.c,v 1.7 2003/11/08 02:48:59 dschleef Exp 
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <gstnavigationtest.h>
31 #include <string.h>
32 #include <math.h>
33
34 typedef struct
35 {
36   double x;
37   double y;
38   gint images_left;
39   guint8 cy, cu, cv;
40 } ButtonClick;
41
42 static void gst_navigationtest_base_init (gpointer g_class);
43 static void gst_navigationtest_class_init (gpointer g_class,
44     gpointer class_data);
45 static void gst_navigationtest_init (GTypeInstance * instance,
46     gpointer g_class);
47
48 static gboolean gst_navigationtest_handle_src_event (GstPad * pad,
49     GstEvent * event);
50
51 static GstStateChangeReturn
52 gst_navigationtest_change_state (GstElement * element,
53     GstStateChange transition);
54
55 static void gst_navigationtest_planar411 (GstVideofilter * videofilter,
56     void *dest, void *src);
57 static void gst_navigationtest_setup (GstVideofilter * videofilter);
58
59 static GstVideofilterClass *parent_class;       /* NULL */
60
61 GType
62 gst_navigationtest_get_type (void)
63 {
64   static GType navigationtest_type = 0;
65
66   if (!navigationtest_type) {
67     static const GTypeInfo navigationtest_info = {
68       sizeof (GstNavigationtestClass),
69       gst_navigationtest_base_init,
70       NULL,
71       gst_navigationtest_class_init,
72       NULL,
73       NULL,
74       sizeof (GstNavigationtest),
75       0,
76       gst_navigationtest_init,
77     };
78
79     navigationtest_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
80         "GstNavigationtest", &navigationtest_info, 0);
81   }
82   return navigationtest_type;
83 }
84
85 static GstVideofilterFormat gst_navigationtest_formats[] = {
86   {"I420", 12, gst_navigationtest_planar411,},
87 };
88
89
90 static void
91 gst_navigationtest_base_init (gpointer g_class)
92 {
93   static GstElementDetails navigationtest_details =
94       GST_ELEMENT_DETAILS ("Video Filter Template",
95       "Filter/Video",
96       "Template for a video filter",
97       "David Schleef <ds@schleef.org>");
98   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
99   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
100   int i;
101
102   gst_element_class_set_details (element_class, &navigationtest_details);
103
104   for (i = 0; i < G_N_ELEMENTS (gst_navigationtest_formats); i++) {
105     gst_videofilter_class_add_format (videofilter_class,
106         gst_navigationtest_formats + i);
107   }
108
109   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
110 }
111
112 static void
113 gst_navigationtest_class_init (gpointer g_class, gpointer class_data)
114 {
115   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
116   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
117
118   parent_class = g_type_class_peek_parent (g_class);
119
120   element_class->change_state = gst_navigationtest_change_state;
121
122   videofilter_class->setup = gst_navigationtest_setup;
123 }
124
125 static void
126 gst_navigationtest_init (GTypeInstance * instance, gpointer g_class)
127 {
128   GstNavigationtest *navtest = GST_NAVIGATIONTEST (instance);
129   GstVideofilter *videofilter = GST_VIDEOFILTER (navtest);
130
131   gst_pad_set_event_function (videofilter->srcpad,
132       GST_DEBUG_FUNCPTR (gst_navigationtest_handle_src_event));
133
134   navtest->x = -1;
135   navtest->y = -1;
136 }
137
138 static gboolean
139 gst_navigationtest_handle_src_event (GstPad * pad, GstEvent * event)
140 {
141   GstNavigationtest *navtest;
142   const gchar *type;
143
144   navtest = GST_NAVIGATIONTEST (GST_PAD_PARENT (pad));
145
146   switch (GST_EVENT_TYPE (event)) {
147     case GST_EVENT_NAVIGATION:
148     {
149       const GstStructure *s = gst_event_get_structure (event);
150       gint fps_n, fps_d;
151
152       fps_n = gst_value_get_fraction_numerator (
153           (&GST_VIDEOFILTER (navtest)->framerate));
154       fps_d = gst_value_get_fraction_denominator (
155           (&GST_VIDEOFILTER (navtest)->framerate));
156
157       type = gst_structure_get_string (s, "event");
158       if (g_str_equal (type, "mouse-move")) {
159         gst_structure_get_double (s, "pointer_x", &navtest->x);
160         gst_structure_get_double (s, "pointer_y", &navtest->y);
161       } else if (g_str_equal (type, "mouse-button-press")) {
162         ButtonClick *click = g_new (ButtonClick, 1);
163
164         gst_structure_get_double (s, "pointer_x", &click->x);
165         gst_structure_get_double (s, "pointer_y", &click->y);
166         click->images_left = (fps_n + fps_d - 1) / fps_d;
167         /* green */
168         click->cy = 150;
169         click->cu = 46;
170         click->cv = 21;
171         navtest->clicks = g_slist_prepend (navtest->clicks, click);
172       } else if (g_str_equal (type, "mouse-button-release")) {
173         ButtonClick *click = g_new (ButtonClick, 1);
174
175         gst_structure_get_double (s, "pointer_x", &click->x);
176         gst_structure_get_double (s, "pointer_y", &click->y);
177         click->images_left = (fps_n + fps_d - 1) / fps_d;
178         /* red */
179         click->cy = 76;
180         click->cu = 85;
181         click->cv = 255;
182         navtest->clicks = g_slist_prepend (navtest->clicks, click);
183       }
184       break;
185     }
186     default:
187       break;
188   }
189   return gst_pad_event_default (pad, event);
190 }
191
192 static void
193 gst_navigationtest_setup (GstVideofilter * videofilter)
194 {
195   GstNavigationtest *navigationtest;
196
197   g_return_if_fail (GST_IS_NAVIGATIONTEST (videofilter));
198   navigationtest = GST_NAVIGATIONTEST (videofilter);
199
200   /* if any setup needs to be done, do it here */
201
202 }
203
204 static void
205 draw_box_planar411 (guint8 * dest, int width, int height, int x, int y,
206     guint8 colory, guint8 coloru, guint8 colorv)
207 {
208   int x1, x2, y1, y2;
209
210   if (x < 0 || y < 0 || x >= width || y >= height)
211     return;
212
213   x1 = MAX (x - 5, 0);
214   x2 = MIN (x + 5, width);
215   y1 = MAX (y - 5, 0);
216   y2 = MIN (y + 5, height);
217
218   for (y = y1; y < y2; y++) {
219     for (x = x1; x < x2; x++) {
220       ((guint8 *) dest)[y * width + x] = colory;
221     }
222   }
223
224   dest += height * width;
225   width /= 2;
226   height /= 2;
227   x1 /= 2;
228   x2 /= 2;
229   y1 /= 2;
230   y2 /= 2;
231   for (y = y1; y < y2; y++) {
232     for (x = x1; x < x2; x++) {
233       ((guint8 *) dest)[y * width + x] = coloru;
234     }
235   }
236
237   dest += height * width;
238   for (y = y1; y < y2; y++) {
239     for (x = x1; x < x2; x++) {
240       ((guint8 *) dest)[y * width + x] = colorv;
241     }
242   }
243 }
244
245 static void
246 gst_navigationtest_planar411 (GstVideofilter * videofilter,
247     void *dest, void *src)
248 {
249   GstNavigationtest *navtest = (GstNavigationtest *) videofilter;
250   gint width, height;
251   GSList *walk;
252
253   g_return_if_fail (GST_IS_NAVIGATIONTEST (videofilter));
254
255   width = gst_videofilter_get_input_width (videofilter);
256   height = gst_videofilter_get_input_height (videofilter);
257
258   /* do something interesting here.  This simply copies the source
259    * to the destination. */
260   memcpy (dest, src, width * height + (width / 2) * (height / 2) * 2);
261
262   walk = navtest->clicks;
263   while (walk) {
264     ButtonClick *click = walk->data;
265
266     walk = g_slist_next (walk);
267     draw_box_planar411 (dest, width, height, rint (click->x),
268         rint (click->y), click->cy, click->cu, click->cv);
269     if (--click->images_left < 1) {
270       navtest->clicks = g_slist_remove (navtest->clicks, click);
271       g_free (click);
272     }
273   }
274   draw_box_planar411 (dest, width, height, rint (navtest->x),
275       rint (navtest->y), 0, 128, 128);
276 }
277
278 static GstStateChangeReturn
279 gst_navigationtest_change_state (GstElement * element,
280     GstStateChange transition)
281 {
282   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
283   GstNavigationtest *navtest = GST_NAVIGATIONTEST (element);
284
285   /* upwards state changes */
286   switch (transition) {
287     default:
288       break;
289   }
290
291   if (GST_ELEMENT_CLASS (parent_class)->change_state)
292     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
293
294   /* downwards state changes */
295   switch (transition) {
296     case GST_STATE_CHANGE_PAUSED_TO_READY:
297     {
298       g_slist_foreach (navtest->clicks, (GFunc) g_free, NULL);
299       g_slist_free (navtest->clicks);
300       navtest->clicks = NULL;
301       break;
302     }
303     default:
304       break;
305   }
306
307   return ret;
308 }
309
310 static gboolean
311 plugin_init (GstPlugin * plugin)
312 {
313   return gst_element_register (plugin, "navigationtest", GST_RANK_NONE,
314       GST_TYPE_NAVIGATIONTEST);
315 }
316
317 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
318     GST_VERSION_MINOR,
319     "navigationtest",
320     "Template for a video filter",
321     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)