aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_coding/neteq/buffer_level_filter.cc
blob: 0388b19502404476a171077687db9cf49542f755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h"

#include <algorithm>  // Provide access to std::max.

namespace webrtc {

BufferLevelFilter::BufferLevelFilter() {
  Reset();
}

void BufferLevelFilter::Reset() {
  filtered_current_level_ = 0;
  level_factor_ = 253;
}

void BufferLevelFilter::Update(int buffer_size_packets,
                               int time_stretched_samples,
                               int packet_len_samples) {
  // Filter:
  // |filtered_current_level_| = |level_factor_| * |filtered_current_level_| +
  //                            (1 - |level_factor_|) * |buffer_size_packets|
  // |level_factor_| and |filtered_current_level_| are in Q8.
  // |buffer_size_packets| is in Q0.
  filtered_current_level_ = ((level_factor_ * filtered_current_level_) >> 8) +
      ((256 - level_factor_) * buffer_size_packets);

  // Account for time-scale operations (accelerate and pre-emptive expand).
  if (time_stretched_samples && packet_len_samples > 0) {
    // Time-scaling has been performed since last filter update. Subtract the
    // value of |time_stretched_samples| from |filtered_current_level_| after
    // converting |time_stretched_samples| from samples to packets in Q8.
    // Make sure that the filtered value remains non-negative.
    filtered_current_level_ = std::max(0,
        filtered_current_level_ -
        (time_stretched_samples << 8) / packet_len_samples);
  }
}

void BufferLevelFilter::SetTargetBufferLevel(int target_buffer_level) {
  if (target_buffer_level <= 1) {
    level_factor_ = 251;
  } else if (target_buffer_level <= 3) {
    level_factor_ = 252;
  } else if (target_buffer_level <= 7) {
    level_factor_ = 253;
  } else {
    level_factor_ = 254;
  }
}
}  // namespace webrtc