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

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

Issue 2692333002: Optionally disable APM limiter in AudioMixer. (Closed)
Patch Set: Deprecated old factory method. Created 3 years, 10 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
(Empty)
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
11 #include "webrtc/modules/audio_mixer/frame_combiner.h"
12
13 #include <algorithm>
14 #include <array>
15 #include <functional>
16 #include <memory>
17
18 #include "webrtc/base/logging.h"
ivoc 2017/02/15 16:58:47 Order of includes
aleloi 2017/02/16 14:01:36 Done.
19 #include "webrtc/audio/utility/audio_frame_operations.h"
20 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h"
21 #include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
22
23 namespace webrtc {
24 namespace {
25
26 void CombineZeroFrames(AudioFrame* audio_frame_for_mixing) {
27 audio_frame_for_mixing->elapsed_time_ms_ = -1;
28 AudioFrameOperations::Mute(audio_frame_for_mixing);
29 }
30
31 void CombineOneFrame(const AudioFrame* input_frame,
32 AudioFrame* audio_frame_for_mixing) {
33 audio_frame_for_mixing->timestamp_ = input_frame->timestamp_;
34 audio_frame_for_mixing->elapsed_time_ms_ = input_frame->elapsed_time_ms_;
35 std::copy(input_frame->data_,
36 input_frame->data_ +
37 input_frame->num_channels_ * input_frame->samples_per_channel_,
38 audio_frame_for_mixing->data_);
39 }
40
41 std::unique_ptr<AudioProcessing> CreateLimiter() {
42 Config config;
43 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
44 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config));
45 if (!limiter.get()) {
46 return nullptr;
47 }
48
49 if (limiter->gain_control()->set_mode(GainControl::kFixedDigital) !=
50 limiter->kNoError) {
51 return nullptr;
52 }
53
54 // We smoothly limit the mixed frame to -7 dbFS. -6 would correspond to the
55 // divide-by-2 but -7 is used instead to give a bit of headroom since the
56 // AGC is not a hard limiter.
57 if (limiter->gain_control()->set_target_level_dbfs(7) != limiter->kNoError) {
58 return nullptr;
59 }
60
61 if (limiter->gain_control()->set_compression_gain_db(0) !=
62 limiter->kNoError) {
63 return nullptr;
64 }
65
66 if (limiter->gain_control()->enable_limiter(true) != limiter->kNoError) {
67 return nullptr;
68 }
69
70 if (limiter->gain_control()->Enable(true) != limiter->kNoError) {
71 return nullptr;
72 }
73 return limiter;
74 }
75 } // namespace
76
77 FrameCombiner::FrameCombiner(bool use_apm_limiter)
78 : use_apm_limiter_(use_apm_limiter),
79 limiter_(use_apm_limiter ? CreateLimiter() : nullptr) {}
80
81 FrameCombiner::~FrameCombiner() = default;
82
83 void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list,
84 size_t number_of_channels,
85 int sample_rate,
86 AudioFrame* audio_frame_for_mixing) {
87 const size_t kSamplesPerChannel = static_cast<size_t>(
88 (sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000);
89
90 for (const auto& frame : mix_list) {
91 RTC_DCHECK_EQ(kSamplesPerChannel, frame->samples_per_channel_);
92 RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_);
93 }
94
95 // Frames could be both stereo and mono.
96 for (const auto& frame : mix_list) {
ivoc 2017/02/15 16:58:47 Can be merged with the previous loop.
aleloi 2017/02/16 14:01:36 Yes, but I'd like to keep the modification from th
ivoc 2017/02/21 09:59:16 Acknowledged.
97 RemixFrame(number_of_channels, frame);
98 }
99
100 // TODO(aleloi): Issue 3390.
ivoc 2017/02/15 16:58:47 A url to the issue would be helpful here.
aleloi 2017/02/16 14:01:35 Done.
101 // Audio frame timestamp . The 'timestamp_' field is set to dummy
ivoc 2017/02/15 16:58:46 The space between "timestamp" and the "." bothers
aleloi 2017/02/16 14:01:36 Done.
102 // value '0', because it is only supported in one channel case and
ivoc 2017/02/15 16:58:46 the one channel case
aleloi 2017/02/16 14:01:36 Done.
103 // is then updated in the helper functions.
104 audio_frame_for_mixing->UpdateFrame(
105 -1, 0, NULL, kSamplesPerChannel, sample_rate, AudioFrame::kNormalSpeech,
106 AudioFrame::kVadPassive, number_of_channels);
107
108 if (mix_list.size() == 0) {
109 CombineZeroFrames(audio_frame_for_mixing);
110 } else if (mix_list.size() == 1) {
111 CombineOneFrame(mix_list.front(), audio_frame_for_mixing);
112 } else {
113 std::vector<rtc::ArrayView<const int16_t>> input_frames;
114 for (size_t i = 0; i < mix_list.size(); ++i) {
115 input_frames.push_back(rtc::ArrayView<const int16_t>(
116 mix_list[i]->data_, kSamplesPerChannel * number_of_channels));
117 }
118 CombineMultipleFrames(input_frames, audio_frame_for_mixing);
119 }
120 }
121
122 void FrameCombiner::CombineMultipleFrames(
123 const std::vector<rtc::ArrayView<const int16_t>>& input_frames,
124 AudioFrame* audio_frame_for_mixing) {
125 RTC_DCHECK(!input_frames.empty());
126
127 const size_t frame_length = input_frames.front().size();
128 for (const auto& frame : input_frames) {
129 RTC_DCHECK_EQ(frame_length, frame.size());
130 }
131
132 // Maximal frame size: stereo, 48 kHz, 10 ms.
133 RTC_DCHECK_GE(2 * 48 * 10, frame_length);
134 std::array<int32_t, 2 * 48 * 10> add_buffer;
ivoc 2017/02/15 16:58:47 I suggest declaring these constants as constexprs
aleloi 2017/02/16 14:01:36 Done. Reasons for having an intermediate 32-bit ar
ivoc 2017/02/21 09:59:16 Thanks for the profiling, that makes sense.
135
136 add_buffer.fill(0);
ivoc 2017/02/15 16:58:47 It's more efficient to copy the first frame into t
aleloi 2017/02/16 14:01:35 I profiled this and some other changes. On x86-64
ivoc 2017/02/21 09:59:16 Great! Thanks for looking into this. I agree that
137
138 for (const auto& frame : input_frames) {
139 std::transform(frame.begin(), frame.end(), add_buffer.begin(),
140 add_buffer.begin(), std::plus<int32_t>());
141 }
142
143 if (use_apm_limiter_) {
144 // Half all samples to avoid saturation before limiting.
ivoc 2017/02/15 16:58:47 Halve
aleloi 2017/02/16 14:01:36 Done.
145 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
ivoc 2017/02/15 16:58:47 For maximum efficiency it would be more efficient
aleloi 2017/02/16 14:01:36 Actually no performance improvement. See comment a
ivoc 2017/02/21 09:59:16 Acknowledged.
146 audio_frame_for_mixing->data_, [](int32_t a) {
147 return rtc::saturated_cast<int16_t>(a / 2);
148 });
149
150 // Smoothly limit the audio.
151 RTC_DCHECK(limiter_);
152 const int error = limiter_->ProcessStream(audio_frame_for_mixing);
153 if (error != limiter_->kNoError) {
154 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error;
155 RTC_NOTREACHED();
156 }
157
158 // And now we can safely restore the level. This procedure results in
159 // some loss of resolution, deemed acceptable.
160 //
161 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS
162 // and compression gain of 6 dB). However, in the transition frame when this
163 // is enabled (moving from one to two audio sources) it has the potential to
164 // create discontinuities in the mixed frame.
165 //
166 // Instead we double the frame (with addition since left-shifting a
167 // negative value is undefined).
168 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
169 } else {
170 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
ivoc 2017/02/15 16:58:46 See remark about combining with adding last frame.
aleloi 2017/02/16 14:01:36 Here as well.
171 audio_frame_for_mixing->data_,
172 [](int32_t a) { return rtc::saturated_cast<int16_t>(a); });
173 }
174 }
175 } // namespace webrtc
OLDNEW
« webrtc/modules/audio_mixer/frame_combiner.h ('K') | « webrtc/modules/audio_mixer/frame_combiner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698