Still more licence fixups.
[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
20 /** @file  src/sound_mxf.cc
21  *  @brief SoundAsset class.
22  */
23
24 #include "sound_asset.h"
25 #include "util.h"
26 #include "exceptions.h"
27 #include "sound_frame.h"
28 #include "sound_asset_writer.h"
29 #include "sound_asset_reader.h"
30 #include "compose.hpp"
31 #include "KM_fileio.h"
32 #include "AS_DCP.h"
33 #include "dcp_assert.h"
34 #include <libxml++/nodes/element.h>
35 #include <boost/filesystem.hpp>
36 #include <iostream>
37 #include <stdexcept>
38
39 using std::string;
40 using std::stringstream;
41 using std::ostream;
42 using std::vector;
43 using std::list;
44 using boost::shared_ptr;
45 using boost::dynamic_pointer_cast;
46 using namespace dcp;
47
48 SoundAsset::SoundAsset (boost::filesystem::path file)
49         : Asset (file)
50 {
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));
55         }
56
57         ASDCP::PCM::AudioDescriptor desc;
58         if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
59                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
60         }
61
62         _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
63         _channels = desc.ChannelCount;
64         _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
65
66         _intrinsic_duration = desc.ContainerDuration;
67
68         ASDCP::WriterInfo info;
69         if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
70                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
71         }
72
73         _id = read_writer_info (info);
74 }
75
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)
81 {
82
83 }
84
85 bool
86 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
87 {
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));
92         }
93
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));
98         }
99
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"));
103         }
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"));
107         }
108
109         if (
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 ||
120                 ) {
121
122                 note (DCP_ERROR, "audio MXF picture descriptors differ");
123                 return false;
124         }
125
126         shared_ptr<const SoundAsset> other_sound = dynamic_pointer_cast<const SoundAsset> (other);
127
128         shared_ptr<const SoundAssetReader> reader = start_read ();
129         shared_ptr<const SoundAssetReader> other_reader = other_sound->start_read ();
130
131         for (int i = 0; i < _intrinsic_duration; ++i) {
132
133                 shared_ptr<const SoundFrame> frame_A = reader->get_frame (i);
134                 shared_ptr<const SoundFrame> frame_B = other_reader->get_frame (i);
135
136                 if (frame_A->size() != frame_B->size()) {
137                         note (DCP_ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
138                         return false;
139                 }
140
141                 if (memcmp (frame_A->data(), frame_B->data(), frame_A->size()) != 0) {
142                         for (int i = 0; i < frame_A->size(); ++i) {
143                                 int const d = abs (frame_A->data()[i] - frame_B->data()[i]);
144                                 if (d > opt.max_audio_sample_error) {
145                                         note (DCP_ERROR, String::compose ("PCM data difference of %1", d));
146                                         return false;
147                                 }
148                         }
149                 }
150         }
151
152         return true;
153 }
154
155 shared_ptr<SoundAssetWriter>
156 SoundAsset::start_write (boost::filesystem::path file, Standard standard)
157 {
158         /* XXX: can't we use a shared_ptr here? */
159         return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this, file, standard));
160 }
161
162 shared_ptr<SoundAssetReader>
163 SoundAsset::start_read () const
164 {
165         return shared_ptr<SoundAssetReader> (new SoundAssetReader (this));
166 }
167
168 string
169 SoundAsset::pkl_type (Standard standard) const
170 {
171         switch (standard) {
172         case INTEROP:
173                 return "application/x-smpte-mxf;asdcpKind=Sound";
174         case SMPTE:
175                 return "application/mxf";
176         default:
177                 DCP_ASSERT (false);
178         }
179 }
180
181 bool
182 SoundAsset::valid_mxf (boost::filesystem::path file)
183 {
184         ASDCP::PCM::MXFReader reader;
185         Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
186         return !ASDCP_FAILURE (r);
187 }