2 Copyright (C) 2012-2013 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, string 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 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)));
55 ASDCP::JP2K::PictureDescriptor picture_desc;
56 j2k_parser.FillPictureDescriptor (picture_desc);
57 picture_desc.EditRate = ASDCP::Rational (_edit_rate, 1);
59 ASDCP::WriterInfo writer_info;
60 fill_writer_info (&writer_info, _uuid, _interop, _metadata);
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()));
67 for (int i = 0; i < _intrinsic_duration; ++i) {
69 boost::filesystem::path const path = get_path (i);
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));
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()));
80 (*_progress) (0.5 * float (i) / _intrinsic_duration);
84 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
85 boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string()));
90 MonoPictureAsset::read ()
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()));
97 ASDCP::JP2K::PictureDescriptor desc;
98 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
99 boost::throw_exception (DCPReadError ("could not read video MXF information"));
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;
109 boost::filesystem::path
110 MonoPictureAsset::path_from_list (int f, vector<boost::filesystem::path> const & files) const
115 shared_ptr<const MonoPictureFrame>
116 MonoPictureAsset::get_frame (int n) const
118 return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path(), n, _decryption_context));
122 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
124 if (!PictureAsset::equals (other, opt, note)) {
128 shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
129 assert (other_picture);
131 for (int i = 0; i < _intrinsic_duration; ++i) {
132 if (i >= other_picture->intrinsic_duration()) {
136 note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
137 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
138 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
140 if (!frame_buffer_equals (
142 frame_A->j2k_data(), frame_A->j2k_size(),
143 frame_B->j2k_data(), frame_B->j2k_size()
152 shared_ptr<PictureAssetWriter>
153 MonoPictureAsset::start_write (bool overwrite)
155 /* XXX: can't we use shared_ptr here? */
156 return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite));
160 MonoPictureAsset::cpl_node_name () const
162 return "MainPicture";
166 MonoPictureAsset::edit_rate_factor () const