1 /*
2  * GStreamer
3  * Copyright (C) 2017 Vivia Nikolaidou <vivia@toolsonair.com>
4  *
5  * gstaudiomixmatrix.c
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /**
24  * SECTION:element-audiomixmatrix
25  * @title: audiomixmatrix
26  * @short_description: Transform input/output channels according to a matrix
27  *
28  * This element transforms a given number of input channels into a given
29  * number of output channels according to a given transformation matrix. The
30  * matrix coefficients must be between -1 and 1: the number of rows is equal
31  * to the number of output channels and the number of columns is equal to the
32  * number of input channels. In the first-channels mode, input/output channels
33  * are automatically negotiated and the transformation matrix is a truncated
34  * identity matrix.
35  *
36  * ## Example matrix generation code
37  * To generate the matrix using code:
38  *
39  * |[
40  * GValue v = G_VALUE_INIT;
41  * GValue v2 = G_VALUE_INIT;
42  * GValue v3 = G_VALUE_INIT;
43  *
44  * g_value_init (&v2, GST_TYPE_ARRAY);
45  * g_value_init (&v3, G_TYPE_DOUBLE);
46  * g_value_set_double (&v3, 1);
47  * gst_value_array_append_value (&v2, &v3);
48  * g_value_unset (&v3);
49  * [ Repeat for as many double as your input channels - unset and reinit v3 ]
50  * g_value_init (&v, GST_TYPE_ARRAY);
51  * gst_value_array_append_value (&v, &v2);
52  * g_value_unset (&v2);
53  * [ Repeat for as many v2's as your output channels - unset and reinit v2]
54  * g_object_set_property (G_OBJECT (audiomixmatrix), "matrix", &v);
55  * g_value_unset (&v);
56  * ]|
57  *
58  * ## Example launch line
59  * |[
60  * gst-launch-1.0 audiotestsrc ! audio/x-raw,channels=4 ! audiomixmatrix in-channels=4 out-channels=2 channel-mask=-1 matrix="<<(double)1, (double)0, (double)0, (double)0>, <0.0, 1.0, 0.0, 0.0>>" ! audio/x-raw,channels=2 ! autoaudiosink
61  * ]|
62  *
63  */
64 
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68 
69 #include "gstaudiomixmatrix.h"
70 
71 #include <gst/gst.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <math.h>
75 
76 GST_DEBUG_CATEGORY_STATIC (audiomixmatrix_debug);
77 #define GST_CAT_DEFAULT audiomixmatrix_debug
78 
79 /* GstAudioMixMatrix properties */
80 enum
81 {
82   PROP_0,
83   PROP_IN_CHANNELS,
84   PROP_OUT_CHANNELS,
85   PROP_MATRIX,
86   PROP_CHANNEL_MASK,
87   PROP_MODE
88 };
89 
90 GType
gst_audio_mix_matrix_mode_get_type(void)91 gst_audio_mix_matrix_mode_get_type (void)
92 {
93   static GType gst_audio_mix_matrix_mode_type = 0;
94   static const GEnumValue gst_audio_mix_matrix_mode[] = {
95     {GST_AUDIO_MIX_MATRIX_MODE_MANUAL,
96           "Manual mode: please specify input/output channels and transformation matrix",
97         "manual"},
98     {GST_AUDIO_MIX_MATRIX_MODE_FIRST_CHANNELS,
99           "First channels mode: input/output channels are auto-negotiated, transformation matrix is a truncated identity matrix",
100         "first-channels"},
101     {0, NULL, NULL}
102 
103   };
104 
105   if (!gst_audio_mix_matrix_mode_type) {
106     gst_audio_mix_matrix_mode_type =
107         g_enum_register_static ("GstAudioMixMatrixModeType",
108         gst_audio_mix_matrix_mode);
109   }
110   return gst_audio_mix_matrix_mode_type;
111 }
112 
113 static GstStaticPadTemplate gst_audio_mix_matrix_src_template =
114 GST_STATIC_PAD_TEMPLATE ("src",
115     GST_PAD_SRC,
116     GST_PAD_ALWAYS,
117     GST_STATIC_CAPS
118     ("audio/x-raw, channels = [1, max], layout = (string) interleaved, format = (string) {"
119         GST_AUDIO_NE (F32) "," GST_AUDIO_NE (F64) "," GST_AUDIO_NE (S16) ","
120         GST_AUDIO_NE (S32) "}")
121     );
122 
123 static GstStaticPadTemplate gst_audio_mix_matrix_sink_template =
124 GST_STATIC_PAD_TEMPLATE ("sink",
125     GST_PAD_SINK,
126     GST_PAD_ALWAYS,
127     GST_STATIC_CAPS
128     ("audio/x-raw, channels = [1, max], layout = (string) interleaved, format = (string) {"
129         GST_AUDIO_NE (F32) "," GST_AUDIO_NE (F64) "," GST_AUDIO_NE (S16) ","
130         GST_AUDIO_NE (S32) "}")
131     );
132 
133 static void gst_audio_mix_matrix_set_property (GObject * object, guint prop_id,
134     const GValue * value, GParamSpec * pspec);
135 static void gst_audio_mix_matrix_get_property (GObject * object, guint prop_id,
136     GValue * value, GParamSpec * pspec);
137 static void gst_audio_mix_matrix_dispose (GObject * object);
138 static gboolean gst_audio_mix_matrix_get_unit_size (GstBaseTransform * trans,
139     GstCaps * caps, gsize * size);
140 static gboolean gst_audio_mix_matrix_set_caps (GstBaseTransform * trans,
141     GstCaps * incaps, GstCaps * outcaps);
142 static GstFlowReturn gst_audio_mix_matrix_transform (GstBaseTransform * trans,
143     GstBuffer * inbuf, GstBuffer * outbuf);
144 static GstCaps *gst_audio_mix_matrix_transform_caps (GstBaseTransform * trans,
145     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
146 static GstCaps *gst_audio_mix_matrix_fixate_caps (GstBaseTransform * trans,
147     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
148 static GstStateChangeReturn gst_audio_mix_matrix_change_state (GstElement *
149     element, GstStateChange transition);
150 
151 G_DEFINE_TYPE (GstAudioMixMatrix, gst_audio_mix_matrix,
152     GST_TYPE_BASE_TRANSFORM);
153 
154 static void
gst_audio_mix_matrix_class_init(GstAudioMixMatrixClass * klass)155 gst_audio_mix_matrix_class_init (GstAudioMixMatrixClass * klass)
156 {
157   GObjectClass *gobject_class = (GObjectClass *) klass;
158   GstElementClass *element_class = (GstElementClass *) klass;
159   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
160 
161   GST_DEBUG_CATEGORY_INIT (audiomixmatrix_debug, "audiomixmatrix", 0,
162       "audiomixmatrix");
163   gst_element_class_set_static_metadata (element_class, "Matrix audio mix",
164       "Filter/Audio",
165       "Mixes a number of input channels into a number of output channels according to a transformation matrix",
166       "Vivia Nikolaidou <vivia@toolsonair.com>");
167 
168   gobject_class->set_property = gst_audio_mix_matrix_set_property;
169   gobject_class->get_property = gst_audio_mix_matrix_get_property;
170   gobject_class->dispose = gst_audio_mix_matrix_dispose;
171 
172   g_object_class_install_property (gobject_class, PROP_IN_CHANNELS,
173       g_param_spec_uint ("in-channels", "Input audio channels",
174           "How many audio channels we have on the input side",
175           0, 64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176   g_object_class_install_property (gobject_class, PROP_OUT_CHANNELS,
177       g_param_spec_uint ("out-channels", "Output audio channels",
178           "How many audio channels we have on the output side",
179           0, 64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180   g_object_class_install_property (gobject_class, PROP_MATRIX,
181       gst_param_spec_array ("matrix",
182           "Input/output channel matrix",
183           "Transformation matrix for input/output channels",
184           gst_param_spec_array ("matrix-in1", "rows", "rows",
185               g_param_spec_double ("matrix-in2", "cols", "cols",
186                   -1, 1, 0,
187                   G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
188               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
189           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190   g_object_class_install_property (gobject_class, PROP_CHANNEL_MASK,
191       g_param_spec_uint64 ("channel-mask",
192           "Output channel mask",
193           "Output channel mask (-1 means \"default for these channels\")",
194           0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195   g_object_class_install_property (gobject_class, PROP_MODE,
196       g_param_spec_enum ("mode",
197           "Channel/matrix mode",
198           "Whether to auto-negotiate input/output channels and matrix",
199           GST_TYPE_AUDIO_MIX_MATRIX_MODE,
200           GST_AUDIO_MIX_MATRIX_MODE_MANUAL,
201           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 
203   gst_element_class_add_pad_template (element_class,
204       gst_static_pad_template_get (&gst_audio_mix_matrix_sink_template));
205   gst_element_class_add_pad_template (element_class,
206       gst_static_pad_template_get (&gst_audio_mix_matrix_src_template));
207 
208   trans_class->get_unit_size =
209       GST_DEBUG_FUNCPTR (gst_audio_mix_matrix_get_unit_size);
210   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_mix_matrix_set_caps);
211   trans_class->transform = GST_DEBUG_FUNCPTR (gst_audio_mix_matrix_transform);
212   trans_class->transform_caps =
213       GST_DEBUG_FUNCPTR (gst_audio_mix_matrix_transform_caps);
214   trans_class->fixate_caps =
215       GST_DEBUG_FUNCPTR (gst_audio_mix_matrix_fixate_caps);
216 
217   element_class->change_state =
218       GST_DEBUG_FUNCPTR (gst_audio_mix_matrix_change_state);
219 }
220 
221 static void
gst_audio_mix_matrix_init(GstAudioMixMatrix * self)222 gst_audio_mix_matrix_init (GstAudioMixMatrix * self)
223 {
224   self->in_channels = 0;
225   self->out_channels = 0;
226   self->matrix = NULL;
227   self->channel_mask = 0;
228   self->s16_conv_matrix = NULL;
229   self->s32_conv_matrix = NULL;
230   self->mode = GST_AUDIO_MIX_MATRIX_MODE_MANUAL;
231 }
232 
233 static void
gst_audio_mix_matrix_dispose(GObject * object)234 gst_audio_mix_matrix_dispose (GObject * object)
235 {
236   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (object);
237 
238   if (self->matrix) {
239     g_free (self->matrix);
240     self->matrix = NULL;
241   }
242 
243   G_OBJECT_CLASS (gst_audio_mix_matrix_parent_class)->dispose (object);
244 }
245 
246 static void
gst_audio_mix_matrix_convert_s16_matrix(GstAudioMixMatrix * self)247 gst_audio_mix_matrix_convert_s16_matrix (GstAudioMixMatrix * self)
248 {
249   gint i;
250 
251   /* converted bits - input bits - sign - bits needed for channel */
252   self->shift_bytes = 32 - 16 - 1 - ceil (log (self->in_channels) / log (2));
253 
254   if (self->s16_conv_matrix)
255     g_free (self->s16_conv_matrix);
256   self->s16_conv_matrix =
257       g_new (gint32, self->in_channels * self->out_channels);
258   for (i = 0; i < self->in_channels * self->out_channels; i++) {
259     self->s16_conv_matrix[i] =
260         (gint32) ((self->matrix[i]) * (1 << self->shift_bytes));
261   }
262 }
263 
264 static void
gst_audio_mix_matrix_convert_s32_matrix(GstAudioMixMatrix * self)265 gst_audio_mix_matrix_convert_s32_matrix (GstAudioMixMatrix * self)
266 {
267   gint i;
268 
269   /* converted bits - input bits - sign - bits needed for channel */
270   self->shift_bytes = 64 - 32 - 1 - (gint) (log (self->in_channels) / log (2));
271 
272   if (self->s32_conv_matrix)
273     g_free (self->s32_conv_matrix);
274   self->s32_conv_matrix =
275       g_new (gint64, self->in_channels * self->out_channels);
276   for (i = 0; i < self->in_channels * self->out_channels; i++) {
277     self->s32_conv_matrix[i] =
278         (gint64) ((self->matrix[i]) * (1 << self->shift_bytes));
279   }
280 }
281 
282 
283 static void
gst_audio_mix_matrix_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)284 gst_audio_mix_matrix_set_property (GObject * object, guint prop_id,
285     const GValue * value, GParamSpec * pspec)
286 {
287   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (object);
288 
289   switch (prop_id) {
290     case PROP_IN_CHANNELS:
291       self->in_channels = g_value_get_uint (value);
292       if (self->matrix) {
293         gst_audio_mix_matrix_convert_s16_matrix (self);
294         gst_audio_mix_matrix_convert_s32_matrix (self);
295       }
296       break;
297     case PROP_OUT_CHANNELS:
298       self->out_channels = g_value_get_uint (value);
299       if (self->matrix) {
300         gst_audio_mix_matrix_convert_s16_matrix (self);
301         gst_audio_mix_matrix_convert_s32_matrix (self);
302       }
303       break;
304     case PROP_MATRIX:{
305       gint in, out;
306 
307       if (self->matrix)
308         g_free (self->matrix);
309       self->matrix = g_new (gdouble, self->in_channels * self->out_channels);
310 
311       g_return_if_fail (gst_value_array_get_size (value) == self->out_channels);
312       for (out = 0; out < self->out_channels; out++) {
313         const GValue *row = gst_value_array_get_value (value, out);
314         g_return_if_fail (gst_value_array_get_size (row) == self->in_channels);
315         for (in = 0; in < self->in_channels; in++) {
316           const GValue *itm;
317           gdouble coefficient;
318 
319           itm = gst_value_array_get_value (row, in);
320           g_return_if_fail (G_VALUE_HOLDS_DOUBLE (itm));
321           coefficient = g_value_get_double (itm);
322           self->matrix[out * self->in_channels + in] = coefficient;
323         }
324       }
325       gst_audio_mix_matrix_convert_s16_matrix (self);
326       gst_audio_mix_matrix_convert_s32_matrix (self);
327       break;
328     }
329     case PROP_CHANNEL_MASK:
330       self->channel_mask = g_value_get_uint64 (value);
331       break;
332     case PROP_MODE:
333       self->mode = g_value_get_enum (value);
334       break;
335     default:
336       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337       break;
338   }
339 }
340 
341 static void
gst_audio_mix_matrix_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)342 gst_audio_mix_matrix_get_property (GObject * object, guint prop_id,
343     GValue * value, GParamSpec * pspec)
344 {
345   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (object);
346 
347   switch (prop_id) {
348     case PROP_IN_CHANNELS:
349       g_value_set_uint (value, self->in_channels);
350       break;
351     case PROP_OUT_CHANNELS:
352       g_value_set_uint (value, self->out_channels);
353       break;
354     case PROP_MATRIX:{
355       gint in, out;
356 
357       if (self->matrix == NULL)
358         break;
359 
360       for (out = 0; out < self->out_channels; out++) {
361         GValue row = G_VALUE_INIT;
362         g_value_init (&row, GST_TYPE_ARRAY);
363         for (in = 0; in < self->in_channels; in++) {
364           GValue itm = G_VALUE_INIT;
365           g_value_init (&itm, G_TYPE_DOUBLE);
366           g_value_set_double (&itm, self->matrix[out * self->in_channels + in]);
367           gst_value_array_append_value (&row, &itm);
368           g_value_unset (&itm);
369         }
370         gst_value_array_append_value (value, &row);
371         g_value_unset (&row);
372       }
373       break;
374     }
375     case PROP_CHANNEL_MASK:
376       g_value_set_uint64 (value, self->channel_mask);
377       break;
378     case PROP_MODE:
379       g_value_set_enum (value, self->mode);
380       break;
381     default:
382       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383       break;
384   }
385 }
386 
387 static GstStateChangeReturn
gst_audio_mix_matrix_change_state(GstElement * element,GstStateChange transition)388 gst_audio_mix_matrix_change_state (GstElement * element,
389     GstStateChange transition)
390 {
391   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (element);
392   GstStateChangeReturn s;
393 
394   s = GST_ELEMENT_CLASS (gst_audio_mix_matrix_parent_class)->change_state
395       (element, transition);
396 
397   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
398     if (self->s16_conv_matrix) {
399       g_free (self->s16_conv_matrix);
400       self->s16_conv_matrix = NULL;
401     }
402 
403     if (self->s32_conv_matrix) {
404       g_free (self->s32_conv_matrix);
405       self->s32_conv_matrix = NULL;
406     }
407   }
408 
409   return s;
410 }
411 
412 
413 static GstFlowReturn
gst_audio_mix_matrix_transform(GstBaseTransform * vfilter,GstBuffer * inbuf,GstBuffer * outbuf)414 gst_audio_mix_matrix_transform (GstBaseTransform * vfilter,
415     GstBuffer * inbuf, GstBuffer * outbuf)
416 {
417   GstMapInfo inmap, outmap;
418   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (vfilter);
419   gint in, out, sample;
420   guint inchannels = self->in_channels;
421   guint outchannels = self->out_channels;
422   gdouble *matrix = self->matrix;
423 
424   if (!gst_buffer_map (inbuf, &inmap, GST_MAP_READ)) {
425     return GST_FLOW_ERROR;
426   }
427   if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE)) {
428     gst_buffer_unmap (inbuf, &inmap);
429     return GST_FLOW_ERROR;
430   }
431 
432   switch (self->format) {
433     case GST_AUDIO_FORMAT_F32LE:
434     case GST_AUDIO_FORMAT_F32BE:{
435       const gfloat *inarray;
436       gfloat *outarray;
437       guint n_samples = outmap.size / (sizeof (gfloat) * outchannels);
438 
439       inarray = (gfloat *) inmap.data;
440       outarray = (gfloat *) outmap.data;
441 
442       for (sample = 0; sample < n_samples; sample++) {
443         for (out = 0; out < outchannels; out++) {
444           gfloat outval = 0;
445           for (in = 0; in < inchannels; in++) {
446             outval +=
447                 inarray[sample * inchannels +
448                 in] * matrix[out * inchannels + in];
449           }
450           outarray[sample * outchannels + out] = outval;
451         }
452       }
453       break;
454     }
455     case GST_AUDIO_FORMAT_F64LE:
456     case GST_AUDIO_FORMAT_F64BE:{
457       const gdouble *inarray;
458       gdouble *outarray;
459       guint n_samples = outmap.size / (sizeof (gdouble) * outchannels);
460 
461       inarray = (gdouble *) inmap.data;
462       outarray = (gdouble *) outmap.data;
463 
464       for (sample = 0; sample < n_samples; sample++) {
465         for (out = 0; out < outchannels; out++) {
466           gdouble outval = 0;
467           for (in = 0; in < inchannels; in++) {
468             outval +=
469                 inarray[sample * inchannels +
470                 in] * matrix[out * inchannels + in];
471           }
472           outarray[sample * outchannels + out] = outval;
473         }
474       }
475       break;
476     }
477     case GST_AUDIO_FORMAT_S16LE:
478     case GST_AUDIO_FORMAT_S16BE:{
479       const gint16 *inarray;
480       gint16 *outarray;
481       guint n_samples = outmap.size / (sizeof (gint16) * outchannels);
482       guint n = self->shift_bytes;
483       gint32 *conv_matrix = self->s16_conv_matrix;
484 
485       inarray = (gint16 *) inmap.data;
486       outarray = (gint16 *) outmap.data;
487 
488       for (sample = 0; sample < n_samples; sample++) {
489         for (out = 0; out < outchannels; out++) {
490           gint32 outval = 0;
491           for (in = 0; in < inchannels; in++) {
492             outval += (gint32) (inarray[sample * inchannels + in] *
493                 conv_matrix[out * inchannels + in]);
494           }
495           outarray[sample * outchannels + out] = (gint16) (outval >> n);
496         }
497       }
498       break;
499     }
500     case GST_AUDIO_FORMAT_S32LE:
501     case GST_AUDIO_FORMAT_S32BE:{
502       const gint32 *inarray;
503       gint32 *outarray;
504       guint n_samples = outmap.size / (sizeof (gint32) * outchannels);
505       guint n = self->shift_bytes;
506       gint64 *conv_matrix = self->s32_conv_matrix;
507 
508       inarray = (gint32 *) inmap.data;
509       outarray = (gint32 *) outmap.data;
510 
511       for (sample = 0; sample < n_samples; sample++) {
512         for (out = 0; out < outchannels; out++) {
513           gint64 outval = 0;
514           for (in = 0; in < inchannels; in++) {
515             outval += (gint64) (inarray[sample * inchannels + in] *
516                 conv_matrix[out * inchannels + in]);
517           }
518           outarray[sample * outchannels + out] = (gint32) (outval >> n);
519         }
520       }
521       break;
522     }
523     default:
524       gst_buffer_unmap (inbuf, &inmap);
525       gst_buffer_unmap (outbuf, &outmap);
526       return GST_FLOW_NOT_SUPPORTED;
527 
528   }
529 
530   gst_buffer_unmap (inbuf, &inmap);
531   gst_buffer_unmap (outbuf, &outmap);
532   return GST_FLOW_OK;
533 }
534 
535 static gboolean
gst_audio_mix_matrix_get_unit_size(GstBaseTransform * trans,GstCaps * caps,gsize * size)536 gst_audio_mix_matrix_get_unit_size (GstBaseTransform * trans,
537     GstCaps * caps, gsize * size)
538 {
539   GstAudioInfo info;
540 
541   if (!gst_audio_info_from_caps (&info, caps))
542     return FALSE;
543 
544   *size = GST_AUDIO_INFO_BPF (&info);
545 
546   return TRUE;
547 }
548 
549 static gboolean
gst_audio_mix_matrix_set_caps(GstBaseTransform * trans,GstCaps * incaps,GstCaps * outcaps)550 gst_audio_mix_matrix_set_caps (GstBaseTransform * trans, GstCaps * incaps,
551     GstCaps * outcaps)
552 {
553   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (trans);
554   GstAudioInfo info, out_info;
555 
556   if (!gst_audio_info_from_caps (&info, incaps))
557     return FALSE;
558 
559   if (!gst_audio_info_from_caps (&out_info, outcaps))
560     return FALSE;
561 
562   self->format = info.finfo->format;
563 
564   if (self->mode == GST_AUDIO_MIX_MATRIX_MODE_FIRST_CHANNELS) {
565     gint in, out;
566 
567     self->in_channels = info.channels;
568     self->out_channels = out_info.channels;
569 
570     self->matrix = g_new (gdouble, self->in_channels * self->out_channels);
571 
572     for (out = 0; out < self->out_channels; out++) {
573       for (in = 0; in < self->in_channels; in++) {
574         self->matrix[out * self->in_channels + in] = (out == in);
575       }
576     }
577   } else if (!self->matrix || info.channels != self->in_channels ||
578       out_info.channels != self->out_channels) {
579     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
580         ("Erroneous matrix detected"),
581         ("Please enter a matrix with the correct input and output channels"));
582     return FALSE;
583   }
584 
585   switch (self->format) {
586     case GST_AUDIO_FORMAT_S16LE:
587     case GST_AUDIO_FORMAT_S16BE:{
588       gst_audio_mix_matrix_convert_s16_matrix (self);
589       break;
590     }
591     case GST_AUDIO_FORMAT_S32LE:
592     case GST_AUDIO_FORMAT_S32BE:{
593       gst_audio_mix_matrix_convert_s32_matrix (self);
594       break;
595     }
596     default:
597       break;
598   }
599   return TRUE;
600 }
601 
602 static GstCaps *
gst_audio_mix_matrix_fixate_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * othercaps)603 gst_audio_mix_matrix_fixate_caps (GstBaseTransform * trans,
604     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
605 {
606   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (trans);
607   GstStructure *s, *s2;
608   guint capssize = gst_caps_get_size (othercaps);
609   gint i;
610   gint channels;
611 
612   if (self->mode == GST_AUDIO_MIX_MATRIX_MODE_FIRST_CHANNELS) {
613     s2 = gst_caps_get_structure (caps, 0);
614 
615     /* Try to keep channel configuration as much as possible */
616     if (gst_structure_get_int (s2, "channels", &channels)) {
617       gint mindiff = -1;
618       othercaps = gst_caps_make_writable (othercaps);
619       for (i = 0; i < capssize; i++) {
620         s = gst_caps_get_structure (othercaps, i);
621         if (!gst_structure_has_field (s, "channels")) {
622           mindiff = 0;
623           gst_structure_set (s, "channels", G_TYPE_INT, channels, NULL);
624         } else {
625           gint outchannels;
626           gint diff;
627 
628           gst_structure_fixate_field_nearest_int (s, "channels", channels);
629           if (gst_structure_get_int (s, "channels", &outchannels)) {
630             diff = ABS (channels - outchannels);
631             if (mindiff < 0 || diff < mindiff)
632               mindiff = diff;
633           }
634         }
635       }
636 
637       if (mindiff >= 0) {
638         for (i = 0; i < capssize; i++) {
639           gint outchannels, diff;
640           s = gst_caps_get_structure (othercaps, i);
641           if (gst_structure_get_int (s, "channels", &outchannels)) {
642             diff = ABS (channels - outchannels);
643             if (diff > mindiff) {
644               gst_caps_remove_structure (othercaps, i--);
645               capssize--;
646             }
647           }
648         }
649       }
650     }
651   }
652 
653   if (gst_caps_is_empty (othercaps))
654     return othercaps;
655 
656   othercaps =
657       GST_BASE_TRANSFORM_CLASS (gst_audio_mix_matrix_parent_class)->fixate_caps
658       (trans, direction, caps, othercaps);
659 
660   s = gst_caps_get_structure (othercaps, 0);
661 
662   if (!gst_structure_has_field (s, "channel-mask")) {
663     if (self->mode == GST_AUDIO_MIX_MATRIX_MODE_FIRST_CHANNELS ||
664         self->channel_mask == -1) {
665       gint channels;
666 
667       g_assert (gst_structure_get_int (s, "channels", &channels));
668       gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK,
669           gst_audio_channel_get_fallback_mask (channels), NULL);
670     } else {
671       gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK,
672           self->channel_mask, NULL);
673     }
674   }
675 
676   return othercaps;
677 
678 }
679 
680 static GstCaps *
gst_audio_mix_matrix_transform_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * filter)681 gst_audio_mix_matrix_transform_caps (GstBaseTransform * trans,
682     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
683 {
684   GstAudioMixMatrix *self = GST_AUDIO_MIX_MATRIX (trans);
685   GstCaps *outcaps = gst_caps_copy (caps);
686   GstCaps *ret;
687   GstStructure *s;
688   gint i;
689   guint capssize = gst_caps_get_size (outcaps);
690 
691   if (self->mode == GST_AUDIO_MIX_MATRIX_MODE_FIRST_CHANNELS) {
692     for (i = 0; i < capssize; i++) {
693       s = gst_caps_get_structure (outcaps, i);
694       if (gst_structure_has_field (s, "channels")) {
695         gst_structure_remove_field (s, "channels");
696       }
697       if (gst_structure_has_field (s, "channel-mask")) {
698         gst_structure_remove_field (s, "channel-mask");
699       }
700     }
701     goto beach;
702   }
703 
704   if (self->in_channels == 0 || self->out_channels == 0 || self->matrix == NULL) {
705     /* Not dispatching element error because we return empty caps anyway and
706      * we should let it fail to link. Additionally, the element error would be
707      * printed as WARN, so a possible gst-launch pipeline would appear to
708      * hang. */
709     GST_ERROR_OBJECT (self, "Invalid settings detected in manual mode. "
710         "Please specify in-channels, out-channels and matrix.");
711     return gst_caps_new_empty ();
712   }
713 
714   if (self->in_channels == self->out_channels) {
715     goto beach;
716   }
717 
718   for (i = 0; i < capssize; i++) {
719     s = gst_caps_get_structure (outcaps, i);
720     if (direction == GST_PAD_SRC) {
721       gst_structure_set (s, "channels", G_TYPE_INT, self->in_channels, NULL);
722       gst_structure_remove_field (s, "channel-mask");
723     } else if (direction == GST_PAD_SINK) {
724       gst_structure_set (s, "channels", G_TYPE_INT, self->out_channels,
725           "channel-mask", GST_TYPE_BITMASK, self->channel_mask, NULL);
726     } else {
727       g_assert_not_reached ();
728     }
729   }
730 
731 beach:
732   if (filter) {
733     ret = gst_caps_intersect_full (filter, outcaps, GST_CAPS_INTERSECT_FIRST);
734     gst_caps_unref (outcaps);
735   } else {
736     ret = outcaps;
737   }
738   return ret;
739 }
740 
741 static gboolean
plugin_init(GstPlugin * plugin)742 plugin_init (GstPlugin * plugin)
743 {
744   return gst_element_register (plugin, "audiomixmatrix", GST_RANK_NONE,
745       GST_TYPE_AUDIO_MIX_MATRIX);
746 }
747 
748 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
749     GST_VERSION_MINOR,
750     audiomixmatrix,
751     "Audio matrix mix",
752     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
753