2 Copyright (C) 2012 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 /** @file src/picture_asset.cc
21 * @brief An asset made up of JPEG2000 files
28 #include <boost/filesystem.hpp>
29 #include <boost/lexical_cast.hpp>
31 #include "KM_fileio.h"
32 #include "picture_asset.h"
34 #include "exceptions.h"
37 using namespace boost;
38 using namespace libdcp;
40 PictureAsset::PictureAsset (
41 sigc::slot<string, int> get_path,
44 sigc::signal1<void, float>* progress,
49 : Asset (directory, mxf_name, progress, fps, length)
56 PictureAsset::PictureAsset (
57 vector<string> const & files,
60 sigc::signal1<void, float>* progress,
65 : Asset (directory, mxf_name, progress, fps, length)
69 construct (sigc::bind (sigc::mem_fun (*this, &PictureAsset::path_from_list), files));
72 PictureAsset::PictureAsset (string directory, string mxf_name, int fps, int length, int width, int height)
73 : Asset (directory, mxf_name, 0, fps, length)
81 PictureAsset::path_from_list (int f, vector<string> const & files) const
87 PictureAsset::construct (sigc::slot<string, int> get_path)
89 ASDCP::JP2K::CodestreamParser j2k_parser;
90 ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
91 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
92 throw FileError ("could not open JPEG2000 file for reading", get_path (0));
95 ASDCP::JP2K::PictureDescriptor picture_desc;
96 j2k_parser.FillPictureDescriptor (picture_desc);
97 picture_desc.EditRate = ASDCP::Rational (_fps, 1);
99 ASDCP::WriterInfo writer_info;
100 fill_writer_info (&writer_info);
102 ASDCP::JP2K::MXFWriter mxf_writer;
103 if (ASDCP_FAILURE (mxf_writer.OpenWrite (mxf_path().string().c_str(), writer_info, picture_desc))) {
104 throw FileError ("could not open MXF file for writing", mxf_path().string());
107 for (int i = 0; i < _length; ++i) {
109 string const path = get_path (i);
111 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
112 throw FileError ("could not open JPEG2000 file for reading", path);
115 /* XXX: passing 0 to WriteFrame ok? */
116 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
117 throw MiscError ("error in writing video MXF");
120 (*_progress) (0.5 * float (i) / _length);
123 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
124 throw MiscError ("error in finalising video MXF");
129 PictureAsset::write_to_cpl (ostream& s) const
131 s << " <MainPicture>\n"
132 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
133 << " <AnnotationText>" << _mxf_name << "</AnnotationText>\n"
134 << " <EditRate>" << _fps << " 1</EditRate>\n"
135 << " <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
136 << " <EntryPoint>0</EntryPoint>\n"
137 << " <Duration>" << _length << "</Duration>\n"
138 << " <FrameRate>" << _fps << " 1</FrameRate>\n"
139 << " <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
140 << " </MainPicture>\n";
144 PictureAsset::equals (shared_ptr<const Asset> other, EqualityFlags flags) const
146 list<string> notes = Asset::equals (other, flags);
148 if (flags & MXF_INSPECT) {
149 ASDCP::JP2K::MXFReader reader_A;
150 if (ASDCP_FAILURE (reader_A.OpenRead (mxf_path().string().c_str()))) {
151 throw FileError ("could not open MXF file for reading", mxf_path().string());
154 ASDCP::JP2K::MXFReader reader_B;
155 if (ASDCP_FAILURE (reader_B.OpenRead (other->mxf_path().string().c_str()))) {
156 throw FileError ("could not open MXF file for reading", mxf_path().string());
159 ASDCP::JP2K::PictureDescriptor desc_A;
160 if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
161 throw DCPReadError ("could not read video MXF information");
163 ASDCP::JP2K::PictureDescriptor desc_B;
164 if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
165 throw DCPReadError ("could not read video MXF information");
169 desc_A.EditRate != desc_B.EditRate ||
170 desc_A.ContainerDuration != desc_B.ContainerDuration ||
171 desc_A.SampleRate != desc_B.SampleRate ||
172 desc_A.StoredWidth != desc_B.StoredWidth ||
173 desc_A.StoredHeight != desc_B.StoredHeight ||
174 desc_A.AspectRatio != desc_B.AspectRatio ||
175 desc_A.Rsize != desc_B.Rsize ||
176 desc_A.Xsize != desc_B.Xsize ||
177 desc_A.Ysize != desc_B.Ysize ||
178 desc_A.XOsize != desc_B.XOsize ||
179 desc_A.YOsize != desc_B.YOsize ||
180 desc_A.XTsize != desc_B.XTsize ||
181 desc_A.YTsize != desc_B.YTsize ||
182 desc_A.XTOsize != desc_B.XTOsize ||
183 desc_A.YTOsize != desc_B.YTOsize ||
184 desc_A.Csize != desc_B.Csize
185 // desc_A.CodingStyleDefault != desc_B.CodingStyleDefault ||
186 // desc_A.QuantizationDefault != desc_B.QuantizationDefault
189 notes.push_back ("video MXF picture descriptors differ");
192 // for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
193 // if (desc_A.ImageComponents[j] != desc_B.ImageComponents[j]) {
194 // notes.pack_start ("video MXF picture descriptors differ");
199 ASDCP::JP2K::FrameBuffer buffer_A (4 * Kumu::Megabyte);
200 ASDCP::JP2K::FrameBuffer buffer_B (4 * Kumu::Megabyte);
202 for (int i = 0; i < _length; ++i) {
203 if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
204 throw DCPReadError ("could not read video frame");
207 if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
208 throw DCPReadError ("could not read video frame");
211 if (buffer_A.Size() != buffer_B.Size()) {
212 notes.push_back ("sizes of video data for frame " + lexical_cast<string>(i) + " differ");
216 if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
217 notes.push_back ("J2K data for frame " + lexical_cast<string>(i) + " differ");