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