asdcp headers moved into subdirectory.
[libdcp.git] / src / sound_asset.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/sound_mxf.cc
35  *  @brief SoundAsset class.
36  */
37
38 #include "sound_asset.h"
39 #include "util.h"
40 #include "exceptions.h"
41 #include "sound_frame.h"
42 #include "sound_asset_writer.h"
43 #include "sound_asset_reader.h"
44 #include "compose.hpp"
45 #include "dcp_assert.h"
46 #include <asdcp/KM_fileio.h>
47 #include <asdcp/AS_DCP.h>
48 #include <libxml++/nodes/element.h>
49 #include <boost/filesystem.hpp>
50 #include <iostream>
51 #include <stdexcept>
52
53 using std::string;
54 using std::stringstream;
55 using std::ostream;
56 using std::vector;
57 using std::list;
58 using boost::shared_ptr;
59 using boost::dynamic_pointer_cast;
60 using namespace dcp;
61
62 SoundAsset::SoundAsset (boost::filesystem::path file)
63         : Asset (file)
64 {
65         ASDCP::PCM::MXFReader reader;
66         Kumu::Result_t r = reader.OpenRead (file.string().c_str());
67         if (ASDCP_FAILURE (r)) {
68                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file.string(), r));
69         }
70
71         ASDCP::PCM::AudioDescriptor desc;
72         if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
73                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
74         }
75
76         _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
77         _channels = desc.ChannelCount;
78         _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
79
80         _intrinsic_duration = desc.ContainerDuration;
81
82         ASDCP::WriterInfo info;
83         if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
84                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
85         }
86
87         _id = read_writer_info (info);
88 }
89
90 SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels)
91         : _edit_rate (edit_rate)
92         , _intrinsic_duration (0)
93         , _channels (channels)
94         , _sampling_rate (sampling_rate)
95 {
96
97 }
98
99 bool
100 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
101 {
102         ASDCP::PCM::MXFReader reader_A;
103         Kumu::Result_t r = reader_A.OpenRead (file().string().c_str());
104         if (ASDCP_FAILURE (r)) {
105                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
106         }
107
108         ASDCP::PCM::MXFReader reader_B;
109         r = reader_B.OpenRead (other->file().string().c_str());
110         if (ASDCP_FAILURE (r)) {
111                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
112         }
113
114         ASDCP::PCM::AudioDescriptor desc_A;
115         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
116                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
117         }
118         ASDCP::PCM::AudioDescriptor desc_B;
119         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
120                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
121         }
122
123         if (desc_A.EditRate != desc_B.EditRate) {
124                 note (
125                         DCP_ERROR,
126                         String::compose (
127                                 "audio edit rates differ: %1/%2 cf %3/%4",
128                                 desc_A.EditRate.Numerator, desc_A.EditRate.Denominator, desc_B.EditRate.Numerator, desc_B.EditRate.Denominator
129                                 )
130                         );
131                 return false;
132         } else if (desc_A.AudioSamplingRate != desc_B.AudioSamplingRate) {
133                 note (
134                         DCP_ERROR,
135                         String::compose (
136                                 "audio sampling rates differ: %1 cf %2",
137                                 desc_A.AudioSamplingRate.Numerator, desc_A.AudioSamplingRate.Denominator,
138                                 desc_B.AudioSamplingRate.Numerator, desc_B.AudioSamplingRate.Numerator
139                                 )
140                         );
141                 return false;
142         } else if (desc_A.Locked != desc_B.Locked) {
143                 note (DCP_ERROR, String::compose ("audio locked flags differ: %1 cf %2", desc_A.Locked, desc_B.Locked));
144                 return false;
145         } else if (desc_A.ChannelCount != desc_B.ChannelCount) {
146                 note (DCP_ERROR, String::compose ("audio channel counts differ: %1 cf %2", desc_A.ChannelCount, desc_B.ChannelCount));
147                 return false;
148         } else if (desc_A.QuantizationBits != desc_B.QuantizationBits) {
149                 note (DCP_ERROR, String::compose ("audio bits per sample differ: %1 cf %2", desc_A.QuantizationBits, desc_B.QuantizationBits));
150                 return false;
151         } else if (desc_A.BlockAlign != desc_B.BlockAlign) {
152                 note (DCP_ERROR, String::compose ("audio bytes per sample differ: %1 cf %2", desc_A.BlockAlign, desc_B.BlockAlign));
153                 return false;
154         } else if (desc_A.AvgBps != desc_B.AvgBps) {
155                 note (DCP_ERROR, String::compose ("audio average bps differ: %1 cf %2", desc_A.AvgBps, desc_B.AvgBps));
156                 return false;
157         } else if (desc_A.LinkedTrackID != desc_B.LinkedTrackID) {
158                 note (DCP_ERROR, String::compose ("audio linked track IDs differ: %1 cf %2", desc_A.LinkedTrackID, desc_B.LinkedTrackID));
159                 return false;
160         } else if (desc_A.ContainerDuration != desc_B.ContainerDuration) {
161                 note (DCP_ERROR, String::compose ("audio container durations differ: %1 cf %2", desc_A.ContainerDuration, desc_B.ContainerDuration));
162                 return false;
163         } else if (desc_A.ChannelFormat != desc_B.ChannelFormat) {
164                 /* XXX */
165         }
166
167         shared_ptr<const SoundAsset> other_sound = dynamic_pointer_cast<const SoundAsset> (other);
168
169         shared_ptr<const SoundAssetReader> reader = start_read ();
170         shared_ptr<const SoundAssetReader> other_reader = other_sound->start_read ();
171
172         for (int i = 0; i < _intrinsic_duration; ++i) {
173
174                 shared_ptr<const SoundFrame> frame_A = reader->get_frame (i);
175                 shared_ptr<const SoundFrame> frame_B = other_reader->get_frame (i);
176
177                 if (frame_A->size() != frame_B->size()) {
178                         note (DCP_ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
179                         return false;
180                 }
181
182                 if (memcmp (frame_A->data(), frame_B->data(), frame_A->size()) != 0) {
183                         for (int i = 0; i < frame_A->size(); ++i) {
184                                 int const d = abs (frame_A->data()[i] - frame_B->data()[i]);
185                                 if (d > opt.max_audio_sample_error) {
186                                         note (DCP_ERROR, String::compose ("PCM data difference of %1", d));
187                                         return false;
188                                 }
189                         }
190                 }
191         }
192
193         return true;
194 }
195
196 shared_ptr<SoundAssetWriter>
197 SoundAsset::start_write (boost::filesystem::path file, Standard standard)
198 {
199         /* XXX: can't we use a shared_ptr here? */
200         return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this, file, standard));
201 }
202
203 shared_ptr<SoundAssetReader>
204 SoundAsset::start_read () const
205 {
206         return shared_ptr<SoundAssetReader> (new SoundAssetReader (this));
207 }
208
209 string
210 SoundAsset::pkl_type (Standard standard) const
211 {
212         switch (standard) {
213         case INTEROP:
214                 return "application/x-smpte-mxf;asdcpKind=Sound";
215         case SMPTE:
216                 return "application/mxf";
217         default:
218                 DCP_ASSERT (false);
219         }
220 }
221
222 bool
223 SoundAsset::valid_mxf (boost::filesystem::path file)
224 {
225         ASDCP::PCM::MXFReader reader;
226         Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
227         return !ASDCP_FAILURE (r);
228 }