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"
28 #include <dcp/raw_convert.h>
30 #include <libavutil/pixfmt.h>
32 #include <libxml++/libxml++.h>
38 using boost::shared_ptr;
39 using boost::weak_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42 using boost::function;
44 using dcp::raw_convert;
46 PlayerVideo::PlayerVideo (
47 shared_ptr<const ImageProxy> in,
49 boost::optional<double> fade,
54 optional<ColourConversion> colour_conversion,
55 weak_ptr<Content> content,
56 optional<Frame> video_frame
61 , _inter_size (inter_size)
62 , _out_size (out_size)
65 , _colour_conversion (colour_conversion)
67 , _video_frame (video_frame)
72 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
75 _fade = node->optional_number_child<double> ("Fade");
77 _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
78 _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
79 _eyes = (Eyes) node->number_child<int> ("Eyes");
80 _part = (Part) node->number_child<int> ("Part");
82 /* Assume that the ColourConversion uses the current state version */
83 _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
85 _in = image_proxy_factory (node->node_child ("In"), socket);
87 if (node->optional_number_child<int> ("SubtitleX")) {
89 shared_ptr<Image> image (
90 new Image (AV_PIX_FMT_BGRA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
93 image->read_from_socket (socket);
95 _subtitle = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
100 PlayerVideo::set_subtitle (PositionImage image)
105 /** Create an image for this frame.
106 * @param note Handler for any notes that are made during the process.
107 * @param pixel_format Function which is called to decide what pixel format the output image should be;
108 * it is passed the pixel format of the input image from the ImageProxy, and should return the desired
109 * output pixel format. Two functions always_rgb and keep_xyz_or_rgb are provided for use here.
110 * @param aligned true if the output image should be aligned to 32-byte boundaries.
111 * @param fast true to be fast at the expense of quality.
114 PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
116 pair<shared_ptr<Image>, int> prox = _in->image (optional<dcp::NoteHandler> (note), _inter_size);
117 shared_ptr<Image> im = prox.first;
118 int const reduce = prox.second;
120 Crop total_crop = _crop;
123 total_crop.right += im->size().width / 2;
125 case PART_RIGHT_HALF:
126 total_crop.left += im->size().width / 2;
129 total_crop.bottom += im->size().height / 2;
131 case PART_BOTTOM_HALF:
132 total_crop.top += im->size().height / 2;
139 /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
140 int const r = pow(2, reduce);
141 total_crop.left /= r;
142 total_crop.right /= r;
144 total_crop.bottom /= r;
147 dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
148 if (_colour_conversion) {
149 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
152 shared_ptr<Image> out = im->crop_scale_window (
153 total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format (_in->pixel_format()), aligned, fast
157 out->alpha_blend (Image::ensure_aligned (_subtitle->image), _subtitle->position);
161 out->fade (_fade.get ());
168 PlayerVideo::add_metadata (xmlpp::Node* node) const
172 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
174 _in->add_metadata (node->add_child ("In"));
175 node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
176 node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
177 node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
178 node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
179 node->add_child("Eyes")->add_child_text (raw_convert<string> (static_cast<int> (_eyes)));
180 node->add_child("Part")->add_child_text (raw_convert<string> (static_cast<int> (_part)));
181 if (_colour_conversion) {
182 _colour_conversion.get().as_xml (node);
185 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle->image->size().width));
186 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle->image->size().height));
187 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle->position.x));
188 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle->position.y));
193 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
195 _in->send_binary (socket);
197 _subtitle->image->write_to_socket (socket);
202 PlayerVideo::has_j2k () const
204 /* XXX: maybe other things */
206 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
211 return _crop == Crop () && _out_size == j2k->size() && !_subtitle && !_fade && !_colour_conversion;
215 PlayerVideo::j2k () const
217 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
218 DCPOMATIC_ASSERT (j2k);
223 PlayerVideo::inter_position () const
225 return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
228 /** @return true if this PlayerVideo is definitely the same as another, false if it is probably not */
230 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
232 if (_crop != other->_crop ||
233 _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
234 _inter_size != other->_inter_size ||
235 _out_size != other->_out_size ||
236 _eyes != other->_eyes ||
237 _part != other->_part ||
238 _colour_conversion != other->_colour_conversion) {
242 if ((!_subtitle && other->_subtitle) || (_subtitle && !other->_subtitle)) {
243 /* One has a subtitle and the other doesn't */
247 if (_subtitle && other->_subtitle && !_subtitle->same (other->_subtitle.get ())) {
248 /* They both have subtitles but they are different */
252 /* Now neither has subtitles */
254 return _in->same (other->_in);
258 PlayerVideo::always_rgb (AVPixelFormat)
260 return AV_PIX_FMT_RGB24;
264 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
266 return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
270 PlayerVideo::prepare ()
272 _in->prepare (_inter_size);
276 PlayerVideo::memory_used () const
278 return _in->memory_used();
281 /** @return Shallow copy of this; _in and _subtitle are shared between the original and the copy */
282 shared_ptr<PlayerVideo>
283 PlayerVideo::shallow_copy () const
285 return shared_ptr<PlayerVideo>(
301 /** Re-read crop, fade, inter/out size and colour conversion from our content.
302 * @return true if this was possible, false if not.
305 PlayerVideo::reset_metadata (dcp::Size video_container_size, dcp::Size film_frame_size)
307 shared_ptr<Content> content = _content.lock();
308 if (!content || !_video_frame) {
312 _crop = content->video->crop();
313 _fade = content->video->fade(_video_frame.get());
314 _inter_size = content->video->scale().size(content->video, video_container_size, film_frame_size);
315 _out_size = video_container_size;
316 _colour_conversion = content->video->colour_conversion();