Support reading of encrypted subtitles.
[libdcp.git] / src / subtitle_asset.cc
index 6a57f455a833af928d8736cd49827ec1e009e823..b9f6336c594928d09c6990aeaf035e2f37c936dd 100644 (file)
@@ -1,20 +1,34 @@
 /*
     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
 
-    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 <http://www.gnu.org/licenses/>.
+
+    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.
 */
 
 #include "raw_convert.h"
 #include "text_node.h"
 #include "subtitle_string.h"
 #include "dcp_assert.h"
-#include "AS_DCP.h"
-#include "KM_util.h"
+#include <asdcp/AS_DCP.h>
+#include <asdcp/KM_util.h>
 #include <libxml++/nodes/element.h>
 #include <boost/algorithm/string.hpp>
 #include <boost/shared_array.hpp>
 #include <boost/foreach.hpp>
-#include <fstream>
 
 using std::string;
 using std::list;
-using std::ostream;
-using std::ofstream;
-using std::stringstream;
 using std::cout;
 using std::cerr;
 using std::map;
@@ -59,50 +69,64 @@ SubtitleAsset::SubtitleAsset (boost::filesystem::path file)
 }
 
 void
-SubtitleAsset::parse_subtitles (shared_ptr<cxml::Document> xml, list<shared_ptr<dcp::FontNode> > font_nodes)
+SubtitleAsset::parse_subtitles (
+       shared_ptr<cxml::Document> xml,
+       list<shared_ptr<dcp::FontNode> > font_nodes,
+       list<shared_ptr<dcp::SubtitleNode> > subtitle_nodes
+       )
 {
        /* Make Subtitle objects to represent the raw XML nodes in a sane way */
        ParseState parse_state;
-       examine_font_nodes (xml, font_nodes, parse_state);
+       examine_nodes (xml, font_nodes, parse_state);
+       examine_nodes (xml, subtitle_nodes, parse_state);
 }
 
 void
-SubtitleAsset::examine_font_nodes (
+SubtitleAsset::examine_nodes (
        shared_ptr<const cxml::Node> xml,
-       list<shared_ptr<dcp::FontNode> > const & font_nodes,
+       list<shared_ptr<dcp::SubtitleNode> > const & subtitle_nodes,
        ParseState& parse_state
        )
 {
-       for (list<shared_ptr<dcp::FontNode> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
+       BOOST_FOREACH (shared_ptr<dcp::SubtitleNode> i, subtitle_nodes) {
+               parse_state.subtitle_nodes.push_back (i);
+               examine_nodes (xml, i->text_nodes, parse_state);
+               examine_nodes (xml, i->font_nodes, parse_state);
+               parse_state.subtitle_nodes.pop_back ();
+       }
+}
 
-               parse_state.font_nodes.push_back (*i);
-               maybe_add_subtitle ((*i)->text, parse_state);
+void
+SubtitleAsset::examine_nodes (
+       shared_ptr<const cxml::Node> xml,
+       list<shared_ptr<dcp::FontNode> > const & font_nodes,
+       ParseState& parse_state
+       )
+{
+       BOOST_FOREACH (shared_ptr<dcp::FontNode> i, font_nodes) {
 
-               for (list<shared_ptr<dcp::SubtitleNode> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
-                       parse_state.subtitle_nodes.push_back (*j);
-                       examine_text_nodes (xml, (*j)->text_nodes, parse_state);
-                       examine_font_nodes (xml, (*j)->font_nodes, parse_state);
-                       parse_state.subtitle_nodes.pop_back ();
-               }
+               parse_state.font_nodes.push_back (i);
+               maybe_add_subtitle (i->text, parse_state);
 
-               examine_font_nodes (xml, (*i)->font_nodes, parse_state);
-               examine_text_nodes (xml, (*i)->text_nodes, parse_state);
+               examine_nodes (xml, i->subtitle_nodes, parse_state);
+               examine_nodes (xml, i->font_nodes, parse_state);
+               examine_nodes (xml, i->text_nodes, parse_state);
 
                parse_state.font_nodes.pop_back ();
        }
 }
 
 void
-SubtitleAsset::examine_text_nodes (
+SubtitleAsset::examine_nodes (
        shared_ptr<const cxml::Node> xml,
        list<shared_ptr<dcp::TextNode> > const & text_nodes,
        ParseState& parse_state
        )
 {
-       for (list<shared_ptr<dcp::TextNode> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
-               parse_state.text_nodes.push_back (*i);
-               maybe_add_subtitle ((*i)->text, parse_state);
-               examine_font_nodes (xml, (*i)->font_nodes, parse_state);
+       BOOST_FOREACH (shared_ptr<dcp::TextNode> i, text_nodes) {
+               parse_state.text_nodes.push_back (i);
+               maybe_add_subtitle (i->text, parse_state);
+               examine_nodes (xml, i->font_nodes, parse_state);
                parse_state.text_nodes.pop_back ();
        }
 }
@@ -129,6 +153,8 @@ SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
                SubtitleString (
                        effective_font.id,
                        effective_font.italic.get_value_or (false),
+                       effective_font.bold.get_value_or (false),
+                       effective_font.underline.get_value_or (false),
                        effective_font.colour.get_value_or (dcp::Colour (255, 255, 255)),
                        effective_font.size,
                        effective_font.aspect_adjust.get_value_or (1.0),
@@ -138,6 +164,7 @@ SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
                        effective_text.h_align,
                        effective_text.v_position,
                        effective_text.v_align,
+                       effective_text.direction,
                        text,
                        effective_font.effect.get_value_or (NONE),
                        effective_font.effect_colour.get_value_or (dcp::Colour (0, 0, 0)),
@@ -151,9 +178,9 @@ list<SubtitleString>
 SubtitleAsset::subtitles_during (Time from, Time to, bool starting) const
 {
        list<SubtitleString> s;
-       for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
-               if ((starting && from <= i->in() && i->in() < to) || (!starting && i->out() >= from && i->in() <= to)) {
-                       s.push_back (*i);
+       BOOST_FOREACH (SubtitleString const & i, _subtitles) {
+               if ((starting && from <= i.in() && i.in() < to) || (!starting && i.out() >= from && i.in() <= to)) {
+                       s.push_back (i);
                }
        }
 
@@ -170,9 +197,9 @@ Time
 SubtitleAsset::latest_subtitle_out () const
 {
        Time t;
-       for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
-               if (i->out() > t) {
-                       t = i->out ();
+       BOOST_FOREACH (SubtitleString const & i, _subtitles) {
+               if (i.out() > t) {
+                       t = i.out ();
                }
        }
 
@@ -219,10 +246,12 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Stand
 
        string const xmlns = standard == SMPTE ? "dcst" : "";
 
-       /* XXX: script, underlined, weight not supported */
+       /* XXX: script not supported */
 
        optional<string> font;
        bool italic = false;
+       bool bold = false;
+       bool underline = false;
        Colour colour;
        int size = 0;
        float aspect_adjust = 1.0;
@@ -237,7 +266,7 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Stand
        xmlpp::Element* font_element = 0;
        xmlpp::Element* subtitle_element = 0;
 
-       for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
+       BOOST_FOREACH (SubtitleString const & i, sorted) {
 
                /* We will start a new <Font>...</Font> whenever some font property changes.
                   I suppose we should really make an optimal hierarchy of <Font> tags, but
@@ -245,22 +274,26 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Stand
                */
 
                bool const font_changed =
-                       font          != i->font()          ||
-                       italic        != i->italic()        ||
-                       colour        != i->colour()        ||
-                       size          != i->size()          ||
-                       fabs (aspect_adjust - i->aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
-                       effect        != i->effect()        ||
-                       effect_colour != i->effect_colour();
+                       font          != i.font()          ||
+                       italic        != i.italic()        ||
+                       bold          != i.bold()          ||
+                       underline     != i.underline()     ||
+                       colour        != i.colour()        ||
+                       size          != i.size()          ||
+                       fabs (aspect_adjust - i.aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
+                       effect        != i.effect()        ||
+                       effect_colour != i.effect_colour();
 
                if (font_changed) {
-                       font = i->font ();
-                       italic = i->italic ();
-                       colour = i->colour ();
-                       size = i->size ();
-                       aspect_adjust = i->aspect_adjust ();
-                       effect = i->effect ();
-                       effect_colour = i->effect_colour ();
+                       font = i.font ();
+                       italic = i.italic ();
+                       bold = i.bold ();
+                       underline = i.underline ();
+                       colour = i.colour ();
+                       size = i.size ();
+                       aspect_adjust = i.aspect_adjust ();
+                       effect = i.effect ();
+                       effect_colour = i.effect_colour ();
                }
 
                if (!font_element || font_changed) {
@@ -282,63 +315,67 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Stand
                        font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
                        font_element->set_attribute ("Script", "normal");
                        if (standard == SMPTE) {
-                               font_element->set_attribute ("Underline", "no");
+                               font_element->set_attribute ("Underline", underline ? "yes" : "no");
                        } else {
-                               font_element->set_attribute ("Underlined", "no");
+                               font_element->set_attribute ("Underlined", underline ? "yes" : "no");
                        }
-                       font_element->set_attribute ("Weight", "normal");
+                       font_element->set_attribute ("Weight", bold ? "bold" : "normal");
                }
 
                if (!subtitle_element || font_changed ||
-                   (last_in != i->in() ||
-                    last_out != i->out() ||
-                    last_fade_up_time != i->fade_up_time() ||
-                    last_fade_down_time != i->fade_down_time()
+                   (last_in != i.in() ||
+                    last_out != i.out() ||
+                    last_fade_up_time != i.fade_up_time() ||
+                    last_fade_down_time != i.fade_down_time()
                            )) {
 
                        subtitle_element = font_element->add_child ("Subtitle", xmlns);
                        subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
-                       subtitle_element->set_attribute ("TimeIn", i->in().rebase(time_code_rate).as_string(standard));
-                       subtitle_element->set_attribute ("TimeOut", i->out().rebase(time_code_rate).as_string(standard));
+                       subtitle_element->set_attribute ("TimeIn", i.in().rebase(time_code_rate).as_string(standard));
+                       subtitle_element->set_attribute ("TimeOut", i.out().rebase(time_code_rate).as_string(standard));
                        if (standard == SMPTE) {
-                               subtitle_element->set_attribute ("FadeUpTime", i->fade_up_time().rebase(time_code_rate).as_string(standard));
-                               subtitle_element->set_attribute ("FadeDownTime", i->fade_down_time().rebase(time_code_rate).as_string(standard));
+                               subtitle_element->set_attribute ("FadeUpTime", i.fade_up_time().rebase(time_code_rate).as_string(standard));
+                               subtitle_element->set_attribute ("FadeDownTime", i.fade_down_time().rebase(time_code_rate).as_string(standard));
                        } else {
-                               subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().as_editable_units(time_code_rate)));
-                               subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().as_editable_units(time_code_rate)));
+                               subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i.fade_up_time().as_editable_units(time_code_rate)));
+                               subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i.fade_down_time().as_editable_units(time_code_rate)));
                        }
 
-                       last_in = i->in ();
-                       last_out = i->out ();
-                       last_fade_up_time = i->fade_up_time ();
-                       last_fade_down_time = i->fade_down_time ();
+                       last_in = i.in ();
+                       last_out = i.out ();
+                       last_fade_up_time = i.fade_up_time ();
+                       last_fade_down_time = i.fade_down_time ();
                }
 
                xmlpp::Element* text = subtitle_element->add_child ("Text", xmlns);
-               if (i->h_align() != HALIGN_CENTER) {
+
+               if (i.h_align() != HALIGN_CENTER) {
                        if (standard == SMPTE) {
-                               text->set_attribute ("Halign", halign_to_string (i->h_align ()));
+                               text->set_attribute ("Halign", halign_to_string (i.h_align ()));
                        } else {
-                               text->set_attribute ("HAlign", halign_to_string (i->h_align ()));
+                               text->set_attribute ("HAlign", halign_to_string (i.h_align ()));
                        }
                }
-               if (i->h_position() > ALIGN_EPSILON) {
+
+               if (i.h_position() > ALIGN_EPSILON) {
                        if (standard == SMPTE) {
-                               text->set_attribute ("Hposition", raw_convert<string> (i->h_position() * 100, 6));
+                               text->set_attribute ("Hposition", raw_convert<string> (i.h_position() * 100, 6));
                        } else {
-                               text->set_attribute ("HPosition", raw_convert<string> (i->h_position() * 100, 6));
+                               text->set_attribute ("HPosition", raw_convert<string> (i.h_position() * 100, 6));
                        }
                }
+
                if (standard == SMPTE) {
-                       text->set_attribute ("Valign", valign_to_string (i->v_align()));
+                       text->set_attribute ("Valign", valign_to_string (i.v_align()));
                } else {
-                       text->set_attribute ("VAlign", valign_to_string (i->v_align()));
+                       text->set_attribute ("VAlign", valign_to_string (i.v_align()));
                }
-               if (i->v_position() > ALIGN_EPSILON) {
+
+               if (i.v_position() > ALIGN_EPSILON) {
                        if (standard == SMPTE) {
-                               text->set_attribute ("Vposition", raw_convert<string> (i->v_position() * 100, 6));
+                               text->set_attribute ("Vposition", raw_convert<string> (i.v_position() * 100, 6));
                        } else {
-                               text->set_attribute ("VPosition", raw_convert<string> (i->v_position() * 100, 6));
+                               text->set_attribute ("VPosition", raw_convert<string> (i.v_position() * 100, 6));
                        }
                } else {
                        if (standard == SMPTE) {
@@ -347,7 +384,15 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Stand
                                text->set_attribute ("VPosition", "0");
                        }
                }
-               text->add_child_text (i->text());
+
+               /* Interop only supports "horizontal" or "vertical" for direction, so only write this
+                  for SMPTE.
+               */
+               if (i.direction() != DIRECTION_LTR && standard == SMPTE) {
+                       text->set_attribute ("Direction", direction_to_string (i.direction ()));
+               }
+
+               text->add_child_text (i.text());
        }
 }