Allow progressive writes to be encrypted.
[libdcp.git] / src / picture_asset.h
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 #ifndef LIBDCP_PICTURE_ASSET_H
21 #define LIBDCP_PICTURE_ASSET_H
22
23 /** @file  src/picture_asset.h
24  *  @brief An asset made up of JPEG2000 data
25  */
26
27 #include <openjpeg.h>
28 #include "mxf_asset.h"
29 #include "util.h"
30 #include "metadata.h"
31
32 namespace libdcp
33 {
34
35 class MonoPictureFrame; 
36 class StereoPictureFrame;
37 class PictureAssetWriter;
38
39 /** @brief An asset made up of JPEG2000 data */
40 class PictureAsset : public MXFAsset
41 {
42 public:
43         /** Construct a PictureAsset.
44          *  This class will not write anything to disk in this constructor, but subclasses may.
45          *  
46          *  @param directory Directory where MXF file is.
47          *  @param mxf_name Name of MXF file.
48          */
49         PictureAsset (std::string directory, std::string mxf_name);
50
51         /** Construct a PictureAsset.
52          *  This class will not write anything to disk in this constructor, but subclasses may.
53          *
54          *  @param directory Directory where MXF file is.
55          *  @param mxf_name Name of MXF file.
56          *  @param progress Signal to use to inform of progres, or 0.
57          *  @param fps Video frames per second.
58          *  @param intrinsic_duration Total number of frames in the asset.
59          *  @param size Size of video frame images in pixels.
60          */
61         PictureAsset (
62                 std::string directory,
63                 std::string mxf_name,
64                 boost::signals2::signal<void (float)>* progress,
65                 int fps,
66                 int intrinsic_duration,
67                 bool encrypted,
68                 Size
69                 );
70
71         /** Start a progressive write to this asset.
72          *  @param overwrite true to overwrite an existing MXF file; in this mode, writing can be resumed to a partially-written MXF; false if the
73          *  MXF file does not exist.
74          *  @param metadata MXF metadata to use.
75          */
76         virtual boost::shared_ptr<PictureAssetWriter> start_write (bool overwrite, bool interop, MXFMetadata const & metadata = MXFMetadata ()) = 0;
77         
78         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
79
80         Size size () const {
81                 return _size;
82         }
83
84         void write_to_cpl (xmlpp::Element *, bool) const;
85
86 protected:      
87
88         bool frame_buffer_equals (
89                 int frame, EqualityOptions opt, boost::function<void (NoteType, std::string)> note,
90                 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
91                 ) const;
92
93         /** picture size in pixels */
94         Size _size;
95
96 private:
97         std::string key_type () const;
98         virtual int edit_rate_factor () const = 0;
99 };
100
101 /** A 2D (monoscopic) picture asset */
102 class MonoPictureAsset : public PictureAsset
103 {
104 public:
105         /** Construct a MonoPictureAsset, generating the MXF from the JPEG2000 files.
106          *  This may take some time; progress is indicated by emission of the Progress signal.
107          *
108          *  @param files Pathnames of JPEG2000 files, in frame order.
109          *  @param directory Directory in which to create MXF file.
110          *  @param mxf_name Name of MXF file to create.
111          *  @param progress Signal to inform of progress.
112          *  @param fps Video frames per second.
113          *  @param intrinsic_duration Total number of frames in the asset.
114          *  @param size Size of images in pixels.
115          *  @param encrypted true if asset should be encrypted.
116          */
117         MonoPictureAsset (
118                 std::vector<std::string> const & files,
119                 std::string directory,
120                 std::string mxf_name,
121                 boost::signals2::signal<void (float)>* progress,
122                 int fps,
123                 int intrinsic_duration,
124                 bool encrypted,
125                 Size size,
126                 bool interop,
127                 MXFMetadata const & metadata = MXFMetadata ()
128                 );
129
130         /** Construct a MonoPictureAsset, generating the MXF from the JPEG2000 files.
131          *  This may take some time; progress is indicated by emission of the Progress signal.
132          *
133          *  @param get_path Functor which returns a JPEG2000 file path for a given frame (frames counted from 0).
134          *  @param directory Directory in which to create MXF file.
135          *  @param mxf_name Name of MXF file to create.
136          *  @param progress Signal to inform of progress.
137          *  @param fps Video frames per second.
138          *  @param intrinsic_duration Total number of frames in the asset.
139          *  @param size Size of images in pixels.
140          *  @param encrypted true if asset should be encrypted.
141          */
142         MonoPictureAsset (
143                 boost::function<std::string (int)> get_path,
144                 std::string directory,
145                 std::string mxf_name,
146                 boost::signals2::signal<void (float)>* progress,
147                 int fps,
148                 int intrinsic_duration,
149                 bool encrypted,
150                 Size size,
151                 bool interop,
152                 MXFMetadata const & metadata = MXFMetadata ()
153                 );
154
155         /** Construct a MonoPictureAsset, reading the MXF from disk.
156          *  @param directory Directory that the MXF is in.
157          *  @param mxf_name The filename of the MXF within `directory'.
158          */
159         MonoPictureAsset (std::string directory, std::string mxf_name);
160
161         /** Construct a MonoPictureAsset for progressive writing using
162          *  start_write() and a MonoPictureAssetWriter.
163          *
164          *  @param directory Directory to put the MXF in.
165          *  @param mxf_name Filename of the MXF within this directory.
166          *  @param fps Video frames per second.
167          *  @param size Size in pixels that the picture frames will be.
168          */
169         MonoPictureAsset (std::string directory, std::string mxf_name, int fps, Size size, bool encrypted);
170
171         /** Start a progressive write to a MonoPictureAsset */
172         boost::shared_ptr<PictureAssetWriter> start_write (bool, bool, MXFMetadata const & metadata = MXFMetadata ());
173
174         boost::shared_ptr<const MonoPictureFrame> get_frame (int n) const;
175         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
176
177 private:
178         std::string path_from_list (int f, std::vector<std::string> const & files) const;
179         void construct (boost::function<std::string (int)>, bool, MXFMetadata const &);
180         std::string cpl_node_name () const;
181         int edit_rate_factor () const;
182 };
183
184 /** A 3D (stereoscopic) picture asset */        
185 class StereoPictureAsset : public PictureAsset
186 {
187 public:
188         StereoPictureAsset (std::string directory, std::string mxf_name, int fps, int intrinsic_duration);
189
190         /** Construct a StereoPictureAsset for progressive writing using
191          *  start_write() and a StereoPictureAssetWriter.
192          *
193          *  @param directory Directory to put the MXF in.
194          *  @param mxf_name Filename of the MXF within this directory.
195          *  @param fps Video frames per second.
196          *  @param size Size in pixels that the picture frames will be.
197          */
198         StereoPictureAsset (std::string directory, std::string mxf_name, int fps, Size size, bool encrypted);
199
200         /** Start a progressive write to a StereoPictureAsset */
201         boost::shared_ptr<PictureAssetWriter> start_write (bool, bool, MXFMetadata const & metadata = MXFMetadata ());
202
203         boost::shared_ptr<const StereoPictureFrame> get_frame (int n) const;
204         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
205
206 private:
207         std::string cpl_node_name () const;
208         std::pair<std::string, std::string> cpl_node_attribute (bool) const;
209         int edit_rate_factor () const;
210 };
211         
212
213 }
214
215 #endif