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