Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1452)

Side by Side Diff: webrtc/modules/audio_mixer/frame_combiner.cc

Issue 2776113002: Send data from mixer to APM limiter more often. (Closed)
Patch Set: Minor changes in response to hlundin@'s comments. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_mixer/frame_combiner.h" 11 #include "webrtc/modules/audio_mixer/frame_combiner.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <array> 14 #include <array>
15 #include <functional> 15 #include <functional>
16 #include <memory> 16 #include <memory>
17 17
18 #include "webrtc/audio/utility/audio_frame_operations.h" 18 #include "webrtc/audio/utility/audio_frame_operations.h"
19 #include "webrtc/base/array_view.h"
20 #include "webrtc/base/checks.h"
19 #include "webrtc/base/logging.h" 21 #include "webrtc/base/logging.h"
20 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h" 22 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h"
21 #include "webrtc/modules/audio_mixer/audio_mixer_impl.h" 23 #include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
22 24
23 namespace webrtc { 25 namespace webrtc {
24 namespace { 26 namespace {
25 27
26 // Stereo, 48 kHz, 10 ms. 28 // Stereo, 48 kHz, 10 ms.
27 constexpr int kMaximalFrameSize = 2 * 48 * 10; 29 constexpr int kMaximalFrameSize = 2 * 48 * 10;
28 30
29 void CombineZeroFrames(AudioFrame* audio_frame_for_mixing) { 31 void CombineZeroFrames(bool use_limiter,
32 AudioProcessing* limiter,
33 AudioFrame* audio_frame_for_mixing) {
30 audio_frame_for_mixing->elapsed_time_ms_ = -1; 34 audio_frame_for_mixing->elapsed_time_ms_ = -1;
31 AudioFrameOperations::Mute(audio_frame_for_mixing); 35 AudioFrameOperations::Mute(audio_frame_for_mixing);
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 }
32 } 45 }
33 46
34 void CombineOneFrame(const AudioFrame* input_frame, 47 void CombineOneFrame(const AudioFrame* input_frame,
48 bool use_limiter,
49 AudioProcessing* limiter,
35 AudioFrame* audio_frame_for_mixing) { 50 AudioFrame* audio_frame_for_mixing) {
36 audio_frame_for_mixing->timestamp_ = input_frame->timestamp_; 51 audio_frame_for_mixing->timestamp_ = input_frame->timestamp_;
37 audio_frame_for_mixing->elapsed_time_ms_ = input_frame->elapsed_time_ms_; 52 audio_frame_for_mixing->elapsed_time_ms_ = input_frame->elapsed_time_ms_;
38 std::copy(input_frame->data_, 53 std::copy(input_frame->data_,
39 input_frame->data_ + 54 input_frame->data_ +
40 input_frame->num_channels_ * input_frame->samples_per_channel_, 55 input_frame->num_channels_ * input_frame->samples_per_channel_,
41 audio_frame_for_mixing->data_); 56 audio_frame_for_mixing->data_);
57 if (use_limiter) {
58 AudioFrameOperations::ApplyHalfGain(audio_frame_for_mixing);
59 RTC_DCHECK(limiter);
60 limiter->ProcessStream(audio_frame_for_mixing);
61 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
62 }
63 }
64
65 // Lower-level helper function called from Combine(...) when there
66 // are several input frames.
67 //
68 // TODO(aleloi): change interface to ArrayView<int16_t> output_frame
69 // once we have gotten rid of the APM limiter.
70 //
71 // Only the 'data' field of output_frame should be modified. The
72 // rest are used for potentially sending the output to the APM
73 // limiter.
74 void CombineMultipleFrames(
75 const std::vector<rtc::ArrayView<const int16_t>>& input_frames,
76 bool use_limiter,
77 AudioProcessing* limiter,
78 AudioFrame* audio_frame_for_mixing) {
79 RTC_DCHECK(!input_frames.empty());
80 RTC_DCHECK(audio_frame_for_mixing);
81
82 const size_t frame_length = input_frames.front().size();
83 for (const auto& frame : input_frames) {
84 RTC_DCHECK_EQ(frame_length, frame.size());
85 }
86
87 // Algorithm: int16 frames are added to a sufficiently large
88 // statically allocated int32 buffer. For > 2 participants this is
89 // more efficient than addition in place in the int16 audio
90 // frame. The audio quality loss due to halving the samples is
91 // smaller than 16-bit addition in place.
92 RTC_DCHECK_GE(kMaximalFrameSize, frame_length);
93 std::array<int32_t, kMaximalFrameSize> add_buffer;
94
95 add_buffer.fill(0);
96
97 for (const auto& frame : input_frames) {
98 std::transform(frame.begin(), frame.end(), add_buffer.begin(),
99 add_buffer.begin(), std::plus<int32_t>());
100 }
101
102 if (use_limiter) {
103 // Halve all samples to avoid saturation before limiting.
104 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
105 audio_frame_for_mixing->data_, [](int32_t a) {
106 return rtc::saturated_cast<int16_t>(a / 2);
107 });
108
109 // Smoothly limit the audio.
110 RTC_DCHECK(limiter);
111 const int error = limiter->ProcessStream(audio_frame_for_mixing);
112 if (error != limiter->kNoError) {
113 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error;
114 RTC_NOTREACHED();
115 }
116
117 // And now we can safely restore the level. This procedure results in
118 // some loss of resolution, deemed acceptable.
119 //
120 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS
121 // and compression gain of 6 dB). However, in the transition frame when this
122 // is enabled (moving from one to two audio sources) it has the potential to
123 // create discontinuities in the mixed frame.
124 //
125 // Instead we double the frame (with addition since left-shifting a
126 // negative value is undefined).
127 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
128 } else {
129 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
130 audio_frame_for_mixing->data_,
131 [](int32_t a) { return rtc::saturated_cast<int16_t>(a); });
132 }
42 } 133 }
43 134
44 std::unique_ptr<AudioProcessing> CreateLimiter() { 135 std::unique_ptr<AudioProcessing> CreateLimiter() {
45 Config config; 136 Config config;
46 config.Set<ExperimentalAgc>(new ExperimentalAgc(false)); 137 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
47 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config)); 138 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config));
48 RTC_DCHECK(limiter); 139 RTC_DCHECK(limiter);
49 140
50 const auto check_no_error = [](int x) { 141 const auto check_no_error = [](int x) {
51 RTC_DCHECK_EQ(x, AudioProcessing::kNoError); 142 RTC_DCHECK_EQ(x, AudioProcessing::kNoError);
(...skipping 15 matching lines...) Expand all
67 158
68 FrameCombiner::FrameCombiner(bool use_apm_limiter) 159 FrameCombiner::FrameCombiner(bool use_apm_limiter)
69 : use_apm_limiter_(use_apm_limiter), 160 : use_apm_limiter_(use_apm_limiter),
70 limiter_(use_apm_limiter ? CreateLimiter() : nullptr) {} 161 limiter_(use_apm_limiter ? CreateLimiter() : nullptr) {}
71 162
72 FrameCombiner::~FrameCombiner() = default; 163 FrameCombiner::~FrameCombiner() = default;
73 164
74 void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list, 165 void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list,
75 size_t number_of_channels, 166 size_t number_of_channels,
76 int sample_rate, 167 int sample_rate,
168 size_t number_of_streams,
77 AudioFrame* audio_frame_for_mixing) const { 169 AudioFrame* audio_frame_for_mixing) const {
78 RTC_DCHECK(audio_frame_for_mixing); 170 RTC_DCHECK(audio_frame_for_mixing);
79 const size_t samples_per_channel = static_cast<size_t>( 171 const size_t samples_per_channel = static_cast<size_t>(
80 (sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000); 172 (sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000);
81 173
82 for (const auto* frame : mix_list) { 174 for (const auto* frame : mix_list) {
83 RTC_DCHECK_EQ(samples_per_channel, frame->samples_per_channel_); 175 RTC_DCHECK_EQ(samples_per_channel, frame->samples_per_channel_);
84 RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_); 176 RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_);
85 } 177 }
86 178
87 // Frames could be both stereo and mono. 179 // Frames could be both stereo and mono.
88 for (auto* frame : mix_list) { 180 for (auto* frame : mix_list) {
89 RemixFrame(number_of_channels, frame); 181 RemixFrame(number_of_channels, frame);
90 } 182 }
91 183
92 // TODO(aleloi): Issue bugs.webrtc.org/3390. 184 // TODO(aleloi): Issue bugs.webrtc.org/3390.
93 // Audio frame timestamp. The 'timestamp_' field is set to dummy 185 // Audio frame timestamp. The 'timestamp_' field is set to dummy
94 // value '0', because it is only supported in the one channel case and 186 // value '0', because it is only supported in the one channel case and
95 // is then updated in the helper functions. 187 // is then updated in the helper functions.
96 audio_frame_for_mixing->UpdateFrame( 188 audio_frame_for_mixing->UpdateFrame(
97 -1, 0, nullptr, samples_per_channel, sample_rate, AudioFrame::kUndefined, 189 -1, 0, nullptr, samples_per_channel, sample_rate, AudioFrame::kUndefined,
98 AudioFrame::kVadUnknown, number_of_channels); 190 AudioFrame::kVadUnknown, number_of_channels);
99 191
192 const bool use_limiter_this_round = use_apm_limiter_ && number_of_streams > 1;
193
100 if (mix_list.empty()) { 194 if (mix_list.empty()) {
101 CombineZeroFrames(audio_frame_for_mixing); 195 CombineZeroFrames(use_limiter_this_round, limiter_.get(),
196 audio_frame_for_mixing);
102 } else if (mix_list.size() == 1) { 197 } else if (mix_list.size() == 1) {
103 CombineOneFrame(mix_list.front(), audio_frame_for_mixing); 198 CombineOneFrame(mix_list.front(), use_limiter_this_round, limiter_.get(),
199 audio_frame_for_mixing);
104 } else { 200 } else {
105 std::vector<rtc::ArrayView<const int16_t>> input_frames; 201 std::vector<rtc::ArrayView<const int16_t>> input_frames;
106 for (size_t i = 0; i < mix_list.size(); ++i) { 202 for (size_t i = 0; i < mix_list.size(); ++i) {
107 input_frames.push_back(rtc::ArrayView<const int16_t>( 203 input_frames.push_back(rtc::ArrayView<const int16_t>(
108 mix_list[i]->data_, samples_per_channel * number_of_channels)); 204 mix_list[i]->data_, samples_per_channel * number_of_channels));
109 } 205 }
110 CombineMultipleFrames(input_frames, audio_frame_for_mixing); 206 CombineMultipleFrames(input_frames, use_limiter_this_round, limiter_.get(),
111 } 207 audio_frame_for_mixing);
112 }
113
114 void FrameCombiner::CombineMultipleFrames(
115 const std::vector<rtc::ArrayView<const int16_t>>& input_frames,
116 AudioFrame* audio_frame_for_mixing) const {
117 RTC_DCHECK(!input_frames.empty());
118 RTC_DCHECK(audio_frame_for_mixing);
119
120 const size_t frame_length = input_frames.front().size();
121 for (const auto& frame : input_frames) {
122 RTC_DCHECK_EQ(frame_length, frame.size());
123 }
124
125 // Algorithm: int16 frames are added to a sufficiently large
126 // statically allocated int32 buffer. For > 2 participants this is
127 // more efficient than addition in place in the int16 audio
128 // frame. The audio quality loss due to halving the samples is
129 // smaller than 16-bit addition in place.
130 RTC_DCHECK_GE(kMaximalFrameSize, frame_length);
131 std::array<int32_t, kMaximalFrameSize> add_buffer;
132
133 add_buffer.fill(0);
134
135 for (const auto& frame : input_frames) {
136 std::transform(frame.begin(), frame.end(), add_buffer.begin(),
137 add_buffer.begin(), std::plus<int32_t>());
138 }
139
140 if (use_apm_limiter_) {
141 // Halve all samples to avoid saturation before limiting.
142 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
143 audio_frame_for_mixing->data_, [](int32_t a) {
144 return rtc::saturated_cast<int16_t>(a / 2);
145 });
146
147 // Smoothly limit the audio.
148 RTC_DCHECK(limiter_);
149 const int error = limiter_->ProcessStream(audio_frame_for_mixing);
150 if (error != limiter_->kNoError) {
151 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error;
152 RTC_NOTREACHED();
153 }
154
155 // And now we can safely restore the level. This procedure results in
156 // some loss of resolution, deemed acceptable.
157 //
158 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS
159 // and compression gain of 6 dB). However, in the transition frame when this
160 // is enabled (moving from one to two audio sources) it has the potential to
161 // create discontinuities in the mixed frame.
162 //
163 // Instead we double the frame (with addition since left-shifting a
164 // negative value is undefined).
165 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
166 } else {
167 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
168 audio_frame_for_mixing->data_,
169 [](int32_t a) { return rtc::saturated_cast<int16_t>(a); });
170 } 208 }
171 } 209 }
172 } // namespace webrtc 210 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_mixer/frame_combiner.h ('k') | webrtc/modules/audio_mixer/frame_combiner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698