2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 /** @file src/sound_asset.cc
36 * @brief SoundAsset class
40 #include "compose.hpp"
41 #include "dcp_assert.h"
42 #include "equality_options.h"
43 #include "exceptions.h"
44 #include "filesystem.h"
45 #include "sound_asset.h"
46 #include "sound_asset_reader.h"
47 #include "sound_asset_writer.h"
48 #include "sound_frame.h"
51 LIBDCP_DISABLE_WARNINGS
52 #include <asdcp/AS_DCP.h>
53 #include <asdcp/KM_fileio.h>
54 #include <asdcp/Metadata.h>
55 LIBDCP_ENABLE_WARNINGS
56 #include <libxml++/nodes/element.h>
57 #include <boost/filesystem.hpp>
61 using std::dynamic_pointer_cast;
63 using std::shared_ptr;
66 using boost::optional;
70 SoundAsset::SoundAsset (boost::filesystem::path file)
73 Kumu::FileReaderFactory factory;
74 ASDCP::PCM::MXFReader reader(factory);
75 auto r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
76 if (ASDCP_FAILURE(r)) {
77 boost::throw_exception (MXFFileError("could not open MXF file for reading", file.string(), r));
80 ASDCP::PCM::AudioDescriptor desc;
81 if (ASDCP_FAILURE (reader.FillAudioDescriptor(desc))) {
82 boost::throw_exception (ReadError("could not read audio MXF information"));
85 _sampling_rate = desc.AudioSamplingRate.Denominator ? (desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator) : 0;
86 _channels = desc.ChannelCount;
87 _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
89 _intrinsic_duration = desc.ContainerDuration;
91 ASDCP::WriterInfo info;
92 if (ASDCP_FAILURE (reader.FillWriterInfo(info))) {
93 boost::throw_exception (ReadError("could not read audio MXF information"));
96 ASDCP::MXF::SoundfieldGroupLabelSubDescriptor* soundfield;
97 auto rr = reader.OP1aHeader().GetMDObjectByType(
98 asdcp_smpte_dict->ul(ASDCP::MDD_SoundfieldGroupLabelSubDescriptor),
99 reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&soundfield)
102 if (KM_SUCCESS(rr)) {
103 if (!soundfield->RFC5646SpokenLanguage.empty()) {
105 soundfield->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
110 list<ASDCP::MXF::InterchangeObject*> channel_labels;
111 rr = reader.OP1aHeader().GetMDObjectsByType(
112 asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
116 if (KM_SUCCESS(rr)) {
117 _active_channels = channel_labels.size();
120 _id = read_writer_info (info);
124 SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels, LanguageTag language, Standard standard)
126 , _edit_rate (edit_rate)
127 , _channels (channels)
128 , _sampling_rate (sampling_rate)
129 , _language (language.to_string())
136 SoundAsset::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
138 if (opt.sound_assets_can_differ) {
142 Kumu::FileReaderFactory factory;
143 ASDCP::PCM::MXFReader reader_A(factory);
145 auto r = reader_A.OpenRead(dcp::filesystem::fix_long_path(*file()).string().c_str());
146 if (ASDCP_FAILURE(r)) {
147 boost::throw_exception (MXFFileError("could not open MXF file for reading", file()->string(), r));
150 ASDCP::PCM::MXFReader reader_B(factory);
151 r = reader_B.OpenRead(dcp::filesystem::fix_long_path(*other->file()).string().c_str());
152 if (ASDCP_FAILURE (r)) {
153 boost::throw_exception (MXFFileError("could not open MXF file for reading", other->file()->string(), r));
156 ASDCP::PCM::AudioDescriptor desc_A;
157 if (ASDCP_FAILURE (reader_A.FillAudioDescriptor(desc_A))) {
158 boost::throw_exception (ReadError ("could not read audio MXF information"));
160 ASDCP::PCM::AudioDescriptor desc_B;
161 if (ASDCP_FAILURE (reader_B.FillAudioDescriptor(desc_B))) {
162 boost::throw_exception (ReadError ("could not read audio MXF information"));
165 if (desc_A.EditRate != desc_B.EditRate) {
169 "audio edit rates differ: %1/%2 cf %3/%4",
170 desc_A.EditRate.Numerator, desc_A.EditRate.Denominator, desc_B.EditRate.Numerator, desc_B.EditRate.Denominator
174 } else if (desc_A.AudioSamplingRate != desc_B.AudioSamplingRate) {
178 "audio sampling rates differ: %1 cf %2",
179 desc_A.AudioSamplingRate.Numerator, desc_A.AudioSamplingRate.Denominator,
180 desc_B.AudioSamplingRate.Numerator, desc_B.AudioSamplingRate.Numerator
184 } else if (desc_A.Locked != desc_B.Locked) {
185 note (NoteType::ERROR, String::compose ("audio locked flags differ: %1 cf %2", desc_A.Locked, desc_B.Locked));
187 } else if (desc_A.ChannelCount != desc_B.ChannelCount) {
188 note (NoteType::ERROR, String::compose ("audio channel counts differ: %1 cf %2", desc_A.ChannelCount, desc_B.ChannelCount));
190 } else if (desc_A.QuantizationBits != desc_B.QuantizationBits) {
191 note (NoteType::ERROR, String::compose ("audio bits per sample differ: %1 cf %2", desc_A.QuantizationBits, desc_B.QuantizationBits));
193 } else if (desc_A.BlockAlign != desc_B.BlockAlign) {
194 note (NoteType::ERROR, String::compose ("audio bytes per sample differ: %1 cf %2", desc_A.BlockAlign, desc_B.BlockAlign));
196 } else if (desc_A.AvgBps != desc_B.AvgBps) {
197 note (NoteType::ERROR, String::compose ("audio average bps differ: %1 cf %2", desc_A.AvgBps, desc_B.AvgBps));
199 } else if (desc_A.LinkedTrackID != desc_B.LinkedTrackID) {
200 note (NoteType::ERROR, String::compose ("audio linked track IDs differ: %1 cf %2", desc_A.LinkedTrackID, desc_B.LinkedTrackID));
202 } else if (desc_A.ContainerDuration != desc_B.ContainerDuration) {
203 note (NoteType::ERROR, String::compose ("audio container durations differ: %1 cf %2", desc_A.ContainerDuration, desc_B.ContainerDuration));
205 } else if (desc_A.ChannelFormat != desc_B.ChannelFormat) {
209 auto other_sound = dynamic_pointer_cast<const SoundAsset> (other);
211 auto reader = start_read ();
212 auto other_reader = other_sound->start_read ();
214 for (int i = 0; i < _intrinsic_duration; ++i) {
216 auto frame_A = reader->get_frame (i);
217 auto frame_B = other_reader->get_frame (i);
219 if (frame_A->size() != frame_B->size()) {
220 note (NoteType::ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
224 if (memcmp (frame_A->data(), frame_B->data(), frame_A->size()) != 0) {
225 for (int sample = 0; sample < frame_A->samples(); ++sample) {
226 for (int channel = 0; channel < frame_A->channels(); ++channel) {
227 int32_t const d = abs(frame_A->get(channel, sample) - frame_B->get(channel, sample));
228 if (d > opt.max_audio_sample_error) {
229 note (NoteType::ERROR, String::compose("PCM data difference of %1 in frame %2, channel %3, sample %4", d, i, channel, sample));
241 shared_ptr<SoundAssetWriter>
242 SoundAsset::start_write(
243 boost::filesystem::path file,
244 vector<dcp::Channel> extra_active_channels,
245 AtmosSync atmos_sync,
246 MCASubDescriptors include_mca_subdescriptors
249 if (atmos_sync == AtmosSync::ENABLED && _channels < 14) {
250 throw MiscError ("Insufficient channels to write ATMOS sync (there must be at least 14)");
253 return shared_ptr<SoundAssetWriter>(
254 new SoundAssetWriter(this, file, extra_active_channels, atmos_sync == AtmosSync::ENABLED, include_mca_subdescriptors == MCASubDescriptors::ENABLED)
259 shared_ptr<SoundAssetReader>
260 SoundAsset::start_read () const
262 return shared_ptr<SoundAssetReader> (new SoundAssetReader(this, key(), standard()));
267 SoundAsset::static_pkl_type (Standard standard)
270 case Standard::INTEROP:
271 return "application/x-smpte-mxf;asdcpKind=Sound";
272 case Standard::SMPTE:
273 return "application/mxf";
281 SoundAsset::valid_mxf (boost::filesystem::path file)
283 Kumu::FileReaderFactory factory;
284 ASDCP::PCM::MXFReader reader(factory);
285 Kumu::Result_t r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
286 return !ASDCP_FAILURE (r);
291 SoundAsset::active_channels() const
293 return _active_channels.get_value_or(_channels);