c4234b1dacdcba7e8a701c4639852c8e658c499b
[dcpomatic.git] / src / lib / dcp_video_frame.cc
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 /** @file  src/dcp_video_frame.cc
22  *  @brief A single frame of video destined for a DCP.
23  *
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.
26  *
27  *  Objects of this class are used for the queue that we keep
28  *  of images that require encoding.
29  */
30
31 #include <stdint.h>
32 #include <cstring>
33 #include <cstdlib>
34 #include <stdexcept>
35 #include <cstdio>
36 #include <iomanip>
37 #include <sstream>
38 #include <iostream>
39 #include <fstream>
40 #include <unistd.h>
41 #include <errno.h>
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 "film.h"
50 #include "dcp_video_frame.h"
51 #include "config.h"
52 #include "exceptions.h"
53 #include "server.h"
54 #include "util.h"
55 #include "scaler.h"
56 #include "image.h"
57 #include "log.h"
58 #include "colour_matrices.h"
59
60 #include "i18n.h"
61
62 using std::string;
63 using std::stringstream;
64 using std::ofstream;
65 using std::cout;
66 using boost::shared_ptr;
67 using libdcp::Size;
68
69 #define DCI_COEFFICENT (48.0 / 52.37)
70
71 /** Construct a DCP video frame.
72  *  @param input Input image.
73  *  @param f Index of the frame within the DCP.
74  *  @param clut Colour look-up table to use (see Config::colour_lut_index ())
75  *  @param bw J2K bandwidth to use (see Config::j2k_bandwidth ())
76  *  @param l Log to write to.
77  */
78 DCPVideoFrame::DCPVideoFrame (
79         shared_ptr<const Image> image, int f, int dcp_fps, int clut, int bw, shared_ptr<Log> l
80         )
81         : _image (image)
82         , _frame (f)
83         , _frames_per_second (dcp_fps)
84         , _colour_lut (clut)
85         , _j2k_bandwidth (bw)
86         , _log (l)
87         , _opj_image (0)
88         , _parameters (0)
89         , _cinfo (0)
90         , _cio (0)
91 {
92         
93 }
94
95 /** Create a libopenjpeg container suitable for our output image */
96 void
97 DCPVideoFrame::create_openjpeg_container ()
98 {
99         for (int i = 0; i < 3; ++i) {
100                 _cmptparm[i].dx = 1;
101                 _cmptparm[i].dy = 1;
102                 _cmptparm[i].w = _image->size().width;
103                 _cmptparm[i].h = _image->size().height;
104                 _cmptparm[i].x0 = 0;
105                 _cmptparm[i].y0 = 0;
106                 _cmptparm[i].prec = 12;
107                 _cmptparm[i].bpp = 12;
108                 _cmptparm[i].sgnd = 0;
109         }
110
111         _opj_image = opj_image_create (3, &_cmptparm[0], CLRSPC_SRGB);
112         if (_opj_image == 0) {
113                 throw EncodeError (N_("could not create libopenjpeg image"));
114         }
115
116         _opj_image->x0 = 0;
117         _opj_image->y0 = 0;
118         _opj_image->x1 = _image->size().width;
119         _opj_image->y1 = _image->size().height;
120 }
121
122 DCPVideoFrame::~DCPVideoFrame ()
123 {
124         if (_opj_image) {
125                 opj_image_destroy (_opj_image);
126         }
127
128         if (_cio) {
129                 opj_cio_close (_cio);
130         }
131
132         if (_cinfo) {
133                 opj_destroy_compress (_cinfo);
134         }
135
136         if (_parameters) {
137                 free (_parameters->cp_comment);
138         }
139         
140         delete _parameters;
141 }
142
143 /** J2K-encode this frame on the local host.
144  *  @return Encoded data.
145  */
146 shared_ptr<EncodedData>
147 DCPVideoFrame::encode_locally ()
148 {
149         create_openjpeg_container ();
150
151         struct {
152                 double r, g, b;
153         } s;
154
155         struct {
156                 double x, y, z;
157         } d;
158
159         /* In sRGB / Rec709 gamma LUT */
160         shared_ptr<libdcp::LUT<float> > lut_in;
161         if (_colour_lut == 0) {
162                 lut_in = libdcp::SRGBLinearisedGammaLUT::cache.get (12, 2.4);
163         } else {
164                 lut_in = libdcp::Rec709LinearisedGammaLUT::cache.get (12, 1 / 0.45);
165         }
166
167         /* Out DCI gamma LUT */
168         shared_ptr<libdcp::LUT<float> > lut_out = libdcp::GammaLUT::cache.get (16, 1 / 2.6);
169
170         /* Copy our RGB into the openjpeg container, converting to XYZ in the process */
171
172         int jn = 0;
173         for (int y = 0; y < _image->size().height; ++y) {
174                 uint8_t* p = _image->data()[0] + y * _image->stride()[0];
175                 for (int x = 0; x < _image->size().width; ++x) {
176
177                         /* In gamma LUT (converting 8-bit input to 12-bit) */
178                         s.r = lut_in->lut()[*p++ << 4];
179                         s.g = lut_in->lut()[*p++ << 4];
180                         s.b = lut_in->lut()[*p++ << 4];
181                         
182                         /* RGB to XYZ Matrix */
183                         d.x = ((s.r * color_matrix[_colour_lut][0][0]) +
184                                (s.g * color_matrix[_colour_lut][0][1]) +
185                                (s.b * color_matrix[_colour_lut][0][2]));
186                         
187                         d.y = ((s.r * color_matrix[_colour_lut][1][0]) +
188                                (s.g * color_matrix[_colour_lut][1][1]) +
189                                (s.b * color_matrix[_colour_lut][1][2]));
190                         
191                         d.z = ((s.r * color_matrix[_colour_lut][2][0]) +
192                                (s.g * color_matrix[_colour_lut][2][1]) +
193                                (s.b * color_matrix[_colour_lut][2][2]));
194                         
195                         /* DCI companding */
196                         d.x = d.x * DCI_COEFFICENT * 65535;
197                         d.y = d.y * DCI_COEFFICENT * 65535;
198                         d.z = d.z * DCI_COEFFICENT * 65535;
199                         
200                         /* Out gamma LUT */
201                         _opj_image->comps[0].data[jn] = lut_out->lut()[(int) d.x] * 4096;
202                         _opj_image->comps[1].data[jn] = lut_out->lut()[(int) d.y] * 4096;
203                         _opj_image->comps[2].data[jn] = lut_out->lut()[(int) d.z] * 4096;
204
205                         ++jn;
206                 }
207         }
208
209         /* Set the max image and component sizes based on frame_rate */
210         int const max_cs_len = ((float) _j2k_bandwidth) / 8 / _frames_per_second;
211         int const max_comp_size = max_cs_len / 1.25;
212
213         /* Set encoding parameters to default values */
214         _parameters = new opj_cparameters_t;
215         opj_set_default_encoder_parameters (_parameters);
216
217         /* Set default cinema parameters */
218         _parameters->tile_size_on = false;
219         _parameters->cp_tdx = 1;
220         _parameters->cp_tdy = 1;
221         
222         /* Tile part */
223         _parameters->tp_flag = 'C';
224         _parameters->tp_on = 1;
225         
226         /* Tile and Image shall be at (0,0) */
227         _parameters->cp_tx0 = 0;
228         _parameters->cp_ty0 = 0;
229         _parameters->image_offset_x0 = 0;
230         _parameters->image_offset_y0 = 0;
231
232         /* Codeblock size = 32x32 */
233         _parameters->cblockw_init = 32;
234         _parameters->cblockh_init = 32;
235         _parameters->csty |= 0x01;
236         
237         /* The progression order shall be CPRL */
238         _parameters->prog_order = CPRL;
239         
240         /* No ROI */
241         _parameters->roi_compno = -1;
242         
243         _parameters->subsampling_dx = 1;
244         _parameters->subsampling_dy = 1;
245         
246         /* 9-7 transform */
247         _parameters->irreversible = 1;
248         
249         _parameters->tcp_rates[0] = 0;
250         _parameters->tcp_numlayers++;
251         _parameters->cp_disto_alloc = 1;
252         _parameters->cp_rsiz = CINEMA2K;
253         _parameters->cp_comment = strdup (N_("DCP-o-matic"));
254         _parameters->cp_cinema = CINEMA2K_24;
255
256         /* 3 components, so use MCT */
257         _parameters->tcp_mct = 1;
258         
259         /* set max image */
260         _parameters->max_comp_size = max_comp_size;
261         _parameters->tcp_rates[0] = ((float) (3 * _opj_image->comps[0].w * _opj_image->comps[0].h * _opj_image->comps[0].prec)) / (max_cs_len * 8);
262
263         /* get a J2K compressor handle */
264         _cinfo = opj_create_compress (CODEC_J2K);
265         if (_cinfo == 0) {
266                 throw EncodeError (N_("could not create JPEG2000 encoder"));
267         }
268
269         /* Set event manager to null (openjpeg 1.3 bug) */
270         _cinfo->event_mgr = 0;
271
272         /* Setup the encoder parameters using the current image and user parameters */
273         opj_setup_encoder (_cinfo, _parameters, _opj_image);
274
275         _cio = opj_cio_open ((opj_common_ptr) _cinfo, 0, 0);
276         if (_cio == 0) {
277                 throw EncodeError (N_("could not open JPEG2000 stream"));
278         }
279
280         int const r = opj_encode (_cinfo, _cio, _opj_image, 0);
281         if (r == 0) {
282                 throw EncodeError (N_("JPEG2000 encoding failed"));
283         }
284
285         _log->log (String::compose (N_("Finished locally-encoded frame %1"), _frame));
286         
287         return shared_ptr<EncodedData> (new LocallyEncodedData (_cio->buffer, cio_tell (_cio)));
288 }
289
290 /** Send this frame to a remote server for J2K encoding, then read the result.
291  *  @param serv Server to send to.
292  *  @return Encoded data.
293  */
294 shared_ptr<EncodedData>
295 DCPVideoFrame::encode_remotely (ServerDescription const * serv)
296 {
297         boost::asio::io_service io_service;
298         boost::asio::ip::tcp::resolver resolver (io_service);
299         boost::asio::ip::tcp::resolver::query query (serv->host_name(), boost::lexical_cast<string> (Config::instance()->server_port ()));
300         boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
301
302         shared_ptr<Socket> socket (new Socket);
303
304         socket->connect (*endpoint_iterator);
305
306         stringstream s;
307         s << N_("encode please\n")
308           << N_("width ") << _image->size().width << N_("\n")
309           << N_("height ") << _image->size().height << N_("\n")
310           << N_("frame ") << _frame << N_("\n")
311           << N_("frames_per_second ") << _frames_per_second << N_("\n")
312           << N_("colour_lut ") << _colour_lut << N_("\n")
313           << N_("j2k_bandwidth ") << _j2k_bandwidth << N_("\n");
314
315         _log->log (String::compose (
316                            N_("Sending to remote; pixel format %1, components %2, lines (%3,%4,%5), line sizes (%6,%7,%8)"),
317                            _image->pixel_format(), _image->components(),
318                            _image->lines(0), _image->lines(1), _image->lines(2),
319                            _image->line_size()[0], _image->line_size()[1], _image->line_size()[2]
320                            ));
321
322         socket->write (s.str().length() + 1);
323         socket->write ((uint8_t *) s.str().c_str(), s.str().length() + 1);
324
325         _image->write_to_socket (socket);
326
327         shared_ptr<EncodedData> e (new RemotelyEncodedData (socket->read_uint32 ()));
328         socket->read (e->data(), e->size());
329
330         _log->log (String::compose (N_("Finished remotely-encoded frame %1"), _frame));
331         
332         return e;
333 }
334
335 EncodedData::EncodedData (int s)
336         : _data (new uint8_t[s])
337         , _size (s)
338 {
339
340 }
341
342 EncodedData::EncodedData (string file)
343 {
344         _size = boost::filesystem::file_size (file);
345         _data = new uint8_t[_size];
346
347         FILE* f = fopen (file.c_str(), N_("rb"));
348         if (!f) {
349                 throw FileError (_("could not open file for reading"), file);
350         }
351         
352         fread (_data, 1, _size, f);
353         fclose (f);
354 }
355
356
357 EncodedData::~EncodedData ()
358 {
359         delete[] _data;
360 }
361
362 /** Write this data to a J2K file.
363  *  @param Film Film.
364  *  @param frame DCP frame index.
365  */
366 void
367 EncodedData::write (shared_ptr<const Film> film, int frame) const
368 {
369         string const tmp_j2c = film->j2c_path (frame, true);
370
371         FILE* f = fopen (tmp_j2c.c_str (), N_("wb"));
372         
373         if (!f) {
374                 throw WriteFileError (tmp_j2c, errno);
375         }
376
377         fwrite (_data, 1, _size, f);
378         fclose (f);
379
380         string const real_j2c = film->j2c_path (frame, false);
381
382         /* Rename the file from foo.j2c.tmp to foo.j2c now that it is complete */
383         boost::filesystem::rename (tmp_j2c, real_j2c);
384 }
385
386 void
387 EncodedData::write_info (shared_ptr<const Film> film, int frame, libdcp::FrameInfo fin) const
388 {
389         string const info = film->info_path (frame);
390         ofstream h (info.c_str());
391         fin.write (h);
392 }
393
394 /** Send this data to a socket.
395  *  @param socket Socket
396  */
397 void
398 EncodedData::send (shared_ptr<Socket> socket)
399 {
400         socket->write (_size);
401         socket->write (_data, _size);
402 }
403
404 LocallyEncodedData::LocallyEncodedData (uint8_t* d, int s)
405         : EncodedData (s)
406 {
407         memcpy (_data, d, s);
408 }
409
410 /** @param s Size of data in bytes */
411 RemotelyEncodedData::RemotelyEncodedData (int s)
412         : EncodedData (s)
413 {
414
415 }