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