Merge master.
[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 <libdcp/picture_asset.h>
23 #include "util.h"
24
25 /** @file  src/dcp_video_frame.h
26  *  @brief A single frame of video destined for a DCP.
27  */
28
29 class Film;
30 class ServerDescription;
31 class Scaler;
32 class Image;
33 class Log;
34 class Subtitle;
35
36 /** @class EncodedData
37  *  @brief Container for J2K-encoded data.
38  */
39 class EncodedData
40 {
41 public:
42         /** @param s Size of data, in bytes */
43         EncodedData (int s);
44
45         EncodedData (std::string f);
46
47         virtual ~EncodedData ();
48
49         void send (boost::shared_ptr<Socket> socket);
50         void write (boost::shared_ptr<const Film>, int) const;
51         void write_info (boost::shared_ptr<const Film>, int, libdcp::FrameInfo) const;
52
53         /** @return data */
54         uint8_t* data () const {
55                 return _data;
56         }
57
58         /** @return data size, in bytes */
59         int size () const {
60                 return _size;
61         }
62
63 protected:
64         uint8_t* _data; ///< data
65         int _size;      ///< data size in bytes
66
67 private:
68         /* No copy construction */
69         EncodedData (EncodedData const &);
70 };
71
72 /** @class LocallyEncodedData
73  *  @brief EncodedData that was encoded locally; this class
74  *  just keeps a pointer to the data, but does no memory
75  *  management.
76  */
77 class LocallyEncodedData : public EncodedData
78 {
79 public:
80         /** @param d Data (which will be copied by this class)
81          *  @param s Size of data, in bytes.
82          */
83         LocallyEncodedData (uint8_t* d, int s);
84 };
85
86 /** @class RemotelyEncodedData
87  *  @brief EncodedData that is being read from a remote server;
88  *  this class allocates and manages memory for the data.
89  */
90 class RemotelyEncodedData : public EncodedData
91 {
92 public:
93         RemotelyEncodedData (int s);
94 };
95
96 /** @class DCPVideoFrame
97  *  @brief A single frame of video destined for a DCP.
98  *
99  *  Given an Image and some settings, this class knows how to encode
100  *  the image to J2K either on the local host or on a remote server.
101  *
102  *  Objects of this class are used for the queue that we keep
103  *  of images that require encoding.
104  */
105 class DCPVideoFrame
106 {
107 public:
108         DCPVideoFrame (boost::shared_ptr<const Image>, int, int, int, int, boost::shared_ptr<Log>);
109         ~DCPVideoFrame ();
110
111         boost::shared_ptr<EncodedData> encode_locally ();
112         boost::shared_ptr<EncodedData> encode_remotely (ServerDescription const *);
113
114         int frame () const {
115                 return _frame;
116         }
117         
118 private:
119         void create_openjpeg_container ();
120
121         boost::shared_ptr<const Image> _image;
122         int _frame;                      ///< frame index within the DCP's intrinsic duration
123         int _frames_per_second;          ///< Frames per second that we will use for the DCP
124         int _colour_lut;                 ///< Colour look-up table to use
125         int _j2k_bandwidth;              ///< J2K bandwidth to use
126
127         boost::shared_ptr<Log> _log; ///< log
128
129         opj_image_cmptparm_t _cmptparm[3]; ///< libopenjpeg's opj_image_cmptparm_t
130         opj_image* _opj_image;             ///< libopenjpeg's image container 
131         opj_cparameters_t* _parameters;    ///< libopenjpeg's parameters
132         opj_cinfo_t* _cinfo;               ///< libopenjpeg's opj_cinfo_t
133         opj_cio_t* _cio;                   ///< libopenjpeg's opj_cio_t
134 };