Add OpenSSL licence exception.
[libdcp.git] / src / mono_picture_asset.cc
1 /*
2     Copyright (C) 2012-2016 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 #include "mono_picture_asset.h"
35 #include "mono_picture_asset_writer.h"
36 #include "mono_picture_asset_reader.h"
37 #include "AS_DCP.h"
38 #include "KM_fileio.h"
39 #include "exceptions.h"
40 #include "dcp_assert.h"
41 #include "mono_picture_frame.h"
42 #include "compose.hpp"
43
44 using std::string;
45 using std::vector;
46 using std::list;
47 using std::pair;
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
50 using namespace dcp;
51
52 MonoPictureAsset::MonoPictureAsset (boost::filesystem::path file)
53         : PictureAsset (file)
54 {
55         ASDCP::JP2K::MXFReader reader;
56         Kumu::Result_t r = reader.OpenRead (file.string().c_str());
57         if (ASDCP_FAILURE (r)) {
58                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file.string(), r));
59         }
60
61         ASDCP::JP2K::PictureDescriptor desc;
62         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
63                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
64         }
65
66         read_picture_descriptor (desc);
67
68         ASDCP::WriterInfo info;
69         if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
70                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
71         }
72
73         _id = read_writer_info (info);
74 }
75
76 MonoPictureAsset::MonoPictureAsset (Fraction edit_rate)
77         : PictureAsset (edit_rate)
78 {
79
80 }
81
82 static void
83 storing_note_handler (list<pair<NoteType, string> >& notes, NoteType t, string s)
84 {
85         notes.push_back (make_pair (t, s));
86 }
87
88 bool
89 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
90 {
91         if (!dynamic_pointer_cast<const MonoPictureAsset> (other)) {
92                 return false;
93         }
94
95         ASDCP::JP2K::MXFReader reader_A;
96         Kumu::Result_t r = reader_A.OpenRead (_file.string().c_str());
97         if (ASDCP_FAILURE (r)) {
98                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", _file.string(), r));
99         }
100
101         ASDCP::JP2K::MXFReader reader_B;
102         r = reader_B.OpenRead (other->file().string().c_str());
103         if (ASDCP_FAILURE (r)) {
104                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", other->file().string(), r));
105         }
106
107         ASDCP::JP2K::PictureDescriptor desc_A;
108         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
109                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
110         }
111         ASDCP::JP2K::PictureDescriptor desc_B;
112         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
113                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
114         }
115
116         if (!descriptor_equals (desc_A, desc_B, note)) {
117                 return false;
118         }
119
120         shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
121         DCP_ASSERT (other_picture);
122
123         bool result = true;
124
125 #ifdef LIBDCP_OPENMP
126 #pragma omp parallel for
127 #endif
128
129         shared_ptr<MonoPictureAssetReader> reader = start_read ();
130         shared_ptr<MonoPictureAssetReader> other_reader = other_picture->start_read ();
131
132         for (int i = 0; i < _intrinsic_duration; ++i) {
133                 if (i >= other_picture->intrinsic_duration()) {
134                         result = false;
135                 }
136
137                 if (result || opt.keep_going) {
138
139                         shared_ptr<const MonoPictureFrame> frame_A = reader->get_frame (i);
140                         shared_ptr<const MonoPictureFrame> frame_B = other_reader->get_frame (i);
141
142                         list<pair<NoteType, string> > notes;
143
144                         if (!frame_buffer_equals (
145                                     i, opt, bind (&storing_note_handler, boost::ref(notes), _1, _2),
146                                     frame_A->j2k_data(), frame_A->j2k_size(),
147                                     frame_B->j2k_data(), frame_B->j2k_size()
148                                     )) {
149                                 result = false;
150                         }
151
152 #ifdef LIBDCP_OPENMP
153 #pragma omp critical
154 #endif
155                         {
156                                 note (DCP_PROGRESS, String::compose ("Compared video frame %1 of %2", i, _intrinsic_duration));
157                                 for (list<pair<NoteType, string> >::const_iterator i = notes.begin(); i != notes.end(); ++i) {
158                                         note (i->first, i->second);
159                                 }
160                         }
161                 }
162         }
163
164         return result;
165 }
166
167 shared_ptr<PictureAssetWriter>
168 MonoPictureAsset::start_write (boost::filesystem::path file, Standard standard, bool overwrite)
169 {
170         /* XXX: can't we use shared_ptr here? */
171         return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, file, standard, overwrite));
172 }
173
174 shared_ptr<MonoPictureAssetReader>
175 MonoPictureAsset::start_read () const
176 {
177         return shared_ptr<MonoPictureAssetReader> (new MonoPictureAssetReader (this));
178 }
179
180 string
181 MonoPictureAsset::cpl_node_name () const
182 {
183         return "MainPicture";
184 }