2 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * gstchannelmix.c: setup of channel conversion matrices
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.
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.
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.
28 #include <gst/audio/multichannel.h>
30 #include "gstchannelmix.h"
32 /* GLib < 2.4 compatibility */
34 #define G_MININT32 ((gint32) 0x80000000)
38 #define G_MAXINT32 ((gint32) 0x7fffffff)
42 * Channel matrix functions.
46 gst_audio_convert_unset_matrix (GstAudioConvert * this)
50 /* don't access if nothing there */
55 for (i = 0; i < this->sinkcaps.channels; i++)
56 g_free (this->matrix[i]);
57 g_free (this->matrix);
63 * Detect and fill in identical channels. E.g.
64 * forward the left/right front channels in a
65 * 5.1 to 2.0 conversion.
69 gst_audio_convert_fill_identical (GstAudioConvert * this)
73 /* Apart from the compatible channel assignments, we can also have
74 * same channel assignments. This is much simpler, we simply copy
75 * the value from source to dest! */
76 for (co = 0; co < this->srccaps.channels; co++) {
77 /* find a channel in input with same position */
78 for (ci = 0; ci < this->sinkcaps.channels; ci++) {
79 if (this->sinkcaps.pos[ci] == this->srccaps.pos[co]) {
80 this->matrix[ci][co] = 1.0;
87 * Detect and fill in compatible channels. E.g.
88 * forward left/right front to mono (or the other
89 * way around) when going from 2.0 to 1.0.
93 gst_audio_convert_fill_compatible (GstAudioConvert * this)
95 /* Conversions from one-channel to compatible two-channel configs */
98 GstAudioChannelPosition pos1[2];
99 GstAudioChannelPosition pos2[1];
101 /* front: mono <-> stereo */
103 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
104 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
105 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}},
106 /* front center: 2 <-> 1 */
108 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
109 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
110 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}},
113 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
114 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
115 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { {
116 GST_AUDIO_CHANNEL_POSITION_INVALID}}
120 /* conversions from compatible (but not the same) channel schemes. This
121 * goes two ways: if the sink has both pos1[0,1] and src has pos2[0] or
122 * if the src has both pos1[0,1] and sink has pos2[0], then we do the
123 * conversion. We hereby assume that the existance of pos1[0,1] and
124 * pos2[0] are mututally exclusive. There are no checks for that,
125 * unfortunately. This shouldn't lead to issues (like crashes or so),
127 for (c = 0; conv[c].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; c++) {
128 gint pos1_0 = -1, pos1_1 = -1, pos2_0 = -1, n;
130 /* Try to go from the given 2 channels to the given 1 channel */
131 for (n = 0; n < this->sinkcaps.channels; n++) {
132 if (this->sinkcaps.pos[n] == conv[c].pos1[0])
134 else if (this->sinkcaps.pos[n] == conv[c].pos1[1])
137 for (n = 0; n < this->srccaps.channels; n++) {
138 if (this->srccaps.pos[n] == conv[c].pos2[0])
142 if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
143 this->matrix[pos1_0][pos2_0] = 1.0;
144 this->matrix[pos1_1][pos2_0] = 1.0;
147 /* Try to go from the given 1 channel to the given 2 channels */
152 for (n = 0; n < this->srccaps.channels; n++) {
153 if (this->srccaps.pos[n] == conv[c].pos1[0])
155 else if (this->srccaps.pos[n] == conv[c].pos1[1])
158 for (n = 0; n < this->sinkcaps.channels; n++) {
159 if (this->sinkcaps.pos[n] == conv[c].pos2[0])
163 if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
164 this->matrix[pos2_0][pos1_0] = 1.0;
165 this->matrix[pos2_0][pos1_1] = 1.0;
171 * Detect and fill in channels not handled by the
172 * above two, e.g. center to left/right front in
173 * 5.1 to 2.0 (or the other way around).
175 * Unfortunately, limited to static conversions
180 gst_audio_convert_detect_pos (GstAudioConvertCaps * caps,
181 gint * f, gboolean * has_f,
182 gint * c, gboolean * has_c, gint * r, gboolean * has_r,
183 gint * s, gboolean * has_s, gint * b, gboolean * has_b)
187 for (n = 0; n < caps->channels; n++) {
188 switch (caps->pos[n]) {
189 case GST_AUDIO_CHANNEL_POSITION_FRONT_MONO:
190 case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
191 case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
198 case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
199 case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
200 case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
207 case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
208 case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
209 case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
216 case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
217 case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
224 case GST_AUDIO_CHANNEL_POSITION_LFE:
235 gst_audio_convert_fill_one_other (gfloat ** matrix,
236 GstAudioConvertCaps * from_caps, gint * from_idx,
237 GstAudioChannelPosition from_pos_l,
238 GstAudioChannelPosition from_pos_r,
239 GstAudioChannelPosition from_pos_c,
240 GstAudioConvertCaps * to_caps, gint * to_idx,
241 GstAudioChannelPosition to_pos_l,
242 GstAudioChannelPosition to_pos_r,
243 GstAudioChannelPosition to_pos_c, gfloat ratio)
245 gfloat in_r, out_r[2];
248 * The idea is that we add up from the input (which means that if we
249 * have stereo input, we divide their sum by two) and put that in
250 * the matrix for their output ratio (given in $ratio).
251 * For left channels, we need to invert the signal sign (* -1).
254 if (from_caps->pos[from_idx[0]] == from_pos_c)
259 if (to_caps->pos[to_idx[0]] == to_pos_l)
260 out_r[0] = in_r * -ratio;
262 out_r[0] = in_r * ratio;
264 if (to_idx[1] != -1) {
265 if (to_caps->pos[to_idx[1]] == to_pos_l)
266 out_r[1] = in_r * -ratio;
268 out_r[1] = in_r * ratio;
271 matrix[from_idx[0]][to_idx[0]] = out_r[0];
273 matrix[from_idx[0]][to_idx[1]] = out_r[1];
274 if (from_idx[1] != -1) {
275 matrix[from_idx[1]][to_idx[0]] = out_r[0];
277 matrix[from_idx[1]][to_idx[1]] = out_r[1];
281 #define RATIO_FRONT_CENTER (1.0 / sqrt (2.0))
282 #define RATIO_FRONT_REAR (1.0 / sqrt (2.0))
283 #define RATIO_FRONT_BASS (1.0)
284 #define RATIO_REAR_BASS (1.0 / sqrt (2.0))
285 #define RATIO_CENTER_BASS (1.0 / sqrt (2.0))
288 gst_audio_convert_fill_others (GstAudioConvert * this)
290 gboolean in_has_front = FALSE, out_has_front = FALSE,
291 in_has_center = FALSE, out_has_center = FALSE,
292 in_has_rear = FALSE, out_has_rear = FALSE,
293 in_has_side = FALSE, out_has_side = FALSE,
294 in_has_bass = FALSE, out_has_bass = FALSE;
295 gint in_f[2] = { -1, -1 }, out_f[2] = {
297 -1, -1}, out_c[2] = {
299 -1, -1}, out_r[2] = {
301 -1, -1}, out_s[2] = {
303 -1, -1}, out_b[2] = {
306 /* First see where (if at all) the various channels from/to
307 * which we want to convert are located in our matrix/array. */
308 gst_audio_convert_detect_pos (&this->sinkcaps,
310 in_c, &in_has_center, in_r, &in_has_rear,
311 in_s, &in_has_side, in_b, &in_has_bass);
312 gst_audio_convert_detect_pos (&this->srccaps,
313 out_f, &out_has_front,
314 out_c, &out_has_center, out_r, &out_has_rear,
315 out_s, &out_has_side, out_b, &out_has_bass);
318 if (!in_has_center && in_has_front && out_has_center) {
319 gst_audio_convert_fill_one_other (this->matrix,
320 &this->sinkcaps, in_f,
321 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
322 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
323 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
324 &this->srccaps, out_c,
325 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
326 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
327 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, RATIO_FRONT_CENTER);
328 } else if (in_has_center && !out_has_center && out_has_front) {
329 gst_audio_convert_fill_one_other (this->matrix,
330 &this->sinkcaps, in_c,
331 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
332 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
333 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
334 &this->srccaps, out_f,
335 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
336 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
337 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_CENTER);
341 if (!in_has_rear && in_has_front && out_has_rear) {
342 gst_audio_convert_fill_one_other (this->matrix,
343 &this->sinkcaps, in_f,
344 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
345 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
346 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
347 &this->srccaps, out_r,
348 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
349 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
350 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, RATIO_FRONT_REAR);
351 } else if (in_has_center && !out_has_center && out_has_front) {
352 gst_audio_convert_fill_one_other (this->matrix,
353 &this->sinkcaps, in_r,
354 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
355 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
356 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
357 &this->srccaps, out_f,
358 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
359 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
360 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_REAR);
364 if (in_has_bass && !out_has_bass) {
366 gst_audio_convert_fill_one_other (this->matrix,
367 &this->sinkcaps, in_b,
368 GST_AUDIO_CHANNEL_POSITION_INVALID,
369 GST_AUDIO_CHANNEL_POSITION_INVALID,
370 GST_AUDIO_CHANNEL_POSITION_LFE,
371 &this->srccaps, out_f,
372 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
373 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
374 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_BASS);
376 if (out_has_center) {
377 gst_audio_convert_fill_one_other (this->matrix,
378 &this->sinkcaps, in_b,
379 GST_AUDIO_CHANNEL_POSITION_INVALID,
380 GST_AUDIO_CHANNEL_POSITION_INVALID,
381 GST_AUDIO_CHANNEL_POSITION_LFE,
382 &this->srccaps, out_c,
383 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
384 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
385 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, RATIO_CENTER_BASS);
388 gst_audio_convert_fill_one_other (this->matrix,
389 &this->sinkcaps, in_b,
390 GST_AUDIO_CHANNEL_POSITION_INVALID,
391 GST_AUDIO_CHANNEL_POSITION_INVALID,
392 GST_AUDIO_CHANNEL_POSITION_LFE,
393 &this->srccaps, out_r,
394 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
395 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
396 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, RATIO_REAR_BASS);
398 } else if (!in_has_bass && out_has_bass) {
400 gst_audio_convert_fill_one_other (this->matrix,
401 &this->sinkcaps, in_f,
402 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
403 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
404 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
405 &this->srccaps, out_b,
406 GST_AUDIO_CHANNEL_POSITION_INVALID,
407 GST_AUDIO_CHANNEL_POSITION_INVALID,
408 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_FRONT_BASS);
411 gst_audio_convert_fill_one_other (this->matrix,
412 &this->sinkcaps, in_c,
413 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
414 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
415 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
416 &this->srccaps, out_b,
417 GST_AUDIO_CHANNEL_POSITION_INVALID,
418 GST_AUDIO_CHANNEL_POSITION_INVALID,
419 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_CENTER_BASS);
422 gst_audio_convert_fill_one_other (this->matrix,
423 &this->sinkcaps, in_r,
424 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
425 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
426 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
427 &this->srccaps, out_b,
428 GST_AUDIO_CHANNEL_POSITION_INVALID,
429 GST_AUDIO_CHANNEL_POSITION_INVALID,
430 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_REAR_BASS);
438 * Normalize output values.
442 gst_audio_convert_fill_normalize (GstAudioConvert * this)
447 for (j = 0; j < this->srccaps.channels; j++) {
450 for (i = 0; i < this->sinkcaps.channels; i++) {
451 sum += fabs (this->matrix[i][j]);
458 /* normalize to this */
459 for (j = 0; j < this->srccaps.channels; j++) {
460 for (i = 0; i < this->sinkcaps.channels; i++) {
461 this->matrix[i][j] /= top;
467 * Automagically generate conversion matrix.
471 gst_audio_convert_fill_matrix (GstAudioConvert * this)
473 gst_audio_convert_fill_identical (this);
474 gst_audio_convert_fill_compatible (this);
475 gst_audio_convert_fill_others (this);
476 gst_audio_convert_fill_normalize (this);
480 gst_audio_convert_setup_matrix (GstAudioConvert * this)
485 /* don't lose memory */
486 gst_audio_convert_unset_matrix (this);
489 this->matrix = g_new0 (gfloat *, this->sinkcaps.channels);
490 for (i = 0; i < this->sinkcaps.channels; i++) {
491 this->matrix[i] = g_new (gfloat, this->srccaps.channels);
492 for (j = 0; j < this->srccaps.channels; j++)
493 this->matrix[i][j] = 0.;
496 /* setup the matrix' internal values */
497 gst_audio_convert_fill_matrix (this);
500 s = g_string_new ("Matrix for");
501 g_string_append_printf (s, " %d -> %d: ",
502 this->sinkcaps.channels, this->srccaps.channels);
503 g_string_append (s, "{");
504 for (i = 0; i < this->sinkcaps.channels; i++) {
506 g_string_append (s, ",");
507 g_string_append (s, " {");
508 for (j = 0; j < this->srccaps.channels; j++) {
510 g_string_append (s, ",");
511 g_string_append_printf (s, " %f", this->matrix[i][j]);
513 g_string_append (s, " }");
515 g_string_append (s, " }");
517 g_string_free (s, TRUE);
521 gst_audio_convert_passthrough (GstAudioConvert * this)
525 /* only NxN matrices can be identities */
526 if (this->sinkcaps.channels != this->srccaps.channels)
529 /* this assumes a normalized matrix */
530 for (i = 0; i < this->sinkcaps.channels; i++)
531 if (this->matrix[i][i] != 1.)
537 /* IMPORTANT: out_data == in_data is possible, make sure to not overwrite data
538 * you might need later on! */
540 gst_audio_convert_mix (GstAudioConvert * this,
541 gint32 * in_data, gint32 * out_data, gint samples)
545 gint32 tmp[this->srccaps.channels];
546 gboolean backwards = this->srccaps.channels > this->sinkcaps.channels;
548 /* FIXME: use liboil here? */
549 for (n = (backwards ? samples - 1 : 0); n < samples && n >= 0;
550 backwards ? n-- : n++) {
551 for (out = 0; out < this->srccaps.channels; out++) {
554 for (in = 0; in < this->sinkcaps.channels; in++) {
555 res += in_data[n * this->sinkcaps.channels + in] *
556 this->matrix[in][out];
559 /* clip (shouldn't we use doubles instead as intermediate format?) */
560 if (res < G_MININT32)
562 else if (res > G_MAXINT32)
566 memcpy (&out_data[n * this->srccaps.channels], tmp,
567 sizeof (gint32) * this->srccaps.channels);