First go at making the line spacing do something.
[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         _decoded_text.clear ();
136         _decoded_image.clear ();
137 }
138
139 void
140 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
141 {
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();
151                         }
152                 }
153         }
154
155         list<dcp::SubtitleString> out;
156         BOOST_FOREACH (sub::Line i, subtitle.lines) {
157                 BOOST_FOREACH (sub::Block j, i.blocks) {
158
159                         if (!j.font_size.specified()) {
160                                 /* Fallback default font size if none other has been specified */
161                                 j.font_size.set_points (48);
162                         }
163
164                         float v_position;
165                         dcp::VAlign v_align;
166                         if (needs_placement) {
167                                 DCPOMATIC_ASSERT (i.vertical_position.line);
168                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
169                                    of the screen a bit to a pleasing degree.
170                                 */
171                                 v_position = 1.015 - (1 + bottom_line.get() - i.vertical_position.line.get())
172                                         * 1.2 * content()->line_spacing() * j.font_size.proportional (72 * 11);
173
174                                 v_align = dcp::VALIGN_TOP;
175                         } else {
176                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
177                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
178                                 v_position = i.vertical_position.proportional.get();
179                                 switch (i.vertical_position.reference.get()) {
180                                 case sub::TOP_OF_SCREEN:
181                                         v_align = dcp::VALIGN_TOP;
182                                         break;
183                                 case sub::CENTRE_OF_SCREEN:
184                                         v_align = dcp::VALIGN_CENTER;
185                                         break;
186                                 case sub::BOTTOM_OF_SCREEN:
187                                         v_align = dcp::VALIGN_BOTTOM;
188                                         break;
189                                 default:
190                                         v_align = dcp::VALIGN_TOP;
191                                         break;
192                                 }
193                         }
194
195                         out.push_back (
196                                 dcp::SubtitleString (
197                                         string(TEXT_FONT_ID),
198                                         j.italic,
199                                         j.bold,
200                                         j.underline,
201                                         /* force the colour to whatever is configured */
202                                         content()->colour(),
203                                         j.font_size.points (72 * 11),
204                                         1.0,
205                                         dcp::Time (period.from.seconds(), 1000),
206                                         dcp::Time (period.to.seconds(), 1000),
207                                         0,
208                                         dcp::HALIGN_CENTER,
209                                         v_position,
210                                         v_align,
211                                         dcp::DIRECTION_LTR,
212                                         j.text,
213                                         content()->outline() ? dcp::BORDER : dcp::NONE,
214                                         content()->outline_colour(),
215                                         dcp::Time (0, 1000),
216                                         dcp::Time (0, 1000)
217                                         )
218                                 );
219                 }
220         }
221
222         give_text (period, out);
223 }