Allow changes to colours of FFmpeg subtitles (#795).
[dcpomatic.git] / src / lib / ffmpeg_subtitle_stream.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "ffmpeg_subtitle_stream.h"
21 #include "raw_convert.h"
22 #include <libxml++/libxml++.h>
23 #include <boost/foreach.hpp>
24 #include <iostream>
25
26 using std::string;
27 using std::map;
28 using std::list;
29 using std::cout;
30 using std::make_pair;
31
32 /** Construct a SubtitleStream from a value returned from to_string().
33  *  @param t String returned from to_string().
34  *  @param v State file version.
35  */
36 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node, int version)
37         : FFmpegStream (node)
38 {
39         if (version == 32) {
40                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
41                         /* In version 32 we assumed that from times were unique, so they were
42                            used as identifiers.  All subtitles were image subtitles.
43                         */
44                         add_image_subtitle (
45                                 raw_convert<string> (i->string_child ("From")),
46                                 ContentTimePeriod (
47                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
48                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
49                                         )
50                                 );
51                 }
52         } else {
53                 /* In version 33 we use a hash of various parts of the subtitle as the id.
54                    <Subtitle> was initially used for image subtitles; later we have
55                    <ImageSubtitle> and <TextSubtitle>
56                 */
57                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Subtitle")) {
58                         add_image_subtitle (
59                                 raw_convert<string> (i->string_child ("Id")),
60                                 ContentTimePeriod (
61                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
62                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
63                                         )
64                                 );
65                 }
66
67                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("ImageSubtitle")) {
68                         add_image_subtitle (
69                                 raw_convert<string> (i->string_child ("Id")),
70                                 ContentTimePeriod (
71                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
72                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
73                                         )
74                                 );
75                 }
76
77                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("TextSubtitle")) {
78                         add_text_subtitle (
79                                 raw_convert<string> (i->string_child ("Id")),
80                                 ContentTimePeriod (
81                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
82                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
83                                         )
84                                 );
85                 }
86
87                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Colour")) {
88                         _colours[RGBA(i->node_child("From"))] = RGBA (i->node_child("To"));
89                 }
90         }
91 }
92
93 void
94 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
95 {
96         FFmpegStream::as_xml (root);
97
98         as_xml (root, _image_subtitles, "ImageSubtitle");
99         as_xml (root, _text_subtitles, "TextSubtitle");
100
101         for (map<RGBA, RGBA>::const_iterator i = _colours.begin(); i != _colours.end(); ++i) {
102                 xmlpp::Node* node = root->add_child("Colour");
103                 i->first.as_xml (node->add_child("From"));
104                 i->second.as_xml (node->add_child("To"));
105         }
106 }
107
108 void
109 FFmpegSubtitleStream::as_xml (xmlpp::Node* root, PeriodMap const & subs, string node_name) const
110 {
111         for (PeriodMap::const_iterator i = subs.begin(); i != subs.end(); ++i) {
112                 xmlpp::Node* node = root->add_child (node_name);
113                 node->add_child("Id")->add_child_text (i->first);
114                 node->add_child("From")->add_child_text (raw_convert<string> (i->second.from.get ()));
115                 node->add_child("To")->add_child_text (raw_convert<string> (i->second.to.get ()));
116         }
117 }
118
119 void
120 FFmpegSubtitleStream::add_image_subtitle (string id, ContentTimePeriod period)
121 {
122         DCPOMATIC_ASSERT (_image_subtitles.find (id) == _image_subtitles.end ());
123         _image_subtitles[id] = period;
124 }
125
126 void
127 FFmpegSubtitleStream::add_text_subtitle (string id, ContentTimePeriod period)
128 {
129         DCPOMATIC_ASSERT (_text_subtitles.find (id) == _text_subtitles.end ());
130         _text_subtitles[id] = period;
131 }
132
133 list<ContentTimePeriod>
134 FFmpegSubtitleStream::image_subtitles_during (ContentTimePeriod period, bool starting) const
135 {
136         return subtitles_during (period, starting, _image_subtitles);
137 }
138
139 list<ContentTimePeriod>
140 FFmpegSubtitleStream::text_subtitles_during (ContentTimePeriod period, bool starting) const
141 {
142         return subtitles_during (period, starting, _text_subtitles);
143 }
144
145 list<ContentTimePeriod>
146 FFmpegSubtitleStream::subtitles_during (ContentTimePeriod period, bool starting, PeriodMap const & subs) const
147 {
148         list<ContentTimePeriod> d;
149
150         /* XXX: inefficient */
151         for (map<string, ContentTimePeriod>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
152                 if ((starting && period.contains (i->second.from)) || (!starting && period.overlaps (i->second))) {
153                         d.push_back (i->second);
154                 }
155         }
156
157         return d;
158 }
159
160 ContentTime
161 FFmpegSubtitleStream::find_subtitle_to (string id) const
162 {
163         PeriodMap::const_iterator i = _image_subtitles.find (id);
164         if (i != _image_subtitles.end ()) {
165                 return i->second.to;
166         }
167
168         i = _text_subtitles.find (id);
169         DCPOMATIC_ASSERT (i != _text_subtitles.end ());
170         return i->second.to;
171 }
172
173 /** Add some offset to all the times in the stream */
174 void
175 FFmpegSubtitleStream::add_offset (ContentTime offset)
176 {
177         for (PeriodMap::iterator i = _image_subtitles.begin(); i != _image_subtitles.end(); ++i) {
178                 i->second.from += offset;
179                 i->second.to += offset;
180         }
181
182         for (PeriodMap::iterator i = _text_subtitles.begin(); i != _text_subtitles.end(); ++i) {
183                 i->second.from += offset;
184                 i->second.to += offset;
185         }
186 }
187
188 map<RGBA, RGBA>
189 FFmpegSubtitleStream::colours () const
190 {
191         return _colours;
192 }
193
194 void
195 FFmpegSubtitleStream::set_colour (RGBA from, RGBA to)
196 {
197         _colours[from] = to;
198 }