No-op: whitespace.
[libdcp.git] / src / picture_asset.cc
1 /*
2     Copyright (C) 2012-2015 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_asset.h"
21 #include "util.h"
22 #include "exceptions.h"
23 #include "openjpeg_image.h"
24 #include "picture_asset_writer.h"
25 #include "dcp_assert.h"
26 #include "compose.hpp"
27 #include "AS_DCP.h"
28 #include "KM_fileio.h"
29 #include <libxml++/nodes/element.h>
30 #include <openjpeg.h>
31 #include <boost/filesystem.hpp>
32 #include <list>
33 #include <stdexcept>
34 #include <iostream>
35 #include <sstream>
36
37 using std::string;
38 using std::ostream;
39 using std::list;
40 using std::vector;
41 using std::max;
42 using std::stringstream;
43 using std::pair;
44 using std::make_pair;
45 using std::istream;
46 using std::cout;
47 using boost::shared_ptr;
48 using namespace dcp;
49
50 PictureAsset::PictureAsset (boost::filesystem::path file)
51         : Asset (file)
52         , _intrinsic_duration (0)
53 {
54
55 }
56
57 PictureAsset::PictureAsset (Fraction edit_rate)
58         : _edit_rate (edit_rate)
59         , _intrinsic_duration (0)
60 {
61
62 }
63
64 void
65 PictureAsset::read_picture_descriptor (ASDCP::JP2K::PictureDescriptor const & desc)
66 {
67         _size.width = desc.StoredWidth;
68         _size.height = desc.StoredHeight;
69         _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
70         _intrinsic_duration = desc.ContainerDuration;
71         _frame_rate = Fraction (desc.SampleRate.Numerator, desc.SampleRate.Denominator);
72         _screen_aspect_ratio = Fraction (desc.AspectRatio.Numerator, desc.AspectRatio.Denominator);
73 }
74
75 bool
76 PictureAsset::descriptor_equals (
77         ASDCP::JP2K::PictureDescriptor const & a, ASDCP::JP2K::PictureDescriptor const & b, NoteHandler note
78         ) const
79 {
80         if (
81                 a.EditRate != b.EditRate ||
82                 a.SampleRate != b.SampleRate ||
83                 a.StoredWidth != b.StoredWidth ||
84                 a.StoredHeight != b.StoredHeight ||
85                 a.AspectRatio != b.AspectRatio ||
86                 a.Rsize != b.Rsize ||
87                 a.Xsize != b.Xsize ||
88                 a.Ysize != b.Ysize ||
89                 a.XOsize != b.XOsize ||
90                 a.YOsize != b.YOsize ||
91                 a.XTsize != b.XTsize ||
92                 a.YTsize != b.YTsize ||
93                 a.XTOsize != b.XTOsize ||
94                 a.YTOsize != b.YTOsize ||
95                 a.Csize != b.Csize
96 //              a.CodingStyleDefault != b.CodingStyleDefault ||
97 //              a.QuantizationDefault != b.QuantizationDefault
98                 ) {
99
100                 note (DCP_ERROR, "video MXF picture descriptors differ");
101                 return false;
102         }
103
104         if (a.ContainerDuration != b.ContainerDuration) {
105                 note (DCP_ERROR, "video container durations differ");
106         }
107
108 //              for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
109 //                      if (a.ImageComponents[j] != b.ImageComponents[j]) {
110 //                              notes.pack_start ("video MXF picture descriptors differ");
111 //                      }
112 //              }
113
114         return true;
115 }
116
117 bool
118 PictureAsset::frame_buffer_equals (
119         int frame, EqualityOptions opt, NoteHandler note,
120         uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
121         ) const
122 {
123         if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
124                 note (DCP_NOTE, "J2K identical");
125                 /* Easy result; the J2K data is identical */
126                 return true;
127         }
128
129         /* Decompress the images to bitmaps */
130         shared_ptr<OpenJPEGImage> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
131         shared_ptr<OpenJPEGImage> image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
132
133         /* Compare them */
134
135         vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
136         int d = 0;
137         int max_diff = 0;
138
139         for (int c = 0; c < 3; ++c) {
140
141                 if (image_A->size() != image_B->size()) {
142                         note (DCP_ERROR, String::compose ("image sizes for frame %1 differ", frame));
143                         return false;
144                 }
145
146                 int const pixels = image_A->size().width * image_A->size().height;
147                 for (int j = 0; j < pixels; ++j) {
148                         int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
149                         abs_diffs[d++] = t;
150                         max_diff = max (max_diff, t);
151                 }
152         }
153
154         uint64_t total = 0;
155         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
156                 total += *j;
157         }
158
159         double const mean = double (total) / abs_diffs.size ();
160
161         uint64_t total_squared_deviation = 0;
162         for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
163                 total_squared_deviation += pow (*j - mean, 2);
164         }
165
166         double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
167
168         note (DCP_NOTE, String::compose ("mean difference %1, deviation %2", mean, std_dev));
169
170         if (mean > opt.max_mean_pixel_error) {
171                 note (
172                         DCP_ERROR,
173                         String::compose ("mean %1 out of range %2 in frame %3", mean, opt.max_mean_pixel_error, frame)
174                         );
175
176                 return false;
177         }
178
179         if (std_dev > opt.max_std_dev_pixel_error) {
180                 note (
181                         DCP_ERROR,
182                         String::compose ("standard deviation %1 out of range %2 in frame %3", std_dev, opt.max_std_dev_pixel_error, frame)
183                         );
184
185                 return false;
186         }
187
188         return true;
189 }
190
191 string
192 PictureAsset::pkl_type (Standard standard) const
193 {
194         switch (standard) {
195         case INTEROP:
196                 return "application/x-smpte-mxf;asdcpKind=Picture";
197         case SMPTE:
198                 return "application/mxf";
199         default:
200                 DCP_ASSERT (false);
201         }
202 }