Move some stuff into FilmViewer::dcp().
[dcpomatic.git] / test / subtitle_timing_test.cc
1 /*
2     Copyright (C) 2021 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
22 #include "lib/content.h"
23 #include "lib/content_factory.h"
24 #include "lib/content_text.h"
25 #include "lib/dcpomatic_time.h"
26 #include "lib/film.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"
32 #include "test.h"
33 #include <dcp/cpl.h>
34 #include <dcp/dcp.h>
35 #include <dcp/reel.h>
36 #include <dcp/reel_subtitle_asset.h>
37 #include <boost/test/unit_test.hpp>
38 #include <iostream>
39
40
41 using std::dynamic_pointer_cast;
42
43
44 BOOST_AUTO_TEST_CASE (test_subtitle_timing_with_frame_rate_change)
45 {
46         Cleanup cl;
47
48         using boost::filesystem::path;
49
50         constexpr auto content_frame_rate = 29.976f;
51         const std::string name = "test_subtitle_timing_with_frame_rate_change";
52
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"));
56
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();
61
62         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME, dcp::VerificationNote::Code::INVALID_PICTURE_FRAME_RATE_FOR_2K });
63
64         dcp::DCP dcp(path("build/test") / name / film->dcp_name());
65         dcp.read();
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());
72
73         auto subs = reel->main_subtitle()->asset()->subtitles();
74         int index = 0;
75         for (auto i: subs) {
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));
78                 ++index;
79         }
80
81         cl.run();
82 }
83
84
85 BOOST_AUTO_TEST_CASE(dvb_subtitles_replace_the_last)
86 {
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.
91          */
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] });
95
96         FFmpegDecoder decoder(film, dynamic_pointer_cast<FFmpegContent>(content[0]), false);
97         BOOST_REQUIRE(!decoder.text.empty());
98
99         struct Event {
100                 std::string type;
101                 dcpomatic::ContentTime time;
102
103                 bool operator==(Event const& other) const {
104                         return type == other.type && time == other.time;
105                 }
106         };
107
108         std::vector<Event> events;
109
110         auto start = [&events](ContentBitmapText text) {
111                 events.push_back({"start", text.from()});
112         };
113
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.
118                          */
119                         events.pop_back();
120                 }
121                 events.push_back({"stop", time});
122         };
123
124         decoder.text.front()->BitmapStart.connect(start);
125         decoder.text.front()->Stop.connect(stop);
126
127         while (!decoder.pass()) {}
128
129         using dcpomatic::ContentTime;
130
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
141         };
142
143         BOOST_REQUIRE(events.size() > correct.size());
144         BOOST_CHECK(std::vector<Event>(events.begin(), events.begin() + correct.size()) == correct);
145 }
146