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