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