Include tidying src/lib/a-j*.h
[dcpomatic.git] / src / lib / player_video.cc
1 /*
2     Copyright (C) 2013-2015 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 "player_video.h"
21 #include "image.h"
22 #include "image_proxy.h"
23 #include "j2k_image_proxy.h"
24 #include "film.h"
25 #include "raw_convert.h"
26 #include <libxml++/libxml++.h>
27
28 using std::string;
29 using std::cout;
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<double> fade,
39         dcp::Size inter_size,
40         dcp::Size out_size,
41         Eyes eyes,
42         Part part,
43         optional<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         , _eyes (eyes)
52         , _part (part)
53         , _colour_conversion (colour_conversion)
54 {
55
56 }
57
58 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
59 {
60         _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
61         _crop = Crop (node);
62         _fade = node->optional_number_child<double> ("Fade");
63
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");
68
69         /* Assume that the ColourConversion uses the current state version */
70         _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
71
72         _in = image_proxy_factory (node->node_child ("In"), socket);
73
74         if (node->optional_number_child<int> ("SubtitleX")) {
75
76                 shared_ptr<Image> image (
77                         new Image (PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
78                         );
79
80                 image->read_from_socket (socket);
81
82                 _subtitle = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
83         }
84 }
85
86 void
87 PlayerVideo::set_subtitle (PositionImage image)
88 {
89         _subtitle = image;
90 }
91
92 shared_ptr<Image>
93 PlayerVideo::image (AVPixelFormat pixel_format, dcp::NoteHandler note) const
94 {
95         shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note));
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         dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
116         if (_colour_conversion) {
117                 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
118         }
119
120         shared_ptr<Image> out = im->crop_scale_window (total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format, true);
121
122         if (_subtitle) {
123                 out->alpha_blend (_subtitle->image, _subtitle->position);
124         }
125
126         if (_fade) {
127                 out->fade (_fade.get ());
128         }
129
130         return out;
131 }
132
133 void
134 PlayerVideo::add_metadata (xmlpp::Node* node) const
135 {
136         node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
137         _crop.as_xml (node);
138         if (_fade) {
139                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
140         }
141         _in->add_metadata (node->add_child ("In"));
142         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
143         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
144         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
145         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
146         node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
147         node->add_child("Part")->add_child_text (raw_convert<string> (_part));
148         if (_colour_conversion) {
149                 _colour_conversion.get().as_xml (node);
150         }
151         if (_subtitle) {
152                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle->image->size().width));
153                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle->image->size().height));
154                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle->position.x));
155                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle->position.y));
156         }
157 }
158
159 void
160 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
161 {
162         _in->send_binary (socket);
163         if (_subtitle) {
164                 _subtitle->image->write_to_socket (socket);
165         }
166 }
167
168 bool
169 PlayerVideo::has_j2k () const
170 {
171         /* XXX: maybe other things */
172
173         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
174         if (!j2k) {
175                 return false;
176         }
177
178         return _crop == Crop () && _inter_size == j2k->size() && !_subtitle && !_fade;
179 }
180
181 Data
182 PlayerVideo::j2k () const
183 {
184         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
185         DCPOMATIC_ASSERT (j2k);
186         return j2k->j2k ();
187 }
188
189 Position<int>
190 PlayerVideo::inter_position () const
191 {
192         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
193 }
194
195 /** @return true if this PlayerVideo is definitely the same as another
196  * (apart from _time), false if it is probably not
197  */
198 bool
199 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
200 {
201         if (_crop != other->_crop ||
202             _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
203             _inter_size != other->_inter_size ||
204             _out_size != other->_out_size ||
205             _eyes != other->_eyes ||
206             _part != other->_part ||
207             _colour_conversion != other->_colour_conversion) {
208                 return false;
209         }
210
211         if ((!_subtitle && other->_subtitle) || (_subtitle && !other->_subtitle)) {
212                 /* One has a subtitle and the other doesn't */
213                 return false;
214         }
215
216         if (_subtitle && other->_subtitle && !_subtitle->same (other->_subtitle.get ())) {
217                 /* They both have subtitles but they are different */
218                 return false;
219         }
220
221         /* Now neither has subtitles */
222
223         return _in->same (other->_in);
224 }