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