2 Copyright (C) 2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file test/torture_test.cc
22 * @brief Tricky arrangements of content whose resulting DCPs are checked programmatically.
23 * @ingroup completedcp
26 #include "lib/audio_content.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"
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>
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48 using namespace dcpomatic;
50 /** Test start/end trim and positioning of some audio content */
51 BOOST_AUTO_TEST_CASE (torture_test1)
53 shared_ptr<Film> film = new_test_film ("torture_test1");
54 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
55 film->set_name ("torture_test1");
56 film->set_container (Ratio::from_id ("185"));
57 film->set_sequence (false);
59 /* Staircase at an offset of 2000 samples, trimmed both start and end, with a gain of exactly 2 (linear) */
60 shared_ptr<Content> staircase = content_factory("test/data/staircase.wav").front ();
61 film->examine_and_add_content (staircase);
62 BOOST_REQUIRE (!wait_for_jobs());
63 staircase->set_position (film, DCPTime::from_frames (2000, film->audio_frame_rate()));
64 staircase->set_trim_start (ContentTime::from_frames (12, 48000));
65 staircase->set_trim_end (ContentTime::from_frames (35, 48000));
66 staircase->audio->set_gain (20 * log10(2));
68 /* And again at an offset of 50000 samples, trimmed both start and end, with a gain of exactly 2 (linear) */
69 staircase = content_factory("test/data/staircase.wav").front ();
70 film->examine_and_add_content (staircase);
71 BOOST_REQUIRE (!wait_for_jobs());
72 staircase->set_position (film, DCPTime::from_frames(50000, film->audio_frame_rate()));
73 staircase->set_trim_start (ContentTime::from_frames (12, 48000));
74 staircase->set_trim_end (ContentTime::from_frames (35, 48000));
75 staircase->audio->set_gain (20 * log10(2));
77 /* 1s of red at 5s in */
78 shared_ptr<Content> red = content_factory("test/data/flat_red.png").front ();
79 film->examine_and_add_content (red);
80 BOOST_REQUIRE (!wait_for_jobs());
81 red->set_position (film, DCPTime::from_seconds(5));
82 red->video->set_length (24);
84 film->set_video_frame_rate (24);
85 film->write_metadata ();
87 BOOST_REQUIRE (!wait_for_jobs());
89 dcp::DCP dcp ("build/test/torture_test1/" + film->dcp_name(false));
92 list<shared_ptr<dcp::CPL> > cpls = dcp.cpls ();
93 BOOST_REQUIRE_EQUAL (cpls.size(), 1U);
94 list<shared_ptr<dcp::Reel> > reels = cpls.front()->reels ();
95 BOOST_REQUIRE_EQUAL (reels.size(), 1U);
99 shared_ptr<dcp::ReelSoundAsset> reel_sound = reels.front()->main_sound();
100 BOOST_REQUIRE (reel_sound);
101 shared_ptr<dcp::SoundAsset> sound = reel_sound->asset();
102 BOOST_REQUIRE (sound);
103 BOOST_CHECK_EQUAL (sound->intrinsic_duration(), 144);
105 shared_ptr<dcp::SoundAssetReader> sound_reader = sound->start_read ();
107 /* First frame silent */
108 shared_ptr<const dcp::SoundFrame> fr = sound_reader->get_frame (0);
109 for (int i = 0; i < fr->samples(); ++i) {
110 for (int j = 0; j < 6; ++j) {
111 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
115 /* The first staircase is 4800 - 12 - 35 = 4753 samples. One frame is 2000 samples, so we span 3 frames */
117 BOOST_REQUIRE_EQUAL (fr->samples(), 2000);
121 BOOST_TEST_CONTEXT("First staircase, frame #1") {
122 fr = sound_reader->get_frame (1);
123 for (int i = 0; i < fr->samples(); ++i) {
124 for (int j = 0; j < 6; ++j) {
126 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
129 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
135 BOOST_TEST_CONTEXT("First staircase, frame #2") {
136 fr = sound_reader->get_frame (2);
137 for (int i = 0; i < fr->samples(); ++i) {
138 for (int j = 0; j < 6; ++j) {
140 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
143 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
149 BOOST_TEST_CONTEXT("First staircase, frame #3") {
150 fr = sound_reader->get_frame (3);
151 for (int i = 0; i < fr->samples(); ++i) {
152 for (int j = 0; j < 6; ++j) {
153 if (j == 2 && i < (4753 - (2000 * 2))) {
154 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
157 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
163 /* Then some silence */
165 BOOST_TEST_CONTEXT("Silence") {
166 for (int i = 4; i < 24; ++i) {
167 fr = sound_reader->get_frame (i);
168 for (int j = 0; j < fr->samples(); ++j) {
169 for (int k = 0; k < 6; ++k) {
170 BOOST_CHECK_EQUAL (fr->get(k, j), 0);
176 /* Then the same thing again */
180 fr = sound_reader->get_frame (25);
181 for (int i = 0; i < fr->samples(); ++i) {
182 for (int j = 0; j < 6; ++j) {
184 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
187 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
192 fr = sound_reader->get_frame (26);
193 for (int i = 0; i < fr->samples(); ++i) {
194 for (int j = 0; j < 6; ++j) {
196 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
199 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
204 fr = sound_reader->get_frame (27);
205 for (int i = 0; i < fr->samples(); ++i) {
206 for (int j = 0; j < 6; ++j) {
207 if (j == 2 && i < (4753 - (2000 * 2))) {
208 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
211 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
216 /* Then some silence */
218 for (int i = 28; i < 144; ++i) {
219 fr = sound_reader->get_frame (i);
220 for (int j = 0; j < fr->samples(); ++j) {
221 for (int k = 0; k < 6; ++k) {
222 BOOST_CHECK_EQUAL (fr->get(k, j), 0);
229 shared_ptr<dcp::ReelPictureAsset> reel_picture = reels.front()->main_picture();
230 BOOST_REQUIRE (reel_picture);
231 shared_ptr<dcp::MonoPictureAsset> picture = dynamic_pointer_cast<dcp::MonoPictureAsset> (reel_picture->asset());
232 BOOST_REQUIRE (picture);
233 BOOST_CHECK_EQUAL (picture->intrinsic_duration(), 144);
235 shared_ptr<dcp::MonoPictureAssetReader> picture_reader = picture->start_read ();
237 /* First 5 * 24 = 120 frames should be black */
239 shared_ptr<dcp::OpenJPEGImage> ref;
240 for (int i = 0; i < 120; ++i) {
241 shared_ptr<const dcp::MonoPictureFrame> fr = picture_reader->get_frame (i);
242 shared_ptr<dcp::OpenJPEGImage> image = fr->xyz_image ();
243 dcp::Size const size = image->size ();
245 for (int c = 0; c < 3; ++c) {
246 for (int y = 0; y < size.height; ++y) {
247 for (int x = 0; x < size.width; ++x) {
248 BOOST_REQUIRE_EQUAL (image->data(c)[y * size.height + x], 0);
254 for (int c = 0; c < 3; ++c) {
255 BOOST_REQUIRE_MESSAGE (
256 memcmp (image->data(c), ref->data(c), size.width * size.height * sizeof(int)) == 0,
257 "failed on frame " << i << " component " << c
265 for (int i = 120; i < 144; ++i) {
266 shared_ptr<const dcp::MonoPictureFrame> fr = picture_reader->get_frame (i);
267 shared_ptr<dcp::OpenJPEGImage> image = fr->xyz_image ();
268 dcp::Size const size = image->size ();
270 for (int y = 0; y < size.height; ++y) {
271 for (int x = 0; x < size.width; ++x) {
272 BOOST_REQUIRE_MESSAGE (
273 abs(image->data(0)[y * size.height + x] - 2808) <= 2,
274 "failed on frame " << i << " with image data " << image->data(0)[y * size.height + x]
276 BOOST_REQUIRE_MESSAGE (
277 abs(image->data(1)[y * size.height + x] - 2176) <= 2,
278 "failed on frame " << i << " with image data " << image->data(1)[y * size.height + x]
280 BOOST_REQUIRE_MESSAGE (
281 abs(image->data(2)[y * size.height + x] - 865) <= 2,
282 "failed on frame " << i << " with image data " << image->data(2)[y * size.height + x]
288 for (int c = 0; c < 3; ++c) {
289 BOOST_REQUIRE_MESSAGE (
290 memcmp (image->data(c), ref->data(c), size.width * size.height * sizeof(int)) == 0,
291 "failed on frame " << i << " component " << c