OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2. | 5 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2. |
6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
7 | 7 |
8 #include "media/base/channel_mixer.h" | 8 #include "media/base/channel_mixer.h" |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 20 matching lines...) Expand all Loading... | |
31 // Verify there's at least one channel. Should always be true here by virtue | 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. | 32 // of not being one of the invalid layouts, but lets double check to be sure. |
33 int channel_count = ChannelLayoutToChannelCount(layout); | 33 int channel_count = ChannelLayoutToChannelCount(layout); |
34 DCHECK_GT(channel_count, 0); | 34 DCHECK_GT(channel_count, 0); |
35 | 35 |
36 // If we have more than one channel, verify a symmetric layout for sanity. | 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. | 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 | 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. | 39 // assume that if one channel of a pair exists, the other will too. |
40 if (channel_count > 1) { | 40 if (channel_count > 1) { |
41 DCHECK((ChannelOrder(layout, LEFT) >= 0 && | 41 DCHECK((ChannelOrder(layout, LEFT) >= 0) == |
42 ChannelOrder(layout, RIGHT) >= 0) || | 42 (ChannelOrder(layout, RIGHT) >= 0)) |
43 (ChannelOrder(layout, SIDE_LEFT) >= 0 && | 43 << "Non-symmetric channel layout encountered."; |
44 ChannelOrder(layout, SIDE_RIGHT) >= 0) || | 44 DCHECK((ChannelOrder(layout, SIDE_LEFT) >= 0) == |
45 (ChannelOrder(layout, BACK_LEFT) >= 0 && | 45 (ChannelOrder(layout, SIDE_RIGHT) >= 0)) |
46 ChannelOrder(layout, BACK_RIGHT) >= 0) || | 46 << "Non-symmetric channel layout encountered."; |
47 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 && | 47 DCHECK((ChannelOrder(layout, BACK_LEFT) >= 0) == |
48 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0)) | 48 (ChannelOrder(layout, BACK_RIGHT) >= 0)) |
49 << "Non-symmetric channel layout encountered."; | |
50 DCHECK((ChannelOrder(layout, LEFT_OF_CENTER) >= 0) == | |
51 (ChannelOrder(layout, RIGHT_OF_CENTER) >= 0)) | |
wtc
2014/10/21 20:42:37
Please check this change carefully. I don't unders
DaleCurtis
2014/10/21 20:52:03
The original DCHECK just checks to see if we have
wtc
2014/10/21 21:16:58
Here is an example that the original check conside
DaleCurtis
2014/10/21 21:22:59
Ah, whoops, yeah I see what you mean. You're appro
wtc
2014/10/21 21:48:50
Done.
| |
49 << "Non-symmetric channel layout encountered."; | 52 << "Non-symmetric channel layout encountered."; |
50 } else { | 53 } else { |
51 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); | 54 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); |
52 } | 55 } |
53 | |
54 return; | |
55 } | 56 } |
56 | 57 |
57 class MatrixBuilder { | 58 class MatrixBuilder { |
58 public: | 59 public: |
59 MatrixBuilder(ChannelLayout input_layout, int input_channels, | 60 MatrixBuilder(ChannelLayout input_layout, int input_channels, |
60 ChannelLayout output_layout, int output_channels) | 61 ChannelLayout output_layout, int output_channels) |
61 : input_layout_(input_layout), | 62 : input_layout_(input_layout), |
62 input_channels_(input_channels), | 63 input_channels_(input_channels), |
63 output_layout_(output_layout), | 64 output_layout_(output_layout), |
64 output_channels_(output_channels) { | 65 output_channels_(output_channels) { |
(...skipping 24 matching lines...) Expand all Loading... | |
89 int input_channels_; | 90 int input_channels_; |
90 ChannelLayout output_layout_; | 91 ChannelLayout output_layout_; |
91 int output_channels_; | 92 int output_channels_; |
92 | 93 |
93 // Helper variable for tracking which inputs are currently unaccounted, | 94 // Helper variable for tracking which inputs are currently unaccounted, |
94 // should be empty after construction completes. | 95 // should be empty after construction completes. |
95 std::vector<Channels> unaccounted_inputs_; | 96 std::vector<Channels> unaccounted_inputs_; |
96 | 97 |
97 // Helper methods for managing unaccounted input channels. | 98 // Helper methods for managing unaccounted input channels. |
98 void AccountFor(Channels ch); | 99 void AccountFor(Channels ch); |
99 bool IsUnaccounted(Channels ch); | 100 bool IsUnaccounted(Channels ch) const; |
100 | 101 |
101 // Helper methods for checking if |ch| exists in either |input_layout_| or | 102 // Helper methods for checking if |ch| exists in either |input_layout_| or |
102 // |output_layout_| respectively. | 103 // |output_layout_| respectively. |
103 bool HasInputChannel(Channels ch); | 104 bool HasInputChannel(Channels ch) const; |
104 bool HasOutputChannel(Channels ch); | 105 bool HasOutputChannel(Channels ch) const; |
105 | 106 |
106 // Helper methods for updating |matrix_| with the proper value for | 107 // Helper methods for updating |matrix_| with the proper value for |
107 // mixing |input_ch| into |output_ch|. MixWithoutAccounting() does not | 108 // mixing |input_ch| into |output_ch|. MixWithoutAccounting() does not |
108 // remove the channel from |unaccounted_inputs_|. | 109 // remove the channel from |unaccounted_inputs_|. |
109 void Mix(Channels input_ch, Channels output_ch, float scale); | 110 void Mix(Channels input_ch, Channels output_ch, float scale); |
110 void MixWithoutAccounting(Channels input_ch, Channels output_ch, | 111 void MixWithoutAccounting(Channels input_ch, Channels output_ch, float scale); |
111 float scale); | |
112 | 112 |
113 DISALLOW_COPY_AND_ASSIGN(MatrixBuilder); | 113 DISALLOW_COPY_AND_ASSIGN(MatrixBuilder); |
114 }; | 114 }; |
115 | 115 |
116 ChannelMixer::ChannelMixer(ChannelLayout input_layout, | 116 ChannelMixer::ChannelMixer(ChannelLayout input_layout, |
117 ChannelLayout output_layout) { | 117 ChannelLayout output_layout) { |
118 Initialize(input_layout, | 118 Initialize(input_layout, |
119 ChannelLayoutToChannelCount(input_layout), | 119 ChannelLayoutToChannelCount(input_layout), |
120 output_layout, | 120 output_layout, |
121 ChannelLayoutToChannelCount(output_layout)); | 121 ChannelLayoutToChannelCount(output_layout)); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 // When up mixing from mono, just do a copy to front LR. | 212 // When up mixing from mono, just do a copy to front LR. |
213 float scale = | 213 float scale = |
214 (input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale; | 214 (input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale; |
215 MixWithoutAccounting(CENTER, LEFT, scale); | 215 MixWithoutAccounting(CENTER, LEFT, scale); |
216 Mix(CENTER, RIGHT, scale); | 216 Mix(CENTER, RIGHT, scale); |
217 } | 217 } |
218 | 218 |
219 // Mix back LR into: side LR || back center || front LR || front center. | 219 // Mix back LR into: side LR || back center || front LR || front center. |
220 if (IsUnaccounted(BACK_LEFT)) { | 220 if (IsUnaccounted(BACK_LEFT)) { |
221 if (HasOutputChannel(SIDE_LEFT)) { | 221 if (HasOutputChannel(SIDE_LEFT)) { |
222 // If we have side LR, mix back LR into side LR, but instead if the input | 222 // If the input has side LR, mix back LR into side LR, but instead if the |
223 // doesn't have side LR (but output does) copy back LR to side LR. | 223 // input doesn't have side LR (but output does) copy back LR to side LR. |
224 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1; | 224 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1; |
225 Mix(BACK_LEFT, SIDE_LEFT, scale); | 225 Mix(BACK_LEFT, SIDE_LEFT, scale); |
226 Mix(BACK_RIGHT, SIDE_RIGHT, scale); | 226 Mix(BACK_RIGHT, SIDE_RIGHT, scale); |
227 } else if (HasOutputChannel(BACK_CENTER)) { | 227 } else if (HasOutputChannel(BACK_CENTER)) { |
228 // Mix back LR into back center. | 228 // Mix back LR into back center. |
229 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale); | 229 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale); |
230 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale); | 230 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale); |
231 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { | 231 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { |
232 // Mix back LR into front LR. | 232 // Mix back LR into front LR. |
233 Mix(BACK_LEFT, LEFT, kEqualPowerScale); | 233 Mix(BACK_LEFT, LEFT, kEqualPowerScale); |
234 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale); | 234 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale); |
235 } else { | 235 } else { |
236 // Mix back LR into front center. | 236 // Mix back LR into front center. |
237 Mix(BACK_LEFT, CENTER, kEqualPowerScale); | 237 Mix(BACK_LEFT, CENTER, kEqualPowerScale); |
238 Mix(BACK_RIGHT, CENTER, kEqualPowerScale); | 238 Mix(BACK_RIGHT, CENTER, kEqualPowerScale); |
239 } | 239 } |
240 } | 240 } |
241 | 241 |
242 // Mix side LR into: back LR || back center || front LR || front center. | 242 // Mix side LR into: back LR || back center || front LR || front center. |
243 if (IsUnaccounted(SIDE_LEFT)) { | 243 if (IsUnaccounted(SIDE_LEFT)) { |
244 if (HasOutputChannel(BACK_LEFT)) { | 244 if (HasOutputChannel(BACK_LEFT)) { |
245 // If we have back LR, mix side LR into back LR, but instead if the input | 245 // If the input has back LR, mix side LR into back LR, but instead if the |
246 // doesn't have back LR (but output does) copy side LR to back LR. | 246 // input doesn't have back LR (but output does) copy side LR to back LR. |
247 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1; | 247 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1; |
248 Mix(SIDE_LEFT, BACK_LEFT, scale); | 248 Mix(SIDE_LEFT, BACK_LEFT, scale); |
249 Mix(SIDE_RIGHT, BACK_RIGHT, scale); | 249 Mix(SIDE_RIGHT, BACK_RIGHT, scale); |
250 } else if (HasOutputChannel(BACK_CENTER)) { | 250 } else if (HasOutputChannel(BACK_CENTER)) { |
251 // Mix side LR into back center. | 251 // Mix side LR into back center. |
252 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale); | 252 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale); |
253 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale); | 253 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale); |
254 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { | 254 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { |
255 // Mix side LR into front LR. | 255 // Mix side LR into front LR. |
256 Mix(SIDE_LEFT, LEFT, kEqualPowerScale); | 256 Mix(SIDE_LEFT, LEFT, kEqualPowerScale); |
(...skipping 20 matching lines...) Expand all Loading... | |
277 // TODO(dalecurtis): Not sure about these values? | 277 // TODO(dalecurtis): Not sure about these values? |
278 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale); | 278 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale); |
279 Mix(BACK_CENTER, RIGHT, kEqualPowerScale); | 279 Mix(BACK_CENTER, RIGHT, kEqualPowerScale); |
280 } else { | 280 } else { |
281 // Mix back center into front center. | 281 // Mix back center into front center. |
282 // TODO(dalecurtis): Not sure about these values? | 282 // TODO(dalecurtis): Not sure about these values? |
283 Mix(BACK_CENTER, CENTER, kEqualPowerScale); | 283 Mix(BACK_CENTER, CENTER, kEqualPowerScale); |
284 } | 284 } |
285 } | 285 } |
286 | 286 |
287 // Mix LR of center into: front center || front LR. | 287 // Mix LR of center into: front LR || front center. |
288 if (IsUnaccounted(LEFT_OF_CENTER)) { | 288 if (IsUnaccounted(LEFT_OF_CENTER)) { |
289 if (HasOutputChannel(LEFT)) { | 289 if (HasOutputChannel(LEFT)) { |
290 // Mix LR of center into front LR. | 290 // Mix LR of center into front LR. |
291 // TODO(wtc): Not sure about these values? | |
wtc
2014/10/21 20:42:37
I'm actually not sure about several other scale fa
DaleCurtis
2014/10/21 20:52:03
These values are chosen based on discussions with
wtc
2014/10/21 21:16:58
I will remove this TODO comment. I elaborate on th
| |
291 Mix(LEFT_OF_CENTER, LEFT, kEqualPowerScale); | 292 Mix(LEFT_OF_CENTER, LEFT, kEqualPowerScale); |
292 Mix(RIGHT_OF_CENTER, RIGHT, kEqualPowerScale); | 293 Mix(RIGHT_OF_CENTER, RIGHT, kEqualPowerScale); |
293 } else { | 294 } else { |
294 // Mix LR of center into front center. | 295 // Mix LR of center into front center. |
296 // TODO(wtc): How do we know center exists? | |
DaleCurtis
2014/10/21 20:52:03
CENTER == mono, which every layout has.
wtc
2014/10/21 21:16:58
Not sure what you meant here. In any case, I came
DaleCurtis
2014/10/21 21:22:59
Yes, that's what I was trying to say.
| |
295 Mix(LEFT_OF_CENTER, CENTER, kEqualPowerScale); | 297 Mix(LEFT_OF_CENTER, CENTER, kEqualPowerScale); |
296 Mix(RIGHT_OF_CENTER, CENTER, kEqualPowerScale); | 298 Mix(RIGHT_OF_CENTER, CENTER, kEqualPowerScale); |
297 } | 299 } |
298 } | 300 } |
299 | 301 |
300 // Mix LFE into: front LR || front center. | 302 // Mix LFE into: front center || front LR. |
301 if (IsUnaccounted(LFE)) { | 303 if (IsUnaccounted(LFE)) { |
302 if (!HasOutputChannel(CENTER)) { | 304 if (!HasOutputChannel(CENTER)) { |
303 // Mix LFE into front LR. | 305 // Mix LFE into front LR. |
304 MixWithoutAccounting(LFE, LEFT, kEqualPowerScale); | 306 MixWithoutAccounting(LFE, LEFT, kEqualPowerScale); |
305 Mix(LFE, RIGHT, kEqualPowerScale); | 307 Mix(LFE, RIGHT, kEqualPowerScale); |
306 } else { | 308 } else { |
307 // Mix LFE into front center. | 309 // Mix LFE into front center. |
310 // TODO(wtc): why isn't the scale 1? | |
DaleCurtis
2014/10/21 20:52:03
Because we may have just mixed a bunch of other ch
wtc
2014/10/21 21:16:58
Understood. I will remove this TODO comment.
This
DaleCurtis
2014/10/21 21:22:59
Yeah, certainly there are some odd edge cases here
| |
308 Mix(LFE, CENTER, kEqualPowerScale); | 311 Mix(LFE, CENTER, kEqualPowerScale); |
309 } | 312 } |
310 } | 313 } |
311 | 314 |
312 // All channels should now be accounted for. | 315 // All channels should now be accounted for. |
313 DCHECK(unaccounted_inputs_.empty()); | 316 DCHECK(unaccounted_inputs_.empty()); |
314 | 317 |
315 // See if the output |matrix_| is simply a remapping matrix. If each input | 318 // 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 | 319 // channel maps to a single output channel we can simply remap. Doing this |
317 // programmatically is less fragile than logic checks on channel mappings. | 320 // programmatically is less fragile than logic checks on channel mappings. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
366 } | 369 } |
367 } | 370 } |
368 } | 371 } |
369 } | 372 } |
370 | 373 |
371 void MatrixBuilder::AccountFor(Channels ch) { | 374 void MatrixBuilder::AccountFor(Channels ch) { |
372 unaccounted_inputs_.erase(std::find( | 375 unaccounted_inputs_.erase(std::find( |
373 unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch)); | 376 unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch)); |
374 } | 377 } |
375 | 378 |
376 bool MatrixBuilder::IsUnaccounted(Channels ch) { | 379 bool MatrixBuilder::IsUnaccounted(Channels ch) const { |
377 return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(), | 380 return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(), |
378 ch) != unaccounted_inputs_.end(); | 381 ch) != unaccounted_inputs_.end(); |
379 } | 382 } |
380 | 383 |
381 bool MatrixBuilder::HasInputChannel(Channels ch) { | 384 bool MatrixBuilder::HasInputChannel(Channels ch) const { |
382 return ChannelOrder(input_layout_, ch) >= 0; | 385 return ChannelOrder(input_layout_, ch) >= 0; |
383 } | 386 } |
384 | 387 |
385 bool MatrixBuilder::HasOutputChannel(Channels ch) { | 388 bool MatrixBuilder::HasOutputChannel(Channels ch) const { |
386 return ChannelOrder(output_layout_, ch) >= 0; | 389 return ChannelOrder(output_layout_, ch) >= 0; |
387 } | 390 } |
388 | 391 |
389 void MatrixBuilder::Mix(Channels input_ch, Channels output_ch, float scale) { | 392 void MatrixBuilder::Mix(Channels input_ch, Channels output_ch, float scale) { |
390 MixWithoutAccounting(input_ch, output_ch, scale); | 393 MixWithoutAccounting(input_ch, output_ch, scale); |
391 AccountFor(input_ch); | 394 AccountFor(input_ch); |
392 } | 395 } |
393 | 396 |
394 void MatrixBuilder::MixWithoutAccounting(Channels input_ch, Channels output_ch, | 397 void MatrixBuilder::MixWithoutAccounting(Channels input_ch, Channels output_ch, |
395 float scale) { | 398 float scale) { |
396 int input_ch_index = ChannelOrder(input_layout_, input_ch); | 399 int input_ch_index = ChannelOrder(input_layout_, input_ch); |
397 int output_ch_index = ChannelOrder(output_layout_, output_ch); | 400 int output_ch_index = ChannelOrder(output_layout_, output_ch); |
398 | 401 |
399 DCHECK(IsUnaccounted(input_ch)); | 402 DCHECK(IsUnaccounted(input_ch)); |
400 DCHECK_GE(input_ch_index, 0); | 403 DCHECK_GE(input_ch_index, 0); |
401 DCHECK_GE(output_ch_index, 0); | 404 DCHECK_GE(output_ch_index, 0); |
402 | 405 |
403 DCHECK_EQ((*matrix_)[output_ch_index][input_ch_index], 0); | 406 DCHECK_EQ((*matrix_)[output_ch_index][input_ch_index], 0); |
404 (*matrix_)[output_ch_index][input_ch_index] = scale; | 407 (*matrix_)[output_ch_index][input_ch_index] = scale; |
405 } | 408 } |
406 | 409 |
407 } // namespace media | 410 } // namespace media |
OLD | NEW |