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