Merge master.
[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/dcp.h>
21 #include <dcp/cpl.h>
22 #include <dcp/reel.h>
23 #include <dcp/mono_picture_mxf.h>
24 #include <dcp/stereo_picture_mxf.h>
25 #include <dcp/reel_picture_asset.h>
26 #include <dcp/reel_sound_asset.h>
27 #include <dcp/mono_picture_frame.h>
28 #include <dcp/stereo_picture_frame.h>
29 #include <dcp/sound_frame.h>
30 #include "dcp_decoder.h"
31 #include "dcp_content.h"
32 #include "j2k_image_proxy.h"
33 #include "image.h"
34
35 using std::list;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39
40 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c, shared_ptr<Log> log)
41         : VideoDecoder (c)
42         , AudioDecoder (c)
43         , SubtitleDecoder (c)
44         , _log (log)
45         , _dcp_content (c)
46 {
47         dcp::DCP dcp (c->directory ());
48         dcp.read ();
49         assert (dcp.cpls().size() == 1);
50         _reels = dcp.cpls().front()->reels ();
51         _reel = _reels.begin ();
52 }
53
54 bool
55 DCPDecoder::pass ()
56 {
57         if (_reel == _reels.end ()) {
58                 return true;
59         }
60
61         float const vfr = _dcp_content->video_frame_rate ();
62         int64_t const frame = _next.frames (vfr);
63         
64         if ((*_reel)->main_picture ()) {
65                 shared_ptr<dcp::PictureMXF> mxf = (*_reel)->main_picture()->mxf ();
66                 shared_ptr<dcp::MonoPictureMXF> mono = dynamic_pointer_cast<dcp::MonoPictureMXF> (mxf);
67                 shared_ptr<dcp::StereoPictureMXF> stereo = dynamic_pointer_cast<dcp::StereoPictureMXF> (mxf);
68                 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
69                 if (mono) {
70                         video (shared_ptr<ImageProxy> (new J2KImageProxy (mono->get_frame (entry_point + frame), mxf->size(), _log)), frame);
71                 } else {
72                         video (
73                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), mxf->size(), dcp::EYE_LEFT, _log)),
74                                 frame
75                                 );
76                         
77                         video (
78                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), mxf->size(), dcp::EYE_RIGHT, _log)),
79                                 frame
80                                 );
81                 }
82         }
83
84         if ((*_reel)->main_sound ()) {
85                 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
86                 shared_ptr<const dcp::SoundFrame> sf = (*_reel)->main_sound()->mxf()->get_frame (entry_point + frame);
87                 uint8_t const * from = sf->data ();
88
89                 int const channels = _dcp_content->audio_channels ();
90                 int const frames = sf->size() / (3 * channels);
91                 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
92                 for (int i = 0; i < frames; ++i) {
93                         for (int j = 0; j < channels; ++j) {
94                                 data->data()[j][i] = float (from[0] | (from[1] << 8) | (from[2] << 16)) / (1 << 23);
95                                 from += 3;
96                         }
97                 }
98
99                 audio (data, _next);
100         }
101
102         /* XXX: subtitle */
103
104         _next += ContentTime::from_frames (1, vfr);
105
106         if ((*_reel)->main_picture ()) {
107                 if (_next.frames (vfr) >= (*_reel)->main_picture()->duration()) {
108                         ++_reel;
109                 }
110         }
111         
112         return false;
113 }
114
115 void
116 DCPDecoder::seek (ContentTime t, bool accurate)
117 {
118         VideoDecoder::seek (t, accurate);
119         AudioDecoder::seek (t, accurate);
120         SubtitleDecoder::seek (t, accurate);
121
122         _reel = _reels.begin ();
123         while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video_frame_rate ())) {
124                 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video_frame_rate ());
125                 ++_reel;
126         }
127
128         _next = t;
129 }
130
131
132 list<ContentTimePeriod>
133 DCPDecoder::subtitles_during (ContentTimePeriod, bool starting) const
134 {
135         return list<ContentTimePeriod> ();
136 }