X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fdcp_time.cc;h=0bd0a4096c96d47f5e0d74421ceeeabf1f31dbc0;hb=2c1faeb15715794525f48110c2b8a9df96b387c1;hp=605c4a4028d82e8fe1c3fa3cf46b301cacb891d6;hpb=41fc6e366ad3de85704c35961beaf539ab9ceb8b;p=libdcp.git diff --git a/src/dcp_time.cc b/src/dcp_time.cc index 605c4a40..0bd0a409 100644 --- a/src/dcp_time.cc +++ b/src/dcp_time.cc @@ -1,123 +1,198 @@ /* - Copyright (C) 2012 Carl Hetherington + Copyright (C) 2012-2021 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of libdcp. + + libdcp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - This program is distributed in the hope that it will be useful, + libdcp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - + along with libdcp. If not, see . + + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. */ + /** @file src/dcp_time.cc - * @brief A representation of time within a DCP. + * @brief Time class */ + +#include "raw_convert.h" +#include "dcp_time.h" +#include "exceptions.h" +#include "compose.hpp" +#include "dcp_assert.h" +#include +#include #include #include -#include -#include #include -#include "dcp_time.h" -#include "exceptions.h" + using namespace std; using namespace boost; using namespace dcp; -Time::Time (int frame, int frames_per_second) - : h (0) - , m (0) - , s (0) - , t (0) + +Time::Time (int frame, double frames_per_second, int tcr_) { - set (double (frame) / frames_per_second); + set (double (frame) / frames_per_second, tcr_); } -Time::Time (int64_t ticks) + +Time::Time (double seconds, int tcr_) { - 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 (seconds, tcr_); } + +/** Construct a Time with specified timecode rate and using the supplied + * number of seconds. + * + * @param seconds A number of seconds. + * @param tcr_ Timecode rate to use. + */ void -Time::set (double ss) +Time::set (double seconds, int tcr_) { - t = (int (round (ss * 1000)) % 1000) / 4; - s = floor (ss); + s = floor (seconds); + tcr = tcr_; + + e = int (round ((seconds - s) * tcr)); - if (s > 60) { + if (s >= 60) { m = s / 60; s -= m * 60; + } else { + m = 0; } - if (m > 60) { + if (m >= 60) { h = m / 60; m -= h * 60; + } else { + h = 0; } } -Time::Time (string time) + +Time::Time (string time, optional tcr_) { vector b; split (b, time, is_any_of (":")); - if (b.size() != 4) { - boost::throw_exception (DCPReadError ("unrecognised time specification")); + + if (b.size() < 3 || b[0].empty() || b[1].empty() || b[0].length() > 2 || b[1].length() > 2) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time))); + } + + if (!tcr_) { + /* Interop */ + if (b.size() == 3) { + /* HH:MM:SS.s[s[s]] */ + vector bs; + split (bs, b[2], is_any_of (".")); + if (bs.size() != 2) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time))); + } + + h = raw_convert (b[0]); + m = raw_convert (b[1]); + if (bs[0].empty() || bs[0].length() > 2) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[0]))); + } + s = raw_convert (bs[0]); + if (bs[1].empty() || bs[1].length() > 3) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[1]))); + } + e = raw_convert (bs[1]); + tcr = 1000; + } else if (b.size() == 4) { + /* HH:MM:SS:EE[E] */ + h = raw_convert (b[0]); + m = raw_convert (b[1]); + if (b[2].empty() || b[2].length() > 2) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2]))); + } + s = raw_convert (b[2]); + if (b[3].empty() || b[3].length() > 3) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3]))); + } + e = raw_convert (b[3]); + tcr = 250; + } else { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time))); + } + + } else { + /* SMPTE: HH:MM:SS:EE */ + split (b, time, is_any_of (":")); + if (b.size() != 4) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; does not have 4 parts", time))); + } + + h = raw_convert (b[0]); + m = raw_convert (b[1]); + if (b[2].empty() || b[2].length() > 2) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2]))); + } + s = raw_convert (b[2]); + if (b[3].empty() || b[3].length() > 2) { + boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3]))); + } + e = raw_convert (b[3]); + tcr = tcr_.get(); } - - h = lexical_cast (b[0]); - m = lexical_cast (b[1]); - s = lexical_cast (b[2]); - t = lexical_cast (b[3]); } + bool dcp::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 dcp::operator!= (Time const & a, Time const & b) { return !(a == b); } + bool dcp::operator<= (Time const & a, Time const & b) { - if (a.h != b.h) { - return a.h <= b.h; - } - - if (a.m != b.m) { - return a.m <= b.m; - } - - if (a.s != b.s) { - return a.s <= b.s; - } + return a < b || a == b; +} - if (a.t != b.t) { - return a.t <= b.t; - } - return true; +bool +dcp::operator>= (Time const & a, Time const & b) +{ + return a > b || a == b; } + bool dcp::operator< (Time const & a, Time const & b) { @@ -133,13 +208,10 @@ dcp::operator< (Time const & a, Time const & b) return a.s < b.s; } - if (a.t != b.t) { - return a.t < b.t; - } - - return true; + return (a.e * b.tcr) < (b.e * a.tcr); } + bool dcp::operator> (Time const & a, Time const & b) { @@ -155,28 +227,35 @@ dcp::operator> (Time const & a, Time const & b) return a.s > b.s; } - if (a.t != b.t) { - return a.t > b.t; - } - - return true; + return (a.e * b.tcr) > (b.e * a.tcr); } + ostream & dcp::operator<< (ostream& s, Time const & t) { - s << t.h << ":" << t.m << ":" << t.s << "." << t.t; + s << t.h << ":" << t.m << ":" << t.s << "." << t.e; return s; } + dcp::Time -dcp::operator+ (Time a, Time const & b) +dcp::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++; } @@ -197,14 +276,24 @@ dcp::operator+ (Time a, Time const & b) return r; } + dcp::Time -dcp::operator- (Time a, Time const & b) +dcp::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 += r.tcr; r.s--; } @@ -225,27 +314,71 @@ dcp::operator- (Time a, Time const & b) return r; } + float dcp::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; + int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr; + int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr; return float (at) / bt; } -/** @return A string of the form h:m:s:t */ + string -Time::to_string () const +Time::as_string (Standard standard) const { - stringstream str; - str << h << ":" << m << ":" << s << ":" << t; - return str.str (); + char buffer[64]; + + if (standard == Standard::SMPTE) { + snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", h, m, s, e); + } else { + snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%03d", h, m, s, e); + } + + return buffer; } -/** @return This time in ticks */ + int64_t -Time::to_ticks () const +Time::as_editable_units_floor (int tcr_) const { - return int64_t(t) + int64_t(s) * 250 + int64_t(m) * 60 * 250 + int64_t(h) * 60 * 60 * 250; + return floor(int64_t(e) * double(tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_; } + +int64_t +Time::as_editable_units_ceil (int tcr_) const +{ + return ceil(int64_t(e) * double(tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_; +} + + +double +Time::as_seconds () const +{ + return h * 3600 + m * 60 + s + double(e) / tcr; +} + + +Time +Time::rebase (int tcr_) const +{ + long int e_ = lrintf (float (e) * tcr_ / tcr); + int s_ = s; + if (e_ >= tcr_) { + e_ -= tcr_; + ++s_; + } + int m_ = m; + if (s_ >= 60) { + s_ -= 60; + ++m_; + } + int h_ = h; + if (m_ >= 60) { + m_ -= 60; + ++h_; + } + + return Time (h_, m_, s_, e_, tcr_); +}