playsinkconvertbin: Make sure to return all allowed caps in the GET_CAPS query
[platform/upstream/gstreamer.git] / gst / audioconvert / gstaudioconvert.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
4  * Copyright (C) 2011 Wim Taymans <wim.taymans at gmail dot com>
5  *
6  * gstaudioconvert.c: Convert audio to different audio formats automatically
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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:element-audioconvert
26  *
27  * Audioconvert converts raw audio buffers between various possible formats.
28  * It supports integer to float conversion, width/depth conversion,
29  * signedness and endianness conversion and channel transformations.
30  *
31  * <refsect2>
32  * <title>Example launch line</title>
33  * |[
34  * gst-launch -v -m audiotestsrc ! audioconvert ! audio/x-raw,format=S8,channels=2 ! level ! fakesink silent=TRUE
35  * ]| This pipeline converts audio to 8-bit.  The level element shows that
36  * the output levels still match the one for a sine wave.
37  * |[
38  * gst-launch -v -m audiotestsrc ! audioconvert ! vorbisenc ! fakesink silent=TRUE
39  * ]| The vorbis encoder takes float audio data instead of the integer data
40  * generated by audiotestsrc.
41  * </refsect2>
42  *
43  * Last reviewed on 2006-03-02 (0.10.4)
44  */
45
46 /*
47  * design decisions:
48  * - audioconvert converts buffers in a set of supported caps. If it supports
49  *   a caps, it supports conversion from these caps to any other caps it
50  *   supports. (example: if it does A=>B and A=>C, it also does B=>C)
51  * - audioconvert does not save state between buffers. Every incoming buffer is
52  *   converted and the converted buffer is pushed out.
53  * conclusion:
54  * audioconvert is not supposed to be a one-element-does-anything solution for
55  * audio conversions.
56  */
57
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
61
62 #include <string.h>
63
64 #include "gstaudioconvert.h"
65 #include "gstchannelmix.h"
66 #include "gstaudioquantize.h"
67 #include "plugin.h"
68
69 GST_DEBUG_CATEGORY (audio_convert_debug);
70 GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
71
72 /*** DEFINITIONS **************************************************************/
73
74 /* type functions */
75 static void gst_audio_convert_dispose (GObject * obj);
76
77 /* gstreamer functions */
78 static gboolean gst_audio_convert_get_unit_size (GstBaseTransform * base,
79     GstCaps * caps, gsize * size);
80 static GstCaps *gst_audio_convert_transform_caps (GstBaseTransform * base,
81     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
82 static GstCaps *gst_audio_convert_fixate_caps (GstBaseTransform * base,
83     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
84 static gboolean gst_audio_convert_set_caps (GstBaseTransform * base,
85     GstCaps * incaps, GstCaps * outcaps);
86 static GstFlowReturn gst_audio_convert_transform (GstBaseTransform * base,
87     GstBuffer * inbuf, GstBuffer * outbuf);
88 static void gst_audio_convert_set_property (GObject * object, guint prop_id,
89     const GValue * value, GParamSpec * pspec);
90 static void gst_audio_convert_get_property (GObject * object, guint prop_id,
91     GValue * value, GParamSpec * pspec);
92
93 /* AudioConvert signals and args */
94 enum
95 {
96   /* FILL ME */
97   LAST_SIGNAL
98 };
99
100 enum
101 {
102   ARG_0,
103   ARG_DITHERING,
104   ARG_NOISE_SHAPING,
105 };
106
107 #define DEBUG_INIT \
108   GST_DEBUG_CATEGORY_INIT (audio_convert_debug, "audioconvert", 0, "audio conversion element"); \
109   GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
110 #define gst_audio_convert_parent_class parent_class
111 G_DEFINE_TYPE_WITH_CODE (GstAudioConvert, gst_audio_convert,
112     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
113
114 /*** GSTREAMER PROTOTYPES *****************************************************/
115
116 #define STATIC_CAPS \
117 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL) \
118     ", layout = (string) interleaved")
119
120 static GstStaticPadTemplate gst_audio_convert_src_template =
121 GST_STATIC_PAD_TEMPLATE ("src",
122     GST_PAD_SRC,
123     GST_PAD_ALWAYS,
124     STATIC_CAPS);
125
126 static GstStaticPadTemplate gst_audio_convert_sink_template =
127 GST_STATIC_PAD_TEMPLATE ("sink",
128     GST_PAD_SINK,
129     GST_PAD_ALWAYS,
130     STATIC_CAPS);
131
132 #define GST_TYPE_AUDIO_CONVERT_DITHERING (gst_audio_convert_dithering_get_type ())
133 static GType
134 gst_audio_convert_dithering_get_type (void)
135 {
136   static GType gtype = 0;
137
138   if (gtype == 0) {
139     static const GEnumValue values[] = {
140       {DITHER_NONE, "No dithering",
141           "none"},
142       {DITHER_RPDF, "Rectangular dithering", "rpdf"},
143       {DITHER_TPDF, "Triangular dithering (default)", "tpdf"},
144       {DITHER_TPDF_HF, "High frequency triangular dithering", "tpdf-hf"},
145       {0, NULL, NULL}
146     };
147
148     gtype = g_enum_register_static ("GstAudioConvertDithering", values);
149   }
150   return gtype;
151 }
152
153 #define GST_TYPE_AUDIO_CONVERT_NOISE_SHAPING (gst_audio_convert_ns_get_type ())
154 static GType
155 gst_audio_convert_ns_get_type (void)
156 {
157   static GType gtype = 0;
158
159   if (gtype == 0) {
160     static const GEnumValue values[] = {
161       {NOISE_SHAPING_NONE, "No noise shaping (default)",
162           "none"},
163       {NOISE_SHAPING_ERROR_FEEDBACK, "Error feedback", "error-feedback"},
164       {NOISE_SHAPING_SIMPLE, "Simple 2-pole noise shaping", "simple"},
165       {NOISE_SHAPING_MEDIUM, "Medium 5-pole noise shaping", "medium"},
166       {NOISE_SHAPING_HIGH, "High 8-pole noise shaping", "high"},
167       {0, NULL, NULL}
168     };
169
170     gtype = g_enum_register_static ("GstAudioConvertNoiseShaping", values);
171   }
172   return gtype;
173 }
174
175
176 /*** TYPE FUNCTIONS ***********************************************************/
177 static void
178 gst_audio_convert_class_init (GstAudioConvertClass * klass)
179 {
180   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
181   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
182   GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass);
183
184   gobject_class->dispose = gst_audio_convert_dispose;
185   gobject_class->set_property = gst_audio_convert_set_property;
186   gobject_class->get_property = gst_audio_convert_get_property;
187
188   g_object_class_install_property (gobject_class, ARG_DITHERING,
189       g_param_spec_enum ("dithering", "Dithering",
190           "Selects between different dithering methods.",
191           GST_TYPE_AUDIO_CONVERT_DITHERING, DITHER_TPDF,
192           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193
194   g_object_class_install_property (gobject_class, ARG_NOISE_SHAPING,
195       g_param_spec_enum ("noise-shaping", "Noise shaping",
196           "Selects between different noise shaping methods.",
197           GST_TYPE_AUDIO_CONVERT_NOISE_SHAPING, NOISE_SHAPING_NONE,
198           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199
200   gst_element_class_add_pad_template (element_class,
201       gst_static_pad_template_get (&gst_audio_convert_src_template));
202   gst_element_class_add_pad_template (element_class,
203       gst_static_pad_template_get (&gst_audio_convert_sink_template));
204   gst_element_class_set_static_metadata (element_class,
205       "Audio converter", "Filter/Converter/Audio",
206       "Convert audio to different formats", "Benjamin Otte <otte@gnome.org>");
207
208   basetransform_class->get_unit_size =
209       GST_DEBUG_FUNCPTR (gst_audio_convert_get_unit_size);
210   basetransform_class->transform_caps =
211       GST_DEBUG_FUNCPTR (gst_audio_convert_transform_caps);
212   basetransform_class->fixate_caps =
213       GST_DEBUG_FUNCPTR (gst_audio_convert_fixate_caps);
214   basetransform_class->set_caps =
215       GST_DEBUG_FUNCPTR (gst_audio_convert_set_caps);
216   basetransform_class->transform =
217       GST_DEBUG_FUNCPTR (gst_audio_convert_transform);
218
219   basetransform_class->passthrough_on_same_caps = TRUE;
220 }
221
222 static void
223 gst_audio_convert_init (GstAudioConvert * this)
224 {
225   this->dither = DITHER_TPDF;
226   this->ns = NOISE_SHAPING_NONE;
227   memset (&this->ctx, 0, sizeof (AudioConvertCtx));
228
229   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (this), TRUE);
230 }
231
232 static void
233 gst_audio_convert_dispose (GObject * obj)
234 {
235   GstAudioConvert *this = GST_AUDIO_CONVERT (obj);
236
237   audio_convert_clean_context (&this->ctx);
238
239   G_OBJECT_CLASS (parent_class)->dispose (obj);
240 }
241
242 /*** GSTREAMER FUNCTIONS ******************************************************/
243
244 /* BaseTransform vmethods */
245 static gboolean
246 gst_audio_convert_get_unit_size (GstBaseTransform * base, GstCaps * caps,
247     gsize * size)
248 {
249   GstAudioInfo info;
250
251   g_assert (size);
252
253   if (!gst_audio_info_from_caps (&info, caps))
254     goto parse_error;
255
256   *size = info.bpf;
257   GST_INFO_OBJECT (base, "unit_size = %" G_GSIZE_FORMAT, *size);
258
259   return TRUE;
260
261 parse_error:
262   {
263     GST_INFO_OBJECT (base, "failed to parse caps to get unit_size");
264     return FALSE;
265   }
266 }
267
268 /* copies the given caps */
269 static GstCaps *
270 gst_audio_convert_caps_remove_format_info (GstCaps * caps, gboolean channels)
271 {
272   GstStructure *st;
273   gint i, n;
274   GstCaps *res;
275   guint64 channel_mask;
276
277   res = gst_caps_new_empty ();
278
279   n = gst_caps_get_size (caps);
280   for (i = 0; i < n; i++) {
281     gboolean remove_channels = FALSE;
282
283     st = gst_caps_get_structure (caps, i);
284
285     /* If this is already expressed by the existing caps
286      * skip this structure */
287     if (i > 0 && gst_caps_is_subset_structure (res, st))
288       continue;
289
290     st = gst_structure_copy (st);
291     gst_structure_remove_field (st, "format");
292
293     /* Only remove the channels and channel-mask for non-NONE layouts */
294     if (gst_structure_get (st, "channel-mask", GST_TYPE_BITMASK, &channel_mask,
295             NULL)) {
296       if (channel_mask != 0)
297         remove_channels = TRUE;
298     } else {
299       remove_channels = TRUE;
300     }
301
302     if (remove_channels && channels)
303       gst_structure_remove_fields (st, "channel-mask", "channels", NULL);
304
305     gst_caps_append_structure (res, st);
306   }
307
308   return res;
309 }
310
311 /* The caps can be transformed into any other caps with format info removed.
312  * However, we should prefer passthrough, so if passthrough is possible,
313  * put it first in the list. */
314 static GstCaps *
315 gst_audio_convert_transform_caps (GstBaseTransform * btrans,
316     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
317 {
318   GstCaps *tmp, *tmp2;
319   GstCaps *result;
320
321   /* Get all possible caps that we can transform to */
322   tmp = gst_audio_convert_caps_remove_format_info (caps, TRUE);
323
324   if (filter) {
325     tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
326     gst_caps_unref (tmp);
327     tmp = tmp2;
328   }
329
330   result = tmp;
331
332   GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
333       GST_PTR_FORMAT, caps, result);
334
335   return result;
336 }
337
338 static const GstAudioChannelPosition default_positions[8][8] = {
339   /* 1 channel */
340   {
341         GST_AUDIO_CHANNEL_POSITION_MONO,
342       },
343   /* 2 channels */
344   {
345         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
346         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
347       },
348   /* 3 channels (2.1) */
349   {
350         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
351         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
352         GST_AUDIO_CHANNEL_POSITION_LFE1,
353       },
354   /* 4 channels (4.0) */
355   {
356         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
357         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
358         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
359         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
360       },
361   /* 5 channels */
362   {
363         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
364         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
365         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
366         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
367         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
368       },
369   /* 6 channels (5.1) */
370   {
371         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
372         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
373         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
374         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
375         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
376         GST_AUDIO_CHANNEL_POSITION_LFE1,
377       },
378   /* 7 channels (6.1) */
379   {
380         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
381         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
382         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
383         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
384         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
385         GST_AUDIO_CHANNEL_POSITION_LFE1,
386         GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
387       },
388   /* 8 channels (7.1) */
389   {
390         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
391         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
392         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
393         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
394         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
395         GST_AUDIO_CHANNEL_POSITION_LFE1,
396         GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
397         GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
398       }
399 };
400
401 static gint
402 n_bits_set (guint64 x)
403 {
404   gint i;
405   gint c = 0;
406   guint64 y = 1;
407
408   for (i = 0; i < 64; i++) {
409     if (x & y)
410       c++;
411     y <<= 1;
412   }
413
414   return c;
415 }
416
417 static guint64
418 find_suitable_mask (guint64 mask, gint n_chans)
419 {
420   guint64 intersection;
421   gint i;
422
423   i = 0;
424
425   g_assert (n_bits_set (mask) >= n_chans);
426
427   intersection = mask;
428   do {
429     intersection = intersection & ((~G_GUINT64_CONSTANT (0)) >> i);
430     i++;
431   } while (n_bits_set (intersection) > n_chans && i < 64);
432
433   if (i < 64)
434     return intersection;
435   return 0;
436 }
437
438 static void
439 gst_audio_convert_fixate_format (GstBaseTransform * base, GstStructure * ins,
440     GstStructure * outs)
441 {
442   const gchar *in_format;
443   const GValue *format;
444   const GstAudioFormatInfo *in_info, *out_info = NULL;
445   GstAudioFormatFlags in_flags, out_flags;
446
447   in_format = gst_structure_get_string (ins, "format");
448   if (!in_format)
449     return;
450
451   format = gst_structure_get_value (outs, "format");
452   /* should not happen */
453   if (format == NULL)
454     return;
455
456   in_info =
457       gst_audio_format_get_info (gst_audio_format_from_string (in_format));
458   if (!in_info)
459     return;
460
461   in_flags = GST_AUDIO_FORMAT_INFO_FLAGS (in_info);
462   in_flags &= ~(GST_AUDIO_FORMAT_FLAG_UNPACK);
463   in_flags &= ~(GST_AUDIO_FORMAT_FLAG_SIGNED);
464
465   if (GST_VALUE_HOLDS_LIST (format)) {
466     gint i, len;
467
468     len = gst_value_list_get_size (format);
469     for (i = 0; i < len; i++) {
470       const GValue *val;
471       const gchar *fname;
472
473       val = gst_value_list_get_value (format, i);
474       if (G_VALUE_HOLDS_STRING (val)) {
475         const GstAudioFormatInfo *t_info;
476         fname = g_value_get_string (val);
477         t_info =
478             gst_audio_format_get_info (gst_audio_format_from_string (fname));
479         if (!t_info)
480           continue;
481         /* accept input format immediately */
482         if (strcmp (fname, in_format) == 0) {
483           out_info = t_info;
484           break;
485         }
486
487         out_flags = GST_AUDIO_FORMAT_INFO_FLAGS (t_info);
488         out_flags &= ~(GST_AUDIO_FORMAT_FLAG_UNPACK);
489         out_flags &= ~(GST_AUDIO_FORMAT_FLAG_SIGNED);
490         /* or another format without losing precision */
491         if (in_flags == out_flags) {
492           if (GST_AUDIO_FORMAT_INFO_DEPTH (t_info) ==
493               GST_AUDIO_FORMAT_INFO_DEPTH (in_info) &&
494               (!out_info
495                   || GST_AUDIO_FORMAT_INFO_DEPTH (out_info) >=
496                   GST_AUDIO_FORMAT_INFO_DEPTH (in_info))) {
497             /* exact match of depth, we still continue
498              * to iterate to see if we can get exactly
499              * the same format.
500              * Only go here if we don't have another
501              * format with the same depth already. We
502              * always take the first to prefer caps
503              * order. */
504             out_info = t_info;
505           } else if ((GST_AUDIO_FORMAT_INFO_DEPTH (t_info) >=
506                   GST_AUDIO_FORMAT_INFO_DEPTH (in_info)) && !out_info) {
507             /* match where we do not lose precision. This could
508              * be ok, but keep searching for an exact match.
509              * Only go here if we don't have another format with
510              * a bigger/equal depth already. We always take the
511              * first to prefer caps order. */
512             out_info = t_info;
513           }
514         }
515       }
516     }
517     if (out_info)
518       gst_structure_set (outs, "format", G_TYPE_STRING,
519           GST_AUDIO_FORMAT_INFO_NAME (out_info), NULL);
520   } else {
521     /* nothing to fixate */
522     return;
523   }
524 }
525
526 static void
527 gst_audio_convert_fixate_channels (GstBaseTransform * base, GstStructure * ins,
528     GstStructure * outs)
529 {
530   gint in_chans, out_chans;
531   guint64 in_mask = 0, out_mask = 0;
532   gboolean has_in_mask = FALSE, has_out_mask = FALSE;
533
534   if (!gst_structure_get_int (ins, "channels", &in_chans))
535     return;                     /* this shouldn't really happen, should it? */
536
537   if (!gst_structure_has_field (outs, "channels")) {
538     /* we could try to get the implied number of channels from the layout,
539      * but that seems overdoing it for a somewhat exotic corner case */
540     gst_structure_remove_field (outs, "channel-mask");
541     return;
542   }
543
544   /* ok, let's fixate the channels if they are not fixated yet */
545   gst_structure_fixate_field_nearest_int (outs, "channels", in_chans);
546
547   if (!gst_structure_get_int (outs, "channels", &out_chans)) {
548     /* shouldn't really happen ... */
549     gst_structure_remove_field (outs, "channel-mask");
550     return;
551   }
552
553   /* get the channel layout of the output if any */
554   has_out_mask = gst_structure_has_field (outs, "channel-mask");
555   if (has_out_mask) {
556     gst_structure_get (outs, "channel-mask", GST_TYPE_BITMASK, &out_mask, NULL);
557   } else {
558     /* channels == 1 => MONO */
559     if (out_chans == 2) {
560       out_mask =
561           GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) |
562           GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT);
563       has_out_mask = TRUE;
564       gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, out_mask,
565           NULL);
566     }
567   }
568
569   /* get the channel layout of the input if any */
570   has_in_mask = gst_structure_has_field (ins, "channel-mask");
571   if (has_in_mask) {
572     gst_structure_get (ins, "channel-mask", GST_TYPE_BITMASK, &in_mask, NULL);
573   } else {
574     /* channels == 1 => MONO */
575     if (in_chans == 2) {
576       in_mask =
577           GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) |
578           GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT);
579       has_in_mask = TRUE;
580     } else if (in_chans > 2)
581       g_warning ("%s: Upstream caps contain no channel mask",
582           GST_ELEMENT_NAME (base));
583   }
584
585   if (!has_out_mask && out_chans == 1 && (in_chans != out_chans
586           || !has_in_mask))
587     return;                     /* nothing to do, default layout will be assumed */
588
589   if (in_chans == out_chans && (has_in_mask || in_chans == 1)) {
590     /* same number of channels and no output layout: just use input layout */
591     if (!has_out_mask) {
592       /* in_chans == 1 handled above already */
593       gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, in_mask, NULL);
594       return;
595     }
596
597     /* If both masks are the same we're done, this includes the NONE layout case */
598     if (in_mask == out_mask)
599       return;
600
601     /* if output layout is fixed already and looks sane, we're done */
602     if (n_bits_set (out_mask) == out_chans)
603       return;
604
605     if (n_bits_set (out_mask) < in_chans) {
606       /* Not much we can do here, this shouldn't just happen */
607       g_warning ("%s: Invalid downstream channel-mask with too few bits set",
608           GST_ELEMENT_NAME (base));
609     } else {
610       guint64 intersection;
611
612       /* if the output layout is not fixed, check if the output layout contains
613        * the input layout */
614       intersection = in_mask & out_mask;
615       if (n_bits_set (intersection) >= in_chans) {
616         gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, in_mask,
617             NULL);
618         return;
619       }
620
621       /* output layout is not fixed and does not contain the input layout, so
622        * just pick the first possibility */
623       intersection = find_suitable_mask (out_mask, out_chans);
624       if (intersection) {
625         gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, intersection,
626             NULL);
627         return;
628       }
629     }
630
631     /* ... else fall back to default layout (NB: out_layout is NULL here) */
632     GST_WARNING_OBJECT (base, "unexpected output channel layout");
633   } else {
634     guint64 intersection;
635
636     /* number of input channels != number of output channels:
637      * if this value contains a list of channel layouts (or even worse: a list
638      * with another list), just pick the first value and repeat until we find a
639      * channel position array or something else that's not a list; we assume
640      * the input if half-way sane and don't try to fall back on other list items
641      * if the first one is something unexpected or non-channel-pos-array-y */
642     if (n_bits_set (out_mask) >= out_chans) {
643       intersection = find_suitable_mask (out_mask, out_chans);
644       gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, intersection,
645           NULL);
646       return;
647     }
648
649     /* what now?! Just ignore what we're given and use default positions */
650     GST_WARNING_OBJECT (base, "invalid or unexpected channel-positions");
651   }
652
653   /* missing or invalid output layout and we can't use the input layout for
654    * one reason or another, so just pick a default layout (we could be smarter
655    * and try to add/remove channels from the input layout, or pick a default
656    * layout based on LFE-presence in input layout, but let's save that for
657    * another day) */
658   if (out_chans > 0 && out_chans <= G_N_ELEMENTS (default_positions[0])) {
659     gint i;
660
661     GST_DEBUG_OBJECT (base, "using default channel layout as fallback");
662
663     out_mask = 0;
664     for (i = 0; i < out_chans; i++)
665       out_mask |= G_GUINT64_CONSTANT (1) << default_positions[out_chans - 1][i];
666
667     gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, out_mask, NULL);
668   } else {
669     GST_ERROR_OBJECT (base, "Have no default layout for %d channels",
670         out_chans);
671   }
672 }
673
674 /* try to keep as many of the structure members the same by fixating the
675  * possible ranges; this way we convert the least amount of things as possible
676  */
677 static GstCaps *
678 gst_audio_convert_fixate_caps (GstBaseTransform * base,
679     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
680 {
681   GstStructure *ins, *outs;
682   GstCaps *result;
683
684   GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
685       " based on caps %" GST_PTR_FORMAT, othercaps, caps);
686
687   result = gst_caps_intersect (othercaps, caps);
688   if (gst_caps_is_empty (result)) {
689     GstCaps *removed;
690
691     if (result)
692       gst_caps_unref (result);
693     /* try to preserve channels */
694     removed = gst_audio_convert_caps_remove_format_info (caps, FALSE);
695     result = gst_caps_intersect (othercaps, removed);
696     gst_caps_unref (removed);
697     if (gst_caps_is_empty (result)) {
698       if (result)
699         gst_caps_unref (result);
700       result = othercaps;
701     } else {
702       gst_caps_unref (othercaps);
703     }
704   } else {
705     gst_caps_unref (othercaps);
706   }
707
708   GST_DEBUG_OBJECT (base, "now fixating %" GST_PTR_FORMAT, result);
709
710   /* fixate remaining fields */
711   result = gst_caps_make_writable (result);
712
713   ins = gst_caps_get_structure (caps, 0);
714   outs = gst_caps_get_structure (result, 0);
715
716   gst_audio_convert_fixate_channels (base, ins, outs);
717   gst_audio_convert_fixate_format (base, ins, outs);
718
719   /* fixate remaining */
720   result = gst_caps_fixate (result);
721
722   GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, result);
723
724   return result;
725 }
726
727 static gboolean
728 gst_audio_convert_set_caps (GstBaseTransform * base, GstCaps * incaps,
729     GstCaps * outcaps)
730 {
731   GstAudioConvert *this = GST_AUDIO_CONVERT (base);
732   GstAudioInfo in_info;
733   GstAudioInfo out_info;
734
735   GST_DEBUG_OBJECT (base, "incaps %" GST_PTR_FORMAT ", outcaps %"
736       GST_PTR_FORMAT, incaps, outcaps);
737
738   if (!gst_audio_info_from_caps (&in_info, incaps))
739     goto invalid_in;
740   if (!gst_audio_info_from_caps (&out_info, outcaps))
741     goto invalid_out;
742
743   if (!audio_convert_prepare_context (&this->ctx, &in_info, &out_info,
744           this->dither, this->ns))
745     goto no_converter;
746
747   return TRUE;
748
749   /* ERRORS */
750 invalid_in:
751   {
752     GST_ERROR_OBJECT (base, "invalid input caps");
753     return FALSE;
754   }
755 invalid_out:
756   {
757     GST_ERROR_OBJECT (base, "invalid output caps");
758     return FALSE;
759   }
760 no_converter:
761   {
762     GST_ERROR_OBJECT (base, "could not find converter");
763     return FALSE;
764   }
765 }
766
767 static GstFlowReturn
768 gst_audio_convert_transform (GstBaseTransform * base, GstBuffer * inbuf,
769     GstBuffer * outbuf)
770 {
771   GstFlowReturn ret;
772   GstAudioConvert *this = GST_AUDIO_CONVERT (base);
773   GstMapInfo srcmap, dstmap;
774   gint insize, outsize;
775
776   gint samples;
777
778   /* get amount of samples to convert. */
779   samples = gst_buffer_get_size (inbuf) / this->ctx.in.bpf;
780
781   /* get in/output sizes, to see if the buffers we got are of correct
782    * sizes */
783   if (!audio_convert_get_sizes (&this->ctx, samples, &insize, &outsize))
784     goto error;
785
786   if (insize == 0 || outsize == 0)
787     return GST_FLOW_OK;
788
789   /* get src and dst data */
790   gst_buffer_map (inbuf, &srcmap, GST_MAP_READ);
791   gst_buffer_map (outbuf, &dstmap, GST_MAP_WRITE);
792
793   /* check in and outsize */
794   if (srcmap.size < insize)
795     goto wrong_size;
796   if (dstmap.size < outsize)
797     goto wrong_size;
798
799   /* and convert the samples */
800   if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
801     if (!audio_convert_convert (&this->ctx, srcmap.data, dstmap.data,
802             samples, gst_buffer_is_writable (inbuf)))
803       goto convert_error;
804   } else {
805     /* Create silence buffer */
806     gst_audio_format_fill_silence (this->ctx.out.finfo, dstmap.data, outsize);
807   }
808   ret = GST_FLOW_OK;
809
810 done:
811   gst_buffer_unmap (outbuf, &dstmap);
812   gst_buffer_unmap (inbuf, &srcmap);
813
814   return ret;
815
816   /* ERRORS */
817 error:
818   {
819     GST_ELEMENT_ERROR (this, STREAM, FORMAT,
820         (NULL), ("cannot get input/output sizes for %d samples", samples));
821     return GST_FLOW_ERROR;
822   }
823 wrong_size:
824   {
825     GST_ELEMENT_ERROR (this, STREAM, FORMAT,
826         (NULL),
827         ("input/output buffers are of wrong size in: %" G_GSIZE_FORMAT " < %d"
828             " or out: %" G_GSIZE_FORMAT " < %d",
829             srcmap.size, insize, dstmap.size, outsize));
830     ret = GST_FLOW_ERROR;
831     goto done;
832   }
833 convert_error:
834   {
835     GST_ELEMENT_ERROR (this, STREAM, FORMAT,
836         (NULL), ("error while converting"));
837     ret = GST_FLOW_ERROR;
838     goto done;
839   }
840 }
841
842 static void
843 gst_audio_convert_set_property (GObject * object, guint prop_id,
844     const GValue * value, GParamSpec * pspec)
845 {
846   GstAudioConvert *this = GST_AUDIO_CONVERT (object);
847
848   switch (prop_id) {
849     case ARG_DITHERING:
850       this->dither = g_value_get_enum (value);
851       break;
852     case ARG_NOISE_SHAPING:
853       this->ns = g_value_get_enum (value);
854       break;
855     default:
856       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
857       break;
858   }
859 }
860
861 static void
862 gst_audio_convert_get_property (GObject * object, guint prop_id,
863     GValue * value, GParamSpec * pspec)
864 {
865   GstAudioConvert *this = GST_AUDIO_CONVERT (object);
866
867   switch (prop_id) {
868     case ARG_DITHERING:
869       g_value_set_enum (value, this->dither);
870       break;
871     case ARG_NOISE_SHAPING:
872       g_value_set_enum (value, this->ns);
873       break;
874     default:
875       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
876       break;
877   }
878 }