Allow functor to obtain content paths.
[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 /** @file  src/picture_asset.cc
21  *  @brief An asset made up of JPEG2000 files
22  */
23
24 #include <list>
25 #include <stdexcept>
26 #include <iostream>
27 #include <sstream>
28 #include <boost/filesystem.hpp>
29 #include "AS_DCP.h"
30 #include "KM_fileio.h"
31 #include "picture_asset.h"
32 #include "util.h"
33
34 using namespace std;
35 using namespace boost;
36 using namespace libdcp;
37
38 PictureAsset::PictureAsset (
39         sigc::slot<string, int> get_path,
40         string mxf_path,
41         sigc::signal1<void, float>* progress,
42         int fps,
43         int length,
44         int width,
45         int height)
46         : Asset (mxf_path, progress, fps, length)
47         , _width (width)
48         , _height (height)
49 {
50         construct (get_path);
51 }
52
53 PictureAsset::PictureAsset (
54         vector<string> const & files,
55         string mxf_path,
56         sigc::signal1<void, float>* progress,
57         int fps,
58         int length,
59         int width,
60         int height)
61         : Asset (mxf_path, progress, fps, length)
62         , _width (width)
63         , _height (height)
64 {
65         construct (sigc::bind (sigc::mem_fun (*this, &PictureAsset::path_from_list), files));
66 }
67
68 string
69 PictureAsset::path_from_list (int f, vector<string> const & files) const
70 {
71         return files[f];
72 }
73
74 void
75 PictureAsset::construct (sigc::slot<string, int> get_path)
76 {
77         ASDCP::JP2K::CodestreamParser j2k_parser;
78         ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
79         if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
80                 stringstream s;
81                 s << "could not open " << get_path(0) << " for reading";
82                 throw runtime_error (s.str());
83         }
84         
85         ASDCP::JP2K::PictureDescriptor picture_desc;
86         j2k_parser.FillPictureDescriptor (picture_desc);
87         picture_desc.EditRate = ASDCP::Rational (_fps, 1);
88         
89         ASDCP::WriterInfo writer_info;
90         fill_writer_info (&writer_info);
91         
92         ASDCP::JP2K::MXFWriter mxf_writer;
93         if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, picture_desc))) {
94                 throw runtime_error ("could not open MXF for writing");
95         }
96
97         for (int i = 0; i < _length; ++i) {
98
99                 string const path = get_path (i);
100                 
101                 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
102                         stringstream s;
103                         s << "could not open " << path << " for reading";
104                         throw runtime_error (s.str());
105                 }
106
107                 /* XXX: passing 0 to WriteFrame ok? */
108                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
109                         throw runtime_error ("error in writing video MXF");
110                 }
111                 
112                 (*_progress) (0.5 * float (i) / _length);
113         }
114         
115         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
116                 throw runtime_error ("error in finalising video MXF");
117         }
118
119         _digest = make_digest (_mxf_path, _progress);
120 }
121
122 void
123 PictureAsset::write_to_cpl (ostream& s) const
124 {
125         s << "        <MainPicture>\n"
126           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
127           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
128           << "          <EditRate>" << _fps << " 1</EditRate>\n"
129           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
130           << "          <EntryPoint>0</EntryPoint>\n"
131           << "          <Duration>" << _length << "</Duration>\n"
132           << "          <FrameRate>" << _fps << " 1</FrameRate>\n"
133           << "          <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
134           << "        </MainPicture>\n";
135 }