Some maths operations with Time.
[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 "AS_DCP.h"
33 #include "KM_fileio.h"
34 #include "picture_asset.h"
35 #include "util.h"
36 #include "exceptions.h"
37 #include "picture_frame.h"
38
39 using namespace std;
40 using namespace boost;
41 using namespace libdcp;
42
43 PictureAsset::PictureAsset (
44         sigc::slot<string, int> get_path,
45         string directory,
46         string mxf_name,
47         sigc::signal1<void, float>* progress,
48         int fps,
49         int length,
50         int width,
51         int height)
52         : MXFAsset (directory, mxf_name, progress, fps, length)
53         , _width (width)
54         , _height (height)
55 {
56         construct (get_path);
57 }
58
59 PictureAsset::PictureAsset (
60         vector<string> const & files,
61         string directory,
62         string mxf_name,
63         sigc::signal1<void, float>* progress,
64         int fps,
65         int length,
66         int width,
67         int height)
68         : MXFAsset (directory, mxf_name, progress, fps, length)
69         , _width (width)
70         , _height (height)
71 {
72         construct (sigc::bind (sigc::mem_fun (*this, &PictureAsset::path_from_list), files));
73 }
74
75 PictureAsset::PictureAsset (string directory, string mxf_name, int fps, int length)
76         : MXFAsset (directory, mxf_name, 0, fps, length)
77 {
78         ASDCP::JP2K::MXFReader reader;
79         if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
80                 throw FileError ("could not open MXF file for reading", path().string());
81         }
82         
83         ASDCP::JP2K::PictureDescriptor desc;
84         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
85                 throw DCPReadError ("could not read video MXF information");
86         }
87
88         _width = desc.StoredWidth;
89         _height = desc.StoredHeight;
90
91 }
92
93 string
94 PictureAsset::path_from_list (int f, vector<string> const & files) const
95 {
96         return files[f];
97 }
98
99 void
100 PictureAsset::construct (sigc::slot<string, int> get_path)
101 {
102         ASDCP::JP2K::CodestreamParser j2k_parser;
103         ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
104         if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
105                 throw FileError ("could not open JPEG2000 file for reading", get_path (0));
106         }
107         
108         ASDCP::JP2K::PictureDescriptor picture_desc;
109         j2k_parser.FillPictureDescriptor (picture_desc);
110         picture_desc.EditRate = ASDCP::Rational (_fps, 1);
111         
112         ASDCP::WriterInfo writer_info;
113         fill_writer_info (&writer_info);
114         
115         ASDCP::JP2K::MXFWriter mxf_writer;
116         if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc))) {
117                 throw FileError ("could not open MXF file for writing", path().string());
118         }
119
120         for (int i = 0; i < _length; ++i) {
121
122                 string const path = get_path (i);
123                 
124                 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
125                         throw FileError ("could not open JPEG2000 file for reading", path);
126                 }
127
128                 /* XXX: passing 0 to WriteFrame ok? */
129                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
130                         throw MiscError ("error in writing video MXF");
131                 }
132                 
133                 (*_progress) (0.5 * float (i) / _length);
134         }
135         
136         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
137                 throw MiscError ("error in finalising video MXF");
138         }
139 }
140
141 void
142 PictureAsset::write_to_cpl (ostream& s) const
143 {
144         s << "        <MainPicture>\n"
145           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
146           << "          <AnnotationText>" << _file_name << "</AnnotationText>\n"
147           << "          <EditRate>" << _fps << " 1</EditRate>\n"
148           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
149           << "          <EntryPoint>0</EntryPoint>\n"
150           << "          <Duration>" << _length << "</Duration>\n"
151           << "          <FrameRate>" << _fps << " 1</FrameRate>\n"
152           << "          <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
153           << "        </MainPicture>\n";
154 }
155
156 /* XXX: could use get_frame()? */
157 list<string>
158 PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt) const
159 {
160         list<string> notes = MXFAsset::equals (other, opt);
161                      
162         if (opt.flags & MXF_INSPECT) {
163                 ASDCP::JP2K::MXFReader reader_A;
164                 if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
165                         throw FileError ("could not open MXF file for reading", path().string());
166                 }
167
168                 ASDCP::JP2K::MXFReader reader_B;
169                 if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
170                         throw FileError ("could not open MXF file for reading", path().string());
171                 }
172
173                 ASDCP::JP2K::PictureDescriptor desc_A;
174                 if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
175                         throw DCPReadError ("could not read video MXF information");
176                 }
177                 ASDCP::JP2K::PictureDescriptor desc_B;
178                 if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
179                         throw DCPReadError ("could not read video MXF information");
180                 }
181
182                 if (
183                         desc_A.EditRate != desc_B.EditRate ||
184                         desc_A.ContainerDuration != desc_B.ContainerDuration ||
185                         desc_A.SampleRate != desc_B.SampleRate ||
186                         desc_A.StoredWidth != desc_B.StoredWidth ||
187                         desc_A.StoredHeight != desc_B.StoredHeight ||
188                         desc_A.AspectRatio != desc_B.AspectRatio ||
189                         desc_A.Rsize != desc_B.Rsize ||
190                         desc_A.Xsize != desc_B.Xsize ||
191                         desc_A.Ysize != desc_B.Ysize ||
192                         desc_A.XOsize != desc_B.XOsize ||
193                         desc_A.YOsize != desc_B.YOsize ||
194                         desc_A.XTsize != desc_B.XTsize ||
195                         desc_A.YTsize != desc_B.YTsize ||
196                         desc_A.XTOsize != desc_B.XTOsize ||
197                         desc_A.YTOsize != desc_B.YTOsize ||
198                         desc_A.Csize != desc_B.Csize
199 //                      desc_A.CodingStyleDefault != desc_B.CodingStyleDefault ||
200 //                      desc_A.QuantizationDefault != desc_B.QuantizationDefault
201                         ) {
202                 
203                         notes.push_back ("video MXF picture descriptors differ");
204                 }
205
206 //              for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
207 //                      if (desc_A.ImageComponents[j] != desc_B.ImageComponents[j]) {
208 //                              notes.pack_start ("video MXF picture descriptors differ");
209 //                      }
210 //              }
211                                 
212
213                 ASDCP::JP2K::FrameBuffer buffer_A (4 * Kumu::Megabyte);
214                 ASDCP::JP2K::FrameBuffer buffer_B (4 * Kumu::Megabyte);
215
216                 for (int i = 0; i < _length; ++i) {
217                         if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
218                                 throw DCPReadError ("could not read video frame");
219                         }
220
221                         if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
222                                 throw DCPReadError ("could not read video frame");
223                         }
224
225                         bool j2k_same = true;
226
227                         if (buffer_A.Size() != buffer_B.Size()) {
228                                 notes.push_back ("sizes of video data for frame " + lexical_cast<string>(i) + " differ");
229                                 j2k_same = false;
230                         } else if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
231                                 notes.push_back ("J2K data for frame " + lexical_cast<string>(i) + " differ");
232                                 j2k_same = false;
233                         }
234
235                         if (!j2k_same) {
236
237                                 if (opt.verbose) {
238                                         cout << "J2K images for " << i << " differ; checking by pixel\n";
239                                 }
240                                 
241                                 /* Decompress the images to bitmaps */
242                                 opj_image_t* image_A = decompress_j2k (const_cast<uint8_t*> (buffer_A.RoData()), buffer_A.Size ());
243                                 opj_image_t* image_B = decompress_j2k (const_cast<uint8_t*> (buffer_B.RoData()), buffer_B.Size ());
244
245                                 /* Compare them */
246                                 
247                                 if (image_A->numcomps != image_B->numcomps) {
248                                         notes.push_back ("image component counts for frame " + lexical_cast<string>(i) + " differ");
249                                 }
250
251                                 vector<int> abs_diffs (image_A->comps[0].w * image_A->comps[0].h * image_A->numcomps);
252                                 int d = 0;
253                                 int max_diff = 0;
254
255                                 for (int c = 0; c < image_A->numcomps; ++c) {
256
257                                         if (image_A->comps[c].w != image_B->comps[c].w || image_A->comps[c].h != image_B->comps[c].h) {
258                                                 notes.push_back ("image sizes for frame " + lexical_cast<string>(i) + " differ");
259                                         }
260
261                                         int const pixels = image_A->comps[c].w * image_A->comps[c].h;
262                                         for (int j = 0; j < pixels; ++j) {
263                                                 int const t = abs (image_A->comps[c].data[j] - image_B->comps[c].data[j]);
264                                                 abs_diffs[d++] = t;
265                                                 max_diff = max (max_diff, t);
266                                         }
267                                 }
268
269                                 uint64_t total = 0;
270                                 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
271                                         total += *j;
272                                 }
273
274                                 double const mean = double (total) / abs_diffs.size ();
275
276                                 uint64_t total_squared_deviation = 0;
277                                 for (vector<int>::iterator j = abs_diffs.begin(); j != abs_diffs.end(); ++j) {
278                                         total_squared_deviation += pow (*j - mean, 2);
279                                 }
280
281                                 double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
282
283                                 if (mean > opt.max_mean_pixel_error || std_dev > opt.max_std_dev_pixel_error) {
284                                         notes.push_back ("mean or standard deviation out of range for " + lexical_cast<string>(i));
285                                 }
286
287                                 if (opt.verbose) {
288                                         cout << "\tmax pixel error " << max_diff << ", mean pixel error " << mean << ", standard deviation " << std_dev << "\n";
289                                 }
290
291                                 opj_image_destroy (image_A);
292                                 opj_image_destroy (image_B);
293                         }
294                 }
295         }
296
297         return notes;
298 }
299
300 opj_image_t *
301 PictureAsset::decompress_j2k (uint8_t* data, int64_t size) const
302 {
303         opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
304         opj_dparameters_t parameters;
305         opj_set_default_decoder_parameters (&parameters);
306         opj_setup_decoder (decoder, &parameters);
307         opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
308         opj_image_t* image = opj_decode (decoder, cio);
309         if (!image) {
310                 opj_destroy_decompress (decoder);
311                 opj_cio_close (cio);
312                 throw DCPReadError ("could not decode JPEG2000 codestream");
313         }
314
315         opj_cio_close (cio);
316         return image;
317 }
318
319 shared_ptr<const PictureFrame>
320 PictureAsset::get_frame (int n) const
321 {
322         return shared_ptr<const PictureFrame> (new PictureFrame (path().string(), n));
323 }