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_MAKE ("I420"))
45 static GstStaticPadTemplate gst_navigationtest_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
52 #define gst_navigationtest_parent_class parent_class
53 G_DEFINE_TYPE (GstNavigationtest, gst_navigationtest, GST_TYPE_VIDEO_FILTER);
56 gst_navigationtest_src_event (GstBaseTransform * trans, GstEvent * event)
58 GstNavigationtest *navtest;
61 navtest = GST_NAVIGATIONTEST (trans);
63 switch (GST_EVENT_TYPE (event)) {
64 case GST_EVENT_NAVIGATION:
66 const GstStructure *s = gst_event_get_structure (event);
69 fps_n = GST_VIDEO_INFO_FPS_N (&navtest->info);
70 fps_d = GST_VIDEO_INFO_FPS_D (&navtest->info);
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);
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;
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);
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;
97 navtest->clicks = g_slist_prepend (navtest->clicks, click);
104 return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
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)
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))
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))
119 gst_navigationtest_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
122 GstNavigationtest *navtest;
123 GstStructure *structure;
124 gboolean ret = FALSE;
127 navtest = GST_NAVIGATIONTEST (btrans);
129 structure = gst_caps_get_structure (caps, 0);
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);
135 GST_DEBUG_OBJECT (navtest,
136 "our frame size is %" G_GSIZE_FORMAT " bytes (%dx%d)", *size, width,
144 gst_navigationtest_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
147 GstNavigationtest *navtest = GST_NAVIGATIONTEST (btrans);
150 if (!gst_video_info_from_caps (&info, incaps))
153 navtest->info = info;
160 GST_ERROR_OBJECT (navtest, "invalid caps");
166 draw_box_planar411 (GstVideoFrame * frame, int x, int y,
167 guint8 colory, guint8 coloru, guint8 colorv)
174 width = GST_VIDEO_FRAME_WIDTH (frame);
175 height = GST_VIDEO_FRAME_HEIGHT (frame);
177 if (x < 0 || y < 0 || x >= width || y >= height)
181 x2 = MIN (x + 5, width);
183 y2 = MIN (y + 5, height);
185 d = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
186 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
188 for (y = y1; y < y2; y++) {
189 for (x = x1; x < x2; x++) {
190 d[y * stride + x] = colory;
194 d = GST_VIDEO_FRAME_PLANE_DATA (frame, 1);
195 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 1);
201 for (y = y1; y < y2; y++) {
202 for (x = x1; x < x2; x++) {
203 d[y * stride + x] = coloru;
207 d = GST_VIDEO_FRAME_PLANE_DATA (frame, 2);
208 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 2);
210 for (y = y1; y < y2; y++) {
211 for (x = x1; x < x2; x++) {
212 d[y * stride + x] = colorv;
218 gst_navigationtest_transform (GstBaseTransform * trans, GstBuffer * in,
221 GstNavigationtest *navtest = GST_NAVIGATIONTEST (trans);
223 GstVideoFrame in_frame, out_frame;
225 if (!gst_video_frame_map (&in_frame, &navtest->info, in, GST_MAP_READ))
228 if (!gst_video_frame_map (&out_frame, &navtest->info, out, GST_MAP_WRITE))
231 gst_video_frame_copy (&out_frame, &in_frame);
233 walk = navtest->clicks;
235 ButtonClick *click = walk->data;
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);
245 draw_box_planar411 (&out_frame,
246 rint (navtest->x), rint (navtest->y), 0, 128, 128);
248 gst_video_frame_unmap (&out_frame);
249 gst_video_frame_unmap (&in_frame);
256 GST_ERROR_OBJECT (navtest, "received invalid input buffer");
261 GST_ERROR_OBJECT (navtest, "received invalid output buffer");
262 gst_video_frame_unmap (&in_frame);
267 static GstStateChangeReturn
268 gst_navigationtest_change_state (GstElement * element,
269 GstStateChange transition)
271 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
272 GstNavigationtest *navtest = GST_NAVIGATIONTEST (element);
274 if (GST_ELEMENT_CLASS (parent_class)->change_state)
275 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
277 /* downwards state changes */
278 switch (transition) {
279 case GST_STATE_CHANGE_PAUSED_TO_READY:
281 g_slist_foreach (navtest->clicks, (GFunc) g_free, NULL);
282 g_slist_free (navtest->clicks);
283 navtest->clicks = NULL;
294 gst_navigationtest_class_init (GstNavigationtestClass * klass)
296 GstElementClass *element_class;
297 GstBaseTransformClass *trans_class;
299 element_class = (GstElementClass *) klass;
300 trans_class = (GstBaseTransformClass *) klass;
302 parent_class = g_type_class_peek_parent (klass);
304 element_class->change_state =
305 GST_DEBUG_FUNCPTR (gst_navigationtest_change_state);
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>");
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));
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);
325 gst_navigationtest_init (GstNavigationtest * navtest)
332 plugin_init (GstPlugin * plugin)
334 GST_DEBUG_CATEGORY_INIT (navigationtest_debug, "navigationtest", 0,
337 return gst_element_register (plugin, "navigationtest", GST_RANK_NONE,
338 GST_TYPE_NAVIGATIONTEST);
341 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
344 "Template for a video filter",
345 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)