Add equality option sound_assets_can_differ.
[libdcp.git] / src / sound_asset.cc
1 /*
2     Copyright (C) 2012-2021 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
35 /** @file  src/sound_asset.cc
36  *  @brief SoundAsset class
37  */
38
39
40 #include "compose.hpp"
41 #include "dcp_assert.h"
42 #include "equality_options.h"
43 #include "exceptions.h"
44 #include "sound_asset.h"
45 #include "sound_asset_reader.h"
46 #include "sound_asset_writer.h"
47 #include "sound_frame.h"
48 #include "util.h"
49 #include "warnings.h"
50 LIBDCP_DISABLE_WARNINGS
51 #include <asdcp/AS_DCP.h>
52 #include <asdcp/KM_fileio.h>
53 #include <asdcp/Metadata.h>
54 LIBDCP_ENABLE_WARNINGS
55 #include <libxml++/nodes/element.h>
56 #include <boost/filesystem.hpp>
57 #include <stdexcept>
58
59
60 using std::dynamic_pointer_cast;
61 using std::list;
62 using std::shared_ptr;
63 using std::string;
64 using std::vector;
65 using boost::optional;
66 using namespace dcp;
67
68
69 SoundAsset::SoundAsset (boost::filesystem::path file)
70         : Asset (file)
71 {
72         ASDCP::PCM::MXFReader reader;
73         auto r = reader.OpenRead (file.string().c_str());
74         if (ASDCP_FAILURE(r)) {
75                 boost::throw_exception (MXFFileError("could not open MXF file for reading", file.string(), r));
76         }
77
78         ASDCP::PCM::AudioDescriptor desc;
79         if (ASDCP_FAILURE (reader.FillAudioDescriptor(desc))) {
80                 boost::throw_exception (ReadError("could not read audio MXF information"));
81         }
82
83         _sampling_rate = desc.AudioSamplingRate.Denominator ? (desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator) : 0;
84         _channels = desc.ChannelCount;
85         _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
86
87         _intrinsic_duration = desc.ContainerDuration;
88
89         ASDCP::WriterInfo info;
90         if (ASDCP_FAILURE (reader.FillWriterInfo(info))) {
91                 boost::throw_exception (ReadError("could not read audio MXF information"));
92         }
93
94         ASDCP::MXF::SoundfieldGroupLabelSubDescriptor* soundfield;
95         auto rr = reader.OP1aHeader().GetMDObjectByType(
96                 asdcp_smpte_dict->ul(ASDCP::MDD_SoundfieldGroupLabelSubDescriptor),
97                 reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&soundfield)
98                 );
99
100         if (KM_SUCCESS(rr)) {
101                 if (!soundfield->RFC5646SpokenLanguage.empty()) {
102                         char buffer[64];
103                         soundfield->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
104                         _language = buffer;
105                 }
106         }
107
108         list<ASDCP::MXF::InterchangeObject*> channel_labels;
109         rr = reader.OP1aHeader().GetMDObjectsByType(
110                 asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
111                 channel_labels
112                 );
113
114         if (KM_SUCCESS(rr)) {
115                 _active_channels = channel_labels.size();
116         }
117
118         _id = read_writer_info (info);
119 }
120
121
122 SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels, LanguageTag language, Standard standard)
123         : MXF (standard)
124         , _edit_rate (edit_rate)
125         , _channels (channels)
126         , _sampling_rate (sampling_rate)
127         , _language (language.to_string())
128 {
129
130 }
131
132
133 bool
134 SoundAsset::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
135 {
136         if (opt.sound_assets_can_differ) {
137                 return true;
138         }
139
140         ASDCP::PCM::MXFReader reader_A;
141         DCP_ASSERT (file());
142         auto r = reader_A.OpenRead (file()->string().c_str());
143         if (ASDCP_FAILURE(r)) {
144                 boost::throw_exception (MXFFileError("could not open MXF file for reading", file()->string(), r));
145         }
146
147         ASDCP::PCM::MXFReader reader_B;
148         r = reader_B.OpenRead (other->file()->string().c_str());
149         if (ASDCP_FAILURE (r)) {
150                 boost::throw_exception (MXFFileError("could not open MXF file for reading", other->file()->string(), r));
151         }
152
153         ASDCP::PCM::AudioDescriptor desc_A;
154         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor(desc_A))) {
155                 boost::throw_exception (ReadError ("could not read audio MXF information"));
156         }
157         ASDCP::PCM::AudioDescriptor desc_B;
158         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor(desc_B))) {
159                 boost::throw_exception (ReadError ("could not read audio MXF information"));
160         }
161
162         if (desc_A.EditRate != desc_B.EditRate) {
163                 note (
164                         NoteType::ERROR,
165                         String::compose (
166                                 "audio edit rates differ: %1/%2 cf %3/%4",
167                                 desc_A.EditRate.Numerator, desc_A.EditRate.Denominator, desc_B.EditRate.Numerator, desc_B.EditRate.Denominator
168                                 )
169                         );
170                 return false;
171         } else if (desc_A.AudioSamplingRate != desc_B.AudioSamplingRate) {
172                 note (
173                         NoteType::ERROR,
174                         String::compose (
175                                 "audio sampling rates differ: %1 cf %2",
176                                 desc_A.AudioSamplingRate.Numerator, desc_A.AudioSamplingRate.Denominator,
177                                 desc_B.AudioSamplingRate.Numerator, desc_B.AudioSamplingRate.Numerator
178                                 )
179                         );
180                 return false;
181         } else if (desc_A.Locked != desc_B.Locked) {
182                 note (NoteType::ERROR, String::compose ("audio locked flags differ: %1 cf %2", desc_A.Locked, desc_B.Locked));
183                 return false;
184         } else if (desc_A.ChannelCount != desc_B.ChannelCount) {
185                 note (NoteType::ERROR, String::compose ("audio channel counts differ: %1 cf %2", desc_A.ChannelCount, desc_B.ChannelCount));
186                 return false;
187         } else if (desc_A.QuantizationBits != desc_B.QuantizationBits) {
188                 note (NoteType::ERROR, String::compose ("audio bits per sample differ: %1 cf %2", desc_A.QuantizationBits, desc_B.QuantizationBits));
189                 return false;
190         } else if (desc_A.BlockAlign != desc_B.BlockAlign) {
191                 note (NoteType::ERROR, String::compose ("audio bytes per sample differ: %1 cf %2", desc_A.BlockAlign, desc_B.BlockAlign));
192                 return false;
193         } else if (desc_A.AvgBps != desc_B.AvgBps) {
194                 note (NoteType::ERROR, String::compose ("audio average bps differ: %1 cf %2", desc_A.AvgBps, desc_B.AvgBps));
195                 return false;
196         } else if (desc_A.LinkedTrackID != desc_B.LinkedTrackID) {
197                 note (NoteType::ERROR, String::compose ("audio linked track IDs differ: %1 cf %2", desc_A.LinkedTrackID, desc_B.LinkedTrackID));
198                 return false;
199         } else if (desc_A.ContainerDuration != desc_B.ContainerDuration) {
200                 note (NoteType::ERROR, String::compose ("audio container durations differ: %1 cf %2", desc_A.ContainerDuration, desc_B.ContainerDuration));
201                 return false;
202         } else if (desc_A.ChannelFormat != desc_B.ChannelFormat) {
203                 /* XXX */
204         }
205
206         auto other_sound = dynamic_pointer_cast<const SoundAsset> (other);
207
208         auto reader = start_read ();
209         auto other_reader = other_sound->start_read ();
210
211         for (int i = 0; i < _intrinsic_duration; ++i) {
212
213                 auto frame_A = reader->get_frame (i);
214                 auto frame_B = other_reader->get_frame (i);
215
216                 if (frame_A->size() != frame_B->size()) {
217                         note (NoteType::ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
218                         return false;
219                 }
220
221                 if (memcmp (frame_A->data(), frame_B->data(), frame_A->size()) != 0) {
222                         for (int sample = 0; sample < frame_A->samples(); ++sample) {
223                                 for (int channel = 0; channel < frame_A->channels(); ++channel) {
224                                         int32_t const d = abs(frame_A->get(channel, sample) - frame_B->get(channel, sample));
225                                         if (d > opt.max_audio_sample_error) {
226                                                 note (NoteType::ERROR, String::compose("PCM data difference of %1 in frame %2, channel %3, sample %4", d, i, channel, sample));
227                                                 return false;
228                                         }
229                                 }
230                         }
231                 }
232         }
233
234         return true;
235 }
236
237
238 shared_ptr<SoundAssetWriter>
239 SoundAsset::start_write(
240         boost::filesystem::path file,
241         vector<dcp::Channel> extra_active_channels,
242         AtmosSync atmos_sync,
243         MCASubDescriptors include_mca_subdescriptors
244         )
245 {
246         if (atmos_sync == AtmosSync::ENABLED && _channels < 14) {
247                 throw MiscError ("Insufficient channels to write ATMOS sync (there must be at least 14)");
248         }
249
250         return shared_ptr<SoundAssetWriter>(
251                 new SoundAssetWriter(this, file, extra_active_channels, atmos_sync == AtmosSync::ENABLED, include_mca_subdescriptors == MCASubDescriptors::ENABLED)
252                 );
253 }
254
255
256 shared_ptr<SoundAssetReader>
257 SoundAsset::start_read () const
258 {
259         return shared_ptr<SoundAssetReader> (new SoundAssetReader(this, key(), standard()));
260 }
261
262
263 string
264 SoundAsset::static_pkl_type (Standard standard)
265 {
266         switch (standard) {
267         case Standard::INTEROP:
268                 return "application/x-smpte-mxf;asdcpKind=Sound";
269         case Standard::SMPTE:
270                 return "application/mxf";
271         default:
272                 DCP_ASSERT (false);
273         }
274 }
275
276
277 bool
278 SoundAsset::valid_mxf (boost::filesystem::path file)
279 {
280         ASDCP::PCM::MXFReader reader;
281         Kumu::Result_t r = reader.OpenRead (file.string().c_str());
282         return !ASDCP_FAILURE (r);
283 }
284
285
286 int
287 SoundAsset::active_channels() const
288 {
289         return _active_channels.get_value_or(_channels);
290 }
291