Better error message.
[libdcp.git] / src / mono_picture_asset_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_asset_writer.h"
23 #include "exceptions.h"
24 #include "picture_asset.h"
25
26 #include "picture_asset_writer_common.cc"
27
28 using std::istream;
29 using std::ostream;
30 using std::string;
31 using boost::shared_ptr;
32 using namespace libdcp;
33
34 struct MonoPictureAssetWriter::ASDCPState : public ASDCPStateBase
35 {
36         ASDCP::JP2K::MXFWriter mxf_writer;
37 };
38
39 /** @param a Asset to write to.  `a' must not be deleted while
40  *  this writer class still exists, or bad things will happen.
41  */
42 MonoPictureAssetWriter::MonoPictureAssetWriter (PictureAsset* asset, bool overwrite)
43         : PictureAssetWriter (asset, overwrite)
44         , _state (new MonoPictureAssetWriter::ASDCPState)
45 {
46         _state->encryption_context = asset->encryption_context ();
47 }
48
49 void
50 MonoPictureAssetWriter::start (uint8_t* data, int size)
51 {
52         libdcp::start (this, _state, _asset, data, size);
53 }
54
55 FrameInfo
56 MonoPictureAssetWriter::write (uint8_t* data, int size)
57 {
58         assert (!_finalized);
59
60         if (!_started) {
61                 start (data, size);
62         }
63
64         if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
65                 boost::throw_exception (MiscError ("could not parse J2K frame"));
66         }
67
68         uint64_t const before_offset = _state->mxf_writer.Tell ();
69
70         string hash;
71         if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0, &hash))) {
72                 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
73         }
74
75         ++_frames_written;
76         return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
77 }
78
79 void
80 MonoPictureAssetWriter::fake_write (int size)
81 {
82         assert (_started);
83         assert (!_finalized);
84
85         if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
86                 boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
87         }
88
89         ++_frames_written;
90 }
91
92 void
93 MonoPictureAssetWriter::finalize ()
94 {
95         assert (!_finalized);
96         
97         if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
98                 boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
99         }
100
101         _finalized = true;
102         _asset->set_intrinsic_duration (_frames_written);
103         _asset->set_duration (_frames_written);
104 }
105