summaryrefslogtreecommitdiff
path: root/src/dcp_time.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2015-01-08 23:22:05 +0000
committerCarl Hetherington <cth@carlh.net>2015-01-08 23:22:05 +0000
commit7ca029dea19ab0aa1b7e96d363fe6de61350d2e7 (patch)
treed520d2f6597fa4ccbe2a78e3aea4d1d2614d9373 /src/dcp_time.cc
parentbbb3db16cc7e6f2262a89da4bec9fc356d6c3c12 (diff)
Change libdcp::Time to allow sub-second units to be anything, so that
we can support SMPTE subtitles which use TimeCodeRate as the base rather than the arbitrary "ticks" (4ms) of Interop.
Diffstat (limited to 'src/dcp_time.cc')
-rw-r--r--src/dcp_time.cc109
1 files changed, 61 insertions, 48 deletions
diff --git a/src/dcp_time.cc b/src/dcp_time.cc
index 428215cb..1f3ff06d 100644
--- a/src/dcp_time.cc
+++ b/src/dcp_time.cc
@@ -1,5 +1,5 @@
/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -33,31 +33,26 @@ using namespace std;
using namespace boost;
using namespace libdcp;
-Time::Time (int frame, int frames_per_second)
+Time::Time (int frame, int frames_per_second, int tcr_)
: h (0)
, m (0)
, s (0)
- , t (0)
+ , e (0)
+ , tcr (tcr_)
{
- set (double (frame) / frames_per_second);
-}
-
-Time::Time (int64_t ticks)
-{
- h = ticks / (60 * 60 * 250);
- ticks -= int64_t (h) * 60 * 60 * 250;
- m = ticks / (60 * 250);
- ticks -= int64_t (m) * 60 * 250;
- s = ticks / 250;
- ticks -= int64_t (s) * 250;
- t = ticks;
+ set (double (frame) / frames_per_second, tcr);
}
+/** @param s_ Seconds.
+ * @param tcr Timecode rate.
+ */
void
-Time::set (double ss)
+Time::set (double seconds, int tcr_)
{
- s = floor (ss);
- t = int (round (1000 * (ss - s) / 4));
+ s = floor (seconds);
+ tcr = tcr_;
+
+ e = int (round ((seconds - s) * tcr));
if (s >= 60) {
m = s / 60;
@@ -70,7 +65,8 @@ Time::set (double ss)
}
}
-Time::Time (string time)
+Time::Time (string time, int tcr_)
+ : tcr (tcr_)
{
vector<string> b;
split (b, time, is_any_of (":"));
@@ -81,13 +77,13 @@ Time::Time (string time)
h = raw_convert<int> (b[0]);
m = raw_convert<int> (b[1]);
s = raw_convert<int> (b[2]);
- t = raw_convert<int> (b[3]);
+ e = raw_convert<int> (b[3]);
}
bool
libdcp::operator== (Time const & a, Time const & b)
{
- return (a.h == b.h && a.m == b.m && a.s == b.s && a.t == b.t);
+ return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
}
bool
@@ -117,8 +113,8 @@ libdcp::operator< (Time const & a, Time const & b)
return a.s < b.s;
}
- if (a.t != b.t) {
- return a.t < b.t;
+ if ((a.e * b.tcr) != (b.e * a.tcr)) {
+ return (a.e * b.tcr) < (b.e * a.tcr);
}
return true;
@@ -139,8 +135,8 @@ libdcp::operator> (Time const & a, Time const & b)
return a.s > b.s;
}
- if (a.t != b.t) {
- return a.t > b.t;
+ if ((a.e * b.tcr) != (b.e * a.tcr)) {
+ return (a.e * b.tcr) > (b.e * a.tcr);
}
return true;
@@ -152,21 +148,23 @@ libdcp::operator>= (Time const & a, Time const & b)
return a == b || a > b;
}
-ostream &
-libdcp::operator<< (ostream& s, Time const & t)
-{
- s << t.h << ":" << t.m << ":" << t.s << "." << t.t;
- return s;
-}
-
libdcp::Time
-libdcp::operator+ (Time a, Time const & b)
+libdcp::operator+ (Time a, Time b)
{
Time r;
- r.t = a.t + b.t;
- if (r.t >= 250) {
- r.t -= 250;
+ /* Make sure we have a common tcr */
+ if (a.tcr != b.tcr) {
+ a.e *= b.tcr;
+ b.e *= a.tcr;
+ r.tcr = a.tcr * b.tcr;
+ } else {
+ r.tcr = a.tcr;
+ }
+
+ r.e = a.e + b.e;
+ if (r.e >= r.tcr) {
+ r.e -= r.tcr;
r.s++;
}
@@ -188,13 +186,22 @@ libdcp::operator+ (Time a, Time const & b)
}
libdcp::Time
-libdcp::operator- (Time a, Time const & b)
+libdcp::operator- (Time a, Time b)
{
Time r;
- r.t = a.t - b.t;
- if (r.t < 0) {
- r.t += 250;
+ /* Make sure we have a common tcr */
+ if (a.tcr != b.tcr) {
+ a.e *= b.tcr;
+ b.e *= a.tcr;
+ r.tcr = a.tcr * b.tcr;
+ } else {
+ r.tcr = a.tcr;
+ }
+
+ r.e = a.e - b.e;
+ if (r.e < 0) {
+ r.e += a.tcr;
r.s--;
}
@@ -218,24 +225,30 @@ libdcp::operator- (Time a, Time const & b)
float
libdcp::operator/ (Time a, Time const & b)
{
- int64_t const at = a.h * 3600 * 250 + a.m * 60 * 250 + a.s * 250 + a.t;
- int64_t const bt = b.h * 3600 * 250 + b.m * 60 * 250 + b.s * 250 + b.t;
- return float (at) / bt;
+ float const as = a.h * 3600 * 250 + a.m * 60 * 250 + a.s * float (a.e) / a.tcr;
+ float const bs = b.h * 3600 * 250 + b.m * 60 * 250 + b.s * float (b.e) / b.tcr;
+ return as / bs;
}
-/** @return A string of the form h:m:s:t */
+/** @return A string of the form h:m:s:e */
string
Time::to_string () const
{
stringstream str;
- str << h << ":" << m << ":" << s << ":" << t;
+ str << h << ":" << m << ":" << s << ":" << e;
return str.str ();
}
-/** @return This time in ticks */
int64_t
-Time::to_ticks () const
+Time::to_editable_units (int tcr_) const
+{
+ return (int64_t(e) * float (tcr_ / tcr)) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
+}
+
+ostream &
+libdcp::operator<< (ostream& s, Time const & t)
{
- return int64_t(t) + int64_t(s) * 250 + int64_t(m) * 60 * 250 + int64_t(h) * 60 * 60 * 250;
+ s << t.to_string ();
+ return s;
}