Basics of subtitle split.
[dcpomatic.git] / src / lib / dcp_decoder.cc
1 /*
2     Copyright (C) 2014-2015 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_decoder.h"
21 #include "dcp_content.h"
22 #include "j2k_image_proxy.h"
23 #include "image.h"
24 #include "config.h"
25 #include <dcp/dcp.h>
26 #include <dcp/decrypted_kdm.h>
27 #include <dcp/cpl.h>
28 #include <dcp/reel.h>
29 #include <dcp/mono_picture_asset.h>
30 #include <dcp/stereo_picture_asset.h>
31 #include <dcp/reel_picture_asset.h>
32 #include <dcp/reel_sound_asset.h>
33 #include <dcp/reel_subtitle_asset.h>
34 #include <dcp/mono_picture_frame.h>
35 #include <dcp/stereo_picture_frame.h>
36 #include <dcp/sound_frame.h>
37 #include <boost/foreach.hpp>
38 #include <iostream>
39
40 using std::list;
41 using std::cout;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44
45 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c, shared_ptr<Log> log, bool fast)
46         : VideoDecoder (c->video, log)
47         , AudioDecoder (c, fast)
48         , SubtitleDecoder (c->subtitle)
49         , _dcp_content (c)
50 {
51         dcp::DCP dcp (c->directory ());
52         dcp.read (false, 0, true);
53         if (c->kdm ()) {
54                 dcp.add (dcp::DecryptedKDM (c->kdm().get (), Config::instance()->decryption_chain()->key().get ()));
55         }
56         DCPOMATIC_ASSERT (dcp.cpls().size() == 1);
57         _reels = dcp.cpls().front()->reels ();
58         _reel = _reels.begin ();
59 }
60
61 bool
62 DCPDecoder::pass (PassReason reason, bool)
63 {
64         if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
65                 return true;
66         }
67
68         /* Offset of the start of the current reel from the start of the content in frames */
69         int offset = 0;
70         list<shared_ptr<dcp::Reel> >::const_iterator i = _reels.begin();
71         while (i != _reel) {
72                 offset += (*i)->main_picture()->duration ();
73                 ++i;
74         }
75
76         double const vfr = _dcp_content->video->video_frame_rate ();
77
78         /* Frame within the (played part of the) reel that is coming up next */
79         int64_t const frame = _next.frames_round (vfr);
80
81         if ((*_reel)->main_picture () && reason != PASS_REASON_SUBTITLE) {
82                 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
83                 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
84                 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
85                 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
86                 if (mono) {
87                         video (shared_ptr<ImageProxy> (new J2KImageProxy (mono->get_frame (entry_point + frame), asset->size())), offset + frame);
88                 } else {
89                         video (
90                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_LEFT)),
91                                 offset + frame
92                                 );
93
94                         video (
95                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_RIGHT)),
96                                 offset + frame
97                                 );
98                 }
99         }
100
101         if ((*_reel)->main_sound () && reason != PASS_REASON_SUBTITLE) {
102                 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
103                 shared_ptr<const dcp::SoundFrame> sf = (*_reel)->main_sound()->asset()->get_frame (entry_point + frame);
104                 uint8_t const * from = sf->data ();
105
106                 int const channels = _dcp_content->audio_stream()->channels ();
107                 int const frames = sf->size() / (3 * channels);
108                 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
109                 for (int i = 0; i < frames; ++i) {
110                         for (int j = 0; j < channels; ++j) {
111                                 data->data()[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
112                                 from += 3;
113                         }
114                 }
115
116                 audio (_dcp_content->audio_stream(), data, ContentTime::from_frames (offset, vfr) + _next);
117         }
118
119         if ((*_reel)->main_subtitle ()) {
120                 int64_t const entry_point = (*_reel)->main_subtitle()->entry_point ();
121                 list<dcp::SubtitleString> subs = (*_reel)->main_subtitle()->asset()->subtitles_during (
122                         dcp::Time (entry_point + frame, vfr, vfr),
123                         dcp::Time (entry_point + frame + 1, vfr, vfr),
124                         true
125                         );
126
127                 if (!subs.empty ()) {
128                         /* XXX: assuming that all `subs' are at the same time; maybe this is ok */
129                         text_subtitle (
130                                 ContentTimePeriod (
131                                         ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().in().as_seconds ()),
132                                         ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().out().as_seconds ())
133                                         ),
134                                 subs
135                                 );
136                 }
137         }
138
139         _next += ContentTime::from_frames (1, vfr);
140
141         if ((*_reel)->main_picture ()) {
142                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
143                         ++_reel;
144                         _next = ContentTime ();
145                 }
146         }
147
148         return false;
149 }
150
151 void
152 DCPDecoder::seek (ContentTime t, bool accurate)
153 {
154         VideoDecoder::seek (t, accurate);
155         AudioDecoder::seek (t, accurate);
156         SubtitleDecoder::seek (t, accurate);
157
158         _reel = _reels.begin ();
159         while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video->video_frame_rate ())) {
160                 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video->video_frame_rate ());
161                 ++_reel;
162         }
163
164         _next = t;
165 }
166
167
168 list<ContentTimePeriod>
169 DCPDecoder::image_subtitles_during (ContentTimePeriod, bool) const
170 {
171         return list<ContentTimePeriod> ();
172 }
173
174 list<ContentTimePeriod>
175 DCPDecoder::text_subtitles_during (ContentTimePeriod period, bool starting) const
176 {
177         /* XXX: inefficient */
178
179         list<ContentTimePeriod> ctp;
180         double const vfr = _dcp_content->video->video_frame_rate ();
181
182         BOOST_FOREACH (shared_ptr<dcp::Reel> r, _reels) {
183                 if (!r->main_subtitle ()) {
184                         continue;
185                 }
186
187                 int64_t const entry_point = r->main_subtitle()->entry_point ();
188
189                 list<dcp::SubtitleString> subs = r->main_subtitle()->asset()->subtitles_during (
190                         dcp::Time (period.from.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
191                         dcp::Time (period.to.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
192                         starting
193                         );
194
195                 BOOST_FOREACH (dcp::SubtitleString const & s, subs) {
196                         ctp.push_back (
197                                 ContentTimePeriod (
198                                         ContentTime::from_seconds (s.in().as_seconds ()),
199                                         ContentTime::from_seconds (s.out().as_seconds ())
200                                         )
201                                 );
202                 }
203         }
204
205         return ctp;
206 }