BOOST_FOREACH.
[dcpomatic.git] / src / lib / dcpomatic_time.h
1 /*
2     Copyright (C) 2014-2018 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 "dcpomatic_assert.h"
30 #include <boost/optional.hpp>
31 #include <stdint.h>
32 #include <cmath>
33 #include <ostream>
34 #include <iomanip>
35 #include <cstdio>
36
37 struct dcpomatic_time_ceil_test;
38 struct dcpomatic_time_floor_test;
39
40 namespace dcpomatic {
41
42 /** A time in seconds, expressed as a number scaled up by Time::HZ.  We want two different
43  *  versions of this class, dcpomatic::ContentTime and dcpomatic::DCPTime, and we want it to be impossible to
44  *  convert implicitly between the two.  Hence there's this template hack.  I'm not
45  *  sure if it's the best way to do it.
46  *
47  *  S is the name of `this' class and O is its opposite (see the typedefs below).
48  */
49 template <class S, class O>
50 class Time
51 {
52 public:
53         Time ()
54                 : _t (0)
55         {}
56
57         typedef int64_t Type;
58
59         explicit Time (Type t)
60                 : _t (t)
61         {}
62
63         explicit Time (Type n, Type d)
64                 : _t (n * HZ / d)
65         {}
66
67         /* Explicit conversion from type O */
68         Time (Time<O, S> d, FrameRateChange f);
69
70         Type get () const {
71                 return _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         bool operator> (Time<S, O> const & o) const {
95                 return _t > o._t;
96         }
97
98         Time<S, O> operator+ (Time<S, O> const & o) const {
99                 return Time<S, O> (_t + o._t);
100         }
101
102         Time<S, O> & operator+= (Time<S, O> const & o) {
103                 _t += o._t;
104                 return *this;
105         }
106
107         Time<S, O> operator- () const {
108                 return Time<S, O> (-_t);
109         }
110
111         Time<S, O> operator- (Time<S, O> const & o) const {
112                 return Time<S, O> (_t - o._t);
113         }
114
115         Time<S, O> & operator-= (Time<S, O> const & o) {
116                 _t -= o._t;
117                 return *this;
118         }
119
120         Time<S, O> operator/ (int o) const {
121                 return Time<S, O> (_t / o);
122         }
123
124         /** Round up to the nearest sampling interval
125          *  at some sampling rate.
126          *  @param r Sampling rate.
127          */
128         Time<S, O> ceil (double r) const {
129                 return Time<S, O> (llrint (HZ * frames_ceil(r) / r));
130         }
131
132         Time<S, O> floor (double r) const {
133                 return Time<S, O> (llrint (HZ * frames_floor(r) / r));
134         }
135
136         Time<S, O> round (double r) const {
137                 return Time<S, O> (llrint (HZ * frames_round(r) / r));
138         }
139
140         double seconds () const {
141                 return double (_t) / HZ;
142         }
143
144         Time<S, O> abs () const {
145                 return Time<S, O> (std::abs (_t));
146         }
147
148         template <typename T>
149         int64_t frames_round (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 llrint().
153                 */
154                 return llrint (_t * double(r) / HZ);
155         }
156
157         template <typename T>
158         int64_t frames_floor (T r) const {
159                 return ::floor (_t * r / HZ);
160         }
161
162         template <typename T>
163         int64_t frames_ceil (T r) const {
164                 /* We must cast to double here otherwise if T is integer
165                    the calculation will round down before we get the chance
166                    to ceil().
167                 */
168                 return ::ceil (_t * double(r) / HZ);
169         }
170
171         /** Split a time into hours, minutes, seconds and frames.
172          *  @param r Frames per second.
173          *  @param h Returned hours.
174          *  @param m Returned minutes.
175          *  @param s Returned seconds.
176          *  @param f Returned frames.
177          */
178         template <typename T>
179         void split (T r, int& h, int& m, int& s, int& f) const
180         {
181                 /* Do this calculation with frames so that we can round
182                    to a frame boundary at the start rather than the end.
183                 */
184                 int64_t ff = frames_round (r);
185
186                 h = ff / (3600 * r);
187                 ff -= h * 3600 * r;
188                 m = ff / (60 * r);
189                 ff -= m * 60 * r;
190                 s = ff / r;
191                 ff -= s * r;
192
193                 f = static_cast<int> (ff);
194         }
195
196         template <typename T>
197         std::string timecode (T r) const {
198                 int h;
199                 int m;
200                 int s;
201                 int f;
202                 split (r, h, m, s, f);
203
204                 char buffer[128];
205                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
206                 return buffer;
207         }
208
209
210         static Time<S, O> from_seconds (double s) {
211                 return Time<S, O> (llrint (s * HZ));
212         }
213
214         template <class T>
215         static Time<S, O> from_frames (int64_t f, T r) {
216                 DCPOMATIC_ASSERT (r > 0);
217                 return Time<S, O> (f * HZ / r);
218         }
219
220         static Time<S, O> delta () {
221                 return Time<S, O> (1);
222         }
223
224         static Time<S, O> min () {
225                 return Time<S, O> (-INT64_MAX);
226         }
227
228         static Time<S, O> max () {
229                 return Time<S, O> (INT64_MAX);
230         }
231
232         static const int HZ = 96000;
233
234 private:
235         friend struct ::dcpomatic_time_ceil_test;
236         friend struct ::dcpomatic_time_floor_test;
237
238         Type _t;
239 };
240
241 class ContentTimeDifferentiator {};
242 class DCPTimeDifferentiator {};
243
244 /* Specializations for the two allowed explicit conversions */
245
246 template<>
247 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
248
249 template<>
250 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
251
252 /** Time relative to the start or position of a piece of content in its native frame rate */
253 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
254 /** Time relative to the start of the output DCP in its frame rate */
255 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
256
257 template <class T>
258 class TimePeriod
259 {
260 public:
261         TimePeriod () {}
262
263         TimePeriod (T f, T t)
264                 : from (f)
265                 , to (t)
266         {}
267
268         /** start time of sampling interval that the period is from */
269         T from;
270         /** start time of next sampling interval after the period */
271         T to;
272
273         T duration () const {
274                 return to - from;
275         }
276
277         TimePeriod<T> operator+ (T const & o) const {
278                 return TimePeriod<T> (from + o, to + o);
279         }
280
281         boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) const {
282                 T const max_from = std::max (from, other.from);
283                 T const min_to = std::min (to, other.to);
284
285                 if (max_from >= min_to) {
286                         return boost::optional<TimePeriod<T> > ();
287                 }
288
289                 return TimePeriod<T> (max_from, min_to);
290         }
291
292         bool contains (T const & other) const {
293                 return (from <= other && other < to);
294         }
295
296         bool operator< (TimePeriod<T> const & o) const {
297                 if (from != o.from) {
298                         return from < o.from;
299                 }
300                 return to < o.to;
301         }
302
303         bool operator== (TimePeriod<T> const & other) const {
304                 return from == other.from && to == other.to;
305         }
306
307         bool operator!= (TimePeriod<T> const & other) const {
308                 return !(*this == other);
309         }
310 };
311
312 /** @param A Period which is subtracted from.
313  *  @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
314  */
315 template <class T>
316 std::list<TimePeriod<T> > subtract (TimePeriod<T> A, std::list<TimePeriod<T> > const & B)
317 {
318         std::list<TimePeriod<T> > result;
319         result.push_back (A);
320
321         for (auto i: B) {
322                 std::list<TimePeriod<T> > new_result;
323                 for (auto j: result) {
324                         boost::optional<TimePeriod<T> > ov = i.overlap (j);
325                         if (ov) {
326                                 if (*ov == i) {
327                                         /* A contains all of B */
328                                         if (i.from != j.from) {
329                                                 new_result.push_back (TimePeriod<T> (j.from, i.from));
330                                         }
331                                         if (i.to != j.to) {
332                                                 new_result.push_back (TimePeriod<T> (i.to, j.to));
333                                         }
334                                 } else if (*ov == j) {
335                                         /* B contains all of A */
336                                 } else if (i.from < j.from) {
337                                         /* B overlaps start of A */
338                                         new_result.push_back (TimePeriod<T> (i.to, j.to));
339                                 } else if (i.to > j.to) {
340                                         /* B overlaps end of A */
341                                         new_result.push_back (TimePeriod<T> (j.from, i.from));
342                                 }
343                         } else {
344                                 new_result.push_back (j);
345                         }
346                 }
347                 result = new_result;
348         }
349
350         return result;
351 }
352
353 typedef TimePeriod<ContentTime> ContentTimePeriod;
354 typedef TimePeriod<DCPTime> DCPTimePeriod;
355
356 DCPTime min (DCPTime a, DCPTime b);
357 DCPTime max (DCPTime a, DCPTime b);
358 ContentTime min (ContentTime a, ContentTime b);
359 ContentTime max (ContentTime a, ContentTime b);
360 std::string to_string (ContentTime t);
361 std::string to_string (DCPTime t);
362 std::string to_string (DCPTimePeriod p);
363
364 }
365
366 #endif