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
29 #include <boost/filesystem.hpp>
30 #include <boost/lexical_cast.hpp>
33 #include "KM_fileio.h"
34 #include "picture_asset.h"
36 #include "exceptions.h"
37 #include "picture_frame.h"
40 using namespace boost;
41 using namespace libdcp;
43 PictureAsset::PictureAsset (string directory, string mxf_name, sigc::signal1<void, float>* progress, int fps, int entry_point, int length)
44 : MXFAsset (directory, mxf_name, progress, fps, entry_point, length)
52 PictureAsset::write_to_cpl (ostream& s) const
54 s << " <MainPicture>\n"
55 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
56 << " <AnnotationText>" << _file_name << "</AnnotationText>\n"
57 << " <EditRate>" << _fps << " 1</EditRate>\n"
58 << " <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
59 << " <EntryPoint>0</EntryPoint>\n"
60 << " <Duration>" << _length << "</Duration>\n"
61 << " <FrameRate>" << _fps << " 1</FrameRate>\n"
62 << " <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
63 << " </MainPicture>\n";
67 PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<string>& notes) const
69 if (!MXFAsset::equals (other, opt, notes)) {
74 ASDCP::JP2K::MXFReader reader_A;
75 if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
76 throw MXFFileError ("could not open MXF file for reading", path().string());
79 ASDCP::JP2K::MXFReader reader_B;
80 if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
81 throw MXFFileError ("could not open MXF file for reading", path().string());
84 ASDCP::JP2K::PictureDescriptor desc_A;
85 if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
86 throw DCPReadError ("could not read video MXF information");
88 ASDCP::JP2K::PictureDescriptor desc_B;
89 if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
90 throw DCPReadError ("could not read video MXF information");
94 desc_A.EditRate != desc_B.EditRate ||
95 desc_A.ContainerDuration != desc_B.ContainerDuration ||
96 desc_A.SampleRate != desc_B.SampleRate ||
97 desc_A.StoredWidth != desc_B.StoredWidth ||
98 desc_A.StoredHeight != desc_B.StoredHeight ||
99 desc_A.AspectRatio != desc_B.AspectRatio ||
100 desc_A.Rsize != desc_B.Rsize ||
101 desc_A.Xsize != desc_B.Xsize ||
102 desc_A.Ysize != desc_B.Ysize ||
103 desc_A.XOsize != desc_B.XOsize ||
104 desc_A.YOsize != desc_B.YOsize ||
105 desc_A.XTsize != desc_B.XTsize ||
106 desc_A.YTsize != desc_B.YTsize ||
107 desc_A.XTOsize != desc_B.XTOsize ||
108 desc_A.YTOsize != desc_B.YTOsize ||
109 desc_A.Csize != desc_B.Csize
110 // desc_A.CodingStyleDefault != desc_B.CodingStyleDefault ||
111 // desc_A.QuantizationDefault != desc_B.QuantizationDefault
114 notes.push_back ("video MXF picture descriptors differ");
117 // for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
118 // if (desc_A.ImageComponents[j] != desc_B.ImageComponents[j]) {
119 // notes.pack_start ("video MXF picture descriptors differ");
128 MonoPictureAsset::MonoPictureAsset (
129 sigc::slot<string, int> get_path,
132 sigc::signal1<void, float>* progress,
137 : PictureAsset (directory, mxf_name, progress, fps, 0, length)
141 construct (get_path);
144 MonoPictureAsset::MonoPictureAsset (
145 vector<string> const & files,
148 sigc::signal1<void, float>* progress,
153 : PictureAsset (directory, mxf_name, progress, fps, 0, length)
157 construct (sigc::bind (sigc::mem_fun (*this, &MonoPictureAsset::path_from_list), files));
160 MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name, int fps, int entry_point, int length)
161 : PictureAsset (directory, mxf_name, 0, fps, entry_point, length)
163 ASDCP::JP2K::MXFReader reader;
164 if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
165 throw MXFFileError ("could not open MXF file for reading", path().string());
168 ASDCP::JP2K::PictureDescriptor desc;
169 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
170 throw DCPReadError ("could not read video MXF information");
173 _width = desc.StoredWidth;
174 _height = desc.StoredHeight;
178 MonoPictureAsset::construct (sigc::slot<string, int> get_path)
180 ASDCP::JP2K::CodestreamParser j2k_parser;
181 ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
182 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
183 throw FileError ("could not open JPEG2000 file for reading", get_path (0));
186 ASDCP::JP2K::PictureDescriptor picture_desc;
187 j2k_parser.FillPictureDescriptor (picture_desc);
188 picture_desc.EditRate = ASDCP::Rational (_fps, 1);
190 ASDCP::WriterInfo writer_info;
191 fill_writer_info (&writer_info);
193 ASDCP::JP2K::MXFWriter mxf_writer;
194 if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc))) {
195 throw MXFFileError ("could not open MXF file for writing", path().string());
198 for (int i = 0; i < _length; ++i) {
200 string const path = get_path (i);
202 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
203 throw FileError ("could not open JPEG2000 file for reading", path);
206 /* XXX: passing 0 to WriteFrame ok? */
207 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
208 throw MiscError ("error in writing video MXF");
212 (*_progress) (0.5 * float (i) / _length);
216 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
217 throw MiscError ("error in finalising video MXF");
222 MonoPictureAsset::path_from_list (int f, vector<string> const & files) const
227 shared_ptr<const MonoPictureFrame>
228 MonoPictureAsset::get_frame (int n) const
230 return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n + _entry_point));
235 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<string>& notes) const
237 if (!PictureAsset::equals (other, opt, notes)) {
241 shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
242 assert (other_picture);
245 for (int i = 0; i < _length; ++i) {
246 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
247 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
249 if (!frame_buffer_equals (
251 frame_A->j2k_frame()->RoData(), frame_A->j2k_frame()->Size(),
252 frame_B->j2k_frame()->RoData(), frame_B->j2k_frame()->Size()
263 StereoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<string>& notes) const
265 if (!PictureAsset::equals (other, opt, notes)) {
269 shared_ptr<const StereoPictureAsset> other_picture = dynamic_pointer_cast<const StereoPictureAsset> (other);
270 assert (other_picture);
274 for (int i = 0; i < _length; ++i) {
275 shared_ptr<const StereoPictureFrame> frame_A = get_frame (i);
276 shared_ptr<const StereoPictureFrame> frame_B = other_picture->get_frame (i);
278 if (!frame_buffer_equals (
280 frame_A->j2k_frame()->Left.RoData(), frame_A->j2k_frame()->Left.Size(),
281 frame_B->j2k_frame()->Left.RoData(), frame_B->j2k_frame()->Left.Size()
286 if (!frame_buffer_equals (
288 frame_A->j2k_frame()->Right.RoData(), frame_A->j2k_frame()->Right.Size(),
289 frame_B->j2k_frame()->Right.RoData(), frame_B->j2k_frame()->Right.Size()
300 PictureAsset::frame_buffer_equals (
301 int frame, EqualityOptions opt, list<string>& notes, uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
304 if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
305 /* Easy result; the J2K data is identical */
309 /* Decompress the images to bitmaps */
310 opj_image_t* image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
311 opj_image_t* image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
315 if (image_A->numcomps != image_B->numcomps) {
316 notes.push_back ("image component counts for frame " + lexical_cast<string>(frame) + " differ");
320 vector<int> abs_diffs (image_A->comps[0].w * image_A->comps[0].h * image_A->numcomps);
324 for (int c = 0; c < image_A->numcomps; ++c) {
326 if (image_A->comps[c].w != image_B->comps[c].w || image_A->comps[c].h != image_B->comps[c].h) {
327 notes.push_back ("image sizes for frame " + lexical_cast<string>(frame) + " differ");
331 int const pixels = image_A->comps[c].w * image_A->comps[c].h;
332 for (int j = 0; j < pixels; ++j) {
333 int const t = abs (image_A->comps[c].data[j] - image_B->comps[c].data[j]);
335 max_diff = max (max_diff, t);
340 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
344 double const mean = double (total) / abs_diffs.size ();
346 uint64_t total_squared_deviation = 0;
347 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
348 total_squared_deviation += pow (*j - mean, 2);
351 double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
353 if (mean > opt.max_mean_pixel_error || std_dev > opt.max_std_dev_pixel_error) {
354 notes.push_back ("mean or standard deviation out of range for " + lexical_cast<string>(frame));
358 opj_image_destroy (image_A);
359 opj_image_destroy (image_B);
365 StereoPictureAsset::StereoPictureAsset (string directory, string mxf_name, int fps, int entry_point, int length)
366 : PictureAsset (directory, mxf_name, 0, fps, entry_point, length)
368 ASDCP::JP2K::MXFSReader reader;
369 if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
370 throw MXFFileError ("could not open MXF file for reading", path().string());
373 ASDCP::JP2K::PictureDescriptor desc;
374 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
375 throw DCPReadError ("could not read video MXF information");
378 _width = desc.StoredWidth;
379 _height = desc.StoredHeight;
382 shared_ptr<const StereoPictureFrame>
383 StereoPictureAsset::get_frame (int n) const
385 return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n + _entry_point));