2 Copyright (C) 2012 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.
23 #include <boost/filesystem.hpp>
25 #include "KM_fileio.h"
26 #include "picture_asset.h"
30 using namespace boost;
31 using namespace libdcp;
33 /** Construct a PictureAsset, generating the MXF from the JPEG2000 files.
34 * This may take some time; progress is indicated by emission of the Progress signal.
35 * @param files Pathnames of JPEG2000 files, in frame order.
36 * @param p Pathname of MXF file to create.
37 * @param fps Frames per second.
38 * @param len Length in frames.
39 * @param w Width of images in pixels.
40 * @param h Height of images in pixels.
43 PictureAsset::PictureAsset (list<string> const & files, string p, int fps, int len, int w, int h)
48 ASDCP::JP2K::CodestreamParser j2k_parser;
49 ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
50 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (files.front().c_str(), frame_buffer))) {
51 throw runtime_error ("could not open J2K file for reading");
54 ASDCP::JP2K::PictureDescriptor picture_desc;
55 j2k_parser.FillPictureDescriptor (picture_desc);
56 picture_desc.EditRate = ASDCP::Rational (_fps, 1);
58 ASDCP::WriterInfo writer_info;
59 fill_writer_info (&writer_info);
61 ASDCP::JP2K::MXFWriter mxf_writer;
62 if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, picture_desc))) {
63 throw runtime_error ("could not open MXF for writing");
67 for (list<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
68 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (i->c_str(), frame_buffer))) {
69 throw runtime_error ("could not open J2K file for reading");
72 /* XXX: passing 0 to WriteFrame ok? */
73 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
74 throw runtime_error ("error in writing video MXF");
78 Progress (float (j) / files.size ());
81 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
82 throw runtime_error ("error in finalising video MXF");
85 _digest = make_digest (_mxf_path);
88 /** Write details of this asset to a CPL stream.
92 PictureAsset::write_to_cpl (ostream& s) const
94 s << " <MainPicture>\n"
95 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
96 << " <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
97 << " <EditRate>" << _fps << " 1</EditRate>\n"
98 << " <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
99 << " <EntryPoint>0</EntryPoint>\n"
100 << " <Duration>" << _length << "</Duration>\n"
101 << " <FrameRate>" << _fps << " 1</FrameRate>\n"
102 << " <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
103 << " </MainPicture>\n";