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