Missing rounding.
[dcpomatic.git] / src / lib / dcpomatic_time.h
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/lib/dcpomatic_time.h
21  *  @brief Types to describe time.
22  */
23
24 #ifndef DCPOMATIC_TIME_H
25 #define DCPOMATIC_TIME_H
26
27 #include "frame_rate_change.h"
28 #include "safe_stringstream.h"
29 #include "dcpomatic_assert.h"
30 #include <stdint.h>
31 #include <cmath>
32 #include <ostream>
33 #include <sstream>
34 #include <iomanip>
35
36 class dcpomatic_round_up_test;
37
38 /** A time in seconds, expressed as a number scaled up by Time::HZ.  We want two different
39  *  versions of this class, ContentTime and DCPTime, and we want it to be impossible to
40  *  convert implicitly between the two.  Hence there's this template hack.  I'm not
41  *  sure if it's the best way to do it.
42  *
43  *  S is the name of `this' class and O is its opposite (see the typedefs below).
44  */
45 template <class S, class O>
46 class Time
47 {
48 public:
49         Time ()
50                 : _t (0)
51         {}
52
53         typedef int64_t Type;
54
55         explicit Time (Type t)
56                 : _t (t)
57         {}
58
59         explicit Time (Type n, Type d)
60                 : _t (n * HZ / d)
61         {}
62
63         /* Explicit conversion from type O */
64         Time (Time<O, S> d, FrameRateChange f);
65
66         Type get () const {
67                 return _t;
68         }
69
70         bool operator< (Time<S, O> const & o) const {
71                 return _t < o._t;
72         }
73
74         bool operator<= (Time<S, O> const & o) const {
75                 return _t <= o._t;
76         }
77
78         bool operator== (Time<S, O> const & o) const {
79                 return _t == o._t;
80         }
81
82         bool operator!= (Time<S, O> const & o) const {
83                 return _t != o._t;
84         }
85
86         bool operator>= (Time<S, O> const & o) const {
87                 return _t >= o._t;
88         }
89
90         bool operator> (Time<S, O> const & o) const {
91                 return _t > o._t;
92         }
93
94         Time<S, O> operator+ (Time<S, O> const & o) const {
95                 return Time<S, O> (_t + o._t);
96         }
97
98         Time<S, O> & operator+= (Time<S, O> const & o) {
99                 _t += o._t;
100                 return *this;
101         }
102
103         Time<S, O> operator- () const {
104                 return Time<S, O> (-_t);
105         }
106
107         Time<S, O> operator- (Time<S, O> const & o) const {
108                 return Time<S, O> (_t - o._t);
109         }
110
111         Time<S, O> & operator-= (Time<S, O> const & o) {
112                 _t -= o._t;
113                 return *this;
114         }
115
116         /** Round up to the nearest sampling interval
117          *  at some sampling rate.
118          *  @param r Sampling rate.
119          */
120         Time<S, O> round_up (float r) {
121                 Type const n = rint (HZ / r);
122                 Type const a = _t + n - 1;
123                 return Time<S, O> (a - (a % n));
124         }
125
126         double seconds () const {
127                 return double (_t) / HZ;
128         }
129
130         Time<S, O> abs () const {
131                 return Time<S, O> (std::abs (_t));
132         }
133
134         template <typename T>
135         int64_t frames (T r) const {
136                 return rint (double (_t) * r / HZ);
137         }
138
139         /** @param r Frames per second */
140         template <typename T>
141         void split (T r, int& h, int& m, int& s, int& f) const
142         {
143                 /* Do this calculation with frames so that we can round
144                    to a frame boundary at the start rather than the end.
145                 */
146                 int64_t ff = frames (r);
147
148                 h = ff / (3600 * r);
149                 ff -= h * 3600 * r;
150                 m = ff / (60 * r);
151                 ff -= m * 60 * r;
152                 s = ff / r;
153                 ff -= s * r;
154
155                 f = static_cast<int> (ff);
156         }
157
158         template <typename T>
159         std::string timecode (T r) const {
160                 int h;
161                 int m;
162                 int s;
163                 int f;
164                 split (r, h, m, s, f);
165
166                 SafeStringStream o;
167                 o.width (2);
168                 o.fill ('0');
169                 o << std::setw(2) << std::setfill('0') << h << ":"
170                   << std::setw(2) << std::setfill('0') << m << ":"
171                   << std::setw(2) << std::setfill('0') << s << ":"
172                   << std::setw(2) << std::setfill('0') << f;
173                 return o.str ();
174         }
175
176
177         static Time<S, O> from_seconds (double s) {
178                 return Time<S, O> (rint (s * HZ));
179         }
180
181         template <class T>
182         static Time<S, O> from_frames (int64_t f, T r) {
183                 DCPOMATIC_ASSERT (r > 0);
184                 return Time<S, O> (f * HZ / r);
185         }
186
187         static Time<S, O> delta () {
188                 return Time<S, O> (1);
189         }
190
191         static Time<S, O> min () {
192                 return Time<S, O> (-INT64_MAX);
193         }
194
195         static Time<S, O> max () {
196                 return Time<S, O> (INT64_MAX);
197         }
198
199 private:
200         friend struct dcptime_round_up_test;
201
202         Type _t;
203         static const int HZ = 96000;
204 };
205
206 class ContentTimeDifferentiator {};
207 class DCPTimeDifferentiator {};
208
209 /* Specializations for the two allowed explicit conversions */
210
211 template<>
212 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
213
214 template<>
215 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
216
217 /** Time relative to the start or position of a piece of content in its native frame rate */
218 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
219 /** Time relative to the start of the output DCP in its frame rate */
220 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
221
222 class ContentTimePeriod
223 {
224 public:
225         ContentTimePeriod () {}
226
227         ContentTimePeriod (ContentTime f, ContentTime t)
228                 : from (f)
229                 , to (t)
230         {}
231
232         ContentTime from;
233         ContentTime to;
234
235         ContentTimePeriod operator+ (ContentTime const & o) const {
236                 return ContentTimePeriod (from + o, to + o);
237         }
238
239         bool overlaps (ContentTimePeriod const & o) const;
240         bool contains (ContentTime const & o) const;
241 };
242
243 DCPTime min (DCPTime a, DCPTime b);
244 std::ostream& operator<< (std::ostream& s, ContentTime t);
245 std::ostream& operator<< (std::ostream& s, DCPTime t);
246
247 #endif