fbe863a8eaa2e574b5840916237218c6df48c2ea
[dcpomatic.git] / src / lib / dcp_video.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/dcp_video_frame.cc
21  *  @brief A single frame of video destined for a DCP.
22  *
23  *  Given an Image and some settings, this class knows how to encode
24  *  the image to J2K either on the local host or on a remote server.
25  *
26  *  Objects of this class are used for the queue that we keep
27  *  of images that require encoding.
28  */
29
30 #include "dcp_video.h"
31 #include "config.h"
32 #include "exceptions.h"
33 #include "encode_server_description.h"
34 #include "dcpomatic_socket.h"
35 #include "image.h"
36 #include "log.h"
37 #include "cross.h"
38 #include "player_video.h"
39 #include "raw_convert.h"
40 #include "compose.hpp"
41 #include <libcxml/cxml.h>
42 #include <dcp/openjpeg_image.h>
43 #include <dcp/rgb_xyz.h>
44 #include <dcp/j2k.h>
45 #include <dcp/colour_matrix.h>
46 #include <libxml++/libxml++.h>
47 #include <boost/asio.hpp>
48 #include <boost/thread.hpp>
49 #include <stdint.h>
50 #include <iomanip>
51 #include <iostream>
52
53 #define LOG_GENERAL(...) _log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
54 #define LOG_DEBUG_ENCODE(...) _log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_DEBUG_ENCODE);
55 #define LOG_TIMING(...) _log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_TIMING);
56
57 #include "i18n.h"
58
59 using std::string;
60 using std::cout;
61 using boost::shared_ptr;
62 using dcp::Size;
63 using dcp::Data;
64
65 #define DCI_COEFFICENT (48.0 / 52.37)
66
67 /** Construct a DCP video frame.
68  *  @param frame Input frame.
69  *  @param index Index of the frame within the DCP.
70  *  @param bw J2K bandwidth to use (see Config::j2k_bandwidth ())
71  *  @param l Log to write to.
72  */
73 DCPVideo::DCPVideo (
74         shared_ptr<const PlayerVideo> frame, int index, int dcp_fps, int bw, Resolution r, shared_ptr<Log> l
75         )
76         : _frame (frame)
77         , _index (index)
78         , _frames_per_second (dcp_fps)
79         , _j2k_bandwidth (bw)
80         , _resolution (r)
81         , _log (l)
82 {
83
84 }
85
86 DCPVideo::DCPVideo (shared_ptr<const PlayerVideo> frame, shared_ptr<const cxml::Node> node, shared_ptr<Log> log)
87         : _frame (frame)
88         , _log (log)
89 {
90         _index = node->number_child<int> ("Index");
91         _frames_per_second = node->number_child<int> ("FramesPerSecond");
92         _j2k_bandwidth = node->number_child<int> ("J2KBandwidth");
93         _resolution = Resolution (node->optional_number_child<int>("Resolution").get_value_or (RESOLUTION_2K));
94 }
95
96 shared_ptr<dcp::OpenJPEGImage>
97 DCPVideo::convert_to_xyz (shared_ptr<const PlayerVideo> frame, dcp::NoteHandler note)
98 {
99         shared_ptr<dcp::OpenJPEGImage> xyz;
100
101         shared_ptr<Image> image = frame->image (note, bind (&PlayerVideo::keep_xyz_or_rgb, _1), true, false);
102         if (frame->colour_conversion()) {
103                 xyz = dcp::rgb_to_xyz (
104                         image->data()[0],
105                         image->size(),
106                         image->stride()[0],
107                         frame->colour_conversion().get(),
108                         note
109                         );
110         } else {
111                 xyz = dcp::xyz_to_xyz (image->data()[0], image->size(), image->stride()[0]);
112         }
113
114         return xyz;
115 }
116
117 /** J2K-encode this frame on the local host.
118  *  @return Encoded data.
119  */
120 Data
121 DCPVideo::encode_locally (dcp::NoteHandler note)
122 {
123         Data enc = compress_j2k (
124                 convert_to_xyz (_frame, note),
125                 _j2k_bandwidth,
126                 _frames_per_second,
127                 _frame->eyes() == EYES_LEFT || _frame->eyes() == EYES_RIGHT,
128                 _resolution == RESOLUTION_4K
129                 );
130
131         switch (_frame->eyes()) {
132         case EYES_BOTH:
133                 LOG_DEBUG_ENCODE (N_("Finished locally-encoded frame %1 for mono"), _index);
134                 break;
135         case EYES_LEFT:
136                 LOG_DEBUG_ENCODE (N_("Finished locally-encoded frame %1 for L"), _index);
137                 break;
138         case EYES_RIGHT:
139                 LOG_DEBUG_ENCODE (N_("Finished locally-encoded frame %1 for R"), _index);
140                 break;
141         default:
142                 break;
143         }
144
145         return enc;
146 }
147
148 /** Send this frame to a remote server for J2K encoding, then read the result.
149  *  @param serv Server to send to.
150  *  @return Encoded data.
151  */
152 Data
153 DCPVideo::encode_remotely (EncodeServerDescription serv, int timeout)
154 {
155         boost::asio::io_service io_service;
156         boost::asio::ip::tcp::resolver resolver (io_service);
157         boost::asio::ip::tcp::resolver::query query (serv.host_name(), raw_convert<string> (Config::instance()->server_port_base ()));
158         boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
159
160         shared_ptr<Socket> socket (new Socket (timeout));
161
162         socket->connect (*endpoint_iterator);
163
164         /* Collect all XML metadata */
165         xmlpp::Document doc;
166         xmlpp::Element* root = doc.create_root_node ("EncodingRequest");
167         root->add_child("Version")->add_child_text (raw_convert<string> (SERVER_LINK_VERSION));
168         add_metadata (root);
169
170         LOG_DEBUG_ENCODE (N_("Sending frame %1 to remote"), _index);
171
172         /* Send XML metadata */
173         string xml = doc.write_to_string ("UTF-8");
174         socket->write (xml.length() + 1);
175         socket->write ((uint8_t *) xml.c_str(), xml.length() + 1);
176
177         /* Send binary data */
178         LOG_TIMING("start-remote-send thread=%1", boost::this_thread::get_id());
179         _frame->send_binary (socket);
180
181         /* Read the response (JPEG2000-encoded data); this blocks until the data
182            is ready and sent back.
183         */
184         LOG_TIMING("start-remote-encode thread=%1", boost::this_thread::get_id ());
185         Data e (socket->read_uint32 ());
186         LOG_TIMING("start-remote-receive thread=%1", boost::this_thread::get_id ());
187         socket->read (e.data().get(), e.size());
188         LOG_TIMING("finish-remote-receive thread=%1", boost::this_thread::get_id ());
189
190         LOG_DEBUG_ENCODE (N_("Finished remotely-encoded frame %1"), _index);
191
192         return e;
193 }
194
195 void
196 DCPVideo::add_metadata (xmlpp::Element* el) const
197 {
198         el->add_child("Index")->add_child_text (raw_convert<string> (_index));
199         el->add_child("FramesPerSecond")->add_child_text (raw_convert<string> (_frames_per_second));
200         el->add_child("J2KBandwidth")->add_child_text (raw_convert<string> (_j2k_bandwidth));
201         el->add_child("Resolution")->add_child_text (raw_convert<string> (int (_resolution)));
202         _frame->add_metadata (el);
203 }
204
205 Eyes
206 DCPVideo::eyes () const
207 {
208         return _frame->eyes ();
209 }
210
211 /** @return true if this DCPVideo is definitely the same as another;
212  *  (apart from the frame index), false if it is probably not.
213  */
214 bool
215 DCPVideo::same (shared_ptr<const DCPVideo> other) const
216 {
217         if (_frames_per_second != other->_frames_per_second ||
218             _j2k_bandwidth != other->_j2k_bandwidth ||
219             _resolution != other->_resolution) {
220                 return false;
221         }
222
223         return _frame->same (other->_frame);
224 }