Fix erroneous reports of unresolved assets when checking OV/VF pairs.
[libdcp.git] / src / sound_asset_writer.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
34 #include "sound_asset_writer.h"
35 #include "sound_asset.h"
36 #include "exceptions.h"
37 #include "dcp_assert.h"
38 #include "compose.hpp"
39 #include "crypto_context.h"
40 #include <asdcp/AS_DCP.h>
41 #include <iostream>
42
43 using std::min;
44 using std::max;
45 using std::cout;
46 using namespace dcp;
47
48 struct SoundAssetWriter::ASDCPState
49 {
50         ASDCP::PCM::MXFWriter mxf_writer;
51         ASDCP::PCM::FrameBuffer frame_buffer;
52         ASDCP::WriterInfo writer_info;
53         ASDCP::PCM::AudioDescriptor desc;
54 };
55
56 SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file)
57         : AssetWriter (asset, file)
58         , _state (new SoundAssetWriter::ASDCPState)
59         , _asset (asset)
60         , _frame_buffer_offset (0)
61 {
62         /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
63         _state->desc.EditRate = ASDCP::Rational (_asset->edit_rate().numerator, _asset->edit_rate().denominator);
64         _state->desc.AudioSamplingRate = ASDCP::Rational (_asset->sampling_rate(), 1);
65         _state->desc.Locked = 0;
66         _state->desc.ChannelCount = _asset->channels ();
67         _state->desc.QuantizationBits = 24;
68         _state->desc.BlockAlign = 3 * _asset->channels();
69         _state->desc.AvgBps = _asset->sampling_rate() * _state->desc.BlockAlign;
70         _state->desc.LinkedTrackID = 0;
71         if (asset->standard() == INTEROP) {
72                 _state->desc.ChannelFormat = ASDCP::PCM::CF_NONE;
73         } else {
74                 /* Just use WTF ("wild track format") for SMPTE for now; searches suggest that this
75                    uses the same assignment as Interop.
76                 */
77                 _state->desc.ChannelFormat = ASDCP::PCM::CF_CFG_4;
78         }
79
80         /* I'm fairly sure this is not necessary, as ContainerDuration is written
81            in ASDCP's WriteMXFFooter, but it stops a valgrind warning.
82         */
83         _state->desc.ContainerDuration = 0;
84
85         _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->desc));
86         _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->desc));
87         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
88
89         _asset->fill_writer_info (&_state->writer_info, _asset->id());
90 }
91
92 void
93 SoundAssetWriter::write (float const * const * data, int frames)
94 {
95         DCP_ASSERT (!_finalized);
96         DCP_ASSERT (frames > 0);
97
98         static float const clip = 1.0f - (1.0f / pow (2, 23));
99
100         if (!_started) {
101                 Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->desc);
102                 if (ASDCP_FAILURE (r)) {
103                         boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r));
104                 }
105
106                 _asset->set_file (_file);
107                 _started = true;
108         }
109
110         int const ch = _asset->channels ();
111
112         for (int i = 0; i < frames; ++i) {
113
114                 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
115
116                 /* Write one sample per channel */
117                 for (int j = 0; j < ch; ++j) {
118                         /* Convert sample to 24-bit int, clipping if necessary. */
119                         float x = data[j][i];
120                         if (x > clip) {
121                                 x = clip;
122                         } else if (x < -clip) {
123                                 x = -clip;
124                         }
125                         int32_t const s = x * (1 << 23);
126                         *out++ = (s & 0xff);
127                         *out++ = (s & 0xff00) >> 8;
128                         *out++ = (s & 0xff0000) >> 16;
129                 }
130                 _frame_buffer_offset += 3 * ch;
131
132                 DCP_ASSERT (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
133
134                 /* Finish the MXF frame if required */
135                 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
136                         write_current_frame ();
137                         _frame_buffer_offset = 0;
138                         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
139                 }
140         }
141 }
142
143 void
144 SoundAssetWriter::write_current_frame ()
145 {
146         ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
147         if (ASDCP_FAILURE (r)) {
148                 boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
149         }
150
151         ++_frames_written;
152 }
153
154 bool
155 SoundAssetWriter::finalize ()
156 {
157         if (_frame_buffer_offset > 0) {
158                 write_current_frame ();
159         }
160
161         if (_started) {
162                 ASDCP::Result_t const r = _state->mxf_writer.Finalize();
163                 if (ASDCP_FAILURE(r)) {
164                         boost::throw_exception (MiscError (String::compose ("could not finalise audio MXF (%1)", int(r))));
165                 }
166         }
167
168         _asset->_intrinsic_duration = _frames_written;
169         return AssetWriter::finalize ();
170 }