Add some more bits to the torture test.
[dcpomatic.git] / test / torture_test.cc
1 /*
2     Copyright (C) 2017 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 /** @file  test/torture_test.cc
22  *  @brief Entire projects that are programmatically created and checked.
23  */
24
25 #include "lib/audio_content.h"
26 #include "lib/film.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/ratio.h"
29 #include "lib/content_factory.h"
30 #include "lib/video_content.h"
31 #include "test.h"
32 #include <dcp/cpl.h>
33 #include <dcp/reel.h>
34 #include <dcp/reel_sound_asset.h>
35 #include <dcp/reel_picture_asset.h>
36 #include <dcp/sound_asset.h>
37 #include <dcp/mono_picture_asset.h>
38 #include <dcp/mono_picture_frame.h>
39 #include <dcp/openjpeg_image.h>
40 #include <boost/test/unit_test.hpp>
41
42 using std::list;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45
46 /** Test start/end trim and positioning of some audio content */
47 BOOST_AUTO_TEST_CASE (torture_test1)
48 {
49         shared_ptr<Film> film = new_test_film ("torture_test1");
50         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
51         film->set_name ("torture_test1");
52         film->set_container (Ratio::from_id ("185"));
53         film->set_sequence (false);
54
55         /* Staircase at an offset of 2000 samples, trimmed both start and end, with a gain of 6dB */
56         shared_ptr<Content> staircase = content_factory(film, "test/data/staircase.wav").front ();
57         film->examine_and_add_content (staircase);
58         wait_for_jobs ();
59         staircase->set_position (DCPTime::from_frames (2000, film->audio_frame_rate()));
60         staircase->set_trim_start (ContentTime::from_frames (12, 48000));
61         staircase->set_trim_end (ContentTime::from_frames (35, 48000));
62         staircase->audio->set_gain (20 * log10(2));
63
64         /* And again at an offset of 50000 samples, trimmed both start and end, with a gain of 6dB */
65         staircase = content_factory(film, "test/data/staircase.wav").front ();
66         film->examine_and_add_content (staircase);
67         wait_for_jobs ();
68         staircase->set_position (DCPTime::from_frames (50000, film->audio_frame_rate()));
69         staircase->set_trim_start (ContentTime::from_frames (12, 48000));
70         staircase->set_trim_end (ContentTime::from_frames (35, 48000));
71         staircase->audio->set_gain (20 * log10(2));
72
73         /* 1s of red at 5s in */
74         shared_ptr<Content> red = content_factory(film, "test/data/flat_red.png").front ();
75         film->examine_and_add_content (red);
76         wait_for_jobs ();
77         red->set_position (DCPTime::from_seconds (5));
78         red->video->set_length (24);
79
80         film->set_video_frame_rate (24);
81         film->write_metadata ();
82         film->make_dcp ();
83         wait_for_jobs ();
84
85         dcp::DCP dcp ("build/test/torture_test/" + film->dcp_name(false));
86         dcp.read ();
87
88         list<shared_ptr<dcp::CPL> > cpls = dcp.cpls ();
89         BOOST_REQUIRE_EQUAL (cpls.size(), 1);
90         list<shared_ptr<dcp::Reel> > reels = cpls.front()->reels ();
91         BOOST_REQUIRE_EQUAL (reels.size(), 1);
92
93         /* Check sound */
94
95         shared_ptr<dcp::ReelSoundAsset> reel_sound = reels.front()->main_sound();
96         BOOST_REQUIRE (reel_sound);
97         shared_ptr<dcp::SoundAsset> sound = reel_sound->asset();
98         BOOST_REQUIRE (sound);
99         BOOST_CHECK_EQUAL (sound->intrinsic_duration(), 144);
100
101         shared_ptr<dcp::SoundAssetReader> sound_reader = sound->start_read ();
102
103         /* First frame silent */
104         shared_ptr<const dcp::SoundFrame> fr = sound_reader->get_frame (0);
105         for (int i = 0; i < fr->samples(); ++i) {
106                 for (int j = 0; j < 6; ++j) {
107                         BOOST_CHECK_EQUAL (fr->get(j, i), 0);
108                 }
109         }
110
111         /* The first staircase is 4800 - 12 - 35 = 4753 samples.  One frame is 2000 samples, so we span 3 frames */
112
113         BOOST_REQUIRE_EQUAL (fr->samples(), 2000);
114
115         int stair = 12;
116
117         fr = sound_reader->get_frame (1);
118         for (int i = 0; i < fr->samples(); ++i) {
119                 for (int j = 0; j < 6; ++j) {
120                         if (j == 2) {
121                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
122                                 ++stair;
123                         } else {
124                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
125                         }
126                 }
127         }
128
129         fr = sound_reader->get_frame (2);
130         for (int i = 0; i < fr->samples(); ++i) {
131                 for (int j = 0; j < 6; ++j) {
132                         if (j == 2) {
133                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
134                                 ++stair;
135                         } else {
136                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
137                         }
138                 }
139         }
140
141         fr = sound_reader->get_frame (3);
142         for (int i = 0; i < fr->samples(); ++i) {
143                 for (int j = 0; j < 6; ++j) {
144                         if (j == 2 && i < (4753 - (2000 * 2))) {
145                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
146                                 ++stair;
147                         } else {
148                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
149                         }
150                 }
151         }
152
153         /* Then some silence */
154
155         for (int i = 4; i < 24; ++i) {
156                 fr = sound_reader->get_frame (i);
157                 for (int j = 0; j < fr->samples(); ++j) {
158                         for (int k = 0; k < 6; ++k) {
159                                 BOOST_CHECK_EQUAL (fr->get(k, j), 0);
160                         }
161                 }
162         }
163
164         /* Then the same thing again */
165
166         stair = 12;
167
168         fr = sound_reader->get_frame (25);
169         for (int i = 0; i < fr->samples(); ++i) {
170                 for (int j = 0; j < 6; ++j) {
171                         if (j == 2) {
172                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
173                                 ++stair;
174                         } else {
175                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
176                         }
177                 }
178         }
179
180         fr = sound_reader->get_frame (26);
181         for (int i = 0; i < fr->samples(); ++i) {
182                 for (int j = 0; j < 6; ++j) {
183                         if (j == 2) {
184                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
185                                 ++stair;
186                         } else {
187                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
188                         }
189                 }
190         }
191
192         fr = sound_reader->get_frame (27);
193         for (int i = 0; i < fr->samples(); ++i) {
194                 for (int j = 0; j < 6; ++j) {
195                         if (j == 2 && i < (4753 - (2000 * 2))) {
196                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
197                                 ++stair;
198                         } else {
199                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
200                         }
201                 }
202         }
203
204         /* Then some silence */
205
206         for (int i = 28; i < 144; ++i) {
207                 fr = sound_reader->get_frame (i);
208                 for (int j = 0; j < fr->samples(); ++j) {
209                         for (int k = 0; k < 6; ++k) {
210                                 BOOST_CHECK_EQUAL (fr->get(k, j), 0);
211                         }
212                 }
213         }
214
215         /* Check picture */
216
217         shared_ptr<dcp::ReelPictureAsset> reel_picture = reels.front()->main_picture();
218         BOOST_REQUIRE (reel_picture);
219         shared_ptr<dcp::MonoPictureAsset> picture = dynamic_pointer_cast<dcp::MonoPictureAsset> (reel_picture->asset());
220         BOOST_REQUIRE (picture);
221         BOOST_CHECK_EQUAL (picture->intrinsic_duration(), 144);
222
223         shared_ptr<dcp::MonoPictureAssetReader> picture_reader = picture->start_read ();
224
225         /* First 5 * 24 = 120 frames should be black */
226
227         shared_ptr<dcp::OpenJPEGImage> ref;
228         for (int i = 0; i < 120; ++i) {
229                 shared_ptr<const dcp::MonoPictureFrame> fr = picture_reader->get_frame (i);
230                 shared_ptr<dcp::OpenJPEGImage> image = fr->xyz_image ();
231                 dcp::Size const size = image->size ();
232                 if (i == 0) {
233                         for (int c = 0; c < 3; ++c) {
234                                 for (int y = 0; y < size.height; ++y) {
235                                         for (int x = 0; x < size.width; ++x) {
236                                                 BOOST_REQUIRE_EQUAL (image->data(c)[y * size.height + x], 0);
237                                         }
238                                 }
239                         }
240                         ref = image;
241                 } else {
242                         for (int c = 0; c < 3; ++c) {
243                                 BOOST_REQUIRE_MESSAGE (
244                                         memcmp (image->data(c), ref->data(c), size.width * size.height * sizeof(int)) == 0,
245                                         "failed on frame " << i << " component " << c
246                                         );
247                         }
248                 }
249         }
250
251         /* Then 24 red */
252
253         for (int i = 120; i < 144; ++i) {
254                 shared_ptr<const dcp::MonoPictureFrame> fr = picture_reader->get_frame (i);
255                 shared_ptr<dcp::OpenJPEGImage> image = fr->xyz_image ();
256                 dcp::Size const size = image->size ();
257                 if (i == 120) {
258                         for (int y = 0; y < size.height; ++y) {
259                                 for (int x = 0; x < size.width; ++x) {
260                                         BOOST_REQUIRE_MESSAGE (
261                                                 abs(image->data(0)[y * size.height + x] - 2808) <= 2,
262                                                 "failed on frame " << i << " with image data " << image->data(0)[y * size.height + x]
263                                                 );
264                                         BOOST_REQUIRE_MESSAGE (
265                                                 abs(image->data(1)[y * size.height + x] - 2176) <= 2,
266                                                 "failed on frame " << i << " with image data " << image->data(1)[y * size.height + x]
267                                                 );
268                                         BOOST_REQUIRE_MESSAGE (
269                                                 abs(image->data(2)[y * size.height + x] - 865) <= 2,
270                                                 "failed on frame " << i << " with image data " << image->data(2)[y * size.height + x]
271                                                 );
272                                 }
273                         }
274                         ref = image;
275                 } else {
276                         for (int c = 0; c < 3; ++c) {
277                                 BOOST_REQUIRE_MESSAGE (
278                                         memcmp (image->data(c), ref->data(c), size.width * size.height * sizeof(int)) == 0,
279                                         "failed on frame " << i << " component " << c
280                                         );
281                         }
282                 }
283         }
284
285 }