Supporters update.
[dcpomatic.git] / test / player_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/player_test.cc
23  *  @brief Test Player class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/audio_buffers.h"
29 #include "lib/audio_content.h"
30 #include "lib/butler.h"
31 #include "lib/compose.hpp"
32 #include "lib/config.h"
33 #include "lib/content_factory.h"
34 #include "lib/cross.h"
35 #include "lib/dcp_content.h"
36 #include "lib/dcp_content_type.h"
37 #include "lib/dcpomatic_log.h"
38 #include "lib/ffmpeg_content.h"
39 #include "lib/film.h"
40 #include "lib/image_content.h"
41 #include "lib/image_png.h"
42 #include "lib/player.h"
43 #include "lib/ratio.h"
44 #include "lib/string_text_file_content.h"
45 #include "lib/text_content.h"
46 #include "lib/video_content.h"
47 #include "test.h"
48 #include <boost/test/unit_test.hpp>
49 #include <boost/algorithm/string.hpp>
50 #include <iostream>
51
52
53 using std::cout;
54 using std::list;
55 using std::shared_ptr;
56 using std::make_shared;
57 using boost::bind;
58 using boost::optional;
59 #if BOOST_VERSION >= 106100
60 using namespace boost::placeholders;
61 #endif
62 using namespace dcpomatic;
63
64
65 static shared_ptr<AudioBuffers> accumulated;
66
67
68 static void
69 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
70 {
71         BOOST_REQUIRE (accumulated);
72         accumulated->append (audio);
73 }
74
75
76 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
77 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
78 {
79         auto film = new_test_film ("player_silence_padding_test");
80         film->set_name ("player_silence_padding_test");
81         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
82         film->set_container (Ratio::from_id ("185"));
83         film->set_audio_channels (6);
84
85         film->examine_and_add_content (c);
86         BOOST_REQUIRE (!wait_for_jobs());
87
88         accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
89
90         Player player(film, Image::Alignment::COMPACT);
91         player.Audio.connect(bind(&accumulate, _1, _2));
92         while (!player.pass()) {}
93         BOOST_REQUIRE (accumulated->frames() >= 48000);
94         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
95
96         for (int i = 0; i < 48000; ++i) {
97                 for (int c = 0; c < accumulated->channels(); ++c) {
98                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
99                 }
100         }
101 }
102
103
104 /* Test insertion of black frames between separate bits of video content */
105 BOOST_AUTO_TEST_CASE (player_black_fill_test)
106 {
107         auto film = new_test_film ("black_fill_test");
108         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
109         film->set_name ("black_fill_test");
110         film->set_container (Ratio::from_id ("185"));
111         film->set_sequence (false);
112         film->set_interop (false);
113         auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
114         auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
115
116         film->examine_and_add_content (contentA);
117         film->examine_and_add_content (contentB);
118         BOOST_REQUIRE (!wait_for_jobs());
119
120         contentA->video->set_length (3);
121         contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
122         contentA->video->set_custom_ratio (1.85);
123         contentB->video->set_length (1);
124         contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
125         contentB->video->set_custom_ratio (1.85);
126
127         make_and_verify_dcp (
128                 film,
129                 {
130                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
131                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
132                 });
133
134         boost::filesystem::path ref;
135         ref = "test";
136         ref /= "data";
137         ref /= "black_fill_test";
138
139         boost::filesystem::path check;
140         check = "build";
141         check /= "test";
142         check /= "black_fill_test";
143         check /= film->dcp_name();
144
145         /* This test is concerned with the image, so we'll ignore any
146          * differences in sound between the DCP and the reference to avoid test
147          * failures for unrelated reasons.
148          */
149         check_dcp(ref.string(), check.string(), true);
150 }
151
152
153 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
154 BOOST_AUTO_TEST_CASE (player_subframe_test)
155 {
156         auto film = new_test_film ("reels_test7");
157         film->set_name ("reels_test7");
158         film->set_container (Ratio::from_id("185"));
159         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
160         auto A = content_factory("test/data/flat_red.png")[0];
161         film->examine_and_add_content (A);
162         BOOST_REQUIRE (!wait_for_jobs());
163         auto B = content_factory("test/data/awkward_length.wav")[0];
164         film->examine_and_add_content (B);
165         BOOST_REQUIRE (!wait_for_jobs());
166         film->set_video_frame_rate (24);
167         A->video->set_length (3 * 24);
168
169         BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
170         BOOST_CHECK (B->full_length(film) == DCPTime(289920));
171         /* Length should be rounded up from B's length to the next video frame */
172         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
173
174         Player player(film, Image::Alignment::COMPACT);
175         player.setup_pieces();
176         BOOST_REQUIRE_EQUAL(player._black._periods.size(), 1U);
177         BOOST_CHECK(player._black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
178         BOOST_REQUIRE_EQUAL(player._silent._periods.size(), 1U);
179         BOOST_CHECK(player._silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
180 }
181
182
183 static Frame video_frames;
184 static Frame audio_frames;
185
186
187 static void
188 video (shared_ptr<PlayerVideo>, DCPTime)
189 {
190         ++video_frames;
191 }
192
193 static void
194 audio (shared_ptr<AudioBuffers> audio, DCPTime)
195 {
196         audio_frames += audio->frames();
197 }
198
199
200 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
201 BOOST_AUTO_TEST_CASE (player_interleave_test)
202 {
203         auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
204         film->set_name ("ffmpeg_transcoder_basic_test");
205         film->set_container (Ratio::from_id ("185"));
206         film->set_audio_channels (6);
207
208         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
209         film->examine_and_add_content (c);
210         BOOST_REQUIRE (!wait_for_jobs ());
211
212         auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
213         film->examine_and_add_content (s);
214         BOOST_REQUIRE (!wait_for_jobs ());
215
216         Player player(film, Image::Alignment::COMPACT);
217         player.Video.connect(bind(&video, _1, _2));
218         player.Audio.connect(bind(&audio, _1, _2));
219         video_frames = audio_frames = 0;
220         while (!player.pass()) {
221                 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
222         }
223 }
224
225
226 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
227  *  and a number of others.  I thought this was a player seek bug but in fact it was
228  *  caused by the subtitle starting just after the start of the video frame and hence
229  *  being faded out.
230  */
231 BOOST_AUTO_TEST_CASE (player_seek_test)
232 {
233         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
234         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
235         film->examine_and_add_content (dcp, true);
236         BOOST_REQUIRE (!wait_for_jobs ());
237         dcp->only_text()->set_use (true);
238
239         Player player(film, Image::Alignment::COMPACT);
240         player.set_fast();
241         player.set_always_burn_open_subtitles();
242         player.set_play_referenced();
243
244         auto butler = std::make_shared<Butler>(
245                 film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
246                 );
247
248         for (int i = 0; i < 10; ++i) {
249                 auto t = DCPTime::from_frames (i, 24);
250                 butler->seek (t, true);
251                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
252                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
253                 write_image(video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test_%1.png", i));
254                 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
255                    (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
256                    text altogether.
257                 */
258                 check_image(TestPaths::private_data() / String::compose("player_seek_test_%1.png", i), String::compose("build/test/player_seek_test_%1.png", i), 14.08);
259         }
260 }
261
262
263 /** Test some more seeks towards the start of a DCP with awkward subtitles */
264 BOOST_AUTO_TEST_CASE (player_seek_test2)
265 {
266         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
267         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
268         film->examine_and_add_content (dcp, true);
269         BOOST_REQUIRE (!wait_for_jobs ());
270         dcp->only_text()->set_use (true);
271
272         Player player(film, Image::Alignment::COMPACT);
273         player.set_fast();
274         player.set_always_burn_open_subtitles();
275         player.set_play_referenced();
276
277         auto butler = std::make_shared<Butler>
278                 (film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
279                  );
280
281         butler->seek(DCPTime::from_seconds(5), true);
282
283         for (int i = 0; i < 10; ++i) {
284                 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
285                 butler->seek (t, true);
286                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
287                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
288                 write_image(
289                         video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
290                         );
291                 check_image(TestPaths::private_data() / String::compose("player_seek_test2_%1.png", i), String::compose("build/test/player_seek_test2_%1.png", i), 14.08);
292         }
293 }
294
295
296 /** Test a bug when trimmed content follows other content */
297 BOOST_AUTO_TEST_CASE (player_trim_test)
298 {
299        auto film = new_test_film2 ("player_trim_test");
300        auto A = content_factory("test/data/flat_red.png")[0];
301        film->examine_and_add_content (A);
302        BOOST_REQUIRE (!wait_for_jobs ());
303        A->video->set_length (10 * 24);
304        auto B = content_factory("test/data/flat_red.png")[0];
305        film->examine_and_add_content (B);
306        BOOST_REQUIRE (!wait_for_jobs ());
307        B->video->set_length (10 * 24);
308        B->set_position (film, DCPTime::from_seconds(10));
309        B->set_trim_start(film, ContentTime::from_seconds(2));
310
311        make_and_verify_dcp (film);
312 }
313
314
315 struct Sub {
316         PlayerText text;
317         TextType type;
318         optional<DCPTextTrack> track;
319         DCPTimePeriod period;
320 };
321
322
323 static void
324 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
325 {
326         Sub s;
327         s.text = text;
328         s.type = type;
329         s.track = track;
330         s.period = period;
331         out->push_back (s);
332 }
333
334
335 /** Test ignoring both video and audio */
336 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
337 {
338         auto film = new_test_film2 ("player_ignore_video_and_audio_test");
339         auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
340         film->examine_and_add_content (ff);
341         auto text = content_factory("test/data/subrip.srt")[0];
342         film->examine_and_add_content (text);
343         BOOST_REQUIRE (!wait_for_jobs());
344         text->only_text()->set_type (TextType::CLOSED_CAPTION);
345         text->only_text()->set_use (true);
346
347         Player player(film, Image::Alignment::COMPACT);
348         player.set_ignore_video();
349         player.set_ignore_audio();
350
351         list<Sub> out;
352         player.Text.connect(bind (&store, &out, _1, _2, _3, _4));
353         while (!player.pass()) {}
354
355         BOOST_CHECK_EQUAL (out.size(), 6U);
356 }
357
358
359 /** Trigger a crash due to the assertion failure in Player::emit_audio */
360 BOOST_AUTO_TEST_CASE (player_trim_crash)
361 {
362         auto film = new_test_film2 ("player_trim_crash");
363         auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
364         film->examine_and_add_content (boon);
365         BOOST_REQUIRE (!wait_for_jobs());
366
367         Player player(film, Image::Alignment::COMPACT);
368         player.set_fast();
369         auto butler = std::make_shared<Butler>(
370                 film, player, AudioMapping(), 6, bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false, Butler::Audio::ENABLED
371                 );
372
373         /* Wait for the butler to fill */
374         dcpomatic_sleep_seconds (5);
375
376         boon->set_trim_start(film, ContentTime::from_seconds(5));
377
378         butler->seek (DCPTime(), true);
379
380         /* Wait for the butler to refill */
381         dcpomatic_sleep_seconds (5);
382
383         butler->rethrow ();
384 }
385
386
387 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
388 BOOST_AUTO_TEST_CASE (player_silence_crash)
389 {
390         auto film = new_test_film2 ("player_silence_crash");
391         auto sine = content_factory("test/data/impulse_train.wav")[0];
392         film->examine_and_add_content (sine);
393         BOOST_REQUIRE (!wait_for_jobs());
394
395         sine->set_video_frame_rate(film, 23.976);
396         film->write_metadata ();
397         make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
398 }
399
400
401 /** Test a crash when processing a 3D DCP */
402 BOOST_AUTO_TEST_CASE (player_3d_test_1)
403 {
404         auto film = new_test_film2 ("player_3d_test_1a");
405         auto left = content_factory("test/data/flat_red.png")[0];
406         film->examine_and_add_content (left);
407         auto right = content_factory("test/data/flat_blue.png")[0];
408         film->examine_and_add_content (right);
409         BOOST_REQUIRE (!wait_for_jobs());
410
411         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
412         left->set_position (film, DCPTime());
413         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
414         right->set_position (film, DCPTime());
415         film->set_three_d (true);
416
417         make_and_verify_dcp (film);
418
419         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
420         auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
421
422         film2->set_three_d (true);
423         make_and_verify_dcp (film2);
424 }
425
426
427 /** Test a crash when processing a 3D DCP as content in a 2D project */
428 BOOST_AUTO_TEST_CASE (player_3d_test_2)
429 {
430         auto left = content_factory("test/data/flat_red.png")[0];
431         auto right = content_factory("test/data/flat_blue.png")[0];
432         auto film = new_test_film2 ("player_3d_test_2a", {left, right});
433
434         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
435         left->set_position (film, DCPTime());
436         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
437         right->set_position (film, DCPTime());
438         film->set_three_d (true);
439
440         make_and_verify_dcp (film);
441
442         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
443         auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
444
445         make_and_verify_dcp (film2);
446 }
447
448
449 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
450  *  #1691.
451  */
452 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
453 {
454         /* 25fps DCP with some audio */
455         auto content1 = content_factory("test/data/flat_red.png")[0];
456         auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
457         content1->video->set_length (25);
458         film1->set_video_frame_rate (25);
459         make_and_verify_dcp (film1);
460
461         /* Make another project importing this DCP */
462         auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
463         auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
464
465         /* and importing just the video MXF on its own at the end */
466         optional<boost::filesystem::path> video;
467         for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
468                 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
469                         video = i.path();
470                 }
471         }
472
473         BOOST_REQUIRE (video);
474         auto content3 = content_factory(*video)[0];
475         film2->examine_and_add_content (content3);
476         BOOST_REQUIRE (!wait_for_jobs());
477         content3->set_position (film2, DCPTime::from_seconds(1.5));
478         film2->set_video_frame_rate (24);
479         make_and_verify_dcp (film2);
480 }
481
482
483 /** #2257 */
484 BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
485 {
486         auto content = content_factory("test/data/flat_red.png")[0];
487         auto film = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error", { content });
488         int constexpr length = 24 * 25;
489         content->video->set_length(length);
490         film->set_encrypted (true);
491         make_and_verify_dcp (
492                 film,
493                 {
494                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
495                 });
496
497         auto content2 = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
498         auto film2 = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error2", { content2 });
499
500         Player player(film, Image::Alignment::COMPACT);
501         Butler butler(film2, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED);
502
503         float buffer[2000 * 6];
504         for (int i = 0; i < length; ++i) {
505                 butler.get_video(Butler::Behaviour::BLOCKING, 0);
506                 butler.get_audio(Butler::Behaviour::BLOCKING, buffer, 2000);
507         }
508
509         BOOST_CHECK_NO_THROW(butler.rethrow());
510 }
511
512
513 BOOST_AUTO_TEST_CASE (interleaved_subtitle_are_emitted_correctly)
514 {
515         boost::filesystem::path paths[2] = {
516                 "build/test/interleaved_subtitle_are_emitted_correctly1.srt",
517                 "build/test/interleaved_subtitle_are_emitted_correctly2.srt"
518         };
519
520         dcp::File subs_file[2] = { dcp::File(paths[0], "w"), dcp::File(paths[1], "w") };
521
522         fprintf(subs_file[0].get(), "1\n00:00:01,000 -> 00:00:02,000\nSub 1/1\n\n");
523         fprintf(subs_file[0].get(), "2\n00:00:05,000 -> 00:00:06,000\nSub 1/2\n\n");
524
525         fprintf(subs_file[1].get(), "1\n00:00:00,500 -> 00:00:01,500\nSub 2/1\n\n");
526         fprintf(subs_file[1].get(), "2\n00:00:02,000 -> 00:00:03,000\nSub 2/2\n\n");
527
528         subs_file[0].close();
529         subs_file[1].close();
530
531         auto subs1 = content_factory(paths[0])[0];
532         auto subs2 = content_factory(paths[1])[0];
533         auto film = new_test_film2("interleaved_subtitle_are_emitted_correctly", { subs1, subs2 });
534         film->set_sequence(false);
535         subs1->set_position(film, DCPTime());
536         subs2->set_position(film, DCPTime());
537
538         Player player(film, Image::Alignment::COMPACT);
539         dcp::Time last;
540         player.Text.connect([&last](PlayerText text, TextType, optional<DCPTextTrack>, dcpomatic::DCPTimePeriod) {
541                 for (auto sub: text.string) {
542                         BOOST_CHECK(sub.in() >= last);
543                         last = sub.in();
544                 }
545         });
546         while (!player.pass()) {}
547 }
548
549
550 BOOST_AUTO_TEST_CASE(multiple_sound_files_bug)
551 {
552         Cleanup cl;
553
554         Config::instance()->set_log_types(Config::instance()->log_types() | LogEntry::TYPE_DEBUG_PLAYER);
555
556         auto A = content_factory(TestPaths::private_data() / "kook" / "1.wav").front();
557         auto B = content_factory(TestPaths::private_data() / "kook" / "2.wav").front();
558         auto C = content_factory(TestPaths::private_data() / "kook" / "3.wav").front();
559
560         auto film = new_test_film2("multiple_sound_files_bug", { A, B, C }, &cl);
561         film->set_audio_channels(16);
562         C->set_position(film, DCPTime(3840000));
563
564         make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
565
566         check_mxf_audio_file(TestPaths::private_data() / "kook" / "reference.mxf", dcp_file(film, "pcm_"));
567
568         cl.run();
569 }
570
571
572 BOOST_AUTO_TEST_CASE(trimmed_sound_mix_bug_13)
573 {
574         auto A = content_factory("test/data/sine_16_48_440_10.wav").front();
575         auto B = content_factory("test/data/sine_16_44.1_440_10.wav").front();
576         auto film = new_test_film2("trimmed_sound_mix_bug_13", { A, B });
577         film->set_audio_channels(16);
578
579         A->set_position(film, DCPTime());
580         A->audio->set_gain(-12);
581         B->set_position(film, DCPTime());
582         B->audio->set_gain(-12);
583         B->set_trim_start(film, ContentTime(13));
584
585         make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
586         check_mxf_audio_file("test/data/trimmed_sound_mix_bug_13.mxf", dcp_file(film, "pcm_"));
587 }
588
589
590 BOOST_AUTO_TEST_CASE(trimmed_sound_mix_bug_13_frame_rate_change)
591 {
592         auto A = content_factory("test/data/sine_16_48_440_10.wav").front();
593         auto B = content_factory("test/data/sine_16_44.1_440_10.wav").front();
594         auto film = new_test_film2("trimmed_sound_mix_bug_13_frame_rate_change", { A, B });
595
596         A->set_position(film, DCPTime());
597         A->audio->set_gain(-12);
598         B->set_position(film, DCPTime());
599         B->audio->set_gain(-12);
600         B->set_trim_start(film, ContentTime(13));
601
602         A->set_video_frame_rate(film, 24);
603         B->set_video_frame_rate(film, 24);
604         film->set_video_frame_rate(25);
605         film->set_audio_channels(16);
606
607         make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
608         check_mxf_audio_file("test/data/trimmed_sound_mix_bug_13_frame_rate_change.mxf", dcp_file(film, "pcm_"));
609 }
610
611
612 BOOST_AUTO_TEST_CASE(two_d_in_three_d_duplicates)
613 {
614         auto A = content_factory("test/data/flat_red.png").front();
615         auto B = content_factory("test/data/flat_green.png").front();
616         auto film = new_test_film2("two_d_in_three_d_duplicates", { A, B });
617
618         film->set_three_d(true);
619         B->video->set_frame_type(VideoFrameType::THREE_D_LEFT_RIGHT);
620         B->set_position(film, DCPTime::from_seconds(10));
621         B->video->set_custom_size(dcp::Size(1998, 1080));
622
623         auto player = std::make_shared<Player>(film, film->playlist());
624
625         std::vector<uint8_t> red_line(1998 * 3);
626         for (int i = 0; i < 1998; ++i) {
627                 red_line[i * 3] = 255;
628         };
629
630         std::vector<uint8_t> green_line(1998 * 3);
631         for (int i = 0; i < 1998; ++i) {
632                 green_line[i * 3 + 1] = 255;
633         };
634
635         Eyes last_eyes = Eyes::RIGHT;
636         optional<DCPTime> last_time;
637         player->Video.connect([&last_eyes, &last_time, &red_line, &green_line](shared_ptr<PlayerVideo> video, dcpomatic::DCPTime time) {
638                 BOOST_CHECK(last_eyes != video->eyes());
639                 last_eyes = video->eyes();
640                 if (video->eyes() == Eyes::LEFT) {
641                         BOOST_CHECK(!last_time || time == *last_time + DCPTime::from_frames(1, 24));
642                 } else {
643                         BOOST_CHECK(time == *last_time);
644                 }
645                 last_time = time;
646
647                 auto image = video->image([](AVPixelFormat) { return AV_PIX_FMT_RGB24; }, VideoRange::FULL, false);
648                 auto const size = image->size();
649                 for (int y = 0; y < size.height; ++y) {
650                         uint8_t* line = image->data()[0] + y * image->stride()[0];
651                         if (time < DCPTime::from_seconds(10)) {
652                                 BOOST_REQUIRE_EQUAL(memcmp(line, red_line.data(), 1998 * 3), 0);
653                         } else {
654                                 BOOST_REQUIRE_EQUAL(memcmp(line, green_line.data(), 1998 * 3), 0);
655                         }
656                 }
657         });
658
659         BOOST_CHECK(film->length() == DCPTime::from_seconds(20));
660         while (!player->pass()) {}
661 }
662
663
664 BOOST_AUTO_TEST_CASE(three_d_in_two_d_chooses_left)
665 {
666         auto left = content_factory("test/data/flat_red.png").front();
667         auto right = content_factory("test/data/flat_green.png").front();
668         auto mono = content_factory("test/data/flat_blue.png").front();
669
670         auto film = new_test_film2("three_d_in_two_d_chooses_left", { left, right, mono });
671
672         left->video->set_frame_type(VideoFrameType::THREE_D_LEFT);
673         left->set_position(film, dcpomatic::DCPTime());
674         right->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
675         right->set_position(film, dcpomatic::DCPTime());
676
677         mono->set_position(film, dcpomatic::DCPTime::from_seconds(10));
678
679         auto player = std::make_shared<Player>(film, film->playlist());
680
681         std::vector<uint8_t> red_line(1998 * 3);
682         for (int i = 0; i < 1998; ++i) {
683                 red_line[i * 3] = 255;
684         };
685
686         std::vector<uint8_t> blue_line(1998 * 3);
687         for (int i = 0; i < 1998; ++i) {
688                 blue_line[i * 3 + 2] = 255;
689         };
690
691         optional<DCPTime> last_time;
692         player->Video.connect([&last_time, &red_line, &blue_line](shared_ptr<PlayerVideo> video, dcpomatic::DCPTime time) {
693                 BOOST_CHECK(video->eyes() == Eyes::BOTH);
694                 BOOST_CHECK(!last_time || time == *last_time + DCPTime::from_frames(1, 24));
695                 last_time = time;
696
697                 auto image = video->image([](AVPixelFormat) { return AV_PIX_FMT_RGB24; }, VideoRange::FULL, false);
698                 auto const size = image->size();
699                 for (int y = 0; y < size.height; ++y) {
700                         uint8_t* line = image->data()[0] + y * image->stride()[0];
701                         if (time < DCPTime::from_seconds(10)) {
702                                 BOOST_REQUIRE_EQUAL(memcmp(line, red_line.data(), 1998 * 3), 0);
703                         } else {
704                                 BOOST_REQUIRE_EQUAL(memcmp(line, blue_line.data(), 1998 * 3), 0);
705                         }
706                 }
707         });
708
709         BOOST_CHECK(film->length() == DCPTime::from_seconds(20));
710         while (!player->pass()) {}
711 }
712