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> round_up (float r) const {
122 Type const n = llrintf (HZ / r);
123 Type const a = _t + n - 1;
124 return Time<S, O> (a - (a % n));
127 double seconds () const {
128 return double (_t) / HZ;
131 Time<S, O> abs () const {
132 return Time<S, O> (std::abs (_t));
135 template <typename T>
136 int64_t frames_round (T r) const {
137 /* We must cast to double here otherwise if T is integer
138 the calculation will round down before we get the chance
141 return llrint (_t * double(r) / HZ);
144 template <typename T>
145 int64_t frames_floor (T r) const {
146 return floor (_t * r / HZ);
149 template <typename T>
150 int64_t frames_ceil (T r) const {
151 /* We must cast to double here otherwise if T is integer
152 the calculation will round down before we get the chance
155 return ceil (_t * double(r) / HZ);
158 /** @param r Frames per second */
159 template <typename T>
160 void split (T r, int& h, int& m, int& s, int& f) const
162 /* Do this calculation with frames so that we can round
163 to a frame boundary at the start rather than the end.
165 int64_t ff = frames_round (r);
174 f = static_cast<int> (ff);
177 template <typename T>
178 std::string timecode (T r) const {
183 split (r, h, m, s, f);
186 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
191 static Time<S, O> from_seconds (double s) {
192 return Time<S, O> (llrint (s * HZ));
196 static Time<S, O> from_frames (int64_t f, T r) {
197 DCPOMATIC_ASSERT (r > 0);
198 return Time<S, O> (f * HZ / r);
201 static Time<S, O> delta () {
202 return Time<S, O> (1);
205 static Time<S, O> min () {
206 return Time<S, O> (-INT64_MAX);
209 static Time<S, O> max () {
210 return Time<S, O> (INT64_MAX);
214 friend struct dcptime_round_up_test;
217 static const int HZ = 96000;
220 class ContentTimeDifferentiator {};
221 class DCPTimeDifferentiator {};
223 /* Specializations for the two allowed explicit conversions */
226 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
229 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
231 /** Time relative to the start or position of a piece of content in its native frame rate */
232 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
233 /** Time relative to the start of the output DCP in its frame rate */
234 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
242 TimePeriod (T f, T t)
247 /** start time of sampling interval that the period is from */
249 /** start time of next sampling interval after the period */
252 T duration () const {
256 TimePeriod<T> operator+ (T const & o) const {
257 return TimePeriod<T> (from + o, to + o);
260 boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) {
261 T const max_from = std::max (from, other.from);
262 T const min_to = std::min (to, other.to);
264 if (max_from >= min_to) {
265 return boost::optional<TimePeriod<T> > ();
268 return TimePeriod<T> (max_from, min_to);
271 bool contains (T const & other) const {
272 return (from <= other && other < to);
275 bool operator< (TimePeriod<T> const & o) const {
276 if (from != o.from) {
277 return from < o.from;
282 bool operator== (TimePeriod<T> const & other) const {
283 return from == other.from && to == other.to;
286 bool operator!= (TimePeriod<T> const & other) const {
287 return !(*this == other);
291 typedef TimePeriod<ContentTime> ContentTimePeriod;
292 typedef TimePeriod<DCPTime> DCPTimePeriod;
294 DCPTime min (DCPTime a, DCPTime b);
295 DCPTime max (DCPTime a, DCPTime b);
296 ContentTime min (ContentTime a, ContentTime b);
297 ContentTime max (ContentTime a, ContentTime b);
298 std::string to_string (ContentTime t);
299 std::string to_string (DCPTime t);
300 std::string to_string (DCPTimePeriod p);