Shaka Player Embedded
utils.h
Go to the documentation of this file.
1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_UTILS_H_
16 #define SHAKA_EMBEDDED_UTILS_H_
17 
18 #include <assert.h>
19 
20 #include <iostream>
21 #include <limits>
22 #include <string>
23 #include <type_traits>
24 
25 #include "macros.h"
26 
27 namespace shaka {
28 
41 enum class VideoFillMode : uint8_t {
48 
52  Stretch,
53 
59  Zoom,
60 };
61 
62 
67 template <typename T>
69  T x;
70  T y;
71  T w;
72  T h;
73 
74  bool operator==(const ShakaRect& other) const {
75  return x == other.x && y == other.y && w == other.w && h == other.h;
76  }
77  bool operator!=(const ShakaRect& other) const {
78  return !(*this == other);
79  }
80 };
81 
82 template <typename T>
83 std::ostream& operator<<(std::ostream& os, const ShakaRect<T>& rect) {
84  return os << "{x=" << rect.x << ",y=" << rect.y << ",w=" << rect.w << ",h="
85  << rect.h << "}";
86 }
87 
94 template <typename T>
95 struct SHAKA_EXPORT Rational final {
96  private:
97  template <typename U>
98  using common = typename std::common_type<T, U>::type;
99  template <typename U>
100  using enable_if_num =
101  typename std::enable_if<std::is_arithmetic<U>::value>::type;
102 
103  static T gcd(T a, T b) {
104  // Calculate the gcd(a, b) using the euclidean algorithm.
105  while (b != 0) {
106  T temp = a % b;
107  a = b;
108  b = temp;
109  }
110  return a;
111  }
112 
114  static void reduce(T* num, T* den) {
115  T a = gcd(*num, *den);
116  *num /= a;
117  *den /= a;
118  }
119 
126  Rational(T num1, T num2, T den1, T den2) {
127  if (num1 == 0 || num2 == 0 || den1 == 0 || den2 == 0) {
128  numerator = denominator = 0;
129  return;
130  }
131 
132  reduce(&num1, &den1);
133  reduce(&num1, &den2);
134  reduce(&num2, &den1);
135  reduce(&num2, &den2);
136 
137  // Avoid errors later by just crashing on overflow.
138  // TODO: Consider converting to float and rounding if this would overflow.
139  assert(std::numeric_limits<T>::max() / num1 >= num2);
140  assert(std::numeric_limits<T>::max() / den1 >= den2);
141 
142  // These should now be relatively prime since any common prime factor should
143  // have been removed in the reductions above.
144  numerator = num1 * num2;
145  denominator = den1 * den2;
146  assert(gcd(numerator, denominator) == 1);
147  }
148 
149  public:
150  Rational() = default;
151  Rational(T num, T den) : Rational(num, 1, den, 1) {}
152 
153  T truncate() const {
154  return numerator / denominator;
155  }
157  return {denominator, numerator};
158  }
159 
160  operator bool() const {
161  return numerator != 0 && denominator != 0;
162  }
163  operator double() const {
164  return static_cast<double>(numerator) / denominator;
165  }
166 
167  template <typename U>
168  bool operator==(const Rational<U>& other) const {
169  return numerator == other.numerator && denominator == other.denominator;
170  }
171 
172  template <typename U>
173  bool operator!=(const Rational<U>& other) const {
174  return !(*this == other);
175  }
176 
177  template <typename U>
178  Rational<common<U>> operator*(const Rational<U>& other) const {
179  return {numerator, other.numerator, denominator, other.denominator};
180  }
181 
182  template <typename U, typename = enable_if_num<U>>
183  Rational<common<U>> operator*(U other) const {
184  return {numerator, other, denominator, 1};
185  }
186 
187  template <typename U>
188  Rational<common<U>> operator/(const Rational<U>& other) const {
189  return {numerator, other.denominator, denominator, other.numerator};
190  }
191 
192  template <typename U, typename = enable_if_num<U>>
193  Rational<common<U>> operator/(U other) const {
194  return {numerator, 1, denominator, other};
195  }
196 
199 };
200 static_assert(std::is_pod<Rational<int>>::value, "Rational should be POD");
201 
202 template <typename T>
203 std::ostream& operator<<(std::ostream& os, const Rational<T>& bar) {
204  return os << bar.numerator << "/" << bar.denominator;
205 }
206 
207 // operator*(T, Rational<T>);
208 template <typename T, typename U, typename =
209  typename std::enable_if<std::is_arithmetic<T>::value>::type>
211  T a, const Rational<U>& b) {
212  return Rational<T>{a, 1} * b;
213 }
214 
215 // operator/(T, Rational<T>);
216 template <typename T, typename U, typename =
217  typename std::enable_if<std::is_arithmetic<T>::value>::type>
219  T a, const Rational<U>& b) {
220  return b.inverse() * a;
221 }
222 
223 
242  ShakaRect<uint32_t> bounds,
243  Rational<uint32_t> sample_aspect_ratio,
246 
247 
253 inline std::string EscapeKeySystem(const std::string& key_system) {
254  std::string ret = key_system;
255  std::string::size_type pos = 0;
256  while ((pos = ret.find('.', pos)) != std::string::npos) {
257  ret.insert(pos, "\\");
258  pos += 2;
259  }
260  return ret;
261 }
262 
272 inline std::string LicenseServerConfig(const std::string& key_system) {
273  return "drm.servers." + EscapeKeySystem(key_system);
274 }
275 
284 inline std::string AdvancedDrmConfig(const std::string& key_system,
285  const std::string& property) {
286  return "drm.advanced." + EscapeKeySystem(key_system) + "." + property;
287 }
288 
291 } // namespace shaka
292 
293 #endif // SHAKA_EMBEDDED_UTILS_H_
Rational(T num, T den)
Definition: utils.h:151
Rational< common< U > > operator/(U other) const
Definition: utils.h:193
const char * dest
Definition: media_utils.cc:31
bool operator==(const Rational< U > &other) const
Definition: utils.h:168
#define SHAKA_EXPORT
Definition: macros.h:30
Rational< common< U > > operator*(const Rational< U > &other) const
Definition: utils.h:178
VideoFillMode
Definition: utils.h:41
bool operator==(const ShakaRect &other) const
Definition: utils.h:74
std::string LicenseServerConfig(const std::string &key_system)
Definition: utils.h:272
std::shared_ptr< shaka::media::DecodedFrame > frame
bool operator!=(const ShakaRect &other) const
Definition: utils.h:77
Rational< T > inverse() const
Definition: utils.h:156
ExceptionCode type
bool operator!=(const Rational< U > &other) const
Definition: utils.h:173
Rational< common< U > > operator*(U other) const
Definition: utils.h:183
Rational< typename std::common_type< T, U >::type > operator/(T a, const Rational< U > &b)
Definition: utils.h:218
T truncate() const
Definition: utils.h:153
void FitVideoToRegion(ShakaRect< uint32_t > frame, ShakaRect< uint32_t > bounds, Rational< uint32_t > sample_aspect_ratio, VideoFillMode mode, ShakaRect< uint32_t > *src, ShakaRect< uint32_t > *dest)
Definition: shaka_utils.cc:23
Rational< typename std::common_type< T, U >::type > operator*(T a, const Rational< U > &b)
Definition: utils.h:210
std::string EscapeKeySystem(const std::string &key_system)
Definition: utils.h:253
std::string AdvancedDrmConfig(const std::string &key_system, const std::string &property)
Definition: utils.h:284
Rational< common< U > > operator/(const Rational< U > &other) const
Definition: utils.h:188