X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fdcp_time.cc;h=7a4a67d667ead2ad0a0559f94e210da12c5163eb;hb=76e3325a16cdf6d7220a61e2b5cfdb9c804cc32c;hp=d34804e981ba3f4cd19e2cd7960a04041694b7fd;hpb=951572c6eb7e31834d2d7f1fbc67a9cdb1336696;p=libdcp.git diff --git a/src/dcp_time.cc b/src/dcp_time.cc index d34804e9..7a4a67d6 100644 --- a/src/dcp_time.cc +++ b/src/dcp_time.cc @@ -1,20 +1,34 @@ /* - Copyright (C) 2012-2015 Carl Hetherington + Copyright (C) 2012-2016 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 @@ -24,7 +38,10 @@ #include "raw_convert.h" #include "dcp_time.h" #include "exceptions.h" +#include "compose.hpp" +#include "dcp_assert.h" #include +#include #include #include #include @@ -33,22 +50,33 @@ using namespace std; using namespace boost; using namespace dcp; -Time::Time (int frame, int frames_per_second, int tcr_) +Time::Time (int frame, double frames_per_second, int tcr_) { set (double (frame) / frames_per_second, tcr_); } -Time::Time (double seconds) +/** Construct a Time from a number of seconds and a timecode rate. + * + * @param seconds A number of seconds. + * @param tcr_ Timecode rate. + */ +Time::Time (double seconds, int tcr_) { - set (seconds, 24); + 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 seconds, int tcr_) { s = floor (seconds); tcr = tcr_; - + e = int (round ((seconds - s) * tcr)); if (s >= 60) { @@ -66,19 +94,80 @@ Time::set (double seconds, int tcr_) } } -Time::Time (string time, int tcr_) - : tcr (tcr_) +/** @param time String of the form + * HH:MM:SS:EE for SMPTE + * HH:MM:SS:E[E[E]] or HH:MM:SS.s[s[s]] for Interop + * where HH are hours, MM minutes, SS seconds, EE editable units and + * sss millseconds. + * + * @param tcr_ Timecode rate if this is a SMPTE time, otherwise empty for an Interop 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 = raw_convert (b[0]); - m = raw_convert (b[1]); - s = raw_convert (b[2]); - e = raw_convert (b[3]); } bool @@ -120,11 +209,7 @@ dcp::operator< (Time const & a, Time const & b) return a.s < b.s; } - if ((a.e * b.tcr) != (b.e * a.tcr)) { - return (a.e * b.tcr) < (b.e * a.tcr); - } - - return true; + return (a.e * b.tcr) < (b.e * a.tcr); } bool @@ -142,11 +227,7 @@ dcp::operator> (Time const & a, Time const & b) return a.s > b.s; } - if ((a.e * b.tcr) != (b.e * a.tcr)) { - return (a.e * b.tcr) > (b.e * a.tcr); - } - - return true; + return (a.e * b.tcr) > (b.e * a.tcr); } ostream & @@ -206,7 +287,7 @@ dcp::operator- (Time a, Time b) } else { r.tcr = a.tcr; } - + r.e = a.e - b.e; if (r.e < 0) { r.e += r.tcr; @@ -238,32 +319,60 @@ dcp::operator/ (Time a, Time const & b) return float (at) / bt; } -/** @return A string of the form h:m:s:e padded as in 00:00:00:000 */ +/** @return A string of the form h:m:s:e padded as in 00:00:00:000 (for Interop) or 00:00:00:00 (for SMPTE) */ string -Time::as_string () const +Time::as_string (Standard standard) const { - stringstream str; - str << setw(2) << setfill('0') << h << ":" - << setw(2) << setfill('0') << m << ":" - << setw(2) << setfill('0') << s << ":" - << setw(3) << setfill('0') << e; - 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; } +/** @param tcr_ Timecode rate with which the return value should be counted. + * @return the total number of editable units that this time consists of at the specified timecode rate, rounded up + * to the nearest editable unit. For example, as_editable_units (24) returns the total time in frames at 24fps. + */ int64_t Time::as_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_; + return ceil (int64_t(e) * double (tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_; } +/** @return the total number of seconds that this time consists of */ double Time::as_seconds () const { return h * 3600 + m * 60 + s + double(e) / tcr; } +/** @param tcr_ New timecode rate. + * @return A new Time which is this time at the spcified new timecode rate. + */ Time Time::rebase (int tcr_) const { - return Time (h, m, s, rint (float (e) * tcr_ / tcr), tcr_); + 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_); }