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>
37 struct dcpomatic_time_ceil_test;
38 struct dcpomatic_time_floor_test;
42 /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different
43 * versions of this class, dcpomatic::ContentTime and dcpomatic::DCPTime, and we want it to be impossible to
44 * convert implicitly between the two. Hence there's this template hack. I'm not
45 * sure if it's the best way to do it.
47 * S is the name of `this' class and O is its opposite (see the typedefs below).
49 template <class S, class O>
59 explicit Time (Type t)
63 explicit Time (Type n, Type d)
67 /* Explicit conversion from type O */
68 Time (Time<O, S> d, FrameRateChange f);
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 bool operator> (Time<S, O> const & o) const {
98 Time<S, O> operator+ (Time<S, O> const & o) const {
99 return Time<S, O> (_t + o._t);
102 Time<S, O> & operator+= (Time<S, O> const & o) {
107 Time<S, O> operator- () const {
108 return Time<S, O> (-_t);
111 Time<S, O> operator- (Time<S, O> const & o) const {
112 return Time<S, O> (_t - o._t);
115 Time<S, O> & operator-= (Time<S, O> const & o) {
120 Time<S, O> operator/ (int o) const {
121 return Time<S, O> (_t / o);
124 /** Round up to the nearest sampling interval
125 * at some sampling rate.
126 * @param r Sampling rate.
128 Time<S, O> ceil (double r) const {
129 return Time<S, O> (llrint (HZ * frames_ceil(r) / r));
132 Time<S, O> floor (double r) const {
133 return Time<S, O> (llrint (HZ * frames_floor(r) / r));
136 Time<S, O> round (double r) const {
137 return Time<S, O> (llrint (HZ * frames_round(r) / r));
140 double seconds () const {
141 return double (_t) / HZ;
144 Time<S, O> abs () const {
145 return Time<S, O> (std::abs (_t));
148 template <typename T>
149 int64_t frames_round (T r) const {
150 /* We must cast to double here otherwise if T is integer
151 the calculation will round down before we get the chance
154 return llrint (_t * double(r) / HZ);
157 template <typename T>
158 int64_t frames_floor (T r) const {
159 return ::floor (_t * r / HZ);
162 template <typename T>
163 int64_t frames_ceil (T r) const {
164 /* We must cast to double here otherwise if T is integer
165 the calculation will round down before we get the chance
168 return ::ceil (_t * double(r) / HZ);
171 /** Split a time into hours, minutes, seconds and frames.
172 * @param r Frames per second.
173 * @param h Returned hours.
174 * @param m Returned minutes.
175 * @param s Returned seconds.
176 * @param f Returned frames.
178 template <typename T>
179 void split (T r, int& h, int& m, int& s, int& f) const
181 /* Do this calculation with frames so that we can round
182 to a frame boundary at the start rather than the end.
184 int64_t ff = frames_round (r);
193 f = static_cast<int> (ff);
196 template <typename T>
197 std::string timecode (T r) const {
202 split (r, h, m, s, f);
205 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
210 static Time<S, O> from_seconds (double s) {
211 return Time<S, O> (llrint (s * HZ));
215 static Time<S, O> from_frames (int64_t f, T r) {
216 DCPOMATIC_ASSERT (r > 0);
217 return Time<S, O> (f * HZ / r);
220 static Time<S, O> delta () {
221 return Time<S, O> (1);
224 static Time<S, O> min () {
225 return Time<S, O> (-INT64_MAX);
228 static Time<S, O> max () {
229 return Time<S, O> (INT64_MAX);
232 static const int HZ = 96000;
235 friend struct ::dcpomatic_time_ceil_test;
236 friend struct ::dcpomatic_time_floor_test;
241 class ContentTimeDifferentiator {};
242 class DCPTimeDifferentiator {};
244 /* Specializations for the two allowed explicit conversions */
247 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
250 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
252 /** Time relative to the start or position of a piece of content in its native frame rate */
253 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
254 /** Time relative to the start of the output DCP in its frame rate */
255 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
263 TimePeriod (T f, T t)
268 /** start time of sampling interval that the period is from */
270 /** start time of next sampling interval after the period */
273 T duration () const {
277 TimePeriod<T> operator+ (T const & o) const {
278 return TimePeriod<T> (from + o, to + o);
281 boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) const {
282 T const max_from = std::max (from, other.from);
283 T const min_to = std::min (to, other.to);
285 if (max_from >= min_to) {
286 return boost::optional<TimePeriod<T> > ();
289 return TimePeriod<T> (max_from, min_to);
292 bool contains (T const & other) const {
293 return (from <= other && other < to);
296 bool operator< (TimePeriod<T> const & o) const {
297 if (from != o.from) {
298 return from < o.from;
303 bool operator== (TimePeriod<T> const & other) const {
304 return from == other.from && to == other.to;
307 bool operator!= (TimePeriod<T> const & other) const {
308 return !(*this == other);
312 /** @param A Period which is subtracted from.
313 * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
316 std::list<TimePeriod<T> > subtract (TimePeriod<T> A, std::list<TimePeriod<T> > const & B)
318 std::list<TimePeriod<T> > result;
319 result.push_back (A);
322 std::list<TimePeriod<T> > new_result;
323 for (auto j: result) {
324 boost::optional<TimePeriod<T> > ov = i.overlap (j);
327 /* A contains all of B */
328 if (i.from != j.from) {
329 new_result.push_back (TimePeriod<T> (j.from, i.from));
332 new_result.push_back (TimePeriod<T> (i.to, j.to));
334 } else if (*ov == j) {
335 /* B contains all of A */
336 } else if (i.from < j.from) {
337 /* B overlaps start of A */
338 new_result.push_back (TimePeriod<T> (i.to, j.to));
339 } else if (i.to > j.to) {
340 /* B overlaps end of A */
341 new_result.push_back (TimePeriod<T> (j.from, i.from));
344 new_result.push_back (j);
353 typedef TimePeriod<ContentTime> ContentTimePeriod;
354 typedef TimePeriod<DCPTime> DCPTimePeriod;
356 DCPTime min (DCPTime a, DCPTime b);
357 DCPTime max (DCPTime a, DCPTime b);
358 ContentTime min (ContentTime a, ContentTime b);
359 ContentTime max (ContentTime a, ContentTime b);
360 std::string to_string (ContentTime t);
361 std::string to_string (DCPTime t);
362 std::string to_string (DCPTimePeriod p);