Remove create-by-files method for sound and picture assets.
[libdcp.git] / src / mono_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 "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 "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 MonoPictureAsset::MonoPictureAsset (boost::filesystem::path directory, boost::filesystem::path mxf_name)
35         : PictureAsset (directory, mxf_name)
36 {
37
38 }
39
40 void
41 MonoPictureAsset::read ()
42 {
43         ASDCP::JP2K::MXFReader reader;
44         Kumu::Result_t r = reader.OpenRead (path().string().c_str());
45         if (ASDCP_FAILURE (r)) {
46                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
47         }
48         
49         ASDCP::JP2K::PictureDescriptor desc;
50         if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
51                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
52         }
53
54         _size.width = desc.StoredWidth;
55         _size.height = desc.StoredHeight;
56         _edit_rate = desc.EditRate.Numerator;
57         assert (desc.EditRate.Denominator == 1);
58         _intrinsic_duration = desc.ContainerDuration;
59 }
60
61 boost::filesystem::path
62 MonoPictureAsset::path_from_list (int f, vector<boost::filesystem::path> const & files) const
63 {
64         return files[f];
65 }
66
67 shared_ptr<const MonoPictureFrame>
68 MonoPictureAsset::get_frame (int n) const
69 {
70         return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path(), n, _decryption_context));
71 }
72
73 bool
74 MonoPictureAsset::equals (shared_ptr<const ContentAsset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
75 {
76         if (!MXFAsset::equals (other, opt, note)) {
77                 return false;
78         }
79
80         ASDCP::JP2K::MXFReader reader_A;
81         Kumu::Result_t r = reader_A.OpenRead (path().string().c_str());
82         if (ASDCP_FAILURE (r)) {
83                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
84         }
85         
86         ASDCP::JP2K::MXFReader reader_B;
87         r = reader_B.OpenRead (other->path().string().c_str());
88         if (ASDCP_FAILURE (r)) {
89                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
90         }
91         
92         ASDCP::JP2K::PictureDescriptor desc_A;
93         if (ASDCP_FAILURE (reader_A.FillPictureDescriptor (desc_A))) {
94                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
95         }
96         ASDCP::JP2K::PictureDescriptor desc_B;
97         if (ASDCP_FAILURE (reader_B.FillPictureDescriptor (desc_B))) {
98                 boost::throw_exception (DCPReadError ("could not read video MXF information"));
99         }
100         
101         if (!descriptor_equals (desc_A, desc_B, note)) {
102                 return false;
103         }
104
105         shared_ptr<const MonoPictureAsset> other_picture = dynamic_pointer_cast<const MonoPictureAsset> (other);
106         assert (other_picture);
107
108         for (int i = 0; i < _intrinsic_duration; ++i) {
109                 if (i >= other_picture->intrinsic_duration()) {
110                         return false;
111                 }
112                 
113                 note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
114                 shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
115                 shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
116                 
117                 if (!frame_buffer_equals (
118                             i, opt, note,
119                             frame_A->j2k_data(), frame_A->j2k_size(),
120                             frame_B->j2k_data(), frame_B->j2k_size()
121                             )) {
122                         return false;
123                 }
124         }
125
126         return true;
127 }
128
129 shared_ptr<PictureAssetWriter>
130 MonoPictureAsset::start_write (bool overwrite)
131 {
132         /* XXX: can't we use shared_ptr here? */
133         return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite));
134 }
135
136 string
137 MonoPictureAsset::cpl_node_name () const
138 {
139         return "MainPicture";
140 }
141
142 int
143 MonoPictureAsset::edit_rate_factor () const
144 {
145         return 1;
146 }