Add SoundAsset::valid_mxf().
[libdcp.git] / src / sound_asset.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
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 "compose.hpp"
30 #include "KM_fileio.h"
31 #include "AS_DCP.h"
32 #include "dcp_assert.h"
33 #include <libxml++/nodes/element.h>
34 #include <boost/filesystem.hpp>
35 #include <iostream>
36 #include <stdexcept>
37
38 using std::string;
39 using std::stringstream;
40 using std::ostream;
41 using std::vector;
42 using std::list;
43 using boost::shared_ptr;
44 using namespace dcp;
45
46 SoundAsset::SoundAsset (boost::filesystem::path file)
47         : Asset (file)
48 {
49         ASDCP::PCM::MXFReader reader;
50         Kumu::Result_t r = reader.OpenRead (file.string().c_str());
51         if (ASDCP_FAILURE (r)) {
52                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file.string(), r));
53         }
54
55         ASDCP::PCM::AudioDescriptor desc;
56         if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
57                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
58         }
59
60         _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
61         _channels = desc.ChannelCount;
62         _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
63
64         _intrinsic_duration = desc.ContainerDuration;
65
66         ASDCP::WriterInfo info;
67         if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
68                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
69         }
70
71         _id = read_writer_info (info);
72 }
73
74 SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels)
75         : _edit_rate (edit_rate)
76         , _intrinsic_duration (0)
77         , _channels (channels)
78         , _sampling_rate (sampling_rate)
79 {
80
81 }
82
83 bool
84 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
85 {
86         ASDCP::PCM::MXFReader reader_A;
87         Kumu::Result_t r = reader_A.OpenRead (file().string().c_str());
88         if (ASDCP_FAILURE (r)) {
89                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
90         }
91
92         ASDCP::PCM::MXFReader reader_B;
93         r = reader_B.OpenRead (other->file().string().c_str());
94         if (ASDCP_FAILURE (r)) {
95                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
96         }
97
98         ASDCP::PCM::AudioDescriptor desc_A;
99         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
100                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
101         }
102         ASDCP::PCM::AudioDescriptor desc_B;
103         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
104                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
105         }
106
107         if (
108                 desc_A.EditRate != desc_B.EditRate ||
109                 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
110                 desc_A.Locked != desc_B.Locked ||
111                 desc_A.ChannelCount != desc_B.ChannelCount ||
112                 desc_A.QuantizationBits != desc_B.QuantizationBits ||
113                 desc_A.BlockAlign != desc_B.BlockAlign ||
114                 desc_A.AvgBps != desc_B.AvgBps ||
115                 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
116                 desc_A.ContainerDuration != desc_B.ContainerDuration
117 //              desc_A.ChannelFormat != desc_B.ChannelFormat ||
118                 ) {
119
120                 note (DCP_ERROR, "audio MXF picture descriptors differ");
121                 return false;
122         }
123
124         ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
125         ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
126
127         for (int i = 0; i < _intrinsic_duration; ++i) {
128                 if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
129                         boost::throw_exception (DCPReadError ("could not read audio frame"));
130                 }
131
132                 if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
133                         boost::throw_exception (DCPReadError ("could not read audio frame"));
134                 }
135
136                 if (buffer_A.Size() != buffer_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 (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
142                         for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
143                                 int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[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<const SoundFrame>
156 SoundAsset::get_frame (int n) const
157 {
158         /* XXX: should add on entry point here? */
159         return shared_ptr<const SoundFrame> (new SoundFrame (file(), n, _decryption_context));
160 }
161
162 shared_ptr<SoundAssetWriter>
163 SoundAsset::start_write (boost::filesystem::path file, Standard standard)
164 {
165         /* XXX: can't we use a shared_ptr here? */
166         return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this, file, standard));
167 }
168
169 string
170 SoundAsset::pkl_type (Standard standard) const
171 {
172         switch (standard) {
173         case INTEROP:
174                 return "application/x-smpte-mxf;asdcpKind=Sound";
175         case SMPTE:
176                 return "application/mxf";
177         default:
178                 DCP_ASSERT (false);
179         }
180 }
181
182 bool
183 SoundAsset::valid_mxf (boost::filesystem::path file)
184 {
185         ASDCP::PCM::MXFReader reader;
186         Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
187         return !ASDCP_FAILURE (r);
188 }