Index: media/base/channel_mixing_matrix.cc |
diff --git a/media/base/channel_mixer.cc b/media/base/channel_mixing_matrix.cc |
similarity index 65% |
copy from media/base/channel_mixer.cc |
copy to media/base/channel_mixing_matrix.cc |
index ef1bd5c096e96421f2fc6c02fce1ea3a3f57fa42..eee0efd0c33017196f5c3e6481408ac8918285dc 100644 |
--- a/media/base/channel_mixer.cc |
+++ b/media/base/channel_mixing_matrix.cc |
@@ -5,15 +5,12 @@ |
// MSVC++ requires this to be set before any other includes to get M_SQRT1_2. |
#define _USE_MATH_DEFINES |
-#include "media/base/channel_mixer.h" |
+#include "media/base/channel_mixing_matrix.h" |
#include <algorithm> |
#include <cmath> |
#include "base/logging.h" |
-#include "media/audio/audio_parameters.h" |
-#include "media/base/audio_bus.h" |
-#include "media/base/vector_math.h" |
namespace media { |
@@ -52,83 +49,14 @@ static void ValidateLayout(ChannelLayout layout) { |
} |
} |
-class MatrixBuilder { |
- public: |
- MatrixBuilder(ChannelLayout input_layout, int input_channels, |
- ChannelLayout output_layout, int output_channels) |
- : input_layout_(input_layout), |
- input_channels_(input_channels), |
- output_layout_(output_layout), |
- output_channels_(output_channels) { |
- // Special case for 5.0, 5.1 with back channels when upmixed to 7.0, 7.1, |
- // which should map the back LR to side LR. |
- if (input_layout_ == CHANNEL_LAYOUT_5_0_BACK && |
- output_layout_ == CHANNEL_LAYOUT_7_0) { |
- input_layout_ = CHANNEL_LAYOUT_5_0; |
- } else if (input_layout_ == CHANNEL_LAYOUT_5_1_BACK && |
- output_layout_ == CHANNEL_LAYOUT_7_1) { |
- input_layout_ = CHANNEL_LAYOUT_5_1; |
- } |
- } |
- |
- ~MatrixBuilder() { } |
- |
- // Create the transformation matrix of input channels to output channels. |
- // Updates the empty matrix with the transformation, and returns true |
- // if the transformation is just a remapping of channels (no mixing). |
- bool CreateTransformationMatrix(std::vector< std::vector<float> >* matrix); |
- |
- private: |
- // Result transformation of input channels to output channels |
- std::vector< std::vector<float> >* matrix_; |
- |
- // Input and output channel layout provided during construction. |
- ChannelLayout input_layout_; |
- int input_channels_; |
- ChannelLayout output_layout_; |
- int output_channels_; |
- |
- // Helper variable for tracking which inputs are currently unaccounted, |
- // should be empty after construction completes. |
- std::vector<Channels> unaccounted_inputs_; |
- |
- // Helper methods for managing unaccounted input channels. |
- void AccountFor(Channels ch); |
- bool IsUnaccounted(Channels ch) const; |
- |
- // Helper methods for checking if |ch| exists in either |input_layout_| or |
- // |output_layout_| respectively. |
- bool HasInputChannel(Channels ch) const; |
- bool HasOutputChannel(Channels ch) const; |
- |
- // Helper methods for updating |matrix_| with the proper value for |
- // mixing |input_ch| into |output_ch|. MixWithoutAccounting() does not |
- // remove the channel from |unaccounted_inputs_|. |
- void Mix(Channels input_ch, Channels output_ch, float scale); |
- void MixWithoutAccounting(Channels input_ch, Channels output_ch, float scale); |
- |
- DISALLOW_COPY_AND_ASSIGN(MatrixBuilder); |
-}; |
- |
-ChannelMixer::ChannelMixer(ChannelLayout input_layout, |
- ChannelLayout output_layout) { |
- Initialize(input_layout, |
- ChannelLayoutToChannelCount(input_layout), |
- output_layout, |
- ChannelLayoutToChannelCount(output_layout)); |
-} |
- |
-ChannelMixer::ChannelMixer( |
- const AudioParameters& input, const AudioParameters& output) { |
- Initialize(input.channel_layout(), |
- input.channels(), |
- output.channel_layout(), |
- output.channels()); |
-} |
- |
-void ChannelMixer::Initialize( |
- ChannelLayout input_layout, int input_channels, |
- ChannelLayout output_layout, int output_channels) { |
+ChannelMixingMatrix::ChannelMixingMatrix(ChannelLayout input_layout, |
+ int input_channels, |
+ ChannelLayout output_layout, |
+ int output_channels) |
+ : input_layout_(input_layout), |
+ input_channels_(input_channels), |
+ output_layout_(output_layout), |
+ output_channels_(output_channels) { |
// Stereo down mix should never be the output layout. |
CHECK_NE(output_layout, CHANNEL_LAYOUT_STEREO_DOWNMIX); |
@@ -138,14 +66,22 @@ void ChannelMixer::Initialize( |
if (output_layout != CHANNEL_LAYOUT_DISCRETE) |
ValidateLayout(output_layout); |
- // Create the transformation matrix |
- MatrixBuilder matrix_builder(input_layout, input_channels, |
- output_layout, output_channels); |
- remapping_ = matrix_builder.CreateTransformationMatrix(&matrix_); |
+ // Special case for 5.0, 5.1 with back channels when upmixed to 7.0, 7.1, |
+ // which should map the back LR to side LR. |
+ if (input_layout_ == CHANNEL_LAYOUT_5_0_BACK && |
+ output_layout_ == CHANNEL_LAYOUT_7_0) { |
+ input_layout_ = CHANNEL_LAYOUT_5_0; |
+ } else if (input_layout_ == CHANNEL_LAYOUT_5_1_BACK && |
+ output_layout_ == CHANNEL_LAYOUT_7_1) { |
+ input_layout_ = CHANNEL_LAYOUT_5_1; |
+ } |
} |
-bool MatrixBuilder::CreateTransformationMatrix( |
- std::vector< std::vector<float> >* matrix) { |
+ChannelMixingMatrix::~ChannelMixingMatrix() { |
+} |
+ |
+bool ChannelMixingMatrix::CreateTransformationMatrix( |
+ std::vector<std::vector<float>>* matrix) { |
matrix_ = matrix; |
// Size out the initial matrix. |
@@ -326,70 +262,34 @@ bool MatrixBuilder::CreateTransformationMatrix( |
return true; |
} |
-ChannelMixer::~ChannelMixer() {} |
- |
-void ChannelMixer::Transform(const AudioBus* input, AudioBus* output) { |
- CHECK_EQ(matrix_.size(), static_cast<size_t>(output->channels())); |
- CHECK_EQ(matrix_[0].size(), static_cast<size_t>(input->channels())); |
- CHECK_EQ(input->frames(), output->frames()); |
- |
- // Zero initialize |output| so we're accumulating from zero. |
- output->Zero(); |
- |
- // If we're just remapping we can simply copy the correct input to output. |
- if (remapping_) { |
- for (int output_ch = 0; output_ch < output->channels(); ++output_ch) { |
- for (int input_ch = 0; input_ch < input->channels(); ++input_ch) { |
- float scale = matrix_[output_ch][input_ch]; |
- if (scale > 0) { |
- DCHECK_EQ(scale, 1.0f); |
- memcpy(output->channel(output_ch), input->channel(input_ch), |
- sizeof(*output->channel(output_ch)) * output->frames()); |
- break; |
- } |
- } |
- } |
- return; |
- } |
- |
- for (int output_ch = 0; output_ch < output->channels(); ++output_ch) { |
- for (int input_ch = 0; input_ch < input->channels(); ++input_ch) { |
- float scale = matrix_[output_ch][input_ch]; |
- // Scale should always be positive. Don't bother scaling by zero. |
- DCHECK_GE(scale, 0); |
- if (scale > 0) { |
- vector_math::FMAC(input->channel(input_ch), scale, output->frames(), |
- output->channel(output_ch)); |
- } |
- } |
- } |
-} |
- |
-void MatrixBuilder::AccountFor(Channels ch) { |
+void ChannelMixingMatrix::AccountFor(Channels ch) { |
unaccounted_inputs_.erase(std::find( |
unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch)); |
} |
-bool MatrixBuilder::IsUnaccounted(Channels ch) const { |
+bool ChannelMixingMatrix::IsUnaccounted(Channels ch) const { |
return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(), |
ch) != unaccounted_inputs_.end(); |
} |
-bool MatrixBuilder::HasInputChannel(Channels ch) const { |
+bool ChannelMixingMatrix::HasInputChannel(Channels ch) const { |
return ChannelOrder(input_layout_, ch) >= 0; |
} |
-bool MatrixBuilder::HasOutputChannel(Channels ch) const { |
+bool ChannelMixingMatrix::HasOutputChannel(Channels ch) const { |
return ChannelOrder(output_layout_, ch) >= 0; |
} |
-void MatrixBuilder::Mix(Channels input_ch, Channels output_ch, float scale) { |
+void ChannelMixingMatrix::Mix(Channels input_ch, |
+ Channels output_ch, |
+ float scale) { |
MixWithoutAccounting(input_ch, output_ch, scale); |
AccountFor(input_ch); |
} |
-void MatrixBuilder::MixWithoutAccounting(Channels input_ch, Channels output_ch, |
- float scale) { |
+void ChannelMixingMatrix::MixWithoutAccounting(Channels input_ch, |
+ Channels output_ch, |
+ float scale) { |
int input_ch_index = ChannelOrder(input_layout_, input_ch); |
int output_ch_index = ChannelOrder(output_layout_, output_ch); |