2 Copyright (C) 2013-2014 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;
33 PlayerVideo::PlayerVideo (
34 shared_ptr<const ImageProxy> in,
39 Scaler const * scaler,
42 ColourConversion colour_conversion
47 , _inter_size (inter_size)
48 , _out_size (out_size)
52 , _colour_conversion (colour_conversion)
57 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket, shared_ptr<Log> log)
59 _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
62 _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
63 _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
64 _scaler = Scaler::from_id (node->string_child ("Scaler"));
65 _eyes = (Eyes) node->number_child<int> ("Eyes");
66 _part = (Part) node->number_child<int> ("Part");
67 _colour_conversion = ColourConversion (node);
69 _in = image_proxy_factory (node->node_child ("In"), socket, log);
71 if (node->optional_number_child<int> ("SubtitleX")) {
73 _subtitle.position = Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY"));
75 _subtitle.image.reset (
76 new Image (PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
79 _subtitle.image->read_from_socket (socket);
84 PlayerVideo::set_subtitle (PositionImage image)
90 PlayerVideo::image (bool burn_subtitle) const
92 shared_ptr<Image> im = _in->image ();
94 Crop total_crop = _crop;
97 total_crop.right += im->size().width / 2;
100 total_crop.left += im->size().width / 2;
103 total_crop.bottom += im->size().height / 2;
105 case PART_BOTTOM_HALF:
106 total_crop.top += im->size().height / 2;
112 shared_ptr<Image> out = im->crop_scale_window (total_crop, _inter_size, _out_size, _scaler, PIX_FMT_RGB24, true);
114 Position<int> const container_offset ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.width) / 2);
116 if (burn_subtitle && _subtitle.image) {
117 out->alpha_blend (_subtitle.image, _subtitle.position);
124 PlayerVideo::add_metadata (xmlpp::Node* node, bool send_subtitles) const
126 node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
128 _in->add_metadata (node->add_child ("In"));
129 node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
130 node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
131 node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
132 node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
133 node->add_child("Scaler")->add_child_text (_scaler->id ());
134 node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
135 node->add_child("Part")->add_child_text (raw_convert<string> (_part));
136 _colour_conversion.as_xml (node);
137 if (send_subtitles && _subtitle.image) {
138 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle.image->size().width));
139 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle.image->size().height));
140 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle.position.x));
141 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle.position.y));
146 PlayerVideo::send_binary (shared_ptr<Socket> socket, bool send_subtitles) const
148 _in->send_binary (socket);
149 if (send_subtitles && _subtitle.image) {
150 _subtitle.image->write_to_socket (socket);
155 PlayerVideo::has_j2k () const
157 /* XXX: burnt-in subtitle; maybe other things */
159 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
164 return _crop == Crop () && _inter_size == j2k->size();
167 shared_ptr<EncodedData>
168 PlayerVideo::j2k () const
170 shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);