2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/sound_mxf.cc
21 * @brief SoundMXF class.
24 #include "sound_mxf.h"
26 #include "exceptions.h"
27 #include "sound_frame.h"
28 #include "sound_mxf_writer.h"
29 #include "KM_fileio.h"
31 #include <libxml++/nodes/element.h>
32 #include <boost/filesystem.hpp>
33 #include <boost/lexical_cast.hpp>
38 using std::stringstream;
42 using boost::shared_ptr;
43 using boost::lexical_cast;
46 SoundMXF::SoundMXF (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;
69 SoundMXF::SoundMXF (Fraction edit_rate, int sampling_rate, int channels)
71 , _channels (channels)
72 , _sampling_rate (sampling_rate)
78 SoundMXF::equals (shared_ptr<const Content> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
80 if (!MXF::equals (other, opt, note)) {
84 ASDCP::PCM::MXFReader reader_A;
85 Kumu::Result_t r = reader_A.OpenRead (file().string().c_str());
86 if (ASDCP_FAILURE (r)) {
87 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
90 ASDCP::PCM::MXFReader reader_B;
91 r = reader_B.OpenRead (other->file().string().c_str());
92 if (ASDCP_FAILURE (r)) {
93 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
96 ASDCP::PCM::AudioDescriptor desc_A;
97 if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
98 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
100 ASDCP::PCM::AudioDescriptor desc_B;
101 if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
102 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
106 desc_A.EditRate != desc_B.EditRate ||
107 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
108 desc_A.Locked != desc_B.Locked ||
109 desc_A.ChannelCount != desc_B.ChannelCount ||
110 desc_A.QuantizationBits != desc_B.QuantizationBits ||
111 desc_A.BlockAlign != desc_B.BlockAlign ||
112 desc_A.AvgBps != desc_B.AvgBps ||
113 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
114 desc_A.ContainerDuration != desc_B.ContainerDuration
115 // desc_A.ChannelFormat != desc_B.ChannelFormat ||
118 note (ERROR, "audio MXF picture descriptors differ");
122 ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
123 ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
125 for (int i = 0; i < _intrinsic_duration; ++i) {
126 if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
127 boost::throw_exception (DCPReadError ("could not read audio frame"));
130 if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
131 boost::throw_exception (DCPReadError ("could not read audio frame"));
134 if (buffer_A.Size() != buffer_B.Size()) {
135 note (ERROR, "sizes of audio data for frame " + lexical_cast<string>(i) + " differ");
139 if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
140 for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
141 int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[i]);
142 if (d > opt.max_audio_sample_error) {
143 note (ERROR, "PCM data difference of " + lexical_cast<string> (d));
153 shared_ptr<const SoundFrame>
154 SoundMXF::get_frame (int n) const
156 /* XXX: should add on entry point here? */
157 return shared_ptr<const SoundFrame> (new SoundFrame (file().string(), n, _decryption_context));
160 shared_ptr<SoundMXFWriter>
161 SoundMXF::start_write (boost::filesystem::path file, Standard standard)
163 /* XXX: can't we use a shared_ptr here? */
164 return shared_ptr<SoundMXFWriter> (new SoundMXFWriter (this, file, standard));
168 SoundMXF::key_type () const