2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "player_video.h"
22 #include "image_proxy.h"
23 #include "j2k_image_proxy.h"
25 #include "raw_convert.h"
27 #include <libavutil/pixfmt.h>
29 #include <libxml++/libxml++.h>
34 using boost::shared_ptr;
35 using boost::dynamic_pointer_cast;
36 using boost::optional;
37 using boost::function;
40 PlayerVideo::PlayerVideo (
41 shared_ptr<const ImageProxy> in,
44 boost::optional<double> fade,
49 optional<ColourConversion> colour_conversion
55 , _inter_size (inter_size)
56 , _out_size (out_size)
59 , _colour_conversion (colour_conversion)
64 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
66 _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
68 _fade = node->optional_number_child<double> ("Fade");
70 _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
71 _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
72 _eyes = (Eyes) node->number_child<int> ("Eyes");
73 _part = (Part) node->number_child<int> ("Part");
75 /* Assume that the ColourConversion uses the current state version */
76 _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
78 _in = image_proxy_factory (node->node_child ("In"), socket);
80 if (node->optional_number_child<int> ("SubtitleX")) {
82 shared_ptr<Image> image (
83 new Image (AV_PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
86 image->read_from_socket (socket);
88 _subtitle = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
93 PlayerVideo::set_subtitle (PositionImage image)
98 /** Create an image for this frame.
99 * @param note Handler for any notes that are made during the process.
100 * @param pixel_format Function which is called to decide what pixel format the output image should be;
101 * it is passed the pixel format of the input image from the ImageProxy, and should return the desired
102 * output pixel format. Two functions always_rgb and keep_xyz_or_rgb are provided for use here.
105 PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
107 shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note));
109 Crop total_crop = _crop;
112 total_crop.right += im->size().width / 2;
114 case PART_RIGHT_HALF:
115 total_crop.left += im->size().width / 2;
118 total_crop.bottom += im->size().height / 2;
120 case PART_BOTTOM_HALF:
121 total_crop.top += im->size().height / 2;
127 dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
128 if (_colour_conversion) {
129 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
132 shared_ptr<Image> out = im->crop_scale_window (
133 total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format (_in->pixel_format()), aligned, fast
137 out->alpha_blend (_subtitle->image, _subtitle->position);
141 out->fade (_fade.get ());
148 PlayerVideo::add_metadata (xmlpp::Node* node) const
150 node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
153 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
155 _in->add_metadata (node->add_child ("In"));
156 node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
157 node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
158 node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
159 node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
160 node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
161 node->add_child("Part")->add_child_text (raw_convert<string> (_part));
162 if (_colour_conversion) {
163 _colour_conversion.get().as_xml (node);
166 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle->image->size().width));
167 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle->image->size().height));
168 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle->position.x));
169 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle->position.y));
174 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
176 _in->send_binary (socket);
178 _subtitle->image->write_to_socket (socket);
183 PlayerVideo::has_j2k () const
185 /* XXX: maybe other things */
187 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
192 return _crop == Crop () && _inter_size == j2k->size() && !_subtitle && !_fade && !_colour_conversion;
196 PlayerVideo::j2k () const
198 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
199 DCPOMATIC_ASSERT (j2k);
204 PlayerVideo::inter_position () const
206 return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
209 /** @return true if this PlayerVideo is definitely the same as another
210 * (apart from _time), false if it is probably not
213 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
215 if (_crop != other->_crop ||
216 _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
217 _inter_size != other->_inter_size ||
218 _out_size != other->_out_size ||
219 _eyes != other->_eyes ||
220 _part != other->_part ||
221 _colour_conversion != other->_colour_conversion) {
225 if ((!_subtitle && other->_subtitle) || (_subtitle && !other->_subtitle)) {
226 /* One has a subtitle and the other doesn't */
230 if (_subtitle && other->_subtitle && !_subtitle->same (other->_subtitle.get ())) {
231 /* They both have subtitles but they are different */
235 /* Now neither has subtitles */
237 return _in->same (other->_in);
241 PlayerVideo::always_rgb (AVPixelFormat)
243 return AV_PIX_FMT_RGB24;
247 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
249 return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;