Logging improvements to allow prettier displays in the server GUI.
[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 #include <iostream>
28
29 using std::string;
30 using std::cout;
31 using boost::shared_ptr;
32 using boost::dynamic_pointer_cast;
33 using boost::optional;
34
35 PlayerVideo::PlayerVideo (
36         shared_ptr<const ImageProxy> in,
37         DCPTime time,
38         Crop crop,
39         boost::optional<double> fade,
40         dcp::Size inter_size,
41         dcp::Size out_size,
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         , _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)
60 {
61         _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
62         _crop = Crop (node);
63         _fade = node->optional_number_child<double> ("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         _eyes = (Eyes) node->number_child<int> ("Eyes");
68         _part = (Part) node->number_child<int> ("Part");
69
70         /* Assume that the ColourConversion uses the current state version */
71         _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
72
73         _in = image_proxy_factory (node->node_child ("In"), socket);
74
75         if (node->optional_number_child<int> ("SubtitleX")) {
76
77                 shared_ptr<Image> image (
78                         new Image (PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
79                         );
80
81                 image->read_from_socket (socket);
82
83                 _subtitle = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
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, dcp::NoteHandler note) const
95 {
96         shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note));
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         dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
117         if (_colour_conversion) {
118                 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
119         }
120
121         shared_ptr<Image> out = im->crop_scale_window (total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format, true);
122
123         if (_subtitle) {
124                 out->alpha_blend (_subtitle->image, _subtitle->position);
125         }
126
127         if (_fade) {
128                 out->fade (_fade.get ());
129         }
130
131         return out;
132 }
133
134 void
135 PlayerVideo::add_metadata (xmlpp::Node* node) const
136 {
137         node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
138         _crop.as_xml (node);
139         if (_fade) {
140                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
141         }
142         _in->add_metadata (node->add_child ("In"));
143         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
144         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
145         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
146         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
147         node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
148         node->add_child("Part")->add_child_text (raw_convert<string> (_part));
149         if (_colour_conversion) {
150                 _colour_conversion.get().as_xml (node);
151         }
152         if (_subtitle) {
153                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle->image->size().width));
154                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle->image->size().height));
155                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle->position.x));
156                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle->position.y));
157         }
158 }
159
160 void
161 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
162 {
163         _in->send_binary (socket);
164         if (_subtitle) {
165                 _subtitle->image->write_to_socket (socket);
166         }
167 }
168
169 bool
170 PlayerVideo::has_j2k () const
171 {
172         /* XXX: maybe other things */
173
174         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
175         if (!j2k) {
176                 return false;
177         }
178
179         return _crop == Crop () && _inter_size == j2k->size() && !_subtitle && !_fade && !_colour_conversion;
180 }
181
182 Data
183 PlayerVideo::j2k () const
184 {
185         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
186         DCPOMATIC_ASSERT (j2k);
187         return j2k->j2k ();
188 }
189
190 Position<int>
191 PlayerVideo::inter_position () const
192 {
193         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
194 }
195
196 /** @return true if this PlayerVideo is definitely the same as another
197  * (apart from _time), false if it is probably not
198  */
199 bool
200 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
201 {
202         if (_crop != other->_crop ||
203             _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
204             _inter_size != other->_inter_size ||
205             _out_size != other->_out_size ||
206             _eyes != other->_eyes ||
207             _part != other->_part ||
208             _colour_conversion != other->_colour_conversion) {
209                 return false;
210         }
211
212         if ((!_subtitle && other->_subtitle) || (_subtitle && !other->_subtitle)) {
213                 /* One has a subtitle and the other doesn't */
214                 return false;
215         }
216
217         if (_subtitle && other->_subtitle && !_subtitle->same (other->_subtitle.get ())) {
218                 /* They both have subtitles but they are different */
219                 return false;
220         }
221
222         /* Now neither has subtitles */
223
224         return _in->same (other->_in);
225 }