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"
33 * Channel matrix functions.
37 gst_channel_mix_unset_matrix (AudioConvertCtx * this)
41 /* don't access if nothing there */
46 for (i = 0; i < this->in.channels; i++)
47 g_free (this->matrix[i]);
48 g_free (this->matrix);
56 * Detect and fill in identical channels. E.g.
57 * forward the left/right front channels in a
58 * 5.1 to 2.0 conversion.
62 gst_channel_mix_fill_identical (AudioConvertCtx * this)
66 /* Apart from the compatible channel assignments, we can also have
67 * same channel assignments. This is much simpler, we simply copy
68 * the value from source to dest! */
69 for (co = 0; co < this->out.channels; co++) {
70 /* find a channel in input with same position */
71 for (ci = 0; ci < this->in.channels; ci++) {
72 if (this->in.pos[ci] == this->out.pos[co]) {
73 this->matrix[ci][co] = 1.0;
80 * Detect and fill in compatible channels. E.g.
81 * forward left/right front to mono (or the other
82 * way around) when going from 2.0 to 1.0.
86 gst_channel_mix_fill_compatible (AudioConvertCtx * this)
88 /* Conversions from one-channel to compatible two-channel configs */
91 GstAudioChannelPosition pos1[2];
92 GstAudioChannelPosition pos2[1];
94 /* front: mono <-> stereo */
96 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
97 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
98 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}},
99 /* front center: 2 <-> 1 */
101 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
102 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
103 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}},
106 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
107 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
108 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { {
109 GST_AUDIO_CHANNEL_POSITION_INVALID}}
113 /* conversions from compatible (but not the same) channel schemes. This
114 * goes two ways: if the sink has both pos1[0,1] and src has pos2[0] or
115 * if the src has both pos1[0,1] and sink has pos2[0], then we do the
116 * conversion. We hereby assume that the existance of pos1[0,1] and
117 * pos2[0] are mututally exclusive. There are no checks for that,
118 * unfortunately. This shouldn't lead to issues (like crashes or so),
120 for (c = 0; conv[c].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; c++) {
121 gint pos1_0 = -1, pos1_1 = -1, pos2_0 = -1, n;
123 /* Try to go from the given 2 channels to the given 1 channel */
124 for (n = 0; n < this->in.channels; n++) {
125 if (this->in.pos[n] == conv[c].pos1[0])
127 else if (this->in.pos[n] == conv[c].pos1[1])
130 for (n = 0; n < this->out.channels; n++) {
131 if (this->out.pos[n] == conv[c].pos2[0])
135 if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
136 this->matrix[pos1_0][pos2_0] = 1.0;
137 this->matrix[pos1_1][pos2_0] = 1.0;
140 /* Try to go from the given 1 channel to the given 2 channels */
145 for (n = 0; n < this->out.channels; n++) {
146 if (this->out.pos[n] == conv[c].pos1[0])
148 else if (this->out.pos[n] == conv[c].pos1[1])
151 for (n = 0; n < this->in.channels; n++) {
152 if (this->in.pos[n] == conv[c].pos2[0])
156 if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
157 this->matrix[pos2_0][pos1_0] = 1.0;
158 this->matrix[pos2_0][pos1_1] = 1.0;
164 * Detect and fill in channels not handled by the
165 * above two, e.g. center to left/right front in
166 * 5.1 to 2.0 (or the other way around).
168 * Unfortunately, limited to static conversions
173 gst_channel_mix_detect_pos (AudioConvertFmt * caps,
174 gint * f, gboolean * has_f,
175 gint * c, gboolean * has_c, gint * r, gboolean * has_r,
176 gint * s, gboolean * has_s, gint * b, gboolean * has_b)
180 for (n = 0; n < caps->channels; n++) {
181 switch (caps->pos[n]) {
182 case GST_AUDIO_CHANNEL_POSITION_FRONT_MONO:
183 case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
184 case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
191 case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
192 case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
193 case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
200 case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
201 case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
202 case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
209 case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
210 case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
217 case GST_AUDIO_CHANNEL_POSITION_LFE:
228 gst_channel_mix_fill_one_other (gfloat ** matrix,
229 AudioConvertFmt * from_caps, gint * from_idx,
230 GstAudioChannelPosition from_pos_l,
231 GstAudioChannelPosition from_pos_r,
232 GstAudioChannelPosition from_pos_c,
233 AudioConvertFmt * to_caps, gint * to_idx,
234 GstAudioChannelPosition to_pos_l,
235 GstAudioChannelPosition to_pos_r,
236 GstAudioChannelPosition to_pos_c, gfloat ratio)
238 gfloat in_r, out_r[2] = { 0.f, 0.f };
241 * The idea is that we add up from the input (which means that if we
242 * have stereo input, we divide their sum by two) and put that in
243 * the matrix for their output ratio (given in $ratio).
244 * For left channels, we need to invert the signal sign (* -1).
247 if (from_caps->pos[from_idx[0]] == from_pos_c)
252 if (to_caps->pos[to_idx[0]] == to_pos_l)
253 out_r[0] = in_r * -ratio;
255 out_r[0] = in_r * ratio;
257 if (to_idx[1] != -1) {
258 if (to_caps->pos[to_idx[1]] == to_pos_l)
259 out_r[1] = in_r * -ratio;
261 out_r[1] = in_r * ratio;
264 matrix[from_idx[0]][to_idx[0]] = out_r[0];
266 matrix[from_idx[0]][to_idx[1]] = out_r[1];
267 if (from_idx[1] != -1) {
268 matrix[from_idx[1]][to_idx[0]] = out_r[0];
270 matrix[from_idx[1]][to_idx[1]] = out_r[1];
274 #define RATIO_FRONT_CENTER (1.0 / sqrt (2.0))
275 #define RATIO_FRONT_REAR (1.0 / sqrt (2.0))
276 #define RATIO_FRONT_BASS (1.0)
277 #define RATIO_REAR_BASS (1.0 / sqrt (2.0))
278 #define RATIO_CENTER_BASS (1.0 / sqrt (2.0))
281 gst_channel_mix_fill_others (AudioConvertCtx * this)
283 gboolean in_has_front = FALSE, out_has_front = FALSE,
284 in_has_center = FALSE, out_has_center = FALSE,
285 in_has_rear = FALSE, out_has_rear = FALSE,
286 in_has_side = FALSE, out_has_side = FALSE,
287 in_has_bass = FALSE, out_has_bass = FALSE;
288 gint in_f[2] = { -1, -1 }, out_f[2] = {
290 -1, -1}, out_c[2] = {
292 -1, -1}, out_r[2] = {
294 -1, -1}, out_s[2] = {
296 -1, -1}, out_b[2] = {
299 /* First see where (if at all) the various channels from/to
300 * which we want to convert are located in our matrix/array. */
301 gst_channel_mix_detect_pos (&this->in,
303 in_c, &in_has_center, in_r, &in_has_rear,
304 in_s, &in_has_side, in_b, &in_has_bass);
305 gst_channel_mix_detect_pos (&this->out,
306 out_f, &out_has_front,
307 out_c, &out_has_center, out_r, &out_has_rear,
308 out_s, &out_has_side, out_b, &out_has_bass);
311 if (!in_has_center && in_has_front && out_has_center) {
312 gst_channel_mix_fill_one_other (this->matrix,
314 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
315 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
316 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
318 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
319 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
320 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, RATIO_FRONT_CENTER);
321 } else if (in_has_center && !out_has_center && out_has_front) {
322 gst_channel_mix_fill_one_other (this->matrix,
324 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
325 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
326 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
328 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
329 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
330 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_CENTER);
334 if (!in_has_rear && in_has_front && out_has_rear) {
335 gst_channel_mix_fill_one_other (this->matrix,
337 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
338 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
339 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
341 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
342 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
343 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, RATIO_FRONT_REAR);
344 } else if (in_has_rear && !out_has_rear && out_has_front) {
345 gst_channel_mix_fill_one_other (this->matrix,
347 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
348 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
349 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
351 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
352 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
353 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_REAR);
357 if (in_has_bass && !out_has_bass) {
359 gst_channel_mix_fill_one_other (this->matrix,
361 GST_AUDIO_CHANNEL_POSITION_INVALID,
362 GST_AUDIO_CHANNEL_POSITION_INVALID,
363 GST_AUDIO_CHANNEL_POSITION_LFE,
365 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
366 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
367 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, RATIO_FRONT_BASS);
369 if (out_has_center) {
370 gst_channel_mix_fill_one_other (this->matrix,
372 GST_AUDIO_CHANNEL_POSITION_INVALID,
373 GST_AUDIO_CHANNEL_POSITION_INVALID,
374 GST_AUDIO_CHANNEL_POSITION_LFE,
376 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
377 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
378 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, RATIO_CENTER_BASS);
381 gst_channel_mix_fill_one_other (this->matrix,
383 GST_AUDIO_CHANNEL_POSITION_INVALID,
384 GST_AUDIO_CHANNEL_POSITION_INVALID,
385 GST_AUDIO_CHANNEL_POSITION_LFE,
387 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
388 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
389 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, RATIO_REAR_BASS);
391 } else if (!in_has_bass && out_has_bass) {
393 gst_channel_mix_fill_one_other (this->matrix,
395 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
396 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
397 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
399 GST_AUDIO_CHANNEL_POSITION_INVALID,
400 GST_AUDIO_CHANNEL_POSITION_INVALID,
401 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_FRONT_BASS);
404 gst_channel_mix_fill_one_other (this->matrix,
406 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
407 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
408 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
410 GST_AUDIO_CHANNEL_POSITION_INVALID,
411 GST_AUDIO_CHANNEL_POSITION_INVALID,
412 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_CENTER_BASS);
415 gst_channel_mix_fill_one_other (this->matrix,
417 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
418 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
419 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
421 GST_AUDIO_CHANNEL_POSITION_INVALID,
422 GST_AUDIO_CHANNEL_POSITION_INVALID,
423 GST_AUDIO_CHANNEL_POSITION_LFE, RATIO_REAR_BASS);
431 * Normalize output values.
435 gst_channel_mix_fill_normalize (AudioConvertCtx * this)
440 for (j = 0; j < this->out.channels; j++) {
443 for (i = 0; i < this->in.channels; i++) {
444 sum += fabs (this->matrix[i][j]);
451 /* normalize to this */
452 for (j = 0; j < this->out.channels; j++) {
453 for (i = 0; i < this->in.channels; i++) {
454 this->matrix[i][j] /= top;
460 * Automagically generate conversion matrix.
464 gst_channel_mix_fill_matrix (AudioConvertCtx * this)
466 gst_channel_mix_fill_identical (this);
468 if (!this->in.unpositioned_layout) {
469 gst_channel_mix_fill_compatible (this);
470 gst_channel_mix_fill_others (this);
471 gst_channel_mix_fill_normalize (this);
475 /* only call after this->out and this->in are filled in */
477 gst_channel_mix_setup_matrix (AudioConvertCtx * this)
482 /* don't lose memory */
483 gst_channel_mix_unset_matrix (this);
486 if (this->in.is_int || this->out.is_int) {
487 this->tmp = (gpointer) g_new (gint32, this->out.channels);
489 this->tmp = (gpointer) g_new (gdouble, this->out.channels);
493 this->matrix = g_new0 (gfloat *, this->in.channels);
494 for (i = 0; i < this->in.channels; i++) {
495 this->matrix[i] = g_new (gfloat, this->out.channels);
496 for (j = 0; j < this->out.channels; j++)
497 this->matrix[i][j] = 0.;
500 /* setup the matrix' internal values */
501 gst_channel_mix_fill_matrix (this);
504 s = g_string_new ("Matrix for");
505 g_string_append_printf (s, " %d -> %d: ",
506 this->in.channels, this->out.channels);
507 g_string_append (s, "{");
508 for (i = 0; i < this->in.channels; i++) {
510 g_string_append (s, ",");
511 g_string_append (s, " {");
512 for (j = 0; j < this->out.channels; j++) {
514 g_string_append (s, ",");
515 g_string_append_printf (s, " %f", this->matrix[i][j]);
517 g_string_append (s, " }");
519 g_string_append (s, " }");
521 g_string_free (s, TRUE);
525 gst_channel_mix_passthrough (AudioConvertCtx * this)
529 /* only NxN matrices can be identities */
530 if (this->in.channels != this->out.channels)
533 /* this assumes a normalized matrix */
534 for (i = 0; i < this->in.channels; i++)
535 if (this->matrix[i][i] != 1.)
541 /* IMPORTANT: out_data == in_data is possible, make sure to not overwrite data
542 * you might need later on! */
544 gst_channel_mix_mix_int (AudioConvertCtx * this,
545 gint32 * in_data, gint32 * out_data, gint samples)
550 gint inchannels, outchannels;
551 gint32 *tmp = (gint32 *) this->tmp;
553 g_return_if_fail (this->matrix != NULL);
554 g_return_if_fail (this->tmp != NULL);
556 inchannels = this->in.channels;
557 outchannels = this->out.channels;
558 backwards = outchannels > inchannels;
560 /* FIXME: use liboil here? */
561 for (n = (backwards ? samples - 1 : 0); n < samples && n >= 0;
562 backwards ? n-- : n++) {
563 for (out = 0; out < outchannels; out++) {
566 for (in = 0; in < inchannels; in++) {
567 res += in_data[n * inchannels + in] * this->matrix[in][out];
570 /* clip (shouldn't we use doubles instead as intermediate format?) */
571 if (res < G_MININT32)
573 else if (res > G_MAXINT32)
577 memcpy (&out_data[n * outchannels], this->tmp,
578 sizeof (gint32) * outchannels);
583 gst_channel_mix_mix_float (AudioConvertCtx * this,
584 gdouble * in_data, gdouble * out_data, gint samples)
589 gint inchannels, outchannels;
590 gdouble *tmp = (gdouble *) this->tmp;
592 g_return_if_fail (this->matrix != NULL);
593 g_return_if_fail (this->tmp != NULL);
595 inchannels = this->in.channels;
596 outchannels = this->out.channels;
597 backwards = outchannels > inchannels;
599 /* FIXME: use liboil here? */
600 for (n = (backwards ? samples - 1 : 0); n < samples && n >= 0;
601 backwards ? n-- : n++) {
602 for (out = 0; out < outchannels; out++) {
605 for (in = 0; in < inchannels; in++) {
606 res += in_data[n * inchannels + in] * this->matrix[in][out];
609 /* clip (shouldn't we use doubles instead as intermediate format?) */
616 memcpy (&out_data[n * outchannels], this->tmp,
617 sizeof (gdouble) * outchannels);