summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_coding/acm2/acm_remixing.cc
blob: 13709dbbee94298cccd2e969e9cd9f2291f4d456 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/audio_coding/acm2/acm_remixing.h"

#include "rtc_base/checks.h"

namespace webrtc {

void DownMixFrame(const AudioFrame& input, rtc::ArrayView<int16_t> output) {
  RTC_DCHECK_EQ(input.num_channels_, 2);
  RTC_DCHECK_EQ(output.size(), input.samples_per_channel_);

  if (input.muted()) {
    std::fill(output.begin(), output.begin() + input.samples_per_channel_, 0);
  } else {
    const int16_t* const input_data = input.data();
    for (size_t n = 0; n < input.samples_per_channel_; ++n) {
      output[n] = rtc::dchecked_cast<int16_t>(
          (int32_t{input_data[2 * n]} + int32_t{input_data[2 * n + 1]}) >> 1);
    }
  }
}

void ReMixFrame(const AudioFrame& input,
                size_t num_output_channels,
                std::vector<int16_t>* output) {
  const size_t output_size = num_output_channels * input.samples_per_channel_;
  RTC_DCHECK(!(input.num_channels_ == 0 && num_output_channels > 0 &&
               input.samples_per_channel_ > 0));

  if (output->size() != output_size) {
    output->resize(output_size);
  }

  // For muted frames, fill the frame with zeros.
  if (input.muted()) {
    std::fill(output->begin(), output->end(), 0);
    return;
  }

  // Ensure that the special case of zero input channels is handled correctly
  // (zero samples per channel is already handled correctly in the code below).
  if (input.num_channels_ == 0) {
    return;
  }

  const int16_t* const input_data = input.data();
  size_t out_index = 0;

  // When upmixing is needed and the input is mono copy the left channel
  // into the left and right channels, and set any remaining channels to zero.
  if (input.num_channels_ == 1 && input.num_channels_ < num_output_channels) {
    for (size_t k = 0; k < input.samples_per_channel_; ++k) {
      (*output)[out_index++] = input_data[k];
      (*output)[out_index++] = input_data[k];
      for (size_t j = 2; j < num_output_channels; ++j) {
        (*output)[out_index++] = 0;
      }
      RTC_DCHECK_EQ(out_index, (k + 1) * num_output_channels);
    }
    RTC_DCHECK_EQ(out_index, input.samples_per_channel_ * num_output_channels);
    return;
  }

  size_t in_index = 0;

  // When upmixing is needed and the output is surround, copy the available
  // channels directly, and set the remaining channels to zero.
  if (input.num_channels_ < num_output_channels) {
    for (size_t k = 0; k < input.samples_per_channel_; ++k) {
      for (size_t j = 0; j < input.num_channels_; ++j) {
        (*output)[out_index++] = input_data[in_index++];
      }
      for (size_t j = input.num_channels_; j < num_output_channels; ++j) {
        (*output)[out_index++] = 0;
      }
      RTC_DCHECK_EQ(in_index, (k + 1) * input.num_channels_);
      RTC_DCHECK_EQ(out_index, (k + 1) * num_output_channels);
    }
    RTC_DCHECK_EQ(in_index, input.samples_per_channel_ * input.num_channels_);
    RTC_DCHECK_EQ(out_index, input.samples_per_channel_ * num_output_channels);

    return;
  }

  // When downmixing is needed, and the input is stereo, average the channels.
  if (input.num_channels_ == 2) {
    for (size_t n = 0; n < input.samples_per_channel_; ++n) {
      (*output)[n] = rtc::dchecked_cast<int16_t>(
          (int32_t{input_data[2 * n]} + int32_t{input_data[2 * n + 1]}) >> 1);
    }
    return;
  }

  // When downmixing is needed, and the input is multichannel, drop the surplus
  // channels.
  const size_t num_channels_to_drop = input.num_channels_ - num_output_channels;
  for (size_t k = 0; k < input.samples_per_channel_; ++k) {
    for (size_t j = 0; j < num_output_channels; ++j) {
      (*output)[out_index++] = input_data[in_index++];
    }
    in_index += num_channels_to_drop;
  }
}

}  // namespace webrtc