Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 1 | /* |
| 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 Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
| 19 | |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
| 22 | class Histogram { |
| 23 | public: |
| 24 | // Creates histogram with capacity |num_buckets| and |forget_factor| in Q15. |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 25 | Histogram(size_t num_buckets, |
| 26 | int forget_factor, |
| 27 | absl::optional<double> start_forget_weight = absl::nullopt); |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 28 | |
| 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 Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 50 | // 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 Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 55 | // 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 Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 62 | private: |
| 63 | std::vector<int> buckets_; |
| 64 | int forget_factor_; // Q15 |
| 65 | const int base_forget_factor_; |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 66 | int add_count_; |
| 67 | const absl::optional<double> start_forget_weight_; |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | } // namespace webrtc |
| 71 | |
| 72 | #endif // MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_ |