Fix black borders around the preview in some cases (#2331).
[dcpomatic.git] / src / lib / video_content.cc
index 0ef1021161d7a69004494d733da1b83bc02ca046..1d1d010a95662193bc52ab84e5dacb2b8746fb10 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2022 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#include "video_content.h"
-#include "content.h"
-#include "video_examiner.h"
+
+#include "colour_conversion.h"
 #include "compose.hpp"
-#include "ratio.h"
 #include "config.h"
-#include "colour_conversion.h"
-#include "util.h"
-#include "film.h"
+#include "content.h"
+#include "dcpomatic_log.h"
 #include "exceptions.h"
+#include "film.h"
 #include "frame_rate_change.h"
 #include "log.h"
-#include "dcpomatic_log.h"
+#include "ratio.h"
+#include "util.h"
+#include "video_content.h"
+#include "video_examiner.h"
 #include <dcp/raw_convert.h>
 #include <libcxml/cxml.h>
 #include <libxml++/libxml++.h>
@@ -39,6 +40,7 @@
 
 #include "i18n.h"
 
+
 int const VideoContentProperty::USE               = 0;
 int const VideoContentProperty::SIZE              = 1;
 int const VideoContentProperty::FRAME_TYPE        = 2;
@@ -51,23 +53,25 @@ int const VideoContentProperty::CUSTOM_RATIO      = 8;
 int const VideoContentProperty::CUSTOM_SIZE       = 9;
 int const VideoContentProperty::BURNT_SUBTITLE_LANGUAGE = 10;
 
-using std::string;
-using std::setprecision;
+
 using std::cout;
-using std::vector;
-using std::min;
-using std::max;
+using std::dynamic_pointer_cast;
 using std::fixed;
-using std::setprecision;
 using std::list;
+using std::make_shared;
+using std::max;
+using std::min;
 using std::pair;
+using std::setprecision;
+using std::setprecision;
 using std::shared_ptr;
-using std::make_shared;
+using std::string;
+using std::vector;
 using boost::optional;
-using std::dynamic_pointer_cast;
 using dcp::raw_convert;
 using namespace dcpomatic;
 
+
 VideoContent::VideoContent (Content* parent)
        : ContentPart (parent)
        , _use (true)
@@ -81,17 +85,21 @@ VideoContent::VideoContent (Content* parent)
 
 }
 
+
+/** @param video_range_hint Video range to use if none is given in the XML */
 shared_ptr<VideoContent>
-VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
+VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version, VideoRange video_range_hint)
 {
        if (!node->optional_number_child<int> ("VideoWidth")) {
                return {};
        }
 
-       return make_shared<VideoContent>(parent, node, version);
+       return make_shared<VideoContent>(parent, node, version, video_range_hint);
 }
 
-VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
+
+/** @param video_range_hint Video range to use if none is given in the XML */
+VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version, VideoRange video_range_hint)
        : ContentPart (parent)
 {
        _size.width = node->number_child<int> ("VideoWidth");
@@ -173,14 +181,19 @@ VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int versio
        _yuv = node->optional_bool_child("YUV").get_value_or (true);
 
        if (version >= 32) {
+               /* These should be VideoFadeIn and VideoFadeOut but we'll leave them like this until 2.18.x */
                _fade_in = node->number_child<Frame> ("FadeIn");
                _fade_out = node->number_child<Frame> ("FadeOut");
        } else {
                _fade_in = _fade_out = 0;
        }
 
-       _range = VideoRange::FULL;
-       if (node->optional_string_child("Range").get_value_or("full") == "video") {
+       auto video_range = node->optional_string_child("Range");
+       if (!video_range) {
+               _range = video_range_hint;
+       } else if (*video_range == "full") {
+               _range = VideoRange::FULL;
+       } else {
                _range = VideoRange::VIDEO;
        }
 
@@ -516,6 +529,12 @@ VideoContent::set_length (Frame len)
        maybe_set (_length, len, ContentProperty::LENGTH);
 }
 
+void
+VideoContent::set_crop (Crop c)
+{
+       maybe_set (_crop, c, VideoContentProperty::CROP);
+}
+
 void
 VideoContent::set_left_crop (int c)
 {
@@ -641,7 +660,7 @@ VideoContent::scaled_size (dcp::Size film_container)
        }
 
        auto size = size_after_crop ();
-       size.width *= _sample_aspect_ratio.get_value_or(1);
+       size.width = std::lrint(size.width * _sample_aspect_ratio.get_value_or(1));
 
        /* This is what we will return unless there is any legacy stuff to take into account */
        auto auto_size = fit_ratio_within (size.ratio(), film_container);
@@ -655,7 +674,7 @@ VideoContent::scaled_size (dcp::Size film_container)
                _legacy_ratio = boost::optional<float>();
        }
 
-       return auto_size;
+       return _pixel_quanta.round (auto_size);
 }