2 Copyright (C) 2012-2013 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.
21 #include "KM_fileio.h"
22 #include "picture_asset_writer.h"
23 #include "exceptions.h"
24 #include "picture_asset.h"
29 using boost::shared_ptr;
30 using namespace libdcp;
32 FrameInfo::FrameInfo (istream& s)
39 /* Make sure we zero these if something bad happened, otherwise
40 the caller might try to alloc lots of RAM.
49 FrameInfo::write (ostream& s)
51 s << offset << " " << size << " " << hash;
55 PictureAssetWriter::PictureAssetWriter (PictureAsset* asset, bool overwrite, MXFMetadata const & metadata)
60 , _overwrite (overwrite)
61 , _metadata (metadata)
69 : frame_buffer (4 * Kumu::Megabyte)
72 ASDCP::JP2K::CodestreamParser j2k_parser;
73 ASDCP::JP2K::FrameBuffer frame_buffer;
74 ASDCP::WriterInfo writer_info;
75 ASDCP::JP2K::PictureDescriptor picture_descriptor;
78 struct MonoPictureAssetWriter::ASDCPState : public ASDCPStateBase
80 ASDCP::JP2K::MXFWriter mxf_writer;
83 struct StereoPictureAssetWriter::ASDCPState : public ASDCPStateBase
85 ASDCP::JP2K::MXFSWriter mxf_writer;
88 /** @param a Asset to write to. `a' must not be deleted while
89 * this writer class still exists, or bad things will happen.
91 MonoPictureAssetWriter::MonoPictureAssetWriter (PictureAsset* asset, bool overwrite, MXFMetadata const & metadata)
92 : PictureAssetWriter (asset, overwrite, metadata)
93 , _state (new MonoPictureAssetWriter::ASDCPState)
98 StereoPictureAssetWriter::StereoPictureAssetWriter (PictureAsset* asset, bool overwrite, MXFMetadata const & metadata)
99 : PictureAssetWriter (asset, overwrite, metadata)
100 , _state (new StereoPictureAssetWriter::ASDCPState)
101 , _next_eye (EYE_LEFT)
106 template <class P, class Q>
107 void libdcp::start (PictureAssetWriter* writer, shared_ptr<P> state, Q* asset, uint8_t* data, int size)
109 if (ASDCP_FAILURE (state->j2k_parser.OpenReadFrame (data, size, state->frame_buffer))) {
110 boost::throw_exception (MiscError ("could not parse J2K frame"));
113 state->j2k_parser.FillPictureDescriptor (state->picture_descriptor);
114 state->picture_descriptor.EditRate = ASDCP::Rational (asset->edit_rate(), 1);
116 asset->fill_writer_info (&state->writer_info, asset->uuid(), writer->_metadata);
118 if (ASDCP_FAILURE (state->mxf_writer.OpenWrite (
119 asset->path().string().c_str(),
121 state->picture_descriptor,
126 boost::throw_exception (MXFFileError ("could not open MXF file for writing", asset->path().string()));
129 writer->_started = true;
133 MonoPictureAssetWriter::start (uint8_t* data, int size)
135 libdcp::start (this, _state, _asset, data, size);
139 StereoPictureAssetWriter::start (uint8_t* data, int size)
141 libdcp::start (this, _state, _asset, data, size);
145 MonoPictureAssetWriter::write (uint8_t* data, int size)
147 assert (!_finalized);
153 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
154 boost::throw_exception (MiscError ("could not parse J2K frame"));
157 uint64_t const before_offset = _state->mxf_writer.Tell ();
160 if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0, &hash))) {
161 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
165 return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
168 /** Write a frame for one eye. Frames must be written left, then right, then left etc.
169 * @param data JPEG2000 data.
170 * @param size Size of data.
173 StereoPictureAssetWriter::write (uint8_t* data, int size)
175 assert (!_finalized);
181 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
182 boost::throw_exception (MiscError ("could not parse J2K frame"));
185 uint64_t const before_offset = _state->mxf_writer.Tell ();
189 _state->mxf_writer.WriteFrame (
190 _state->frame_buffer,
191 _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT,
197 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
200 _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
202 return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
206 MonoPictureAssetWriter::fake_write (int size)
209 assert (!_finalized);
211 if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
212 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
219 StereoPictureAssetWriter::fake_write (int size)
222 assert (!_finalized);
224 if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
225 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
228 _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
233 MonoPictureAssetWriter::finalize ()
235 assert (!_finalized);
237 if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
238 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
242 _asset->set_intrinsic_duration (_frames_written);
243 _asset->set_duration (_frames_written);
247 StereoPictureAssetWriter::finalize ()
249 assert (!_finalized);
251 if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
252 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
256 _asset->set_intrinsic_duration (_frames_written / 2);
257 _asset->set_duration (_frames_written / 2);