blob: 7eb90d97a30fa42331168cbc96bcd1180d623f03 [file] [log] [blame]
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +01001/*
2 * Copyright (c) 2019 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#ifndef MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_
12#define MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_
13
14#include <string.h> // Provide access to size_t.
15
16#include <vector>
17
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020018#include "absl/types/optional.h"
19
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010020namespace webrtc {
21
22class Histogram {
23 public:
24 // Creates histogram with capacity |num_buckets| and |forget_factor| in Q15.
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020025 Histogram(size_t num_buckets,
26 int forget_factor,
27 absl::optional<double> start_forget_weight = absl::nullopt);
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010028
29 virtual ~Histogram();
30
31 // Resets the histogram to the default start distribution.
32 virtual void Reset();
33
34 // Add entry in bucket |index|.
35 virtual void Add(int index);
36
37 // Calculates the quantile at |probability| (in Q30) of the histogram
38 // distribution.
39 virtual int Quantile(int probability);
40
41 // Apply compression or stretching to the histogram.
42 virtual void Scale(int old_bucket_width, int new_bucket_width);
43
44 // Returns the number of buckets in the histogram.
45 virtual int NumBuckets() const;
46
47 // Returns the probability for each bucket in Q30.
48 std::vector<int> buckets() const { return buckets_; }
49
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010050 // Made public for testing.
51 static std::vector<int> ScaleBuckets(const std::vector<int>& buckets,
52 int old_bucket_width,
53 int new_bucket_width);
54
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020055 // Accessors only intended for testing purposes.
56 int base_forget_factor_for_testing() const { return base_forget_factor_; }
57 int forget_factor_for_testing() const { return forget_factor_; }
58 absl::optional<double> start_forget_weight_for_testing() const {
59 return start_forget_weight_;
60 }
61
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010062 private:
63 std::vector<int> buckets_;
64 int forget_factor_; // Q15
65 const int base_forget_factor_;
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020066 int add_count_;
67 const absl::optional<double> start_forget_weight_;
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010068};
69
70} // namespace webrtc
71
72#endif // MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_