Merge master; MXF subtitle stuff not included.
[libdcp.git] / src / sound_mxf_writer.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "sound_mxf_writer.h"
21 #include "sound_mxf.h"
22 #include "exceptions.h"
23 #include "compose.hpp"
24 #include "AS_DCP.h"
25
26 using namespace dcp;
27
28 struct SoundMXFWriter::ASDCPState
29 {
30         ASDCP::PCM::MXFWriter mxf_writer;
31         ASDCP::PCM::FrameBuffer frame_buffer;
32         ASDCP::WriterInfo writer_info;
33         ASDCP::PCM::AudioDescriptor audio_desc;
34         ASDCP::AESEncContext* encryption_context;
35 };
36
37 SoundMXFWriter::SoundMXFWriter (SoundMXF* m, boost::filesystem::path file, Standard standard)
38         : MXFWriter (m, file)
39         , _state (new SoundMXFWriter::ASDCPState)
40         , _sound_mxf (m)
41         , _frame_buffer_offset (0)
42 {
43         _state->encryption_context = m->encryption_context ();
44         
45         /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
46         _state->audio_desc.EditRate = ASDCP::Rational (_sound_mxf->edit_rate().numerator, _sound_mxf->edit_rate().denominator);
47         _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_sound_mxf->sampling_rate(), 1);
48         _state->audio_desc.Locked = 0;
49         _state->audio_desc.ChannelCount = _sound_mxf->channels ();
50         _state->audio_desc.QuantizationBits = 24;
51         _state->audio_desc.BlockAlign = 3 * _sound_mxf->channels();
52         _state->audio_desc.AvgBps = _sound_mxf->sampling_rate() * _state->audio_desc.BlockAlign;
53         _state->audio_desc.LinkedTrackID = 0;
54         _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE;
55         
56         _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
57         _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
58         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
59         
60         _sound_mxf->fill_writer_info (&_state->writer_info, standard);
61         
62         Kumu::Result_t r = _state->mxf_writer.OpenWrite (file.string().c_str(), _state->writer_info, _state->audio_desc);
63         if (ASDCP_FAILURE (r)) {
64                 boost::throw_exception (FileError ("could not open audio MXF for writing", file.string(), r));
65         }
66 }
67
68 void
69 SoundMXFWriter::write (float const * const * data, int frames)
70 {
71         assert (!_finalized);
72         
73         for (int i = 0; i < frames; ++i) {
74
75                 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
76
77                 /* Write one sample per channel */
78                 for (int j = 0; j < _sound_mxf->channels(); ++j) {
79                         int32_t const s = data[j][i] * (1 << 23);
80                         *out++ = (s & 0xff);
81                         *out++ = (s & 0xff00) >> 8;
82                         *out++ = (s & 0xff0000) >> 16;
83                 }
84                 _frame_buffer_offset += 3 * _sound_mxf->channels();
85
86                 assert (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
87
88                 /* Finish the MXF frame if required */
89                 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
90                         write_current_frame ();
91                         _frame_buffer_offset = 0;
92                         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
93                 }
94         }
95 }
96
97 void
98 SoundMXFWriter::write_current_frame ()
99 {
100         ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0);
101         if (ASDCP_FAILURE (r)) {
102                 boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
103         }
104
105         ++_frames_written;
106 }
107
108 void
109 SoundMXFWriter::finalize ()
110 {
111         if (_frame_buffer_offset > 0) {
112                 write_current_frame ();
113         }
114         
115         if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
116                 boost::throw_exception (MiscError ("could not finalise audio MXF"));
117         }
118
119         MXFWriter::finalize ();
120 }