Bump asdcplib to dcpomatic-2.13.0 branch.
[libdcp.git] / src / mono_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/mono_picture_asset.cc
36  *  @brief MonoPictureAsset class
37  */
38
39
40 #include "compose.hpp"
41 #include "dcp_assert.h"
42 #include "equality_options.h"
43 #include "exceptions.h"
44 #include "filesystem.h"
45 #include "mono_picture_asset.h"
46 #include "mono_picture_asset_reader.h"
47 #include "mono_picture_asset_writer.h"
48 #include "mono_picture_frame.h"
49 #include <asdcp/AS_DCP.h>
50 #include <asdcp/KM_fileio.h>
51
52
53 using std::dynamic_pointer_cast;
54 using std::list;
55 using std::make_shared;
56 using std::pair;
57 using std::shared_ptr;
58 using std::string;
59 using std::vector;
60 #if BOOST_VERSION >= 106100
61 using namespace boost::placeholders;
62 #endif
63 using namespace dcp;
64
65
66 MonoPictureAsset::MonoPictureAsset (boost::filesystem::path file)
67         : PictureAsset (file)
68 {
69         Kumu::FileReaderFactory factory;
70         ASDCP::JP2K::MXFReader reader(factory);
71         auto r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
72         if (ASDCP_FAILURE(r)) {
73                 boost::throw_exception (MXFFileError("could not open MXF file for reading", file.string(), r));
74         }
75
76         ASDCP::JP2K::PictureDescriptor desc;
77         if (ASDCP_FAILURE (reader.FillPictureDescriptor(desc))) {
78                 boost::throw_exception (ReadError("could not read video MXF information"));
79         }
80
81         read_picture_descriptor (desc);
82
83         ASDCP::WriterInfo info;
84         if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
85                 boost::throw_exception (ReadError("could not read video MXF information"));
86         }
87
88         _id = read_writer_info (info);
89 }
90
91
92 MonoPictureAsset::MonoPictureAsset (Fraction edit_rate, Standard standard)
93         : PictureAsset (edit_rate, standard)
94 {
95
96 }
97
98
99 static void
100 storing_note_handler (list<pair<NoteType, string>>& notes, NoteType t, string s)
101 {
102         notes.push_back (make_pair (t, s));
103 }
104
105
106 bool
107 MonoPictureAsset::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
108 {
109         if (!dynamic_pointer_cast<const MonoPictureAsset>(other)) {
110                 return false;
111         }
112
113         Kumu::FileReaderFactory factory;
114         ASDCP::JP2K::MXFReader reader_A(factory);
115         DCP_ASSERT (_file);
116         auto r = reader_A.OpenRead(dcp::filesystem::fix_long_path(*_file).string().c_str());
117         if (ASDCP_FAILURE(r)) {
118                 boost::throw_exception (MXFFileError("could not open MXF file for reading", _file->string(), r));
119         }
120
121         ASDCP::JP2K::MXFReader reader_B(factory);
122         DCP_ASSERT (other->file ());
123         r = reader_B.OpenRead(dcp::filesystem::fix_long_path(*other->file()).string().c_str());
124         if (ASDCP_FAILURE (r)) {
125                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", other->file()->string(), r));
126         }
127
128         ASDCP::JP2K::PictureDescriptor desc_A;
129         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
130                 boost::throw_exception (ReadError ("could not read video MXF information"));
131         }
132         ASDCP::JP2K::PictureDescriptor desc_B;
133         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
134                 boost::throw_exception (ReadError ("could not read video MXF information"));
135         }
136
137         if (!descriptor_equals (desc_A, desc_B, note)) {
138                 return false;
139         }
140
141         auto other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
142         DCP_ASSERT (other_picture);
143
144         bool result = true;
145
146         auto reader = start_read ();
147         auto other_reader = other_picture->start_read ();
148
149 #ifdef LIBDCP_OPENMP
150 #pragma omp parallel for
151 #endif
152
153         for (int i = 0; i < _intrinsic_duration; ++i) {
154                 if (i >= other_picture->intrinsic_duration()) {
155                         result = false;
156                 }
157
158                 if (result || opt.keep_going) {
159
160                         auto frame_A = reader->get_frame (i);
161                         auto frame_B = other_reader->get_frame (i);
162
163                         list<pair<NoteType, string>> notes;
164
165                         if (!frame_buffer_equals (
166                                     i, opt, bind (&storing_note_handler, boost::ref(notes), _1, _2),
167                                     frame_A->data(), frame_A->size(),
168                                     frame_B->data(), frame_B->size()
169                                     )) {
170                                 result = false;
171                         }
172
173 #ifdef LIBDCP_OPENMP
174 #pragma omp critical
175 #endif
176                         {
177                                 note (NoteType::PROGRESS, String::compose("Compared video frame %1 of %2", i, _intrinsic_duration));
178                                 for (auto const& i: notes) {
179                                         note (i.first, i.second);
180                                 }
181                         }
182                 }
183         }
184
185         return result;
186 }
187
188
189 shared_ptr<PictureAssetWriter>
190 MonoPictureAsset::start_write(boost::filesystem::path file, Behaviour behaviour)
191 {
192         /* Can't use make_shared here as the MonoPictureAssetWriter constructor is private */
193         return shared_ptr<MonoPictureAssetWriter>(new MonoPictureAssetWriter(this, file, behaviour == Behaviour::OVERWRITE_EXISTING));
194 }
195
196 shared_ptr<MonoPictureAssetReader>
197 MonoPictureAsset::start_read () const
198 {
199         /* Can't use make_shared here as the MonoPictureAssetReader constructor is private */
200         return shared_ptr<MonoPictureAssetReader>(new MonoPictureAssetReader(this, key(), standard()));
201
202 }
203
204 string
205 MonoPictureAsset::cpl_node_name () const
206 {
207         return "MainPicture";
208 }