expand tabs
[platform/upstream/gstreamer.git] / gst / audioconvert / gstchannelmix.c
1 /* GStreamer
2  * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * gstchannelmix.c: setup of channel conversion matrices
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <math.h>
27 #include <string.h>
28 #include <gst/audio/multichannel.h>
29
30 #include "gstchannelmix.h"
31
32 /* GLib < 2.4 compatibility */
33 #ifndef G_MININT32
34 #define G_MININT32      ((gint32)  0x80000000)
35 #endif
36
37 #ifndef G_MAXINT32
38 #define G_MAXINT32      ((gint32)  0x7fffffff)
39 #endif
40
41 /*
42  * Channel matrix functions.
43  */
44
45 void
46 gst_channel_mix_unset_matrix (AudioConvertCtx * this)
47 {
48   gint i;
49
50   /* don't access if nothing there */
51   if (!this->matrix)
52     return;
53
54   /* free */
55   for (i = 0; i < this->in.channels; i++)
56     g_free (this->matrix[i]);
57   g_free (this->matrix);
58
59   this->matrix = NULL;
60   g_free (this->tmp);
61   this->tmp = NULL;
62 }
63
64 /*
65  * Detect and fill in identical channels. E.g.
66  * forward the left/right front channels in a
67  * 5.1 to 2.0 conversion.
68  */
69
70 static void
71 gst_channel_mix_fill_identical (AudioConvertCtx * this)
72 {
73   gint ci, co;
74
75   /* Apart from the compatible channel assignments, we can also have
76    * same channel assignments. This is much simpler, we simply copy
77    * the value from source to dest! */
78   for (co = 0; co < this->out.channels; co++) {
79     /* find a channel in input with same position */
80     for (ci = 0; ci < this->in.channels; ci++) {
81       if (this->in.pos[ci] == this->out.pos[co]) {
82         this->matrix[ci][co] = 1.0;
83       }
84     }
85   }
86 }
87
88 /*
89  * Detect and fill in compatible channels. E.g.
90  * forward left/right front to mono (or the other
91  * way around) when going from 2.0 to 1.0.
92  */
93
94 static void
95 gst_channel_mix_fill_compatible (AudioConvertCtx * this)
96 {
97   /* Conversions from one-channel to compatible two-channel configs */
98   struct
99   {
100     GstAudioChannelPosition pos1[2];
101     GstAudioChannelPosition pos2[1];
102   } conv[] = {
103     /* front: mono <-> stereo */
104     { {
105     GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
106             GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
107     GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}},
108         /* front center: 2 <-> 1 */
109     { {
110     GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
111             GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
112     GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}},
113         /* rear: 2 <-> 1 */
114     { {
115     GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
116             GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
117     GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { {
118     GST_AUDIO_CHANNEL_POSITION_INVALID}}
119   };
120   gint c;
121
122   /* conversions from compatible (but not the same) channel schemes. This
123    * goes two ways: if the sink has both pos1[0,1] and src has pos2[0] or
124    * if the src has both pos1[0,1] and sink has pos2[0], then we do the
125    * conversion. We hereby assume that the existance of pos1[0,1] and
126    * pos2[0] are mututally exclusive. There are no checks for that,
127    * unfortunately. This shouldn't lead to issues (like crashes or so),
128    * though. */
129   for (c = 0; conv[c].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; c++) {
130     gint pos1_0 = -1, pos1_1 = -1, pos2_0 = -1, n;
131
132     /* Try to go from the given 2 channels to the given 1 channel */
133     for (n = 0; n < this->in.channels; n++) {
134       if (this->in.pos[n] == conv[c].pos1[0])
135         pos1_0 = n;
136       else if (this->in.pos[n] == conv[c].pos1[1])
137         pos1_1 = n;
138     }
139     for (n = 0; n < this->out.channels; n++) {
140       if (this->out.pos[n] == conv[c].pos2[0])
141         pos2_0 = n;
142     }
143
144     if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
145       this->matrix[pos1_0][pos2_0] = 1.0;
146       this->matrix[pos1_1][pos2_0] = 1.0;
147     }
148
149     /* Try to go from the given 1 channel to the given 2 channels */
150     pos1_0 = -1;
151     pos1_1 = -1;
152     pos2_0 = -1;
153
154     for (n = 0; n < this->out.channels; n++) {
155       if (this->out.pos[n] == conv[c].pos1[0])
156         pos1_0 = n;
157       else if (this->out.pos[n] == conv[c].pos1[1])
158         pos1_1 = n;
159     }
160     for (n = 0; n < this->in.channels; n++) {
161       if (this->in.pos[n] == conv[c].pos2[0])
162         pos2_0 = n;
163     }
164
165     if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
166       this->matrix[pos2_0][pos1_0] = 1.0;
167       this->matrix[pos2_0][pos1_1] = 1.0;
168     }
169   }
170 }
171
172 /*
173  * Detect and fill in channels not handled by the
174  * above two, e.g. center to left/right front in
175  * 5.1 to 2.0 (or the other way around).
176  *
177  * Unfortunately, limited to static conversions
178  * for now.
179  */
180
181 static void
182 gst_channel_mix_detect_pos (AudioConvertFmt * caps,
183     gint * f, gboolean * has_f,
184     gint * c, gboolean * has_c, gint * r, gboolean * has_r,
185     gint * s, gboolean * has_s, gint * b, gboolean * has_b)
186 {
187   gint n;
188
189   for (n = 0; n < caps->channels; n++) {
190     switch (caps->pos[n]) {
191       case GST_AUDIO_CHANNEL_POSITION_FRONT_MONO:
192       case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
193       case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
194         *has_f = TRUE;
195         if (f[0] == -1)
196           f[0] = n;
197         else
198           f[1] = n;
199         break;
200       case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
201       case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
202       case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
203         *has_c = TRUE;
204         if (c[0] == -1)
205           c[0] = n;
206         else
207           c[1] = n;
208         break;
209       case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
210       case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
211       case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
212         *has_r = TRUE;
213         if (r[0] == -1)
214           r[0] = n;
215         else
216           r[1] = n;
217         break;
218       case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
219       case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
220         *has_s = TRUE;
221         if (s[0] == -1)
222           s[0] = n;
223         else
224           s[1] = n;
225         break;
226       case GST_AUDIO_CHANNEL_POSITION_LFE:
227         *has_b = TRUE;
228         b[0] = n;
229         break;
230       default:
231         break;
232     }
233   }
234 }
235
236 static void
237 gst_channel_mix_fill_one_other (gfloat ** matrix,
238     AudioConvertFmt * from_caps, gint * from_idx,
239     GstAudioChannelPosition from_pos_l,
240     GstAudioChannelPosition from_pos_r,
241     GstAudioChannelPosition from_pos_c,
242     AudioConvertFmt * to_caps, gint * to_idx,
243     GstAudioChannelPosition to_pos_l,
244     GstAudioChannelPosition to_pos_r,
245     GstAudioChannelPosition to_pos_c, gfloat ratio)
246 {
247   gfloat in_r, out_r[2] = { 0.f, 0.f };
248
249   /*
250    * The idea is that we add up from the input (which means that if we
251    * have stereo input, we divide their sum by two) and put that in
252    * the matrix for their output ratio (given in $ratio).
253    * For left channels, we need to invert the signal sign (* -1).
254    */
255
256   if (from_caps->pos[from_idx[0]] == from_pos_c)
257     in_r = 1.0;
258   else
259     in_r = 0.5;
260
261   if (to_caps->pos[to_idx[0]] == to_pos_l)
262     out_r[0] = in_r * -ratio;
263   else
264     out_r[0] = in_r * ratio;
265
266   if (to_idx[1] != -1) {
267     if (to_caps->pos[to_idx[1]] == to_pos_l)
268       out_r[1] = in_r * -ratio;
269     else
270       out_r[1] = in_r * ratio;
271   }
272
273   matrix[from_idx[0]][to_idx[0]] = out_r[0];
274   if (to_idx[1] != -1)
275     matrix[from_idx[0]][to_idx[1]] = out_r[1];
276   if (from_idx[1] != -1) {
277     matrix[from_idx[1]][to_idx[0]] = out_r[0];
278     if (to_idx[1] != -1)
279       matrix[from_idx[1]][to_idx[1]] = out_r[1];
280   }
281 }
282
283 #define RATIO_FRONT_CENTER (1.0 / sqrt (2.0))
284 #define RATIO_FRONT_REAR (1.0 / sqrt (2.0))
285 #define RATIO_FRONT_BASS (1.0)
286 #define RATIO_REAR_BASS (1.0 / sqrt (2.0))
287 #define RATIO_CENTER_BASS (1.0 / sqrt (2.0))
288
289 static void
290 gst_channel_mix_fill_others (AudioConvertCtx * this)
291 {
292   gboolean in_has_front = FALSE, out_has_front = FALSE,
293       in_has_center = FALSE, out_has_center = FALSE,
294       in_has_rear = FALSE, out_has_rear = FALSE,
295       in_has_side = FALSE, out_has_side = FALSE,
296       in_has_bass = FALSE, out_has_bass = FALSE;
297   gint in_f[2] = { -1, -1 }, out_f[2] = {
298   -1, -1}, in_c[2] = {
299   -1, -1}, out_c[2] = {
300   -1, -1}, in_r[2] = {
301   -1, -1}, out_r[2] = {
302   -1, -1}, in_s[2] = {
303   -1, -1}, out_s[2] = {
304   -1, -1}, in_b[2] = {
305   -1, -1}, out_b[2] = {
306   -1, -1};
307
308   /* First see where (if at all) the various channels from/to
309    * which we want to convert are located in our matrix/array. */
310   gst_channel_mix_detect_pos (&this->in,
311       in_f, &in_has_front,
312       in_c, &in_has_center, in_r, &in_has_rear,
313       in_s, &in_has_side, in_b, &in_has_bass);
314   gst_channel_mix_detect_pos (&this->out,
315       out_f, &out_has_front,
316       out_c, &out_has_center, out_r, &out_has_rear,
317       out_s, &out_has_side, out_b, &out_has_bass);
318
319   /* center/front */
320   if (!in_has_center && in_has_front && out_has_center) {
321     gst_channel_mix_fill_one_other (this->matrix,
322         &this->in, in_f,
323         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
324         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
325         GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
326         &this->out, out_c,
327         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
328         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
329         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, RATIO_FRONT_CENTER);
330   } else if (in_has_center && !out_has_center && out_has_front) {
331     gst_channel_mix_fill_one_other (this->matrix,
332         &this->in, in_c,
333         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
334         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
335         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
336         &this->out, out_f,
337         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
338         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
339         GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_CENTER);
340   }
341
342   /* rear/front */
343   if (!in_has_rear && in_has_front && out_has_rear) {
344     gst_channel_mix_fill_one_other (this->matrix,
345         &this->in, in_f,
346         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
347         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
348         GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
349         &this->out, out_r,
350         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
351         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
352         GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, RATIO_FRONT_REAR);
353   } else if (in_has_center && !out_has_center && out_has_front) {
354     gst_channel_mix_fill_one_other (this->matrix,
355         &this->in, in_r,
356         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
357         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
358         GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
359         &this->out, out_f,
360         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
361         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
362         GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_REAR);
363   }
364
365   /* bass/any */
366   if (in_has_bass && !out_has_bass) {
367     if (out_has_front) {
368       gst_channel_mix_fill_one_other (this->matrix,
369           &this->in, in_b,
370           GST_AUDIO_CHANNEL_POSITION_INVALID,
371           GST_AUDIO_CHANNEL_POSITION_INVALID,
372           GST_AUDIO_CHANNEL_POSITION_LFE,
373           &this->out, out_f,
374           GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
375           GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
376           GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_BASS);
377     }
378     if (out_has_center) {
379       gst_channel_mix_fill_one_other (this->matrix,
380           &this->in, in_b,
381           GST_AUDIO_CHANNEL_POSITION_INVALID,
382           GST_AUDIO_CHANNEL_POSITION_INVALID,
383           GST_AUDIO_CHANNEL_POSITION_LFE,
384           &this->out, out_c,
385           GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
386           GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
387           GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, RATIO_CENTER_BASS);
388     }
389     if (out_has_rear) {
390       gst_channel_mix_fill_one_other (this->matrix,
391           &this->in, in_b,
392           GST_AUDIO_CHANNEL_POSITION_INVALID,
393           GST_AUDIO_CHANNEL_POSITION_INVALID,
394           GST_AUDIO_CHANNEL_POSITION_LFE,
395           &this->out, out_r,
396           GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
397           GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
398           GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, RATIO_REAR_BASS);
399     }
400   } else if (!in_has_bass && out_has_bass) {
401     if (in_has_front) {
402       gst_channel_mix_fill_one_other (this->matrix,
403           &this->in, in_f,
404           GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
405           GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
406           GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
407           &this->out, out_b,
408           GST_AUDIO_CHANNEL_POSITION_INVALID,
409           GST_AUDIO_CHANNEL_POSITION_INVALID,
410           GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_FRONT_BASS);
411     }
412     if (in_has_center) {
413       gst_channel_mix_fill_one_other (this->matrix,
414           &this->in, in_c,
415           GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
416           GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
417           GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
418           &this->out, out_b,
419           GST_AUDIO_CHANNEL_POSITION_INVALID,
420           GST_AUDIO_CHANNEL_POSITION_INVALID,
421           GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_CENTER_BASS);
422     }
423     if (in_has_rear) {
424       gst_channel_mix_fill_one_other (this->matrix,
425           &this->in, in_r,
426           GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
427           GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
428           GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
429           &this->out, out_b,
430           GST_AUDIO_CHANNEL_POSITION_INVALID,
431           GST_AUDIO_CHANNEL_POSITION_INVALID,
432           GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_REAR_BASS);
433     }
434   }
435
436   /* FIXME: side */
437 }
438
439 /*
440  * Normalize output values.
441  */
442
443 static void
444 gst_channel_mix_fill_normalize (AudioConvertCtx * this)
445 {
446   gfloat sum, top = 0;
447   gint i, j;
448
449   for (j = 0; j < this->out.channels; j++) {
450     /* calculate sum */
451     sum = 0.0;
452     for (i = 0; i < this->in.channels; i++) {
453       sum += fabs (this->matrix[i][j]);
454     }
455     if (sum > top) {
456       top = sum;
457     }
458   }
459
460   /* normalize to this */
461   for (j = 0; j < this->out.channels; j++) {
462     for (i = 0; i < this->in.channels; i++) {
463       this->matrix[i][j] /= top;
464     }
465   }
466 }
467
468 /*
469  * Automagically generate conversion matrix.
470  */
471
472 static void
473 gst_channel_mix_fill_matrix (AudioConvertCtx * this)
474 {
475   gst_channel_mix_fill_identical (this);
476   gst_channel_mix_fill_compatible (this);
477   gst_channel_mix_fill_others (this);
478   gst_channel_mix_fill_normalize (this);
479 }
480
481 /* only call after this->out and this->in are filled in */
482 void
483 gst_channel_mix_setup_matrix (AudioConvertCtx * this)
484 {
485   gint i, j;
486   GString *s;
487
488   /* don't lose memory */
489   gst_channel_mix_unset_matrix (this);
490
491   /* temp storage */
492   this->tmp = g_new (gint32, this->out.channels);
493
494   /* allocate */
495   this->matrix = g_new0 (gfloat *, this->in.channels);
496   for (i = 0; i < this->in.channels; i++) {
497     this->matrix[i] = g_new (gfloat, this->out.channels);
498     for (j = 0; j < this->out.channels; j++)
499       this->matrix[i][j] = 0.;
500   }
501
502   /* setup the matrix' internal values */
503   gst_channel_mix_fill_matrix (this);
504
505   /* debug */
506   s = g_string_new ("Matrix for");
507   g_string_append_printf (s, " %d -> %d: ",
508       this->in.channels, this->out.channels);
509   g_string_append (s, "{");
510   for (i = 0; i < this->in.channels; i++) {
511     if (i != 0)
512       g_string_append (s, ",");
513     g_string_append (s, " {");
514     for (j = 0; j < this->out.channels; j++) {
515       if (j != 0)
516         g_string_append (s, ",");
517       g_string_append_printf (s, " %f", this->matrix[i][j]);
518     }
519     g_string_append (s, " }");
520   }
521   g_string_append (s, " }");
522   GST_DEBUG (s->str);
523   g_string_free (s, TRUE);
524 }
525
526 gboolean
527 gst_channel_mix_passthrough (AudioConvertCtx * this)
528 {
529   gint i;
530
531   /* only NxN matrices can be identities */
532   if (this->in.channels != this->out.channels)
533     return FALSE;
534
535   /* this assumes a normalized matrix */
536   for (i = 0; i < this->in.channels; i++)
537     if (this->matrix[i][i] != 1.)
538       return FALSE;
539
540   return TRUE;
541 }
542
543 /* IMPORTANT: out_data == in_data is possible, make sure to not overwrite data
544  * you might need later on! */
545 void
546 gst_channel_mix_mix (AudioConvertCtx * this,
547     gint32 * in_data, gint32 * out_data, gint samples)
548 {
549   gint in, out, n;
550   gint64 res;
551   gboolean backwards;
552   gint inchannels, outchannels;
553
554   g_return_if_fail (this->matrix != NULL);
555   g_return_if_fail (this->tmp != NULL);
556
557   inchannels = this->in.channels;
558   outchannels = this->out.channels;
559   backwards = outchannels > inchannels;
560
561   /* FIXME: use liboil here? */
562   for (n = (backwards ? samples - 1 : 0); n < samples && n >= 0;
563       backwards ? n-- : n++) {
564     for (out = 0; out < outchannels; out++) {
565       /* convert */
566       res = 0;
567       for (in = 0; in < inchannels; in++) {
568         res += in_data[n * inchannels + in] * this->matrix[in][out];
569       }
570
571       /* clip (shouldn't we use doubles instead as intermediate format?) */
572       if (res < G_MININT32)
573         res = G_MININT32;
574       else if (res > G_MAXINT32)
575         res = G_MAXINT32;
576       this->tmp[out] = res;
577     }
578     memcpy (&out_data[n * outchannels], this->tmp,
579         sizeof (gint32) * outchannels);
580   }
581 }