2 Copyright (C) 2014-2021 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/>.
22 /** @file src/lib/dcpomatic_time.h
23 * @brief Types to describe time.
27 #ifndef DCPOMATIC_TIME_H
28 #define DCPOMATIC_TIME_H
31 #include "frame_rate_change.h"
32 #include "dcpomatic_assert.h"
33 #include <boost/optional.hpp>
41 struct dcpomatic_time_ceil_test;
42 struct dcpomatic_time_floor_test;
53 HMSF (int h_, int m_, int s_, int f_)
67 /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different
68 * versions of this class, dcpomatic::ContentTime and dcpomatic::DCPTime, and we want it to be impossible to
69 * convert implicitly between the two. Hence there's this template hack. I'm not
70 * sure if it's the best way to do it.
72 * S is the name of `this' class and O is its opposite (see the typedefs below).
74 template <class S, class O>
84 explicit Time (Type t)
88 explicit Time (Type n, Type d)
92 /* Explicit conversion from type O */
93 Time (Time<O, S> d, FrameRateChange f);
95 /** @param hmsf Hours, minutes, seconds, frames.
96 * @param fps Frame rate
98 Time (HMSF const& hmsf, float fps) {
99 *this = from_seconds (hmsf.h * 3600)
100 + from_seconds (hmsf.m * 60)
101 + from_seconds (hmsf.s)
102 + from_frames (hmsf.f, fps);
109 bool operator< (Time<S, O> const & o) const {
113 bool operator<= (Time<S, O> const & o) const {
117 bool operator== (Time<S, O> const & o) const {
121 bool operator!= (Time<S, O> const & o) const {
125 bool operator>= (Time<S, O> const & o) const {
129 bool operator> (Time<S, O> const & o) const {
133 Time<S, O> operator+ (Time<S, O> const & o) const {
134 return Time<S, O> (_t + o._t);
137 Time<S, O> & operator+= (Time<S, O> const & o) {
142 Time<S, O> operator- () const {
143 return Time<S, O> (-_t);
146 Time<S, O> operator- (Time<S, O> const & o) const {
147 return Time<S, O> (_t - o._t);
150 Time<S, O> & operator-= (Time<S, O> const & o) {
155 Time<S, O> operator* (int o) const {
156 return Time<S, O> (_t * o);
159 Time<S, O> operator/ (int o) const {
160 return Time<S, O> (_t / o);
163 /** Round up to the nearest sampling interval
164 * at some sampling rate.
165 * @param r Sampling rate.
167 Time<S, O> ceil (double r) const {
168 return Time<S, O> (llrint(HZ * frames_ceil(r) / r));
171 Time<S, O> floor (double r) const {
172 return Time<S, O> (llrint(HZ * frames_floor(r) / r));
175 Time<S, O> round (double r) const {
176 return Time<S, O> (llrint(HZ * frames_round(r) / r));
179 double seconds () const {
180 return double (_t) / HZ;
183 Time<S, O> abs () const {
184 return Time<S, O> (std::abs(_t));
187 template <typename T>
188 int64_t frames_round (T r) const {
189 /* We must cast to double here otherwise if T is integer
190 the calculation will round down before we get the chance
193 return llrint (_t * double(r) / HZ);
196 template <typename T>
197 int64_t frames_floor (T r) const {
198 return ::floor (_t * r / HZ);
201 template <typename T>
202 int64_t frames_ceil (T r) const {
203 /* We must cast to double here otherwise if T is integer
204 the calculation will round down before we get the chance
207 return ::ceil (_t * double(r) / HZ);
210 /** Split a time into hours, minutes, seconds and frames.
211 * @param r Frames per second.
212 * @return Split time.
214 template <typename T>
215 HMSF split (T r) const
217 /* Do this calculation with frames so that we can round
218 to a frame boundary at the start rather than the end.
220 auto ff = frames_round (r);
223 hmsf.h = ff / (3600 * r);
224 ff -= hmsf.h * 3600 * r;
225 hmsf.m = ff / (60 * r);
226 ff -= hmsf.m * 60 * r;
230 hmsf.f = static_cast<int> (ff);
234 template <typename T>
235 std::string timecode (T r) const {
236 auto hmsf = split (r);
239 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
243 static Time<S, O> from_seconds (double s) {
244 return Time<S, O> (llrint (s * HZ));
248 static Time<S, O> from_frames (int64_t f, T r) {
249 DCPOMATIC_ASSERT (r > 0);
250 return Time<S, O> (f * HZ / r);
253 static Time<S, O> delta () {
254 return Time<S, O> (1);
257 static Time<S, O> min () {
258 return Time<S, O> (-INT64_MAX);
261 static Time<S, O> max () {
262 return Time<S, O> (INT64_MAX);
265 static const int HZ = 96000;
268 friend struct ::dcpomatic_time_ceil_test;
269 friend struct ::dcpomatic_time_floor_test;
275 class ContentTimeDifferentiator {};
276 class DCPTimeDifferentiator {};
279 /* Specializations for the two allowed explicit conversions */
282 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
285 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
288 /** Time relative to the start or position of a piece of content in its native frame rate */
289 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
290 /** Time relative to the start of the output DCP in its frame rate */
291 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
299 TimePeriod (T f, T t)
304 /** start time of sampling interval that the period is from */
306 /** start time of next sampling interval after the period */
309 T duration () const {
313 TimePeriod<T> operator+ (T const & o) const {
314 return TimePeriod<T> (from + o, to + o);
317 boost::optional<TimePeriod<T>> overlap (TimePeriod<T> const & other) const {
318 T const max_from = std::max (from, other.from);
319 T const min_to = std::min (to, other.to);
321 if (max_from >= min_to) {
325 return TimePeriod<T> (max_from, min_to);
328 bool contains (T const & other) const {
329 return (from <= other && other < to);
332 bool operator< (TimePeriod<T> const & o) const {
333 if (from != o.from) {
334 return from < o.from;
339 bool operator== (TimePeriod<T> const & other) const {
340 return from == other.from && to == other.to;
343 bool operator!= (TimePeriod<T> const & other) const {
344 return !(*this == other);
349 /** @param A Period which is subtracted from.
350 * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
353 std::list<TimePeriod<T>> subtract (TimePeriod<T> A, std::list<TimePeriod<T>> const & B)
355 std::list<TimePeriod<T>> result;
356 result.push_back (A);
359 std::list<TimePeriod<T>> new_result;
360 for (auto j: result) {
361 auto ov = i.overlap (j);
364 /* A contains all of B */
365 if (i.from != j.from) {
366 new_result.push_back (TimePeriod<T>(j.from, i.from));
369 new_result.push_back (TimePeriod<T>(i.to, j.to));
371 } else if (*ov == j) {
372 /* B contains all of A */
373 } else if (i.from < j.from) {
374 /* B overlaps start of A */
375 new_result.push_back (TimePeriod<T>(i.to, j.to));
376 } else if (i.to > j.to) {
377 /* B overlaps end of A */
378 new_result.push_back (TimePeriod<T>(j.from, i.from));
381 new_result.push_back (j);
391 typedef TimePeriod<ContentTime> ContentTimePeriod;
392 typedef TimePeriod<DCPTime> DCPTimePeriod;
395 DCPTime min (DCPTime a, DCPTime b);
396 DCPTime max (DCPTime a, DCPTime b);
397 ContentTime min (ContentTime a, ContentTime b);
398 ContentTime max (ContentTime a, ContentTime b);
399 std::string to_string (ContentTime t);
400 std::string to_string (DCPTime t);
401 std::string to_string (DCPTimePeriod p);