Move raw_convert into libdcp.
[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 <dcp/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 using dcp::raw_convert;
41
42 PlayerVideo::PlayerVideo (
43         shared_ptr<const ImageProxy> in,
44         DCPTime time,
45         Crop crop,
46         boost::optional<double> fade,
47         dcp::Size inter_size,
48         dcp::Size out_size,
49         Eyes eyes,
50         Part part,
51         optional<ColourConversion> colour_conversion
52         )
53         : _in (in)
54         , _time (time)
55         , _crop (crop)
56         , _fade (fade)
57         , _inter_size (inter_size)
58         , _out_size (out_size)
59         , _eyes (eyes)
60         , _part (part)
61         , _colour_conversion (colour_conversion)
62 {
63
64 }
65
66 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
67 {
68         _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
69         _crop = Crop (node);
70         _fade = node->optional_number_child<double> ("Fade");
71
72         _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
73         _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
74         _eyes = (Eyes) node->number_child<int> ("Eyes");
75         _part = (Part) node->number_child<int> ("Part");
76
77         /* Assume that the ColourConversion uses the current state version */
78         _colour_conversion = ColourConversion::from_xml (node, Film::current_state_version);
79
80         _in = image_proxy_factory (node->node_child ("In"), socket);
81
82         if (node->optional_number_child<int> ("SubtitleX")) {
83
84                 shared_ptr<Image> image (
85                         new Image (AV_PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
86                         );
87
88                 image->read_from_socket (socket);
89
90                 _subtitle = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
91         }
92 }
93
94 void
95 PlayerVideo::set_subtitle (PositionImage image)
96 {
97         _subtitle = image;
98 }
99
100 /** Create an image for this frame.
101  *  @param note Handler for any notes that are made during the process.
102  *  @param pixel_format Function which is called to decide what pixel format the output image should be;
103  *  it is passed the pixel format of the input image from the ImageProxy, and should return the desired
104  *  output pixel format.  Two functions always_rgb and keep_xyz_or_rgb are provided for use here.
105  */
106 shared_ptr<Image>
107 PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
108 {
109         shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note));
110
111         Crop total_crop = _crop;
112         switch (_part) {
113         case PART_LEFT_HALF:
114                 total_crop.right += im->size().width / 2;
115                 break;
116         case PART_RIGHT_HALF:
117                 total_crop.left += im->size().width / 2;
118                 break;
119         case PART_TOP_HALF:
120                 total_crop.bottom += im->size().height / 2;
121                 break;
122         case PART_BOTTOM_HALF:
123                 total_crop.top += im->size().height / 2;
124                 break;
125         default:
126                 break;
127         }
128
129         dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
130         if (_colour_conversion) {
131                 yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
132         }
133
134         shared_ptr<Image> out = im->crop_scale_window (
135                 total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format (_in->pixel_format()), aligned, fast
136                 );
137
138         if (_subtitle) {
139                 out->alpha_blend (_subtitle->image, _subtitle->position);
140         }
141
142         if (_fade) {
143                 out->fade (_fade.get ());
144         }
145
146         return out;
147 }
148
149 void
150 PlayerVideo::add_metadata (xmlpp::Node* node) const
151 {
152         node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
153         _crop.as_xml (node);
154         if (_fade) {
155                 node->add_child("Fade")->add_child_text (raw_convert<string> (_fade.get ()));
156         }
157         _in->add_metadata (node->add_child ("In"));
158         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
159         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
160         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
161         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
162         node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
163         node->add_child("Part")->add_child_text (raw_convert<string> (_part));
164         if (_colour_conversion) {
165                 _colour_conversion.get().as_xml (node);
166         }
167         if (_subtitle) {
168                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle->image->size().width));
169                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle->image->size().height));
170                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle->position.x));
171                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle->position.y));
172         }
173 }
174
175 void
176 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
177 {
178         _in->send_binary (socket);
179         if (_subtitle) {
180                 _subtitle->image->write_to_socket (socket);
181         }
182 }
183
184 bool
185 PlayerVideo::has_j2k () const
186 {
187         /* XXX: maybe other things */
188
189         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
190         if (!j2k) {
191                 return false;
192         }
193
194         return _crop == Crop () && _inter_size == j2k->size() && !_subtitle && !_fade && !_colour_conversion;
195 }
196
197 Data
198 PlayerVideo::j2k () const
199 {
200         shared_ptr<const J2KImageProxy> j2k = dynamic_pointer_cast<const J2KImageProxy> (_in);
201         DCPOMATIC_ASSERT (j2k);
202         return j2k->j2k ();
203 }
204
205 Position<int>
206 PlayerVideo::inter_position () const
207 {
208         return Position<int> ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.height) / 2);
209 }
210
211 /** @return true if this PlayerVideo is definitely the same as another
212  * (apart from _time), false if it is probably not
213  */
214 bool
215 PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
216 {
217         if (_crop != other->_crop ||
218             _fade.get_value_or(0) != other->_fade.get_value_or(0) ||
219             _inter_size != other->_inter_size ||
220             _out_size != other->_out_size ||
221             _eyes != other->_eyes ||
222             _part != other->_part ||
223             _colour_conversion != other->_colour_conversion) {
224                 return false;
225         }
226
227         if ((!_subtitle && other->_subtitle) || (_subtitle && !other->_subtitle)) {
228                 /* One has a subtitle and the other doesn't */
229                 return false;
230         }
231
232         if (_subtitle && other->_subtitle && !_subtitle->same (other->_subtitle.get ())) {
233                 /* They both have subtitles but they are different */
234                 return false;
235         }
236
237         /* Now neither has subtitles */
238
239         return _in->same (other->_in);
240 }
241
242 AVPixelFormat
243 PlayerVideo::always_rgb (AVPixelFormat)
244 {
245         return AV_PIX_FMT_RGB24;
246 }
247
248 AVPixelFormat
249 PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
250 {
251         return p == AV_PIX_FMT_XYZ12LE ? AV_PIX_FMT_XYZ12LE : AV_PIX_FMT_RGB48LE;
252 }