OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_RATIO_H_ | |
6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_RATIO_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <limits> | |
11 | |
12 #include "mojo/public/cpp/environment/logging.h" | |
13 | |
14 namespace mojo { | |
15 namespace media { | |
16 | |
17 // TODO(dalesat): Consider always allowing inexact results. | |
18 | |
19 // Expresses a non-negative rational number as the ratio between two uint32_t | |
20 // values. | |
21 struct Ratio { | |
22 // Used to indicate overflow of scaling operations. | |
23 static constexpr int64_t kOverflow = std::numeric_limits<int64_t>::max(); | |
24 | |
25 // Reduces the ratio of *numerator and *denominator. | |
26 static void Reduce(uint32_t* numerator, uint32_t* denominator); | |
27 | |
28 // Produces the product of the ratios. If exact is true, DCHECKs on loss of | |
29 // precision. | |
30 static void Product(uint32_t a_numerator, | |
31 uint32_t a_denominator, | |
32 uint32_t b_numerator, | |
33 uint32_t b_denominator, | |
34 uint32_t* product_numerator, | |
35 uint32_t* product_denominator, | |
36 bool exact = true); | |
37 | |
38 // Produces the product of the ratios and the int64_t as an int64_t. Returns | |
39 // kOverflow on overflow. | |
40 static int64_t Scale(int64_t value, uint32_t numerator, uint32_t denominator); | |
41 | |
42 // Returns the product of the ratios. If exact is true, DCHECKs on loss of | |
43 // precision. | |
44 static Ratio Product(const Ratio& a, const Ratio& b, bool exact = true) { | |
45 uint32_t result_numerator; | |
46 uint32_t result_denominator; | |
47 Product(a.numerator(), a.denominator(), b.numerator(), b.denominator(), | |
48 &result_numerator, &result_denominator, exact); | |
49 return Ratio(result_numerator, result_denominator); | |
50 } | |
51 | |
52 Ratio() : numerator_(0), denominator_(1) {} | |
53 | |
54 explicit Ratio(uint32_t numerator) : numerator_(numerator), denominator_(1) {} | |
55 | |
56 Ratio(uint32_t numerator, uint32_t denominator) | |
57 : numerator_(numerator), denominator_(denominator) { | |
58 MOJO_DCHECK(denominator != 0); | |
59 Reduce(&numerator_, &denominator_); | |
60 } | |
61 | |
62 // Returns the inverse of the ratio. DCHECKs if the numerator of this ratio | |
63 // is zero. | |
64 Ratio Inverse() const { | |
65 MOJO_DCHECK(numerator_ != 0); | |
66 return Ratio(denominator_, numerator_); | |
67 } | |
68 | |
69 // Scales the value by this ratio. Returns kOverflow on overflow. | |
70 int64_t Scale(int64_t value) const { | |
71 return Scale(value, numerator_, denominator_); | |
72 } | |
73 | |
74 uint32_t numerator() const { return numerator_; } | |
75 uint32_t denominator() const { return denominator_; } | |
76 | |
77 private: | |
78 uint32_t numerator_; | |
79 uint32_t denominator_; | |
80 }; | |
81 | |
82 // Tests two ratios for equality. | |
83 inline bool operator==(const Ratio& a, const Ratio& b) { | |
84 return a.numerator() == b.numerator() && a.denominator() == b.denominator(); | |
85 } | |
86 | |
87 // Tests two ratios for inequality. | |
88 inline bool operator!=(const Ratio& a, const Ratio& b) { | |
89 return !(a == b); | |
90 } | |
91 | |
92 // Returns the product of the two ratios. DCHECKs on loss of precision. | |
93 inline Ratio operator*(const Ratio& a, const Ratio& b) { | |
94 return Ratio::Product(a, b); | |
95 } | |
96 | |
97 // Returns the product of the ratio and the int64_t. Returns kOverflow on | |
98 // overflow. | |
99 inline int64_t operator*(const Ratio& a, int64_t b) { | |
100 return a.Scale(b); | |
101 } | |
102 | |
103 // Returns the product of the ratio and the int64_t. Returns kOverflow on | |
104 // overflow. | |
105 inline int64_t operator*(int64_t a, const Ratio& b) { | |
106 return b.Scale(a); | |
107 } | |
108 | |
109 // Returns the the int64_t divided by the ratio. Returns kOverflow on | |
110 // overflow. | |
111 inline int64_t operator/(int64_t a, const Ratio& b) { | |
112 return b.Inverse().Scale(a); | |
113 } | |
114 | |
115 } // namespace media | |
116 } // namespace mojo | |
117 | |
118 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_LINEAR_TRANSFORM_H_ | |
OLD | NEW |