Missed update to private test repo version.
[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 <boost/foreach.hpp>
32 #include <stdint.h>
33 #include <cmath>
34 #include <ostream>
35 #include <iomanip>
36 #include <cstdio>
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         Time<S, O> operator/ (int o) const {
117                 return Time<S, O> (_t / o);
118         }
119
120         /** Round up to the nearest sampling interval
121          *  at some sampling rate.
122          *  @param r Sampling rate.
123          */
124         Time<S, O> ceil (double r) const {
125                 return Time<S, O> (llrint (HZ * frames_ceil(r) / r));
126         }
127
128         Time<S, O> floor (double r) const {
129                 return Time<S, O> (llrint (HZ * frames_floor(r) / r));
130         }
131
132         Time<S, O> round (double r) const {
133                 return Time<S, O> (llrint (HZ * frames_round(r) / r));
134         }
135
136         double seconds () const {
137                 return double (_t) / HZ;
138         }
139
140         Time<S, O> abs () const {
141                 return Time<S, O> (std::abs (_t));
142         }
143
144         template <typename T>
145         int64_t frames_round (T r) const {
146                 /* We must cast to double here otherwise if T is integer
147                    the calculation will round down before we get the chance
148                    to llrint().
149                 */
150                 return llrint (_t * double(r) / HZ);
151         }
152
153         template <typename T>
154         int64_t frames_floor (T r) const {
155                 return ::floor (_t * r / HZ);
156         }
157
158         template <typename T>
159         int64_t frames_ceil (T r) const {
160                 /* We must cast to double here otherwise if T is integer
161                    the calculation will round down before we get the chance
162                    to ceil().
163                 */
164                 return ::ceil (_t * double(r) / HZ);
165         }
166
167         /** Split a time into hours, minutes, seconds and frames.
168          *  @param r Frames per second.
169          *  @param h Returned hours.
170          *  @param m Returned minutes.
171          *  @param s Returned seconds.
172          *  @param f Returned frames.
173          */
174         template <typename T>
175         void split (T r, int& h, int& m, int& s, int& f) const
176         {
177                 /* Do this calculation with frames so that we can round
178                    to a frame boundary at the start rather than the end.
179                 */
180                 int64_t ff = frames_round (r);
181
182                 h = ff / (3600 * r);
183                 ff -= h * 3600 * r;
184                 m = ff / (60 * r);
185                 ff -= m * 60 * r;
186                 s = ff / r;
187                 ff -= s * r;
188
189                 f = static_cast<int> (ff);
190         }
191
192         template <typename T>
193         std::string timecode (T r) const {
194                 int h;
195                 int m;
196                 int s;
197                 int f;
198                 split (r, h, m, s, f);
199
200                 char buffer[128];
201                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
202                 return buffer;
203         }
204
205
206         static Time<S, O> from_seconds (double s) {
207                 return Time<S, O> (llrint (s * HZ));
208         }
209
210         template <class T>
211         static Time<S, O> from_frames (int64_t f, T r) {
212                 DCPOMATIC_ASSERT (r > 0);
213                 return Time<S, O> (f * HZ / r);
214         }
215
216         static Time<S, O> delta () {
217                 return Time<S, O> (1);
218         }
219
220         static Time<S, O> min () {
221                 return Time<S, O> (-INT64_MAX);
222         }
223
224         static Time<S, O> max () {
225                 return Time<S, O> (INT64_MAX);
226         }
227
228         static const int HZ = 96000;
229
230 private:
231         friend struct dcpomatic_time_ceil_test;
232         friend struct dcpomatic_time_floor_test;
233
234         Type _t;
235 };
236
237 class ContentTimeDifferentiator {};
238 class DCPTimeDifferentiator {};
239
240 /* Specializations for the two allowed explicit conversions */
241
242 template<>
243 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
244
245 template<>
246 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
247
248 /** Time relative to the start or position of a piece of content in its native frame rate */
249 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
250 /** Time relative to the start of the output DCP in its frame rate */
251 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
252
253 template <class T>
254 class TimePeriod
255 {
256 public:
257         TimePeriod () {}
258
259         TimePeriod (T f, T t)
260                 : from (f)
261                 , to (t)
262         {}
263
264         /** start time of sampling interval that the period is from */
265         T from;
266         /** start time of next sampling interval after the period */
267         T to;
268
269         T duration () const {
270                 return to - from;
271         }
272
273         TimePeriod<T> operator+ (T const & o) const {
274                 return TimePeriod<T> (from + o, to + o);
275         }
276
277         boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) const {
278                 T const max_from = std::max (from, other.from);
279                 T const min_to = std::min (to, other.to);
280
281                 if (max_from >= min_to) {
282                         return boost::optional<TimePeriod<T> > ();
283                 }
284
285                 return TimePeriod<T> (max_from, min_to);
286         }
287
288         bool contains (T const & other) const {
289                 return (from <= other && other < to);
290         }
291
292         bool operator< (TimePeriod<T> const & o) const {
293                 if (from != o.from) {
294                         return from < o.from;
295                 }
296                 return to < o.to;
297         }
298
299         bool operator== (TimePeriod<T> const & other) const {
300                 return from == other.from && to == other.to;
301         }
302
303         bool operator!= (TimePeriod<T> const & other) const {
304                 return !(*this == other);
305         }
306 };
307
308 /** @param A Period which is subtracted from.
309  *  @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
310  */
311 template <class T>
312 std::list<TimePeriod<T> > subtract (TimePeriod<T> A, std::list<TimePeriod<T> > const & B)
313 {
314         std::list<TimePeriod<T> > result;
315         result.push_back (A);
316
317         BOOST_FOREACH (TimePeriod<T> i, B) {
318                 std::list<TimePeriod<T> > new_result;
319                 BOOST_FOREACH (TimePeriod<T> j, result) {
320                         boost::optional<TimePeriod<T> > ov = i.overlap (j);
321                         if (ov) {
322                                 if (*ov == i) {
323                                         /* A contains all of B */
324                                         if (i.from != j.from) {
325                                                 new_result.push_back (TimePeriod<T> (j.from, i.from));
326                                         }
327                                         if (i.to != j.to) {
328                                                 new_result.push_back (TimePeriod<T> (i.to, j.to));
329                                         }
330                                 } else if (*ov == j) {
331                                         /* B contains all of A */
332                                 } else if (i.from < j.from) {
333                                         /* B overlaps start of A */
334                                         new_result.push_back (TimePeriod<T> (i.to, j.to));
335                                 } else if (i.to > j.to) {
336                                         /* B overlaps end of A */
337                                         new_result.push_back (TimePeriod<T> (j.from, i.from));
338                                 }
339                         } else {
340                                 new_result.push_back (j);
341                         }
342                 }
343                 result = new_result;
344         }
345
346         return result;
347 }
348
349 typedef TimePeriod<ContentTime> ContentTimePeriod;
350 typedef TimePeriod<DCPTime> DCPTimePeriod;
351
352 DCPTime min (DCPTime a, DCPTime b);
353 DCPTime max (DCPTime a, DCPTime b);
354 ContentTime min (ContentTime a, ContentTime b);
355 ContentTime max (ContentTime a, ContentTime b);
356 std::string to_string (ContentTime t);
357 std::string to_string (DCPTime t);
358 std::string to_string (DCPTimePeriod p);
359
360 #endif