Add a test to trigger #1786 - crash when exporting 7.1 project to MP4.
[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 using boost::optional;
40 using namespace dcpomatic;
41
42 static void
43 ffmpeg_content_test (int number, boost::filesystem::path content, ExportFormat format)
44 {
45         string name = "ffmpeg_encoder_";
46         string extension;
47         switch (format) {
48         case EXPORT_FORMAT_H264_AAC:
49                 name += "h264";
50                 extension = "mp4";
51                 break;
52         case EXPORT_FORMAT_PRORES:
53                 name += "prores";
54                 extension = "mov";
55                 break;
56         case EXPORT_FORMAT_H264_PCM:
57         case EXPORT_FORMAT_SUBTITLES_DCP:
58                 BOOST_REQUIRE (false);
59         }
60
61         name = String::compose("%1_test%2", name, number);
62
63         shared_ptr<Film> film = new_test_film (name);
64         film->set_name (name);
65         shared_ptr<FFmpegContent> c (new FFmpegContent(content));
66         film->set_container (Ratio::from_id ("185"));
67         film->set_audio_channels (6);
68
69         film->examine_and_add_content (c);
70         BOOST_REQUIRE (!wait_for_jobs ());
71
72         film->write_metadata ();
73         shared_ptr<Job> job (new TranscodeJob (film));
74         FFmpegEncoder encoder (film, job, String::compose("build/test/%1.%2", name, extension), format, false, false, 23
75 #ifdef DCPOMATIC_VARIANT_SWAROOP
76                                , optional<dcp::Key>(), optional<string>()
77 #endif
78                 );
79         encoder.go ();
80 }
81
82 /** Red / green / blue MP4 -> Prores */
83 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test1)
84 {
85         ffmpeg_content_test (1, "test/data/test.mp4", EXPORT_FORMAT_PRORES);
86 }
87
88 /** Dolby Aurora trailer VOB -> Prores */
89 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test2)
90 {
91         ffmpeg_content_test (2, TestPaths::private_data / "dolby_aurora.vob", EXPORT_FORMAT_PRORES);
92 }
93
94 /** Sintel trailer -> Prores */
95 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test3)
96 {
97         ffmpeg_content_test (3, TestPaths::private_data / "Sintel_Trailer1.480p.DivX_Plus_HD.mkv", EXPORT_FORMAT_PRORES);
98 }
99
100 /** Big Buck Bunny trailer -> Prores */
101 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test4)
102 {
103         ffmpeg_content_test (4, TestPaths::private_data / "big_buck_bunny_trailer_480p.mov", EXPORT_FORMAT_PRORES);
104 }
105
106 /** Still image -> Prores */
107 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test5)
108 {
109         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test5");
110         film->set_name ("ffmpeg_encoder_prores_test5");
111         shared_ptr<ImageContent> c (new ImageContent(TestPaths::private_data / "bbc405.png"));
112         film->set_container (Ratio::from_id ("185"));
113         film->set_audio_channels (6);
114
115         film->examine_and_add_content (c);
116         BOOST_REQUIRE (!wait_for_jobs ());
117
118         c->video->set_length (240);
119
120         film->write_metadata ();
121         shared_ptr<Job> job (new TranscodeJob (film));
122         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test5.mov", EXPORT_FORMAT_PRORES, false, false, 23
123 #ifdef DCPOMATIC_VARIANT_SWAROOP
124                                , optional<dcp::Key>(), optional<string>()
125 #endif
126                 );
127         encoder.go ();
128 }
129
130 /** Subs -> Prores */
131 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test6)
132 {
133         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test6");
134         film->set_name ("ffmpeg_encoder_prores_test6");
135         film->set_container (Ratio::from_id ("185"));
136         film->set_audio_channels (6);
137
138         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip2.srt"));
139         film->examine_and_add_content (s);
140         BOOST_REQUIRE (!wait_for_jobs ());
141         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
142         s->only_text()->set_effect (dcp::SHADOW);
143         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
144         film->write_metadata();
145
146         shared_ptr<Job> job (new TranscodeJob (film));
147         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test6.mov", EXPORT_FORMAT_PRORES, false, false, 23
148 #ifdef DCPOMATIC_VARIANT_SWAROOP
149                                , optional<dcp::Key>(), optional<string>()
150 #endif
151                                          );
152         encoder.go ();
153 }
154
155 /** Video + subs -> Prores */
156 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test7)
157 {
158         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test7");
159         film->set_name ("ffmpeg_encoder_prores_test7");
160         film->set_container (Ratio::from_id ("185"));
161         film->set_audio_channels (6);
162
163         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
164         film->examine_and_add_content (c);
165         BOOST_REQUIRE (!wait_for_jobs ());
166
167         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
168         film->examine_and_add_content (s);
169         BOOST_REQUIRE (!wait_for_jobs ());
170         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
171         s->only_text()->set_effect (dcp::SHADOW);
172         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
173
174         shared_ptr<Job> job (new TranscodeJob (film));
175         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test7.mov", EXPORT_FORMAT_PRORES, false, false, 23
176 #ifdef DCPOMATIC_VARIANT_SWAROOP
177                                , optional<dcp::Key>(), optional<string>()
178 #endif
179                 );
180         encoder.go ();
181 }
182
183 /** Red / green / blue MP4 -> H264 */
184 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test1)
185 {
186         ffmpeg_content_test(1, "test/data/test.mp4", EXPORT_FORMAT_H264_AAC);
187 }
188
189 /** Just subtitles -> H264 */
190 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test2)
191 {
192         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test2");
193         film->set_name ("ffmpeg_encoder_h264_test2");
194         film->set_container (Ratio::from_id ("185"));
195         film->set_audio_channels (6);
196
197         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip2.srt"));
198         film->examine_and_add_content (s);
199         BOOST_REQUIRE (!wait_for_jobs ());
200         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
201         s->only_text()->set_effect (dcp::SHADOW);
202         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
203         film->write_metadata();
204
205         shared_ptr<Job> job (new TranscodeJob (film));
206         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test2.mp4", EXPORT_FORMAT_H264_AAC, false, false, 23
207 #ifdef DCPOMATIC_VARIANT_SWAROOP
208                                , optional<dcp::Key>(), optional<string>()
209 #endif
210                 );
211         encoder.go ();
212 }
213
214 /** Video + subs -> H264 */
215 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test3)
216 {
217         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test3");
218         film->set_name ("ffmpeg_encoder_h264_test3");
219         film->set_container (Ratio::from_id ("185"));
220         film->set_audio_channels (6);
221
222         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
223         film->examine_and_add_content (c);
224         BOOST_REQUIRE (!wait_for_jobs ());
225
226         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
227         film->examine_and_add_content (s);
228         BOOST_REQUIRE (!wait_for_jobs ());
229         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
230         s->only_text()->set_effect (dcp::SHADOW);
231         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
232         film->write_metadata();
233
234         shared_ptr<Job> job (new TranscodeJob (film));
235         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test3.mp4", EXPORT_FORMAT_H264_AAC, false, false, 23
236 #ifdef DCPOMATIC_VARIANT_SWAROOP
237                                , optional<dcp::Key>(), optional<string>()
238 #endif
239                 );
240         encoder.go ();
241 }
242
243 /** Scope-in-flat DCP -> H264 */
244 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test4)
245 {
246         shared_ptr<Film> film = new_test_film2("ffmpeg_encoder_h264_test4");
247         film->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent("test/data/scope_dcp")));
248         BOOST_REQUIRE(!wait_for_jobs());
249
250         film->set_container(Ratio::from_id("185"));
251
252         shared_ptr<Job> job(new TranscodeJob(film));
253         FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test4.mp4", EXPORT_FORMAT_H264_AAC, false, false, 23
254 #ifdef DCPOMATIC_VARIANT_SWAROOP
255                                , optional<dcp::Key>(), optional<string>()
256 #endif
257                 );
258         encoder.go();
259 }
260
261 /** Test mixdown from 5.1 to stereo */
262 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test5)
263 {
264         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_h264_test5");
265         film->set_name ("ffmpeg_transcoder_h264_test5");
266         film->set_container (Ratio::from_id ("185"));
267         film->set_audio_channels (6);
268
269         shared_ptr<FFmpegContent> L (new FFmpegContent("test/data/L.wav"));
270         film->examine_and_add_content (L);
271         shared_ptr<FFmpegContent> R (new FFmpegContent("test/data/R.wav"));
272         film->examine_and_add_content (R);
273         shared_ptr<FFmpegContent> C (new FFmpegContent("test/data/C.wav"));
274         film->examine_and_add_content (C);
275         shared_ptr<FFmpegContent> Ls (new FFmpegContent("test/data/Ls.wav"));
276         film->examine_and_add_content (Ls);
277         shared_ptr<FFmpegContent> Rs (new FFmpegContent("test/data/Rs.wav"));
278         film->examine_and_add_content (Rs);
279         shared_ptr<FFmpegContent> Lfe (new FFmpegContent("test/data/Lfe.wav"));
280         film->examine_and_add_content (Lfe);
281         BOOST_REQUIRE (!wait_for_jobs ());
282
283         AudioMapping map (1, MAX_DCP_AUDIO_CHANNELS);
284
285         L->set_position (film, DCPTime::from_seconds(0));
286         map.make_zero ();
287         map.set (0, 0, 1);
288         L->audio->set_mapping (map);
289         R->set_position (film, DCPTime::from_seconds(1));
290         map.make_zero ();
291         map.set (0, 1, 1);
292         R->audio->set_mapping (map);
293         C->set_position (film, DCPTime::from_seconds(2));
294         map.make_zero ();
295         map.set (0, 2, 1);
296         C->audio->set_mapping (map);
297         Lfe->set_position (film, DCPTime::from_seconds(3));
298         map.make_zero ();
299         map.set (0, 3, 1);
300         Lfe->audio->set_mapping (map);
301         Ls->set_position (film, DCPTime::from_seconds(4));
302         map.make_zero ();
303         map.set (0, 4, 1);
304         Ls->audio->set_mapping (map);
305         Rs->set_position (film, DCPTime::from_seconds(5));
306         map.make_zero ();
307         map.set (0, 5, 1);
308         Rs->audio->set_mapping (map);
309
310         shared_ptr<Job> job (new TranscodeJob (film));
311         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test5.mp4", EXPORT_FORMAT_H264_AAC, true, false, 23
312 #ifdef DCPOMATIC_VARIANT_SWAROOP
313                                , optional<dcp::Key>(), optional<string>()
314 #endif
315                 );
316         encoder.go ();
317
318         check_ffmpeg ("build/test/ffmpeg_encoder_h264_test5.mp4", "test/data/ffmpeg_encoder_h264_test5.mp4", 1);
319 }
320
321 /** Test export of a VF */
322 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test6)
323 {
324         shared_ptr<Film> film = new_test_film2 ("ffmpeg_encoder_h264_test6_ov");
325         film->examine_and_add_content (shared_ptr<ImageContent>(new ImageContent(TestPaths::private_data / "bbc405.png")));
326         BOOST_REQUIRE (!wait_for_jobs());
327         film->make_dcp ();
328         BOOST_REQUIRE (!wait_for_jobs());
329
330         shared_ptr<Film> film2 = new_test_film2 ("ffmpeg_encoder_h264_test6_vf");
331         shared_ptr<DCPContent> ov (new DCPContent("build/test/ffmpeg_encoder_h264_test6_ov/" + film->dcp_name(false)));
332         film2->examine_and_add_content (ov);
333         BOOST_REQUIRE (!wait_for_jobs());
334         ov->set_reference_video (true);
335         shared_ptr<Content> subs = content_factory("test/data/subrip.srt").front();
336         film2->examine_and_add_content (subs);
337         BOOST_REQUIRE (!wait_for_jobs());
338         BOOST_FOREACH (shared_ptr<TextContent> i, subs->text) {
339                 i->set_use (true);
340         }
341
342         shared_ptr<Job> job (new TranscodeJob (film2));
343         FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test6_vf.mp4", EXPORT_FORMAT_H264_AAC, true, false, 23
344 #ifdef DCPOMATIC_VARIANT_SWAROOP
345                                , optional<dcp::Key>(), optional<string>()
346 #endif
347                 );
348         encoder.go ();
349 }
350
351 /** Test export of a 3D DCP in a 2D project */
352 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test7)
353 {
354         shared_ptr<Film> film = new_test_film2 ("ffmpeg_encoder_h264_test7_data");
355         shared_ptr<Content> L (shared_ptr<ImageContent>(new ImageContent(TestPaths::private_data / "bbc405.png")));
356         film->examine_and_add_content (L);
357         shared_ptr<Content> R (shared_ptr<ImageContent>(new ImageContent(TestPaths::private_data / "bbc405.png")));
358         film->examine_and_add_content (R);
359         BOOST_REQUIRE (!wait_for_jobs());
360         L->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT);
361         L->set_position (film, DCPTime());
362         R->video->set_frame_type (VIDEO_FRAME_TYPE_3D_RIGHT);
363         R->set_position (film, DCPTime());
364         film->set_three_d (true);
365         film->make_dcp ();
366         BOOST_REQUIRE (!wait_for_jobs());
367
368         shared_ptr<Film> film2 = new_test_film2 ("ffmpeg_encoder_h264_test7_export");
369         shared_ptr<Content> dcp (new DCPContent(film->dir(film->dcp_name())));
370         film2->examine_and_add_content (dcp);
371         BOOST_REQUIRE (!wait_for_jobs());
372
373         shared_ptr<Job> job (new TranscodeJob (film2));
374         FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test7.mp4", EXPORT_FORMAT_H264_AAC, true, false, 23
375 #ifdef DCPOMATIC_VARIANT_SWAROOP
376                                , optional<dcp::Key>(), optional<string>()
377 #endif
378                 );
379         encoder.go ();
380 }
381
382
383 /** Stereo project with mixdown-to-stereo set */
384 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test8)
385 {
386         shared_ptr<Film> film = new_test_film2("ffmpeg_encoder_h264_test4");
387         film->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent("test/data/scope_dcp")));
388         BOOST_REQUIRE(!wait_for_jobs());
389         film->set_audio_channels (2);
390
391         shared_ptr<Job> job(new TranscodeJob(film));
392         FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test8.mp4", EXPORT_FORMAT_H264_AAC, true, false, 23
393 #ifdef DCPOMATIC_VARIANT_SWAROOP
394                                , optional<dcp::Key>(), optional<string>()
395 #endif
396                 );
397         encoder.go();
398 }
399
400
401 /** 7.1/HI/VI (i.e. 12-channel) project */
402 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test9)
403 {
404         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test9");
405         film->set_name ("ffmpeg_encoder_prores_test9");
406         shared_ptr<ImageContent> c (new ImageContent(TestPaths::private_data / "bbc405.png"));
407         film->set_container (Ratio::from_id ("185"));
408         film->set_audio_channels (12);
409
410         film->examine_and_add_content (c);
411         BOOST_REQUIRE (!wait_for_jobs ());
412
413         c->video->set_length (240);
414
415         film->write_metadata ();
416         shared_ptr<Job> job (new TranscodeJob (film));
417         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test9.mov", EXPORT_FORMAT_H264_AAC, false, false, 23
418 #ifdef DCPOMATIC_VARIANT_SWAROOP
419                                , optional<dcp::Key>(), optional<string>()
420 #endif
421                 );
422         encoder.go ();
423 }