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>
33 using boost::shared_ptr;
34 using boost::optional;
35 using boost::function;
37 SubtitleDecoder::SubtitleDecoder (
39 shared_ptr<const SubtitleContent> c,
40 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
41 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
45 , _image_during (image_during)
46 , _text_during (text_during)
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
59 SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
61 _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
65 SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
67 _decoded_text.push_back (ContentTextSubtitle (period, s));
70 /** @param sp Full periods of subtitles that are showing or starting during the specified period */
73 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool starting, bool accurate)
76 /* Nothing in this period */
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);
85 /* Now enough pass() calls will either:
86 * (a) give us what we want, or
87 * (b) hit the end of the decoder.
89 while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
91 /* Now look for what we wanted in the data we have collected */
92 /* XXX: inefficient */
95 for (typename list<T>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
96 if ((starting && period.contains(i->period().from)) || (!starting && period.overlap(i->period()))) {
101 /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
103 list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
104 while (i != _decoded_image.end()) {
105 list<ContentImageSubtitle>::iterator tmp = i;
109 i->period().to < (period.from - ContentTime::from_seconds (5)) ||
110 i->period().from > (period.to + ContentTime::from_seconds (5))
112 _decoded_image.erase (i);
121 list<ContentTextSubtitle>
122 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
124 return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, starting, accurate);
127 list<ContentImageSubtitle>
128 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
130 return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, starting, accurate);
134 SubtitleDecoder::seek (ContentTime, bool)
140 SubtitleDecoder::reset ()
142 _decoded_text.clear ();
143 _decoded_image.clear ();
147 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
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();
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;
169 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
174 list<dcp::SubtitleString> out;
175 BOOST_FOREACH (sub::Line i, subtitle.lines) {
176 BOOST_FOREACH (sub::Block j, i.blocks) {
178 if (!j.font_size.specified()) {
179 /* Fallback default font size if none other has been specified */
180 j.font_size.set_points (48);
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.
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);
194 v_align = dcp::VALIGN_TOP;
196 DCPOMATIC_ASSERT (i.vertical_position.proportional);
197 DCPOMATIC_ASSERT (i.vertical_position.reference);
198 v_position = i.vertical_position.proportional.get();
200 if (lowest_proportional) {
201 /* Adjust line spacing */
202 v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
205 switch (i.vertical_position.reference.get()) {
206 case sub::TOP_OF_SCREEN:
207 v_align = dcp::VALIGN_TOP;
209 case sub::CENTRE_OF_SCREEN:
210 v_align = dcp::VALIGN_CENTER;
212 case sub::BOTTOM_OF_SCREEN:
213 v_align = dcp::VALIGN_BOTTOM;
216 v_align = dcp::VALIGN_TOP;
221 dcp::Effect effect = dcp::NONE;
222 if (content()->outline()) {
223 effect = dcp::BORDER;
224 } else if (content()->shadow()) {
225 effect = dcp::SHADOW;
229 dcp::SubtitleString (
230 string(TEXT_FONT_ID),
234 /* force the colour to whatever is configured */
236 j.font_size.points (72 * 11),
238 dcp::Time (period.from.seconds(), 1000),
239 dcp::Time (period.to.seconds(), 1000),
247 content()->effect_colour(),
255 give_text (period, out);