2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 /** @file src/picture_asset.cc
36 * @brief PictureAsset class
40 #include "compose.hpp"
41 #include "dcp_assert.h"
42 #include "equality_options.h"
43 #include "exceptions.h"
44 #include "j2k_transcode.h"
45 #include "openjpeg_image.h"
46 #include "picture_asset.h"
47 #include "picture_asset_writer.h"
49 #include <asdcp/AS_DCP.h>
50 #include <asdcp/KM_fileio.h>
51 #include <libxml++/nodes/element.h>
52 #include <boost/filesystem.hpp>
63 using std::shared_ptr;
67 PictureAsset::PictureAsset (boost::filesystem::path file)
74 PictureAsset::PictureAsset (Fraction edit_rate, Standard standard)
76 , _edit_rate (edit_rate)
83 PictureAsset::read_picture_descriptor (ASDCP::JP2K::PictureDescriptor const & desc)
85 _size.width = desc.StoredWidth;
86 _size.height = desc.StoredHeight;
87 _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
88 _intrinsic_duration = desc.ContainerDuration;
89 _frame_rate = Fraction (desc.SampleRate.Numerator, desc.SampleRate.Denominator);
90 _screen_aspect_ratio = Fraction (desc.AspectRatio.Numerator, desc.AspectRatio.Denominator);
95 PictureAsset::descriptor_equals (
96 ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, NoteHandler note
100 a.EditRate != b.EditRate ||
101 a.SampleRate != b.SampleRate ||
102 a.StoredWidth != b.StoredWidth ||
103 a.StoredHeight != b.StoredHeight ||
104 a.AspectRatio != b.AspectRatio ||
105 a.Rsize != b.Rsize ||
106 a.Xsize != b.Xsize ||
107 a.Ysize != b.Ysize ||
108 a.XOsize != b.XOsize ||
109 a.YOsize != b.YOsize ||
110 a.XTsize != b.XTsize ||
111 a.YTsize != b.YTsize ||
112 a.XTOsize != b.XTOsize ||
113 a.YTOsize != b.YTOsize ||
115 // a.CodingStyleDefault != b.CodingStyleDefault ||
116 // a.QuantizationDefault != b.QuantizationDefault
119 note (NoteType::ERROR, "video MXF picture descriptors differ");
123 if (a.ContainerDuration != b.ContainerDuration) {
124 note (NoteType::ERROR, "video container durations differ");
127 // for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
128 // if (a.ImageComponents[j] != b.ImageComponents[j]) {
129 // notes.pack_start ("video MXF picture descriptors differ");
138 PictureAsset::frame_buffer_equals (
139 int frame, EqualityOptions const& opt, NoteHandler note,
140 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
143 if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
144 note (NoteType::NOTE, "J2K identical");
145 /* Easy result; the J2K data is identical */
149 /* Decompress the images to bitmaps */
150 auto image_A = decompress_j2k (const_cast<uint8_t*>(data_A), size_A, 0);
151 auto image_B = decompress_j2k (const_cast<uint8_t*>(data_B), size_B, 0);
155 vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
159 for (int c = 0; c < 3; ++c) {
161 if (image_A->size() != image_B->size()) {
162 note (NoteType::ERROR, String::compose ("image sizes for frame %1 differ", frame));
166 int const pixels = image_A->size().width * image_A->size().height;
167 for (int j = 0; j < pixels; ++j) {
168 int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
170 max_diff = max (max_diff, t);
175 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
179 double const mean = double (total) / abs_diffs.size ();
181 uint64_t total_squared_deviation = 0;
182 for (auto j: abs_diffs) {
183 total_squared_deviation += pow (j - mean, 2);
186 auto const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
188 note (NoteType::NOTE, String::compose("mean difference %1 deviation %2", mean, std_dev));
190 if (mean > opt.max_mean_pixel_error) {
193 String::compose ("mean %1 out of range %2 in frame %3", mean, opt.max_mean_pixel_error, frame)
199 if (std_dev > opt.max_std_dev_pixel_error) {
202 String::compose ("standard deviation %1 out of range %2 in frame %3", std_dev, opt.max_std_dev_pixel_error, frame)
213 PictureAsset::static_pkl_type (Standard standard)
216 case Standard::INTEROP:
217 return "application/x-smpte-mxf;asdcpKind=Picture";
218 case Standard::SMPTE:
219 return "application/mxf";
227 PictureAsset::pkl_type (Standard standard) const
229 return static_pkl_type (standard);