Don't bother decoding video frames when we're seeking around trying to find subtitles.
[dcpomatic.git] / src / lib / dcp_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_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/cpl.h>
27 #include <dcp/reel.h>
28 #include <dcp/mono_picture_mxf.h>
29 #include <dcp/stereo_picture_mxf.h>
30 #include <dcp/reel_picture_asset.h>
31 #include <dcp/reel_sound_asset.h>
32 #include <dcp/mono_picture_frame.h>
33 #include <dcp/stereo_picture_frame.h>
34 #include <dcp/sound_frame.h>
35
36 using std::list;
37 using std::cout;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40
41 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c)
42         : VideoDecoder (c)
43         , AudioDecoder (c)
44         , SubtitleDecoder (c)
45         , _dcp_content (c)
46 {
47         dcp::DCP dcp (c->directory ());
48         dcp.read ();
49         if (c->kdm ()) {
50                 dcp.add (dcp::DecryptedKDM (c->kdm().get (), Config::instance()->decryption_private_key ()));
51         }
52         DCPOMATIC_ASSERT (dcp.cpls().size() == 1);
53         _reels = dcp.cpls().front()->reels ();
54         _reel = _reels.begin ();
55 }
56
57 bool
58 DCPDecoder::pass (PassReason)
59 {
60         if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
61                 return true;
62         }
63
64         float const vfr = _dcp_content->video_frame_rate ();
65         int64_t const frame = _next.frames (vfr);
66         
67         if ((*_reel)->main_picture ()) {
68                 shared_ptr<dcp::PictureMXF> mxf = (*_reel)->main_picture()->mxf ();
69                 shared_ptr<dcp::MonoPictureMXF> mono = dynamic_pointer_cast<dcp::MonoPictureMXF> (mxf);
70                 shared_ptr<dcp::StereoPictureMXF> stereo = dynamic_pointer_cast<dcp::StereoPictureMXF> (mxf);
71                 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
72                 if (mono) {
73                         video (shared_ptr<ImageProxy> (new J2KImageProxy (mono->get_frame (entry_point + frame), mxf->size())), frame);
74                 } else {
75                         video (
76                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), mxf->size(), dcp::EYE_LEFT)),
77                                 frame
78                                 );
79                         
80                         video (
81                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), mxf->size(), dcp::EYE_RIGHT)),
82                                 frame
83                                 );
84                 }
85         }
86
87         if ((*_reel)->main_sound ()) {
88                 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
89                 shared_ptr<const dcp::SoundFrame> sf = (*_reel)->main_sound()->mxf()->get_frame (entry_point + frame);
90                 uint8_t const * from = sf->data ();
91
92                 int const channels = _dcp_content->audio_channels ();
93                 int const frames = sf->size() / (3 * channels);
94                 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
95                 for (int i = 0; i < frames; ++i) {
96                         for (int j = 0; j < channels; ++j) {
97                                 data->data()[j][i] = float (from[0] | (from[1] << 8) | (from[2] << 16)) / (1 << 23);
98                                 from += 3;
99                         }
100                 }
101
102                 audio (data, _next);
103         }
104
105         /* XXX: subtitle */
106
107         _next += ContentTime::from_frames (1, vfr);
108
109         if ((*_reel)->main_picture ()) {
110                 if (_next.frames (vfr) >= (*_reel)->main_picture()->duration()) {
111                         ++_reel;
112                 }
113         }
114         
115         return false;
116 }
117
118 void
119 DCPDecoder::seek (ContentTime t, bool accurate)
120 {
121         VideoDecoder::seek (t, accurate);
122         AudioDecoder::seek (t, accurate);
123         SubtitleDecoder::seek (t, accurate);
124
125         _reel = _reels.begin ();
126         while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video_frame_rate ())) {
127                 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video_frame_rate ());
128                 ++_reel;
129         }
130
131         _next = t;
132 }
133
134
135 list<ContentTimePeriod>
136 DCPDecoder::image_subtitles_during (ContentTimePeriod, bool) const
137 {
138         return list<ContentTimePeriod> ();
139 }
140
141 list<ContentTimePeriod>
142 DCPDecoder::text_subtitles_during (ContentTimePeriod, bool) const
143 {
144         /* XXX */
145         return list<ContentTimePeriod> ();
146 }