ASDCPLib builds.
[libdcp.git] / src / picture_asset.cc
1 /*
2     Copyright (C) 2012 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 <list>
21 #include <stdexcept>
22 #include <iostream>
23 #include <boost/filesystem.hpp>
24 #include "AS_DCP.h"
25 #include "KM_fileio.h"
26 #include "picture_asset.h"
27 #include "util.h"
28
29 using namespace std;
30 using namespace boost;
31 using namespace libdcp;
32
33 PictureAsset::PictureAsset (list<string> const & files, string p, int fps, int len, int w, int h)
34         : Asset (p, fps, len)
35         , _width (w)
36         , _height (h)
37 {
38         ASDCP::JP2K::CodestreamParser j2k_parser;
39         ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
40         if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (files.front().c_str(), frame_buffer))) {
41                 throw runtime_error ("could not open J2K file for reading");
42         }
43         
44         ASDCP::JP2K::PictureDescriptor picture_desc;
45         j2k_parser.FillPictureDescriptor (picture_desc);
46         /* XXX: we round for DCP: not sure if this is right */
47         picture_desc.EditRate = ASDCP::Rational (_fps, 1);
48         
49         ASDCP::WriterInfo writer_info;
50         fill_writer_info (&writer_info);
51         
52         ASDCP::JP2K::MXFWriter mxf_writer;
53         if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, picture_desc))) {
54                 throw runtime_error ("could not open MXF for writing");
55         }
56
57         int j = 0;
58         for (list<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
59                 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (i->c_str(), frame_buffer))) {
60                         throw runtime_error ("could not open J2K file for reading");
61                 }
62
63                 /* XXX: passing 0 to WriteFrame ok? */
64                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
65                         throw runtime_error ("error in writing video MXF");
66                 }
67                 
68                 ++j;
69                 Progress (float (j) / files.size ());
70         }
71         
72         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
73                 throw runtime_error ("error in finalising video MXF");
74         }
75
76         _digest = make_digest (_mxf_path);
77 }
78
79 void
80 PictureAsset::write_to_cpl (ostream& s) const
81 {
82         s << "        <MainPicture>\n"
83           << "          <Id>" << _uuid << "</Id>\n"
84           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
85           << "          <EditRate>" << _fps << "</EditRate>\n"
86           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
87           << "          <EntryPoint>0</EntryPoint>\n"
88           << "          <Duration>" << _length << "</Duration>\n"
89           << "          <FrameRate>" << _fps << "</FrameRate>\n"
90           << "          <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
91           << "        </MainPicture>\n";
92 }