| 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> |
| 11 #include <cmath> | 11 #include <cmath> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "media/audio/audio_parameters.h" | |
| 15 #include "media/base/audio_bus.h" | 14 #include "media/base/audio_bus.h" |
| 16 #include "media/base/vector_math.h" | 15 #include "media/base/vector_math.h" |
| 17 | 16 |
| 18 namespace media { | 17 namespace media { |
| 19 | 18 |
| 20 // Default scale factor for mixing two channels together. We use a different | 19 // Default scale factor for mixing two channels together. We use a different |
| 21 // value for stereo -> mono and mono -> stereo mixes. | 20 // value for stereo -> mono and mono -> stereo mixes. |
| 22 static const float kEqualPowerScale = static_cast<float>(M_SQRT1_2); | 21 static const float kEqualPowerScale = static_cast<float>(M_SQRT1_2); |
| 23 | 22 |
| 24 static void ValidateLayout(ChannelLayout layout) { | 23 static int ValidateLayout(ChannelLayout layout) { |
| 25 CHECK_NE(layout, CHANNEL_LAYOUT_NONE); | 24 CHECK_NE(layout, CHANNEL_LAYOUT_NONE); |
| 26 CHECK_NE(layout, CHANNEL_LAYOUT_MAX); | 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. |
| 27 CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED); | 30 CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED); |
| 28 CHECK_NE(layout, CHANNEL_LAYOUT_DISCRETE); | |
| 29 | 31 |
| 30 // Verify there's at least one channel. Should always be true here by virtue | 32 // Verify there's at least one channel. Should always be true here by virtue |
| 31 // of not being one of the invalid layouts, but lets double check to be sure. | 33 // of not being one of the invalid layouts, but lets double check to be sure. |
| 32 int channel_count = ChannelLayoutToChannelCount(layout); | 34 int channel_count = ChannelLayoutToChannelCount(layout); |
| 33 DCHECK_GT(channel_count, 0); | 35 DCHECK_GT(channel_count, 0); |
| 34 | 36 |
| 35 // If we have more than one channel, verify a symmetric layout for sanity. | 37 // If we have more than one channel, verify a symmetric layout for sanity. |
| 36 // The unit test will verify all possible layouts, so this can be a DCHECK. | 38 // The unit test will verify all possible layouts, so this can be a DCHECK. |
| 37 // Symmetry allows simplifying the matrix building code by allowing us to | 39 // Symmetry allows simplifying the matrix building code by allowing us to |
| 38 // assume that if one channel of a pair exists, the other will too. | 40 // assume that if one channel of a pair exists, the other will too. |
| 39 if (channel_count > 1) { | 41 if (channel_count > 1) { |
| 40 DCHECK((ChannelOrder(layout, LEFT) >= 0 && | 42 DCHECK((ChannelOrder(layout, LEFT) >= 0 && |
| 41 ChannelOrder(layout, RIGHT) >= 0) || | 43 ChannelOrder(layout, RIGHT) >= 0) || |
| 42 (ChannelOrder(layout, SIDE_LEFT) >= 0 && | 44 (ChannelOrder(layout, SIDE_LEFT) >= 0 && |
| 43 ChannelOrder(layout, SIDE_RIGHT) >= 0) || | 45 ChannelOrder(layout, SIDE_RIGHT) >= 0) || |
| 44 (ChannelOrder(layout, BACK_LEFT) >= 0 && | 46 (ChannelOrder(layout, BACK_LEFT) >= 0 && |
| 45 ChannelOrder(layout, BACK_RIGHT) >= 0) || | 47 ChannelOrder(layout, BACK_RIGHT) >= 0) || |
| 46 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 && | 48 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 && |
| 47 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0)) | 49 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0)) |
| 48 << "Non-symmetric channel layout encountered."; | 50 << "Non-symmetric channel layout encountered."; |
| 49 } else { | 51 } else { |
| 50 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); | 52 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); |
| 51 } | 53 } |
| 52 | 54 |
| 53 return; | 55 return channel_count; |
| 54 } | 56 } |
| 55 | 57 |
| 56 ChannelMixer::ChannelMixer(ChannelLayout input_layout, | 58 ChannelMixer::ChannelMixer(ChannelLayout input, ChannelLayout output) |
| 57 ChannelLayout output_layout) { | 59 : input_layout_(input), |
| 58 Initialize(input_layout, | 60 output_layout_(output), |
| 59 ChannelLayoutToChannelCount(input_layout), | 61 remapping_(false) { |
| 60 output_layout, | |
| 61 ChannelLayoutToChannelCount(output_layout)); | |
| 62 } | |
| 63 | |
| 64 ChannelMixer::ChannelMixer( | |
| 65 const AudioParameters& input, const AudioParameters& output) { | |
| 66 Initialize(input.channel_layout(), | |
| 67 input.channels(), | |
| 68 output.channel_layout(), | |
| 69 output.channels()); | |
| 70 } | |
| 71 | |
| 72 void ChannelMixer::Initialize( | |
| 73 ChannelLayout input_layout, int input_channels, | |
| 74 ChannelLayout output_layout, int output_channels) { | |
| 75 input_layout_ = input_layout; | |
| 76 output_layout_ = output_layout; | |
| 77 remapping_ = false; | |
| 78 | |
| 79 // Stereo down mix should never be the output layout. | 62 // Stereo down mix should never be the output layout. |
| 80 CHECK_NE(output_layout_, CHANNEL_LAYOUT_STEREO_DOWNMIX); | 63 CHECK_NE(output_layout_, CHANNEL_LAYOUT_STEREO_DOWNMIX); |
| 81 | 64 |
| 82 if (input_layout_ != CHANNEL_LAYOUT_DISCRETE) | 65 int input_channels = ValidateLayout(input_layout_); |
| 83 ValidateLayout(input_layout_); | 66 int output_channels = ValidateLayout(output_layout_); |
| 84 if (output_layout_ != CHANNEL_LAYOUT_DISCRETE) | |
| 85 ValidateLayout(output_layout_); | |
| 86 | 67 |
| 87 // Size out the initial matrix. | 68 // Size out the initial matrix. |
| 88 matrix_.reserve(output_channels); | 69 matrix_.reserve(output_channels); |
| 89 for (int output_ch = 0; output_ch < output_channels; ++output_ch) | 70 for (int output_ch = 0; output_ch < output_channels; ++output_ch) |
| 90 matrix_.push_back(std::vector<float>(input_channels, 0)); | 71 matrix_.push_back(std::vector<float>(input_channels, 0)); |
| 91 | 72 |
| 92 // First check for discrete case. | |
| 93 if (input_layout_ == CHANNEL_LAYOUT_DISCRETE || | |
| 94 output_layout_ == CHANNEL_LAYOUT_DISCRETE) { | |
| 95 // If the number of input channels is more than output channels, then | |
| 96 // copy as many as we can then drop the remaining input channels. | |
| 97 // If the number of input channels is less than output channels, then | |
| 98 // copy them all, then zero out the remaining output channels. | |
| 99 int passthrough_channels = std::min(input_channels, output_channels); | |
| 100 for (int i = 0; i < passthrough_channels; ++i) | |
| 101 matrix_[i][i] = 1; | |
| 102 | |
| 103 remapping_ = true; | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 // Route matching channels and figure out which ones aren't accounted for. | 73 // Route matching channels and figure out which ones aren't accounted for. |
| 108 for (Channels ch = LEFT; ch < CHANNELS_MAX; | 74 for (Channels ch = LEFT; ch < CHANNELS_MAX; |
| 109 ch = static_cast<Channels>(ch + 1)) { | 75 ch = static_cast<Channels>(ch + 1)) { |
| 110 int input_ch_index = ChannelOrder(input_layout_, ch); | 76 int input_ch_index = ChannelOrder(input_layout_, ch); |
| 111 int output_ch_index = ChannelOrder(output_layout_, ch); | 77 int output_ch_index = ChannelOrder(output_layout_, ch); |
| 112 | 78 |
| 113 if (input_ch_index < 0) | 79 if (input_ch_index < 0) |
| 114 continue; | 80 continue; |
| 115 | 81 |
| 116 if (output_ch_index < 0) { | 82 if (output_ch_index < 0) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 129 // Since all output channels map directly to inputs we can optimize. | 95 // Since all output channels map directly to inputs we can optimize. |
| 130 remapping_ = true; | 96 remapping_ = true; |
| 131 return; | 97 return; |
| 132 } | 98 } |
| 133 | 99 |
| 134 // Mix front LR into center. | 100 // Mix front LR into center. |
| 135 if (IsUnaccounted(LEFT)) { | 101 if (IsUnaccounted(LEFT)) { |
| 136 // When down mixing to mono from stereo, we need to be careful of full scale | 102 // When down mixing to mono from stereo, we need to be careful of full scale |
| 137 // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping | 103 // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping |
| 138 // so we use 1 / 2 instead. | 104 // so we use 1 / 2 instead. |
| 139 float scale = | 105 float scale = (output == CHANNEL_LAYOUT_MONO && input_channels == 2) ? |
| 140 (output_layout_ == CHANNEL_LAYOUT_MONO && input_channels == 2) ? | |
| 141 0.5 : kEqualPowerScale; | 106 0.5 : kEqualPowerScale; |
| 142 Mix(LEFT, CENTER, scale); | 107 Mix(LEFT, CENTER, scale); |
| 143 Mix(RIGHT, CENTER, scale); | 108 Mix(RIGHT, CENTER, scale); |
| 144 } | 109 } |
| 145 | 110 |
| 146 // Mix center into front LR. | 111 // Mix center into front LR. |
| 147 if (IsUnaccounted(CENTER)) { | 112 if (IsUnaccounted(CENTER)) { |
| 148 // When up mixing from mono, just do a copy to front LR. | 113 // When up mixing from mono, just do a copy to front LR. |
| 149 float scale = | 114 float scale = (input == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale; |
| 150 (input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale; | |
| 151 MixWithoutAccounting(CENTER, LEFT, scale); | 115 MixWithoutAccounting(CENTER, LEFT, scale); |
| 152 Mix(CENTER, RIGHT, scale); | 116 Mix(CENTER, RIGHT, scale); |
| 153 } | 117 } |
| 154 | 118 |
| 155 // Mix back LR into: side LR || back center || front LR || front center. | 119 // Mix back LR into: side LR || back center || front LR || front center. |
| 156 if (IsUnaccounted(BACK_LEFT)) { | 120 if (IsUnaccounted(BACK_LEFT)) { |
| 157 if (HasOutputChannel(SIDE_LEFT)) { | 121 if (HasOutputChannel(SIDE_LEFT)) { |
| 158 // If we have side LR, mix back LR into side LR, but instead if the input | 122 // If we have side LR, mix back LR into side LR, but instead if the input |
| 159 // doesn't have side LR (but output does) copy back LR to side LR. | 123 // doesn't have side LR (but output does) copy back LR to side LR. |
| 160 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1; | 124 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1; |
| 161 Mix(BACK_LEFT, SIDE_LEFT, scale); | 125 Mix(BACK_LEFT, SIDE_LEFT, scale); |
| 162 Mix(BACK_RIGHT, SIDE_RIGHT, scale); | 126 Mix(BACK_RIGHT, SIDE_RIGHT, scale); |
| 163 } else if (HasOutputChannel(BACK_CENTER)) { | 127 } else if (HasOutputChannel(BACK_CENTER)) { |
| 164 // Mix back LR into back center. | 128 // Mix back LR into back center. |
| 165 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale); | 129 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale); |
| 166 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale); | 130 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale); |
| 167 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { | 131 } else if (output > CHANNEL_LAYOUT_MONO) { |
| 168 // Mix back LR into front LR. | 132 // Mix back LR into front LR. |
| 169 Mix(BACK_LEFT, LEFT, kEqualPowerScale); | 133 Mix(BACK_LEFT, LEFT, kEqualPowerScale); |
| 170 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale); | 134 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale); |
| 171 } else { | 135 } else { |
| 172 // Mix back LR into front center. | 136 // Mix back LR into front center. |
| 173 Mix(BACK_LEFT, CENTER, kEqualPowerScale); | 137 Mix(BACK_LEFT, CENTER, kEqualPowerScale); |
| 174 Mix(BACK_RIGHT, CENTER, kEqualPowerScale); | 138 Mix(BACK_RIGHT, CENTER, kEqualPowerScale); |
| 175 } | 139 } |
| 176 } | 140 } |
| 177 | 141 |
| 178 // Mix side LR into: back LR || back center || front LR || front center. | 142 // Mix side LR into: back LR || back center || front LR || front center. |
| 179 if (IsUnaccounted(SIDE_LEFT)) { | 143 if (IsUnaccounted(SIDE_LEFT)) { |
| 180 if (HasOutputChannel(BACK_LEFT)) { | 144 if (HasOutputChannel(BACK_LEFT)) { |
| 181 // If we have back LR, mix side LR into back LR, but instead if the input | 145 // If we have back LR, mix side LR into back LR, but instead if the input |
| 182 // doesn't have back LR (but output does) copy side LR to back LR. | 146 // doesn't have back LR (but output does) copy side LR to back LR. |
| 183 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1; | 147 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1; |
| 184 Mix(SIDE_LEFT, BACK_LEFT, scale); | 148 Mix(SIDE_LEFT, BACK_LEFT, scale); |
| 185 Mix(SIDE_RIGHT, BACK_RIGHT, scale); | 149 Mix(SIDE_RIGHT, BACK_RIGHT, scale); |
| 186 } else if (HasOutputChannel(BACK_CENTER)) { | 150 } else if (HasOutputChannel(BACK_CENTER)) { |
| 187 // Mix side LR into back center. | 151 // Mix side LR into back center. |
| 188 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale); | 152 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale); |
| 189 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale); | 153 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale); |
| 190 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { | 154 } else if (output > CHANNEL_LAYOUT_MONO) { |
| 191 // Mix side LR into front LR. | 155 // Mix side LR into front LR. |
| 192 Mix(SIDE_LEFT, LEFT, kEqualPowerScale); | 156 Mix(SIDE_LEFT, LEFT, kEqualPowerScale); |
| 193 Mix(SIDE_RIGHT, RIGHT, kEqualPowerScale); | 157 Mix(SIDE_RIGHT, RIGHT, kEqualPowerScale); |
| 194 } else { | 158 } else { |
| 195 // Mix side LR into front center. | 159 // Mix side LR into front center. |
| 196 Mix(SIDE_LEFT, CENTER, kEqualPowerScale); | 160 Mix(SIDE_LEFT, CENTER, kEqualPowerScale); |
| 197 Mix(SIDE_RIGHT, CENTER, kEqualPowerScale); | 161 Mix(SIDE_RIGHT, CENTER, kEqualPowerScale); |
| 198 } | 162 } |
| 199 } | 163 } |
| 200 | 164 |
| 201 // Mix back center into: back LR || side LR || front LR || front center. | 165 // Mix back center into: back LR || side LR || front LR || front center. |
| 202 if (IsUnaccounted(BACK_CENTER)) { | 166 if (IsUnaccounted(BACK_CENTER)) { |
| 203 if (HasOutputChannel(BACK_LEFT)) { | 167 if (HasOutputChannel(BACK_LEFT)) { |
| 204 // Mix back center into back LR. | 168 // Mix back center into back LR. |
| 205 MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kEqualPowerScale); | 169 MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kEqualPowerScale); |
| 206 Mix(BACK_CENTER, BACK_RIGHT, kEqualPowerScale); | 170 Mix(BACK_CENTER, BACK_RIGHT, kEqualPowerScale); |
| 207 } else if (HasOutputChannel(SIDE_LEFT)) { | 171 } else if (HasOutputChannel(SIDE_LEFT)) { |
| 208 // Mix back center into side LR. | 172 // Mix back center into side LR. |
| 209 MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kEqualPowerScale); | 173 MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kEqualPowerScale); |
| 210 Mix(BACK_CENTER, SIDE_RIGHT, kEqualPowerScale); | 174 Mix(BACK_CENTER, SIDE_RIGHT, kEqualPowerScale); |
| 211 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) { | 175 } else if (output > CHANNEL_LAYOUT_MONO) { |
| 212 // Mix back center into front LR. | 176 // Mix back center into front LR. |
| 213 // TODO(dalecurtis): Not sure about these values? | 177 // TODO(dalecurtis): Not sure about these values? |
| 214 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale); | 178 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale); |
| 215 Mix(BACK_CENTER, RIGHT, kEqualPowerScale); | 179 Mix(BACK_CENTER, RIGHT, kEqualPowerScale); |
| 216 } else { | 180 } else { |
| 217 // Mix back center into front center. | 181 // Mix back center into front center. |
| 218 // TODO(dalecurtis): Not sure about these values? | 182 // TODO(dalecurtis): Not sure about these values? |
| 219 Mix(BACK_CENTER, CENTER, kEqualPowerScale); | 183 Mix(BACK_CENTER, CENTER, kEqualPowerScale); |
| 220 } | 184 } |
| 221 } | 185 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 298 |
| 335 DCHECK(IsUnaccounted(input_ch)); | 299 DCHECK(IsUnaccounted(input_ch)); |
| 336 DCHECK_GE(input_ch_index, 0); | 300 DCHECK_GE(input_ch_index, 0); |
| 337 DCHECK_GE(output_ch_index, 0); | 301 DCHECK_GE(output_ch_index, 0); |
| 338 | 302 |
| 339 DCHECK_EQ(matrix_[output_ch_index][input_ch_index], 0); | 303 DCHECK_EQ(matrix_[output_ch_index][input_ch_index], 0); |
| 340 matrix_[output_ch_index][input_ch_index] = scale; | 304 matrix_[output_ch_index][input_ch_index] = scale; |
| 341 } | 305 } |
| 342 | 306 |
| 343 } // namespace media | 307 } // namespace media |
| OLD | NEW |