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