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