Fix silent stereo mixdown exports when the project audio channel count is > 6.
[dcpomatic.git] / test / overlap_video_test.cc
1 /*
2     Copyright (C) 2020-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 #include "lib/content.h"
23 #include "lib/content_factory.h"
24 #include "lib/dcpomatic_time.h"
25 #include "lib/film.h"
26 #include "lib/piece.h"
27 #include "lib/player.h"
28 #include "lib/video_content.h"
29 #include "test.h"
30 #include <dcp/cpl.h>
31 #include <dcp/dcp.h>
32 #include <dcp/j2k_transcode.h>
33 #include <dcp/mono_picture_asset.h>
34 #include <dcp/mono_picture_frame.h>
35 #include <dcp/openjpeg_image.h>
36 #include <dcp/reel.h>
37 #include <dcp/reel_mono_picture_asset.h>
38 #include <boost/test/unit_test.hpp>
39
40
41 using std::dynamic_pointer_cast;
42 using std::make_shared;
43 using std::shared_ptr;
44 using std::vector;
45
46
47 BOOST_AUTO_TEST_CASE (overlap_video_test1)
48 {
49         auto A = content_factory("test/data/flat_red.png")[0];
50         auto B = content_factory("test/data/flat_green.png")[0];
51         auto C = content_factory("test/data/flat_blue.png")[0];
52         auto film = new_test_film2("overlap_video_test1", { A, B, C });
53         film->set_sequence (false);
54
55         auto const fps = 24;
56
57         // 01234
58         // AAAAA
59         //  B
60         //    C
61
62         A->video->set_length(5 * fps);
63         B->video->set_length(1 * fps);
64         C->video->set_length(1 * fps);
65
66         B->set_position(film, dcpomatic::DCPTime::from_seconds(1));
67         C->set_position(film, dcpomatic::DCPTime::from_seconds(3));
68
69         auto player = make_shared<Player>(film, Image::Alignment::COMPACT);
70         auto pieces = player->_pieces;
71         BOOST_REQUIRE_EQUAL (pieces.size(), 3U);
72         BOOST_CHECK_EQUAL(pieces[0]->content, A);
73         BOOST_CHECK_EQUAL(pieces[1]->content, B);
74         BOOST_CHECK_EQUAL(pieces[2]->content, C);
75         BOOST_CHECK_EQUAL(pieces[0]->ignore_video.size(), 2U);
76         BOOST_CHECK(pieces[0]->ignore_video[0] == dcpomatic::DCPTimePeriod(dcpomatic::DCPTime::from_seconds(1), dcpomatic::DCPTime::from_seconds(1) + B->length_after_trim(film)));
77         BOOST_CHECK(pieces[0]->ignore_video[1] == dcpomatic::DCPTimePeriod(dcpomatic::DCPTime::from_seconds(3), dcpomatic::DCPTime::from_seconds(3) + C->length_after_trim(film)));
78
79         BOOST_CHECK (player->_black.done());
80
81         make_and_verify_dcp (film);
82
83         dcp::DCP back (film->dir(film->dcp_name()));
84         back.read ();
85         BOOST_REQUIRE_EQUAL (back.cpls().size(), 1U);
86         auto cpl = back.cpls()[0];
87         BOOST_REQUIRE_EQUAL (cpl->reels().size(), 1U);
88         auto reel = cpl->reels()[0];
89         BOOST_REQUIRE (reel->main_picture());
90         auto mono_picture = dynamic_pointer_cast<dcp::ReelMonoPictureAsset>(reel->main_picture());
91         BOOST_REQUIRE (mono_picture);
92         auto asset = mono_picture->mono_asset();
93         BOOST_REQUIRE (asset);
94         BOOST_CHECK_EQUAL (asset->intrinsic_duration(), fps * 5);
95         auto reader = asset->start_read ();
96
97         auto close = [](shared_ptr<const dcp::OpenJPEGImage> image, vector<int> rgb) {
98                 for (int component = 0; component < 3; ++component) {
99                         BOOST_REQUIRE(std::abs(image->data(component)[0] - rgb[component]) < 2);
100                 }
101         };
102
103         vector<int> const red = { 2808, 2176, 865 };
104         vector<int> const blue = { 2657, 3470, 1742 };
105         vector<int> const green = { 2044, 1437, 3871 };
106
107         for (int i = 0; i < 5 * fps; ++i) {
108                 auto frame = reader->get_frame (i);
109                 auto image = dcp::decompress_j2k(*frame.get(), 0);
110                 if (i < fps) {
111                         close(image, red);
112                 } else if (i < 2 * fps) {
113                         close(image, blue);
114                 } else if (i < 3 * fps) {
115                         close(image, red);
116                 } else if (i < 4 * fps) {
117                         close(image, green);
118                 } else if (i < 5 * fps) {
119                         close(image, red);
120                 }
121         }
122 }
123