Supporters update.
[dcpomatic.git] / test / subtitle_position_test.cc
1 /*
2     Copyright (C) 2022 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/film.h"
25 #include "lib/make_dcp.h"
26 #include "lib/text_content.h"
27 #include "test.h"
28 #include <dcp/interop_subtitle_asset.h>
29 #include <dcp/language_tag.h>
30 #include <dcp/smpte_subtitle_asset.h>
31 #include <boost/test/unit_test.hpp>
32 #include <vector>
33
34
35 using std::shared_ptr;
36 using std::string;
37 using std::vector;
38
39
40 BOOST_AUTO_TEST_CASE(srt_correctly_placed_in_interop)
41 {
42         string const name = "srt_in_interop_position_test";
43         auto fr = content_factory("test/data/short.srt");
44         auto film = new_test_film2(name, fr);
45         fr[0]->only_text()->set_language(dcp::LanguageTag("de"));
46
47         film->set_interop(true);
48
49         make_and_verify_dcp (
50                 film,
51                 {
52                         dcp::VerificationNote::Code::INVALID_STANDARD,
53                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
54                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME
55                 });
56
57         auto output = subtitle_file(film);
58
59         dcp::InteropSubtitleAsset asset(output);
60         auto output_subs = asset.subtitles();
61         BOOST_REQUIRE_EQUAL(output_subs.size(), 1U);
62
63         BOOST_CHECK(output_subs[0]->v_align() == dcp::VAlign::BOTTOM);
64         BOOST_CHECK_CLOSE(output_subs[0]->v_position(), 0.172726989, 1e-3);
65 }
66
67
68 BOOST_AUTO_TEST_CASE(srt_correctly_placed_in_smpte)
69 {
70         string const name = "srt_in_smpte_position_test";
71         auto fr = content_factory("test/data/short.srt");
72         auto film = new_test_film2(name, fr);
73
74         fr[0]->text[0]->set_language(dcp::LanguageTag("en"));
75         film->set_interop(false);
76
77         make_and_verify_dcp (
78                 film,
79                 {
80                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
81                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
82                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME
83                 });
84
85         auto output = subtitle_file(film);
86
87         dcp::SMPTESubtitleAsset asset(output);
88         auto output_subs = asset.subtitles();
89         BOOST_REQUIRE_EQUAL(output_subs.size(), 1U);
90
91         BOOST_CHECK(output_subs[0]->v_align() == dcp::VAlign::BOTTOM);
92         BOOST_CHECK_CLOSE(output_subs[0]->v_position(), 0.172726989, 1e-3);
93 }
94
95
96 /** Make a DCP from some DCP subtitles and check the vertical alignment */
97 static
98 void
99 vpos_test(dcp::VAlign reference, float position, dcp::SubtitleStandard from, dcp::Standard to)
100 {
101         string standard;
102         switch (from) {
103                 case dcp::SubtitleStandard::SMPTE_2007:
104                 case dcp::SubtitleStandard::SMPTE_2010:
105                         standard = "smpte_2010";
106                         break;
107                 case dcp::SubtitleStandard::INTEROP:
108                         standard = "interop";
109                         break;
110                 case dcp::SubtitleStandard::SMPTE_2014:
111                         standard = "smpte_2014";
112                         break;
113         }
114
115         auto name = String::compose("vpos_test_%1_%2", standard, valign_to_string(reference));
116         auto in = content_factory(String::compose("test/data/%1.xml", name));
117         auto film = new_test_film2(name, in);
118
119         film->set_interop(to == dcp::Standard::INTEROP);
120
121         film->write_metadata();
122         make_dcp(film, TranscodeJob::ChangedBehaviour::IGNORE);
123         BOOST_REQUIRE(!wait_for_jobs());
124
125         auto out = subtitle_file(film);
126         vector<shared_ptr<const dcp::Subtitle>> subtitles;
127         if (to == dcp::Standard::INTEROP) {
128                 dcp::InteropSubtitleAsset asset(out);
129                 subtitles = asset.subtitles();
130         } else {
131                 dcp::SMPTESubtitleAsset asset(out);
132                 subtitles = asset.subtitles();
133         }
134
135         BOOST_REQUIRE_EQUAL(subtitles.size(), 1U);
136
137         BOOST_CHECK(subtitles[0]->v_align() == reference);
138         BOOST_CHECK_CLOSE(subtitles[0]->v_position(), position, 2);
139 }
140
141
142 BOOST_AUTO_TEST_CASE(subtitles_correctly_placed_with_all_references)
143 {
144         constexpr auto baseline_to_bottom = 0.00925926;
145         constexpr auto height = 0.0462963;
146
147         /* Interop source */
148         auto from = dcp::SubtitleStandard::INTEROP;
149
150         // -> Interop
151         vpos_test(dcp::VAlign::TOP, 0.2, from, dcp::Standard::INTEROP);
152         vpos_test(dcp::VAlign::CENTER, 0.11, from, dcp::Standard::INTEROP);
153         vpos_test(dcp::VAlign::BOTTOM, 0.08, from, dcp::Standard::INTEROP);
154
155         // -> SMPTE (2014)
156         vpos_test(dcp::VAlign::TOP, 0.2, from, dcp::Standard::SMPTE);
157         vpos_test(dcp::VAlign::CENTER, 0.11, from, dcp::Standard::SMPTE);
158         vpos_test(dcp::VAlign::BOTTOM, 0.08, from, dcp::Standard::SMPTE);
159
160         /* SMPTE 2010 source */
161         from = dcp::SubtitleStandard::SMPTE_2010;
162
163         // -> Interop
164         vpos_test(dcp::VAlign::TOP, 0.1 + height - baseline_to_bottom, from, dcp::Standard::INTEROP);
165         vpos_test(dcp::VAlign::CENTER, 0.15 + (height / 2) - baseline_to_bottom, from, dcp::Standard::INTEROP);
166         vpos_test(dcp::VAlign::BOTTOM, 0.10 + baseline_to_bottom, from, dcp::Standard::INTEROP);
167
168         // -> SMPTE (2014)
169         vpos_test(dcp::VAlign::TOP, 0.1 + height - baseline_to_bottom, from, dcp::Standard::SMPTE);
170         vpos_test(dcp::VAlign::CENTER, 0.15 + (height / 2) - baseline_to_bottom, from, dcp::Standard::SMPTE);
171         vpos_test(dcp::VAlign::BOTTOM, 0.10 + baseline_to_bottom, from, dcp::Standard::SMPTE);
172
173         /* SMPTE 2014 source */
174         from = dcp::SubtitleStandard::SMPTE_2014;
175
176         // -> Interop
177         vpos_test(dcp::VAlign::TOP, 0.2, from, dcp::Standard::INTEROP);
178         vpos_test(dcp::VAlign::CENTER, 0.11, from, dcp::Standard::INTEROP);
179         vpos_test(dcp::VAlign::BOTTOM, 0.08, from, dcp::Standard::INTEROP);
180
181         // -> SMPTE (2014)
182         vpos_test(dcp::VAlign::TOP, 0.2, from, dcp::Standard::SMPTE);
183         vpos_test(dcp::VAlign::CENTER, 0.11, from, dcp::Standard::SMPTE);
184         vpos_test(dcp::VAlign::BOTTOM, 0.08, from, dcp::Standard::SMPTE);
185 }
186