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