Add priority control buttons to batch converter (#961).
[dcpomatic.git] / src / lib / subtitle_decoder.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 "subtitle_decoder.h"
22 #include "subtitle_content.h"
23 #include "util.h"
24 #include "log.h"
25 #include "compose.hpp"
26 #include <sub/subtitle.h>
27 #include <boost/shared_ptr.hpp>
28 #include <boost/foreach.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <iostream>
31
32 using std::list;
33 using std::cout;
34 using std::string;
35 using std::min;
36 using boost::shared_ptr;
37 using boost::optional;
38 using boost::function;
39
40 SubtitleDecoder::SubtitleDecoder (
41         Decoder* parent,
42         shared_ptr<const SubtitleContent> c,
43         shared_ptr<Log> log,
44         function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
45         function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
46         )
47         : DecoderPart (parent, log)
48         , _content (c)
49         , _image_during (image_during)
50         , _text_during (text_during)
51 {
52
53 }
54
55 /** Called by subclasses when an image subtitle is ready.
56  *  @param period Period of the subtitle.
57  *  @param image Subtitle image.
58  *  @param rect Area expressed as a fraction of the video frame that this subtitle
59  *  is for (e.g. a width of 0.5 means the width of the subtitle is half the width
60  *  of the video frame)
61  */
62 void
63 SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
64 {
65         _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
66         _position = period.from;
67 }
68
69 void
70 SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
71 {
72         /* We must escape < and > in strings, otherwise they might confuse our subtitle
73            renderer (which uses some HTML-esque markup to do bold/italic etc.)
74         */
75         BOOST_FOREACH (dcp::SubtitleString& i, s) {
76                 string t = i.text ();
77                 boost::algorithm::replace_all (t, "<", "&lt;");
78                 boost::algorithm::replace_all (t, ">", "&gt;");
79                 i.set_text (t);
80         }
81
82         _decoded_text.push_back (ContentTextSubtitle (period, s));
83         _position = period.to;
84 }
85
86 /** Get the subtitles that correspond to a given list of periods.
87  *  @param subs Subtitles.
88  *  @param sp Periods for which to extract subtitles from subs.
89  */
90 template <class T>
91 list<T>
92 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool accurate)
93 {
94         if (sp.empty ()) {
95                 return list<T> ();
96         }
97
98         /* Find the time of the first subtitle we don't have in subs */
99         optional<ContentTime> missing;
100         BOOST_FOREACH (ContentTimePeriod i, sp) {
101                 typename list<T>::const_iterator j = subs.begin();
102                 while (j != subs.end() && j->period() != i) {
103                         ++j;
104                 }
105                 if (j == subs.end ()) {
106                         missing = i.from;
107                         break;
108                 }
109         }
110
111         /* Suggest to our parent decoder that it might want to seek if we haven't got what we're being asked for */
112         if (missing) {
113                 _log->log (
114                         String::compose (
115                                 "SD suggests seek to %1 from %2",
116                                 to_string (*missing),
117                                 position() ? to_string(*position()) : "nowhere"),
118                         LogEntry::TYPE_DEBUG_DECODE);
119                 maybe_seek (*missing, true);
120         }
121
122         /* Now enough pass() calls will either:
123          *  (a) give us what we want, or
124          *  (b) hit the end of the decoder.
125          */
126         while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
127
128         /* Now look for what we wanted in the data we have collected */
129         /* XXX: inefficient */
130
131         list<T> out;
132         BOOST_FOREACH (ContentTimePeriod i, sp) {
133                 typename list<T>::const_iterator j = subs.begin();
134                 while (j != subs.end() && j->period() != i) {
135                         ++j;
136                 }
137                 if (j != subs.end()) {
138                         out.push_back (*j);
139                 }
140         }
141
142         /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
143
144         list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
145         while (i != _decoded_image.end()) {
146                 list<ContentImageSubtitle>::iterator tmp = i;
147                 ++tmp;
148
149                 if (
150                         i->period().to < (period.from - ContentTime::from_seconds (5)) ||
151                         i->period().from > (period.to + ContentTime::from_seconds (5))
152                         ) {
153                         _decoded_image.erase (i);
154                 }
155
156                 i = tmp;
157         }
158
159         return out;
160 }
161
162 list<ContentTextSubtitle>
163 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
164 {
165         return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, accurate);
166 }
167
168 list<ContentImageSubtitle>
169 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
170 {
171         return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, accurate);
172 }
173
174 void
175 SubtitleDecoder::seek (ContentTime t, bool)
176 {
177         _log->log (String::compose ("SD seek to %1", to_string(t)), LogEntry::TYPE_DEBUG_DECODE);
178         reset ();
179         _position.reset ();
180 }
181
182 void
183 SubtitleDecoder::reset ()
184 {
185         _decoded_text.clear ();
186         _decoded_image.clear ();
187 }
188
189 void
190 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
191 {
192         /* See if our next subtitle needs to be placed on screen by us */
193         bool needs_placement = false;
194         optional<int> bottom_line;
195         BOOST_FOREACH (sub::Line i, subtitle.lines) {
196                 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
197                         needs_placement = true;
198                         DCPOMATIC_ASSERT (i.vertical_position.line);
199                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
200                                 bottom_line = i.vertical_position.line.get();
201                         }
202                 }
203         }
204
205         /* Find the lowest proportional position */
206         optional<float> lowest_proportional;
207         BOOST_FOREACH (sub::Line i, subtitle.lines) {
208                 if (i.vertical_position.proportional) {
209                         if (!lowest_proportional) {
210                                 lowest_proportional = i.vertical_position.proportional;
211                         } else {
212                                 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
213                         }
214                 }
215         }
216
217         list<dcp::SubtitleString> out;
218         BOOST_FOREACH (sub::Line i, subtitle.lines) {
219                 BOOST_FOREACH (sub::Block j, i.blocks) {
220
221                         if (!j.font_size.specified()) {
222                                 /* Fallback default font size if no other has been specified */
223                                 j.font_size.set_points (48);
224                         }
225
226                         float v_position;
227                         dcp::VAlign v_align;
228                         if (needs_placement) {
229                                 DCPOMATIC_ASSERT (i.vertical_position.line);
230                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
231                                    of the screen a bit to a pleasing degree.
232                                 */
233                                 v_position = 1.015 -
234                                         (1 + bottom_line.get() - i.vertical_position.line.get())
235                                         * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
236
237                                 v_align = dcp::VALIGN_TOP;
238                         } else {
239                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
240                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
241                                 v_position = i.vertical_position.proportional.get();
242
243                                 if (lowest_proportional) {
244                                         /* Adjust line spacing */
245                                         v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
246                                 }
247
248                                 switch (i.vertical_position.reference.get()) {
249                                 case sub::TOP_OF_SCREEN:
250                                         v_align = dcp::VALIGN_TOP;
251                                         break;
252                                 case sub::CENTRE_OF_SCREEN:
253                                         v_align = dcp::VALIGN_CENTER;
254                                         break;
255                                 case sub::BOTTOM_OF_SCREEN:
256                                         v_align = dcp::VALIGN_BOTTOM;
257                                         break;
258                                 default:
259                                         v_align = dcp::VALIGN_TOP;
260                                         break;
261                                 }
262                         }
263
264                         dcp::Effect effect = dcp::NONE;
265                         if (content()->outline()) {
266                                 effect = dcp::BORDER;
267                         } else if (content()->shadow()) {
268                                 effect = dcp::SHADOW;
269                         }
270
271                         out.push_back (
272                                 dcp::SubtitleString (
273                                         string(TEXT_FONT_ID),
274                                         j.italic,
275                                         j.bold,
276                                         j.underline,
277                                         /* force the colour to whatever is configured */
278                                         content()->colour(),
279                                         j.font_size.points (72 * 11),
280                                         1.0,
281                                         dcp::Time (period.from.seconds(), 1000),
282                                         dcp::Time (period.to.seconds(), 1000),
283                                         0,
284                                         dcp::HALIGN_CENTER,
285                                         v_position,
286                                         v_align,
287                                         dcp::DIRECTION_LTR,
288                                         j.text,
289                                         effect,
290                                         content()->effect_colour(),
291                                         dcp::Time (content()->fade_in().seconds(), 1000),
292                                         dcp::Time (content()->fade_out().seconds(), 1000)
293                                         )
294                                 );
295                 }
296         }
297
298         give_text (period, out);
299 }