c538357ccbe12c57093fb59bbc91758f8e1a1138
[dcpomatic.git] / src / lib / dcpomatic_time.h
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/lib/dcpomatic_time.h
22  *  @brief Types to describe time.
23  */
24
25 #ifndef DCPOMATIC_TIME_H
26 #define DCPOMATIC_TIME_H
27
28 #include "frame_rate_change.h"
29 #include "safe_stringstream.h"
30 #include "dcpomatic_assert.h"
31 #include <stdint.h>
32 #include <cmath>
33 #include <ostream>
34 #include <iomanip>
35
36 class dcpomatic_round_up_test;
37
38 /** A time in seconds, expressed as a number scaled up by Time::HZ.  We want two different
39  *  versions of this class, ContentTime and DCPTime, and we want it to be impossible to
40  *  convert implicitly between the two.  Hence there's this template hack.  I'm not
41  *  sure if it's the best way to do it.
42  *
43  *  S is the name of `this' class and O is its opposite (see the typedefs below).
44  */
45 template <class S, class O>
46 class Time
47 {
48 public:
49         Time ()
50                 : _t (0)
51         {}
52
53         typedef int64_t Type;
54
55         explicit Time (Type t)
56                 : _t (t)
57         {}
58
59         explicit Time (Type n, Type d)
60                 : _t (n * HZ / d)
61         {}
62
63         /* Explicit conversion from type O */
64         Time (Time<O, S> d, FrameRateChange f);
65
66         Type get () const {
67                 return _t;
68         }
69
70         bool operator< (Time<S, O> const & o) const {
71                 return _t < o._t;
72         }
73
74         bool operator<= (Time<S, O> const & o) const {
75                 return _t <= o._t;
76         }
77
78         bool operator== (Time<S, O> const & o) const {
79                 return _t == o._t;
80         }
81
82         bool operator!= (Time<S, O> const & o) const {
83                 return _t != o._t;
84         }
85
86         bool operator>= (Time<S, O> const & o) const {
87                 return _t >= o._t;
88         }
89
90         bool operator> (Time<S, O> const & o) const {
91                 return _t > o._t;
92         }
93
94         Time<S, O> operator+ (Time<S, O> const & o) const {
95                 return Time<S, O> (_t + o._t);
96         }
97
98         Time<S, O> & operator+= (Time<S, O> const & o) {
99                 _t += o._t;
100                 return *this;
101         }
102
103         Time<S, O> operator- () const {
104                 return Time<S, O> (-_t);
105         }
106
107         Time<S, O> operator- (Time<S, O> const & o) const {
108                 return Time<S, O> (_t - o._t);
109         }
110
111         Time<S, O> & operator-= (Time<S, O> const & o) {
112                 _t -= o._t;
113                 return *this;
114         }
115
116         /** Round up to the nearest sampling interval
117          *  at some sampling rate.
118          *  @param r Sampling rate.
119          */
120         Time<S, O> round_up (float r) const {
121                 Type const n = llrintf (HZ / r);
122                 Type const a = _t + n - 1;
123                 return Time<S, O> (a - (a % n));
124         }
125
126         double seconds () const {
127                 return double (_t) / HZ;
128         }
129
130         Time<S, O> abs () const {
131                 return Time<S, O> (std::abs (_t));
132         }
133
134         template <typename T>
135         int64_t frames_round (T r) const {
136                 /* We must cast to double here otherwise if T is integer
137                    the calculation will round down before we get the chance
138                    to llrint().
139                 */
140                 return llrint (_t * double(r) / HZ);
141         }
142
143         template <typename T>
144         int64_t frames_floor (T r) const {
145                 return floor (_t * r / HZ);
146         }
147
148         template <typename T>
149         int64_t frames_ceil (T r) const {
150                 /* We must cast to double here otherwise if T is integer
151                    the calculation will round down before we get the chance
152                    to ceil().
153                 */
154                 return ceil (_t * double(r) / HZ);
155         }
156
157         /** @param r Frames per second */
158         template <typename T>
159         void split (T r, int& h, int& m, int& s, int& f) const
160         {
161                 /* Do this calculation with frames so that we can round
162                    to a frame boundary at the start rather than the end.
163                 */
164                 int64_t ff = frames_round (r);
165
166                 h = ff / (3600 * r);
167                 ff -= h * 3600 * r;
168                 m = ff / (60 * r);
169                 ff -= m * 60 * r;
170                 s = ff / r;
171                 ff -= s * r;
172
173                 f = static_cast<int> (ff);
174         }
175
176         template <typename T>
177         std::string timecode (T r) const {
178                 int h;
179                 int m;
180                 int s;
181                 int f;
182                 split (r, h, m, s, f);
183
184                 SafeStringStream o;
185                 o.width (2);
186                 o.fill ('0');
187                 o << std::setw(2) << std::setfill('0') << h << ":"
188                   << std::setw(2) << std::setfill('0') << m << ":"
189                   << std::setw(2) << std::setfill('0') << s << ":"
190                   << std::setw(2) << std::setfill('0') << f;
191                 return o.str ();
192         }
193
194
195         static Time<S, O> from_seconds (double s) {
196                 return Time<S, O> (llrint (s * HZ));
197         }
198
199         template <class T>
200         static Time<S, O> from_frames (int64_t f, T r) {
201                 DCPOMATIC_ASSERT (r > 0);
202                 return Time<S, O> (f * HZ / r);
203         }
204
205         static Time<S, O> delta () {
206                 return Time<S, O> (1);
207         }
208
209         static Time<S, O> min () {
210                 return Time<S, O> (-INT64_MAX);
211         }
212
213         static Time<S, O> max () {
214                 return Time<S, O> (INT64_MAX);
215         }
216
217 private:
218         friend struct dcptime_round_up_test;
219
220         Type _t;
221         static const int HZ = 96000;
222 };
223
224 class ContentTimeDifferentiator {};
225 class DCPTimeDifferentiator {};
226
227 /* Specializations for the two allowed explicit conversions */
228
229 template<>
230 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
231
232 template<>
233 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
234
235 /** Time relative to the start or position of a piece of content in its native frame rate */
236 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
237 /** Time relative to the start of the output DCP in its frame rate */
238 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
239
240 template <class T>
241 class TimePeriod
242 {
243 public:
244         TimePeriod () {}
245
246         TimePeriod (T f, T t)
247                 : from (f)
248                 , to (t)
249         {}
250
251         /** start time of sampling interval that the period is from */
252         T from;
253         /** start time of next sampling interval after the period */
254         T to;
255
256         T duration () const {
257                 return to - from;
258         }
259
260         TimePeriod<T> operator+ (T const & o) const {
261                 return TimePeriod<T> (from + o, to + o);
262         }
263
264         bool overlaps (TimePeriod<T> const & other) const {
265                 return (from < other.to && to > other.from);
266         }
267
268         bool contains (T const & other) const {
269                 return (from <= other && other < to);
270         }
271
272         bool operator== (TimePeriod<T> const & other) const {
273                 return from == other.from && to == other.to;
274         }
275 };
276
277 typedef TimePeriod<ContentTime> ContentTimePeriod;
278 typedef TimePeriod<DCPTime> DCPTimePeriod;
279
280 DCPTime min (DCPTime a, DCPTime b);
281 DCPTime max (DCPTime a, DCPTime b);
282 ContentTime min (ContentTime a, ContentTime b);
283 ContentTime max (ContentTime a, ContentTime b);
284 std::ostream& operator<< (std::ostream& s, ContentTime t);
285 std::ostream& operator<< (std::ostream& s, DCPTime t);
286 std::ostream& operator<< (std::ostream& s, DCPTimePeriod p);
287
288 #endif