Supporters update.
[dcpomatic.git] / test / ffmpeg_audio_test.cc
1 /*
2     Copyright (C) 2013-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/ffmpeg_audio_test.cc
23  *  @brief Test reading audio from an FFmpeg file.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/constants.h"
29 #include "lib/content_factory.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/film.h"
34 #include "lib/player.h"
35 #include "lib/ratio.h"
36 #include "lib/video_content.h"
37 #include "test.h"
38 #include <dcp/cpl.h>
39 #include <dcp/dcp.h>
40 #include <dcp/sound_asset.h>
41 #include <dcp/sound_frame.h>
42 #include <dcp/reel_sound_asset.h>
43 #include <dcp/sound_asset_reader.h>
44 #include <dcp/reel.h>
45 #include <boost/test/unit_test.hpp>
46
47
48 using std::make_shared;
49 using std::string;
50
51
52 BOOST_AUTO_TEST_CASE (ffmpeg_audio_test)
53 {
54         auto film = new_test_film ("ffmpeg_audio_test");
55         film->set_name ("ffmpeg_audio_test");
56         auto c = make_shared<FFmpegContent> ("test/data/staircase.mov");
57         film->examine_and_add_content (c);
58
59         BOOST_REQUIRE (!wait_for_jobs());
60
61         int constexpr audio_channels = 6;
62
63         film->set_container (Ratio::from_id ("185"));
64         film->set_audio_channels(audio_channels);
65         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
66         make_and_verify_dcp (film);
67
68         boost::filesystem::path path = "build/test";
69         path /= "ffmpeg_audio_test";
70         path /= film->dcp_name ();
71         dcp::DCP check (path.string ());
72         check.read ();
73
74         auto sound_asset = check.cpls().front()->reels().front()->main_sound ();
75         BOOST_CHECK (sound_asset);
76         BOOST_REQUIRE_EQUAL(sound_asset->asset()->channels (), audio_channels);
77
78         /* Sample index in the DCP */
79         int n = 0;
80         /* DCP sound asset frame */
81         int frame = 0;
82
83         while (n < sound_asset->asset()->intrinsic_duration()) {
84                 auto sound_frame = sound_asset->asset()->start_read()->get_frame (frame++);
85                 uint8_t const * d = sound_frame->data ();
86                 for (int offset = 0; offset < sound_frame->size(); offset += (3 * sound_asset->asset()->channels())) {
87                         for (auto channel = 0; channel < audio_channels; ++channel) {
88                                 auto const sample = d[offset + channel * 3 + 1] | (d[offset + channel * 3 + 2] << 8);
89                                 if (channel == 2) {
90                                         /* Input should be on centre */
91                                         BOOST_CHECK_EQUAL(sample, n);
92                                 } else {
93                                         /* Everything else should be silent */
94                                         BOOST_CHECK_EQUAL(sample, 0);
95                                 }
96                         }
97                         ++n;
98                 }
99         }
100 }
101
102
103 /** Decode a file containing truehd so we can profile it; this is with the player set to normal */
104 BOOST_AUTO_TEST_CASE (ffmpeg_audio_test2)
105 {
106         auto film = new_test_film2 ("ffmpeg_audio_test2");
107         auto content = content_factory(TestPaths::private_data() / "wayne.mkv")[0];
108         film->examine_and_add_content (content);
109         BOOST_REQUIRE (!wait_for_jobs ());
110
111         auto player = make_shared<Player>(film, Image::Alignment::COMPACT);
112         while (!player->pass ()) {}
113 }
114
115
116 /** Decode a file containing truehd so we can profile it; this is with the player set to fast */
117 BOOST_AUTO_TEST_CASE (ffmpeg_audio_test3)
118 {
119         auto film = new_test_film2 ("ffmpeg_audio_test3");
120         auto content = content_factory(TestPaths::private_data() / "wayne.mkv")[0];
121         film->examine_and_add_content (content);
122         BOOST_REQUIRE (!wait_for_jobs ());
123
124         auto player = make_shared<Player>(film, Image::Alignment::COMPACT);
125         player->set_fast ();
126         while (!player->pass ()) {}
127 }
128
129
130 /** Decode a file whose audio previously crashed DCP-o-matic (#1857) */
131 BOOST_AUTO_TEST_CASE (ffmpeg_audio_test4)
132 {
133         auto film = new_test_film2 ("ffmpeg_audio_test4");
134         auto content = content_factory(TestPaths::private_data() / "Actuellement aout 2020.wmv")[0];
135         film->examine_and_add_content (content);
136         BOOST_REQUIRE (!wait_for_jobs ());
137
138         auto player = make_shared<Player>(film, Image::Alignment::COMPACT);
139         player->set_fast ();
140         BOOST_CHECK_NO_THROW (while (!player->pass()) {});
141 }
142