2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "ffmpeg_subtitle_stream.h"
22 #include <dcp/raw_convert.h>
23 #include <libxml++/libxml++.h>
24 #include <boost/foreach.hpp>
32 using dcp::raw_convert;
34 /** Construct a SubtitleStream from a value returned from to_string().
35 * @param t String returned from to_string().
36 * @param v State file version.
38 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node, int version)
42 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
43 /* In version 32 we assumed that from times were unique, so they were
44 used as identifiers. All subtitles were image subtitles.
47 raw_convert<string> (i->string_child ("From")),
49 ContentTime (i->number_child<ContentTime::Type> ("From")),
50 ContentTime (i->number_child<ContentTime::Type> ("To"))
55 /* In version 33 we use a hash of various parts of the subtitle as the id.
56 <Subtitle> was initially used for image subtitles; later we have
57 <ImageSubtitle> and <TextSubtitle>
59 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Subtitle")) {
61 raw_convert<string> (i->string_child ("Id")),
63 ContentTime (i->number_child<ContentTime::Type> ("From")),
64 ContentTime (i->number_child<ContentTime::Type> ("To"))
69 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("ImageSubtitle")) {
71 raw_convert<string> (i->string_child ("Id")),
73 ContentTime (i->number_child<ContentTime::Type> ("From")),
74 ContentTime (i->number_child<ContentTime::Type> ("To"))
79 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("TextSubtitle")) {
81 raw_convert<string> (i->string_child ("Id")),
83 ContentTime (i->number_child<ContentTime::Type> ("From")),
84 ContentTime (i->number_child<ContentTime::Type> ("To"))
89 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Colour")) {
90 _colours[RGBA(i->node_child("From"))] = RGBA (i->node_child("To"));
96 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
98 FFmpegStream::as_xml (root);
100 as_xml (root, _image_subtitles, "ImageSubtitle");
101 as_xml (root, _text_subtitles, "TextSubtitle");
103 for (map<RGBA, RGBA>::const_iterator i = _colours.begin(); i != _colours.end(); ++i) {
104 xmlpp::Node* node = root->add_child("Colour");
105 i->first.as_xml (node->add_child("From"));
106 i->second.as_xml (node->add_child("To"));
111 FFmpegSubtitleStream::as_xml (xmlpp::Node* root, PeriodMap const & subs, string node_name) const
113 for (PeriodMap::const_iterator i = subs.begin(); i != subs.end(); ++i) {
114 xmlpp::Node* node = root->add_child (node_name);
115 node->add_child("Id")->add_child_text (i->first);
116 node->add_child("From")->add_child_text (raw_convert<string> (i->second.from.get ()));
117 node->add_child("To")->add_child_text (raw_convert<string> (i->second.to.get ()));
122 FFmpegSubtitleStream::add_image_subtitle (string id, ContentTimePeriod period)
124 DCPOMATIC_ASSERT (_image_subtitles.find (id) == _image_subtitles.end ());
125 _image_subtitles[id] = period;
129 FFmpegSubtitleStream::add_text_subtitle (string id, ContentTimePeriod period)
131 DCPOMATIC_ASSERT (_text_subtitles.find (id) == _text_subtitles.end ());
132 _text_subtitles[id] = period;
136 FFmpegSubtitleStream::find_subtitle_to (string id) const
138 PeriodMap::const_iterator i = _image_subtitles.find (id);
139 if (i != _image_subtitles.end ()) {
143 i = _text_subtitles.find (id);
144 DCPOMATIC_ASSERT (i != _text_subtitles.end ());
148 /** @param id Subtitle id.
149 * @return true if the `from' and `to' times for this id are equal, which indicates
150 * that the `to' time is unknown.
153 FFmpegSubtitleStream::unknown_to (string id) const
155 PeriodMap::const_iterator i = _image_subtitles.find (id);
156 if (i != _image_subtitles.end ()) {
157 return i->second.from == i->second.to;
160 i = _text_subtitles.find (id);
161 DCPOMATIC_ASSERT (i != _text_subtitles.end ());
162 return i->second.from == i->second.to;
165 /** Add some offset to all the times in the stream */
167 FFmpegSubtitleStream::add_offset (ContentTime offset)
169 for (PeriodMap::iterator i = _image_subtitles.begin(); i != _image_subtitles.end(); ++i) {
170 i->second.from += offset;
171 i->second.to += offset;
174 for (PeriodMap::iterator i = _text_subtitles.begin(); i != _text_subtitles.end(); ++i) {
175 i->second.from += offset;
176 i->second.to += offset;
181 FFmpegSubtitleStream::colours () const
187 FFmpegSubtitleStream::set_colour (RGBA from, RGBA to)
193 FFmpegSubtitleStream::has_text () const
195 return !_text_subtitles.empty ();
199 FFmpegSubtitleStream::has_image () const
201 return !_image_subtitles.empty ();
205 FFmpegSubtitleStream::set_subtitle_to (string id, ContentTime to)
207 PeriodMap::iterator i = _image_subtitles.find (id);
208 if (i != _image_subtitles.end ()) {
211 i = _text_subtitles.find (id);
212 if (i != _text_subtitles.end ()) {
215 DCPOMATIC_ASSERT (false);