Tweak to build onboost filesystem v2
[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 <boost/filesystem.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include "AS_DCP.h"
31 #include "KM_fileio.h"
32 #include "picture_asset.h"
33 #include "util.h"
34 #include "exceptions.h"
35
36 using namespace std;
37 using namespace boost;
38 using namespace libdcp;
39
40 PictureAsset::PictureAsset (
41         sigc::slot<string, int> get_path,
42         string directory,
43         string mxf_name,
44         sigc::signal1<void, float>* progress,
45         int fps,
46         int length,
47         int width,
48         int height)
49         : Asset (directory, mxf_name, progress, fps, length)
50         , _width (width)
51         , _height (height)
52 {
53         construct (get_path);
54 }
55
56 PictureAsset::PictureAsset (
57         vector<string> const & files,
58         string directory,
59         string mxf_name,
60         sigc::signal1<void, float>* progress,
61         int fps,
62         int length,
63         int width,
64         int height)
65         : Asset (directory, mxf_name, progress, fps, length)
66         , _width (width)
67         , _height (height)
68 {
69         construct (sigc::bind (sigc::mem_fun (*this, &PictureAsset::path_from_list), files));
70 }
71
72 PictureAsset::PictureAsset (string directory, string mxf_name, int fps, int length, int width, int height)
73         : Asset (directory, mxf_name, 0, fps, length)
74         , _width (width)
75         , _height (height)
76 {
77
78 }
79
80 string
81 PictureAsset::path_from_list (int f, vector<string> const & files) const
82 {
83         return files[f];
84 }
85
86 void
87 PictureAsset::construct (sigc::slot<string, int> get_path)
88 {
89         ASDCP::JP2K::CodestreamParser j2k_parser;
90         ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
91         if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
92                 throw FileError ("could not open JPEG2000 file for reading", get_path (0));
93         }
94         
95         ASDCP::JP2K::PictureDescriptor picture_desc;
96         j2k_parser.FillPictureDescriptor (picture_desc);
97         picture_desc.EditRate = ASDCP::Rational (_fps, 1);
98         
99         ASDCP::WriterInfo writer_info;
100         fill_writer_info (&writer_info);
101         
102         ASDCP::JP2K::MXFWriter mxf_writer;
103 #if BOOST_FILESYSTEM_VERSION == 3       
104         if (ASDCP_FAILURE (mxf_writer.OpenWrite (mxf_path().c_str(), writer_info, picture_desc))) {
105 #else           
106         if (ASDCP_FAILURE (mxf_writer.OpenWrite (mxf_path().string().c_str(), writer_info, picture_desc))) {
107 #endif          
108                 throw FileError ("could not open MXF file for writing", mxf_path().string());
109         }
110
111         for (int i = 0; i < _length; ++i) {
112
113                 string const path = get_path (i);
114                 
115                 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
116                         throw FileError ("could not open JPEG2000 file for reading", path);
117                 }
118
119                 /* XXX: passing 0 to WriteFrame ok? */
120                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
121                         throw MiscError ("error in writing video MXF");
122                 }
123                 
124                 (*_progress) (0.5 * float (i) / _length);
125         }
126         
127         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
128                 throw MiscError ("error in finalising video MXF");
129         }
130 }
131
132 void
133 PictureAsset::write_to_cpl (ostream& s) const
134 {
135         s << "        <MainPicture>\n"
136           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
137           << "          <AnnotationText>" << _mxf_name << "</AnnotationText>\n"
138           << "          <EditRate>" << _fps << " 1</EditRate>\n"
139           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
140           << "          <EntryPoint>0</EntryPoint>\n"
141           << "          <Duration>" << _length << "</Duration>\n"
142           << "          <FrameRate>" << _fps << " 1</FrameRate>\n"
143           << "          <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
144           << "        </MainPicture>\n";
145 }
146
147 list<string>
148 PictureAsset::equals (shared_ptr<const Asset> other, EqualityFlags flags) const
149 {
150         list<string> notes = Asset::equals (other, flags);
151                      
152         if (flags & MXF_INSPECT) {
153                 ASDCP::JP2K::MXFReader reader_A;
154 #if BOOST_FILESYSTEM_VERSION == 3               
155                 if (ASDCP_FAILURE (reader_A.OpenRead (mxf_path().c_str()))) {
156 #else                   
157                 if (ASDCP_FAILURE (reader_A.OpenRead (mxf_path().string().c_str()))) {
158 #endif                  
159                         throw FileError ("could not open MXF file for reading", mxf_path().string());
160                 }
161
162                 ASDCP::JP2K::MXFReader reader_B;
163 #if BOOST_FILESYSTEM_VERSION == 3               
164                 if (ASDCP_FAILURE (reader_B.OpenRead (other->mxf_path().c_str()))) {
165 #else                   
166                 if (ASDCP_FAILURE (reader_B.OpenRead (other->mxf_path().string().c_str()))) {
167 #endif                  
168                         throw FileError ("could not open MXF file for reading", mxf_path().string());
169                 }
170
171                 ASDCP::JP2K::PictureDescriptor desc_A;
172                 if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
173                         throw DCPReadError ("could not read video MXF information");
174                 }
175                 ASDCP::JP2K::PictureDescriptor desc_B;
176                 if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
177                         throw DCPReadError ("could not read video MXF information");
178                 }
179
180                 if (
181                         desc_A.EditRate != desc_B.EditRate ||
182                         desc_A.ContainerDuration != desc_B.ContainerDuration ||
183                         desc_A.SampleRate != desc_B.SampleRate ||
184                         desc_A.StoredWidth != desc_B.StoredWidth ||
185                         desc_A.StoredHeight != desc_B.StoredHeight ||
186                         desc_A.AspectRatio != desc_B.AspectRatio ||
187                         desc_A.Rsize != desc_B.Rsize ||
188                         desc_A.Xsize != desc_B.Xsize ||
189                         desc_A.Ysize != desc_B.Ysize ||
190                         desc_A.XOsize != desc_B.XOsize ||
191                         desc_A.YOsize != desc_B.YOsize ||
192                         desc_A.XTsize != desc_B.XTsize ||
193                         desc_A.YTsize != desc_B.YTsize ||
194                         desc_A.XTOsize != desc_B.XTOsize ||
195                         desc_A.YTOsize != desc_B.YTOsize ||
196                         desc_A.Csize != desc_B.Csize
197 //                      desc_A.CodingStyleDefault != desc_B.CodingStyleDefault ||
198 //                      desc_A.QuantizationDefault != desc_B.QuantizationDefault
199                         ) {
200                 
201                         notes.push_back ("video MXF picture descriptors differ");
202                 }
203
204 //              for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
205 //                      if (desc_A.ImageComponents[j] != desc_B.ImageComponents[j]) {
206 //                              notes.pack_start ("video MXF picture descriptors differ");
207 //                      }
208 //              }
209                                 
210
211                 ASDCP::JP2K::FrameBuffer buffer_A (4 * Kumu::Megabyte);
212                 ASDCP::JP2K::FrameBuffer buffer_B (4 * Kumu::Megabyte);
213
214                 for (int i = 0; i < _length; ++i) {
215                         if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
216                                 throw DCPReadError ("could not read video frame");
217                         }
218
219                         if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
220                                 throw DCPReadError ("could not read video frame");
221                         }
222
223                         if (buffer_A.Size() != buffer_B.Size()) {
224                                 notes.push_back ("sizes of video data for frame " + lexical_cast<string>(i) + " differ");
225                                 continue;
226                         }
227
228                         if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
229                                 notes.push_back ("J2K data for frame " + lexical_cast<string>(i) + " differ");
230                                 continue;
231                         }
232                 }
233         }
234
235         return notes;
236 }