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