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 "stereo_picture_asset_writer.h"
23 #include "exceptions.h"
24 #include "picture_asset.h"
26 #include "picture_asset_writer_common.cc"
31 using boost::shared_ptr;
32 using namespace libdcp;
34 struct StereoPictureAssetWriter::ASDCPState : public ASDCPStateBase
36 ASDCP::JP2K::MXFSWriter mxf_writer;
39 StereoPictureAssetWriter::StereoPictureAssetWriter (PictureAsset* asset, bool overwrite)
40 : PictureAssetWriter (asset, overwrite)
41 , _state (new StereoPictureAssetWriter::ASDCPState)
42 , _next_eye (EYE_LEFT)
44 _state->encryption_context = asset->encryption_context ();
48 StereoPictureAssetWriter::start (uint8_t* data, int size)
50 libdcp::start (this, _state, _asset, data, size);
53 /** Write a frame for one eye. Frames must be written left, then right, then left etc.
54 * @param data JPEG2000 data.
55 * @param size Size of data.
58 StereoPictureAssetWriter::write (uint8_t* data, int size)
66 if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
67 boost::throw_exception (MiscError ("could not parse J2K frame"));
70 uint64_t const before_offset = _state->mxf_writer.Tell ();
73 Kumu::Result_t r = _state->mxf_writer.WriteFrame (
75 _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT,
76 _state->encryption_context,
81 if (ASDCP_FAILURE (r)) {
82 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string(), r));
85 _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
88 return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
92 StereoPictureAssetWriter::fake_write (int size)
97 Kumu::Result_t r = _state->mxf_writer.FakeWriteFrame (size, _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT);
98 if (ASDCP_FAILURE (r)) {
99 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string(), r));
102 _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
107 StereoPictureAssetWriter::finalize ()
109 assert (!_finalized);
111 Kumu::Result_t r = _state->mxf_writer.Finalize();
112 if (ASDCP_FAILURE (r)) {
113 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string(), r));
117 _asset->set_intrinsic_duration (_frames_written / 2);
118 _asset->set_duration (_frames_written / 2);