It builds again.
[libdcp.git] / src / mono_picture_mxf_writer.cc
1 /*
2     Copyright (C) 2012-2013 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 "AS_DCP.h"
21 #include "KM_fileio.h"
22 #include "mono_picture_mxf_writer.h"
23 #include "exceptions.h"
24 #include "picture_mxf.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 boost::lexical_cast;
33 using namespace dcp;
34
35 struct MonoPictureMXFWriter::ASDCPState : public ASDCPStateBase
36 {
37         ASDCP::JP2K::MXFWriter mxf_writer;
38 };
39
40 /** @param a Asset to write to.  `a' must not be deleted while
41  *  this writer class still exists, or bad things will happen.
42  */
43 MonoPictureMXFWriter::MonoPictureMXFWriter (PictureMXF* asset, boost::filesystem::path file, Standard standard, bool overwrite)
44         : PictureMXFWriter (asset, file, standard, overwrite)
45         , _state (new MonoPictureMXFWriter::ASDCPState)
46 {
47         _state->encryption_context = asset->encryption_context ();
48 }
49
50 void
51 MonoPictureMXFWriter::start (uint8_t* data, int size)
52 {
53         dcp::start (this, _state, _standard, _picture_mxf, data, size);
54 }
55
56 FrameInfo
57 MonoPictureMXFWriter::write (uint8_t* data, int size)
58 {
59         assert (!_finalized);
60
61         if (!_started) {
62                 start (data, size);
63         }
64
65         if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
66                 boost::throw_exception (MiscError ("could not parse J2K frame"));
67         }
68
69         uint64_t const before_offset = _state->mxf_writer.Tell ();
70
71         string hash;
72         ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0, &hash);
73         if (ASDCP_FAILURE (r)) {
74                 boost::throw_exception (MXFFileError ("error in writing video MXF", _file.string(), r));
75         }
76
77         ++_frames_written;
78         return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
79 }
80
81 void
82 MonoPictureMXFWriter::fake_write (int size)
83 {
84         assert (_started);
85         assert (!_finalized);
86
87         Kumu::Result_t r = _state->mxf_writer.FakeWriteFrame (size);
88         if (ASDCP_FAILURE (r)) {
89                 boost::throw_exception (MXFFileError ("error in writing video MXF", _mxf->file().string(), r));
90         }
91
92         ++_frames_written;
93 }
94
95 void
96 MonoPictureMXFWriter::finalize ()
97 {
98         Kumu::Result_t r = _state->mxf_writer.Finalize();
99         if (ASDCP_FAILURE (r)) {
100                 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _mxf->file().string(), r));
101         }
102
103         PictureMXFWriter::finalize ();
104 }
105