2 Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "lib/content.h"
23 #include "lib/content_factory.h"
24 #include "lib/content_text.h"
25 #include "lib/dcpomatic_time.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/ffmpeg_decoder.h"
29 #include "lib/text_content.h"
30 #include "lib/text_decoder.h"
31 #include "lib/video_content.h"
36 #include <dcp/reel_subtitle_asset.h>
37 #include <boost/test/unit_test.hpp>
41 using std::dynamic_pointer_cast;
44 BOOST_AUTO_TEST_CASE (test_subtitle_timing_with_frame_rate_change)
48 using boost::filesystem::path;
50 constexpr auto content_frame_rate = 29.976f;
51 const std::string name = "test_subtitle_timing_with_frame_rate_change";
53 auto picture = content_factory("test/data/flat_red.png")[0];
54 auto sub = content_factory("test/data/hour.srt")[0];
55 sub->text.front()->set_language(dcp::LanguageTag("en"));
57 auto film = new_test_film2(name, { picture, sub }, &cl);
58 picture->set_video_frame_rate(film, content_frame_rate);
59 auto const dcp_frame_rate = film->video_frame_rate();
61 make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME, dcp::VerificationNote::Code::INVALID_PICTURE_FRAME_RATE_FOR_2K });
63 dcp::DCP dcp(path("build/test") / name / film->dcp_name());
65 BOOST_REQUIRE_EQUAL(dcp.cpls().size(), 1U);
66 auto cpl = dcp.cpls()[0];
67 BOOST_REQUIRE_EQUAL(cpl->reels().size(), 1U);
68 auto reel = cpl->reels()[0];
69 BOOST_REQUIRE(reel->main_subtitle());
70 BOOST_REQUIRE(reel->main_subtitle()->asset());
72 auto subs = reel->main_subtitle()->asset()->subtitles();
75 auto error = std::abs(i->in().as_seconds() - (index * content_frame_rate / dcp_frame_rate));
76 BOOST_CHECK (error < (1.0f / dcp_frame_rate));
84 BOOST_AUTO_TEST_CASE(dvb_subtitles_replace_the_last)
86 /* roh.mkv contains subtitles that come out of FFmpeg with incorrect stop times (30s
87 * after the start, which seems to be some kind of DVB "standard" timeout).
88 * Between actual subtitles it contains blanks that are apparently supposed to clear
89 * the previous subtitle. Make sure that happens.
91 auto content = content_factory(TestPaths::private_data() / "roh.mkv");
92 BOOST_REQUIRE(!content.empty());
93 auto film = new_test_film2("dvb_subtitles_replace_the_last", { content[0] });
95 FFmpegDecoder decoder(film, dynamic_pointer_cast<FFmpegContent>(content[0]), false);
96 BOOST_REQUIRE(!decoder.text.empty());
100 dcpomatic::ContentTime time;
102 bool operator==(Event const& other) const {
103 return type == other.type && time == other.time;
107 std::vector<Event> events;
109 auto start = [&events](ContentBitmapText text) {
110 events.push_back({"start", text.from()});
113 auto stop = [&events](dcpomatic::ContentTime time) {
114 if (!events.empty() && events.back().type == "stop") {
115 /* We'll get a bad (too-late) stop time, then the correct one
116 * when the "clearing" subtitle arrives.
120 events.push_back({"stop", time});
123 decoder.text.front()->BitmapStart.connect(start);
124 decoder.text.front()->Stop.connect(stop);
126 while (!decoder.pass()) {}
128 using dcpomatic::ContentTime;
130 std::vector<Event> correct = {
131 { "start", ContentTime(439872) }, // 4.582000s actual subtitle #1
132 { "stop", ContentTime(998400) }, // 10.400000s stop caused by incoming blank
133 { "start", ContentTime(998400) }, // 10.400000s blank
134 { "stop", ContentTime(1141248) }, // 11.888000s stop caused by incoming subtitle #2
135 { "start", ContentTime(1141248) }, // 11.888000s subtitle #2
136 { "stop", ContentTime(1455936) }, // 15.166000s ...
137 { "start", ContentTime(1455936) }, // 15.166000s
138 { "stop", ContentTime(1626816) }, // 16.946000s
139 { "start", ContentTime(1626816) }, // 16.946000s
142 BOOST_REQUIRE(events.size() > correct.size());
143 BOOST_CHECK(std::vector<Event>(events.begin(), events.begin() + correct.size()) == correct);