2 Copyright (C) 2013-2018 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/>.
21 #include "player_video.h"
23 #include "video_content.h"
25 #include "image_proxy.h"
26 #include "j2k_image_proxy.h"
29 #include <dcp/raw_convert.h>
31 #include <libavutil/pixfmt.h>
33 #include <libxml++/libxml++.h>
39 using boost::shared_ptr;
40 using boost::weak_ptr;
41 using boost::dynamic_pointer_cast;
42 using boost::optional;
43 using boost::function;
45 using dcp::raw_convert;
46 using namespace dcpomatic;
48 PlayerVideo::PlayerVideo (
49 shared_ptr<const ImageProxy> in,
51 boost::optional<double> fade,
56 optional<ColourConversion> colour_conversion,
57 VideoRange video_range,
58 weak_ptr<Content> content,
59 optional<Frame> video_frame,
65 , _inter_size (inter_size)
66 , _out_size (out_size)
69 , _colour_conversion (colour_conversion)
70 , _video_range (video_range)
72 , _video_frame (video_frame)
78 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
81 _fade = node->optional_number_child<double> ("Fade");
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 = (Eyes) node->number_child<int> ("Eyes");
86 _part = (Part) node->number_child<int> ("Part");
87 _video_range = (VideoRange) node->number_child<int>("VideoRange");
88 _error = node->optional_bool_child("Error").get_value_or (false);
90 /* Assume that the ColourConversion uses the current state version */
91 _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
93 _in = image_proxy_factory (node->node_child ("In"), socket);
95 if (node->optional_number_child<int> ("SubtitleX")) {
97 shared_ptr<Image> image (
98 new Image (AV_PIX_FMT_BGRA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
101 image->read_from_socket (socket);
103 _text = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
108 PlayerVideo::set_text (PositionImage image)
114 PlayerVideo::image (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
116 /* XXX: this assumes that image() and prepare() are only ever called with the same parameters (except crop, inter size, out size, fade) */
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 timestamped_printf("_________________!!! make_image() for %d [%d]\n", time.frames_round(24), !_image);
121 make_image (pixel_format, aligned, fast);
126 /** Create an image for this frame. A lock must be held on _mutex.
127 * @param pixel_format Function which is called to decide what pixel format the output image should be;
128 * it is passed the pixel format of the input image from the ImageProxy, and should return the desired
129 * output pixel format. Two functions force and keep_xyz_or_rgb are provided for use here.
130 * @param aligned true if the output image should be aligned to 32-byte boundaries.
131 * @param fast true to be fast at the expense of quality.
134 PlayerVideo::make_image (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
137 _image_inter_size = _inter_size;
138 _image_out_size = _out_size;
141 ImageProxy::Result prox = _in->image (_inter_size);
144 Crop total_crop = _crop;
147 total_crop.right += prox.image->size().width / 2;
149 case PART_RIGHT_HALF:
150 total_crop.left += prox.image->size().width / 2;
153 total_crop.bottom += prox.image->size().height / 2;
155 case PART_BOTTOM_HALF:
156 total_crop.top += prox.image->size().height / 2;
162 if (prox.log2_scaling > 0) {
163 /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
164 int const r = pow(2, prox.log2_scaling);
165 total_crop.left /= r;
166 total_crop.right /= r;
168 total_crop.bottom /= r;
171 dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
172 if (_colour_conversion) {
173 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
176 _image = prox.image->crop_scale_window (
177 total_crop, _inter_size, _out_size, yuv_to_rgb, _video_range, pixel_format (prox.image->pixel_format()), aligned, fast
181 _image->alpha_blend (Image::ensure_aligned (_text->image), _text->position);
185 _image->fade (_fade.get ());
190 PlayerVideo::add_metadata (xmlpp::Node* node) const
194 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
196 _in->add_metadata (node->add_child ("In"));
197 node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
198 node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
199 node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
200 node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
201 node->add_child("Eyes")->add_child_text (raw_convert<string> (static_cast<int> (_eyes)));
202 node->add_child("Part")->add_child_text (raw_convert<string> (static_cast<int> (_part)));
203 node->add_child("VideoRange")->add_child_text(raw_convert<string>(static_cast<int>(_video_range)));
204 node->add_child("Error")->add_child_text(_error ? "1" : "0");
205 if (_colour_conversion) {
206 _colour_conversion.get().as_xml (node);
209 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_text->image->size().width));
210 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_text->image->size().height));
211 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_text->position.x));
212 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_text->position.y));
217 PlayerVideo::write_to_socket (shared_ptr<Socket> socket) const
219 _in->write_to_socket (socket);
221 _text->image->write_to_socket (socket);
226 PlayerVideo::has_j2k () const
228 /* XXX: maybe other things */
230 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
235 return _crop == Crop () && _out_size == j2k->size() && !_text && !_fade && !_colour_conversion;
239 PlayerVideo::j2k () const
241 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
242 DCPOMATIC_ASSERT (j2k);
247 PlayerVideo::inter_position () const
249 return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
252 /** @return true if this PlayerVideo is definitely the same as another, false if it is probably not */
254 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
256 if (_crop != other->_crop ||
257 _fade != other->_fade ||
258 _inter_size != other->_inter_size ||
259 _out_size != other->_out_size ||
260 _eyes != other->_eyes ||
261 _part != other->_part ||
262 _colour_conversion != other->_colour_conversion ||
263 _video_range != other->_video_range) {
267 if ((!_text && other->_text) || (_text && !other->_text)) {
268 /* One has a text and the other doesn't */
272 if (_text && other->_text && !_text->same (other->_text.get ())) {
273 /* They both have texts but they are different */
277 /* Now neither has subtitles */
279 return _in->same (other->_in);
283 PlayerVideo::force (AVPixelFormat, AVPixelFormat force_to)
289 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
291 return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
295 PlayerVideo::prepare (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast)
297 _in->prepare (_inter_size);
298 boost::mutex::scoped_lock lm (_mutex);
300 make_image (pixel_format, aligned, fast);
305 PlayerVideo::memory_used () const
307 return _in->memory_used();
310 /** @return Shallow copy of this; _in and _text are shared between the original and the copy */
311 shared_ptr<PlayerVideo>
312 PlayerVideo::shallow_copy () const
314 return shared_ptr<PlayerVideo>(
332 /** Re-read crop, fade, inter/out size, colour conversion and video range from our content.
333 * @return true if this was possible, false if not.
336 PlayerVideo::reset_metadata (shared_ptr<const Film> film, dcp::Size video_container_size, dcp::Size film_frame_size)
338 shared_ptr<Content> content = _content.lock();
339 if (!content || !_video_frame) {
343 _crop = content->video->crop();
344 _fade = content->video->fade(film, _video_frame.get());
345 _inter_size = scale_for_display(content->video->scaled_size(film_frame_size), video_container_size, film_frame_size);
346 _out_size = video_container_size;
347 _colour_conversion = content->video->colour_conversion();
348 _video_range = content->video->range();