Fix build.
[libdcp.git] / src / mono_picture_asset.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_asset.h"
21 #include "mono_picture_asset_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::list;
32 using std::pair;
33 using boost::shared_ptr;
34 using boost::dynamic_pointer_cast;
35 using namespace dcp;
36
37 MonoPictureAsset::MonoPictureAsset (boost::filesystem::path file)
38         : PictureAsset (file)
39 {
40         ASDCP::JP2K::MXFReader reader;
41         Kumu::Result_t r = reader.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::PictureDescriptor desc;
47         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
48                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
49         }
50
51         read_picture_descriptor (desc);
52
53         ASDCP::WriterInfo info;
54         if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
55                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
56         }
57
58         _id = read_writer_info (info);
59 }
60
61 MonoPictureAsset::MonoPictureAsset (Fraction edit_rate)
62         : PictureAsset (edit_rate)
63 {
64
65 }
66
67 shared_ptr<const MonoPictureFrame>
68 MonoPictureAsset::get_frame (int n) const
69 {
70         return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (_file, n, _decryption_context));
71 }
72
73 static void
74 storing_note_handler (list<pair<NoteType, string> >& notes, NoteType t, string s)
75 {
76         notes.push_back (make_pair (t, s));
77 }
78
79 bool
80 MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
81 {
82         if (!dynamic_pointer_cast<const MonoPictureAsset> (other)) {
83                 return false;
84         }
85
86         ASDCP::JP2K::MXFReader reader_A;
87         Kumu::Result_t r = reader_A.OpenRead (_file.string().c_str());
88         if (ASDCP_FAILURE (r)) {
89                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", _file.string(), r));
90         }
91
92         ASDCP::JP2K::MXFReader reader_B;
93         r = reader_B.OpenRead (other->file().string().c_str());
94         if (ASDCP_FAILURE (r)) {
95                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", other->file().string(), r));
96         }
97
98         ASDCP::JP2K::PictureDescriptor desc_A;
99         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
100                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
101         }
102         ASDCP::JP2K::PictureDescriptor desc_B;
103         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
104                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
105         }
106
107         if (!descriptor_equals (desc_A, desc_B, note)) {
108                 return false;
109         }
110
111         shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
112         DCP_ASSERT (other_picture);
113
114         bool result = true;
115
116 #pragma omp parallel for
117         for (int i = 0; i < _intrinsic_duration; ++i) {
118                 if (i >= other_picture->intrinsic_duration()) {
119                         result = false;
120                 }
121
122                 if (result || opt.keep_going) {
123
124 #pragma omp critical
125                         note (DCP_PROGRESS, String::compose ("Comparing video frame %1 of %2", i, _intrinsic_duration));
126
127                         shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
128                         shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
129
130                         list<pair<NoteType, string> > notes;
131
132                         if (!frame_buffer_equals (
133                                     i, opt, bind (&storing_note_handler, notes, _1, _2),
134                                     frame_A->j2k_data(), frame_A->j2k_size(),
135                                     frame_B->j2k_data(), frame_B->j2k_size()
136                                     )) {
137                                 result = false;
138                         }
139
140 #pragma omp critical
141                         for (list<pair<NoteType, string> >::const_iterator i = notes.begin(); i != notes.end(); ++i) {
142                                 note (i->first, i->second);
143                         }
144                 }
145         }
146
147         return result;
148 }
149
150 shared_ptr<PictureAssetWriter>
151 MonoPictureAsset::start_write (boost::filesystem::path file, Standard standard, bool overwrite)
152 {
153         /* XXX: can't we use shared_ptr here? */
154         return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, file, standard, overwrite));
155 }
156
157 string
158 MonoPictureAsset::cpl_node_name () const
159 {
160         return "MainPicture";
161 }