Fix crash when exporting a 2D project containing 3D content (#1680).
[dcpomatic.git] / test / ffmpeg_encoder_test.cc
1 /*
2     Copyright (C) 2017-2019 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 #include "lib/ffmpeg_encoder.h"
22 #include "lib/film.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/image_content.h"
25 #include "lib/video_content.h"
26 #include "lib/audio_content.h"
27 #include "lib/string_text_file_content.h"
28 #include "lib/ratio.h"
29 #include "lib/transcode_job.h"
30 #include "lib/dcp_content.h"
31 #include "lib/text_content.h"
32 #include "lib/compose.hpp"
33 #include "lib/content_factory.h"
34 #include "test.h"
35 #include <boost/test/unit_test.hpp>
36
37 using std::string;
38 using boost::shared_ptr;
39
40 static void
41 ffmpeg_content_test (int number, boost::filesystem::path content, ExportFormat format)
42 {
43         string name = "ffmpeg_encoder_";
44         string extension;
45         switch (format) {
46         case EXPORT_FORMAT_H264:
47                 name += "h264";
48                 extension = "mp4";
49                 break;
50         case EXPORT_FORMAT_PRORES:
51                 name += "prores";
52                 extension = "mov";
53                 break;
54         }
55
56         name = String::compose("%1_test%2", name, number);
57
58         shared_ptr<Film> film = new_test_film (name);
59         film->set_name (name);
60         shared_ptr<FFmpegContent> c (new FFmpegContent(content));
61         film->set_container (Ratio::from_id ("185"));
62         film->set_audio_channels (6);
63
64         film->examine_and_add_content (c);
65         BOOST_REQUIRE (!wait_for_jobs ());
66
67         film->write_metadata ();
68         shared_ptr<Job> job (new TranscodeJob (film));
69         FFmpegEncoder encoder (film, job, String::compose("build/test/%1.%2", name, extension), format, false, false, 23);
70         encoder.go ();
71 }
72
73 /** Red / green / blue MP4 -> Prores */
74 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test1)
75 {
76         ffmpeg_content_test (1, "test/data/test.mp4", EXPORT_FORMAT_PRORES);
77 }
78
79 /** Dolby Aurora trailer VOB -> Prores */
80 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test2)
81 {
82         ffmpeg_content_test (2, private_data / "dolby_aurora.vob", EXPORT_FORMAT_PRORES);
83 }
84
85 /** Sintel trailer -> Prores */
86 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test3)
87 {
88         ffmpeg_content_test (3, private_data / "Sintel_Trailer1.480p.DivX_Plus_HD.mkv", EXPORT_FORMAT_PRORES);
89 }
90
91 /** Big Buck Bunny trailer -> Prores */
92 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test4)
93 {
94         ffmpeg_content_test (4, private_data / "big_buck_bunny_trailer_480p.mov", EXPORT_FORMAT_PRORES);
95 }
96
97 /** Still image -> Prores */
98 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test5)
99 {
100         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test5");
101         film->set_name ("ffmpeg_encoder_prores_test5");
102         shared_ptr<ImageContent> c (new ImageContent(private_data / "bbc405.png"));
103         film->set_container (Ratio::from_id ("185"));
104         film->set_audio_channels (6);
105
106         film->examine_and_add_content (c);
107         BOOST_REQUIRE (!wait_for_jobs ());
108
109         c->video->set_length (240);
110
111         film->write_metadata ();
112         shared_ptr<Job> job (new TranscodeJob (film));
113         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test5.mov", EXPORT_FORMAT_PRORES, false, false, 23);
114         encoder.go ();
115 }
116
117 /** Subs -> Prores */
118 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test6)
119 {
120         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test6");
121         film->set_name ("ffmpeg_encoder_prores_test6");
122         film->set_container (Ratio::from_id ("185"));
123         film->set_audio_channels (6);
124
125         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip2.srt"));
126         film->examine_and_add_content (s);
127         BOOST_REQUIRE (!wait_for_jobs ());
128         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
129         s->only_text()->set_effect (dcp::SHADOW);
130         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
131         film->write_metadata();
132
133         shared_ptr<Job> job (new TranscodeJob (film));
134         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test6.mov", EXPORT_FORMAT_PRORES, false, false, 23);
135         encoder.go ();
136 }
137
138 /** Video + subs -> Prores */
139 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test7)
140 {
141         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test7");
142         film->set_name ("ffmpeg_encoder_prores_test7");
143         film->set_container (Ratio::from_id ("185"));
144         film->set_audio_channels (6);
145
146         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
147         film->examine_and_add_content (c);
148         BOOST_REQUIRE (!wait_for_jobs ());
149
150         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
151         film->examine_and_add_content (s);
152         BOOST_REQUIRE (!wait_for_jobs ());
153         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
154         s->only_text()->set_effect (dcp::SHADOW);
155         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
156
157         shared_ptr<Job> job (new TranscodeJob (film));
158         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test7.mov", EXPORT_FORMAT_PRORES, false, false, 23);
159         encoder.go ();
160 }
161
162 /** Red / green / blue MP4 -> H264 */
163 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test1)
164 {
165         ffmpeg_content_test(1, "test/data/test.mp4", EXPORT_FORMAT_H264);
166 }
167
168 /** Just subtitles -> H264 */
169 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test2)
170 {
171         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test2");
172         film->set_name ("ffmpeg_encoder_h264_test2");
173         film->set_container (Ratio::from_id ("185"));
174         film->set_audio_channels (6);
175
176         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip2.srt"));
177         film->examine_and_add_content (s);
178         BOOST_REQUIRE (!wait_for_jobs ());
179         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
180         s->only_text()->set_effect (dcp::SHADOW);
181         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
182         film->write_metadata();
183
184         shared_ptr<Job> job (new TranscodeJob (film));
185         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test2.mp4", EXPORT_FORMAT_H264, false, false, 23);
186         encoder.go ();
187 }
188
189 /** Video + subs -> H264 */
190 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test3)
191 {
192         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test3");
193         film->set_name ("ffmpeg_encoder_h264_test3");
194         film->set_container (Ratio::from_id ("185"));
195         film->set_audio_channels (6);
196
197         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
198         film->examine_and_add_content (c);
199         BOOST_REQUIRE (!wait_for_jobs ());
200
201         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
202         film->examine_and_add_content (s);
203         BOOST_REQUIRE (!wait_for_jobs ());
204         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
205         s->only_text()->set_effect (dcp::SHADOW);
206         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
207         film->write_metadata();
208
209         shared_ptr<Job> job (new TranscodeJob (film));
210         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test3.mp4", EXPORT_FORMAT_H264, false, false, 23);
211         encoder.go ();
212 }
213
214 /** Scope-in-flat DCP -> H264 */
215 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test4)
216 {
217         shared_ptr<Film> film = new_test_film2("ffmpeg_encoder_h264_test4");
218         film->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent("test/data/scope_dcp")));
219         BOOST_REQUIRE(!wait_for_jobs());
220
221         film->set_container(Ratio::from_id("185"));
222
223         shared_ptr<Job> job(new TranscodeJob(film));
224         FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test4.mp4", EXPORT_FORMAT_H264, false, false, 23);
225         encoder.go();
226 }
227
228 /** Test mixdown from 5.1 to stereo */
229 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test5)
230 {
231         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_h264_test5");
232         film->set_name ("ffmpeg_transcoder_h264_test5");
233         film->set_container (Ratio::from_id ("185"));
234         film->set_audio_channels (6);
235
236         shared_ptr<FFmpegContent> L (new FFmpegContent("test/data/L.wav"));
237         film->examine_and_add_content (L);
238         shared_ptr<FFmpegContent> R (new FFmpegContent("test/data/R.wav"));
239         film->examine_and_add_content (R);
240         shared_ptr<FFmpegContent> C (new FFmpegContent("test/data/C.wav"));
241         film->examine_and_add_content (C);
242         shared_ptr<FFmpegContent> Ls (new FFmpegContent("test/data/Ls.wav"));
243         film->examine_and_add_content (Ls);
244         shared_ptr<FFmpegContent> Rs (new FFmpegContent("test/data/Rs.wav"));
245         film->examine_and_add_content (Rs);
246         shared_ptr<FFmpegContent> Lfe (new FFmpegContent("test/data/Lfe.wav"));
247         film->examine_and_add_content (Lfe);
248         BOOST_REQUIRE (!wait_for_jobs ());
249
250         AudioMapping map (1, MAX_DCP_AUDIO_CHANNELS);
251
252         L->set_position (film, DCPTime::from_seconds(0));
253         map.make_zero ();
254         map.set (0, 0, 1);
255         L->audio->set_mapping (map);
256         R->set_position (film, DCPTime::from_seconds(1));
257         map.make_zero ();
258         map.set (0, 1, 1);
259         R->audio->set_mapping (map);
260         C->set_position (film, DCPTime::from_seconds(2));
261         map.make_zero ();
262         map.set (0, 2, 1);
263         C->audio->set_mapping (map);
264         Lfe->set_position (film, DCPTime::from_seconds(3));
265         map.make_zero ();
266         map.set (0, 3, 1);
267         Lfe->audio->set_mapping (map);
268         Ls->set_position (film, DCPTime::from_seconds(4));
269         map.make_zero ();
270         map.set (0, 4, 1);
271         Ls->audio->set_mapping (map);
272         Rs->set_position (film, DCPTime::from_seconds(5));
273         map.make_zero ();
274         map.set (0, 5, 1);
275         Rs->audio->set_mapping (map);
276
277         shared_ptr<Job> job (new TranscodeJob (film));
278         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test5.mp4", EXPORT_FORMAT_H264, true, false, 23);
279         encoder.go ();
280
281         check_ffmpeg ("build/test/ffmpeg_encoder_h264_test5.mp4", "test/data/ffmpeg_encoder_h264_test5.mp4", 1);
282 }
283
284 /** Test export of a VF */
285 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test6)
286 {
287         shared_ptr<Film> film = new_test_film2 ("ffmpeg_encoder_h264_test6_ov");
288         film->examine_and_add_content (shared_ptr<ImageContent>(new ImageContent(private_data / "bbc405.png")));
289         BOOST_REQUIRE (!wait_for_jobs());
290         film->make_dcp ();
291         BOOST_REQUIRE (!wait_for_jobs());
292
293         shared_ptr<Film> film2 = new_test_film2 ("ffmpeg_encoder_h264_test6_vf");
294         shared_ptr<DCPContent> ov (new DCPContent("build/test/ffmpeg_encoder_h264_test6_ov/" + film->dcp_name(false)));
295         film2->examine_and_add_content (ov);
296         BOOST_REQUIRE (!wait_for_jobs());
297         ov->set_reference_video (true);
298         shared_ptr<Content> subs = content_factory("test/data/subrip.srt").front();
299         film2->examine_and_add_content (subs);
300         BOOST_REQUIRE (!wait_for_jobs());
301         BOOST_FOREACH (shared_ptr<TextContent> i, subs->text) {
302                 i->set_use (true);
303         }
304
305         shared_ptr<Job> job (new TranscodeJob (film2));
306         FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test6_vf.mp4", EXPORT_FORMAT_H264, true, false, 23);
307         encoder.go ();
308 }
309
310 /** Test export of a 3D DCP in a 2D project */
311 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test7)
312 {
313         shared_ptr<Film> film = new_test_film2 ("ffmpeg_encoder_h264_test7_data");
314         shared_ptr<Content> L (shared_ptr<ImageContent>(new ImageContent(private_data / "bbc405.png")));
315         film->examine_and_add_content (L);
316         shared_ptr<Content> R (shared_ptr<ImageContent>(new ImageContent(private_data / "bbc405.png")));
317         film->examine_and_add_content (R);
318         BOOST_REQUIRE (!wait_for_jobs());
319         L->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT);
320         L->set_position (film, DCPTime());
321         R->video->set_frame_type (VIDEO_FRAME_TYPE_3D_RIGHT);
322         R->set_position (film, DCPTime());
323         film->set_three_d (true);
324         film->make_dcp ();
325         BOOST_REQUIRE (!wait_for_jobs());
326
327         shared_ptr<Film> film2 = new_test_film2 ("ffmpeg_encoder_h264_test7_export");
328         shared_ptr<Content> dcp (new DCPContent(film->dir(film->dcp_name())));
329         film2->examine_and_add_content (dcp);
330         BOOST_REQUIRE (!wait_for_jobs());
331
332         shared_ptr<Job> job (new TranscodeJob (film2));
333         FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test7.mp4", EXPORT_FORMAT_H264, true, false, 23
334 #ifdef DCPOMATIC_VARIANT_SWAROOP
335                                , optional<dcp::Key>(), optional<string>()
336 #endif
337                 );
338         encoder.go ();
339 }
340