philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 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 | #ifndef WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_ |
| 13 | |
| 14 | #include <limits> |
| 15 | #include <type_traits> |
| 16 | |
kwiberg | 84f6a3f | 2017-09-05 08:43:13 -0700 | [diff] [blame] | 17 | #include "webrtc/api/optional.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 18 | #include "webrtc/rtc_base/mod_ops.h" |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 19 | #include "webrtc/rtc_base/safe_compare.h" |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // Test if the sequence number |a| is ahead or at sequence number |b|. |
| 24 | // |
| 25 | // If |M| is an even number and the two sequence numbers are at max distance |
| 26 | // from each other, then the sequence number with the highest value is |
| 27 | // considered to be ahead. |
| 28 | template <typename T, T M> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 29 | inline typename std::enable_if<(M > 0), bool>::type AheadOrAt(T a, T b) { |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 30 | static_assert(std::is_unsigned<T>::value, |
| 31 | "Type must be an unsigned integer."); |
| 32 | const T maxDist = M / 2; |
| 33 | if (!(M & 1) && MinDiff<T, M>(a, b) == maxDist) |
| 34 | return b < a; |
| 35 | return ForwardDiff<T, M>(b, a) <= maxDist; |
| 36 | } |
| 37 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 38 | template <typename T, T M> |
| 39 | inline typename std::enable_if<(M == 0), bool>::type AheadOrAt(T a, T b) { |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 40 | static_assert(std::is_unsigned<T>::value, |
| 41 | "Type must be an unsigned integer."); |
| 42 | const T maxDist = std::numeric_limits<T>::max() / 2 + T(1); |
| 43 | if (a - b == maxDist) |
| 44 | return b < a; |
| 45 | return ForwardDiff(b, a) < maxDist; |
| 46 | } |
| 47 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 48 | template <typename T> |
| 49 | inline bool AheadOrAt(T a, T b) { |
| 50 | return AheadOrAt<T, 0>(a, b); |
| 51 | } |
| 52 | |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 53 | // Test if the sequence number |a| is ahead of sequence number |b|. |
| 54 | // |
| 55 | // If |M| is an even number and the two sequence numbers are at max distance |
| 56 | // from each other, then the sequence number with the highest value is |
| 57 | // considered to be ahead. |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 58 | template <typename T, T M = 0> |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 59 | inline bool AheadOf(T a, T b) { |
| 60 | static_assert(std::is_unsigned<T>::value, |
| 61 | "Type must be an unsigned integer."); |
| 62 | return a != b && AheadOrAt<T, M>(a, b); |
| 63 | } |
| 64 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 65 | // Comparator used to compare sequence numbers in a continuous fashion. |
| 66 | // |
| 67 | // WARNING! If used to sort sequence numbers of length M then the interval |
| 68 | // covered by the sequence numbers may not be larger than floor(M/2). |
| 69 | template <typename T, T M = 0> |
| 70 | struct AscendingSeqNumComp { |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 71 | bool operator()(T a, T b) const { return AheadOf<T, M>(a, b); } |
| 72 | }; |
| 73 | |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 74 | // Comparator used to compare sequence numbers in a continuous fashion. |
| 75 | // |
| 76 | // WARNING! If used to sort sequence numbers of length M then the interval |
| 77 | // covered by the sequence numbers may not be larger than floor(M/2). |
| 78 | template <typename T, T M = 0> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 79 | struct DescendingSeqNumComp { |
| 80 | bool operator()(T a, T b) const { return AheadOf<T, M>(b, a); } |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 83 | // A sequencer number unwrapper where the start value of the unwrapped sequence |
| 84 | // can be set. The unwrapped value is not allowed to wrap. |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 85 | template <typename T, T M = 0> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 86 | class SeqNumUnwrapper { |
brucedawson | 452ea0d | 2017-08-09 10:00:11 -0700 | [diff] [blame] | 87 | // Use '<' instead of rtc::SafeLt to avoid crbug.com/753488 |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 88 | static_assert( |
| 89 | std::is_unsigned<T>::value && |
brucedawson | 452ea0d | 2017-08-09 10:00:11 -0700 | [diff] [blame] | 90 | std::numeric_limits<T>::max() < std::numeric_limits<uint64_t>::max(), |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 91 | "Type unwrapped must be an unsigned integer smaller than uint64_t."); |
| 92 | |
| 93 | public: |
philipel | 3b3c9c4 | 2017-09-11 09:38:36 -0700 | [diff] [blame] | 94 | // We want a default value that is close to 2^62 for a two reasons. Firstly, |
| 95 | // we can unwrap wrapping numbers in either direction, and secondly, the |
| 96 | // unwrapped numbers can be stored in either int64_t or uint64_t. We also want |
| 97 | // the default value to be human readable, which makes a power of 10 suitable. |
| 98 | static constexpr uint64_t kDefaultStartValue = 1000000000000000000UL; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 99 | |
| 100 | SeqNumUnwrapper() : last_unwrapped_(kDefaultStartValue) {} |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 101 | explicit SeqNumUnwrapper(uint64_t start_at) : last_unwrapped_(start_at) {} |
| 102 | |
| 103 | uint64_t Unwrap(T value) { |
| 104 | if (!last_value_) |
| 105 | last_value_.emplace(value); |
| 106 | |
| 107 | uint64_t unwrapped = 0; |
| 108 | if (AheadOrAt<T, M>(value, *last_value_)) { |
| 109 | unwrapped = last_unwrapped_ + ForwardDiff<T, M>(*last_value_, value); |
| 110 | RTC_CHECK_GE(unwrapped, last_unwrapped_); |
| 111 | } else { |
| 112 | unwrapped = last_unwrapped_ - ReverseDiff<T, M>(*last_value_, value); |
| 113 | RTC_CHECK_LT(unwrapped, last_unwrapped_); |
| 114 | } |
| 115 | |
| 116 | *last_value_ = value; |
| 117 | last_unwrapped_ = unwrapped; |
| 118 | return last_unwrapped_; |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 119 | } |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | uint64_t last_unwrapped_; |
| 123 | rtc::Optional<T> last_value_; |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace webrtc |
| 127 | |
| 128 | #endif // WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_ |