Fix some code duplication and crashes when decoding FFmpeg-embedded ASS subtitles...
[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         BOOST_FOREACH (sub::Line i, subtitle.lines) {
145                 if (!i.vertical_position.reference && i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
146                         needs_placement = true;
147                 }
148         }
149
150         list<dcp::SubtitleString> out;
151         BOOST_FOREACH (sub::Line i, subtitle.lines) {
152                 BOOST_FOREACH (sub::Block j, i.blocks) {
153
154                         float v_position;
155                         dcp::VAlign v_align;
156                         if (needs_placement) {
157                                 DCPOMATIC_ASSERT (i.vertical_position.line);
158                                 /* This 0.878 is an arbitrary value to lift the bottom sub off the bottom
159                                    of the screen a bit to a pleasing degree.
160                                 */
161                                 v_position = 0.878 + i.vertical_position.line.get() * 1.5 / 22;
162                                 v_align = dcp::VALIGN_BOTTOM;
163                         } else {
164                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
165                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
166                                 v_position = i.vertical_position.proportional.get();
167                                 switch (i.vertical_position.reference.get()) {
168                                 case sub::TOP_OF_SCREEN:
169                                         v_align = dcp::VALIGN_TOP;
170                                         break;
171                                 case sub::CENTRE_OF_SCREEN:
172                                         v_align = dcp::VALIGN_CENTER;
173                                         break;
174                                 case sub::BOTTOM_OF_SCREEN:
175                                         v_align = dcp::VALIGN_BOTTOM;
176                                         break;
177                                 default:
178                                         v_align = dcp::VALIGN_TOP;
179                                         break;
180                                 }
181                         }
182
183                         out.push_back (
184                                 dcp::SubtitleString (
185                                         string(TEXT_FONT_ID),
186                                         j.italic,
187                                         j.bold,
188                                         /* force the colour to whatever is configured */
189                                         content()->colour(),
190                                         j.font_size.points (72 * 11),
191                                         1.0,
192                                         dcp::Time (subtitle.from.all_as_seconds(), 1000),
193                                         dcp::Time (subtitle.to.all_as_seconds(), 1000),
194                                         0,
195                                         dcp::HALIGN_CENTER,
196                                         v_position,
197                                         v_align,
198                                         dcp::DIRECTION_LTR,
199                                         j.text,
200                                         content()->outline() ? dcp::BORDER : dcp::NONE,
201                                         content()->outline_colour(),
202                                         dcp::Time (0, 1000),
203                                         dcp::Time (0, 1000)
204                                         )
205                                 );
206                 }
207         }
208
209         give_text (period, out);
210 }