2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
25 #include "image_proxy.h"
26 #include "j2k_image_proxy.h"
28 #include "player_video.h"
29 #include "video_content.h"
30 #include <dcp/raw_convert.h>
32 #include <libavutil/pixfmt.h>
34 #include <libxml++/libxml++.h>
39 using std::dynamic_pointer_cast;
41 using std::make_shared;
42 using std::shared_ptr;
45 using boost::optional;
47 using dcp::raw_convert;
50 PlayerVideo::PlayerVideo (
51 shared_ptr<const ImageProxy> in,
53 boost::optional<double> fade,
58 optional<ColourConversion> colour_conversion,
59 VideoRange video_range,
60 weak_ptr<Content> content,
61 optional<Frame> video_frame,
67 , _inter_size (inter_size)
68 , _out_size (out_size)
71 , _colour_conversion (colour_conversion)
72 , _video_range (video_range)
74 , _video_frame (video_frame)
81 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
84 _fade = node->optional_number_child<double> ("Fade");
86 _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
87 _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
88 _eyes = static_cast<Eyes>(node->number_child<int>("Eyes"));
89 _part = static_cast<Part>(node->number_child<int>("Part"));
90 _video_range = static_cast<VideoRange>(node->number_child<int>("VideoRange"));
91 _error = node->optional_bool_child("Error").get_value_or (false);
93 /* Assume that the ColourConversion uses the current state version */
94 _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
96 _in = image_proxy_factory (node->node_child("In"), socket);
98 if (node->optional_number_child<int>("SubtitleX")) {
100 auto image = make_shared<Image> (
101 AV_PIX_FMT_BGRA, dcp::Size(node->number_child<int>("SubtitleWidth"), node->number_child<int>("SubtitleHeight")), Image::Alignment::PADDED
104 image->read_from_socket (socket);
106 _text = PositionImage (image, Position<int>(node->number_child<int>("SubtitleX"), node->number_child<int>("SubtitleY")));
112 PlayerVideo::set_text (PositionImage image)
119 PlayerVideo::image (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool fast) const
121 /* XXX: this assumes that image() and prepare() are only ever called with the same parameters (except crop, inter size, out size, fade) */
123 boost::mutex::scoped_lock lm (_mutex);
124 if (!_image || _crop != _image_crop || _inter_size != _image_inter_size || _out_size != _image_out_size || _fade != _image_fade) {
125 make_image (pixel_format, video_range, fast);
131 shared_ptr<const Image>
132 PlayerVideo::raw_image () const
134 return _in->image(Image::Alignment::COMPACT, _inter_size).image;
138 /** Create an image for this frame. A lock must be held on _mutex.
139 * @param pixel_format Function which is called to decide what pixel format the output image should be;
140 * it is passed the pixel format of the input image from the ImageProxy, and should return the desired
141 * output pixel format. Two functions force and keep_xyz_or_rgb are provided for use here.
142 * @param fast true to be fast at the expense of quality.
145 PlayerVideo::make_image (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool fast) const
148 _image_inter_size = _inter_size;
149 _image_out_size = _out_size;
152 auto prox = _in->image (Image::Alignment::PADDED, _inter_size);
155 auto total_crop = _crop;
157 case Part::LEFT_HALF:
158 total_crop.right += prox.image->size().width / 2;
160 case Part::RIGHT_HALF:
161 total_crop.left += prox.image->size().width / 2;
164 total_crop.bottom += prox.image->size().height / 2;
166 case Part::BOTTOM_HALF:
167 total_crop.top += prox.image->size().height / 2;
173 if (prox.log2_scaling > 0) {
174 /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
175 int const r = pow(2, prox.log2_scaling);
176 total_crop.left /= r;
177 total_crop.right /= r;
179 total_crop.bottom /= r;
182 dcp::YUVToRGB yuv_to_rgb = dcp::YUVToRGB::REC601;
183 if (_colour_conversion) {
184 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
187 _image = prox.image->crop_scale_window (
188 total_crop, _inter_size, _out_size, yuv_to_rgb, _video_range, pixel_format (prox.image->pixel_format()), video_range, Image::Alignment::COMPACT, fast
192 _image->alpha_blend (_text->image, _text->position);
196 _image->fade (_fade.get ());
202 PlayerVideo::add_metadata (xmlpp::Node* node) const
206 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
208 _in->add_metadata (node->add_child ("In"));
209 node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
210 node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
211 node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
212 node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
213 node->add_child("Eyes")->add_child_text (raw_convert<string> (static_cast<int> (_eyes)));
214 node->add_child("Part")->add_child_text (raw_convert<string> (static_cast<int> (_part)));
215 node->add_child("VideoRange")->add_child_text(raw_convert<string>(static_cast<int>(_video_range)));
216 node->add_child("Error")->add_child_text(_error ? "1" : "0");
217 if (_colour_conversion) {
218 _colour_conversion.get().as_xml (node);
221 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_text->image->size().width));
222 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_text->image->size().height));
223 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_text->position.x));
224 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_text->position.y));
230 PlayerVideo::write_to_socket (shared_ptr<Socket> socket) const
232 _in->write_to_socket (socket);
234 _text->image->write_to_socket (socket);
240 PlayerVideo::has_j2k () const
242 /* XXX: maybe other things */
244 auto j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
249 return _crop == Crop() && _out_size == j2k->size() && _inter_size == j2k->size() && !_text && !_fade && !_colour_conversion;
253 shared_ptr<const dcp::Data>
254 PlayerVideo::j2k () const
256 auto j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
257 DCPOMATIC_ASSERT (j2k);
263 PlayerVideo::inter_position () const
265 return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
269 /** @return true if this PlayerVideo is definitely the same as another, false if it is probably not */
271 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
273 if (_crop != other->_crop ||
274 _fade != other->_fade ||
275 _inter_size != other->_inter_size ||
276 _out_size != other->_out_size ||
277 _eyes != other->_eyes ||
278 _part != other->_part ||
279 _colour_conversion != other->_colour_conversion ||
280 _video_range != other->_video_range) {
284 if ((!_text && other->_text) || (_text && !other->_text)) {
285 /* One has a text and the other doesn't */
289 if (_text && other->_text && !_text->same (other->_text.get ())) {
290 /* They both have texts but they are different */
294 /* Now neither has subtitles */
296 return _in->same (other->_in);
301 PlayerVideo::force (AVPixelFormat force_to)
307 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
309 return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
314 PlayerVideo::prepare (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, Image::Alignment alignment, bool fast, bool proxy_only)
316 _in->prepare (alignment, _inter_size);
317 boost::mutex::scoped_lock lm (_mutex);
318 if (!_image && !proxy_only) {
319 make_image (pixel_format, video_range, fast);
325 PlayerVideo::memory_used () const
327 return _in->memory_used();
331 /** @return Shallow copy of this; _in and _text are shared between the original and the copy */
332 shared_ptr<PlayerVideo>
333 PlayerVideo::shallow_copy () const
335 return std::make_shared<PlayerVideo>(
352 /** Re-read crop, fade, inter/out size, colour conversion and video range from our content.
353 * @return true if this was possible, false if not.
356 PlayerVideo::reset_metadata (shared_ptr<const Film> film, dcp::Size player_video_container_size)
358 auto content = _content.lock();
359 if (!content || !_video_frame) {
363 _crop = content->video->actual_crop();
364 _fade = content->video->fade(film, _video_frame.get());
365 _inter_size = scale_for_display(
366 content->video->scaled_size(film->frame_size()),
367 player_video_container_size,
369 content->video->pixel_quanta()
371 _out_size = player_video_container_size;
372 _colour_conversion = content->video->colour_conversion();
373 _video_range = content->video->range();