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