Add operator* for Time and int.
[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         Time<S, O> operator/ (int o) const {
160                 return Time<S, O> (_t / o);
161         }
162
163         /** Round up to the nearest sampling interval
164          *  at some sampling rate.
165          *  @param r Sampling rate.
166          */
167         Time<S, O> ceil (double r) const {
168                 return Time<S, O> (llrint(HZ * frames_ceil(r) / r));
169         }
170
171         Time<S, O> floor (double r) const {
172                 return Time<S, O> (llrint(HZ * frames_floor(r) / r));
173         }
174
175         Time<S, O> round (double r) const {
176                 return Time<S, O> (llrint(HZ * frames_round(r) / r));
177         }
178
179         double seconds () const {
180                 return double (_t) / HZ;
181         }
182
183         Time<S, O> abs () const {
184                 return Time<S, O> (std::abs(_t));
185         }
186
187         template <typename T>
188         int64_t frames_round (T r) const {
189                 /* We must cast to double here otherwise if T is integer
190                    the calculation will round down before we get the chance
191                    to llrint().
192                 */
193                 return llrint (_t * double(r) / HZ);
194         }
195
196         template <typename T>
197         int64_t frames_floor (T r) const {
198                 return ::floor (_t * r / HZ);
199         }
200
201         template <typename T>
202         int64_t frames_ceil (T r) const {
203                 /* We must cast to double here otherwise if T is integer
204                    the calculation will round down before we get the chance
205                    to ceil().
206                 */
207                 return ::ceil (_t * double(r) / HZ);
208         }
209
210         /** Split a time into hours, minutes, seconds and frames.
211          *  @param r Frames per second.
212          *  @return Split time.
213          */
214         template <typename T>
215         HMSF split (T r) const
216         {
217                 /* Do this calculation with frames so that we can round
218                    to a frame boundary at the start rather than the end.
219                 */
220                 auto ff = frames_round (r);
221                 HMSF hmsf;
222
223                 hmsf.h = ff / (3600 * r);
224                 ff -= hmsf.h * 3600 * r;
225                 hmsf.m = ff / (60 * r);
226                 ff -= hmsf.m * 60 * r;
227                 hmsf.s = ff / r;
228                 ff -= hmsf.s * r;
229
230                 hmsf.f = static_cast<int> (ff);
231                 return hmsf;
232         }
233
234         template <typename T>
235         std::string timecode (T r) const {
236                 auto hmsf = split (r);
237
238                 char buffer[128];
239                 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
240                 return buffer;
241         }
242
243         static Time<S, O> from_seconds (double s) {
244                 return Time<S, O> (llrint (s * HZ));
245         }
246
247         template <class T>
248         static Time<S, O> from_frames (int64_t f, T r) {
249                 DCPOMATIC_ASSERT (r > 0);
250                 return Time<S, O> (f * HZ / r);
251         }
252
253         static Time<S, O> delta () {
254                 return Time<S, O> (1);
255         }
256
257         static Time<S, O> min () {
258                 return Time<S, O> (-INT64_MAX);
259         }
260
261         static Time<S, O> max () {
262                 return Time<S, O> (INT64_MAX);
263         }
264
265         static const int HZ = 96000;
266
267 private:
268         friend struct ::dcpomatic_time_ceil_test;
269         friend struct ::dcpomatic_time_floor_test;
270
271         Type _t;
272 };
273
274
275 class ContentTimeDifferentiator {};
276 class DCPTimeDifferentiator {};
277
278
279 /* Specializations for the two allowed explicit conversions */
280
281 template<>
282 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
283
284 template<>
285 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
286
287
288 /** Time relative to the start or position of a piece of content in its native frame rate */
289 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
290 /** Time relative to the start of the output DCP in its frame rate */
291 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
292
293 template <class T>
294 class TimePeriod
295 {
296 public:
297         TimePeriod () {}
298
299         TimePeriod (T f, T t)
300                 : from (f)
301                 , to (t)
302         {}
303
304         /** start time of sampling interval that the period is from */
305         T from;
306         /** start time of next sampling interval after the period */
307         T to;
308
309         T duration () const {
310                 return to - from;
311         }
312
313         TimePeriod<T> operator+ (T const & o) const {
314                 return TimePeriod<T> (from + o, to + o);
315         }
316
317         boost::optional<TimePeriod<T>> overlap (TimePeriod<T> const & other) const {
318                 T const max_from = std::max (from, other.from);
319                 T const min_to = std::min (to, other.to);
320
321                 if (max_from >= min_to) {
322                         return {};
323                 }
324
325                 return TimePeriod<T> (max_from, min_to);
326         }
327
328         bool contains (T const & other) const {
329                 return (from <= other && other < to);
330         }
331
332         bool operator< (TimePeriod<T> const & o) const {
333                 if (from != o.from) {
334                         return from < o.from;
335                 }
336                 return to < o.to;
337         }
338
339         bool operator== (TimePeriod<T> const & other) const {
340                 return from == other.from && to == other.to;
341         }
342
343         bool operator!= (TimePeriod<T> const & other) const {
344                 return !(*this == other);
345         }
346 };
347
348
349 /** @param A Period which is subtracted from.
350  *  @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
351  */
352 template <class T>
353 std::list<TimePeriod<T>> subtract (TimePeriod<T> A, std::list<TimePeriod<T>> const & B)
354 {
355         std::list<TimePeriod<T>> result;
356         result.push_back (A);
357
358         for (auto i: B) {
359                 std::list<TimePeriod<T>> new_result;
360                 for (auto j: result) {
361                         auto ov = i.overlap (j);
362                         if (ov) {
363                                 if (*ov == i) {
364                                         /* A contains all of B */
365                                         if (i.from != j.from) {
366                                                 new_result.push_back (TimePeriod<T>(j.from, i.from));
367                                         }
368                                         if (i.to != j.to) {
369                                                 new_result.push_back (TimePeriod<T>(i.to, j.to));
370                                         }
371                                 } else if (*ov == j) {
372                                         /* B contains all of A */
373                                 } else if (i.from < j.from) {
374                                         /* B overlaps start of A */
375                                         new_result.push_back (TimePeriod<T>(i.to, j.to));
376                                 } else if (i.to > j.to) {
377                                         /* B overlaps end of A */
378                                         new_result.push_back (TimePeriod<T>(j.from, i.from));
379                                 }
380                         } else {
381                                 new_result.push_back (j);
382                         }
383                 }
384                 result = new_result;
385         }
386
387         return result;
388 }
389
390
391 typedef TimePeriod<ContentTime> ContentTimePeriod;
392 typedef TimePeriod<DCPTime> DCPTimePeriod;
393
394
395 DCPTime min (DCPTime a, DCPTime b);
396 DCPTime max (DCPTime a, DCPTime b);
397 ContentTime min (ContentTime a, ContentTime b);
398 ContentTime max (ContentTime a, ContentTime b);
399 std::string to_string (ContentTime t);
400 std::string to_string (DCPTime t);
401 std::string to_string (DCPTimePeriod p);
402
403
404 }
405
406
407 #endif