dd69e352f5ff7512416a84e12999ea328b087477
[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 "AS_DCP.h"
26 #include "mxf_asset.h"
27 #include "util.h"
28
29 namespace libdcp
30 {
31
32 class MonoPictureFrame; 
33 class StereoPictureFrame;       
34
35 /** @brief An asset made up of JPEG2000 files */
36 class PictureAsset : public MXFAsset
37 {
38 public:
39         PictureAsset (std::string directory, std::string mxf_name);
40         PictureAsset (std::string directory, std::string mxf_name, boost::signals2::signal<void (float)>* progress, int fps, int intrinsic_duration, Size size);
41         
42         /** Write details of this asset to a CPL stream.
43          *  @param s Stream.
44          */
45         void write_to_cpl (std::ostream& s) const;
46
47         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, std::list<std::string>& notes) const;
48
49         Size size () const {
50                 return _size;
51         }
52
53 protected:      
54
55         bool frame_buffer_equals (
56                 int frame, EqualityOptions opt, std::list<std::string>& notes,
57                 uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
58                 ) const;
59
60         /** picture size in pixels */
61         Size _size;
62 };
63
64 class MonoPictureAsset;
65
66 class MonoPictureAssetWriter
67 {
68 public:
69         ~MonoPictureAssetWriter ();
70
71         void write (uint8_t* data, int size);
72         void finalize ();
73
74 private:
75         friend class MonoPictureAsset;
76         
77         MonoPictureAssetWriter (MonoPictureAsset *);
78
79         ASDCP::JP2K::CodestreamParser _j2k_parser;
80         ASDCP::JP2K::FrameBuffer _frame_buffer;
81         ASDCP::JP2K::MXFWriter _mxf_writer;
82         ASDCP::WriterInfo _writer_info;
83         ASDCP::JP2K::PictureDescriptor _picture_descriptor;
84         MonoPictureAsset* _asset;
85         int _frames_written;
86         bool _finalized;
87 };
88
89 /** A 2D (monoscopic) picture asset */
90 class MonoPictureAsset : public PictureAsset
91 {
92 public:
93         /** Construct a PictureAsset, generating the MXF from the JPEG2000 files.
94          *  This may take some time; progress is indicated by emission of the Progress signal.
95          *  @param files Pathnames of JPEG2000 files, in frame order.
96          *  @param directory Directory in which to create MXF file.
97          *  @param mxf_name Name of MXF file to create.
98          *  @param progress Signal to inform of progress.
99          *  @param fps Frames per second.
100          *  @param intrinsic_duration Length of the whole asset in frames.
101          *  @param size Size of images in pixels.
102          */
103         MonoPictureAsset (
104                 std::vector<std::string> const & files,
105                 std::string directory,
106                 std::string mxf_name,
107                 boost::signals2::signal<void (float)>* progress,
108                 int fps,
109                 int intrinsic_duration,
110                 Size size
111                 );
112
113         /** Construct a PictureAsset, generating the MXF from the JPEG2000 files.
114          *  This may take some time; progress is indicated by emission of the Progress signal.
115          *  @param get_path Functor which returns a JPEG2000 file path for a given frame (frames counted from 0).
116          *  @param directory Directory in which to create MXF file.
117          *  @param mxf_name Name of MXF file to create.
118          *  @param progress Signal to inform of progress.
119          *  @param fps Frames per second.
120          *  @param intrinsic_duration Length of the whole asset in frames.
121          *  @param size Size of images in pixels.
122          */
123         MonoPictureAsset (
124                 boost::function<std::string (int)> get_path,
125                 std::string directory,
126                 std::string mxf_name,
127                 boost::signals2::signal<void (float)>* progress,
128                 int fps,
129                 int intrinsic_duration,
130                 Size size
131                 );
132
133         MonoPictureAsset (std::string directory, std::string mxf_name);
134         
135         MonoPictureAsset (std::string directory, std::string mxf_name, int fps, Size size);
136
137         boost::shared_ptr<MonoPictureAssetWriter> start_write ();
138
139         boost::shared_ptr<const MonoPictureFrame> get_frame (int n) const;
140         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, std::list<std::string>& notes) const;
141
142 private:
143         std::string path_from_list (int f, std::vector<std::string> const & files) const;
144         void construct (boost::function<std::string (int)>);
145 };
146
147 /** A 3D (stereoscopic) picture asset */        
148 class StereoPictureAsset : public PictureAsset
149 {
150 public:
151         StereoPictureAsset (std::string directory, std::string mxf_name, int fps, int intrinsic_duration);
152         
153         boost::shared_ptr<const StereoPictureFrame> get_frame (int n) const;
154         bool equals (boost::shared_ptr<const Asset> other, EqualityOptions opt, std::list<std::string>& notes) const;
155 };
156         
157
158 }