libdcp
atmos_asset_writer.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016-2021 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 
40 #include "atmos_asset_writer.h"
41 #include "atmos_asset.h"
42 #include "exceptions.h"
43 #include "dcp_assert.h"
44 #include "compose.hpp"
45 #include "crypto_context.h"
46 #include <asdcp/AS_DCP.h>
47 
48 
49 using std::min;
50 using std::max;
51 using std::shared_ptr;
52 using namespace dcp;
53 
54 
55 struct AtmosAssetWriter::ASDCPState
56 {
57  ASDCP::ATMOS::MXFWriter mxf_writer;
58  ASDCP::DCData::FrameBuffer frame_buffer;
59  ASDCP::WriterInfo writer_info;
60  ASDCP::ATMOS::AtmosDescriptor desc;
61 };
62 
63 
64 AtmosAssetWriter::AtmosAssetWriter (AtmosAsset* asset, boost::filesystem::path file)
65  : AssetWriter (asset, file)
66  , _state (new AtmosAssetWriter::ASDCPState)
67  , _asset (asset)
68 {
69  _state->desc.EditRate = ASDCP::Rational (_asset->edit_rate().numerator, _asset->edit_rate().denominator);
70  _state->desc.FirstFrame = _asset->first_frame ();
71  _state->desc.MaxChannelCount = _asset->max_channel_count ();
72  _state->desc.MaxObjectCount = _asset->max_object_count ();
73 
74  unsigned int c;
75  Kumu::hex2bin (_asset->atmos_id().c_str(), _state->desc.AtmosID, ASDCP::UUIDlen, &c);
76  DCP_ASSERT (c == ASDCP::UUIDlen);
77 
78  _state->desc.AtmosVersion = _asset->atmos_version ();
79 
80  _asset->fill_writer_info (&_state->writer_info, _asset->id());
81 }
82 
83 
84 void
85 AtmosAssetWriter::write (shared_ptr<const AtmosFrame> frame)
86 {
87  write (frame->data(), frame->size());
88 }
89 
90 
91 void
92 AtmosAssetWriter::write (uint8_t const * data, int size)
93 {
94  DCP_ASSERT (!_finalized);
95 
96  if (!_started) {
97  auto r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->desc);
98  if (ASDCP_FAILURE(r)) {
99  boost::throw_exception (FileError ("could not open atmos MXF for writing", _file.string(), r));
100  }
101 
102  _asset->set_file (_file);
103  _started = true;
104  }
105 
106  _state->frame_buffer.Capacity (size);
107  _state->frame_buffer.Size (size);
108  memcpy (_state->frame_buffer.Data(), data, size);
109 
110  auto const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
111  if (ASDCP_FAILURE(r)) {
112  boost::throw_exception (MiscError(String::compose("could not write atmos MXF frame (%1)", static_cast<int>(r))));
113  }
114 
115  ++_frames_written;
116 }
117 
118 
119 bool
121 {
122  if (_started && ASDCP_FAILURE(_state->mxf_writer.Finalize())) {
123  boost::throw_exception (MiscError("could not finalise atmos MXF"));
124  }
125 
126  _asset->_intrinsic_duration = _frames_written;
127  return AssetWriter::finalize ();
128 }
AtmosAsset class.
AtmosAssetWriter class.
Parent class for classes which can write MXF-based assets.
Definition: asset_writer.h:62
virtual bool finalize()
Definition: asset_writer.cc:65
boost::filesystem::path _file
Definition: asset_writer.h:82
int64_t _frames_written
Definition: asset_writer.h:86
void set_file(boost::filesystem::path file) const
Definition: asset.cc:165
A helper class for writing to AtmosAssets.
An asset of Dolby ATMOS sound data.
Definition: atmos_asset.h:59
An exception related to a file.
Definition: exceptions.h:56
A miscellaneous exception.
Definition: exceptions.h:94
DCP_ASSERT macro.
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50