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