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_film(name, { picture, sub }, &cl);
58 film->set_video_bit_rate(VideoEncoding::JPEG2000, 10000000);
59 picture->set_video_frame_rate(film, content_frame_rate);
60 auto const dcp_frame_rate = film->video_frame_rate();
62 make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME, dcp::VerificationNote::Code::INVALID_PICTURE_FRAME_RATE_FOR_2K });
64 dcp::DCP dcp(path("build/test") / name / film->dcp_name());
66 BOOST_REQUIRE_EQUAL(dcp.cpls().size(), 1U);
67 auto cpl = dcp.cpls()[0];
68 BOOST_REQUIRE_EQUAL(cpl->reels().size(), 1U);
69 auto reel = cpl->reels()[0];
70 BOOST_REQUIRE(reel->main_subtitle());
71 BOOST_REQUIRE(reel->main_subtitle()->asset());
73 auto subs = reel->main_subtitle()->asset()->subtitles();
76 auto error = std::abs(i->in().as_seconds() - (index * content_frame_rate / dcp_frame_rate));
77 BOOST_CHECK (error < (1.0f / dcp_frame_rate));
85 BOOST_AUTO_TEST_CASE(dvb_subtitles_replace_the_last)
87 /* roh.mkv contains subtitles that come out of FFmpeg with incorrect stop times (30s
88 * after the start, which seems to be some kind of DVB "standard" timeout).
89 * Between actual subtitles it contains blanks that are apparently supposed to clear
90 * the previous subtitle. Make sure that happens.
92 auto content = content_factory(TestPaths::private_data() / "roh.mkv");
93 BOOST_REQUIRE(!content.empty());
94 auto film = new_test_film("dvb_subtitles_replace_the_last", { content[0] });
96 FFmpegDecoder decoder(film, dynamic_pointer_cast<FFmpegContent>(content[0]), false);
97 BOOST_REQUIRE(!decoder.text.empty());
101 dcpomatic::ContentTime time;
103 bool operator==(Event const& other) const {
104 return type == other.type && time == other.time;
108 std::vector<Event> events;
110 auto start = [&events](ContentBitmapText text) {
111 events.push_back({"start", text.from()});
114 auto stop = [&events](dcpomatic::ContentTime time) {
115 if (!events.empty() && events.back().type == "stop") {
116 /* We'll get a bad (too-late) stop time, then the correct one
117 * when the "clearing" subtitle arrives.
121 events.push_back({"stop", time});
124 decoder.text.front()->BitmapStart.connect(start);
125 decoder.text.front()->Stop.connect(stop);
127 while (!decoder.pass()) {}
129 using dcpomatic::ContentTime;
131 std::vector<Event> correct = {
132 { "start", ContentTime(439872) }, // 4.582000s actual subtitle #1
133 { "stop", ContentTime(998400) }, // 10.400000s stop caused by incoming blank
134 { "start", ContentTime(998400) }, // 10.400000s blank
135 { "stop", ContentTime(1141248) }, // 11.888000s stop caused by incoming subtitle #2
136 { "start", ContentTime(1141248) }, // 11.888000s subtitle #2
137 { "stop", ContentTime(1455936) }, // 15.166000s ...
138 { "start", ContentTime(1455936) }, // 15.166000s
139 { "stop", ContentTime(1626816) }, // 16.946000s
140 { "start", ContentTime(1626816) }, // 16.946000s
143 BOOST_REQUIRE(events.size() > correct.size());
144 BOOST_CHECK(std::vector<Event>(events.begin(), events.begin() + correct.size()) == correct);