2ce3cdc90f386ed172e7bc23315c6a6ed8ed8910
[libdcp.git] / src / stereo_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
35 /** @file  src/stereo_picture_asset.cc
36  *  @brief StereoPictureAsset class
37  */
38
39
40 #include "dcp_assert.h"
41 #include "equality_options.h"
42 #include "exceptions.h"
43 #include "filesystem.h"
44 #include "stereo_picture_asset.h"
45 #include "stereo_picture_asset_reader.h"
46 #include "stereo_picture_asset_writer.h"
47 #include "stereo_picture_frame.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 StereoPictureAsset::StereoPictureAsset (boost::filesystem::path file)
60         : PictureAsset (file)
61 {
62         ASDCP::JP2K::MXFSReader reader;
63         auto r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
64         if (ASDCP_FAILURE(r)) {
65                 boost::throw_exception (MXFFileError("could not open MXF file for reading", file.string(), r));
66         }
67
68         ASDCP::JP2K::PictureDescriptor desc;
69         if (ASDCP_FAILURE (reader.FillPictureDescriptor(desc))) {
70                 boost::throw_exception (ReadError("could not read video MXF information"));
71         }
72
73         read_picture_descriptor (desc);
74
75         ASDCP::WriterInfo info;
76         if (ASDCP_FAILURE (reader.FillWriterInfo(info))) {
77                 boost::throw_exception (ReadError("could not read video MXF information"));
78         }
79
80         _id = read_writer_info (info);
81 }
82
83
84 StereoPictureAsset::StereoPictureAsset (Fraction edit_rate, Standard standard)
85         : PictureAsset (edit_rate, standard)
86 {
87
88 }
89
90
91 shared_ptr<PictureAssetWriter>
92 StereoPictureAsset::start_write(boost::filesystem::path file, Behaviour behaviour)
93 {
94         return shared_ptr<StereoPictureAssetWriter>(new StereoPictureAssetWriter(this, file, behaviour == Behaviour::OVERWRITE_EXISTING));
95 }
96
97
98 shared_ptr<StereoPictureAssetReader>
99 StereoPictureAsset::start_read () const
100 {
101         return shared_ptr<StereoPictureAssetReader> (new StereoPictureAssetReader(this, key(), standard()));
102 }
103
104
105 bool
106 StereoPictureAsset::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
107 {
108         ASDCP::JP2K::MXFSReader reader_A;
109         DCP_ASSERT (file());
110         auto r = reader_A.OpenRead(dcp::filesystem::fix_long_path(*file()).string().c_str());
111         if (ASDCP_FAILURE (r)) {
112                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file()->string(), r));
113         }
114
115         ASDCP::JP2K::MXFSReader reader_B;
116         DCP_ASSERT (other->file());
117         r = reader_B.OpenRead(dcp::filesystem::fix_long_path(*other->file()).string().c_str());
118         if (ASDCP_FAILURE (r)) {
119                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", other->file()->string(), r));
120         }
121
122         ASDCP::JP2K::PictureDescriptor desc_A;
123         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
124                 boost::throw_exception (ReadError ("could not read video MXF information"));
125         }
126         ASDCP::JP2K::PictureDescriptor desc_B;
127         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
128                 boost::throw_exception (ReadError ("could not read video MXF information"));
129         }
130
131         if (!descriptor_equals (desc_A, desc_B, note)) {
132                 return false;
133         }
134
135         auto other_picture = dynamic_pointer_cast<const StereoPictureAsset> (other);
136         DCP_ASSERT (other_picture);
137
138         auto reader = start_read ();
139         auto other_reader = other_picture->start_read ();
140
141         bool result = true;
142
143         for (int i = 0; i < _intrinsic_duration; ++i) {
144                 shared_ptr<const StereoPictureFrame> frame_A;
145                 shared_ptr<const StereoPictureFrame> frame_B;
146                 try {
147                         frame_A = reader->get_frame (i);
148                         frame_B = other_reader->get_frame (i);
149                 } catch (ReadError& e) {
150                         /* If there was a problem reading the frame data we'll just assume
151                            the two frames are not equal.
152                         */
153                         note (NoteType::ERROR, e.what ());
154                         return false;
155                 }
156
157                 if (!frame_buffer_equals (
158                             i, opt, note,
159                             frame_A->left()->data(), frame_A->left()->size(),
160                             frame_B->left()->data(), frame_B->left()->size()
161                             )) {
162                         result = false;
163                         if (!opt.keep_going) {
164                                 return result;
165                         }
166                 }
167
168                 if (!frame_buffer_equals (
169                             i, opt, note,
170                             frame_A->right()->data(), frame_A->right()->size(),
171                             frame_B->right()->data(), frame_B->right()->size()
172                             )) {
173                         result = false;
174                         if (!opt.keep_going) {
175                                 return result;
176                         }
177                 }
178         }
179
180         return result;
181 }