3 * unit test for audioconvert
5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
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.
25 #include <gst/check/gstcheck.h>
27 GList *buffers = NULL;
28 gboolean have_eos = FALSE;
30 /* For ease of programming we use globals to keep refs for our floating
31 * src and sink pads we create; otherwise we always have to do get_pad,
32 * get_peer, and then remove references in every test function */
33 GstPad *mysrcpad, *mysinkpad;
35 #define CONVERT_CAPS_TEMPLATE_STRING \
36 "audio/x-raw-float, " \
37 "rate = (int) [ 1, MAX ], " \
38 "channels = (int) [ 1, 8 ], " \
39 "endianness = (int) BYTE_ORDER, " \
42 "rate = (int) [ 1, MAX ], " \
43 "channels = (int) [ 1, 8 ], " \
44 "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
45 "width = (int) 32, " \
46 "depth = (int) [ 1, 32 ], " \
47 "signed = (boolean) { true, false }; " \
49 "rate = (int) [ 1, MAX ], " \
50 "channels = (int) [ 1, 8 ], " \
51 "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
52 "width = (int) 24, " \
53 "depth = (int) [ 1, 24 ], " \
54 "signed = (boolean) { true, false }; " \
56 "rate = (int) [ 1, MAX ], " \
57 "channels = (int) [ 1, 8 ], " \
58 "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
59 "width = (int) 16, " \
60 "depth = (int) [ 1, 16 ], " \
61 "signed = (boolean) { true, false }; " \
63 "rate = (int) [ 1, MAX ], " \
64 "channels = (int) [ 1, 8 ], " \
65 "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
67 "depth = (int) [ 1, 8 ], " \
68 "signed = (boolean) { true, false } "
70 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS (CONVERT_CAPS_TEMPLATE_STRING)
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
78 GST_STATIC_CAPS (CONVERT_CAPS_TEMPLATE_STRING)
81 /* takes over reference for outcaps */
83 setup_audioconvert (GstCaps * outcaps)
85 GstElement *audioconvert;
87 GST_DEBUG ("setup_audioconvert with caps %" GST_PTR_FORMAT, outcaps);
88 audioconvert = gst_check_setup_element ("audioconvert");
89 mysrcpad = gst_check_setup_src_pad (audioconvert, &srctemplate, NULL);
90 mysinkpad = gst_check_setup_sink_pad (audioconvert, &sinktemplate, NULL);
91 /* this installs a getcaps func that will always return the caps we set
93 gst_pad_use_fixed_caps (mysinkpad);
94 gst_pad_set_caps (mysinkpad, outcaps);
95 gst_caps_unref (outcaps);
96 outcaps = gst_pad_get_negotiated_caps (mysinkpad);
97 fail_unless (gst_caps_is_fixed (outcaps));
98 gst_caps_unref (outcaps);
100 gst_pad_set_active (mysrcpad, TRUE);
101 gst_pad_set_active (mysinkpad, TRUE);
107 cleanup_audioconvert (GstElement * audioconvert)
109 GST_DEBUG ("cleanup_audioconvert");
111 gst_pad_set_active (mysrcpad, FALSE);
112 gst_pad_set_active (mysinkpad, FALSE);
113 gst_check_teardown_src_pad (audioconvert);
114 gst_check_teardown_sink_pad (audioconvert);
115 gst_check_teardown_element (audioconvert);
118 /* returns a newly allocated caps */
120 get_int_caps (guint channels, gchar * endianness, guint width,
121 guint depth, gboolean signedness)
126 string = g_strdup_printf ("audio/x-raw-int, "
127 "rate = (int) 44100, "
128 "channels = (int) %d, "
129 "endianness = (int) %s, "
132 "signed = (boolean) %s ",
133 channels, endianness, width, depth, signedness ? "true" : "false");
134 GST_DEBUG ("creating caps from %s", string);
135 caps = gst_caps_from_string (string);
137 fail_unless (caps != NULL);
138 GST_DEBUG ("returning caps %p", caps);
142 /* eats the refs to the caps */
144 verify_convert (void *in, int inlength,
145 GstCaps * incaps, void *out, int outlength, GstCaps * outcaps)
147 GstBuffer *inbuffer, *outbuffer;
148 GstElement *audioconvert;
150 GST_DEBUG ("incaps: %" GST_PTR_FORMAT, incaps);
151 GST_DEBUG ("outcaps: %" GST_PTR_FORMAT, outcaps);
152 ASSERT_CAPS_REFCOUNT (incaps, "incaps", 1);
153 ASSERT_CAPS_REFCOUNT (outcaps, "outcaps", 1);
154 audioconvert = setup_audioconvert (outcaps);
155 ASSERT_CAPS_REFCOUNT (outcaps, "outcaps", 1);
157 fail_unless (gst_element_set_state (audioconvert,
158 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
159 "could not set to playing");
161 GST_DEBUG ("Creating buffer of %d bytes", inlength);
162 inbuffer = gst_buffer_new_and_alloc (inlength);
163 memcpy (GST_BUFFER_DATA (inbuffer), in, inlength);
164 gst_buffer_set_caps (inbuffer, incaps);
165 ASSERT_CAPS_REFCOUNT (incaps, "incaps", 2);
166 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
168 /* pushing gives away my reference ... */
169 GST_DEBUG ("push it");
170 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
171 GST_DEBUG ("pushed it");
172 /* ... and puts a new buffer on the global list */
173 fail_unless (g_list_length (buffers) == 1);
174 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
176 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
177 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), outlength);
178 fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, outlength) == 0);
179 buffers = g_list_remove (buffers, outbuffer);
180 gst_buffer_unref (outbuffer);
181 fail_unless (gst_element_set_state (audioconvert,
182 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
184 GST_DEBUG ("cleanup audioconvert");
185 cleanup_audioconvert (audioconvert);
186 GST_DEBUG ("cleanup, unref incaps");
187 ASSERT_CAPS_REFCOUNT (incaps, "incaps", 1);
188 gst_caps_unref (incaps);
192 #define RUN_CONVERSION(inarray, in_get_caps, outarray, out_get_caps) \
193 verify_convert (inarray, sizeof (inarray), \
194 in_get_caps, outarray, sizeof (outarray), out_get_caps)
196 GST_START_TEST (test_int16)
200 gint16 in[] = { 16384, -256, 1024, 1024 };
201 gint16 out[] = { 8064, 1024 };
203 RUN_CONVERSION (in, get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE),
204 out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
208 gint16 in[] = { 512, 1024 };
209 gint16 out[] = { 512, 512, 1024, 1024 };
211 RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
212 out, get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE));
214 /* signed -> unsigned */
216 gint16 in[] = { 0, -32767, 32767, -32768 };
217 guint16 out[] = { 32768, 1, 65535, 0 };
219 RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
220 out, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE));
221 RUN_CONVERSION (out, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE),
222 in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
228 GST_START_TEST (test_int_conversion)
230 /* 8 <-> 16 signed */
231 /* NOTE: if audioconvert was doing dithering we'd have a problem */
233 gint8 in[] = { 0, 1, 2, 127, -127 };
234 gint16 out[] = { 0, 256, 512, 32512, -32512 };
236 RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE),
237 out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE)
239 RUN_CONVERSION (out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
240 in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
245 gint16 in[] = { 0, 255, 256, 257 };
246 gint8 out[] = { 0, 0, 1, 1 };
248 RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
249 out, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
252 /* 8 unsigned <-> 16 signed */
253 /* NOTE: if audioconvert was doing dithering we'd have a problem */
255 guint8 in[] = { 128, 129, 130, 255, 1 };
256 gint16 out[] = { 0, 256, 512, 32512, -32512 };
257 GstCaps *incaps, *outcaps;
259 /* exploded for easier valgrinding */
260 incaps = get_int_caps (1, "BYTE_ORDER", 8, 8, FALSE);
261 outcaps = get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE);
262 GST_DEBUG ("incaps: %" GST_PTR_FORMAT, incaps);
263 GST_DEBUG ("outcaps: %" GST_PTR_FORMAT, outcaps);
264 RUN_CONVERSION (in, incaps, out, outcaps);
265 RUN_CONVERSION (out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
266 in, get_int_caps (1, "BYTE_ORDER", 8, 8, FALSE)
269 /* 8 <-> 24 signed */
270 /* NOTE: if audioconvert was doing dithering we'd have a problem */
272 gint8 in[] = { 0, 1, 127 };
273 guint8 out[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7f };
275 RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE),
276 out, get_int_caps (1, "BYTE_ORDER", 24, 24, TRUE)
278 RUN_CONVERSION (out, get_int_caps (1, "BYTE_ORDER", 24, 24, TRUE),
279 in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
288 audioconvert_suite (void)
290 Suite *s = suite_create ("audioconvert");
291 TCase *tc_chain = tcase_create ("general");
293 suite_add_tcase (s, tc_chain);
294 tcase_add_test (tc_chain, test_int16);
295 //tcase_add_test (tc_chain, test_int_conversion);
301 main (int argc, char **argv)
305 Suite *s = audioconvert_suite ();
306 SRunner *sr = srunner_create (s);
308 gst_check_init (&argc, &argv);
310 srunner_run_all (sr, CK_NORMAL);
311 nf = srunner_ntests_failed (sr);