OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2. | |
6 #define _USE_MATH_DEFINES | |
7 | |
8 #include "media/base/channel_mixer.h" | |
9 | |
10 #include <algorithm> | |
11 #include <cmath> | |
12 | |
13 #include "base/logging.h" | |
14 #include "media/base/audio_bus.h" | |
15 #include "media/base/vector_math.h" | |
16 | |
17 namespace media { | |
18 | |
19 // Default scale factor for mixing two channels together. We use a different | |
20 // value for stereo -> mono and mono -> stereo mixes. | |
21 static const float kDefaultScale = static_cast<float>(M_SQRT1_2); | |
Chris Rogers
2012/10/19 20:48:17
A better name might be kEqualPowerScale
DaleCurtis
2012/10/19 22:33:20
Done.
| |
22 | |
23 static int ValidateLayout(ChannelLayout layout) { | |
24 CHECK_NE(layout, CHANNEL_LAYOUT_NONE); | |
25 CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED); | |
26 CHECK_NE(layout, CHANNEL_LAYOUT_MAX); | |
Chris Rogers
2012/10/19 20:48:17
Can't we handle the CHANNEL_LAYOUT_UNSUPPORTED cas
DaleCurtis
2012/10/19 22:33:20
No, this means the AudioParameters are invalid and
Chris Rogers
2012/10/19 22:52:26
We might require that *now*, but this needs to be
DaleCurtis
2012/10/19 23:14:32
Sure we definitely want to handle this some day, b
| |
27 | |
28 // Verify there's at least one channel. Should always be true here by virtue | |
29 // of not being one of the invalid layouts, but lets double check to be sure. | |
30 int channel_count = ChannelLayoutToChannelCount(layout); | |
31 DCHECK_GT(channel_count, 0); | |
32 | |
33 // If we have more than one channel, verify a symmetric layout for sanity. | |
34 // The unit test will verify all possible layouts, so this can be a DCHECK. | |
35 // Symmetry allows simplifying the matrix building code by allowing us to | |
36 // assume that if one channel of a pair exists, the other will too. | |
37 if (channel_count > 1) { | |
38 DCHECK((ChannelOrder(layout, LEFT) >= 0 && | |
39 ChannelOrder(layout, RIGHT) >= 0) || | |
40 (ChannelOrder(layout, SIDE_LEFT) >= 0 && | |
41 ChannelOrder(layout, SIDE_RIGHT) >= 0) || | |
42 (ChannelOrder(layout, BACK_LEFT) >= 0 && | |
43 ChannelOrder(layout, BACK_RIGHT) >= 0) || | |
44 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 && | |
45 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0)) | |
46 << "Non-symmetric channel layout encountered."; | |
47 } else { | |
48 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); | |
49 } | |
50 | |
51 return channel_count; | |
52 } | |
53 | |
54 ChannelMixer::ChannelMixer(ChannelLayout input, ChannelLayout output) | |
55 : input_layout_(input), | |
56 output_layout_(output), | |
57 remapping_(false) { | |
58 // Stereo down mix should never be the output layout. | |
59 CHECK_NE(output_layout_, CHANNEL_LAYOUT_STEREO_DOWNMIX); | |
60 | |
Chris Rogers
2012/10/19 20:48:17
maybe we should at least DCHECK that input!=output
DaleCurtis
2012/10/19 22:33:20
Since these layouts might be chosen dynamically, i
| |
61 int input_channels = ValidateLayout(input_layout_); | |
62 int output_channels = ValidateLayout(output_layout_); | |
63 | |
64 // Size out the initial matrix. | |
65 matrix_.reserve(output_channels); | |
66 for (int output_ch = 0; output_ch < output_channels; ++output_ch) | |
67 matrix_.push_back(std::vector<float>(input_channels, 0)); | |
68 | |
69 // Route matching channels and figure out which ones aren't accounted for. | |
70 for (Channels ch = LEFT; ch < CHANNELS_MAX; | |
71 ch = static_cast<Channels>(ch + 1)) { | |
72 int input_ch_index = ChannelOrder(input_layout_, ch); | |
73 int output_ch_index = ChannelOrder(output_layout_, ch); | |
74 | |
75 if (input_ch_index < 0) | |
76 continue; | |
77 | |
78 if (output_ch_index < 0) { | |
79 unaccounted_inputs_.push_back(ch); | |
80 continue; | |
81 } | |
82 | |
83 DCHECK_LT(static_cast<size_t>(output_ch_index), matrix_.size()); | |
84 DCHECK_LT(static_cast<size_t>(input_ch_index), | |
85 matrix_[output_ch_index].size()); | |
86 matrix_[output_ch_index][input_ch_index] = 1; | |
87 } | |
88 | |
89 // If all input channels are accounted for, there's nothing left to do. | |
90 if (unaccounted_inputs_.empty()) { | |
91 // Since all output channels map directly to inputs we can optimize. | |
92 remapping_ = true; | |
93 return; | |
94 } | |
95 | |
96 // Mix front LR into center. | |
97 if (IsUnaccounted(LEFT)) { | |
98 // When down mixing to mono from stereo, we need to be careful of full scale | |
99 // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping | |
100 // so we use 1 / 2 instead. | |
101 float scale = (output == CHANNEL_LAYOUT_MONO && input_channels == 2) ? | |
102 0.5 : kDefaultScale; | |
103 Mix(LEFT, CENTER, scale); | |
104 Mix(RIGHT, CENTER, scale); | |
105 } | |
106 | |
107 // Mix center into front LR. | |
108 if (IsUnaccounted(CENTER)) { | |
109 // When up mixing from mono, just do a copy to front LR. | |
110 float scale = (input == CHANNEL_LAYOUT_MONO) ? 1 : kDefaultScale; | |
111 MixWithoutAccounting(CENTER, LEFT, scale); | |
112 Mix(CENTER, RIGHT, scale); | |
113 } | |
114 | |
115 // Mix back LR into: side LR || back center || front LR || front center. | |
116 if (IsUnaccounted(BACK_LEFT)) { | |
117 if (HasOutputChannel(SIDE_LEFT)) { | |
118 // If we have side LR, mix back LR into side LR, but instead if the input | |
119 // doesn't have side LR (but output does) copy back LR to side LR. | |
120 float scale = HasInputChannel(SIDE_LEFT) ? kDefaultScale : 1; | |
121 Mix(BACK_LEFT, SIDE_LEFT, scale); | |
122 Mix(BACK_RIGHT, SIDE_RIGHT, scale); | |
123 } else if (HasOutputChannel(BACK_CENTER)) { | |
124 // Mix back LR into back center. | |
125 Mix(BACK_LEFT, BACK_CENTER, kDefaultScale); | |
126 Mix(BACK_RIGHT, BACK_CENTER, kDefaultScale); | |
127 } else if (output > CHANNEL_LAYOUT_MONO) { | |
128 // Mix back LR into front LR. | |
129 Mix(BACK_LEFT, LEFT, kDefaultScale); | |
130 Mix(BACK_RIGHT, RIGHT, kDefaultScale); | |
131 } else { | |
132 // Mix back LR into front center. | |
133 Mix(BACK_LEFT, CENTER, kDefaultScale); | |
134 Mix(BACK_RIGHT, CENTER, kDefaultScale); | |
135 } | |
136 } | |
137 | |
138 // Mix side LR into: back LR || back center || front LR || front center. | |
139 if (IsUnaccounted(SIDE_LEFT)) { | |
140 if (HasOutputChannel(BACK_LEFT)) { | |
141 // If we have back LR, mix side LR into back LR, but instead if the input | |
142 // doesn't have back LR (but output does) copy side LR to back LR. | |
143 float scale = HasInputChannel(BACK_LEFT) ? kDefaultScale : 1; | |
144 Mix(SIDE_LEFT, BACK_LEFT, scale); | |
145 Mix(SIDE_RIGHT, BACK_RIGHT, scale); | |
146 } else if (HasOutputChannel(BACK_CENTER)) { | |
147 // Mix side LR into back center. | |
148 Mix(SIDE_LEFT, BACK_CENTER, kDefaultScale); | |
149 Mix(SIDE_RIGHT, BACK_CENTER, kDefaultScale); | |
150 } else if (output > CHANNEL_LAYOUT_MONO) { | |
151 // Mix side LR into front LR. | |
152 Mix(SIDE_LEFT, LEFT, kDefaultScale); | |
153 Mix(SIDE_RIGHT, RIGHT, kDefaultScale); | |
154 } else { | |
155 // Mix side LR into front center. | |
156 Mix(SIDE_LEFT, CENTER, kDefaultScale); | |
157 Mix(SIDE_RIGHT, CENTER, kDefaultScale); | |
158 } | |
159 } | |
160 | |
161 // Mix back center into: back LR || side LR || front LR || front center. | |
162 if (IsUnaccounted(BACK_CENTER)) { | |
163 if (HasOutputChannel(BACK_LEFT)) { | |
164 // Mix back center into back LR. | |
165 MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kDefaultScale); | |
166 Mix(BACK_CENTER, BACK_RIGHT, kDefaultScale); | |
167 } else if (HasOutputChannel(SIDE_LEFT)) { | |
168 // Mix back center into side LR. | |
169 MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kDefaultScale); | |
170 Mix(BACK_CENTER, SIDE_RIGHT, kDefaultScale); | |
171 } else if (output > CHANNEL_LAYOUT_MONO) { | |
172 // Mix back center into front LR. | |
173 // TODO(dalecurtis): Not sure about these values? | |
174 MixWithoutAccounting(BACK_CENTER, LEFT, kDefaultScale * kDefaultScale); | |
Chris Rogers
2012/10/19 20:48:17
why kDefaultScale*kDefaultScale ? -- I would just
DaleCurtis
2012/10/19 22:33:20
ffmpeg used that value, my assumption was that you
| |
175 Mix(BACK_CENTER, RIGHT, kDefaultScale * kDefaultScale); | |
176 } else { | |
177 // Mix back center into front center. | |
178 // TODO(dalecurtis): Not sure about these values? | |
179 Mix(BACK_CENTER, CENTER, kDefaultScale * kDefaultScale); | |
180 } | |
181 } | |
182 | |
183 // Mix LR of center into: front center || front LR. | |
184 if (IsUnaccounted(LEFT_OF_CENTER)) { | |
185 if (HasOutputChannel(CENTER)) { | |
186 // Mix LR of center into front center. | |
187 Mix(LEFT_OF_CENTER, CENTER, kDefaultScale); | |
188 Mix(RIGHT_OF_CENTER, CENTER, kDefaultScale); | |
189 } else { | |
190 // Mix LR of center into front LR. | |
191 Mix(LEFT_OF_CENTER, LEFT, kDefaultScale); | |
192 Mix(RIGHT_OF_CENTER, RIGHT, kDefaultScale); | |
193 } | |
194 } | |
195 | |
196 // Mix LFE into: front LR || front center. | |
197 if (IsUnaccounted(LFE)) { | |
198 if (!HasOutputChannel(CENTER)) { | |
199 // Mix LFE into front LR. | |
200 MixWithoutAccounting(LFE, LEFT, kDefaultScale); | |
201 Mix(LFE, RIGHT, kDefaultScale); | |
202 } else { | |
203 // Mix LFE into front center. | |
204 Mix(LFE, CENTER, kDefaultScale); | |
205 } | |
206 } | |
207 | |
208 // All channels should now be accounted for. | |
209 DCHECK(unaccounted_inputs_.empty()); | |
210 | |
211 // See if the output |matrix_| is simply a remapping matrix. If each input | |
212 // channel maps to a single output channel we can simply remap. Doing this | |
213 // programmatically is less fragile than logic checks on channel mappings. | |
214 for (int output_ch = 0; output_ch < output_channels; ++output_ch) { | |
215 int input_mappings = 0; | |
216 for (int input_ch = 0; input_ch < input_channels; ++input_ch) { | |
217 // We can only remap if each row contains a single scale of 1. I.e., each | |
218 // output channel is mapped from a single unscaled input channel. | |
219 if (matrix_[output_ch][input_ch] != 1 || ++input_mappings > 1) | |
220 return; | |
221 } | |
222 } | |
223 | |
224 // If we've gotten here, |matrix_| is simply a remapping. | |
225 remapping_ = true; | |
226 } | |
227 | |
228 ChannelMixer::~ChannelMixer() {} | |
229 | |
230 void ChannelMixer::Transform(const AudioBus* input, AudioBus* output) { | |
231 CHECK_EQ(matrix_.size(), static_cast<size_t>(output->channels())); | |
232 CHECK_EQ(matrix_[0].size(), static_cast<size_t>(input->channels())); | |
233 CHECK_EQ(input->frames(), output->frames()); | |
234 | |
235 // Zero initialize |output| so we're accumulating from zero. | |
236 output->Zero(); | |
237 | |
238 // If we're just remapping we can simply copy the correct input to output. | |
239 if (remapping_) { | |
240 for (int output_ch = 0; output_ch < output->channels(); ++output_ch) { | |
241 for (int input_ch = 0; input_ch < input->channels(); ++input_ch) { | |
242 float scale = matrix_[output_ch][input_ch]; | |
243 if (scale > 0) { | |
244 DCHECK_EQ(scale, 1.0f); | |
245 memcpy(output->channel(output_ch), input->channel(input_ch), | |
246 sizeof(*output->channel(output_ch)) * output->frames()); | |
247 break; | |
248 } | |
249 } | |
250 } | |
251 return; | |
252 } | |
253 | |
254 for (int output_ch = 0; output_ch < output->channels(); ++output_ch) { | |
255 for (int input_ch = 0; input_ch < input->channels(); ++input_ch) { | |
256 float scale = matrix_[output_ch][input_ch]; | |
257 // Scale should always be positive. Don't bother scaling by zero. | |
258 DCHECK_GE(scale, 0); | |
259 if (scale > 0) { | |
260 vector_math::FMAC(input->channel(input_ch), scale, output->frames(), | |
261 output->channel(output_ch)); | |
262 } | |
263 } | |
264 } | |
265 } | |
266 | |
267 void ChannelMixer::AccountFor(Channels ch) { | |
268 unaccounted_inputs_.erase(std::find( | |
269 unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch)); | |
270 } | |
271 | |
272 bool ChannelMixer::IsUnaccounted(Channels ch) { | |
273 return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(), | |
274 ch) != unaccounted_inputs_.end(); | |
275 } | |
276 | |
277 bool ChannelMixer::HasInputChannel(Channels ch) { | |
278 return ChannelOrder(input_layout_, ch) >= 0; | |
279 } | |
280 | |
281 bool ChannelMixer::HasOutputChannel(Channels ch) { | |
282 return ChannelOrder(output_layout_, ch) >= 0; | |
283 } | |
284 | |
285 void ChannelMixer::Mix(Channels input_ch, Channels output_ch, float scale) { | |
286 MixWithoutAccounting(input_ch, output_ch, scale); | |
287 AccountFor(input_ch); | |
288 } | |
289 | |
290 void ChannelMixer::MixWithoutAccounting(Channels input_ch, Channels output_ch, | |
291 float scale) { | |
292 int input_ch_index = ChannelOrder(input_layout_, input_ch); | |
293 int output_ch_index = ChannelOrder(output_layout_, output_ch); | |
294 | |
295 DCHECK(IsUnaccounted(input_ch)); | |
296 DCHECK_GE(input_ch_index, 0); | |
297 DCHECK_GE(output_ch_index, 0); | |
298 | |
299 matrix_[output_ch_index][input_ch_index] += scale; | |
Chris Rogers
2012/10/19 20:48:17
I'm confused why we're accumulating |scale| here.
DaleCurtis
2012/10/19 22:33:20
Ah, yes, you're correct. This is a carryover from
| |
300 } | |
301 | |
302 } // namespace media | |
OLD | NEW |