X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fsound_asset_writer.cc;h=8df4d911a0334377bbe01f7b71e71581cba0f648;hb=8c852cbececaa94e7efbb0e8508484e073b06c67;hp=46321a857f9648dd30e285aa3f5fae86664010c6;hpb=f9cba324c8160a70b108d9e5b60a4ccad6ee9be2;p=libdcp.git diff --git a/src/sound_asset_writer.cc b/src/sound_asset_writer.cc index 46321a85..8df4d911 100644 --- a/src/sound_asset_writer.cc +++ b/src/sound_asset_writer.cc @@ -16,6 +16,19 @@ You should have received a copy of the GNU General Public License along with libdcp. If not, see . + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. */ #include "sound_asset_writer.h" @@ -23,10 +36,13 @@ #include "exceptions.h" #include "dcp_assert.h" #include "compose.hpp" -#include "AS_DCP.h" +#include "crypto_context.h" +#include +#include using std::min; using std::max; +using std::cout; using namespace dcp; struct SoundAssetWriter::ASDCPState @@ -34,63 +50,84 @@ struct SoundAssetWriter::ASDCPState ASDCP::PCM::MXFWriter mxf_writer; ASDCP::PCM::FrameBuffer frame_buffer; ASDCP::WriterInfo writer_info; - ASDCP::PCM::AudioDescriptor audio_desc; + ASDCP::PCM::AudioDescriptor desc; }; -SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, Standard standard) - : AssetWriter (asset, file, standard) +SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file) + : AssetWriter (asset, file) , _state (new SoundAssetWriter::ASDCPState) - , _sound_asset (asset) + , _asset (asset) , _frame_buffer_offset (0) { /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */ - _state->audio_desc.EditRate = ASDCP::Rational (_sound_asset->edit_rate().numerator, _sound_asset->edit_rate().denominator); - _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_sound_asset->sampling_rate(), 1); - _state->audio_desc.Locked = 0; - _state->audio_desc.ChannelCount = _sound_asset->channels (); - _state->audio_desc.QuantizationBits = 24; - _state->audio_desc.BlockAlign = 3 * _sound_asset->channels(); - _state->audio_desc.AvgBps = _sound_asset->sampling_rate() * _state->audio_desc.BlockAlign; - _state->audio_desc.LinkedTrackID = 0; - _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE; - - _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc)); - _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc)); + _state->desc.EditRate = ASDCP::Rational (_asset->edit_rate().numerator, _asset->edit_rate().denominator); + _state->desc.AudioSamplingRate = ASDCP::Rational (_asset->sampling_rate(), 1); + _state->desc.Locked = 0; + _state->desc.ChannelCount = _asset->channels (); + _state->desc.QuantizationBits = 24; + _state->desc.BlockAlign = 3 * _asset->channels(); + _state->desc.AvgBps = _asset->sampling_rate() * _state->desc.BlockAlign; + _state->desc.LinkedTrackID = 0; + if (asset->standard() == INTEROP) { + _state->desc.ChannelFormat = ASDCP::PCM::CF_NONE; + } else { + /* Just use WTF ("wild track format") for SMPTE for now; searches suggest that this + uses the same assignment as Interop. + */ + _state->desc.ChannelFormat = ASDCP::PCM::CF_CFG_4; + } + + /* I'm fairly sure this is not necessary, as ContainerDuration is written + in ASDCP's WriteMXFFooter, but it stops a valgrind warning. + */ + _state->desc.ContainerDuration = 0; + + _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->desc)); + _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->desc)); memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity()); - _sound_asset->fill_writer_info (&_state->writer_info, _sound_asset->id(), standard); + _asset->fill_writer_info (&_state->writer_info, _asset->id()); } void SoundAssetWriter::write (float const * const * data, int frames) { DCP_ASSERT (!_finalized); + DCP_ASSERT (frames > 0); static float const clip = 1.0f - (1.0f / pow (2, 23)); if (!_started) { - Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->audio_desc); + Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->desc); if (ASDCP_FAILURE (r)) { boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r)); } - _sound_asset->set_file (_file); + _asset->set_file (_file); _started = true; } + int const ch = _asset->channels (); + for (int i = 0; i < frames; ++i) { byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset; /* Write one sample per channel */ - for (int j = 0; j < _sound_asset->channels(); ++j) { + for (int j = 0; j < ch; ++j) { /* Convert sample to 24-bit int, clipping if necessary. */ - int32_t const s = min (clip, max (-clip, data[j][i])) * (1 << 23); + float x = data[j][i]; + if (x > clip) { + x = clip; + } else if (x < -clip) { + x = -clip; + } + int32_t const s = x * (1 << 23); *out++ = (s & 0xff); *out++ = (s & 0xff00) >> 8; *out++ = (s & 0xff0000) >> 16; } - _frame_buffer_offset += 3 * _sound_asset->channels(); + _frame_buffer_offset += 3 * ch; DCP_ASSERT (_frame_buffer_offset <= int (_state->frame_buffer.Capacity())); @@ -106,7 +143,7 @@ SoundAssetWriter::write (float const * const * data, int frames) void SoundAssetWriter::write_current_frame () { - ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _encryption_context, _hmac_context); + ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac()); if (ASDCP_FAILURE (r)) { boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r)))); } @@ -121,10 +158,13 @@ SoundAssetWriter::finalize () write_current_frame (); } - if (_started && ASDCP_FAILURE (_state->mxf_writer.Finalize())) { - boost::throw_exception (MiscError ("could not finalise audio MXF")); + if (_started) { + ASDCP::Result_t const r = _state->mxf_writer.Finalize(); + if (ASDCP_FAILURE(r)) { + boost::throw_exception (MiscError (String::compose ("could not finalise audio MXF (%1)", int(r)))); + } } - _sound_asset->_intrinsic_duration = _frames_written; + _asset->_intrinsic_duration = _frames_written; return AssetWriter::finalize (); }