8d3b0347fe144a9ef8c3f8acd4cfaa6f4800f631
[libdcp.git] / src / picture_asset.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 /** @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 #include "raw_convert.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, boost::filesystem::path mxf_name)
57         : MXFAsset (directory, mxf_name)
58 {
59
60 }
61
62 void
63 PictureAsset::write_to_cpl (xmlpp::Element* node) const
64 {
65         MXFAsset::write_to_cpl (node);
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 (raw_convert<string> (_edit_rate * edit_rate_factor ()) + " 1");
76         if (_interop) {
77                 stringstream s;
78                 s << std::fixed << std::setprecision (2) << (float (_size.width) / _size.height);
79                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (s.str ());
80         } else {
81                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (raw_convert<string> (_size.width) + " " + raw_convert<string> (_size.height));
82         }
83 }
84
85 bool
86 PictureAsset::descriptor_equals (
87         ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, boost::function<void (NoteType, string)> note
88         ) const
89 {
90         if (
91                 a.EditRate != b.EditRate ||
92                 a.SampleRate != b.SampleRate ||
93                 a.StoredWidth != b.StoredWidth ||
94                 a.StoredHeight != b.StoredHeight ||
95                 a.AspectRatio != b.AspectRatio ||
96                 a.Rsize != b.Rsize ||
97                 a.Xsize != b.Xsize ||
98                 a.Ysize != b.Ysize ||
99                 a.XOsize != b.XOsize ||
100                 a.YOsize != b.YOsize ||
101                 a.XTsize != b.XTsize ||
102                 a.YTsize != b.YTsize ||
103                 a.XTOsize != b.XTOsize ||
104                 a.YTOsize != b.YTOsize ||
105                 a.Csize != b.Csize
106 //              a.CodingStyleDefault != b.CodingStyleDefault ||
107 //              a.QuantizationDefault != b.QuantizationDefault
108                 ) {
109                 
110                 note (ERROR, "video MXF picture descriptors differ");
111                 return false;
112         }
113
114         if (a.ContainerDuration != b.ContainerDuration) {
115                 note (ERROR, "video container durations differ");
116         }
117         
118 //              for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
119 //                      if (a.ImageComponents[j] != b.ImageComponents[j]) {
120 //                              notes.pack_start ("video MXF picture descriptors differ");
121 //                      }
122 //              }
123
124         return true;
125 }
126
127 bool
128 PictureAsset::frame_buffer_equals (
129         int frame, EqualityOptions opt, boost::function<void (NoteType, string)> note,
130         uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
131         ) const
132 {
133         if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
134                 note (NOTE, "J2K identical");
135                 /* Easy result; the J2K data is identical */
136                 return true;
137         }
138                 
139         /* Decompress the images to bitmaps */
140         shared_ptr<XYZFrame> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
141         shared_ptr<XYZFrame> image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
142         
143         /* Compare them */
144         
145         vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
146         int d = 0;
147         int max_diff = 0;
148         
149         for (int c = 0; c < 3; ++c) {
150                 
151                 if (image_A->size() != image_B->size()) {
152                         note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
153                         return false;
154                 }
155                 
156                 int const pixels = image_A->size().width * image_A->size().height;
157                 for (int j = 0; j < pixels; ++j) {
158                         int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
159                         abs_diffs[d++] = t;
160                         max_diff = max (max_diff, t);
161                 }
162         }
163                 
164         uint64_t total = 0;
165         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
166                 total += *j;
167         }
168         
169         double const mean = double (total) / abs_diffs.size ();
170         
171         uint64_t total_squared_deviation = 0;
172         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
173                 total_squared_deviation += pow (*j - mean, 2);
174         }
175         
176         double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
177         
178         note (NOTE, "mean difference " + lexical_cast<string> (mean) + ", deviation " + lexical_cast<string> (std_dev));
179         
180         if (mean > opt.max_mean_pixel_error) {
181                 note (
182                         ERROR,
183                         "mean " + lexical_cast<string>(mean) +
184                         " out of range " + lexical_cast<string>(opt.max_mean_pixel_error) +
185                         " in frame " + lexical_cast<string>(frame)
186                         );
187                 
188                 return false;
189         }
190
191         if (std_dev > opt.max_std_dev_pixel_error) {
192                 note (
193                         ERROR,
194                         "standard deviation " + lexical_cast<string>(std_dev) +
195                         " out of range " + lexical_cast<string>(opt.max_std_dev_pixel_error) +
196                         " in frame " + lexical_cast<string>(frame)
197                         );
198                 
199                 return false;
200         }
201
202         return true;
203 }
204
205 string
206 PictureAsset::key_type () const
207 {
208         return "MDIK";
209 }
210
211
212