Don't use flat_red.j2c for the simple DCP test as it's only 8-bit.
[libdcp.git] / test / frame_info_hash_test.cc
1 /*
2     Copyright (C) 2016-2019 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 #include "mono_picture_asset.h"
35 #include "mono_picture_asset_writer.h"
36 #include "j2k_transcode.h"
37 #include "openjpeg_image.h"
38 #include <boost/test/unit_test.hpp>
39
40 using std::string;
41 using std::shared_ptr;
42 using std::make_shared;
43
44 static void
45 check (unsigned int* seed, shared_ptr<dcp::PictureAssetWriter> writer, string hash)
46 {
47         shared_ptr<dcp::OpenJPEGImage> xyz (new dcp::OpenJPEGImage (dcp::Size (1998, 1080)));
48         for (int c = 0; c < 3; ++c) {
49                 for (int p = 0; p < (1998 * 1080); ++p) {
50                         xyz->data(c)[p] = rand_r (seed) & 0xfff;
51                 }
52         }
53
54         dcp::ArrayData data = dcp::compress_j2k (xyz, 100000000, 24, false, false);
55
56         dcp::FrameInfo info = writer->write (data.data(), data.size());
57         BOOST_CHECK_EQUAL (info.hash, hash);
58 }
59
60 /** Test the hashing of data written to JPEG2000 MXFs with some random inputs */
61 BOOST_AUTO_TEST_CASE (frame_info_hash_test)
62 {
63         auto mp = make_shared<dcp::MonoPictureAsset>(dcp::Fraction (24, 1), dcp::Standard::SMPTE);
64         auto writer = mp->start_write ("build/test/frame_info_hash_test.mxf", false);
65
66         unsigned int seed = 42;
67
68         /* Check a few random frames */
69         check (&seed, writer, "9da3d1d93a80683e65d996edae4101ed");
70         check (&seed, writer, "ecd77b3fbf459591f24119d4118783fb");
71         check (&seed, writer, "9f10303495b58ccb715c893d40127e22");
72 }