BOOST_FOREACH.
[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 std::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, false, 23);
75         encoder.go ();
76 }
77
78 /** Red / green / blue MP4 -> Prores */
79 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test1)
80 {
81         ffmpeg_content_test (1, "test/data/test.mp4", EXPORT_FORMAT_PRORES);
82 }
83
84 /** Dolby Aurora trailer VOB -> Prores */
85 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test2)
86 {
87         ffmpeg_content_test (2, TestPaths::private_data() / "dolby_aurora.vob", EXPORT_FORMAT_PRORES);
88 }
89
90 /** Sintel trailer -> Prores */
91 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test3)
92 {
93         ffmpeg_content_test (3, TestPaths::private_data() / "Sintel_Trailer1.480p.DivX_Plus_HD.mkv", EXPORT_FORMAT_PRORES);
94 }
95
96 /** Big Buck Bunny trailer -> Prores */
97 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test4)
98 {
99         ffmpeg_content_test (4, TestPaths::private_data() / "big_buck_bunny_trailer_480p.mov", EXPORT_FORMAT_PRORES);
100 }
101
102 /** Still image -> Prores */
103 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test5)
104 {
105         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test5");
106         film->set_name ("ffmpeg_encoder_prores_test5");
107         shared_ptr<ImageContent> c (new ImageContent(TestPaths::private_data() / "bbc405.png"));
108         film->set_container (Ratio::from_id ("185"));
109         film->set_audio_channels (6);
110
111         film->examine_and_add_content (c);
112         BOOST_REQUIRE (!wait_for_jobs ());
113
114         c->video->set_length (240);
115
116         film->write_metadata ();
117         shared_ptr<Job> job (new TranscodeJob (film));
118         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test5.mov", EXPORT_FORMAT_PRORES, false, false, false, 23);
119         encoder.go ();
120 }
121
122 /** Subs -> Prores */
123 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test6)
124 {
125         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test6");
126         film->set_name ("ffmpeg_encoder_prores_test6");
127         film->set_container (Ratio::from_id ("185"));
128         film->set_audio_channels (6);
129
130         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip2.srt"));
131         film->examine_and_add_content (s);
132         BOOST_REQUIRE (!wait_for_jobs ());
133         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
134         s->only_text()->set_effect (dcp::SHADOW);
135         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
136         film->write_metadata();
137
138         shared_ptr<Job> job (new TranscodeJob (film));
139         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test6.mov", EXPORT_FORMAT_PRORES, false, false, false, 23);
140         encoder.go ();
141 }
142
143 /** Video + subs -> Prores */
144 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test7)
145 {
146         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test7");
147         film->set_name ("ffmpeg_encoder_prores_test7");
148         film->set_container (Ratio::from_id ("185"));
149         film->set_audio_channels (6);
150
151         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
152         film->examine_and_add_content (c);
153         BOOST_REQUIRE (!wait_for_jobs ());
154
155         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
156         film->examine_and_add_content (s);
157         BOOST_REQUIRE (!wait_for_jobs ());
158         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
159         s->only_text()->set_effect (dcp::SHADOW);
160         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
161
162         shared_ptr<Job> job (new TranscodeJob (film));
163         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test7.mov", EXPORT_FORMAT_PRORES, false, false, false, 23);
164         encoder.go ();
165 }
166
167 /** Red / green / blue MP4 -> H264 */
168 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test1)
169 {
170         ffmpeg_content_test(1, "test/data/test.mp4", EXPORT_FORMAT_H264_AAC);
171 }
172
173 /** Just subtitles -> H264 */
174 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test2)
175 {
176         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test2");
177         film->set_name ("ffmpeg_encoder_h264_test2");
178         film->set_container (Ratio::from_id ("185"));
179         film->set_audio_channels (6);
180
181         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip2.srt"));
182         film->examine_and_add_content (s);
183         BOOST_REQUIRE (!wait_for_jobs ());
184         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
185         s->only_text()->set_effect (dcp::SHADOW);
186         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
187         film->write_metadata();
188
189         shared_ptr<Job> job (new TranscodeJob (film));
190         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test2.mp4", EXPORT_FORMAT_H264_AAC, false, false, false, 23);
191         encoder.go ();
192 }
193
194 /** Video + subs -> H264 */
195 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test3)
196 {
197         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test3");
198         film->set_name ("ffmpeg_encoder_h264_test3");
199         film->set_container (Ratio::from_id ("185"));
200         film->set_audio_channels (6);
201
202         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
203         film->examine_and_add_content (c);
204         BOOST_REQUIRE (!wait_for_jobs ());
205
206         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
207         film->examine_and_add_content (s);
208         BOOST_REQUIRE (!wait_for_jobs ());
209         s->only_text()->set_colour (dcp::Colour (255, 255, 0));
210         s->only_text()->set_effect (dcp::SHADOW);
211         s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
212         film->write_metadata();
213
214         shared_ptr<Job> job (new TranscodeJob (film));
215         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test3.mp4", EXPORT_FORMAT_H264_AAC, false, false, false, 23);
216         encoder.go ();
217 }
218
219 /** Scope-in-flat DCP -> H264 */
220 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test4)
221 {
222         shared_ptr<Film> film = new_test_film2("ffmpeg_encoder_h264_test4");
223         film->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent("test/data/scope_dcp")));
224         BOOST_REQUIRE(!wait_for_jobs());
225
226         film->set_container(Ratio::from_id("185"));
227
228         shared_ptr<Job> job(new TranscodeJob(film));
229         FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test4.mp4", EXPORT_FORMAT_H264_AAC, false, false, false, 23);
230         encoder.go();
231 }
232
233 /** Test mixdown from 5.1 to stereo */
234 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test5)
235 {
236         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_h264_test5");
237         film->set_name ("ffmpeg_transcoder_h264_test5");
238         film->set_container (Ratio::from_id ("185"));
239         film->set_audio_channels (6);
240
241         shared_ptr<FFmpegContent> L (new FFmpegContent("test/data/L.wav"));
242         film->examine_and_add_content (L);
243         shared_ptr<FFmpegContent> R (new FFmpegContent("test/data/R.wav"));
244         film->examine_and_add_content (R);
245         shared_ptr<FFmpegContent> C (new FFmpegContent("test/data/C.wav"));
246         film->examine_and_add_content (C);
247         shared_ptr<FFmpegContent> Ls (new FFmpegContent("test/data/Ls.wav"));
248         film->examine_and_add_content (Ls);
249         shared_ptr<FFmpegContent> Rs (new FFmpegContent("test/data/Rs.wav"));
250         film->examine_and_add_content (Rs);
251         shared_ptr<FFmpegContent> Lfe (new FFmpegContent("test/data/Lfe.wav"));
252         film->examine_and_add_content (Lfe);
253         BOOST_REQUIRE (!wait_for_jobs ());
254
255         AudioMapping map (1, MAX_DCP_AUDIO_CHANNELS);
256
257         L->set_position (film, DCPTime::from_seconds(0));
258         map.make_zero ();
259         map.set (0, 0, 1);
260         L->audio->set_mapping (map);
261         R->set_position (film, DCPTime::from_seconds(1));
262         map.make_zero ();
263         map.set (0, 1, 1);
264         R->audio->set_mapping (map);
265         C->set_position (film, DCPTime::from_seconds(2));
266         map.make_zero ();
267         map.set (0, 2, 1);
268         C->audio->set_mapping (map);
269         Lfe->set_position (film, DCPTime::from_seconds(3));
270         map.make_zero ();
271         map.set (0, 3, 1);
272         Lfe->audio->set_mapping (map);
273         Ls->set_position (film, DCPTime::from_seconds(4));
274         map.make_zero ();
275         map.set (0, 4, 1);
276         Ls->audio->set_mapping (map);
277         Rs->set_position (film, DCPTime::from_seconds(5));
278         map.make_zero ();
279         map.set (0, 5, 1);
280         Rs->audio->set_mapping (map);
281
282         shared_ptr<Job> job (new TranscodeJob (film));
283         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test5.mp4", EXPORT_FORMAT_H264_AAC, true, false, false, 23);
284         encoder.go ();
285
286         check_ffmpeg ("build/test/ffmpeg_encoder_h264_test5.mp4", "test/data/ffmpeg_encoder_h264_test5.mp4", 1);
287 }
288
289 /** Test export of a VF */
290 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test6)
291 {
292         shared_ptr<Film> film = new_test_film2 ("ffmpeg_encoder_h264_test6_ov");
293         film->examine_and_add_content (shared_ptr<ImageContent>(new ImageContent(TestPaths::private_data() / "bbc405.png")));
294         BOOST_REQUIRE (!wait_for_jobs());
295         film->make_dcp ();
296         BOOST_REQUIRE (!wait_for_jobs());
297
298         shared_ptr<Film> film2 = new_test_film2 ("ffmpeg_encoder_h264_test6_vf");
299         shared_ptr<DCPContent> ov (new DCPContent("build/test/ffmpeg_encoder_h264_test6_ov/" + film->dcp_name(false)));
300         film2->examine_and_add_content (ov);
301         BOOST_REQUIRE (!wait_for_jobs());
302         ov->set_reference_video (true);
303         shared_ptr<Content> subs = content_factory("test/data/subrip.srt").front();
304         film2->examine_and_add_content (subs);
305         BOOST_REQUIRE (!wait_for_jobs());
306         for (auto i: subs->text) {
307                 i->set_use (true);
308         }
309
310         shared_ptr<Job> job (new TranscodeJob (film2));
311         FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test6_vf.mp4", EXPORT_FORMAT_H264_AAC, true, false, false, 23);
312         encoder.go ();
313 }
314
315 /** Test export of a 3D DCP in a 2D project */
316 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test7)
317 {
318         shared_ptr<Film> film = new_test_film2 ("ffmpeg_encoder_h264_test7_data");
319         shared_ptr<Content> L (shared_ptr<ImageContent>(new ImageContent(TestPaths::private_data() / "bbc405.png")));
320         film->examine_and_add_content (L);
321         shared_ptr<Content> R (shared_ptr<ImageContent>(new ImageContent(TestPaths::private_data() / "bbc405.png")));
322         film->examine_and_add_content (R);
323         BOOST_REQUIRE (!wait_for_jobs());
324         L->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT);
325         L->set_position (film, DCPTime());
326         R->video->set_frame_type (VIDEO_FRAME_TYPE_3D_RIGHT);
327         R->set_position (film, DCPTime());
328         film->set_three_d (true);
329         film->make_dcp ();
330         BOOST_REQUIRE (!wait_for_jobs());
331
332         shared_ptr<Film> film2 = new_test_film2 ("ffmpeg_encoder_h264_test7_export");
333         shared_ptr<Content> dcp (new DCPContent(film->dir(film->dcp_name())));
334         film2->examine_and_add_content (dcp);
335         BOOST_REQUIRE (!wait_for_jobs());
336
337         shared_ptr<Job> job (new TranscodeJob (film2));
338         FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test7.mp4", EXPORT_FORMAT_H264_AAC, true, false, false, 23);
339         encoder.go ();
340 }
341
342
343 /** Stereo project with mixdown-to-stereo set */
344 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test8)
345 {
346         shared_ptr<Film> film = new_test_film2("ffmpeg_encoder_h264_test4");
347         film->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent("test/data/scope_dcp")));
348         BOOST_REQUIRE(!wait_for_jobs());
349         film->set_audio_channels (2);
350
351         shared_ptr<Job> job(new TranscodeJob(film));
352         FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test8.mp4", EXPORT_FORMAT_H264_AAC, true, false, false, 23);
353         encoder.go();
354 }
355
356
357 /** 7.1/HI/VI (i.e. 12-channel) project */
358 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test9)
359 {
360         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test9");
361         film->set_name ("ffmpeg_encoder_prores_test9");
362         shared_ptr<ImageContent> c (new ImageContent(TestPaths::private_data() / "bbc405.png"));
363         film->set_container (Ratio::from_id ("185"));
364         film->set_audio_channels (12);
365
366         film->examine_and_add_content (c);
367         BOOST_REQUIRE (!wait_for_jobs ());
368
369         c->video->set_length (240);
370
371         film->write_metadata ();
372         shared_ptr<Job> job (new TranscodeJob (film));
373         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test9.mov", EXPORT_FORMAT_H264_AAC, false, false, false, 23);
374         encoder.go ();
375 }