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