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