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