Allow a slightly worrying amount of error so that the Windows tests
[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/smpte_subtitle_asset.h>
30 #include <dcp/language_tag.h>
31 #include <boost/test/unit_test.hpp>
32 #include <vector>
33
34
35 using std::string;
36 using std::vector;
37 using std::shared_ptr;
38
39
40 BOOST_AUTO_TEST_CASE(interop_correctly_placed_in_interop)
41 {
42         string const name = "interop_in_interop_position_test";
43         auto fr = content_factory("test/data/dcp_sub.xml");
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_DURATION
54                 });
55
56         auto output = subtitle_file(film);
57         dcp::InteropSubtitleAsset asset(output);
58         auto output_subs = asset.subtitles();
59         BOOST_REQUIRE_EQUAL(output_subs.size(), 1U);
60
61         /* The input subtitle should have been left alone */
62         BOOST_CHECK(output_subs[0]->v_align() == dcp::VAlign::BOTTOM);
63         BOOST_CHECK_CLOSE(output_subs[0]->v_position(), 0.08, 1e-3);
64 }
65
66
67 BOOST_AUTO_TEST_CASE(interop_correctly_placed_in_smpte)
68 {
69         string const name = "interop_in_smpte_position_test";
70         auto fr = content_factory("test/data/dcp_sub.xml");
71         auto film = new_test_film2(name, fr);
72
73         film->set_interop(false);
74
75         make_and_verify_dcp (
76                 film,
77                 {
78                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
79                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
80                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
81                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME
82                 });
83
84         auto output = subtitle_file(film);
85         dcp::SMPTESubtitleAsset asset(output);
86         auto output_subs = asset.subtitles();
87         BOOST_REQUIRE_EQUAL(output_subs.size(), 1U);
88
89         BOOST_CHECK(output_subs[0]->v_align() == dcp::VAlign::BOTTOM);
90         BOOST_CHECK_CLOSE(output_subs[0]->v_position(), 0.07074, 2);
91 }
92
93
94 BOOST_AUTO_TEST_CASE(smpte_correctly_placed_in_interop)
95 {
96         string const name = "smpte_in_interop_position_test";
97         auto fr = content_factory("test/data/short.srt");
98         auto film = new_test_film2(name, fr);
99
100         film->set_interop(true);
101
102         make_and_verify_dcp (
103                 film,
104                 {
105                         dcp::VerificationNote::Code::INVALID_STANDARD,
106                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
107                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME
108                 });
109
110         auto output = subtitle_file(film);
111         dcp::InteropSubtitleAsset asset(output);
112         auto output_subs = asset.subtitles();
113         BOOST_REQUIRE_EQUAL(output_subs.size(), 1U);
114
115         BOOST_CHECK(output_subs[0]->v_align() == dcp::VAlign::TOP);
116         BOOST_CHECK_CLOSE(output_subs[0]->v_position(), 0.87079, 1e-3);
117 }
118
119
120 static
121 void
122 vpos_test(dcp::VAlign reference, float position, dcp::Standard from, dcp::Standard to)
123 {
124         string standard = from == dcp::Standard::INTEROP ? "interop" : "smpte";
125         auto name = String::compose("vpos_test_%1_%2", standard, valign_to_string(reference));
126         auto in = content_factory(String::compose("test/data/%1.xml", name));
127         auto film = new_test_film2(name, in);
128
129         film->set_interop(to == dcp::Standard::INTEROP);
130
131         film->write_metadata();
132         make_dcp(film, TranscodeJob::ChangedBehaviour::IGNORE);
133         BOOST_REQUIRE(!wait_for_jobs());
134
135         auto out = subtitle_file(film);
136         vector<shared_ptr<const dcp::Subtitle>> subtitles;
137         if (to == dcp::Standard::INTEROP) {
138                 dcp::InteropSubtitleAsset asset(out);
139                 subtitles = asset.subtitles();
140         } else {
141                 dcp::SMPTESubtitleAsset asset(out);
142                 subtitles = asset.subtitles();
143         }
144
145         BOOST_REQUIRE_EQUAL(subtitles.size(), 1U);
146
147         BOOST_CHECK(subtitles[0]->v_align() == reference);
148         BOOST_CHECK_CLOSE(subtitles[0]->v_position(), position, 2);
149 }
150
151
152 BOOST_AUTO_TEST_CASE(subtitles_correctly_placed_with_all_references)
153 {
154         constexpr auto baseline_to_bottom = 0.00925926;
155         constexpr auto height = 0.0462963;
156
157         vpos_test(dcp::VAlign::TOP, 0.2 - height + baseline_to_bottom, dcp::Standard::INTEROP, dcp::Standard::SMPTE);
158         vpos_test(dcp::VAlign::CENTER, 0.11 - (height / 2) + baseline_to_bottom, dcp::Standard::INTEROP, dcp::Standard::SMPTE);
159         vpos_test(dcp::VAlign::BOTTOM, 0.08 - baseline_to_bottom, dcp::Standard::INTEROP, dcp::Standard::SMPTE);
160         vpos_test(dcp::VAlign::TOP, 0.1 + height - baseline_to_bottom, dcp::Standard::SMPTE, dcp::Standard::INTEROP);
161         vpos_test(dcp::VAlign::CENTER, 0.15 + (height / 2) - baseline_to_bottom, dcp::Standard::SMPTE, dcp::Standard::INTEROP);
162         vpos_test(dcp::VAlign::BOTTOM, 0.10 + baseline_to_bottom, dcp::Standard::SMPTE, dcp::Standard::INTEROP);
163 }
164