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