Fix failure to decode multiple video frames from one packet (#2159).
[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/butler.h"
30 #include "lib/compose.hpp"
31 #include "lib/content_factory.h"
32 #include "lib/cross.h"
33 #include "lib/dcp_content.h"
34 #include "lib/dcp_content_type.h"
35 #include "lib/ffmpeg_content.h"
36 #include "lib/film.h"
37 #include "lib/image_content.h"
38 #include "lib/player.h"
39 #include "lib/ratio.h"
40 #include "lib/string_text_file_content.h"
41 #include "lib/text_content.h"
42 #include "lib/video_content.h"
43 #include "test.h"
44 #include <boost/test/unit_test.hpp>
45 #include <boost/algorithm/string.hpp>
46 #include <iostream>
47
48
49 using std::cout;
50 using std::list;
51 using std::shared_ptr;
52 using std::make_shared;
53 using boost::bind;
54 using boost::optional;
55 #if BOOST_VERSION >= 106100
56 using namespace boost::placeholders;
57 #endif
58 using namespace dcpomatic;
59
60
61 static shared_ptr<AudioBuffers> accumulated;
62
63
64 static void
65 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
66 {
67         BOOST_REQUIRE (accumulated);
68         accumulated->append (audio);
69 }
70
71
72 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
73 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
74 {
75         auto film = new_test_film ("player_silence_padding_test");
76         film->set_name ("player_silence_padding_test");
77         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
78         film->set_container (Ratio::from_id ("185"));
79         film->set_audio_channels (6);
80
81         film->examine_and_add_content (c);
82         BOOST_REQUIRE (!wait_for_jobs());
83
84         accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
85
86         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
87         player->Audio.connect (bind (&accumulate, _1, _2));
88         while (!player->pass ()) {}
89         BOOST_REQUIRE (accumulated->frames() >= 48000);
90         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
91
92         for (int i = 0; i < 48000; ++i) {
93                 for (int c = 0; c < accumulated->channels(); ++c) {
94                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
95                 }
96         }
97 }
98
99
100 /* Test insertion of black frames between separate bits of video content */
101 BOOST_AUTO_TEST_CASE (player_black_fill_test)
102 {
103         auto film = new_test_film ("black_fill_test");
104         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
105         film->set_name ("black_fill_test");
106         film->set_container (Ratio::from_id ("185"));
107         film->set_sequence (false);
108         film->set_interop (false);
109         auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
110         auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
111
112         film->examine_and_add_content (contentA);
113         film->examine_and_add_content (contentB);
114         BOOST_REQUIRE (!wait_for_jobs());
115
116         contentA->video->set_length (3);
117         contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
118         contentA->video->set_custom_ratio (1.85);
119         contentB->video->set_length (1);
120         contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
121         contentB->video->set_custom_ratio (1.85);
122
123         make_and_verify_dcp (
124                 film,
125                 {
126                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
127                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
128                 });
129
130         boost::filesystem::path ref;
131         ref = "test";
132         ref /= "data";
133         ref /= "black_fill_test";
134
135         boost::filesystem::path check;
136         check = "build";
137         check /= "test";
138         check /= "black_fill_test";
139         check /= film->dcp_name();
140
141         check_dcp (ref.string(), check.string());
142 }
143
144
145 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
146 BOOST_AUTO_TEST_CASE (player_subframe_test)
147 {
148         auto film = new_test_film ("reels_test7");
149         film->set_name ("reels_test7");
150         film->set_container (Ratio::from_id("185"));
151         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
152         auto A = content_factory("test/data/flat_red.png").front();
153         film->examine_and_add_content (A);
154         BOOST_REQUIRE (!wait_for_jobs());
155         auto B = content_factory("test/data/awkward_length.wav").front();
156         film->examine_and_add_content (B);
157         BOOST_REQUIRE (!wait_for_jobs());
158         film->set_video_frame_rate (24);
159         A->video->set_length (3 * 24);
160
161         BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
162         BOOST_CHECK (B->full_length(film) == DCPTime(289920));
163         /* Length should be rounded up from B's length to the next video frame */
164         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
165
166         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
167         player->setup_pieces ();
168         BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1U);
169         BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
170         BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1U);
171         BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
172 }
173
174
175 static Frame video_frames;
176 static Frame audio_frames;
177
178
179 static void
180 video (shared_ptr<PlayerVideo>, DCPTime)
181 {
182         ++video_frames;
183 }
184
185 static void
186 audio (shared_ptr<AudioBuffers> audio, DCPTime)
187 {
188         audio_frames += audio->frames();
189 }
190
191
192 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
193 BOOST_AUTO_TEST_CASE (player_interleave_test)
194 {
195         auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
196         film->set_name ("ffmpeg_transcoder_basic_test");
197         film->set_container (Ratio::from_id ("185"));
198         film->set_audio_channels (6);
199
200         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
201         film->examine_and_add_content (c);
202         BOOST_REQUIRE (!wait_for_jobs ());
203
204         auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
205         film->examine_and_add_content (s);
206         BOOST_REQUIRE (!wait_for_jobs ());
207
208         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
209         player->Video.connect (bind (&video, _1, _2));
210         player->Audio.connect (bind (&audio, _1, _2));
211         video_frames = audio_frames = 0;
212         while (!player->pass ()) {
213                 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
214         }
215 }
216
217
218 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
219  *  and a number of others.  I thought this was a player seek bug but in fact it was
220  *  caused by the subtitle starting just after the start of the video frame and hence
221  *  being faded out.
222  */
223 BOOST_AUTO_TEST_CASE (player_seek_test)
224 {
225         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
226         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
227         film->examine_and_add_content (dcp, true);
228         BOOST_REQUIRE (!wait_for_jobs ());
229         dcp->only_text()->set_use (true);
230
231         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
232         player->set_fast ();
233         player->set_always_burn_open_subtitles ();
234         player->set_play_referenced ();
235
236         auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false);
237         butler->disable_audio();
238
239         for (int i = 0; i < 10; ++i) {
240                 auto t = DCPTime::from_frames (i, 24);
241                 butler->seek (t, true);
242                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
243                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
244                 write_image(video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test_%1.png", i));
245                 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
246                    (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
247                    text altogether.
248                 */
249                 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);
250         }
251 }
252
253
254 /** Test some more seeks towards the start of a DCP with awkward subtitles */
255 BOOST_AUTO_TEST_CASE (player_seek_test2)
256 {
257         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
258         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
259         film->examine_and_add_content (dcp, true);
260         BOOST_REQUIRE (!wait_for_jobs ());
261         dcp->only_text()->set_use (true);
262
263         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
264         player->set_fast ();
265         player->set_always_burn_open_subtitles ();
266         player->set_play_referenced ();
267
268         auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false);
269         butler->disable_audio();
270
271         butler->seek(DCPTime::from_seconds(5), true);
272
273         for (int i = 0; i < 10; ++i) {
274                 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
275                 butler->seek (t, true);
276                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
277                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
278                 write_image(
279                         video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
280                         );
281                 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);
282         }
283 }
284
285
286 /** Test a bug when trimmed content follows other content */
287 BOOST_AUTO_TEST_CASE (player_trim_test)
288 {
289        auto film = new_test_film2 ("player_trim_test");
290        auto A = content_factory("test/data/flat_red.png").front();
291        film->examine_and_add_content (A);
292        BOOST_REQUIRE (!wait_for_jobs ());
293        A->video->set_length (10 * 24);
294        auto B = content_factory("test/data/flat_red.png").front();
295        film->examine_and_add_content (B);
296        BOOST_REQUIRE (!wait_for_jobs ());
297        B->video->set_length (10 * 24);
298        B->set_position (film, DCPTime::from_seconds(10));
299        B->set_trim_start (ContentTime::from_seconds (2));
300
301        make_and_verify_dcp (film);
302 }
303
304
305 struct Sub {
306         PlayerText text;
307         TextType type;
308         optional<DCPTextTrack> track;
309         DCPTimePeriod period;
310 };
311
312
313 static void
314 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
315 {
316         Sub s;
317         s.text = text;
318         s.type = type;
319         s.track = track;
320         s.period = period;
321         out->push_back (s);
322 }
323
324
325 /** Test ignoring both video and audio */
326 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
327 {
328         auto film = new_test_film2 ("player_ignore_video_and_audio_test");
329         auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
330         film->examine_and_add_content (ff);
331         auto text = content_factory("test/data/subrip.srt").front();
332         film->examine_and_add_content (text);
333         BOOST_REQUIRE (!wait_for_jobs());
334         text->only_text()->set_type (TextType::CLOSED_CAPTION);
335         text->only_text()->set_use (true);
336
337         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
338         player->set_ignore_video ();
339         player->set_ignore_audio ();
340
341         list<Sub> out;
342         player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
343         while (!player->pass ()) {}
344
345         BOOST_CHECK_EQUAL (out.size(), 6U);
346 }
347
348
349 /** Trigger a crash due to the assertion failure in Player::emit_audio */
350 BOOST_AUTO_TEST_CASE (player_trim_crash)
351 {
352         auto film = new_test_film2 ("player_trim_crash");
353         auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
354         film->examine_and_add_content (boon);
355         BOOST_REQUIRE (!wait_for_jobs());
356
357         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
358         player->set_fast ();
359         auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false);
360
361         /* Wait for the butler to fill */
362         dcpomatic_sleep_seconds (5);
363
364         boon->set_trim_start (ContentTime::from_seconds(5));
365
366         butler->seek (DCPTime(), true);
367
368         /* Wait for the butler to refill */
369         dcpomatic_sleep_seconds (5);
370
371         butler->rethrow ();
372 }
373
374
375 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
376 BOOST_AUTO_TEST_CASE (player_silence_crash)
377 {
378         auto film = new_test_film2 ("player_silence_crash");
379         auto sine = content_factory("test/data/impulse_train.wav").front();
380         film->examine_and_add_content (sine);
381         BOOST_REQUIRE (!wait_for_jobs());
382
383         sine->set_video_frame_rate (23.976);
384         film->write_metadata ();
385         make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
386 }
387
388
389 /** Test a crash when processing a 3D DCP */
390 BOOST_AUTO_TEST_CASE (player_3d_test_1)
391 {
392         auto film = new_test_film2 ("player_3d_test_1a");
393         auto left = content_factory("test/data/flat_red.png").front();
394         film->examine_and_add_content (left);
395         auto right = content_factory("test/data/flat_blue.png").front();
396         film->examine_and_add_content (right);
397         BOOST_REQUIRE (!wait_for_jobs());
398
399         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
400         left->set_position (film, DCPTime());
401         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
402         right->set_position (film, DCPTime());
403         film->set_three_d (true);
404
405         make_and_verify_dcp (film);
406
407         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
408         auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
409
410         film2->set_three_d (true);
411         make_and_verify_dcp (film2);
412 }
413
414
415 /** Test a crash when processing a 3D DCP as content in a 2D project */
416 BOOST_AUTO_TEST_CASE (player_3d_test_2)
417 {
418         auto left = content_factory("test/data/flat_red.png").front();
419         auto right = content_factory("test/data/flat_blue.png").front();
420         auto film = new_test_film2 ("player_3d_test_2a", {left, right});
421
422         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
423         left->set_position (film, DCPTime());
424         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
425         right->set_position (film, DCPTime());
426         film->set_three_d (true);
427
428         make_and_verify_dcp (film);
429
430         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
431         auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
432
433         make_and_verify_dcp (film2);
434 }
435
436
437 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
438  *  #1691.
439  */
440 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
441 {
442         /* 25fps DCP with some audio */
443         auto content1 = content_factory("test/data/flat_red.png").front();
444         auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
445         content1->video->set_length (25);
446         film1->set_video_frame_rate (25);
447         make_and_verify_dcp (film1);
448
449         /* Make another project importing this DCP */
450         auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
451         auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
452
453         /* and importing just the video MXF on its own at the end */
454         optional<boost::filesystem::path> video;
455         for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
456                 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
457                         video = i.path();
458                 }
459         }
460
461         BOOST_REQUIRE (video);
462         auto content3 = content_factory(*video).front();
463         film2->examine_and_add_content (content3);
464         BOOST_REQUIRE (!wait_for_jobs());
465         content3->set_position (film2, DCPTime::from_seconds(1.5));
466         film2->set_video_frame_rate (24);
467         make_and_verify_dcp (film2);
468 }