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_image.h"
24 #include "picture_mxf_writer.h"
25 #include "compose.hpp"
27 #include "KM_fileio.h"
28 #include <libxml++/nodes/element.h>
30 #include <boost/filesystem.hpp>
41 using std::stringstream;
46 using boost::shared_ptr;
49 PictureMXF::PictureMXF (boost::filesystem::path file)
51 , _intrinsic_duration (0)
56 PictureMXF::PictureMXF (Fraction edit_rate)
57 : _edit_rate (edit_rate)
58 , _intrinsic_duration (0)
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, NoteHandler 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 (DCP_ERROR, "video MXF picture descriptors differ");
103 if (a.ContainerDuration != b.ContainerDuration) {
104 note (DCP_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, NoteHandler 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 (DCP_NOTE, "J2K identical");
124 /* Easy result; the J2K data is identical */
128 /* Decompress the images to bitmaps */
129 shared_ptr<XYZImage> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
130 shared_ptr<XYZImage> 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 (DCP_ERROR, String::compose ("image sizes for frame %1 differ", frame));
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 (DCP_NOTE, String::compose ("mean difference %1, deviation %2", mean, std_dev));
169 if (mean > opt.max_mean_pixel_error) {
172 String::compose ("mean %1 out of range %2 in frame %3", mean, opt.max_mean_pixel_error, frame)
178 if (std_dev > opt.max_std_dev_pixel_error) {
181 String::compose ("standard deviation %1 out of range %2 in frame %3", std_dev, opt.max_std_dev_pixel_error, frame)