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 /** Round up to the nearest sampling interval
160 * at some sampling rate.
161 * @param r Sampling rate.
163 Time<S, O> ceil (double r) const {
164 return Time<S, O> (llrint(HZ * frames_ceil(r) / r));
167 Time<S, O> floor (double r) const {
168 return Time<S, O> (llrint(HZ * frames_floor(r) / r));
171 Time<S, O> round (double r) const {
172 return Time<S, O> (llrint(HZ * frames_round(r) / r));
175 double seconds () const {
176 return double (_t) / HZ;
179 Time<S, O> abs () const {
180 return Time<S, O> (std::abs(_t));
183 template <typename T>
184 int64_t frames_round (T r) const {
185 /* We must cast to double here otherwise if T is integer
186 the calculation will round down before we get the chance
189 return llrint (_t * double(r) / HZ);
192 template <typename T>
193 int64_t frames_floor (T r) const {
194 return ::floor (_t * r / HZ);
197 template <typename T>
198 int64_t frames_ceil (T r) const {
199 /* We must cast to double here otherwise if T is integer
200 the calculation will round down before we get the chance
203 return ::ceil (_t * double(r) / HZ);
206 /** Split a time into hours, minutes, seconds and frames.
207 * @param r Frames per second.
208 * @return Split time.
210 template <typename T>
211 HMSF split (T r) const
213 /* Do this calculation with frames so that we can round
214 to a frame boundary at the start rather than the end.
216 auto ff = frames_round (r);
219 hmsf.h = ff / (3600 * r);
220 ff -= hmsf.h * 3600 * r;
221 hmsf.m = ff / (60 * r);
222 ff -= hmsf.m * 60 * r;
226 hmsf.f = static_cast<int> (ff);
230 template <typename T>
231 std::string timecode (T r) const {
232 auto hmsf = split (r);
235 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
239 static Time<S, O> from_seconds (double s) {
240 return Time<S, O> (llrint (s * HZ));
244 static Time<S, O> from_frames (int64_t f, T r) {
245 DCPOMATIC_ASSERT (r > 0);
246 return Time<S, O> (f * HZ / r);
249 static Time<S, O> delta () {
250 return Time<S, O> (1);
253 static Time<S, O> min () {
254 return Time<S, O> (-INT64_MAX);
257 static Time<S, O> max () {
258 return Time<S, O> (INT64_MAX);
261 static const int HZ = 96000;
264 friend struct ::dcpomatic_time_ceil_test;
265 friend struct ::dcpomatic_time_floor_test;
271 class ContentTimeDifferentiator {};
272 class DCPTimeDifferentiator {};
275 /* Specializations for the two allowed explicit conversions */
278 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
281 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
284 /** Time relative to the start or position of a piece of content in its native frame rate */
285 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
286 /** Time relative to the start of the output DCP in its frame rate */
287 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
295 TimePeriod (T f, T t)
300 /** start time of sampling interval that the period is from */
302 /** start time of next sampling interval after the period */
305 T duration () const {
309 TimePeriod<T> operator+ (T const & o) const {
310 return TimePeriod<T> (from + o, to + o);
313 boost::optional<TimePeriod<T>> overlap (TimePeriod<T> const & other) const {
314 T const max_from = std::max (from, other.from);
315 T const min_to = std::min (to, other.to);
317 if (max_from >= min_to) {
321 return TimePeriod<T> (max_from, min_to);
324 bool contains (T const & other) const {
325 return (from <= other && other < to);
328 bool operator< (TimePeriod<T> const & o) const {
329 if (from != o.from) {
330 return from < o.from;
335 bool operator== (TimePeriod<T> const & other) const {
336 return from == other.from && to == other.to;
339 bool operator!= (TimePeriod<T> const & other) const {
340 return !(*this == other);
345 /** @param A Period which is subtracted from.
346 * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
349 std::list<TimePeriod<T>> subtract (TimePeriod<T> A, std::list<TimePeriod<T>> const & B)
351 std::list<TimePeriod<T>> result;
352 result.push_back (A);
355 std::list<TimePeriod<T>> new_result;
356 for (auto j: result) {
357 auto ov = i.overlap (j);
360 /* A contains all of B */
361 if (i.from != j.from) {
362 new_result.push_back (TimePeriod<T>(j.from, i.from));
365 new_result.push_back (TimePeriod<T>(i.to, j.to));
367 } else if (*ov == j) {
368 /* B contains all of A */
369 } else if (i.from < j.from) {
370 /* B overlaps start of A */
371 new_result.push_back (TimePeriod<T>(i.to, j.to));
372 } else if (i.to > j.to) {
373 /* B overlaps end of A */
374 new_result.push_back (TimePeriod<T>(j.from, i.from));
377 new_result.push_back (j);
387 typedef TimePeriod<ContentTime> ContentTimePeriod;
388 typedef TimePeriod<DCPTime> DCPTimePeriod;
391 DCPTime min (DCPTime a, DCPTime b);
392 DCPTime max (DCPTime a, DCPTime b);
393 ContentTime min (ContentTime a, ContentTime b);
394 ContentTime max (ContentTime a, ContentTime b);
395 std::string to_string (ContentTime t);
396 std::string to_string (DCPTime t);
397 std::string to_string (DCPTimePeriod p);