1 /*
2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/audio_coding/acm2/acm_remixing.h"
12
13 #include "rtc_base/checks.h"
14
15 namespace webrtc {
16
DownMixFrame(const AudioFrame & input,rtc::ArrayView<int16_t> output)17 void DownMixFrame(const AudioFrame& input, rtc::ArrayView<int16_t> output) {
18 RTC_DCHECK_EQ(input.num_channels_, 2);
19 RTC_DCHECK_EQ(output.size(), input.samples_per_channel_);
20
21 if (input.muted()) {
22 std::fill(output.begin(), output.begin() + input.samples_per_channel_, 0);
23 } else {
24 const int16_t* const input_data = input.data();
25 for (size_t n = 0; n < input.samples_per_channel_; ++n) {
26 output[n] = rtc::dchecked_cast<int16_t>(
27 (int32_t{input_data[2 * n]} + int32_t{input_data[2 * n + 1]}) >> 1);
28 }
29 }
30 }
31
ReMixFrame(const AudioFrame & input,size_t num_output_channels,std::vector<int16_t> * output)32 void ReMixFrame(const AudioFrame& input,
33 size_t num_output_channels,
34 std::vector<int16_t>* output) {
35 const size_t output_size = num_output_channels * input.samples_per_channel_;
36 RTC_DCHECK(!(input.num_channels_ == 0 && num_output_channels > 0 &&
37 input.samples_per_channel_ > 0));
38
39 if (output->size() != output_size) {
40 output->resize(output_size);
41 }
42
43 // For muted frames, fill the frame with zeros.
44 if (input.muted()) {
45 std::fill(output->begin(), output->end(), 0);
46 return;
47 }
48
49 // Ensure that the special case of zero input channels is handled correctly
50 // (zero samples per channel is already handled correctly in the code below).
51 if (input.num_channels_ == 0) {
52 return;
53 }
54
55 const int16_t* const input_data = input.data();
56 size_t out_index = 0;
57
58 // When upmixing is needed and the input is mono copy the left channel
59 // into the left and right channels, and set any remaining channels to zero.
60 if (input.num_channels_ == 1 && input.num_channels_ < num_output_channels) {
61 for (size_t k = 0; k < input.samples_per_channel_; ++k) {
62 (*output)[out_index++] = input_data[k];
63 (*output)[out_index++] = input_data[k];
64 for (size_t j = 2; j < num_output_channels; ++j) {
65 (*output)[out_index++] = 0;
66 }
67 RTC_DCHECK_EQ(out_index, (k + 1) * num_output_channels);
68 }
69 RTC_DCHECK_EQ(out_index, input.samples_per_channel_ * num_output_channels);
70 return;
71 }
72
73 size_t in_index = 0;
74
75 // When upmixing is needed and the output is surround, copy the available
76 // channels directly, and set the remaining channels to zero.
77 if (input.num_channels_ < num_output_channels) {
78 for (size_t k = 0; k < input.samples_per_channel_; ++k) {
79 for (size_t j = 0; j < input.num_channels_; ++j) {
80 (*output)[out_index++] = input_data[in_index++];
81 }
82 for (size_t j = input.num_channels_; j < num_output_channels; ++j) {
83 (*output)[out_index++] = 0;
84 }
85 RTC_DCHECK_EQ(in_index, (k + 1) * input.num_channels_);
86 RTC_DCHECK_EQ(out_index, (k + 1) * num_output_channels);
87 }
88 RTC_DCHECK_EQ(in_index, input.samples_per_channel_ * input.num_channels_);
89 RTC_DCHECK_EQ(out_index, input.samples_per_channel_ * num_output_channels);
90
91 return;
92 }
93
94 // When downmixing is needed, and the input is stereo, average the channels.
95 if (input.num_channels_ == 2) {
96 for (size_t n = 0; n < input.samples_per_channel_; ++n) {
97 (*output)[n] = rtc::dchecked_cast<int16_t>(
98 (int32_t{input_data[2 * n]} + int32_t{input_data[2 * n + 1]}) >> 1);
99 }
100 return;
101 }
102
103 // When downmixing is needed, and the input is multichannel, drop the surplus
104 // channels.
105 const size_t num_channels_to_drop = input.num_channels_ - num_output_channels;
106 for (size_t k = 0; k < input.samples_per_channel_; ++k) {
107 for (size_t j = 0; j < num_output_channels; ++j) {
108 (*output)[out_index++] = input_data[in_index++];
109 }
110 in_index += num_channels_to_drop;
111 }
112 }
113
114 } // namespace webrtc
115