libdcp
stereo_j2k_picture_asset.cc
1 /*
2  Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3 
4  This file is part of libdcp.
5 
6  libdcp is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  libdcp is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with libdcp. If not, see <http://www.gnu.org/licenses/>.
18 
19  In addition, as a special exception, the copyright holders give
20  permission to link the code of portions of this program with the
21  OpenSSL library under certain conditions as described in each
22  individual source file, and distribute linked combinations
23  including the two.
24 
25  You must obey the GNU General Public License in all respects
26  for all of the code used other than OpenSSL. If you modify
27  file(s) with this exception, you may extend this exception to your
28  version of the file(s), but you are not obligated to do so. If you
29  do not wish to do so, delete this exception statement from your
30  version. If you delete this exception statement from all source
31  files in the program, then also delete it here.
32 */
33 
34 
40 #include "dcp_assert.h"
41 #include "equality_options.h"
42 #include "exceptions.h"
43 #include "filesystem.h"
48 #include <asdcp/AS_DCP.h>
49 
50 
51 using std::string;
52 using std::pair;
53 using std::make_pair;
54 using std::shared_ptr;
55 using std::dynamic_pointer_cast;
56 using namespace dcp;
57 
58 
59 StereoJ2KPictureAsset::StereoJ2KPictureAsset (boost::filesystem::path file)
60  : J2KPictureAsset (file)
61 {
62  Kumu::FileReaderFactory factory;
63  ASDCP::JP2K::MXFSReader reader(factory);
64  auto r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
65  if (ASDCP_FAILURE(r)) {
66  boost::throw_exception (MXFFileError("could not open MXF file for reading", file.string(), r));
67  }
68 
69  ASDCP::JP2K::PictureDescriptor desc;
70  if (ASDCP_FAILURE (reader.FillPictureDescriptor(desc))) {
71  boost::throw_exception (ReadError("could not read video MXF information"));
72  }
73 
74  read_picture_descriptor (desc);
75 
76  ASDCP::WriterInfo info;
77  if (ASDCP_FAILURE (reader.FillWriterInfo(info))) {
78  boost::throw_exception (ReadError("could not read video MXF information"));
79  }
80 
81  _id = read_writer_info (info);
82 }
83 
84 
85 StereoJ2KPictureAsset::StereoJ2KPictureAsset (Fraction edit_rate, Standard standard)
86  : J2KPictureAsset (edit_rate, standard)
87 {
88 
89 }
90 
91 
92 shared_ptr<J2KPictureAssetWriter>
93 StereoJ2KPictureAsset::start_write(boost::filesystem::path file, Behaviour behaviour)
94 {
95  return shared_ptr<StereoJ2KPictureAssetWriter>(new StereoJ2KPictureAssetWriter(this, file, behaviour == Behaviour::OVERWRITE_EXISTING));
96 }
97 
98 
99 shared_ptr<StereoJ2KPictureAssetReader>
100 StereoJ2KPictureAsset::start_read () const
101 {
102  return shared_ptr<StereoJ2KPictureAssetReader> (new StereoJ2KPictureAssetReader(this, key(), standard()));
103 }
104 
105 
106 bool
107 StereoJ2KPictureAsset::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
108 {
109  Kumu::FileReaderFactory factory;
110  ASDCP::JP2K::MXFSReader reader_A(factory);
111  DCP_ASSERT (file());
112  auto r = reader_A.OpenRead(dcp::filesystem::fix_long_path(*file()).string().c_str());
113  if (ASDCP_FAILURE (r)) {
114  boost::throw_exception (MXFFileError ("could not open MXF file for reading", file()->string(), r));
115  }
116 
117  ASDCP::JP2K::MXFSReader reader_B(factory);
118  DCP_ASSERT (other->file());
119  r = reader_B.OpenRead(dcp::filesystem::fix_long_path(*other->file()).string().c_str());
120  if (ASDCP_FAILURE (r)) {
121  boost::throw_exception (MXFFileError ("could not open MXF file for reading", other->file()->string(), r));
122  }
123 
124  ASDCP::JP2K::PictureDescriptor desc_A;
125  if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
126  boost::throw_exception (ReadError ("could not read video MXF information"));
127  }
128  ASDCP::JP2K::PictureDescriptor desc_B;
129  if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
130  boost::throw_exception (ReadError ("could not read video MXF information"));
131  }
132 
133  if (!descriptor_equals (desc_A, desc_B, note)) {
134  return false;
135  }
136 
137  auto other_picture = dynamic_pointer_cast<const StereoJ2KPictureAsset> (other);
138  DCP_ASSERT (other_picture);
139 
140  auto reader = start_read ();
141  auto other_reader = other_picture->start_read ();
142 
143  bool result = true;
144 
145  for (int i = 0; i < _intrinsic_duration; ++i) {
146  shared_ptr<const StereoJ2KPictureFrame> frame_A;
147  shared_ptr<const StereoJ2KPictureFrame> frame_B;
148  try {
149  frame_A = reader->get_frame (i);
150  frame_B = other_reader->get_frame (i);
151  } catch (ReadError& e) {
152  /* If there was a problem reading the frame data we'll just assume
153  the two frames are not equal.
154  */
155  note (NoteType::ERROR, e.what ());
156  return false;
157  }
158 
159  if (!frame_buffer_equals (
160  i, opt, note,
161  frame_A->left()->data(), frame_A->left()->size(),
162  frame_B->left()->data(), frame_B->left()->size()
163  )) {
164  result = false;
165  if (!opt.keep_going) {
166  return result;
167  }
168  }
169 
170  if (!frame_buffer_equals (
171  i, opt, note,
172  frame_A->right()->data(), frame_A->right()->size(),
173  frame_B->right()->data(), frame_B->right()->size()
174  )) {
175  result = false;
176  if (!opt.keep_going) {
177  return result;
178  }
179  }
180  }
181 
182  return result;
183 }
boost::optional< boost::filesystem::path > file() const
Definition: asset.h:100
A class to describe what "equality" means for a particular test.
A fraction (i.e. a thing with an integer numerator and an integer denominator).
Definition: types.h:168
An asset made up of JPEG2000 data.
An exception related to an MXF file.
Definition: exceptions.h:82
boost::optional< Key > key() const
Definition: mxf.h:104
int64_t _intrinsic_duration
Definition: picture_asset.h:90
Any error that occurs when reading data from a DCP.
Definition: exceptions.h:106
A helper class for writing to StereoJ2KPictureAssets.
DCP_ASSERT macro.
Class to describe what equality means when calling Asset::equals().
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50
StereoJ2KPictureAsset class.
StereoJ2KPictureAssetReader typedef.
StereoJ2KPictureAssetWriter class.
StereoJ2KPictureFrame class.