Cleanup: move convert_to_xyz().
[dcpomatic.git] / src / lib / player_video.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "content.h"
23 #include "film.h"
24 #include "image.h"
25 #include "image_proxy.h"
26 #include "j2k_image_proxy.h"
27 #include "player.h"
28 #include "player_video.h"
29 #include "video_content.h"
30 #include <dcp/openjpeg_image.h>
31 #include <dcp/raw_convert.h>
32 #include <dcp/rgb_xyz.h>
33 extern "C" {
34 #include <libavutil/pixfmt.h>
35 }
36 #include <libxml++/libxml++.h>
37 #include <iostream>
38
39
40 using std::cout;
41 using std::dynamic_pointer_cast;
42 using std::function;
43 using std::make_shared;
44 using std::shared_ptr;
45 using std::string;
46 using std::weak_ptr;
47 using boost::optional;
48 #if BOOST_VERSION >= 106100
49 using namespace boost::placeholders;
50 #endif
51 using dcp::Data;
52 using dcp::raw_convert;
53
54
55 PlayerVideo::PlayerVideo (
56         shared_ptr<const ImageProxy> in,
57         Crop crop,
58         boost::optional<double> fade,
59         dcp::Size inter_size,
60         dcp::Size out_size,
61         Eyes eyes,
62         Part part,
63         optional<ColourConversion> colour_conversion,
64         VideoRange video_range,
65         weak_ptr<Content> content,
66         optional<Frame> video_frame,
67         bool error
68         )
69         : _in (in)
70         , _crop (crop)
71         , _fade (fade)
72         , _inter_size (inter_size)
73         , _out_size (out_size)
74         , _eyes (eyes)
75         , _part (part)
76         , _colour_conversion (colour_conversion)
77         , _video_range (video_range)
78         , _content (content)
79         , _video_frame (video_frame)
80         , _error (error)
81 {
82
83 }
84
85
86 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
87 {
88         _crop = Crop (node);
89         _fade = node->optional_number_child<double> ("Fade");
90
91         _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
92         _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
93         _eyes = static_cast<Eyes>(node->number_child<int>("Eyes"));
94         _part = static_cast<Part>(node->number_child<int>("Part"));
95         _video_range = static_cast<VideoRange>(node->number_child<int>("VideoRange"));
96         _error = node->optional_bool_child("Error").get_value_or (false);
97
98         /* Assume that the ColourConversion uses the current state version */
99         _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
100
101         _in = image_proxy_factory (node->node_child("In"), socket);
102
103         if (node->optional_number_child<int>("SubtitleX")) {
104
105                 auto image = make_shared<Image> (
106                         AV_PIX_FMT_BGRA, dcp::Size(node->number_child<int>("SubtitleWidth"), node->number_child<int>("SubtitleHeight")), Image::Alignment::PADDED
107                         );
108
109                 image->read_from_socket (socket);
110
111                 _text = PositionImage (image, Position<int>(node->number_child<int>("SubtitleX"), node->number_child<int>("SubtitleY")));
112         }
113 }
114
115
116 void
117 PlayerVideo::set_text (PositionImage image)
118 {
119         _text = image;
120 }
121
122
123 shared_ptr<Image>
124 PlayerVideo::image (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool fast) const
125 {
126         /* XXX: this assumes that image() and prepare() are only ever called with the same parameters (except crop, inter size, out size, fade) */
127
128         boost::mutex::scoped_lock lm (_mutex);
129         if (!_image || _crop != _image_crop || _inter_size != _image_inter_size || _out_size != _image_out_size || _fade != _image_fade) {
130                 make_image (pixel_format, video_range, fast);
131         }
132         return _image;
133 }
134
135
136 shared_ptr<const Image>
137 PlayerVideo::raw_image () const
138 {
139         return _in->image(Image::Alignment::COMPACT, _inter_size).image;
140 }
141
142
143 /** Create an image for this frame.  A lock must be held on _mutex.
144  *  @param pixel_format Function which is called to decide what pixel format the output image should be;
145  *  it is passed the pixel format of the input image from the ImageProxy, and should return the desired
146  *  output pixel format.  Two functions force and keep_xyz_or_rgb are provided for use here.
147  *  @param fast true to be fast at the expense of quality.
148  */
149 void
150 PlayerVideo::make_image (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool fast) const
151 {
152         _image_crop = _crop;
153         _image_inter_size = _inter_size;
154         _image_out_size = _out_size;
155         _image_fade = _fade;
156
157         auto prox = _in->image (Image::Alignment::PADDED, _inter_size);
158         _error = prox.error;
159
160         auto total_crop = _crop;
161         switch (_part) {
162         case Part::LEFT_HALF:
163                 total_crop.right += prox.image->size().width / 2;
164                 break;
165         case Part::RIGHT_HALF:
166                 total_crop.left += prox.image->size().width / 2;
167                 break;
168         case Part::TOP_HALF:
169                 total_crop.bottom += prox.image->size().height / 2;
170                 break;
171         case Part::BOTTOM_HALF:
172                 total_crop.top += prox.image->size().height / 2;
173                 break;
174         default:
175                 break;
176         }
177
178         if (prox.log2_scaling > 0) {
179                 /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
180                 int const r = pow(2, prox.log2_scaling);
181                 total_crop.left /= r;
182                 total_crop.right /= r;
183                 total_crop.top /= r;
184                 total_crop.bottom /= r;
185         }
186
187         dcp::YUVToRGB yuv_to_rgb = dcp::YUVToRGB::REC601;
188         if (_colour_conversion) {
189                 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
190         }
191
192         _image = prox.image->crop_scale_window (
193                 total_crop, _inter_size, _out_size, yuv_to_rgb, _video_range, pixel_format (prox.image->pixel_format()), video_range, Image::Alignment::COMPACT, fast
194                 );
195
196         if (_text) {
197                 _image->alpha_blend (_text->image, _text->position);
198         }
199
200         if (_fade) {
201                 _image->fade (_fade.get ());
202         }
203 }
204
205
206 void
207 PlayerVideo::add_metadata (xmlpp::Node* node) const
208 {
209         _crop.as_xml (node);
210         if (_fade) {
211                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
212         }
213         _in->add_metadata (node->add_child ("In"));
214         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
215         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
216         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
217         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
218         node->add_child("Eyes")->add_child_text (raw_convert<string> (static_cast<int> (_eyes)));
219         node->add_child("Part")->add_child_text (raw_convert<string> (static_cast<int> (_part)));
220         node->add_child("VideoRange")->add_child_text(raw_convert<string>(static_cast<int>(_video_range)));
221         node->add_child("Error")->add_child_text(_error ? "1" : "0");
222         if (_colour_conversion) {
223                 _colour_conversion.get().as_xml (node);
224         }
225         if (_text) {
226                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_text->image->size().width));
227                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_text->image->size().height));
228                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_text->position.x));
229                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_text->position.y));
230         }
231 }
232
233
234 void
235 PlayerVideo::write_to_socket (shared_ptr<Socket> socket) const
236 {
237         _in->write_to_socket (socket);
238         if (_text) {
239                 _text->image->write_to_socket (socket);
240         }
241 }
242
243
244 bool
245 PlayerVideo::has_j2k () const
246 {
247         /* XXX: maybe other things */
248
249         auto j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
250         if (!j2k) {
251                 return false;
252         }
253
254         return _crop == Crop() && _out_size == j2k->size() && _inter_size == j2k->size() && !_text && !_fade && !_colour_conversion;
255 }
256
257
258 shared_ptr<const dcp::Data>
259 PlayerVideo::j2k () const
260 {
261         auto j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
262         DCPOMATIC_ASSERT (j2k);
263         return j2k->j2k ();
264 }
265
266
267 Position<int>
268 PlayerVideo::inter_position () const
269 {
270         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
271 }
272
273
274 /** @return true if this PlayerVideo is definitely the same as another, false if it is probably not */
275 bool
276 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
277 {
278         if (_crop != other->_crop ||
279             _fade != other->_fade ||
280             _inter_size != other->_inter_size ||
281             _out_size != other->_out_size ||
282             _eyes != other->_eyes ||
283             _part != other->_part ||
284             _colour_conversion != other->_colour_conversion ||
285             _video_range != other->_video_range) {
286                 return false;
287         }
288
289         if ((!_text && other->_text) || (_text && !other->_text)) {
290                 /* One has a text and the other doesn't */
291                 return false;
292         }
293
294         if (_text && other->_text && !_text->same (other->_text.get ())) {
295                 /* They both have texts but they are different */
296                 return false;
297         }
298
299         /* Now neither has subtitles */
300
301         return _in->same (other->_in);
302 }
303
304
305 AVPixelFormat
306 PlayerVideo::force (AVPixelFormat force_to)
307 {
308         return force_to;
309 }
310
311 AVPixelFormat
312 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
313 {
314         return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
315 }
316
317
318 void
319 PlayerVideo::prepare (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, Image::Alignment alignment, bool fast, bool proxy_only)
320 {
321         _in->prepare (alignment, _inter_size);
322         boost::mutex::scoped_lock lm (_mutex);
323         if (!_image && !proxy_only) {
324                 make_image (pixel_format, video_range, fast);
325         }
326 }
327
328
329 size_t
330 PlayerVideo::memory_used () const
331 {
332         return _in->memory_used();
333 }
334
335
336 /** @return Shallow copy of this; _in and _text are shared between the original and the copy */
337 shared_ptr<PlayerVideo>
338 PlayerVideo::shallow_copy () const
339 {
340         return std::make_shared<PlayerVideo>(
341                 _in,
342                 _crop,
343                 _fade,
344                 _inter_size,
345                 _out_size,
346                 _eyes,
347                 _part,
348                 _colour_conversion,
349                 _video_range,
350                 _content,
351                 _video_frame,
352                 _error
353                 );
354 }
355
356
357 /** Re-read crop, fade, inter/out size, colour conversion and video range from our content.
358  *  @return true if this was possible, false if not.
359  */
360 bool
361 PlayerVideo::reset_metadata (shared_ptr<const Film> film, dcp::Size player_video_container_size)
362 {
363         auto content = _content.lock();
364         if (!content || !_video_frame) {
365                 return false;
366         }
367
368         _crop = content->video->actual_crop();
369         _fade = content->video->fade(film, _video_frame.get());
370         _inter_size = scale_for_display(
371                 content->video->scaled_size(film->frame_size()),
372                 player_video_container_size,
373                 film->frame_size(),
374                 content->video->pixel_quanta()
375                 );
376         _out_size = player_video_container_size;
377         _colour_conversion = content->video->colour_conversion();
378         _video_range = content->video->range();
379
380         return true;
381 }
382
383
384 shared_ptr<dcp::OpenJPEGImage>
385 PlayerVideo::convert_to_xyz(dcp::NoteHandler note) const
386 {
387         auto frame_image = image(bind(&PlayerVideo::keep_xyz_or_rgb, _1), VideoRange::FULL, false);
388         if (colour_conversion()) {
389                 return dcp::rgb_to_xyz (
390                         frame_image->data()[0],
391                         frame_image->size(),
392                         frame_image->stride()[0],
393                         colour_conversion().get(),
394                         note
395                         );
396         } else {
397                 return make_shared<dcp::OpenJPEGImage>(frame_image->data()[0], frame_image->size(), frame_image->stride()[0]);
398         }
399 }
400