Fix typo if -> of (thanks to Uwe Dittes)
[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/player.h"
27 #include "lib/video_content.h"
28 #include "test.h"
29 #include <dcp/cpl.h>
30 #include <dcp/dcp.h>
31 #include <dcp/j2k_transcode.h>
32 #include <dcp/mono_picture_asset.h>
33 #include <dcp/mono_picture_frame.h>
34 #include <dcp/openjpeg_image.h>
35 #include <dcp/reel.h>
36 #include <dcp/reel_mono_picture_asset.h>
37 #include <boost/shared_ptr.hpp>
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
45
46 BOOST_AUTO_TEST_CASE (overlap_video_test1)
47 {
48         auto film = new_test_film2 ("overlap_video_test1");
49         film->set_sequence (false);
50         auto A = content_factory("test/data/flat_red.png").front();
51         film->examine_and_add_content (A);
52         auto B = content_factory("test/data/flat_green.png").front();
53         film->examine_and_add_content (B);
54         BOOST_REQUIRE (!wait_for_jobs());
55
56         A->video->set_length (72);
57         B->video->set_length (24);
58         B->set_position (film, dcpomatic::DCPTime::from_seconds(1));
59
60         auto player = make_shared<Player>(film);
61         auto pieces = player->_pieces;
62         BOOST_REQUIRE_EQUAL (pieces.size(), 2U);
63         BOOST_CHECK_EQUAL (pieces.front()->content, A);
64         BOOST_CHECK_EQUAL (pieces.back()->content, B);
65         BOOST_CHECK (pieces.front()->ignore_video);
66         BOOST_CHECK (pieces.front()->ignore_video.get() == dcpomatic::DCPTimePeriod(dcpomatic::DCPTime::from_seconds(1), dcpomatic::DCPTime::from_seconds(1) + B->length_after_trim(film)));
67
68         BOOST_CHECK (player->_black.done());
69
70         make_and_verify_dcp (film);
71
72         dcp::DCP back (film->dir(film->dcp_name()));
73         back.read ();
74         BOOST_REQUIRE_EQUAL (back.cpls().size(), 1U);
75         auto cpl = back.cpls()[0];
76         BOOST_REQUIRE_EQUAL (cpl->reels().size(), 1U);
77         auto reel = cpl->reels()[0];
78         BOOST_REQUIRE (reel->main_picture());
79         auto mono_picture = dynamic_pointer_cast<dcp::ReelMonoPictureAsset>(reel->main_picture());
80         BOOST_REQUIRE (mono_picture);
81         auto asset = mono_picture->mono_asset();
82         BOOST_REQUIRE (asset);
83         BOOST_CHECK_EQUAL (asset->intrinsic_duration(), 72);
84         auto reader = asset->start_read ();
85
86         auto close = [](int a, int b, int d) {
87                 BOOST_CHECK (std::abs(a - b) < d);
88         };
89
90         for (int i = 0; i < 24; ++i) {
91                 auto frame = reader->get_frame (i);
92                 auto image = dcp::decompress_j2k(*frame.get(), 0);
93                 close (image->data(0)[0], 2808, 2);
94                 close (image->data(1)[0], 2176, 2);
95                 close (image->data(2)[0], 865, 2);
96         }
97
98         for (int i = 24; i < 48; ++i) {
99                 auto frame = reader->get_frame (i);
100                 auto image = dcp::decompress_j2k(*frame.get(), 0);
101                 close (image->data(0)[0], 2657, 2);
102                 close (image->data(1)[0], 3470, 2);
103                 close (image->data(2)[0], 1742, 2);
104         }
105
106         for (int i = 48; i < 72; ++i) {
107                 auto frame = reader->get_frame (i);
108                 auto image = dcp::decompress_j2k(*frame.get(), 0);
109                 close (image->data(0)[0], 2808, 2);
110                 close (image->data(1)[0], 2176, 2);
111                 close (image->data(2)[0], 865, 2);
112         }
113 }
114