c4241ed9b83224bfdf90d708226896c4323e1084
[dcpomatic.git] / test / player_video_test.cc
1 /*
2     Copyright (C) 2022 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
22 #include "lib/colour_conversion.h"
23 #include "lib/j2k_image_proxy.h"
24 #include "lib/player_video.h"
25 #include <dcp/openjpeg_image.h>
26 #include <dcp/j2k_transcode.h>
27 #include <dcp/mono_picture_frame.h>
28 #include <boost/optional.hpp>
29 #include <boost/test/unit_test.hpp>
30
31
32 using std::make_shared;
33 using std::weak_ptr;
34 using boost::optional;
35
36
37 BOOST_AUTO_TEST_CASE(player_video_j2k_passthrough_test)
38 {
39         auto size = dcp::Size(4096, 2048);
40         auto image = make_shared<dcp::OpenJPEGImage>(size);
41         for (int x = 0; x < size.width; ++x) {
42                 image->data(0)[x] = x;
43                 image->data(1)[x] = x;
44                 image->data(2)[x] = x;
45         }
46         for (int y = 1; y < size.height; ++y) {
47                 memcpy(image->data(0) + y * size.width, image->data(0), size.width);
48                 memcpy(image->data(1) + y * size.width, image->data(1), size.width);
49                 memcpy(image->data(2) + y * size.width, image->data(2), size.width);
50         }
51         auto compressed = compress_j2k(image, 250000000LL, 24, false, true);
52         auto decompressed = decompress_j2k(compressed, 0);
53         auto frame = make_shared<dcp::MonoPictureFrame>(compressed.data(), compressed.size());
54         auto proxy = make_shared<J2KImageProxy>(frame, size, AV_PIX_FMT_XYZ12LE, optional<int>());
55         auto video = make_shared<PlayerVideo>(proxy, Crop(), optional<double>(), size, size, Eyes::BOTH, Part::WHOLE, optional<ColourConversion>(), VideoRange::FULL, weak_ptr<Content>(), optional<Frame>(), false);
56         auto out_image = video->image([](AVPixelFormat) { return AV_PIX_FMT_XYZ12LE; }, VideoRange::FULL, false);
57
58         BOOST_REQUIRE_EQUAL(out_image->size().width, size.width);
59         BOOST_REQUIRE_EQUAL(out_image->size().height, size.height);
60
61         for (int c = 0; c < 3; ++c) {
62                 for (auto y = 0; y < size.height; ++y) {
63                         auto in = decompressed->data(c) + y * size.width;
64                         auto out = reinterpret_cast<uint16_t*>(out_image->data()[0] + y * out_image->stride()[0]) + c;
65                         for (int x = 0; x < size.width; ++x) {
66                                 BOOST_REQUIRE_MESSAGE(*in == (*out >> 4), *in << " != " << *out << " at x=" << x << ", y=" << y << ", c=" << c);
67                                 ++in;
68                                 out += 3;
69                         }
70                 }
71         }
72 }
73