Bits.
[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 PictureAsset::PictureAsset (string mxf_path, int fps, int length, int width, int height)
70         : Asset (mxf_path, 0, fps, length)
71         , _width (width)
72         , _height (height)
73 {
74
75 }
76
77 string
78 PictureAsset::path_from_list (int f, vector<string> const & files) const
79 {
80         return files[f];
81 }
82
83 void
84 PictureAsset::construct (sigc::slot<string, int> get_path)
85 {
86         ASDCP::JP2K::CodestreamParser j2k_parser;
87         ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
88         if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
89                 throw FileError ("could not open JPEG2000 file for reading", get_path (0));
90         }
91         
92         ASDCP::JP2K::PictureDescriptor picture_desc;
93         j2k_parser.FillPictureDescriptor (picture_desc);
94         picture_desc.EditRate = ASDCP::Rational (_fps, 1);
95         
96         ASDCP::WriterInfo writer_info;
97         fill_writer_info (&writer_info);
98         
99         ASDCP::JP2K::MXFWriter mxf_writer;
100         if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, picture_desc))) {
101                 throw FileError ("could not open MXF file for writing", _mxf_path);
102         }
103
104         for (int i = 0; i < _length; ++i) {
105
106                 string const path = get_path (i);
107                 
108                 if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
109                         throw FileError ("could not open JPEG2000 file for reading", path);
110                 }
111
112                 /* XXX: passing 0 to WriteFrame ok? */
113                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
114                         throw MiscError ("error in writing video MXF");
115                 }
116                 
117                 (*_progress) (0.5 * float (i) / _length);
118         }
119         
120         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
121                 throw MiscError ("error in finalising video MXF");
122         }
123
124         _digest = make_digest (_mxf_path, _progress);
125 }
126
127 void
128 PictureAsset::write_to_cpl (ostream& s) const
129 {
130         s << "        <MainPicture>\n"
131           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
132 #if BOOST_FILESYSTEM_VERSION == 3
133           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename().string() << "</AnnotationText>\n"
134 #else           
135           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
136 #endif          
137           << "          <EditRate>" << _fps << " 1</EditRate>\n"
138           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
139           << "          <EntryPoint>0</EntryPoint>\n"
140           << "          <Duration>" << _length << "</Duration>\n"
141           << "          <FrameRate>" << _fps << " 1</FrameRate>\n"
142           << "          <ScreenAspectRatio>" << _width << " " << _height << "</ScreenAspectRatio>\n"
143           << "        </MainPicture>\n";
144 }