35e9c06d15007ecfac98272efefd95cf00f553aa
[platform/upstream/gstreamer.git] / tests / check / elements / videoscale.c
1 /* GStreamer
2  *
3  * unit test for videoscale
4  *
5  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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 <gst/check/gstcheck.h>
24 #include <string.h>
25
26 static GstCaps **
27 videoscale_get_allowed_caps (void)
28 {
29   GstElement *scale = gst_element_factory_make ("videoscale", "scale");
30   GstPadTemplate *templ;
31   GstCaps *caps, **ret;
32   GstStructure *s;
33   gint i, n;
34
35   templ =
36       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (scale),
37       "sink");
38   fail_unless (templ != NULL);
39
40   caps = gst_pad_template_get_caps (templ);
41
42   n = gst_caps_get_size (caps);
43   ret = g_new0 (GstCaps *, n + 1);
44
45   for (i = 0; i < n; i++) {
46     s = gst_caps_get_structure (caps, i);
47     ret[i] = gst_caps_new_empty ();
48     gst_caps_append_structure (ret[i], gst_structure_copy (s));
49   }
50
51   gst_object_unref (scale);
52
53   return ret;
54 }
55
56 typedef struct
57 {
58   GMainLoop *loop;
59   gboolean eos;
60 } OnMessageUserData;
61
62 static void
63 on_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
64 {
65   OnMessageUserData *d = user_data;
66
67   switch (GST_MESSAGE_TYPE (message)) {
68     case GST_MESSAGE_ERROR:
69     case GST_MESSAGE_WARNING:
70       g_assert_not_reached ();
71       break;
72     case GST_MESSAGE_EOS:
73       g_main_loop_quit (d->loop);
74       d->eos = TRUE;
75       break;
76     default:
77       break;
78   }
79 }
80
81 static void
82 on_sink_handoff (GstElement * element, GstBuffer * buffer, GstPad * pad,
83     gpointer user_data)
84 {
85   guint *n_buffers = user_data;
86
87   *n_buffers = *n_buffers + 1;
88 }
89
90 static void
91 run_test (const GstCaps * caps, gint src_width, gint src_height,
92     gint dest_width, gint dest_height, gint method,
93     GCallback src_handoff, gpointer src_handoff_user_data,
94     GCallback sink_handoff, gpointer sink_handoff_user_data)
95 {
96   GstElement *pipeline;
97   GstElement *src, *capsfilter1, *identity, *scale, *capsfilter2, *sink;
98   GstBus *bus;
99   GMainLoop *loop;
100   GstCaps *copy;
101   guint n_buffers = 0;
102   OnMessageUserData omud = { NULL, };
103
104   pipeline = gst_element_factory_make ("pipeline", "pipeline");
105   fail_unless (pipeline != NULL);
106
107   src = gst_element_factory_make ("videotestsrc", "src");
108   fail_unless (src != NULL);
109   g_object_set (G_OBJECT (src), "num-buffers", 5, NULL);
110
111   capsfilter1 = gst_element_factory_make ("capsfilter", "filter1");
112   fail_unless (capsfilter1 != NULL);
113   copy = gst_caps_copy (caps);
114   gst_caps_set_simple (copy, "width", G_TYPE_INT, src_width, "height",
115       G_TYPE_INT, src_height, "framerate", GST_TYPE_FRACTION, 30, 1, NULL);
116   g_object_set (G_OBJECT (capsfilter1), "caps", copy, NULL);
117   gst_caps_unref (copy);
118
119   identity = gst_element_factory_make ("identity", "identity");
120   fail_unless (identity != NULL);
121   if (src_handoff) {
122     g_object_set (G_OBJECT (identity), "signal-handoffs", TRUE, NULL);
123     g_signal_connect (identity, "handoff", G_CALLBACK (src_handoff),
124         src_handoff_user_data);
125   }
126
127   scale = gst_element_factory_make ("videoscale", "scale");
128   fail_unless (scale != NULL);
129   g_object_set (G_OBJECT (scale), "method", method, NULL);
130
131   capsfilter2 = gst_element_factory_make ("capsfilter", "filter2");
132   fail_unless (capsfilter2 != NULL);
133   copy = gst_caps_copy (caps);
134   gst_caps_set_simple (copy, "width", G_TYPE_INT, dest_width, "height",
135       G_TYPE_INT, dest_height, NULL);
136   g_object_set (G_OBJECT (capsfilter2), "caps", copy, NULL);
137   gst_caps_unref (copy);
138
139   sink = gst_element_factory_make ("fakesink", "sink");
140   fail_unless (sink != NULL);
141   g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, "async", FALSE, NULL);
142   g_signal_connect (sink, "handoff", G_CALLBACK (on_sink_handoff), &n_buffers);
143   if (sink_handoff) {
144     g_signal_connect (sink, "handoff", G_CALLBACK (sink_handoff),
145         sink_handoff_user_data);
146   }
147
148   gst_bin_add_many (GST_BIN (pipeline), src, capsfilter1, identity, scale,
149       capsfilter2, sink, NULL);
150   fail_unless (gst_element_link_many (src, capsfilter1, identity, scale,
151           capsfilter2, sink, NULL));
152
153   loop = g_main_loop_new (NULL, FALSE);
154
155   bus = gst_element_get_bus (pipeline);
156   fail_unless (bus != NULL);
157   gst_bus_add_signal_watch (bus);
158
159   omud.loop = loop;
160   omud.eos = FALSE;
161
162   g_signal_connect (bus, "message", (GCallback) on_message_cb, &omud);
163
164   gst_object_unref (bus);
165
166   fail_unless (gst_element_set_state (pipeline,
167           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
168
169   g_main_loop_run (loop);
170
171   fail_unless (gst_element_set_state (pipeline,
172           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
173
174   fail_unless (omud.eos == TRUE);
175   fail_unless (n_buffers == 5);
176
177   gst_object_unref (pipeline);
178   g_main_loop_unref (loop);
179 }
180
181 static void
182 on_sink_handoff_passthrough (GstElement * element, GstBuffer * buffer,
183     GstPad * pad, gpointer user_data)
184 {
185   GList **list = user_data;
186
187   *list = g_list_prepend (*list, gst_buffer_ref (buffer));
188 }
189
190 static void
191 on_src_handoff_passthrough (GstElement * element, GstBuffer * buffer,
192     gpointer user_data)
193 {
194   GList **list = user_data;
195
196   *list = g_list_prepend (*list, gst_buffer_ref (buffer));
197 }
198
199 GST_START_TEST (test_passthrough)
200 {
201   GList *l1, *l2, *src_buffers = NULL, *sink_buffers = NULL;
202   GstCaps **allowed_caps = NULL, **p;
203   gint method;
204   static const gint src_width = 640, src_height = 480;
205   static const gint dest_width = 640, dest_height = 480;
206
207   p = allowed_caps = videoscale_get_allowed_caps ();
208
209   while (*p) {
210     GstCaps *caps = *p;
211
212     for (method = 0; method < 3; method++) {
213       GST_DEBUG ("Running test for caps '%" GST_PTR_FORMAT "'"
214           " from %dx%u to %dx%d with method %d", caps, src_width, src_height,
215           dest_width, dest_height, method);
216       run_test (caps, src_width, src_height,
217           dest_width, dest_height, method,
218           G_CALLBACK (on_src_handoff_passthrough), &src_buffers,
219           G_CALLBACK (on_sink_handoff_passthrough), &sink_buffers);
220
221       fail_unless (src_buffers && sink_buffers);
222       fail_unless_equals_int (g_list_length (src_buffers),
223           g_list_length (sink_buffers));
224
225       for (l1 = src_buffers, l2 = sink_buffers; l1 && l2;
226           l1 = l1->next, l2 = l2->next) {
227         GstBuffer *a = l1->data;
228         GstBuffer *b = l2->data;
229
230         fail_unless_equals_int (GST_BUFFER_SIZE (a), GST_BUFFER_SIZE (b));
231         fail_unless (GST_BUFFER_DATA (a) == GST_BUFFER_DATA (b));
232
233         gst_buffer_unref (a);
234         gst_buffer_unref (b);
235       }
236       g_list_free (src_buffers);
237       src_buffers = NULL;
238       g_list_free (sink_buffers);
239       sink_buffers = NULL;
240     }
241
242     gst_caps_unref (caps);
243     p++;
244   }
245   g_free (allowed_caps);
246 }
247
248 GST_END_TEST;
249
250 #define CREATE_TEST(name,method,src_width,src_height,dest_width,dest_height) \
251 GST_START_TEST (name) \
252 { \
253   GstCaps **allowed_caps = NULL, **p; \
254   \
255   p = allowed_caps = videoscale_get_allowed_caps (); \
256   \
257   while (*p) { \
258     GstCaps *caps = *p; \
259     \
260     GST_DEBUG ("Running test for caps '%" GST_PTR_FORMAT "'" \
261         " from %dx%u to %dx%d with method %d", caps, src_width, src_height, \
262         dest_width, dest_height, method); \
263     run_test (caps, src_width, src_height, \
264         dest_width, dest_height, method, \
265         NULL, NULL, NULL, NULL); \
266     \
267     gst_caps_unref (caps); \
268     p++; \
269   } \
270   g_free (allowed_caps); \
271 } \
272 \
273 GST_END_TEST;
274
275 CREATE_TEST (test_downscale_640x480_320x240_method_0, 0, 640, 480, 320, 240);
276 CREATE_TEST (test_downscale_640x480_320x240_method_1, 1, 640, 480, 320, 240);
277 CREATE_TEST (test_downscale_640x480_320x240_method_2, 2, 640, 480, 320, 240);
278 CREATE_TEST (test_upscale_320x240_640x480_method_0, 0, 320, 240, 640, 480);
279 CREATE_TEST (test_upscale_320x240_640x480_method_1, 1, 320, 240, 640, 480);
280 CREATE_TEST (test_upscale_320x240_640x480_method_2, 2, 320, 240, 640, 480);
281 CREATE_TEST (test_downscale_640x480_1x1_method_0, 0, 640, 480, 1, 1);
282 CREATE_TEST (test_downscale_640x480_1x1_method_1, 1, 640, 480, 1, 1);
283 CREATE_TEST (test_downscale_640x480_1x1_method_2, 2, 640, 480, 1, 1);
284 CREATE_TEST (test_upscale_1x1_640x480_method_0, 0, 1, 1, 640, 480);
285 CREATE_TEST (test_upscale_1x1_640x480_method_1, 1, 1, 1, 640, 480);
286 CREATE_TEST (test_upscale_1x1_640x480_method_2, 2, 1, 1, 640, 480);
287 CREATE_TEST (test_downscale_641x481_111x30_method_0, 0, 641, 481, 111, 30);
288 CREATE_TEST (test_downscale_641x481_111x30_method_1, 1, 641, 481, 111, 30);
289 CREATE_TEST (test_downscale_641x481_111x30_method_2, 2, 641, 481, 111, 30);
290 CREATE_TEST (test_upscale_111x30_641x481_method_0, 0, 111, 30, 641, 481);
291 CREATE_TEST (test_upscale_111x30_641x481_method_1, 1, 111, 30, 641, 481);
292 CREATE_TEST (test_upscale_111x30_641x481_method_2, 2, 111, 30, 641, 481);
293 CREATE_TEST (test_downscale_641x481_30x111_method_0, 0, 641, 481, 30, 111);
294 CREATE_TEST (test_downscale_641x481_30x111_method_1, 1, 641, 481, 30, 111);
295 CREATE_TEST (test_downscale_641x481_30x111_method_2, 2, 641, 481, 30, 111);
296 CREATE_TEST (test_upscale_30x111_641x481_method_0, 0, 30, 111, 641, 481);
297 CREATE_TEST (test_upscale_30x111_641x481_method_1, 1, 30, 111, 641, 481);
298 CREATE_TEST (test_upscale_30x111_641x481_method_2, 2, 30, 111, 641, 481);
299 CREATE_TEST (test_downscale_640x480_320x1_method_0, 0, 640, 480, 320, 1);
300 CREATE_TEST (test_downscale_640x480_320x1_method_1, 1, 640, 480, 320, 1);
301 CREATE_TEST (test_downscale_640x480_320x1_method_2, 2, 640, 480, 320, 1);
302 CREATE_TEST (test_upscale_320x1_640x480_method_0, 0, 320, 1, 640, 480);
303 CREATE_TEST (test_upscale_320x1_640x480_method_1, 1, 320, 1, 640, 480);
304 CREATE_TEST (test_upscale_320x1_640x480_method_2, 2, 320, 1, 640, 480);
305 CREATE_TEST (test_downscale_640x480_1x240_method_0, 0, 640, 480, 1, 240);
306 CREATE_TEST (test_downscale_640x480_1x240_method_1, 1, 640, 480, 1, 240);
307 CREATE_TEST (test_downscale_640x480_1x240_method_2, 2, 640, 480, 1, 240);
308 CREATE_TEST (test_upscale_1x240_640x480_method_0, 0, 1, 240, 640, 480);
309 CREATE_TEST (test_upscale_1x240_640x480_method_1, 1, 1, 240, 640, 480);
310 CREATE_TEST (test_upscale_1x240_640x480_method_2, 2, 1, 240, 640, 480);
311
312 static Suite *
313 videoscale_suite (void)
314 {
315   Suite *s = suite_create ("videoscale");
316   TCase *tc_chain = tcase_create ("general");
317
318   suite_add_tcase (s, tc_chain);
319   tcase_set_timeout (tc_chain, 180);
320   tcase_add_test (tc_chain, test_passthrough);
321   tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_0);
322   tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_1);
323   tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_2);
324   tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_0);
325   tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_1);
326   tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_2);
327   tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_0);
328   tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_1);
329   tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_2);
330   tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_0);
331   tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_1);
332   tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_2);
333   tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_0);
334   tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_1);
335   tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_2);
336   tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_0);
337   tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_1);
338   tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_2);
339   tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_0);
340   tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_1);
341   tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_2);
342   tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_0);
343   tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_1);
344   tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_2);
345   tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_0);
346   tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_1);
347   tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_2);
348   tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_0);
349   tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_1);
350   tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_2);
351   tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_0);
352   tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_1);
353   tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_2);
354   tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_0);
355   tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_1);
356   tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_2);
357
358   return s;
359 }
360
361 GST_CHECK_MAIN (videoscale);