xref: /aosp_15_r20/external/webrtc/audio/utility/channel_mixer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2012 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 "audio/utility/channel_mixer.h"
12 
13 #include "audio/utility/channel_mixing_matrix.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/logging.h"
16 #include "rtc_base/numerics/safe_conversions.h"
17 
18 namespace webrtc {
19 
ChannelMixer(ChannelLayout input_layout,ChannelLayout output_layout)20 ChannelMixer::ChannelMixer(ChannelLayout input_layout,
21                            ChannelLayout output_layout)
22     : input_layout_(input_layout),
23       output_layout_(output_layout),
24       input_channels_(ChannelLayoutToChannelCount(input_layout)),
25       output_channels_(ChannelLayoutToChannelCount(output_layout)) {
26   // Create the transformation matrix.
27   ChannelMixingMatrix matrix_builder(input_layout_, input_channels_,
28                                      output_layout_, output_channels_);
29   remapping_ = matrix_builder.CreateTransformationMatrix(&matrix_);
30 }
31 
32 ChannelMixer::~ChannelMixer() = default;
33 
Transform(AudioFrame * frame)34 void ChannelMixer::Transform(AudioFrame* frame) {
35   RTC_DCHECK(frame);
36   RTC_DCHECK_EQ(matrix_[0].size(), static_cast<size_t>(input_channels_));
37   RTC_DCHECK_EQ(matrix_.size(), static_cast<size_t>(output_channels_));
38 
39   // Leave the audio frame intact if the channel layouts for in and out are
40   // identical.
41   if (input_layout_ == output_layout_) {
42     return;
43   }
44 
45   if (IsUpMixing()) {
46     RTC_CHECK_LE(frame->samples_per_channel() * output_channels_,
47                  frame->max_16bit_samples());
48   }
49 
50   // Only change the number of output channels if the audio frame is muted.
51   if (frame->muted()) {
52     frame->num_channels_ = output_channels_;
53     frame->channel_layout_ = output_layout_;
54     return;
55   }
56 
57   const int16_t* in_audio = frame->data();
58 
59   // Only allocate fresh memory at first access or if the required size has
60   // increased.
61   // TODO(henrika): we might be able to do downmixing in-place and thereby avoid
62   // extra memory allocation and a memcpy.
63   const size_t num_elements = frame->samples_per_channel() * output_channels_;
64   if (audio_vector_ == nullptr || num_elements > audio_vector_size_) {
65     audio_vector_.reset(new int16_t[num_elements]);
66     audio_vector_size_ = num_elements;
67   }
68   int16_t* out_audio = audio_vector_.get();
69 
70   // Modify the number of channels by creating a weighted sum of input samples
71   // where the weights (scale factors) for each output sample are given by the
72   // transformation matrix.
73   for (size_t i = 0; i < frame->samples_per_channel(); i++) {
74     for (size_t output_ch = 0; output_ch < output_channels_; ++output_ch) {
75       float acc_value = 0.0f;
76       for (size_t input_ch = 0; input_ch < input_channels_; ++input_ch) {
77         const float scale = matrix_[output_ch][input_ch];
78         // Scale should always be positive.
79         RTC_DCHECK_GE(scale, 0);
80         // Each output sample is a weighted sum of input samples.
81         acc_value += scale * in_audio[i * input_channels_ + input_ch];
82       }
83       const size_t index = output_channels_ * i + output_ch;
84       RTC_CHECK_LE(index, audio_vector_size_);
85       out_audio[index] = rtc::saturated_cast<int16_t>(acc_value);
86     }
87   }
88 
89   // Update channel information.
90   frame->num_channels_ = output_channels_;
91   frame->channel_layout_ = output_layout_;
92 
93   // Copy the output result to the audio frame in `frame`.
94   memcpy(
95       frame->mutable_data(), out_audio,
96       sizeof(int16_t) * frame->samples_per_channel() * frame->num_channels());
97 }
98 
99 }  // namespace webrtc
100