Try to fix up paths for video MXFs, hashes and temporarily-stored frames.
[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 "film.h"
47 #include "dcp_video_frame.h"
48 #include "lut.h"
49 #include "config.h"
50 #include "options.h"
51 #include "exceptions.h"
52 #include "server.h"
53 #include "util.h"
54 #include "scaler.h"
55 #include "image.h"
56 #include "log.h"
57 #include "subtitle.h"
58
59 using std::string;
60 using std::stringstream;
61 using std::ofstream;
62 using std::cout;
63 using boost::shared_ptr;
64 using libdcp::Size;
65
66 /** Construct a DCP video frame.
67  *  @param input Input image.
68  *  @param out Required size of output, in pixels (including any padding).
69  *  @param s Scaler to use.
70  *  @param p Number of pixels of padding either side of the image.
71  *  @param f Index of the frame within the DCP's intrinsic duration.
72  *  @param fps Frames per second of the Film's source.
73  *  @param pp FFmpeg post-processing string to use.
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> yuv, shared_ptr<Subtitle> sub,
80         Size out, int p, int subtitle_offset, float subtitle_scale,
81         Scaler const * s, int f, float fps, string pp, int clut, int bw, Log* l
82         )
83         : _input (yuv)
84         , _subtitle (sub)
85         , _out_size (out)
86         , _padding (p)
87         , _subtitle_offset (subtitle_offset)
88         , _subtitle_scale (subtitle_scale)
89         , _scaler (s)
90         , _frame (f)
91         , _frames_per_second (DCPFrameRate(fps).frames_per_second)
92         , _post_process (pp)
93         , _colour_lut (clut)
94         , _j2k_bandwidth (bw)
95         , _log (l)
96         , _image (0)
97         , _parameters (0)
98         , _cinfo (0)
99         , _cio (0)
100 {
101         
102 }
103
104 /** Create a libopenjpeg container suitable for our output image */
105 void
106 DCPVideoFrame::create_openjpeg_container ()
107 {
108         for (int i = 0; i < 3; ++i) {
109                 _cmptparm[i].dx = 1;
110                 _cmptparm[i].dy = 1;
111                 _cmptparm[i].w = _out_size.width;
112                 _cmptparm[i].h = _out_size.height;
113                 _cmptparm[i].x0 = 0;
114                 _cmptparm[i].y0 = 0;
115                 _cmptparm[i].prec = 12;
116                 _cmptparm[i].bpp = 12;
117                 _cmptparm[i].sgnd = 0;
118         }
119
120         _image = opj_image_create (3, &_cmptparm[0], CLRSPC_SRGB);
121         if (_image == 0) {
122                 throw EncodeError ("could not create libopenjpeg image");
123         }
124
125         _image->x0 = 0;
126         _image->y0 = 0;
127         _image->x1 = _out_size.width;
128         _image->y1 = _out_size.height;
129 }
130
131 DCPVideoFrame::~DCPVideoFrame ()
132 {
133         if (_image) {
134                 opj_image_destroy (_image);
135         }
136
137         if (_cio) {
138                 opj_cio_close (_cio);
139         }
140
141         if (_cinfo) {
142                 opj_destroy_compress (_cinfo);
143         }
144
145         if (_parameters) {
146                 free (_parameters->cp_comment);
147         }
148         
149         delete _parameters;
150 }
151
152 /** J2K-encode this frame on the local host.
153  *  @return Encoded data.
154  */
155 shared_ptr<EncodedData>
156 DCPVideoFrame::encode_locally ()
157 {
158         if (!_post_process.empty ()) {
159                 _input = _input->post_process (_post_process, true);
160         }
161         
162         shared_ptr<Image> prepared = _input->scale_and_convert_to_rgb (_out_size, _padding, _scaler, true);
163
164         if (_subtitle) {
165                 Rect tx = subtitle_transformed_area (
166                         float (_out_size.width) / _input->size().width,
167                         float (_out_size.height) / _input->size().height,
168                         _subtitle->area(), _subtitle_offset, _subtitle_scale
169                         );
170
171                 shared_ptr<Image> im = _subtitle->image()->scale (tx.size(), _scaler, true);
172                 prepared->alpha_blend (im, tx.position());
173         }
174
175         create_openjpeg_container ();
176
177         struct {
178                 double r, g, b;
179         } s;
180
181         struct {
182                 double x, y, z;
183         } d;
184
185         /* Copy our RGB into the openjpeg container, converting to XYZ in the process */
186
187         int jn = 0;
188         for (int y = 0; y < _out_size.height; ++y) {
189                 uint8_t* p = prepared->data()[0] + y * prepared->stride()[0];
190                 for (int x = 0; x < _out_size.width; ++x) {
191
192                         /* In gamma LUT (converting 8-bit input to 12-bit) */
193                         s.r = lut_in[_colour_lut][*p++ << 4];
194                         s.g = lut_in[_colour_lut][*p++ << 4];
195                         s.b = lut_in[_colour_lut][*p++ << 4];
196                         
197                         /* RGB to XYZ Matrix */
198                         d.x = ((s.r * color_matrix[_colour_lut][0][0]) +
199                                (s.g * color_matrix[_colour_lut][0][1]) +
200                                (s.b * color_matrix[_colour_lut][0][2]));
201                         
202                         d.y = ((s.r * color_matrix[_colour_lut][1][0]) +
203                                (s.g * color_matrix[_colour_lut][1][1]) +
204                                (s.b * color_matrix[_colour_lut][1][2]));
205                         
206                         d.z = ((s.r * color_matrix[_colour_lut][2][0]) +
207                                (s.g * color_matrix[_colour_lut][2][1]) +
208                                (s.b * color_matrix[_colour_lut][2][2]));
209                         
210                         /* DCI companding */
211                         d.x = d.x * DCI_COEFFICENT * (DCI_LUT_SIZE - 1);
212                         d.y = d.y * DCI_COEFFICENT * (DCI_LUT_SIZE - 1);
213                         d.z = d.z * DCI_COEFFICENT * (DCI_LUT_SIZE - 1);
214                         
215                         /* Out gamma LUT */
216                         _image->comps[0].data[jn] = lut_out[LO_DCI][(int) d.x];
217                         _image->comps[1].data[jn] = lut_out[LO_DCI][(int) d.y];
218                         _image->comps[2].data[jn] = lut_out[LO_DCI][(int) d.z];
219
220                         ++jn;
221                 }
222         }
223
224         /* Set the max image and component sizes based on frame_rate */
225         int const max_cs_len = ((float) _j2k_bandwidth) / 8 / _frames_per_second;
226         int const max_comp_size = max_cs_len / 1.25;
227
228         /* Set encoding parameters to default values */
229         _parameters = new opj_cparameters_t;
230         opj_set_default_encoder_parameters (_parameters);
231
232         /* Set default cinema parameters */
233         _parameters->tile_size_on = false;
234         _parameters->cp_tdx = 1;
235         _parameters->cp_tdy = 1;
236         
237         /* Tile part */
238         _parameters->tp_flag = 'C';
239         _parameters->tp_on = 1;
240         
241         /* Tile and Image shall be at (0,0) */
242         _parameters->cp_tx0 = 0;
243         _parameters->cp_ty0 = 0;
244         _parameters->image_offset_x0 = 0;
245         _parameters->image_offset_y0 = 0;
246
247         /* Codeblock size = 32x32 */
248         _parameters->cblockw_init = 32;
249         _parameters->cblockh_init = 32;
250         _parameters->csty |= 0x01;
251         
252         /* The progression order shall be CPRL */
253         _parameters->prog_order = CPRL;
254         
255         /* No ROI */
256         _parameters->roi_compno = -1;
257         
258         _parameters->subsampling_dx = 1;
259         _parameters->subsampling_dy = 1;
260         
261         /* 9-7 transform */
262         _parameters->irreversible = 1;
263         
264         _parameters->tcp_rates[0] = 0;
265         _parameters->tcp_numlayers++;
266         _parameters->cp_disto_alloc = 1;
267         _parameters->cp_rsiz = CINEMA2K;
268         _parameters->cp_comment = strdup ("DVD-o-matic");
269         _parameters->cp_cinema = CINEMA2K_24;
270
271         /* 3 components, so use MCT */
272         _parameters->tcp_mct = 1;
273         
274         /* set max image */
275         _parameters->max_comp_size = max_comp_size;
276         _parameters->tcp_rates[0] = ((float) (3 * _image->comps[0].w * _image->comps[0].h * _image->comps[0].prec)) / (max_cs_len * 8);
277
278         /* get a J2K compressor handle */
279         _cinfo = opj_create_compress (CODEC_J2K);
280         if (_cinfo == 0) {
281                 throw EncodeError ("could not create JPEG2000 encoder");
282         }
283
284         /* Set event manager to null (openjpeg 1.3 bug) */
285         _cinfo->event_mgr = 0;
286
287         /* Setup the encoder parameters using the current image and user parameters */
288         opj_setup_encoder (_cinfo, _parameters, _image);
289
290         _cio = opj_cio_open ((opj_common_ptr) _cinfo, 0, 0);
291         if (_cio == 0) {
292                 throw EncodeError ("could not open JPEG2000 stream");
293         }
294
295         int const r = opj_encode (_cinfo, _cio, _image, 0);
296         if (r == 0) {
297                 throw EncodeError ("JPEG2000 encoding failed");
298         }
299
300         _log->log (String::compose ("Finished locally-encoded frame %1", _frame));
301         
302         return shared_ptr<EncodedData> (new LocallyEncodedData (_cio->buffer, cio_tell (_cio)));
303 }
304
305 /** Send this frame to a remote server for J2K encoding, then read the result.
306  *  @param serv Server to send to.
307  *  @return Encoded data.
308  */
309 shared_ptr<EncodedData>
310 DCPVideoFrame::encode_remotely (ServerDescription const * serv)
311 {
312         boost::asio::io_service io_service;
313         boost::asio::ip::tcp::resolver resolver (io_service);
314         boost::asio::ip::tcp::resolver::query query (serv->host_name(), boost::lexical_cast<string> (Config::instance()->server_port ()));
315         boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
316
317         shared_ptr<Socket> socket (new Socket);
318
319         socket->connect (*endpoint_iterator, 30);
320
321         stringstream s;
322         s << "encode please\n"
323           << "input_width " << _input->size().width << "\n"
324           << "input_height " << _input->size().height << "\n"
325           << "input_pixel_format " << _input->pixel_format() << "\n"
326           << "output_width " << _out_size.width << "\n"
327           << "output_height " << _out_size.height << "\n"
328           << "padding " <<  _padding << "\n"
329           << "subtitle_offset " << _subtitle_offset << "\n"
330           << "subtitle_scale " << _subtitle_scale << "\n"
331           << "scaler " << _scaler->id () << "\n"
332           << "frame " << _frame << "\n"
333           << "frames_per_second " << _frames_per_second << "\n";
334
335         if (!_post_process.empty()) {
336                 s << "post_process " << _post_process << "\n";
337         }
338         
339         s << "colour_lut " << _colour_lut << "\n"
340           << "j2k_bandwidth " << _j2k_bandwidth << "\n";
341
342         if (_subtitle) {
343                 s << "subtitle_x " << _subtitle->position().x << "\n"
344                   << "subtitle_y " << _subtitle->position().y << "\n"
345                   << "subtitle_width " << _subtitle->image()->size().width << "\n"
346                   << "subtitle_height " << _subtitle->image()->size().height << "\n";
347         }
348
349         _log->log (String::compose (
350                            "Sending to remote; pixel format %1, components %2, lines (%3,%4,%5), line sizes (%6,%7,%8)",
351                            _input->pixel_format(), _input->components(),
352                            _input->lines(0), _input->lines(1), _input->lines(2),
353                            _input->line_size()[0], _input->line_size()[1], _input->line_size()[2]
354                            ));
355         
356         socket->write ((uint8_t *) s.str().c_str(), s.str().length() + 1, 30);
357
358         _input->write_to_socket (socket);
359         if (_subtitle) {
360                 _subtitle->image()->write_to_socket (socket);
361         }
362
363         char buffer[32];
364         socket->read_indefinite ((uint8_t *) buffer, sizeof (buffer), 30);
365         socket->consume (strlen (buffer) + 1);
366         shared_ptr<EncodedData> e (new RemotelyEncodedData (atoi (buffer)));
367
368         /* now read the rest */
369         socket->read_definite_and_consume (e->data(), e->size(), 30);
370
371         _log->log (String::compose ("Finished remotely-encoded frame %1", _frame));
372         
373         return e;
374 }
375
376 EncodedData::EncodedData (int s)
377         : _data (new uint8_t[s])
378         , _size (s)
379 {
380
381 }
382
383 EncodedData::EncodedData (string file)
384 {
385         _size = boost::filesystem::file_size (file);
386         _data = new uint8_t[_size];
387
388         FILE* f = fopen (file.c_str(), "rb");
389         if (!f) {
390                 throw FileError ("could not open file for reading", file);
391         }
392         
393         fread (_data, 1, _size, f);
394         fclose (f);
395 }
396
397
398 EncodedData::~EncodedData ()
399 {
400         delete[] _data;
401 }
402
403 /** Write this data to a J2K file.
404  *  @param Film Film.
405  *  @param frame DCP frame index.
406  */
407 void
408 EncodedData::write (shared_ptr<const Film> film, int frame) const
409 {
410         string const tmp_j2c = film->j2c_path (frame, true);
411
412         FILE* f = fopen (tmp_j2c.c_str (), "wb");
413         
414         if (!f) {
415                 throw WriteFileError (tmp_j2c, errno);
416         }
417
418         fwrite (_data, 1, _size, f);
419         fclose (f);
420
421         string const real_j2c = film->j2c_path (frame, false);
422
423         /* Rename the file from foo.j2c.tmp to foo.j2c now that it is complete */
424         boost::filesystem::rename (tmp_j2c, real_j2c);
425 }
426
427 void
428 EncodedData::write_hash (shared_ptr<const Film> film, int frame) const
429 {
430         string const hash = film->hash_path (frame);
431         ofstream h (hash.c_str());
432         h << md5_digest (_data, _size) << "\n";
433 }
434
435 /** Send this data to a socket.
436  *  @param socket Socket
437  */
438 void
439 EncodedData::send (shared_ptr<Socket> socket)
440 {
441         stringstream s;
442         s << _size;
443         socket->write ((uint8_t *) s.str().c_str(), s.str().length() + 1, 30);
444         socket->write (_data, _size, 30);
445 }
446
447 LocallyEncodedData::LocallyEncodedData (uint8_t* d, int s)
448         : EncodedData (s)
449 {
450         memcpy (_data, d, s);
451 }
452
453 /** @param s Size of data in bytes */
454 RemotelyEncodedData::RemotelyEncodedData (int s)
455         : EncodedData (s)
456 {
457
458 }