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