1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2.
6 #define _USE_MATH_DEFINES
8 #include "media/base/channel_mixer.h"
13 #include "base/logging.h"
14 #include "media/audio/audio_parameters.h"
15 #include "media/base/audio_bus.h"
16 #include "media/base/vector_math.h"
20 // Default scale factor for mixing two channels together. We use a different
21 // value for stereo -> mono and mono -> stereo mixes.
22 static const float kEqualPowerScale
= static_cast<float>(M_SQRT1_2
);
24 static void ValidateLayout(ChannelLayout layout
) {
25 CHECK_NE(layout
, CHANNEL_LAYOUT_NONE
);
26 CHECK_LE(layout
, CHANNEL_LAYOUT_MAX
);
27 CHECK_NE(layout
, CHANNEL_LAYOUT_UNSUPPORTED
);
28 CHECK_NE(layout
, CHANNEL_LAYOUT_DISCRETE
);
29 CHECK_NE(layout
, CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC
);
31 // Verify there's at least one channel. Should always be true here by virtue
32 // of not being one of the invalid layouts, but lets double check to be sure.
33 int channel_count
= ChannelLayoutToChannelCount(layout
);
34 DCHECK_GT(channel_count
, 0);
36 // If we have more than one channel, verify a symmetric layout for sanity.
37 // The unit test will verify all possible layouts, so this can be a DCHECK.
38 // Symmetry allows simplifying the matrix building code by allowing us to
39 // assume that if one channel of a pair exists, the other will too.
40 if (channel_count
> 1) {
41 DCHECK((ChannelOrder(layout
, LEFT
) >= 0 &&
42 ChannelOrder(layout
, RIGHT
) >= 0) ||
43 (ChannelOrder(layout
, SIDE_LEFT
) >= 0 &&
44 ChannelOrder(layout
, SIDE_RIGHT
) >= 0) ||
45 (ChannelOrder(layout
, BACK_LEFT
) >= 0 &&
46 ChannelOrder(layout
, BACK_RIGHT
) >= 0) ||
47 (ChannelOrder(layout
, LEFT_OF_CENTER
) >= 0 &&
48 ChannelOrder(layout
, RIGHT_OF_CENTER
) >= 0))
49 << "Non-symmetric channel layout encountered.";
51 DCHECK_EQ(layout
, CHANNEL_LAYOUT_MONO
);
59 MatrixBuilder(ChannelLayout input_layout
, int input_channels
,
60 ChannelLayout output_layout
, int output_channels
)
61 : input_layout_(input_layout
),
62 input_channels_(input_channels
),
63 output_layout_(output_layout
),
64 output_channels_(output_channels
) {
65 // Special case for 5.0, 5.1 with back channels when upmixed to 7.0, 7.1,
66 // which should map the back LR to side LR.
67 if (input_layout_
== CHANNEL_LAYOUT_5_0_BACK
&&
68 output_layout_
== CHANNEL_LAYOUT_7_0
) {
69 input_layout_
= CHANNEL_LAYOUT_5_0
;
70 } else if (input_layout_
== CHANNEL_LAYOUT_5_1_BACK
&&
71 output_layout_
== CHANNEL_LAYOUT_7_1
) {
72 input_layout_
= CHANNEL_LAYOUT_5_1
;
78 // Create the transformation matrix of input channels to output channels.
79 // Updates the empty matrix with the transformation, and returns true
80 // if the transformation is just a remapping of channels (no mixing).
81 bool CreateTransformationMatrix(std::vector
< std::vector
<float> >* matrix
);
84 // Result transformation of input channels to output channels
85 std::vector
< std::vector
<float> >* matrix_
;
87 // Input and output channel layout provided during construction.
88 ChannelLayout input_layout_
;
90 ChannelLayout output_layout_
;
93 // Helper variable for tracking which inputs are currently unaccounted,
94 // should be empty after construction completes.
95 std::vector
<Channels
> unaccounted_inputs_
;
97 // Helper methods for managing unaccounted input channels.
98 void AccountFor(Channels ch
);
99 bool IsUnaccounted(Channels ch
);
101 // Helper methods for checking if |ch| exists in either |input_layout_| or
102 // |output_layout_| respectively.
103 bool HasInputChannel(Channels ch
);
104 bool HasOutputChannel(Channels ch
);
106 // Helper methods for updating |matrix_| with the proper value for
107 // mixing |input_ch| into |output_ch|. MixWithoutAccounting() does not
108 // remove the channel from |unaccounted_inputs_|.
109 void Mix(Channels input_ch
, Channels output_ch
, float scale
);
110 void MixWithoutAccounting(Channels input_ch
, Channels output_ch
,
113 DISALLOW_COPY_AND_ASSIGN(MatrixBuilder
);
116 ChannelMixer::ChannelMixer(ChannelLayout input_layout
,
117 ChannelLayout output_layout
) {
118 Initialize(input_layout
,
119 ChannelLayoutToChannelCount(input_layout
),
121 ChannelLayoutToChannelCount(output_layout
));
124 ChannelMixer::ChannelMixer(
125 const AudioParameters
& input
, const AudioParameters
& output
) {
126 Initialize(input
.channel_layout(),
128 output
.channel_layout(),
132 void ChannelMixer::Initialize(
133 ChannelLayout input_layout
, int input_channels
,
134 ChannelLayout output_layout
, int output_channels
) {
135 // Stereo down mix should never be the output layout.
136 CHECK_NE(output_layout
, CHANNEL_LAYOUT_STEREO_DOWNMIX
);
138 // Verify that the layouts are supported
139 if (input_layout
!= CHANNEL_LAYOUT_DISCRETE
)
140 ValidateLayout(input_layout
);
141 if (output_layout
!= CHANNEL_LAYOUT_DISCRETE
)
142 ValidateLayout(output_layout
);
144 // Create the transformation matrix
145 MatrixBuilder
matrix_builder(input_layout
, input_channels
,
146 output_layout
, output_channels
);
147 remapping_
= matrix_builder
.CreateTransformationMatrix(&matrix_
);
150 bool MatrixBuilder::CreateTransformationMatrix(
151 std::vector
< std::vector
<float> >* matrix
) {
154 // Size out the initial matrix.
155 matrix_
->reserve(output_channels_
);
156 for (int output_ch
= 0; output_ch
< output_channels_
; ++output_ch
)
157 matrix_
->push_back(std::vector
<float>(input_channels_
, 0));
159 // First check for discrete case.
160 if (input_layout_
== CHANNEL_LAYOUT_DISCRETE
||
161 output_layout_
== CHANNEL_LAYOUT_DISCRETE
) {
162 // If the number of input channels is more than output channels, then
163 // copy as many as we can then drop the remaining input channels.
164 // If the number of input channels is less than output channels, then
165 // copy them all, then zero out the remaining output channels.
166 int passthrough_channels
= std::min(input_channels_
, output_channels_
);
167 for (int i
= 0; i
< passthrough_channels
; ++i
)
168 (*matrix_
)[i
][i
] = 1;
173 // Route matching channels and figure out which ones aren't accounted for.
174 for (Channels ch
= LEFT
; ch
< CHANNELS_MAX
+ 1;
175 ch
= static_cast<Channels
>(ch
+ 1)) {
176 int input_ch_index
= ChannelOrder(input_layout_
, ch
);
177 if (input_ch_index
< 0)
180 int output_ch_index
= ChannelOrder(output_layout_
, ch
);
181 if (output_ch_index
< 0) {
182 unaccounted_inputs_
.push_back(ch
);
186 DCHECK_LT(static_cast<size_t>(output_ch_index
), matrix_
->size());
187 DCHECK_LT(static_cast<size_t>(input_ch_index
),
188 (*matrix_
)[output_ch_index
].size());
189 (*matrix_
)[output_ch_index
][input_ch_index
] = 1;
192 // If all input channels are accounted for, there's nothing left to do.
193 if (unaccounted_inputs_
.empty()) {
194 // Since all output channels map directly to inputs we can optimize.
198 // Mix front LR into center.
199 if (IsUnaccounted(LEFT
)) {
200 // When down mixing to mono from stereo, we need to be careful of full scale
201 // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping
202 // so we use 1 / 2 instead.
204 (output_layout_
== CHANNEL_LAYOUT_MONO
&& input_channels_
== 2) ?
205 0.5 : kEqualPowerScale
;
206 Mix(LEFT
, CENTER
, scale
);
207 Mix(RIGHT
, CENTER
, scale
);
210 // Mix center into front LR.
211 if (IsUnaccounted(CENTER
)) {
212 // When up mixing from mono, just do a copy to front LR.
214 (input_layout_
== CHANNEL_LAYOUT_MONO
) ? 1 : kEqualPowerScale
;
215 MixWithoutAccounting(CENTER
, LEFT
, scale
);
216 Mix(CENTER
, RIGHT
, scale
);
219 // Mix back LR into: side LR || back center || front LR || front center.
220 if (IsUnaccounted(BACK_LEFT
)) {
221 if (HasOutputChannel(SIDE_LEFT
)) {
222 // If we have side LR, mix back LR into side LR, but instead if the input
223 // doesn't have side LR (but output does) copy back LR to side LR.
224 float scale
= HasInputChannel(SIDE_LEFT
) ? kEqualPowerScale
: 1;
225 Mix(BACK_LEFT
, SIDE_LEFT
, scale
);
226 Mix(BACK_RIGHT
, SIDE_RIGHT
, scale
);
227 } else if (HasOutputChannel(BACK_CENTER
)) {
228 // Mix back LR into back center.
229 Mix(BACK_LEFT
, BACK_CENTER
, kEqualPowerScale
);
230 Mix(BACK_RIGHT
, BACK_CENTER
, kEqualPowerScale
);
231 } else if (output_layout_
> CHANNEL_LAYOUT_MONO
) {
232 // Mix back LR into front LR.
233 Mix(BACK_LEFT
, LEFT
, kEqualPowerScale
);
234 Mix(BACK_RIGHT
, RIGHT
, kEqualPowerScale
);
236 // Mix back LR into front center.
237 Mix(BACK_LEFT
, CENTER
, kEqualPowerScale
);
238 Mix(BACK_RIGHT
, CENTER
, kEqualPowerScale
);
242 // Mix side LR into: back LR || back center || front LR || front center.
243 if (IsUnaccounted(SIDE_LEFT
)) {
244 if (HasOutputChannel(BACK_LEFT
)) {
245 // If we have back LR, mix side LR into back LR, but instead if the input
246 // doesn't have back LR (but output does) copy side LR to back LR.
247 float scale
= HasInputChannel(BACK_LEFT
) ? kEqualPowerScale
: 1;
248 Mix(SIDE_LEFT
, BACK_LEFT
, scale
);
249 Mix(SIDE_RIGHT
, BACK_RIGHT
, scale
);
250 } else if (HasOutputChannel(BACK_CENTER
)) {
251 // Mix side LR into back center.
252 Mix(SIDE_LEFT
, BACK_CENTER
, kEqualPowerScale
);
253 Mix(SIDE_RIGHT
, BACK_CENTER
, kEqualPowerScale
);
254 } else if (output_layout_
> CHANNEL_LAYOUT_MONO
) {
255 // Mix side LR into front LR.
256 Mix(SIDE_LEFT
, LEFT
, kEqualPowerScale
);
257 Mix(SIDE_RIGHT
, RIGHT
, kEqualPowerScale
);
259 // Mix side LR into front center.
260 Mix(SIDE_LEFT
, CENTER
, kEqualPowerScale
);
261 Mix(SIDE_RIGHT
, CENTER
, kEqualPowerScale
);
265 // Mix back center into: back LR || side LR || front LR || front center.
266 if (IsUnaccounted(BACK_CENTER
)) {
267 if (HasOutputChannel(BACK_LEFT
)) {
268 // Mix back center into back LR.
269 MixWithoutAccounting(BACK_CENTER
, BACK_LEFT
, kEqualPowerScale
);
270 Mix(BACK_CENTER
, BACK_RIGHT
, kEqualPowerScale
);
271 } else if (HasOutputChannel(SIDE_LEFT
)) {
272 // Mix back center into side LR.
273 MixWithoutAccounting(BACK_CENTER
, SIDE_LEFT
, kEqualPowerScale
);
274 Mix(BACK_CENTER
, SIDE_RIGHT
, kEqualPowerScale
);
275 } else if (output_layout_
> CHANNEL_LAYOUT_MONO
) {
276 // Mix back center into front LR.
277 // TODO(dalecurtis): Not sure about these values?
278 MixWithoutAccounting(BACK_CENTER
, LEFT
, kEqualPowerScale
);
279 Mix(BACK_CENTER
, RIGHT
, kEqualPowerScale
);
281 // Mix back center into front center.
282 // TODO(dalecurtis): Not sure about these values?
283 Mix(BACK_CENTER
, CENTER
, kEqualPowerScale
);
287 // Mix LR of center into: front center || front LR.
288 if (IsUnaccounted(LEFT_OF_CENTER
)) {
289 if (HasOutputChannel(LEFT
)) {
290 // Mix LR of center into front LR.
291 Mix(LEFT_OF_CENTER
, LEFT
, kEqualPowerScale
);
292 Mix(RIGHT_OF_CENTER
, RIGHT
, kEqualPowerScale
);
294 // Mix LR of center into front center.
295 Mix(LEFT_OF_CENTER
, CENTER
, kEqualPowerScale
);
296 Mix(RIGHT_OF_CENTER
, CENTER
, kEqualPowerScale
);
300 // Mix LFE into: front LR || front center.
301 if (IsUnaccounted(LFE
)) {
302 if (!HasOutputChannel(CENTER
)) {
303 // Mix LFE into front LR.
304 MixWithoutAccounting(LFE
, LEFT
, kEqualPowerScale
);
305 Mix(LFE
, RIGHT
, kEqualPowerScale
);
307 // Mix LFE into front center.
308 Mix(LFE
, CENTER
, kEqualPowerScale
);
312 // All channels should now be accounted for.
313 DCHECK(unaccounted_inputs_
.empty());
315 // See if the output |matrix_| is simply a remapping matrix. If each input
316 // channel maps to a single output channel we can simply remap. Doing this
317 // programmatically is less fragile than logic checks on channel mappings.
318 for (int output_ch
= 0; output_ch
< output_channels_
; ++output_ch
) {
319 int input_mappings
= 0;
320 for (int input_ch
= 0; input_ch
< input_channels_
; ++input_ch
) {
321 // We can only remap if each row contains a single scale of 1. I.e., each
322 // output channel is mapped from a single unscaled input channel.
323 if ((*matrix_
)[output_ch
][input_ch
] != 1 || ++input_mappings
> 1)
328 // If we've gotten here, |matrix_| is simply a remapping.
332 ChannelMixer::~ChannelMixer() {}
334 void ChannelMixer::Transform(const AudioBus
* input
, AudioBus
* output
) {
335 CHECK_EQ(matrix_
.size(), static_cast<size_t>(output
->channels()));
336 CHECK_EQ(matrix_
[0].size(), static_cast<size_t>(input
->channels()));
337 CHECK_EQ(input
->frames(), output
->frames());
339 // Zero initialize |output| so we're accumulating from zero.
342 // If we're just remapping we can simply copy the correct input to output.
344 for (int output_ch
= 0; output_ch
< output
->channels(); ++output_ch
) {
345 for (int input_ch
= 0; input_ch
< input
->channels(); ++input_ch
) {
346 float scale
= matrix_
[output_ch
][input_ch
];
348 DCHECK_EQ(scale
, 1.0f
);
349 memcpy(output
->channel(output_ch
), input
->channel(input_ch
),
350 sizeof(*output
->channel(output_ch
)) * output
->frames());
358 for (int output_ch
= 0; output_ch
< output
->channels(); ++output_ch
) {
359 for (int input_ch
= 0; input_ch
< input
->channels(); ++input_ch
) {
360 float scale
= matrix_
[output_ch
][input_ch
];
361 // Scale should always be positive. Don't bother scaling by zero.
364 vector_math::FMAC(input
->channel(input_ch
), scale
, output
->frames(),
365 output
->channel(output_ch
));
371 void MatrixBuilder::AccountFor(Channels ch
) {
372 unaccounted_inputs_
.erase(std::find(
373 unaccounted_inputs_
.begin(), unaccounted_inputs_
.end(), ch
));
376 bool MatrixBuilder::IsUnaccounted(Channels ch
) {
377 return std::find(unaccounted_inputs_
.begin(), unaccounted_inputs_
.end(),
378 ch
) != unaccounted_inputs_
.end();
381 bool MatrixBuilder::HasInputChannel(Channels ch
) {
382 return ChannelOrder(input_layout_
, ch
) >= 0;
385 bool MatrixBuilder::HasOutputChannel(Channels ch
) {
386 return ChannelOrder(output_layout_
, ch
) >= 0;
389 void MatrixBuilder::Mix(Channels input_ch
, Channels output_ch
, float scale
) {
390 MixWithoutAccounting(input_ch
, output_ch
, scale
);
391 AccountFor(input_ch
);
394 void MatrixBuilder::MixWithoutAccounting(Channels input_ch
, Channels output_ch
,
396 int input_ch_index
= ChannelOrder(input_layout_
, input_ch
);
397 int output_ch_index
= ChannelOrder(output_layout_
, output_ch
);
399 DCHECK(IsUnaccounted(input_ch
));
400 DCHECK_GE(input_ch_index
, 0);
401 DCHECK_GE(output_ch_index
, 0);
403 DCHECK_EQ((*matrix_
)[output_ch_index
][input_ch_index
], 0);
404 (*matrix_
)[output_ch_index
][input_ch_index
] = scale
;