2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
35 /** @file src/sound_asset_writer.cc
36 * @brief SoundAssetWriter class
40 #include "bitstream.h"
41 #include "compose.hpp"
42 #include "crypto_context.h"
43 #include "dcp_assert.h"
44 #include "exceptions.h"
45 #include "sound_asset.h"
46 #include "sound_asset_writer.h"
48 LIBDCP_DISABLE_WARNINGS
49 #include <asdcp/AS_DCP.h>
50 #include <asdcp/Metadata.h>
51 LIBDCP_ENABLE_WARNINGS
63 struct SoundAssetWriter::ASDCPState
65 ASDCP::PCM::MXFWriter mxf_writer;
66 ASDCP::PCM::FrameBuffer frame_buffer;
67 ASDCP::WriterInfo writer_info;
68 ASDCP::PCM::AudioDescriptor desc;
72 SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, bool sync, bool include_mca_subdescriptors)
73 : AssetWriter (asset, file)
74 , _state (new SoundAssetWriter::ASDCPState)
77 , _include_mca_subdescriptors(include_mca_subdescriptors)
79 DCP_ASSERT (!_sync || _asset->channels() >= 14);
80 DCP_ASSERT (!_sync || _asset->standard() == Standard::SMPTE);
82 /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
83 _state->desc.EditRate = ASDCP::Rational (_asset->edit_rate().numerator, _asset->edit_rate().denominator);
84 _state->desc.AudioSamplingRate = ASDCP::Rational (_asset->sampling_rate(), 1);
85 _state->desc.Locked = 0;
86 _state->desc.ChannelCount = _asset->channels();
87 _state->desc.QuantizationBits = 24;
88 _state->desc.BlockAlign = 3 * _asset->channels();
89 _state->desc.AvgBps = _asset->sampling_rate() * _state->desc.BlockAlign;
90 _state->desc.LinkedTrackID = 0;
91 if (asset->standard() == Standard::INTEROP) {
92 _state->desc.ChannelFormat = ASDCP::PCM::CF_NONE;
94 /* As required by Bv2.1 */
95 _state->desc.ChannelFormat = ASDCP::PCM::CF_CFG_4;
98 /* I'm fairly sure this is not necessary, as ContainerDuration is written
99 in ASDCP's WriteMXFFooter, but it stops a valgrind warning.
101 _state->desc.ContainerDuration = 0;
103 _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->desc));
104 _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->desc));
105 memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
107 _asset->fill_writer_info (&_state->writer_info, _asset->id());
110 _fsk.set_data (create_sync_packets());
115 SoundAssetWriter::~SoundAssetWriter()
118 /* Last-resort finalization to close the file, at least */
120 _state->mxf_writer.Finalize();
127 SoundAssetWriter::start ()
129 auto r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->desc);
130 if (ASDCP_FAILURE(r)) {
131 boost::throw_exception (FileError("could not open audio MXF for writing", _file.string(), r));
134 if (_asset->standard() == Standard::SMPTE && _include_mca_subdescriptors) {
136 ASDCP::MXF::WaveAudioDescriptor* essence_descriptor = nullptr;
137 _state->mxf_writer.OP1aHeader().GetMDObjectByType(
138 asdcp_smpte_dict->ul(ASDCP::MDD_WaveAudioDescriptor), reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&essence_descriptor)
140 DCP_ASSERT (essence_descriptor);
141 essence_descriptor->ChannelAssignment = asdcp_smpte_dict->ul(ASDCP::MDD_DCAudioChannelCfg_4_WTF);
143 auto soundfield = new ASDCP::MXF::SoundfieldGroupLabelSubDescriptor(asdcp_smpte_dict);
144 GenRandomValue (soundfield->MCALinkID);
145 if (auto lang = _asset->language()) {
146 soundfield->RFC5646SpokenLanguage = *lang;
149 const MCASoundField field = _asset->channels() > 10 ? MCASoundField::SEVEN_POINT_ONE : MCASoundField::FIVE_POINT_ONE;
151 if (field == MCASoundField::SEVEN_POINT_ONE) {
152 soundfield->MCATagSymbol = "sg71";
153 soundfield->MCATagName = "7.1DS";
154 LIBDCP_DISABLE_WARNINGS
155 soundfield->MCALabelDictionaryID = asdcp_smpte_dict->ul(ASDCP::MDD_DCAudioSoundfield_71);
156 LIBDCP_ENABLE_WARNINGS
158 soundfield->MCATagSymbol = "sg51";
159 soundfield->MCATagName = "5.1";
160 LIBDCP_DISABLE_WARNINGS
161 soundfield->MCALabelDictionaryID = asdcp_smpte_dict->ul(ASDCP::MDD_DCAudioSoundfield_51);
162 LIBDCP_ENABLE_WARNINGS
165 _state->mxf_writer.OP1aHeader().AddChildObject(soundfield);
166 essence_descriptor->SubDescriptors.push_back(soundfield->InstanceUID);
168 /* We must describe at least the number of channels in `field', even if they aren't
169 * in the asset (I think)
171 int descriptors = max(_asset->channels(), field == MCASoundField::FIVE_POINT_ONE ? 6 : 8);
173 auto const used = used_audio_channels();
175 for (auto i = 0; i < descriptors; ++i) {
176 auto dcp_channel = static_cast<dcp::Channel>(i);
177 if (find(used.begin(), used.end(), dcp_channel) == used.end()) {
180 auto channel = new ASDCP::MXF::AudioChannelLabelSubDescriptor(asdcp_smpte_dict);
181 GenRandomValue (channel->MCALinkID);
182 channel->SoundfieldGroupLinkID = soundfield->MCALinkID;
183 channel->MCAChannelID = i + 1;
184 channel->MCATagSymbol = "ch" + channel_to_mca_id(dcp_channel, field);
185 channel->MCATagName = channel_to_mca_name(dcp_channel, field);
186 if (auto lang = _asset->language()) {
187 channel->RFC5646SpokenLanguage = *lang;
189 LIBDCP_DISABLE_WARNINGS
190 channel->MCALabelDictionaryID = channel_to_mca_universal_label(dcp_channel, field, asdcp_smpte_dict);
191 LIBDCP_ENABLE_WARNINGS
192 _state->mxf_writer.OP1aHeader().AddChildObject(channel);
193 essence_descriptor->SubDescriptors.push_back(channel->InstanceUID);
197 _asset->set_file (_file);
203 SoundAssetWriter::write (float const * const * data, int frames)
205 DCP_ASSERT (!_finalized);
206 DCP_ASSERT (frames > 0);
208 static float const clip = 1.0f - (1.0f / pow (2, 23));
214 int const ch = _asset->channels ();
216 for (int i = 0; i < frames; ++i) {
218 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
220 /* Write one sample per channel */
221 for (int j = 0; j < ch; ++j) {
223 if (j == 13 && _sync) {
226 /* Convert sample to 24-bit int, clipping if necessary. */
227 float x = data[j][i];
230 } else if (x < -clip) {
236 *out++ = (s & 0xff00) >> 8;
237 *out++ = (s & 0xff0000) >> 16;
239 _frame_buffer_offset += 3 * ch;
241 DCP_ASSERT (_frame_buffer_offset <= int(_state->frame_buffer.Capacity()));
243 /* Finish the MXF frame if required */
244 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
245 write_current_frame ();
246 _frame_buffer_offset = 0;
247 memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
253 SoundAssetWriter::write_current_frame ()
255 auto const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
256 if (ASDCP_FAILURE(r)) {
257 boost::throw_exception (MiscError(String::compose("could not write audio MXF frame (%1)", static_cast<int>(r))));
263 /* We need a new set of sync packets for this frame */
264 _fsk.set_data (create_sync_packets());
269 SoundAssetWriter::finalize ()
271 if (_frame_buffer_offset > 0) {
272 write_current_frame ();
276 auto const r = _state->mxf_writer.Finalize();
277 if (ASDCP_FAILURE(r)) {
278 boost::throw_exception (MiscError(String::compose ("could not finalise audio MXF (%1)", static_cast<int>(r))));
282 _asset->_intrinsic_duration = _frames_written;
283 return AssetWriter::finalize ();
287 /** Calculate and return the sync packets required for this edit unit (aka "frame") */
289 SoundAssetWriter::create_sync_packets ()
291 /* Parts of this code assumes 48kHz */
292 DCP_ASSERT (_asset->sampling_rate() == 48000);
294 /* Encoding of edit rate */
295 int edit_rate_code = 0;
296 /* How many 0 bits are used to pad the end of the packet */
297 int remaining_bits = 0;
298 /* How many packets in this edit unit (i.e. "frame") */
300 auto const edit_rate = _asset->edit_rate ();
301 if (edit_rate == Fraction(24, 1)) {
305 } else if (edit_rate == Fraction(25, 1)) {
309 } else if (edit_rate == Fraction(30, 1)) {
313 } else if (edit_rate == Fraction(48, 1)) {
317 } else if (edit_rate == Fraction(50, 1)) {
321 } else if (edit_rate == Fraction(60, 1)) {
325 } else if (edit_rate == Fraction(96, 1)) {
329 } else if (edit_rate == Fraction(100, 1)) {
333 } else if (edit_rate == Fraction(120, 1)) {
342 DCP_ASSERT (id.DecodeHex(_asset->id().c_str()));
344 for (int i = 0; i < packets; ++i) {
345 bs.write_from_byte (0x4d);
346 bs.write_from_byte (0x56);
347 bs.start_crc (0x1021);
348 bs.write_from_byte (edit_rate_code, 4);
349 bs.write_from_byte (0, 2);
350 bs.write_from_byte (_sync_packet, 2);
351 bs.write_from_byte (id.Value()[i * 4 + 0]);
352 bs.write_from_byte (id.Value()[i * 4 + 1]);
353 bs.write_from_byte (id.Value()[i * 4 + 2]);
354 bs.write_from_byte (id.Value()[i * 4 + 3]);
355 bs.write_from_word (_frames_written, 24);
357 bs.write_from_byte (0, 4);
358 bs.write_from_word (0, remaining_bits);
361 if (_sync_packet == 4) {