No-op; Fix GPL address and mention libdcp by name.
[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
21 /** @file  src/sound_mxf.cc
22  *  @brief SoundAsset class.
23  */
24
25 #include "sound_asset.h"
26 #include "util.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"
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         for (int i = 0; i < _intrinsic_duration; ++i) {
129
130                 shared_ptr<const SoundFrame> frame_A = get_frame (i);
131                 shared_ptr<const SoundFrame> frame_B = other_sound->get_frame (i);
132
133                 if (frame_A->size() != frame_B->size()) {
134                         note (DCP_ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
135                         return false;
136                 }
137
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));
143                                         return false;
144                                 }
145                         }
146                 }
147         }
148
149         return true;
150 }
151
152 shared_ptr<const SoundFrame>
153 SoundAsset::get_frame (int n) const
154 {
155         /* XXX: should add on entry point here? */
156         return shared_ptr<const SoundFrame> (new SoundFrame (file(), n, _decryption_context));
157 }
158
159 shared_ptr<SoundAssetWriter>
160 SoundAsset::start_write (boost::filesystem::path file, Standard standard)
161 {
162         /* XXX: can't we use a shared_ptr here? */
163         return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this, file, standard));
164 }
165
166 string
167 SoundAsset::pkl_type (Standard standard) const
168 {
169         switch (standard) {
170         case INTEROP:
171                 return "application/x-smpte-mxf;asdcpKind=Sound";
172         case SMPTE:
173                 return "application/mxf";
174         default:
175                 DCP_ASSERT (false);
176         }
177 }
178
179 bool
180 SoundAsset::valid_mxf (boost::filesystem::path file)
181 {
182         ASDCP::PCM::MXFReader reader;
183         Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
184         return !ASDCP_FAILURE (r);
185 }