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
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 /** @param sp Full periods of subtitles that are showing or starting during the specified period */
84 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool starting, bool accurate)
87 /* Nothing in this period */
91 /* Seek if what we want is before what we have, or a more than a little bit after */
92 if (subs.empty() || sp.back().to < subs.front().period().from || sp.front().from > (subs.back().period().to + ContentTime::from_seconds (1))) {
93 _parent->seek (sp.front().from, true);
96 /* Now enough pass() calls will either:
97 * (a) give us what we want, or
98 * (b) hit the end of the decoder.
100 while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
102 /* Now look for what we wanted in the data we have collected */
103 /* XXX: inefficient */
106 for (typename list<T>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
107 if ((starting && period.contains(i->period().from)) || (!starting && period.overlap(i->period()))) {
112 /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
114 list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
115 while (i != _decoded_image.end()) {
116 list<ContentImageSubtitle>::iterator tmp = i;
120 i->period().to < (period.from - ContentTime::from_seconds (5)) ||
121 i->period().from > (period.to + ContentTime::from_seconds (5))
123 _decoded_image.erase (i);
132 list<ContentTextSubtitle>
133 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
135 return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, starting, accurate);
138 list<ContentImageSubtitle>
139 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
141 return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, starting, accurate);
145 SubtitleDecoder::seek (ContentTime, bool)
151 SubtitleDecoder::reset ()
153 _decoded_text.clear ();
154 _decoded_image.clear ();
158 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
160 /* See if our next subtitle needs to be placed on screen by us */
161 bool needs_placement = false;
162 optional<int> bottom_line;
163 BOOST_FOREACH (sub::Line i, subtitle.lines) {
164 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
165 needs_placement = true;
166 DCPOMATIC_ASSERT (i.vertical_position.line);
167 if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
168 bottom_line = i.vertical_position.line.get();
173 /* Find the lowest proportional postion */
174 optional<float> lowest_proportional;
175 BOOST_FOREACH (sub::Line i, subtitle.lines) {
176 if (i.vertical_position.proportional) {
177 if (!lowest_proportional) {
178 lowest_proportional = i.vertical_position.proportional;
180 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
185 list<dcp::SubtitleString> out;
186 BOOST_FOREACH (sub::Line i, subtitle.lines) {
187 BOOST_FOREACH (sub::Block j, i.blocks) {
189 if (!j.font_size.specified()) {
190 /* Fallback default font size if none other has been specified */
191 j.font_size.set_points (48);
196 if (needs_placement) {
197 DCPOMATIC_ASSERT (i.vertical_position.line);
198 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
199 of the screen a bit to a pleasing degree.
202 (1 + bottom_line.get() - i.vertical_position.line.get())
203 * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
205 v_align = dcp::VALIGN_TOP;
207 DCPOMATIC_ASSERT (i.vertical_position.proportional);
208 DCPOMATIC_ASSERT (i.vertical_position.reference);
209 v_position = i.vertical_position.proportional.get();
211 if (lowest_proportional) {
212 /* Adjust line spacing */
213 v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
216 switch (i.vertical_position.reference.get()) {
217 case sub::TOP_OF_SCREEN:
218 v_align = dcp::VALIGN_TOP;
220 case sub::CENTRE_OF_SCREEN:
221 v_align = dcp::VALIGN_CENTER;
223 case sub::BOTTOM_OF_SCREEN:
224 v_align = dcp::VALIGN_BOTTOM;
227 v_align = dcp::VALIGN_TOP;
232 dcp::Effect effect = dcp::NONE;
233 if (content()->outline()) {
234 effect = dcp::BORDER;
235 } else if (content()->shadow()) {
236 effect = dcp::SHADOW;
240 dcp::SubtitleString (
241 string(TEXT_FONT_ID),
245 /* force the colour to whatever is configured */
247 j.font_size.points (72 * 11),
249 dcp::Time (period.from.seconds(), 1000),
250 dcp::Time (period.to.seconds(), 1000),
258 content()->effect_colour(),
259 dcp::Time (content()->fade_in().seconds(), 1000),
260 dcp::Time (content()->fade_out().seconds(), 1000)
266 give_text (period, out);