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"
35 class dcpomatic_round_up_test;
37 /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different
38 * versions of this class, ContentTime and DCPTime, and we want it to be impossible to
39 * convert implicitly between the two. Hence there's this template hack. I'm not
40 * sure if it's the best way to do it.
42 * S is the name of `this' class and O is its opposite (see the typedefs below).
44 template <class S, class O>
54 explicit Time (Type t)
58 explicit Time (Type n, Type d)
62 /* Explicit conversion from type O */
63 Time (Time<O, S> d, FrameRateChange f);
69 bool operator< (Time<S, O> const & o) const {
73 bool operator<= (Time<S, O> const & o) const {
77 bool operator== (Time<S, O> const & o) const {
81 bool operator!= (Time<S, O> const & o) const {
85 bool operator>= (Time<S, O> const & o) const {
89 bool operator> (Time<S, O> const & o) const {
93 Time<S, O> operator+ (Time<S, O> const & o) const {
94 return Time<S, O> (_t + o._t);
97 Time<S, O> & operator+= (Time<S, O> const & o) {
102 Time<S, O> operator- () const {
103 return Time<S, O> (-_t);
106 Time<S, O> operator- (Time<S, O> const & o) const {
107 return Time<S, O> (_t - o._t);
110 Time<S, O> & operator-= (Time<S, O> const & o) {
115 /** Round up to the nearest sampling interval
116 * at some sampling rate.
117 * @param r Sampling rate.
119 Time<S, O> round_up (float r) const {
120 Type const n = llrintf (HZ / r);
121 Type const a = _t + n - 1;
122 return Time<S, O> (a - (a % n));
125 double seconds () const {
126 return double (_t) / HZ;
129 Time<S, O> abs () const {
130 return Time<S, O> (std::abs (_t));
133 template <typename T>
134 int64_t frames_round (T r) const {
135 return llrint (_t * r / HZ);
138 template <typename T>
139 int64_t frames_floor (T r) const {
140 return floor (_t * r / HZ);
143 template <typename T>
144 int64_t frames_ceil (T r) const {
145 return ceil (_t * r / HZ);
148 /** @param r Frames per second */
149 template <typename T>
150 void split (T r, int& h, int& m, int& s, int& f) const
152 /* Do this calculation with frames so that we can round
153 to a frame boundary at the start rather than the end.
155 int64_t ff = frames_round (r);
164 f = static_cast<int> (ff);
167 template <typename T>
168 std::string timecode (T r) const {
173 split (r, h, m, s, f);
178 o << std::setw(2) << std::setfill('0') << h << ":"
179 << std::setw(2) << std::setfill('0') << m << ":"
180 << std::setw(2) << std::setfill('0') << s << ":"
181 << std::setw(2) << std::setfill('0') << f;
186 static Time<S, O> from_seconds (double s) {
187 return Time<S, O> (llrint (s * HZ));
191 static Time<S, O> from_frames (int64_t f, T r) {
192 DCPOMATIC_ASSERT (r > 0);
193 return Time<S, O> (f * HZ / r);
196 static Time<S, O> delta () {
197 return Time<S, O> (1);
200 static Time<S, O> min () {
201 return Time<S, O> (-INT64_MAX);
204 static Time<S, O> max () {
205 return Time<S, O> (INT64_MAX);
209 friend struct dcptime_round_up_test;
212 static const int HZ = 96000;
215 class ContentTimeDifferentiator {};
216 class DCPTimeDifferentiator {};
218 /* Specializations for the two allowed explicit conversions */
221 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
224 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
226 /** Time relative to the start or position of a piece of content in its native frame rate */
227 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
228 /** Time relative to the start of the output DCP in its frame rate */
229 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
231 class ContentTimePeriod
234 ContentTimePeriod () {}
236 ContentTimePeriod (ContentTime f, ContentTime t)
244 ContentTimePeriod operator+ (ContentTime const & o) const {
245 return ContentTimePeriod (from + o, to + o);
248 bool overlaps (ContentTimePeriod const & o) const;
249 bool contains (ContentTime const & o) const;
251 bool operator== (ContentTimePeriod const & o) const {
252 return from == o.from && to == o.to;
256 DCPTime min (DCPTime a, DCPTime b);
257 DCPTime max (DCPTime a, DCPTime b);
258 ContentTime min (ContentTime a, ContentTime b);
259 ContentTime max (ContentTime a, ContentTime b);
260 std::ostream& operator<< (std::ostream& s, ContentTime t);
261 std::ostream& operator<< (std::ostream& s, DCPTime t);