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>
32 using boost::shared_ptr;
33 using boost::optional;
34 using boost::function;
36 SubtitleDecoder::SubtitleDecoder (
38 shared_ptr<const SubtitleContent> c,
39 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
40 function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
44 , _image_during (image_during)
45 , _text_during (text_during)
50 /** Called by subclasses when an image subtitle is ready.
51 * @param period Period of the subtitle.
52 * @param image Subtitle image.
53 * @param rect Area expressed as a fraction of the video frame that this subtitle
54 * is for (e.g. a width of 0.5 means the width of the subtitle is half the width
58 SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
60 _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
64 SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
66 _decoded_text.push_back (ContentTextSubtitle (period, s));
69 /** @param sp Full periods of subtitles that are showing or starting during the specified period */
72 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool starting, bool accurate)
75 /* Nothing in this period */
79 /* Seek if what we want is before what we have, or a more than a little bit after */
80 if (subs.empty() || sp.back().to < subs.front().period().from || sp.front().from > (subs.back().period().to + ContentTime::from_seconds (1))) {
81 _parent->seek (sp.front().from, true);
84 /* Now enough pass() calls will either:
85 * (a) give us what we want, or
86 * (b) hit the end of the decoder.
88 while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
90 /* Now look for what we wanted in the data we have collected */
91 /* XXX: inefficient */
94 for (typename list<T>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
95 if ((starting && period.contains (i->period().from)) || (!starting && period.overlaps (i->period ()))) {
100 /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
102 list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
103 while (i != _decoded_image.end()) {
104 list<ContentImageSubtitle>::iterator tmp = i;
108 i->period().to < (period.from - ContentTime::from_seconds (5)) ||
109 i->period().from > (period.to + ContentTime::from_seconds (5))
111 _decoded_image.erase (i);
120 list<ContentTextSubtitle>
121 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
123 return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, starting, accurate);
126 list<ContentImageSubtitle>
127 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
129 return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, starting, accurate);
133 SubtitleDecoder::seek (ContentTime, bool)
135 _decoded_text.clear ();
136 _decoded_image.clear ();
140 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
142 /* See if our next subtitle needs to be placed on screen by us */
143 bool needs_placement = false;
144 optional<int> bottom_line;
145 BOOST_FOREACH (sub::Line i, subtitle.lines) {
146 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
147 needs_placement = true;
148 DCPOMATIC_ASSERT (i.vertical_position.line);
149 if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
150 bottom_line = i.vertical_position.line.get();
155 list<dcp::SubtitleString> out;
156 BOOST_FOREACH (sub::Line i, subtitle.lines) {
157 BOOST_FOREACH (sub::Block j, i.blocks) {
159 if (!j.font_size.specified()) {
160 /* Fallback default font size if none other has been specified */
161 j.font_size.set_points (48);
166 if (needs_placement) {
167 DCPOMATIC_ASSERT (i.vertical_position.line);
168 /* This 0.053 is an arbitrary value to lift the bottom sub off the bottom
169 of the screen a bit to a pleasing degree.
171 v_position = 1.015 - (1 + bottom_line.get() - i.vertical_position.line.get()) * 1.2 * j.font_size.proportional (72 * 11);
172 v_align = dcp::VALIGN_TOP;
174 DCPOMATIC_ASSERT (i.vertical_position.proportional);
175 DCPOMATIC_ASSERT (i.vertical_position.reference);
176 v_position = i.vertical_position.proportional.get();
177 switch (i.vertical_position.reference.get()) {
178 case sub::TOP_OF_SCREEN:
179 v_align = dcp::VALIGN_TOP;
181 case sub::CENTRE_OF_SCREEN:
182 v_align = dcp::VALIGN_CENTER;
184 case sub::BOTTOM_OF_SCREEN:
185 v_align = dcp::VALIGN_BOTTOM;
188 v_align = dcp::VALIGN_TOP;
194 dcp::SubtitleString (
195 string(TEXT_FONT_ID),
198 /* force the colour to whatever is configured */
200 j.font_size.points (72 * 11),
202 dcp::Time (period.from.seconds(), 1000),
203 dcp::Time (period.to.seconds(), 1000),
210 content()->outline() ? dcp::BORDER : dcp::NONE,
211 content()->outline_colour(),
219 give_text (period, out);