Some tidying up.
[libdcp.git] / src / picture_mxf.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "picture_mxf.h"
21 #include "util.h"
22 #include "exceptions.h"
23 #include "xyz_frame.h"
24 #include "picture_mxf_writer.h"
25 #include "AS_DCP.h"
26 #include "KM_fileio.h"
27 #include <libxml++/nodes/element.h>
28 #include <openjpeg.h>
29 #include <boost/filesystem.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <list>
32 #include <stdexcept>
33 #include <iostream>
34 #include <sstream>
35
36 using std::string;
37 using std::ostream;
38 using std::list;
39 using std::vector;
40 using std::max;
41 using std::stringstream;
42 using std::pair;
43 using std::make_pair;
44 using std::istream;
45 using std::cout;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48 using boost::lexical_cast;
49 using namespace dcp;
50
51 PictureMXF::PictureMXF (boost::filesystem::path file)
52         : MXF (file)
53 {
54
55 }
56
57 PictureMXF::PictureMXF (Fraction edit_rate)
58         : MXF (edit_rate)
59 {
60
61 }
62
63 void
64 PictureMXF::read_picture_descriptor (ASDCP::JP2K::PictureDescriptor const & desc)
65 {
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);
72 }
73
74 bool
75 PictureMXF::descriptor_equals (
76         ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, boost::function<void (NoteType, string)> note
77         ) const
78 {
79         if (
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 ||
85                 a.Rsize != b.Rsize ||
86                 a.Xsize != b.Xsize ||
87                 a.Ysize != b.Ysize ||
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 ||
94                 a.Csize != b.Csize
95 //              a.CodingStyleDefault != b.CodingStyleDefault ||
96 //              a.QuantizationDefault != b.QuantizationDefault
97                 ) {
98                 
99                 note (ERROR, "video MXF picture descriptors differ");
100                 return false;
101         }
102
103         if (a.ContainerDuration != b.ContainerDuration) {
104                 note (ERROR, "video container durations differ");
105         }
106         
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");
110 //                      }
111 //              }
112
113         return true;
114 }
115
116 bool
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
120         ) const
121 {
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 */
125                 return true;
126         }
127                 
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);
131         
132         /* Compare them */
133         
134         vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
135         int d = 0;
136         int max_diff = 0;
137         
138         for (int c = 0; c < 3; ++c) {
139                 
140                 if (image_A->size() != image_B->size()) {
141                         note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
142                         return false;
143                 }
144                 
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]);
148                         abs_diffs[d++] = t;
149                         max_diff = max (max_diff, t);
150                 }
151         }
152                 
153         uint64_t total = 0;
154         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
155                 total += *j;
156         }
157         
158         double const mean = double (total) / abs_diffs.size ();
159         
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);
163         }
164         
165         double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
166         
167         note (NOTE, "mean difference " + lexical_cast<string> (mean) + ", deviation " + lexical_cast<string> (std_dev));
168         
169         if (mean > opt.max_mean_pixel_error) {
170                 note (
171                         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)
175                         );
176                 
177                 return false;
178         }
179
180         if (std_dev > opt.max_std_dev_pixel_error) {
181                 note (
182                         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)
186                         );
187                 
188                 return false;
189         }
190
191         return true;
192 }
193
194 string
195 PictureMXF::key_type () const
196 {
197         return "MDIK";
198 }