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