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