1 /* GStreamer unit test for the alpha element
2 *
3 * Copyright (C) 2007 Ravi Kiran K N <ravi.kiran@samsung.com>
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 #include <gst/check/gstcheck.h>
22 #include <gst/video/video.h>
23
24
25 GstPad *srcpad, *sinkpad;
26
27 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
28 GST_PAD_SINK,
29 GST_PAD_ALWAYS,
30 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("AYUV"))
31 );
32 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
33 GST_PAD_SRC,
34 GST_PAD_ALWAYS,
35 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
36 "ARGB, BGRA, ABGR, RGBA, Y444, xRGB, BGRx, xBGR, "
37 "RGBx, RGB, BGR, Y42B, YUY2, YVYU, UYVY, I420, YV12, Y41B } "))
38 );
39
40
41 typedef enum
42 {
43 FILL_GREEN,
44 FILL_BLUE
45 }
46 FillColor;
47
48 static GstElement *
setup_alpha(void)49 setup_alpha (void)
50 {
51 GstElement *alpha;
52
53 alpha = gst_check_setup_element ("alpha");
54 srcpad = gst_check_setup_src_pad (alpha, &srctemplate);
55 sinkpad = gst_check_setup_sink_pad (alpha, &sinktemplate);
56
57 gst_pad_set_active (srcpad, TRUE);
58 gst_pad_set_active (sinkpad, TRUE);
59
60 return alpha;
61 }
62
63 static void
cleanup_alpha(GstElement * alpha)64 cleanup_alpha (GstElement * alpha)
65 {
66 gst_pad_set_active (srcpad, FALSE);
67 gst_pad_set_active (sinkpad, FALSE);
68 gst_check_teardown_src_pad (alpha);
69 gst_check_teardown_sink_pad (alpha);
70 gst_check_teardown_element (alpha);
71 }
72
73 #define WIDTH 3
74 #define HEIGHT 4
75
76 static GstCaps *
create_caps_rgba32(void)77 create_caps_rgba32 (void)
78 {
79 GstCaps *caps;
80
81 caps = gst_caps_new_simple ("video/x-raw",
82 "width", G_TYPE_INT, WIDTH,
83 "height", G_TYPE_INT, HEIGHT,
84 "framerate", GST_TYPE_FRACTION, 0, 1,
85 "format", G_TYPE_STRING, "RGBA", NULL);
86
87 return caps;
88 }
89
90 #if G_BYTE_ORDER == G_BIG_ENDIAN
91 #define RGBA(r,g,b,a) (guint32)(((r&0xff)<<24)|((g&0xff)<<16)|((b&0xff)<<8)|(a&0xff))
92 #else
93 #define RGBA(r,g,b,a) (guint32)(((a&0xff)<<24)|((b&0xff)<<16)|((g&0xff)<<8)|(r&0xff))
94 #endif
95 #define RGB(r,g,b) RGBA(r,g,b,0xff)
96
97 static GstBuffer *
create_buffer_rgba32(FillColor color)98 create_buffer_rgba32 (FillColor color)
99 {
100 guint8 rgba32_img[HEIGHT * WIDTH * 4];
101 guint32 *rgba32 = (guint32 *) rgba32_img;
102
103 GstBuffer *buf;
104 GstMapInfo map;
105 guint32 rgba_col;
106 int i;
107
108 if (color == FILL_GREEN)
109 rgba_col = RGB (0, 255, 0); /* GREEN */
110 else
111 rgba_col = RGB (0, 0, 255); /* BLUE */
112
113 for (i = 0; i < HEIGHT * WIDTH; i++)
114 rgba32[i] = rgba_col;
115
116 buf = gst_buffer_new_and_alloc (HEIGHT * WIDTH * 4);
117 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
118 fail_unless_equals_int (map.size, sizeof (rgba32_img));
119 memcpy (map.data, rgba32_img, sizeof (rgba32_img));
120
121 gst_buffer_unmap (buf, &map);
122
123 return buf;
124 }
125
126
GST_START_TEST(test_chromakeying)127 GST_START_TEST (test_chromakeying)
128 {
129 GstElement *alpha;
130 GstBuffer *inbuffer;
131 GstBuffer *outbuffer;
132 GstCaps *incaps;
133 guint8 *ayuv;
134 guint outlength;
135 GstMapInfo map;
136 int i;
137
138 incaps = create_caps_rgba32 ();
139
140 alpha = setup_alpha ();
141
142 g_object_set (alpha, "method", 1, NULL); /* Chroma-keying GREEN */
143
144 fail_unless_equals_int (gst_element_set_state (alpha, GST_STATE_PLAYING),
145 GST_STATE_CHANGE_SUCCESS);
146
147 gst_check_setup_events (srcpad, alpha, incaps, GST_FORMAT_TIME);
148
149 inbuffer = create_buffer_rgba32 (FILL_GREEN);
150 GST_DEBUG ("Created buffer of %" G_GSIZE_FORMAT " bytes",
151 gst_buffer_get_size (inbuffer));
152 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
153
154 fail_unless_equals_int (gst_pad_push (srcpad, inbuffer), GST_FLOW_OK);
155
156 fail_unless (g_list_length (buffers) == 1);
157 outbuffer = (GstBuffer *) buffers->data;
158 fail_if (outbuffer == NULL);
159 fail_unless (GST_IS_BUFFER (outbuffer));
160
161 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
162 outlength = WIDTH * HEIGHT * 4; /* output is AYUV */
163 gst_buffer_map (outbuffer, &map, GST_MAP_READ);
164 fail_unless_equals_int (map.size, outlength);
165
166 ayuv = map.data;
167
168 /* check chroma keying GREEN */
169 for (i = 0; i < HEIGHT * WIDTH; i += 4)
170 fail_unless_equals_int (ayuv[i], 0x00);
171
172 gst_buffer_unmap (outbuffer, &map);
173
174 buffers = g_list_remove (buffers, outbuffer);
175 gst_buffer_unref (outbuffer);
176
177 fail_unless_equals_int (gst_element_set_state (alpha, GST_STATE_NULL),
178 GST_STATE_CHANGE_SUCCESS);
179
180 /* cleanup */
181 cleanup_alpha (alpha);
182 ASSERT_CAPS_REFCOUNT (incaps, "incaps", 1);
183 gst_caps_unref (incaps);
184
185 }
186
187 GST_END_TEST;
188
189
190
GST_START_TEST(test_alpha)191 GST_START_TEST (test_alpha)
192 {
193 GstElement *alpha;
194 GstBuffer *inbuffer;
195 GstBuffer *outbuffer;
196 GstCaps *incaps;
197 guint8 *ayuv;
198 guint outlength;
199 GstMapInfo map;
200 int i;
201
202 incaps = create_caps_rgba32 ();
203
204 alpha = setup_alpha ();
205
206 g_object_set (alpha, "alpha", 0.5, NULL); /* Alpha value 0.5 */
207
208 fail_unless_equals_int (gst_element_set_state (alpha, GST_STATE_PLAYING),
209 GST_STATE_CHANGE_SUCCESS);
210
211 gst_check_setup_events (srcpad, alpha, incaps, GST_FORMAT_TIME);
212
213 inbuffer = create_buffer_rgba32 (FILL_BLUE);
214 GST_DEBUG ("Created buffer of %" G_GSIZE_FORMAT " bytes",
215 gst_buffer_get_size (inbuffer));
216 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
217
218 /* pushing gives away reference */
219 GST_DEBUG ("push it");
220 fail_unless_equals_int (gst_pad_push (srcpad, inbuffer), GST_FLOW_OK);
221 GST_DEBUG ("pushed it");
222
223 /* ... and puts a new buffer on the global list */
224 fail_unless (g_list_length (buffers) == 1);
225 outbuffer = (GstBuffer *) buffers->data;
226 fail_if (outbuffer == NULL);
227 fail_unless (GST_IS_BUFFER (outbuffer));
228
229 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
230 outlength = WIDTH * HEIGHT * 4; /* output is AYUV */
231 gst_buffer_map (outbuffer, &map, GST_MAP_READ);
232 fail_unless_equals_int (map.size, outlength);
233
234 ayuv = map.data;
235
236 for (i = 0; i < HEIGHT * WIDTH; i += 4)
237 fail_unless_equals_int (ayuv[i], 0x7F);
238
239 gst_buffer_unmap (outbuffer, &map);
240
241 buffers = g_list_remove (buffers, outbuffer);
242 gst_buffer_unref (outbuffer);
243
244 fail_unless_equals_int (gst_element_set_state (alpha, GST_STATE_NULL),
245 GST_STATE_CHANGE_SUCCESS);
246
247 /* cleanup */
248 GST_DEBUG ("cleanup alpha");
249 cleanup_alpha (alpha);
250 GST_DEBUG ("cleanup, unref incaps");
251 ASSERT_CAPS_REFCOUNT (incaps, "incaps", 1);
252 gst_caps_unref (incaps);
253
254 }
255
256 GST_END_TEST;
257
258
259 static Suite *
alpha_suite(void)260 alpha_suite (void)
261 {
262 Suite *s = suite_create ("alpha");
263 TCase *tc_chain = tcase_create ("general");
264
265 suite_add_tcase (s, tc_chain);
266 tcase_add_test (tc_chain, test_alpha);
267 tcase_add_test (tc_chain, test_chromakeying);
268
269 return s;
270 }
271
272 GST_CHECK_MAIN (alpha);
273