// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/channel_mixer.h"

#include <stddef.h>
#include <string.h>

#include "base/check_op.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_parameters.h"
#include "media/base/channel_mixing_matrix.h"
#include "media/base/vector_math.h"

namespace media {

ChannelMixer::ChannelMixer(const ChannelLayoutConfig& input_config,
                           const ChannelLayoutConfig& output_config) {
  Initialize(input_config, output_config);
}

ChannelMixer::ChannelMixer(
    const AudioParameters& input, const AudioParameters& output) {
  Initialize(input.channel_layout_config(), output.channel_layout_config());
}

void ChannelMixer::Initialize(const ChannelLayoutConfig& input_config,
                              const ChannelLayoutConfig& output_config) {
  // Create the transformation matrix
  ChannelMixingMatrix matrix_builder(input_config, output_config);
  remapping_ = matrix_builder.CreateTransformationMatrix(&matrix_);
}

ChannelMixer::~ChannelMixer() = default;

void ChannelMixer::Transform(const AudioBus* input, AudioBus* output) {
  CHECK_EQ(input->frames(), output->frames());
  TransformPartial(input, input->frames(), output);
}

void ChannelMixer::TransformPartial(const AudioBus* input,
                                    int frame_count,
                                    AudioBus* output) {
  CHECK_EQ(matrix_.size(), static_cast<size_t>(output->channels()));
  CHECK_EQ(matrix_[0].size(), static_cast<size_t>(input->channels()));
  CHECK_LE(frame_count, input->frames());
  CHECK_LE(frame_count, output->frames());

  if (frame_count <= 0) {
    return;
  }
  // Zero initialize |output| so we're accumulating from zero.
  output->ZeroFrames(frame_count);

  // If we're just remapping we can simply copy the correct input to output.
  if (remapping_) {
    const size_t frames = static_cast<size_t>(frame_count);

    for (int output_ch = 0; output_ch < output->channels(); ++output_ch) {
      auto output_channel = output->channel(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);
          output_channel.first(frames).copy_from_nonoverlapping(
              input->channel(input_ch).first(frames));
          break;
        }
      }
    }
    return;
  }

  for (int output_ch = 0; output_ch < output->channels(); ++output_ch) {
    auto output_channel = output->channel(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);
      const size_t frames = static_cast<size_t>(frame_count);
      if (scale > 0) {
        vector_math::FMAC(input->channel(input_ch).first(frames), scale,
                          output_channel.first(frames));
      }
    }
  }
}

}  // namespace media
