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)
59 GstNavigationtest *navtest;
62 navtest = GST_NAVIGATIONTEST (trans);
64 info = &GST_VIDEO_FILTER (trans)->in_info;
66 switch (GST_EVENT_TYPE (event)) {
67 case GST_EVENT_NAVIGATION:
69 const GstStructure *s = gst_event_get_structure (event);
72 fps_n = GST_VIDEO_INFO_FPS_N (info);
73 fps_d = GST_VIDEO_INFO_FPS_D (info);
75 type = gst_structure_get_string (s, "event");
76 if (g_str_equal (type, "mouse-move")) {
77 gst_structure_get_double (s, "pointer_x", &navtest->x);
78 gst_structure_get_double (s, "pointer_y", &navtest->y);
79 } else if (g_str_equal (type, "mouse-button-press")) {
80 ButtonClick *click = g_new (ButtonClick, 1);
82 gst_structure_get_double (s, "pointer_x", &click->x);
83 gst_structure_get_double (s, "pointer_y", &click->y);
84 click->images_left = (fps_n + fps_d - 1) / fps_d;
89 navtest->clicks = g_slist_prepend (navtest->clicks, click);
90 } else if (g_str_equal (type, "mouse-button-release")) {
91 ButtonClick *click = g_new (ButtonClick, 1);
93 gst_structure_get_double (s, "pointer_x", &click->x);
94 gst_structure_get_double (s, "pointer_y", &click->y);
95 click->images_left = (fps_n + fps_d - 1) / fps_d;
100 navtest->clicks = g_slist_prepend (navtest->clicks, click);
107 return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
111 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
112 #define GST_VIDEO_I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
113 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
115 #define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
116 #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)))
117 #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))
119 #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 draw_box_planar411 (GstVideoFrame * frame, int x, int y,
123 guint8 colory, guint8 coloru, guint8 colorv)
130 width = GST_VIDEO_FRAME_WIDTH (frame);
131 height = GST_VIDEO_FRAME_HEIGHT (frame);
133 if (x < 0 || y < 0 || x >= width || y >= height)
137 x2 = MIN (x + 5, width);
139 y2 = MIN (y + 5, height);
141 d = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
142 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
144 for (y = y1; y < y2; y++) {
145 for (x = x1; x < x2; x++) {
146 d[y * stride + x] = colory;
150 d = GST_VIDEO_FRAME_PLANE_DATA (frame, 1);
151 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 1);
157 for (y = y1; y < y2; y++) {
158 for (x = x1; x < x2; x++) {
159 d[y * stride + x] = coloru;
163 d = GST_VIDEO_FRAME_PLANE_DATA (frame, 2);
164 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 2);
166 for (y = y1; y < y2; y++) {
167 for (x = x1; x < x2; x++) {
168 d[y * stride + x] = colorv;
174 gst_navigationtest_transform_frame (GstVideoFilter * filter,
175 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
177 GstNavigationtest *navtest = GST_NAVIGATIONTEST (filter);
180 gst_video_frame_copy (out_frame, in_frame);
182 walk = navtest->clicks;
184 ButtonClick *click = walk->data;
186 walk = g_slist_next (walk);
187 draw_box_planar411 (out_frame,
188 rint (click->x), rint (click->y), click->cy, click->cu, click->cv);
189 if (--click->images_left < 1) {
190 navtest->clicks = g_slist_remove (navtest->clicks, click);
194 draw_box_planar411 (out_frame,
195 rint (navtest->x), rint (navtest->y), 0, 128, 128);
200 static GstStateChangeReturn
201 gst_navigationtest_change_state (GstElement * element,
202 GstStateChange transition)
204 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
205 GstNavigationtest *navtest = GST_NAVIGATIONTEST (element);
207 if (GST_ELEMENT_CLASS (parent_class)->change_state)
208 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
210 /* downwards state changes */
211 switch (transition) {
212 case GST_STATE_CHANGE_PAUSED_TO_READY:
214 g_slist_foreach (navtest->clicks, (GFunc) g_free, NULL);
215 g_slist_free (navtest->clicks);
216 navtest->clicks = NULL;
227 gst_navigationtest_class_init (GstNavigationtestClass * klass)
229 GstElementClass *element_class;
230 GstBaseTransformClass *trans_class;
231 GstVideoFilterClass *vfilter_class;
233 element_class = (GstElementClass *) klass;
234 trans_class = (GstBaseTransformClass *) klass;
235 vfilter_class = (GstVideoFilterClass *) klass;
237 element_class->change_state =
238 GST_DEBUG_FUNCPTR (gst_navigationtest_change_state);
240 gst_element_class_set_static_metadata (element_class, "Video navigation test",
241 "Filter/Effect/Video",
242 "Handle navigation events showing a black square following mouse pointer",
243 "David Schleef <ds@schleef.org>");
245 gst_element_class_add_pad_template (element_class,
246 gst_static_pad_template_get (&gst_navigationtest_sink_template));
247 gst_element_class_add_pad_template (element_class,
248 gst_static_pad_template_get (&gst_navigationtest_src_template));
250 trans_class->src_event = GST_DEBUG_FUNCPTR (gst_navigationtest_src_event);
252 vfilter_class->transform_frame =
253 GST_DEBUG_FUNCPTR (gst_navigationtest_transform_frame);
257 gst_navigationtest_init (GstNavigationtest * navtest)
264 plugin_init (GstPlugin * plugin)
266 GST_DEBUG_CATEGORY_INIT (navigationtest_debug, "navigationtest", 0,
269 return gst_element_register (plugin, "navigationtest", GST_RANK_NONE,
270 GST_TYPE_NAVIGATIONTEST);
273 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
276 "Template for a video filter",
277 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)