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"
24 #include <sub/subtitle.h>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/foreach.hpp>
27 #include <boost/algorithm/string.hpp>
34 using boost::shared_ptr;
35 using boost::optional;
36 using boost::function;
38 SubtitleDecoder::SubtitleDecoder (
40 shared_ptr<const SubtitleContent> c,
41 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
42 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
44 : DecoderPart (parent)
46 , _image_during (image_during)
47 , _text_during (text_during)
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
60 SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
62 _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
66 SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
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.)
71 BOOST_FOREACH (dcp::SubtitleString& i, s) {
73 boost::algorithm::replace_all (t, "<", "<");
74 boost::algorithm::replace_all (t, ">", ">");
78 _decoded_text.push_back (ContentTextSubtitle (period, s));
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.
87 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool accurate)
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) {
100 if (j == subs.end ()) {
105 /* Suggest to our parent decoder that it might want to seek if we haven't got what we're being asked for */
107 maybe_seek (*missing, true);
110 /* Now enough pass() calls will either:
111 * (a) give us what we want, or
112 * (b) hit the end of the decoder.
114 while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
116 /* Now look for what we wanted in the data we have collected */
117 /* XXX: inefficient */
120 BOOST_FOREACH (ContentTimePeriod i, sp) {
121 typename list<T>::const_iterator j = subs.begin();
122 while (j != subs.end() && j->period() != i) {
125 if (j != subs.end()) {
130 /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
132 list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
133 while (i != _decoded_image.end()) {
134 list<ContentImageSubtitle>::iterator tmp = i;
138 i->period().to < (period.from - ContentTime::from_seconds (5)) ||
139 i->period().from > (period.to + ContentTime::from_seconds (5))
141 _decoded_image.erase (i);
150 list<ContentTextSubtitle>
151 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
153 return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, accurate);
156 list<ContentImageSubtitle>
157 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
159 return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, accurate);
163 SubtitleDecoder::seek (ContentTime, bool)
169 SubtitleDecoder::reset ()
171 _decoded_text.clear ();
172 _decoded_image.clear ();
176 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
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();
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;
198 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
203 list<dcp::SubtitleString> out;
204 BOOST_FOREACH (sub::Line i, subtitle.lines) {
205 BOOST_FOREACH (sub::Block j, i.blocks) {
207 if (!j.font_size.specified()) {
208 /* Fallback default font size if no other has been specified */
209 j.font_size.set_points (48);
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.
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);
223 v_align = dcp::VALIGN_TOP;
225 DCPOMATIC_ASSERT (i.vertical_position.proportional);
226 DCPOMATIC_ASSERT (i.vertical_position.reference);
227 v_position = i.vertical_position.proportional.get();
229 if (lowest_proportional) {
230 /* Adjust line spacing */
231 v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
234 switch (i.vertical_position.reference.get()) {
235 case sub::TOP_OF_SCREEN:
236 v_align = dcp::VALIGN_TOP;
238 case sub::CENTRE_OF_SCREEN:
239 v_align = dcp::VALIGN_CENTER;
241 case sub::BOTTOM_OF_SCREEN:
242 v_align = dcp::VALIGN_BOTTOM;
245 v_align = dcp::VALIGN_TOP;
250 dcp::Effect effect = dcp::NONE;
251 if (content()->outline()) {
252 effect = dcp::BORDER;
253 } else if (content()->shadow()) {
254 effect = dcp::SHADOW;
258 dcp::SubtitleString (
259 string(TEXT_FONT_ID),
263 /* force the colour to whatever is configured */
265 j.font_size.points (72 * 11),
267 dcp::Time (period.from.seconds(), 1000),
268 dcp::Time (period.to.seconds(), 1000),
276 content()->effect_colour(),
277 dcp::Time (content()->fade_in().seconds(), 1000),
278 dcp::Time (content()->fade_out().seconds(), 1000)
284 give_text (period, out);