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