Merge.
[dcpomatic.git] / src / lib / dcp_video_frame.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3     Taken from code Copyright (C) 2010-2011 Terrence Meiczinger
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <openjpeg.h>
22 #include "util.h"
23
24 /** @file  src/dcp_video_frame.h
25  *  @brief A single frame of video destined for a DCP.
26  */
27
28 class FilmState;
29 class Options;
30 class Server;
31 class Scaler;
32 class Image;
33 class Log;
34
35 /** @class EncodedData
36  *  @brief Container for J2K-encoded data.
37  */
38 class EncodedData
39 {
40 public:
41         /** @param d Data (will not be freed by this class, but may be by subclasses)
42          *  @param s Size of data, in bytes.
43          */
44         EncodedData (uint8_t* d, int s)
45                 : _data (d)
46                 , _size (s)
47         {}
48
49         virtual ~EncodedData () {}
50
51 #ifdef DVDOMATIC_POSIX  
52         void send (int);
53 #endif  
54         void write (boost::shared_ptr<const Options>, int);
55
56 #ifdef DEBUG_HASH
57         void hash (std::string) const;
58 #endif  
59
60         /** @return data */
61         uint8_t* data () const {
62                 return _data;
63         }
64
65         /** @return data size, in bytes */
66         int size () const {
67                 return _size;
68         }
69
70 protected:
71         uint8_t* _data; ///< data
72         int _size;      ///< data size in bytes
73 };
74
75 /** @class LocallyEncodedData
76  *  @brief EncodedData that was encoded locally; this class
77  *  just keeps a pointer to the data, but does no memory
78  *  management.
79  */
80 class LocallyEncodedData : public EncodedData
81 {
82 public:
83         /** @param d Data (which will not be freed by this class)
84          *  @param s Size of data, in bytes.
85          */
86         LocallyEncodedData (uint8_t* d, int s)
87                 : EncodedData (d, s)
88         {}
89 };
90
91 /** @class RemotelyEncodedData
92  *  @brief EncodedData that is being read from a remote server;
93  *  this class allocates and manages memory for the data.
94  */
95 class RemotelyEncodedData : public EncodedData
96 {
97 public:
98         RemotelyEncodedData (int s);
99         ~RemotelyEncodedData ();
100 };
101
102 /** @class DCPVideoFrame
103  *  @brief A single frame of video destined for a DCP.
104  *
105  *  Given an Image and some settings, this class knows how to encode
106  *  the image to J2K either on the local host or on a remote server.
107  *
108  *  Objects of this class are used for the queue that we keep
109  *  of images that require encoding.
110  */
111 class DCPVideoFrame
112 {
113 public:
114         DCPVideoFrame (boost::shared_ptr<Image>, Size, int, Scaler const *, int, float, std::string, int, int, Log *);
115         virtual ~DCPVideoFrame ();
116
117         boost::shared_ptr<EncodedData> encode_locally ();
118         boost::shared_ptr<EncodedData> encode_remotely (Server const *);
119
120         int frame () const {
121                 return _frame;
122         }
123         
124 private:
125         void create_openjpeg_container ();
126         void write_encoded (boost::shared_ptr<const Options>, uint8_t *, int);
127
128         boost::shared_ptr<Image> _input; ///< the input image
129         Size _out_size;                  ///< the required size of the output, in pixels
130         int _padding;
131         Scaler const * _scaler;          ///< scaler to use
132         int _frame;                      ///< frame index within the Film
133         int _frames_per_second;          ///< Frames per second that we will use for the DCP (rounded)
134         std::string _post_process;       ///< FFmpeg post-processing string to use
135         int _colour_lut_index;           ///< Colour look-up table to use (see Config::colour_lut_index ())
136         int _j2k_bandwidth;              ///< J2K bandwidth to use (see Config::j2k_bandwidth ())
137
138         Log* _log; ///< log
139
140         opj_image_cmptparm_t _cmptparm[3]; ///< libopenjpeg's opj_image_cmptparm_t
141         opj_image* _image;                 ///< libopenjpeg's image container 
142         opj_cparameters_t* _parameters;    ///< libopenjpeg's parameters
143         opj_cinfo_t* _cinfo;               ///< libopenjpeg's opj_cinfo_t
144         opj_cio_t* _cio;                   ///< libopenjpeg's opj_cio_t
145 };