OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 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 WEBRTC_MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_UTIL_THRESHOLD_CURVE_H
_ |
| 12 #define WEBRTC_MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_UTIL_THRESHOLD_CURVE_H
_ |
| 13 |
| 14 #include "webrtc/base/checks.h" |
| 15 |
| 16 namespace webrtc { |
| 17 |
| 18 class ThresholdCurve { |
| 19 public: |
| 20 struct Point { |
| 21 constexpr Point(float x, float y) : x(x), y(y) {} |
| 22 float x; |
| 23 float y; |
| 24 }; |
| 25 |
| 26 // ThresholdCurve defines a curve. The curve is characterized by the two |
| 27 // conjunction points: A and B. The curve segments the metric space into |
| 28 // three domains - above the curve, on it and below it. |
| 29 // |
| 30 // y-axis ^ | |
| 31 // | A| |
| 32 // | \ A: (a.x, a.y) |
| 33 // | \ B: (b.x, b.y) |
| 34 // | B\________ |
| 35 // |---------------> bandwidth |
| 36 // |
| 37 // If either a.x == b.x or a.y == b.y, the curve can be defined |
| 38 // by a single point. (We merge the two points into one - either the lower or |
| 39 // the leftmost one - for easier treatment.) |
| 40 // |
| 41 // y-axis ^ | |
| 42 // | | |
| 43 // | | |
| 44 // | | |
| 45 // | P|__________ |
| 46 // |---------------> bandwidth |
| 47 ThresholdCurve(const Point& left, const Point& right) |
| 48 : a(GetPoint(left, right, true)), |
| 49 b(GetPoint(left, right, false)), |
| 50 slope(b.x - a.x == 0.0f ? 0.0f : (b.y - a.y) / (b.x - a.x)), |
| 51 offset(a.y - slope * a.x) { |
| 52 // TODO(elad.alon): We might want to introduce some numerical validations. |
| 53 } |
| 54 |
| 55 ThresholdCurve(float a_x, float a_y, float b_x, float b_y) |
| 56 : ThresholdCurve(Point{a_x, a_y}, Point{b_x, b_y}) {} |
| 57 |
| 58 // Checks if a point is strictly below the curve. |
| 59 bool IsBelowCurve(const Point& p) const { |
| 60 if (p.x < a.x) { |
| 61 return true; |
| 62 } else if (p.x == a.x) { |
| 63 // In principle, we could merge this into the next else, but to avoid |
| 64 // numerical errors, we treat it separately. |
| 65 return p.y < a.y; |
| 66 } else if (a.x < p.x && p.x < b.x) { |
| 67 return p.y < offset + slope * p.x; |
| 68 } else { // if (b.x <= p.x) |
| 69 return p.y < b.y; |
| 70 } |
| 71 } |
| 72 |
| 73 // Checks if a point is strictly above the curve. |
| 74 bool IsAboveCurve(const Point& p) const { |
| 75 if (p.x <= a.x) { |
| 76 return false; |
| 77 } else if (a.x < p.x && p.x < b.x) { |
| 78 return p.y > offset + slope * p.x; |
| 79 } else { // if (b.x <= p.x) |
| 80 return p.y > b.y; |
| 81 } |
| 82 } |
| 83 |
| 84 bool operator<=(const ThresholdCurve& rhs) const { |
| 85 // This curve is <= the rhs curve if no point from this curve is |
| 86 // above a corresponding point from the rhs curve. |
| 87 return !IsBelowCurve(rhs.a) && !IsBelowCurve(rhs.b) && |
| 88 !rhs.IsAboveCurve(a) && !rhs.IsAboveCurve(b); |
| 89 } |
| 90 |
| 91 private: |
| 92 static const Point& GetPoint(const Point& left, |
| 93 const Point& right, |
| 94 bool is_for_left) { |
| 95 RTC_DCHECK_LE(left.x, right.x); |
| 96 RTC_DCHECK_GE(left.y, right.y); |
| 97 |
| 98 // Same X-value or Y-value triggers merging both points to the |
| 99 // lower and/or left of the two points, respectively. |
| 100 if (left.x == right.x) { |
| 101 return right; |
| 102 } else if (left.y == right.y) { |
| 103 return left; |
| 104 } |
| 105 |
| 106 // If unmerged, boolean flag determines which of the points is desired. |
| 107 return is_for_left ? left : right; |
| 108 } |
| 109 |
| 110 const Point a; |
| 111 const Point b; |
| 112 const float slope; |
| 113 const float offset; |
| 114 }; |
| 115 |
| 116 } // namespace webrtc |
| 117 |
| 118 #endif // WEBRTC_MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_UTIL_THRESHOLD_CURV
E_H_ |
OLD | NEW |