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