2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "picture_mxf.h"
22 #include "exceptions.h"
23 #include "xyz_frame.h"
24 #include "picture_mxf_writer.h"
26 #include "KM_fileio.h"
27 #include <libxml++/nodes/element.h>
29 #include <boost/filesystem.hpp>
30 #include <boost/lexical_cast.hpp>
41 using std::stringstream;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48 using boost::lexical_cast;
51 PictureMXF::PictureMXF (boost::filesystem::path file)
57 PictureMXF::PictureMXF (Fraction edit_rate)
64 PictureMXF::read_picture_descriptor (ASDCP::JP2K::PictureDescriptor const & desc)
66 _size.width = desc.StoredWidth;
67 _size.height = desc.StoredHeight;
68 _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
69 _intrinsic_duration = desc.ContainerDuration;
70 _frame_rate = Fraction (desc.SampleRate.Numerator, desc.SampleRate.Denominator);
71 _screen_aspect_ratio = Fraction (desc.AspectRatio.Numerator, desc.AspectRatio.Denominator);
75 PictureMXF::descriptor_equals (
76 ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, boost::function<void (NoteType, string)> note
80 a.EditRate != b.EditRate ||
81 a.SampleRate != b.SampleRate ||
82 a.StoredWidth != b.StoredWidth ||
83 a.StoredHeight != b.StoredHeight ||
84 a.AspectRatio != b.AspectRatio ||
88 a.XOsize != b.XOsize ||
89 a.YOsize != b.YOsize ||
90 a.XTsize != b.XTsize ||
91 a.YTsize != b.YTsize ||
92 a.XTOsize != b.XTOsize ||
93 a.YTOsize != b.YTOsize ||
95 // a.CodingStyleDefault != b.CodingStyleDefault ||
96 // a.QuantizationDefault != b.QuantizationDefault
99 note (ERROR, "video MXF picture descriptors differ");
103 if (a.ContainerDuration != b.ContainerDuration) {
104 note (ERROR, "video container durations differ");
107 // for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
108 // if (a.ImageComponents[j] != b.ImageComponents[j]) {
109 // notes.pack_start ("video MXF picture descriptors differ");
117 PictureMXF::frame_buffer_equals (
118 int frame, EqualityOptions opt, boost::function<void (NoteType, string)> note,
119 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
122 if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
123 note (NOTE, "J2K identical");
124 /* Easy result; the J2K data is identical */
128 /* Decompress the images to bitmaps */
129 shared_ptr<XYZFrame> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
130 shared_ptr<XYZFrame> image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
134 vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
138 for (int c = 0; c < 3; ++c) {
140 if (image_A->size() != image_B->size()) {
141 note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
145 int const pixels = image_A->size().width * image_A->size().height;
146 for (int j = 0; j < pixels; ++j) {
147 int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
149 max_diff = max (max_diff, t);
154 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
158 double const mean = double (total) / abs_diffs.size ();
160 uint64_t total_squared_deviation = 0;
161 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
162 total_squared_deviation += pow (*j - mean, 2);
165 double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
167 note (NOTE, "mean difference " + lexical_cast<string> (mean) + ", deviation " + lexical_cast<string> (std_dev));
169 if (mean > opt.max_mean_pixel_error) {
172 "mean " + lexical_cast<string>(mean) +
173 " out of range " + lexical_cast<string>(opt.max_mean_pixel_error) +
174 " in frame " + lexical_cast<string>(frame)
180 if (std_dev > opt.max_std_dev_pixel_error) {
183 "standard deviation " + lexical_cast<string>(std_dev) +
184 " out of range " + lexical_cast<string>(opt.max_std_dev_pixel_error) +
185 " in frame " + lexical_cast<string>(frame)
195 PictureMXF::key_type () const