Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
|
scherkus (not reviewing)
2012/10/18 01:30:39
nit: move closer to where we need it (i.e., after
DaleCurtis
2012/10/18 01:56:44
Can't do that, since channel_mixer.h and headers i
scherkus (not reviewing)
2012/10/18 02:24:03
Gah.
| |
| 7 | |
| 8 #include "media/base/channel_mixer.h" | |
| 9 | |
| 10 #include <algorithm> | |
| 11 #include <cmath> | |
| 12 #include <limits> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "media/base/audio_bus.h" | |
| 16 #include "media/base/vector_math.h" | |
| 17 | |
| 18 namespace media { | |
| 19 | |
| 20 // Scale factor for mixing two channels together; could use 1 / 2 as well. | |
| 21 // TODO(dalecurtis): Figure out which value we should use here. Per crogers it | |
| 22 // may depend on whether we're down mixing from multichannel or upmixing. | |
| 23 // FFmpeg uses 1 / sqrt(2) so I've left it the same for now. | |
| 24 static const float kDefaultScale = static_cast<float>(M_SQRT1_2); | |
| 25 | |
| 26 static int ValidateLayout(ChannelLayout layout) { | |
| 27 CHECK_NE(layout, CHANNEL_LAYOUT_NONE); | |
| 28 CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED); | |
| 29 CHECK_NE(layout, CHANNEL_LAYOUT_MAX); | |
| 30 | |
| 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. | |
| 33 int channel_count = ChannelLayoutToChannelCount(layout); | |
| 34 DCHECK_GT(channel_count, 0); | |
| 35 | |
| 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. | |
| 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. | |
| 40 if (channel_count > 1) { | |
|
scherkus (not reviewing)
2012/10/18 01:30:39
is it worth asserting that channel_count == 1 iff
DaleCurtis
2012/10/18 05:22:04
Can't hurt, done.
| |
| 41 DCHECK((ChannelOrder(layout, LEFT) >= 0 && | |
|
Chris Rogers
2012/10/17 19:46:50
Where's the ChannelOrder() function defined?
DaleCurtis
2012/10/17 20:06:47
http://codereview.chromium.org/11198018/
Will lan
| |
| 42 ChannelOrder(layout, RIGHT) >= 0) || | |
| 43 (ChannelOrder(layout, SIDE_LEFT) >= 0 && | |
| 44 ChannelOrder(layout, SIDE_RIGHT) >= 0) || | |
| 45 (ChannelOrder(layout, BACK_LEFT) >= 0 && | |
| 46 ChannelOrder(layout, BACK_RIGHT) >= 0) || | |
| 47 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 && | |
| 48 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0) || | |
| 49 (ChannelOrder(layout, STEREO_LEFT) >= 0 && | |
| 50 ChannelOrder(layout, STEREO_RIGHT) >= 0)) | |
| 51 << "Non-symmetric channel layout encountered."; | |
| 52 } | |
| 53 | |
| 54 return channel_count; | |
| 55 } | |
| 56 | |
| 57 ChannelMixer::ChannelMixer(ChannelLayout input, ChannelLayout output) | |
| 58 : input_layout_(input), | |
| 59 output_layout_(output) { | |
| 60 // Stereo downmix should never be the output layout. | |
| 61 CHECK_NE(output_layout_, CHANNEL_LAYOUT_STEREO_DOWNMIX); | |
| 62 | |
| 63 int input_channels = ValidateLayout(input_layout_); | |
| 64 int output_channels = ValidateLayout(output_layout_); | |
| 65 | |
| 66 // Size out the initial matrix. | |
| 67 matrix_.reserve(output_channels); | |
| 68 for (int output_ch = 0; output_ch < output_channels; ++output_ch) | |
| 69 matrix_.push_back(std::vector<float>(input_channels, 0)); | |
| 70 | |
| 71 // Route matching channels and figure out which ones aren't accounted for. | |
| 72 for (Channels ch = LEFT; ch < CHANNELS_MAX; | |
| 73 ch = static_cast<Channels>(ch + 1)) { | |
| 74 | |
|
Chris Rogers
2012/10/17 19:46:50
extra blank line
DaleCurtis
2012/10/18 05:22:04
Done.
| |
| 75 int input_ch_index = ChannelOrder(input_layout_, ch); | |
| 76 int output_ch_index = ChannelOrder(output_layout_, ch); | |
| 77 | |
| 78 if (input_ch_index < 0) | |
| 79 continue; | |
| 80 | |
| 81 if (output_ch_index < 0) { | |
| 82 unaccounted_inputs_.push_back(ch); | |
| 83 continue; | |
| 84 } | |
| 85 | |
| 86 DCHECK_LT(static_cast<size_t>(output_ch_index), matrix_.size()); | |
| 87 DCHECK_LT(static_cast<size_t>(input_ch_index), matrix_[0].size()); | |
| 88 matrix_[output_ch_index][input_ch_index] = 1; | |
| 89 } | |
| 90 | |
| 91 // If all input channels are accounted for, there's nothing left to do. | |
| 92 if (unaccounted_inputs_.empty()) | |
| 93 return; | |
| 94 | |
| 95 // Mix front LR into center. | |
| 96 if (IsUnaccounted(LEFT)) { | |
| 97 Mix(LEFT, CENTER, kDefaultScale); | |
| 98 Mix(RIGHT, CENTER, kDefaultScale); | |
| 99 } | |
|
Chris Rogers
2012/10/17 19:46:50
I think this is the wrong approach for stereo -> m
DaleCurtis
2012/10/17 20:06:47
Okay, sounds like we just need two special case bl
DaleCurtis
2012/10/18 01:56:44
Done.
| |
| 100 | |
| 101 // Mix center into front LR. | |
| 102 if (IsUnaccounted(CENTER)) { | |
| 103 MixWithoutAccounting(CENTER, LEFT, kDefaultScale); | |
| 104 Mix(CENTER, RIGHT, kDefaultScale); | |
| 105 } | |
|
Chris Rogers
2012/10/17 19:46:50
This is odd and very much different from what audi
DaleCurtis
2012/10/17 20:06:47
Ditto.
DaleCurtis
2012/10/18 01:56:44
Done.
| |
| 106 | |
| 107 // Mix back LR into: side LR || back center || front LR || front center. | |
| 108 if (IsUnaccounted(BACK_LEFT)) { | |
| 109 if (HasOutputChannel(SIDE_LEFT)) { | |
| 110 // If we have side LR, mix back LR into side LR, but instead if the input | |
| 111 // doesn't have side LR (but output does) copy back LR to side LR. | |
| 112 float scale = HasInputChannel(SIDE_LEFT) ? kDefaultScale : 1; | |
| 113 Mix(BACK_LEFT, SIDE_LEFT, scale); | |
| 114 Mix(BACK_RIGHT, SIDE_RIGHT, scale); | |
| 115 } else if (HasOutputChannel(BACK_CENTER)) { | |
| 116 // Mix back LR into back center. | |
| 117 Mix(BACK_LEFT, BACK_CENTER, kDefaultScale); | |
| 118 Mix(BACK_RIGHT, BACK_CENTER, kDefaultScale); | |
| 119 } else if (output > CHANNEL_LAYOUT_MONO) { | |
| 120 // Mix back LR into front LR. | |
| 121 Mix(BACK_LEFT, LEFT, kDefaultScale); | |
| 122 Mix(BACK_RIGHT, RIGHT, kDefaultScale); | |
| 123 } else { | |
| 124 // Mix back LR into front center. | |
| 125 Mix(BACK_LEFT, CENTER, kDefaultScale); | |
| 126 Mix(BACK_RIGHT, CENTER, kDefaultScale); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 // Mix side LR into: back LR || back center || front LR || front center. | |
| 131 if (IsUnaccounted(SIDE_LEFT)) { | |
| 132 if (HasOutputChannel(BACK_LEFT)) { | |
| 133 // If we have back LR, mix side LR into back LR, but instead if the input | |
| 134 // doesn't have back LR (but output does) copy side LR to back LR. | |
| 135 float scale = HasInputChannel(BACK_LEFT) ? kDefaultScale : 1; | |
| 136 Mix(SIDE_LEFT, BACK_LEFT, scale); | |
| 137 Mix(SIDE_RIGHT, BACK_RIGHT, scale); | |
| 138 } else if (HasOutputChannel(BACK_CENTER)) { | |
| 139 // Mix side LR into back center. | |
| 140 Mix(SIDE_LEFT, BACK_CENTER, kDefaultScale); | |
| 141 Mix(SIDE_RIGHT, BACK_CENTER, kDefaultScale); | |
| 142 } else if (output > CHANNEL_LAYOUT_MONO) { | |
| 143 // Mix side LR into front LR. | |
| 144 Mix(SIDE_LEFT, LEFT, kDefaultScale); | |
| 145 Mix(SIDE_RIGHT, RIGHT, kDefaultScale); | |
| 146 } else { | |
| 147 // Mix side LR into front center. | |
| 148 Mix(SIDE_LEFT, CENTER, kDefaultScale); | |
| 149 Mix(SIDE_RIGHT, CENTER, kDefaultScale); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 // Mix back center into: back LR || side LR || front LR || front center. | |
| 154 if (IsUnaccounted(BACK_CENTER)) { | |
| 155 if (HasOutputChannel(BACK_LEFT)) { | |
| 156 // Mix back center into back LR. | |
| 157 MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kDefaultScale); | |
| 158 Mix(BACK_CENTER, BACK_RIGHT, kDefaultScale); | |
| 159 } else if (HasOutputChannel(SIDE_LEFT)) { | |
| 160 // Mix back center into side LR. | |
| 161 MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kDefaultScale); | |
| 162 Mix(BACK_CENTER, SIDE_RIGHT, kDefaultScale); | |
| 163 } else if (output > CHANNEL_LAYOUT_MONO) { | |
| 164 // Mix back center into front LR. | |
| 165 // TODO(dalecurtis): Not sure about these values? | |
| 166 MixWithoutAccounting(BACK_CENTER, LEFT, kDefaultScale * kDefaultScale); | |
| 167 Mix(BACK_CENTER, RIGHT, kDefaultScale * kDefaultScale); | |
| 168 } else { | |
| 169 // Mix back center into front center. | |
| 170 // TODO(dalecurtis): Not sure about these values? | |
| 171 Mix(BACK_CENTER, CENTER, kDefaultScale * kDefaultScale); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 // Mix LR of center into: front center || front LR. | |
| 176 if (IsUnaccounted(LEFT_OF_CENTER)) { | |
| 177 if (HasOutputChannel(CENTER)) { | |
| 178 // Mix LR of center into front center. | |
| 179 Mix(LEFT_OF_CENTER, CENTER, kDefaultScale); | |
| 180 Mix(RIGHT_OF_CENTER, CENTER, kDefaultScale); | |
| 181 } else { | |
| 182 // Mix LR of center into front LR. | |
| 183 Mix(LEFT_OF_CENTER, LEFT, kDefaultScale); | |
| 184 Mix(RIGHT_OF_CENTER, RIGHT, kDefaultScale); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 // Mix LFE into: front LR || front center. | |
| 189 if (IsUnaccounted(LFE)) { | |
| 190 if (!HasOutputChannel(CENTER)) { | |
| 191 // Mix LFE into front LR. | |
| 192 MixWithoutAccounting(LFE, LEFT, kDefaultScale); | |
| 193 Mix(LFE, RIGHT, kDefaultScale); | |
| 194 } else { | |
| 195 // Mix LFE into front center. | |
| 196 Mix(LFE, CENTER, kDefaultScale); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 // All channels should now be accounted for. | |
| 201 DCHECK(unaccounted_inputs_.empty()); | |
| 202 } | |
| 203 | |
| 204 ChannelMixer::~ChannelMixer() {} | |
| 205 | |
| 206 void ChannelMixer::Rematrix(const AudioBus* input, AudioBus* output) { | |
|
Chris Rogers
2012/10/17 19:46:50
What is our plan for simple channel swizzling to a
DaleCurtis
2012/10/17 20:06:47
Yes I plan to add an AudioBus::Remap(int ch, int c
| |
| 207 CHECK_EQ(matrix_.size(), static_cast<size_t>(output->channels())); | |
| 208 CHECK_EQ(matrix_[0].size(), static_cast<size_t>(input->channels())); | |
| 209 CHECK_EQ(input->frames(), output->frames()); | |
| 210 | |
| 211 for (int output_ch = 0; output_ch < output->channels(); ++output_ch) { | |
| 212 // Zero initialize |output| so we're accumulating from zero. | |
|
scherkus (not reviewing)
2012/10/18 01:30:39
silly optimization question... (I think chris aske
DaleCurtis
2012/10/18 01:56:44
Hmm, we don't need to accumulate when the output l
scherkus (not reviewing)
2012/10/18 02:24:03
Not sure.
I was thinking of a situation (for what
DaleCurtis
2012/10/18 05:22:04
Added memcpy support for remaps, pretty trivial an
| |
| 213 memset(output->channel(output_ch), 0, | |
| 214 output->frames() * sizeof(*output->channel(output_ch))); | |
|
Chris Rogers
2012/10/17 19:46:50
This cries out for a AudioBus::ZeroChannel() metho
DaleCurtis
2012/10/17 20:06:47
Good idea. Will add.
DaleCurtis
2012/10/18 05:22:04
Actually, output->Zero() outside of the loop is be
| |
| 215 | |
| 216 for (int input_ch = 0; input_ch < input->channels(); ++input_ch) { | |
| 217 float scale = matrix_[output_ch][input_ch]; | |
| 218 // Scale should always be positive. Don't bother scaling by zero. | |
| 219 DCHECK_GE(scale, 0); | |
| 220 if (scale > std::numeric_limits<float>::epsilon()) { | |
| 221 vector_math::FMAC(input->channel(input_ch), scale, output->frames(), | |
| 222 output->channel(output_ch)); | |
|
Chris Rogers
2012/10/17 19:46:50
What if the scale is 1, or does FMAC optimize for
DaleCurtis
2012/10/17 20:06:47
FMAC doesn't optimize for this case. I could add a
| |
| 223 } | |
| 224 } | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 void ChannelMixer::AccountFor(Channels ch) { | |
| 229 unaccounted_inputs_.erase(std::find( | |
| 230 unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch)); | |
| 231 } | |
| 232 | |
| 233 bool ChannelMixer::IsUnaccounted(Channels ch) { | |
| 234 return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(), | |
| 235 ch) != unaccounted_inputs_.end(); | |
| 236 } | |
| 237 | |
| 238 bool ChannelMixer::HasInputChannel(Channels ch) { | |
| 239 return ChannelOrder(input_layout_, ch) >= 0; | |
| 240 } | |
| 241 | |
| 242 bool ChannelMixer::HasOutputChannel(Channels ch) { | |
| 243 return ChannelOrder(output_layout_, ch) >= 0; | |
| 244 } | |
| 245 | |
| 246 void ChannelMixer::Mix(Channels input_ch, Channels output_ch, float scale) { | |
| 247 MixWithoutAccounting(input_ch, output_ch, scale); | |
| 248 AccountFor(input_ch); | |
| 249 } | |
| 250 | |
| 251 void ChannelMixer::MixWithoutAccounting(Channels input_ch, Channels output_ch, | |
| 252 float scale) { | |
| 253 int input_ch_index = ChannelOrder(input_layout_, input_ch); | |
| 254 int output_ch_index = ChannelOrder(output_layout_, output_ch); | |
| 255 | |
| 256 DCHECK(IsUnaccounted(input_ch)); | |
| 257 DCHECK_GE(input_ch_index, 0); | |
| 258 DCHECK_GE(output_ch_index, 0); | |
| 259 | |
| 260 matrix_[output_ch_index][input_ch_index] += scale; | |
| 261 } | |
| 262 | |
| 263 } // namespace media | |
| OLD | NEW |