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 <dcp/raw_convert.h>
21 #include "player_video.h"
23 #include "image_proxy.h"
24 #include "j2k_image_proxy.h"
29 using dcp::raw_convert;
30 using boost::shared_ptr;
31 using boost::dynamic_pointer_cast;
32 using boost::optional;
34 PlayerVideo::PlayerVideo (
35 shared_ptr<const ImageProxy> in,
38 boost::optional<float> fade,
43 optional<ColourConversion> colour_conversion
49 , _inter_size (inter_size)
50 , _out_size (out_size)
53 , _colour_conversion (colour_conversion)
58 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
60 _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
62 _fade = node->optional_number_child<float> ("Fade");
64 _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
65 _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
66 _eyes = (Eyes) node->number_child<int> ("Eyes");
67 _part = (Part) node->number_child<int> ("Part");
69 /* Assume that the ColourConversion uses the current state version */
70 _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
72 _in = image_proxy_factory (node->node_child ("In"), socket);
74 if (node->optional_number_child<int> ("SubtitleX")) {
76 _subtitle.position = Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY"));
78 _subtitle.image.reset (
79 new Image (PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
82 _subtitle.image->read_from_socket (socket);
87 PlayerVideo::set_subtitle (PositionImage image)
93 PlayerVideo::image (AVPixelFormat pixel_format, bool burn_subtitle, dcp::NoteHandler note) const
95 shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note));
97 Crop total_crop = _crop;
100 total_crop.right += im->size().width / 2;
102 case PART_RIGHT_HALF:
103 total_crop.left += im->size().width / 2;
106 total_crop.bottom += im->size().height / 2;
108 case PART_BOTTOM_HALF:
109 total_crop.top += im->size().height / 2;
115 shared_ptr<Image> out = im->crop_scale_window (total_crop, _inter_size, _out_size, pixel_format, true);
117 if (burn_subtitle && _subtitle.image) {
118 out->alpha_blend (_subtitle.image, _subtitle.position);
122 out->fade (_fade.get ());
129 PlayerVideo::add_metadata (xmlpp::Node* node, bool send_subtitles) const
131 node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
134 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
136 _in->add_metadata (node->add_child ("In"));
137 node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
138 node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
139 node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
140 node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
141 node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
142 node->add_child("Part")->add_child_text (raw_convert<string> (_part));
143 if (_colour_conversion) {
144 _colour_conversion.get().as_xml (node);
146 if (send_subtitles && _subtitle.image) {
147 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle.image->size().width));
148 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle.image->size().height));
149 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle.position.x));
150 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle.position.y));
155 PlayerVideo::send_binary (shared_ptr<Socket> socket, bool send_subtitles) const
157 _in->send_binary (socket);
158 if (send_subtitles && _subtitle.image) {
159 _subtitle.image->write_to_socket (socket);
164 PlayerVideo::has_j2k () const
166 /* XXX: burnt-in subtitle; maybe other things */
168 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
173 return _crop == Crop () && _inter_size == j2k->size();
176 shared_ptr<EncodedData>
177 PlayerVideo::j2k () const
179 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
180 DCPOMATIC_ASSERT (j2k);
185 PlayerVideo::inter_position () const
187 return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
190 /** @return true if this PlayerVideo is definitely the same as another
191 * (apart from _time), false if it is probably not
194 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
196 if (_in != other->_in ||
197 _crop != other->_crop ||
198 _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
199 _inter_size != other->_inter_size ||
200 _out_size != other->_out_size ||
201 _eyes != other->_eyes ||
202 _part != other->_part ||
203 _colour_conversion != other->_colour_conversion ||
204 !_subtitle.same (other->_subtitle)) {
208 return _in->same (other->_in);