2 Copyright (C) 2014-2018 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>
31 #include <boost/foreach.hpp>
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> ceil (double r) const {
121 return Time<S, O> (llrint (HZ * frames_ceil(r) / r));
124 Time<S, O> floor (double r) const {
125 return Time<S, O> (llrint (HZ * frames_floor(r) / r));
128 Time<S, O> round (double r) const {
129 return Time<S, O> (llrint (HZ * frames_round(r) / r));
132 double seconds () const {
133 return double (_t) / HZ;
136 Time<S, O> abs () const {
137 return Time<S, O> (std::abs (_t));
140 template <typename T>
141 int64_t frames_round (T r) const {
142 /* We must cast to double here otherwise if T is integer
143 the calculation will round down before we get the chance
146 return llrint (_t * double(r) / HZ);
149 template <typename T>
150 int64_t frames_floor (T r) const {
151 return ::floor (_t * r / HZ);
154 template <typename T>
155 int64_t frames_ceil (T r) const {
156 /* We must cast to double here otherwise if T is integer
157 the calculation will round down before we get the chance
160 return ::ceil (_t * double(r) / HZ);
163 /** Split a time into hours, minutes, seconds and frames.
164 * @param r Frames per second.
165 * @param h Returned hours.
166 * @param m Returned minutes.
167 * @param s Returned seconds.
168 * @param f Returned frames.
170 template <typename T>
171 void split (T r, int& h, int& m, int& s, int& f) const
173 /* Do this calculation with frames so that we can round
174 to a frame boundary at the start rather than the end.
176 int64_t ff = frames_round (r);
185 f = static_cast<int> (ff);
188 template <typename T>
189 std::string timecode (T r) const {
194 split (r, h, m, s, f);
197 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
202 static Time<S, O> from_seconds (double s) {
203 return Time<S, O> (llrint (s * HZ));
207 static Time<S, O> from_frames (int64_t f, T r) {
208 DCPOMATIC_ASSERT (r > 0);
209 return Time<S, O> (f * HZ / r);
212 static Time<S, O> delta () {
213 return Time<S, O> (1);
216 static Time<S, O> min () {
217 return Time<S, O> (-INT64_MAX);
220 static Time<S, O> max () {
221 return Time<S, O> (INT64_MAX);
225 friend struct dcpomatic_time_ceil_test;
226 friend struct dcpomatic_time_floor_test;
229 static const int HZ = 96000;
232 class ContentTimeDifferentiator {};
233 class DCPTimeDifferentiator {};
235 /* Specializations for the two allowed explicit conversions */
238 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
241 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
243 /** Time relative to the start or position of a piece of content in its native frame rate */
244 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
245 /** Time relative to the start of the output DCP in its frame rate */
246 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
254 TimePeriod (T f, T t)
259 /** start time of sampling interval that the period is from */
261 /** start time of next sampling interval after the period */
264 T duration () const {
268 TimePeriod<T> operator+ (T const & o) const {
269 return TimePeriod<T> (from + o, to + o);
272 boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) const {
273 T const max_from = std::max (from, other.from);
274 T const min_to = std::min (to, other.to);
276 if (max_from >= min_to) {
277 return boost::optional<TimePeriod<T> > ();
280 return TimePeriod<T> (max_from, min_to);
283 bool contains (T const & other) const {
284 return (from <= other && other < to);
287 bool operator< (TimePeriod<T> const & o) const {
288 if (from != o.from) {
289 return from < o.from;
294 bool operator== (TimePeriod<T> const & other) const {
295 return from == other.from && to == other.to;
298 bool operator!= (TimePeriod<T> const & other) const {
299 return !(*this == other);
303 /** @param A Period which is subtracted from.
304 * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
307 std::list<TimePeriod<T> > subtract (TimePeriod<T> A, std::list<TimePeriod<T> > const & B)
309 std::list<TimePeriod<T> > result;
310 result.push_back (A);
312 BOOST_FOREACH (TimePeriod<T> i, B) {
313 std::list<TimePeriod<T> > new_result;
314 BOOST_FOREACH (TimePeriod<T> j, result) {
315 boost::optional<TimePeriod<T> > ov = i.overlap (j);
318 /* A contains all of B */
319 if (i.from != j.from) {
320 new_result.push_back (TimePeriod<T> (j.from, i.from));
323 new_result.push_back (TimePeriod<T> (i.to, j.to));
325 } else if (*ov == j) {
326 /* B contains all of A */
327 } else if (i.from < j.from) {
328 /* B overlaps start of A */
329 new_result.push_back (TimePeriod<T> (i.to, j.to));
330 } else if (i.to > j.to) {
331 /* B overlaps end of A */
332 new_result.push_back (TimePeriod<T> (j.from, i.from));
335 new_result.push_back (j);
344 typedef TimePeriod<ContentTime> ContentTimePeriod;
345 typedef TimePeriod<DCPTime> DCPTimePeriod;
347 DCPTime min (DCPTime a, DCPTime b);
348 DCPTime max (DCPTime a, DCPTime b);
349 ContentTime min (ContentTime a, ContentTime b);
350 ContentTime max (ContentTime a, ContentTime b);
351 std::string to_string (ContentTime t);
352 std::string to_string (DCPTime t);
353 std::string to_string (DCPTimePeriod p);