9911301ed7f50994ae4984f27c7735fb27483dd9
[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  * Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <unistd.h>
25
26 #include <gst/floatcast/floatcast.h>
27 #include <gst/check/gstcheck.h>
28 #include <gst/audio/multichannel.h>
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 static GstPad *mysrcpad, *mysinkpad;
34
35 #define CONVERT_CAPS_TEMPLATE_STRING    \
36   "audio/x-raw-float, " \
37     "rate = (int) [ 1, MAX ], " \
38     "channels = (int) [ 1, MAX ], " \
39     "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
40     "width = (int) { 32, 64 };" \
41   "audio/x-raw-int, " \
42     "rate = (int) [ 1, MAX ], " \
43     "channels = (int) [ 1, MAX ], " \
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, MAX ], " \
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, MAX ], " \
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, MAX ], " \
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 static 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   g_object_set (G_OBJECT (audioconvert), "dithering", 0, NULL);
90   g_object_set (G_OBJECT (audioconvert), "noise-shaping", 0, NULL);
91   mysrcpad = gst_check_setup_src_pad (audioconvert, &srctemplate, NULL);
92   mysinkpad = gst_check_setup_sink_pad (audioconvert, &sinktemplate, NULL);
93   /* this installs a getcaps func that will always return the caps we set
94    * later */
95   gst_pad_use_fixed_caps (mysinkpad);
96
97   gst_pad_set_active (mysrcpad, TRUE);
98   gst_pad_set_active (mysinkpad, TRUE);
99
100   gst_pad_set_caps (mysinkpad, outcaps);
101   gst_caps_unref (outcaps);
102   outcaps = gst_pad_get_current_caps (mysinkpad);
103   fail_unless (gst_caps_is_fixed (outcaps));
104   gst_caps_unref (outcaps);
105
106   return audioconvert;
107 }
108
109 static void
110 cleanup_audioconvert (GstElement * audioconvert)
111 {
112   GST_DEBUG ("cleanup_audioconvert");
113
114   gst_pad_set_active (mysrcpad, FALSE);
115   gst_pad_set_active (mysinkpad, FALSE);
116   gst_check_teardown_src_pad (audioconvert);
117   gst_check_teardown_sink_pad (audioconvert);
118   gst_check_teardown_element (audioconvert);
119 }
120
121 /* returns a newly allocated caps */
122 static GstCaps *
123 get_int_caps (guint channels, const gchar * endianness, guint width,
124     guint depth, gboolean signedness)
125 {
126   GstCaps *caps;
127   gchar *string;
128
129   string = g_strdup_printf ("audio/x-raw-int, "
130       "rate = (int) 44100, "
131       "channels = (int) %d, "
132       "endianness = (int) %s, "
133       "width = (int) %d, "
134       "depth = (int) %d, "
135       "signed = (boolean) %s ",
136       channels, endianness, width, depth, signedness ? "true" : "false");
137   GST_DEBUG ("creating caps from %s", string);
138   caps = gst_caps_from_string (string);
139   g_free (string);
140   fail_unless (caps != NULL);
141   GST_DEBUG ("returning caps %p", caps);
142   return caps;
143 }
144
145 /* returns a newly allocated caps */
146 static GstCaps *
147 get_float_caps (guint channels, const gchar * endianness, guint width)
148 {
149   GstCaps *caps;
150   gchar *string;
151
152   string = g_strdup_printf ("audio/x-raw-float, "
153       "rate = (int) 44100, "
154       "channels = (int) %d, "
155       "endianness = (int) %s, "
156       "width = (int) %d ", channels, endianness, width);
157   GST_DEBUG ("creating caps from %s", string);
158   caps = gst_caps_from_string (string);
159   g_free (string);
160   fail_unless (caps != NULL);
161   GST_DEBUG ("returning caps %p", caps);
162   return caps;
163 }
164
165 /* Copied from vorbis; the particular values used don't matter */
166 static GstAudioChannelPosition channelpositions[][6] = {
167   {                             /* Mono */
168       GST_AUDIO_CHANNEL_POSITION_FRONT_MONO},
169   {                             /* Stereo */
170         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
171       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
172   {                             /* Stereo + Centre */
173         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
174         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
175       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
176   {                             /* Quadraphonic */
177         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
178         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
179         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
180         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
181       },
182   {                             /* Stereo + Centre + rear stereo */
183         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
184         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
185         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
186         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
187         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
188       },
189   {                             /* Full 5.1 Surround */
190         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
191         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
192         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
193         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
194         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
195         GST_AUDIO_CHANNEL_POSITION_LFE,
196       }
197 };
198
199 /* these are a bunch of random positions, they are mostly just
200  * different from the ones above, don't use elsewhere */
201 static GstAudioChannelPosition mixed_up_positions[][6] = {
202   {
203       GST_AUDIO_CHANNEL_POSITION_FRONT_MONO},
204   {
205         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
206       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT},
207   {
208         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
209         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
210       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT},
211   {
212         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
213         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
214         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
215         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
216       },
217   {
218         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
219         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
220         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
221         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
222         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
223       },
224   {
225         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
226         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
227         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
228         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
229         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
230         GST_AUDIO_CHANNEL_POSITION_LFE,
231       }
232 };
233
234 /* we get this when recording from a soundcard with lots of input channels */
235 static GstAudioChannelPosition undefined_positions[][15] = {
236   {
237       GST_AUDIO_CHANNEL_POSITION_NONE},
238   {
239         GST_AUDIO_CHANNEL_POSITION_NONE,
240       GST_AUDIO_CHANNEL_POSITION_NONE},
241   {
242         GST_AUDIO_CHANNEL_POSITION_NONE,
243         GST_AUDIO_CHANNEL_POSITION_NONE,
244       GST_AUDIO_CHANNEL_POSITION_NONE},
245   {
246         GST_AUDIO_CHANNEL_POSITION_NONE,
247         GST_AUDIO_CHANNEL_POSITION_NONE,
248         GST_AUDIO_CHANNEL_POSITION_NONE,
249       GST_AUDIO_CHANNEL_POSITION_NONE},
250   {
251         GST_AUDIO_CHANNEL_POSITION_NONE,
252         GST_AUDIO_CHANNEL_POSITION_NONE,
253         GST_AUDIO_CHANNEL_POSITION_NONE,
254         GST_AUDIO_CHANNEL_POSITION_NONE,
255       GST_AUDIO_CHANNEL_POSITION_NONE},
256   {
257         GST_AUDIO_CHANNEL_POSITION_NONE,
258         GST_AUDIO_CHANNEL_POSITION_NONE,
259         GST_AUDIO_CHANNEL_POSITION_NONE,
260         GST_AUDIO_CHANNEL_POSITION_NONE,
261         GST_AUDIO_CHANNEL_POSITION_NONE,
262       GST_AUDIO_CHANNEL_POSITION_NONE},
263   {
264         GST_AUDIO_CHANNEL_POSITION_NONE,
265         GST_AUDIO_CHANNEL_POSITION_NONE,
266         GST_AUDIO_CHANNEL_POSITION_NONE,
267         GST_AUDIO_CHANNEL_POSITION_NONE,
268         GST_AUDIO_CHANNEL_POSITION_NONE,
269         GST_AUDIO_CHANNEL_POSITION_NONE,
270       GST_AUDIO_CHANNEL_POSITION_NONE},
271   {
272         GST_AUDIO_CHANNEL_POSITION_NONE,
273         GST_AUDIO_CHANNEL_POSITION_NONE,
274         GST_AUDIO_CHANNEL_POSITION_NONE,
275         GST_AUDIO_CHANNEL_POSITION_NONE,
276         GST_AUDIO_CHANNEL_POSITION_NONE,
277         GST_AUDIO_CHANNEL_POSITION_NONE,
278         GST_AUDIO_CHANNEL_POSITION_NONE,
279       GST_AUDIO_CHANNEL_POSITION_NONE},
280   {
281         GST_AUDIO_CHANNEL_POSITION_NONE,
282         GST_AUDIO_CHANNEL_POSITION_NONE,
283         GST_AUDIO_CHANNEL_POSITION_NONE,
284         GST_AUDIO_CHANNEL_POSITION_NONE,
285         GST_AUDIO_CHANNEL_POSITION_NONE,
286         GST_AUDIO_CHANNEL_POSITION_NONE,
287         GST_AUDIO_CHANNEL_POSITION_NONE,
288         GST_AUDIO_CHANNEL_POSITION_NONE,
289       GST_AUDIO_CHANNEL_POSITION_NONE},
290   {
291         GST_AUDIO_CHANNEL_POSITION_NONE,
292         GST_AUDIO_CHANNEL_POSITION_NONE,
293         GST_AUDIO_CHANNEL_POSITION_NONE,
294         GST_AUDIO_CHANNEL_POSITION_NONE,
295         GST_AUDIO_CHANNEL_POSITION_NONE,
296         GST_AUDIO_CHANNEL_POSITION_NONE,
297         GST_AUDIO_CHANNEL_POSITION_NONE,
298         GST_AUDIO_CHANNEL_POSITION_NONE,
299         GST_AUDIO_CHANNEL_POSITION_NONE,
300       GST_AUDIO_CHANNEL_POSITION_NONE},
301   {
302         GST_AUDIO_CHANNEL_POSITION_NONE,
303         GST_AUDIO_CHANNEL_POSITION_NONE,
304         GST_AUDIO_CHANNEL_POSITION_NONE,
305         GST_AUDIO_CHANNEL_POSITION_NONE,
306         GST_AUDIO_CHANNEL_POSITION_NONE,
307         GST_AUDIO_CHANNEL_POSITION_NONE,
308         GST_AUDIO_CHANNEL_POSITION_NONE,
309         GST_AUDIO_CHANNEL_POSITION_NONE,
310         GST_AUDIO_CHANNEL_POSITION_NONE,
311         GST_AUDIO_CHANNEL_POSITION_NONE,
312       GST_AUDIO_CHANNEL_POSITION_NONE},
313   {
314         GST_AUDIO_CHANNEL_POSITION_NONE,
315         GST_AUDIO_CHANNEL_POSITION_NONE,
316         GST_AUDIO_CHANNEL_POSITION_NONE,
317         GST_AUDIO_CHANNEL_POSITION_NONE,
318         GST_AUDIO_CHANNEL_POSITION_NONE,
319         GST_AUDIO_CHANNEL_POSITION_NONE,
320         GST_AUDIO_CHANNEL_POSITION_NONE,
321         GST_AUDIO_CHANNEL_POSITION_NONE,
322         GST_AUDIO_CHANNEL_POSITION_NONE,
323         GST_AUDIO_CHANNEL_POSITION_NONE,
324         GST_AUDIO_CHANNEL_POSITION_NONE,
325       GST_AUDIO_CHANNEL_POSITION_NONE},
326   {
327         GST_AUDIO_CHANNEL_POSITION_NONE,
328         GST_AUDIO_CHANNEL_POSITION_NONE,
329         GST_AUDIO_CHANNEL_POSITION_NONE,
330         GST_AUDIO_CHANNEL_POSITION_NONE,
331         GST_AUDIO_CHANNEL_POSITION_NONE,
332         GST_AUDIO_CHANNEL_POSITION_NONE,
333         GST_AUDIO_CHANNEL_POSITION_NONE,
334         GST_AUDIO_CHANNEL_POSITION_NONE,
335         GST_AUDIO_CHANNEL_POSITION_NONE,
336         GST_AUDIO_CHANNEL_POSITION_NONE,
337         GST_AUDIO_CHANNEL_POSITION_NONE,
338         GST_AUDIO_CHANNEL_POSITION_NONE,
339       GST_AUDIO_CHANNEL_POSITION_NONE},
340   {
341         GST_AUDIO_CHANNEL_POSITION_NONE,
342         GST_AUDIO_CHANNEL_POSITION_NONE,
343         GST_AUDIO_CHANNEL_POSITION_NONE,
344         GST_AUDIO_CHANNEL_POSITION_NONE,
345         GST_AUDIO_CHANNEL_POSITION_NONE,
346         GST_AUDIO_CHANNEL_POSITION_NONE,
347         GST_AUDIO_CHANNEL_POSITION_NONE,
348         GST_AUDIO_CHANNEL_POSITION_NONE,
349         GST_AUDIO_CHANNEL_POSITION_NONE,
350         GST_AUDIO_CHANNEL_POSITION_NONE,
351         GST_AUDIO_CHANNEL_POSITION_NONE,
352         GST_AUDIO_CHANNEL_POSITION_NONE,
353         GST_AUDIO_CHANNEL_POSITION_NONE,
354       GST_AUDIO_CHANNEL_POSITION_NONE},
355   {
356         GST_AUDIO_CHANNEL_POSITION_NONE,
357         GST_AUDIO_CHANNEL_POSITION_NONE,
358         GST_AUDIO_CHANNEL_POSITION_NONE,
359         GST_AUDIO_CHANNEL_POSITION_NONE,
360         GST_AUDIO_CHANNEL_POSITION_NONE,
361         GST_AUDIO_CHANNEL_POSITION_NONE,
362         GST_AUDIO_CHANNEL_POSITION_NONE,
363         GST_AUDIO_CHANNEL_POSITION_NONE,
364         GST_AUDIO_CHANNEL_POSITION_NONE,
365         GST_AUDIO_CHANNEL_POSITION_NONE,
366         GST_AUDIO_CHANNEL_POSITION_NONE,
367         GST_AUDIO_CHANNEL_POSITION_NONE,
368         GST_AUDIO_CHANNEL_POSITION_NONE,
369         GST_AUDIO_CHANNEL_POSITION_NONE,
370       GST_AUDIO_CHANNEL_POSITION_NONE}
371 };
372
373 static void
374 set_channel_positions (GstCaps * caps, int channels,
375     GstAudioChannelPosition * channelpositions)
376 {
377   GValue chanpos = { 0 };
378   GValue pos = { 0 };
379   GstStructure *structure = gst_caps_get_structure (caps, 0);
380   int c;
381
382   g_value_init (&chanpos, GST_TYPE_ARRAY);
383   g_value_init (&pos, GST_TYPE_AUDIO_CHANNEL_POSITION);
384
385   for (c = 0; c < channels; c++) {
386     g_value_set_enum (&pos, channelpositions[c]);
387     gst_value_array_append_value (&chanpos, &pos);
388   }
389   g_value_unset (&pos);
390
391   gst_structure_set_value (structure, "channel-positions", &chanpos);
392   g_value_unset (&chanpos);
393 }
394
395 /* For channels > 2, caps have to have channel positions. This adds some simple
396  * ones. Only implemented for channels between 1 and 6.
397  */
398 static GstCaps *
399 get_float_mc_caps (guint channels, const gchar * endianness, guint width,
400     gboolean mixed_up_layout)
401 {
402   GstCaps *caps = get_float_caps (channels, endianness, width);
403
404   if (channels <= 6) {
405     if (mixed_up_layout)
406       set_channel_positions (caps, channels, mixed_up_positions[channels - 1]);
407     else
408       set_channel_positions (caps, channels, channelpositions[channels - 1]);
409   }
410
411   return caps;
412 }
413
414 static GstCaps *
415 get_int_mc_caps (guint channels, const gchar * endianness, guint width,
416     guint depth, gboolean signedness, gboolean mixed_up_layout)
417 {
418   GstCaps *caps = get_int_caps (channels, endianness, width, depth, signedness);
419
420   if (channels <= 6) {
421     if (mixed_up_layout)
422       set_channel_positions (caps, channels, mixed_up_positions[channels - 1]);
423     else
424       set_channel_positions (caps, channels, channelpositions[channels - 1]);
425   }
426
427   return caps;
428 }
429
430 /* eats the refs to the caps */
431 static void
432 verify_convert (const gchar * which, void *in, int inlength,
433     GstCaps * incaps, void *out, int outlength, GstCaps * outcaps,
434     GstFlowReturn expected_flow)
435 {
436   GstBuffer *inbuffer, *outbuffer;
437   GstElement *audioconvert;
438
439   GST_DEBUG ("verifying conversion %s", which);
440   GST_DEBUG ("incaps: %" GST_PTR_FORMAT, incaps);
441   GST_DEBUG ("outcaps: %" GST_PTR_FORMAT, outcaps);
442   ASSERT_CAPS_REFCOUNT (incaps, "incaps", 1);
443   ASSERT_CAPS_REFCOUNT (outcaps, "outcaps", 1);
444   audioconvert = setup_audioconvert (outcaps);
445   ASSERT_CAPS_REFCOUNT (outcaps, "outcaps", 1);
446
447   fail_unless (gst_element_set_state (audioconvert,
448           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
449       "could not set to playing");
450
451   gst_pad_push_event (mysrcpad, gst_event_new_caps (incaps));
452
453   GST_DEBUG ("Creating buffer of %d bytes", inlength);
454   inbuffer = gst_buffer_new_and_alloc (inlength);
455   gst_buffer_fill (inbuffer, 0, in, inlength);
456   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
457
458   /* pushing gives away my reference ... */
459   GST_DEBUG ("push it");
460   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), expected_flow);
461   GST_DEBUG ("pushed it");
462
463   if (expected_flow != GST_FLOW_OK)
464     goto done;
465
466   /* ... and puts a new buffer on the global list */
467   fail_unless (g_list_length (buffers) == 1);
468   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
469
470   ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
471   fail_unless_equals_int (gst_buffer_get_size (outbuffer), outlength);
472
473   gst_check_buffer_data (outbuffer, out, outlength);
474 #if 0
475   if (memcmp (GST_BUFFER_DATA (outbuffer), out, outlength) != 0) {
476     g_print ("\nInput data:\n");
477     gst_util_dump_mem (in, inlength);
478     g_print ("\nConverted data:\n");
479     gst_util_dump_mem (GST_BUFFER_DATA (outbuffer), outlength);
480     g_print ("\nExpected data:\n");
481     gst_util_dump_mem (out, outlength);
482   }
483   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, outlength) == 0,
484       "failed converting %s", which);
485 #endif
486
487   /* make sure that the channel positions are not lost */
488   {
489     GstStructure *in_s, *out_s;
490     gint out_chans;
491
492     in_s = gst_caps_get_structure (incaps, 0);
493     out_s = gst_caps_get_structure (gst_pad_get_current_caps (mysinkpad), 0);
494     fail_unless (gst_structure_get_int (out_s, "channels", &out_chans));
495
496     /* positions for 1 and 2 channels are implicit if not provided */
497     if (out_chans > 2 && gst_structure_has_field (in_s, "channel-positions")) {
498       if (!gst_structure_has_field (out_s, "channel-positions")) {
499         g_error ("Channel layout got lost somewhere:\n\nIns : %s\nOuts: %s\n",
500             gst_structure_to_string (in_s), gst_structure_to_string (out_s));
501       }
502     }
503   }
504
505   buffers = g_list_remove (buffers, outbuffer);
506   gst_buffer_unref (outbuffer);
507
508 done:
509   fail_unless (gst_element_set_state (audioconvert,
510           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
511   /* cleanup */
512   GST_DEBUG ("cleanup audioconvert");
513   cleanup_audioconvert (audioconvert);
514   GST_DEBUG ("cleanup, unref incaps");
515   gst_caps_unref (incaps);
516 }
517
518
519 #define RUN_CONVERSION(which, inarray, in_get_caps, outarray, out_get_caps)    \
520   verify_convert (which, inarray, sizeof (inarray),                            \
521         in_get_caps, outarray, sizeof (outarray), out_get_caps, GST_FLOW_OK)
522
523 #define RUN_CONVERSION_TO_FAIL(which, inarray, in_caps, outarray, out_caps)    \
524   verify_convert (which, inarray, sizeof (inarray),                            \
525         in_caps, outarray, sizeof (outarray), out_caps, GST_FLOW_NOT_NEGOTIATED)
526
527
528 GST_START_TEST (test_int16)
529 {
530   /* stereo to mono */
531   {
532     gint16 in[] = { 16384, -256, 1024, 1024 };
533     gint16 out[] = { 8064, 1024 };
534
535     RUN_CONVERSION ("int16 stereo to mono",
536         in, get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE),
537         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
538   }
539   /* mono to stereo */
540   {
541     gint16 in[] = { 512, 1024 };
542     gint16 out[] = { 512, 512, 1024, 1024 };
543
544     RUN_CONVERSION ("int16 mono to stereo",
545         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
546         out, get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE));
547   }
548   /* signed -> unsigned */
549   {
550     gint16 in[] = { 0, -32767, 32767, -32768 };
551     guint16 out[] = { 32768, 1, 65535, 0 };
552
553     RUN_CONVERSION ("int16 signed to unsigned",
554         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
555         out, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE));
556     RUN_CONVERSION ("int16 unsigned to signed",
557         out, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE),
558         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
559   }
560 }
561
562 GST_END_TEST;
563
564
565 GST_START_TEST (test_float32)
566 {
567   /* stereo to mono */
568   {
569     gfloat in[] = { 0.6, -0.0078125, 0.03125, 0.03125 };
570     gfloat out[] = { 0.29609375, 0.03125 };
571
572     RUN_CONVERSION ("float32 stereo to mono",
573         in, get_float_caps (2, "BYTE_ORDER", 32),
574         out, get_float_caps (1, "BYTE_ORDER", 32));
575   }
576   /* mono to stereo */
577   {
578     gfloat in[] = { 0.015625, 0.03125 };
579     gfloat out[] = { 0.015625, 0.015625, 0.03125, 0.03125 };
580
581     RUN_CONVERSION ("float32 mono to stereo",
582         in, get_float_caps (1, "BYTE_ORDER", 32),
583         out, get_float_caps (2, "BYTE_ORDER", 32));
584   }
585 }
586
587 GST_END_TEST;
588
589
590 GST_START_TEST (test_int_conversion)
591 {
592   /* 8 <-> 16 signed */
593   /* NOTE: if audioconvert was doing dithering we'd have a problem */
594   {
595     gint8 in[] = { 0, 1, 2, 127, -127 };
596     gint16 out[] = { 0, 256, 512, 32512, -32512 };
597
598     RUN_CONVERSION ("int 8bit to 16bit signed",
599         in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE),
600         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE)
601         );
602     RUN_CONVERSION ("int 16bit signed to 8bit",
603         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
604         in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
605         );
606   }
607   /* 16 -> 8 signed */
608   {
609     gint16 in[] = { 0, 127, 128, 256, 256 + 127, 256 + 128 };
610     gint8 out[] = { 0, 0, 1, 1, 1, 2 };
611
612     RUN_CONVERSION ("16 bit to 8 signed",
613         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
614         out, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
615         );
616   }
617   /* 8 unsigned <-> 16 signed */
618   /* NOTE: if audioconvert was doing dithering we'd have a problem */
619   {
620     guint8 in[] = { 128, 129, 130, 255, 1 };
621     gint16 out[] = { 0, 256, 512, 32512, -32512 };
622     GstCaps *incaps, *outcaps;
623
624     /* exploded for easier valgrinding */
625     incaps = get_int_caps (1, "BYTE_ORDER", 8, 8, FALSE);
626     outcaps = get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE);
627     GST_DEBUG ("incaps: %" GST_PTR_FORMAT, incaps);
628     GST_DEBUG ("outcaps: %" GST_PTR_FORMAT, outcaps);
629     RUN_CONVERSION ("8 unsigned to 16 signed", in, incaps, out, outcaps);
630     RUN_CONVERSION ("16 signed to 8 unsigned", out, get_int_caps (1,
631             "BYTE_ORDER", 16, 16, TRUE), in, get_int_caps (1, "BYTE_ORDER", 8,
632             8, FALSE)
633         );
634   }
635   /* 8 <-> 24 signed */
636   /* NOTE: if audioconvert was doing dithering we'd have a problem */
637   {
638     gint8 in[] = { 0, 1, 127 };
639     guint8 out[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7f };
640     /* out has the bytes in little-endian, so that's how they should be
641      * interpreted during conversion */
642
643     RUN_CONVERSION ("8 to 24 signed", in, get_int_caps (1, "BYTE_ORDER", 8, 8,
644             TRUE), out, get_int_caps (1, "LITTLE_ENDIAN", 24, 24, TRUE)
645         );
646     RUN_CONVERSION ("24 signed to 8", out, get_int_caps (1, "LITTLE_ENDIAN", 24,
647             24, TRUE), in, get_int_caps (1, "BYTE_ORDER", 8, 8, TRUE)
648         );
649   }
650
651   /* 16 bit signed <-> unsigned */
652   {
653     gint16 in[] = { 0, 128, -128 };
654     guint16 out[] = { 32768, 32896, 32640 };
655     RUN_CONVERSION ("16 signed to 16 unsigned",
656         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
657         out, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE)
658         );
659     RUN_CONVERSION ("16 unsigned to 16 signed",
660         out, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE),
661         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE)
662         );
663   }
664
665   /* 16 bit signed <-> 8 in 16 bit signed */
666   /* NOTE: if audioconvert was doing dithering we'd have a problem */
667   {
668     gint16 in[] = { 0, 64 << 8, -64 << 8 };
669     gint16 out[] = { 0, 64, -64 };
670     RUN_CONVERSION ("16 signed to 8 in 16 signed",
671         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
672         out, get_int_caps (1, "BYTE_ORDER", 16, 8, TRUE)
673         );
674     RUN_CONVERSION ("8 in 16 signed to 16 signed",
675         out, get_int_caps (1, "BYTE_ORDER", 16, 8, TRUE),
676         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE)
677         );
678   }
679
680   /* 16 bit unsigned <-> 8 in 16 bit unsigned */
681   /* NOTE: if audioconvert was doing dithering we'd have a problem */
682   {
683     guint16 in[] = { 1 << 15, (1 << 15) - (64 << 8), (1 << 15) + (64 << 8) };
684     guint16 out[] = { 1 << 7, (1 << 7) - 64, (1 << 7) + 64 };
685     RUN_CONVERSION ("16 unsigned to 8 in 16 unsigned",
686         in, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE),
687         out, get_int_caps (1, "BYTE_ORDER", 16, 8, FALSE)
688         );
689     RUN_CONVERSION ("8 in 16 unsigned to 16 unsigned",
690         out, get_int_caps (1, "BYTE_ORDER", 16, 8, FALSE),
691         in, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE)
692         );
693   }
694
695   /* 32 bit signed -> 16 bit signed for rounding check */
696   /* NOTE: if audioconvert was doing dithering we'd have a problem */
697   {
698     gint32 in[] = { 0, G_MININT32, G_MAXINT32,
699       (32 << 16), (32 << 16) + (1 << 15), (32 << 16) - (1 << 15),
700       (32 << 16) + (2 << 15), (32 << 16) - (2 << 15),
701       (-32 << 16) + (1 << 15), (-32 << 16) - (1 << 15),
702       (-32 << 16) + (2 << 15), (-32 << 16) - (2 << 15),
703       (-32 << 16)
704     };
705     gint16 out[] = { 0, G_MININT16, G_MAXINT16,
706       32, 33, 32,
707       33, 31,
708       -31, -32,
709       -31, -33,
710       -32
711     };
712     RUN_CONVERSION ("32 signed to 16 signed for rounding",
713         in, get_int_caps (1, "BYTE_ORDER", 32, 32, TRUE),
714         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE)
715         );
716   }
717
718   /* 32 bit signed -> 16 bit unsigned for rounding check */
719   /* NOTE: if audioconvert was doing dithering we'd have a problem */
720   {
721     gint32 in[] = { 0, G_MININT32, G_MAXINT32,
722       (32 << 16), (32 << 16) + (1 << 15), (32 << 16) - (1 << 15),
723       (32 << 16) + (2 << 15), (32 << 16) - (2 << 15),
724       (-32 << 16) + (1 << 15), (-32 << 16) - (1 << 15),
725       (-32 << 16) + (2 << 15), (-32 << 16) - (2 << 15),
726       (-32 << 16)
727     };
728     guint16 out[] = { (1 << 15), 0, G_MAXUINT16,
729       (1 << 15) + 32, (1 << 15) + 33, (1 << 15) + 32,
730       (1 << 15) + 33, (1 << 15) + 31,
731       (1 << 15) - 31, (1 << 15) - 32,
732       (1 << 15) - 31, (1 << 15) - 33,
733       (1 << 15) - 32
734     };
735     RUN_CONVERSION ("32 signed to 16 unsigned for rounding",
736         in, get_int_caps (1, "BYTE_ORDER", 32, 32, TRUE),
737         out, get_int_caps (1, "BYTE_ORDER", 16, 16, FALSE)
738         );
739   }
740 }
741
742 GST_END_TEST;
743
744 GST_START_TEST (test_float_conversion)
745 {
746   /* 32 float <-> 16 signed */
747   /* NOTE: if audioconvert was doing dithering we'd have a problem */
748   {
749     gfloat in_le[] =
750         { GFLOAT_TO_LE (0.0), GFLOAT_TO_LE (1.0), GFLOAT_TO_LE (-1.0),
751       GFLOAT_TO_LE (0.5), GFLOAT_TO_LE (-0.5), GFLOAT_TO_LE (1.1),
752       GFLOAT_TO_LE (-1.1)
753     };
754     gfloat in_be[] =
755         { GFLOAT_TO_BE (0.0), GFLOAT_TO_BE (1.0), GFLOAT_TO_BE (-1.0),
756       GFLOAT_TO_BE (0.5), GFLOAT_TO_BE (-0.5), GFLOAT_TO_BE (1.1),
757       GFLOAT_TO_BE (-1.1)
758     };
759     gint16 out[] = { 0, 32767, -32768, 16384, -16384, 32767, -32768 };
760
761     /* only one direction conversion, the other direction does
762      * not produce exactly the same as the input due to floating
763      * point rounding errors etc. */
764     RUN_CONVERSION ("32 float le to 16 signed",
765         in_le, get_float_caps (1, "LITTLE_ENDIAN", 32),
766         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
767     RUN_CONVERSION ("32 float be to 16 signed",
768         in_be, get_float_caps (1, "BIG_ENDIAN", 32),
769         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
770   }
771
772   {
773     gint16 in[] = { 0, -32768, 16384, -16384 };
774     gfloat out[] = { 0.0, -1.0, 0.5, -0.5 };
775
776     RUN_CONVERSION ("16 signed to 32 float",
777         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
778         out, get_float_caps (1, "BYTE_ORDER", 32));
779   }
780
781   /* 64 float <-> 16 signed */
782   /* NOTE: if audioconvert was doing dithering we'd have a problem */
783   {
784     gdouble in_le[] =
785         { GDOUBLE_TO_LE (0.0), GDOUBLE_TO_LE (1.0), GDOUBLE_TO_LE (-1.0),
786       GDOUBLE_TO_LE (0.5), GDOUBLE_TO_LE (-0.5), GDOUBLE_TO_LE (1.1),
787       GDOUBLE_TO_LE (-1.1)
788     };
789     gdouble in_be[] =
790         { GDOUBLE_TO_BE (0.0), GDOUBLE_TO_BE (1.0), GDOUBLE_TO_BE (-1.0),
791       GDOUBLE_TO_BE (0.5), GDOUBLE_TO_BE (-0.5), GDOUBLE_TO_BE (1.1),
792       GDOUBLE_TO_BE (-1.1)
793     };
794     gint16 out[] = { 0, 32767, -32768, 16384, -16384, 32767, -32768 };
795
796     /* only one direction conversion, the other direction does
797      * not produce exactly the same as the input due to floating
798      * point rounding errors etc. */
799     RUN_CONVERSION ("64 float LE to 16 signed",
800         in_le, get_float_caps (1, "LITTLE_ENDIAN", 64),
801         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
802     RUN_CONVERSION ("64 float BE to 16 signed",
803         in_be, get_float_caps (1, "BIG_ENDIAN", 64),
804         out, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE));
805   }
806   {
807     gint16 in[] = { 0, -32768, 16384, -16384 };
808     gdouble out[] = { 0.0,
809       (gdouble) (-32768L << 16) / 2147483647.0, /* ~ -1.0 */
810       (gdouble) (16384L << 16) / 2147483647.0,  /* ~  0.5 */
811       (gdouble) (-16384L << 16) / 2147483647.0, /* ~ -0.5 */
812     };
813
814     RUN_CONVERSION ("16 signed to 64 float",
815         in, get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE),
816         out, get_float_caps (1, "BYTE_ORDER", 64));
817   }
818   {
819     gint32 in[] = { 0, (-1L << 31), (1L << 30), (-1L << 30) };
820     gdouble out[] = { 0.0,
821       (gdouble) (-1L << 31) / 2147483647.0,     /* ~ -1.0 */
822       (gdouble) (1L << 30) / 2147483647.0,      /* ~  0.5 */
823       (gdouble) (-1L << 30) / 2147483647.0,     /* ~ -0.5 */
824     };
825
826     RUN_CONVERSION ("32 signed to 64 float",
827         in, get_int_caps (1, "BYTE_ORDER", 32, 32, TRUE),
828         out, get_float_caps (1, "BYTE_ORDER", 64));
829   }
830
831   /* 64-bit float <-> 32-bit float */
832   {
833     gdouble in[] = { 0.0, 1.0, -1.0, 0.5, -0.5 };
834     gfloat out[] = { 0.0, 1.0, -1.0, 0.5, -0.5 };
835
836     RUN_CONVERSION ("64 float to 32 float",
837         in, get_float_caps (1, "BYTE_ORDER", 64),
838         out, get_float_caps (1, "BYTE_ORDER", 32));
839
840     RUN_CONVERSION ("32 float to 64 float",
841         out, get_float_caps (1, "BYTE_ORDER", 32),
842         in, get_float_caps (1, "BYTE_ORDER", 64));
843   }
844
845   /* 32-bit float little endian <-> big endian */
846   {
847     gfloat le[] = { GFLOAT_TO_LE (0.0), GFLOAT_TO_LE (1.0), GFLOAT_TO_LE (-1.0),
848       GFLOAT_TO_LE (0.5), GFLOAT_TO_LE (-0.5)
849     };
850     gfloat be[] = { GFLOAT_TO_BE (0.0), GFLOAT_TO_BE (1.0), GFLOAT_TO_BE (-1.0),
851       GFLOAT_TO_BE (0.5), GFLOAT_TO_BE (-0.5)
852     };
853
854     RUN_CONVERSION ("32 float LE to BE",
855         le, get_float_caps (1, "LITTLE_ENDIAN", 32),
856         be, get_float_caps (1, "BIG_ENDIAN", 32));
857
858     RUN_CONVERSION ("32 float BE to LE",
859         be, get_float_caps (1, "BIG_ENDIAN", 32),
860         le, get_float_caps (1, "LITTLE_ENDIAN", 32));
861   }
862
863   /* 64-bit float little endian <-> big endian */
864   {
865     gdouble le[] =
866         { GDOUBLE_TO_LE (0.0), GDOUBLE_TO_LE (1.0), GDOUBLE_TO_LE (-1.0),
867       GDOUBLE_TO_LE (0.5), GDOUBLE_TO_LE (-0.5)
868     };
869     gdouble be[] =
870         { GDOUBLE_TO_BE (0.0), GDOUBLE_TO_BE (1.0), GDOUBLE_TO_BE (-1.0),
871       GDOUBLE_TO_BE (0.5), GDOUBLE_TO_BE (-0.5)
872     };
873
874     RUN_CONVERSION ("64 float LE to BE",
875         le, get_float_caps (1, "LITTLE_ENDIAN", 64),
876         be, get_float_caps (1, "BIG_ENDIAN", 64));
877
878     RUN_CONVERSION ("64 float BE to LE",
879         be, get_float_caps (1, "BIG_ENDIAN", 64),
880         le, get_float_caps (1, "LITTLE_ENDIAN", 64));
881   }
882 }
883
884 GST_END_TEST;
885
886
887 GST_START_TEST (test_multichannel_conversion)
888 {
889   {
890     gfloat in[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
891     gfloat out[] = { 0.0, 0.0 };
892
893     RUN_CONVERSION ("3 channels to 1", in, get_float_mc_caps (3,
894             "BYTE_ORDER", 32, FALSE), out, get_float_caps (1, "BYTE_ORDER",
895             32));
896     RUN_CONVERSION ("1 channels to 3", out, get_float_caps (1,
897             "BYTE_ORDER", 32), in, get_float_mc_caps (3, "BYTE_ORDER",
898             32, TRUE));
899   }
900
901   {
902     gint16 in[] = { 0, 0, 0, 0, 0, 0 };
903     gint16 out[] = { 0, 0 };
904
905     RUN_CONVERSION ("3 channels to 1", in, get_int_mc_caps (3,
906             "BYTE_ORDER", 16, 16, TRUE, FALSE), out, get_int_caps (1,
907             "BYTE_ORDER", 16, 16, TRUE));
908     RUN_CONVERSION ("1 channels to 3", out, get_int_caps (1, "BYTE_ORDER", 16,
909             16, TRUE), in, get_int_mc_caps (3, "BYTE_ORDER", 16, 16, TRUE,
910             TRUE));
911   }
912
913   {
914     gint16 in[] = { 1, 2 };
915     gint16 out[] = { 1, 1, 2, 2 };
916     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
917     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
918     GstAudioChannelPosition in_layout[1] =
919         { GST_AUDIO_CHANNEL_POSITION_FRONT_MONO };
920     GstAudioChannelPosition out_layout[2] =
921         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
922       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
923     };
924
925     set_channel_positions (in_caps, 1, in_layout);
926     set_channel_positions (out_caps, 2, out_layout);
927
928     RUN_CONVERSION ("1 channels to 2 with standard layout", in,
929         in_caps, out, out_caps);
930   }
931
932   {
933     gint16 in[] = { 1, 2 };
934     gint16 out[] = { 1, 1, 2, 2 };
935     GstCaps *in_caps = get_int_caps (1, "BYTE_ORDER", 16, 16, TRUE);
936     GstCaps *out_caps = get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE);
937
938     RUN_CONVERSION ("1 channels to 2 with standard layout and no positions set",
939         in, gst_caps_copy (in_caps), out, gst_caps_copy (out_caps));
940
941     RUN_CONVERSION ("2 channels to 1 with standard layout and no positions set",
942         out, out_caps, in, in_caps);
943   }
944
945   {
946     gint16 in[] = { 1, 2 };
947     gint16 out[] = { 1, 0, 2, 0 };
948     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
949     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
950     GstAudioChannelPosition in_layout[1] =
951         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT };
952     GstAudioChannelPosition out_layout[2] =
953         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
954       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
955     };
956
957     set_channel_positions (in_caps, 1, in_layout);
958     set_channel_positions (out_caps, 2, out_layout);
959
960     RUN_CONVERSION ("1 channels to 2 with non-standard layout", in,
961         in_caps, out, out_caps);
962   }
963
964   {
965     gint16 in[] = { 1, 2, 3, 4 };
966     gint16 out[] = { 2, 4 };
967     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
968     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
969     GstAudioChannelPosition in_layout[2] =
970         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
971       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
972     };
973     GstAudioChannelPosition out_layout[1] =
974         { GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER };
975
976     set_channel_positions (in_caps, 2, in_layout);
977     set_channel_positions (out_caps, 1, out_layout);
978
979     RUN_CONVERSION ("2 channels to 1 with non-standard layout", in,
980         in_caps, out, out_caps);
981   }
982
983   {
984     gint16 in[] = { 1, 2, 3, 4 };
985     gint16 out[] = { 2, 4 };
986     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
987     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
988     GstAudioChannelPosition in_layout[2] =
989         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
990       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
991     };
992     GstAudioChannelPosition out_layout[1] =
993         { GST_AUDIO_CHANNEL_POSITION_FRONT_MONO };
994
995     set_channel_positions (in_caps, 2, in_layout);
996     set_channel_positions (out_caps, 1, out_layout);
997
998     RUN_CONVERSION ("2 channels to 1 with standard layout", in,
999         in_caps, out, out_caps);
1000   }
1001
1002   {
1003     gint16 in[] = { 1, 2, 3, 4 };
1004     gint16 out[] = { 1, 3 };
1005     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1006     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1007     GstAudioChannelPosition in_layout[2] =
1008         { GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
1009       GST_AUDIO_CHANNEL_POSITION_REAR_CENTER
1010     };
1011     GstAudioChannelPosition out_layout[1] =
1012         { GST_AUDIO_CHANNEL_POSITION_FRONT_MONO };
1013
1014     set_channel_positions (in_caps, 2, in_layout);
1015     set_channel_positions (out_caps, 1, out_layout);
1016
1017     RUN_CONVERSION ("2 channels to 1 with non-standard layout", in,
1018         in_caps, out, out_caps);
1019   }
1020
1021   {
1022     gint16 in[] = { 1, 2, 3, 4 };
1023     gint16 out[] = { 1, 3 };
1024     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1025     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1026     GstAudioChannelPosition in_layout[2] =
1027         { GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
1028       GST_AUDIO_CHANNEL_POSITION_REAR_LEFT
1029     };
1030     GstAudioChannelPosition out_layout[1] =
1031         { GST_AUDIO_CHANNEL_POSITION_FRONT_MONO };
1032
1033     set_channel_positions (in_caps, 2, in_layout);
1034     set_channel_positions (out_caps, 1, out_layout);
1035
1036     RUN_CONVERSION ("2 channels to 1 with non-standard layout", in,
1037         in_caps, out, out_caps);
1038   }
1039   {
1040     gint16 in[] = { 4, 5, 4, 2, 2, 1 };
1041     gint16 out[] = { 3, 3 };
1042     GstCaps *in_caps = get_int_mc_caps (6, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1043     GstCaps *out_caps = get_int_caps (2, "BYTE_ORDER", 16, 16, TRUE);
1044
1045     RUN_CONVERSION ("5.1 to 2 channels", in, in_caps, out, out_caps);
1046   }
1047   {
1048     gint16 in[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1049     gint16 out[] = { 0, 0 };
1050     GstCaps *in_caps = get_int_mc_caps (11, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1051     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1052     GstAudioChannelPosition in_layout[11] = {
1053       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
1054       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
1055       GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
1056       GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
1057       GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
1058       GST_AUDIO_CHANNEL_POSITION_LFE,
1059       GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
1060       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
1061       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
1062       GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
1063       GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
1064     };
1065
1066     set_channel_positions (in_caps, 11, in_layout);
1067
1068     RUN_CONVERSION ("11 channels to 2", in,
1069         gst_caps_copy (in_caps), out, gst_caps_copy (out_caps));
1070     RUN_CONVERSION ("2 channels to 11", out, out_caps, in, in_caps);
1071   }
1072
1073 }
1074
1075 GST_END_TEST;
1076
1077 /* for testing channel remapping with 8 channels */
1078 static GstAudioChannelPosition n8chan_pos_remap_in[8] = {
1079   GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
1080   GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
1081   GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
1082   GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
1083   GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
1084   GST_AUDIO_CHANNEL_POSITION_LFE,
1085   GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
1086   GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
1087 };
1088
1089 static GstAudioChannelPosition n8chan_pos_remap_out[8] = {
1090   GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
1091   GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
1092   GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
1093   GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
1094   GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
1095   GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
1096   GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
1097   GST_AUDIO_CHANNEL_POSITION_LFE
1098 };
1099
1100 GST_START_TEST (test_channel_remapping)
1101 {
1102   /* float */
1103   {
1104     gfloat in[] = { 0.0, 1.0, -0.5 };
1105     gfloat out[] = { -0.5, 1.0, 0.0 };
1106     GstCaps *in_caps = get_float_mc_caps (3, "BYTE_ORDER", 32, FALSE);
1107     GstCaps *out_caps = get_float_mc_caps (3, "BYTE_ORDER", 32, TRUE);
1108
1109     RUN_CONVERSION ("3 channels layout remapping float", in, in_caps,
1110         out, out_caps);
1111   }
1112
1113   /* int */
1114   {
1115     guint16 in[] = { 0, 65535, 0x9999 };
1116     guint16 out[] = { 0x9999, 65535, 0 };
1117     GstCaps *in_caps = get_int_mc_caps (3, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1118     GstCaps *out_caps = get_int_mc_caps (3, "BYTE_ORDER", 16, 16, FALSE, TRUE);
1119
1120     RUN_CONVERSION ("3 channels layout remapping int", in, in_caps,
1121         out, out_caps);
1122   }
1123
1124   /* int with 8 channels (= largest number allowed with channel positions) */
1125   {
1126     guint16 in[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
1127     guint16 out[] = { 4, 0, 1, 6, 7, 2, 3, 5 };
1128     GstCaps *in_caps = get_int_mc_caps (8, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1129     GstCaps *out_caps = get_int_mc_caps (8, "BYTE_ORDER", 16, 16, FALSE, TRUE);
1130
1131     set_channel_positions (in_caps, 8, n8chan_pos_remap_in);
1132     set_channel_positions (out_caps, 8, n8chan_pos_remap_out);
1133
1134     RUN_CONVERSION ("8 channels layout remapping int", in, in_caps,
1135         out, out_caps);
1136   }
1137
1138   /* int16 to int32 with 8 channels (= largest number allowed with channel positions) */
1139   {
1140     guint16 in[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
1141     guint32 out[] =
1142         { 4 << 16, 0, 1 << 16, 6 << 16, 7 << 16, 2 << 16, 3 << 16, 5 << 16 };
1143     GstCaps *in_caps = get_int_mc_caps (8, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1144     GstCaps *out_caps = get_int_mc_caps (8, "BYTE_ORDER", 32, 32, FALSE, TRUE);
1145
1146     set_channel_positions (in_caps, 8, n8chan_pos_remap_in);
1147     set_channel_positions (out_caps, 8, n8chan_pos_remap_out);
1148
1149     RUN_CONVERSION ("8 channels layout remapping int16 --> int32", in, in_caps,
1150         out, out_caps);
1151
1152     in_caps = get_int_mc_caps (8, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1153     out_caps = get_int_mc_caps (8, "BYTE_ORDER", 32, 32, FALSE, TRUE);
1154     set_channel_positions (in_caps, 8, n8chan_pos_remap_in);
1155     set_channel_positions (out_caps, 8, n8chan_pos_remap_out);
1156     RUN_CONVERSION ("8 channels layout remapping int16 <-- int32", out,
1157         out_caps, in, in_caps);
1158   }
1159
1160   /* float to gint16 with 3 channels */
1161   {
1162     gfloat in[] = { 100.0 / G_MAXINT16, 0.0, -100.0 / G_MAXINT16 };
1163     gint16 out[] = { -100, 0, 100 };
1164     GstCaps *in_caps = get_float_mc_caps (3, "BYTE_ORDER", 32, TRUE);
1165     GstCaps *out_caps = get_int_mc_caps (3, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1166
1167     RUN_CONVERSION ("3 channels layout remapping float32 --> int16", in,
1168         in_caps, out, out_caps);
1169   }
1170
1171   /* gint16 to gint16 with 2 channels and non-standard layout */
1172   {
1173     gint16 in[] = { 1, 2, 3, 4 };
1174     gint16 out[] = { 1, 2, 2, 4 };
1175     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1176     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1177     GstAudioChannelPosition in_layout[2] =
1178         { GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
1179       GST_AUDIO_CHANNEL_POSITION_LFE
1180     };
1181     GstAudioChannelPosition out_layout[2] =
1182         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
1183       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
1184     };
1185
1186     set_channel_positions (in_caps, 2, in_layout);
1187     set_channel_positions (out_caps, 2, out_layout);
1188
1189     RUN_CONVERSION ("2 channels layout remapping int16 --> int16", in,
1190         in_caps, out, out_caps);
1191   }
1192
1193   /* gint16 to gint16 with 2 channels and non-standard layout */
1194   {
1195     gint16 in[] = { 1, 2, 3, 4 };
1196     gint16 out[] = { 2, 1, 4, 3 };
1197     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1198     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1199     GstAudioChannelPosition in_layout[2] =
1200         { GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
1201       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT
1202     };
1203     GstAudioChannelPosition out_layout[2] =
1204         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
1205       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
1206     };
1207
1208     set_channel_positions (in_caps, 2, in_layout);
1209     set_channel_positions (out_caps, 2, out_layout);
1210
1211     RUN_CONVERSION ("2 channels layout remapping int16 --> int16", in,
1212         in_caps, out, out_caps);
1213   }
1214
1215   /* gint16 to gint16 with 2 channels and non-standard layout */
1216   {
1217     gint16 in[] = { 1, 2, 3, 4 };
1218     gint16 out[] = { 1, 1, 3, 3 };
1219     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1220     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1221     GstAudioChannelPosition in_layout[2] =
1222         { GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
1223       GST_AUDIO_CHANNEL_POSITION_REAR_CENTER
1224     };
1225     GstAudioChannelPosition out_layout[2] =
1226         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
1227       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
1228     };
1229
1230     set_channel_positions (in_caps, 2, in_layout);
1231     set_channel_positions (out_caps, 2, out_layout);
1232
1233     RUN_CONVERSION ("2 channels layout remapping int16 --> int16", in,
1234         in_caps, out, out_caps);
1235   }
1236
1237   /* gint16 to gint16 with 1 channel and non-standard layout */
1238   {
1239     gint16 in[] = { 1, 2, 3, 4 };
1240     gint16 out[] = { 0, 0, 0, 0 };
1241     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1242     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1243     GstAudioChannelPosition in_layout[1] =
1244         { GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT };
1245     GstAudioChannelPosition out_layout[1] =
1246         { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT };
1247
1248     set_channel_positions (in_caps, 1, in_layout);
1249     set_channel_positions (out_caps, 1, out_layout);
1250
1251     RUN_CONVERSION ("1 channels layout remapping int16 --> int16", in,
1252         in_caps, out, out_caps);
1253   }
1254
1255   /* gint16 to gint16 with 1 channel and non-standard layout */
1256   {
1257     gint16 in[] = { 1, 2, 3, 4 };
1258     gint16 out[] = { 1, 2, 3, 4 };
1259     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1260     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1261     GstAudioChannelPosition in_layout[1] =
1262         { GST_AUDIO_CHANNEL_POSITION_FRONT_MONO };
1263     GstAudioChannelPosition out_layout[1] =
1264         { GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER };
1265
1266     set_channel_positions (in_caps, 1, in_layout);
1267     set_channel_positions (out_caps, 1, out_layout);
1268
1269     RUN_CONVERSION ("1 channels layout remapping int16 --> int16", in,
1270         in_caps, out, out_caps);
1271   }
1272
1273   /* gint16 to gint16 with 1 channel and non-standard layout */
1274   {
1275     gint16 in[] = { 1, 2, 3, 4 };
1276     gint16 out[] = { 1, 2, 3, 4 };
1277     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1278     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1279     GstAudioChannelPosition in_layout[1] =
1280         { GST_AUDIO_CHANNEL_POSITION_FRONT_MONO };
1281     GstAudioChannelPosition out_layout[1] =
1282         { GST_AUDIO_CHANNEL_POSITION_REAR_LEFT };
1283
1284     set_channel_positions (in_caps, 1, in_layout);
1285     set_channel_positions (out_caps, 1, out_layout);
1286
1287     RUN_CONVERSION ("1 channels layout remapping int16 --> int16", in,
1288         in_caps, out, out_caps);
1289   }
1290 }
1291
1292 GST_END_TEST;
1293
1294 GST_START_TEST (test_caps_negotiation)
1295 {
1296   GstElement *src, *ac1, *ac2, *ac3, *sink;
1297   GstElement *pipeline;
1298   GstPad *ac3_src;
1299   GstCaps *caps1, *caps2;
1300
1301   pipeline = gst_pipeline_new ("test");
1302
1303   /* create elements */
1304   src = gst_element_factory_make ("audiotestsrc", "src");
1305   ac1 = gst_element_factory_make ("audioconvert", "ac1");
1306   ac2 = gst_element_factory_make ("audioconvert", "ac2");
1307   ac3 = gst_element_factory_make ("audioconvert", "ac3");
1308   sink = gst_element_factory_make ("fakesink", "sink");
1309   ac3_src = gst_element_get_static_pad (ac3, "src");
1310
1311   /* test with 2 audioconvert elements */
1312   gst_bin_add_many (GST_BIN (pipeline), src, ac1, ac3, sink, NULL);
1313   gst_element_link_many (src, ac1, ac3, sink, NULL);
1314
1315   /* Set to PAUSED and wait for PREROLL */
1316   fail_if (gst_element_set_state (pipeline, GST_STATE_PAUSED) ==
1317       GST_STATE_CHANGE_FAILURE, "Failed to set test pipeline to PAUSED");
1318   fail_if (gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE) !=
1319       GST_STATE_CHANGE_SUCCESS, "Failed to set test pipeline to PAUSED");
1320
1321   caps1 = gst_pad_get_caps (ac3_src, NULL);
1322   fail_if (caps1 == NULL, "gst_pad_get_caps returned NULL");
1323   GST_DEBUG ("Caps size 1 : %d", gst_caps_get_size (caps1));
1324
1325   fail_if (gst_element_set_state (pipeline, GST_STATE_READY) ==
1326       GST_STATE_CHANGE_FAILURE, "Failed to set test pipeline back to READY");
1327   fail_if (gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE) !=
1328       GST_STATE_CHANGE_SUCCESS, "Failed to set test pipeline back to READY");
1329
1330   /* test with 3 audioconvert elements */
1331   gst_element_unlink (ac1, ac3);
1332   gst_bin_add (GST_BIN (pipeline), ac2);
1333   gst_element_link_many (ac1, ac2, ac3, NULL);
1334
1335   fail_if (gst_element_set_state (pipeline, GST_STATE_PAUSED) ==
1336       GST_STATE_CHANGE_FAILURE, "Failed to set test pipeline back to PAUSED");
1337   fail_if (gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE) !=
1338       GST_STATE_CHANGE_SUCCESS, "Failed to set test pipeline back to PAUSED");
1339
1340   caps2 = gst_pad_get_caps (ac3_src, NULL);
1341
1342   fail_if (caps2 == NULL, "gst_pad_get_caps returned NULL");
1343   GST_DEBUG ("Caps size 2 : %d", gst_caps_get_size (caps2));
1344   fail_unless (gst_caps_get_size (caps1) == gst_caps_get_size (caps2));
1345
1346   gst_caps_unref (caps1);
1347   gst_caps_unref (caps2);
1348
1349   fail_if (gst_element_set_state (pipeline, GST_STATE_NULL) ==
1350       GST_STATE_CHANGE_FAILURE, "Failed to set test pipeline back to NULL");
1351   fail_if (gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE) !=
1352       GST_STATE_CHANGE_SUCCESS, "Failed to set test pipeline back to NULL");
1353
1354   gst_object_unref (ac3_src);
1355   gst_object_unref (pipeline);
1356 }
1357
1358 GST_END_TEST;
1359
1360 GST_START_TEST (test_convert_undefined_multichannel)
1361 {
1362   /* (A) CONVERSION FROM 'WORSE' TO 'BETTER' FORMAT */
1363
1364   /* 1 channel, NONE positions, int8 => int16 */
1365   {
1366     guint16 out[] = { 0x2000 };
1367     guint8 in[] = { 0x20 };
1368     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1369     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1370
1371     set_channel_positions (out_caps, 1, undefined_positions[1 - 1]);
1372     set_channel_positions (in_caps, 1, undefined_positions[1 - 1]);
1373
1374     RUN_CONVERSION ("1 channel, undefined layout, identity conversion, "
1375         "int8 => int16", in, in_caps, out, out_caps);
1376   }
1377
1378   /* 2 channels, NONE positions, int8 => int16 */
1379   {
1380     guint16 out[] = { 0x8000, 0x2000 };
1381     guint8 in[] = { 0x80, 0x20 };
1382     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1383     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1384
1385     set_channel_positions (out_caps, 2, undefined_positions[2 - 1]);
1386     set_channel_positions (in_caps, 2, undefined_positions[2 - 1]);
1387
1388     RUN_CONVERSION ("2 channels, undefined layout, identity conversion, "
1389         "int8 => int16", in, in_caps, out, out_caps);
1390   }
1391
1392   /* 6 channels, NONE positions, int8 => int16 */
1393   {
1394     guint16 out[] = { 0x0000, 0x2000, 0x8000, 0x2000, 0x0000, 0xff00 };
1395     guint8 in[] = { 0x00, 0x20, 0x80, 0x20, 0x00, 0xff };
1396     GstCaps *out_caps = get_int_mc_caps (6, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1397     GstCaps *in_caps = get_int_mc_caps (6, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1398
1399     set_channel_positions (out_caps, 6, undefined_positions[6 - 1]);
1400     set_channel_positions (in_caps, 6, undefined_positions[6 - 1]);
1401
1402     RUN_CONVERSION ("6 channels, undefined layout, identity conversion, "
1403         "int8 => int16", in, in_caps, out, out_caps);
1404   }
1405
1406   /* 9 channels, NONE positions, int8 => int16 */
1407   {
1408     guint16 out[] = { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1409       0x0000, 0xff00, 0x0000
1410     };
1411     guint8 in[] = { 0x00, 0xff, 0x00, 0x20, 0x80, 0x20, 0x00, 0xff, 0x00 };
1412     GstCaps *out_caps = get_int_mc_caps (9, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1413     GstCaps *in_caps = get_int_mc_caps (9, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1414
1415     set_channel_positions (out_caps, 9, undefined_positions[9 - 1]);
1416     set_channel_positions (in_caps, 9, undefined_positions[9 - 1]);
1417
1418     RUN_CONVERSION ("9 channels, undefined layout, identity conversion, "
1419         "int8 => int16", in, in_caps, out, out_caps);
1420   }
1421
1422   /* 15 channels, NONE positions, int8 => int16 */
1423   {
1424     guint16 out[] =
1425         { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000, 0x0000, 0xff00,
1426       0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000, 0x0000
1427     };
1428     guint8 in[] =
1429         { 0x00, 0xff, 0x00, 0x20, 0x80, 0x20, 0x00, 0xff, 0x00, 0xff, 0x00,
1430       0x20, 0x80, 0x20, 0x00
1431     };
1432     GstCaps *out_caps =
1433         get_int_mc_caps (15, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1434     GstCaps *in_caps = get_int_mc_caps (15, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1435
1436     set_channel_positions (out_caps, 15, undefined_positions[15 - 1]);
1437     set_channel_positions (in_caps, 15, undefined_positions[15 - 1]);
1438
1439     RUN_CONVERSION ("15 channels, undefined layout, identity conversion, "
1440         "int8 => int16", in, in_caps, out, out_caps);
1441   }
1442
1443   /* (B) CONVERSION FROM 'BETTER' TO 'WORSE' FORMAT */
1444
1445   /* 1 channel, NONE positions, int16 => int8 */
1446   {
1447     guint16 in[] = { 0x2000 };
1448     guint8 out[] = { 0x20 };
1449     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1450     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1451
1452     set_channel_positions (out_caps, 1, undefined_positions[1 - 1]);
1453     set_channel_positions (in_caps, 1, undefined_positions[1 - 1]);
1454
1455     RUN_CONVERSION ("1 channel, undefined layout, identity conversion, "
1456         "int16 => int8", in, in_caps, out, out_caps);
1457   }
1458
1459   /* 2 channels, NONE positions, int16 => int8 */
1460   {
1461     guint16 in[] = { 0x8000, 0x2000 };
1462     guint8 out[] = { 0x80, 0x20 };
1463     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1464     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1465
1466     set_channel_positions (out_caps, 2, undefined_positions[2 - 1]);
1467     set_channel_positions (in_caps, 2, undefined_positions[2 - 1]);
1468
1469     RUN_CONVERSION ("2 channels, undefined layout, identity conversion, "
1470         "int16 => int8", in, in_caps, out, out_caps);
1471   }
1472
1473   /* 6 channels, NONE positions, int16 => int8 */
1474   {
1475     guint16 in[] = { 0x0000, 0x2000, 0x8000, 0x2000, 0x0000, 0xff00 };
1476     guint8 out[] = { 0x00, 0x20, 0x80, 0x20, 0x00, 0xff };
1477     GstCaps *in_caps = get_int_mc_caps (6, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1478     GstCaps *out_caps = get_int_mc_caps (6, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1479
1480     set_channel_positions (out_caps, 6, undefined_positions[6 - 1]);
1481     set_channel_positions (in_caps, 6, undefined_positions[6 - 1]);
1482
1483     RUN_CONVERSION ("6 channels, undefined layout, identity conversion, "
1484         "int16 => int8", in, in_caps, out, out_caps);
1485   }
1486
1487   /* 9 channels, NONE positions, int16 => int8 */
1488   {
1489     guint16 in[] = { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1490       0x0000, 0xff00, 0x0000
1491     };
1492     guint8 out[] = { 0x00, 0xff, 0x00, 0x20, 0x80, 0x20, 0x00, 0xff, 0x00 };
1493     GstCaps *in_caps = get_int_mc_caps (9, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1494     GstCaps *out_caps = get_int_mc_caps (9, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1495
1496     set_channel_positions (out_caps, 9, undefined_positions[9 - 1]);
1497     set_channel_positions (in_caps, 9, undefined_positions[9 - 1]);
1498
1499     RUN_CONVERSION ("9 channels, undefined layout, identity conversion, "
1500         "int16 => int8", in, in_caps, out, out_caps);
1501   }
1502
1503   /* 15 channels, NONE positions, int16 => int8 */
1504   {
1505     guint16 in[] = { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1506       0x0000, 0xff00, 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1507       0x0000
1508     };
1509     guint8 out[] =
1510         { 0x00, 0xff, 0x00, 0x20, 0x80, 0x20, 0x00, 0xff, 0x00, 0xff, 0x00,
1511       0x20, 0x80, 0x20, 0x00
1512     };
1513     GstCaps *in_caps = get_int_mc_caps (15, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1514     GstCaps *out_caps = get_int_mc_caps (15, "BYTE_ORDER", 8, 8, FALSE, FALSE);
1515
1516     set_channel_positions (out_caps, 15, undefined_positions[15 - 1]);
1517     set_channel_positions (in_caps, 15, undefined_positions[15 - 1]);
1518
1519     RUN_CONVERSION ("15 channels, undefined layout, identity conversion, "
1520         "int16 => int8", in, in_caps, out, out_caps);
1521   }
1522
1523
1524   /* (C) NO CONVERSION, SAME FORMAT */
1525
1526   /* 1 channel, NONE positions, int16 => int16 */
1527   {
1528     guint16 in[] = { 0x2000 };
1529     guint16 out[] = { 0x2000 };
1530     GstCaps *in_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1531     GstCaps *out_caps = get_int_mc_caps (1, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1532
1533     set_channel_positions (out_caps, 1, undefined_positions[1 - 1]);
1534     set_channel_positions (in_caps, 1, undefined_positions[1 - 1]);
1535
1536     RUN_CONVERSION ("1 channel, undefined layout, identity conversion, "
1537         "int16 => int16", in, in_caps, out, out_caps);
1538   }
1539
1540   /* 2 channels, NONE positions, int16 => int16 */
1541   {
1542     guint16 in[] = { 0x8000, 0x2000 };
1543     guint16 out[] = { 0x8000, 0x2000 };
1544     GstCaps *in_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1545     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1546
1547     set_channel_positions (out_caps, 2, undefined_positions[2 - 1]);
1548     set_channel_positions (in_caps, 2, undefined_positions[2 - 1]);
1549
1550     RUN_CONVERSION ("2 channels, undefined layout, identity conversion, "
1551         "int16 => int16", in, in_caps, out, out_caps);
1552   }
1553
1554   /* 6 channels, NONE positions, int16 => int16 */
1555   {
1556     guint16 in[] = { 0x0000, 0x2000, 0x8000, 0x2000, 0x0000, 0xff00 };
1557     guint16 out[] = { 0x0000, 0x2000, 0x8000, 0x2000, 0x0000, 0xff00 };
1558     GstCaps *in_caps = get_int_mc_caps (6, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1559     GstCaps *out_caps = get_int_mc_caps (6, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1560
1561     set_channel_positions (out_caps, 6, undefined_positions[6 - 1]);
1562     set_channel_positions (in_caps, 6, undefined_positions[6 - 1]);
1563
1564     RUN_CONVERSION ("6 channels, undefined layout, identity conversion, "
1565         "int16 => int16", in, in_caps, out, out_caps);
1566   }
1567
1568   /* 9 channels, NONE positions, int16 => int16 */
1569   {
1570     guint16 in[] = { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1571       0x0000, 0xff00, 0x0000
1572     };
1573     guint16 out[] = { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1574       0x0000, 0xff00, 0x0000
1575     };
1576     GstCaps *in_caps = get_int_mc_caps (9, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1577     GstCaps *out_caps = get_int_mc_caps (9, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1578
1579     set_channel_positions (out_caps, 9, undefined_positions[9 - 1]);
1580     set_channel_positions (in_caps, 9, undefined_positions[9 - 1]);
1581
1582     RUN_CONVERSION ("9 channels, undefined layout, identity conversion, "
1583         "int16 => int16", in, in_caps, out, out_caps);
1584   }
1585
1586   /* 15 channels, NONE positions, int16 => int16 */
1587   {
1588     guint16 in[] = { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1589       0x0000, 0xff00, 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1590       0x0000
1591     };
1592     guint16 out[] = { 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1593       0x0000, 0xff00, 0x0000, 0xff00, 0x0000, 0x2000, 0x8000, 0x2000,
1594       0x0000
1595     };
1596     GstCaps *in_caps = get_int_mc_caps (15, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1597     GstCaps *out_caps =
1598         get_int_mc_caps (15, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1599
1600     set_channel_positions (out_caps, 15, undefined_positions[15 - 1]);
1601     set_channel_positions (in_caps, 15, undefined_positions[15 - 1]);
1602
1603     RUN_CONVERSION ("15 channels, undefined layout, identity conversion, "
1604         "int16 => int16", in, in_caps, out, out_caps);
1605   }
1606
1607
1608   /* (C) int16 => float */
1609
1610   /* 9 channels, NONE positions, int16 => float */
1611   {
1612     guint16 in[] = { 0x0000, 0x8000, 0x0000, 0x8000, 0x8000, 0x8000,
1613       0x0000, 0x8000, 0x0000
1614     };
1615     gfloat out[] = { -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0 };
1616     GstCaps *in_caps = get_int_mc_caps (9, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1617     GstCaps *out_caps = get_float_mc_caps (9, "BYTE_ORDER", 32, FALSE);
1618
1619     set_channel_positions (out_caps, 9, undefined_positions[9 - 1]);
1620     set_channel_positions (in_caps, 9, undefined_positions[9 - 1]);
1621
1622     RUN_CONVERSION ("9 channels, undefined layout, identity conversion, "
1623         "int16 => float", in, in_caps, out, out_caps);
1624   }
1625
1626   /* 15 channels, NONE positions, int16 => float */
1627   {
1628     guint16 in[] = { 0x0000, 0x8000, 0x0000, 0x8000, 0x8000, 0x8000,
1629       0x0000, 0x8000, 0x0000, 0x8000, 0x0000, 0x8000, 0x8000, 0x8000,
1630       0x0000
1631     };
1632     gfloat out[] =
1633         { -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0, 0.0,
1634       0.0, -1.0
1635     };
1636     GstCaps *in_caps = get_int_mc_caps (15, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1637     GstCaps *out_caps = get_float_mc_caps (15, "BYTE_ORDER", 32, FALSE);
1638
1639     set_channel_positions (out_caps, 15, undefined_positions[15 - 1]);
1640     set_channel_positions (in_caps, 15, undefined_positions[15 - 1]);
1641
1642     RUN_CONVERSION ("15 channels, undefined layout, identity conversion, "
1643         "int16 => float", in, in_caps, out, out_caps);
1644   }
1645
1646
1647   /* 9 channels, NONE positions, int16 => float (same as above, but no
1648    * position on output caps to see if audioconvert transforms correctly) */
1649   {
1650     guint16 in[] = { 0x0000, 0x8000, 0x0000, 0x8000, 0x8000, 0x8000,
1651       0x0000, 0x8000, 0x0000
1652     };
1653     gfloat out[] = { -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0 };
1654     GstCaps *in_caps = get_int_mc_caps (9, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1655     GstCaps *out_caps = get_float_mc_caps (9, "BYTE_ORDER", 32, FALSE);
1656
1657     //set_channel_positions (out_caps, 9, undefined_positions[9 - 1]);
1658     set_channel_positions (in_caps, 9, undefined_positions[9 - 1]);
1659
1660     RUN_CONVERSION ("9 channels, undefined layout, identity conversion, "
1661         "int16 => float", in, in_caps, out, out_caps);
1662   }
1663
1664   /* 15 channels, NONE positions, int16 => float (same as above, but no
1665    * position on output caps to see if audioconvert transforms correctly) */
1666   {
1667     guint16 in[] = { 0x0000, 0x8000, 0x0000, 0x8000, 0x8000, 0x8000,
1668       0x0000, 0x8000, 0x0000, 0x8000, 0x0000, 0x8000, 0x8000, 0x8000,
1669       0x0000
1670     };
1671     gfloat out[] =
1672         { -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0, 0.0,
1673       0.0, -1.0
1674     };
1675     GstCaps *in_caps = get_int_mc_caps (15, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1676     GstCaps *out_caps = get_float_mc_caps (15, "BYTE_ORDER", 32, FALSE);
1677
1678     //set_channel_positions (out_caps, 9, undefined_positions[9 - 1]);
1679     set_channel_positions (in_caps, 15, undefined_positions[15 - 1]);
1680
1681     RUN_CONVERSION ("15 channels, undefined layout, identity conversion, "
1682         "int16 => float", in, in_caps, out, out_caps);
1683   }
1684
1685   /* 8 channels, NONE positions => 2 channels: should fail, no mixing allowed */
1686   {
1687     guint16 in[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1688     gfloat out[] = { -1.0, -1.0 };
1689     GstCaps *in_caps = get_int_mc_caps (8, "BYTE_ORDER", 16, 16, FALSE, FALSE);
1690     GstCaps *out_caps = get_float_mc_caps (2, "BYTE_ORDER", 32, FALSE);
1691
1692     set_channel_positions (in_caps, 8, undefined_positions[8 - 1]);
1693
1694     RUN_CONVERSION_TO_FAIL ("8 channels with layout => 2 channels",
1695         in, in_caps, out, out_caps);
1696   }
1697
1698   /* 8 channels, with positions => 2 channels (makes sure channel-position
1699    * fields are removed properly in some cases in ::transform_caps, so we
1700    * don't up with caps with 2 channels and 8 channel positions) */
1701   {
1702     GstAudioChannelPosition layout8ch[] = {
1703       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
1704       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
1705       GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
1706       GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
1707       GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
1708       GST_AUDIO_CHANNEL_POSITION_LFE,
1709       GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
1710       GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
1711     };
1712     gint16 in[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1713     gint16 out[] = { 0, 0 };
1714     GstCaps *in_caps = get_int_mc_caps (8, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1715     GstCaps *out_caps = get_int_mc_caps (2, "BYTE_ORDER", 16, 16, TRUE, FALSE);
1716
1717     set_channel_positions (in_caps, 8, layout8ch);
1718
1719     RUN_CONVERSION ("8 channels with layout => 2 channels",
1720         in, in_caps, out, out_caps);
1721   }
1722 }
1723
1724 GST_END_TEST;
1725
1726 static Suite *
1727 audioconvert_suite (void)
1728 {
1729   Suite *s = suite_create ("audioconvert");
1730   TCase *tc_chain = tcase_create ("general");
1731
1732   suite_add_tcase (s, tc_chain);
1733   tcase_add_test (tc_chain, test_int16);
1734   tcase_add_test (tc_chain, test_float32);
1735   tcase_add_test (tc_chain, test_int_conversion);
1736   tcase_add_test (tc_chain, test_float_conversion);
1737   tcase_add_test (tc_chain, test_multichannel_conversion);
1738   tcase_add_test (tc_chain, test_channel_remapping);
1739   tcase_add_test (tc_chain, test_caps_negotiation);
1740   tcase_add_test (tc_chain, test_convert_undefined_multichannel);
1741
1742   return s;
1743 }
1744
1745 GST_CHECK_MAIN (audioconvert);