Rename alpha_blend_test_one -> alpha_blend_test_bgra_onto.
[dcpomatic.git] / test / burnt_subtitle_test.cc
1 /*
2     Copyright (C) 2014-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 /** @file  test/burnt_subtitle_test.cc
23  *  @brief Test the burning of subtitles into the DCP.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/config.h"
29 #include "lib/content_factory.h"
30 #include "lib/dcp_content.h"
31 #include "lib/dcp_content_type.h"
32 #include "lib/film.h"
33 #include "lib/log_entry.h"
34 #include "lib/ratio.h"
35 #include "lib/text_content.h"
36 #include "test.h"
37 #include <dcp/dcp.h>
38 #include <dcp/cpl.h>
39 #include <dcp/reel.h>
40 #include <dcp/j2k_transcode.h>
41 #include <dcp/mono_picture_asset.h>
42 #include <dcp/mono_picture_asset_reader.h>
43 #include <dcp/mono_picture_frame.h>
44 #include <dcp/openjpeg_image.h>
45 #include <dcp/reel_picture_asset.h>
46 #include <dcp/reel_mono_picture_asset.h>
47 #include <boost/test/unit_test.hpp>
48
49
50 using std::dynamic_pointer_cast;
51 using std::make_shared;
52 using std::map;
53 using std::string;
54 using namespace dcpomatic;
55
56
57 /** Build a small DCP with no picture and a single subtitle overlaid onto it from a SubRip file */
58 BOOST_AUTO_TEST_CASE (burnt_subtitle_test_subrip)
59 {
60         auto film = new_test_film ("burnt_subtitle_test_subrip");
61         film->set_container (Ratio::from_id ("185"));
62         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
63         film->set_name ("frobozz");
64         auto content = content_factory("test/data/subrip2.srt")[0];
65         content->text[0]->set_use(true);
66         content->text[0]->set_burn(true);
67         film->examine_and_add_content (content);
68         BOOST_REQUIRE (!wait_for_jobs());
69         make_and_verify_dcp(
70                 film,
71                 { dcp::VerificationNote::Code::MISSING_CPL_METADATA }
72                 );
73
74 #ifdef DCPOMATIC_WINDOWS
75         check_dcp("test/data/windows/burnt_subtitle_test_subrip", film);
76 #else
77         check_dcp("test/data/burnt_subtitle_test_subrip", film);
78 #endif
79 }
80
81 /** Build a small DCP with no picture and a single subtitle overlaid onto it from a DCP XML file */
82 BOOST_AUTO_TEST_CASE (burnt_subtitle_test_dcp)
83 {
84         auto film = new_test_film ("burnt_subtitle_test_dcp");
85         film->set_container (Ratio::from_id ("185"));
86         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
87         film->set_name ("frobozz");
88         auto content = content_factory("test/data/dcp_sub.xml")[0];
89         content->text[0]->set_use(true);
90         film->examine_and_add_content (content);
91         BOOST_REQUIRE (!wait_for_jobs());
92         make_and_verify_dcp(
93                 film,
94                 {
95                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
96                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
97                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
98                 });
99
100         check_dcp("test/data/burnt_subtitle_test_dcp", film);
101 }
102
103 /** Burn some subtitles into an existing DCP to check the colour conversion */
104 BOOST_AUTO_TEST_CASE (burnt_subtitle_test_onto_dcp)
105 {
106         auto film = new_test_film ("burnt_subtitle_test_onto_dcp");
107         film->set_container (Ratio::from_id ("185"));
108         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
109         film->set_name ("frobozz");
110         film->examine_and_add_content(content_factory("test/data/flat_black.png")[0]);
111         BOOST_REQUIRE (!wait_for_jobs());
112         make_and_verify_dcp (film);
113
114         Config::instance()->set_log_types (Config::instance()->log_types() | LogEntry::TYPE_DEBUG_ENCODE);
115         auto film2 = new_test_film ("burnt_subtitle_test_onto_dcp2");
116         film2->set_container (Ratio::from_id ("185"));
117         film2->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
118         film2->set_name ("frobozz");
119         auto background_dcp = make_shared<DCPContent>(film->dir(film->dcp_name()));
120         film2->examine_and_add_content (background_dcp);
121         auto sub = content_factory("test/data/subrip2.srt")[0];
122         sub->text[0]->set_burn(true);
123         sub->text[0]->set_effect(dcp::Effect::BORDER);
124         film2->examine_and_add_content (sub);
125         BOOST_REQUIRE (!wait_for_jobs());
126         make_and_verify_dcp (film2);
127
128         BOOST_CHECK (background_dcp->position() == DCPTime());
129         BOOST_CHECK (sub->position() == DCPTime());
130
131         dcp::DCP dcp (film2->dir (film2->dcp_name ()));
132         dcp.read ();
133         BOOST_REQUIRE_EQUAL(dcp.cpls().size(), 1U);
134         BOOST_REQUIRE_EQUAL(dcp.cpls().front()->reels().size(), 1U);
135         BOOST_REQUIRE (dcp.cpls().front()->reels().front()->main_picture());
136         BOOST_REQUIRE (dcp.cpls().front()->reels().front()->main_picture()->asset());
137         auto pic = dynamic_pointer_cast<dcp::ReelMonoPictureAsset> (
138                 dcp.cpls().front()->reels().front()->main_picture()
139                 )->mono_asset();
140         BOOST_REQUIRE (pic);
141         auto frame = pic->start_read()->get_frame(12);
142         auto xyz = frame->xyz_image ();
143         BOOST_CHECK_EQUAL (xyz->size().width, 1998);
144         BOOST_CHECK_EQUAL (xyz->size().height, 1080);
145
146 #ifdef DCPOMATIC_WINDOWS
147         check_dcp("test/data/windows/burnt_subtitle_test_onto_dcp2", film2);
148 #else
149         check_dcp("test/data/burnt_subtitle_test_onto_dcp2", film2);
150 #endif
151 }
152
153
154
155 /** Check positioning of some burnt subtitles from XML files */
156 BOOST_AUTO_TEST_CASE(burnt_subtitle_test_position)
157 {
158         auto check = [](string alignment)
159         {
160                 auto const name = String::compose("burnt_subtitle_test_position_%1", alignment);
161                 auto subs = content_factory(String::compose("test/data/burn_%1.xml", alignment));
162                 auto film = new_test_film2(name, subs);
163                 subs[0]->text[0]->set_use(true);
164                 subs[0]->text[0]->set_burn(true);
165                 make_and_verify_dcp(
166                         film,
167                         {
168                                 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
169                                 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
170                                 dcp::VerificationNote::Code::MISSING_CPL_METADATA
171                         });
172
173 #ifdef DCPOMATIC_WINDOWS
174                 check_dcp(String::compose("test/data/windows/%1", name), film);
175 #else
176                 check_dcp(String::compose("test/data/%1", name), film);
177 #endif
178         };
179
180         /* Should have a baseline 216 pixels from the top (0.2 * 1080) */
181         check("top");
182         /* Should have a baseline 756 pixels from the top ((0.5 + 0.2) * 1080) */
183         check("center");
184         /* Should have a baseline 864 pixels from the top ((1 - 0.2) * 1080) */
185         check("bottom");
186 }