2031a9ed5bd052b72074e07a6bc52be3e5eb7226
[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 class MonoPictureAssetWriter;
38 class StereoPictureAssetWriter;
39
40 /** @brief An asset made up of JPEG2000 files */
41 class PictureAsset : public MXFAsset
42 {
43 public:
44         /** Construct a PictureAsset.
45          *  This class will not write anything to disk in this constructor, but subclasses may.
46          *  
47          *  @param directory Directory where MXF file is.
48          *  @param mxf_name Name of MXF file.
49          */
50         PictureAsset (std::string directory, std::string mxf_name);
51
52         /** Construct a PictureAsset.
53          *  This class will not write anything to disk in this constructor, but subclasses may.
54          *
55          *  @param directory Directory where MXF file is.
56          *  @param mxf_name Name of MXF file.
57          *  @param progress Signal to use to inform of progres, or 0.
58          *  @param fps Video Frames per second.
59          *  @param intrinsic_duration Duration of all the frames in the asset.
60          *  @param size Size of video frame images in pixels.
61          */
62         PictureAsset (std::string directory, std::string mxf_name, boost::signals2::signal<void (float)>* progress, int fps, int intrinsic_duration, bool encrypted, Size);
63         
64         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
65
66         Size size () const {
67                 return _size;
68         }
69
70         void write_to_cpl (xmlpp::Node *) const;
71
72 protected:      
73
74         bool frame_buffer_equals (
75                 int frame, EqualityOptions opt, boost::function<void (NoteType, std::string)> note,
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 private:
83         std::string key_type () const;
84         std::string cpl_node_name () const;
85 };
86
87 class MonoPictureAsset;
88
89
90 /** A 2D (monoscopic) picture asset */
91 class MonoPictureAsset : public PictureAsset
92 {
93 public:
94         /** Construct a MonoPictureAsset, generating the MXF from the JPEG2000 files.
95          *  This may take some time; progress is indicated by emission of the Progress signal.
96          *
97          *  @param files Pathnames of JPEG2000 files, in frame order.
98          *  @param directory Directory in which to create MXF file.
99          *  @param mxf_name Name of MXF file to create.
100          *  @param progress Signal to inform of progress.
101          *  @param fps Video frames per second.
102          *  @param intrinsic_duration Length of the whole asset in frames.
103          *  @param size Size of images in pixels.
104          *  @param encrypted true if asset should be encrypted.
105          */
106         MonoPictureAsset (
107                 std::vector<std::string> const & files,
108                 std::string directory,
109                 std::string mxf_name,
110                 boost::signals2::signal<void (float)>* progress,
111                 int fps,
112                 int intrinsic_duration,
113                 bool encrypted,
114                 Size size,
115                 MXFMetadata const & metadata = MXFMetadata ()
116                 );
117
118         /** Construct a MonoPictureAsset, generating the MXF from the JPEG2000 files.
119          *  This may take some time; progress is indicated by emission of the Progress signal.
120          *
121          *  @param get_path Functor which returns a JPEG2000 file path for a given frame (frames counted from 0).
122          *  @param directory Directory in which to create MXF file.
123          *  @param mxf_name Name of MXF file to create.
124          *  @param progress Signal to inform of progress.
125          *  @param fps Video frames per second.
126          *  @param intrinsic_duration Length of the whole asset in frames.
127          *  @param size Size of images in pixels.
128          *  @param encrypted true if asset should be encrypted.
129          */
130         MonoPictureAsset (
131                 boost::function<std::string (int)> get_path,
132                 std::string directory,
133                 std::string mxf_name,
134                 boost::signals2::signal<void (float)>* progress,
135                 int fps,
136                 int intrinsic_duration,
137                 bool encrypted,
138                 Size size,
139                 MXFMetadata const & metadata = MXFMetadata ()
140                 );
141
142         /** Construct a MonoPictureAsset, reading the MXF from disk.
143          *  @param directory Directory that the MXF is in.
144          *  @param mxf_name The filename of the MXF within `directory'.
145          */
146         MonoPictureAsset (std::string directory, std::string mxf_name);
147
148         /** Construct a MonoPictureAsset for progressive writing using
149          *  start_write() and a MonoPictureAssetWriter.
150          *
151          *  @param directory Directory to put the MXF in.
152          *  @param mxf_name Filename of the MXF within this directory.
153          *  @param fps Video frames per second.
154          *  @param size Size in pixels that the picture frames will be.
155          */
156         MonoPictureAsset (std::string directory, std::string mxf_name, int fps, Size size);
157
158         /** Start a progressive write to a MonoPictureAsset */
159         boost::shared_ptr<MonoPictureAssetWriter> start_write (bool, MXFMetadata const & metadata = MXFMetadata ());
160
161         boost::shared_ptr<const MonoPictureFrame> get_frame (int n) const;
162         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
163
164 private:
165         std::string path_from_list (int f, std::vector<std::string> const & files) const;
166         void construct (boost::function<std::string (int)>, MXFMetadata const &);
167 };
168
169 /** A 3D (stereoscopic) picture asset */        
170 class StereoPictureAsset : public PictureAsset
171 {
172 public:
173         StereoPictureAsset (std::string directory, std::string mxf_name, int fps, int intrinsic_duration);
174
175         /** Construct a StereoPictureAsset for progressive writing using
176          *  start_write() and a StereoPictureAssetWriter.
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         StereoPictureAsset (std::string directory, std::string mxf_name, int fps, Size size);
184
185         /** Start a progressive write to a StereoPictureAsset */
186         boost::shared_ptr<StereoPictureAssetWriter> start_write (bool, MXFMetadata const & metadata = MXFMetadata ());
187
188         boost::shared_ptr<const StereoPictureFrame> get_frame (int n) const;
189         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
190 };
191         
192
193 }
194
195 #endif