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