4f98c60e3d70ef95e0b1c53dc1223758141bd065
[libdcp.git] / src / stereo_picture_mxf_writer.cc
1 /*
2     Copyright (C) 2012-2014 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_mxf_writer.h"
21 #include "exceptions.h"
22 #include "picture_mxf.h"
23 #include "AS_DCP.h"
24 #include "KM_fileio.h"
25
26 #include "picture_mxf_writer_common.cc"
27
28 using std::istream;
29 using std::ostream;
30 using std::string;
31 using boost::shared_ptr;
32 using namespace dcp;
33
34 struct StereoPictureMXFWriter::ASDCPState : public ASDCPStateBase
35 {
36         ASDCP::JP2K::MXFSWriter mxf_writer;
37 };
38
39 StereoPictureMXFWriter::StereoPictureMXFWriter (PictureMXF* mxf, boost::filesystem::path file, Standard standard, bool overwrite)
40         : PictureMXFWriter (mxf, file, standard, overwrite)
41         , _state (new StereoPictureMXFWriter::ASDCPState)
42         , _next_eye (EYE_LEFT)
43 {
44         _state->encryption_context = mxf->encryption_context ();
45 }
46
47 void
48 StereoPictureMXFWriter::start (uint8_t* data, int size)
49 {
50         dcp::start (this, _state, _standard, _picture_mxf, data, size);
51         _picture_mxf->set_frame_rate (Fraction (_picture_mxf->edit_rate().numerator * 2, _picture_mxf->edit_rate().denominator));
52 }
53
54 /** Write a frame for one eye.  Frames must be written left, then right, then left etc.
55  *  @param data JPEG2000 data.
56  *  @param size Size of data.
57  */
58 FrameInfo
59 StereoPictureMXFWriter::write (uint8_t* data, int size)
60 {
61         assert (!_finalized);
62
63         if (!_started) {
64                 start (data, size);
65         }
66
67         if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
68                 boost::throw_exception (MiscError ("could not parse J2K frame"));
69         }
70
71         uint64_t const before_offset = _state->mxf_writer.Tell ();
72
73         string hash;
74         Kumu::Result_t r = _state->mxf_writer.WriteFrame (
75                 _state->frame_buffer,
76                 _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT,
77                 _state->encryption_context,
78                 0,
79                 &hash
80                 );
81
82         if (ASDCP_FAILURE (r)) {
83                 boost::throw_exception (MXFFileError ("error in writing video MXF", _mxf->file().string(), r));
84         }
85
86         _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
87
88         if (_next_eye == EYE_LEFT) {
89                 ++_frames_written;
90         }
91                         
92         return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
93 }
94
95 void
96 StereoPictureMXFWriter::fake_write (int size)
97 {
98         assert (_started);
99         assert (!_finalized);
100
101         Kumu::Result_t r = _state->mxf_writer.FakeWriteFrame (size, _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT);
102         if (ASDCP_FAILURE (r)) {
103                 boost::throw_exception (MXFFileError ("error in writing video MXF", _mxf->file().string(), r));
104         }
105
106         _next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
107         if (_next_eye == EYE_LEFT) {
108                 ++_frames_written;
109         }
110 }
111
112 void
113 StereoPictureMXFWriter::finalize ()
114 {
115         Kumu::Result_t r = _state->mxf_writer.Finalize();
116         if (ASDCP_FAILURE (r)) {
117                 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _mxf->file().string(), r));
118         }
119
120         PictureMXFWriter::finalize ();
121 }