Clarify some documentation slightly.
[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/ffmpeg_film_encoder.h"
34 #include "lib/log_entry.h"
35 #include "lib/ratio.h"
36 #include "lib/text_content.h"
37 #include "test.h"
38 #include <dcp/dcp.h>
39 #include <dcp/cpl.h>
40 #include <dcp/reel.h>
41 #include <dcp/j2k_transcode.h>
42 #include <dcp/mono_j2k_picture_asset.h>
43 #include <dcp/mono_j2k_picture_asset_reader.h>
44 #include <dcp/mono_j2k_picture_frame.h>
45 #include <dcp/openjpeg_image.h>
46 #include <dcp/reel_picture_asset.h>
47 #include <dcp/reel_mono_picture_asset.h>
48 #include <boost/test/unit_test.hpp>
49
50
51 using std::dynamic_pointer_cast;
52 using std::make_shared;
53 using std::map;
54 using std::string;
55 using namespace dcpomatic;
56
57
58 /** Build a small DCP with no picture and a single subtitle overlaid onto it from a SubRip file */
59 BOOST_AUTO_TEST_CASE (burnt_subtitle_test_subrip)
60 {
61         auto content = content_factory("test/data/subrip2.srt")[0];
62         auto film = new_test_film("burnt_subtitle_test_subrip", { content });
63         film->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
64         content->text[0]->set_use(true);
65         content->text[0]->set_burn(true);
66         make_and_verify_dcp(
67                 film,
68                 { dcp::VerificationNote::Code::MISSING_CPL_METADATA }
69                 );
70
71 #if defined(DCPOMATIC_WINDOWS)
72         check_dcp("test/data/windows/burnt_subtitle_test_subrip", film);
73 #elif defined(DCPOMATIC_OSX)
74         check_dcp("test/data/mac/burnt_subtitle_test_subrip", film);
75 #else
76         check_dcp("test/data/burnt_subtitle_test_subrip", film);
77 #endif
78 }
79
80 /** Build a small DCP with no picture and a single subtitle overlaid onto it from a DCP XML file */
81 BOOST_AUTO_TEST_CASE (burnt_subtitle_test_dcp)
82 {
83         auto content = content_factory("test/data/dcp_sub.xml")[0];
84         auto film = new_test_film("burnt_subtitle_test_dcp", { content });
85         film->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
86         film->set_name("frobozz");
87         content->text[0]->set_use(true);
88         make_and_verify_dcp(
89                 film,
90                 {
91                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
92                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
93                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
94                 });
95
96         check_dcp("test/data/burnt_subtitle_test_dcp", film);
97 }
98
99 /** Burn some subtitles into an existing DCP to check the colour conversion */
100 BOOST_AUTO_TEST_CASE (burnt_subtitle_test_onto_dcp)
101 {
102         auto film = new_test_film("burnt_subtitle_test_onto_dcp", { content_factory("test/data/flat_black.png")[0] });
103         film->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
104         make_and_verify_dcp (film);
105
106         Config::instance()->set_log_types (Config::instance()->log_types() | LogEntry::TYPE_DEBUG_ENCODE);
107         auto background_dcp = make_shared<DCPContent>(film->dir(film->dcp_name()));
108         auto sub = content_factory("test/data/subrip2.srt")[0];
109         auto film2 = new_test_film("burnt_subtitle_test_onto_dcp2", { background_dcp, sub });
110         film2->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
111         film2->set_name("frobozz");
112         sub->text[0]->set_burn(true);
113         sub->text[0]->set_effect(dcp::Effect::BORDER);
114         make_and_verify_dcp (film2);
115
116         BOOST_CHECK (background_dcp->position() == DCPTime());
117         BOOST_CHECK (sub->position() == DCPTime());
118
119         dcp::DCP dcp (film2->dir (film2->dcp_name ()));
120         dcp.read ();
121         BOOST_REQUIRE_EQUAL(dcp.cpls().size(), 1U);
122         BOOST_REQUIRE_EQUAL(dcp.cpls().front()->reels().size(), 1U);
123         BOOST_REQUIRE (dcp.cpls().front()->reels().front()->main_picture());
124         BOOST_REQUIRE (dcp.cpls().front()->reels().front()->main_picture()->asset());
125         auto pic = dynamic_pointer_cast<dcp::ReelMonoPictureAsset> (
126                 dcp.cpls().front()->reels().front()->main_picture()
127                 )->mono_j2k_asset();
128         BOOST_REQUIRE (pic);
129         auto frame = pic->start_read()->get_frame(12);
130         auto xyz = frame->xyz_image ();
131         BOOST_CHECK_EQUAL (xyz->size().width, 1998);
132         BOOST_CHECK_EQUAL (xyz->size().height, 1080);
133
134 #if defined(DCPOMATIC_WINDOWS)
135         check_dcp("test/data/windows/burnt_subtitle_test_onto_dcp2", film2);
136 #elif defined(DCPOMATIC_OSX)
137         check_dcp("test/data/mac/burnt_subtitle_test_onto_dcp2", film2);
138 #else
139         check_dcp("test/data/burnt_subtitle_test_onto_dcp2", film2);
140 #endif
141 }
142
143
144
145 /** Check positioning of some burnt subtitles from XML files */
146 BOOST_AUTO_TEST_CASE(burnt_subtitle_test_position)
147 {
148         auto check = [](string alignment)
149         {
150                 auto const name = String::compose("burnt_subtitle_test_position_%1", alignment);
151                 auto subs = content_factory(String::compose("test/data/burn_%1.xml", alignment));
152                 auto film = new_test_film(name, subs);
153                 subs[0]->text[0]->set_use(true);
154                 subs[0]->text[0]->set_burn(true);
155                 make_and_verify_dcp(
156                         film,
157                         {
158                                 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
159                                 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
160                                 dcp::VerificationNote::Code::MISSING_CPL_METADATA
161                         });
162
163 #if defined(DCPOMATIC_WINDOWS)
164                 check_dcp(String::compose("test/data/windows/%1", name), film);
165 #elif defined(DCPOMATIC_OSX)
166                 check_dcp(String::compose("test/data/mac/%1", name), film);
167 #else
168                 check_dcp(String::compose("test/data/%1", name), film);
169 #endif
170         };
171
172         /* Should have a baseline 216 pixels from the top (0.2 * 1080) */
173         check("top");
174         /* Should have a baseline 756 pixels from the top ((0.5 + 0.2) * 1080) */
175         check("center");
176         /* Should have a baseline 864 pixels from the top ((1 - 0.2) * 1080) */
177         check("bottom");
178 }
179
180
181 /* Bug #2743 */
182 BOOST_AUTO_TEST_CASE(burn_empty_subtitle_test)
183 {
184         Cleanup cl;
185
186         auto content = content_factory("test/data/empty_sub.xml")[0];
187         auto film = new_test_film("burnt_empty_subtitle_test", { content });
188         content->text[0]->set_use(true);
189
190         auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
191         auto file = boost::filesystem::path("build") / "test" / "burnt_empty_subtitle_test.mov";
192         cl.add(file);
193         FFmpegFilmEncoder encoder(film, job, file, ExportFormat::PRORES_4444, false, false, false, 23);
194         encoder.go();
195
196         cl.run();
197 }
198
199