Fix silent stereo mixdown exports when the project audio channel count is > 6.
[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_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();
60
61         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME, dcp::VerificationNote::Code::INVALID_PICTURE_FRAME_RATE_FOR_2K });
62
63         dcp::DCP dcp(path("build/test") / name / film->dcp_name());
64         dcp.read();
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());
71
72         auto subs = reel->main_subtitle()->asset()->subtitles();
73         int index = 0;
74         for (auto i: subs) {
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));
77                 ++index;
78         }
79
80         cl.run();
81 }
82
83
84 BOOST_AUTO_TEST_CASE(dvb_subtitles_replace_the_last)
85 {
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.
90          */
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] });
94
95         FFmpegDecoder decoder(film, dynamic_pointer_cast<FFmpegContent>(content[0]), false);
96         BOOST_REQUIRE(!decoder.text.empty());
97
98         struct Event {
99                 std::string type;
100                 dcpomatic::ContentTime time;
101
102                 bool operator==(Event const& other) const {
103                         return type == other.type && time == other.time;
104                 }
105         };
106
107         std::vector<Event> events;
108
109         auto start = [&events](ContentBitmapText text) {
110                 events.push_back({"start", text.from()});
111         };
112
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.
117                          */
118                         events.pop_back();
119                 }
120                 events.push_back({"stop", time});
121         };
122
123         decoder.text.front()->BitmapStart.connect(start);
124         decoder.text.front()->Stop.connect(stop);
125
126         while (!decoder.pass()) {}
127
128         using dcpomatic::ContentTime;
129
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
140         };
141
142         BOOST_REQUIRE(events.size() > correct.size());
143         BOOST_CHECK(std::vector<Event>(events.begin(), events.begin() + correct.size()) == correct);
144 }
145