Extract frame info read/write to new class.
[dcpomatic.git] / test / reel_writer_test.cc
1 /*
2     Copyright (C) 2019-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/reel_writer_test.cc
23  *  @brief Test ReelWriter class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/audio_content.h"
29 #include "lib/content.h"
30 #include "lib/content_factory.h"
31 #include "lib/cross.h"
32 #include "lib/film.h"
33 #include "lib/frame_info.h"
34 #include "lib/reel_writer.h"
35 #include "lib/video_content.h"
36 #include "test.h"
37 #include <dcp/dcp.h>
38 #include <dcp/cpl.h>
39 #include <dcp/reel.h>
40 #include <dcp/reel_picture_asset.h>
41 #include <dcp/reel_sound_asset.h>
42 #include <boost/test/unit_test.hpp>
43
44
45 using std::shared_ptr;
46 using std::string;
47 using boost::optional;
48
49
50 static bool equal(J2KFrameInfo a, shared_ptr<InfoFileHandle> file, Frame frame, Eyes eyes)
51 {
52         auto b = J2KFrameInfo(file, frame, eyes);
53         return a.offset == b.offset && a.size == b.size && a.hash == b.hash;
54 }
55
56
57 BOOST_AUTO_TEST_CASE (write_frame_info_test)
58 {
59         auto film = new_test_film2 ("write_frame_info_test");
60         dcpomatic::DCPTimePeriod const period (dcpomatic::DCPTime(0), dcpomatic::DCPTime(96000));
61         ReelWriter writer (film, period, shared_ptr<Job>(), 0, 1, false);
62
63         /* Write the first one */
64
65         J2KFrameInfo info1(0, 123, "12345678901234567890123456789012");
66         info1.write(film->info_file_handle(period, false), 0, Eyes::LEFT);
67         {
68                 auto file = film->info_file_handle(period, true);
69                 BOOST_CHECK(equal(info1, file, 0, Eyes::LEFT));
70         }
71
72         /* Write some more */
73
74         J2KFrameInfo info2(596, 14921, "123acb789f1234ae782012n456339522");
75         info2.write(film->info_file_handle(period, false), 5, Eyes::RIGHT);
76
77         {
78                 auto file = film->info_file_handle(period, true);
79                 BOOST_CHECK(equal(info1, file, 0, Eyes::LEFT));
80                 BOOST_CHECK(equal(info2, file, 5, Eyes::RIGHT));
81         }
82
83         J2KFrameInfo info3(12494, 99157123, "xxxxyyyyabc12356ffsfdsf456339522");
84         info3.write(film->info_file_handle(period, false), 10, Eyes::LEFT);
85
86         {
87                 auto file = film->info_file_handle(period, true);
88                 BOOST_CHECK(equal(info1, file, 0, Eyes::LEFT));
89                 BOOST_CHECK(equal(info2, file, 5, Eyes::RIGHT));
90                 BOOST_CHECK(equal(info3, file, 10, Eyes::LEFT));
91         }
92
93         /* Overwrite one */
94
95         J2KFrameInfo info4(55512494, 123599157123, "ABCDEFGyabc12356ffsfdsf4563395ZZ");
96         info4.write(film->info_file_handle(period, false), 5, Eyes::RIGHT);
97
98         {
99                 auto file = film->info_file_handle(period, true);
100                 BOOST_CHECK(equal(info1, file, 0, Eyes::LEFT));
101                 BOOST_CHECK(equal(info4, file, 5, Eyes::RIGHT));
102                 BOOST_CHECK(equal(info3, file, 10, Eyes::LEFT));
103         }
104 }
105
106
107 /** Check that the reel writer correctly re-uses a video asset changed if we remake
108  *  a DCP with no video changes.
109  */
110 BOOST_AUTO_TEST_CASE (reel_reuse_video_test)
111 {
112         /* Make a DCP */
113         auto video = content_factory("test/data/flat_red.png")[0];
114         auto audio = content_factory("test/data/white.wav")[0];
115         auto film = new_test_film2 ("reel_reuse_video_test", { video, audio });
116         make_and_verify_dcp (film);
117
118         /* Find main picture and sound asset IDs */
119         dcp::DCP dcp1 (film->dir(film->dcp_name()));
120         dcp1.read ();
121         BOOST_REQUIRE_EQUAL (dcp1.cpls().size(), 1U);
122         BOOST_REQUIRE_EQUAL (dcp1.cpls()[0]->reels().size(), 1U);
123         BOOST_REQUIRE (dcp1.cpls()[0]->reels()[0]->main_picture());
124         BOOST_REQUIRE (dcp1.cpls()[0]->reels()[0]->main_sound());
125         auto const picture_id = dcp1.cpls()[0]->reels()[0]->main_picture()->asset()->id();
126         auto const sound_id = dcp1.cpls()[0]->reels()[0]->main_sound()->asset()->id();
127
128         /* Change the audio and re-make */
129         audio->audio->set_gain (-3);
130         /* >1 CPLs in the DCP raises an error in ClairMeta */
131         make_and_verify_dcp(film, {}, true, false);
132
133         /* Video ID should be the same, sound different */
134         dcp::DCP dcp2 (film->dir(film->dcp_name()));
135         dcp2.read ();
136         BOOST_REQUIRE_EQUAL (dcp2.cpls().size(), 1U);
137         BOOST_REQUIRE_EQUAL (dcp2.cpls()[0]->reels().size(), 1U);
138         BOOST_REQUIRE (dcp2.cpls()[0]->reels()[0]->main_picture());
139         BOOST_REQUIRE (dcp2.cpls()[0]->reels()[0]->main_sound());
140         BOOST_CHECK_EQUAL (picture_id, dcp2.cpls()[0]->reels()[0]->main_picture()->asset()->id());
141         BOOST_CHECK (sound_id != dcp2.cpls()[0]->reels().front()->main_sound()->asset()->id());
142
143         /* Crop video and re-make */
144         video->video->set_left_crop (5);
145         /* >1 CPLs in the DCP raises an error in ClairMeta */
146         make_and_verify_dcp(film, {}, true, false);
147
148         /* Video and sound IDs should be different */
149         dcp::DCP dcp3 (film->dir(film->dcp_name()));
150         dcp3.read ();
151         BOOST_REQUIRE_EQUAL (dcp3.cpls().size(), 1U);
152         BOOST_REQUIRE_EQUAL (dcp3.cpls()[0]->reels().size(), 1U);
153         BOOST_REQUIRE (dcp3.cpls()[0]->reels()[0]->main_picture());
154         BOOST_REQUIRE (dcp3.cpls()[0]->reels()[0]->main_sound());
155         BOOST_CHECK (picture_id != dcp3.cpls()[0]->reels()[0]->main_picture()->asset()->id());
156         BOOST_CHECK (sound_id != dcp3.cpls()[0]->reels().front()->main_sound()->asset()->id());
157 }