Allow incremental writing of picture MXFs.
[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, int fps, int intrinsic_duration)
79         : MXFAsset (directory, mxf_name, 0, fps, intrinsic_duration)
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         
89         ASDCP::PCM::AudioDescriptor desc;
90         if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
91                 throw DCPReadError ("could not read audio MXF information");
92         }
93
94         _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
95         _channels = desc.ChannelCount;
96 }
97
98 string
99 SoundAsset::path_from_channel (Channel channel, vector<string> const & files)
100 {
101         unsigned int const c = int (channel);
102         assert (c < files.size ());
103         return files[c];
104 }
105
106 void
107 SoundAsset::construct (boost::function<string (Channel)> get_path)
108 {
109         ASDCP::Rational asdcp_fps (_fps, 1);
110
111         ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
112         if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_fps)) {
113                 throw FileError ("could not open WAV file for reading", get_path(LEFT));
114         }
115         
116         ASDCP::PCM::AudioDescriptor audio_desc;
117         pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
118         audio_desc.ChannelCount = 0;
119         audio_desc.BlockAlign = 0;
120         audio_desc.EditRate = asdcp_fps;
121         audio_desc.AvgBps = audio_desc.AvgBps * _channels;
122
123         Channel channels[] = {
124                 LEFT,
125                 RIGHT,
126                 CENTRE,
127                 LFE,
128                 LS,
129                 RS,
130                 /* XXX: not quite sure what these should be yet */
131                 CHANNEL_7,
132                 CHANNEL_8
133         };
134
135         assert (int(_channels) <= int(sizeof(channels) / sizeof(Channel)));
136
137         ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
138         ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
139
140         for (int i = 0; i < _channels; ++i) {
141
142                 string const path = get_path (channels[i]);
143                 
144                 if (ASDCP_FAILURE (pcm_parser_channel[i].OpenRead (path.c_str(), asdcp_fps))) {
145                         throw FileError ("could not open WAV file for reading", path);
146                 }
147
148                 pcm_parser_channel[i].FillAudioDescriptor (audio_desc_channel[i]);
149                 frame_buffer_channel[i].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[i]));
150
151                 audio_desc.ChannelCount += audio_desc_channel[i].ChannelCount;
152                 audio_desc.BlockAlign += audio_desc_channel[i].BlockAlign;
153         }
154
155         ASDCP::PCM::FrameBuffer frame_buffer;
156         frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
157         frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
158
159         ASDCP::WriterInfo writer_info;
160         fill_writer_info (&writer_info, _uuid);
161
162         ASDCP::PCM::MXFWriter mxf_writer;
163         if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc))) {
164                 throw FileError ("could not open audio MXF for writing", path().string());
165         }
166
167         /* Skip through up to our _start_frame; this is pretty inefficient... */
168         for (int i = 0; i < _start_frame; ++i) {
169                 for (int j = 0; j < _channels; ++j) {
170                         if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
171                                 throw MiscError ("could not read audio frame");
172                         }
173                 }
174         }
175         
176         for (int i = 0; i < _intrinsic_duration; ++i) {
177
178                 for (int j = 0; j < _channels; ++j) {
179                         memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
180                         if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
181                                 throw MiscError ("could not read audio frame");
182                         }
183                 }
184
185                 byte_t *data_s = frame_buffer.Data();
186                 byte_t *data_e = data_s + frame_buffer.Capacity();
187                 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
188                 int offset = 0;
189
190                 while (data_s < data_e) {
191                         for (int j = 0; j < _channels; ++j) {
192                                 byte_t* frame = frame_buffer_channel[j].Data() + offset;
193                                 memcpy (data_s, frame, sample_size);
194                                 data_s += sample_size;
195                         }
196                         offset += sample_size;
197                 }
198
199                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
200                         throw MiscError ("could not write audio MXF frame");
201                 }
202
203                 if (_progress) {
204                         (*_progress) (0.5 * float (i) / _intrinsic_duration);
205                 }
206         }
207
208         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
209                 throw MiscError ("could not finalise audio MXF");
210         }
211 }
212
213 void
214 SoundAsset::write_to_cpl (ostream& s) const
215 {
216         s << "        <MainSound>\n"
217           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
218           << "          <AnnotationText>" << _file_name << "</AnnotationText>\n"
219           << "          <EditRate>" << _fps << " 1</EditRate>\n"
220           << "          <IntrinsicDuration>" << _intrinsic_duration << "</IntrinsicDuration>\n"
221           << "          <EntryPoint>" << _entry_point << "</EntryPoint>\n"
222           << "          <Duration>" << _duration << "</Duration>\n"
223           << "        </MainSound>\n";
224 }
225
226 bool
227 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<string>& notes) const
228 {
229         if (!MXFAsset::equals (other, opt, notes)) {
230                 return false;
231         }
232                      
233         ASDCP::PCM::MXFReader reader_A;
234         if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
235                 throw MXFFileError ("could not open MXF file for reading", path().string());
236         }
237
238         ASDCP::PCM::MXFReader reader_B;
239         if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
240                 throw MXFFileError ("could not open MXF file for reading", path().string());
241         }
242
243         ASDCP::PCM::AudioDescriptor desc_A;
244         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
245                 throw DCPReadError ("could not read audio MXF information");
246         }
247         ASDCP::PCM::AudioDescriptor desc_B;
248         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
249                 throw DCPReadError ("could not read audio MXF information");
250         }
251         
252         if (
253                 desc_A.EditRate != desc_B.EditRate ||
254                 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
255                 desc_A.Locked != desc_B.Locked ||
256                 desc_A.ChannelCount != desc_B.ChannelCount ||
257                 desc_A.QuantizationBits != desc_B.QuantizationBits ||
258                 desc_A.BlockAlign != desc_B.BlockAlign ||
259                 desc_A.AvgBps != desc_B.AvgBps ||
260                 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
261                 desc_A.ContainerDuration != desc_B.ContainerDuration
262 //              desc_A.ChannelFormat != desc_B.ChannelFormat ||
263                 ) {
264                 
265                 notes.push_back ("audio MXF picture descriptors differ");
266                 return false;
267         }
268         
269         ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
270         ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
271         
272         for (int i = 0; i < _intrinsic_duration; ++i) {
273                 if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
274                         throw DCPReadError ("could not read audio frame");
275                 }
276                 
277                 if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
278                         throw DCPReadError ("could not read audio frame");
279                 }
280                 
281                 if (buffer_A.Size() != buffer_B.Size()) {
282                         notes.push_back ("sizes of audio data for frame " + lexical_cast<string>(i) + " differ");
283                         return false;
284                 }
285                 
286                 if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
287                         for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
288                                 int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[i]);
289                                 if (d > opt.max_audio_sample_error) {
290                                         notes.push_back ("PCM data difference of " + lexical_cast<string> (d));
291                                         return false;
292                                 }
293                         }
294                 }
295         }
296
297         return true;
298 }
299
300 shared_ptr<const SoundFrame>
301 SoundAsset::get_frame (int n) const
302 {
303         return shared_ptr<const SoundFrame> (new SoundFrame (path().string(), n + _entry_point));
304 }