summaryrefslogtreecommitdiff
path: root/src/sound_asset_writer.cc
blob: a7de871c0b96983f457bd3c83da84f15e36f5ba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>

    This file is part of libdcp.

    libdcp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libdcp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.

    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.
*/


/** @file  src/sound_asset_writer.cc
 *  @brief SoundAssetWriter class
 */


#include "bitstream.h"
#include "compose.hpp"
#include "crypto_context.h"
#include "dcp_assert.h"
#include "exceptions.h"
#include "filesystem.h"
#include "main_sound_configuration.h"
#include "sound_asset.h"
#include "sound_asset_writer.h"
#include "warnings.h"
LIBDCP_DISABLE_WARNINGS
#include <asdcp/AS_DCP.h>
#include <asdcp/Metadata.h>
LIBDCP_ENABLE_WARNINGS
#include <fmt/format.h>
#include <iostream>


using std::min;
using std::max;
using std::cout;
using std::string;
using std::vector;
using namespace dcp;


struct SoundAssetWriter::ASDCPState
{
	ASDCP::PCM::MXFWriter mxf_writer;
	ASDCP::PCM::FrameBuffer frame_buffer;
	ASDCP::WriterInfo writer_info;
	ASDCP::PCM::AudioDescriptor desc;
};


SoundAssetWriter::SoundAssetWriter(SoundAsset* asset, boost::filesystem::path file, vector<dcp::Channel> extra_active_channels, bool sync, bool include_mca_subdescriptors)
	: AssetWriter (asset, file)
	, _state (new SoundAssetWriter::ASDCPState)
	, _asset (asset)
	, _extra_active_channels(extra_active_channels)
	, _sync (sync)
	, _include_mca_subdescriptors(include_mca_subdescriptors)
{
	DCP_ASSERT (!_sync || _asset->channels() >= 14);
	DCP_ASSERT (!_sync || _asset->standard() == Standard::SMPTE);

	/* None of these are allowed in extra_active_channels; some are implicit, and (it seems) should never have a descriptor
	 * written for them.
	 */
	vector<Channel> disallowed_extra = {
		Channel::LEFT,
		Channel::RIGHT,
		Channel::CENTRE,
		Channel::LFE,
		Channel::LS,
		Channel::RS,
		Channel::MOTION_DATA,
		Channel::SIGN_LANGUAGE,
		Channel::CHANNEL_COUNT
	};
	for (auto disallowed: disallowed_extra) {
		DCP_ASSERT(std::find(extra_active_channels.begin(), extra_active_channels.end(), disallowed) == extra_active_channels.end());
	}

	/* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
	_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() == Standard::INTEROP) {
		_state->desc.ChannelFormat = ASDCP::PCM::CF_NONE;
	} else {
		/* As required by Bv2.1 */
		_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());

	_asset->fill_writer_info (&_state->writer_info, _asset->id());

	if (_sync) {
		_fsk.set_data (create_sync_packets());
	}
}


SoundAssetWriter::~SoundAssetWriter()
{
	try {
		/* Last-resort finalization to close the file, at least */
		if (!_finalized) {
			_state->mxf_writer.Finalize();
		}
	} catch (...) {}
}


void
SoundAssetWriter::start ()
{
	auto r = _state->mxf_writer.OpenWrite(dcp::filesystem::fix_long_path(_file).string().c_str(), _state->writer_info, _state->desc);
	if (ASDCP_FAILURE(r)) {
		throw_from_asdcplib(r, _file, FileError("could not open audio MXF for writing", _file.string(), r));
	}

	if (_asset->standard() == Standard::SMPTE && _include_mca_subdescriptors) {

		ASDCP::MXF::WaveAudioDescriptor* essence_descriptor = nullptr;
		_state->mxf_writer.OP1aHeader().GetMDObjectByType(
			asdcp_smpte_dict->ul(ASDCP::MDD_WaveAudioDescriptor), reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&essence_descriptor)
			);
		DCP_ASSERT (essence_descriptor);
		essence_descriptor->ChannelAssignment = asdcp_smpte_dict->ul(ASDCP::MDD_DCAudioChannelCfg_4_WTF);

		auto soundfield = new ASDCP::MXF::SoundfieldGroupLabelSubDescriptor(asdcp_smpte_dict);
		GenRandomValue (soundfield->MCALinkID);
		if (auto lang = _asset->language()) {
			soundfield->RFC5646SpokenLanguage = *lang;
		}

		MCASoundField const field =
			(
				find(_extra_active_channels.begin(), _extra_active_channels.end(), dcp::Channel::BSL) != _extra_active_channels.end() ||
				find(_extra_active_channels.begin(), _extra_active_channels.end(), dcp::Channel::BSR) != _extra_active_channels.end()
			) ? MCASoundField::SEVEN_POINT_ONE : MCASoundField::FIVE_POINT_ONE;

		if (field == MCASoundField::SEVEN_POINT_ONE) {
			soundfield->MCATagSymbol = "sg71";
			soundfield->MCATagName = "7.1DS";
LIBDCP_DISABLE_WARNINGS
			soundfield->MCALabelDictionaryID = asdcp_smpte_dict->ul(ASDCP::MDD_DCAudioSoundfield_71);
LIBDCP_ENABLE_WARNINGS
		} else {
			soundfield->MCATagSymbol = "sg51";
			soundfield->MCATagName = "5.1";
LIBDCP_DISABLE_WARNINGS
			soundfield->MCALabelDictionaryID = asdcp_smpte_dict->ul(ASDCP::MDD_DCAudioSoundfield_51);
LIBDCP_ENABLE_WARNINGS
		}

		_state->mxf_writer.OP1aHeader().AddChildObject(soundfield);
		essence_descriptor->SubDescriptors.push_back(soundfield->InstanceUID);

		/* We always make a descriptor for these channels if they are present in the asset;
		 * there's no way for the caller to tell us whether they are active or not.
		 */
		std::vector<dcp::Channel> dcp_channels = {
			Channel::LEFT,
			Channel::RIGHT,
			Channel::CENTRE,
			Channel::LFE,
			Channel::LS,
			Channel::RS
		};

		/* We add descriptors for some extra channels that the caller gave us (we made sure earlier
		 * that nothing "bad" is in this list).
		 */
		std::copy(_extra_active_channels.begin(), _extra_active_channels.end(), back_inserter(dcp_channels));

		/* Remove duplicates */
		std::sort(dcp_channels.begin(), dcp_channels.end());
		dcp_channels.erase(std::unique(dcp_channels.begin(), dcp_channels.end()), dcp_channels.end());

		/* Remove channels that aren't actually in this MXF at all */
		dcp_channels.erase(
			std::remove_if(dcp_channels.begin(), dcp_channels.end(), [this](dcp::Channel channel) {
			return static_cast<int>(channel) >= _asset->channels();
			}),
			dcp_channels.end()
		);

		for (auto dcp_channel: dcp_channels) {
			auto channel = new ASDCP::MXF::AudioChannelLabelSubDescriptor(asdcp_smpte_dict);
			GenRandomValue (channel->MCALinkID);
			channel->SoundfieldGroupLinkID = soundfield->MCALinkID;
			channel->MCAChannelID = static_cast<int>(dcp_channel) + 1;
			channel->MCATagSymbol = "ch" + channel_to_mca_id(dcp_channel, field);
			channel->MCATagName = channel_to_mca_name(dcp_channel, field);
			if (auto lang = _asset->language()) {
				channel->RFC5646SpokenLanguage = *lang;
			}
LIBDCP_DISABLE_WARNINGS
			channel->MCALabelDictionaryID = channel_to_mca_universal_label(dcp_channel, field, asdcp_smpte_dict);
LIBDCP_ENABLE_WARNINGS
			_state->mxf_writer.OP1aHeader().AddChildObject(channel);
			essence_descriptor->SubDescriptors.push_back(channel->InstanceUID);
		}
	}

	_asset->set_file (_file);
	_started = true;
}


void
SoundAssetWriter::write(float const * const * data, int data_channels, int frames)
{
	do_write(data, data_channels, frames);
}


void
SoundAssetWriter::write(int32_t const * const * data, int data_channels, int frames)
{
	do_write(data, data_channels, frames);
}


void
SoundAssetWriter::write_current_frame ()
{
	auto const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
	if (ASDCP_FAILURE(r)) {
		throw_from_asdcplib(r, _file, MiscError(fmt::format("could not write audio MXF frame ({})", static_cast<int>(r))));
	}

	++_frames_written;

	if (_sync) {
		/* We need a new set of sync packets for this frame */
		_fsk.set_data (create_sync_packets());
	}
}

bool
SoundAssetWriter::finalize ()
{
	if (_frame_buffer_offset > 0) {
		write_current_frame ();
	}

	if (_started) {
		auto const r = _state->mxf_writer.Finalize();
		if (ASDCP_FAILURE(r)) {
			throw_from_asdcplib(r, _file, MiscError(fmt::format("could not finalise audio MXF ({})", static_cast<int>(r))));
		}
	}

	_asset->_intrinsic_duration = _frames_written;
	return AssetWriter::finalize ();
}


/** Calculate and return the sync packets required for this edit unit (aka "frame") */
vector<bool>
SoundAssetWriter::create_sync_packets ()
{
	/* Parts of this code assumes 48kHz */
	DCP_ASSERT (_asset->sampling_rate() == 48000);

	/* Encoding of edit rate */
	int edit_rate_code = 0;
	/* How many 0 bits are used to pad the end of the packet */
	int remaining_bits = 0;
	/* How many packets in this edit unit (i.e. "frame") */
	int packets = 0;
	auto const edit_rate = _asset->edit_rate ();
	if (edit_rate == Fraction(24, 1)) {
		edit_rate_code = 0;
		remaining_bits = 25;
		packets = 4;
	} else if (edit_rate == Fraction(25, 1)) {
		edit_rate_code = 1;
		remaining_bits = 20;
		packets = 4;
	} else if (edit_rate == Fraction(30, 1)) {
		edit_rate_code = 2;
		remaining_bits = 0;
		packets = 4;
	} else if (edit_rate == Fraction(48, 1)) {
		edit_rate_code = 3;
		remaining_bits = 25;
		packets = 2;
	} else if (edit_rate == Fraction(50, 1)) {
		edit_rate_code = 4;
		remaining_bits = 20;
		packets = 2;
	} else if (edit_rate == Fraction(60, 1)) {
		edit_rate_code = 5;
		remaining_bits = 0;
		packets = 2;
	} else if (edit_rate == Fraction(96, 1)) {
		edit_rate_code = 6;
		remaining_bits = 25;
		packets = 1;
	} else if (edit_rate == Fraction(100, 1)) {
		edit_rate_code = 7;
		remaining_bits = 20;
		packets = 1;
	} else if (edit_rate == Fraction(120, 1)) {
		edit_rate_code = 8;
		remaining_bits = 0;
		packets = 1;
	}

	Bitstream bs;

	Kumu::UUID id;
	DCP_ASSERT (id.DecodeHex(_asset->id().c_str()));

	for (int i = 0; i < packets; ++i) {
		bs.write_from_byte (0x4d);
		bs.write_from_byte (0x56);
		bs.start_crc (0x1021);
		bs.write_from_byte (edit_rate_code, 4);
		bs.write_from_byte (0, 2);
		bs.write_from_byte (_sync_packet, 2);
		bs.write_from_byte (id.Value()[i * 4 + 0]);
		bs.write_from_byte (id.Value()[i * 4 + 1]);
		bs.write_from_byte (id.Value()[i * 4 + 2]);
		bs.write_from_byte (id.Value()[i * 4 + 3]);
		bs.write_from_word (_frames_written, 24);
		bs.write_crc ();
		bs.write_from_byte (0, 4);
		bs.write_from_word (0, remaining_bits);

		++_sync_packet;
		if (_sync_packet == 4) {
			_sync_packet = 0;
		}
	}

	return bs.get();
}


byte_t*
SoundAssetWriter::frame_buffer_data() const
{
	return _state->frame_buffer.Data();
}


int
SoundAssetWriter::frame_buffer_capacity() const
{
	return _state->frame_buffer.Capacity();
}