tests: Remove pointless unistd.h include
[platform/upstream/gst-plugins-good.git] / tests / check / elements / audioamplify.c
1 /* GStreamer
2  *
3  * unit test for audioamplify
4  *
5  * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
6  *
7  * Greatly based on the audiopanorama unit test
8  * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25
26 #include <gst/base/gstbasetransform.h>
27 #include <gst/check/gstcheck.h>
28 #include <gst/audio/audio.h>
29
30 gboolean have_eos = FALSE;
31
32 /* For ease of programming we use globals to keep refs for our floating
33  * src and sink pads we create; otherwise we always have to do get_pad,
34  * get_peer, and then remove references in every test function */
35 GstPad *mysrcpad, *mysinkpad;
36
37
38 #define AMPLIFY_CAPS_STRING    \
39     "audio/x-raw, "                     \
40     "channels = (int) 1, "              \
41     "rate = (int) 44100, "              \
42     "layout = (string) interleaved, "   \
43     "format = (string) " GST_AUDIO_NE(S16)
44
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("audio/x-raw, "
49         "channels = (int) 1, "
50         "rate = (int) [ 1,  MAX ], "
51         "layout = (string) interleaved, "
52         "format = (string) " GST_AUDIO_NE (S16)));
53 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
54     GST_PAD_SRC,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("audio/x-raw, "
57         "channels = (int) 1, "
58         "rate = (int) [ 1,  MAX ], "
59         "layout = (string) interleaved, "
60         "format = (string) " GST_AUDIO_NE (S16)));
61
62 static GstElement *
63 setup_amplify (void)
64 {
65   GstElement *amplify;
66
67   GST_DEBUG ("setup_amplify");
68   amplify = gst_check_setup_element ("audioamplify");
69   mysrcpad = gst_check_setup_src_pad (amplify, &srctemplate);
70   mysinkpad = gst_check_setup_sink_pad (amplify, &sinktemplate);
71   gst_pad_set_active (mysrcpad, TRUE);
72   gst_pad_set_active (mysinkpad, TRUE);
73
74   return amplify;
75 }
76
77 static void
78 cleanup_amplify (GstElement * amplify)
79 {
80   GST_DEBUG ("cleanup_amplify");
81
82   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
83   g_list_free (buffers);
84   buffers = NULL;
85
86   gst_pad_set_active (mysrcpad, FALSE);
87   gst_pad_set_active (mysinkpad, FALSE);
88   gst_check_teardown_src_pad (amplify);
89   gst_check_teardown_sink_pad (amplify);
90   gst_check_teardown_element (amplify);
91 }
92
93 GST_START_TEST (test_passthrough)
94 {
95   GstElement *amplify;
96   GstBuffer *inbuffer, *outbuffer;
97   GstCaps *caps;
98   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
99   gint16 res[6];
100
101   amplify = setup_amplify ();
102   fail_unless (gst_element_set_state (amplify,
103           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
104       "could not set to playing");
105
106   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
107   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
108   gst_caps_unref (caps);
109
110   inbuffer =
111       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
112       NULL, NULL);
113   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
114   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
115
116   /* pushing gives away my reference ... */
117   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
118   /* ... but it ends up being collected on the global buffer list */
119   fail_unless_equals_int (g_list_length (buffers), 1);
120   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
121
122   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
123   GST_INFO
124       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
125       in[0], in[1], in[2], in[3], in[4], in[5], res[0], res[1], res[2], res[3],
126       res[4], res[5]);
127   fail_unless (gst_buffer_memcmp (outbuffer, 0, in, 12) == 0);
128
129   /* cleanup */
130   cleanup_amplify (amplify);
131 }
132
133 GST_END_TEST;
134
135 GST_START_TEST (test_zero)
136 {
137   GstElement *amplify;
138   GstBuffer *inbuffer, *outbuffer;
139   GstCaps *caps;
140   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
141   gint16 out[6] = { 0, 0, 0, 0, 0, 0 };
142   gint16 res[6];
143
144   amplify = setup_amplify ();
145   g_object_set (G_OBJECT (amplify), "amplification", 0.0, NULL);
146   fail_unless (gst_element_set_state (amplify,
147           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
148       "could not set to playing");
149
150   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
151   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
152   gst_caps_unref (caps);
153
154   inbuffer =
155       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
156       NULL, NULL);
157   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
158   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
159
160   /* pushing gives away my reference ... */
161   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
162   /* ... and puts a new buffer on the global list */
163   fail_unless_equals_int (g_list_length (buffers), 1);
164   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
165
166   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
167   GST_INFO
168       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
169       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
170       res[3], res[4], res[5]);
171   fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
172
173   /* cleanup */
174   cleanup_amplify (amplify);
175 }
176
177 GST_END_TEST;
178
179 GST_START_TEST (test_050_clip)
180 {
181   GstElement *amplify;
182   GstBuffer *inbuffer, *outbuffer;
183   GstCaps *caps;
184   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
185   gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
186   gint16 res[6];
187
188   amplify = setup_amplify ();
189   g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
190   fail_unless (gst_element_set_state (amplify,
191           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
192       "could not set to playing");
193
194   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
195   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
196   gst_caps_unref (caps);
197
198   inbuffer =
199       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
200       NULL, NULL);
201   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
202   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
203
204   /* pushing gives away my reference ... */
205   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
206   /* ... and puts a new buffer on the global list */
207   fail_unless_equals_int (g_list_length (buffers), 1);
208   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
209
210   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
211   GST_INFO
212       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
213       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
214       res[3], res[4], res[5]);
215   fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
216
217   /* cleanup */
218   cleanup_amplify (amplify);
219 }
220
221 GST_END_TEST;
222
223 GST_START_TEST (test_200_clip)
224 {
225   GstElement *amplify;
226   GstBuffer *inbuffer, *outbuffer;
227   GstCaps *caps;
228   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
229   gint16 out[6] = { G_MAXINT16, -32768, 512, -256, 0, G_MININT16 };
230   gint16 res[6];
231
232   amplify = setup_amplify ();
233   g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
234   fail_unless (gst_element_set_state (amplify,
235           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
236       "could not set to playing");
237
238   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
239   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
240   gst_caps_unref (caps);
241
242   inbuffer =
243       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
244       NULL, NULL);
245   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
246   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
247
248   /* pushing gives away my reference ... */
249   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
250   /* ... and puts a new buffer on the global list */
251   fail_unless_equals_int (g_list_length (buffers), 1);
252   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
253
254   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
255   GST_INFO
256       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
257       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
258       res[3], res[4], res[5]);
259   fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
260
261   /* cleanup */
262   cleanup_amplify (amplify);
263 }
264
265 GST_END_TEST;
266
267 GST_START_TEST (test_050_wrap_negative)
268 {
269   GstElement *amplify;
270   GstBuffer *inbuffer, *outbuffer;
271   GstCaps *caps;
272   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
273   gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
274   gint16 res[6];
275
276   amplify = setup_amplify ();
277   g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
278   g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
279   fail_unless (gst_element_set_state (amplify,
280           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
281       "could not set to playing");
282
283   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
284   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
285   gst_caps_unref (caps);
286
287   inbuffer =
288       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
289       NULL, NULL);
290   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
291   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
292
293   /* pushing gives away my reference ... */
294   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
295   /* ... and puts a new buffer on the global list */
296   fail_unless_equals_int (g_list_length (buffers), 1);
297   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
298
299   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
300   GST_INFO
301       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
302       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
303       res[3], res[4], res[5]);
304   fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
305
306   /* cleanup */
307   cleanup_amplify (amplify);
308 }
309
310 GST_END_TEST;
311
312 GST_START_TEST (test_200_wrap_negative)
313 {
314   GstElement *amplify;
315   GstBuffer *inbuffer, *outbuffer;
316   GstCaps *caps;
317   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
318   gint16 out[6] = { -16384, -32768, 512, -256, 0, 16384 };
319   gint16 res[6];
320
321   amplify = setup_amplify ();
322   g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
323   g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
324   fail_unless (gst_element_set_state (amplify,
325           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
326       "could not set to playing");
327
328   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
329   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
330   gst_caps_unref (caps);
331
332   inbuffer =
333       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
334       NULL, NULL);
335   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
336   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
337
338   /* pushing gives away my reference ... */
339   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
340   /* ... and puts a new buffer on the global list */
341   fail_unless_equals_int (g_list_length (buffers), 1);
342   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
343
344   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
345   GST_INFO
346       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
347       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
348       res[3], res[4], res[5]);
349   fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
350
351   /* cleanup */
352   cleanup_amplify (amplify);
353 }
354
355 GST_END_TEST;
356
357 GST_START_TEST (test_050_wrap_positive)
358 {
359   GstElement *amplify;
360   GstBuffer *inbuffer, *outbuffer;
361   GstCaps *caps;
362   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
363   gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
364   gint16 res[6];
365
366   amplify = setup_amplify ();
367   g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
368   g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
369   fail_unless (gst_element_set_state (amplify,
370           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
371       "could not set to playing");
372
373   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
374   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
375   gst_caps_unref (caps);
376
377   inbuffer =
378       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
379       NULL, NULL);
380   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
381   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
382
383   /* pushing gives away my reference ... */
384   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
385   /* ... and puts a new buffer on the global list */
386   fail_unless_equals_int (g_list_length (buffers), 1);
387   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
388
389   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
390   GST_INFO
391       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
392       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
393       res[3], res[4], res[5]);
394   fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
395
396   /* cleanup */
397   cleanup_amplify (amplify);
398 }
399
400 GST_END_TEST;
401
402 GST_START_TEST (test_200_wrap_positive)
403 {
404   GstElement *amplify;
405   GstBuffer *inbuffer, *outbuffer;
406   GstCaps *caps;
407   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
408   gint16 out[6] = { 16382, -32768, 512, -256, 0, -16384 };
409   gint16 res[6];
410
411   amplify = setup_amplify ();
412   g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
413   g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
414   fail_unless (gst_element_set_state (amplify,
415           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
416       "could not set to playing");
417
418   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
419   gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
420   gst_caps_unref (caps);
421
422   inbuffer =
423       gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
424       NULL, NULL);
425   fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
426   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
427
428   /* pushing gives away my reference ... */
429   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
430   /* ... and puts a new buffer on the global list */
431   fail_unless_equals_int (g_list_length (buffers), 1);
432   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
433
434   fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
435   GST_INFO
436       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
437       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
438       res[3], res[4], res[5]);
439   fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
440
441   /* cleanup */
442   cleanup_amplify (amplify);
443 }
444
445 GST_END_TEST;
446
447 static Suite *
448 amplify_suite (void)
449 {
450   Suite *s = suite_create ("amplify");
451   TCase *tc_chain = tcase_create ("general");
452
453   suite_add_tcase (s, tc_chain);
454   tcase_add_test (tc_chain, test_passthrough);
455   tcase_add_test (tc_chain, test_zero);
456   tcase_add_test (tc_chain, test_050_clip);
457   tcase_add_test (tc_chain, test_200_clip);
458   tcase_add_test (tc_chain, test_050_wrap_negative);
459   tcase_add_test (tc_chain, test_200_wrap_negative);
460   tcase_add_test (tc_chain, test_050_wrap_positive);
461   tcase_add_test (tc_chain, test_200_wrap_positive);
462   return s;
463 }
464
465 GST_CHECK_MAIN (amplify);