Fix precision of ScreenAspectRatio tags in Interop mode.
[libdcp.git] / src / picture_asset.cc
1 /*
2     Copyright (C) 2012 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 /** @file  src/picture_asset.cc
21  *  @brief An asset made up of JPEG2000 files
22  */
23
24 #include <list>
25 #include <stdexcept>
26 #include <iostream>
27 #include <sstream>
28 #include <boost/filesystem.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <openjpeg.h>
31 #include <libxml++/nodes/element.h>
32 #include "AS_DCP.h"
33 #include "KM_fileio.h"
34 #include "picture_asset.h"
35 #include "util.h"
36 #include "exceptions.h"
37 #include "xyz_frame.h"
38 #include "picture_asset_writer.h"
39
40 using std::string;
41 using std::ostream;
42 using std::list;
43 using std::vector;
44 using std::max;
45 using std::stringstream;
46 using std::pair;
47 using std::make_pair;
48 using std::istream;
49 using std::cout;
50 using boost::shared_ptr;
51 using boost::dynamic_pointer_cast;
52 using boost::lexical_cast;
53 using namespace libdcp;
54
55 PictureAsset::PictureAsset (boost::filesystem::path directory, boost::filesystem::path mxf_name)
56         : MXFAsset (directory, mxf_name)
57 {
58
59 }
60
61 void
62 PictureAsset::write_to_cpl (xmlpp::Element* node, bool interop) const
63 {
64         MXFAsset::write_to_cpl (node, interop);
65         
66         xmlpp::Node::NodeList c = node->get_children ();
67         xmlpp::Node::NodeList::iterator i = c.begin();
68         while (i != c.end() && (*i)->get_name() != cpl_node_name ()) {
69                 ++i;
70         }
71
72         assert (i != c.end ());
73
74         (*i)->add_child ("FrameRate")->add_child_text (lexical_cast<string> (_edit_rate * edit_rate_factor ()) + " 1");
75         if (interop) {
76                 stringstream s;
77                 s << std::fixed << std::setprecision (2) << (float (_size.width) / _size.height);
78                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (s.str ());
79         } else {
80                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (lexical_cast<string> (_size.width) + " " + lexical_cast<string> (_size.height));
81         }
82 }
83
84 bool
85 PictureAsset::descriptor_equals (
86         ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, boost::function<void (NoteType, string)> note
87         ) const
88 {
89         if (
90                 a.EditRate != b.EditRate ||
91                 a.SampleRate != b.SampleRate ||
92                 a.StoredWidth != b.StoredWidth ||
93                 a.StoredHeight != b.StoredHeight ||
94                 a.AspectRatio != b.AspectRatio ||
95                 a.Rsize != b.Rsize ||
96                 a.Xsize != b.Xsize ||
97                 a.Ysize != b.Ysize ||
98                 a.XOsize != b.XOsize ||
99                 a.YOsize != b.YOsize ||
100                 a.XTsize != b.XTsize ||
101                 a.YTsize != b.YTsize ||
102                 a.XTOsize != b.XTOsize ||
103                 a.YTOsize != b.YTOsize ||
104                 a.Csize != b.Csize
105 //              a.CodingStyleDefault != b.CodingStyleDefault ||
106 //              a.QuantizationDefault != b.QuantizationDefault
107                 ) {
108                 
109                 note (ERROR, "video MXF picture descriptors differ");
110                 return false;
111         }
112
113         if (a.ContainerDuration != b.ContainerDuration) {
114                 note (ERROR, "video container durations differ");
115         }
116         
117 //              for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
118 //                      if (a.ImageComponents[j] != b.ImageComponents[j]) {
119 //                              notes.pack_start ("video MXF picture descriptors differ");
120 //                      }
121 //              }
122
123         return true;
124 }
125
126 bool
127 PictureAsset::frame_buffer_equals (
128         int frame, EqualityOptions opt, boost::function<void (NoteType, string)> note,
129         uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
130         ) const
131 {
132         if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
133                 note (NOTE, "J2K identical");
134                 /* Easy result; the J2K data is identical */
135                 return true;
136         }
137                 
138         /* Decompress the images to bitmaps */
139         shared_ptr<XYZFrame> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
140         shared_ptr<XYZFrame> image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
141         
142         /* Compare them */
143         
144         vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
145         int d = 0;
146         int max_diff = 0;
147         
148         for (int c = 0; c < 3; ++c) {
149                 
150                 if (image_A->size() != image_B->size()) {
151                         note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
152                         return false;
153                 }
154                 
155                 int const pixels = image_A->size().width * image_A->size().height;
156                 for (int j = 0; j < pixels; ++j) {
157                         int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
158                         abs_diffs[d++] = t;
159                         max_diff = max (max_diff, t);
160                 }
161         }
162                 
163         uint64_t total = 0;
164         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
165                 total += *j;
166         }
167         
168         double const mean = double (total) / abs_diffs.size ();
169         
170         uint64_t total_squared_deviation = 0;
171         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
172                 total_squared_deviation += pow (*j - mean, 2);
173         }
174         
175         double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
176         
177         note (NOTE, "mean difference " + lexical_cast<string> (mean) + ", deviation " + lexical_cast<string> (std_dev));
178         
179         if (mean > opt.max_mean_pixel_error) {
180                 note (
181                         ERROR,
182                         "mean " + lexical_cast<string>(mean) +
183                         " out of range " + lexical_cast<string>(opt.max_mean_pixel_error) +
184                         " in frame " + lexical_cast<string>(frame)
185                         );
186                 
187                 return false;
188         }
189
190         if (std_dev > opt.max_std_dev_pixel_error) {
191                 note (
192                         ERROR,
193                         "standard deviation " + lexical_cast<string>(std_dev) +
194                         " out of range " + lexical_cast<string>(opt.max_std_dev_pixel_error) +
195                         " in frame " + lexical_cast<string>(frame)
196                         );
197                 
198                 return false;
199         }
200
201         return true;
202 }
203
204 string
205 PictureAsset::key_type () const
206 {
207         return "MDIK";
208 }
209
210
211