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