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