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