2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
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.
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.
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.
25 #include "gstnavigationtest.h"
29 #include <gst/video/video.h>
32 #define rint(x) (floor((x)+0.5))
35 GST_DEBUG_CATEGORY_STATIC (navigationtest_debug);
36 #define GST_CAT_DEFAULT navigationtest_debug
38 static GstStaticPadTemplate gst_navigationtest_src_template =
39 GST_STATIC_PAD_TEMPLATE ("src",
42 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
45 static GstStaticPadTemplate gst_navigationtest_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
52 static GstVideoFilterClass *parent_class = NULL;
55 gst_navigationtest_handle_src_event (GstPad * pad, GstEvent * event)
57 GstNavigationtest *navtest;
60 navtest = GST_NAVIGATIONTEST (GST_PAD_PARENT (pad));
62 switch (GST_EVENT_TYPE (event)) {
63 case GST_EVENT_NAVIGATION:
65 const GstStructure *s = gst_event_get_structure (event);
68 fps_n = gst_value_get_fraction_numerator ((&navtest->framerate));
69 fps_d = gst_value_get_fraction_denominator ((&navtest->framerate));
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);
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;
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);
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;
96 navtest->clicks = g_slist_prepend (navtest->clicks, click);
103 return gst_pad_event_default (pad, event);
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)
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))
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))
118 gst_navigationtest_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
121 GstNavigationtest *navtest;
122 GstStructure *structure;
123 gboolean ret = FALSE;
126 navtest = GST_NAVIGATIONTEST (btrans);
128 structure = gst_caps_get_structure (caps, 0);
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);
134 GST_DEBUG_OBJECT (navtest, "our frame size is %d bytes (%dx%d)", *size,
142 gst_navigationtest_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
145 GstNavigationtest *navtest = GST_NAVIGATIONTEST (btrans);
146 gboolean ret = FALSE;
147 GstStructure *structure;
149 structure = gst_caps_get_structure (incaps, 0);
151 if (gst_structure_get_int (structure, "width", &navtest->width) &&
152 gst_structure_get_int (structure, "height", &navtest->height)) {
153 const GValue *framerate;
155 framerate = gst_structure_get_value (structure, "framerate");
156 if (framerate && GST_VALUE_HOLDS_FRACTION (framerate)) {
157 g_value_copy (framerate, &navtest->framerate);
166 draw_box_planar411 (guint8 * dest, int width, int height, int x, int y,
167 guint8 colory, guint8 coloru, guint8 colorv)
172 if (x < 0 || y < 0 || x >= width || y >= height)
176 x2 = MIN (x + 5, width);
178 y2 = MIN (y + 5, height);
180 for (y = y1; y < y2; y++) {
181 for (x = x1; x < x2; x++) {
182 ((guint8 *) d)[y * GST_VIDEO_I420_Y_ROWSTRIDE (width) + x] = colory;
186 d = dest + GST_VIDEO_I420_U_OFFSET (width, height);
191 for (y = y1; y < y2; y++) {
192 for (x = x1; x < x2; x++) {
193 ((guint8 *) d)[y * GST_VIDEO_I420_U_ROWSTRIDE (width) + x] = coloru;
197 d = dest + GST_VIDEO_I420_V_OFFSET (width, height);
198 for (y = y1; y < y2; y++) {
199 for (x = x1; x < x2; x++) {
200 ((guint8 *) d)[y * GST_VIDEO_I420_V_ROWSTRIDE (width) + x] = colorv;
206 gst_navigationtest_transform (GstBaseTransform * trans, GstBuffer * in,
209 GstNavigationtest *navtest = GST_NAVIGATIONTEST (trans);
211 GstFlowReturn ret = GST_FLOW_OK;
213 /* do something interesting here. This simply copies the source
214 * to the destination. */
215 gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
217 memcpy (GST_BUFFER_DATA (out), GST_BUFFER_DATA (in),
218 MIN (GST_BUFFER_SIZE (in), GST_BUFFER_SIZE (out)));
220 walk = navtest->clicks;
222 ButtonClick *click = walk->data;
224 walk = g_slist_next (walk);
225 draw_box_planar411 (GST_BUFFER_DATA (out), navtest->width, navtest->height,
226 rint (click->x), rint (click->y), click->cy, click->cu, click->cv);
227 if (--click->images_left < 1) {
228 navtest->clicks = g_slist_remove (navtest->clicks, click);
232 draw_box_planar411 (GST_BUFFER_DATA (out), navtest->width, navtest->height,
233 rint (navtest->x), rint (navtest->y), 0, 128, 128);
238 static GstStateChangeReturn
239 gst_navigationtest_change_state (GstElement * element,
240 GstStateChange transition)
242 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
243 GstNavigationtest *navtest = GST_NAVIGATIONTEST (element);
245 if (GST_ELEMENT_CLASS (parent_class)->change_state)
246 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
248 /* downwards state changes */
249 switch (transition) {
250 case GST_STATE_CHANGE_PAUSED_TO_READY:
252 g_slist_foreach (navtest->clicks, (GFunc) g_free, NULL);
253 g_slist_free (navtest->clicks);
254 navtest->clicks = NULL;
265 gst_navigationtest_base_init (gpointer g_class)
267 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
269 gst_element_class_set_details_simple (element_class, "Video navigation test",
270 "Filter/Effect/Video",
271 "Handle navigation events showing a black square following mouse pointer",
272 "David Schleef <ds@schleef.org>");
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));
281 gst_navigationtest_class_init (gpointer klass, gpointer class_data)
283 GstElementClass *element_class;
284 GstBaseTransformClass *trans_class;
286 element_class = (GstElementClass *) klass;
287 trans_class = (GstBaseTransformClass *) klass;
289 parent_class = g_type_class_peek_parent (klass);
291 element_class->change_state =
292 GST_DEBUG_FUNCPTR (gst_navigationtest_change_state);
294 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_navigationtest_set_caps);
295 trans_class->get_unit_size =
296 GST_DEBUG_FUNCPTR (gst_navigationtest_get_unit_size);
297 trans_class->transform = GST_DEBUG_FUNCPTR (gst_navigationtest_transform);
301 gst_navigationtest_init (GTypeInstance * instance, gpointer g_class)
303 GstNavigationtest *navtest = GST_NAVIGATIONTEST (instance);
304 GstBaseTransform *btrans = GST_BASE_TRANSFORM (instance);
306 gst_pad_set_event_function (btrans->srcpad,
307 GST_DEBUG_FUNCPTR (gst_navigationtest_handle_src_event));
311 g_value_init (&navtest->framerate, GST_TYPE_FRACTION);
315 gst_navigationtest_get_type (void)
317 static GType navigationtest_type = 0;
319 if (!navigationtest_type) {
320 static const GTypeInfo navigationtest_info = {
321 sizeof (GstNavigationtestClass),
322 gst_navigationtest_base_init,
324 gst_navigationtest_class_init,
327 sizeof (GstNavigationtest),
329 gst_navigationtest_init,
332 navigationtest_type = g_type_register_static (GST_TYPE_VIDEO_FILTER,
333 "GstNavigationtest", &navigationtest_info, 0);
335 return navigationtest_type;
339 plugin_init (GstPlugin * plugin)
341 GST_DEBUG_CATEGORY_INIT (navigationtest_debug, "navigationtest", 0,
344 return gst_element_register (plugin, "navigationtest", GST_RANK_NONE,
345 GST_TYPE_NAVIGATIONTEST);
348 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
351 "Template for a video filter",
352 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)