summaryrefslogtreecommitdiff
path: root/src/dcp
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2015-01-14 17:39:32 +0000
committerCarl Hetherington <cth@carlh.net>2015-01-20 11:20:25 +0000
commit3f630fb8334238ab8a58fbe1a0f513ae2c00de80 (patch)
tree4b773b91029d6374bfd4f2194053d3e249d597cd /src/dcp
parent49cafda01b3e07c47e3b20dd5ee91e1426446aea (diff)
Simplify time representation; better in-tree DCP subtitle parser.
Diffstat (limited to 'src/dcp')
-rw-r--r--src/dcp/font.cc76
-rw-r--r--src/dcp/font.h58
-rw-r--r--src/dcp/interop_load_font.cc56
-rw-r--r--src/dcp/interop_load_font.h43
-rw-r--r--src/dcp/load_font.h38
-rw-r--r--src/dcp/smpte_load_font.cc31
-rw-r--r--src/dcp/smpte_load_font.h41
-rw-r--r--src/dcp/subtitle.cc119
-rw-r--r--src/dcp/subtitle.h58
-rw-r--r--src/dcp/text.cc65
-rw-r--r--src/dcp/text.h57
-rw-r--r--src/dcp/wscript1
12 files changed, 643 insertions, 0 deletions
diff --git a/src/dcp/font.cc b/src/dcp/font.cc
new file mode 100644
index 0000000..b2fd128
--- /dev/null
+++ b/src/dcp/font.cc
@@ -0,0 +1,76 @@
+/*
+ Copyright (C) 2012-2014 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 "font.h"
+#include "text.h"
+#include <libcxml/cxml.h>
+#include <boost/foreach.hpp>
+
+using std::string;
+using std::list;
+using boost::shared_ptr;
+using boost::optional;
+using namespace sub;
+
+dcp::Font::Font (cxml::ConstNodePtr node)
+{
+ id = node->optional_string_attribute ("Id");
+ size = node->optional_number_attribute<int64_t> ("Size").get_value_or (0);
+ italic = node->optional_bool_attribute ("Italic");
+ optional<string> c = node->optional_string_attribute ("Color");
+ if (c) {
+ colour = Colour (c.get ());
+ }
+ optional<string> const e = node->optional_string_attribute ("Effect");
+ if (e) {
+ effect = string_to_effect (e.get ());
+ }
+ c = node->optional_string_attribute ("EffectColor");
+ if (c) {
+ effect_colour = Colour (c.get ());
+ }
+}
+
+dcp::Font::Font (std::list<boost::shared_ptr<Font> > const & font_nodes)
+ : size (0)
+ , italic (false)
+ , colour ("FFFFFFFF")
+ , effect_colour ("FFFFFFFF")
+{
+ for (list<shared_ptr<Font> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
+ if ((*i)->id) {
+ id = (*i)->id;
+ }
+ if ((*i)->size != 0) {
+ size = (*i)->size;
+ }
+ if ((*i)->italic) {
+ italic = (*i)->italic.get ();
+ }
+ if ((*i)->colour) {
+ colour = (*i)->colour.get ();
+ }
+ if ((*i)->effect) {
+ effect = (*i)->effect.get ();
+ }
+ if ((*i)->effect_colour) {
+ effect_colour = (*i)->effect_colour.get ();
+ }
+ }
+}
diff --git a/src/dcp/font.h b/src/dcp/font.h
new file mode 100644
index 0000000..a1d8223
--- /dev/null
+++ b/src/dcp/font.h
@@ -0,0 +1,58 @@
+/*
+ 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
+ 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.
+
+*/
+
+/** @file src/dcp/font.h
+ * @brief Font class
+ */
+
+#include "../colour.h"
+#include "../effect.h"
+#include "subtitle.h"
+#include <libcxml/cxml.h>
+#include <boost/shared_ptr.hpp>
+#include <boost/optional.hpp>
+#include <list>
+
+namespace sub {
+namespace dcp {
+
+/** @class Font
+ * @brief Helper class for parsing subtitle XML.
+ */
+class Font
+{
+public:
+ Font ()
+ : size (0)
+ {}
+
+ Font (cxml::ConstNodePtr node);
+ Font (std::list<boost::shared_ptr<Font> > const & font_nodes);
+
+ boost::optional<std::string> id;
+ int size;
+ boost::optional<bool> italic;
+ boost::optional<Colour> colour;
+ boost::optional<Effect> effect;
+ boost::optional<Colour> effect_colour;
+};
+
+}
+
+}
diff --git a/src/dcp/interop_load_font.cc b/src/dcp/interop_load_font.cc
new file mode 100644
index 0000000..2ee3ee9
--- /dev/null
+++ b/src/dcp/interop_load_font.cc
@@ -0,0 +1,56 @@
+/*
+ Copyright (C) 2012-2014 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 "interop_load_font.h"
+#include <libcxml/cxml.h>
+
+using std::string;
+using boost::shared_ptr;
+using boost::optional;
+using namespace sub;
+
+dcp::InteropLoadFont::InteropLoadFont (string id_, string uri_)
+ : LoadFont (id_)
+ , uri (uri_)
+{
+
+}
+
+dcp::InteropLoadFont::InteropLoadFont (cxml::ConstNodePtr node)
+{
+ optional<string> x = node->optional_string_attribute ("Id");
+ if (!x) {
+ x = node->optional_string_attribute ("ID");
+ }
+ id = x.get_value_or ("");
+
+ uri = node->string_attribute ("URI");
+}
+
+bool
+dcp::operator== (InteropLoadFont const & a, InteropLoadFont const & b)
+{
+ return a.id == b.id && a.uri == b.uri;
+}
+
+bool
+dcp::operator!= (InteropLoadFont const & a, InteropLoadFont const & b)
+{
+ return !(a == b);
+}
diff --git a/src/dcp/interop_load_font.h b/src/dcp/interop_load_font.h
new file mode 100644
index 0000000..373b26c
--- /dev/null
+++ b/src/dcp/interop_load_font.h
@@ -0,0 +1,43 @@
+/*
+ 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
+ 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 "load_font.h"
+#include <libcxml/cxml.h>
+#include <boost/shared_ptr.hpp>
+#include <boost/optional.hpp>
+
+namespace sub {
+namespace dcp {
+
+class InteropLoadFont : public LoadFont
+{
+public:
+ InteropLoadFont () {}
+ InteropLoadFont (std::string id, std::string uri);
+ InteropLoadFont (cxml::ConstNodePtr node);
+
+ std::string uri;
+};
+
+bool operator== (InteropLoadFont const & a, InteropLoadFont const & b);
+bool operator!= (InteropLoadFont const & a, InteropLoadFont const & b);
+
+}
+
+}
diff --git a/src/dcp/load_font.h b/src/dcp/load_font.h
new file mode 100644
index 0000000..f269c87
--- /dev/null
+++ b/src/dcp/load_font.h
@@ -0,0 +1,38 @@
+/*
+ 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
+ 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 <string>
+
+namespace sub {
+namespace dcp {
+
+class LoadFont
+{
+public:
+ LoadFont () {}
+ LoadFont (std::string id_)
+ : id (id_)
+ {}
+
+ std::string id;
+};
+
+}
+
+}
diff --git a/src/dcp/smpte_load_font.cc b/src/dcp/smpte_load_font.cc
new file mode 100644
index 0000000..d433cbf
--- /dev/null
+++ b/src/dcp/smpte_load_font.cc
@@ -0,0 +1,31 @@
+/*
+ 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
+ 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 "smpte_load_font.h"
+#include <libcxml/cxml.h>
+
+using std::string;
+using boost::shared_ptr;
+using namespace sub;
+
+dcp::SMPTELoadFont::SMPTELoadFont (shared_ptr<const cxml::Node> node)
+ : LoadFont (node->string_attribute ("ID"))
+{
+ urn = node->content().substr (9);
+}
diff --git a/src/dcp/smpte_load_font.h b/src/dcp/smpte_load_font.h
new file mode 100644
index 0000000..aba05c8
--- /dev/null
+++ b/src/dcp/smpte_load_font.h
@@ -0,0 +1,41 @@
+/*
+ Copyright (C) 2012-2014 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 "load_font.h"
+#include <boost/shared_ptr.hpp>
+#include <boost/optional.hpp>
+
+namespace cxml {
+ class Node;
+}
+
+namespace sub {
+namespace dcp {
+
+class SMPTELoadFont : public LoadFont
+{
+public:
+ SMPTELoadFont (boost::shared_ptr<const cxml::Node> node);
+
+ std::string urn;
+};
+
+}
+
+}
diff --git a/src/dcp/subtitle.cc b/src/dcp/subtitle.cc
new file mode 100644
index 0000000..68ca559
--- /dev/null
+++ b/src/dcp/subtitle.cc
@@ -0,0 +1,119 @@
+/*
+ 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
+ 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 "../exceptions.h"
+#include "../raw_convert.h"
+#include "subtitle.h"
+#include <libcxml/cxml.h>
+#include <boost/lexical_cast.hpp>
+#include <boost/algorithm/string.hpp>
+
+using std::string;
+using std::vector;
+using std::list;
+using boost::optional;
+using boost::shared_ptr;
+using boost::lexical_cast;
+using boost::is_any_of;
+using namespace sub;
+
+dcp::Subtitle::Subtitle (boost::shared_ptr<const cxml::Node> node, optional<int> tcr)
+{
+ if (tcr) {
+ in = smpte_time (node, "TimeIn", tcr.get ()).get ();
+ out = smpte_time (node, "TimeOut", tcr.get ()).get ();
+ } else {
+ in = interop_time (node, "TimeIn").get ();
+ out = interop_time (node, "TimeOut").get ();
+ }
+
+ if (tcr) {
+ fade_up_time = smpte_time (node, "FadeUpTime", tcr.get ()).get_value_or (Time::from_hmsf (0, 0, 0, 2, Rational (tcr.get(), 1)));
+ fade_down_time = smpte_time (node, "FadeDownTime", tcr.get ()).get_value_or (Time::from_hmsf (0, 0, 0, 2, Rational (tcr.get (), 1)));
+ } else {
+ fade_up_time = interop_time (node, "FadeUpTime").get_value_or (Time::from_hms (0, 0, 0, 80));
+ if (fade_up_time > Time::from_hms (0, 0, 8, 0)) {
+ fade_up_time = Time::from_hms (0, 0, 8, 0);
+ }
+ fade_down_time = interop_time (node, "FadeDownTime").get_value_or (Time::from_hms (0, 0, 0, 80));
+ if (fade_down_time > Time::from_hms (0, 0, 8, 0)) {
+ fade_down_time = Time::from_hms (0, 0, 8, 0);
+ }
+ }
+}
+
+optional<Time>
+dcp::Subtitle::smpte_time (shared_ptr<const cxml::Node> node, string name, int tcr)
+{
+ optional<string> u = node->optional_string_attribute (name);
+ if (!u) {
+ return optional<Time> ();
+ }
+
+ vector<string> b;
+ split (b, u.get (), is_any_of (":"));
+ if (b.size() != 4) {
+ boost::throw_exception (DCPError ("unrecognised time specification " + u.get ()));
+ }
+
+ return Time::from_hmsf (
+ raw_convert<int> (b[0]),
+ raw_convert<int> (b[1]),
+ raw_convert<int> (b[2]),
+ raw_convert<int> (b[3]),
+ Rational (tcr, 1)
+ );
+}
+
+optional<Time>
+dcp::Subtitle::interop_time (shared_ptr<const cxml::Node> node, string name)
+{
+ optional<string> u = node->optional_string_attribute (name);
+ if (!u) {
+ return optional<Time> ();
+ }
+
+ if (u.get().find (":") != string::npos) {
+ /* HH:MM:SS:TTT or HH:MM:SS.sss */
+ vector<string> b;
+ split (b, u.get(), is_any_of (":."));
+ if (b.size() != 4) {
+ boost::throw_exception (DCPError ("unrecognised time specification " + u.get ()));
+ }
+
+ if (u.get().find (".") != string::npos) {
+ return Time::from_hms (
+ raw_convert<int> (b[0]),
+ raw_convert<int> (b[1]),
+ raw_convert<int> (b[2]),
+ rint (raw_convert<double> ("." + b[3]) * 1000)
+ );
+ } else {
+ return Time::from_hms (
+ raw_convert<int> (b[0]),
+ raw_convert<int> (b[1]),
+ raw_convert<int> (b[2]),
+ raw_convert<int> (b[3]) * 4
+ );
+ }
+ } else {
+ return Time::from_hms (0, 0, 0, raw_convert<int> (u.get ()) * 4);
+ }
+}
+
diff --git a/src/dcp/subtitle.h b/src/dcp/subtitle.h
new file mode 100644
index 0000000..672dc9a
--- /dev/null
+++ b/src/dcp/subtitle.h
@@ -0,0 +1,58 @@
+/*
+ Copyright (C) 2012-2014 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.
+
+*/
+
+#ifndef LIBSUB_DCP_SUBTITLE_H
+#define LIBSUB_DCP_SUBTITLE_H
+
+#include "../sub_time.h"
+#include <boost/shared_ptr.hpp>
+#include <boost/optional.hpp>
+#include <list>
+
+namespace cxml {
+ class Node;
+}
+
+namespace sub {
+namespace dcp {
+
+class Font;
+class Text;
+
+class Subtitle
+{
+public:
+ Subtitle () {}
+ Subtitle (boost::shared_ptr<const cxml::Node> node, boost::optional<int> tcr);
+
+ Time in;
+ Time out;
+ Time fade_up_time;
+ Time fade_down_time;
+
+private:
+ boost::optional<Time> smpte_time (boost::shared_ptr<const cxml::Node> node, std::string name, int tcr);
+ boost::optional<Time> interop_time (boost::shared_ptr<const cxml::Node> node, std::string name);
+};
+
+}
+
+}
+
+#endif
diff --git a/src/dcp/text.cc b/src/dcp/text.cc
new file mode 100644
index 0000000..313ebdd
--- /dev/null
+++ b/src/dcp/text.cc
@@ -0,0 +1,65 @@
+/*
+ Copyright (C) 2012-2014 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.
+
+*/
+
+/** @file src/text.cc
+ * @brief Text class for parsing DCP subtitle XML.
+ */
+
+#include "../xml.h"
+#include "text.h"
+#include "font.h"
+#include <libcxml/cxml.h>
+#include <boost/foreach.hpp>
+
+using std::string;
+using std::list;
+using boost::shared_ptr;
+using boost::optional;
+using namespace sub;
+
+/** Read a &lt;Text&gt; node from a subtitle XML file, noting its contents
+ * in this object's member variables.
+ * @param node Node to read.
+ */
+dcp::Text::Text (boost::shared_ptr<const cxml::Node> node)
+ : v_align (CENTRE_OF_SCREEN)
+{
+ optional<float> x = node->optional_number_attribute<float> ("VPosition");
+ if (!x) {
+ x = node->number_attribute<float> ("Vposition");
+ }
+ v_position = x.get ();
+
+ optional<string> v = node->optional_string_attribute ("VAlign");
+ if (!v) {
+ v = node->optional_string_attribute ("Valign");
+ }
+
+ if (v) {
+ if (v.get() == "top") {
+ v_align = TOP_OF_SCREEN;
+ } else if (v.get() == "center") {
+ v_align = CENTRE_OF_SCREEN;
+ } else if (v.get() == "bottom") {
+ v_align = BOTTOM_OF_SCREEN;
+ } else {
+ boost::throw_exception (DCPError ("unknown subtitle valign type"));
+ }
+ }
+}
diff --git a/src/dcp/text.h b/src/dcp/text.h
new file mode 100644
index 0000000..f2c5059
--- /dev/null
+++ b/src/dcp/text.h
@@ -0,0 +1,57 @@
+/*
+ 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
+ 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.
+
+*/
+
+/** @file src/text.h
+ * @brief Text class for parsing DCP subtitle XML.
+ */
+
+#include "../vertical_reference.h"
+#include <boost/shared_ptr.hpp>
+#include <list>
+
+namespace cxml {
+ class Node;
+}
+
+namespace sub {
+namespace dcp {
+
+class Font;
+
+/** @class Text
+ * @brief Parser for Text nodes from subtitle XML.
+ */
+class Text
+{
+public:
+ /** Construct a default text node */
+ Text ()
+ : v_position (0)
+ , v_align (TOP_OF_SCREEN)
+ {}
+
+ Text (boost::shared_ptr<const cxml::Node> node);
+
+ float v_position;
+ VerticalReference v_align;
+};
+
+}
+
+}
diff --git a/src/dcp/wscript b/src/dcp/wscript
new file mode 100644
index 0000000..a70ef26
--- /dev/null
+++ b/src/dcp/wscript
@@ -0,0 +1 @@
+# This is a dummy just so my waft script works