Allow incremental writing of picture MXFs.
[libdcp.git] / src / sound_asset.cc
index ed815eda96549c45cc3cb6672c79e6659f538ab2..04ca88da3a7a22cf2a6c36598d7a9066d2e340f6 100644 (file)
 #include "sound_asset.h"
 #include "util.h"
 #include "exceptions.h"
-
-using namespace std;
-using namespace boost;
+#include "sound_frame.h"
+
+using std::string;
+using std::stringstream;
+using std::ostream;
+using std::vector;
+using std::list;
+using boost::shared_ptr;
+using boost::lexical_cast;
 using namespace libdcp;
 
 SoundAsset::SoundAsset (
-       vector<string> const & files, string directory, string mxf_name, sigc::signal1<void, float>* progress, int fps, int length
+       vector<string> const & files,
+       string directory,
+       string mxf_name,
+       boost::signals2::signal<void (float)>* progress,
+       int fps, int intrinsic_duration, int start_frame
        )
-       : Asset (directory, mxf_name, progress, fps, length)
+       : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration)
        , _channels (files.size ())
+       , _sampling_rate (0)
+       , _start_frame (start_frame)
 {
-       construct (sigc::bind (sigc::mem_fun (*this, &SoundAsset::path_from_channel), files));
+       assert (_channels);
+       
+       construct (boost::bind (&SoundAsset::path_from_channel, this, _1, files));
 }
 
 SoundAsset::SoundAsset (
-       sigc::slot<string, Channel> get_path, string directory, string mxf_name, sigc::signal1<void, float>* progress, int fps, int length, int channels
+       boost::function<string (Channel)> get_path,
+       string directory,
+       string mxf_name,
+       boost::signals2::signal<void (float)>* progress,
+       int fps, int intrinsic_duration, int start_frame, int channels
        )
-       : Asset (directory, mxf_name, progress, fps, length)
+       : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration)
        , _channels (channels)
+       , _sampling_rate (0)
+       , _start_frame (start_frame)
 {
+       assert (_channels);
+       
        construct (get_path);
 }
 
-SoundAsset::SoundAsset (string directory, string mxf_name, int fps, int length)
-       : Asset (directory, mxf_name, 0, fps, length)
+SoundAsset::SoundAsset (string directory, string mxf_name, int fps, int intrinsic_duration)
+       : MXFAsset (directory, mxf_name, 0, fps, intrinsic_duration)
        , _channels (0)
+       , _start_frame (0)
 {
+       ASDCP::PCM::MXFReader reader;
+       if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
+               throw MXFFileError ("could not open MXF file for reading", path().string());
+       }
+
+       
+       ASDCP::PCM::AudioDescriptor desc;
+       if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
+               throw DCPReadError ("could not read audio MXF information");
+       }
 
+       _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
+       _channels = desc.ChannelCount;
 }
 
 string
@@ -69,10 +104,10 @@ SoundAsset::path_from_channel (Channel channel, vector<string> const & files)
 }
 
 void
-SoundAsset::construct (sigc::slot<string, Channel> get_path)
+SoundAsset::construct (boost::function<string (Channel)> get_path)
 {
        ASDCP::Rational asdcp_fps (_fps, 1);
-       
+
        ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
        if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_fps)) {
                throw FileError ("could not open WAV file for reading", get_path(LEFT));
@@ -91,9 +126,14 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
                CENTRE,
                LFE,
                LS,
-               RS
+               RS,
+               /* XXX: not quite sure what these should be yet */
+               CHANNEL_7,
+               CHANNEL_8
        };
 
+       assert (int(_channels) <= int(sizeof(channels) / sizeof(Channel)));
+
        ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
        ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
 
@@ -117,31 +157,36 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
        frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
 
        ASDCP::WriterInfo writer_info;
-       fill_writer_info (&writer_info);
+       fill_writer_info (&writer_info, _uuid);
 
        ASDCP::PCM::MXFWriter mxf_writer;
-       if (ASDCP_FAILURE (mxf_writer.OpenWrite (mxf_path().string().c_str(), writer_info, audio_desc))) {
-               throw FileError ("could not open audio MXF for writing", mxf_path().string());
+       if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc))) {
+               throw FileError ("could not open audio MXF for writing", path().string());
        }
 
-       for (int i = 0; i < _length; ++i) {
-
-               byte_t *data_s = frame_buffer.Data();
-               byte_t *data_e = data_s + frame_buffer.Capacity();
-               byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
-               int offset = 0;
+       /* Skip through up to our _start_frame; this is pretty inefficient... */
+       for (int i = 0; i < _start_frame; ++i) {
+               for (int j = 0; j < _channels; ++j) {
+                       if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
+                               throw MiscError ("could not read audio frame");
+                       }
+               }
+       }
+       
+       for (int i = 0; i < _intrinsic_duration; ++i) {
 
                for (int j = 0; j < _channels; ++j) {
                        memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
                        if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
                                throw MiscError ("could not read audio frame");
                        }
-                       
-                       if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
-                               throw MiscError ("short audio frame");
-                       }
                }
 
+               byte_t *data_s = frame_buffer.Data();
+               byte_t *data_e = data_s + frame_buffer.Capacity();
+               byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
+               int offset = 0;
+
                while (data_s < data_e) {
                        for (int j = 0; j < _channels; ++j) {
                                byte_t* frame = frame_buffer_channel[j].Data() + offset;
@@ -155,7 +200,9 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
                        throw MiscError ("could not write audio MXF frame");
                }
 
-               (*_progress) (0.5 * float (i) / _length);
+               if (_progress) {
+                       (*_progress) (0.5 * float (i) / _intrinsic_duration);
+               }
        }
 
        if (ASDCP_FAILURE (mxf_writer.Finalize())) {
@@ -168,80 +215,90 @@ SoundAsset::write_to_cpl (ostream& s) const
 {
        s << "        <MainSound>\n"
          << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
-         << "          <AnnotationText>" << _mxf_name << "</AnnotationText>\n"
+         << "          <AnnotationText>" << _file_name << "</AnnotationText>\n"
          << "          <EditRate>" << _fps << " 1</EditRate>\n"
-         << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
-         << "          <EntryPoint>0</EntryPoint>\n"
-         << "          <Duration>" << _length << "</Duration>\n"
+         << "          <IntrinsicDuration>" << _intrinsic_duration << "</IntrinsicDuration>\n"
+         << "          <EntryPoint>" << _entry_point << "</EntryPoint>\n"
+         << "          <Duration>" << _duration << "</Duration>\n"
          << "        </MainSound>\n";
 }
 
-list<string>
-SoundAsset::equals (shared_ptr<const Asset> other, EqualityFlags flags) const
+bool
+SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<string>& notes) const
 {
-       list<string> notes = Asset::equals (other, flags);
+       if (!MXFAsset::equals (other, opt, notes)) {
+               return false;
+       }
                     
-       if (flags & MXF_INSPECT) {
-               ASDCP::PCM::MXFReader reader_A;
-               if (ASDCP_FAILURE (reader_A.OpenRead (mxf_path().string().c_str()))) {
-                       cout << "failed " << mxf_path() << "\n";
-                       throw FileError ("could not open MXF file for reading", mxf_path().string());
-               }
+       ASDCP::PCM::MXFReader reader_A;
+       if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
+               throw MXFFileError ("could not open MXF file for reading", path().string());
+       }
 
-               ASDCP::PCM::MXFReader reader_B;
-               if (ASDCP_FAILURE (reader_B.OpenRead (other->mxf_path().string().c_str()))) {
-                       cout << "failed " << other->mxf_path() << "\n";
-                       throw FileError ("could not open MXF file for reading", mxf_path().string());
-               }
+       ASDCP::PCM::MXFReader reader_B;
+       if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
+               throw MXFFileError ("could not open MXF file for reading", path().string());
+       }
 
-               ASDCP::PCM::AudioDescriptor desc_A;
-               if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
-                       throw DCPReadError ("could not read audio MXF information");
+       ASDCP::PCM::AudioDescriptor desc_A;
+       if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
+               throw DCPReadError ("could not read audio MXF information");
+       }
+       ASDCP::PCM::AudioDescriptor desc_B;
+       if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
+               throw DCPReadError ("could not read audio MXF information");
+       }
+       
+       if (
+               desc_A.EditRate != desc_B.EditRate ||
+               desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
+               desc_A.Locked != desc_B.Locked ||
+               desc_A.ChannelCount != desc_B.ChannelCount ||
+               desc_A.QuantizationBits != desc_B.QuantizationBits ||
+               desc_A.BlockAlign != desc_B.BlockAlign ||
+               desc_A.AvgBps != desc_B.AvgBps ||
+               desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
+               desc_A.ContainerDuration != desc_B.ContainerDuration
+//             desc_A.ChannelFormat != desc_B.ChannelFormat ||
+               ) {
+               
+               notes.push_back ("audio MXF picture descriptors differ");
+               return false;
+       }
+       
+       ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
+       ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
+       
+       for (int i = 0; i < _intrinsic_duration; ++i) {
+               if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
+                       throw DCPReadError ("could not read audio frame");
                }
-               ASDCP::PCM::AudioDescriptor desc_B;
-               if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
-                       throw DCPReadError ("could not read audio MXF information");
+               
+               if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
+                       throw DCPReadError ("could not read audio frame");
                }
-
-               if (
-                       desc_A.EditRate != desc_B.EditRate ||
-                       desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
-                       desc_A.Locked != desc_B.Locked ||
-                       desc_A.ChannelCount != desc_B.ChannelCount ||
-                       desc_A.QuantizationBits != desc_B.QuantizationBits ||
-                       desc_A.BlockAlign != desc_B.BlockAlign ||
-                       desc_A.AvgBps != desc_B.AvgBps ||
-                       desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
-                       desc_A.ContainerDuration != desc_B.ContainerDuration
-//                     desc_A.ChannelFormat != desc_B.ChannelFormat ||
-                       ) {
                
-                       notes.push_back ("audio MXF picture descriptors differ");
+               if (buffer_A.Size() != buffer_B.Size()) {
+                       notes.push_back ("sizes of audio data for frame " + lexical_cast<string>(i) + " differ");
+                       return false;
                }
-
-               ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
-               ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
-
-               for (int i = 0; i < _length; ++i) {
-                       if (ASDCP_FAILURE (reader_A.ReadFrame (0, buffer_A))) {
-                               throw DCPReadError ("could not read audio frame");
-                       }
-
-                       if (ASDCP_FAILURE (reader_B.ReadFrame (0, buffer_B))) {
-                               throw DCPReadError ("could not read audio frame");
-                       }
-
-                       if (buffer_A.Size() != buffer_B.Size()) {
-                               notes.push_back ("sizes of video data for frame " + lexical_cast<string>(i) + " differ");
-                               continue;
-                       }
-
-                       if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
-                               notes.push_back ("PCM data for frame " + lexical_cast<string>(i) + " differ");
-                               continue;
+               
+               if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
+                       for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
+                               int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[i]);
+                               if (d > opt.max_audio_sample_error) {
+                                       notes.push_back ("PCM data difference of " + lexical_cast<string> (d));
+                                       return false;
+                               }
                        }
                }
        }
 
-       return notes;
+       return true;
+}
+
+shared_ptr<const SoundFrame>
+SoundAsset::get_frame (int n) const
+{
+       return shared_ptr<const SoundFrame> (new SoundFrame (path().string(), n + _entry_point));
 }