Bump asdcplib to dcpomatic-2.13.0 branch.
[libdcp.git] / src / sound_asset.cc
index 0eaa8fb50f69269c66b86b37e428a4f0e0368707..90075278566de02278843e2c9ff1e0ab69711022 100644 (file)
@@ -39,7 +39,9 @@
 
 #include "compose.hpp"
 #include "dcp_assert.h"
+#include "equality_options.h"
 #include "exceptions.h"
+#include "filesystem.h"
 #include "sound_asset.h"
 #include "sound_asset_reader.h"
 #include "sound_asset_writer.h"
@@ -56,19 +58,21 @@ LIBDCP_ENABLE_WARNINGS
 #include <stdexcept>
 
 
-using std::string;
-using std::vector;
+using std::dynamic_pointer_cast;
 using std::list;
 using std::shared_ptr;
-using std::dynamic_pointer_cast;
+using std::string;
+using std::vector;
+using boost::optional;
 using namespace dcp;
 
 
 SoundAsset::SoundAsset (boost::filesystem::path file)
        : Asset (file)
 {
-       ASDCP::PCM::MXFReader reader;
-       auto r = reader.OpenRead (file.string().c_str());
+       Kumu::FileReaderFactory factory;
+       ASDCP::PCM::MXFReader reader(factory);
+       auto r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
        if (ASDCP_FAILURE(r)) {
                boost::throw_exception (MXFFileError("could not open MXF file for reading", file.string(), r));
        }
@@ -78,7 +82,7 @@ SoundAsset::SoundAsset (boost::filesystem::path file)
                boost::throw_exception (ReadError("could not read audio MXF information"));
        }
 
-       _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
+       _sampling_rate = desc.AudioSamplingRate.Denominator ? (desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator) : 0;
        _channels = desc.ChannelCount;
        _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
 
@@ -103,6 +107,16 @@ SoundAsset::SoundAsset (boost::filesystem::path file)
                }
        }
 
+       list<ASDCP::MXF::InterchangeObject*> channel_labels;
+       rr = reader.OP1aHeader().GetMDObjectsByType(
+               asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
+               channel_labels
+               );
+
+       if (KM_SUCCESS(rr)) {
+               _active_channels = channel_labels.size();
+       }
+
        _id = read_writer_info (info);
 }
 
@@ -119,17 +133,22 @@ SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels, Lan
 
 
 bool
-SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
+SoundAsset::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
 {
-       ASDCP::PCM::MXFReader reader_A;
+       if (opt.sound_assets_can_differ) {
+               return true;
+       }
+
+       Kumu::FileReaderFactory factory;
+       ASDCP::PCM::MXFReader reader_A(factory);
        DCP_ASSERT (file());
-       auto r = reader_A.OpenRead (file()->string().c_str());
+       auto r = reader_A.OpenRead(dcp::filesystem::fix_long_path(*file()).string().c_str());
        if (ASDCP_FAILURE(r)) {
                boost::throw_exception (MXFFileError("could not open MXF file for reading", file()->string(), r));
        }
 
-       ASDCP::PCM::MXFReader reader_B;
-       r = reader_B.OpenRead (other->file()->string().c_str());
+       ASDCP::PCM::MXFReader reader_B(factory);
+       r = reader_B.OpenRead(dcp::filesystem::fix_long_path(*other->file()).string().c_str());
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (MXFFileError("could not open MXF file for reading", other->file()->string(), r));
        }
@@ -220,13 +239,20 @@ SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHand
 
 
 shared_ptr<SoundAssetWriter>
-SoundAsset::start_write (boost::filesystem::path file, bool atmos_sync)
+SoundAsset::start_write(
+       boost::filesystem::path file,
+       vector<dcp::Channel> extra_active_channels,
+       AtmosSync atmos_sync,
+       MCASubDescriptors include_mca_subdescriptors
+       )
 {
-       if (atmos_sync && _channels < 14) {
+       if (atmos_sync == AtmosSync::ENABLED && _channels < 14) {
                throw MiscError ("Insufficient channels to write ATMOS sync (there must be at least 14)");
        }
 
-       return shared_ptr<SoundAssetWriter> (new SoundAssetWriter(this, file, atmos_sync));
+       return shared_ptr<SoundAssetWriter>(
+               new SoundAssetWriter(this, file, extra_active_channels, atmos_sync == AtmosSync::ENABLED, include_mca_subdescriptors == MCASubDescriptors::ENABLED)
+               );
 }
 
 
@@ -254,7 +280,16 @@ SoundAsset::static_pkl_type (Standard standard)
 bool
 SoundAsset::valid_mxf (boost::filesystem::path file)
 {
-       ASDCP::PCM::MXFReader reader;
-       Kumu::Result_t r = reader.OpenRead (file.string().c_str());
+       Kumu::FileReaderFactory factory;
+       ASDCP::PCM::MXFReader reader(factory);
+       Kumu::Result_t r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
        return !ASDCP_FAILURE (r);
 }
+
+
+int
+SoundAsset::active_channels() const
+{
+       return _active_channels.get_value_or(_channels);
+}
+