2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/lib/dcpomatic_time.h
21 * @brief Types to describe time.
24 #ifndef DCPOMATIC_TIME_H
25 #define DCPOMATIC_TIME_H
27 #include "frame_rate_change.h"
28 #include "safe_stringstream.h"
29 #include "dcpomatic_assert.h"
36 class dcpomatic_round_up_test;
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.
43 * S is the name of `this' class and O is its opposite (see the typedefs below).
45 template <class S, class O>
55 explicit Time (Type t)
59 explicit Time (Type n, Type d)
63 /* Explicit conversion from type O */
64 Time (Time<O, S> d, FrameRateChange f);
70 bool operator< (Time<S, O> const & o) const {
74 bool operator<= (Time<S, O> const & o) const {
78 bool operator== (Time<S, O> const & o) const {
82 bool operator!= (Time<S, O> const & o) const {
86 bool operator>= (Time<S, O> const & o) const {
90 bool operator> (Time<S, O> const & o) const {
94 Time<S, O> operator+ (Time<S, O> const & o) const {
95 return Time<S, O> (_t + o._t);
98 Time<S, O> & operator+= (Time<S, O> const & o) {
103 Time<S, O> operator- () const {
104 return Time<S, O> (-_t);
107 Time<S, O> operator- (Time<S, O> const & o) const {
108 return Time<S, O> (_t - o._t);
111 Time<S, O> & operator-= (Time<S, O> const & o) {
116 /** Round up to the nearest sampling interval
117 * at some sampling rate.
118 * @param r Sampling rate.
120 Time<S, O> round_up (float r) {
121 Type const n = llrintf (HZ / r);
122 Type const a = _t + n - 1;
123 return Time<S, O> (a - (a % n));
126 double seconds () const {
127 return double (_t) / HZ;
130 Time<S, O> abs () const {
131 return Time<S, O> (std::abs (_t));
134 template <typename T>
135 int64_t frames_round (T r) const {
136 return llrint (_t * r / HZ);
139 template <typename T>
140 int64_t frames_floor (T r) const {
141 return floor (_t * r / HZ);
144 /** @param r Frames per second */
145 template <typename T>
146 void split (T r, int& h, int& m, int& s, int& f) const
148 /* Do this calculation with frames so that we can round
149 to a frame boundary at the start rather than the end.
151 int64_t ff = frames_round (r);
160 f = static_cast<int> (ff);
163 template <typename T>
164 std::string timecode (T r) const {
169 split (r, h, m, s, f);
174 o << std::setw(2) << std::setfill('0') << h << ":"
175 << std::setw(2) << std::setfill('0') << m << ":"
176 << std::setw(2) << std::setfill('0') << s << ":"
177 << std::setw(2) << std::setfill('0') << f;
182 static Time<S, O> from_seconds (double s) {
183 return Time<S, O> (llrint (s * HZ));
187 static Time<S, O> from_frames (int64_t f, T r) {
188 DCPOMATIC_ASSERT (r > 0);
189 return Time<S, O> (f * HZ / r);
192 static Time<S, O> delta () {
193 return Time<S, O> (1);
196 static Time<S, O> min () {
197 return Time<S, O> (-INT64_MAX);
200 static Time<S, O> max () {
201 return Time<S, O> (INT64_MAX);
205 friend struct dcptime_round_up_test;
208 static const int HZ = 96000;
211 class ContentTimeDifferentiator {};
212 class DCPTimeDifferentiator {};
214 /* Specializations for the two allowed explicit conversions */
217 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
220 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
222 /** Time relative to the start or position of a piece of content in its native frame rate */
223 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
224 /** Time relative to the start of the output DCP in its frame rate */
225 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
227 class ContentTimePeriod
230 ContentTimePeriod () {}
232 ContentTimePeriod (ContentTime f, ContentTime t)
240 ContentTimePeriod operator+ (ContentTime const & o) const {
241 return ContentTimePeriod (from + o, to + o);
244 bool overlaps (ContentTimePeriod const & o) const;
245 bool contains (ContentTime const & o) const;
247 bool operator== (ContentTimePeriod const & o) const {
248 return from == o.from && to == o.to;
252 DCPTime min (DCPTime a, DCPTime b);
253 DCPTime max (DCPTime a, DCPTime b);
254 ContentTime min (ContentTime a, ContentTime b);
255 ContentTime max (ContentTime a, ContentTime b);
256 std::ostream& operator<< (std::ostream& s, ContentTime t);
257 std::ostream& operator<< (std::ostream& s, DCPTime t);