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 * @title: audioconvert
27 *
28 * Audioconvert converts raw audio buffers between various possible formats.
29 * It supports integer to float conversion, width/depth conversion,
30 * signedness and endianness conversion and channel transformations
31 * (ie. upmixing and downmixing), as well as dithering and noise-shaping.
32 *
33 * ## Example launch line
34 * |[
35 * gst-launch-1.0 -v -m audiotestsrc ! audioconvert ! audio/x-raw,format=S8,channels=2 ! level ! fakesink silent=TRUE
36 * ]|
37 * This pipeline converts audio to 8-bit. The level element shows that
38 * the output levels still match the one for a sine wave.
39 * |[
40 * gst-launch-1.0 -v -m uridecodebin uri=file:///path/to/audio.flac ! audioconvert ! vorbisenc ! oggmux ! filesink location=audio.ogg
41 * ]|
42 * The vorbis encoder takes float audio data instead of the integer data
43 * output by most other audio elements. This pipeline decodes a FLAC audio file
44 * (or any other audio file for which decoders are installed) and re-encodes
45 * it into an Ogg/Vorbis audio file.
46 *
47 * A mix matrix can be passed to audioconvert, that will govern the
48 * remapping of input to output channels.
49 * ## Example matrix generation code
50 * To generate the matrix using code:
51 *
52 * |[
53 * GValue v = G_VALUE_INIT;
54 * GValue v2 = G_VALUE_INIT;
55 * GValue v3 = G_VALUE_INIT;
56 *
57 * g_value_init (&v2, GST_TYPE_ARRAY);
58 * g_value_init (&v3, G_TYPE_FLOAT);
59 * g_value_set_float (&v3, 1);
60 * gst_value_array_append_value (&v2, &v3);
61 * g_value_unset (&v3);
62 * [ Repeat for as many float as your input channels - unset and reinit v3 ]
63 * g_value_init (&v, GST_TYPE_ARRAY);
64 * gst_value_array_append_value (&v, &v2);
65 * g_value_unset (&v2);
66 * [ Repeat for as many v2's as your output channels - unset and reinit v2]
67 * g_object_set_property (G_OBJECT (audioconvert), "mix-matrix", &v);
68 * g_value_unset (&v);
69 * ]|
70 *
71 * ## Example launch line
72 * |[
73 * gst-launch-1.0 audiotestsrc ! audio/x-raw, channels=4 ! audioconvert mix-matrix="<<1.0, 0.0, 0.0, 0.0>, <0.0, 1.0, 0.0, 0.0>>" ! audio/x-raw,channels=2 ! autoaudiosink
74 * ]|
75 *
76 * > If an empty mix matrix is specified, a (potentially truncated)
77 * > identity matrix will be generated.
78 *
79 * ## Example empty matrix generation code
80 * |[
81 * GValue v = G_VALUE_INIT;
82 *
83 * g_value_init (&v, GST_TYPE_ARRAY);
84 * g_object_set_property (G_OBJECT (audioconvert), "mix-matrix", &v);
85 * g_value_unset (&v);
86 * ]|
87 *
88 * ## Example empty matrix launch line
89 * |[
90 * gst-launch-1.0 -v audiotestsrc ! audio/x-raw,channels=8 ! audioconvert mix-matrix="<>" ! audio/x-raw,channels=16,channel-mask=\(bitmask\)0x0000000000000000 ! fakesink
91 * ]|
92 */
93
94 /*
95 * design decisions:
96 * - audioconvert converts buffers in a set of supported caps. If it supports
97 * a caps, it supports conversion from these caps to any other caps it
98 * supports. (example: if it does A=>B and A=>C, it also does B=>C)
99 * - audioconvert does not save state between buffers. Every incoming buffer is
100 * converted and the converted buffer is pushed out.
101 * conclusion:
102 * audioconvert is not supposed to be a one-element-does-anything solution for
103 * audio conversions.
104 */
105
106 #ifdef HAVE_CONFIG_H
107 #include "config.h"
108 #endif
109
110 #include <string.h>
111
112 #include "gstaudioconvert.h"
113
114 GST_DEBUG_CATEGORY (audio_convert_debug);
115 GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
116 #define GST_CAT_DEFAULT (audio_convert_debug)
117
118 /*** DEFINITIONS **************************************************************/
119
120 /* type functions */
121 static void gst_audio_convert_dispose (GObject * obj);
122
123 /* gstreamer functions */
124 static gboolean gst_audio_convert_get_unit_size (GstBaseTransform * base,
125 GstCaps * caps, gsize * size);
126 static GstCaps *gst_audio_convert_transform_caps (GstBaseTransform * base,
127 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
128 static GstCaps *gst_audio_convert_fixate_caps (GstBaseTransform * base,
129 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
130 static gboolean gst_audio_convert_set_caps (GstBaseTransform * base,
131 GstCaps * incaps, GstCaps * outcaps);
132 static GstFlowReturn gst_audio_convert_transform (GstBaseTransform * base,
133 GstBuffer * inbuf, GstBuffer * outbuf);
134 static GstFlowReturn gst_audio_convert_transform_ip (GstBaseTransform * base,
135 GstBuffer * buf);
136 static gboolean gst_audio_convert_transform_meta (GstBaseTransform * trans,
137 GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf);
138 static GstFlowReturn gst_audio_convert_submit_input_buffer (GstBaseTransform *
139 base, gboolean is_discont, GstBuffer * input);
140 static GstFlowReturn gst_audio_convert_prepare_output_buffer (GstBaseTransform *
141 base, GstBuffer * inbuf, GstBuffer ** outbuf);
142 static void gst_audio_convert_set_property (GObject * object, guint prop_id,
143 const GValue * value, GParamSpec * pspec);
144 static void gst_audio_convert_get_property (GObject * object, guint prop_id,
145 GValue * value, GParamSpec * pspec);
146
147 /* AudioConvert signals and args */
148 enum
149 {
150 /* FILL ME */
151 LAST_SIGNAL
152 };
153
154 enum
155 {
156 PROP_0,
157 PROP_DITHERING,
158 PROP_NOISE_SHAPING,
159 PROP_MIX_MATRIX,
160 };
161
162 #define DEBUG_INIT \
163 GST_DEBUG_CATEGORY_INIT (audio_convert_debug, "audioconvert", 0, "audio conversion element"); \
164 GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
165 #define gst_audio_convert_parent_class parent_class
166 G_DEFINE_TYPE_WITH_CODE (GstAudioConvert, gst_audio_convert,
167 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
168 GST_ELEMENT_REGISTER_DEFINE (audioconvert, "audioconvert",
169 GST_RANK_PRIMARY, GST_TYPE_AUDIO_CONVERT);
170 /*** GSTREAMER PROTOTYPES *****************************************************/
171
172 #define STATIC_CAPS \
173 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL) \
174 ", layout = (string) { interleaved, non-interleaved }")
175
176 static GstStaticPadTemplate gst_audio_convert_src_template =
177 GST_STATIC_PAD_TEMPLATE ("src",
178 GST_PAD_SRC,
179 GST_PAD_ALWAYS,
180 STATIC_CAPS);
181
182 static GstStaticPadTemplate gst_audio_convert_sink_template =
183 GST_STATIC_PAD_TEMPLATE ("sink",
184 GST_PAD_SINK,
185 GST_PAD_ALWAYS,
186 STATIC_CAPS);
187
188 /* cached quark to avoid contention on the global quark table lock */
189 #define META_TAG_AUDIO meta_tag_audio_quark
190 static GQuark meta_tag_audio_quark;
191
192 /*** TYPE FUNCTIONS ***********************************************************/
193 static void
gst_audio_convert_class_init(GstAudioConvertClass * klass)194 gst_audio_convert_class_init (GstAudioConvertClass * klass)
195 {
196 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
197 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
198 GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass);
199
200 gobject_class->dispose = gst_audio_convert_dispose;
201 gobject_class->set_property = gst_audio_convert_set_property;
202 gobject_class->get_property = gst_audio_convert_get_property;
203
204 g_object_class_install_property (gobject_class, PROP_DITHERING,
205 g_param_spec_enum ("dithering", "Dithering",
206 "Selects between different dithering methods.",
207 GST_TYPE_AUDIO_DITHER_METHOD, GST_AUDIO_DITHER_TPDF,
208 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209
210 g_object_class_install_property (gobject_class, PROP_NOISE_SHAPING,
211 g_param_spec_enum ("noise-shaping", "Noise shaping",
212 "Selects between different noise shaping methods.",
213 GST_TYPE_AUDIO_NOISE_SHAPING_METHOD, GST_AUDIO_NOISE_SHAPING_NONE,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215
216 g_object_class_install_property (gobject_class, PROP_MIX_MATRIX,
217 gst_param_spec_array ("mix-matrix",
218 "Input/output channel matrix",
219 "Transformation matrix for input/output channels",
220 gst_param_spec_array ("matrix-rows", "rows", "rows",
221 g_param_spec_float ("matrix-cols", "cols", "cols",
222 -1, 1, 0,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226
227 gst_element_class_add_static_pad_template (element_class,
228 &gst_audio_convert_src_template);
229 gst_element_class_add_static_pad_template (element_class,
230 &gst_audio_convert_sink_template);
231 gst_element_class_set_static_metadata (element_class, "Audio converter",
232 "Filter/Converter/Audio", "Convert audio to different formats",
233 "Benjamin Otte <otte@gnome.org>");
234
235 basetransform_class->get_unit_size =
236 GST_DEBUG_FUNCPTR (gst_audio_convert_get_unit_size);
237 basetransform_class->transform_caps =
238 GST_DEBUG_FUNCPTR (gst_audio_convert_transform_caps);
239 basetransform_class->fixate_caps =
240 GST_DEBUG_FUNCPTR (gst_audio_convert_fixate_caps);
241 basetransform_class->set_caps =
242 GST_DEBUG_FUNCPTR (gst_audio_convert_set_caps);
243 basetransform_class->transform =
244 GST_DEBUG_FUNCPTR (gst_audio_convert_transform);
245 basetransform_class->transform_ip =
246 GST_DEBUG_FUNCPTR (gst_audio_convert_transform_ip);
247 basetransform_class->transform_meta =
248 GST_DEBUG_FUNCPTR (gst_audio_convert_transform_meta);
249 basetransform_class->submit_input_buffer =
250 GST_DEBUG_FUNCPTR (gst_audio_convert_submit_input_buffer);
251 basetransform_class->prepare_output_buffer =
252 GST_DEBUG_FUNCPTR (gst_audio_convert_prepare_output_buffer);
253
254 basetransform_class->transform_ip_on_passthrough = FALSE;
255
256 meta_tag_audio_quark = g_quark_from_static_string (GST_META_TAG_AUDIO_STR);
257 }
258
259 static void
gst_audio_convert_init(GstAudioConvert * this)260 gst_audio_convert_init (GstAudioConvert * this)
261 {
262 this->dither = GST_AUDIO_DITHER_TPDF;
263 this->ns = GST_AUDIO_NOISE_SHAPING_NONE;
264 g_value_init (&this->mix_matrix, GST_TYPE_ARRAY);
265
266 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (this), TRUE);
267 }
268
269 static void
gst_audio_convert_dispose(GObject * obj)270 gst_audio_convert_dispose (GObject * obj)
271 {
272 GstAudioConvert *this = GST_AUDIO_CONVERT (obj);
273
274 if (this->convert) {
275 gst_audio_converter_free (this->convert);
276 this->convert = NULL;
277 }
278
279 g_value_unset (&this->mix_matrix);
280
281 G_OBJECT_CLASS (parent_class)->dispose (obj);
282 }
283
284 /*** GSTREAMER FUNCTIONS ******************************************************/
285
286 /* BaseTransform vmethods */
287 static gboolean
gst_audio_convert_get_unit_size(GstBaseTransform * base,GstCaps * caps,gsize * size)288 gst_audio_convert_get_unit_size (GstBaseTransform * base, GstCaps * caps,
289 gsize * size)
290 {
291 GstAudioInfo info;
292
293 g_assert (size);
294
295 if (!gst_audio_info_from_caps (&info, caps))
296 goto parse_error;
297
298 *size = info.bpf;
299 GST_INFO_OBJECT (base, "unit_size = %" G_GSIZE_FORMAT, *size);
300
301 return TRUE;
302
303 parse_error:
304 {
305 GST_INFO_OBJECT (base, "failed to parse caps to get unit_size");
306 return FALSE;
307 }
308 }
309
310 static gboolean
remove_format_from_structure(GstCapsFeatures * features,GstStructure * structure,gpointer user_data G_GNUC_UNUSED)311 remove_format_from_structure (GstCapsFeatures * features,
312 GstStructure * structure, gpointer user_data G_GNUC_UNUSED)
313 {
314 gst_structure_remove_field (structure, "format");
315 return TRUE;
316 }
317
318 static gboolean
remove_layout_from_structure(GstCapsFeatures * features,GstStructure * structure,gpointer user_data G_GNUC_UNUSED)319 remove_layout_from_structure (GstCapsFeatures * features,
320 GstStructure * structure, gpointer user_data G_GNUC_UNUSED)
321 {
322 gst_structure_remove_field (structure, "layout");
323 return TRUE;
324 }
325
326 static gboolean
remove_channels_from_structure(GstCapsFeatures * features,GstStructure * s,gpointer user_data)327 remove_channels_from_structure (GstCapsFeatures * features, GstStructure * s,
328 gpointer user_data)
329 {
330 guint64 mask;
331 gint channels;
332 GstAudioConvert *this = GST_AUDIO_CONVERT (user_data);
333
334 /* Only remove the channels and channel-mask for non-NONE layouts,
335 * or if a mix matrix was manually specified */
336 if (this->mix_matrix_is_set ||
337 !gst_structure_get (s, "channel-mask", GST_TYPE_BITMASK, &mask, NULL) ||
338 (mask != 0 || (gst_structure_get_int (s, "channels", &channels)
339 && channels == 1))) {
340 gst_structure_remove_fields (s, "channel-mask", "channels", NULL);
341 }
342
343 return TRUE;
344 }
345
346 static gboolean
add_other_channels_to_structure(GstCapsFeatures * features,GstStructure * s,gpointer user_data)347 add_other_channels_to_structure (GstCapsFeatures * features, GstStructure * s,
348 gpointer user_data)
349 {
350 gint other_channels = GPOINTER_TO_INT (user_data);
351
352 gst_structure_set (s, "channels", G_TYPE_INT, other_channels, NULL);
353
354 return TRUE;
355 }
356
357 /* The caps can be transformed into any other caps with format info removed.
358 * However, we should prefer passthrough, so if passthrough is possible,
359 * put it first in the list. */
360 static GstCaps *
gst_audio_convert_transform_caps(GstBaseTransform * btrans,GstPadDirection direction,GstCaps * caps,GstCaps * filter)361 gst_audio_convert_transform_caps (GstBaseTransform * btrans,
362 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
363 {
364 GstCaps *tmp, *tmp2;
365 GstCaps *result;
366 GstAudioConvert *this = GST_AUDIO_CONVERT (btrans);
367
368 tmp = gst_caps_copy (caps);
369
370 gst_caps_map_in_place (tmp, remove_format_from_structure, NULL);
371 gst_caps_map_in_place (tmp, remove_layout_from_structure, NULL);
372 gst_caps_map_in_place (tmp, remove_channels_from_structure, btrans);
373
374 /* We can infer the required input / output channels based on the
375 * matrix dimensions */
376 if (gst_value_array_get_size (&this->mix_matrix)) {
377 gint other_channels;
378
379 if (direction == GST_PAD_SRC) {
380 const GValue *first_row =
381 gst_value_array_get_value (&this->mix_matrix, 0);
382 other_channels = gst_value_array_get_size (first_row);
383 } else {
384 other_channels = gst_value_array_get_size (&this->mix_matrix);
385 }
386
387 gst_caps_map_in_place (tmp, add_other_channels_to_structure,
388 GINT_TO_POINTER (other_channels));
389 }
390
391 if (filter) {
392 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
393 gst_caps_unref (tmp);
394 tmp = tmp2;
395 }
396
397 result = tmp;
398
399 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
400 GST_PTR_FORMAT, caps, result);
401
402 return result;
403 }
404
405 /* Count the number of bits set
406 * Optimized for the common case, assuming that the number of channels
407 * (i.e. bits set) is small
408 */
409 static gint
n_bits_set(guint64 x)410 n_bits_set (guint64 x)
411 {
412 gint c;
413
414 for (c = 0; x; c++)
415 x &= x - 1;
416
417 return c;
418 }
419
420 /* Reduce the mask to the n_chans lowest set bits
421 *
422 * The algorithm clears the n_chans lowest set bits and subtracts the
423 * result from the original mask to get the desired mask.
424 * It is optimized for the common case where n_chans is a small
425 * number. In the worst case, however, it stops after 64 iterations.
426 */
427 static guint64
find_suitable_mask(guint64 mask,gint n_chans)428 find_suitable_mask (guint64 mask, gint n_chans)
429 {
430 guint64 x = mask;
431
432 for (; x && n_chans; n_chans--)
433 x &= x - 1;
434
435 g_assert (x || n_chans == 0);
436 /* assertion fails if mask contained less bits than n_chans
437 * or n_chans was < 0 */
438
439 return mask - x;
440 }
441
442 static void
gst_audio_convert_fixate_format(GstBaseTransform * base,GstStructure * ins,GstStructure * outs)443 gst_audio_convert_fixate_format (GstBaseTransform * base, GstStructure * ins,
444 GstStructure * outs)
445 {
446 const gchar *in_format;
447 const GValue *format;
448 const GstAudioFormatInfo *in_info, *out_info = NULL;
449 GstAudioFormatFlags in_flags, out_flags = 0;
450 gint in_depth, out_depth = -1;
451 gint i, len;
452
453 in_format = gst_structure_get_string (ins, "format");
454 if (!in_format)
455 return;
456
457 format = gst_structure_get_value (outs, "format");
458 /* should not happen */
459 if (format == NULL)
460 return;
461
462 /* nothing to fixate? */
463 if (!GST_VALUE_HOLDS_LIST (format))
464 return;
465
466 in_info =
467 gst_audio_format_get_info (gst_audio_format_from_string (in_format));
468 if (!in_info)
469 return;
470
471 in_flags = GST_AUDIO_FORMAT_INFO_FLAGS (in_info);
472 in_flags &= ~(GST_AUDIO_FORMAT_FLAG_UNPACK);
473 in_flags &= ~(GST_AUDIO_FORMAT_FLAG_SIGNED);
474
475 in_depth = GST_AUDIO_FORMAT_INFO_DEPTH (in_info);
476
477 len = gst_value_list_get_size (format);
478 for (i = 0; i < len; i++) {
479 const GstAudioFormatInfo *t_info;
480 GstAudioFormatFlags t_flags;
481 gboolean t_flags_better;
482 const GValue *val;
483 const gchar *fname;
484 gint t_depth;
485
486 val = gst_value_list_get_value (format, i);
487 if (!G_VALUE_HOLDS_STRING (val))
488 continue;
489
490 fname = g_value_get_string (val);
491 t_info = gst_audio_format_get_info (gst_audio_format_from_string (fname));
492 if (!t_info)
493 continue;
494
495 /* accept input format immediately */
496 if (strcmp (fname, in_format) == 0) {
497 out_info = t_info;
498 break;
499 }
500
501 t_flags = GST_AUDIO_FORMAT_INFO_FLAGS (t_info);
502 t_flags &= ~(GST_AUDIO_FORMAT_FLAG_UNPACK);
503 t_flags &= ~(GST_AUDIO_FORMAT_FLAG_SIGNED);
504
505 t_depth = GST_AUDIO_FORMAT_INFO_DEPTH (t_info);
506
507 /* Any output format is better than no output format at all */
508 if (!out_info) {
509 out_info = t_info;
510 out_depth = t_depth;
511 out_flags = t_flags;
512 continue;
513 }
514
515 t_flags_better = (t_flags == in_flags && out_flags != in_flags);
516
517 if (t_depth == in_depth && (out_depth != in_depth || t_flags_better)) {
518 /* Prefer to use the first format that has the same depth with the same
519 * flags, and if none with the same flags exist use the first other one
520 * that has the same depth */
521 out_info = t_info;
522 out_depth = t_depth;
523 out_flags = t_flags;
524 } else if (t_depth >= in_depth && (in_depth > out_depth
525 || (out_depth >= in_depth && t_flags_better))) {
526 /* Otherwise use the first format that has a higher depth with the same flags,
527 * if none with the same flags exist use the first other one that has a higher
528 * depth */
529 out_info = t_info;
530 out_depth = t_depth;
531 out_flags = t_flags;
532 } else if ((t_depth > out_depth && out_depth < in_depth)
533 || (t_flags_better && out_depth == t_depth)) {
534 /* Else get at least the one with the highest depth, ideally with the same flags */
535 out_info = t_info;
536 out_depth = t_depth;
537 out_flags = t_flags;
538 }
539
540 }
541
542 if (out_info)
543 gst_structure_set (outs, "format", G_TYPE_STRING,
544 GST_AUDIO_FORMAT_INFO_NAME (out_info), NULL);
545 }
546
547 static void
gst_audio_convert_fixate_channels(GstBaseTransform * base,GstStructure * ins,GstStructure * outs)548 gst_audio_convert_fixate_channels (GstBaseTransform * base, GstStructure * ins,
549 GstStructure * outs)
550 {
551 gint in_chans, out_chans;
552 guint64 in_mask = 0, out_mask = 0;
553 gboolean has_in_mask = FALSE, has_out_mask = FALSE;
554
555 if (!gst_structure_get_int (ins, "channels", &in_chans))
556 return; /* this shouldn't really happen, should it? */
557
558 if (!gst_structure_has_field (outs, "channels")) {
559 /* we could try to get the implied number of channels from the layout,
560 * but that seems overdoing it for a somewhat exotic corner case */
561 gst_structure_remove_field (outs, "channel-mask");
562 return;
563 }
564
565 /* ok, let's fixate the channels if they are not fixated yet */
566 gst_structure_fixate_field_nearest_int (outs, "channels", in_chans);
567
568 if (!gst_structure_get_int (outs, "channels", &out_chans)) {
569 /* shouldn't really happen ... */
570 gst_structure_remove_field (outs, "channel-mask");
571 return;
572 }
573
574 /* get the channel layout of the output if any */
575 has_out_mask = gst_structure_has_field (outs, "channel-mask");
576 if (has_out_mask) {
577 gst_structure_get (outs, "channel-mask", GST_TYPE_BITMASK, &out_mask, NULL);
578 } else {
579 /* channels == 1 => MONO */
580 if (out_chans == 2) {
581 out_mask =
582 GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) |
583 GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT);
584 has_out_mask = TRUE;
585 gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, out_mask,
586 NULL);
587 }
588 }
589
590 /* get the channel layout of the input if any */
591 has_in_mask = gst_structure_has_field (ins, "channel-mask");
592 if (has_in_mask) {
593 gst_structure_get (ins, "channel-mask", GST_TYPE_BITMASK, &in_mask, NULL);
594 } else {
595 /* channels == 1 => MONO */
596 if (in_chans == 2) {
597 in_mask =
598 GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) |
599 GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT);
600 has_in_mask = TRUE;
601 } else if (in_chans > 2)
602 g_warning ("%s: Upstream caps contain no channel mask",
603 GST_ELEMENT_NAME (base));
604 }
605
606 if (!has_out_mask && out_chans == 1 && (in_chans != out_chans
607 || !has_in_mask))
608 return; /* nothing to do, default layout will be assumed */
609
610 if (in_chans == out_chans && (has_in_mask || in_chans == 1)) {
611 /* same number of channels and no output layout: just use input layout */
612 if (!has_out_mask) {
613 /* in_chans == 1 handled above already */
614 gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, in_mask, NULL);
615 return;
616 }
617
618 /* If both masks are the same we're done, this includes the NONE layout case */
619 if (in_mask == out_mask)
620 return;
621
622 /* if output layout is fixed already and looks sane, we're done */
623 if (n_bits_set (out_mask) == out_chans)
624 return;
625
626 if (n_bits_set (out_mask) < in_chans) {
627 /* Not much we can do here, this shouldn't just happen */
628 g_warning ("%s: Invalid downstream channel-mask with too few bits set",
629 GST_ELEMENT_NAME (base));
630 } else {
631 guint64 intersection;
632
633 /* if the output layout is not fixed, check if the output layout contains
634 * the input layout */
635 intersection = in_mask & out_mask;
636 if (n_bits_set (intersection) >= in_chans) {
637 gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, in_mask,
638 NULL);
639 return;
640 }
641
642 /* output layout is not fixed and does not contain the input layout, so
643 * just pick the first possibility */
644 intersection = find_suitable_mask (out_mask, out_chans);
645 if (intersection) {
646 gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, intersection,
647 NULL);
648 return;
649 }
650 }
651
652 /* ... else fall back to default layout (NB: out_layout is NULL here) */
653 GST_WARNING_OBJECT (base, "unexpected output channel layout");
654 } else {
655 guint64 intersection;
656
657 /* number of input channels != number of output channels:
658 * if this value contains a list of channel layouts (or even worse: a list
659 * with another list), just pick the first value and repeat until we find a
660 * channel position array or something else that's not a list; we assume
661 * the input if half-way sane and don't try to fall back on other list items
662 * if the first one is something unexpected or non-channel-pos-array-y */
663 if (n_bits_set (out_mask) >= out_chans) {
664 intersection = find_suitable_mask (out_mask, out_chans);
665 gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, intersection,
666 NULL);
667 return;
668 }
669
670 /* what now?! Just ignore what we're given and use default positions */
671 GST_WARNING_OBJECT (base, "invalid or unexpected channel-positions");
672 }
673
674 /* missing or invalid output layout and we can't use the input layout for
675 * one reason or another, so just pick a default layout (we could be smarter
676 * and try to add/remove channels from the input layout, or pick a default
677 * layout based on LFE-presence in input layout, but let's save that for
678 * another day). For mono, no mask is required and the fallback mask is 0 */
679 if (out_chans > 1
680 && (out_mask = gst_audio_channel_get_fallback_mask (out_chans))) {
681 GST_DEBUG_OBJECT (base, "using default channel layout as fallback");
682 gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK, out_mask, NULL);
683 } else if (out_chans > 1) {
684 GST_ERROR_OBJECT (base, "Have no default layout for %d channels",
685 out_chans);
686 gst_structure_set (outs, "channel-mask", GST_TYPE_BITMASK,
687 G_GUINT64_CONSTANT (0), NULL);
688 }
689 }
690
691 /* try to keep as many of the structure members the same by fixating the
692 * possible ranges; this way we convert the least amount of things as possible
693 */
694 static GstCaps *
gst_audio_convert_fixate_caps(GstBaseTransform * base,GstPadDirection direction,GstCaps * caps,GstCaps * othercaps)695 gst_audio_convert_fixate_caps (GstBaseTransform * base,
696 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
697 {
698 GstStructure *ins, *outs;
699 GstCaps *result;
700
701 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
702 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
703
704 result = gst_caps_intersect (othercaps, caps);
705 if (gst_caps_is_empty (result)) {
706 GstCaps *removed = gst_caps_copy (caps);
707
708 if (result)
709 gst_caps_unref (result);
710 gst_caps_map_in_place (removed, remove_format_from_structure, NULL);
711 gst_caps_map_in_place (removed, remove_layout_from_structure, NULL);
712 result = gst_caps_intersect (othercaps, removed);
713 gst_caps_unref (removed);
714 if (gst_caps_is_empty (result)) {
715 if (result)
716 gst_caps_unref (result);
717 result = othercaps;
718 } else {
719 gst_caps_unref (othercaps);
720 }
721 } else {
722 gst_caps_unref (othercaps);
723 }
724
725 GST_DEBUG_OBJECT (base, "now fixating %" GST_PTR_FORMAT, result);
726
727 /* fixate remaining fields */
728 result = gst_caps_make_writable (result);
729
730 ins = gst_caps_get_structure (caps, 0);
731 outs = gst_caps_get_structure (result, 0);
732
733 gst_audio_convert_fixate_channels (base, ins, outs);
734 gst_audio_convert_fixate_format (base, ins, outs);
735
736 /* fixate remaining */
737 result = gst_caps_fixate (result);
738
739 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, result);
740
741 return result;
742 }
743
744 static gboolean
gst_audio_convert_set_caps(GstBaseTransform * base,GstCaps * incaps,GstCaps * outcaps)745 gst_audio_convert_set_caps (GstBaseTransform * base, GstCaps * incaps,
746 GstCaps * outcaps)
747 {
748 GstAudioConvert *this = GST_AUDIO_CONVERT (base);
749 GstAudioInfo in_info;
750 GstAudioInfo out_info;
751 gboolean in_place;
752 GstStructure *config;
753
754 GST_DEBUG_OBJECT (base, "incaps %" GST_PTR_FORMAT ", outcaps %"
755 GST_PTR_FORMAT, incaps, outcaps);
756
757 if (this->convert) {
758 gst_audio_converter_free (this->convert);
759 this->convert = NULL;
760 }
761
762 if (!gst_audio_info_from_caps (&in_info, incaps))
763 goto invalid_in;
764 if (!gst_audio_info_from_caps (&out_info, outcaps))
765 goto invalid_out;
766
767 config = gst_structure_new ("GstAudioConverterConfig",
768 GST_AUDIO_CONVERTER_OPT_DITHER_METHOD, GST_TYPE_AUDIO_DITHER_METHOD,
769 this->dither,
770 GST_AUDIO_CONVERTER_OPT_NOISE_SHAPING_METHOD,
771 GST_TYPE_AUDIO_NOISE_SHAPING_METHOD, this->ns, NULL);
772
773 if (this->mix_matrix_is_set)
774 gst_structure_set_value (config, GST_AUDIO_CONVERTER_OPT_MIX_MATRIX,
775 &this->mix_matrix);
776
777 this->convert = gst_audio_converter_new (0, &in_info, &out_info, config);
778
779 if (this->convert == NULL)
780 goto no_converter;
781
782 in_place = gst_audio_converter_supports_inplace (this->convert);
783 gst_base_transform_set_in_place (base, in_place);
784
785 gst_base_transform_set_passthrough (base,
786 gst_audio_converter_is_passthrough (this->convert));
787
788 this->in_info = in_info;
789 this->out_info = out_info;
790
791 return TRUE;
792
793 /* ERRORS */
794 invalid_in:
795 {
796 GST_ERROR_OBJECT (base, "invalid input caps");
797 return FALSE;
798 }
799 invalid_out:
800 {
801 GST_ERROR_OBJECT (base, "invalid output caps");
802 return FALSE;
803 }
804 no_converter:
805 {
806 GST_ERROR_OBJECT (base, "could not make converter");
807 return FALSE;
808 }
809 }
810
811 /* if called through gst_audio_convert_transform_ip() inbuf == outbuf */
812 static GstFlowReturn
gst_audio_convert_transform(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer * outbuf)813 gst_audio_convert_transform (GstBaseTransform * base, GstBuffer * inbuf,
814 GstBuffer * outbuf)
815 {
816 GstFlowReturn ret;
817 GstAudioConvert *this = GST_AUDIO_CONVERT (base);
818 GstAudioBuffer srcabuf, dstabuf;
819 gboolean inbuf_writable;
820 GstAudioConverterFlags flags;
821
822 /* https://bugzilla.gnome.org/show_bug.cgi?id=396835 */
823 if (gst_buffer_get_size (inbuf) == 0)
824 return GST_FLOW_OK;
825
826 if (inbuf != outbuf) {
827 inbuf_writable = gst_buffer_is_writable (inbuf)
828 && gst_buffer_n_memory (inbuf) == 1
829 && gst_memory_is_writable (gst_buffer_peek_memory (inbuf, 0));
830
831 if (!gst_audio_buffer_map (&srcabuf, &this->in_info, inbuf,
832 inbuf_writable ? GST_MAP_READWRITE : GST_MAP_READ))
833 goto inmap_error;
834 } else {
835 inbuf_writable = TRUE;
836 }
837
838 if (!gst_audio_buffer_map (&dstabuf, &this->out_info, outbuf, GST_MAP_WRITE))
839 goto outmap_error;
840
841 /* and convert the samples */
842 flags = 0;
843 if (inbuf_writable)
844 flags |= GST_AUDIO_CONVERTER_FLAG_IN_WRITABLE;
845
846 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
847 if (!gst_audio_converter_samples (this->convert, flags,
848 inbuf != outbuf ? srcabuf.planes : dstabuf.planes,
849 dstabuf.n_samples, dstabuf.planes, dstabuf.n_samples))
850 goto convert_error;
851 } else {
852 /* Create silence buffer */
853 gint i;
854 for (i = 0; i < dstabuf.n_planes; i++) {
855 gst_audio_format_info_fill_silence (this->out_info.finfo,
856 dstabuf.planes[i], GST_AUDIO_BUFFER_PLANE_SIZE (&dstabuf));
857 }
858 }
859 ret = GST_FLOW_OK;
860
861 done:
862 gst_audio_buffer_unmap (&dstabuf);
863 if (inbuf != outbuf)
864 gst_audio_buffer_unmap (&srcabuf);
865
866 return ret;
867
868 /* ERRORS */
869 convert_error:
870 {
871 GST_ELEMENT_ERROR (this, STREAM, FORMAT,
872 (NULL), ("error while converting"));
873 ret = GST_FLOW_ERROR;
874 goto done;
875 }
876 inmap_error:
877 {
878 GST_ELEMENT_ERROR (this, STREAM, FORMAT,
879 (NULL), ("failed to map input buffer"));
880 return GST_FLOW_ERROR;
881 }
882 outmap_error:
883 {
884 GST_ELEMENT_ERROR (this, STREAM, FORMAT,
885 (NULL), ("failed to map output buffer"));
886 if (inbuf != outbuf)
887 gst_audio_buffer_unmap (&srcabuf);
888 return GST_FLOW_ERROR;
889 }
890 }
891
892 static GstFlowReturn
gst_audio_convert_transform_ip(GstBaseTransform * base,GstBuffer * buf)893 gst_audio_convert_transform_ip (GstBaseTransform * base, GstBuffer * buf)
894 {
895 return gst_audio_convert_transform (base, buf, buf);
896 }
897
898 static gboolean
gst_audio_convert_transform_meta(GstBaseTransform * trans,GstBuffer * outbuf,GstMeta * meta,GstBuffer * inbuf)899 gst_audio_convert_transform_meta (GstBaseTransform * trans, GstBuffer * outbuf,
900 GstMeta * meta, GstBuffer * inbuf)
901 {
902 const GstMetaInfo *info = meta->info;
903 const gchar *const *tags;
904
905 tags = gst_meta_api_type_get_tags (info->api);
906
907 if (!tags || (g_strv_length ((gchar **) tags) == 1
908 && gst_meta_api_type_has_tag (info->api, META_TAG_AUDIO)))
909 return TRUE;
910
911 return FALSE;
912 }
913
914 static GstFlowReturn
gst_audio_convert_submit_input_buffer(GstBaseTransform * base,gboolean is_discont,GstBuffer * input)915 gst_audio_convert_submit_input_buffer (GstBaseTransform * base,
916 gboolean is_discont, GstBuffer * input)
917 {
918 GstAudioConvert *this = GST_AUDIO_CONVERT (base);
919
920 if (base->segment.format == GST_FORMAT_TIME) {
921 input =
922 gst_audio_buffer_clip (input, &base->segment, this->in_info.rate,
923 this->in_info.bpf);
924
925 if (!input)
926 return GST_FLOW_OK;
927 }
928
929 return GST_BASE_TRANSFORM_CLASS (parent_class)->submit_input_buffer (base,
930 is_discont, input);
931 }
932
933 static GstFlowReturn
gst_audio_convert_prepare_output_buffer(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer ** outbuf)934 gst_audio_convert_prepare_output_buffer (GstBaseTransform * base,
935 GstBuffer * inbuf, GstBuffer ** outbuf)
936 {
937 GstAudioConvert *this = GST_AUDIO_CONVERT (base);
938 GstAudioMeta *meta;
939 GstFlowReturn ret;
940
941 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->prepare_output_buffer (base,
942 inbuf, outbuf);
943
944 if (ret != GST_FLOW_OK)
945 return ret;
946
947 meta = gst_buffer_get_audio_meta (inbuf);
948
949 if (inbuf != *outbuf) {
950 gsize samples = meta ?
951 meta->samples : (gst_buffer_get_size (inbuf) / this->in_info.bpf);
952
953 /* ensure that the output buffer is not bigger than what we need */
954 gst_buffer_resize (*outbuf, 0, samples * this->out_info.bpf);
955
956 /* add the audio meta on the output buffer if it's planar */
957 if (this->out_info.layout == GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
958 gst_buffer_add_audio_meta (*outbuf, &this->out_info, samples, NULL);
959 }
960 } else {
961 /* if the input buffer came with a GstAudioMeta,
962 * update it to reflect the properties of the output format */
963 if (meta)
964 meta->info = this->out_info;
965 }
966
967 return ret;
968 }
969
970 static void
gst_audio_convert_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)971 gst_audio_convert_set_property (GObject * object, guint prop_id,
972 const GValue * value, GParamSpec * pspec)
973 {
974 GstAudioConvert *this = GST_AUDIO_CONVERT (object);
975
976 switch (prop_id) {
977 case PROP_DITHERING:
978 this->dither = g_value_get_enum (value);
979 break;
980 case PROP_NOISE_SHAPING:
981 this->ns = g_value_get_enum (value);
982 break;
983 case PROP_MIX_MATRIX:
984 if (!gst_value_array_get_size (value)) {
985 this->mix_matrix_is_set = FALSE;
986 } else {
987 const GValue *first_row = gst_value_array_get_value (value, 0);
988
989 if (gst_value_array_get_size (first_row)) {
990 g_value_copy (value, &this->mix_matrix);
991 this->mix_matrix_is_set = TRUE;
992
993 /* issue a reconfigure upstream */
994 gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (this));
995 } else {
996 g_warning ("Empty mix matrix's first row");
997 }
998 }
999 break;
1000 default:
1001 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1002 break;
1003 }
1004 }
1005
1006 static void
gst_audio_convert_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1007 gst_audio_convert_get_property (GObject * object, guint prop_id,
1008 GValue * value, GParamSpec * pspec)
1009 {
1010 GstAudioConvert *this = GST_AUDIO_CONVERT (object);
1011
1012 switch (prop_id) {
1013 case PROP_DITHERING:
1014 g_value_set_enum (value, this->dither);
1015 break;
1016 case PROP_NOISE_SHAPING:
1017 g_value_set_enum (value, this->ns);
1018 break;
1019 case PROP_MIX_MATRIX:
1020 if (this->mix_matrix_is_set)
1021 g_value_copy (&this->mix_matrix, value);
1022 break;
1023 default:
1024 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1025 break;
1026 }
1027 }
1028