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