0ac48d37b8077eb7b45b65c70743f7721131f639
[libdcp.git] / src / mono_picture_asset.cc
1 /*
2     Copyright (C) 2012-2013 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 "mono_picture_asset.h"
21 #include "mono_picture_asset_writer.h"
22 #include "AS_DCP.h"
23 #include "KM_fileio.h"
24 #include "exceptions.h"
25 #include "mono_picture_frame.h"
26
27 using std::string;
28 using std::vector;
29 using boost::shared_ptr;
30 using boost::dynamic_pointer_cast;
31 using boost::lexical_cast;
32 using namespace libdcp;
33
34 MonoPictureAsset::MonoPictureAsset (boost::filesystem::path directory, boost::filesystem::path mxf_name)
35         : PictureAsset (directory, mxf_name)
36 {
37
38 }
39
40 void
41 MonoPictureAsset::create (vector<boost::filesystem::path> const & files)
42 {
43         create (boost::bind (&MonoPictureAsset::path_from_list, this, _1, files));
44 }
45
46 void
47 MonoPictureAsset::create (boost::function<boost::filesystem::path (int)> get_path)
48 {
49         ASDCP::JP2K::CodestreamParser j2k_parser;
50         ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
51         Kumu::Result_t r = j2k_parser.OpenReadFrame (get_path(0).string().c_str(), frame_buffer);
52         if (ASDCP_FAILURE (r)) {
53                 boost::throw_exception (FileError ("could not open JPEG2000 file for reading", get_path(0), r));
54         }
55         
56         ASDCP::JP2K::PictureDescriptor picture_desc;
57         j2k_parser.FillPictureDescriptor (picture_desc);
58         picture_desc.EditRate = ASDCP::Rational (_edit_rate, 1);
59         
60         ASDCP::WriterInfo writer_info;
61         fill_writer_info (&writer_info);
62         
63         ASDCP::JP2K::MXFWriter mxf_writer;
64         r = mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc, 16384, false);
65         if (ASDCP_FAILURE (r)) {
66                 boost::throw_exception (MXFFileError ("could not open MXF file for writing", path().string(), r));
67         }
68
69         for (int i = 0; i < _intrinsic_duration; ++i) {
70
71                 boost::filesystem::path const path = get_path (i);
72
73                 Kumu::Result_t r = j2k_parser.OpenReadFrame (path.string().c_str(), frame_buffer);
74                 if (ASDCP_FAILURE (r)) {
75                         boost::throw_exception (FileError ("could not open JPEG2000 file for reading", path, r));
76                 }
77
78                 r = mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0);
79                 if (ASDCP_FAILURE (r)) {
80                         boost::throw_exception (MXFFileError ("error in writing video MXF", this->path().string(), r));
81                 }
82
83                 if (_progress) {
84                         (*_progress) (0.5 * float (i) / _intrinsic_duration);
85                 }
86         }
87         
88         r = mxf_writer.Finalize();
89         if (ASDCP_FAILURE (r)) {
90                 boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string(), r));
91         }
92 }
93
94 void
95 MonoPictureAsset::read ()
96 {
97         ASDCP::JP2K::MXFReader reader;
98         Kumu::Result_t r = reader.OpenRead (path().string().c_str());
99         if (ASDCP_FAILURE (r)) {
100                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
101         }
102         
103         ASDCP::JP2K::PictureDescriptor desc;
104         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
105                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
106         }
107
108         _size.width = desc.StoredWidth;
109         _size.height = desc.StoredHeight;
110         _edit_rate = desc.EditRate.Numerator;
111         assert (desc.EditRate.Denominator == 1);
112         _intrinsic_duration = desc.ContainerDuration;
113 }
114
115 boost::filesystem::path
116 MonoPictureAsset::path_from_list (int f, vector<boost::filesystem::path> const & files) const
117 {
118         return files[f];
119 }
120
121 shared_ptr<const MonoPictureFrame>
122 MonoPictureAsset::get_frame (int n) const
123 {
124         return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path(), n, _decryption_context));
125 }
126
127 bool
128 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
129 {
130         if (!MXFAsset::equals (other, opt, note)) {
131                 return false;
132         }
133
134         ASDCP::JP2K::MXFReader reader_A;
135         Kumu::Result_t r = reader_A.OpenRead (path().string().c_str());
136         if (ASDCP_FAILURE (r)) {
137                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
138         }
139         
140         ASDCP::JP2K::MXFReader reader_B;
141         r = reader_B.OpenRead (other->path().string().c_str());
142         if (ASDCP_FAILURE (r)) {
143                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
144         }
145         
146         ASDCP::JP2K::PictureDescriptor desc_A;
147         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
148                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
149         }
150         ASDCP::JP2K::PictureDescriptor desc_B;
151         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
152                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
153         }
154         
155         if (!descriptor_equals (desc_A, desc_B, note)) {
156                 return false;
157         }
158
159         shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
160         assert (other_picture);
161
162         for (int i = 0; i < _intrinsic_duration; ++i) {
163                 if (i >= other_picture->intrinsic_duration()) {
164                         return false;
165                 }
166                 
167                 note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
168                 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
169                 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
170                 
171                 if (!frame_buffer_equals (
172                             i, opt, note,
173                             frame_A->j2k_data(), frame_A->j2k_size(),
174                             frame_B->j2k_data(), frame_B->j2k_size()
175                             )) {
176                         return false;
177                 }
178         }
179
180         return true;
181 }
182
183 shared_ptr<PictureAssetWriter>
184 MonoPictureAsset::start_write (bool overwrite)
185 {
186         /* XXX: can't we use shared_ptr here? */
187         return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite));
188 }
189
190 string
191 MonoPictureAsset::cpl_node_name () const
192 {
193         return "MainPicture";
194 }
195
196 int
197 MonoPictureAsset::edit_rate_factor () const
198 {
199         return 1;
200 }