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