Some more boost::filesystem::path.
[libdcp.git] / src / stereo_picture_asset.cc
1 /*
2     Copyright (C) 2012-2013 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 "AS_DCP.h"
21 #include "stereo_picture_asset.h"
22 #include "stereo_picture_frame.h"
23 #include "exceptions.h"
24 #include "stereo_picture_asset_writer.h"
25
26 using std::string;
27 using std::pair;
28 using std::make_pair;
29 using boost::shared_ptr;
30 using boost::dynamic_pointer_cast;
31 using namespace libdcp;
32
33 bool
34 StereoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
35 {
36         if (!MXFAsset::equals (other, opt, note)) {
37                 return false;
38         }
39
40         ASDCP::JP2K::MXFSReader reader_A;
41         if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
42                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
43         }
44         
45         ASDCP::JP2K::MXFSReader reader_B;
46         if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
47                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
48         }
49         
50         ASDCP::JP2K::PictureDescriptor desc_A;
51         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
52                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
53         }
54         ASDCP::JP2K::PictureDescriptor desc_B;
55         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
56                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
57         }
58         
59         if (!descriptor_equals (desc_A, desc_B, note)) {
60                 return false;
61         }
62         
63         shared_ptr<const StereoPictureAsset> other_picture = dynamic_pointer_cast<const StereoPictureAsset> (other);
64         assert (other_picture);
65
66         for (int i = 0; i < _intrinsic_duration; ++i) {
67                 shared_ptr<const StereoPictureFrame> frame_A = get_frame (i);
68                 shared_ptr<const StereoPictureFrame> frame_B = other_picture->get_frame (i);
69                 
70                 if (!frame_buffer_equals (
71                             i, opt, note,
72                             frame_A->left_j2k_data(), frame_A->left_j2k_size(),
73                             frame_B->left_j2k_data(), frame_B->left_j2k_size()
74                             )) {
75                         return false;
76                 }
77                 
78                 if (!frame_buffer_equals (
79                             i, opt, note,
80                             frame_A->right_j2k_data(), frame_A->right_j2k_size(),
81                             frame_B->right_j2k_data(), frame_B->right_j2k_size()
82                             )) {
83                         return false;
84                 }
85         }
86
87         return true;
88 }
89
90 StereoPictureAsset::StereoPictureAsset (boost::filesystem::path directory, boost::filesystem::path mxf_name)
91         : PictureAsset (directory, mxf_name)
92 {
93         
94 }
95
96 void
97 StereoPictureAsset::read ()
98 {
99         ASDCP::JP2K::MXFSReader reader;
100         if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
101                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
102         }
103         
104         ASDCP::JP2K::PictureDescriptor desc;
105         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
106                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
107         }
108
109         _size.width = desc.StoredWidth;
110         _size.height = desc.StoredHeight;
111 }
112
113 shared_ptr<const StereoPictureFrame>
114 StereoPictureAsset::get_frame (int n) const
115 {
116         return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n));
117 }
118
119 shared_ptr<PictureAssetWriter>
120 StereoPictureAsset::start_write (bool overwrite)
121 {
122         return shared_ptr<StereoPictureAssetWriter> (new StereoPictureAssetWriter (this, overwrite));
123 }
124
125 string
126 StereoPictureAsset::cpl_node_name () const
127 {
128         return "msp-cpl:MainStereoscopicPicture";
129 }
130
131 pair<string, string>
132 StereoPictureAsset::cpl_node_attribute (bool interop) const
133 {
134         if (interop) {
135                 return make_pair ("xmlns:msp-cpl", "http://www.digicine.com/schemas/437-Y/2007/Main-Stereo-Picture-CPL");
136         } else {
137                 return make_pair ("xmlns:msp-cpl", "http://www.smpte-ra.org/schemas/429-10/2008/Main-Stereo-Picture-CPL");
138         }
139
140         return make_pair ("", "");
141 }
142
143 int
144 StereoPictureAsset::edit_rate_factor () const
145 {
146         return 2;
147 }