Some more boost::filesystem::path.
[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         if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).string().c_str(), frame_buffer))) {
52                 boost::throw_exception (FileError ("could not open JPEG2000 file for reading", get_path (0)));
53         }
54         
55         ASDCP::JP2K::PictureDescriptor picture_desc;
56         j2k_parser.FillPictureDescriptor (picture_desc);
57         picture_desc.EditRate = ASDCP::Rational (_edit_rate, 1);
58         
59         ASDCP::WriterInfo writer_info;
60         fill_writer_info (&writer_info, _uuid, _interop, _metadata);
61         
62         ASDCP::JP2K::MXFWriter mxf_writer;
63         if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc, 16384, false))) {
64                 boost::throw_exception (MXFFileError ("could not open MXF file for writing", path().string()));
65         }
66
67         for (int i = 0; i < _intrinsic_duration; ++i) {
68
69                 boost::filesystem::path const path = get_path (i);
70
71                 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.string().c_str(), frame_buffer))) {
72                         boost::throw_exception (FileError ("could not open JPEG2000 file for reading", path));
73                 }
74
75                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0))) {
76                         boost::throw_exception (MXFFileError ("error in writing video MXF", this->path().string()));
77                 }
78
79                 if (_progress) {
80                         (*_progress) (0.5 * float (i) / _intrinsic_duration);
81                 }
82         }
83         
84         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
85                 boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string()));
86         }
87 }
88
89 void
90 MonoPictureAsset::read ()
91 {
92         ASDCP::JP2K::MXFReader reader;
93         if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
94                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
95         }
96         
97         ASDCP::JP2K::PictureDescriptor desc;
98         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
99                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
100         }
101
102         _size.width = desc.StoredWidth;
103         _size.height = desc.StoredHeight;
104         _edit_rate = desc.EditRate.Numerator;
105         assert (desc.EditRate.Denominator == 1);
106         _intrinsic_duration = desc.ContainerDuration;
107 }
108
109 boost::filesystem::path
110 MonoPictureAsset::path_from_list (int f, vector<boost::filesystem::path> const & files) const
111 {
112         return files[f];
113 }
114
115 shared_ptr<const MonoPictureFrame>
116 MonoPictureAsset::get_frame (int n) const
117 {
118         return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path(), n, _decryption_context));
119 }
120
121 bool
122 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
123 {
124         if (!MXFAsset::equals (other, opt, note)) {
125                 return false;
126         }
127
128         ASDCP::JP2K::MXFReader reader_A;
129         if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
130                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
131         }
132         
133         ASDCP::JP2K::MXFReader reader_B;
134         if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
135                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
136         }
137         
138         ASDCP::JP2K::PictureDescriptor desc_A;
139         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
140                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
141         }
142         ASDCP::JP2K::PictureDescriptor desc_B;
143         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
144                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
145         }
146         
147         if (!descriptor_equals (desc_A, desc_B, note)) {
148                 return false;
149         }
150
151         shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
152         assert (other_picture);
153
154         for (int i = 0; i < _intrinsic_duration; ++i) {
155                 if (i >= other_picture->intrinsic_duration()) {
156                         return false;
157                 }
158                 
159                 note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
160                 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
161                 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
162                 
163                 if (!frame_buffer_equals (
164                             i, opt, note,
165                             frame_A->j2k_data(), frame_A->j2k_size(),
166                             frame_B->j2k_data(), frame_B->j2k_size()
167                             )) {
168                         return false;
169                 }
170         }
171
172         return true;
173 }
174
175 shared_ptr<PictureAssetWriter>
176 MonoPictureAsset::start_write (bool overwrite)
177 {
178         /* XXX: can't we use shared_ptr here? */
179         return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite));
180 }
181
182 string
183 MonoPictureAsset::cpl_node_name () const
184 {
185         return "MainPicture";
186 }
187
188 int
189 MonoPictureAsset::edit_rate_factor () const
190 {
191         return 1;
192 }