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