699cfc230ea335b7e9fe3e87987609e065de79ac
[libdcp.git] / src / mono_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 "mono_picture_mxf.h"
21 #include "mono_picture_mxf_writer.h"
22 #include "AS_DCP.h"
23 #include "KM_fileio.h"
24 #include "exceptions.h"
25 #include "mono_picture_frame.h"
26
27 using std::string;
28 using std::vector;
29 using boost::shared_ptr;
30 using boost::dynamic_pointer_cast;
31 using boost::lexical_cast;
32 using namespace dcp;
33
34 MonoPictureMXF::MonoPictureMXF (boost::filesystem::path file)
35         : PictureMXF (file)
36 {
37         ASDCP::JP2K::MXFReader reader;
38         Kumu::Result_t r = reader.OpenRead (file.string().c_str());
39         if (ASDCP_FAILURE (r)) {
40                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file.string(), r));
41         }
42         
43         ASDCP::JP2K::PictureDescriptor desc;
44         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
45                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
46         }
47
48         read_picture_descriptor (desc);
49 }
50
51 MonoPictureMXF::MonoPictureMXF (Fraction edit_rate)
52         : PictureMXF (edit_rate)
53 {
54         
55 }
56
57 shared_ptr<const MonoPictureFrame>
58 MonoPictureMXF::get_frame (int n) const
59 {
60         return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (_file, n, _decryption_context));
61 }
62
63 bool
64 MonoPictureMXF::equals (shared_ptr<const Content> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
65 {
66         if (!MXF::equals (other, opt, note)) {
67                 return false;
68         }
69
70         ASDCP::JP2K::MXFReader reader_A;
71         Kumu::Result_t r = reader_A.OpenRead (_file.string().c_str());
72         if (ASDCP_FAILURE (r)) {
73                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", _file.string(), r));
74         }
75         
76         ASDCP::JP2K::MXFReader reader_B;
77         r = reader_B.OpenRead (other->file().string().c_str());
78         if (ASDCP_FAILURE (r)) {
79                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", other->file().string(), r));
80         }
81         
82         ASDCP::JP2K::PictureDescriptor desc_A;
83         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
84                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
85         }
86         ASDCP::JP2K::PictureDescriptor desc_B;
87         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
88                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
89         }
90         
91         if (!descriptor_equals (desc_A, desc_B, note)) {
92                 return false;
93         }
94
95         shared_ptr<const MonoPictureMXF> other_picture = dynamic_pointer_cast<const MonoPictureMXF> (other);
96         assert (other_picture);
97
98         for (int i = 0; i < _intrinsic_duration; ++i) {
99                 if (i >= other_picture->intrinsic_duration()) {
100                         return false;
101                 }
102                 
103                 note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
104                 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
105                 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
106                 
107                 if (!frame_buffer_equals (
108                             i, opt, note,
109                             frame_A->j2k_data(), frame_A->j2k_size(),
110                             frame_B->j2k_data(), frame_B->j2k_size()
111                             )) {
112                         return false;
113                 }
114         }
115
116         return true;
117 }
118
119 shared_ptr<PictureMXFWriter>
120 MonoPictureMXF::start_write (boost::filesystem::path file, Standard standard, bool overwrite)
121 {
122         /* XXX: can't we use shared_ptr here? */
123         return shared_ptr<MonoPictureMXFWriter> (new MonoPictureMXFWriter (this, file, standard, overwrite));
124 }
125
126 string
127 MonoPictureMXF::cpl_node_name () const
128 {
129         return "MainPicture";
130 }
131
132 int
133 MonoPictureMXF::edit_rate_factor () const
134 {
135         return 1;
136 }