2 Copyright (C) 2017-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "lib/audio_content.h"
23 #include "lib/compose.hpp"
24 #include "lib/config.h"
25 #include "lib/constants.h"
26 #include "lib/content_factory.h"
27 #include "lib/dcp_content.h"
28 #include "lib/dcpomatic_log.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/ffmpeg_encoder.h"
31 #include "lib/ffmpeg_examiner.h"
33 #include "lib/image_content.h"
34 #include "lib/ratio.h"
35 #include "lib/string_text_file_content.h"
36 #include "lib/text_content.h"
37 #include "lib/transcode_job.h"
38 #include "lib/video_content.h"
40 #include <boost/test/unit_test.hpp>
44 using std::shared_ptr;
45 using std::make_shared;
46 using namespace dcpomatic;
50 ffmpeg_content_test (int number, boost::filesystem::path content, ExportFormat format)
54 string name = "ffmpeg_encoder_";
57 case ExportFormat::H264_AAC:
61 case ExportFormat::PRORES_4444:
65 case ExportFormat::PRORES_HQ:
69 case ExportFormat::SUBTITLES_DCP:
70 BOOST_REQUIRE (false);
73 name = String::compose("%1_test%2", name, number);
75 auto c = make_shared<FFmpegContent>(content);
76 auto film = new_test_film2 (name, {c}, &cl);
77 film->set_name (name);
78 film->set_audio_channels (6);
80 film->write_metadata ();
81 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
82 auto file = boost::filesystem::path("build") / "test" / String::compose("%1.%2", name, extension);
84 FFmpegEncoder encoder (film, job, file, format, false, false, false, 23);
91 /** Red / green / blue MP4 -> Prores */
92 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test1)
94 ffmpeg_content_test (1, "test/data/test.mp4", ExportFormat::PRORES_HQ);
98 /** Dolby Aurora trailer VOB -> Prores */
99 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test2)
101 ffmpeg_content_test (2, TestPaths::private_data() / "dolby_aurora.vob", ExportFormat::PRORES_HQ);
105 /** Sintel trailer -> Prores */
106 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test3)
108 ffmpeg_content_test (3, TestPaths::private_data() / "Sintel_Trailer1.480p.DivX_Plus_HD.mkv", ExportFormat::PRORES_HQ);
112 /** Big Buck Bunny trailer -> Prores */
113 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test4)
115 ffmpeg_content_test (4, TestPaths::private_data() / "big_buck_bunny_trailer_480p.mov", ExportFormat::PRORES_HQ);
119 /** Still image -> Prores */
120 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test5)
122 auto film = new_test_film ("ffmpeg_encoder_prores_test5");
123 film->set_name ("ffmpeg_encoder_prores_test5");
124 auto c = make_shared<ImageContent>(TestPaths::private_data() / "bbc405.png");
125 film->set_container (Ratio::from_id ("185"));
126 film->set_audio_channels (6);
128 film->examine_and_add_content (c);
129 BOOST_REQUIRE (!wait_for_jobs ());
131 c->video->set_length (240);
133 film->write_metadata ();
134 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
135 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test5.mov", ExportFormat::PRORES_HQ, false, false, false, 23);
140 /** Subs -> Prores */
141 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test6)
143 auto film = new_test_film ("ffmpeg_encoder_prores_test6");
144 film->set_name ("ffmpeg_encoder_prores_test6");
145 film->set_container (Ratio::from_id ("185"));
146 film->set_audio_channels (6);
148 auto s = make_shared<StringTextFileContent>("test/data/subrip2.srt");
149 film->examine_and_add_content (s);
150 BOOST_REQUIRE (!wait_for_jobs ());
151 s->only_text()->set_colour (dcp::Colour (255, 255, 0));
152 s->only_text()->set_effect (dcp::Effect::SHADOW);
153 s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
154 film->write_metadata();
156 auto job = make_shared<TranscodeJob> (film, TranscodeJob::ChangedBehaviour::IGNORE);
157 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test6.mov", ExportFormat::PRORES_HQ, false, false, false, 23);
162 /** Video + subs -> Prores */
163 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test7)
165 auto film = new_test_film ("ffmpeg_encoder_prores_test7");
166 film->set_name ("ffmpeg_encoder_prores_test7");
167 film->set_container (Ratio::from_id ("185"));
168 film->set_audio_channels (6);
170 auto c = make_shared<FFmpegContent>("test/data/test.mp4");
171 film->examine_and_add_content (c);
172 BOOST_REQUIRE (!wait_for_jobs ());
174 auto s = make_shared<StringTextFileContent>("test/data/subrip.srt");
175 film->examine_and_add_content (s);
176 BOOST_REQUIRE (!wait_for_jobs ());
177 s->only_text()->set_colour (dcp::Colour (255, 255, 0));
178 s->only_text()->set_effect (dcp::Effect::SHADOW);
179 s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
181 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
182 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test7.mov", ExportFormat::PRORES_HQ, false, false, false, 23);
187 /** Red / green / blue MP4 -> H264 */
188 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test1)
190 ffmpeg_content_test(1, "test/data/test.mp4", ExportFormat::H264_AAC);
194 /** Just subtitles -> H264 */
195 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test2)
197 auto film = new_test_film ("ffmpeg_encoder_h264_test2");
198 film->set_name ("ffmpeg_encoder_h264_test2");
199 film->set_container (Ratio::from_id ("185"));
200 film->set_audio_channels (6);
202 auto s = make_shared<StringTextFileContent>("test/data/subrip2.srt");
203 film->examine_and_add_content (s);
204 BOOST_REQUIRE (!wait_for_jobs ());
205 s->only_text()->set_colour (dcp::Colour (255, 255, 0));
206 s->only_text()->set_effect (dcp::Effect::SHADOW);
207 s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
208 film->write_metadata();
210 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
211 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test2.mp4", ExportFormat::H264_AAC, false, false, false, 23);
216 /** Video + subs -> H264 */
217 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test3)
219 auto film = new_test_film ("ffmpeg_encoder_h264_test3");
220 film->set_name ("ffmpeg_encoder_h264_test3");
221 film->set_container (Ratio::from_id ("185"));
222 film->set_audio_channels (6);
224 auto c = make_shared<FFmpegContent>("test/data/test.mp4");
225 film->examine_and_add_content (c);
226 BOOST_REQUIRE (!wait_for_jobs());
228 auto s = make_shared<StringTextFileContent>("test/data/subrip.srt");
229 film->examine_and_add_content (s);
230 BOOST_REQUIRE (!wait_for_jobs ());
231 s->only_text()->set_colour (dcp::Colour (255, 255, 0));
232 s->only_text()->set_effect (dcp::Effect::SHADOW);
233 s->only_text()->set_effect_colour (dcp::Colour (0, 255, 255));
234 film->write_metadata();
236 auto job = make_shared<TranscodeJob> (film, TranscodeJob::ChangedBehaviour::IGNORE);
237 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test3.mp4", ExportFormat::H264_AAC, false, false, false, 23);
242 /** Scope-in-flat DCP -> H264 */
243 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test4)
245 auto film = new_test_film2("ffmpeg_encoder_h264_test4", {make_shared<DCPContent>("test/data/scope_dcp")});
246 BOOST_REQUIRE(!wait_for_jobs());
248 film->set_container(Ratio::from_id("185"));
250 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
251 FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test4.mp4", ExportFormat::H264_AAC, false, false, false, 23);
256 /** Test mixdown from 5.1 to stereo */
257 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test5)
259 auto film = new_test_film ("ffmpeg_transcoder_h264_test5");
260 film->set_name ("ffmpeg_transcoder_h264_test5");
261 film->set_container (Ratio::from_id ("185"));
262 film->set_audio_channels (6);
264 auto L = make_shared<FFmpegContent>("test/data/L.wav");
265 film->examine_and_add_content (L);
266 auto R = make_shared<FFmpegContent>("test/data/R.wav");
267 film->examine_and_add_content (R);
268 auto C = make_shared<FFmpegContent>("test/data/C.wav");
269 film->examine_and_add_content (C);
270 auto Ls = make_shared<FFmpegContent>("test/data/Ls.wav");
271 film->examine_and_add_content (Ls);
272 auto Rs = make_shared<FFmpegContent>("test/data/Rs.wav");
273 film->examine_and_add_content (Rs);
274 auto Lfe = make_shared<FFmpegContent>("test/data/Lfe.wav");
275 film->examine_and_add_content (Lfe);
276 BOOST_REQUIRE (!wait_for_jobs ());
278 AudioMapping map (1, MAX_DCP_AUDIO_CHANNELS);
280 L->set_position (film, DCPTime::from_seconds(0));
283 L->audio->set_mapping (map);
284 R->set_position (film, DCPTime::from_seconds(1));
287 R->audio->set_mapping (map);
288 C->set_position (film, DCPTime::from_seconds(2));
291 C->audio->set_mapping (map);
292 Lfe->set_position (film, DCPTime::from_seconds(3));
295 Lfe->audio->set_mapping (map);
296 Ls->set_position (film, DCPTime::from_seconds(4));
299 Ls->audio->set_mapping (map);
300 Rs->set_position (film, DCPTime::from_seconds(5));
303 Rs->audio->set_mapping (map);
305 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
306 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test5.mp4", ExportFormat::H264_AAC, true, false, false, 23);
309 check_ffmpeg ("build/test/ffmpeg_encoder_h264_test5.mp4", "test/data/ffmpeg_encoder_h264_test5.mp4", 1);
313 /** Test export of a VF */
314 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test6)
316 auto film = new_test_film2 ("ffmpeg_encoder_h264_test6_ov");
317 film->examine_and_add_content (make_shared<ImageContent>(TestPaths::private_data() / "bbc405.png"));
318 BOOST_REQUIRE (!wait_for_jobs());
319 make_and_verify_dcp (film);
321 auto film2 = new_test_film2 ("ffmpeg_encoder_h264_test6_vf");
322 auto ov = make_shared<DCPContent>("build/test/ffmpeg_encoder_h264_test6_ov/" + film->dcp_name(false));
323 film2->examine_and_add_content (ov);
324 BOOST_REQUIRE (!wait_for_jobs());
325 ov->set_reference_video (true);
326 auto subs = content_factory("test/data/subrip.srt")[0];
327 film2->examine_and_add_content (subs);
328 BOOST_REQUIRE (!wait_for_jobs());
329 for (auto i: subs->text) {
333 auto job = make_shared<TranscodeJob>(film2, TranscodeJob::ChangedBehaviour::IGNORE);
334 FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test6_vf.mp4", ExportFormat::H264_AAC, true, false, false, 23);
339 /** Test export of a 3D DCP in a 2D project */
340 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_3d_dcp_to_h264)
342 auto dcp = make_shared<DCPContent>(TestPaths::private_data() / "xm");
343 auto film2 = new_test_film2 ("ffmpeg_encoder_3d_dcp_to_h264_export", {dcp});
345 auto job = make_shared<TranscodeJob>(film2, TranscodeJob::ChangedBehaviour::IGNORE);
346 FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_3d_dcp_to_h264.mp4", ExportFormat::H264_AAC, true, false, false, 23);
351 /** Test export of a 3D DCP in a 2D project */
352 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test7)
354 auto L = make_shared<ImageContent>(TestPaths::private_data() / "bbc405.png");
355 auto R = make_shared<ImageContent>(TestPaths::private_data() / "bbc405.png");
356 auto film = new_test_film2 ("ffmpeg_encoder_h264_test7_data", {L, R});
357 L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
358 L->set_position (film, DCPTime());
359 R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
360 R->set_position (film, DCPTime());
361 film->set_three_d (true);
362 make_and_verify_dcp (film);
364 auto dcp = make_shared<DCPContent>(film->dir(film->dcp_name()));
365 auto film2 = new_test_film2 ("ffmpeg_encoder_h264_test7_export", {dcp});
367 auto job = make_shared<TranscodeJob> (film2, TranscodeJob::ChangedBehaviour::IGNORE);
368 FFmpegEncoder encoder (film2, job, "build/test/ffmpeg_encoder_h264_test7.mp4", ExportFormat::H264_AAC, true, false, false, 23);
373 /** Stereo project with mixdown-to-stereo set */
374 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test8)
376 auto film = new_test_film2("ffmpeg_encoder_h264_test4");
377 film->examine_and_add_content(make_shared<DCPContent>("test/data/scope_dcp"));
378 BOOST_REQUIRE(!wait_for_jobs());
379 film->set_audio_channels (2);
381 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
382 FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test8.mp4", ExportFormat::H264_AAC, true, false, false, 23);
387 /** 7.1/HI/VI (i.e. 12-channel) project */
388 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test9)
390 shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test9");
391 film->set_name ("ffmpeg_encoder_prores_test9");
392 auto c = make_shared<ImageContent>(TestPaths::private_data() / "bbc405.png");
393 film->set_container (Ratio::from_id ("185"));
394 film->set_audio_channels (12);
396 film->examine_and_add_content (c);
397 BOOST_REQUIRE (!wait_for_jobs ());
399 c->video->set_length (240);
401 film->write_metadata ();
402 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
403 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test9.mov", ExportFormat::H264_AAC, false, false, false, 23);
408 /** DCP -> Prores with crop */
409 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_from_dcp_with_crop)
411 auto dcp = make_shared<DCPContent>("test/data/import_dcp_test2");
412 auto film = new_test_film2 ("ffmpeg_encoder_prores_from_dcp_with_crop", { dcp });
413 dcp->video->set_left_crop (32);
414 dcp->video->set_right_crop (32);
415 film->write_metadata ();
417 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
418 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_from_dcp_with_crop.mov", ExportFormat::PRORES_HQ, false, false, false, 23);
423 /** DCP -> H264 with crop */
424 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_from_dcp_with_crop)
426 auto dcp = make_shared<DCPContent>("test/data/import_dcp_test2");
427 auto film = new_test_film2 ("ffmpeg_encoder_h264_from_dcp_with_crop", { dcp });
428 dcp->video->set_left_crop (32);
429 dcp->video->set_right_crop (32);
430 film->write_metadata ();
432 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
433 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_from_dcp_with_crop.mov", ExportFormat::H264_AAC, false, false, false, 23);
438 /** Export to H264 with reels */
439 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_with_reels)
441 auto content1 = content_factory("test/data/flat_red.png")[0];
442 auto content2 = content_factory("test/data/flat_red.png")[0];
443 auto film = new_test_film2 ("ffmpeg_encoder_h264_with_reels", { content1, content2 });
444 film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
445 content1->video->set_length (240);
446 content2->video->set_length (240);
448 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
449 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_with_reels.mov", ExportFormat::H264_AAC, false, true, false, 23);
452 auto check = [](boost::filesystem::path path) {
453 auto reel = std::dynamic_pointer_cast<FFmpegContent>(content_factory(path)[0]);
454 BOOST_REQUIRE (reel);
455 FFmpegExaminer examiner(reel);
456 BOOST_CHECK_EQUAL (examiner.video_length(), 240U);
459 check ("build/test/ffmpeg_encoder_h264_with_reels_reel1.mov");
460 check ("build/test/ffmpeg_encoder_h264_with_reels_reel2.mov");
464 /** Regression test for "Error during decoding: Butler finished" (#2097) */
465 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_regression_1)
467 auto content = content_factory(TestPaths::private_data() / "arrietty_JP-EN.mkv")[0];
468 auto film = new_test_film2 ("ffmpeg_encoder_prores_regression_1", { content });
470 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
471 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_regression_1.mov", ExportFormat::PRORES_HQ, false, true, false, 23);
476 /** Regression test for Butler video buffers reached 480 frames (audio is 0) (#2101) */
477 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_regression_2)
479 auto logs = dcpomatic_log->types();
480 dcpomatic_log->set_types(logs | LogEntry::TYPE_DEBUG_PLAYER);
482 auto content = content_factory(TestPaths::private_data() / "tge_clip.mkv")[0];
483 auto film = new_test_film2 ("ffmpeg_encoder_prores_regression_2", { content });
485 auto job = make_shared<TranscodeJob>(film, TranscodeJob::ChangedBehaviour::IGNORE);
486 FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_regression_2.mov", ExportFormat::PRORES_HQ, false, true, false, 23);
489 dcpomatic_log->set_types(logs);