Fix invalid return values from fade() causing various odd effects (#2932).
authorCarl Hetherington <cth@carlh.net>
Fri, 10 Jan 2025 00:35:42 +0000 (01:35 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 10 Jan 2025 01:05:41 +0000 (02:05 +0100)
src/lib/video_content.cc
test/video_content_test.cc [new file with mode: 0644]
test/wscript

index 6177ba7fe314a1c7e1a7711f5547651d24e32e25..392dc40527b897016bffc7783f58cd159dc6c2d9 100644 (file)
@@ -435,16 +435,19 @@ VideoContent::fade(shared_ptr<const Film> film, ContentTime time) const
 
        double const vfr = _parent->active_video_frame_rate(film);
 
-       auto const ts = _parent->trim_start();
+       auto const trim_start = _parent->trim_start();
        auto const fade_in_time = ContentTime::from_frames(fade_in(), vfr);
-       if ((time - ts) < fade_in_time) {
-               return double(ContentTime(time - ts).get()) / fade_in_time.get();
+       /* time after the trimmed start of the content */
+       auto const time_after_start = time - trim_start;
+       if (fade_in_time.get() && time_after_start < fade_in_time) {
+               return std::max(0.0, static_cast<double>(time_after_start.get()) / fade_in_time.get());
        }
 
        auto const fade_out_time = ContentTime::from_frames(fade_out(), vfr);
-       auto fade_out_start = ContentTime::from_frames(length(), vfr) - _parent->trim_end() - fade_out_time;
-       if (time >= fade_out_start) {
-               return 1 - double(ContentTime(time - fade_out_start).get()) / fade_out_time.get();
+       auto const end = ContentTime::from_frames(length(), vfr) - _parent->trim_end();
+       auto const time_after_end_fade_start = time - (end - fade_out_time);
+       if (time_after_end_fade_start > ContentTime()) {
+               return std::max(0.0, 1 - static_cast<double>(time_after_end_fade_start.get()) / fade_out_time.get());
        }
 
        return {};
diff --git a/test/video_content_test.cc b/test/video_content_test.cc
new file mode 100644 (file)
index 0000000..ab5f38b
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+    Copyright (C) 2025 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "lib/content_factory.h"
+#include "lib/dcpomatic_time.h"
+#include "lib/video_content.h"
+#include "test.h"
+#include <boost/test/unit_test.hpp>
+
+
+BOOST_AUTO_TEST_CASE(video_content_fade_test)
+{
+       auto content = content_factory("test/data/flat_red.png")[0];
+       auto film = new_test_film("video_content_fade_test", { content });
+
+       content->video->set_length(240);
+       content->set_trim_start(film, dcpomatic::ContentTime::from_frames(24, 24));
+       content->video->set_fade_in(15);
+       content->video->set_fade_out(4);
+
+       /* Before fade-in */
+       BOOST_CHECK(content->video->fade(film, dcpomatic::ContentTime::from_frames(24 - 12, 24)).get_value_or(-99) == 0);
+       /* Start of fade-in */
+       BOOST_CHECK(content->video->fade(film, dcpomatic::ContentTime::from_frames(24, 24)).get_value_or(-99) == 0);
+       /* During fade-in */
+       BOOST_CHECK(content->video->fade(film, dcpomatic::ContentTime::from_frames(24 + 13, 24)).get_value_or(-99) > 0);
+       BOOST_CHECK(content->video->fade(film, dcpomatic::ContentTime::from_frames(24 + 13, 24)).get_value_or(-99) < 1);
+       /* After fade-in */
+       BOOST_CHECK(!static_cast<bool>(content->video->fade(film, dcpomatic::ContentTime::from_frames(24 + 55, 24))));
+       /* During fade-out */
+       BOOST_CHECK(content->video->fade(film, dcpomatic::ContentTime::from_frames(240 - 16, 24)).get_value_or(-90) <= 1);
+       /* After fade-out */
+       BOOST_CHECK(content->video->fade(film, dcpomatic::ContentTime::from_frames(240 + 20, 24)).get_value_or(-90) >= 0);
+}
+
index 229330265b54f6a69fdc5e04ef973b90865b13e1..1cbcab29fb7f31e3c8c37075e0303900f99eb7be 100644 (file)
@@ -177,6 +177,7 @@ def build(bld):
                  upmixer_a_test.cc
                  util_test.cc
                  vf_test.cc
+                 video_content_test.cc
                  video_content_scale_test.cc
                  video_level_test.cc
                  video_mxf_content_test.cc