| 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 // Assert that LEFT exists if and only if RIGHT exists, and so on. |
| 42 ChannelOrder(layout, RIGHT) >= 0) || | 42 DCHECK_EQ(ChannelOrder(layout, LEFT) >= 0, |
| 43 (ChannelOrder(layout, SIDE_LEFT) >= 0 && | 43 ChannelOrder(layout, RIGHT) >= 0); |
| 44 ChannelOrder(layout, SIDE_RIGHT) >= 0) || | 44 DCHECK_EQ(ChannelOrder(layout, SIDE_LEFT) >= 0, |
| 45 (ChannelOrder(layout, BACK_LEFT) >= 0 && | 45 ChannelOrder(layout, SIDE_RIGHT) >= 0); |
| 46 ChannelOrder(layout, BACK_RIGHT) >= 0) || | 46 DCHECK_EQ(ChannelOrder(layout, BACK_LEFT) >= 0, |
| 47 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 && | 47 ChannelOrder(layout, BACK_RIGHT) >= 0); |
| 48 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0)) | 48 DCHECK_EQ(ChannelOrder(layout, LEFT_OF_CENTER) >= 0, |
| 49 << "Non-symmetric channel layout encountered."; | 49 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0); |
| 50 } else { | 50 } else { |
| 51 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); | 51 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); |
| 52 } | 52 } |
| 53 | |
| 54 return; | |
| 55 } | 53 } |
| 56 | 54 |
| 57 class MatrixBuilder { | 55 class MatrixBuilder { |
| 58 public: | 56 public: |
| 59 MatrixBuilder(ChannelLayout input_layout, int input_channels, | 57 MatrixBuilder(ChannelLayout input_layout, int input_channels, |
| 60 ChannelLayout output_layout, int output_channels) | 58 ChannelLayout output_layout, int output_channels) |
| 61 : input_layout_(input_layout), | 59 : input_layout_(input_layout), |
| 62 input_channels_(input_channels), | 60 input_channels_(input_channels), |
| 63 output_layout_(output_layout), | 61 output_layout_(output_layout), |
| 64 output_channels_(output_channels) { | 62 output_channels_(output_channels) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 89 int input_channels_; | 87 int input_channels_; |
| 90 ChannelLayout output_layout_; | 88 ChannelLayout output_layout_; |
| 91 int output_channels_; | 89 int output_channels_; |
| 92 | 90 |
| 93 // Helper variable for tracking which inputs are currently unaccounted, | 91 // Helper variable for tracking which inputs are currently unaccounted, |
| 94 // should be empty after construction completes. | 92 // should be empty after construction completes. |
| 95 std::vector<Channels> unaccounted_inputs_; | 93 std::vector<Channels> unaccounted_inputs_; |
| 96 | 94 |
| 97 // Helper methods for managing unaccounted input channels. | 95 // Helper methods for managing unaccounted input channels. |
| 98 void AccountFor(Channels ch); | 96 void AccountFor(Channels ch); |
| 99 bool IsUnaccounted(Channels ch); | 97 bool IsUnaccounted(Channels ch) const; |
| 100 | 98 |
| 101 // Helper methods for checking if |ch| exists in either |input_layout_| or | 99 // Helper methods for checking if |ch| exists in either |input_layout_| or |
| 102 // |output_layout_| respectively. | 100 // |output_layout_| respectively. |
| 103 bool HasInputChannel(Channels ch); | 101 bool HasInputChannel(Channels ch) const; |
| 104 bool HasOutputChannel(Channels ch); | 102 bool HasOutputChannel(Channels ch) const; |
| 105 | 103 |
| 106 // Helper methods for updating |matrix_| with the proper value for | 104 // Helper methods for updating |matrix_| with the proper value for |
| 107 // mixing |input_ch| into |output_ch|. MixWithoutAccounting() does not | 105 // mixing |input_ch| into |output_ch|. MixWithoutAccounting() does not |
| 108 // remove the channel from |unaccounted_inputs_|. | 106 // remove the channel from |unaccounted_inputs_|. |
| 109 void Mix(Channels input_ch, Channels output_ch, float scale); | 107 void Mix(Channels input_ch, Channels output_ch, float scale); |
| 110 void MixWithoutAccounting(Channels input_ch, Channels output_ch, | 108 void MixWithoutAccounting(Channels input_ch, Channels output_ch, float scale); |
| 111 float scale); | |
| 112 | 109 |
| 113 DISALLOW_COPY_AND_ASSIGN(MatrixBuilder); | 110 DISALLOW_COPY_AND_ASSIGN(MatrixBuilder); |
| 114 }; | 111 }; |
| 115 | 112 |
| 116 ChannelMixer::ChannelMixer(ChannelLayout input_layout, | 113 ChannelMixer::ChannelMixer(ChannelLayout input_layout, |
| 117 ChannelLayout output_layout) { | 114 ChannelLayout output_layout) { |
| 118 Initialize(input_layout, | 115 Initialize(input_layout, |
| 119 ChannelLayoutToChannelCount(input_layout), | 116 ChannelLayoutToChannelCount(input_layout), |
| 120 output_layout, | 117 output_layout, |
| 121 ChannelLayoutToChannelCount(output_layout)); | 118 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. | 209 // When up mixing from mono, just do a copy to front LR. |
| 213 float scale = | 210 float scale = |
| 214 (input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale; | 211 (input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale; |
| 215 MixWithoutAccounting(CENTER, LEFT, scale); | 212 MixWithoutAccounting(CENTER, LEFT, scale); |
| 216 Mix(CENTER, RIGHT, scale); | 213 Mix(CENTER, RIGHT, scale); |
| 217 } | 214 } |
| 218 | 215 |
| 219 // Mix back LR into: side LR || back center || front LR || front center. | 216 // Mix back LR into: side LR || back center || front LR || front center. |
| 220 if (IsUnaccounted(BACK_LEFT)) { | 217 if (IsUnaccounted(BACK_LEFT)) { |
| 221 if (HasOutputChannel(SIDE_LEFT)) { | 218 if (HasOutputChannel(SIDE_LEFT)) { |
| 222 // If we have side LR, mix back LR into side LR, but instead if the input | 219 // 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. | 220 // input doesn't have side LR (but output does) copy back LR to side LR. |
| 224 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1; | 221 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1; |
| 225 Mix(BACK_LEFT, SIDE_LEFT, scale); | 222 Mix(BACK_LEFT, SIDE_LEFT, scale); |
| 226 Mix(BACK_RIGHT, SIDE_RIGHT, scale); | 223 Mix(BACK_RIGHT, SIDE_RIGHT, scale); |
| 227 } else if (HasOutputChannel(BACK_CENTER)) { | 224 } else if (HasOutputChannel(BACK_CENTER)) { |
| 228 // Mix back LR into back center. | 225 // Mix back LR into back center. |
| 229 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale); | 226 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale); |
| 230 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale); | 227 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale); |
| 231 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { | 228 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { |
| 232 // Mix back LR into front LR. | 229 // Mix back LR into front LR. |
| 233 Mix(BACK_LEFT, LEFT, kEqualPowerScale); | 230 Mix(BACK_LEFT, LEFT, kEqualPowerScale); |
| 234 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale); | 231 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale); |
| 235 } else { | 232 } else { |
| 236 // Mix back LR into front center. | 233 // Mix back LR into front center. |
| 237 Mix(BACK_LEFT, CENTER, kEqualPowerScale); | 234 Mix(BACK_LEFT, CENTER, kEqualPowerScale); |
| 238 Mix(BACK_RIGHT, CENTER, kEqualPowerScale); | 235 Mix(BACK_RIGHT, CENTER, kEqualPowerScale); |
| 239 } | 236 } |
| 240 } | 237 } |
| 241 | 238 |
| 242 // Mix side LR into: back LR || back center || front LR || front center. | 239 // Mix side LR into: back LR || back center || front LR || front center. |
| 243 if (IsUnaccounted(SIDE_LEFT)) { | 240 if (IsUnaccounted(SIDE_LEFT)) { |
| 244 if (HasOutputChannel(BACK_LEFT)) { | 241 if (HasOutputChannel(BACK_LEFT)) { |
| 245 // If we have back LR, mix side LR into back LR, but instead if the input | 242 // 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. | 243 // input doesn't have back LR (but output does) copy side LR to back LR. |
| 247 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1; | 244 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1; |
| 248 Mix(SIDE_LEFT, BACK_LEFT, scale); | 245 Mix(SIDE_LEFT, BACK_LEFT, scale); |
| 249 Mix(SIDE_RIGHT, BACK_RIGHT, scale); | 246 Mix(SIDE_RIGHT, BACK_RIGHT, scale); |
| 250 } else if (HasOutputChannel(BACK_CENTER)) { | 247 } else if (HasOutputChannel(BACK_CENTER)) { |
| 251 // Mix side LR into back center. | 248 // Mix side LR into back center. |
| 252 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale); | 249 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale); |
| 253 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale); | 250 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale); |
| 254 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { | 251 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { |
| 255 // Mix side LR into front LR. | 252 // Mix side LR into front LR. |
| 256 Mix(SIDE_LEFT, LEFT, kEqualPowerScale); | 253 Mix(SIDE_LEFT, LEFT, kEqualPowerScale); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 277 // TODO(dalecurtis): Not sure about these values? | 274 // TODO(dalecurtis): Not sure about these values? |
| 278 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale); | 275 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale); |
| 279 Mix(BACK_CENTER, RIGHT, kEqualPowerScale); | 276 Mix(BACK_CENTER, RIGHT, kEqualPowerScale); |
| 280 } else { | 277 } else { |
| 281 // Mix back center into front center. | 278 // Mix back center into front center. |
| 282 // TODO(dalecurtis): Not sure about these values? | 279 // TODO(dalecurtis): Not sure about these values? |
| 283 Mix(BACK_CENTER, CENTER, kEqualPowerScale); | 280 Mix(BACK_CENTER, CENTER, kEqualPowerScale); |
| 284 } | 281 } |
| 285 } | 282 } |
| 286 | 283 |
| 287 // Mix LR of center into: front center || front LR. | 284 // Mix LR of center into: front LR || front center. |
| 288 if (IsUnaccounted(LEFT_OF_CENTER)) { | 285 if (IsUnaccounted(LEFT_OF_CENTER)) { |
| 289 if (HasOutputChannel(LEFT)) { | 286 if (HasOutputChannel(LEFT)) { |
| 290 // Mix LR of center into front LR. | 287 // Mix LR of center into front LR. |
| 291 Mix(LEFT_OF_CENTER, LEFT, kEqualPowerScale); | 288 Mix(LEFT_OF_CENTER, LEFT, kEqualPowerScale); |
| 292 Mix(RIGHT_OF_CENTER, RIGHT, kEqualPowerScale); | 289 Mix(RIGHT_OF_CENTER, RIGHT, kEqualPowerScale); |
| 293 } else { | 290 } else { |
| 294 // Mix LR of center into front center. | 291 // Mix LR of center into front center. |
| 295 Mix(LEFT_OF_CENTER, CENTER, kEqualPowerScale); | 292 Mix(LEFT_OF_CENTER, CENTER, kEqualPowerScale); |
| 296 Mix(RIGHT_OF_CENTER, CENTER, kEqualPowerScale); | 293 Mix(RIGHT_OF_CENTER, CENTER, kEqualPowerScale); |
| 297 } | 294 } |
| 298 } | 295 } |
| 299 | 296 |
| 300 // Mix LFE into: front LR || front center. | 297 // Mix LFE into: front center || front LR. |
| 301 if (IsUnaccounted(LFE)) { | 298 if (IsUnaccounted(LFE)) { |
| 302 if (!HasOutputChannel(CENTER)) { | 299 if (!HasOutputChannel(CENTER)) { |
| 303 // Mix LFE into front LR. | 300 // Mix LFE into front LR. |
| 304 MixWithoutAccounting(LFE, LEFT, kEqualPowerScale); | 301 MixWithoutAccounting(LFE, LEFT, kEqualPowerScale); |
| 305 Mix(LFE, RIGHT, kEqualPowerScale); | 302 Mix(LFE, RIGHT, kEqualPowerScale); |
| 306 } else { | 303 } else { |
| 307 // Mix LFE into front center. | 304 // Mix LFE into front center. |
| 308 Mix(LFE, CENTER, kEqualPowerScale); | 305 Mix(LFE, CENTER, kEqualPowerScale); |
| 309 } | 306 } |
| 310 } | 307 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 } | 363 } |
| 367 } | 364 } |
| 368 } | 365 } |
| 369 } | 366 } |
| 370 | 367 |
| 371 void MatrixBuilder::AccountFor(Channels ch) { | 368 void MatrixBuilder::AccountFor(Channels ch) { |
| 372 unaccounted_inputs_.erase(std::find( | 369 unaccounted_inputs_.erase(std::find( |
| 373 unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch)); | 370 unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch)); |
| 374 } | 371 } |
| 375 | 372 |
| 376 bool MatrixBuilder::IsUnaccounted(Channels ch) { | 373 bool MatrixBuilder::IsUnaccounted(Channels ch) const { |
| 377 return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(), | 374 return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(), |
| 378 ch) != unaccounted_inputs_.end(); | 375 ch) != unaccounted_inputs_.end(); |
| 379 } | 376 } |
| 380 | 377 |
| 381 bool MatrixBuilder::HasInputChannel(Channels ch) { | 378 bool MatrixBuilder::HasInputChannel(Channels ch) const { |
| 382 return ChannelOrder(input_layout_, ch) >= 0; | 379 return ChannelOrder(input_layout_, ch) >= 0; |
| 383 } | 380 } |
| 384 | 381 |
| 385 bool MatrixBuilder::HasOutputChannel(Channels ch) { | 382 bool MatrixBuilder::HasOutputChannel(Channels ch) const { |
| 386 return ChannelOrder(output_layout_, ch) >= 0; | 383 return ChannelOrder(output_layout_, ch) >= 0; |
| 387 } | 384 } |
| 388 | 385 |
| 389 void MatrixBuilder::Mix(Channels input_ch, Channels output_ch, float scale) { | 386 void MatrixBuilder::Mix(Channels input_ch, Channels output_ch, float scale) { |
| 390 MixWithoutAccounting(input_ch, output_ch, scale); | 387 MixWithoutAccounting(input_ch, output_ch, scale); |
| 391 AccountFor(input_ch); | 388 AccountFor(input_ch); |
| 392 } | 389 } |
| 393 | 390 |
| 394 void MatrixBuilder::MixWithoutAccounting(Channels input_ch, Channels output_ch, | 391 void MatrixBuilder::MixWithoutAccounting(Channels input_ch, Channels output_ch, |
| 395 float scale) { | 392 float scale) { |
| 396 int input_ch_index = ChannelOrder(input_layout_, input_ch); | 393 int input_ch_index = ChannelOrder(input_layout_, input_ch); |
| 397 int output_ch_index = ChannelOrder(output_layout_, output_ch); | 394 int output_ch_index = ChannelOrder(output_layout_, output_ch); |
| 398 | 395 |
| 399 DCHECK(IsUnaccounted(input_ch)); | 396 DCHECK(IsUnaccounted(input_ch)); |
| 400 DCHECK_GE(input_ch_index, 0); | 397 DCHECK_GE(input_ch_index, 0); |
| 401 DCHECK_GE(output_ch_index, 0); | 398 DCHECK_GE(output_ch_index, 0); |
| 402 | 399 |
| 403 DCHECK_EQ((*matrix_)[output_ch_index][input_ch_index], 0); | 400 DCHECK_EQ((*matrix_)[output_ch_index][input_ch_index], 0); |
| 404 (*matrix_)[output_ch_index][input_ch_index] = scale; | 401 (*matrix_)[output_ch_index][input_ch_index] = scale; |
| 405 } | 402 } |
| 406 | 403 |
| 407 } // namespace media | 404 } // namespace media |
| OLD | NEW |