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;
48 HMSF (int h_, int m_, int s_, int f_)
62 /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different
63 * versions of this class, dcpomatic::ContentTime and dcpomatic::DCPTime, and we want it to be impossible to
64 * convert implicitly between the two. Hence there's this template hack. I'm not
65 * sure if it's the best way to do it.
67 * S is the name of `this' class and O is its opposite (see the typedefs below).
69 template <class S, class O>
79 explicit Time (Type t)
83 explicit Time (Type n, Type d)
87 /* Explicit conversion from type O */
88 Time (Time<O, S> d, FrameRateChange f);
90 /** @param hmsf Hours, minutes, seconds, frames.
91 * @param fps Frame rate
93 Time (HMSF const& hmsf, float fps) {
94 *this = from_seconds (hmsf.h * 3600)
95 + from_seconds (hmsf.m * 60)
96 + from_seconds (hmsf.s)
97 + from_frames (hmsf.f, fps);
104 bool operator< (Time<S, O> const & o) const {
108 bool operator<= (Time<S, O> const & o) const {
112 bool operator== (Time<S, O> const & o) const {
116 bool operator!= (Time<S, O> const & o) const {
120 bool operator>= (Time<S, O> const & o) const {
124 bool operator> (Time<S, O> const & o) const {
128 Time<S, O> operator+ (Time<S, O> const & o) const {
129 return Time<S, O> (_t + o._t);
132 Time<S, O> & operator+= (Time<S, O> const & o) {
137 Time<S, O> operator- () const {
138 return Time<S, O> (-_t);
141 Time<S, O> operator- (Time<S, O> const & o) const {
142 return Time<S, O> (_t - o._t);
145 Time<S, O> & operator-= (Time<S, O> const & o) {
150 Time<S, O> operator/ (int o) const {
151 return Time<S, O> (_t / o);
154 /** Round up to the nearest sampling interval
155 * at some sampling rate.
156 * @param r Sampling rate.
158 Time<S, O> ceil (double r) const {
159 return Time<S, O> (llrint (HZ * frames_ceil(r) / r));
162 Time<S, O> floor (double r) const {
163 return Time<S, O> (llrint (HZ * frames_floor(r) / r));
166 Time<S, O> round (double r) const {
167 return Time<S, O> (llrint (HZ * frames_round(r) / r));
170 double seconds () const {
171 return double (_t) / HZ;
174 Time<S, O> abs () const {
175 return Time<S, O> (std::abs (_t));
178 template <typename T>
179 int64_t frames_round (T r) const {
180 /* We must cast to double here otherwise if T is integer
181 the calculation will round down before we get the chance
184 return llrint (_t * double(r) / HZ);
187 template <typename T>
188 int64_t frames_floor (T r) const {
189 return ::floor (_t * r / HZ);
192 template <typename T>
193 int64_t frames_ceil (T r) const {
194 /* We must cast to double here otherwise if T is integer
195 the calculation will round down before we get the chance
198 return ::ceil (_t * double(r) / HZ);
201 /** Split a time into hours, minutes, seconds and frames.
202 * @param r Frames per second.
203 * @return Split time.
205 template <typename T>
206 HMSF split (T r) const
208 /* Do this calculation with frames so that we can round
209 to a frame boundary at the start rather than the end.
211 auto ff = frames_round (r);
214 hmsf.h = ff / (3600 * r);
215 ff -= hmsf.h * 3600 * r;
216 hmsf.m = ff / (60 * r);
217 ff -= hmsf.m * 60 * r;
221 hmsf.f = static_cast<int> (ff);
225 template <typename T>
226 std::string timecode (T r) const {
227 auto hmsf = split (r);
230 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
235 static Time<S, O> from_seconds (double s) {
236 return Time<S, O> (llrint (s * HZ));
240 static Time<S, O> from_frames (int64_t f, T r) {
241 DCPOMATIC_ASSERT (r > 0);
242 return Time<S, O> (f * HZ / r);
245 static Time<S, O> delta () {
246 return Time<S, O> (1);
249 static Time<S, O> min () {
250 return Time<S, O> (-INT64_MAX);
253 static Time<S, O> max () {
254 return Time<S, O> (INT64_MAX);
257 static const int HZ = 96000;
260 friend struct ::dcpomatic_time_ceil_test;
261 friend struct ::dcpomatic_time_floor_test;
266 class ContentTimeDifferentiator {};
267 class DCPTimeDifferentiator {};
269 /* Specializations for the two allowed explicit conversions */
272 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
275 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
277 /** Time relative to the start or position of a piece of content in its native frame rate */
278 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
279 /** Time relative to the start of the output DCP in its frame rate */
280 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
288 TimePeriod (T f, T t)
293 /** start time of sampling interval that the period is from */
295 /** start time of next sampling interval after the period */
298 T duration () const {
302 TimePeriod<T> operator+ (T const & o) const {
303 return TimePeriod<T> (from + o, to + o);
306 boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) const {
307 T const max_from = std::max (from, other.from);
308 T const min_to = std::min (to, other.to);
310 if (max_from >= min_to) {
311 return boost::optional<TimePeriod<T> > ();
314 return TimePeriod<T> (max_from, min_to);
317 bool contains (T const & other) const {
318 return (from <= other && other < to);
321 bool operator< (TimePeriod<T> const & o) const {
322 if (from != o.from) {
323 return from < o.from;
328 bool operator== (TimePeriod<T> const & other) const {
329 return from == other.from && to == other.to;
332 bool operator!= (TimePeriod<T> const & other) const {
333 return !(*this == other);
337 /** @param A Period which is subtracted from.
338 * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
341 std::list<TimePeriod<T> > subtract (TimePeriod<T> A, std::list<TimePeriod<T> > const & B)
343 std::list<TimePeriod<T> > result;
344 result.push_back (A);
347 std::list<TimePeriod<T> > new_result;
348 for (auto j: result) {
349 boost::optional<TimePeriod<T> > ov = i.overlap (j);
352 /* A contains all of B */
353 if (i.from != j.from) {
354 new_result.push_back (TimePeriod<T> (j.from, i.from));
357 new_result.push_back (TimePeriod<T> (i.to, j.to));
359 } else if (*ov == j) {
360 /* B contains all of A */
361 } else if (i.from < j.from) {
362 /* B overlaps start of A */
363 new_result.push_back (TimePeriod<T> (i.to, j.to));
364 } else if (i.to > j.to) {
365 /* B overlaps end of A */
366 new_result.push_back (TimePeriod<T> (j.from, i.from));
369 new_result.push_back (j);
378 typedef TimePeriod<ContentTime> ContentTimePeriod;
379 typedef TimePeriod<DCPTime> DCPTimePeriod;
381 DCPTime min (DCPTime a, DCPTime b);
382 DCPTime max (DCPTime a, DCPTime b);
383 ContentTime min (ContentTime a, ContentTime b);
384 ContentTime max (ContentTime a, ContentTime b);
385 std::string to_string (ContentTime t);
386 std::string to_string (DCPTime t);
387 std::string to_string (DCPTimePeriod p);