X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Fdcpomatic_time.h;h=a09dd93e9c179f515b08bfd093f8aaada36f2937;hp=204af3c1eb4cc6df8fbae1c290f9b98577f1e2ab;hb=HEAD;hpb=0bb3c873e28552293e7132c93cdeeef65845596d diff --git a/src/lib/dcpomatic_time.h b/src/lib/dcpomatic_time.h index 204af3c1e..1b12ea901 100644 --- a/src/lib/dcpomatic_time.h +++ b/src/lib/dcpomatic_time.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2014-2018 Carl Hetherington + Copyright (C) 2014-2021 Carl Hetherington This file is part of DCP-o-matic. @@ -18,25 +18,54 @@ */ + /** @file src/lib/dcpomatic_time.h * @brief Types to describe time. */ + #ifndef DCPOMATIC_TIME_H #define DCPOMATIC_TIME_H + #include "frame_rate_change.h" #include "dcpomatic_assert.h" #include -#include #include #include #include #include #include + +struct dcpomatic_time_ceil_test; +struct dcpomatic_time_floor_test; + + +namespace dcpomatic { + + +class HMSF +{ +public: + HMSF () {} + + HMSF (int h_, int m_, int s_, int f_) + : h(h_) + , m(m_) + , s(s_) + , f(f_) + {} + + int h = 0; + int m = 0; + int s = 0; + int f = 0; +}; + + /** A time in seconds, expressed as a number scaled up by Time::HZ. We want two different - * versions of this class, ContentTime and DCPTime, and we want it to be impossible to + * versions of this class, dcpomatic::ContentTime and dcpomatic::DCPTime, and we want it to be impossible to * convert implicitly between the two. Hence there's this template hack. I'm not * sure if it's the best way to do it. * @@ -63,6 +92,16 @@ public: /* Explicit conversion from type O */ Time (Time d, FrameRateChange f); + /** @param hmsf Hours, minutes, seconds, frames. + * @param fps Frame rate + */ + Time (HMSF const& hmsf, float fps) { + *this = from_seconds (hmsf.h * 3600) + + from_seconds (hmsf.m * 60) + + from_seconds (hmsf.s) + + from_frames (hmsf.f, fps); + } + Type get () const { return _t; } @@ -113,16 +152,24 @@ public: return *this; } + Time operator/ (int o) const { + return Time (_t / o); + } + /** Round up to the nearest sampling interval * at some sampling rate. * @param r Sampling rate. */ Time ceil (double r) const { - return Time (llrint (HZ * frames_ceil(r) / r)); + return Time (llrint(HZ * frames_ceil(r) / r)); } Time floor (double r) const { - return Time (llrint (HZ * frames_floor(r) / r)); + return Time (llrint(HZ * frames_floor(r) / r)); + } + + Time round (double r) const { + return Time (llrint(HZ * frames_round(r) / r)); } double seconds () const { @@ -130,7 +177,7 @@ public: } Time abs () const { - return Time (std::abs (_t)); + return Time (std::abs(_t)); } template @@ -158,43 +205,37 @@ public: /** Split a time into hours, minutes, seconds and frames. * @param r Frames per second. - * @param h Returned hours. - * @param m Returned minutes. - * @param s Returned seconds. - * @param f Returned frames. + * @return Split time. */ template - void split (T r, int& h, int& m, int& s, int& f) const + HMSF split (T r) const { /* Do this calculation with frames so that we can round to a frame boundary at the start rather than the end. */ - int64_t ff = frames_round (r); + auto ff = frames_round (r); + HMSF hmsf; - h = ff / (3600 * r); - ff -= h * 3600 * r; - m = ff / (60 * r); - ff -= m * 60 * r; - s = ff / r; - ff -= s * r; + hmsf.h = ff / (3600 * r); + ff -= static_cast(hmsf.h) * 3600 * r; + hmsf.m = ff / (60 * r); + ff -= static_cast(hmsf.m) * 60 * r; + hmsf.s = ff / r; + ff -= static_cast(hmsf.s) * r; - f = static_cast (ff); + hmsf.f = static_cast (ff); + return hmsf; } template std::string timecode (T r) const { - int h; - int m; - int s; - int f; - split (r, h, m, s, f); + auto hmsf = split (r); char buffer[128]; - snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f); + snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f); return buffer; } - static Time from_seconds (double s) { return Time (llrint (s * HZ)); } @@ -217,17 +258,20 @@ public: return Time (INT64_MAX); } + static const int HZ = 96000; + private: - friend struct dcpomatic_time_ceil_test; - friend struct dcpomatic_time_floor_test; + friend struct ::dcpomatic_time_ceil_test; + friend struct ::dcpomatic_time_floor_test; Type _t; - static const int HZ = 96000; }; + class ContentTimeDifferentiator {}; class DCPTimeDifferentiator {}; + /* Specializations for the two allowed explicit conversions */ template<> @@ -236,6 +280,7 @@ Time::Time (Time Time::Time (Time d, FrameRateChange f); + /** Time relative to the start or position of a piece of content in its native frame rate */ typedef Time ContentTime; /** Time relative to the start of the output DCP in its frame rate */ @@ -265,12 +310,12 @@ public: return TimePeriod (from + o, to + o); } - boost::optional > overlap (TimePeriod const & other) const { + boost::optional> overlap (TimePeriod const & other) const { T const max_from = std::max (from, other.from); T const min_to = std::min (to, other.to); if (max_from >= min_to) { - return boost::optional > (); + return {}; } return TimePeriod (max_from, min_to); @@ -296,36 +341,37 @@ public: } }; + /** @param A Period which is subtracted from. * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap. */ template -std::list > subtract (TimePeriod A, std::list > const & B) +std::list> subtract (TimePeriod A, std::list> const & B) { - std::list > result; + std::list> result; result.push_back (A); - BOOST_FOREACH (TimePeriod i, B) { - std::list > new_result; - BOOST_FOREACH (TimePeriod j, result) { - boost::optional > ov = i.overlap (j); + for (auto i: B) { + std::list> new_result; + for (auto j: result) { + auto ov = i.overlap (j); if (ov) { if (*ov == i) { /* A contains all of B */ if (i.from != j.from) { - new_result.push_back (TimePeriod (j.from, i.from)); + new_result.push_back (TimePeriod(j.from, i.from)); } if (i.to != j.to) { - new_result.push_back (TimePeriod (i.to, j.to)); + new_result.push_back (TimePeriod(i.to, j.to)); } } else if (*ov == j) { /* B contains all of A */ } else if (i.from < j.from) { /* B overlaps start of A */ - new_result.push_back (TimePeriod (i.to, j.to)); + new_result.push_back (TimePeriod(i.to, j.to)); } else if (i.to > j.to) { /* B overlaps end of A */ - new_result.push_back (TimePeriod (j.from, i.from)); + new_result.push_back (TimePeriod(j.from, i.from)); } } else { new_result.push_back (j); @@ -337,9 +383,11 @@ std::list > subtract (TimePeriod A, std::list > c return result; } + typedef TimePeriod ContentTimePeriod; typedef TimePeriod DCPTimePeriod; + DCPTime min (DCPTime a, DCPTime b); DCPTime max (DCPTime a, DCPTime b); ContentTime min (ContentTime a, ContentTime b); @@ -348,4 +396,8 @@ std::string to_string (ContentTime t); std::string to_string (DCPTime t); std::string to_string (DCPTimePeriod p); + +} + + #endif