Add basics of colourspace conversion bypass (#266).
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2014 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 <iomanip>
21 #include <libcxml/cxml.h>
22 #include <libdcp/colour_matrix.h>
23 #include <libdcp/raw_convert.h>
24 #include "video_content.h"
25 #include "video_examiner.h"
26 #include "compose.hpp"
27 #include "ratio.h"
28 #include "config.h"
29 #include "colour_conversion.h"
30 #include "util.h"
31 #include "film.h"
32 #include "exceptions.h"
33 #include "frame_rate_change.h"
34 #include "safe_stringstream.h"
35
36 #include "i18n.h"
37
38 int const VideoContentProperty::VIDEO_SIZE        = 0;
39 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
40 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
41 int const VideoContentProperty::VIDEO_CROP        = 3;
42 int const VideoContentProperty::VIDEO_SCALE       = 4;
43 int const VideoContentProperty::COLOUR_CONVERSION = 5;
44
45 using std::string;
46 using std::setprecision;
47 using std::cout;
48 using std::vector;
49 using std::min;
50 using std::max;
51 using boost::shared_ptr;
52 using boost::optional;
53 using boost::dynamic_pointer_cast;
54 using libdcp::raw_convert;
55
56 VideoContent::VideoContent (shared_ptr<const Film> f)
57         : Content (f)
58         , _video_length (0)
59         , _original_video_frame_rate (0)
60         , _video_frame_rate (0)
61         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
62         , _scale (Config::instance()->default_scale ())
63 {
64         set_default_colour_conversion ();
65 }
66
67 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
68         : Content (f, s)
69         , _video_length (len)
70         , _original_video_frame_rate (0)
71         , _video_frame_rate (0)
72         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
73         , _scale (Config::instance()->default_scale ())
74 {
75         set_default_colour_conversion ();
76 }
77
78 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
79         : Content (f, p)
80         , _video_length (0)
81         , _original_video_frame_rate (0)
82         , _video_frame_rate (0)
83         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
84         , _scale (Config::instance()->default_scale ())
85 {
86         set_default_colour_conversion ();
87 }
88
89 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
90         : Content (f, node)
91 {
92         _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
93         _video_size.width = node->number_child<int> ("VideoWidth");
94         _video_size.height = node->number_child<int> ("VideoHeight");
95         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
96         _original_video_frame_rate = node->optional_number_child<float> ("OriginalVideoFrameRate").get_value_or (_video_frame_rate);
97         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
98         _crop.left = node->number_child<int> ("LeftCrop");
99         _crop.right = node->number_child<int> ("RightCrop");
100         _crop.top = node->number_child<int> ("TopCrop");
101         _crop.bottom = node->number_child<int> ("BottomCrop");
102
103         if (version <= 7) {
104                 optional<string> r = node->optional_string_child ("Ratio");
105                 if (r) {
106                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
107                 }
108         } else {
109                 _scale = VideoContentScale (node->node_child ("Scale"));
110         }
111
112         if (node->optional_node_child ("ColourConversion")) {
113                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
114         }
115 }
116
117 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
118         : Content (f, c)
119         , _video_length (0)
120 {
121         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
122         assert (ref);
123
124         for (size_t i = 0; i < c.size(); ++i) {
125                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
126
127                 if (vc->video_size() != ref->video_size()) {
128                         throw JoinError (_("Content to be joined must have the same picture size."));
129                 }
130
131                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
132                         throw JoinError (_("Content to be joined must have the same video frame rate."));
133                 }
134
135                 if (vc->video_frame_type() != ref->video_frame_type()) {
136                         throw JoinError (_("Content to be joined must have the same video frame type."));
137                 }
138
139                 if (vc->crop() != ref->crop()) {
140                         throw JoinError (_("Content to be joined must have the same crop."));
141                 }
142
143                 if (vc->scale() != ref->scale()) {
144                         throw JoinError (_("Content to be joined must have the same scale setting."));
145                 }
146
147                 if (vc->colour_conversion() != ref->colour_conversion()) {
148                         throw JoinError (_("Content to be joined must have the same colour conversion."));
149                 }
150
151                 _video_length += vc->video_length ();
152         }
153
154         _video_size = ref->video_size ();
155         _original_video_frame_rate = ref->original_video_frame_rate ();
156         _video_frame_rate = ref->video_frame_rate ();
157         _video_frame_type = ref->video_frame_type ();
158         _crop = ref->crop ();
159         _scale = ref->scale ();
160         _colour_conversion = ref->colour_conversion ();
161 }
162
163 void
164 VideoContent::as_xml (xmlpp::Node* node) const
165 {
166         boost::mutex::scoped_lock lm (_mutex);
167         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
168         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
169         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
170         node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
171         node->add_child("OriginalVideoFrameRate")->add_child_text (raw_convert<string> (_original_video_frame_rate));
172         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
173         _crop.as_xml (node);
174         _scale.as_xml (node->add_child("Scale"));
175         if (_colour_conversion) {
176                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
177         }
178 }
179
180 void
181 VideoContent::set_default_colour_conversion ()
182 {
183         set_colour_conversion (PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion);
184 }
185
186 void
187 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
188 {
189         /* These examiner calls could call other content methods which take a lock on the mutex */
190         libdcp::Size const vs = d->video_size ();
191         float const vfr = d->video_frame_rate ();
192         
193         {
194                 boost::mutex::scoped_lock lm (_mutex);
195                 _video_size = vs;
196                 _video_frame_rate = vfr;
197                 _original_video_frame_rate = vfr;
198         }
199         
200         signal_changed (VideoContentProperty::VIDEO_SIZE);
201         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
202 }
203
204
205 string
206 VideoContent::information () const
207 {
208         if (video_size().width == 0 || video_size().height == 0) {
209                 return "";
210         }
211         
212         SafeStringStream s;
213
214         s << String::compose (
215                 _("%1x%2 pixels (%3:1)"),
216                 video_size().width,
217                 video_size().height,
218                 setprecision (3), video_size().ratio ()
219                 );
220         
221         return s.str ();
222 }
223
224 void
225 VideoContent::set_left_crop (int c)
226 {
227         {
228                 boost::mutex::scoped_lock lm (_mutex);
229                 
230                 if (_crop.left == c) {
231                         return;
232                 }
233                 
234                 _crop.left = c;
235         }
236         
237         signal_changed (VideoContentProperty::VIDEO_CROP);
238 }
239
240 void
241 VideoContent::set_right_crop (int c)
242 {
243         {
244                 boost::mutex::scoped_lock lm (_mutex);
245                 if (_crop.right == c) {
246                         return;
247                 }
248                 
249                 _crop.right = c;
250         }
251         
252         signal_changed (VideoContentProperty::VIDEO_CROP);
253 }
254
255 void
256 VideoContent::set_top_crop (int c)
257 {
258         {
259                 boost::mutex::scoped_lock lm (_mutex);
260                 if (_crop.top == c) {
261                         return;
262                 }
263                 
264                 _crop.top = c;
265         }
266         
267         signal_changed (VideoContentProperty::VIDEO_CROP);
268 }
269
270 void
271 VideoContent::set_bottom_crop (int c)
272 {
273         {
274                 boost::mutex::scoped_lock lm (_mutex);
275                 if (_crop.bottom == c) {
276                         return;
277                 }
278                 
279                 _crop.bottom = c;
280         }
281
282         signal_changed (VideoContentProperty::VIDEO_CROP);
283 }
284
285 void
286 VideoContent::set_scale (VideoContentScale s)
287 {
288         {
289                 boost::mutex::scoped_lock lm (_mutex);
290                 if (_scale == s) {
291                         return;
292                 }
293
294                 _scale = s;
295         }
296
297         signal_changed (VideoContentProperty::VIDEO_SCALE);
298 }
299
300 /** @return string which includes everything about how this content looks */
301 string
302 VideoContent::identifier () const
303 {
304         SafeStringStream s;
305         s << Content::identifier()
306           << "_" << crop().left
307           << "_" << crop().right
308           << "_" << crop().top
309           << "_" << crop().bottom
310           << "_" << scale().id();
311
312         if (colour_conversion()) {
313                 s << "_" << colour_conversion().get().identifier ();
314         }
315
316         return s.str ();
317 }
318
319 void
320 VideoContent::set_video_frame_type (VideoFrameType t)
321 {
322         {
323                 boost::mutex::scoped_lock lm (_mutex);
324                 _video_frame_type = t;
325         }
326
327         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
328 }
329
330 string
331 VideoContent::technical_summary () const
332 {
333         return String::compose (
334                 "video: length %1, size %2x%3, rate %4",
335                 video_length_after_3d_combine(), video_size().width, video_size().height, video_frame_rate()
336                 );
337 }
338
339 libdcp::Size
340 VideoContent::video_size_after_3d_split () const
341 {
342         libdcp::Size const s = video_size ();
343         switch (video_frame_type ()) {
344         case VIDEO_FRAME_TYPE_2D:
345         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
346         case VIDEO_FRAME_TYPE_3D_LEFT:
347         case VIDEO_FRAME_TYPE_3D_RIGHT:
348                 return s;
349         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
350                 return libdcp::Size (s.width / 2, s.height);
351         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
352                 return libdcp::Size (s.width, s.height / 2);
353         }
354
355         assert (false);
356 }
357
358 void
359 VideoContent::unset_colour_conversion ()
360 {
361         {
362                 boost::mutex::scoped_lock lm (_mutex);
363                 _colour_conversion = boost::optional<ColourConversion> ();
364         }
365
366         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
367 }
368
369 void
370 VideoContent::set_colour_conversion (ColourConversion c)
371 {
372         {
373                 boost::mutex::scoped_lock lm (_mutex);
374                 _colour_conversion = c;
375         }
376
377         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
378 }
379
380 /** @return Video size after 3D split and crop */
381 libdcp::Size
382 VideoContent::video_size_after_crop () const
383 {
384         return crop().apply (video_size_after_3d_split ());
385 }
386
387 /** @param t A time offset from the start of this piece of content.
388  *  @return Corresponding frame index.
389  */
390 VideoContent::Frame
391 VideoContent::time_to_content_video_frames (Time t) const
392 {
393         shared_ptr<const Film> film = _film.lock ();
394         assert (film);
395         
396         FrameRateChange frc (video_frame_rate(), film->video_frame_rate());
397
398         /* Here we are converting from time (in the DCP) to a frame number in the content.
399            Hence we need to use the DCP's frame rate and the double/skip correction, not
400            the source's rate.
401         */
402         return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
403 }
404
405 void
406 VideoContent::scale_and_crop_to_fit_width ()
407 {
408         shared_ptr<const Film> film = _film.lock ();
409         assert (film);
410
411         set_scale (VideoContentScale (film->container ()));
412
413         int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
414         set_top_crop (crop / 2);
415         set_bottom_crop (crop / 2);
416 }
417
418 void
419 VideoContent::scale_and_crop_to_fit_height ()
420 {
421         shared_ptr<const Film> film = _film.lock ();
422         assert (film);
423
424         set_scale (VideoContentScale (film->container ()));
425
426         int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
427         set_left_crop (crop / 2);
428         set_right_crop (crop / 2);
429 }
430
431 void
432 VideoContent::set_video_frame_rate (float r)
433 {
434         {
435                 boost::mutex::scoped_lock lm (_mutex);
436                 if (_video_frame_rate == r) {
437                         return;
438                 }
439                 
440                 _video_frame_rate = r;
441         }
442         
443         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
444 }
445