You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2016 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. #include "modules/video_coding/histogram.h"
  11. #include <algorithm>
  12. #include "rtc_base/checks.h"
  13. namespace webrtc {
  14. namespace video_coding {
  15. Histogram::Histogram(size_t num_buckets, size_t max_num_values) {
  16. RTC_DCHECK_GT(num_buckets, 0);
  17. RTC_DCHECK_GT(max_num_values, 0);
  18. buckets_.resize(num_buckets);
  19. values_.reserve(max_num_values);
  20. index_ = 0;
  21. }
  22. void Histogram::Add(size_t value) {
  23. value = std::min<size_t>(value, buckets_.size() - 1);
  24. if (index_ < values_.size()) {
  25. --buckets_[values_[index_]];
  26. RTC_DCHECK_LT(values_[index_], buckets_.size());
  27. values_[index_] = value;
  28. } else {
  29. values_.emplace_back(value);
  30. }
  31. ++buckets_[value];
  32. index_ = (index_ + 1) % values_.capacity();
  33. }
  34. size_t Histogram::InverseCdf(float probability) const {
  35. RTC_DCHECK_GE(probability, 0.f);
  36. RTC_DCHECK_LE(probability, 1.f);
  37. RTC_DCHECK_GT(values_.size(), 0ul);
  38. size_t bucket = 0;
  39. float accumulated_probability = 0;
  40. while (accumulated_probability < probability && bucket < buckets_.size()) {
  41. accumulated_probability +=
  42. static_cast<float>(buckets_[bucket]) / values_.size();
  43. ++bucket;
  44. }
  45. return bucket;
  46. }
  47. size_t Histogram::NumValues() const {
  48. return values_.size();
  49. }
  50. } // namespace video_coding
  51. } // namespace webrtc