Tweak API a little.
[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 files
25  */
26
27 #include <openjpeg.h>
28 #include "mxf_asset.h"
29 #include "util.h"
30
31 namespace libdcp
32 {
33
34 class MonoPictureFrame; 
35 class StereoPictureFrame;       
36
37 /** @brief An asset made up of JPEG2000 files */
38 class PictureAsset : public MXFAsset
39 {
40 public:
41         /** Construct a PictureAsset.
42          *  This class will not write anything to disk in this constructor, but subclasses may.
43          *  
44          *  @param directory Directory where MXF file is.
45          *  @param mxf_name Name of MXF file.
46          */
47         PictureAsset (std::string directory, std::string mxf_name);
48
49         /** Construct a PictureAsset.
50          *  This class will not write anything to disk in this constructor, but subclasses may.
51          *
52          *  @param directory Directory where MXF file is.
53          *  @param mxf_name Name of MXF file.
54          *  @param progress Signal to use to inform of progres, or 0.
55          *  @param fps Video Frames per second.
56          *  @param intrinsic_duration Duration of all the frames in the asset.
57          *  @param size Size of video frame images in pixels.
58          */
59         PictureAsset (std::string directory, std::string mxf_name, boost::signals2::signal<void (float)>* progress, int fps, int intrinsic_duration, Size size);
60         
61         /** Write details of this asset to a CPL stream.
62          *  @param s Stream.
63          */
64         void write_to_cpl (std::ostream& s) const;
65
66         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, std::list<std::string>& notes) const;
67
68         Size size () const {
69                 return _size;
70         }
71
72 protected:      
73
74         bool frame_buffer_equals (
75                 int frame, EqualityOptions opt, std::list<std::string>& notes,
76                 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
77                 ) const;
78
79         /** picture size in pixels */
80         Size _size;
81 };
82
83 class MonoPictureAsset;
84
85 struct FrameInfo
86 {
87         FrameInfo (uint64_t o, uint64_t s, std::string h)
88                 : offset (o)
89                 , size (s)
90                 , hash (h)
91         {}
92
93         FrameInfo (std::istream& s);
94
95         void write (std::ostream& s);
96         
97         uint64_t offset;
98         uint64_t size;
99         std::string hash;
100 };
101
102 /** A helper class for writing to MonoPictureAssets progressively (i.e. writing frame-by-frame,
103  *  rather than giving libdcp all the frames in one go).
104  *
105  *  Objects of this class can only be created with MonoPictureAsset::start_write().
106  *
107  *  Frames can be written to the MonoPictureAsset by calling write() with a JPEG2000 image
108  *  (a verbatim .j2 file).  finalize() must be called after the last frame has been written.
109  *  The action of finalize() can't be done in MonoPictureAssetWriter's destructor as it may
110  *  throw an exception.
111  */
112 class MonoPictureAssetWriter
113 {
114 public:
115         ~MonoPictureAssetWriter ();
116
117         FrameInfo write (uint8_t* data, int size);
118         void fake_write (int size);
119         void finalize ();
120
121 private:
122         friend class MonoPictureAsset;
123
124         MonoPictureAssetWriter (MonoPictureAsset *, bool);
125         void start (uint8_t *, int);
126
127         /* no copy construction */
128         MonoPictureAssetWriter (MonoPictureAssetWriter const &);
129         MonoPictureAssetWriter& operator= (MonoPictureAssetWriter const &);
130
131         /* do this with an opaque pointer so we don't have to include
132            ASDCP headers
133         */
134            
135         struct ASDCPState;
136         boost::shared_ptr<ASDCPState> _state;
137
138         MonoPictureAsset* _asset;
139         /** Number of picture frames written to the asset so far */
140         int _frames_written;
141         bool _started;
142         /** true if finalize() has been called */
143         bool _finalized;
144         bool _overwrite;
145 };
146
147 /** A 2D (monoscopic) picture asset */
148 class MonoPictureAsset : public PictureAsset
149 {
150 public:
151         /** Construct a MonoPictureAsset, generating the MXF from the JPEG2000 files.
152          *  This may take some time; progress is indicated by emission of the Progress signal.
153          *
154          *  @param files Pathnames of JPEG2000 files, in frame order.
155          *  @param directory Directory in which to create MXF file.
156          *  @param mxf_name Name of MXF file to create.
157          *  @param progress Signal to inform of progress.
158          *  @param fps Video frames per second.
159          *  @param intrinsic_duration Length of the whole asset in frames.
160          *  @param size Size of images in pixels.
161          */
162         MonoPictureAsset (
163                 std::vector<std::string> const & files,
164                 std::string directory,
165                 std::string mxf_name,
166                 boost::signals2::signal<void (float)>* progress,
167                 int fps,
168                 int intrinsic_duration,
169                 Size size
170                 );
171
172         /** Construct a MonoPictureAsset, generating the MXF from the JPEG2000 files.
173          *  This may take some time; progress is indicated by emission of the Progress signal.
174          *
175          *  @param get_path Functor which returns a JPEG2000 file path for a given frame (frames counted from 0).
176          *  @param directory Directory in which to create MXF file.
177          *  @param mxf_name Name of MXF file to create.
178          *  @param progress Signal to inform of progress.
179          *  @param fps Video frames per second.
180          *  @param intrinsic_duration Length of the whole asset in frames.
181          *  @param size Size of images in pixels.
182          */
183         MonoPictureAsset (
184                 boost::function<std::string (int)> get_path,
185                 std::string directory,
186                 std::string mxf_name,
187                 boost::signals2::signal<void (float)>* progress,
188                 int fps,
189                 int intrinsic_duration,
190                 Size size
191                 );
192
193         /** Construct a MonoPictureAsset, reading the MXF from disk.
194          *  @param directory Directory that the MXF is in.
195          *  @param mxf_name The filename of the MXF within `directory'.
196          */
197         MonoPictureAsset (std::string directory, std::string mxf_name);
198
199         /** Construct a MonoPictureAsset for progressive writing using
200          *  start_write() and a MonoPictureAssetWriter.
201          *
202          *  @param directory Directory to put the MXF in.
203          *  @param mxf_name Filename of the MXF within this directory.
204          *  @param fps Video frames per second.
205          *  @param size Size in pixels that the picture frames will be.
206          */
207         MonoPictureAsset (std::string directory, std::string mxf_name, int fps, Size size);
208
209         /** Start a progressive write to a MonoPictureAsset */
210         boost::shared_ptr<MonoPictureAssetWriter> start_write (bool);
211
212         boost::shared_ptr<const MonoPictureFrame> get_frame (int n) const;
213         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, std::list<std::string>& notes) const;
214
215 private:
216         std::string path_from_list (int f, std::vector<std::string> const & files) const;
217         void construct (boost::function<std::string (int)>);
218 };
219
220 /** A 3D (stereoscopic) picture asset */        
221 class StereoPictureAsset : public PictureAsset
222 {
223 public:
224         StereoPictureAsset (std::string directory, std::string mxf_name, int fps, int intrinsic_duration);
225         
226         boost::shared_ptr<const StereoPictureFrame> get_frame (int n) const;
227         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, std::list<std::string>& notes) const;
228 };
229         
230
231 }
232
233 #endif