2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "sound_mxf_writer.h"
21 #include "sound_mxf.h"
22 #include "exceptions.h"
23 #include "compose.hpp"
25 #include <boost/lexical_cast.hpp>
27 using boost::lexical_cast;
30 struct SoundMXFWriter::ASDCPState
32 ASDCP::PCM::MXFWriter mxf_writer;
33 ASDCP::PCM::FrameBuffer frame_buffer;
34 ASDCP::WriterInfo writer_info;
35 ASDCP::PCM::AudioDescriptor audio_desc;
36 ASDCP::AESEncContext* encryption_context;
39 SoundMXFWriter::SoundMXFWriter (SoundMXF* m, boost::filesystem::path file, Standard standard)
41 , _state (new SoundMXFWriter::ASDCPState)
43 , _frame_buffer_offset (0)
45 _state->encryption_context = m->encryption_context ();
47 /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
48 _state->audio_desc.EditRate = ASDCP::Rational (_sound_mxf->edit_rate().numerator, _sound_mxf->edit_rate().denominator);
49 _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_sound_mxf->sampling_rate(), 1);
50 _state->audio_desc.Locked = 0;
51 _state->audio_desc.ChannelCount = _sound_mxf->channels ();
52 _state->audio_desc.QuantizationBits = 24;
53 _state->audio_desc.BlockAlign = 3 * _sound_mxf->channels();
54 _state->audio_desc.AvgBps = _sound_mxf->sampling_rate() * _state->audio_desc.BlockAlign;
55 _state->audio_desc.LinkedTrackID = 0;
56 _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE;
58 _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
59 _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
60 memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
62 _sound_mxf->fill_writer_info (&_state->writer_info, standard);
64 Kumu::Result_t r = _state->mxf_writer.OpenWrite (file.string().c_str(), _state->writer_info, _state->audio_desc);
65 if (ASDCP_FAILURE (r)) {
66 boost::throw_exception (FileError ("could not open audio MXF for writing", file.string(), r));
71 SoundMXFWriter::write (float const * const * data, int frames)
75 for (int i = 0; i < frames; ++i) {
77 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
79 /* Write one sample per channel */
80 for (int j = 0; j < _sound_mxf->channels(); ++j) {
81 int32_t const s = data[j][i] * (1 << 23);
83 *out++ = (s & 0xff00) >> 8;
84 *out++ = (s & 0xff0000) >> 16;
86 _frame_buffer_offset += 3 * _sound_mxf->channels();
88 assert (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
90 /* Finish the MXF frame if required */
91 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
92 write_current_frame ();
93 _frame_buffer_offset = 0;
94 memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
100 SoundMXFWriter::write_current_frame ()
102 ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0);
103 if (ASDCP_FAILURE (r)) {
104 boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
111 SoundMXFWriter::finalize ()
113 if (_frame_buffer_offset > 0) {
114 write_current_frame ();
117 if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
118 boost::throw_exception (MiscError ("could not finalise audio MXF"));
121 MXFWriter::finalize ();