blob: 7491f5a2fb4401c3e33617fc9aeb09ab3698b7b4 [file] [log] [blame]
Ed Tanous93f987d2017-04-17 17:52:36 -07001#include "ast_jpeg_decoder.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -07002
Ed Tanous9140a672017-04-24 17:01:32 -07003#include <gmock/gmock.h>
4#include <gtest/gtest.h>
Ed Tanous93f987d2017-04-17 17:52:36 -07005
6#ifdef BUILD_CIMG
7#define cimg_display 0
8#include <CImg.h>
9#endif
10
11using namespace testing;
Ed Tanous911ac312017-08-15 09:37:42 -070012MATCHER_P2(IsBetween, a, b,
13 std::string(negation ? "isn't" : "is") + " between " +
Ed Tanous1abe55e2018-09-05 08:30:59 -070014 PrintToString(a) + " and " + PrintToString(b))
15{
16 return a <= arg && arg <= b;
Ed Tanous93f987d2017-04-17 17:52:36 -070017};
18
Ed Tanous1abe55e2018-09-05 08:30:59 -070019TEST(AstJpegDecoder, AllBlue)
20{
21 ast_video::RawVideoBuffer out;
Ed Tanous93f987d2017-04-17 17:52:36 -070022
Ed Tanous1abe55e2018-09-05 08:30:59 -070023 // This binary blog was created on the aspeed hardware using a blue screen
24 // consisting of the color 0x8EFFFA in a web browser window
Gunnar Mills1214b7e2020-06-04 10:11:30 -050025 FILE* fp = fopen("test_resources/aspeedbluescreen.bin", "rb");
Ed Tanous1abe55e2018-09-05 08:30:59 -070026 EXPECT_NE(fp, nullptr);
27 size_t bufferlen =
28 fread(out.buffer.data(), sizeof(decltype(out.buffer)::value_type),
29 out.buffer.size(), fp);
30 fclose(fp);
Ed Tanous93f987d2017-04-17 17:52:36 -070031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032 ASSERT_GT(bufferlen, 0);
Ed Tanous93f987d2017-04-17 17:52:36 -070033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034 out.ySelector = 0;
35 out.uvSelector = 0;
36 out.mode = ast_video::YuvMode::YUV444;
37 out.width = 800;
38 out.height = 600;
Ed Tanous93f987d2017-04-17 17:52:36 -070039
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 ast_video::AstJpegDecoder d;
41 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
42 out.uvSelector);
Ed Tanous93f987d2017-04-17 17:52:36 -070043
Ed Tanous1abe55e2018-09-05 08:30:59 -070044 int tolerance = 16;
Ed Tanous93f987d2017-04-17 17:52:36 -070045
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 // All pixels should be blue (0x8EFFFA) to within a tolerance (due to jpeg
47 // compression artifacts and quanitization)
48 for (int i = 0; i < out.width * out.height; i++)
49 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -050050 ast_video::RGB& pixel = d.outBuffer[i];
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 EXPECT_GT(pixel.r, 0x8E - tolerance);
52 EXPECT_LT(pixel.r, 0x8E + tolerance);
53 EXPECT_GT(pixel.g, 0xFF - tolerance);
54 EXPECT_LT(pixel.g, 0xFF + tolerance);
55 EXPECT_GT(pixel.b, 0xF1 - tolerance);
56 EXPECT_LT(pixel.b, 0xF1 + tolerance);
Ed Tanous93f987d2017-04-17 17:52:36 -070057 }
Ed Tanous93f987d2017-04-17 17:52:36 -070058}
Ed Tanous9140a672017-04-24 17:01:32 -070059
Ed Tanous1abe55e2018-09-05 08:30:59 -070060TEST(AstJpegDecoder, AllBlack)
61{
62 ast_video::RawVideoBuffer out;
Ed Tanous93f987d2017-04-17 17:52:36 -070063
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 // This binary blog was created on the aspeed hardware using a black screen
Gunnar Mills1214b7e2020-06-04 10:11:30 -050065 FILE* fp = fopen("test_resources/aspeedblackscreen.bin", "rb");
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 EXPECT_NE(fp, nullptr);
67 size_t bufferlen = fread(out.buffer.data(), sizeof(char),
68 out.buffer.size() * sizeof(long), fp);
69 fclose(fp);
Ed Tanous93f987d2017-04-17 17:52:36 -070070
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 ASSERT_GT(bufferlen, 0);
Ed Tanous93f987d2017-04-17 17:52:36 -070072
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 out.ySelector = 0;
74 out.uvSelector = 0;
75 out.mode = ast_video::YuvMode::YUV444;
76 out.width = 800;
77 out.height = 600;
Ed Tanous93f987d2017-04-17 17:52:36 -070078
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 ast_video::AstJpegDecoder d;
80 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
81 out.uvSelector);
Ed Tanous93f987d2017-04-17 17:52:36 -070082
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 // All pixels should be blue (0x8EFFFA) to within a tolerance (due to jpeg
84 // compression artifacts and quanitization)
85 for (int x = 0; x < out.width; x++)
86 {
87 for (int y = 0; y < out.height; y++)
88 {
89 ast_video::RGB pixel = d.outBuffer[x + (y * out.width)];
90 ASSERT_EQ(pixel.r, 0x00) << "X:" << x << " Y: " << y;
91 ASSERT_EQ(pixel.g, 0x00) << "X:" << x << " Y: " << y;
92 ASSERT_EQ(pixel.b, 0x00) << "X:" << x << " Y: " << y;
93 }
94 }
95}
96
97TEST(AstJpegDecoder, TestColors)
98{
99 ast_video::RawVideoBuffer out;
100
101 // This binary blog was created on the aspeed hardware using a blue screen
102 // consisting of the color 0x8EFFFA in a web browser window
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500103 FILE* fp = fopen("test_resources/ubuntu_444_800x600_0chrom_0lum.bin", "rb");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 EXPECT_NE(fp, nullptr);
105 size_t bufferlen = fread(out.buffer.data(), sizeof(char),
106 out.buffer.size() * sizeof(long), fp);
107 fclose(fp);
108
109 ASSERT_GT(bufferlen, 0);
110
111 out.ySelector = 0;
112 out.uvSelector = 0;
113 out.mode = ast_video::YuvMode::YUV444;
114 out.width = 800;
115 out.height = 600;
116
117 ast_video::AstJpegDecoder d;
118 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
119 out.uvSelector);
120
121 int tolerance = 16;
122 /*
123 for (int i = 0; i < out.width * out.height; i++) {
124 ast_video::RGB &pixel = d.outBuffer[i];
125 EXPECT_GT(pixel.r, 0x8E - tolerance);
126 EXPECT_LT(pixel.r, 0x8E + tolerance);
127 EXPECT_GT(pixel.g, 0xFF - tolerance);
128 EXPECT_LT(pixel.g, 0xFF + tolerance);
129 EXPECT_GT(pixel.b, 0xF1 - tolerance);
130 EXPECT_LT(pixel.b, 0xF1 + tolerance);
131 }
132 */
Ed Tanous93f987d2017-04-17 17:52:36 -0700133}
Ed Tanous9140a672017-04-24 17:01:32 -0700134
Ed Tanous93f987d2017-04-17 17:52:36 -0700135// Tests the buffers around the screen aren't written to
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136TEST(AstJpegDecoder, BufferLimits)
137{
138 ast_video::RawVideoBuffer out;
Ed Tanous93f987d2017-04-17 17:52:36 -0700139
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 // This binary blog was created on the aspeed hardware using a black screen
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500141 FILE* fp = fopen("test_resources/aspeedblackscreen.bin", "rb");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700142 EXPECT_NE(fp, nullptr);
143 size_t bufferlen = fread(out.buffer.data(), sizeof(char),
144 out.buffer.size() * sizeof(long), fp);
145 fclose(fp);
Ed Tanous93f987d2017-04-17 17:52:36 -0700146
Ed Tanous1abe55e2018-09-05 08:30:59 -0700147 ASSERT_GT(bufferlen, 0);
Ed Tanous93f987d2017-04-17 17:52:36 -0700148
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 out.ySelector = 0;
150 out.uvSelector = 0;
151 out.mode = ast_video::YuvMode::YUV444;
152 out.width = 800;
153 out.height = 600;
Ed Tanous93f987d2017-04-17 17:52:36 -0700154
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 ast_video::AstJpegDecoder d;
156 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
157 out.uvSelector);
158 // reserved pixel should be default value
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500159 for (auto& pixel : d.outBuffer)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 {
161 EXPECT_EQ(pixel.reserved, 0xAA);
162 }
163 // All pixels beyond the buffer should be zero
164 for (int i = out.width * out.height; i < d.outBuffer.size(); i++)
165 {
166 EXPECT_EQ(d.outBuffer[i].r, 0x00) << "index:" << i;
167 EXPECT_EQ(d.outBuffer[i].b, 0x00) << "index:" << i;
168 EXPECT_EQ(d.outBuffer[i].g, 0x00) << "index:" << i;
169 }
Ed Tanous93f987d2017-04-17 17:52:36 -0700170}