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