2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3 Taken from code Copyright (C) 2010-2011 Terrence Meiczinger
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.
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.
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.
21 /** @file src/dcp_video_frame.cc
22 * @brief A single frame of video destined for a DCP.
24 * Given an Image and some settings, this class knows how to encode
25 * the image to J2K either on the local host or on a remote server.
27 * Objects of this class are used for the queue that we keep
28 * of images that require encoding.
42 #include <boost/array.hpp>
43 #include <boost/asio.hpp>
44 #include <boost/filesystem.hpp>
45 #include <boost/lexical_cast.hpp>
46 #include <libdcp/rec709_linearised_gamma_lut.h>
47 #include <libdcp/srgb_linearised_gamma_lut.h>
48 #include <libdcp/gamma_lut.h>
49 #include <libdcp/xyz_frame.h>
50 #include <libdcp/rgb_xyz.h>
51 #include <libdcp/colour_matrix.h>
53 #include "dcp_video_frame.h"
55 #include "exceptions.h"
65 using std::stringstream;
68 using boost::shared_ptr;
71 #define DCI_COEFFICENT (48.0 / 52.37)
73 /** Construct a DCP video frame.
74 * @param input Input image.
75 * @param f Index of the frame within the DCP.
76 * @param clut Colour look-up table to use (see Config::colour_lut_index ())
77 * @param bw J2K bandwidth to use (see Config::j2k_bandwidth ())
78 * @param l Log to write to.
80 DCPVideoFrame::DCPVideoFrame (
81 shared_ptr<const Image> image, int f, int dcp_fps, int clut, int bw, shared_ptr<Log> l
85 , _frames_per_second (dcp_fps)
96 DCPVideoFrame::~DCPVideoFrame ()
103 opj_destroy_compress (_cinfo);
107 free (_parameters->cp_comment);
113 /** J2K-encode this frame on the local host.
114 * @return Encoded data.
116 shared_ptr<EncodedData>
117 DCPVideoFrame::encode_locally ()
119 /* In sRGB / Rec709 gamma LUT */
120 shared_ptr<libdcp::LUT> lut_in;
121 if (_colour_lut == 0) {
122 lut_in = libdcp::SRGBLinearisedGammaLUT::cache.get (12, 2.4);
124 lut_in = libdcp::Rec709LinearisedGammaLUT::cache.get (12, 1 / 0.45);
127 /* Out DCI gamma LUT */
128 shared_ptr<libdcp::LUT> lut_out = libdcp::GammaLUT::cache.get (16, 1 / 2.6);
130 shared_ptr<libdcp::XYZFrame> xyz = libdcp::rgb_to_xyz (
131 _image, lut_in, lut_out, _colour_lut == 0 ? libdcp::colour_matrix::srgb_to_xyz : libdcp::colour_matrix::rec709_to_xyz
134 /* Set the max image and component sizes based on frame_rate */
135 int const max_cs_len = ((float) _j2k_bandwidth) / 8 / _frames_per_second;
136 int const max_comp_size = max_cs_len / 1.25;
138 /* Set encoding parameters to default values */
139 _parameters = new opj_cparameters_t;
140 opj_set_default_encoder_parameters (_parameters);
142 /* Set default cinema parameters */
143 _parameters->tile_size_on = false;
144 _parameters->cp_tdx = 1;
145 _parameters->cp_tdy = 1;
148 _parameters->tp_flag = 'C';
149 _parameters->tp_on = 1;
151 /* Tile and Image shall be at (0,0) */
152 _parameters->cp_tx0 = 0;
153 _parameters->cp_ty0 = 0;
154 _parameters->image_offset_x0 = 0;
155 _parameters->image_offset_y0 = 0;
157 /* Codeblock size = 32x32 */
158 _parameters->cblockw_init = 32;
159 _parameters->cblockh_init = 32;
160 _parameters->csty |= 0x01;
162 /* The progression order shall be CPRL */
163 _parameters->prog_order = CPRL;
166 _parameters->roi_compno = -1;
168 _parameters->subsampling_dx = 1;
169 _parameters->subsampling_dy = 1;
172 _parameters->irreversible = 1;
174 _parameters->tcp_rates[0] = 0;
175 _parameters->tcp_numlayers++;
176 _parameters->cp_disto_alloc = 1;
177 _parameters->cp_rsiz = CINEMA2K;
178 _parameters->cp_comment = strdup (N_("DCP-o-matic"));
179 _parameters->cp_cinema = CINEMA2K_24;
181 /* 3 components, so use MCT */
182 _parameters->tcp_mct = 1;
185 _parameters->max_comp_size = max_comp_size;
186 _parameters->tcp_rates[0] = ((float) (3 * xyz->size().width * xyz->size().height * 12)) / (max_cs_len * 8);
188 /* get a J2K compressor handle */
189 _cinfo = opj_create_compress (CODEC_J2K);
191 throw EncodeError (N_("could not create JPEG2000 encoder"));
194 /* Set event manager to null (openjpeg 1.3 bug) */
195 _cinfo->event_mgr = 0;
197 /* Setup the encoder parameters using the current image and user parameters */
198 opj_setup_encoder (_cinfo, _parameters, xyz->opj_image ());
200 _cio = opj_cio_open ((opj_common_ptr) _cinfo, 0, 0);
202 throw EncodeError (N_("could not open JPEG2000 stream"));
205 int const r = opj_encode (_cinfo, _cio, xyz->opj_image(), 0);
207 throw EncodeError (N_("JPEG2000 encoding failed"));
210 _log->log (String::compose (N_("Finished locally-encoded frame %1"), _frame));
212 return shared_ptr<EncodedData> (new LocallyEncodedData (_cio->buffer, cio_tell (_cio)));
215 /** Send this frame to a remote server for J2K encoding, then read the result.
216 * @param serv Server to send to.
217 * @return Encoded data.
219 shared_ptr<EncodedData>
220 DCPVideoFrame::encode_remotely (ServerDescription const * serv)
222 boost::asio::io_service io_service;
223 boost::asio::ip::tcp::resolver resolver (io_service);
224 boost::asio::ip::tcp::resolver::query query (serv->host_name(), boost::lexical_cast<string> (Config::instance()->server_port ()));
225 boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
227 shared_ptr<Socket> socket (new Socket);
229 socket->connect (*endpoint_iterator);
232 s << N_("encode please\n")
233 << N_("width ") << _image->size().width << N_("\n")
234 << N_("height ") << _image->size().height << N_("\n")
235 << N_("frame ") << _frame << N_("\n")
236 << N_("frames_per_second ") << _frames_per_second << N_("\n")
237 << N_("colour_lut ") << _colour_lut << N_("\n")
238 << N_("j2k_bandwidth ") << _j2k_bandwidth << N_("\n");
240 _log->log (String::compose (
241 N_("Sending to remote; pixel format %1, components %2, lines (%3,%4,%5), line sizes (%6,%7,%8)"),
242 _image->pixel_format(), _image->components(),
243 _image->lines(0), _image->lines(1), _image->lines(2),
244 _image->line_size()[0], _image->line_size()[1], _image->line_size()[2]
247 socket->write (s.str().length() + 1);
248 socket->write ((uint8_t *) s.str().c_str(), s.str().length() + 1);
250 _image->write_to_socket (socket);
252 shared_ptr<EncodedData> e (new RemotelyEncodedData (socket->read_uint32 ()));
253 socket->read (e->data(), e->size());
255 _log->log (String::compose (N_("Finished remotely-encoded frame %1"), _frame));
260 EncodedData::EncodedData (int s)
261 : _data (new uint8_t[s])
267 EncodedData::EncodedData (string file)
269 _size = boost::filesystem::file_size (file);
270 _data = new uint8_t[_size];
272 FILE* f = fopen (file.c_str(), N_("rb"));
274 throw FileError (_("could not open file for reading"), file);
277 fread (_data, 1, _size, f);
282 EncodedData::~EncodedData ()
287 /** Write this data to a J2K file.
289 * @param frame DCP frame index.
292 EncodedData::write (shared_ptr<const Film> film, int frame) const
294 string const tmp_j2c = film->j2c_path (frame, true);
296 FILE* f = fopen (tmp_j2c.c_str (), N_("wb"));
299 throw WriteFileError (tmp_j2c, errno);
302 fwrite (_data, 1, _size, f);
305 string const real_j2c = film->j2c_path (frame, false);
307 /* Rename the file from foo.j2c.tmp to foo.j2c now that it is complete */
308 boost::filesystem::rename (tmp_j2c, real_j2c);
312 EncodedData::write_info (shared_ptr<const Film> film, int frame, libdcp::FrameInfo fin) const
314 string const info = film->info_path (frame);
315 ofstream h (info.c_str());
319 /** Send this data to a socket.
320 * @param socket Socket
323 EncodedData::send (shared_ptr<Socket> socket)
325 socket->write (_size);
326 socket->write (_data, _size);
329 LocallyEncodedData::LocallyEncodedData (uint8_t* d, int s)
332 memcpy (_data, d, s);
335 /** @param s Size of data in bytes */
336 RemotelyEncodedData::RemotelyEncodedData (int s)