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>
32 #include <libxml++/nodes/element.h>
34 #include "KM_fileio.h"
35 #include "picture_asset.h"
37 #include "exceptions.h"
38 #include "picture_frame.h"
45 using std::stringstream;
50 using boost::shared_ptr;
51 using boost::dynamic_pointer_cast;
52 using boost::lexical_cast;
53 using namespace libdcp;
55 PictureAsset::PictureAsset (string directory, string mxf_name, boost::signals2::signal<void (float)>* progress, int fps, int intrinsic_duration, bool encrypted, Size size)
56 : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration, encrypted)
62 PictureAsset::PictureAsset (string directory, string mxf_name)
63 : MXFAsset (directory, mxf_name)
69 PictureAsset::cpl_node_name () const
75 PictureAsset::write_to_cpl (xmlpp::Node* node) const
77 MXFAsset::write_to_cpl (node);
79 xmlpp::Node::NodeList c = node->get_children ();
80 xmlpp::Node::NodeList::iterator i = c.begin();
81 while (i != c.end() && (*i)->get_name() != cpl_node_name ()) {
85 assert (i != c.end ());
87 (*i)->add_child ("FrameRate")->add_child_text (lexical_cast<string> (_edit_rate) + " 1");
88 (*i)->add_child ("ScreenAspectRatio")->add_child_text (lexical_cast<string> (_size.width) + " " + lexical_cast<string> (_size.height));
92 PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
94 if (!MXFAsset::equals (other, opt, note)) {
98 ASDCP::JP2K::MXFReader reader_A;
99 if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
100 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
103 ASDCP::JP2K::MXFReader reader_B;
104 if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
105 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
108 ASDCP::JP2K::PictureDescriptor desc_A;
109 if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
110 boost::throw_exception (DCPReadError ("could not read video MXF information"));
112 ASDCP::JP2K::PictureDescriptor desc_B;
113 if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
114 boost::throw_exception (DCPReadError ("could not read video MXF information"));
118 desc_A.EditRate != desc_B.EditRate ||
119 desc_A.ContainerDuration != desc_B.ContainerDuration ||
120 desc_A.SampleRate != desc_B.SampleRate ||
121 desc_A.StoredWidth != desc_B.StoredWidth ||
122 desc_A.StoredHeight != desc_B.StoredHeight ||
123 desc_A.AspectRatio != desc_B.AspectRatio ||
124 desc_A.Rsize != desc_B.Rsize ||
125 desc_A.Xsize != desc_B.Xsize ||
126 desc_A.Ysize != desc_B.Ysize ||
127 desc_A.XOsize != desc_B.XOsize ||
128 desc_A.YOsize != desc_B.YOsize ||
129 desc_A.XTsize != desc_B.XTsize ||
130 desc_A.YTsize != desc_B.YTsize ||
131 desc_A.XTOsize != desc_B.XTOsize ||
132 desc_A.YTOsize != desc_B.YTOsize ||
133 desc_A.Csize != desc_B.Csize
134 // desc_A.CodingStyleDefault != desc_B.CodingStyleDefault ||
135 // desc_A.QuantizationDefault != desc_B.QuantizationDefault
138 note (ERROR, "video MXF picture descriptors differ");
142 // for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
143 // if (desc_A.ImageComponents[j] != desc_B.ImageComponents[j]) {
144 // notes.pack_start ("video MXF picture descriptors differ");
152 MonoPictureAsset::MonoPictureAsset (
153 boost::function<string (int)> get_path,
156 boost::signals2::signal<void (float)>* progress,
158 int intrinsic_duration,
161 MXFMetadata const & metadata
163 : PictureAsset (directory, mxf_name, progress, fps, intrinsic_duration, encrypted, size)
165 construct (get_path, metadata);
168 MonoPictureAsset::MonoPictureAsset (
169 vector<string> const & files,
172 boost::signals2::signal<void (float)>* progress,
174 int intrinsic_duration,
177 MXFMetadata const & metadata
179 : PictureAsset (directory, mxf_name, progress, fps, intrinsic_duration, encrypted, size)
181 construct (boost::bind (&MonoPictureAsset::path_from_list, this, _1, files), metadata);
184 MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name, int fps, Size size)
185 : PictureAsset (directory, mxf_name, 0, fps, 0, false, size)
190 MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name)
191 : PictureAsset (directory, mxf_name)
193 ASDCP::JP2K::MXFReader reader;
194 if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
195 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
198 ASDCP::JP2K::PictureDescriptor desc;
199 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
200 boost::throw_exception (DCPReadError ("could not read video MXF information"));
203 _size.width = desc.StoredWidth;
204 _size.height = desc.StoredHeight;
205 _edit_rate = desc.EditRate.Numerator;
206 assert (desc.EditRate.Denominator == 1);
207 _intrinsic_duration = desc.ContainerDuration;
211 MonoPictureAsset::construct (boost::function<string (int)> get_path, MXFMetadata const & metadata)
213 ASDCP::JP2K::CodestreamParser j2k_parser;
214 ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
215 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
216 boost::throw_exception (FileError ("could not open JPEG2000 file for reading", get_path (0)));
219 ASDCP::JP2K::PictureDescriptor picture_desc;
220 j2k_parser.FillPictureDescriptor (picture_desc);
221 picture_desc.EditRate = ASDCP::Rational (_edit_rate, 1);
223 ASDCP::WriterInfo writer_info;
224 fill_writer_info (&writer_info, _uuid, metadata);
226 ASDCP::JP2K::MXFWriter mxf_writer;
227 if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc, 16384, false))) {
228 boost::throw_exception (MXFFileError ("could not open MXF file for writing", path().string()));
231 for (int i = 0; i < _intrinsic_duration; ++i) {
233 string const path = get_path (i);
235 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
236 boost::throw_exception (FileError ("could not open JPEG2000 file for reading", path));
239 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0))) {
240 boost::throw_exception (MXFFileError ("error in writing video MXF", this->path().string()));
244 (*_progress) (0.5 * float (i) / _intrinsic_duration);
248 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
249 boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string()));
254 MonoPictureAsset::path_from_list (int f, vector<string> const & files) const
259 shared_ptr<const MonoPictureFrame>
260 MonoPictureAsset::get_frame (int n) const
262 return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n, _decryption_context));
267 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
269 if (!PictureAsset::equals (other, opt, note)) {
273 shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
274 assert (other_picture);
276 for (int i = 0; i < _intrinsic_duration; ++i) {
277 note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
278 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
279 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
281 if (!frame_buffer_equals (
283 frame_A->j2k_data(), frame_A->j2k_size(),
284 frame_B->j2k_data(), frame_B->j2k_size()
294 StereoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
296 if (!PictureAsset::equals (other, opt, note)) {
300 shared_ptr<const StereoPictureAsset> other_picture = dynamic_pointer_cast<const StereoPictureAsset> (other);
301 assert (other_picture);
303 for (int i = 0; i < _intrinsic_duration; ++i) {
304 shared_ptr<const StereoPictureFrame> frame_A = get_frame (i);
305 shared_ptr<const StereoPictureFrame> frame_B = other_picture->get_frame (i);
307 if (!frame_buffer_equals (
309 frame_A->left_j2k_data(), frame_A->left_j2k_size(),
310 frame_B->left_j2k_data(), frame_B->left_j2k_size()
315 if (!frame_buffer_equals (
317 frame_A->right_j2k_data(), frame_A->right_j2k_size(),
318 frame_B->right_j2k_data(), frame_B->right_j2k_size()
328 PictureAsset::frame_buffer_equals (
329 int frame, EqualityOptions opt, boost::function<void (NoteType, string)> note,
330 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
333 if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
334 note (NOTE, "J2K identical");
335 /* Easy result; the J2K data is identical */
339 /* Decompress the images to bitmaps */
340 opj_image_t* image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
341 opj_image_t* image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
345 if (image_A->numcomps != image_B->numcomps) {
346 note (ERROR, "image component counts for frame " + lexical_cast<string>(frame) + " differ");
350 vector<int> abs_diffs (image_A->comps[0].w * image_A->comps[0].h * image_A->numcomps);
354 for (int c = 0; c < image_A->numcomps; ++c) {
356 if (image_A->comps[c].w != image_B->comps[c].w || image_A->comps[c].h != image_B->comps[c].h) {
357 note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
361 int const pixels = image_A->comps[c].w * image_A->comps[c].h;
362 for (int j = 0; j < pixels; ++j) {
363 int const t = abs (image_A->comps[c].data[j] - image_B->comps[c].data[j]);
365 max_diff = max (max_diff, t);
370 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
374 double const mean = double (total) / abs_diffs.size ();
376 uint64_t total_squared_deviation = 0;
377 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
378 total_squared_deviation += pow (*j - mean, 2);
381 double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
383 note (NOTE, "mean difference " + lexical_cast<string> (mean) + ", deviation " + lexical_cast<string> (std_dev));
385 if (mean > opt.max_mean_pixel_error) {
386 note (ERROR, "mean " + lexical_cast<string>(mean) + " out of range " + lexical_cast<string>(opt.max_mean_pixel_error) + " in frame " + lexical_cast<string>(frame));
390 if (std_dev > opt.max_std_dev_pixel_error) {
391 note (ERROR, "standard deviation " + lexical_cast<string>(std_dev) + " out of range " + lexical_cast<string>(opt.max_std_dev_pixel_error) + " in frame " + lexical_cast<string>(frame));
395 opj_image_destroy (image_A);
396 opj_image_destroy (image_B);
402 StereoPictureAsset::StereoPictureAsset (string directory, string mxf_name, int fps, int intrinsic_duration)
403 : PictureAsset (directory, mxf_name, 0, fps, intrinsic_duration, false, Size (0, 0))
405 ASDCP::JP2K::MXFSReader reader;
406 if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
407 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
410 ASDCP::JP2K::PictureDescriptor desc;
411 if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
412 boost::throw_exception (DCPReadError ("could not read video MXF information"));
415 _size.width = desc.StoredWidth;
416 _size.height = desc.StoredHeight;
419 shared_ptr<const StereoPictureFrame>
420 StereoPictureAsset::get_frame (int n) const
422 return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n));
425 shared_ptr<MonoPictureAssetWriter>
426 MonoPictureAsset::start_write (bool overwrite, MXFMetadata const & metadata)
428 /* XXX: can't we use shared_ptr here? */
429 return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite, metadata));
432 FrameInfo::FrameInfo (istream& s)
434 s >> offset >> size >> hash;
438 FrameInfo::write (ostream& s)
440 s << offset << " " << size << " " << hash;
443 struct MonoPictureAssetWriter::ASDCPState
446 : frame_buffer (4 * Kumu::Megabyte)
449 ASDCP::JP2K::CodestreamParser j2k_parser;
450 ASDCP::JP2K::FrameBuffer frame_buffer;
451 ASDCP::JP2K::MXFWriter mxf_writer;
452 ASDCP::WriterInfo writer_info;
453 ASDCP::JP2K::PictureDescriptor picture_descriptor;
457 /** @param a Asset to write to. `a' must not be deleted while
458 * this writer class still exists, or bad things will happen.
460 MonoPictureAssetWriter::MonoPictureAssetWriter (MonoPictureAsset* a, bool overwrite, MXFMetadata const & m)
461 : _state (new MonoPictureAssetWriter::ASDCPState)
463 , _frames_written (0)
466 , _overwrite (overwrite)
474 MonoPictureAssetWriter::start (uint8_t* data, int size)
476 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
477 boost::throw_exception (MiscError ("could not parse J2K frame"));
480 _state->j2k_parser.FillPictureDescriptor (_state->picture_descriptor);
481 _state->picture_descriptor.EditRate = ASDCP::Rational (_asset->edit_rate(), 1);
483 _asset->fill_writer_info (&_state->writer_info, _asset->uuid(), _metadata);
485 if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (
486 _asset->path().string().c_str(),
488 _state->picture_descriptor,
493 boost::throw_exception (MXFFileError ("could not open MXF file for writing", _asset->path().string()));
500 MonoPictureAssetWriter::write (uint8_t* data, int size)
502 assert (!_finalized);
508 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
509 boost::throw_exception (MiscError ("could not parse J2K frame"));
512 uint64_t const before_offset = _state->mxf_writer.Tell ();
515 if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0, &hash))) {
516 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
520 return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
524 MonoPictureAssetWriter::fake_write (int size)
527 assert (!_finalized);
529 if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
530 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
537 MonoPictureAssetWriter::finalize ()
539 assert (!_finalized);
541 if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
542 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
546 _asset->set_intrinsic_duration (_frames_written);
547 _asset->set_duration (_frames_written);
551 PictureAsset::key_type () const