2 Copyright (C) 2012-2015 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 "stereo_picture_asset_writer.h"
21 #include "exceptions.h"
22 #include "dcp_assert.h"
23 #include "picture_asset.h"
25 #include "KM_fileio.h"
27 #include "picture_asset_writer_common.cc"
32 using boost::shared_ptr;
35 struct StereoPictureAssetWriter::ASDCPState : public ASDCPStateBase
37 ASDCP::JP2K::MXFSWriter mxf_writer;
40 StereoPictureAssetWriter::StereoPictureAssetWriter (PictureAsset* mxf, boost::filesystem::path file, Standard standard, bool overwrite)
41 : PictureAssetWriter (mxf, file, standard, overwrite)
42 , _state (new StereoPictureAssetWriter::ASDCPState)
43 , _next_eye (EYE_LEFT)
49 StereoPictureAssetWriter::start (uint8_t* data, int size)
51 dcp::start (this, _state, _standard, _picture_asset, data, size);
52 _picture_asset->set_frame_rate (Fraction (_picture_asset->edit_rate().numerator * 2, _picture_asset->edit_rate().denominator));
55 /** Write a frame for one eye. Frames must be written left, then right, then left etc.
56 * @param data JPEG2000 data.
57 * @param size Size of data.
60 StereoPictureAssetWriter::write (uint8_t* data, int size)
62 DCP_ASSERT (!_finalized);
68 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
69 boost::throw_exception (MiscError ("could not parse J2K frame"));
72 uint64_t const before_offset = _state->mxf_writer.Tell ();
75 Kumu::Result_t r = _state->mxf_writer.WriteFrame (
77 _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT,
83 if (ASDCP_FAILURE (r)) {
84 boost::throw_exception (MXFFileError ("error in writing video MXF", _file.string(), r));
87 _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
89 if (_next_eye == EYE_LEFT) {
93 return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
97 StereoPictureAssetWriter::fake_write (int size)
99 DCP_ASSERT (_started);
100 DCP_ASSERT (!_finalized);
102 Kumu::Result_t r = _state->mxf_writer.FakeWriteFrame (size, _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT);
103 if (ASDCP_FAILURE (r)) {
104 boost::throw_exception (MXFFileError ("error in writing video MXF", _file.string(), r));
107 _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
108 if (_next_eye == EYE_LEFT) {
114 StereoPictureAssetWriter::finalize ()
117 Kumu::Result_t r = _state->mxf_writer.Finalize();
118 if (ASDCP_FAILURE (r)) {
119 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _file.string(), r));
123 _picture_asset->_intrinsic_duration = _frames_written;
124 return PictureAssetWriter::finalize ();