2 Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/lib/dcpomatic_time.h
22 * @brief Types to describe time.
25 #ifndef DCPOMATIC_TIME_H
26 #define DCPOMATIC_TIME_H
28 #include "frame_rate_change.h"
29 #include "dcpomatic_assert.h"
30 #include <boost/optional.hpp>
37 class dcpomatic_round_up_test;
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.
44 * S is the name of `this' class and O is its opposite (see the typedefs below).
46 template <class S, class O>
56 explicit Time (Type t)
60 explicit Time (Type n, Type d)
64 /* Explicit conversion from type O */
65 Time (Time<O, S> d, FrameRateChange f);
71 bool operator< (Time<S, O> const & o) const {
75 bool operator<= (Time<S, O> const & o) const {
79 bool operator== (Time<S, O> const & o) const {
83 bool operator!= (Time<S, O> const & o) const {
87 bool operator>= (Time<S, O> const & o) const {
91 bool operator> (Time<S, O> const & o) const {
95 Time<S, O> operator+ (Time<S, O> const & o) const {
96 return Time<S, O> (_t + o._t);
99 Time<S, O> & operator+= (Time<S, O> const & o) {
104 Time<S, O> operator- () const {
105 return Time<S, O> (-_t);
108 Time<S, O> operator- (Time<S, O> const & o) const {
109 return Time<S, O> (_t - o._t);
112 Time<S, O> & operator-= (Time<S, O> const & o) {
117 /** Round up to the nearest sampling interval
118 * at some sampling rate.
119 * @param r Sampling rate.
121 Time<S, O> ceil (float r) const {
122 return Time<S, O> (llrint (HZ * frames_ceil(r) / double(r)));
125 Time<S, O> floor (float r) const {
126 return Time<S, O> (llrint (HZ * frames_floor(r) / double(r)));
129 double seconds () const {
130 return double (_t) / HZ;
133 Time<S, O> abs () const {
134 return Time<S, O> (std::abs (_t));
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
143 return llrint (_t * double(r) / HZ);
146 template <typename T>
147 int64_t frames_floor (T r) const {
148 return ::floor (_t * r / HZ);
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
157 return ::ceil (_t * double(r) / HZ);
160 /** @param r Frames per second */
161 template <typename T>
162 void split (T r, int& h, int& m, int& s, int& f) const
164 /* Do this calculation with frames so that we can round
165 to a frame boundary at the start rather than the end.
167 int64_t ff = frames_round (r);
176 f = static_cast<int> (ff);
179 template <typename T>
180 std::string timecode (T r) const {
185 split (r, h, m, s, f);
188 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
193 static Time<S, O> from_seconds (double s) {
194 return Time<S, O> (llrint (s * HZ));
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);
203 static Time<S, O> delta () {
204 return Time<S, O> (1);
207 static Time<S, O> min () {
208 return Time<S, O> (-INT64_MAX);
211 static Time<S, O> max () {
212 return Time<S, O> (INT64_MAX);
216 friend struct dcptime_ceil_test;
217 friend struct dcptime_floor_test;
220 static const int HZ = 96000;
223 class ContentTimeDifferentiator {};
224 class DCPTimeDifferentiator {};
226 /* Specializations for the two allowed explicit conversions */
229 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
232 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
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;
245 TimePeriod (T f, T t)
250 /** start time of sampling interval that the period is from */
252 /** start time of next sampling interval after the period */
255 T duration () const {
259 TimePeriod<T> operator+ (T const & o) const {
260 return TimePeriod<T> (from + o, to + o);
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);
267 if (max_from >= min_to) {
268 return boost::optional<TimePeriod<T> > ();
271 return TimePeriod<T> (max_from, min_to);
274 bool contains (T const & other) const {
275 return (from <= other && other < to);
278 bool operator< (TimePeriod<T> const & o) const {
279 if (from != o.from) {
280 return from < o.from;
285 bool operator== (TimePeriod<T> const & other) const {
286 return from == other.from && to == other.to;
289 bool operator!= (TimePeriod<T> const & other) const {
290 return !(*this == other);
294 typedef TimePeriod<ContentTime> ContentTimePeriod;
295 typedef TimePeriod<DCPTime> DCPTimePeriod;
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);