This source file includes following definitions.
- ValidateLayout
- output_channels_
- Initialize
- CreateTransformationMatrix
- Transform
- AccountFor
- IsUnaccounted
- HasInputChannel
- HasOutputChannel
- Mix
- MixWithoutAccounting
#define _USE_MATH_DEFINES
#include "media/base/channel_mixer.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 {
static const float kEqualPowerScale = static_cast<float>(M_SQRT1_2);
static void ValidateLayout(ChannelLayout layout) {
CHECK_NE(layout, CHANNEL_LAYOUT_NONE);
CHECK_LE(layout, CHANNEL_LAYOUT_MAX);
CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED);
CHECK_NE(layout, CHANNEL_LAYOUT_DISCRETE);
CHECK_NE(layout, CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC);
int channel_count = ChannelLayoutToChannelCount(layout);
DCHECK_GT(channel_count, 0);
if (channel_count > 1) {
DCHECK((ChannelOrder(layout, LEFT) >= 0 &&
ChannelOrder(layout, RIGHT) >= 0) ||
(ChannelOrder(layout, SIDE_LEFT) >= 0 &&
ChannelOrder(layout, SIDE_RIGHT) >= 0) ||
(ChannelOrder(layout, BACK_LEFT) >= 0 &&
ChannelOrder(layout, BACK_RIGHT) >= 0) ||
(ChannelOrder(layout, LEFT_OF_CENTER) >= 0 &&
ChannelOrder(layout, RIGHT_OF_CENTER) >= 0))
<< "Non-symmetric channel layout encountered.";
} else {
DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO);
}
return;
}
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) {
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() { }
bool CreateTransformationMatrix(std::vector< std::vector<float> >* matrix);
private:
std::vector< std::vector<float> >* matrix_;
ChannelLayout input_layout_;
int input_channels_;
ChannelLayout output_layout_;
int output_channels_;
std::vector<Channels> unaccounted_inputs_;
void AccountFor(Channels ch);
bool IsUnaccounted(Channels ch);
bool HasInputChannel(Channels ch);
bool HasOutputChannel(Channels ch);
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) {
CHECK_NE(output_layout, CHANNEL_LAYOUT_STEREO_DOWNMIX);
if (input_layout != CHANNEL_LAYOUT_DISCRETE)
ValidateLayout(input_layout);
if (output_layout != CHANNEL_LAYOUT_DISCRETE)
ValidateLayout(output_layout);
MatrixBuilder matrix_builder(input_layout, input_channels,
output_layout, output_channels);
remapping_ = matrix_builder.CreateTransformationMatrix(&matrix_);
}
bool MatrixBuilder::CreateTransformationMatrix(
std::vector< std::vector<float> >* matrix) {
matrix_ = matrix;
matrix_->reserve(output_channels_);
for (int output_ch = 0; output_ch < output_channels_; ++output_ch)
matrix_->push_back(std::vector<float>(input_channels_, 0));
if (input_layout_ == CHANNEL_LAYOUT_DISCRETE ||
output_layout_ == CHANNEL_LAYOUT_DISCRETE) {
int passthrough_channels = std::min(input_channels_, output_channels_);
for (int i = 0; i < passthrough_channels; ++i)
(*matrix_)[i][i] = 1;
return true;
}
for (Channels ch = LEFT; ch < CHANNELS_MAX + 1;
ch = static_cast<Channels>(ch + 1)) {
int input_ch_index = ChannelOrder(input_layout_, ch);
if (input_ch_index < 0)
continue;
int output_ch_index = ChannelOrder(output_layout_, ch);
if (output_ch_index < 0) {
unaccounted_inputs_.push_back(ch);
continue;
}
DCHECK_LT(static_cast<size_t>(output_ch_index), matrix_->size());
DCHECK_LT(static_cast<size_t>(input_ch_index),
(*matrix_)[output_ch_index].size());
(*matrix_)[output_ch_index][input_ch_index] = 1;
}
if (unaccounted_inputs_.empty()) {
return true;
}
if (IsUnaccounted(LEFT)) {
float scale =
(output_layout_ == CHANNEL_LAYOUT_MONO && input_channels_ == 2) ?
0.5 : kEqualPowerScale;
Mix(LEFT, CENTER, scale);
Mix(RIGHT, CENTER, scale);
}
if (IsUnaccounted(CENTER)) {
float scale =
(input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale;
MixWithoutAccounting(CENTER, LEFT, scale);
Mix(CENTER, RIGHT, scale);
}
if (IsUnaccounted(BACK_LEFT)) {
if (HasOutputChannel(SIDE_LEFT)) {
float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1;
Mix(BACK_LEFT, SIDE_LEFT, scale);
Mix(BACK_RIGHT, SIDE_RIGHT, scale);
} else if (HasOutputChannel(BACK_CENTER)) {
Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale);
Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale);
} else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
Mix(BACK_LEFT, LEFT, kEqualPowerScale);
Mix(BACK_RIGHT, RIGHT, kEqualPowerScale);
} else {
Mix(BACK_LEFT, CENTER, kEqualPowerScale);
Mix(BACK_RIGHT, CENTER, kEqualPowerScale);
}
}
if (IsUnaccounted(SIDE_LEFT)) {
if (HasOutputChannel(BACK_LEFT)) {
float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1;
Mix(SIDE_LEFT, BACK_LEFT, scale);
Mix(SIDE_RIGHT, BACK_RIGHT, scale);
} else if (HasOutputChannel(BACK_CENTER)) {
Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale);
Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale);
} else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
Mix(SIDE_LEFT, LEFT, kEqualPowerScale);
Mix(SIDE_RIGHT, RIGHT, kEqualPowerScale);
} else {
Mix(SIDE_LEFT, CENTER, kEqualPowerScale);
Mix(SIDE_RIGHT, CENTER, kEqualPowerScale);
}
}
if (IsUnaccounted(BACK_CENTER)) {
if (HasOutputChannel(BACK_LEFT)) {
MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kEqualPowerScale);
Mix(BACK_CENTER, BACK_RIGHT, kEqualPowerScale);
} else if (HasOutputChannel(SIDE_LEFT)) {
MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kEqualPowerScale);
Mix(BACK_CENTER, SIDE_RIGHT, kEqualPowerScale);
} else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale);
Mix(BACK_CENTER, RIGHT, kEqualPowerScale);
} else {
Mix(BACK_CENTER, CENTER, kEqualPowerScale);
}
}
if (IsUnaccounted(LEFT_OF_CENTER)) {
if (HasOutputChannel(LEFT)) {
Mix(LEFT_OF_CENTER, LEFT, kEqualPowerScale);
Mix(RIGHT_OF_CENTER, RIGHT, kEqualPowerScale);
} else {
Mix(LEFT_OF_CENTER, CENTER, kEqualPowerScale);
Mix(RIGHT_OF_CENTER, CENTER, kEqualPowerScale);
}
}
if (IsUnaccounted(LFE)) {
if (!HasOutputChannel(CENTER)) {
MixWithoutAccounting(LFE, LEFT, kEqualPowerScale);
Mix(LFE, RIGHT, kEqualPowerScale);
} else {
Mix(LFE, CENTER, kEqualPowerScale);
}
}
DCHECK(unaccounted_inputs_.empty());
for (int output_ch = 0; output_ch < output_channels_; ++output_ch) {
int input_mappings = 0;
for (int input_ch = 0; input_ch < input_channels_; ++input_ch) {
if ((*matrix_)[output_ch][input_ch] != 1 || ++input_mappings > 1)
return false;
}
}
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());
output->Zero();
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];
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) {
unaccounted_inputs_.erase(std::find(
unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch));
}
bool MatrixBuilder::IsUnaccounted(Channels ch) {
return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(),
ch) != unaccounted_inputs_.end();
}
bool MatrixBuilder::HasInputChannel(Channels ch) {
return ChannelOrder(input_layout_, ch) >= 0;
}
bool MatrixBuilder::HasOutputChannel(Channels ch) {
return ChannelOrder(output_layout_, ch) >= 0;
}
void MatrixBuilder::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) {
int input_ch_index = ChannelOrder(input_layout_, input_ch);
int output_ch_index = ChannelOrder(output_layout_, output_ch);
DCHECK(IsUnaccounted(input_ch));
DCHECK_GE(input_ch_index, 0);
DCHECK_GE(output_ch_index, 0);
DCHECK_EQ((*matrix_)[output_ch_index][input_ch_index], 0);
(*matrix_)[output_ch_index][input_ch_index] = scale;
}
}