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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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
gst_navigationtest_src_event(GstBaseTransform * trans,GstEvent * event)56 gst_navigationtest_src_event (GstBaseTransform * trans, GstEvent * event)
57 {
58   GstVideoInfo *info;
59   GstNavigationtest *navtest;
60   const gchar *type;
61 
62   navtest = GST_NAVIGATIONTEST (trans);
63 
64   info = &GST_VIDEO_FILTER (trans)->in_info;
65 
66   switch (GST_EVENT_TYPE (event)) {
67     case GST_EVENT_NAVIGATION:
68     {
69       const GstStructure *s = gst_event_get_structure (event);
70       gint fps_n, fps_d;
71 
72       fps_n = GST_VIDEO_INFO_FPS_N (info);
73       fps_d = GST_VIDEO_INFO_FPS_D (info);
74 
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);
81 
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;
85         /* green */
86         click->cy = 150;
87         click->cu = 46;
88         click->cv = 21;
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);
92 
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;
96         /* red */
97         click->cy = 76;
98         click->cu = 85;
99         click->cv = 255;
100         navtest->clicks = g_slist_prepend (navtest->clicks, click);
101       }
102       break;
103     }
104     default:
105       break;
106   }
107   return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
108 }
109 
110 /* Useful macros */
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)
114 
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))
118 
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))
120 
121 static void
draw_box_planar411(GstVideoFrame * frame,int x,int y,guint8 colory,guint8 coloru,guint8 colorv)122 draw_box_planar411 (GstVideoFrame * frame, int x, int y,
123     guint8 colory, guint8 coloru, guint8 colorv)
124 {
125   gint width, height;
126   int x1, x2, y1, y2;
127   guint8 *d;
128   gint stride;
129 
130   width = GST_VIDEO_FRAME_WIDTH (frame);
131   height = GST_VIDEO_FRAME_HEIGHT (frame);
132 
133   if (x < 0 || y < 0 || x >= width || y >= height)
134     return;
135 
136   x1 = MAX (x - 5, 0);
137   x2 = MIN (x + 5, width);
138   y1 = MAX (y - 5, 0);
139   y2 = MIN (y + 5, height);
140 
141   d = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
142   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
143 
144   for (y = y1; y < y2; y++) {
145     for (x = x1; x < x2; x++) {
146       d[y * stride + x] = colory;
147     }
148   }
149 
150   d = GST_VIDEO_FRAME_PLANE_DATA (frame, 1);
151   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 1);
152 
153   x1 /= 2;
154   x2 /= 2;
155   y1 /= 2;
156   y2 /= 2;
157   for (y = y1; y < y2; y++) {
158     for (x = x1; x < x2; x++) {
159       d[y * stride + x] = coloru;
160     }
161   }
162 
163   d = GST_VIDEO_FRAME_PLANE_DATA (frame, 2);
164   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 2);
165 
166   for (y = y1; y < y2; y++) {
167     for (x = x1; x < x2; x++) {
168       d[y * stride + x] = colorv;
169     }
170   }
171 }
172 
173 static GstFlowReturn
gst_navigationtest_transform_frame(GstVideoFilter * filter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)174 gst_navigationtest_transform_frame (GstVideoFilter * filter,
175     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
176 {
177   GstNavigationtest *navtest = GST_NAVIGATIONTEST (filter);
178   GSList *walk;
179 
180   gst_video_frame_copy (out_frame, in_frame);
181 
182   walk = navtest->clicks;
183   while (walk) {
184     ButtonClick *click = walk->data;
185 
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);
191       g_free (click);
192     }
193   }
194   draw_box_planar411 (out_frame,
195       rint (navtest->x), rint (navtest->y), 0, 128, 128);
196 
197   return GST_FLOW_OK;
198 }
199 
200 static GstStateChangeReturn
gst_navigationtest_change_state(GstElement * element,GstStateChange transition)201 gst_navigationtest_change_state (GstElement * element,
202     GstStateChange transition)
203 {
204   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
205   GstNavigationtest *navtest = GST_NAVIGATIONTEST (element);
206 
207   if (GST_ELEMENT_CLASS (parent_class)->change_state)
208     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
209 
210   /* downwards state changes */
211   switch (transition) {
212     case GST_STATE_CHANGE_PAUSED_TO_READY:
213     {
214       g_slist_foreach (navtest->clicks, (GFunc) g_free, NULL);
215       g_slist_free (navtest->clicks);
216       navtest->clicks = NULL;
217       break;
218     }
219     default:
220       break;
221   }
222 
223   return ret;
224 }
225 
226 static void
gst_navigationtest_class_init(GstNavigationtestClass * klass)227 gst_navigationtest_class_init (GstNavigationtestClass * klass)
228 {
229   GstElementClass *element_class;
230   GstBaseTransformClass *trans_class;
231   GstVideoFilterClass *vfilter_class;
232 
233   element_class = (GstElementClass *) klass;
234   trans_class = (GstBaseTransformClass *) klass;
235   vfilter_class = (GstVideoFilterClass *) klass;
236 
237   element_class->change_state =
238       GST_DEBUG_FUNCPTR (gst_navigationtest_change_state);
239 
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>");
244 
245   gst_element_class_add_static_pad_template (element_class,
246       &gst_navigationtest_sink_template);
247   gst_element_class_add_static_pad_template (element_class,
248       &gst_navigationtest_src_template);
249 
250   trans_class->src_event = GST_DEBUG_FUNCPTR (gst_navigationtest_src_event);
251 
252   vfilter_class->transform_frame =
253       GST_DEBUG_FUNCPTR (gst_navigationtest_transform_frame);
254 }
255 
256 static void
gst_navigationtest_init(GstNavigationtest * navtest)257 gst_navigationtest_init (GstNavigationtest * navtest)
258 {
259   navtest->x = -1;
260   navtest->y = -1;
261 }
262 
263 static gboolean
plugin_init(GstPlugin * plugin)264 plugin_init (GstPlugin * plugin)
265 {
266   GST_DEBUG_CATEGORY_INIT (navigationtest_debug, "navigationtest", 0,
267       "navigationtest");
268 
269   return gst_element_register (plugin, "navigationtest", GST_RANK_NONE,
270       GST_TYPE_NAVIGATIONTEST);
271 }
272 
273 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
274     GST_VERSION_MINOR,
275     navigationtest,
276     "Template for a video filter",
277     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
278