Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / videofilter.c
1 /* GStreamer
2  *
3  * unit test for videofilter elements
4  *
5  * Copyright (C) <2006> Mark Nauwelaerts <manauw@skynet.be>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24 #include <stdarg.h>
25
26 #include <gst/video/video.h>
27 #include <gst/check/gstcheck.h>
28
29 gboolean have_eos = FALSE;
30
31 /* For ease of programming we use globals to keep refs for our floating
32  * src and sink pads we create; otherwise we always have to do get_pad,
33  * get_peer, and then remove references in every test function */
34 GstPad *mysrcpad, *mysinkpad;
35
36 #define VIDEO_CAPS_TEMPLATE_STRING \
37   GST_VIDEO_CAPS_YUV ("I420") ";" \
38   GST_VIDEO_CAPS_YUV ("AYUV") ";" \
39   GST_VIDEO_CAPS_YUV ("YUY2") ";" \
40   GST_VIDEO_CAPS_YUV ("UYVY") ";" \
41   GST_VIDEO_CAPS_YUV ("YVYU") ";" \
42   GST_VIDEO_CAPS_xRGB
43
44 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS (VIDEO_CAPS_TEMPLATE_STRING)
48     );
49 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
50     GST_PAD_SRC,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (VIDEO_CAPS_TEMPLATE_STRING)
53     );
54
55 /* takes over reference for outcaps */
56 static GstElement *
57 setup_filter (const gchar * name, const gchar * prop, va_list var_args)
58 {
59   GstElement *element;
60
61   GST_DEBUG ("setup_element");
62   element = gst_check_setup_element (name);
63   g_object_set_valist (G_OBJECT (element), prop, var_args);
64   mysrcpad = gst_check_setup_src_pad (element, &srctemplate, NULL);
65   gst_pad_set_active (mysrcpad, TRUE);
66   mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate, NULL);
67   gst_pad_set_active (mysinkpad, TRUE);
68
69   return element;
70 }
71
72 static void
73 cleanup_filter (GstElement * filter)
74 {
75   GST_DEBUG ("cleanup_element");
76
77   gst_check_teardown_src_pad (filter);
78   gst_check_teardown_sink_pad (filter);
79   gst_check_teardown_element (filter);
80 }
81
82 static void
83 check_filter_caps (const gchar * name, GstCaps * caps, gint size,
84     gint num_buffers, const gchar * prop, va_list varargs)
85 {
86   GstElement *filter;
87   GstBuffer *inbuffer, *outbuffer;
88   gint i;
89
90   filter = setup_filter (name, prop, varargs);
91   fail_unless (gst_element_set_state (filter,
92           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
93       "could not set to playing");
94
95   for (i = 0; i < num_buffers; ++i) {
96     inbuffer = gst_buffer_new_and_alloc (size);
97     /* makes valgrind's memcheck happier */
98     memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
99     gst_buffer_set_caps (inbuffer, caps);
100     GST_BUFFER_TIMESTAMP (inbuffer) = 0;
101     ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
102     fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
103   }
104
105   fail_unless (g_list_length (buffers) == num_buffers);
106
107   /* clean up buffers */
108   for (i = 0; i < num_buffers; ++i) {
109     outbuffer = GST_BUFFER (buffers->data);
110     fail_if (outbuffer == NULL);
111
112     switch (i) {
113       case 0:
114         fail_unless (GST_BUFFER_SIZE (outbuffer) == size);
115         /* no check on filter operation itself */
116         break;
117       default:
118         break;
119     }
120     buffers = g_list_remove (buffers, outbuffer);
121
122     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
123     gst_buffer_unref (outbuffer);
124     outbuffer = NULL;
125   }
126
127   cleanup_filter (filter);
128   g_list_free (buffers);
129   buffers = NULL;
130 }
131
132 static void
133 check_filter (const gchar * name, gint num_buffers, const gchar * prop, ...)
134 {
135   static const struct
136   {
137     const int width, height;
138   } resolutions[] = { {
139   384, 288}, {
140   385, 289}, {
141   385, 385}};
142   gint i, n, r;
143   GstVideoFormat format;
144   gint size;
145   GstCaps *templ = gst_caps_from_string (VIDEO_CAPS_TEMPLATE_STRING);
146   va_list varargs;
147
148   n = gst_caps_get_size (templ);
149
150   for (i = 0; i < n; i++) {
151     GstStructure *s = gst_caps_get_structure (templ, i);
152     GstCaps *caps = gst_caps_new_empty ();
153
154     gst_caps_append_structure (caps, gst_structure_copy (s));
155
156     /* try various resolutions */
157     for (r = 0; r < G_N_ELEMENTS (resolutions); ++r) {
158       caps = gst_caps_make_writable (caps);
159       gst_caps_set_simple (caps, "width", G_TYPE_INT, resolutions[r].width,
160           "height", G_TYPE_INT, resolutions[r].height,
161           "framerate", GST_TYPE_FRACTION, 25, 1, NULL);
162
163       GST_DEBUG ("Testing with caps: %" GST_PTR_FORMAT, caps);
164       gst_video_format_parse_caps (caps, &format, NULL, NULL);
165       size = gst_video_format_get_size (format, resolutions[r].width,
166           resolutions[r].height);
167
168       va_start (varargs, prop);
169       check_filter_caps (name, caps, size, num_buffers, prop, varargs);
170       va_end (varargs);
171     }
172
173     gst_caps_unref (caps);
174   }
175
176   gst_caps_unref (templ);
177 }
178
179 GST_START_TEST (test_videobalance)
180 {
181   check_filter ("videobalance", 2, NULL);
182   check_filter ("videobalance", 2, "saturation", 0.5, "hue", 0.8, NULL);
183 }
184
185 GST_END_TEST;
186
187
188 GST_START_TEST (test_videoflip)
189 {
190   /* these we can handle with the caps */
191   check_filter ("videoflip", 2, "method", 0, NULL);
192   check_filter ("videoflip", 2, "method", 2, NULL);
193   check_filter ("videoflip", 2, "method", 4, NULL);
194   check_filter ("videoflip", 2, "method", 5, NULL);
195 }
196
197 GST_END_TEST;
198
199 GST_START_TEST (test_gamma)
200 {
201   check_filter ("gamma", 2, NULL);
202   check_filter ("gamma", 2, "gamma", 2.0, NULL);
203 }
204
205 GST_END_TEST;
206
207
208 static Suite *
209 videofilter_suite (void)
210 {
211   Suite *s = suite_create ("videofilter");
212   TCase *tc_chain = tcase_create ("general");
213
214   suite_add_tcase (s, tc_chain);
215   tcase_add_test (tc_chain, test_videobalance);
216   tcase_add_test (tc_chain, test_videoflip);
217   tcase_add_test (tc_chain, test_gamma);
218
219   return s;
220 }
221
222 int
223 main (int argc, char **argv)
224 {
225   int nf;
226
227   Suite *s = videofilter_suite ();
228   SRunner *sr = srunner_create (s);
229
230   gst_check_init (&argc, &argv);
231
232   srunner_run_all (sr, CK_NORMAL);
233   nf = srunner_ntests_failed (sr);
234   srunner_free (sr);
235
236   return nf;
237 }