Try to fix comparison of encrypted sound assets.
[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 boost::dynamic_pointer_cast;
45 using namespace dcp;
46
47 SoundAsset::SoundAsset (boost::filesystem::path file)
48         : Asset (file)
49 {
50         ASDCP::PCM::MXFReader reader;
51         Kumu::Result_t r = reader.OpenRead (file.string().c_str());
52         if (ASDCP_FAILURE (r)) {
53                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file.string(), r));
54         }
55
56         ASDCP::PCM::AudioDescriptor desc;
57         if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
58                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
59         }
60
61         _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
62         _channels = desc.ChannelCount;
63         _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
64
65         _intrinsic_duration = desc.ContainerDuration;
66
67         ASDCP::WriterInfo info;
68         if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
69                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
70         }
71
72         _id = read_writer_info (info);
73 }
74
75 SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels)
76         : _edit_rate (edit_rate)
77         , _intrinsic_duration (0)
78         , _channels (channels)
79         , _sampling_rate (sampling_rate)
80 {
81
82 }
83
84 bool
85 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
86 {
87         ASDCP::PCM::MXFReader reader_A;
88         Kumu::Result_t r = reader_A.OpenRead (file().string().c_str());
89         if (ASDCP_FAILURE (r)) {
90                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
91         }
92
93         ASDCP::PCM::MXFReader reader_B;
94         r = reader_B.OpenRead (other->file().string().c_str());
95         if (ASDCP_FAILURE (r)) {
96                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
97         }
98
99         ASDCP::PCM::AudioDescriptor desc_A;
100         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
101                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
102         }
103         ASDCP::PCM::AudioDescriptor desc_B;
104         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
105                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
106         }
107
108         if (
109                 desc_A.EditRate != desc_B.EditRate ||
110                 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
111                 desc_A.Locked != desc_B.Locked ||
112                 desc_A.ChannelCount != desc_B.ChannelCount ||
113                 desc_A.QuantizationBits != desc_B.QuantizationBits ||
114                 desc_A.BlockAlign != desc_B.BlockAlign ||
115                 desc_A.AvgBps != desc_B.AvgBps ||
116                 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
117                 desc_A.ContainerDuration != desc_B.ContainerDuration
118 //              desc_A.ChannelFormat != desc_B.ChannelFormat ||
119                 ) {
120
121                 note (DCP_ERROR, "audio MXF picture descriptors differ");
122                 return false;
123         }
124
125         shared_ptr<const SoundAsset> other_sound = dynamic_pointer_cast<const SoundAsset> (other);
126
127         for (int i = 0; i < _intrinsic_duration; ++i) {
128
129                 shared_ptr<const SoundFrame> frame_A = get_frame (i);
130                 shared_ptr<const SoundFrame> frame_B = other_sound->get_frame (i);
131
132                 if (frame_A->size() != frame_B->size()) {
133                         note (DCP_ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
134                         return false;
135                 }
136
137                 if (memcmp (frame_A->data(), frame_B->data(), frame_A->size()) != 0) {
138                         for (int i = 0; i < frame_A->size(); ++i) {
139                                 int const d = abs (frame_A->data()[i] - frame_B->data()[i]);
140                                 if (d > opt.max_audio_sample_error) {
141                                         note (DCP_ERROR, String::compose ("PCM data difference of %1", d));
142                                         return false;
143                                 }
144                         }
145                 }
146         }
147
148         return true;
149 }
150
151 shared_ptr<const SoundFrame>
152 SoundAsset::get_frame (int n) const
153 {
154         /* XXX: should add on entry point here? */
155         return shared_ptr<const SoundFrame> (new SoundFrame (file(), n, _decryption_context));
156 }
157
158 shared_ptr<SoundAssetWriter>
159 SoundAsset::start_write (boost::filesystem::path file, Standard standard)
160 {
161         /* XXX: can't we use a shared_ptr here? */
162         return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this, file, standard));
163 }
164
165 string
166 SoundAsset::pkl_type (Standard standard) const
167 {
168         switch (standard) {
169         case INTEROP:
170                 return "application/x-smpte-mxf;asdcpKind=Sound";
171         case SMPTE:
172                 return "application/mxf";
173         default:
174                 DCP_ASSERT (false);
175         }
176 }
177
178 bool
179 SoundAsset::valid_mxf (boost::filesystem::path file)
180 {
181         ASDCP::PCM::MXFReader reader;
182         Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
183         return !ASDCP_FAILURE (r);
184 }