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.
41 #include <netinet/in.h>
43 #include <boost/filesystem.hpp>
45 #include "dcp_video_frame.h"
48 #include "film_state.h"
50 #include "exceptions.h"
62 using namespace boost;
64 /** Construct a DCP video frame.
65 * @param input Input image.
66 * @param out Required size of output, in pixels (including any padding).
67 * @param s Scaler to use.
68 * @param p Number of pixels of padding either side of the image.
69 * @param f Index of the frame within the Film.
70 * @param fps Frames per second of the Film.
71 * @param pp FFmpeg post-processing string to use.
72 * @param clut Colour look-up table to use (see Config::colour_lut_index ())
73 * @param bw J2K bandwidth to use (see Config::j2k_bandwidth ())
74 * @param l Log to write to.
76 DCPVideoFrame::DCPVideoFrame (
77 shared_ptr<Image> yuv, Size out, int p, Scaler const * s, int f, float fps, string pp, int clut, int bw, Log* l)
83 /* we round here; not sure if this is right */
84 , _frames_per_second (rint (fps))
86 , _colour_lut_index (clut)
97 /** Create a libopenjpeg container suitable for our output image */
99 DCPVideoFrame::create_openjpeg_container ()
101 for (int i = 0; i < 3; ++i) {
104 _cmptparm[i].w = _out_size.width;
105 _cmptparm[i].h = _out_size.height;
108 _cmptparm[i].prec = 12;
109 _cmptparm[i].bpp = 12;
110 _cmptparm[i].sgnd = 0;
113 _image = opj_image_create (3, &_cmptparm[0], CLRSPC_SRGB);
115 throw EncodeError ("could not create libopenjpeg image");
120 _image->x1 = _out_size.width;
121 _image->y1 = _out_size.height;
124 DCPVideoFrame::~DCPVideoFrame ()
127 opj_image_destroy (_image);
131 opj_cio_close (_cio);
135 opj_destroy_compress (_cinfo);
139 free (_parameters->cp_comment);
140 free (_parameters->cp_matrice);
146 /** J2K-encode this frame on the local host.
147 * @return Encoded data.
149 shared_ptr<EncodedData>
150 DCPVideoFrame::encode_locally ()
152 shared_ptr<Image> prepared = _input;
154 if (!_post_process.empty ()) {
155 prepared = prepared->post_process (_post_process);
158 prepared = prepared->scale_and_convert_to_rgb (_out_size, _padding, _scaler);
160 create_openjpeg_container ();
162 int const size = _out_size.width * _out_size.height;
172 /* Copy our RGB into the openjpeg container, converting to XYZ in the process */
174 uint8_t* p = prepared->data()[0];
175 for (int i = 0; i < size; ++i) {
176 /* In gamma LUT (converting 8-bit input to 12-bit) */
177 s.r = lut_in[_colour_lut_index][*p++ << 4];
178 s.g = lut_in[_colour_lut_index][*p++ << 4];
179 s.b = lut_in[_colour_lut_index][*p++ << 4];
181 /* RGB to XYZ Matrix */
182 d.x = ((s.r * color_matrix[_colour_lut_index][0][0]) + (s.g * color_matrix[_colour_lut_index][0][1]) + (s.b * color_matrix[_colour_lut_index][0][2]));
183 d.y = ((s.r * color_matrix[_colour_lut_index][1][0]) + (s.g * color_matrix[_colour_lut_index][1][1]) + (s.b * color_matrix[_colour_lut_index][1][2]));
184 d.z = ((s.r * color_matrix[_colour_lut_index][2][0]) + (s.g * color_matrix[_colour_lut_index][2][1]) + (s.b * color_matrix[_colour_lut_index][2][2]));
187 d.x = d.x * DCI_COEFFICENT * (DCI_LUT_SIZE - 1);
188 d.y = d.y * DCI_COEFFICENT * (DCI_LUT_SIZE - 1);
189 d.z = d.z * DCI_COEFFICENT * (DCI_LUT_SIZE - 1);
192 _image->comps[0].data[i] = lut_out[LO_DCI][(int) d.x];
193 _image->comps[1].data[i] = lut_out[LO_DCI][(int) d.y];
194 _image->comps[2].data[i] = lut_out[LO_DCI][(int) d.z];
197 /* Set the max image and component sizes based on frame_rate */
198 int const max_cs_len = ((float) _j2k_bandwidth) / 8 / _frames_per_second;
199 int const max_comp_size = max_cs_len / 1.25;
201 /* Set encoding parameters to default values */
202 _parameters = new opj_cparameters_t;
203 opj_set_default_encoder_parameters (_parameters);
205 /* Set default cinema parameters */
206 _parameters->tile_size_on = false;
207 _parameters->cp_tdx = 1;
208 _parameters->cp_tdy = 1;
211 _parameters->tp_flag = 'C';
212 _parameters->tp_on = 1;
214 /* Tile and Image shall be at (0,0) */
215 _parameters->cp_tx0 = 0;
216 _parameters->cp_ty0 = 0;
217 _parameters->image_offset_x0 = 0;
218 _parameters->image_offset_y0 = 0;
220 /* Codeblock size = 32x32 */
221 _parameters->cblockw_init = 32;
222 _parameters->cblockh_init = 32;
223 _parameters->csty |= 0x01;
225 /* The progression order shall be CPRL */
226 _parameters->prog_order = CPRL;
229 _parameters->roi_compno = -1;
231 _parameters->subsampling_dx = 1;
232 _parameters->subsampling_dy = 1;
235 _parameters->irreversible = 1;
237 _parameters->tcp_rates[0] = 0;
238 _parameters->tcp_numlayers++;
239 _parameters->cp_disto_alloc = 1;
240 _parameters->cp_rsiz = CINEMA2K;
241 _parameters->cp_comment = strdup ("OpenDCP");
242 _parameters->cp_cinema = CINEMA2K_24;
244 /* 3 components, so use MCT */
245 _parameters->tcp_mct = 1;
248 _parameters->max_comp_size = max_comp_size;
249 _parameters->tcp_rates[0] = ((float) (3 * _image->comps[0].w * _image->comps[0].h * _image->comps[0].prec)) / (max_cs_len * 8);
251 /* get a J2K compressor handle */
252 _cinfo = opj_create_compress (CODEC_J2K);
254 /* Set event manager to null (openjpeg 1.3 bug) */
255 _cinfo->event_mgr = 0;
258 md5_data ("J2K in X", _image->comps[0].data, size * sizeof (int));
259 md5_data ("J2K in Y", _image->comps[1].data, size * sizeof (int));
260 md5_data ("J2K in Z", _image->comps[2].data, size * sizeof (int));
263 /* Setup the encoder parameters using the current image and user parameters */
264 opj_setup_encoder (_cinfo, _parameters, _image);
266 _cio = opj_cio_open ((opj_common_ptr) _cinfo, 0, 0);
268 int const r = opj_encode (_cinfo, _cio, _image, 0);
270 throw EncodeError ("jpeg2000 encoding failed");
274 md5_data ("J2K out", _cio->buffer, cio_tell (_cio));
279 s << "Finished locally-encoded frame " << _frame << " length " << cio_tell (_cio);
280 _log->log (s.str ());
283 return shared_ptr<EncodedData> (new LocallyEncodedData (_cio->buffer, cio_tell (_cio)));
286 /** Send this frame to a remote server for J2K encoding, then read the result.
287 * @param serv Server to send to.
288 * @return Encoded data.
290 shared_ptr<EncodedData>
291 DCPVideoFrame::encode_remotely (Server const * serv)
293 int const fd = socket (AF_INET, SOCK_STREAM, 0);
295 throw NetworkError ("could not create socket");
302 if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, (void *) &tv, sizeof (tv)) < 0) {
304 throw NetworkError ("setsockopt failed");
307 if (setsockopt (fd, SOL_SOCKET, SO_SNDTIMEO, (void *) &tv, sizeof (tv)) < 0) {
309 throw NetworkError ("setsockopt failed");
312 struct hostent* server = gethostbyname (serv->host_name().c_str ());
315 throw NetworkError ("gethostbyname failed");
318 struct sockaddr_in server_address;
319 memset (&server_address, 0, sizeof (server_address));
320 server_address.sin_family = AF_INET;
321 memcpy (&server_address.sin_addr.s_addr, server->h_addr, server->h_length);
322 server_address.sin_port = htons (Config::instance()->server_port ());
323 if (connect (fd, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
326 s << "could not connect (" << strerror (errno) << ")";
327 throw NetworkError (s.str());
331 _input->hash ("Input for remote encoding (before sending)");
336 << _input->size().width << " " << _input->size().height << " "
337 << _input->pixel_format() << " "
338 << _out_size.width << " " << _out_size.height << " "
340 << _scaler->id () << " "
342 << _frames_per_second << " "
343 << (_post_process.empty() ? "none" : _post_process) << " "
344 << Config::instance()->colour_lut_index () << " "
345 << Config::instance()->j2k_bandwidth () << " ";
347 for (int i = 0; i < _input->components(); ++i) {
348 s << _input->line_size()[i] << " ";
351 socket_write (fd, (uint8_t *) s.str().c_str(), s.str().length() + 1);
353 for (int i = 0; i < _input->components(); ++i) {
354 socket_write (fd, _input->data()[i], _input->line_size()[i] * _input->lines(i));
357 SocketReader reader (fd);
360 reader.read_indefinite ((uint8_t *) buffer, sizeof (buffer));
361 reader.consume (strlen (buffer) + 1);
362 shared_ptr<EncodedData> e (new RemotelyEncodedData (atoi (buffer)));
364 /* now read the rest */
365 reader.read_definite_and_consume (e->data(), e->size());
368 e->hash ("Encoded image (after receiving)");
373 s << "Finished remotely-encoded frame " << _frame << " length " << e->size();
374 _log->log (s.str ());
381 /** Write this data to a J2K file.
382 * @param opt Options.
383 * @param frame Frame index.
386 EncodedData::write (shared_ptr<const Options> opt, int frame)
388 string const tmp_j2k = opt->frame_out_path (frame, true);
390 FILE* f = fopen (tmp_j2k.c_str (), "wb");
393 throw WriteFileError (tmp_j2k, errno);
396 fwrite (_data, 1, _size, f);
399 /* Rename the file from foo.j2c.tmp to foo.j2c now that it is complete */
400 filesystem::rename (tmp_j2k, opt->frame_out_path (frame, false));
403 /** Send this data to a file descriptor.
404 * @param fd File descriptor.
407 EncodedData::send (int fd)
411 socket_write (fd, (uint8_t *) s.str().c_str(), s.str().length() + 1);
412 socket_write (fd, _data, _size);
417 EncodedData::hash (string n) const
419 md5_data (n, _data, _size);
423 /** @param s Size of data in bytes */
424 RemotelyEncodedData::RemotelyEncodedData (int s)
425 : EncodedData (new uint8_t[s], s)
430 RemotelyEncodedData::~RemotelyEncodedData ()