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