Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #include <unistd.h>
27
28 #include <gst/base/gstbasetransform.h>
29 #include <gst/check/gstcheck.h>
30
31 gboolean have_eos = FALSE;
32
33 /* For ease of programming we use globals to keep refs for our floating
34  * src and sink pads we create; otherwise we always have to do get_pad,
35  * get_peer, and then remove references in every test function */
36 GstPad *mysrcpad, *mysinkpad;
37
38
39 #define AMPLIFY_CAPS_STRING    \
40     "audio/x-raw-int, "                 \
41     "channels = (int) 1, "              \
42     "rate = (int) 44100, "              \
43     "endianness = (int) BYTE_ORDER, "   \
44     "width = (int) 16, "                \
45     "depth = (int) 16, "                \
46     "signed = (bool) TRUE"
47
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
49     GST_PAD_SINK,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("audio/x-raw-int, "
52         "channels = (int) 1, "
53         "rate = (int) [ 1,  MAX ], "
54         "endianness = (int) BYTE_ORDER, "
55         "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE")
56     );
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("audio/x-raw-int, "
61         "channels = (int) 1, "
62         "rate = (int) [ 1,  MAX ], "
63         "endianness = (int) BYTE_ORDER, "
64         "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE")
65     );
66
67 static GstElement *
68 setup_amplify (void)
69 {
70   GstElement *amplify;
71
72   GST_DEBUG ("setup_amplify");
73   amplify = gst_check_setup_element ("audioamplify");
74   mysrcpad = gst_check_setup_src_pad (amplify, &srctemplate, NULL);
75   mysinkpad = gst_check_setup_sink_pad (amplify, &sinktemplate, NULL);
76   gst_pad_set_active (mysrcpad, TRUE);
77   gst_pad_set_active (mysinkpad, TRUE);
78
79   return amplify;
80 }
81
82 static void
83 cleanup_amplify (GstElement * amplify)
84 {
85   GST_DEBUG ("cleanup_amplify");
86
87   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
88   g_list_free (buffers);
89   buffers = NULL;
90
91   gst_pad_set_active (mysrcpad, FALSE);
92   gst_pad_set_active (mysinkpad, FALSE);
93   gst_check_teardown_src_pad (amplify);
94   gst_check_teardown_sink_pad (amplify);
95   gst_check_teardown_element (amplify);
96 }
97
98 GST_START_TEST (test_passthrough)
99 {
100   GstElement *amplify;
101   GstBuffer *inbuffer, *outbuffer;
102   GstCaps *caps;
103   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
104   gint16 *res;
105
106   amplify = setup_amplify ();
107   fail_unless (gst_element_set_state (amplify,
108           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
109       "could not set to playing");
110
111   inbuffer = gst_buffer_new_and_alloc (12);
112   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
113   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
114   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
115   gst_buffer_set_caps (inbuffer, caps);
116   gst_caps_unref (caps);
117   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
118
119   /* pushing gives away my reference ... */
120   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
121   /* ... but it ends up being collected on the global buffer list */
122   fail_unless_equals_int (g_list_length (buffers), 1);
123   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
124
125   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
126   GST_INFO
127       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
128       in[0], in[1], in[2], in[3], in[4], in[5], res[0], res[1], res[2], res[3],
129       res[4], res[5]);
130   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), in, 12) == 0);
131
132   /* cleanup */
133   cleanup_amplify (amplify);
134 }
135
136 GST_END_TEST;
137
138 GST_START_TEST (test_zero)
139 {
140   GstElement *amplify;
141   GstBuffer *inbuffer, *outbuffer;
142   GstCaps *caps;
143   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
144   gint16 out[6] = { 0, 0, 0, 0, 0, 0 };
145   gint16 *res;
146
147   amplify = setup_amplify ();
148   g_object_set (G_OBJECT (amplify), "amplification", 0.0, NULL);
149   fail_unless (gst_element_set_state (amplify,
150           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
151       "could not set to playing");
152
153   inbuffer = gst_buffer_new_and_alloc (12);
154   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
155   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
156   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
157   gst_buffer_set_caps (inbuffer, caps);
158   gst_caps_unref (caps);
159   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
160
161   /* pushing gives away my reference ... */
162   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
163   /* ... and puts a new buffer on the global list */
164   fail_unless_equals_int (g_list_length (buffers), 1);
165   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
166
167   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
168   GST_INFO
169       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
170       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
171       res[3], res[4], res[5]);
172   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 12) == 0);
173
174   /* cleanup */
175   cleanup_amplify (amplify);
176 }
177
178 GST_END_TEST;
179
180 GST_START_TEST (test_050_clip)
181 {
182   GstElement *amplify;
183   GstBuffer *inbuffer, *outbuffer;
184   GstCaps *caps;
185   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
186   gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
187   gint16 *res;
188
189   amplify = setup_amplify ();
190   g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
191   fail_unless (gst_element_set_state (amplify,
192           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
193       "could not set to playing");
194
195   inbuffer = gst_buffer_new_and_alloc (12);
196   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
197   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
198   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
199   gst_buffer_set_caps (inbuffer, caps);
200   gst_caps_unref (caps);
201   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
202
203   /* pushing gives away my reference ... */
204   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
205   /* ... and puts a new buffer on the global list */
206   fail_unless_equals_int (g_list_length (buffers), 1);
207   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
208
209   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
210   GST_INFO
211       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
212       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
213       res[3], res[4], res[5]);
214   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 12) == 0);
215
216   /* cleanup */
217   cleanup_amplify (amplify);
218 }
219
220 GST_END_TEST;
221
222 GST_START_TEST (test_200_clip)
223 {
224   GstElement *amplify;
225   GstBuffer *inbuffer, *outbuffer;
226   GstCaps *caps;
227   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
228   gint16 out[6] = { G_MAXINT16, -32768, 512, -256, 0, G_MININT16 };
229   gint16 *res;
230
231   amplify = setup_amplify ();
232   g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
233   fail_unless (gst_element_set_state (amplify,
234           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
235       "could not set to playing");
236
237   inbuffer = gst_buffer_new_and_alloc (12);
238   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
239   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
240   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
241   gst_buffer_set_caps (inbuffer, caps);
242   gst_caps_unref (caps);
243   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
244
245   /* pushing gives away my reference ... */
246   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
247   /* ... and puts a new buffer on the global list */
248   fail_unless_equals_int (g_list_length (buffers), 1);
249   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
250
251   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
252   GST_INFO
253       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
254       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
255       res[3], res[4], res[5]);
256   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 12) == 0);
257
258   /* cleanup */
259   cleanup_amplify (amplify);
260 }
261
262 GST_END_TEST;
263
264 GST_START_TEST (test_050_wrap_negative)
265 {
266   GstElement *amplify;
267   GstBuffer *inbuffer, *outbuffer;
268   GstCaps *caps;
269   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
270   gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
271   gint16 *res;
272
273   amplify = setup_amplify ();
274   g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
275   g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
276   fail_unless (gst_element_set_state (amplify,
277           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
278       "could not set to playing");
279
280   inbuffer = gst_buffer_new_and_alloc (12);
281   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
282   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
283   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
284   gst_buffer_set_caps (inbuffer, caps);
285   gst_caps_unref (caps);
286   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
287
288   /* pushing gives away my reference ... */
289   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
290   /* ... and puts a new buffer on the global list */
291   fail_unless_equals_int (g_list_length (buffers), 1);
292   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
293
294   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
295   GST_INFO
296       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
297       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
298       res[3], res[4], res[5]);
299   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 12) == 0);
300
301   /* cleanup */
302   cleanup_amplify (amplify);
303 }
304
305 GST_END_TEST;
306
307 GST_START_TEST (test_200_wrap_negative)
308 {
309   GstElement *amplify;
310   GstBuffer *inbuffer, *outbuffer;
311   GstCaps *caps;
312   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
313   gint16 out[6] = { -16384, -32768, 512, -256, 0, 16384 };
314   gint16 *res;
315
316   amplify = setup_amplify ();
317   g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
318   g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
319   fail_unless (gst_element_set_state (amplify,
320           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
321       "could not set to playing");
322
323   inbuffer = gst_buffer_new_and_alloc (12);
324   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
325   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
326   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
327   gst_buffer_set_caps (inbuffer, caps);
328   gst_caps_unref (caps);
329   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
330
331   /* pushing gives away my reference ... */
332   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
333   /* ... and puts a new buffer on the global list */
334   fail_unless_equals_int (g_list_length (buffers), 1);
335   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
336
337   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
338   GST_INFO
339       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
340       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
341       res[3], res[4], res[5]);
342   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 12) == 0);
343
344   /* cleanup */
345   cleanup_amplify (amplify);
346 }
347
348 GST_END_TEST;
349
350 GST_START_TEST (test_050_wrap_positive)
351 {
352   GstElement *amplify;
353   GstBuffer *inbuffer, *outbuffer;
354   GstCaps *caps;
355   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
356   gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
357   gint16 *res;
358
359   amplify = setup_amplify ();
360   g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
361   g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
362   fail_unless (gst_element_set_state (amplify,
363           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
364       "could not set to playing");
365
366   inbuffer = gst_buffer_new_and_alloc (12);
367   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
368   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
369   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
370   gst_buffer_set_caps (inbuffer, caps);
371   gst_caps_unref (caps);
372   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
373
374   /* pushing gives away my reference ... */
375   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
376   /* ... and puts a new buffer on the global list */
377   fail_unless_equals_int (g_list_length (buffers), 1);
378   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
379
380   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
381   GST_INFO
382       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
383       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
384       res[3], res[4], res[5]);
385   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 12) == 0);
386
387   /* cleanup */
388   cleanup_amplify (amplify);
389 }
390
391 GST_END_TEST;
392
393 GST_START_TEST (test_200_wrap_positive)
394 {
395   GstElement *amplify;
396   GstBuffer *inbuffer, *outbuffer;
397   GstCaps *caps;
398   gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
399   gint16 out[6] = { 16382, -32768, 512, -256, 0, -16384 };
400   gint16 *res;
401
402   amplify = setup_amplify ();
403   g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
404   g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
405   fail_unless (gst_element_set_state (amplify,
406           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
407       "could not set to playing");
408
409   inbuffer = gst_buffer_new_and_alloc (12);
410   memcpy (GST_BUFFER_DATA (inbuffer), in, 12);
411   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0);
412   caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
413   gst_buffer_set_caps (inbuffer, caps);
414   gst_caps_unref (caps);
415   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
416
417   /* pushing gives away my reference ... */
418   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
419   /* ... and puts a new buffer on the global list */
420   fail_unless_equals_int (g_list_length (buffers), 1);
421   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
422
423   res = (gint16 *) GST_BUFFER_DATA (outbuffer);
424   GST_INFO
425       ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
426       out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
427       res[3], res[4], res[5]);
428   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 12) == 0);
429
430   /* cleanup */
431   cleanup_amplify (amplify);
432 }
433
434 GST_END_TEST;
435
436 static Suite *
437 amplify_suite (void)
438 {
439   Suite *s = suite_create ("amplify");
440   TCase *tc_chain = tcase_create ("general");
441
442   suite_add_tcase (s, tc_chain);
443   tcase_add_test (tc_chain, test_passthrough);
444   tcase_add_test (tc_chain, test_zero);
445   tcase_add_test (tc_chain, test_050_clip);
446   tcase_add_test (tc_chain, test_200_clip);
447   tcase_add_test (tc_chain, test_050_wrap_negative);
448   tcase_add_test (tc_chain, test_200_wrap_negative);
449   tcase_add_test (tc_chain, test_050_wrap_positive);
450   tcase_add_test (tc_chain, test_200_wrap_positive);
451   return s;
452 }
453
454 int
455 main (int argc, char **argv)
456 {
457   int nf;
458
459   Suite *s = amplify_suite ();
460   SRunner *sr = srunner_create (s);
461
462   gst_check_init (&argc, &argv);
463
464   srunner_run_all (sr, CK_NORMAL);
465   nf = srunner_ntests_failed (sr);
466   srunner_free (sr);
467
468   return nf;
469 }