No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / src / lib / player_video.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "player_video.h"
22 #include "image.h"
23 #include "image_proxy.h"
24 #include "j2k_image_proxy.h"
25 #include "film.h"
26 #include "raw_convert.h"
27 extern "C" {
28 #include <libavutil/pixfmt.h>
29 }
30 #include <libxml++/libxml++.h>
31 #include <iostream>
32
33 using std::string;
34 using std::cout;
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37 using boost::optional;
38 using boost::function;
39 using dcp::Data;
40
41 PlayerVideo::PlayerVideo (
42         shared_ptr<const ImageProxy> in,
43         DCPTime time,
44         Crop crop,
45         boost::optional<double> fade,
46         dcp::Size inter_size,
47         dcp::Size out_size,
48         Eyes eyes,
49         Part part,
50         optional<ColourConversion> colour_conversion
51         )
52         : _in (in)
53         , _time (time)
54         , _crop (crop)
55         , _fade (fade)
56         , _inter_size (inter_size)
57         , _out_size (out_size)
58         , _eyes (eyes)
59         , _part (part)
60         , _colour_conversion (colour_conversion)
61 {
62
63 }
64
65 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
66 {
67         _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
68         _crop = Crop (node);
69         _fade = node->optional_number_child<double> ("Fade");
70
71         _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
72         _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
73         _eyes = (Eyes) node->number_child<int> ("Eyes");
74         _part = (Part) node->number_child<int> ("Part");
75
76         /* Assume that the ColourConversion uses the current state version */
77         _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
78
79         _in = image_proxy_factory (node->node_child ("In"), socket);
80
81         if (node->optional_number_child<int> ("SubtitleX")) {
82
83                 shared_ptr<Image> image (
84                         new Image (AV_PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
85                         );
86
87                 image->read_from_socket (socket);
88
89                 _subtitle = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
90         }
91 }
92
93 void
94 PlayerVideo::set_subtitle (PositionImage image)
95 {
96         _subtitle = image;
97 }
98
99 /** Create an image for this frame.
100  *  @param note Handler for any notes that are made during the process.
101  *  @param pixel_format Function which is called to decide what pixel format the output image should be;
102  *  it is passed the pixel format of the input image from the ImageProxy, and should return the desired
103  *  output pixel format.  Two functions always_rgb and keep_xyz_or_rgb are provided for use here.
104  */
105 shared_ptr<Image>
106 PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
107 {
108         shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note));
109
110         Crop total_crop = _crop;
111         switch (_part) {
112         case PART_LEFT_HALF:
113                 total_crop.right += im->size().width / 2;
114                 break;
115         case PART_RIGHT_HALF:
116                 total_crop.left += im->size().width / 2;
117                 break;
118         case PART_TOP_HALF:
119                 total_crop.bottom += im->size().height / 2;
120                 break;
121         case PART_BOTTOM_HALF:
122                 total_crop.top += im->size().height / 2;
123                 break;
124         default:
125                 break;
126         }
127
128         dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
129         if (_colour_conversion) {
130                 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
131         }
132
133         shared_ptr<Image> out = im->crop_scale_window (
134                 total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format (_in->pixel_format()), aligned, fast
135                 );
136
137         if (_subtitle) {
138                 out->alpha_blend (_subtitle->image, _subtitle->position);
139         }
140
141         if (_fade) {
142                 out->fade (_fade.get ());
143         }
144
145         return out;
146 }
147
148 void
149 PlayerVideo::add_metadata (xmlpp::Node* node) const
150 {
151         node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
152         _crop.as_xml (node);
153         if (_fade) {
154                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
155         }
156         _in->add_metadata (node->add_child ("In"));
157         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
158         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
159         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
160         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
161         node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
162         node->add_child("Part")->add_child_text (raw_convert<string> (_part));
163         if (_colour_conversion) {
164                 _colour_conversion.get().as_xml (node);
165         }
166         if (_subtitle) {
167                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle->image->size().width));
168                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle->image->size().height));
169                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle->position.x));
170                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle->position.y));
171         }
172 }
173
174 void
175 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
176 {
177         _in->send_binary (socket);
178         if (_subtitle) {
179                 _subtitle->image->write_to_socket (socket);
180         }
181 }
182
183 bool
184 PlayerVideo::has_j2k () const
185 {
186         /* XXX: maybe other things */
187
188         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
189         if (!j2k) {
190                 return false;
191         }
192
193         return _crop == Crop () && _inter_size == j2k->size() && !_subtitle && !_fade && !_colour_conversion;
194 }
195
196 Data
197 PlayerVideo::j2k () const
198 {
199         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
200         DCPOMATIC_ASSERT (j2k);
201         return j2k->j2k ();
202 }
203
204 Position<int>
205 PlayerVideo::inter_position () const
206 {
207         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
208 }
209
210 /** @return true if this PlayerVideo is definitely the same as another
211  * (apart from _time), false if it is probably not
212  */
213 bool
214 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
215 {
216         if (_crop != other->_crop ||
217             _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
218             _inter_size != other->_inter_size ||
219             _out_size != other->_out_size ||
220             _eyes != other->_eyes ||
221             _part != other->_part ||
222             _colour_conversion != other->_colour_conversion) {
223                 return false;
224         }
225
226         if ((!_subtitle && other->_subtitle) || (_subtitle && !other->_subtitle)) {
227                 /* One has a subtitle and the other doesn't */
228                 return false;
229         }
230
231         if (_subtitle && other->_subtitle && !_subtitle->same (other->_subtitle.get ())) {
232                 /* They both have subtitles but they are different */
233                 return false;
234         }
235
236         /* Now neither has subtitles */
237
238         return _in->same (other->_in);
239 }
240
241 AVPixelFormat
242 PlayerVideo::always_rgb (AVPixelFormat)
243 {
244         return AV_PIX_FMT_RGB24;
245 }
246
247 AVPixelFormat
248 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
249 {
250         return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
251 }