2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "mono_picture_asset.h"
21 #include "mono_picture_asset_writer.h"
23 #include "KM_fileio.h"
24 #include "exceptions.h"
25 #include "mono_picture_frame.h"
29 using boost::shared_ptr;
30 using boost::dynamic_pointer_cast;
31 using boost::lexical_cast;
32 using namespace libdcp;
34 MonoPictureAsset::MonoPictureAsset (boost::filesystem::path directory, boost::filesystem::path mxf_name)
35 : PictureAsset (directory, mxf_name)
41 MonoPictureAsset::create (vector<boost::filesystem::path> const & files)
43 create (boost::bind (&MonoPictureAsset::path_from_list, this, _1, files));
47 MonoPictureAsset::create (boost::function<boost::filesystem::path (int)> get_path)
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));
56 ASDCP::JP2K::PictureDescriptor picture_desc;
57 j2k_parser.FillPictureDescriptor (picture_desc);
58 picture_desc.EditRate = ASDCP::Rational (_edit_rate, 1);
60 ASDCP::WriterInfo writer_info;
61 fill_writer_info (&writer_info);
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));
69 for (int i = 0; i < _intrinsic_duration; ++i) {
71 boost::filesystem::path const path = get_path (i);
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));
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));
84 (*_progress) (0.5 * float (i) / _intrinsic_duration);
88 r = mxf_writer.Finalize();
89 if (ASDCP_FAILURE (r)) {
90 boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string(), r));
95 MonoPictureAsset::read ()
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));
103 ASDCP::JP2K::PictureDescriptor desc;
104 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
105 boost::throw_exception (DCPReadError ("could not read video MXF information"));
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;
115 boost::filesystem::path
116 MonoPictureAsset::path_from_list (int f, vector<boost::filesystem::path> const & files) const
121 shared_ptr<const MonoPictureFrame>
122 MonoPictureAsset::get_frame (int n) const
124 return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path(), n, _decryption_context));
128 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
130 if (!MXFAsset::equals (other, opt, note)) {
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));
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));
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"));
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"));
155 if (!descriptor_equals (desc_A, desc_B, note)) {
159 shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
160 assert (other_picture);
162 for (int i = 0; i < _intrinsic_duration; ++i) {
163 if (i >= other_picture->intrinsic_duration()) {
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);
171 if (!frame_buffer_equals (
173 frame_A->j2k_data(), frame_A->j2k_size(),
174 frame_B->j2k_data(), frame_B->j2k_size()
183 shared_ptr<PictureAssetWriter>
184 MonoPictureAsset::start_write (bool overwrite)
186 /* XXX: can't we use shared_ptr here? */
187 return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite));
191 MonoPictureAsset::cpl_node_name () const
193 return "MainPicture";
197 MonoPictureAsset::edit_rate_factor () const