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