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