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