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)), |
| 70 reference_delta_( |
| 71 rate_as_float > 1.0f |
| 72 ? static_cast<uint32_t>(kFloatFactor / rate_as_float) |
| 73 : kFloatFactor) { |
| 74 // The expressions above are intended to provide good precision for |
| 75 // 'reasonable' playback rate values (say in the range 0.0 to 4.0). The |
| 76 // expressions always produce a ratio of kFloatFactor and a number smaller |
| 77 // than kFloatFactor. kFloatFactor's value was chosen because floats have |
| 78 // a 23-bit mantissa, and operations with a larger factor would sacrifice |
| 79 // precision. |
| 80 MOJO_DCHECK(rate_as_float >= 0.0f); |
| 81 Reduce(&subject_delta_, &reference_delta_); |
| 82 } |
| 83 |
65 TimelineRate(uint32_t subject_delta, uint32_t reference_delta) | 84 TimelineRate(uint32_t subject_delta, uint32_t reference_delta) |
66 : subject_delta_(subject_delta), reference_delta_(reference_delta) { | 85 : subject_delta_(subject_delta), reference_delta_(reference_delta) { |
67 MOJO_DCHECK(reference_delta != 0); | 86 MOJO_DCHECK(reference_delta != 0); |
68 Reduce(&subject_delta_, &reference_delta_); | 87 Reduce(&subject_delta_, &reference_delta_); |
69 } | 88 } |
70 | 89 |
71 // Returns the inverse of the rate. DCHECKs if the subject_delta of this | 90 // Returns the inverse of the rate. DCHECKs if the subject_delta of this |
72 // rate is zero. | 91 // rate is zero. |
73 TimelineRate Inverse() const { | 92 TimelineRate Inverse() const { |
74 MOJO_DCHECK(subject_delta_ != 0); | 93 MOJO_DCHECK(subject_delta_ != 0); |
75 return TimelineRate(reference_delta_, subject_delta_); | 94 return TimelineRate(reference_delta_, subject_delta_); |
76 } | 95 } |
77 | 96 |
78 // Scales the value by this rate. Returns kOverflow on overflow. | 97 // Scales the value by this rate. Returns kOverflow on overflow. |
79 int64_t Scale(int64_t value) const { | 98 int64_t Scale(int64_t value) const { |
80 return Scale(value, subject_delta_, reference_delta_); | 99 return Scale(value, subject_delta_, reference_delta_); |
81 } | 100 } |
82 | 101 |
83 uint32_t subject_delta() const { return subject_delta_; } | 102 uint32_t subject_delta() const { return subject_delta_; } |
84 uint32_t reference_delta() const { return reference_delta_; } | 103 uint32_t reference_delta() const { return reference_delta_; } |
85 | 104 |
86 private: | 105 private: |
| 106 // A multiplier for float-to-TimelineRate conversions chosen because floats |
| 107 // have a 23-bit mantissa. |
| 108 static constexpr uint32_t kFloatFactor = 1ul << 23; |
| 109 |
87 uint32_t subject_delta_; | 110 uint32_t subject_delta_; |
88 uint32_t reference_delta_; | 111 uint32_t reference_delta_; |
89 }; | 112 }; |
90 | 113 |
91 // Tests two rates for equality. | 114 // Tests two rates for equality. |
92 inline bool operator==(const TimelineRate& a, const TimelineRate& b) { | 115 inline bool operator==(const TimelineRate& a, const TimelineRate& b) { |
93 return a.subject_delta() == b.subject_delta() && | 116 return a.subject_delta() == b.subject_delta() && |
94 a.reference_delta() == b.reference_delta(); | 117 a.reference_delta() == b.reference_delta(); |
95 } | 118 } |
96 | 119 |
(...skipping 22 matching lines...) Expand all Loading... |
119 // Returns the the int64_t divided by the rate. Returns kOverflow on | 142 // Returns the the int64_t divided by the rate. Returns kOverflow on |
120 // overflow. | 143 // overflow. |
121 inline int64_t operator/(int64_t a, const TimelineRate& b) { | 144 inline int64_t operator/(int64_t a, const TimelineRate& b) { |
122 return b.Inverse().Scale(a); | 145 return b.Inverse().Scale(a); |
123 } | 146 } |
124 | 147 |
125 } // namespace media | 148 } // namespace media |
126 } // namespace mojo | 149 } // namespace mojo |
127 | 150 |
128 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ | 151 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_TIMELINE_RATE_H_ |
OLD | NEW |