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