Move raw_convert into libdcp.
[dcpomatic.git] / src / lib / ffmpeg_subtitle_stream.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "ffmpeg_subtitle_stream.h"
22 #include <dcp/raw_convert.h>
23 #include <libxml++/libxml++.h>
24 #include <boost/foreach.hpp>
25 #include <iostream>
26
27 using std::string;
28 using std::map;
29 using std::list;
30 using std::cout;
31 using std::make_pair;
32 using dcp::raw_convert;
33
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.
37  */
38 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node, int version)
39         : FFmpegStream (node)
40 {
41         if (version == 32) {
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.
45                         */
46                         add_image_subtitle (
47                                 raw_convert<string> (i->string_child ("From")),
48                                 ContentTimePeriod (
49                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
50                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
51                                         )
52                                 );
53                 }
54         } else {
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>
58                 */
59                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Subtitle")) {
60                         add_image_subtitle (
61                                 raw_convert<string> (i->string_child ("Id")),
62                                 ContentTimePeriod (
63                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
64                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
65                                         )
66                                 );
67                 }
68
69                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("ImageSubtitle")) {
70                         add_image_subtitle (
71                                 raw_convert<string> (i->string_child ("Id")),
72                                 ContentTimePeriod (
73                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
74                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
75                                         )
76                                 );
77                 }
78
79                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("TextSubtitle")) {
80                         add_text_subtitle (
81                                 raw_convert<string> (i->string_child ("Id")),
82                                 ContentTimePeriod (
83                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
84                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
85                                         )
86                                 );
87                 }
88
89                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Colour")) {
90                         _colours[RGBA(i->node_child("From"))] = RGBA (i->node_child("To"));
91                 }
92         }
93 }
94
95 void
96 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
97 {
98         FFmpegStream::as_xml (root);
99
100         as_xml (root, _image_subtitles, "ImageSubtitle");
101         as_xml (root, _text_subtitles, "TextSubtitle");
102
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"));
107         }
108 }
109
110 void
111 FFmpegSubtitleStream::as_xml (xmlpp::Node* root, PeriodMap const & subs, string node_name) const
112 {
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 ()));
118         }
119 }
120
121 void
122 FFmpegSubtitleStream::add_image_subtitle (string id, ContentTimePeriod period)
123 {
124         DCPOMATIC_ASSERT (_image_subtitles.find (id) == _image_subtitles.end ());
125         _image_subtitles[id] = period;
126 }
127
128 void
129 FFmpegSubtitleStream::add_text_subtitle (string id, ContentTimePeriod period)
130 {
131         DCPOMATIC_ASSERT (_text_subtitles.find (id) == _text_subtitles.end ());
132         _text_subtitles[id] = period;
133 }
134
135 list<ContentTimePeriod>
136 FFmpegSubtitleStream::image_subtitles_during (ContentTimePeriod period, bool starting) const
137 {
138         return subtitles_during (period, starting, _image_subtitles);
139 }
140
141 list<ContentTimePeriod>
142 FFmpegSubtitleStream::text_subtitles_during (ContentTimePeriod period, bool starting) const
143 {
144         return subtitles_during (period, starting, _text_subtitles);
145 }
146
147 struct PeriodSorter
148 {
149         bool operator() (ContentTimePeriod const & a, ContentTimePeriod const & b) {
150                 return a.from < b.from;
151         }
152 };
153
154 list<ContentTimePeriod>
155 FFmpegSubtitleStream::subtitles_during (ContentTimePeriod period, bool starting, PeriodMap const & subs) const
156 {
157         list<ContentTimePeriod> d;
158
159         /* XXX: inefficient */
160         for (map<string, ContentTimePeriod>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
161                 if ((starting && period.contains(i->second.from)) || (!starting && period.overlap(i->second))) {
162                         d.push_back (i->second);
163                 }
164         }
165
166         d.sort (PeriodSorter ());
167
168         return d;
169 }
170
171 ContentTime
172 FFmpegSubtitleStream::find_subtitle_to (string id) const
173 {
174         PeriodMap::const_iterator i = _image_subtitles.find (id);
175         if (i != _image_subtitles.end ()) {
176                 return i->second.to;
177         }
178
179         i = _text_subtitles.find (id);
180         DCPOMATIC_ASSERT (i != _text_subtitles.end ());
181         return i->second.to;
182 }
183
184 /** Add some offset to all the times in the stream */
185 void
186 FFmpegSubtitleStream::add_offset (ContentTime offset)
187 {
188         for (PeriodMap::iterator i = _image_subtitles.begin(); i != _image_subtitles.end(); ++i) {
189                 i->second.from += offset;
190                 i->second.to += offset;
191         }
192
193         for (PeriodMap::iterator i = _text_subtitles.begin(); i != _text_subtitles.end(); ++i) {
194                 i->second.from += offset;
195                 i->second.to += offset;
196         }
197 }
198
199 map<RGBA, RGBA>
200 FFmpegSubtitleStream::colours () const
201 {
202         return _colours;
203 }
204
205 void
206 FFmpegSubtitleStream::set_colour (RGBA from, RGBA to)
207 {
208         _colours[from] = to;
209 }
210
211 bool
212 FFmpegSubtitleStream::has_text () const
213 {
214         return !_text_subtitles.empty ();
215 }
216
217 bool
218 FFmpegSubtitleStream::has_image () const
219 {
220         return !_image_subtitles.empty ();
221 }