Merge identical time periods returned from text_subtitles_during
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-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 "dcp_subtitle_decoder.h"
22 #include "dcp_subtitle_content.h"
23 #include <dcp/interop_subtitle_asset.h>
24 #include <iostream>
25
26 using std::list;
27 using std::cout;
28 using boost::shared_ptr;
29 using boost::bind;
30
31 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const DCPSubtitleContent> content)
32 {
33         subtitle.reset (
34                 new SubtitleDecoder (
35                         this,
36                         content->subtitle,
37                         bind (&DCPSubtitleDecoder::image_subtitles_during, this, _1, _2),
38                         bind (&DCPSubtitleDecoder::text_subtitles_during, this, _1, _2)
39                         )
40                 );
41
42         shared_ptr<dcp::SubtitleAsset> c (load (content->path (0)));
43         _subtitles = c->subtitles ();
44         _next = _subtitles.begin ();
45 }
46
47 void
48 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
49 {
50         subtitle->seek (time, accurate);
51
52         _next = _subtitles.begin ();
53         list<dcp::SubtitleString>::const_iterator i = _subtitles.begin ();
54         while (i != _subtitles.end() && ContentTime::from_seconds (_next->in().as_seconds()) < time) {
55                 ++i;
56         }
57 }
58
59 bool
60 DCPSubtitleDecoder::pass (PassReason, bool)
61 {
62         if (_next == _subtitles.end ()) {
63                 return true;
64         }
65
66         /* Gather all subtitles with the same time period that are next
67            on the list.  We must emit all subtitles for the same time
68            period with the same text_subtitle() call otherwise the
69            SubtitleDecoder will assume there is nothing else at the
70            time of emit the first.
71         */
72
73         list<dcp::SubtitleString> s;
74         ContentTimePeriod const p = content_time_period (*_next);
75
76         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
77                 s.push_back (*_next);
78                 ++_next;
79         }
80
81         subtitle->give_text (p, s);
82
83         return false;
84 }
85
86 list<ContentTimePeriod>
87 DCPSubtitleDecoder::image_subtitles_during (ContentTimePeriod, bool) const
88 {
89         return list<ContentTimePeriod> ();
90 }
91
92 list<ContentTimePeriod>
93 DCPSubtitleDecoder::text_subtitles_during (ContentTimePeriod p, bool starting) const
94 {
95         /* XXX: inefficient */
96
97         list<ContentTimePeriod> d;
98
99         for (list<dcp::SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
100                 ContentTimePeriod period = content_time_period (*i);
101                 if ((starting && p.contains(period.from)) || (!starting && p.overlap(period))) {
102                         d.push_back (period);
103                 }
104         }
105
106         d.sort ();
107         d.unique ();
108
109         return d;
110 }
111
112 ContentTimePeriod
113 DCPSubtitleDecoder::content_time_period (dcp::SubtitleString s) const
114 {
115         return ContentTimePeriod (
116                 ContentTime::from_seconds (s.in().as_seconds ()),
117                 ContentTime::from_seconds (s.out().as_seconds ())
118                 );
119 }