aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_mixer/frame_combiner.h" |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <array> |
| 15 | #include <functional> |
| 16 | #include <memory> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
| 19 | #include "audio/utility/audio_frame_operations.h" |
| 20 | #include "modules/audio_mixer/audio_frame_manipulator.h" |
| 21 | #include "modules/audio_mixer/audio_mixer_impl.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/logging.h" |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace { |
| 27 | |
| 28 | // Stereo, 48 kHz, 10 ms. |
| 29 | constexpr int kMaximalFrameSize = 2 * 48 * 10; |
| 30 | |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 31 | void CombineZeroFrames(bool use_limiter, |
| 32 | AudioProcessing* limiter, |
| 33 | AudioFrame* audio_frame_for_mixing) { |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 34 | audio_frame_for_mixing->elapsed_time_ms_ = -1; |
| 35 | AudioFrameOperations::Mute(audio_frame_for_mixing); |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 36 | // The limiter should still process a zero frame to avoid jumps in |
| 37 | // its gain curve. |
| 38 | if (use_limiter) { |
| 39 | RTC_DCHECK(limiter); |
| 40 | // The limiter smoothly increases frames with half gain to full |
| 41 | // volume. Here there's no need to apply half gain, since the frame |
| 42 | // is zero anyway. |
| 43 | limiter->ProcessStream(audio_frame_for_mixing); |
| 44 | } |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void CombineOneFrame(const AudioFrame* input_frame, |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 48 | bool use_limiter, |
| 49 | AudioProcessing* limiter, |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 50 | AudioFrame* audio_frame_for_mixing) { |
| 51 | audio_frame_for_mixing->timestamp_ = input_frame->timestamp_; |
| 52 | audio_frame_for_mixing->elapsed_time_ms_ = input_frame->elapsed_time_ms_; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 53 | // TODO(yujo): can we optimize muted frames? |
| 54 | std::copy(input_frame->data(), |
| 55 | input_frame->data() + |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 56 | input_frame->num_channels_ * input_frame->samples_per_channel_, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 57 | audio_frame_for_mixing->mutable_data()); |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 58 | if (use_limiter) { |
| 59 | AudioFrameOperations::ApplyHalfGain(audio_frame_for_mixing); |
| 60 | RTC_DCHECK(limiter); |
| 61 | limiter->ProcessStream(audio_frame_for_mixing); |
| 62 | AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Lower-level helper function called from Combine(...) when there |
| 67 | // are several input frames. |
| 68 | // |
| 69 | // TODO(aleloi): change interface to ArrayView<int16_t> output_frame |
| 70 | // once we have gotten rid of the APM limiter. |
| 71 | // |
| 72 | // Only the 'data' field of output_frame should be modified. The |
| 73 | // rest are used for potentially sending the output to the APM |
| 74 | // limiter. |
| 75 | void CombineMultipleFrames( |
| 76 | const std::vector<rtc::ArrayView<const int16_t>>& input_frames, |
| 77 | bool use_limiter, |
| 78 | AudioProcessing* limiter, |
| 79 | AudioFrame* audio_frame_for_mixing) { |
| 80 | RTC_DCHECK(!input_frames.empty()); |
| 81 | RTC_DCHECK(audio_frame_for_mixing); |
| 82 | |
| 83 | const size_t frame_length = input_frames.front().size(); |
| 84 | for (const auto& frame : input_frames) { |
| 85 | RTC_DCHECK_EQ(frame_length, frame.size()); |
| 86 | } |
| 87 | |
| 88 | // Algorithm: int16 frames are added to a sufficiently large |
| 89 | // statically allocated int32 buffer. For > 2 participants this is |
| 90 | // more efficient than addition in place in the int16 audio |
| 91 | // frame. The audio quality loss due to halving the samples is |
| 92 | // smaller than 16-bit addition in place. |
| 93 | RTC_DCHECK_GE(kMaximalFrameSize, frame_length); |
| 94 | std::array<int32_t, kMaximalFrameSize> add_buffer; |
| 95 | |
| 96 | add_buffer.fill(0); |
| 97 | |
| 98 | for (const auto& frame : input_frames) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 99 | // TODO(yujo): skip this for muted frames. |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 100 | std::transform(frame.begin(), frame.end(), add_buffer.begin(), |
| 101 | add_buffer.begin(), std::plus<int32_t>()); |
| 102 | } |
| 103 | |
| 104 | if (use_limiter) { |
| 105 | // Halve all samples to avoid saturation before limiting. |
| 106 | std::transform(add_buffer.begin(), add_buffer.begin() + frame_length, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 107 | audio_frame_for_mixing->mutable_data(), [](int32_t a) { |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 108 | return rtc::saturated_cast<int16_t>(a / 2); |
| 109 | }); |
| 110 | |
| 111 | // Smoothly limit the audio. |
| 112 | RTC_DCHECK(limiter); |
| 113 | const int error = limiter->ProcessStream(audio_frame_for_mixing); |
| 114 | if (error != limiter->kNoError) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 115 | RTC_LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error; |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 116 | RTC_NOTREACHED(); |
| 117 | } |
| 118 | |
| 119 | // And now we can safely restore the level. This procedure results in |
| 120 | // some loss of resolution, deemed acceptable. |
| 121 | // |
| 122 | // It's possible to apply the gain in the AGC (with a target level of 0 dbFS |
| 123 | // and compression gain of 6 dB). However, in the transition frame when this |
| 124 | // is enabled (moving from one to two audio sources) it has the potential to |
| 125 | // create discontinuities in the mixed frame. |
| 126 | // |
| 127 | // Instead we double the frame (with addition since left-shifting a |
| 128 | // negative value is undefined). |
| 129 | AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing); |
| 130 | } else { |
| 131 | std::transform(add_buffer.begin(), add_buffer.begin() + frame_length, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 132 | audio_frame_for_mixing->mutable_data(), |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 133 | [](int32_t a) { return rtc::saturated_cast<int16_t>(a); }); |
| 134 | } |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | std::unique_ptr<AudioProcessing> CreateLimiter() { |
| 138 | Config config; |
| 139 | config.Set<ExperimentalAgc>(new ExperimentalAgc(false)); |
aleloi | 0f23fa8 | 2017-05-11 00:25:45 -0700 | [diff] [blame] | 140 | |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 141 | std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config)); |
| 142 | RTC_DCHECK(limiter); |
| 143 | |
aleloi | 0f23fa8 | 2017-05-11 00:25:45 -0700 | [diff] [blame] | 144 | webrtc::AudioProcessing::Config apm_config; |
| 145 | apm_config.residual_echo_detector.enabled = false; |
| 146 | limiter->ApplyConfig(apm_config); |
| 147 | |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 148 | const auto check_no_error = [](int x) { |
| 149 | RTC_DCHECK_EQ(x, AudioProcessing::kNoError); |
| 150 | }; |
| 151 | auto* const gain_control = limiter->gain_control(); |
| 152 | check_no_error(gain_control->set_mode(GainControl::kFixedDigital)); |
| 153 | |
| 154 | // We smoothly limit the mixed frame to -7 dbFS. -6 would correspond to the |
| 155 | // divide-by-2 but -7 is used instead to give a bit of headroom since the |
| 156 | // AGC is not a hard limiter. |
| 157 | check_no_error(gain_control->set_target_level_dbfs(7)); |
| 158 | |
| 159 | check_no_error(gain_control->set_compression_gain_db(0)); |
| 160 | check_no_error(gain_control->enable_limiter(true)); |
| 161 | check_no_error(gain_control->Enable(true)); |
| 162 | return limiter; |
| 163 | } |
| 164 | } // namespace |
| 165 | |
| 166 | FrameCombiner::FrameCombiner(bool use_apm_limiter) |
| 167 | : use_apm_limiter_(use_apm_limiter), |
| 168 | limiter_(use_apm_limiter ? CreateLimiter() : nullptr) {} |
| 169 | |
| 170 | FrameCombiner::~FrameCombiner() = default; |
| 171 | |
| 172 | void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list, |
| 173 | size_t number_of_channels, |
| 174 | int sample_rate, |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 175 | size_t number_of_streams, |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 176 | AudioFrame* audio_frame_for_mixing) const { |
| 177 | RTC_DCHECK(audio_frame_for_mixing); |
| 178 | const size_t samples_per_channel = static_cast<size_t>( |
| 179 | (sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000); |
| 180 | |
| 181 | for (const auto* frame : mix_list) { |
| 182 | RTC_DCHECK_EQ(samples_per_channel, frame->samples_per_channel_); |
| 183 | RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_); |
| 184 | } |
| 185 | |
| 186 | // Frames could be both stereo and mono. |
| 187 | for (auto* frame : mix_list) { |
| 188 | RemixFrame(number_of_channels, frame); |
| 189 | } |
| 190 | |
| 191 | // TODO(aleloi): Issue bugs.webrtc.org/3390. |
| 192 | // Audio frame timestamp. The 'timestamp_' field is set to dummy |
| 193 | // value '0', because it is only supported in the one channel case and |
| 194 | // is then updated in the helper functions. |
| 195 | audio_frame_for_mixing->UpdateFrame( |
solenberg | c7b4a45 | 2017-09-28 07:37:11 -0700 | [diff] [blame] | 196 | 0, nullptr, samples_per_channel, sample_rate, AudioFrame::kUndefined, |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 197 | AudioFrame::kVadUnknown, number_of_channels); |
| 198 | |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 199 | const bool use_limiter_this_round = use_apm_limiter_ && number_of_streams > 1; |
| 200 | |
aleloi | 61a2b1b | 2017-02-23 01:16:14 -0800 | [diff] [blame] | 201 | if (mix_list.empty()) { |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 202 | CombineZeroFrames(use_limiter_this_round, limiter_.get(), |
| 203 | audio_frame_for_mixing); |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 204 | } else if (mix_list.size() == 1) { |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 205 | CombineOneFrame(mix_list.front(), use_limiter_this_round, limiter_.get(), |
| 206 | audio_frame_for_mixing); |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 207 | } else { |
| 208 | std::vector<rtc::ArrayView<const int16_t>> input_frames; |
| 209 | for (size_t i = 0; i < mix_list.size(); ++i) { |
| 210 | input_frames.push_back(rtc::ArrayView<const int16_t>( |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 211 | mix_list[i]->data(), samples_per_channel * number_of_channels)); |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 212 | } |
aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 213 | CombineMultipleFrames(input_frames, use_limiter_this_round, limiter_.get(), |
| 214 | audio_frame_for_mixing); |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 217 | |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 218 | } // namespace webrtc |