2 Copyright (C) 2012-2015 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/>.
20 #include "picture_asset.h"
22 #include "exceptions.h"
23 #include "openjpeg_image.h"
24 #include "picture_asset_writer.h"
25 #include "dcp_assert.h"
26 #include "compose.hpp"
28 #include "KM_fileio.h"
30 #include <libxml++/nodes/element.h>
31 #include <boost/filesystem.hpp>
42 using std::stringstream;
47 using boost::shared_ptr;
50 PictureAsset::PictureAsset (boost::filesystem::path file)
52 , _intrinsic_duration (0)
57 PictureAsset::PictureAsset (Fraction edit_rate)
58 : _edit_rate (edit_rate)
59 , _intrinsic_duration (0)
65 PictureAsset::read_picture_descriptor (ASDCP::JP2K::PictureDescriptor const & desc)
67 _size.width = desc.StoredWidth;
68 _size.height = desc.StoredHeight;
69 _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
70 _intrinsic_duration = desc.ContainerDuration;
71 _frame_rate = Fraction (desc.SampleRate.Numerator, desc.SampleRate.Denominator);
72 _screen_aspect_ratio = Fraction (desc.AspectRatio.Numerator, desc.AspectRatio.Denominator);
76 PictureAsset::descriptor_equals (
77 ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, NoteHandler note
81 a.EditRate != b.EditRate ||
82 a.SampleRate != b.SampleRate ||
83 a.StoredWidth != b.StoredWidth ||
84 a.StoredHeight != b.StoredHeight ||
85 a.AspectRatio != b.AspectRatio ||
89 a.XOsize != b.XOsize ||
90 a.YOsize != b.YOsize ||
91 a.XTsize != b.XTsize ||
92 a.YTsize != b.YTsize ||
93 a.XTOsize != b.XTOsize ||
94 a.YTOsize != b.YTOsize ||
96 // a.CodingStyleDefault != b.CodingStyleDefault ||
97 // a.QuantizationDefault != b.QuantizationDefault
100 note (DCP_ERROR, "video MXF picture descriptors differ");
104 if (a.ContainerDuration != b.ContainerDuration) {
105 note (DCP_ERROR, "video container durations differ");
108 // for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
109 // if (a.ImageComponents[j] != b.ImageComponents[j]) {
110 // notes.pack_start ("video MXF picture descriptors differ");
118 PictureAsset::frame_buffer_equals (
119 int frame, EqualityOptions opt, NoteHandler note,
120 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
123 if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
124 note (DCP_NOTE, "J2K identical");
125 /* Easy result; the J2K data is identical */
129 /* Decompress the images to bitmaps */
130 shared_ptr<OpenJPEGImage> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
131 shared_ptr<OpenJPEGImage> image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
135 vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
139 for (int c = 0; c < 3; ++c) {
141 if (image_A->size() != image_B->size()) {
142 note (DCP_ERROR, String::compose ("image sizes for frame %1 differ", frame));
146 int const pixels = image_A->size().width * image_A->size().height;
147 for (int j = 0; j < pixels; ++j) {
148 int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
150 max_diff = max (max_diff, t);
155 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
159 double const mean = double (total) / abs_diffs.size ();
161 uint64_t total_squared_deviation = 0;
162 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
163 total_squared_deviation += pow (*j - mean, 2);
166 double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
168 note (DCP_NOTE, String::compose ("mean difference %1 deviation %2", mean, std_dev));
170 if (mean > opt.max_mean_pixel_error) {
173 String::compose ("mean %1 out of range %2 in frame %3", mean, opt.max_mean_pixel_error, frame)
179 if (std_dev > opt.max_std_dev_pixel_error) {
182 String::compose ("standard deviation %1 out of range %2 in frame %3", std_dev, opt.max_std_dev_pixel_error, frame)
192 PictureAsset::pkl_type (Standard standard) const
196 return "application/x-smpte-mxf;asdcpKind=Picture";
198 return "application/mxf";