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