c989539b0ffdbab2d4bfb9c79ac0f944c5096ce0
[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 <sstream>
24 #include <boost/filesystem.hpp>
25 #include "AS_DCP.h"
26 #include "KM_fileio.h"
27 #include "picture_asset.h"
28 #include "util.h"
29
30 using namespace std;
31 using namespace boost;
32 using namespace libdcp;
33
34 /** Construct a PictureAsset, generating the MXF from the JPEG2000 files.
35  *  This may take some time; progress is indicated by emission of the Progress signal.
36  *  @param files Pathnames of JPEG2000 files, in frame order.
37  *  @param p Pathname of MXF file to create.
38  *  @param fps Frames per second.
39  *  @param len Length in frames.
40  *  @param w Width of images in pixels.
41  *  @param h Height of images in pixels.
42  */
43
44 PictureAsset::PictureAsset (list<string> const & files, string p, sigc::signal1<void, float>* progress, int fps, int len, int w, int h)
45         : Asset (p, progress, fps, len)
46         , _width (w)
47         , _height (h)
48 {
49         ASDCP::JP2K::CodestreamParser j2k_parser;
50         ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
51         if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (files.front().c_str(), frame_buffer))) {
52                 stringstream s;
53                 s << "could not open " << files.front() << " for reading";
54                 throw runtime_error (s.str());
55         }
56         
57         ASDCP::JP2K::PictureDescriptor picture_desc;
58         j2k_parser.FillPictureDescriptor (picture_desc);
59         picture_desc.EditRate = ASDCP::Rational (_fps, 1);
60         
61         ASDCP::WriterInfo writer_info;
62         fill_writer_info (&writer_info);
63         
64         ASDCP::JP2K::MXFWriter mxf_writer;
65         if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, picture_desc))) {
66                 throw runtime_error ("could not open MXF for writing");
67         }
68
69         int j = 0;
70         for (list<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
71                 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (i->c_str(), frame_buffer))) {
72                         stringstream s;
73                         s << "could not open " << *i << " for reading";
74                         throw runtime_error (s.str());
75                 }
76
77                 /* XXX: passing 0 to WriteFrame ok? */
78                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
79                         throw runtime_error ("error in writing video MXF");
80                 }
81                 
82                 ++j;
83                 (*_progress) (0.5 * float (j) / files.size ());
84         }
85         
86         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
87                 throw runtime_error ("error in finalising video MXF");
88         }
89
90         _digest = make_digest (_mxf_path, _progress);
91 }
92
93 /** Write details of this asset to a CPL stream.
94  *  @param s Stream.
95  */
96 void
97 PictureAsset::write_to_cpl (ostream& s) const
98 {
99         s << "        <MainPicture>\n"
100           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
101           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
102           << "          <EditRate>" << _fps << " 1</EditRate>\n"
103           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
104           << "          <EntryPoint>0</EntryPoint>\n"
105           << "          <Duration>" << _length << "</Duration>\n"
106           << "          <FrameRate>" << _fps << " 1</FrameRate>\n"
107           << "          <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
108           << "        </MainPicture>\n";
109 }