Ignore parameters in PKL type strings when checking them.
[libdcp.git] / test / sync_test.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "sound_asset.h"
36 #include "sound_asset_reader.h"
37 #include "sound_asset_writer.h"
38 #include "test.h"
39 #include <boost/filesystem.hpp>
40 #include <memory>
41 #include <boost/test/unit_test.hpp>
42 #include <vector>
43
44
45 using std::vector;
46 using boost::shared_array;
47 using std::shared_ptr;
48
49
50 static const int sample_A = 0.038 * 8388608;
51 static const int sample_B = 0.092 * 8388608;
52 static const int sample_C = 0.071 * 8388608;
53
54 static
55 bool
56 close (int reference, int sample)
57 {
58         return abs (reference - sample) < 4096;
59 }
60
61
62 static
63 bool
64 close (int ref1, int ref2, int ref3, int ref4, int check[4])
65 {
66         return close(ref1, check[0]) && close(ref2, check[1]) && close(ref3, check[2]) && close(ref4, check[3]);
67 }
68
69
70 static int const sync_channel = 13;
71 static int const bytes_per_sample = 3;
72
73
74 static int
75 read_sync_sample (uint8_t const* data, int sample_index, int channels)
76 {
77         uint8_t const* p = data + (sample_index * channels * bytes_per_sample) + sync_channel * bytes_per_sample;
78         return static_cast<int>((p[0] << 8) | (p[1] << 16) | (p[2] << 24)) / 256;
79 }
80
81
82 BOOST_AUTO_TEST_CASE (sync_test1)
83 {
84         dcp::SoundAsset asset (private_test / "data" / "atmos_pcm.mxf");
85         shared_ptr<dcp::SoundAssetReader> reader = asset.start_read ();
86         shared_ptr<const dcp::SoundFrame> frame = reader->get_frame (0);
87
88         /* Read the samples from the first MXF frame of channel 14 and decode them to bits */
89         uint8_t const * data = frame->data ();
90         vector<bool> ref;
91         /* There's 2000 samples which contain 500 bits of data */
92         for (int i = 0; i < 500; ++i) {
93                 int bit[4];
94                 for (int j = 0; j < 4; ++j) {
95                         int sample_index = i * 4 + j;
96                         bit[j] = read_sync_sample (data, sample_index, asset.channels());
97                 }
98
99                 if (close(sample_A, sample_B, sample_B, sample_A, bit)) {
100                         ref.push_back (false);
101                 } else if (close(-sample_A, -sample_B, -sample_B, -sample_A, bit)) {
102                         ref.push_back (false);
103                 } else if (close(sample_C, sample_C, -sample_C, -sample_C, bit)) {
104                         ref.push_back (true);
105                 } else if (close(-sample_C, -sample_C, sample_C, sample_C, bit)) {
106                         ref.push_back (true);
107                 } else {
108                         BOOST_CHECK (false);
109                 }
110         }
111
112         shared_ptr<dcp::SoundAssetWriter> writer = asset.start_write ("build/test/foo.mxf", true);
113
114         /* Compare the sync bits made by SoundAssetWriter to the "proper" ones in the MXF */
115         BOOST_CHECK (ref == writer->create_sync_packets());
116 }
117
118
119 BOOST_AUTO_TEST_CASE (sync_test2)
120 {
121         /* Make a MXF with the same ID as atmos_pcm.mxf and write a frame of random stuff */
122         int const channels = 14;
123         dcp::SoundAsset asset (dcp::Fraction(24, 1), 48000, channels, dcp::LanguageTag("en-GB"), dcp::Standard::SMPTE);
124         asset._id = "e004046e09234f90a4ae4355e7e83506";
125         boost::system::error_code ec;
126         boost::filesystem::remove ("build/test/foo.mxf", ec);
127         auto writer = asset.start_write ("build/test/foo.mxf", true);
128
129         int const frames = 2000;
130         float** junk = new float*[channels];
131         for (int i = 0; i < channels; ++i) {
132                 junk[i] = new float[frames];
133                 for (int j = 0; j < frames; ++j) {
134                         junk[i][j] = float(rand()) / RAND_MAX;
135                 }
136         }
137
138         writer->write (junk, frames);
139         for (int i = 0; i < channels; ++i) {
140                 delete[] junk[i];
141         }
142         delete[] junk;
143         writer->finalize ();
144
145         /* Check that channel 14 on the first frame matches channel 14 on the reference */
146         dcp::SoundAsset ref (private_test / "data" / "atmos_pcm.mxf");
147         dcp::SoundAsset check ("build/test/foo.mxf");
148
149         shared_ptr<dcp::SoundAssetReader> ref_read = ref.start_read ();
150         shared_ptr<dcp::SoundAssetReader> check_read = check.start_read ();
151
152         shared_ptr<const dcp::SoundFrame> ref_frame = ref_read->get_frame(0);
153         uint8_t const* ref_data = ref_frame->data();
154         shared_ptr<const dcp::SoundFrame> check_frame = check_read->get_frame(0);
155         uint8_t const* check_data = check_frame->data();
156
157         for (int i = 0; i < frames; ++i) {
158                 int ref_sample = read_sync_sample (ref_data, i, ref.channels());
159                 int check_sample = read_sync_sample (check_data, i, check.channels());
160                 BOOST_CHECK (abs(ref_sample - check_sample) < 2);
161         }
162 }
163