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