Add fade in/out option to the content audio tab (#1026).
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2021 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 "video_content.h"
22 #include "content.h"
23 #include "video_examiner.h"
24 #include "compose.hpp"
25 #include "ratio.h"
26 #include "config.h"
27 #include "colour_conversion.h"
28 #include "util.h"
29 #include "film.h"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
32 #include "log.h"
33 #include "dcpomatic_log.h"
34 #include <dcp/raw_convert.h>
35 #include <libcxml/cxml.h>
36 #include <libxml++/libxml++.h>
37 #include <iomanip>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 int const VideoContentProperty::USE               = 0;
43 int const VideoContentProperty::SIZE              = 1;
44 int const VideoContentProperty::FRAME_TYPE        = 2;
45 int const VideoContentProperty::CROP              = 3;
46 int const VideoContentProperty::COLOUR_CONVERSION = 4;
47 int const VideoContentProperty::FADE_IN           = 5;
48 int const VideoContentProperty::FADE_OUT          = 6;
49 int const VideoContentProperty::RANGE             = 7;
50 int const VideoContentProperty::CUSTOM_RATIO      = 8;
51 int const VideoContentProperty::CUSTOM_SIZE       = 9;
52 int const VideoContentProperty::BURNT_SUBTITLE_LANGUAGE = 10;
53
54 using std::string;
55 using std::setprecision;
56 using std::cout;
57 using std::vector;
58 using std::min;
59 using std::max;
60 using std::fixed;
61 using std::setprecision;
62 using std::list;
63 using std::pair;
64 using std::shared_ptr;
65 using std::make_shared;
66 using boost::optional;
67 using std::dynamic_pointer_cast;
68 using dcp::raw_convert;
69 using namespace dcpomatic;
70
71 VideoContent::VideoContent (Content* parent)
72         : ContentPart (parent)
73         , _use (true)
74         , _length (0)
75         , _frame_type (VideoFrameType::TWO_D)
76         , _yuv (true)
77         , _fade_in (0)
78         , _fade_out (0)
79         , _range (VideoRange::FULL)
80 {
81
82 }
83
84 shared_ptr<VideoContent>
85 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
86 {
87         if (!node->optional_number_child<int> ("VideoWidth")) {
88                 return {};
89         }
90
91         return make_shared<VideoContent>(parent, node, version);
92 }
93
94 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
95         : ContentPart (parent)
96 {
97         _size.width = node->number_child<int> ("VideoWidth");
98         _size.height = node->number_child<int> ("VideoHeight");
99
100         /* Backwards compatibility */
101         auto r = node->optional_number_child<double>("VideoFrameRate");
102         if (r) {
103                 _parent->set_video_frame_rate (r.get ());
104         }
105
106         _use = node->optional_bool_child("Use").get_value_or(true);
107         _length = node->number_child<Frame> ("VideoLength");
108
109         if (version <= 34) {
110                 /* Snapshot of the VideoFrameType enum at version 34 */
111                 switch (node->number_child<int> ("VideoFrameType")) {
112                 case 0:
113                         _frame_type = VideoFrameType::TWO_D;
114                         break;
115                 case 1:
116                         _frame_type = VideoFrameType::THREE_D_LEFT_RIGHT;
117                         break;
118                 case 2:
119                         _frame_type = VideoFrameType::THREE_D_TOP_BOTTOM;
120                         break;
121                 case 3:
122                         _frame_type = VideoFrameType::THREE_D_ALTERNATE;
123                         break;
124                 case 4:
125                         _frame_type = VideoFrameType::THREE_D_LEFT;
126                         break;
127                 case 5:
128                         _frame_type = VideoFrameType::THREE_D_RIGHT;
129                         break;
130                 }
131         } else {
132                 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
133         }
134
135         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
136         _crop.left = node->number_child<int> ("LeftCrop");
137         _crop.right = node->number_child<int> ("RightCrop");
138         _crop.top = node->number_child<int> ("TopCrop");
139         _crop.bottom = node->number_child<int> ("BottomCrop");
140
141         if (version <= 7) {
142                 auto r = node->optional_string_child ("Ratio");
143                 if (r) {
144                         _legacy_ratio = Ratio::from_id(r.get())->ratio();
145                 }
146         } else if (version <= 37) {
147                 auto ratio = node->node_child("Scale")->optional_string_child("Ratio");
148                 if (ratio) {
149                         _legacy_ratio = Ratio::from_id(ratio.get())->ratio();
150                 }
151                 auto scale = node->node_child("Scale")->optional_bool_child("Scale");
152                 if (scale) {
153                         if (*scale) {
154                                 /* This is what we used to call "no stretch" */
155                                 _legacy_ratio = _size.ratio();
156                         } else {
157                                 /* This is what we used to call "no scale" */
158                                 _custom_size = _size;
159                         }
160                 }
161
162         } else {
163                 _custom_ratio = node->optional_number_child<float>("CustomRatio");
164                 if (node->optional_number_child<int>("CustomWidth")) {
165                         _custom_size = dcp::Size (node->number_child<int>("CustomWidth"), node->number_child<int>("CustomHeight"));
166                 }
167         }
168
169         if (node->optional_node_child ("ColourConversion")) {
170                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
171         }
172
173         _yuv = node->optional_bool_child("YUV").get_value_or (true);
174
175         if (version >= 32) {
176                 /* These should be VideoFadeIn and VideoFadeOut but we'll leave them like this until 2.18.x */
177                 _fade_in = node->number_child<Frame> ("FadeIn");
178                 _fade_out = node->number_child<Frame> ("FadeOut");
179         } else {
180                 _fade_in = _fade_out = 0;
181         }
182
183         _range = VideoRange::FULL;
184         if (node->optional_string_child("Range").get_value_or("full") == "video") {
185                 _range = VideoRange::VIDEO;
186         }
187
188         if (auto pixel_quanta = node->optional_node_child("PixelQuanta")) {
189                 _pixel_quanta = PixelQuanta(pixel_quanta);
190         }
191
192         auto burnt = node->optional_string_child("BurntSubtitleLanguage");
193         if (burnt) {
194                 _burnt_subtitle_language = dcp::LanguageTag (*burnt);
195         }
196
197 }
198
199
200 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
201         : ContentPart (parent)
202         , _length (0)
203         , _yuv (false)
204 {
205         auto ref = c[0]->video;
206         DCPOMATIC_ASSERT (ref);
207
208         for (size_t i = 1; i < c.size(); ++i) {
209
210                 if (c[i]->video->use() != ref->use()) {
211                         throw JoinError (_("Content to be joined must have all its video used or not used."));
212                 }
213
214                 if (c[i]->video->size() != ref->size()) {
215                         throw JoinError (_("Content to be joined must have the same picture size."));
216                 }
217
218                 if (c[i]->video->frame_type() != ref->frame_type()) {
219                         throw JoinError (_("Content to be joined must have the same video frame type."));
220                 }
221
222                 if (c[i]->video->requested_crop() != ref->requested_crop()) {
223                         throw JoinError (_("Content to be joined must have the same crop."));
224                 }
225
226                 if (c[i]->video->custom_ratio() != ref->custom_ratio()) {
227                         throw JoinError (_("Content to be joined must have the same custom ratio setting."));
228                 }
229
230                 if (c[i]->video->custom_size() != ref->custom_size()) {
231                         throw JoinError (_("Content to be joined must have the same custom size setting."));
232                 }
233
234                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
235                         throw JoinError (_("Content to be joined must have the same colour conversion."));
236                 }
237
238                 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
239                         throw JoinError (_("Content to be joined must have the same fades."));
240                 }
241
242                 if (c[i]->video->burnt_subtitle_language() != ref->burnt_subtitle_language()) {
243                         throw JoinError (_("Content to be joined must have the same burnt subtitle language."));
244                 }
245
246                 _length += c[i]->video->length ();
247
248                 if (c[i]->video->yuv ()) {
249                         _yuv = true;
250                 }
251
252                 _pixel_quanta = max(_pixel_quanta, c[i]->video->_pixel_quanta);
253         }
254
255         _use = ref->use ();
256         _size = ref->size ();
257         _frame_type = ref->frame_type ();
258         _crop = ref->requested_crop ();
259         _custom_ratio = ref->custom_ratio ();
260         _colour_conversion = ref->colour_conversion ();
261         _fade_in = ref->fade_in ();
262         _fade_out = ref->fade_out ();
263         _range = ref->range ();
264         _burnt_subtitle_language = ref->burnt_subtitle_language ();
265 }
266
267
268 void
269 VideoContent::as_xml (xmlpp::Node* node) const
270 {
271         boost::mutex::scoped_lock lm (_mutex);
272         node->add_child("Use")->add_child_text (_use ? "1" : "0");
273         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
274         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
275         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
276         node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
277         if (_sample_aspect_ratio) {
278                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
279         }
280         _crop.as_xml (node);
281         if (_custom_ratio) {
282                 node->add_child("CustomRatio")->add_child_text(raw_convert<string>(*_custom_ratio));
283         }
284         if (_custom_size) {
285                 node->add_child("CustomWidth")->add_child_text(raw_convert<string>(_custom_size->width));
286                 node->add_child("CustomHeight")->add_child_text(raw_convert<string>(_custom_size->height));
287         }
288         if (_colour_conversion) {
289                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
290         }
291         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
292         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
293         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
294         node->add_child("Range")->add_child_text(_range == VideoRange::FULL ? "full" : "video");
295         _pixel_quanta.as_xml(node->add_child("PixelQuanta"));
296         if (_burnt_subtitle_language) {
297                 node->add_child("BurntSubtitleLanguage")->add_child_text(_burnt_subtitle_language->to_string());
298         }
299 }
300
301 void
302 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
303 {
304         /* These examiner calls could call other content methods which take a lock on the mutex */
305         auto const vs = d->video_size ();
306         auto vl = d->video_length ();
307         auto const ar = d->sample_aspect_ratio ();
308         auto const yuv = d->yuv ();
309         auto const range = d->range ();
310         auto const pixel_quanta = d->pixel_quanta ();
311
312         ContentChangeSignaller cc1 (_parent, VideoContentProperty::SIZE);
313         ContentChangeSignaller cc2 (_parent, ContentProperty::LENGTH);
314         ContentChangeSignaller cc3 (_parent, VideoContentProperty::RANGE);
315
316         {
317                 boost::mutex::scoped_lock lm (_mutex);
318                 _size = vs;
319                 _length = vl;
320                 _sample_aspect_ratio = ar;
321                 _yuv = yuv;
322                 _range = range;
323                 _pixel_quanta = pixel_quanta;
324         }
325
326         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
327
328         if (d->video_frame_rate()) {
329                 _parent->set_video_frame_rate (d->video_frame_rate().get());
330         }
331 }
332
333 /** @return string which includes everything about how this content looks */
334 string
335 VideoContent::identifier () const
336 {
337         char buffer[256];
338         auto const crop = actual_crop();
339         snprintf (
340                 buffer, sizeof(buffer), "%d_%d_%d_%d_%d_%f_%d_%d%" PRId64 "_%" PRId64 "_%d",
341                 (_use ? 1 : 0),
342                 crop.left,
343                 crop.right,
344                 crop.top,
345                 crop.bottom,
346                 _custom_ratio.get_value_or(0),
347                 _custom_size ? _custom_size->width : 0,
348                 _custom_size ? _custom_size->height : 0,
349                 _fade_in,
350                 _fade_out,
351                 _range == VideoRange::FULL ? 0 : 1
352                 );
353
354         string s (buffer);
355
356         if (colour_conversion()) {
357                 s += "_" + colour_conversion().get().identifier ();
358         }
359
360         return s;
361 }
362
363 string
364 VideoContent::technical_summary () const
365 {
366         string s = String::compose (
367                 N_("video: length %1 frames, size %2x%3"),
368                 length_after_3d_combine(),
369                 size().width,
370                 size().height
371                 );
372
373         if (sample_aspect_ratio ()) {
374                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
375         }
376
377         return s;
378 }
379
380 dcp::Size
381 VideoContent::size_after_3d_split () const
382 {
383         auto const s = size ();
384         switch (frame_type ()) {
385         case VideoFrameType::TWO_D:
386         case VideoFrameType::THREE_D:
387         case VideoFrameType::THREE_D_ALTERNATE:
388         case VideoFrameType::THREE_D_LEFT:
389         case VideoFrameType::THREE_D_RIGHT:
390                 return s;
391         case VideoFrameType::THREE_D_LEFT_RIGHT:
392                 return dcp::Size (s.width / 2, s.height);
393         case VideoFrameType::THREE_D_TOP_BOTTOM:
394                 return dcp::Size (s.width, s.height / 2);
395         }
396
397         DCPOMATIC_ASSERT (false);
398 }
399
400 /** @return Video size after 3D split and crop */
401 dcp::Size
402 VideoContent::size_after_crop () const
403 {
404         return actual_crop().apply(size_after_3d_split());
405 }
406
407
408 /** @param f Frame index within the whole (untrimmed) content.
409  *  @return Fade factor (between 0 and 1) or unset if there is no fade.
410  */
411 optional<double>
412 VideoContent::fade (shared_ptr<const Film> film, Frame f) const
413 {
414         DCPOMATIC_ASSERT (f >= 0);
415
416         double const vfr = _parent->active_video_frame_rate(film);
417
418         auto const ts = _parent->trim_start().frames_round(vfr);
419         if ((f - ts) < fade_in()) {
420                 return double (f - ts) / fade_in();
421         }
422
423         auto fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
424         if (f >= fade_out_start) {
425                 return 1 - double (f - fade_out_start) / fade_out();
426         }
427
428         return optional<double> ();
429 }
430
431 string
432 VideoContent::processing_description (shared_ptr<const Film> film)
433 {
434         string d;
435         char buffer[256];
436
437         if (size().width && size().height) {
438                 d += String::compose (
439                         _("Content video is %1x%2"),
440                         size_after_3d_split().width,
441                         size_after_3d_split().height
442                         );
443
444
445                 double ratio = size_after_3d_split().ratio ();
446
447                 if (sample_aspect_ratio ()) {
448                         snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
449                         d += buffer;
450                         ratio *= sample_aspect_ratio().get ();
451                 }
452
453                 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
454                 d += buffer;
455         }
456
457         auto const crop = actual_crop();
458
459         if ((crop.left || crop.right || crop.top || crop.bottom) && size() != dcp::Size(0, 0)) {
460                 auto const cropped = size_after_crop ();
461                 d += String::compose (
462                         _("\nCropped to %1x%2"),
463                         cropped.width, cropped.height
464                         );
465
466                 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
467                 d += buffer;
468         }
469
470         auto const container_size = film->frame_size ();
471         auto const scaled = scaled_size (container_size);
472
473         if (scaled != size_after_crop ()) {
474                 d += String::compose (
475                         _("\nScaled to %1x%2"),
476                         scaled.width, scaled.height
477                         );
478
479                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
480                 d += buffer;
481         }
482
483         if (scaled != container_size) {
484                 d += String::compose (
485                         _("\nPadded with black to fit container %1 (%2x%3)"),
486                         film->container()->container_nickname (),
487                         container_size.width, container_size.height
488                         );
489
490                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
491                 d += buffer;
492         }
493
494         if (_parent->video_frame_rate()) {
495                 double const vfr = _parent->video_frame_rate().get ();
496
497                 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
498                 d += buffer;
499
500                 FrameRateChange frc (vfr, film->video_frame_rate ());
501                 d += frc.description ();
502         }
503
504         return d;
505 }
506
507 void
508 VideoContent::add_properties (list<UserProperty>& p) const
509 {
510         p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
511         p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
512 }
513
514 void
515 VideoContent::set_length (Frame len)
516 {
517         maybe_set (_length, len, ContentProperty::LENGTH);
518 }
519
520 void
521 VideoContent::set_left_crop (int c)
522 {
523         maybe_set (_crop.left, c, VideoContentProperty::CROP);
524 }
525
526 void
527 VideoContent::set_right_crop (int c)
528 {
529         maybe_set (_crop.right, c, VideoContentProperty::CROP);
530 }
531
532 void
533 VideoContent::set_top_crop (int c)
534 {
535         maybe_set (_crop.top, c, VideoContentProperty::CROP);
536 }
537
538 void
539 VideoContent::set_bottom_crop (int c)
540 {
541         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
542 }
543
544
545 void
546 VideoContent::set_frame_type (VideoFrameType t)
547 {
548         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
549 }
550
551 void
552 VideoContent::unset_colour_conversion ()
553 {
554         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
555 }
556
557 void
558 VideoContent::set_colour_conversion (ColourConversion c)
559 {
560         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
561 }
562
563 void
564 VideoContent::set_fade_in (Frame t)
565 {
566         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
567 }
568
569 void
570 VideoContent::set_fade_out (Frame t)
571 {
572         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
573 }
574
575 void
576 VideoContent::set_range (VideoRange r)
577 {
578         maybe_set (_range, r, VideoContentProperty::RANGE);
579 }
580
581 void
582 VideoContent::set_use (bool u)
583 {
584         maybe_set (_use, u, VideoContentProperty::USE);
585 }
586
587
588 void
589 VideoContent::set_burnt_subtitle_language (boost::optional<dcp::LanguageTag> language)
590 {
591         maybe_set (_burnt_subtitle_language, language, VideoContentProperty::BURNT_SUBTITLE_LANGUAGE);
592 }
593
594
595 void
596 VideoContent::take_settings_from (shared_ptr<const VideoContent> c)
597 {
598         if (c->_colour_conversion) {
599                 set_colour_conversion (c->_colour_conversion.get());
600         } else {
601                 unset_colour_conversion ();
602         }
603         set_use (c->_use);
604         set_frame_type (c->_frame_type);
605         set_left_crop (c->_crop.left);
606         set_right_crop (c->_crop.right);
607         set_top_crop (c->_crop.top);
608         set_bottom_crop (c->_crop.bottom);
609         set_custom_ratio (c->_custom_ratio);
610         set_custom_size (c->_custom_size);
611         set_fade_in (c->_fade_in);
612         set_fade_out (c->_fade_out);
613         set_burnt_subtitle_language (c->_burnt_subtitle_language);
614 }
615
616
617 void
618 VideoContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
619 {
620         pos = pos.round (film->video_frame_rate());
621 }
622
623 void
624 VideoContent::modify_trim_start (ContentTime& trim) const
625 {
626         if (_parent->video_frame_rate()) {
627                 trim = trim.round (_parent->video_frame_rate().get());
628         }
629 }
630
631
632 /** @param film_container The size of the container for the DCP that we are working on */
633 dcp::Size
634 VideoContent::scaled_size (dcp::Size film_container)
635 {
636         if (_custom_ratio) {
637                 return fit_ratio_within(*_custom_ratio, film_container);
638         }
639
640         if (_custom_size) {
641                 return *_custom_size;
642         }
643
644         auto size = size_after_crop ();
645         size.width *= _sample_aspect_ratio.get_value_or(1);
646
647         /* This is what we will return unless there is any legacy stuff to take into account */
648         auto auto_size = fit_ratio_within (size.ratio(), film_container);
649
650         if (_legacy_ratio) {
651                 if (fit_ratio_within(*_legacy_ratio, film_container) != auto_size) {
652                         _custom_ratio = *_legacy_ratio;
653                         _legacy_ratio = optional<float>();
654                         return fit_ratio_within(*_custom_ratio, film_container);
655                 }
656                 _legacy_ratio = boost::optional<float>();
657         }
658
659         return _pixel_quanta.round (auto_size);
660 }
661
662
663 void
664 VideoContent::set_custom_ratio (optional<float> ratio)
665 {
666         maybe_set (_custom_ratio, ratio, VideoContentProperty::CUSTOM_RATIO);
667 }
668
669
670 void
671 VideoContent::set_custom_size (optional<dcp::Size> size)
672 {
673         maybe_set (_custom_size, size, VideoContentProperty::CUSTOM_SIZE);
674 }
675
676
677 Crop
678 VideoContent::actual_crop () const
679 {
680         return Crop(
681                 _pixel_quanta.round_x(_crop.left),
682                 _pixel_quanta.round_x(_crop.right),
683                 _pixel_quanta.round_y(_crop.top),
684                 _pixel_quanta.round_y(_crop.bottom)
685         );
686 }
687