expand tabs
[platform/upstream/gstreamer.git] / tests / check / elements / audioconvert.c
1 /* GStreamer
2  *
3  * unit test for audioconvert
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
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
25 #include <gst/check/gstcheck.h>
26
27 GList *buffers = NULL;
28 gboolean have_eos = FALSE;
29
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;
34
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, " \
40     "width = (int) 32;" \
41   "audio/x-raw-int, " \
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 }; " \
48   "audio/x-raw-int, " \
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 }; " \
55   "audio/x-raw-int, " \
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 }; " \
62   "audio/x-raw-int, " \
63     "rate = (int) [ 1, MAX ], " \
64     "channels = (int) [ 1, 8 ], " \
65     "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
66     "width = (int) 8, " \
67     "depth = (int) [ 1, 8 ], " \
68     "signed = (boolean) { true, false } "
69
70 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (CONVERT_CAPS_TEMPLATE_STRING)
74     );
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS (CONVERT_CAPS_TEMPLATE_STRING)
79     );
80
81 /* takes over reference for outcaps */
82 GstElement *
83 setup_audioconvert (GstCaps * outcaps)
84 {
85   GstElement *audioconvert;
86
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
92    * later */
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);
99
100   gst_pad_set_active (mysrcpad, TRUE);
101   gst_pad_set_active (mysinkpad, TRUE);
102
103   return audioconvert;
104 }
105
106 void
107 cleanup_audioconvert (GstElement * audioconvert)
108 {
109   GST_DEBUG ("cleanup_audioconvert");
110
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);
116 }
117
118 /* returns a newly allocated caps */
119 static GstCaps *
120 get_int_caps (guint channels, gchar * endianness, guint width,
121     guint depth, gboolean signedness)
122 {
123   GstCaps *caps;
124   gchar *string;
125
126   string = g_strdup_printf ("audio/x-raw-int, "
127       "rate = (int) 44100, "
128       "channels = (int) %d, "
129       "endianness = (int) %s, "
130       "width = (int) %d, "
131       "depth = (int) %d, "
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);
136   g_free (string);
137   fail_unless (caps != NULL);
138   GST_DEBUG ("returning caps %p", caps);
139   return caps;
140 }
141
142 /* eats the refs to the caps */
143 static void
144 verify_convert (void *in, int inlength,
145     GstCaps * incaps, void *out, int outlength, GstCaps * outcaps)
146 {
147   GstBuffer *inbuffer, *outbuffer;
148   GstElement *audioconvert;
149
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);
156
157   fail_unless (gst_element_set_state (audioconvert,
158           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
159       "could not set to playing");
160
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);
167
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);
175
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");
183   /* cleanup */
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);
189 }
190
191
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)
195
196 GST_START_TEST (test_int16)
197 {
198   /* stereo to mono */
199   {
200     gint16 in[] = { 16384, -256, 1024, 1024 };
201     gint16 out[] = { 8064, 1024 };
202
203     RUN_CONVERSION (in, get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE),
204         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
205   }
206   /* mono to stereo */
207   {
208     gint16 in[] = { 512, 1024 };
209     gint16 out[] = { 512, 512, 1024, 1024 };
210
211     RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
212         out, get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE));
213   }
214   /* signed -> unsigned */
215   {
216     gint16 in[] = { 0, -32767, 32767, -32768 };
217     guint16 out[] = { 32768, 1, 65535, 0 };
218
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));
223   }
224 }
225
226 GST_END_TEST;
227
228 GST_START_TEST (test_int_conversion)
229 {
230   /* 8 <-> 16 signed */
231   /* NOTE: if audioconvert was doing dithering we'd have a problem */
232   {
233     gint8 in[] = { 0, 1, 2, 127, -127 };
234     gint16 out[] = { 0, 256, 512, 32512, -32512 };
235
236     RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE),
237         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE)
238         );
239     RUN_CONVERSION (out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
240         in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
241         );
242   }
243   /* 16 -> 8 signed */
244   {
245     gint16 in[] = { 0, 255, 256, 257 };
246     gint8 out[] = { 0, 0, 1, 1 };
247
248     RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
249         out, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
250         );
251   }
252   /* 8 unsigned <-> 16 signed */
253   /* NOTE: if audioconvert was doing dithering we'd have a problem */
254   {
255     guint8 in[] = { 128, 129, 130, 255, 1 };
256     gint16 out[] = { 0, 256, 512, 32512, -32512 };
257     GstCaps *incaps, *outcaps;
258
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)
267         );
268   }
269   /* 8 <-> 24 signed */
270   /* NOTE: if audioconvert was doing dithering we'd have a problem */
271   {
272     gint8 in[] = { 0, 1, 127 };
273     guint8 out[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7f };
274
275     RUN_CONVERSION (in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE),
276         out, get_int_caps (1, "BYTE_ORDER", 24, 24, TRUE)
277         );
278     RUN_CONVERSION (out, get_int_caps (1, "BYTE_ORDER", 24, 24, TRUE),
279         in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
280         );
281   }
282 }
283
284 GST_END_TEST;
285
286
287 Suite *
288 audioconvert_suite (void)
289 {
290   Suite *s = suite_create ("audioconvert");
291   TCase *tc_chain = tcase_create ("general");
292
293   suite_add_tcase (s, tc_chain);
294   tcase_add_test (tc_chain, test_int16);
295   //tcase_add_test (tc_chain, test_int_conversion);
296
297   return s;
298 }
299
300 int
301 main (int argc, char **argv)
302 {
303   int nf;
304
305   Suite *s = audioconvert_suite ();
306   SRunner *sr = srunner_create (s);
307
308   gst_check_init (&argc, &argv);
309
310   srunner_run_all (sr, CK_NORMAL);
311   nf = srunner_ntests_failed (sr);
312   srunner_free (sr);
313
314   return nf;
315 }