Supporters update.
[dcpomatic.git] / src / lib / dcpomatic_time.h
index e6ca493b2bb921e58fbe0fb5c7e7f3c23c9aaf4c..1b12ea901653ead8afd3f935679eaea8ecf6e57a 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
+
 /** @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 <locked_sstream.h>
+#include <boost/optional.hpp>
 #include <stdint.h>
 #include <cmath>
 #include <ostream>
 #include <iomanip>
+#include <cstdio>
+
+
+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;
+};
 
-class dcpomatic_round_up_test;
 
 /** 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<O, S> 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,14 +152,24 @@ public:
                return *this;
        }
 
+       Time<S, O> operator/ (int o) const {
+               return Time<S, O> (_t / o);
+       }
+
        /** Round up to the nearest sampling interval
         *  at some sampling rate.
         *  @param r Sampling rate.
         */
-       Time<S, O> round_up (float r) const {
-               Type const n = llrintf (HZ / r);
-               Type const a = _t + n - 1;
-               return Time<S, O> (a - (a % n));
+       Time<S, O> ceil (double r) const {
+               return Time<S, O> (llrint(HZ * frames_ceil(r) / r));
+       }
+
+       Time<S, O> floor (double r) const {
+               return Time<S, O> (llrint(HZ * frames_floor(r) / r));
+       }
+
+       Time<S, O> round (double r) const {
+               return Time<S, O> (llrint(HZ * frames_round(r) / r));
        }
 
        double seconds () const {
@@ -128,7 +177,7 @@ public:
        }
 
        Time<S, O> abs () const {
-               return Time<S, O> (std::abs (_t));
+               return Time<S, O> (std::abs(_t));
        }
 
        template <typename T>
@@ -142,7 +191,7 @@ public:
 
        template <typename T>
        int64_t frames_floor (T r) const {
-               return floor (_t * r / HZ);
+               return ::floor (_t * r / HZ);
        }
 
        template <typename T>
@@ -151,47 +200,42 @@ public:
                   the calculation will round down before we get the chance
                   to ceil().
                */
-               return ceil (_t * double(r) / HZ);
+               return ::ceil (_t * double(r) / HZ);
        }
 
-       /** @param r Frames per second */
+       /** Split a time into hours, minutes, seconds and frames.
+        *  @param r Frames per second.
+        *  @return Split time.
+        */
        template <typename T>
-       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);
-
-               h = ff / (3600 * r);
-               ff -= h * 3600 * r;
-               m = ff / (60 * r);
-               ff -= m * 60 * r;
-               s = ff / r;
-               ff -= s * r;
-
-               f = static_cast<int> (ff);
+               auto ff = frames_round (r);
+               HMSF hmsf;
+
+               hmsf.h = ff / (3600 * r);
+               ff -= static_cast<int64_t>(hmsf.h) * 3600 * r;
+               hmsf.m = ff / (60 * r);
+               ff -= static_cast<int64_t>(hmsf.m) * 60 * r;
+               hmsf.s = ff / r;
+               ff -= static_cast<int64_t>(hmsf.s) * r;
+
+               hmsf.f = static_cast<int> (ff);
+               return hmsf;
        }
 
        template <typename T>
        std::string timecode (T r) const {
-               int h;
-               int m;
-               int s;
-               int f;
-               split (r, h, m, s, f);
+               auto hmsf = split (r);
 
-               locked_stringstream o;
-               o.width (2);
-               o.fill ('0');
-               o << std::setw(2) << std::setfill('0') << h << ":"
-                 << std::setw(2) << std::setfill('0') << m << ":"
-                 << std::setw(2) << std::setfill('0') << s << ":"
-                 << std::setw(2) << std::setfill('0') << f;
-               return o.str ();
+               char buffer[128];
+               snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
+               return buffer;
        }
 
-
        static Time<S, O> from_seconds (double s) {
                return Time<S, O> (llrint (s * HZ));
        }
@@ -214,16 +258,20 @@ public:
                return Time<S, O> (INT64_MAX);
        }
 
+       static const int HZ = 96000;
+
 private:
-       friend struct dcptime_round_up_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<>
@@ -232,6 +280,7 @@ Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDiffer
 template<>
 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
 
+
 /** Time relative to the start or position of a piece of content in its native frame rate */
 typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
 /** Time relative to the start of the output DCP in its frame rate */
@@ -261,28 +310,94 @@ public:
                return TimePeriod<T> (from + o, to + o);
        }
 
-       bool overlaps (TimePeriod<T> const & other) const {
-               return (from < other.to && to > other.from);
+       boost::optional<TimePeriod<T>> overlap (TimePeriod<T> 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 {};
+               }
+
+               return TimePeriod<T> (max_from, min_to);
        }
 
        bool contains (T const & other) const {
                return (from <= other && other < to);
        }
 
+       bool operator< (TimePeriod<T> const & o) const {
+               if (from != o.from) {
+                       return from < o.from;
+               }
+               return to < o.to;
+       }
+
        bool operator== (TimePeriod<T> const & other) const {
                return from == other.from && to == other.to;
        }
+
+       bool operator!= (TimePeriod<T> const & other) const {
+               return !(*this == other);
+       }
 };
 
+
+/** @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 <class T>
+std::list<TimePeriod<T>> subtract (TimePeriod<T> A, std::list<TimePeriod<T>> const & B)
+{
+       std::list<TimePeriod<T>> result;
+       result.push_back (A);
+
+       for (auto i: B) {
+               std::list<TimePeriod<T>> 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<T>(j.from, i.from));
+                                       }
+                                       if (i.to != j.to) {
+                                               new_result.push_back (TimePeriod<T>(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<T>(i.to, j.to));
+                               } else if (i.to > j.to) {
+                                       /* B overlaps end of A */
+                                       new_result.push_back (TimePeriod<T>(j.from, i.from));
+                               }
+                       } else {
+                               new_result.push_back (j);
+                       }
+               }
+               result = new_result;
+       }
+
+       return result;
+}
+
+
 typedef TimePeriod<ContentTime> ContentTimePeriod;
 typedef TimePeriod<DCPTime> DCPTimePeriod;
 
+
 DCPTime min (DCPTime a, DCPTime b);
 DCPTime max (DCPTime a, DCPTime b);
 ContentTime min (ContentTime a, ContentTime b);
 ContentTime max (ContentTime a, ContentTime b);
-std::ostream& operator<< (std::ostream& s, ContentTime t);
-std::ostream& operator<< (std::ostream& s, DCPTime t);
-std::ostream& operator<< (std::ostream& s, DCPTimePeriod p);
+std::string to_string (ContentTime t);
+std::string to_string (DCPTime t);
+std::string to_string (DCPTimePeriod p);
+
+
+}
+
 
 #endif