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 /** 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 /* 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);
98 /* Now enough pass() calls will either:
99 * (a) give us what we want, or
100 * (b) hit the end of the decoder.
102 while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
104 /* Now look for what we wanted in the data we have collected */
105 /* XXX: inefficient */
108 BOOST_FOREACH (ContentTimePeriod i, sp) {
109 typename list<T>::const_iterator j = subs.begin();
110 while (j != subs.end() && j->period() != i) {
113 if (j != subs.end()) {
118 /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
120 list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
121 while (i != _decoded_image.end()) {
122 list<ContentImageSubtitle>::iterator tmp = i;
126 i->period().to < (period.from - ContentTime::from_seconds (5)) ||
127 i->period().from > (period.to + ContentTime::from_seconds (5))
129 _decoded_image.erase (i);
138 list<ContentTextSubtitle>
139 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
141 return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, accurate);
144 list<ContentImageSubtitle>
145 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
147 return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, accurate);
151 SubtitleDecoder::seek (ContentTime, bool)
157 SubtitleDecoder::reset ()
159 _decoded_text.clear ();
160 _decoded_image.clear ();
164 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
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();
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;
186 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
191 list<dcp::SubtitleString> out;
192 BOOST_FOREACH (sub::Line i, subtitle.lines) {
193 BOOST_FOREACH (sub::Block j, i.blocks) {
195 if (!j.font_size.specified()) {
196 /* Fallback default font size if no other has been specified */
197 j.font_size.set_points (48);
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.
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);
211 v_align = dcp::VALIGN_TOP;
213 DCPOMATIC_ASSERT (i.vertical_position.proportional);
214 DCPOMATIC_ASSERT (i.vertical_position.reference);
215 v_position = i.vertical_position.proportional.get();
217 if (lowest_proportional) {
218 /* Adjust line spacing */
219 v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
222 switch (i.vertical_position.reference.get()) {
223 case sub::TOP_OF_SCREEN:
224 v_align = dcp::VALIGN_TOP;
226 case sub::CENTRE_OF_SCREEN:
227 v_align = dcp::VALIGN_CENTER;
229 case sub::BOTTOM_OF_SCREEN:
230 v_align = dcp::VALIGN_BOTTOM;
233 v_align = dcp::VALIGN_TOP;
238 dcp::Effect effect = dcp::NONE;
239 if (content()->outline()) {
240 effect = dcp::BORDER;
241 } else if (content()->shadow()) {
242 effect = dcp::SHADOW;
246 dcp::SubtitleString (
247 string(TEXT_FONT_ID),
251 /* force the colour to whatever is configured */
253 j.font_size.points (72 * 11),
255 dcp::Time (period.from.seconds(), 1000),
256 dcp::Time (period.to.seconds(), 1000),
264 content()->effect_colour(),
265 dcp::Time (content()->fade_in().seconds(), 1000),
266 dcp::Time (content()->fade_out().seconds(), 1000)
272 give_text (period, out);