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