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"
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
50 using boost::lexical_cast;
51 using namespace libdcp;
53 PictureAsset::PictureAsset (string directory, string mxf_name, boost::signals2::signal<void (float)>* progress, int fps, int intrinsic_duration, Size size)
54 : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration)
60 PictureAsset::PictureAsset (string directory, string mxf_name)
61 : MXFAsset (directory, mxf_name)
67 PictureAsset::write_to_cpl (ostream& s) const
69 s << " <MainPicture>\n"
70 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
71 << " <AnnotationText>" << _file_name << "</AnnotationText>\n"
72 << " <EditRate>" << _edit_rate << " 1</EditRate>\n"
73 << " <IntrinsicDuration>" << _intrinsic_duration << "</IntrinsicDuration>\n"
74 << " <EntryPoint>" << _entry_point << "</EntryPoint>\n"
75 << " <Duration>" << _duration << "</Duration>\n"
76 << " <FrameRate>" << _edit_rate << " 1</FrameRate>\n"
77 << " <ScreenAspectRatio>" << _size.width << " " << _size.height << "</ScreenAspectRatio>\n"
78 << " </MainPicture>\n";
82 PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
84 if (!MXFAsset::equals (other, opt, note)) {
88 ASDCP::JP2K::MXFReader reader_A;
89 if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
90 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
93 ASDCP::JP2K::MXFReader reader_B;
94 if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
95 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
98 ASDCP::JP2K::PictureDescriptor desc_A;
99 if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
100 boost::throw_exception (DCPReadError ("could not read video MXF information"));
102 ASDCP::JP2K::PictureDescriptor desc_B;
103 if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
104 boost::throw_exception (DCPReadError ("could not read video MXF information"));
108 desc_A.EditRate != desc_B.EditRate ||
109 desc_A.ContainerDuration != desc_B.ContainerDuration ||
110 desc_A.SampleRate != desc_B.SampleRate ||
111 desc_A.StoredWidth != desc_B.StoredWidth ||
112 desc_A.StoredHeight != desc_B.StoredHeight ||
113 desc_A.AspectRatio != desc_B.AspectRatio ||
114 desc_A.Rsize != desc_B.Rsize ||
115 desc_A.Xsize != desc_B.Xsize ||
116 desc_A.Ysize != desc_B.Ysize ||
117 desc_A.XOsize != desc_B.XOsize ||
118 desc_A.YOsize != desc_B.YOsize ||
119 desc_A.XTsize != desc_B.XTsize ||
120 desc_A.YTsize != desc_B.YTsize ||
121 desc_A.XTOsize != desc_B.XTOsize ||
122 desc_A.YTOsize != desc_B.YTOsize ||
123 desc_A.Csize != desc_B.Csize
124 // desc_A.CodingStyleDefault != desc_B.CodingStyleDefault ||
125 // desc_A.QuantizationDefault != desc_B.QuantizationDefault
128 note (ERROR, "video MXF picture descriptors differ");
132 // for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
133 // if (desc_A.ImageComponents[j] != desc_B.ImageComponents[j]) {
134 // notes.pack_start ("video MXF picture descriptors differ");
142 MonoPictureAsset::MonoPictureAsset (
143 boost::function<string (int)> get_path,
146 boost::signals2::signal<void (float)>* progress,
148 int intrinsic_duration,
150 MXFMetadata const & metadata
152 : PictureAsset (directory, mxf_name, progress, fps, intrinsic_duration, size)
154 construct (get_path, metadata);
157 MonoPictureAsset::MonoPictureAsset (
158 vector<string> const & files,
161 boost::signals2::signal<void (float)>* progress,
163 int intrinsic_duration,
165 MXFMetadata const & metadata
167 : PictureAsset (directory, mxf_name, progress, fps, intrinsic_duration, size)
169 construct (boost::bind (&MonoPictureAsset::path_from_list, this, _1, files), metadata);
172 MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name, int fps, Size size)
173 : PictureAsset (directory, mxf_name, 0, fps, 0, size)
178 MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name)
179 : PictureAsset (directory, mxf_name)
181 ASDCP::JP2K::MXFReader reader;
182 if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
183 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
186 ASDCP::JP2K::PictureDescriptor desc;
187 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
188 boost::throw_exception (DCPReadError ("could not read video MXF information"));
191 _size.width = desc.StoredWidth;
192 _size.height = desc.StoredHeight;
193 _edit_rate = desc.EditRate.Numerator;
194 assert (desc.EditRate.Denominator == 1);
195 _intrinsic_duration = desc.ContainerDuration;
199 MonoPictureAsset::construct (boost::function<string (int)> get_path, MXFMetadata const & metadata)
201 ASDCP::JP2K::CodestreamParser j2k_parser;
202 ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
203 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
204 boost::throw_exception (FileError ("could not open JPEG2000 file for reading", get_path (0)));
207 ASDCP::JP2K::PictureDescriptor picture_desc;
208 j2k_parser.FillPictureDescriptor (picture_desc);
209 picture_desc.EditRate = ASDCP::Rational (_edit_rate, 1);
211 ASDCP::WriterInfo writer_info;
212 fill_writer_info (&writer_info, _uuid, metadata);
214 ASDCP::JP2K::MXFWriter mxf_writer;
215 if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc, 16384, false))) {
216 boost::throw_exception (MXFFileError ("could not open MXF file for writing", path().string()));
219 for (int i = 0; i < _intrinsic_duration; ++i) {
221 string const path = get_path (i);
223 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
224 boost::throw_exception (FileError ("could not open JPEG2000 file for reading", path));
227 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
228 boost::throw_exception (MXFFileError ("error in writing video MXF", this->path().string()));
232 (*_progress) (0.5 * float (i) / _intrinsic_duration);
236 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
237 boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string()));
242 MonoPictureAsset::path_from_list (int f, vector<string> const & files) const
247 shared_ptr<const MonoPictureFrame>
248 MonoPictureAsset::get_frame (int n) const
250 return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n));
255 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
257 if (!PictureAsset::equals (other, opt, note)) {
261 shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
262 assert (other_picture);
264 for (int i = 0; i < _intrinsic_duration; ++i) {
265 note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
266 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
267 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
269 if (!frame_buffer_equals (
271 frame_A->j2k_data(), frame_A->j2k_size(),
272 frame_B->j2k_data(), frame_B->j2k_size()
282 StereoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
284 if (!PictureAsset::equals (other, opt, note)) {
288 shared_ptr<const StereoPictureAsset> other_picture = dynamic_pointer_cast<const StereoPictureAsset> (other);
289 assert (other_picture);
291 for (int i = 0; i < _intrinsic_duration; ++i) {
292 shared_ptr<const StereoPictureFrame> frame_A = get_frame (i);
293 shared_ptr<const StereoPictureFrame> frame_B = other_picture->get_frame (i);
295 if (!frame_buffer_equals (
297 frame_A->left_j2k_data(), frame_A->left_j2k_size(),
298 frame_B->left_j2k_data(), frame_B->left_j2k_size()
303 if (!frame_buffer_equals (
305 frame_A->right_j2k_data(), frame_A->right_j2k_size(),
306 frame_B->right_j2k_data(), frame_B->right_j2k_size()
316 PictureAsset::frame_buffer_equals (
317 int frame, EqualityOptions opt, boost::function<void (NoteType, string)> note,
318 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
321 if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
322 note (NOTE, "J2K identical");
323 /* Easy result; the J2K data is identical */
327 /* Decompress the images to bitmaps */
328 opj_image_t* image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
329 opj_image_t* image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
333 if (image_A->numcomps != image_B->numcomps) {
334 note (ERROR, "image component counts for frame " + lexical_cast<string>(frame) + " differ");
338 vector<int> abs_diffs (image_A->comps[0].w * image_A->comps[0].h * image_A->numcomps);
342 for (int c = 0; c < image_A->numcomps; ++c) {
344 if (image_A->comps[c].w != image_B->comps[c].w || image_A->comps[c].h != image_B->comps[c].h) {
345 note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
349 int const pixels = image_A->comps[c].w * image_A->comps[c].h;
350 for (int j = 0; j < pixels; ++j) {
351 int const t = abs (image_A->comps[c].data[j] - image_B->comps[c].data[j]);
353 max_diff = max (max_diff, t);
358 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
362 double const mean = double (total) / abs_diffs.size ();
364 uint64_t total_squared_deviation = 0;
365 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
366 total_squared_deviation += pow (*j - mean, 2);
369 double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
371 if (mean > opt.max_mean_pixel_error || std_dev > opt.max_std_dev_pixel_error) {
372 note (ERROR, "mean or standard deviation out of range for " + lexical_cast<string>(frame));
376 note (NOTE, "mean difference " + lexical_cast<string> (mean) + ", deviation " + lexical_cast<string> (std_dev));
378 opj_image_destroy (image_A);
379 opj_image_destroy (image_B);
385 StereoPictureAsset::StereoPictureAsset (string directory, string mxf_name, int fps, int intrinsic_duration)
386 : PictureAsset (directory, mxf_name, 0, fps, intrinsic_duration, Size (0, 0))
388 ASDCP::JP2K::MXFSReader reader;
389 if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
390 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
393 ASDCP::JP2K::PictureDescriptor desc;
394 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
395 boost::throw_exception (DCPReadError ("could not read video MXF information"));
398 _size.width = desc.StoredWidth;
399 _size.height = desc.StoredHeight;
402 shared_ptr<const StereoPictureFrame>
403 StereoPictureAsset::get_frame (int n) const
405 return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n));
408 shared_ptr<MonoPictureAssetWriter>
409 MonoPictureAsset::start_write (bool overwrite, MXFMetadata const & metadata)
411 /* XXX: can't we use shared_ptr here? */
412 return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite, metadata));
415 FrameInfo::FrameInfo (istream& s)
417 s >> offset >> size >> hash;
421 FrameInfo::write (ostream& s)
423 s << offset << " " << size << " " << hash;
426 struct MonoPictureAssetWriter::ASDCPState
429 : frame_buffer (4 * Kumu::Megabyte)
432 ASDCP::JP2K::CodestreamParser j2k_parser;
433 ASDCP::JP2K::FrameBuffer frame_buffer;
434 ASDCP::JP2K::MXFWriter mxf_writer;
435 ASDCP::WriterInfo writer_info;
436 ASDCP::JP2K::PictureDescriptor picture_descriptor;
440 /** @param a Asset to write to. `a' must not be deleted while
441 * this writer class still exists, or bad things will happen.
443 MonoPictureAssetWriter::MonoPictureAssetWriter (MonoPictureAsset* a, bool overwrite, MXFMetadata const & m)
444 : _state (new MonoPictureAssetWriter::ASDCPState)
446 , _frames_written (0)
449 , _overwrite (overwrite)
457 MonoPictureAssetWriter::start (uint8_t* data, int size)
459 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
460 boost::throw_exception (MiscError ("could not parse J2K frame"));
463 _state->j2k_parser.FillPictureDescriptor (_state->picture_descriptor);
464 _state->picture_descriptor.EditRate = ASDCP::Rational (_asset->edit_rate(), 1);
466 MXFAsset::fill_writer_info (&_state->writer_info, _asset->uuid(), _metadata);
468 if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (
469 _asset->path().string().c_str(),
471 _state->picture_descriptor,
476 boost::throw_exception (MXFFileError ("could not open MXF file for writing", _asset->path().string()));
483 MonoPictureAssetWriter::write (uint8_t* data, int size)
485 assert (!_finalized);
491 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
492 boost::throw_exception (MiscError ("could not parse J2K frame"));
495 uint64_t const before_offset = _state->mxf_writer.Tell ();
498 if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0, &hash))) {
499 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
503 return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
507 MonoPictureAssetWriter::fake_write (int size)
510 assert (!_finalized);
512 if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
513 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
520 MonoPictureAssetWriter::finalize ()
522 assert (!_finalized);
524 if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
525 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
529 _asset->set_intrinsic_duration (_frames_written);
530 _asset->set_duration (_frames_written);
533 MonoPictureAssetWriter::~MonoPictureAssetWriter ()