Add OpenSSL licence exception.
[libdcp.git] / src / picture_asset.cc
1 /*
2     Copyright (C) 2012-2015 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 "picture_asset.h"
35 #include "util.h"
36 #include "exceptions.h"
37 #include "openjpeg_image.h"
38 #include "picture_asset_writer.h"
39 #include "dcp_assert.h"
40 #include "compose.hpp"
41 #include "AS_DCP.h"
42 #include "KM_fileio.h"
43 #include "j2k.h"
44 #include <libxml++/nodes/element.h>
45 #include <boost/filesystem.hpp>
46 #include <list>
47 #include <stdexcept>
48 #include <iostream>
49 #include <sstream>
50
51 using std::string;
52 using std::ostream;
53 using std::list;
54 using std::vector;
55 using std::max;
56 using std::stringstream;
57 using std::pair;
58 using std::make_pair;
59 using std::istream;
60 using std::cout;
61 using boost::shared_ptr;
62 using namespace dcp;
63
64 PictureAsset::PictureAsset (boost::filesystem::path file)
65         : Asset (file)
66         , _intrinsic_duration (0)
67 {
68
69 }
70
71 PictureAsset::PictureAsset (Fraction edit_rate)
72         : _edit_rate (edit_rate)
73         , _intrinsic_duration (0)
74 {
75
76 }
77
78 void
79 PictureAsset::read_picture_descriptor (ASDCP::JP2K::PictureDescriptor const & desc)
80 {
81         _size.width = desc.StoredWidth;
82         _size.height = desc.StoredHeight;
83         _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
84         _intrinsic_duration = desc.ContainerDuration;
85         _frame_rate = Fraction (desc.SampleRate.Numerator, desc.SampleRate.Denominator);
86         _screen_aspect_ratio = Fraction (desc.AspectRatio.Numerator, desc.AspectRatio.Denominator);
87 }
88
89 bool
90 PictureAsset::descriptor_equals (
91         ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, NoteHandler note
92         ) const
93 {
94         if (
95                 a.EditRate != b.EditRate ||
96                 a.SampleRate != b.SampleRate ||
97                 a.StoredWidth != b.StoredWidth ||
98                 a.StoredHeight != b.StoredHeight ||
99                 a.AspectRatio != b.AspectRatio ||
100                 a.Rsize != b.Rsize ||
101                 a.Xsize != b.Xsize ||
102                 a.Ysize != b.Ysize ||
103                 a.XOsize != b.XOsize ||
104                 a.YOsize != b.YOsize ||
105                 a.XTsize != b.XTsize ||
106                 a.YTsize != b.YTsize ||
107                 a.XTOsize != b.XTOsize ||
108                 a.YTOsize != b.YTOsize ||
109                 a.Csize != b.Csize
110 //              a.CodingStyleDefault != b.CodingStyleDefault ||
111 //              a.QuantizationDefault != b.QuantizationDefault
112                 ) {
113
114                 note (DCP_ERROR, "video MXF picture descriptors differ");
115                 return false;
116         }
117
118         if (a.ContainerDuration != b.ContainerDuration) {
119                 note (DCP_ERROR, "video container durations differ");
120         }
121
122 //              for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
123 //                      if (a.ImageComponents[j] != b.ImageComponents[j]) {
124 //                              notes.pack_start ("video MXF picture descriptors differ");
125 //                      }
126 //              }
127
128         return true;
129 }
130
131 bool
132 PictureAsset::frame_buffer_equals (
133         int frame, EqualityOptions opt, NoteHandler note,
134         uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
135         ) const
136 {
137         if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
138                 note (DCP_NOTE, "J2K identical");
139                 /* Easy result; the J2K data is identical */
140                 return true;
141         }
142
143         /* Decompress the images to bitmaps */
144         shared_ptr<OpenJPEGImage> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
145         shared_ptr<OpenJPEGImage> image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
146
147         /* Compare them */
148
149         vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
150         int d = 0;
151         int max_diff = 0;
152
153         for (int c = 0; c < 3; ++c) {
154
155                 if (image_A->size() != image_B->size()) {
156                         note (DCP_ERROR, String::compose ("image sizes for frame %1 differ", frame));
157                         return false;
158                 }
159
160                 int const pixels = image_A->size().width * image_A->size().height;
161                 for (int j = 0; j < pixels; ++j) {
162                         int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
163                         abs_diffs[d++] = t;
164                         max_diff = max (max_diff, t);
165                 }
166         }
167
168         uint64_t total = 0;
169         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
170                 total += *j;
171         }
172
173         double const mean = double (total) / abs_diffs.size ();
174
175         uint64_t total_squared_deviation = 0;
176         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
177                 total_squared_deviation += pow (*j - mean, 2);
178         }
179
180         double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
181
182         note (DCP_NOTE, String::compose ("mean difference %1 deviation %2", mean, std_dev));
183
184         if (mean > opt.max_mean_pixel_error) {
185                 note (
186                         DCP_ERROR,
187                         String::compose ("mean %1 out of range %2 in frame %3", mean, opt.max_mean_pixel_error, frame)
188                         );
189
190                 return false;
191         }
192
193         if (std_dev > opt.max_std_dev_pixel_error) {
194                 note (
195                         DCP_ERROR,
196                         String::compose ("standard deviation %1 out of range %2 in frame %3", std_dev, opt.max_std_dev_pixel_error, frame)
197                         );
198
199                 return false;
200         }
201
202         return true;
203 }
204
205 string
206 PictureAsset::pkl_type (Standard standard) const
207 {
208         switch (standard) {
209         case INTEROP:
210                 return "application/x-smpte-mxf;asdcpKind=Picture";
211         case SMPTE:
212                 return "application/mxf";
213         default:
214                 DCP_ASSERT (false);
215         }
216 }