2 Copyright (C) 2012-2016 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/>.
21 /** @file src/sound_mxf.cc
22 * @brief SoundAsset class.
25 #include "sound_asset.h"
27 #include "exceptions.h"
28 #include "sound_frame.h"
29 #include "sound_asset_writer.h"
30 #include "compose.hpp"
31 #include "KM_fileio.h"
33 #include "dcp_assert.h"
34 #include <libxml++/nodes/element.h>
35 #include <boost/filesystem.hpp>
40 using std::stringstream;
44 using boost::shared_ptr;
45 using boost::dynamic_pointer_cast;
48 SoundAsset::SoundAsset (boost::filesystem::path file)
51 ASDCP::PCM::MXFReader reader;
52 Kumu::Result_t r = reader.OpenRead (file.string().c_str());
53 if (ASDCP_FAILURE (r)) {
54 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file.string(), r));
57 ASDCP::PCM::AudioDescriptor desc;
58 if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
59 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
62 _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
63 _channels = desc.ChannelCount;
64 _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
66 _intrinsic_duration = desc.ContainerDuration;
68 ASDCP::WriterInfo info;
69 if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
70 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
73 _id = read_writer_info (info);
76 SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels)
77 : _edit_rate (edit_rate)
78 , _intrinsic_duration (0)
79 , _channels (channels)
80 , _sampling_rate (sampling_rate)
86 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
88 ASDCP::PCM::MXFReader reader_A;
89 Kumu::Result_t r = reader_A.OpenRead (file().string().c_str());
90 if (ASDCP_FAILURE (r)) {
91 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
94 ASDCP::PCM::MXFReader reader_B;
95 r = reader_B.OpenRead (other->file().string().c_str());
96 if (ASDCP_FAILURE (r)) {
97 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
100 ASDCP::PCM::AudioDescriptor desc_A;
101 if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
102 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
104 ASDCP::PCM::AudioDescriptor desc_B;
105 if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
106 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
110 desc_A.EditRate != desc_B.EditRate ||
111 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
112 desc_A.Locked != desc_B.Locked ||
113 desc_A.ChannelCount != desc_B.ChannelCount ||
114 desc_A.QuantizationBits != desc_B.QuantizationBits ||
115 desc_A.BlockAlign != desc_B.BlockAlign ||
116 desc_A.AvgBps != desc_B.AvgBps ||
117 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
118 desc_A.ContainerDuration != desc_B.ContainerDuration
119 // desc_A.ChannelFormat != desc_B.ChannelFormat ||
122 note (DCP_ERROR, "audio MXF picture descriptors differ");
126 shared_ptr<const SoundAsset> other_sound = dynamic_pointer_cast<const SoundAsset> (other);
128 for (int i = 0; i < _intrinsic_duration; ++i) {
130 shared_ptr<const SoundFrame> frame_A = get_frame (i);
131 shared_ptr<const SoundFrame> frame_B = other_sound->get_frame (i);
133 if (frame_A->size() != frame_B->size()) {
134 note (DCP_ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
138 if (memcmp (frame_A->data(), frame_B->data(), frame_A->size()) != 0) {
139 for (int i = 0; i < frame_A->size(); ++i) {
140 int const d = abs (frame_A->data()[i] - frame_B->data()[i]);
141 if (d > opt.max_audio_sample_error) {
142 note (DCP_ERROR, String::compose ("PCM data difference of %1", d));
152 shared_ptr<const SoundFrame>
153 SoundAsset::get_frame (int n) const
155 /* XXX: should add on entry point here? */
156 return shared_ptr<const SoundFrame> (new SoundFrame (file(), n, _decryption_context));
159 shared_ptr<SoundAssetWriter>
160 SoundAsset::start_write (boost::filesystem::path file, Standard standard)
162 /* XXX: can't we use a shared_ptr here? */
163 return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this, file, standard));
167 SoundAsset::pkl_type (Standard standard) const
171 return "application/x-smpte-mxf;asdcpKind=Sound";
173 return "application/mxf";
180 SoundAsset::valid_mxf (boost::filesystem::path file)
182 ASDCP::PCM::MXFReader reader;
183 Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
184 return !ASDCP_FAILURE (r);