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 bool operator<=(HMSF const& a, HMSF const& b);
70 /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different
71 * versions of this class, dcpomatic::ContentTime and dcpomatic::DCPTime, and we want it to be impossible to
72 * convert implicitly between the two. Hence there's this template hack. I'm not
73 * sure if it's the best way to do it.
75 * S is the name of `this' class and O is its opposite (see the typedefs below).
77 template <class S, class O>
87 explicit Time (Type t)
91 explicit Time (Type n, Type d)
95 /* Explicit conversion from type O */
96 Time (Time<O, S> d, FrameRateChange f);
98 /** @param hmsf Hours, minutes, seconds, frames.
99 * @param fps Frame rate
101 Time (HMSF const& hmsf, float fps) {
102 *this = from_seconds (hmsf.h * 3600)
103 + from_seconds (hmsf.m * 60)
104 + from_seconds (hmsf.s)
105 + from_frames (hmsf.f, fps);
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 bool operator>= (Time<S, O> const & o) const {
132 bool operator> (Time<S, O> const & o) const {
136 Time<S, O> operator+ (Time<S, O> const & o) const {
137 return Time<S, O> (_t + o._t);
140 Time<S, O> & operator+= (Time<S, O> const & o) {
145 Time<S, O> operator- () const {
146 return Time<S, O> (-_t);
149 Time<S, O> operator- (Time<S, O> const & o) const {
150 return Time<S, O> (_t - o._t);
153 Time<S, O> & operator-= (Time<S, O> const & o) {
158 Time<S, O> operator* (int o) const {
159 return Time<S, O> (_t * o);
162 Time<S, O> operator/ (int o) const {
163 return Time<S, O> (_t / o);
166 /** Round up to the nearest sampling interval
167 * at some sampling rate.
168 * @param r Sampling rate.
170 Time<S, O> ceil (double r) const {
171 return Time<S, O> (llrint(HZ * frames_ceil(r) / r));
174 Time<S, O> floor (double r) const {
175 return Time<S, O> (llrint(HZ * frames_floor(r) / r));
178 Time<S, O> round (double r) const {
179 return Time<S, O> (llrint(HZ * frames_round(r) / r));
182 double seconds () const {
183 return double (_t) / HZ;
186 Time<S, O> abs () const {
187 return Time<S, O> (std::abs(_t));
190 template <typename T>
191 int64_t frames_round (T r) const {
192 /* We must cast to double here otherwise if T is integer
193 the calculation will round down before we get the chance
196 return llrint (_t * double(r) / HZ);
199 template <typename T>
200 int64_t frames_floor (T r) const {
201 return ::floor (_t * r / HZ);
204 template <typename T>
205 int64_t frames_ceil (T r) const {
206 /* We must cast to double here otherwise if T is integer
207 the calculation will round down before we get the chance
210 return ::ceil (_t * double(r) / HZ);
213 /** Split a time into hours, minutes, seconds and frames.
214 * @param r Frames per second.
215 * @return Split time.
217 template <typename T>
218 HMSF split (T r) const
220 /* Do this calculation with frames so that we can round
221 to a frame boundary at the start rather than the end.
223 auto ff = frames_round (r);
226 hmsf.h = ff / (3600 * r);
227 ff -= static_cast<int64_t>(hmsf.h) * 3600 * r;
228 hmsf.m = ff / (60 * r);
229 ff -= static_cast<int64_t>(hmsf.m) * 60 * r;
231 ff -= static_cast<int64_t>(hmsf.s) * r;
233 hmsf.f = static_cast<int> (ff);
237 template <typename T>
238 std::string timecode (T r) const {
239 auto hmsf = split (r);
242 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
246 static Time<S, O> from_seconds (double s) {
247 return Time<S, O> (llrint (s * HZ));
251 static Time<S, O> from_frames (int64_t f, T r) {
252 DCPOMATIC_ASSERT (r > 0);
253 return Time<S, O> (f * HZ / r);
256 static Time<S, O> delta () {
257 return Time<S, O> (1);
260 static Time<S, O> min () {
261 return Time<S, O> (-INT64_MAX);
264 static Time<S, O> max () {
265 return Time<S, O> (INT64_MAX);
268 static const int HZ = 96000;
271 friend struct ::dcpomatic_time_ceil_test;
272 friend struct ::dcpomatic_time_floor_test;
278 class ContentTimeDifferentiator {};
279 class DCPTimeDifferentiator {};
282 /* Specializations for the two allowed explicit conversions */
285 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
288 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
291 /** Time relative to the start or position of a piece of content in its native frame rate */
292 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
293 /** Time relative to the start of the output DCP in its frame rate */
294 typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
302 TimePeriod (T f, T t)
307 /** start time of sampling interval that the period is from */
309 /** start time of next sampling interval after the period */
312 T duration () const {
316 TimePeriod<T> operator+ (T const & o) const {
317 return TimePeriod<T> (from + o, to + o);
320 boost::optional<TimePeriod<T>> overlap (TimePeriod<T> const & other) const {
321 T const max_from = std::max (from, other.from);
322 T const min_to = std::min (to, other.to);
324 if (max_from >= min_to) {
328 return TimePeriod<T> (max_from, min_to);
331 bool contains (T const & other) const {
332 return (from <= other && other < to);
335 bool operator< (TimePeriod<T> const & o) const {
336 if (from != o.from) {
337 return from < o.from;
342 bool operator== (TimePeriod<T> const & other) const {
343 return from == other.from && to == other.to;
346 bool operator!= (TimePeriod<T> const & other) const {
347 return !(*this == other);
352 /** @param A Period which is subtracted from.
353 * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap.
356 std::list<TimePeriod<T>> subtract (TimePeriod<T> A, std::list<TimePeriod<T>> const & B)
358 std::list<TimePeriod<T>> result;
359 result.push_back (A);
362 std::list<TimePeriod<T>> new_result;
363 for (auto j: result) {
364 auto ov = i.overlap (j);
367 /* A contains all of B */
368 if (i.from != j.from) {
369 new_result.push_back (TimePeriod<T>(j.from, i.from));
372 new_result.push_back (TimePeriod<T>(i.to, j.to));
374 } else if (*ov == j) {
375 /* B contains all of A */
376 } else if (i.from < j.from) {
377 /* B overlaps start of A */
378 new_result.push_back (TimePeriod<T>(i.to, j.to));
379 } else if (i.to > j.to) {
380 /* B overlaps end of A */
381 new_result.push_back (TimePeriod<T>(j.from, i.from));
384 new_result.push_back (j);
394 typedef TimePeriod<ContentTime> ContentTimePeriod;
395 typedef TimePeriod<DCPTime> DCPTimePeriod;
398 DCPTime min (DCPTime a, DCPTime b);
399 DCPTime max (DCPTime a, DCPTime b);
400 ContentTime min (ContentTime a, ContentTime b);
401 ContentTime max (ContentTime a, ContentTime b);
402 std::string to_string (ContentTime t);
403 std::string to_string (DCPTime t);
404 std::string to_string (DCPTimePeriod p);