Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: media/base/channel_mixer.cc

Issue 11150034: Add support for channel transforms. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add TODO. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/base/channel_mixer.h ('k') | media/base/channel_mixer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 kEqualPowerScale = static_cast<float>(M_SQRT1_2);
22
23 static int ValidateLayout(ChannelLayout layout) {
24 CHECK_NE(layout, CHANNEL_LAYOUT_NONE);
25 CHECK_NE(layout, CHANNEL_LAYOUT_MAX);
26
27 // TODO(dalecurtis, crogers): We will eventually handle unsupported layouts by
28 // simply copying the input channels to the output channels, similar to if the
29 // user requests identical input and output layouts today.
30 CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED);
31
32 // Verify there's at least one channel. Should always be true here by virtue
33 // of not being one of the invalid layouts, but lets double check to be sure.
34 int channel_count = ChannelLayoutToChannelCount(layout);
35 DCHECK_GT(channel_count, 0);
36
37 // If we have more than one channel, verify a symmetric layout for sanity.
38 // The unit test will verify all possible layouts, so this can be a DCHECK.
39 // Symmetry allows simplifying the matrix building code by allowing us to
40 // assume that if one channel of a pair exists, the other will too.
41 if (channel_count > 1) {
42 DCHECK((ChannelOrder(layout, LEFT) >= 0 &&
43 ChannelOrder(layout, RIGHT) >= 0) ||
44 (ChannelOrder(layout, SIDE_LEFT) >= 0 &&
45 ChannelOrder(layout, SIDE_RIGHT) >= 0) ||
46 (ChannelOrder(layout, BACK_LEFT) >= 0 &&
47 ChannelOrder(layout, BACK_RIGHT) >= 0) ||
48 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 &&
49 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0))
50 << "Non-symmetric channel layout encountered.";
51 } else {
52 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO);
53 }
54
55 return channel_count;
56 }
57
58 ChannelMixer::ChannelMixer(ChannelLayout input, ChannelLayout output)
59 : input_layout_(input),
60 output_layout_(output),
61 remapping_(false) {
62 // Stereo down mix should never be the output layout.
63 CHECK_NE(output_layout_, CHANNEL_LAYOUT_STEREO_DOWNMIX);
64
65 int input_channels = ValidateLayout(input_layout_);
66 int output_channels = ValidateLayout(output_layout_);
67
68 // Size out the initial matrix.
69 matrix_.reserve(output_channels);
70 for (int output_ch = 0; output_ch < output_channels; ++output_ch)
71 matrix_.push_back(std::vector<float>(input_channels, 0));
72
73 // Route matching channels and figure out which ones aren't accounted for.
74 for (Channels ch = LEFT; ch < CHANNELS_MAX;
75 ch = static_cast<Channels>(ch + 1)) {
76 int input_ch_index = ChannelOrder(input_layout_, ch);
77 int output_ch_index = ChannelOrder(output_layout_, ch);
78
79 if (input_ch_index < 0)
80 continue;
81
82 if (output_ch_index < 0) {
83 unaccounted_inputs_.push_back(ch);
84 continue;
85 }
86
87 DCHECK_LT(static_cast<size_t>(output_ch_index), matrix_.size());
88 DCHECK_LT(static_cast<size_t>(input_ch_index),
89 matrix_[output_ch_index].size());
90 matrix_[output_ch_index][input_ch_index] = 1;
91 }
92
93 // If all input channels are accounted for, there's nothing left to do.
94 if (unaccounted_inputs_.empty()) {
95 // Since all output channels map directly to inputs we can optimize.
96 remapping_ = true;
97 return;
98 }
99
100 // Mix front LR into center.
101 if (IsUnaccounted(LEFT)) {
102 // When down mixing to mono from stereo, we need to be careful of full scale
103 // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping
104 // so we use 1 / 2 instead.
105 float scale = (output == CHANNEL_LAYOUT_MONO && input_channels == 2) ?
106 0.5 : kEqualPowerScale;
107 Mix(LEFT, CENTER, scale);
108 Mix(RIGHT, CENTER, scale);
109 }
110
111 // Mix center into front LR.
112 if (IsUnaccounted(CENTER)) {
113 // When up mixing from mono, just do a copy to front LR.
114 float scale = (input == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale;
115 MixWithoutAccounting(CENTER, LEFT, scale);
116 Mix(CENTER, RIGHT, scale);
117 }
118
119 // Mix back LR into: side LR || back center || front LR || front center.
120 if (IsUnaccounted(BACK_LEFT)) {
121 if (HasOutputChannel(SIDE_LEFT)) {
122 // If we have side LR, mix back LR into side LR, but instead if the input
123 // doesn't have side LR (but output does) copy back LR to side LR.
124 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1;
125 Mix(BACK_LEFT, SIDE_LEFT, scale);
126 Mix(BACK_RIGHT, SIDE_RIGHT, scale);
127 } else if (HasOutputChannel(BACK_CENTER)) {
128 // Mix back LR into back center.
129 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale);
130 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale);
131 } else if (output > CHANNEL_LAYOUT_MONO) {
132 // Mix back LR into front LR.
133 Mix(BACK_LEFT, LEFT, kEqualPowerScale);
134 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale);
135 } else {
136 // Mix back LR into front center.
137 Mix(BACK_LEFT, CENTER, kEqualPowerScale);
138 Mix(BACK_RIGHT, CENTER, kEqualPowerScale);
139 }
140 }
141
142 // Mix side LR into: back LR || back center || front LR || front center.
143 if (IsUnaccounted(SIDE_LEFT)) {
144 if (HasOutputChannel(BACK_LEFT)) {
145 // If we have back LR, mix side LR into back LR, but instead if the input
146 // doesn't have back LR (but output does) copy side LR to back LR.
147 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1;
148 Mix(SIDE_LEFT, BACK_LEFT, scale);
149 Mix(SIDE_RIGHT, BACK_RIGHT, scale);
150 } else if (HasOutputChannel(BACK_CENTER)) {
151 // Mix side LR into back center.
152 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale);
153 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale);
154 } else if (output > CHANNEL_LAYOUT_MONO) {
155 // Mix side LR into front LR.
156 Mix(SIDE_LEFT, LEFT, kEqualPowerScale);
157 Mix(SIDE_RIGHT, RIGHT, kEqualPowerScale);
158 } else {
159 // Mix side LR into front center.
160 Mix(SIDE_LEFT, CENTER, kEqualPowerScale);
161 Mix(SIDE_RIGHT, CENTER, kEqualPowerScale);
162 }
163 }
164
165 // Mix back center into: back LR || side LR || front LR || front center.
166 if (IsUnaccounted(BACK_CENTER)) {
167 if (HasOutputChannel(BACK_LEFT)) {
168 // Mix back center into back LR.
169 MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kEqualPowerScale);
170 Mix(BACK_CENTER, BACK_RIGHT, kEqualPowerScale);
171 } else if (HasOutputChannel(SIDE_LEFT)) {
172 // Mix back center into side LR.
173 MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kEqualPowerScale);
174 Mix(BACK_CENTER, SIDE_RIGHT, kEqualPowerScale);
175 } else if (output > CHANNEL_LAYOUT_MONO) {
176 // Mix back center into front LR.
177 // TODO(dalecurtis): Not sure about these values?
178 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale);
179 Mix(BACK_CENTER, RIGHT, kEqualPowerScale);
180 } else {
181 // Mix back center into front center.
182 // TODO(dalecurtis): Not sure about these values?
183 Mix(BACK_CENTER, CENTER, kEqualPowerScale);
184 }
185 }
186
187 // Mix LR of center into: front center || front LR.
188 if (IsUnaccounted(LEFT_OF_CENTER)) {
189 if (HasOutputChannel(CENTER)) {
190 // Mix LR of center into front center.
191 Mix(LEFT_OF_CENTER, CENTER, kEqualPowerScale);
192 Mix(RIGHT_OF_CENTER, CENTER, kEqualPowerScale);
193 } else {
194 // Mix LR of center into front LR.
195 Mix(LEFT_OF_CENTER, LEFT, kEqualPowerScale);
196 Mix(RIGHT_OF_CENTER, RIGHT, kEqualPowerScale);
197 }
198 }
199
200 // Mix LFE into: front LR || front center.
201 if (IsUnaccounted(LFE)) {
202 if (!HasOutputChannel(CENTER)) {
203 // Mix LFE into front LR.
204 MixWithoutAccounting(LFE, LEFT, kEqualPowerScale);
205 Mix(LFE, RIGHT, kEqualPowerScale);
206 } else {
207 // Mix LFE into front center.
208 Mix(LFE, CENTER, kEqualPowerScale);
209 }
210 }
211
212 // All channels should now be accounted for.
213 DCHECK(unaccounted_inputs_.empty());
214
215 // See if the output |matrix_| is simply a remapping matrix. If each input
216 // channel maps to a single output channel we can simply remap. Doing this
217 // programmatically is less fragile than logic checks on channel mappings.
218 for (int output_ch = 0; output_ch < output_channels; ++output_ch) {
219 int input_mappings = 0;
220 for (int input_ch = 0; input_ch < input_channels; ++input_ch) {
221 // We can only remap if each row contains a single scale of 1. I.e., each
222 // output channel is mapped from a single unscaled input channel.
223 if (matrix_[output_ch][input_ch] != 1 || ++input_mappings > 1)
224 return;
225 }
226 }
227
228 // If we've gotten here, |matrix_| is simply a remapping.
229 remapping_ = true;
230 }
231
232 ChannelMixer::~ChannelMixer() {}
233
234 void ChannelMixer::Transform(const AudioBus* input, AudioBus* output) {
235 CHECK_EQ(matrix_.size(), static_cast<size_t>(output->channels()));
236 CHECK_EQ(matrix_[0].size(), static_cast<size_t>(input->channels()));
237 CHECK_EQ(input->frames(), output->frames());
238
239 // Zero initialize |output| so we're accumulating from zero.
240 output->Zero();
241
242 // If we're just remapping we can simply copy the correct input to output.
243 if (remapping_) {
244 for (int output_ch = 0; output_ch < output->channels(); ++output_ch) {
245 for (int input_ch = 0; input_ch < input->channels(); ++input_ch) {
246 float scale = matrix_[output_ch][input_ch];
247 if (scale > 0) {
248 DCHECK_EQ(scale, 1.0f);
249 memcpy(output->channel(output_ch), input->channel(input_ch),
250 sizeof(*output->channel(output_ch)) * output->frames());
251 break;
252 }
253 }
254 }
255 return;
256 }
257
258 for (int output_ch = 0; output_ch < output->channels(); ++output_ch) {
259 for (int input_ch = 0; input_ch < input->channels(); ++input_ch) {
260 float scale = matrix_[output_ch][input_ch];
261 // Scale should always be positive. Don't bother scaling by zero.
262 DCHECK_GE(scale, 0);
263 if (scale > 0) {
264 vector_math::FMAC(input->channel(input_ch), scale, output->frames(),
265 output->channel(output_ch));
266 }
267 }
268 }
269 }
270
271 void ChannelMixer::AccountFor(Channels ch) {
272 unaccounted_inputs_.erase(std::find(
273 unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch));
274 }
275
276 bool ChannelMixer::IsUnaccounted(Channels ch) {
277 return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(),
278 ch) != unaccounted_inputs_.end();
279 }
280
281 bool ChannelMixer::HasInputChannel(Channels ch) {
282 return ChannelOrder(input_layout_, ch) >= 0;
283 }
284
285 bool ChannelMixer::HasOutputChannel(Channels ch) {
286 return ChannelOrder(output_layout_, ch) >= 0;
287 }
288
289 void ChannelMixer::Mix(Channels input_ch, Channels output_ch, float scale) {
290 MixWithoutAccounting(input_ch, output_ch, scale);
291 AccountFor(input_ch);
292 }
293
294 void ChannelMixer::MixWithoutAccounting(Channels input_ch, Channels output_ch,
295 float scale) {
296 int input_ch_index = ChannelOrder(input_layout_, input_ch);
297 int output_ch_index = ChannelOrder(output_layout_, output_ch);
298
299 DCHECK(IsUnaccounted(input_ch));
300 DCHECK_GE(input_ch_index, 0);
301 DCHECK_GE(output_ch_index, 0);
302
303 DCHECK_EQ(matrix_[output_ch_index][input_ch_index], 0);
304 matrix_[output_ch_index][input_ch_index] = scale;
305 }
306
307 } // namespace media
OLDNEW
« no previous file with comments | « media/base/channel_mixer.h ('k') | media/base/channel_mixer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698