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