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