3 * unit test for videoscale
5 * Copyright (C) <2009,2010> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
23 #include <gst/check/gstcheck.h>
27 videoscale_get_allowed_caps (void)
29 GstElement *scale = gst_element_factory_make ("videoscale", "scale");
30 GstPadTemplate *templ;
36 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (scale),
38 fail_unless (templ != NULL);
40 caps = gst_pad_template_get_caps (templ);
42 n = gst_caps_get_size (caps);
43 ret = g_new0 (GstCaps *, n + 1);
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));
51 gst_object_unref (scale);
63 on_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
65 OnMessageUserData *d = user_data;
67 switch (GST_MESSAGE_TYPE (message)) {
68 case GST_MESSAGE_ERROR:
69 case GST_MESSAGE_WARNING:
70 g_assert_not_reached ();
73 g_main_loop_quit (d->loop);
82 on_sink_handoff (GstElement * element, GstBuffer * buffer, GstPad * pad,
85 guint *n_buffers = user_data;
87 *n_buffers = *n_buffers + 1;
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)
97 GstElement *src, *capsfilter1, *identity, *scale, *capsfilter2, *sink;
102 OnMessageUserData omud = { NULL, };
104 pipeline = gst_element_factory_make ("pipeline", "pipeline");
105 fail_unless (pipeline != NULL);
107 src = gst_element_factory_make ("videotestsrc", "src");
108 fail_unless (src != NULL);
109 g_object_set (G_OBJECT (src), "num-buffers", 5, NULL);
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);
119 identity = gst_element_factory_make ("identity", "identity");
120 fail_unless (identity != NULL);
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);
127 scale = gst_element_factory_make ("videoscale", "scale");
128 fail_unless (scale != NULL);
129 g_object_set (G_OBJECT (scale), "method", method, NULL);
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);
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);
144 g_signal_connect (sink, "handoff", G_CALLBACK (sink_handoff),
145 sink_handoff_user_data);
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));
153 loop = g_main_loop_new (NULL, FALSE);
155 bus = gst_element_get_bus (pipeline);
156 fail_unless (bus != NULL);
157 gst_bus_add_signal_watch (bus);
162 g_signal_connect (bus, "message", (GCallback) on_message_cb, &omud);
164 gst_object_unref (bus);
166 fail_unless (gst_element_set_state (pipeline,
167 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
169 g_main_loop_run (loop);
171 fail_unless (gst_element_set_state (pipeline,
172 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
174 fail_unless (omud.eos == TRUE);
175 fail_unless (n_buffers == 5);
177 gst_object_unref (pipeline);
178 g_main_loop_unref (loop);
182 on_sink_handoff_passthrough (GstElement * element, GstBuffer * buffer,
183 GstPad * pad, gpointer user_data)
185 GList **list = user_data;
187 *list = g_list_prepend (*list, gst_buffer_ref (buffer));
191 on_src_handoff_passthrough (GstElement * element, GstBuffer * buffer,
194 GList **list = user_data;
196 *list = g_list_prepend (*list, gst_buffer_ref (buffer));
199 GST_START_TEST (test_passthrough)
201 GList *l1, *l2, *src_buffers = NULL, *sink_buffers = NULL;
202 GstCaps **allowed_caps = NULL, **p;
204 static const gint src_width = 640, src_height = 480;
205 static const gint dest_width = 640, dest_height = 480;
207 p = allowed_caps = videoscale_get_allowed_caps ();
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);
221 fail_unless (src_buffers && sink_buffers);
222 fail_unless_equals_int (g_list_length (src_buffers),
223 g_list_length (sink_buffers));
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;
230 fail_unless_equals_int (GST_BUFFER_SIZE (a), GST_BUFFER_SIZE (b));
231 fail_unless (GST_BUFFER_DATA (a) == GST_BUFFER_DATA (b));
233 gst_buffer_unref (a);
234 gst_buffer_unref (b);
236 g_list_free (src_buffers);
238 g_list_free (sink_buffers);
242 gst_caps_unref (caps);
245 g_free (allowed_caps);
250 #define CREATE_TEST(name,method,src_width,src_height,dest_width,dest_height) \
251 GST_START_TEST (name) \
253 GstCaps **allowed_caps = NULL, **p; \
255 p = allowed_caps = videoscale_get_allowed_caps (); \
258 GstCaps *caps = *p; \
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); \
267 gst_caps_unref (caps); \
270 g_free (allowed_caps); \
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);
318 } TestNegotiationData;
321 _test_negotiation_message (GstBus * bus, GstMessage * message,
322 TestNegotiationData * data)
324 switch (GST_MESSAGE_TYPE (message)) {
325 case GST_MESSAGE_ERROR:
326 case GST_MESSAGE_WARNING:
327 g_assert_not_reached ();
329 case GST_MESSAGE_EOS:
330 g_main_loop_quit (data->loop);
338 _test_negotiation_notify_caps (GObject * src, GParamSpec * pspec,
339 TestNegotiationData * data)
344 gint par_n = 0, par_d = 0;
346 g_object_get (src, "caps", &caps, NULL);
350 s = gst_caps_get_structure (caps, 0);
352 fail_unless (gst_structure_get_int (s, "width", &width));
353 fail_unless (gst_structure_get_int (s, "height", &height));
354 fail_unless (gst_structure_get_fraction (s, "pixel-aspect-ratio", &par_n,
355 &par_d) || (data->par_n == 1 && data->par_d == 1));
357 gst_caps_unref (caps);
359 fail_unless_equals_int (width, data->width);
360 fail_unless_equals_int (height, data->height);
361 if (par_n != 0 || par_d != 0) {
362 fail_unless_equals_int (par_n, data->par_n);
363 fail_unless_equals_int (par_d, data->par_d);
366 data->ok = (width == data->width) && (height == data->height)
367 && (par_n == data->par_n) && (par_d == data->par_d);
369 g_main_loop_quit (data->loop);
373 _test_negotiation (const gchar * src_templ, const gchar * sink_templ,
374 gint width, gint height, gint par_n, gint par_d)
376 GstElement *pipeline;
377 GstElement *src, *capsfilter1, *scale, *capsfilter2, *sink;
381 TestNegotiationData data = { 0, 0, 0, 0, FALSE, NULL };
384 pipeline = gst_element_factory_make ("pipeline", "pipeline");
385 fail_unless (pipeline != NULL);
387 src = gst_element_factory_make ("videotestsrc", "src");
388 fail_unless (src != NULL);
389 g_object_set (G_OBJECT (src), "num-buffers", 1, NULL);
391 capsfilter1 = gst_element_factory_make ("capsfilter", "filter1");
392 fail_unless (capsfilter1 != NULL);
393 caps = gst_caps_from_string (src_templ);
394 fail_unless (caps != NULL);
395 g_object_set (G_OBJECT (capsfilter1), "caps", caps, NULL);
396 gst_caps_unref (caps);
398 scale = gst_element_factory_make ("videoscale", "scale");
399 fail_unless (scale != NULL);
401 capsfilter2 = gst_element_factory_make ("capsfilter", "filter2");
402 fail_unless (capsfilter2 != NULL);
403 caps = gst_caps_from_string (sink_templ);
404 fail_unless (caps != NULL);
405 g_object_set (G_OBJECT (capsfilter2), "caps", caps, NULL);
406 gst_caps_unref (caps);
408 pad = gst_element_get_static_pad (capsfilter2, "sink");
409 fail_unless (pad != NULL);
410 g_signal_connect (pad, "notify::caps",
411 G_CALLBACK (_test_negotiation_notify_caps), &data);
412 gst_object_unref (pad);
414 sink = gst_element_factory_make ("fakesink", "sink");
415 fail_unless (sink != NULL);
416 g_object_set (sink, "async", FALSE, NULL);
418 gst_bin_add_many (GST_BIN (pipeline), src, capsfilter1, scale, capsfilter2,
420 fail_unless (gst_element_link_many (src, capsfilter1, scale, capsfilter2,
423 loop = g_main_loop_new (NULL, FALSE);
425 bus = gst_element_get_bus (pipeline);
426 fail_unless (bus != NULL);
427 gst_bus_add_signal_watch (bus);
431 data.height = height;
436 g_signal_connect (bus, "message", G_CALLBACK (_test_negotiation_message),
439 gst_object_unref (bus);
441 fail_unless (gst_element_set_state (pipeline,
442 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
444 g_main_loop_run (loop);
446 fail_unless (data.ok == TRUE);
448 fail_unless (gst_element_set_state (pipeline,
449 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
451 gst_object_unref (pipeline);
452 g_main_loop_unref (loop);
455 GST_START_TEST (test_negotiation)
458 ("video/x-raw-yuv,format=(fourcc)AYUV,width=720,height=576,pixel-aspect-ratio=16/15",
459 "video/x-raw-yuv,format=(fourcc)AYUV,width=768,height=576",
463 ("video/x-raw-yuv,format=(fourcc)AYUV,width=320,height=240",
464 "video/x-raw-yuv,format=(fourcc)AYUV,width=640,height=320",
468 ("video/x-raw-yuv,format=(fourcc)AYUV,width=320,height=240",
469 "video/x-raw-yuv,format=(fourcc)AYUV,width=640,height=320,pixel-aspect-ratio=[0/1, 1/1]",
473 ("video/x-raw-yuv,format=(fourcc)AYUV,width=1920,height=2560,pixel-aspect-ratio=1/1",
474 "video/x-raw-yuv,format=(fourcc)AYUV,width=[1, 2048],height=[1, 2048],pixel-aspect-ratio=1/1",
478 ("video/x-raw-yuv,format=(fourcc)AYUV,width=1920,height=2560,pixel-aspect-ratio=1/1",
479 "video/x-raw-yuv,format=(fourcc)AYUV,width=[1, 2048],height=[1, 2048]",
483 ("video/x-raw-yuv,format=(fourcc)AYUV,width=1920,height=2560",
484 "video/x-raw-yuv,format=(fourcc)AYUV,width=[1, 2048],height=[1, 2048]",
488 ("video/x-raw-yuv,format=(fourcc)AYUV,width=1920,height=2560",
489 "video/x-raw-yuv,format=(fourcc)AYUV,width=1200,height=[1, 2048],pixel-aspect-ratio=1/1",
492 /* Doesn't keep DAR but must be possible! */
494 ("video/x-raw-yuv,format=(fourcc)AYUV,width=320,height=240,pixel-aspect-ratio=1/1",
495 "video/x-raw-yuv,format=(fourcc)AYUV,width=200,height=200,pixel-aspect-ratio=1/2",
502 videoscale_suite (void)
504 Suite *s = suite_create ("videoscale");
505 TCase *tc_chain = tcase_create ("general");
507 suite_add_tcase (s, tc_chain);
508 tcase_set_timeout (tc_chain, 180);
509 tcase_add_test (tc_chain, test_passthrough);
510 tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_0);
511 tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_1);
512 tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_2);
513 tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_0);
514 tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_1);
515 tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_2);
516 tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_0);
517 tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_1);
518 tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_2);
519 tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_0);
520 tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_1);
521 tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_2);
522 tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_0);
523 tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_1);
524 tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_2);
525 tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_0);
526 tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_1);
527 tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_2);
528 tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_0);
529 tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_1);
530 tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_2);
531 tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_0);
532 tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_1);
533 tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_2);
534 tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_0);
535 tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_1);
536 tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_2);
537 tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_0);
538 tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_1);
539 tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_2);
540 tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_0);
541 tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_1);
542 tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_2);
543 tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_0);
544 tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_1);
545 tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_2);
546 tcase_add_test (tc_chain, test_negotiation);
551 GST_CHECK_MAIN (videoscale);