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 struct dcpomatic_time_ceil_test;
39 struct dcpomatic_time_floor_test;
43 /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different
44 * versions of this class, dcpomatic::ContentTime and dcpomatic::DCPTime, and we want it to be impossible to
45 * convert implicitly between the two. Hence there's this template hack. I'm not
46 * sure if it's the best way to do it.
48 * S is the name of `this' class and O is its opposite (see the typedefs below).
50 template <class S, class O>
60 explicit Time (Type t)
64 explicit Time (Type n, Type d)
68 /* Explicit conversion from type O */
69 Time (Time<O, S> d, FrameRateChange f);
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 bool operator> (Time<S, O> const & o) const {
99 Time<S, O> operator+ (Time<S, O> const & o) const {
100 return Time<S, O> (_t + o._t);
103 Time<S, O> & operator+= (Time<S, O> const & o) {
108 Time<S, O> operator- () const {
109 return Time<S, O> (-_t);
112 Time<S, O> operator- (Time<S, O> const & o) const {
113 return Time<S, O> (_t - o._t);
116 Time<S, O> & operator-= (Time<S, O> const & o) {
121 Time<S, O> operator/ (int o) const {
122 return Time<S, O> (_t / o);
125 /** Round up to the nearest sampling interval
126 * at some sampling rate.
127 * @param r Sampling rate.
129 Time<S, O> ceil (double r) const {
130 return Time<S, O> (llrint (HZ * frames_ceil(r) / r));
133 Time<S, O> floor (double r) const {
134 return Time<S, O> (llrint (HZ * frames_floor(r) / r));
137 Time<S, O> round (double r) const {
138 return Time<S, O> (llrint (HZ * frames_round(r) / r));
141 double seconds () const {
142 return double (_t) / HZ;
145 Time<S, O> abs () const {
146 return Time<S, O> (std::abs (_t));
149 template <typename T>
150 int64_t frames_round (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 llrint (_t * double(r) / HZ);
158 template <typename T>
159 int64_t frames_floor (T r) const {
160 return ::floor (_t * r / HZ);
163 template <typename T>
164 int64_t frames_ceil (T r) const {
165 /* We must cast to double here otherwise if T is integer
166 the calculation will round down before we get the chance
169 return ::ceil (_t * double(r) / HZ);
172 /** Split a time into hours, minutes, seconds and frames.
173 * @param r Frames per second.
174 * @param h Returned hours.
175 * @param m Returned minutes.
176 * @param s Returned seconds.
177 * @param f Returned frames.
179 template <typename T>
180 void split (T r, int& h, int& m, int& s, int& f) const
182 /* Do this calculation with frames so that we can round
183 to a frame boundary at the start rather than the end.
185 int64_t ff = frames_round (r);
194 f = static_cast<int> (ff);
197 template <typename T>
198 std::string timecode (T r) const {
203 split (r, h, m, s, f);
206 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
211 static Time<S, O> from_seconds (double s) {
212 return Time<S, O> (llrint (s * HZ));
216 static Time<S, O> from_frames (int64_t f, T r) {
217 DCPOMATIC_ASSERT (r > 0);
218 return Time<S, O> (f * HZ / r);
221 static Time<S, O> delta () {
222 return Time<S, O> (1);
225 static Time<S, O> min () {
226 return Time<S, O> (-INT64_MAX);
229 static Time<S, O> max () {
230 return Time<S, O> (INT64_MAX);
233 static const int HZ = 96000;
236 friend struct ::dcpomatic_time_ceil_test;
237 friend struct ::dcpomatic_time_floor_test;
242 class ContentTimeDifferentiator {};
243 class DCPTimeDifferentiator {};
245 /* Specializations for the two allowed explicit conversions */
248 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
251 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
253 /** Time relative to the start or position of a piece of content in its native frame rate */
254 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
255 /** Time relative to the start of the output DCP in its frame rate */
256 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
264 TimePeriod (T f, T t)
269 /** start time of sampling interval that the period is from */
271 /** start time of next sampling interval after the period */
274 T duration () const {
278 TimePeriod<T> operator+ (T const & o) const {
279 return TimePeriod<T> (from + o, to + o);
282 boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) const {
283 T const max_from = std::max (from, other.from);
284 T const min_to = std::min (to, other.to);
286 if (max_from >= min_to) {
287 return boost::optional<TimePeriod<T> > ();
290 return TimePeriod<T> (max_from, min_to);
293 bool contains (T const & other) const {
294 return (from <= other && other < to);
297 bool operator< (TimePeriod<T> const & o) const {
298 if (from != o.from) {
299 return from < o.from;
304 bool operator== (TimePeriod<T> const & other) const {
305 return from == other.from && to == other.to;
308 bool operator!= (TimePeriod<T> const & other) const {
309 return !(*this == other);
313 /** @param A Period which is subtracted from.
314 * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
317 std::list<TimePeriod<T> > subtract (TimePeriod<T> A, std::list<TimePeriod<T> > const & B)
319 std::list<TimePeriod<T> > result;
320 result.push_back (A);
322 BOOST_FOREACH (TimePeriod<T> i, B) {
323 std::list<TimePeriod<T> > new_result;
324 BOOST_FOREACH (TimePeriod<T> j, result) {
325 boost::optional<TimePeriod<T> > ov = i.overlap (j);
328 /* A contains all of B */
329 if (i.from != j.from) {
330 new_result.push_back (TimePeriod<T> (j.from, i.from));
333 new_result.push_back (TimePeriod<T> (i.to, j.to));
335 } else if (*ov == j) {
336 /* B contains all of A */
337 } else if (i.from < j.from) {
338 /* B overlaps start of A */
339 new_result.push_back (TimePeriod<T> (i.to, j.to));
340 } else if (i.to > j.to) {
341 /* B overlaps end of A */
342 new_result.push_back (TimePeriod<T> (j.from, i.from));
345 new_result.push_back (j);
354 typedef TimePeriod<ContentTime> ContentTimePeriod;
355 typedef TimePeriod<DCPTime> DCPTimePeriod;
357 DCPTime min (DCPTime a, DCPTime b);
358 DCPTime max (DCPTime a, DCPTime b);
359 ContentTime min (ContentTime a, ContentTime b);
360 ContentTime max (ContentTime a, ContentTime b);
361 std::string to_string (ContentTime t);
362 std::string to_string (DCPTime t);
363 std::string to_string (DCPTimePeriod p);