OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ | 5 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ |
6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ | 6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 b.reference_delta(), &result_subject_delta, &result_reference_delta, | 55 b.reference_delta(), &result_subject_delta, &result_reference_delta, |
56 exact); | 56 exact); |
57 return TimelineRate(result_subject_delta, result_reference_delta); | 57 return TimelineRate(result_subject_delta, result_reference_delta); |
58 } | 58 } |
59 | 59 |
60 TimelineRate() : subject_delta_(0), reference_delta_(1) {} | 60 TimelineRate() : subject_delta_(0), reference_delta_(1) {} |
61 | 61 |
62 explicit TimelineRate(uint32_t subject_delta) | 62 explicit TimelineRate(uint32_t subject_delta) |
63 : subject_delta_(subject_delta), reference_delta_(1) {} | 63 : subject_delta_(subject_delta), reference_delta_(1) {} |
64 | 64 |
65 explicit TimelineRate(float rate_as_float) | |
66 : subject_delta_( | |
67 rate_as_float > 1.0f | |
68 ? kFloatFactor | |
69 : static_cast<uint32_t>(kFloatFactor * rate_as_float)), | |
kulakowski
2016/05/24 13:57:24
This kFloatFactor feels a bit opaque.
dalesat
2016/05/24 18:09:15
Added comments.
| |
70 reference_delta_( | |
71 rate_as_float > 1.0f | |
72 ? static_cast<uint32_t>(kFloatFactor / rate_as_float) | |
73 : kFloatFactor) { | |
74 Reduce(&subject_delta_, &reference_delta_); | |
75 } | |
76 | |
65 TimelineRate(uint32_t subject_delta, uint32_t reference_delta) | 77 TimelineRate(uint32_t subject_delta, uint32_t reference_delta) |
66 : subject_delta_(subject_delta), reference_delta_(reference_delta) { | 78 : subject_delta_(subject_delta), reference_delta_(reference_delta) { |
67 MOJO_DCHECK(reference_delta != 0); | 79 MOJO_DCHECK(reference_delta != 0); |
68 Reduce(&subject_delta_, &reference_delta_); | 80 Reduce(&subject_delta_, &reference_delta_); |
69 } | 81 } |
70 | 82 |
71 // Returns the inverse of the rate. DCHECKs if the subject_delta of this | 83 // Returns the inverse of the rate. DCHECKs if the subject_delta of this |
72 // rate is zero. | 84 // rate is zero. |
73 TimelineRate Inverse() const { | 85 TimelineRate Inverse() const { |
74 MOJO_DCHECK(subject_delta_ != 0); | 86 MOJO_DCHECK(subject_delta_ != 0); |
75 return TimelineRate(reference_delta_, subject_delta_); | 87 return TimelineRate(reference_delta_, subject_delta_); |
76 } | 88 } |
77 | 89 |
78 // Scales the value by this rate. Returns kOverflow on overflow. | 90 // Scales the value by this rate. Returns kOverflow on overflow. |
79 int64_t Scale(int64_t value) const { | 91 int64_t Scale(int64_t value) const { |
80 return Scale(value, subject_delta_, reference_delta_); | 92 return Scale(value, subject_delta_, reference_delta_); |
81 } | 93 } |
82 | 94 |
83 uint32_t subject_delta() const { return subject_delta_; } | 95 uint32_t subject_delta() const { return subject_delta_; } |
84 uint32_t reference_delta() const { return reference_delta_; } | 96 uint32_t reference_delta() const { return reference_delta_; } |
85 | 97 |
86 private: | 98 private: |
99 static constexpr uint32_t kFloatFactor = 1ul << 23; | |
100 | |
87 uint32_t subject_delta_; | 101 uint32_t subject_delta_; |
88 uint32_t reference_delta_; | 102 uint32_t reference_delta_; |
89 }; | 103 }; |
90 | 104 |
91 // Tests two rates for equality. | 105 // Tests two rates for equality. |
92 inline bool operator==(const TimelineRate& a, const TimelineRate& b) { | 106 inline bool operator==(const TimelineRate& a, const TimelineRate& b) { |
93 return a.subject_delta() == b.subject_delta() && | 107 return a.subject_delta() == b.subject_delta() && |
94 a.reference_delta() == b.reference_delta(); | 108 a.reference_delta() == b.reference_delta(); |
95 } | 109 } |
96 | 110 |
(...skipping 22 matching lines...) Expand all Loading... | |
119 // Returns the the int64_t divided by the rate. Returns kOverflow on | 133 // Returns the the int64_t divided by the rate. Returns kOverflow on |
120 // overflow. | 134 // overflow. |
121 inline int64_t operator/(int64_t a, const TimelineRate& b) { | 135 inline int64_t operator/(int64_t a, const TimelineRate& b) { |
122 return b.Inverse().Scale(a); | 136 return b.Inverse().Scale(a); |
123 } | 137 } |
124 | 138 |
125 } // namespace media | 139 } // namespace media |
126 } // namespace mojo | 140 } // namespace mojo |
127 | 141 |
128 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ | 142 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ |
OLD | NEW |