Merge branch 'master' into 1.0
[libdcp.git] / src / sound_mxf.cc
1 /*
2     Copyright (C) 2012-2014 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 SoundMXF class.
22  */
23
24 #include "sound_mxf.h"
25 #include "util.h"
26 #include "exceptions.h"
27 #include "sound_frame.h"
28 #include "sound_mxf_writer.h"
29 #include "compose.hpp"
30 #include "KM_fileio.h"
31 #include "AS_DCP.h"
32 #include <libxml++/nodes/element.h>
33 #include <boost/filesystem.hpp>
34 #include <iostream>
35 #include <stdexcept>
36
37 using std::string;
38 using std::stringstream;
39 using std::ostream;
40 using std::vector;
41 using std::list;
42 using boost::shared_ptr;
43 using namespace dcp;
44
45 SoundMXF::SoundMXF (boost::filesystem::path file)
46         : MXF (file)
47         , _channels (0)
48         , _sampling_rate (0)
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         read_writer_info (info);
73
74 }
75
76 SoundMXF::SoundMXF (Fraction edit_rate, int sampling_rate, int channels)
77         : MXF (edit_rate)
78         , _channels (channels)
79         , _sampling_rate (sampling_rate)
80 {
81
82 }
83
84 bool
85 SoundMXF::equals (shared_ptr<const Content> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
86 {
87         if (!MXF::equals (other, opt, note)) {
88                 return false;
89         }
90                      
91         ASDCP::PCM::MXFReader reader_A;
92         Kumu::Result_t r = reader_A.OpenRead (file().string().c_str());
93         if (ASDCP_FAILURE (r)) {
94                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
95         }
96
97         ASDCP::PCM::MXFReader reader_B;
98         r = reader_B.OpenRead (other->file().string().c_str());
99         if (ASDCP_FAILURE (r)) {
100                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
101         }
102
103         ASDCP::PCM::AudioDescriptor desc_A;
104         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
105                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
106         }
107         ASDCP::PCM::AudioDescriptor desc_B;
108         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
109                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
110         }
111         
112         if (
113                 desc_A.EditRate != desc_B.EditRate ||
114                 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
115                 desc_A.Locked != desc_B.Locked ||
116                 desc_A.ChannelCount != desc_B.ChannelCount ||
117                 desc_A.QuantizationBits != desc_B.QuantizationBits ||
118                 desc_A.BlockAlign != desc_B.BlockAlign ||
119                 desc_A.AvgBps != desc_B.AvgBps ||
120                 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
121                 desc_A.ContainerDuration != desc_B.ContainerDuration
122 //              desc_A.ChannelFormat != desc_B.ChannelFormat ||
123                 ) {
124                 
125                 note (ERROR, "audio MXF picture descriptors differ");
126                 return false;
127         }
128         
129         ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
130         ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
131         
132         for (int i = 0; i < _intrinsic_duration; ++i) {
133                 if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
134                         boost::throw_exception (DCPReadError ("could not read audio frame"));
135                 }
136                 
137                 if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
138                         boost::throw_exception (DCPReadError ("could not read audio frame"));
139                 }
140                 
141                 if (buffer_A.Size() != buffer_B.Size()) {
142                         note (ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
143                         return false;
144                 }
145                 
146                 if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
147                         for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
148                                 int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[i]);
149                                 if (d > opt.max_audio_sample_error) {
150                                         note (ERROR, String::compose ("PCM data difference of %1", d));
151                                         return false;
152                                 }
153                         }
154                 }
155         }
156
157         return true;
158 }
159
160 shared_ptr<const SoundFrame>
161 SoundMXF::get_frame (int n) const
162 {
163         /* XXX: should add on entry point here? */
164         return shared_ptr<const SoundFrame> (new SoundFrame (file(), n, _decryption_context));
165 }
166
167 shared_ptr<SoundMXFWriter>
168 SoundMXF::start_write (boost::filesystem::path file, Standard standard)
169 {
170         /* XXX: can't we use a shared_ptr here? */
171         return shared_ptr<SoundMXFWriter> (new SoundMXFWriter (this, file, standard));
172 }
173
174 string
175 SoundMXF::key_type () const
176 {
177         return "MDAK";
178 }