Consider assets outside the DCP as referenced assets (i.e.
[libdcp.git] / src / stereo_picture_asset_writer.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "stereo_picture_asset_writer.h"
21 #include "exceptions.h"
22 #include "dcp_assert.h"
23 #include "picture_asset.h"
24 #include "AS_DCP.h"
25 #include "KM_fileio.h"
26
27 #include "picture_asset_writer_common.cc"
28
29 using std::istream;
30 using std::ostream;
31 using std::string;
32 using boost::shared_ptr;
33 using namespace dcp;
34
35 struct StereoPictureAssetWriter::ASDCPState : public ASDCPStateBase
36 {
37         ASDCP::JP2K::MXFSWriter mxf_writer;
38 };
39
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)
44 {
45
46 }
47
48 void
49 StereoPictureAssetWriter::start (uint8_t* data, int size)
50 {
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));
53 }
54
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.
58  */
59 FrameInfo
60 StereoPictureAssetWriter::write (uint8_t* data, int size)
61 {
62         DCP_ASSERT (!_finalized);
63
64         if (!_started) {
65                 start (data, size);
66         }
67
68         if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
69                 boost::throw_exception (MiscError ("could not parse J2K frame"));
70         }
71
72         uint64_t const before_offset = _state->mxf_writer.Tell ();
73
74         string hash;
75         Kumu::Result_t r = _state->mxf_writer.WriteFrame (
76                 _state->frame_buffer,
77                 _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT,
78                 _encryption_context,
79                 0,
80                 &hash
81                 );
82
83         if (ASDCP_FAILURE (r)) {
84                 boost::throw_exception (MXFFileError ("error in writing video MXF", _file.string(), r));
85         }
86
87         _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
88
89         if (_next_eye == EYE_LEFT) {
90                 ++_frames_written;
91         }
92
93         return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
94 }
95
96 void
97 StereoPictureAssetWriter::fake_write (int size)
98 {
99         DCP_ASSERT (_started);
100         DCP_ASSERT (!_finalized);
101
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));
105         }
106
107         _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
108         if (_next_eye == EYE_LEFT) {
109                 ++_frames_written;
110         }
111 }
112
113 bool
114 StereoPictureAssetWriter::finalize ()
115 {
116         if (_started) {
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));
120                 }
121         }
122
123         _picture_asset->_intrinsic_duration = _frames_written;
124         return PictureAssetWriter::finalize ();
125 }