307226da6b184a25e12585dd14b2d83c4880c0a2
[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         : _parent (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         /* Seek if what we want is before what we have, or a more than a little bit after */
94         if (subs.empty() || sp.back().to < subs.front().period().from || sp.front().from > (subs.back().period().to + ContentTime::from_seconds (1))) {
95                 _parent->seek (sp.front().from, true);
96         }
97
98         /* Now enough pass() calls will either:
99          *  (a) give us what we want, or
100          *  (b) hit the end of the decoder.
101          */
102         while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
103
104         /* Now look for what we wanted in the data we have collected */
105         /* XXX: inefficient */
106
107         list<T> out;
108         BOOST_FOREACH (ContentTimePeriod i, sp) {
109                 typename list<T>::const_iterator j = subs.begin();
110                 while (j != subs.end() && j->period() != i) {
111                         ++j;
112                 }
113                 if (j != subs.end()) {
114                         out.push_back (*j);
115                 }
116         }
117
118         /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
119
120         list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
121         while (i != _decoded_image.end()) {
122                 list<ContentImageSubtitle>::iterator tmp = i;
123                 ++tmp;
124
125                 if (
126                         i->period().to < (period.from - ContentTime::from_seconds (5)) ||
127                         i->period().from > (period.to + ContentTime::from_seconds (5))
128                         ) {
129                         _decoded_image.erase (i);
130                 }
131
132                 i = tmp;
133         }
134
135         return out;
136 }
137
138 list<ContentTextSubtitle>
139 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
140 {
141         return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, accurate);
142 }
143
144 list<ContentImageSubtitle>
145 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
146 {
147         return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, accurate);
148 }
149
150 void
151 SubtitleDecoder::seek (ContentTime, bool)
152 {
153         reset ();
154 }
155
156 void
157 SubtitleDecoder::reset ()
158 {
159         _decoded_text.clear ();
160         _decoded_image.clear ();
161 }
162
163 void
164 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
165 {
166         /* See if our next subtitle needs to be placed on screen by us */
167         bool needs_placement = false;
168         optional<int> bottom_line;
169         BOOST_FOREACH (sub::Line i, subtitle.lines) {
170                 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
171                         needs_placement = true;
172                         DCPOMATIC_ASSERT (i.vertical_position.line);
173                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
174                                 bottom_line = i.vertical_position.line.get();
175                         }
176                 }
177         }
178
179         /* Find the lowest proportional position */
180         optional<float> lowest_proportional;
181         BOOST_FOREACH (sub::Line i, subtitle.lines) {
182                 if (i.vertical_position.proportional) {
183                         if (!lowest_proportional) {
184                                 lowest_proportional = i.vertical_position.proportional;
185                         } else {
186                                 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
187                         }
188                 }
189         }
190
191         list<dcp::SubtitleString> out;
192         BOOST_FOREACH (sub::Line i, subtitle.lines) {
193                 BOOST_FOREACH (sub::Block j, i.blocks) {
194
195                         if (!j.font_size.specified()) {
196                                 /* Fallback default font size if no other has been specified */
197                                 j.font_size.set_points (48);
198                         }
199
200                         float v_position;
201                         dcp::VAlign v_align;
202                         if (needs_placement) {
203                                 DCPOMATIC_ASSERT (i.vertical_position.line);
204                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
205                                    of the screen a bit to a pleasing degree.
206                                 */
207                                 v_position = 1.015 -
208                                         (1 + bottom_line.get() - i.vertical_position.line.get())
209                                         * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
210
211                                 v_align = dcp::VALIGN_TOP;
212                         } else {
213                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
214                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
215                                 v_position = i.vertical_position.proportional.get();
216
217                                 if (lowest_proportional) {
218                                         /* Adjust line spacing */
219                                         v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
220                                 }
221
222                                 switch (i.vertical_position.reference.get()) {
223                                 case sub::TOP_OF_SCREEN:
224                                         v_align = dcp::VALIGN_TOP;
225                                         break;
226                                 case sub::CENTRE_OF_SCREEN:
227                                         v_align = dcp::VALIGN_CENTER;
228                                         break;
229                                 case sub::BOTTOM_OF_SCREEN:
230                                         v_align = dcp::VALIGN_BOTTOM;
231                                         break;
232                                 default:
233                                         v_align = dcp::VALIGN_TOP;
234                                         break;
235                                 }
236                         }
237
238                         dcp::Effect effect = dcp::NONE;
239                         if (content()->outline()) {
240                                 effect = dcp::BORDER;
241                         } else if (content()->shadow()) {
242                                 effect = dcp::SHADOW;
243                         }
244
245                         out.push_back (
246                                 dcp::SubtitleString (
247                                         string(TEXT_FONT_ID),
248                                         j.italic,
249                                         j.bold,
250                                         j.underline,
251                                         /* force the colour to whatever is configured */
252                                         content()->colour(),
253                                         j.font_size.points (72 * 11),
254                                         1.0,
255                                         dcp::Time (period.from.seconds(), 1000),
256                                         dcp::Time (period.to.seconds(), 1000),
257                                         0,
258                                         dcp::HALIGN_CENTER,
259                                         v_position,
260                                         v_align,
261                                         dcp::DIRECTION_LTR,
262                                         j.text,
263                                         effect,
264                                         content()->effect_colour(),
265                                         dcp::Time (content()->fade_in().seconds(), 1000),
266                                         dcp::Time (content()->fade_out().seconds(), 1000)
267                                         )
268                                 );
269                 }
270         }
271
272         give_text (period, out);
273 }