blob: 736f60c0489d87883974f1ebe5a5a2981f4c4106 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 "webrtc/modules/audio_coding/neteq4/delay_manager.h"
12
13#include <assert.h>
14#include <math.h>
15
16#include <algorithm> // max, min
17
18#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
19#include "webrtc/modules/audio_coding/neteq4/delay_peak_detector.h"
20#include "webrtc/system_wrappers/interface/logging.h"
21
22namespace webrtc {
23
24DelayManager::DelayManager(int max_packets_in_buffer,
25 DelayPeakDetector* peak_detector)
26 : first_packet_received_(false),
27 max_packets_in_buffer_(max_packets_in_buffer),
28 iat_vector_(kMaxIat + 1, 0),
29 iat_factor_(0),
30 packet_iat_count_ms_(0),
31 base_target_level_(4), // In Q0 domain.
32 target_level_(base_target_level_ << 8), // In Q8 domain.
33 packet_len_ms_(0),
34 streaming_mode_(false),
35 last_seq_no_(0),
36 last_timestamp_(0),
37 extra_delay_ms_(0),
38 iat_cumulative_sum_(0),
39 max_iat_cumulative_sum_(0),
40 max_timer_ms_(0),
41 peak_detector_(*peak_detector),
42 last_pack_cng_or_dtmf_(1) {
43 assert(peak_detector); // Should never be NULL.
44 Reset();
45}
46
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000047DelayManager::~DelayManager() {}
48
49const DelayManager::IATVector& DelayManager::iat_vector() const {
50 return iat_vector_;
51}
52
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053// Set the histogram vector to an exponentially decaying distribution
54// iat_vector_[i] = 0.5^(i+1), i = 0, 1, 2, ...
55// iat_vector_ is in Q30.
56void DelayManager::ResetHistogram() {
57 // Set temp_prob to (slightly more than) 1 in Q14. This ensures that the sum
58 // of iat_vector_ is 1.
59 uint16_t temp_prob = 0x4002; // 16384 + 2 = 100000000000010 binary.
60 IATVector::iterator it = iat_vector_.begin();
61 for (; it < iat_vector_.end(); it++) {
62 temp_prob >>= 1;
63 (*it) = temp_prob << 16;
64 }
65 base_target_level_ = 4;
66 target_level_ = base_target_level_ << 8;
67}
68
69int DelayManager::Update(uint16_t sequence_number,
70 uint32_t timestamp,
71 int sample_rate_hz) {
72 if (sample_rate_hz <= 0) {
73 return -1;
74 }
75
76 if (!first_packet_received_) {
77 // Prepare for next packet arrival.
78 packet_iat_count_ms_ = 0;
79 last_seq_no_ = sequence_number;
80 last_timestamp_ = timestamp;
81 first_packet_received_ = true;
82 return 0;
83 }
84
85 // Try calculating packet length from current and previous timestamps.
86 // TODO(hlundin): Take care of wrap-around. Not done yet due to legacy
87 // bit-exactness.
88 int packet_len_ms;
89 if ((timestamp <= last_timestamp_) || (sequence_number <= last_seq_no_)) {
90 // Wrong timestamp or sequence order; use stored value.
91 packet_len_ms = packet_len_ms_;
92 } else {
93 // Calculate timestamps per packet and derive packet length in ms.
94 int packet_len_samp =
95 static_cast<uint32_t>(timestamp - last_timestamp_) /
96 static_cast<uint16_t>(sequence_number - last_seq_no_);
97 packet_len_ms = (1000 * packet_len_samp) / sample_rate_hz;
98 }
99
100 if (packet_len_ms > 0) {
101 // Cannot update statistics unless |packet_len_ms| is valid.
102 // Calculate inter-arrival time (IAT) in integer "packet times"
103 // (rounding down). This is the value used as index to the histogram
104 // vector |iat_vector_|.
105 int iat_packets = packet_iat_count_ms_ / packet_len_ms;
106
107 if (streaming_mode_) {
108 UpdateCumulativeSums(packet_len_ms, sequence_number);
109 }
110
111 // Check for discontinuous packet sequence and re-ordering.
112 if (sequence_number > last_seq_no_ + 1) {
113 // TODO(hlundin): Take care of wrap-around. Not done yet due to legacy
114 // bit-exactness.
115 // Compensate for gap in the sequence numbers. Reduce IAT with the
116 // expected extra time due to lost packets, but ensure that the IAT is
117 // not negative.
118 iat_packets -= sequence_number - last_seq_no_ - 1;
119 iat_packets = std::max(iat_packets, 0);
120 } else if (sequence_number < last_seq_no_) {
121 // TODO(hlundin): Take care of wrap-around.
122 // Compensate for re-ordering.
123 iat_packets += last_seq_no_ + 1 - sequence_number;
124 }
125
126 // Saturate IAT at maximum value.
127 const int max_iat = kMaxIat;
128 iat_packets = std::min(iat_packets, max_iat);
129 UpdateHistogram(iat_packets);
130 // Calculate new |target_level_| based on updated statistics.
131 target_level_ = CalculateTargetLevel(iat_packets);
132 if (streaming_mode_) {
133 target_level_ = std::max(target_level_, max_iat_cumulative_sum_);
134 }
135
136 LimitTargetLevel();
137 } // End if (packet_len_ms > 0).
138
139 // Prepare for next packet arrival.
140 packet_iat_count_ms_ = 0;
141 last_seq_no_ = sequence_number;
142 last_timestamp_ = timestamp;
143 return 0;
144}
145
146void DelayManager::UpdateCumulativeSums(int packet_len_ms,
147 uint16_t sequence_number) {
148 // Calculate IAT in Q8, including fractions of a packet (i.e., more
149 // accurate than |iat_packets|.
150 int iat_packets_q8 = (packet_iat_count_ms_ << 8) / packet_len_ms;
151 // Calculate cumulative sum IAT with sequence number compensation. The sum
152 // is zero if there is no clock-drift.
153 iat_cumulative_sum_ += (iat_packets_q8 -
154 (static_cast<int>(sequence_number - last_seq_no_) << 8));
155 // Subtract drift term.
156 iat_cumulative_sum_ -= kCumulativeSumDrift;
157 // Ensure not negative.
158 iat_cumulative_sum_ = std::max(iat_cumulative_sum_, 0);
159 if (iat_cumulative_sum_ > max_iat_cumulative_sum_) {
160 // Found a new maximum.
161 max_iat_cumulative_sum_ = iat_cumulative_sum_;
162 max_timer_ms_ = 0;
163 }
164 if (max_timer_ms_ > kMaxStreamingPeakPeriodMs) {
165 // Too long since the last maximum was observed; decrease max value.
166 max_iat_cumulative_sum_ -= kCumulativeSumDrift;
167 }
168}
169
170// Each element in the vector is first multiplied by the forgetting factor
171// |iat_factor_|. Then the vector element indicated by |iat_packets| is then
172// increased (additive) by 1 - |iat_factor_|. This way, the probability of
173// |iat_packets| is slightly increased, while the sum of the histogram remains
174// constant (=1).
175// Due to inaccuracies in the fixed-point arithmetic, the histogram may no
176// longer sum up to 1 (in Q30) after the update. To correct this, a correction
177// term is added or subtracted from the first element (or elements) of the
178// vector.
179// The forgetting factor |iat_factor_| is also updated. When the DelayManager
180// is reset, the factor is set to 0 to facilitate rapid convergence in the
181// beginning. With each update of the histogram, the factor is increased towards
182// the steady-state value |kIatFactor_|.
183void DelayManager::UpdateHistogram(size_t iat_packets) {
184 assert(iat_packets < iat_vector_.size());
185 int vector_sum = 0; // Sum up the vector elements as they are processed.
186 // Multiply each element in |iat_vector_| with |iat_factor_|.
187 for (IATVector::iterator it = iat_vector_.begin();
188 it != iat_vector_.end(); ++it) {
189 *it = (static_cast<int64_t>(*it) * iat_factor_) >> 15;
190 vector_sum += *it;
191 }
192
193 // Increase the probability for the currently observed inter-arrival time
194 // by 1 - |iat_factor_|. The factor is in Q15, |iat_vector_| in Q30.
195 // Thus, left-shift 15 steps to obtain result in Q30.
196 iat_vector_[iat_packets] += (32768 - iat_factor_) << 15;
197 vector_sum += (32768 - iat_factor_) << 15; // Add to vector sum.
198
199 // |iat_vector_| should sum up to 1 (in Q30), but it may not due to
200 // fixed-point rounding errors.
201 vector_sum -= 1 << 30; // Should be zero. Compensate if not.
202 if (vector_sum != 0) {
203 // Modify a few values early in |iat_vector_|.
204 int flip_sign = vector_sum > 0 ? -1 : 1;
205 IATVector::iterator it = iat_vector_.begin();
206 while (it != iat_vector_.end() && abs(vector_sum) > 0) {
207 // Add/subtract 1/16 of the element, but not more than |vector_sum|.
208 int correction = flip_sign * std::min(abs(vector_sum), (*it) >> 4);
209 *it += correction;
210 vector_sum += correction;
211 ++it;
212 }
213 }
214 assert(vector_sum == 0); // Verify that the above is correct.
215
216 // Update |iat_factor_| (changes only during the first seconds after a reset).
217 // The factor converges to |kIatFactor_|.
218 iat_factor_ += (kIatFactor_ - iat_factor_ + 3) >> 2;
219}
220
221// Enforces upper limit for |target_level_|. The limit is chosen to be
222// 75% of |max_packets_in_buffer_|, to leave some headroom for natural
223// fluctuations around the target. If an extra delay is requested, the
224// cap is lowered even further. Note that in practice, this does not have
225// any impact, since the target level is far below the buffer capacity in
226// all reasonable cases.
227// TODO(hlundin): Move this check to the buffer logistics class.
228void DelayManager::LimitTargetLevel() {
229 int max_buffer_len = max_packets_in_buffer_;
230 if (extra_delay_ms_ > 0 && packet_len_ms_ > 0) {
231 max_buffer_len -= extra_delay_ms_ / packet_len_ms_;
232 max_buffer_len = std::max(max_buffer_len, 1); // Sanity check.
233 }
234 max_buffer_len = (3 * (max_buffer_len << 8)) / 4; // Shift to Q8, then 75%.
235 target_level_ = std::min(target_level_, max_buffer_len);
236}
237
238int DelayManager::CalculateTargetLevel(int iat_packets) {
239 int limit_probability = kLimitProbability;
240 if (streaming_mode_) {
241 limit_probability = kLimitProbabilityStreaming;
242 }
243
244 // Calculate target buffer level from inter-arrival time histogram.
245 // Find the |iat_index| for which the probability of observing an
246 // inter-arrival time larger than or equal to |iat_index| is less than or
247 // equal to |limit_probability|. The sought probability is estimated using
248 // the histogram as the reverse cumulant PDF, i.e., the sum of elements from
249 // the end up until |iat_index|. Now, since the sum of all elements is 1
250 // (in Q30) by definition, and since the solution is often a low value for
251 // |iat_index|, it is more efficient to start with |sum| = 1 and subtract
252 // elements from the start of the histogram.
253 size_t index = 0; // Start from the beginning of |iat_vector_|.
254 int sum = 1 << 30; // Assign to 1 in Q30.
255 sum -= iat_vector_[index]; // Ensure that target level is >= 1.
256
257 do {
258 // Subtract the probabilities one by one until the sum is no longer greater
259 // than limit_probability.
260 ++index;
261 sum -= iat_vector_[index];
262 } while ((sum > limit_probability) && (index < iat_vector_.size() - 1));
263
264 // This is the base value for the target buffer level.
265 int target_level = index;
266 base_target_level_ = index;
267
268 // Update detector for delay peaks.
269 bool delay_peak_found = peak_detector_.Update(iat_packets, target_level);
270 if (delay_peak_found) {
271 target_level = std::max(static_cast<int>(target_level),
272 peak_detector_.MaxPeakHeight());
273 }
274
275 // Sanity check. |target_level| must be strictly positive.
276 target_level = std::max(target_level, 1);
277 // Scale to Q8 and assign to member variable.
278 target_level_ = target_level << 8;
279 return target_level_;
280}
281
282int DelayManager::SetPacketAudioLength(int length_ms) {
283 if (length_ms <= 0) {
284 LOG_F(LS_ERROR) << "length_ms = " << length_ms;
285 return -1;
286 }
287 packet_len_ms_ = length_ms;
288 peak_detector_.SetPacketAudioLength(packet_len_ms_);
289 packet_iat_count_ms_ = 0;
290 last_pack_cng_or_dtmf_ = 1; // TODO(hlundin): Legacy. Remove?
291 return 0;
292}
293
294
295void DelayManager::Reset() {
296 packet_len_ms_ = 0; // Packet size unknown.
297 streaming_mode_ = false;
298 peak_detector_.Reset();
299 ResetHistogram(); // Resets target levels too.
300 iat_factor_ = 0; // Adapt the histogram faster for the first few packets.
301 packet_iat_count_ms_ = 0;
302 max_timer_ms_ = 0;
303 iat_cumulative_sum_ = 0;
304 max_iat_cumulative_sum_ = 0;
305 last_pack_cng_or_dtmf_ = 1;
306}
307
308int DelayManager::AverageIAT() const {
309 int32_t sum_q24 = 0;
310 assert(iat_vector_.size() == 65); // Algorithm is hard-coded for this size.
311 for (size_t i = 0; i < iat_vector_.size(); ++i) {
312 // Shift 6 to fit worst case: 2^30 * 64.
313 sum_q24 += (iat_vector_[i] >> 6) * i;
314 }
315 // Subtract the nominal inter-arrival time 1 = 2^24 in Q24.
316 sum_q24 -= (1 << 24);
317 // Multiply with 1000000 / 2^24 = 15625 / 2^18 to get in parts-per-million.
318 // Shift 7 to Q17 first, then multiply with 15625 and shift another 11.
319 return ((sum_q24 >> 7) * 15625) >> 11;
320}
321
322bool DelayManager::PeakFound() const {
323 return peak_detector_.peak_found();
324}
325
326void DelayManager::UpdateCounters(int elapsed_time_ms) {
327 packet_iat_count_ms_ += elapsed_time_ms;
328 peak_detector_.IncrementCounter(elapsed_time_ms);
329 max_timer_ms_ += elapsed_time_ms;
330}
331
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000332void DelayManager::ResetPacketIatCount() { packet_iat_count_ms_ = 0; }
333
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334void DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const {
335 if (!lower_limit || !higher_limit) {
336 LOG_F(LS_ERROR) << "NULL pointers supplied as input";
337 assert(false);
338 return;
339 }
340
341 int extra_delay_packets_q8 = 0;
342 int window_20ms = 0x7FFF; // Default large value for legacy bit-exactness.
343 if (packet_len_ms_ > 0) {
344 extra_delay_packets_q8 = (extra_delay_ms_ << 8) / packet_len_ms_;
345 window_20ms = (20 << 8) / packet_len_ms_;
346 }
347 // |lower_limit| is 75% of |target_level_| + extra delay.
348 // |target_level_| is in Q8 already.
349 *lower_limit = (target_level_ * 3) / 4 + extra_delay_packets_q8;
350 // |higher_limit| is equal to |target_level_| + extra delay, but should at
351 // least be 20 ms higher than |lower_limit_|.
352 *higher_limit = std::max(target_level_ + extra_delay_packets_q8,
353 *lower_limit + window_20ms);
354}
355
356int DelayManager::TargetLevel() const {
357 if (packet_len_ms_ > 0) {
358 // Add |extra_delay_ms_| converted to packets in Q8.
359 return target_level_ + (extra_delay_ms_ << 8) / packet_len_ms_;
360 } else {
361 // Cannot convert |extra_delay_ms_|; simply return |target_level_|.
362 return target_level_;
363 }
364}
365
366void DelayManager::LastDecoderType(NetEqDecoder decoder_type) {
367 if (decoder_type == kDecoderAVT ||
368 decoder_type == kDecoderCNGnb ||
369 decoder_type == kDecoderCNGwb ||
370 decoder_type == kDecoderCNGswb32kHz ||
371 decoder_type == kDecoderCNGswb48kHz) {
372 last_pack_cng_or_dtmf_ = 1;
373 } else if (last_pack_cng_or_dtmf_ != 0) {
374 last_pack_cng_or_dtmf_ = -1;
375 }
376}
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000377
378void DelayManager::set_extra_delay_ms(int16_t delay) {
379 extra_delay_ms_ = delay;
380}
381
382int DelayManager::base_target_level() const { return base_target_level_; }
383void DelayManager::set_streaming_mode(bool value) { streaming_mode_ = value; }
384int DelayManager::last_pack_cng_or_dtmf() const {
385 return last_pack_cng_or_dtmf_;
386}
387
388void DelayManager::set_last_pack_cng_or_dtmf(int value) {
389 last_pack_cng_or_dtmf_ = value;
390}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391} // namespace webrtc