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