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_channel_mix_unset_matrix (AudioConvertCtx * this)
50 /* don't access if nothing there */
55 for (i = 0; i < this->in.channels; i++)
56 g_free (this->matrix[i]);
57 g_free (this->matrix);
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.
71 gst_channel_mix_fill_identical (AudioConvertCtx * this)
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;
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.
95 gst_channel_mix_fill_compatible (AudioConvertCtx * this)
97 /* Conversions from one-channel to compatible two-channel configs */
100 GstAudioChannelPosition pos1[2];
101 GstAudioChannelPosition pos2[1];
103 /* front: mono <-> stereo */
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 */
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}},
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}}
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),
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;
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])
136 else if (this->in.pos[n] == conv[c].pos1[1])
139 for (n = 0; n < this->out.channels; n++) {
140 if (this->out.pos[n] == conv[c].pos2[0])
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;
149 /* Try to go from the given 1 channel to the given 2 channels */
154 for (n = 0; n < this->out.channels; n++) {
155 if (this->out.pos[n] == conv[c].pos1[0])
157 else if (this->out.pos[n] == conv[c].pos1[1])
160 for (n = 0; n < this->in.channels; n++) {
161 if (this->in.pos[n] == conv[c].pos2[0])
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;
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).
177 * Unfortunately, limited to static conversions
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)
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:
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:
209 case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
210 case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
211 case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
218 case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
219 case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
226 case GST_AUDIO_CHANNEL_POSITION_LFE:
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)
247 gfloat in_r, out_r[2] = { 0.f, 0.f };
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).
256 if (from_caps->pos[from_idx[0]] == from_pos_c)
261 if (to_caps->pos[to_idx[0]] == to_pos_l)
262 out_r[0] = in_r * -ratio;
264 out_r[0] = in_r * ratio;
266 if (to_idx[1] != -1) {
267 if (to_caps->pos[to_idx[1]] == to_pos_l)
268 out_r[1] = in_r * -ratio;
270 out_r[1] = in_r * ratio;
273 matrix[from_idx[0]][to_idx[0]] = out_r[0];
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];
279 matrix[from_idx[1]][to_idx[1]] = out_r[1];
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))
290 gst_channel_mix_fill_others (AudioConvertCtx * this)
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] = {
299 -1, -1}, out_c[2] = {
301 -1, -1}, out_r[2] = {
303 -1, -1}, out_s[2] = {
305 -1, -1}, out_b[2] = {
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,
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);
320 if (!in_has_center && in_has_front && out_has_center) {
321 gst_channel_mix_fill_one_other (this->matrix,
323 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
324 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
325 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
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,
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,
337 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
338 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
339 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_CENTER);
343 if (!in_has_rear && in_has_front && out_has_rear) {
344 gst_channel_mix_fill_one_other (this->matrix,
346 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
347 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
348 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
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,
356 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
357 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
358 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
360 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
361 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
362 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_REAR);
366 if (in_has_bass && !out_has_bass) {
368 gst_channel_mix_fill_one_other (this->matrix,
370 GST_AUDIO_CHANNEL_POSITION_INVALID,
371 GST_AUDIO_CHANNEL_POSITION_INVALID,
372 GST_AUDIO_CHANNEL_POSITION_LFE,
374 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
375 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
376 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_BASS);
378 if (out_has_center) {
379 gst_channel_mix_fill_one_other (this->matrix,
381 GST_AUDIO_CHANNEL_POSITION_INVALID,
382 GST_AUDIO_CHANNEL_POSITION_INVALID,
383 GST_AUDIO_CHANNEL_POSITION_LFE,
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);
390 gst_channel_mix_fill_one_other (this->matrix,
392 GST_AUDIO_CHANNEL_POSITION_INVALID,
393 GST_AUDIO_CHANNEL_POSITION_INVALID,
394 GST_AUDIO_CHANNEL_POSITION_LFE,
396 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
397 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
398 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, RATIO_REAR_BASS);
400 } else if (!in_has_bass && out_has_bass) {
402 gst_channel_mix_fill_one_other (this->matrix,
404 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
405 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
406 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
408 GST_AUDIO_CHANNEL_POSITION_INVALID,
409 GST_AUDIO_CHANNEL_POSITION_INVALID,
410 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_FRONT_BASS);
413 gst_channel_mix_fill_one_other (this->matrix,
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,
419 GST_AUDIO_CHANNEL_POSITION_INVALID,
420 GST_AUDIO_CHANNEL_POSITION_INVALID,
421 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_CENTER_BASS);
424 gst_channel_mix_fill_one_other (this->matrix,
426 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
427 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
428 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
430 GST_AUDIO_CHANNEL_POSITION_INVALID,
431 GST_AUDIO_CHANNEL_POSITION_INVALID,
432 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_REAR_BASS);
440 * Normalize output values.
444 gst_channel_mix_fill_normalize (AudioConvertCtx * this)
449 for (j = 0; j < this->out.channels; j++) {
452 for (i = 0; i < this->in.channels; i++) {
453 sum += fabs (this->matrix[i][j]);
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;
469 * Automagically generate conversion matrix.
473 gst_channel_mix_fill_matrix (AudioConvertCtx * this)
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);
481 /* only call after this->out and this->in are filled in */
483 gst_channel_mix_setup_matrix (AudioConvertCtx * this)
488 /* don't lose memory */
489 gst_channel_mix_unset_matrix (this);
492 this->tmp = g_new (gint32, this->out.channels);
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.;
502 /* setup the matrix' internal values */
503 gst_channel_mix_fill_matrix (this);
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++) {
512 g_string_append (s, ",");
513 g_string_append (s, " {");
514 for (j = 0; j < this->out.channels; j++) {
516 g_string_append (s, ",");
517 g_string_append_printf (s, " %f", this->matrix[i][j]);
519 g_string_append (s, " }");
521 g_string_append (s, " }");
523 g_string_free (s, TRUE);
527 gst_channel_mix_passthrough (AudioConvertCtx * this)
531 /* only NxN matrices can be identities */
532 if (this->in.channels != this->out.channels)
535 /* this assumes a normalized matrix */
536 for (i = 0; i < this->in.channels; i++)
537 if (this->matrix[i][i] != 1.)
543 /* IMPORTANT: out_data == in_data is possible, make sure to not overwrite data
544 * you might need later on! */
546 gst_channel_mix_mix (AudioConvertCtx * this,
547 gint32 * in_data, gint32 * out_data, gint samples)
552 gint inchannels, outchannels;
554 g_return_if_fail (this->matrix != NULL);
555 g_return_if_fail (this->tmp != NULL);
557 inchannels = this->in.channels;
558 outchannels = this->out.channels;
559 backwards = outchannels > inchannels;
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++) {
567 for (in = 0; in < inchannels; in++) {
568 res += in_data[n * inchannels + in] * this->matrix[in][out];
571 /* clip (shouldn't we use doubles instead as intermediate format?) */
572 if (res < G_MININT32)
574 else if (res > G_MAXINT32)
576 this->tmp[out] = res;
578 memcpy (&out_data[n * outchannels], this->tmp,
579 sizeof (gint32) * outchannels);