Fix DCP name in editor; fix use of DCP entry points.
[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/mono_picture_frame.h>
27 #include <dcp/stereo_picture_frame.h>
28 #include "dcp_decoder.h"
29 #include "dcp_content.h"
30 #include "image_proxy.h"
31 #include "image.h"
32
33 using std::list;
34 using std::cout;
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37
38 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c, shared_ptr<Log> log)
39         : VideoDecoder (c)
40         , AudioDecoder (c)
41         , SubtitleDecoder (c)
42         , _log (log)
43         , _dcp_content (c)
44 {
45         dcp::DCP dcp (c->directory ());
46         dcp.read ();
47         assert (dcp.cpls().size() == 1);
48         _reels = dcp.cpls().front()->reels ();
49         _reel = _reels.begin ();
50 }
51
52 bool
53 DCPDecoder::pass ()
54 {
55         if (_reel == _reels.end ()) {
56                 return true;
57         }
58
59         float const vfr = _dcp_content->video_frame_rate ();
60         
61         if ((*_reel)->main_picture ()) {
62                 shared_ptr<dcp::PictureMXF> mxf = (*_reel)->main_picture()->mxf ();
63                 shared_ptr<dcp::MonoPictureMXF> mono = dynamic_pointer_cast<dcp::MonoPictureMXF> (mxf);
64                 shared_ptr<dcp::StereoPictureMXF> stereo = dynamic_pointer_cast<dcp::StereoPictureMXF> (mxf);
65                 int64_t const ep = (*_reel)->main_picture()->entry_point ();
66                 if (mono) {
67                         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, mxf->size(), false));
68                         mono->get_frame (ep + _next.frames (vfr))->rgb_frame (image->data()[0]);
69                         shared_ptr<Image> aligned (new Image (image, true));
70                         video (shared_ptr<ImageProxy> (new RawImageProxy (aligned, _log)), _next.frames (vfr));
71                 } else {
72
73                         shared_ptr<Image> left (new Image (PIX_FMT_RGB24, mxf->size(), false));
74                         stereo->get_frame (ep + _next.frames (vfr))->rgb_frame (dcp::EYE_LEFT, left->data()[0]);
75                         shared_ptr<Image> aligned_left (new Image (left, true));
76                         video (shared_ptr<ImageProxy> (new RawImageProxy (aligned_left, _log)), _next.frames (vfr));
77
78                         shared_ptr<Image> right (new Image (PIX_FMT_RGB24, mxf->size(), false));
79                         stereo->get_frame (ep + _next.frames (vfr))->rgb_frame (dcp::EYE_RIGHT, right->data()[0]);
80                         shared_ptr<Image> aligned_right (new Image (right, true));
81                         video (shared_ptr<ImageProxy> (new RawImageProxy (aligned_right, _log)), _next.frames (vfr));
82                 }
83         }
84
85         /* XXX: sound */
86         /* XXX: subtitle */
87
88         _next += ContentTime::from_frames (1, vfr);
89
90         if ((*_reel)->main_picture ()) {
91                 if ((*_reel)->main_picture()->duration() >= _next.frames (vfr)) {
92                         ++_reel;
93                 }
94         }
95         
96         return false;
97 }
98
99 void
100 DCPDecoder::seek (ContentTime t, bool accurate)
101 {
102         VideoDecoder::seek (t, accurate);
103         AudioDecoder::seek (t, accurate);
104         SubtitleDecoder::seek (t, accurate);
105
106         _reel = _reels.begin ();
107         while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video_frame_rate ())) {
108                 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video_frame_rate ());
109                 ++_reel;
110         }
111
112         _next = t;
113 }
114
115
116 list<ContentTimePeriod>
117 DCPDecoder::subtitles_during (ContentTimePeriod, bool starting) const
118 {
119         return list<ContentTimePeriod> ();
120 }