2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "subtitle_decoder.h"
22 #include "subtitle_content.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>
36 using boost::shared_ptr;
37 using boost::optional;
38 using boost::function;
40 SubtitleDecoder::SubtitleDecoder (
42 shared_ptr<const SubtitleContent> c,
44 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
45 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
47 : DecoderPart (parent, log)
49 , _image_during (image_during)
50 , _text_during (text_during)
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
63 SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
65 _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
66 _position = period.from;
70 SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
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.)
75 BOOST_FOREACH (dcp::SubtitleString& i, s) {
77 boost::algorithm::replace_all (t, "<", "<");
78 boost::algorithm::replace_all (t, ">", ">");
82 _decoded_text.push_back (ContentTextSubtitle (period, s));
83 _position = period.to;
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.
92 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool accurate)
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) {
105 if (j == subs.end ()) {
110 /* Suggest to our parent decoder that it might want to seek if we haven't got what we're being asked for */
114 "SD suggests seek to %1 from %2",
115 to_string (*missing),
116 position() ? to_string(*position()) : "nowhere"),
117 LogEntry::TYPE_DEBUG_DECODE);
118 maybe_seek (*missing, true);
121 /* Now enough pass() calls will either:
122 * (a) give us what we want, or
123 * (b) hit the end of the decoder.
125 while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
127 /* Now look for what we wanted in the data we have collected */
128 /* XXX: inefficient */
131 BOOST_FOREACH (ContentTimePeriod i, sp) {
132 typename list<T>::const_iterator j = subs.begin();
133 while (j != subs.end() && j->period() != i) {
136 if (j != subs.end()) {
141 /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
143 list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
144 while (i != _decoded_image.end()) {
145 list<ContentImageSubtitle>::iterator tmp = i;
149 i->period().to < (period.from - ContentTime::from_seconds (5)) ||
150 i->period().from > (period.to + ContentTime::from_seconds (5))
152 _decoded_image.erase (i);
161 list<ContentTextSubtitle>
162 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
164 return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, accurate);
167 list<ContentImageSubtitle>
168 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
170 return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, accurate);
174 SubtitleDecoder::seek (ContentTime t, bool)
176 _log->log (String::compose ("SD seek to %1", to_string(t)), LogEntry::TYPE_DEBUG_DECODE);
182 SubtitleDecoder::reset ()
184 _decoded_text.clear ();
185 _decoded_image.clear ();
189 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
191 /* See if our next subtitle needs to be placed on screen by us */
192 bool needs_placement = false;
193 optional<int> bottom_line;
194 BOOST_FOREACH (sub::Line i, subtitle.lines) {
195 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
196 needs_placement = true;
197 DCPOMATIC_ASSERT (i.vertical_position.line);
198 if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
199 bottom_line = i.vertical_position.line.get();
204 /* Find the lowest proportional position */
205 optional<float> lowest_proportional;
206 BOOST_FOREACH (sub::Line i, subtitle.lines) {
207 if (i.vertical_position.proportional) {
208 if (!lowest_proportional) {
209 lowest_proportional = i.vertical_position.proportional;
211 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
216 list<dcp::SubtitleString> out;
217 BOOST_FOREACH (sub::Line i, subtitle.lines) {
218 BOOST_FOREACH (sub::Block j, i.blocks) {
220 if (!j.font_size.specified()) {
221 /* Fallback default font size if no other has been specified */
222 j.font_size.set_points (48);
227 if (needs_placement) {
228 DCPOMATIC_ASSERT (i.vertical_position.line);
229 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
230 of the screen a bit to a pleasing degree.
233 (1 + bottom_line.get() - i.vertical_position.line.get())
234 * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
236 v_align = dcp::VALIGN_TOP;
238 DCPOMATIC_ASSERT (i.vertical_position.proportional);
239 DCPOMATIC_ASSERT (i.vertical_position.reference);
240 v_position = i.vertical_position.proportional.get();
242 if (lowest_proportional) {
243 /* Adjust line spacing */
244 v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
247 switch (i.vertical_position.reference.get()) {
248 case sub::TOP_OF_SCREEN:
249 v_align = dcp::VALIGN_TOP;
251 case sub::CENTRE_OF_SCREEN:
252 v_align = dcp::VALIGN_CENTER;
254 case sub::BOTTOM_OF_SCREEN:
255 v_align = dcp::VALIGN_BOTTOM;
258 v_align = dcp::VALIGN_TOP;
263 dcp::Effect effect = dcp::NONE;
264 if (content()->outline()) {
265 effect = dcp::BORDER;
266 } else if (content()->shadow()) {
267 effect = dcp::SHADOW;
271 dcp::SubtitleString (
272 string(TEXT_FONT_ID),
276 /* force the colour to whatever is configured */
278 j.font_size.points (72 * 11),
280 dcp::Time (period.from.seconds(), 1000),
281 dcp::Time (period.to.seconds(), 1000),
289 content()->effect_colour(),
290 dcp::Time (content()->fade_in().seconds(), 1000),
291 dcp::Time (content()->fade_out().seconds(), 1000)
297 give_text (period, out);