63ab7c471a634a421c03930317fe1e991ab55b8b
[dcpomatic.git] / src / lib / subtitle_decoder.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "subtitle_decoder.h"
22 #include "subtitle_content.h"
23 #include "util.h"
24 #include <sub/subtitle.h>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/foreach.hpp>
27 #include <iostream>
28
29 using std::list;
30 using std::cout;
31 using std::string;
32 using boost::shared_ptr;
33 using boost::optional;
34 using boost::function;
35
36 SubtitleDecoder::SubtitleDecoder (
37         Decoder* parent,
38         shared_ptr<const SubtitleContent> c,
39         function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
40         function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
41         )
42         : _parent (parent)
43         , _content (c)
44         , _image_during (image_during)
45         , _text_during (text_during)
46 {
47
48 }
49
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
55  *  of the video frame)
56  */
57 void
58 SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
59 {
60         _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
61 }
62
63 void
64 SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
65 {
66         _decoded_text.push_back (ContentTextSubtitle (period, s));
67 }
68
69 /** @param sp Full periods of subtitles that are showing or starting during the specified period */
70 template <class T>
71 list<T>
72 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool starting, bool accurate)
73 {
74         if (sp.empty ()) {
75                 /* Nothing in this period */
76                 return list<T> ();
77         }
78
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);
82         }
83
84         /* Now enough pass() calls will either:
85          *  (a) give us what we want, or
86          *  (b) hit the end of the decoder.
87          */
88         while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
89
90         /* Now look for what we wanted in the data we have collected */
91         /* XXX: inefficient */
92
93         list<T> out;
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 ()))) {
96                         out.push_back (*i);
97                 }
98         }
99
100         /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
101
102         list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
103         while (i != _decoded_image.end()) {
104                 list<ContentImageSubtitle>::iterator tmp = i;
105                 ++tmp;
106
107                 if (
108                         i->period().to < (period.from - ContentTime::from_seconds (5)) ||
109                         i->period().from > (period.to + ContentTime::from_seconds (5))
110                         ) {
111                         _decoded_image.erase (i);
112                 }
113
114                 i = tmp;
115         }
116
117         return out;
118 }
119
120 list<ContentTextSubtitle>
121 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
122 {
123         return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, starting, accurate);
124 }
125
126 list<ContentImageSubtitle>
127 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
128 {
129         return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, starting, accurate);
130 }
131
132 void
133 SubtitleDecoder::seek (ContentTime, bool)
134 {
135         reset ();
136 }
137
138 void
139 SubtitleDecoder::reset ()
140 {
141         _decoded_text.clear ();
142         _decoded_image.clear ();
143 }
144
145 void
146 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
147 {
148         /* See if our next subtitle needs to be placed on screen by us */
149         bool needs_placement = false;
150         optional<int> bottom_line;
151         BOOST_FOREACH (sub::Line i, subtitle.lines) {
152                 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
153                         needs_placement = true;
154                         DCPOMATIC_ASSERT (i.vertical_position.line);
155                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
156                                 bottom_line = i.vertical_position.line.get();
157                         }
158                 }
159         }
160
161         list<dcp::SubtitleString> out;
162         BOOST_FOREACH (sub::Line i, subtitle.lines) {
163                 BOOST_FOREACH (sub::Block j, i.blocks) {
164
165                         if (!j.font_size.specified()) {
166                                 /* Fallback default font size if none other has been specified */
167                                 j.font_size.set_points (48);
168                         }
169
170                         float v_position;
171                         dcp::VAlign v_align;
172                         if (needs_placement) {
173                                 DCPOMATIC_ASSERT (i.vertical_position.line);
174                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
175                                    of the screen a bit to a pleasing degree.
176                                 */
177                                 v_position = 1.015 - (1 + bottom_line.get() - i.vertical_position.line.get())
178                                         * 1.2 * content()->line_spacing() * j.font_size.proportional (72 * 11);
179
180                                 v_align = dcp::VALIGN_TOP;
181                         } else {
182                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
183                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
184                                 v_position = i.vertical_position.proportional.get();
185                                 switch (i.vertical_position.reference.get()) {
186                                 case sub::TOP_OF_SCREEN:
187                                         v_align = dcp::VALIGN_TOP;
188                                         break;
189                                 case sub::CENTRE_OF_SCREEN:
190                                         v_align = dcp::VALIGN_CENTER;
191                                         break;
192                                 case sub::BOTTOM_OF_SCREEN:
193                                         v_align = dcp::VALIGN_BOTTOM;
194                                         break;
195                                 default:
196                                         v_align = dcp::VALIGN_TOP;
197                                         break;
198                                 }
199                         }
200
201                         out.push_back (
202                                 dcp::SubtitleString (
203                                         string(TEXT_FONT_ID),
204                                         j.italic,
205                                         j.bold,
206                                         j.underline,
207                                         /* force the colour to whatever is configured */
208                                         content()->colour(),
209                                         j.font_size.points (72 * 11),
210                                         1.0,
211                                         dcp::Time (period.from.seconds(), 1000),
212                                         dcp::Time (period.to.seconds(), 1000),
213                                         0,
214                                         dcp::HALIGN_CENTER,
215                                         v_position,
216                                         v_align,
217                                         dcp::DIRECTION_LTR,
218                                         j.text,
219                                         content()->outline() ? dcp::BORDER : dcp::NONE,
220                                         content()->outline_colour(),
221                                         dcp::Time (0, 1000),
222                                         dcp::Time (0, 1000)
223                                         )
224                                 );
225                 }
226         }
227
228         give_text (period, out);
229 }