2 Copyright (C) 2014-2017 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 class dcpomatic_round_up_test;
40 /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different
41 * versions of this class, ContentTime and DCPTime, and we want it to be impossible to
42 * convert implicitly between the two. Hence there's this template hack. I'm not
43 * sure if it's the best way to do it.
45 * S is the name of `this' class and O is its opposite (see the typedefs below).
47 template <class S, class O>
57 explicit Time (Type t)
61 explicit Time (Type n, Type d)
65 /* Explicit conversion from type O */
66 Time (Time<O, S> d, FrameRateChange f);
72 bool operator< (Time<S, O> const & o) const {
76 bool operator<= (Time<S, O> const & o) const {
80 bool operator== (Time<S, O> const & o) const {
84 bool operator!= (Time<S, O> const & o) const {
88 bool operator>= (Time<S, O> const & o) const {
92 bool operator> (Time<S, O> const & o) const {
96 Time<S, O> operator+ (Time<S, O> const & o) const {
97 return Time<S, O> (_t + o._t);
100 Time<S, O> & operator+= (Time<S, O> const & o) {
105 Time<S, O> operator- () const {
106 return Time<S, O> (-_t);
109 Time<S, O> operator- (Time<S, O> const & o) const {
110 return Time<S, O> (_t - o._t);
113 Time<S, O> & operator-= (Time<S, O> const & o) {
118 /** Round up to the nearest sampling interval
119 * at some sampling rate.
120 * @param r Sampling rate.
122 Time<S, O> ceil (float r) const {
123 return Time<S, O> (llrint (HZ * frames_ceil(r) / double(r)));
126 Time<S, O> floor (float r) const {
127 return Time<S, O> (llrint (HZ * frames_floor(r) / double(r)));
130 double seconds () const {
131 return double (_t) / HZ;
134 Time<S, O> abs () const {
135 return Time<S, O> (std::abs (_t));
138 template <typename T>
139 int64_t frames_round (T r) const {
140 /* We must cast to double here otherwise if T is integer
141 the calculation will round down before we get the chance
144 return llrint (_t * double(r) / HZ);
147 template <typename T>
148 int64_t frames_floor (T r) const {
149 return ::floor (_t * r / HZ);
152 template <typename T>
153 int64_t frames_ceil (T r) const {
154 /* We must cast to double here otherwise if T is integer
155 the calculation will round down before we get the chance
158 return ::ceil (_t * double(r) / HZ);
161 /** @param r Frames per second */
162 template <typename T>
163 void split (T r, int& h, int& m, int& s, int& f) const
165 /* Do this calculation with frames so that we can round
166 to a frame boundary at the start rather than the end.
168 int64_t ff = frames_round (r);
177 f = static_cast<int> (ff);
180 template <typename T>
181 std::string timecode (T r) const {
186 split (r, h, m, s, f);
189 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
194 static Time<S, O> from_seconds (double s) {
195 return Time<S, O> (llrint (s * HZ));
199 static Time<S, O> from_frames (int64_t f, T r) {
200 DCPOMATIC_ASSERT (r > 0);
201 return Time<S, O> (f * HZ / r);
204 static Time<S, O> delta () {
205 return Time<S, O> (1);
208 static Time<S, O> min () {
209 return Time<S, O> (-INT64_MAX);
212 static Time<S, O> max () {
213 return Time<S, O> (INT64_MAX);
217 friend struct dcptime_ceil_test;
218 friend struct dcptime_floor_test;
221 static const int HZ = 96000;
224 class ContentTimeDifferentiator {};
225 class DCPTimeDifferentiator {};
227 /* Specializations for the two allowed explicit conversions */
230 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
233 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
235 /** Time relative to the start or position of a piece of content in its native frame rate */
236 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
237 /** Time relative to the start of the output DCP in its frame rate */
238 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
246 TimePeriod (T f, T t)
251 /** start time of sampling interval that the period is from */
253 /** start time of next sampling interval after the period */
256 T duration () const {
260 TimePeriod<T> operator+ (T const & o) const {
261 return TimePeriod<T> (from + o, to + o);
264 boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) const {
265 T const max_from = std::max (from, other.from);
266 T const min_to = std::min (to, other.to);
268 if (max_from >= min_to) {
269 return boost::optional<TimePeriod<T> > ();
272 return TimePeriod<T> (max_from, min_to);
275 bool contains (T const & other) const {
276 return (from <= other && other < to);
279 bool operator< (TimePeriod<T> const & o) const {
280 if (from != o.from) {
281 return from < o.from;
286 bool operator== (TimePeriod<T> const & other) const {
287 return from == other.from && to == other.to;
290 bool operator!= (TimePeriod<T> const & other) const {
291 return !(*this == other);
295 /** @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap */
297 std::list<TimePeriod<T> > subtract (TimePeriod<T> A, std::list<TimePeriod<T> > const & B)
299 std::list<TimePeriod<T> > result;
300 result.push_back (A);
302 BOOST_FOREACH (TimePeriod<T> i, B) {
303 std::list<TimePeriod<T> > new_result;
304 BOOST_FOREACH (TimePeriod<T> j, result) {
305 boost::optional<TimePeriod<T> > ov = i.overlap (j);
308 /* A contains all of B */
309 if (i.from != j.from) {
310 new_result.push_back (TimePeriod<T> (j.from, i.from));
313 new_result.push_back (TimePeriod<T> (i.to, j.to));
315 } else if (*ov == j) {
316 /* B contains all of A */
317 } else if (i.from < j.from) {
318 /* B overlaps start of A */
319 new_result.push_back (TimePeriod<T> (i.to, j.to));
320 } else if (i.to > j.to) {
321 /* B overlaps end of A */
322 new_result.push_back (TimePeriod<T> (j.from, i.from));
325 new_result.push_back (j);
334 typedef TimePeriod<ContentTime> ContentTimePeriod;
335 typedef TimePeriod<DCPTime> DCPTimePeriod;
337 DCPTime min (DCPTime a, DCPTime b);
338 DCPTime max (DCPTime a, DCPTime b);
339 ContentTime min (ContentTime a, ContentTime b);
340 ContentTime max (ContentTime a, ContentTime b);
341 std::string to_string (ContentTime t);
342 std::string to_string (DCPTime t);
343 std::string to_string (DCPTimePeriod p);