Add round-trip KDM test. Fix various bugs in KDM generation. Some string -> path.
[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 (boost::filesystem::path 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                 boost::filesystem::path directory,
63                 std::string mxf_name,
64                 boost::signals2::signal<void (float)>* progress,
65                 int fps,
66                 int intrinsic_duration,
67                 Size
68                 );
69
70         /** Start a progressive write to this asset.
71          *  @param overwrite true to overwrite an existing MXF file; in this mode, writing can be resumed to a partially-written MXF; false if the
72          *  MXF file does not exist.
73          *  @param metadata MXF metadata to use.
74          */
75         virtual boost::shared_ptr<PictureAssetWriter> start_write (bool overwrite, bool interop, MXFMetadata const & metadata = MXFMetadata ()) = 0;
76         
77         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
78
79         Size size () const {
80                 return _size;
81         }
82
83         void write_to_cpl (xmlpp::Element *, bool) const;
84
85 protected:      
86
87         bool frame_buffer_equals (
88                 int frame, EqualityOptions opt, boost::function<void (NoteType, std::string)> note,
89                 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
90                 ) const;
91
92         /** picture size in pixels */
93         Size _size;
94
95 private:
96         std::string key_type () const;
97         virtual int edit_rate_factor () const = 0;
98 };
99
100 /** A 2D (monoscopic) picture asset */
101 class MonoPictureAsset : public PictureAsset
102 {
103 public:
104         /** Construct a MonoPictureAsset, generating the MXF from the JPEG2000 files.
105          *  This may take some time; progress is indicated by emission of the Progress signal.
106          *
107          *  @param files Pathnames of JPEG2000 files, in frame order.
108          *  @param directory Directory in which to create MXF file.
109          *  @param mxf_name Name of MXF file to create.
110          *  @param progress Signal to inform of progress.
111          *  @param fps Video frames per second.
112          *  @param intrinsic_duration Total number of frames in the asset.
113          *  @param size Size of images in pixels.
114          */
115         MonoPictureAsset (
116                 std::vector<boost::filesystem::path> const & files,
117                 boost::filesystem::path directory,
118                 std::string mxf_name,
119                 boost::signals2::signal<void (float)>* progress,
120                 int fps,
121                 int intrinsic_duration,
122                 Size size,
123                 bool interop,
124                 MXFMetadata const & metadata = MXFMetadata ()
125                 );
126
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 get_path Functor which returns a JPEG2000 file path for a given frame (frames counted from 0).
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 Total number of frames in the asset.
136          *  @param size Size of images in pixels.
137          */
138         MonoPictureAsset (
139                 boost::function<boost::filesystem::path (int)> get_path,
140                 boost::filesystem::path directory,
141                 std::string mxf_name,
142                 boost::signals2::signal<void (float)>* progress,
143                 int fps,
144                 int intrinsic_duration,
145                 Size size,
146                 bool interop,
147                 MXFMetadata const & metadata = MXFMetadata ()
148                 );
149
150         /** Construct a MonoPictureAsset, reading the MXF from disk.
151          *  @param directory Directory that the MXF is in.
152          *  @param mxf_name The filename of the MXF within `directory'.
153          */
154         MonoPictureAsset (boost::filesystem::path directory, std::string mxf_name);
155
156         /** Construct a MonoPictureAsset for progressive writing using
157          *  start_write() and a MonoPictureAssetWriter.
158          *
159          *  @param directory Directory to put the MXF in.
160          *  @param mxf_name Filename of the MXF within this directory.
161          *  @param fps Video frames per second.
162          *  @param size Size in pixels that the picture frames will be.
163          */
164         MonoPictureAsset (boost::filesystem::path directory, std::string mxf_name, int fps, Size size);
165
166         /** Start a progressive write to a MonoPictureAsset */
167         boost::shared_ptr<PictureAssetWriter> start_write (bool, bool, MXFMetadata const & metadata = MXFMetadata ());
168
169         boost::shared_ptr<const MonoPictureFrame> get_frame (int n) const;
170         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
171
172 private:
173         boost::filesystem::path path_from_list (int f, std::vector<boost::filesystem::path> const & files) const;
174         void construct (boost::function<boost::filesystem::path (int)>, bool, MXFMetadata const &);
175         std::string cpl_node_name () const;
176         int edit_rate_factor () const;
177 };
178
179 /** A 3D (stereoscopic) picture asset */        
180 class StereoPictureAsset : public PictureAsset
181 {
182 public:
183         StereoPictureAsset (boost::filesystem::path directory, std::string mxf_name, int fps, int intrinsic_duration);
184
185         /** Construct a StereoPictureAsset for progressive writing using
186          *  start_write() and a StereoPictureAssetWriter.
187          *
188          *  @param directory Directory to put the MXF in.
189          *  @param mxf_name Filename of the MXF within this directory.
190          *  @param fps Video frames per second.
191          *  @param size Size in pixels that the picture frames will be.
192          */
193         StereoPictureAsset (boost::filesystem::path directory, std::string mxf_name, int fps, Size size);
194
195         /** Start a progressive write to a StereoPictureAsset */
196         boost::shared_ptr<PictureAssetWriter> start_write (bool, bool, MXFMetadata const & metadata = MXFMetadata ());
197
198         boost::shared_ptr<const StereoPictureFrame> get_frame (int n) const;
199         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const;
200
201 private:
202         std::string cpl_node_name () const;
203         std::pair<std::string, std::string> cpl_node_attribute (bool) const;
204         int edit_rate_factor () const;
205 };
206         
207
208 }
209
210 #endif