diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-12-16 21:39:28 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-12-16 21:39:28 +0000 |
| commit | 485794f8322b090a22da841961025b19642e42a2 (patch) | |
| tree | b6f7fd430fe5de560bc6505eafc9c4e3729e7bed /src | |
| parent | 44ff188fa75cb7954fb1b9bec6b1cb03d8536ef7 (diff) | |
Some missing headers; Time tweaks.
Diffstat (limited to 'src')
| -rw-r--r-- | src/rational.cc | 33 | ||||
| -rw-r--r-- | src/rational.h | 45 | ||||
| -rw-r--r-- | src/sub_time.cc | 47 | ||||
| -rw-r--r-- | src/sub_time.h | 21 | ||||
| -rw-r--r-- | src/wscript | 4 |
5 files changed, 134 insertions, 16 deletions
diff --git a/src/rational.cc b/src/rational.cc new file mode 100644 index 0000000..09dadab --- /dev/null +++ b/src/rational.cc @@ -0,0 +1,33 @@ +/* + Copyright (C) 2014-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 + 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, + 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. + +*/ + +#include "rational.h" + +using namespace sub; + +bool sub::operator== (Rational const & a, Rational const & b) +{ + return (a.numerator == b.numerator && a.denominator == b.denominator); +} + +bool +Rational::integer () const +{ + return (numerator % denominator) == 0; +} diff --git a/src/rational.h b/src/rational.h new file mode 100644 index 0000000..7ff71c0 --- /dev/null +++ b/src/rational.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2014-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 + 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, + 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. + +*/ + +namespace sub { + +class Rational +{ +public: + Rational (int numerator_, int denominator_) + : numerator (numerator_) + , denominator (denominator_) + {} + + int numerator; + int denominator; + + double fraction () const { + return double (numerator) / denominator; + } + + bool integer () const; + int integer_fraction () const { + return numerator / denominator; + } +}; + +bool operator== (Rational const & a, Rational const & b); + +} diff --git a/src/sub_time.cc b/src/sub_time.cc index 68dac19..c4e00ba 100644 --- a/src/sub_time.cc +++ b/src/sub_time.cc @@ -18,6 +18,7 @@ */ #include "sub_time.h" +#include "sub_assert.h" #include "exceptions.h" #include <cmath> #include <iomanip> @@ -154,8 +155,54 @@ Time::from_hms (int h, int m, int s, int ms) return Time (h * 3600 + m * 60 + s, ms, Rational (1000, 1)); } +/** Create a Time from a number of frames. + * rate must be integer. + */ +Time +Time::from_frames (int f, Rational rate) +{ + SUB_ASSERT (rate.denominator != 0); + SUB_ASSERT (rate.integer ()); + return Time (f / rate.integer_fraction(), f % rate.integer_fraction(), rate); +} + double Time::all_as_seconds () const { return _seconds + double(milliseconds ()) / 1000; } + +/** Add a time to this one. This time must have an integer _rate + * and t must have the same rate. + */ +void +Time::add (Time t) +{ + SUB_ASSERT (_rate); + + _seconds += t._seconds; + _frames += t._frames; + + SUB_ASSERT (_rate.get().denominator != 0); + SUB_ASSERT (_rate.get().integer ()); + + if (_frames >= _rate.get().integer_fraction()) { + _frames -= _rate.get().integer_fraction(); + ++_seconds; + } +} + +void +Time::scale (float f) +{ + SUB_ASSERT (_rate); + SUB_ASSERT (_rate->denominator != 0); + SUB_ASSERT (_rate->integer ()); + + _seconds = rint (_seconds * f); + _frames = rint (_frames * f); + if (_frames >= _rate->integer_fraction()) { + _frames -= _rate->integer_fraction (); + ++_seconds; + } +} diff --git a/src/sub_time.h b/src/sub_time.h index 7abdedb..93088cb 100644 --- a/src/sub_time.h +++ b/src/sub_time.h @@ -20,26 +20,11 @@ #ifndef LIBSUB_SUB_TIME_H #define LIBSUB_SUB_TIME_H +#include "rational.h" #include <boost/optional.hpp> namespace sub { -class Rational -{ -public: - Rational (int numerator_, int denominator_) - : numerator (numerator_) - , denominator (denominator_) - {} - - int numerator; - int denominator; - - double fraction () const { - return double (numerator) / denominator; - } -}; - class Time { public: @@ -57,8 +42,12 @@ public: double all_as_seconds () const; + void add (Time t); + void scale (float f); + static Time from_hmsf (int h, int m, int s, int f, boost::optional<Rational> rate = boost::optional<Rational> ()); static Time from_hms (int h, int m, int s, int ms); + static Time from_frames (int frames, Rational rate); private: friend bool operator< (Time const & a, Time const & b); diff --git a/src/wscript b/src/wscript index eafd1c9..ae53ae6 100644 --- a/src/wscript +++ b/src/wscript @@ -20,6 +20,7 @@ def build(bld): interop_dcp_reader.cc iso6937.cc iso6937_tables.cc + rational.cc raw_subtitle.cc reader.cc reader_factory.cc @@ -48,8 +49,11 @@ def build(bld): dcp_reader.h effect.h font_size.h + interop_dcp_reader.h + rational.h raw_subtitle.h reader.h + smpte_dcp_reader.h stl_binary_tables.h stl_binary_reader.h stl_binary_writer.h |
