Switch away from the many-constructor-arguments approach to a hopefully simpler API.
[libdcp.git] / src / sound_asset.cc
1 /*
2     Copyright (C) 2012 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_asset.cc
21  *  @brief An asset made up of WAV files
22  */
23
24 #include <iostream>
25 #include <stdexcept>
26 #include <boost/filesystem.hpp>
27 #include <boost/lexical_cast.hpp>
28 #include <libxml++/nodes/element.h>
29 #include "KM_fileio.h"
30 #include "AS_DCP.h"
31 #include "sound_asset.h"
32 #include "util.h"
33 #include "exceptions.h"
34 #include "sound_frame.h"
35
36 using std::string;
37 using std::stringstream;
38 using std::ostream;
39 using std::vector;
40 using std::list;
41 using boost::shared_ptr;
42 using boost::lexical_cast;
43 using namespace libdcp;
44
45 SoundAsset::SoundAsset (boost::filesystem::path directory, string mxf_name)
46         : MXFAsset (directory, mxf_name)
47         , _channels (0)
48         , _sampling_rate (0)
49 {
50
51 }
52
53 void
54 SoundAsset::create (vector<boost::filesystem::path> const & files)
55 {
56         create (boost::bind (&SoundAsset::path_from_channel, this, _1, files));
57 }
58
59 void
60 SoundAsset::read ()
61 {
62         ASDCP::PCM::MXFReader reader;
63         if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
64                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
65         }
66
67         ASDCP::PCM::AudioDescriptor desc;
68         if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
69                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
70         }
71
72         _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
73         _channels = desc.ChannelCount;
74         _edit_rate = desc.EditRate.Numerator;
75         assert (desc.EditRate.Denominator == 1);
76         _intrinsic_duration = desc.ContainerDuration;
77 }
78
79 boost::filesystem::path
80 SoundAsset::path_from_channel (Channel channel, vector<boost::filesystem::path> const & files)
81 {
82         unsigned int const c = int (channel);
83         assert (c < files.size ());
84         return files[c];
85 }
86
87 void
88 SoundAsset::create (boost::function<boost::filesystem::path (Channel)> get_path)
89 {
90         ASDCP::Rational asdcp_edit_rate (_edit_rate, 1);
91
92         assert (_channels > 0);
93         ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
94         if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_edit_rate)) {
95                 boost::throw_exception (FileError ("could not open WAV file for reading", get_path(LEFT)));
96         }
97         
98         ASDCP::PCM::AudioDescriptor audio_desc;
99         pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
100         audio_desc.ChannelCount = 0;
101         audio_desc.BlockAlign = 0;
102         audio_desc.EditRate = asdcp_edit_rate;
103         audio_desc.AvgBps = audio_desc.AvgBps * _channels;
104
105         Channel channels[] = {
106                 LEFT,
107                 RIGHT,
108                 CENTRE,
109                 LFE,
110                 LS,
111                 RS,
112                 /* XXX: not quite sure what these should be yet */
113                 CHANNEL_7,
114                 CHANNEL_8
115         };
116
117         assert (int(_channels) <= int(sizeof(channels) / sizeof(Channel)));
118
119         ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
120         ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
121
122         for (int i = 0; i < _channels; ++i) {
123
124                 boost::filesystem::path const path = get_path (channels[i]);
125                 
126                 if (ASDCP_FAILURE (pcm_parser_channel[i].OpenRead (path.c_str(), asdcp_edit_rate))) {
127                         boost::throw_exception (FileError ("could not open WAV file for reading", path));
128                 }
129
130                 pcm_parser_channel[i].FillAudioDescriptor (audio_desc_channel[i]);
131                 frame_buffer_channel[i].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[i]));
132
133                 audio_desc.ChannelCount += audio_desc_channel[i].ChannelCount;
134                 audio_desc.BlockAlign += audio_desc_channel[i].BlockAlign;
135         }
136
137         ASDCP::PCM::FrameBuffer frame_buffer;
138         frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
139         frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
140
141         ASDCP::WriterInfo writer_info;
142         MXFAsset::fill_writer_info (&writer_info, _uuid, _interop, _metadata);
143
144         ASDCP::PCM::MXFWriter mxf_writer;
145         if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc))) {
146                 boost::throw_exception (FileError ("could not open audio MXF for writing", path().string()));
147         }
148         
149         for (int i = 0; i < _intrinsic_duration; ++i) {
150
151                 for (int j = 0; j < _channels; ++j) {
152                         memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
153                         if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
154                                 boost::throw_exception (MiscError ("could not read audio frame"));
155                         }
156                 }
157
158                 byte_t *data_s = frame_buffer.Data();
159                 byte_t *data_e = data_s + frame_buffer.Capacity();
160                 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
161                 int offset = 0;
162
163                 while (data_s < data_e) {
164                         for (int j = 0; j < _channels; ++j) {
165                                 byte_t* frame = frame_buffer_channel[j].Data() + offset;
166                                 memcpy (data_s, frame, sample_size);
167                                 data_s += sample_size;
168                         }
169                         offset += sample_size;
170                 }
171
172                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0))) {
173                         boost::throw_exception (MiscError ("could not write audio MXF frame"));
174                 }
175
176                 if (_progress) {
177                         (*_progress) (0.5 * float (i) / _intrinsic_duration);
178                 }
179         }
180
181         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
182                 boost::throw_exception (MiscError ("could not finalise audio MXF"));
183         }
184 }
185
186 string
187 SoundAsset::cpl_node_name () const
188 {
189         return "MainSound";
190 }
191
192 bool
193 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
194 {
195         if (!MXFAsset::equals (other, opt, note)) {
196                 return false;
197         }
198                      
199         ASDCP::PCM::MXFReader reader_A;
200         if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
201                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
202         }
203
204         ASDCP::PCM::MXFReader reader_B;
205         if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
206                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
207         }
208
209         ASDCP::PCM::AudioDescriptor desc_A;
210         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
211                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
212         }
213         ASDCP::PCM::AudioDescriptor desc_B;
214         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
215                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
216         }
217         
218         if (
219                 desc_A.EditRate != desc_B.EditRate ||
220                 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
221                 desc_A.Locked != desc_B.Locked ||
222                 desc_A.ChannelCount != desc_B.ChannelCount ||
223                 desc_A.QuantizationBits != desc_B.QuantizationBits ||
224                 desc_A.BlockAlign != desc_B.BlockAlign ||
225                 desc_A.AvgBps != desc_B.AvgBps ||
226                 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
227                 desc_A.ContainerDuration != desc_B.ContainerDuration
228 //              desc_A.ChannelFormat != desc_B.ChannelFormat ||
229                 ) {
230                 
231                 note (ERROR, "audio MXF picture descriptors differ");
232                 return false;
233         }
234         
235         ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
236         ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
237         
238         for (int i = 0; i < _intrinsic_duration; ++i) {
239                 if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
240                         boost::throw_exception (DCPReadError ("could not read audio frame"));
241                 }
242                 
243                 if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
244                         boost::throw_exception (DCPReadError ("could not read audio frame"));
245                 }
246                 
247                 if (buffer_A.Size() != buffer_B.Size()) {
248                         note (ERROR, "sizes of audio data for frame " + lexical_cast<string>(i) + " differ");
249                         return false;
250                 }
251                 
252                 if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
253                         for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
254                                 int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[i]);
255                                 if (d > opt.max_audio_sample_error) {
256                                         note (ERROR, "PCM data difference of " + lexical_cast<string> (d));
257                                         return false;
258                                 }
259                         }
260                 }
261         }
262
263         return true;
264 }
265
266 shared_ptr<const SoundFrame>
267 SoundAsset::get_frame (int n) const
268 {
269         /* XXX: should add on entry point here? */
270         return shared_ptr<const SoundFrame> (new SoundFrame (path().string(), n, _decryption_context));
271 }
272
273 shared_ptr<SoundAssetWriter>
274 SoundAsset::start_write ()
275 {
276         /* XXX: can't we use a shared_ptr here? */
277         return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this));
278 }
279
280 struct SoundAssetWriter::ASDCPState
281 {
282         ASDCP::PCM::MXFWriter mxf_writer;
283         ASDCP::PCM::FrameBuffer frame_buffer;
284         ASDCP::WriterInfo writer_info;
285         ASDCP::PCM::AudioDescriptor audio_desc;
286         ASDCP::AESEncContext* encryption_context;
287 };
288
289 SoundAssetWriter::SoundAssetWriter (SoundAsset* a)
290         : _state (new SoundAssetWriter::ASDCPState)
291         , _asset (a)
292         , _finalized (false)
293         , _frames_written (0)
294         , _frame_buffer_offset (0)
295 {
296         _state->encryption_context = a->encryption_context ();
297         
298         /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
299         _state->audio_desc.EditRate = ASDCP::Rational (_asset->edit_rate(), 1);
300         _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_asset->sampling_rate(), 1);
301         _state->audio_desc.Locked = 0;
302         _state->audio_desc.ChannelCount = _asset->channels ();
303         _state->audio_desc.QuantizationBits = 24;
304         _state->audio_desc.BlockAlign = 3 * _asset->channels();
305         _state->audio_desc.AvgBps = _asset->sampling_rate() * _state->audio_desc.BlockAlign;
306         _state->audio_desc.LinkedTrackID = 0;
307         _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE;
308         
309         _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
310         _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
311         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
312         
313         _asset->fill_writer_info (&_state->writer_info, _asset->uuid (), _asset->interop(), _asset->metadata());
314         
315         if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (_asset->path().string().c_str(), _state->writer_info, _state->audio_desc))) {
316                 boost::throw_exception (FileError ("could not open audio MXF for writing", _asset->path().string()));
317         }
318 }
319
320 void
321 SoundAssetWriter::write (float const * const * data, int frames)
322 {
323         for (int i = 0; i < frames; ++i) {
324
325                 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
326
327                 /* Write one sample per channel */
328                 for (int j = 0; j < _asset->channels(); ++j) {
329                         int32_t const s = data[j][i] * (1 << 23);
330                         *out++ = (s & 0xff);
331                         *out++ = (s & 0xff00) >> 8;
332                         *out++ = (s & 0xff0000) >> 16;
333                 }
334                 _frame_buffer_offset += 3 * _asset->channels();
335
336                 assert (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
337
338                 /* Finish the MXF frame if required */
339                 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
340                         write_current_frame ();
341                         _frame_buffer_offset = 0;
342                         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
343                 }
344         }
345 }
346
347 void
348 SoundAssetWriter::write_current_frame ()
349 {
350         if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0))) {
351                 boost::throw_exception (MiscError ("could not write audio MXF frame"));
352         }
353
354         ++_frames_written;
355 }
356
357 void
358 SoundAssetWriter::finalize ()
359 {
360         if (_frame_buffer_offset > 0) {
361                 write_current_frame ();
362         }
363         
364         if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
365                 boost::throw_exception (MiscError ("could not finalise audio MXF"));
366         }
367
368         _finalized = true;
369         _asset->set_intrinsic_duration (_frames_written);
370         _asset->set_duration (_frames_written);
371 }
372
373 string
374 SoundAsset::key_type () const
375 {
376         return "MDAK";
377 }