Disable YUV->RGB conversion controls with non-YUV sources (#649).
[dcpomatic.git] / src / lib / video_content.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 "video_content.h"
21 #include "video_examiner.h"
22 #include "compose.hpp"
23 #include "ratio.h"
24 #include "config.h"
25 #include "colour_conversion.h"
26 #include "util.h"
27 #include "film.h"
28 #include "exceptions.h"
29 #include "frame_rate_change.h"
30 #include "log.h"
31 #include "safe_stringstream.h"
32 #include "raw_convert.h"
33 #include <libcxml/cxml.h>
34 #include <dcp/colour_matrix.h>
35 #include <libxml++/libxml++.h>
36 #include <iomanip>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 #define LOG_GENERAL(...) film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
42
43 int const VideoContentProperty::VIDEO_SIZE        = 0;
44 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
45 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
46 int const VideoContentProperty::VIDEO_CROP        = 3;
47 int const VideoContentProperty::VIDEO_SCALE       = 4;
48 int const VideoContentProperty::COLOUR_CONVERSION = 5;
49 int const VideoContentProperty::VIDEO_FADE_IN     = 6;
50 int const VideoContentProperty::VIDEO_FADE_OUT    = 7;
51
52 using std::string;
53 using std::setprecision;
54 using std::cout;
55 using std::vector;
56 using std::min;
57 using std::max;
58 using std::stringstream;
59 using std::fixed;
60 using std::setprecision;
61 using std::list;
62 using std::pair;
63 using boost::shared_ptr;
64 using boost::optional;
65 using boost::dynamic_pointer_cast;
66
67 VideoContent::VideoContent (shared_ptr<const Film> film)
68         : Content (film)
69         , _video_length (0)
70         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
71         , _scale (VideoContentScale (Ratio::from_id ("178")))
72         , _yuv (true)
73         , _fade_in (0)
74         , _fade_out (0)
75 {
76         set_default_colour_conversion ();
77 }
78
79 VideoContent::VideoContent (shared_ptr<const Film> film, DCPTime s, Frame len)
80         : Content (film, s)
81         , _video_length (len)
82         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
83         , _scale (VideoContentScale (Ratio::from_id ("178")))
84         , _yuv (true)
85         , _fade_in (0)
86         , _fade_out (0)
87 {
88         set_default_colour_conversion ();
89 }
90
91 VideoContent::VideoContent (shared_ptr<const Film> film, boost::filesystem::path p)
92         : Content (film, p)
93         , _video_length (0)
94         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
95         , _scale (VideoContentScale (Ratio::from_id ("178")))
96         , _yuv (true)
97         , _fade_in (0)
98         , _fade_out (0)
99 {
100         set_default_colour_conversion ();
101 }
102
103 VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
104         : Content (film, node)
105 {
106         _video_size.width = node->number_child<int> ("VideoWidth");
107         _video_size.height = node->number_child<int> ("VideoHeight");
108         _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
109         _video_length = node->number_child<Frame> ("VideoLength");
110         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
111         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
112         _crop.left = node->number_child<int> ("LeftCrop");
113         _crop.right = node->number_child<int> ("RightCrop");
114         _crop.top = node->number_child<int> ("TopCrop");
115         _crop.bottom = node->number_child<int> ("BottomCrop");
116
117         if (version <= 7) {
118                 optional<string> r = node->optional_string_child ("Ratio");
119                 if (r) {
120                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
121                 }
122         } else {
123                 _scale = VideoContentScale (node->node_child ("Scale"));
124         }
125
126
127         if (node->optional_node_child ("ColourConversion")) {
128                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
129         }
130
131         _yuv = node->optional_bool_child("YUV").get_value_or (true);
132
133         if (version >= 32) {
134                 _fade_in = node->number_child<Frame> ("FadeIn");
135                 _fade_out = node->number_child<Frame> ("FadeOut");
136         } else {
137                 _fade_in = _fade_out = 0;
138         }
139 }
140
141 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
142         : Content (film, c)
143         , _video_length (0)
144         , _yuv (false)
145 {
146         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
147         DCPOMATIC_ASSERT (ref);
148
149         for (size_t i = 0; i < c.size(); ++i) {
150                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
151
152                 if (vc->video_size() != ref->video_size()) {
153                         throw JoinError (_("Content to be joined must have the same picture size."));
154                 }
155
156                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
157                         throw JoinError (_("Content to be joined must have the same video frame rate."));
158                 }
159
160                 if (vc->video_frame_type() != ref->video_frame_type()) {
161                         throw JoinError (_("Content to be joined must have the same video frame type."));
162                 }
163
164                 if (vc->crop() != ref->crop()) {
165                         throw JoinError (_("Content to be joined must have the same crop."));
166                 }
167
168                 if (vc->scale() != ref->scale()) {
169                         throw JoinError (_("Content to be joined must have the same scale setting."));
170                 }
171
172                 if (vc->colour_conversion() != ref->colour_conversion()) {
173                         throw JoinError (_("Content to be joined must have the same colour conversion."));
174                 }
175
176                 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
177                         throw JoinError (_("Content to be joined must have the same fades."));
178                 }
179
180                 _video_length += vc->video_length ();
181
182                 if (vc->yuv ()) {
183                         _yuv = true;
184                 }
185         }
186
187         _video_size = ref->video_size ();
188         _video_frame_rate = ref->video_frame_rate ();
189         _video_frame_type = ref->video_frame_type ();
190         _crop = ref->crop ();
191         _scale = ref->scale ();
192         _colour_conversion = ref->colour_conversion ();
193         _fade_in = ref->fade_in ();
194         _fade_out = ref->fade_out ();
195 }
196
197 void
198 VideoContent::as_xml (xmlpp::Node* node) const
199 {
200         boost::mutex::scoped_lock lm (_mutex);
201         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
202         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
203         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
204         if (_video_frame_rate) {
205                 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
206         }
207         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
208         if (_sample_aspect_ratio) {
209                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
210         }
211         _crop.as_xml (node);
212         _scale.as_xml (node->add_child("Scale"));
213         if (_colour_conversion) {
214                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
215         }
216         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
217         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
218         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
219 }
220
221 void
222 VideoContent::set_default_colour_conversion ()
223 {
224         /* If there's no better offer we'll use Rec. 709 */
225         boost::mutex::scoped_lock lm (_mutex);
226         _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
227 }
228
229 void
230 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
231 {
232         /* These examiner calls could call other content methods which take a lock on the mutex */
233         dcp::Size const vs = d->video_size ();
234         optional<double> const vfr = d->video_frame_rate ();
235         Frame vl = d->video_length ();
236         optional<double> const ar = d->sample_aspect_ratio ();
237         bool const yuv = d->yuv ();
238
239         {
240                 boost::mutex::scoped_lock lm (_mutex);
241                 _video_size = vs;
242                 _video_frame_rate = vfr;
243                 _video_length = vl;
244                 _sample_aspect_ratio = ar;
245                 _yuv = yuv;
246
247                 /* Guess correct scale from size and sample aspect ratio */
248                 _scale = VideoContentScale (
249                         Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
250                         );
251         }
252
253         LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
254
255         set_default_colour_conversion ();
256
257         signal_changed (VideoContentProperty::VIDEO_SIZE);
258         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
259         signal_changed (VideoContentProperty::VIDEO_SCALE);
260         signal_changed (ContentProperty::LENGTH);
261 }
262
263 void
264 VideoContent::set_left_crop (int c)
265 {
266         {
267                 boost::mutex::scoped_lock lm (_mutex);
268
269                 if (_crop.left == c) {
270                         return;
271                 }
272
273                 _crop.left = c;
274         }
275
276         signal_changed (VideoContentProperty::VIDEO_CROP);
277 }
278
279 void
280 VideoContent::set_right_crop (int c)
281 {
282         {
283                 boost::mutex::scoped_lock lm (_mutex);
284                 if (_crop.right == c) {
285                         return;
286                 }
287
288                 _crop.right = c;
289         }
290
291         signal_changed (VideoContentProperty::VIDEO_CROP);
292 }
293
294 void
295 VideoContent::set_top_crop (int c)
296 {
297         {
298                 boost::mutex::scoped_lock lm (_mutex);
299                 if (_crop.top == c) {
300                         return;
301                 }
302
303                 _crop.top = c;
304         }
305
306         signal_changed (VideoContentProperty::VIDEO_CROP);
307 }
308
309 void
310 VideoContent::set_bottom_crop (int c)
311 {
312         {
313                 boost::mutex::scoped_lock lm (_mutex);
314                 if (_crop.bottom == c) {
315                         return;
316                 }
317
318                 _crop.bottom = c;
319         }
320
321         signal_changed (VideoContentProperty::VIDEO_CROP);
322 }
323
324 void
325 VideoContent::set_scale (VideoContentScale s)
326 {
327         {
328                 boost::mutex::scoped_lock lm (_mutex);
329                 if (_scale == s) {
330                         return;
331                 }
332
333                 _scale = s;
334         }
335
336         signal_changed (VideoContentProperty::VIDEO_SCALE);
337 }
338
339 /** @return string which includes everything about how this content looks */
340 string
341 VideoContent::identifier () const
342 {
343         SafeStringStream s;
344         s << Content::identifier()
345           << "_" << crop().left
346           << "_" << crop().right
347           << "_" << crop().top
348           << "_" << crop().bottom
349           << "_" << scale().id()
350           << "_" << _fade_in
351           << "_" << _fade_out;
352
353         if (colour_conversion()) {
354                 s << "_" << colour_conversion().get().identifier ();
355         }
356
357         return s.str ();
358 }
359
360 void
361 VideoContent::set_video_frame_type (VideoFrameType t)
362 {
363         {
364                 boost::mutex::scoped_lock lm (_mutex);
365                 _video_frame_type = t;
366         }
367
368         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
369 }
370
371 string
372 VideoContent::technical_summary () const
373 {
374         string s = String::compose (
375                 N_("video: length %1 frames, size %2x%3, rate %4"),
376                 video_length_after_3d_combine(),
377                 video_size().width,
378                 video_size().height,
379                 video_frame_rate()
380                 );
381
382         if (sample_aspect_ratio ()) {
383                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
384         }
385
386         return s;
387 }
388
389 dcp::Size
390 VideoContent::video_size_after_3d_split () const
391 {
392         dcp::Size const s = video_size ();
393         switch (video_frame_type ()) {
394         case VIDEO_FRAME_TYPE_2D:
395         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
396         case VIDEO_FRAME_TYPE_3D_LEFT:
397         case VIDEO_FRAME_TYPE_3D_RIGHT:
398                 return s;
399         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
400                 return dcp::Size (s.width / 2, s.height);
401         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
402                 return dcp::Size (s.width, s.height / 2);
403         }
404
405         DCPOMATIC_ASSERT (false);
406 }
407
408 void
409 VideoContent::unset_colour_conversion ()
410 {
411         {
412                 boost::mutex::scoped_lock lm (_mutex);
413                 _colour_conversion = boost::optional<ColourConversion> ();
414         }
415
416         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
417 }
418
419 void
420 VideoContent::set_colour_conversion (ColourConversion c)
421 {
422         {
423                 boost::mutex::scoped_lock lm (_mutex);
424                 _colour_conversion = c;
425         }
426
427         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
428 }
429
430 void
431 VideoContent::set_fade_in (Frame t)
432 {
433         {
434                 boost::mutex::scoped_lock lm (_mutex);
435                 _fade_in = t;
436         }
437
438         signal_changed (VideoContentProperty::VIDEO_FADE_IN);
439 }
440
441 void
442 VideoContent::set_fade_out (Frame t)
443 {
444         {
445                 boost::mutex::scoped_lock lm (_mutex);
446                 _fade_out = t;
447         }
448
449         signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
450 }
451
452 /** @return Video size after 3D split and crop */
453 dcp::Size
454 VideoContent::video_size_after_crop () const
455 {
456         return crop().apply (video_size_after_3d_split ());
457 }
458
459 void
460 VideoContent::scale_and_crop_to_fit_width ()
461 {
462         set_scale (VideoContentScale (film()->container ()));
463
464         int const crop = max (0, int (video_size().height - double (film()->frame_size().height) * video_size().width / film()->frame_size().width));
465         set_top_crop (crop / 2);
466         set_bottom_crop (crop / 2);
467 }
468
469 void
470 VideoContent::scale_and_crop_to_fit_height ()
471 {
472         set_scale (VideoContentScale (film()->container ()));
473
474         int const crop = max (0, int (video_size().width - double (film()->frame_size().width) * video_size().height / film()->frame_size().height));
475         set_left_crop (crop / 2);
476         set_right_crop (crop / 2);
477 }
478
479 void
480 VideoContent::set_video_frame_rate (double r)
481 {
482         {
483                 boost::mutex::scoped_lock lm (_mutex);
484                 if (_video_frame_rate == r) {
485                         return;
486                 }
487
488                 _video_frame_rate = r;
489         }
490
491         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
492 }
493
494 optional<double>
495 VideoContent::fade (Frame f) const
496 {
497         DCPOMATIC_ASSERT (f >= 0);
498
499         if (f < fade_in()) {
500                 return double (f) / fade_in();
501         }
502
503         Frame fade_out_start = video_length() - fade_out();
504         if (f >= fade_out_start) {
505                 return 1 - double (f - fade_out_start) / fade_out();
506         }
507
508         return optional<double> ();
509 }
510
511 string
512 VideoContent::processing_description () const
513 {
514         /* stringstream is OK here as this string is just for presentation to the user */
515         stringstream d;
516
517         if (video_size().width && video_size().height) {
518                 d << String::compose (
519                         _("Content video is %1x%2"),
520                         video_size_after_3d_split().width,
521                         video_size_after_3d_split().height
522                         );
523
524
525                 double ratio = video_size_after_3d_split().ratio ();
526
527                 if (sample_aspect_ratio ()) {
528                         d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
529                         ratio *= sample_aspect_ratio().get ();
530                 }
531
532                 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
533         }
534
535         if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
536                 dcp::Size cropped = video_size_after_crop ();
537                 d << String::compose (
538                         _("Cropped to %1x%2"),
539                         cropped.width, cropped.height
540                         );
541
542                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
543         }
544
545         dcp::Size const container_size = film()->frame_size ();
546         dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
547
548         if (scaled != video_size_after_crop ()) {
549                 d << String::compose (
550                         _("Scaled to %1x%2"),
551                         scaled.width, scaled.height
552                         );
553
554                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
555         }
556
557         if (scaled != container_size) {
558                 d << String::compose (
559                         _("Padded with black to fit container %1 (%2x%3)"),
560                         film()->container()->nickname (),
561                         container_size.width, container_size.height
562                         );
563
564                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
565         }
566
567         d << _("Content frame rate");
568         d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
569
570         FrameRateChange frc (video_frame_rate(), film()->video_frame_rate ());
571         d << frc.description () << "\n";
572
573         return d.str ();
574 }
575
576 void
577 VideoContent::add_properties (list<pair<string, string> >& p) const
578 {
579         p.push_back (make_pair (_("Video length"), raw_convert<string> (video_length ()) + " " + _("video frames")));
580         p.push_back (make_pair (_("Video size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
581         p.push_back (make_pair (_("Video frame rate"), raw_convert<string> (video_frame_rate()) + " " + _("frames per second")));
582 }
583
584 list<DCPTime>
585 VideoContent::reel_split_points () const
586 {
587         list<DCPTime> t;
588         /* XXX: this is questionable; perhaps the position should be forced to be on a frame boundary */
589         t.push_back (position().round_up (film()->video_frame_rate()));
590         return t;
591 }
592
593 double
594 VideoContent::video_frame_rate () const
595 {
596         boost::mutex::scoped_lock lm (_mutex);
597         return _video_frame_rate.get_value_or (film()->video_frame_rate ());
598 }