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