Basics of DCP subtitle import.
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <dcp/subtitle_content.h>
21 #include "dcp_subtitle_decoder.h"
22 #include "dcp_subtitle_content.h"
23
24 using std::list;
25 using boost::shared_ptr;
26
27 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const DCPSubtitleContent> content)
28         : SubtitleDecoder (content)
29 {
30         dcp::SubtitleContent c (content->path (0), false);
31         _subtitles = c.subtitles ();
32         _next = _subtitles.begin ();
33 }
34
35 void
36 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
37 {
38         SubtitleDecoder::seek (time, accurate);
39
40         _next = _subtitles.begin ();
41         list<dcp::SubtitleString>::const_iterator i = _subtitles.begin ();
42         while (i != _subtitles.end() && ContentTime::from_seconds (_next->in().to_seconds()) < time) {
43                 ++i;
44         }
45 }
46
47 bool
48 DCPSubtitleDecoder::pass ()
49 {
50         if (_next == _subtitles.end ()) {
51                 return true;
52         }
53
54         list<dcp::SubtitleString> s;
55         s.push_back (*_next);
56         text_subtitle (s);
57         ++_next;
58         
59         return false;
60 }
61
62 list<ContentTimePeriod>
63 DCPSubtitleDecoder::subtitles_during (ContentTimePeriod p, bool starting) const
64 {
65         /* XXX: inefficient */
66
67         list<ContentTimePeriod> d;
68
69         for (list<dcp::SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
70                 ContentTimePeriod period (
71                         ContentTime::from_seconds (i->in().to_seconds ()),
72                         ContentTime::from_seconds (i->out().to_seconds ())
73                         );
74                 
75                 if ((starting && p.contains (period.from)) || (!starting && p.overlaps (period))) {
76                         d.push_back (period);
77                 }
78         }
79
80         return d;
81 }
82