Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / player_video.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "player_video.h"
22 #include "content.h"
23 #include "video_content.h"
24 #include "image.h"
25 #include "image_proxy.h"
26 #include "j2k_image_proxy.h"
27 #include "film.h"
28 #include <dcp/raw_convert.h>
29 extern "C" {
30 #include <libavutil/pixfmt.h>
31 }
32 #include <libxml++/libxml++.h>
33 #include <iostream>
34
35 using std::string;
36 using std::cout;
37 using std::pair;
38 using boost::shared_ptr;
39 using boost::weak_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42 using boost::function;
43 using dcp::Data;
44 using dcp::raw_convert;
45
46 PlayerVideo::PlayerVideo (
47         shared_ptr<const ImageProxy> in,
48         Crop crop,
49         boost::optional<double> fade,
50         dcp::Size inter_size,
51         dcp::Size out_size,
52         Eyes eyes,
53         Part part,
54         optional<ColourConversion> colour_conversion,
55         VideoRange video_range,
56         weak_ptr<Content> content,
57         optional<Frame> video_frame,
58         bool error
59         )
60         : _in (in)
61         , _crop (crop)
62         , _fade (fade)
63         , _inter_size (inter_size)
64         , _out_size (out_size)
65         , _eyes (eyes)
66         , _part (part)
67         , _colour_conversion (colour_conversion)
68         , _video_range (video_range)
69         , _content (content)
70         , _video_frame (video_frame)
71         , _error (error)
72 {
73
74 }
75
76 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
77 {
78         _crop = Crop (node);
79         _fade = node->optional_number_child<double> ("Fade");
80
81         _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
82         _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
83         _eyes = (Eyes) node->number_child<int> ("Eyes");
84         _part = (Part) node->number_child<int> ("Part");
85         _video_range = (VideoRange) node->number_child<int>("VideoRange");
86         _error = node->optional_bool_child("Error").get_value_or (false);
87
88         /* Assume that the ColourConversion uses the current state version */
89         _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
90
91         _in = image_proxy_factory (node->node_child ("In"), socket);
92
93         if (node->optional_number_child<int> ("SubtitleX")) {
94
95                 shared_ptr<Image> image (
96                         new Image (AV_PIX_FMT_BGRA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
97                         );
98
99                 image->read_from_socket (socket);
100
101                 _text = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
102         }
103 }
104
105 void
106 PlayerVideo::set_text (PositionImage image)
107 {
108         _text = image;
109 }
110
111 shared_ptr<Image>
112 PlayerVideo::image (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
113 {
114         /* XXX: this assumes that image() and prepare() are only ever called with the same parameters (except crop, inter size, out size, fade) */
115
116         boost::mutex::scoped_lock lm (_mutex);
117         if (!_image || _crop != _image_crop || _inter_size != _image_inter_size || _out_size != _image_out_size || _fade != _image_fade) {
118                 make_image (pixel_format, aligned, fast);
119         }
120         return _image;
121 }
122
123 /** Create an image for this frame.  A lock must be held on _mutex.
124  *  @param pixel_format Function which is called to decide what pixel format the output image should be;
125  *  it is passed the pixel format of the input image from the ImageProxy, and should return the desired
126  *  output pixel format.  Two functions force and keep_xyz_or_rgb are provided for use here.
127  *  @param aligned true if the output image should be aligned to 32-byte boundaries.
128  *  @param fast true to be fast at the expense of quality.
129  */
130 void
131 PlayerVideo::make_image (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
132 {
133         _image_crop = _crop;
134         _image_inter_size = _inter_size;
135         _image_out_size = _out_size;
136         _image_fade = _fade;
137
138         ImageProxy::Result prox = _in->image (_inter_size);
139         _error = prox.error;
140
141         Crop total_crop = _crop;
142         switch (_part) {
143         case PART_LEFT_HALF:
144                 total_crop.right += prox.image->size().width / 2;
145                 break;
146         case PART_RIGHT_HALF:
147                 total_crop.left += prox.image->size().width / 2;
148                 break;
149         case PART_TOP_HALF:
150                 total_crop.bottom += prox.image->size().height / 2;
151                 break;
152         case PART_BOTTOM_HALF:
153                 total_crop.top += prox.image->size().height / 2;
154                 break;
155         default:
156                 break;
157         }
158
159         if (prox.log2_scaling > 0) {
160                 /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
161                 int const r = pow(2, prox.log2_scaling);
162                 total_crop.left /= r;
163                 total_crop.right /= r;
164                 total_crop.top /= r;
165                 total_crop.bottom /= r;
166         }
167
168         dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
169         if (_colour_conversion) {
170                 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
171         }
172
173         _image = prox.image->crop_scale_window (
174                 total_crop, _inter_size, _out_size, yuv_to_rgb, _video_range, pixel_format (prox.image->pixel_format()), aligned, fast
175                 );
176
177         if (_text) {
178                 _image->alpha_blend (Image::ensure_aligned (_text->image), _text->position);
179         }
180
181         if (_fade) {
182                 _image->fade (_fade.get ());
183         }
184 }
185
186 void
187 PlayerVideo::add_metadata (xmlpp::Node* node) const
188 {
189         _crop.as_xml (node);
190         if (_fade) {
191                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
192         }
193         _in->add_metadata (node->add_child ("In"));
194         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
195         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
196         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
197         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
198         node->add_child("Eyes")->add_child_text (raw_convert<string> (static_cast<int> (_eyes)));
199         node->add_child("Part")->add_child_text (raw_convert<string> (static_cast<int> (_part)));
200         node->add_child("VideoRange")->add_child_text(raw_convert<string>(static_cast<int>(_video_range)));
201         node->add_child("Error")->add_child_text(_error ? "1" : "0");
202         if (_colour_conversion) {
203                 _colour_conversion.get().as_xml (node);
204         }
205         if (_text) {
206                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_text->image->size().width));
207                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_text->image->size().height));
208                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_text->position.x));
209                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_text->position.y));
210         }
211 }
212
213 void
214 PlayerVideo::write_to_socket (shared_ptr<Socket> socket) const
215 {
216         _in->write_to_socket (socket);
217         if (_text) {
218                 _text->image->write_to_socket (socket);
219         }
220 }
221
222 bool
223 PlayerVideo::has_j2k () const
224 {
225         /* XXX: maybe other things */
226
227         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
228         if (!j2k) {
229                 return false;
230         }
231
232         return _crop == Crop () && _out_size == j2k->size() && !_text && !_fade && !_colour_conversion;
233 }
234
235 Data
236 PlayerVideo::j2k () const
237 {
238         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
239         DCPOMATIC_ASSERT (j2k);
240         return j2k->j2k ();
241 }
242
243 Position<int>
244 PlayerVideo::inter_position () const
245 {
246         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
247 }
248
249 /** @return true if this PlayerVideo is definitely the same as another, false if it is probably not */
250 bool
251 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
252 {
253         if (_crop != other->_crop ||
254             _fade != other->_fade ||
255             _inter_size != other->_inter_size ||
256             _out_size != other->_out_size ||
257             _eyes != other->_eyes ||
258             _part != other->_part ||
259             _colour_conversion != other->_colour_conversion ||
260             _video_range != other->_video_range) {
261                 return false;
262         }
263
264         if ((!_text && other->_text) || (_text && !other->_text)) {
265                 /* One has a text and the other doesn't */
266                 return false;
267         }
268
269         if (_text && other->_text && !_text->same (other->_text.get ())) {
270                 /* They both have texts but they are different */
271                 return false;
272         }
273
274         /* Now neither has subtitles */
275
276         return _in->same (other->_in);
277 }
278
279 AVPixelFormat
280 PlayerVideo::force (AVPixelFormat, AVPixelFormat force_to)
281 {
282         return force_to;
283 }
284
285 AVPixelFormat
286 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
287 {
288         return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
289 }
290
291 void
292 PlayerVideo::prepare (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast)
293 {
294         _in->prepare (_inter_size);
295         boost::mutex::scoped_lock lm (_mutex);
296         if (!_image) {
297                 make_image (pixel_format, aligned, fast);
298         }
299 }
300
301 size_t
302 PlayerVideo::memory_used () const
303 {
304         return _in->memory_used();
305 }
306
307 /** @return Shallow copy of this; _in and _text are shared between the original and the copy */
308 shared_ptr<PlayerVideo>
309 PlayerVideo::shallow_copy () const
310 {
311         return shared_ptr<PlayerVideo>(
312                 new PlayerVideo(
313                         _in,
314                         _crop,
315                         _fade,
316                         _inter_size,
317                         _out_size,
318                         _eyes,
319                         _part,
320                         _colour_conversion,
321                         _video_range,
322                         _content,
323                         _video_frame,
324                         _error
325                         )
326                 );
327 }
328
329 /** Re-read crop, fade, inter/out size, colour conversion and video range from our content.
330  *  @return true if this was possible, false if not.
331  */
332 bool
333 PlayerVideo::reset_metadata (shared_ptr<const Film> film, dcp::Size video_container_size, dcp::Size film_frame_size)
334 {
335         shared_ptr<Content> content = _content.lock();
336         if (!content || !_video_frame) {
337                 return false;
338         }
339
340         _crop = content->video->crop();
341         _fade = content->video->fade(film, _video_frame.get());
342         _inter_size = content->video->scale().size(content->video, video_container_size, film_frame_size);
343         _out_size = video_container_size;
344         _colour_conversion = content->video->colour_conversion();
345         _video_range = content->video->range();
346
347         return true;
348 }