94432d66e046c289eb4f60f31923f8ac62623abe
[dcpomatic.git] / src / lib / player_video.cc
1 /*
2     Copyright (C) 2013-2018 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 "content.h"
23 #include "video_content.h"
24 #include "image.h"
25 #include "image_proxy.h"
26 #include "j2k_image_proxy.h"
27 #include "film.h"
28 #include <dcp/raw_convert.h>
29 extern "C" {
30 #include <libavutil/pixfmt.h>
31 }
32 #include <libxml++/libxml++.h>
33 #include <iostream>
34
35 using std::string;
36 using std::cout;
37 using std::pair;
38 using boost::shared_ptr;
39 using boost::weak_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42 using boost::function;
43 using dcp::Data;
44 using dcp::raw_convert;
45
46 PlayerVideo::PlayerVideo (
47         shared_ptr<const ImageProxy> in,
48         Crop crop,
49         boost::optional<double> fade,
50         dcp::Size inter_size,
51         dcp::Size out_size,
52         Eyes eyes,
53         Part part,
54         optional<ColourConversion> colour_conversion,
55         weak_ptr<Content> content,
56         optional<Frame> video_frame
57         )
58         : _in (in)
59         , _crop (crop)
60         , _fade (fade)
61         , _inter_size (inter_size)
62         , _out_size (out_size)
63         , _eyes (eyes)
64         , _part (part)
65         , _colour_conversion (colour_conversion)
66         , _content (content)
67         , _video_frame (video_frame)
68 {
69
70 }
71
72 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
73 {
74         _crop = Crop (node);
75         _fade = node->optional_number_child<double> ("Fade");
76
77         _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
78         _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
79         _eyes = (Eyes) node->number_child<int> ("Eyes");
80         _part = (Part) node->number_child<int> ("Part");
81
82         /* Assume that the ColourConversion uses the current state version */
83         _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
84
85         _in = image_proxy_factory (node->node_child ("In"), socket);
86
87         if (node->optional_number_child<int> ("SubtitleX")) {
88
89                 shared_ptr<Image> image (
90                         new Image (AV_PIX_FMT_BGRA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
91                         );
92
93                 image->read_from_socket (socket);
94
95                 _text = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
96         }
97 }
98
99 void
100 PlayerVideo::set_text (PositionImage image)
101 {
102         _text = image;
103 }
104
105 shared_ptr<Image>
106 PlayerVideo::image (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
107 {
108         /* XXX: this assumes that image() and prepare() are only ever called with the same parameters */
109
110         boost::mutex::scoped_lock lm (_mutex);
111         if (!_image) {
112                 make_image (pixel_format, aligned, fast);
113         }
114         return _image;
115 }
116
117 /** Create an image for this frame.  A lock must be held on _mutex.
118  *  @param pixel_format Function which is called to decide what pixel format the output image should be;
119  *  it is passed the pixel format of the input image from the ImageProxy, and should return the desired
120  *  output pixel format.  Two functions force and keep_xyz_or_rgb are provided for use here.
121  *  @param aligned true if the output image should be aligned to 32-byte boundaries.
122  *  @param fast true to be fast at the expense of quality.
123  */
124 void
125 PlayerVideo::make_image (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
126 {
127         pair<shared_ptr<Image>, int> prox = _in->image (_inter_size);
128         shared_ptr<Image> im = prox.first;
129         int const reduce = prox.second;
130
131         Crop total_crop = _crop;
132         switch (_part) {
133         case PART_LEFT_HALF:
134                 total_crop.right += im->size().width / 2;
135                 break;
136         case PART_RIGHT_HALF:
137                 total_crop.left += im->size().width / 2;
138                 break;
139         case PART_TOP_HALF:
140                 total_crop.bottom += im->size().height / 2;
141                 break;
142         case PART_BOTTOM_HALF:
143                 total_crop.top += im->size().height / 2;
144                 break;
145         default:
146                 break;
147         }
148
149         if (reduce > 0) {
150                 /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
151                 int const r = pow(2, reduce);
152                 total_crop.left /= r;
153                 total_crop.right /= r;
154                 total_crop.top /= r;
155                 total_crop.bottom /= r;
156         }
157
158         dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
159         if (_colour_conversion) {
160                 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
161         }
162
163         _image = im->crop_scale_window (
164                 total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format (im->pixel_format()), aligned, fast
165                 );
166
167         if (_text) {
168                 _image->alpha_blend (Image::ensure_aligned (_text->image), _text->position);
169         }
170
171         if (_fade) {
172                 _image->fade (_fade.get ());
173         }
174 }
175
176 void
177 PlayerVideo::add_metadata (xmlpp::Node* node) const
178 {
179         _crop.as_xml (node);
180         if (_fade) {
181                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
182         }
183         _in->add_metadata (node->add_child ("In"));
184         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
185         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
186         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
187         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
188         node->add_child("Eyes")->add_child_text (raw_convert<string> (static_cast<int> (_eyes)));
189         node->add_child("Part")->add_child_text (raw_convert<string> (static_cast<int> (_part)));
190         if (_colour_conversion) {
191                 _colour_conversion.get().as_xml (node);
192         }
193         if (_text) {
194                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_text->image->size().width));
195                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_text->image->size().height));
196                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_text->position.x));
197                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_text->position.y));
198         }
199 }
200
201 void
202 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
203 {
204         _in->send_binary (socket);
205         if (_text) {
206                 _text->image->write_to_socket (socket);
207         }
208 }
209
210 bool
211 PlayerVideo::has_j2k () const
212 {
213         /* XXX: maybe other things */
214
215         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
216         if (!j2k) {
217                 return false;
218         }
219
220         return _crop == Crop () && _out_size == j2k->size() && !_text && !_fade && !_colour_conversion;
221 }
222
223 Data
224 PlayerVideo::j2k () const
225 {
226         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
227         DCPOMATIC_ASSERT (j2k);
228         return j2k->j2k ();
229 }
230
231 Position<int>
232 PlayerVideo::inter_position () const
233 {
234         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
235 }
236
237 /** @return true if this PlayerVideo is definitely the same as another, false if it is probably not */
238 bool
239 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
240 {
241         if (_crop != other->_crop ||
242             _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
243             _inter_size != other->_inter_size ||
244             _out_size != other->_out_size ||
245             _eyes != other->_eyes ||
246             _part != other->_part ||
247             _colour_conversion != other->_colour_conversion) {
248                 return false;
249         }
250
251         if ((!_text && other->_text) || (_text && !other->_text)) {
252                 /* One has a text and the other doesn't */
253                 return false;
254         }
255
256         if (_text && other->_text && !_text->same (other->_text.get ())) {
257                 /* They both have texts but they are different */
258                 return false;
259         }
260
261         /* Now neither has subtitles */
262
263         return _in->same (other->_in);
264 }
265
266 AVPixelFormat
267 PlayerVideo::force (AVPixelFormat, AVPixelFormat force_to)
268 {
269         return force_to;
270 }
271
272 AVPixelFormat
273 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
274 {
275         return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
276 }
277
278 void
279 PlayerVideo::prepare (function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast)
280 {
281         _in->prepare (_inter_size);
282         boost::mutex::scoped_lock lm (_mutex);
283         if (!_image) {
284                 make_image (pixel_format, aligned, fast);
285         }
286 }
287
288 size_t
289 PlayerVideo::memory_used () const
290 {
291         return _in->memory_used();
292 }
293
294 /** @return Shallow copy of this; _in and _text are shared between the original and the copy */
295 shared_ptr<PlayerVideo>
296 PlayerVideo::shallow_copy () const
297 {
298         return shared_ptr<PlayerVideo>(
299                 new PlayerVideo(
300                         _in,
301                         _crop,
302                         _fade,
303                         _inter_size,
304                         _out_size,
305                         _eyes,
306                         _part,
307                         _colour_conversion,
308                         _content,
309                         _video_frame
310                         )
311                 );
312 }
313
314 /** Re-read crop, fade, inter/out size and colour conversion from our content.
315  *  @return true if this was possible, false if not.
316  */
317 bool
318 PlayerVideo::reset_metadata (dcp::Size video_container_size, dcp::Size film_frame_size)
319 {
320         shared_ptr<Content> content = _content.lock();
321         if (!content || !_video_frame) {
322                 return false;
323         }
324
325         _crop = content->video->crop();
326         _fade = content->video->fade(_video_frame.get());
327         _inter_size = content->video->scale().size(content->video, video_container_size, film_frame_size);
328         _out_size = video_container_size;
329         _colour_conversion = content->video->colour_conversion();
330
331         return true;
332 }