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