Merge remote-tracking branch 'origin/master' into 2.0
[dcpomatic.git] / src / lib / player_video.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <dcp/raw_convert.h>
21 #include "player_video.h"
22 #include "image.h"
23 #include "image_proxy.h"
24 #include "j2k_image_proxy.h"
25 #include "scaler.h"
26
27 using std::string;
28 using std::cout;
29 using dcp::raw_convert;
30 using boost::shared_ptr;
31 using boost::dynamic_pointer_cast;
32
33 PlayerVideo::PlayerVideo (
34         shared_ptr<const ImageProxy> in,
35         DCPTime time,
36         Crop crop,
37         boost::optional<float> fade,
38         dcp::Size inter_size,
39         dcp::Size out_size,
40         Scaler const * scaler,
41         Eyes eyes,
42         Part part,
43         ColourConversion colour_conversion
44         )
45         : _in (in)
46         , _time (time)
47         , _crop (crop)
48         , _fade (fade)
49         , _inter_size (inter_size)
50         , _out_size (out_size)
51         , _scaler (scaler)
52         , _eyes (eyes)
53         , _part (part)
54         , _colour_conversion (colour_conversion)
55 {
56
57 }
58
59 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket, shared_ptr<Log> log)
60 {
61         _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
62         _crop = Crop (node);
63         _fade = node->optional_number_child<float> ("Fade");
64
65         _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
66         _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
67         _scaler = Scaler::from_id (node->string_child ("Scaler"));
68         _eyes = (Eyes) node->number_child<int> ("Eyes");
69         _part = (Part) node->number_child<int> ("Part");
70         _colour_conversion = ColourConversion (node);
71
72         _in = image_proxy_factory (node->node_child ("In"), socket, log);
73
74         if (node->optional_number_child<int> ("SubtitleX")) {
75                 
76                 _subtitle.position = Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY"));
77
78                 _subtitle.image.reset (
79                         new Image (PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
80                         );
81                 
82                 _subtitle.image->read_from_socket (socket);
83         }
84 }
85
86 void
87 PlayerVideo::set_subtitle (PositionImage image)
88 {
89         _subtitle = image;
90 }
91
92 shared_ptr<Image>
93 PlayerVideo::image (bool burn_subtitle) const
94 {
95         shared_ptr<Image> im = _in->image ();
96         
97         Crop total_crop = _crop;
98         switch (_part) {
99         case PART_LEFT_HALF:
100                 total_crop.right += im->size().width / 2;
101                 break;
102         case PART_RIGHT_HALF:
103                 total_crop.left += im->size().width / 2;
104                 break;
105         case PART_TOP_HALF:
106                 total_crop.bottom += im->size().height / 2;
107                 break;
108         case PART_BOTTOM_HALF:
109                 total_crop.top += im->size().height / 2;
110                 break;
111         default:
112                 break;
113         }
114                 
115         shared_ptr<Image> out = im->crop_scale_window (total_crop, _inter_size, _out_size, _scaler, PIX_FMT_RGB24, true);
116
117         if (burn_subtitle && _subtitle.image) {
118                 out->alpha_blend (_subtitle.image, _subtitle.position);
119         }
120
121         if (_fade) {
122                 out->fade (_fade.get ());
123         }
124
125         return out;
126 }
127
128 void
129 PlayerVideo::add_metadata (xmlpp::Node* node, bool send_subtitles) const
130 {
131         node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
132         _crop.as_xml (node);
133         if (_fade) {
134                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
135         }
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("Scaler")->add_child_text (_scaler->id ());
142         node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
143         node->add_child("Part")->add_child_text (raw_convert<string> (_part));
144         _colour_conversion.as_xml (node);
145         if (send_subtitles && _subtitle.image) {
146                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle.image->size().width));
147                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle.image->size().height));
148                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle.position.x));
149                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle.position.y));
150         }
151 }
152
153 void
154 PlayerVideo::send_binary (shared_ptr<Socket> socket, bool send_subtitles) const
155 {
156         _in->send_binary (socket);
157         if (send_subtitles && _subtitle.image) {
158                 _subtitle.image->write_to_socket (socket);
159         }
160 }
161
162 bool
163 PlayerVideo::has_j2k () const
164 {
165         /* XXX: burnt-in subtitle; maybe other things */
166         
167         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
168         if (!j2k) {
169                 return false;
170         }
171         
172         return _crop == Crop () && _inter_size == j2k->size();
173 }
174
175 shared_ptr<EncodedData>
176 PlayerVideo::j2k () const
177 {
178         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
179         assert (j2k);
180         return j2k->j2k ();
181 }
182
183 Position<int>
184 PlayerVideo::inter_position () const
185 {
186         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
187 }
188
189 /** @return true if this PlayerVideo is definitely the same as another
190  * (apart from _time), false if it is probably not
191  */
192 bool
193 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
194 {
195         if (_in != other->_in ||
196             _crop != other->_crop ||
197             _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
198             _inter_size != other->_inter_size ||
199             _out_size != other->_out_size ||
200             _scaler != other->_scaler ||
201             _eyes != other->_eyes ||
202             _part != other->_part ||
203             _colour_conversion != other->_colour_conversion ||
204             !_subtitle.same (other->_subtitle)) {
205                 return false;
206         }
207
208         return _in->same (other->_in);
209 }