1 /*
2  * GStreamer
3  *
4  * unit test for templatematch
5  *
6  * Copyright (C) 2014 Samsung Electronics. All rights reserved.
7  *   Author: Thiago Santos <ts.santos@sisa.samsung.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 #include <gst/check/gstcheck.h>
26 
27 #define CAPS_TMPL   "video/x-raw, format=(string)BGR"
28 
29 GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
30     GST_PAD_SINK,
31     GST_PAD_ALWAYS,
32     GST_STATIC_CAPS (CAPS_TMPL)
33     );
34 GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
35     GST_PAD_SRC,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS (CAPS_TMPL)
38     );
39 
40 /* Create a 16x16 buffer split in 4 equal squares
41  * BG
42  * Rb
43  *
44  * B=Blue, G=Green, R=Red, b=black
45  */
46 static GstBuffer *
create_input_buffer(void)47 create_input_buffer (void)
48 {
49   guint8 *data;
50   gsize size;
51   gint i, j, base;
52 
53   size = 3 * 16 * 16;           /* BGR 16x16 */
54   data = g_malloc0 (size);
55 
56   /* blue and green */
57   for (j = 0; j < 8; j++) {
58     for (i = 0; i < 8; i++) {
59       base = j * 16;
60       data[base + i] = 255;
61       data[base + i + 1] = 0;
62       data[base + i + 2] = 0;
63 
64       data[base + 8 + i] = 0;
65       data[base + 8 + i + 1] = 255;
66       data[base + 8 + i + 2] = 0;
67     }
68   }
69   /* red */
70   for (j = 0; j < 8; j++) {
71     for (i = 0; i < 8; i++) {
72       base = 8 * 8 + j * 16;
73       data[base + i] = 0;
74       data[base + i + 1] = 0;
75       data[base + i + 2] = 255;
76     }
77   }
78 
79   return gst_buffer_new_wrapped (data, size);
80 }
81 
82 /* Test to make sure that we are using the same rgb format as opencv expects for
83  * template matching (BGR).
84  * Will use a blue 8x8 square as the template and as input a 16x16 frame divided
85  * evenly in 4 squares: Blue (top-left), Green (top-right), Red (bottom-left) and
86  * Black (bottom-right)
87  *
88  * https://bugzilla.gnome.org/show_bug.cgi?id=678485
89  */
GST_START_TEST(test_match_blue_square)90 GST_START_TEST (test_match_blue_square)
91 {
92   GstElement *element;
93   GstPad *sinkpad, *srcpad;
94   GstCaps *caps =
95       gst_caps_from_string (CAPS_TMPL
96       ", width=(int)16, height=(int)16, framerate=1/1");
97   GstBus *bus;
98   GstMessage *msg;
99   const GstStructure *structure;
100   gchar *path;
101   GstBuffer *buf;
102   guint x, y, width, height;
103 
104   element = gst_check_setup_element ("templatematch");
105   srcpad = gst_check_setup_src_pad (element, &srctemplate);
106   sinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
107   gst_pad_set_active (srcpad, TRUE);
108   gst_check_setup_events (srcpad, element, caps, GST_FORMAT_TIME);
109   gst_pad_set_active (sinkpad, TRUE);
110 
111   bus = gst_bus_new ();
112   gst_element_set_bus (element, bus);
113 
114   path = g_build_filename (GST_TEST_FILES_PATH, "blue-square.png", NULL);
115   g_object_set (element, "template", path, NULL);
116   g_free (path);
117 
118   fail_unless (gst_element_set_state (element,
119           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
120       "could not set to playing");
121 
122   buf = create_input_buffer ();
123   fail_unless (gst_pad_push (srcpad, buf) == GST_FLOW_OK);
124 
125   /* make sure that the template match message was posted, detecting the
126    * blue area in the top left corner */
127   msg = gst_bus_pop_filtered (bus, GST_MESSAGE_ELEMENT);
128   fail_unless (msg != NULL);
129   fail_unless (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element));
130   structure = gst_message_get_structure (msg);
131   fail_unless (gst_structure_has_name (structure, "template_match"));
132   fail_unless (gst_structure_get_uint (structure, "x", &x));
133   fail_unless (gst_structure_get_uint (structure, "y", &y));
134   fail_unless (gst_structure_get_uint (structure, "width", &width));
135   fail_unless (gst_structure_get_uint (structure, "height", &height));
136   fail_unless (x == 0);
137   fail_unless (y == 0);
138   fail_unless (width == 8);
139   fail_unless (height == 8);
140 
141   gst_message_unref (msg);
142 
143   gst_element_set_state (element, GST_STATE_NULL);
144   gst_bus_set_flushing (bus, TRUE);
145   gst_object_unref (bus);
146   gst_caps_unref (caps);
147   gst_check_drop_buffers ();
148   gst_pad_set_active (srcpad, FALSE);
149   gst_pad_set_active (sinkpad, FALSE);
150   gst_object_unref (srcpad);
151   gst_object_unref (sinkpad);
152   gst_check_teardown_src_pad (element);
153   gst_check_teardown_sink_pad (element);
154   gst_check_teardown_element (element);
155 }
156 
157 GST_END_TEST;
158 
159 static Suite *
templatematch_suite(void)160 templatematch_suite (void)
161 {
162   Suite *s = suite_create ("templatematch");
163   TCase *tc_chain = tcase_create ("general");
164 
165   suite_add_tcase (s, tc_chain);
166   tcase_add_test (tc_chain, test_match_blue_square);
167 
168   return s;
169 }
170 
171 GST_CHECK_MAIN (templatematch);
172