Imported Upstream version 0.9.0
[platform/upstream/libjxl.git] / lib / jxl / fields_test.cc
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 #include "lib/jxl/fields.h"
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <array>
12 #include <utility>
13
14 #include "lib/jxl/base/common.h"
15 #include "lib/jxl/base/span.h"
16 #include "lib/jxl/enc_aux_out.h"
17 #include "lib/jxl/enc_fields.h"
18 #include "lib/jxl/frame_header.h"
19 #include "lib/jxl/headers.h"
20 #include "lib/jxl/testing.h"
21
22 namespace jxl {
23 namespace {
24
25 // Ensures `value` round-trips and in exactly `expected_bits_written`.
26 void TestU32Coder(const uint32_t value, const size_t expected_bits_written) {
27   const U32Enc enc(Val(0), Bits(4), Val(0x7FFFFFFF), Bits(32));
28
29   BitWriter writer;
30   BitWriter::Allotment allotment(
31       &writer, RoundUpBitsToByteMultiple(U32Coder::MaxEncodedBits(enc)));
32
33   size_t precheck_pos;
34   EXPECT_TRUE(U32Coder::CanEncode(enc, value, &precheck_pos));
35   EXPECT_EQ(expected_bits_written, precheck_pos);
36
37   EXPECT_TRUE(U32Coder::Write(enc, value, &writer));
38   EXPECT_EQ(expected_bits_written, writer.BitsWritten());
39   writer.ZeroPadToByte();
40   allotment.ReclaimAndCharge(&writer, 0, nullptr);
41
42   BitReader reader(writer.GetSpan());
43   const uint32_t decoded_value = U32Coder::Read(enc, &reader);
44   EXPECT_EQ(value, decoded_value);
45   EXPECT_TRUE(reader.Close());
46 }
47
48 TEST(FieldsTest, U32CoderTest) {
49   TestU32Coder(0, 2);
50   TestU32Coder(1, 6);
51   TestU32Coder(15, 6);
52   TestU32Coder(0x7FFFFFFF, 2);
53   TestU32Coder(128, 34);
54   TestU32Coder(0x7FFFFFFEu, 34);
55   TestU32Coder(0x80000000u, 34);
56   TestU32Coder(0xFFFFFFFFu, 34);
57 }
58
59 void TestU64Coder(const uint64_t value, const size_t expected_bits_written) {
60   BitWriter writer;
61   BitWriter::Allotment allotment(
62       &writer, RoundUpBitsToByteMultiple(U64Coder::MaxEncodedBits()));
63
64   size_t precheck_pos;
65   EXPECT_TRUE(U64Coder::CanEncode(value, &precheck_pos));
66   EXPECT_EQ(expected_bits_written, precheck_pos);
67
68   EXPECT_TRUE(U64Coder::Write(value, &writer));
69   EXPECT_EQ(expected_bits_written, writer.BitsWritten());
70
71   writer.ZeroPadToByte();
72   allotment.ReclaimAndCharge(&writer, 0, nullptr);
73
74   BitReader reader(writer.GetSpan());
75   const uint64_t decoded_value = U64Coder::Read(&reader);
76   EXPECT_EQ(value, decoded_value);
77   EXPECT_TRUE(reader.Close());
78 }
79
80 TEST(FieldsTest, U64CoderTest) {
81   // Values that should take 2 bits (selector 00): 0
82   TestU64Coder(0, 2);
83
84   // Values that should take 6 bits (2 for selector, 4 for value): 1..16
85   TestU64Coder(1, 6);
86   TestU64Coder(2, 6);
87   TestU64Coder(8, 6);
88   TestU64Coder(15, 6);
89   TestU64Coder(16, 6);
90
91   // Values that should take 10 bits (2 for selector, 8 for value): 17..272
92   TestU64Coder(17, 10);
93   TestU64Coder(18, 10);
94   TestU64Coder(100, 10);
95   TestU64Coder(271, 10);
96   TestU64Coder(272, 10);
97
98   // Values that should take 15 bits (2 for selector, 12 for value, 1 for varint
99   // end): (0)..273..4095
100   TestU64Coder(273, 15);
101   TestU64Coder(274, 15);
102   TestU64Coder(1000, 15);
103   TestU64Coder(4094, 15);
104   TestU64Coder(4095, 15);
105
106   // Take 24 bits (of which 20 actual value): (0)..4096..1048575
107   TestU64Coder(4096, 24);
108   TestU64Coder(4097, 24);
109   TestU64Coder(10000, 24);
110   TestU64Coder(1048574, 24);
111   TestU64Coder(1048575, 24);
112
113   // Take 33 bits (of which 28 actual value): (0)..1048576..268435455
114   TestU64Coder(1048576, 33);
115   TestU64Coder(1048577, 33);
116   TestU64Coder(10000000, 33);
117   TestU64Coder(268435454, 33);
118   TestU64Coder(268435455, 33);
119
120   // Take 42 bits (of which 36 actual value): (0)..268435456..68719476735
121   TestU64Coder(268435456ull, 42);
122   TestU64Coder(268435457ull, 42);
123   TestU64Coder(1000000000ull, 42);
124   TestU64Coder(68719476734ull, 42);
125   TestU64Coder(68719476735ull, 42);
126
127   // Take 51 bits (of which 44 actual value): (0)..68719476736..17592186044415
128   TestU64Coder(68719476736ull, 51);
129   TestU64Coder(68719476737ull, 51);
130   TestU64Coder(1000000000000ull, 51);
131   TestU64Coder(17592186044414ull, 51);
132   TestU64Coder(17592186044415ull, 51);
133
134   // Take 60 bits (of which 52 actual value):
135   // (0)..17592186044416..4503599627370495
136   TestU64Coder(17592186044416ull, 60);
137   TestU64Coder(17592186044417ull, 60);
138   TestU64Coder(100000000000000ull, 60);
139   TestU64Coder(4503599627370494ull, 60);
140   TestU64Coder(4503599627370495ull, 60);
141
142   // Take 69 bits (of which 60 actual value):
143   // (0)..4503599627370496..1152921504606846975
144   TestU64Coder(4503599627370496ull, 69);
145   TestU64Coder(4503599627370497ull, 69);
146   TestU64Coder(10000000000000000ull, 69);
147   TestU64Coder(1152921504606846974ull, 69);
148   TestU64Coder(1152921504606846975ull, 69);
149
150   // Take 73 bits (of which 64 actual value):
151   // (0)..1152921504606846976..18446744073709551615
152   TestU64Coder(1152921504606846976ull, 73);
153   TestU64Coder(1152921504606846977ull, 73);
154   TestU64Coder(10000000000000000000ull, 73);
155   TestU64Coder(18446744073709551614ull, 73);
156   TestU64Coder(18446744073709551615ull, 73);
157 }
158
159 Status TestF16Coder(const float value) {
160   size_t max_encoded_bits;
161   // It is not a fatal error if it can't be encoded.
162   if (!F16Coder::CanEncode(value, &max_encoded_bits)) return false;
163   EXPECT_EQ(F16Coder::MaxEncodedBits(), max_encoded_bits);
164
165   BitWriter writer;
166   BitWriter::Allotment allotment(&writer,
167                                  RoundUpBitsToByteMultiple(max_encoded_bits));
168
169   EXPECT_TRUE(F16Coder::Write(value, &writer));
170   EXPECT_EQ(F16Coder::MaxEncodedBits(), writer.BitsWritten());
171   writer.ZeroPadToByte();
172   allotment.ReclaimAndCharge(&writer, 0, nullptr);
173
174   BitReader reader(writer.GetSpan());
175   float decoded_value;
176   EXPECT_TRUE(F16Coder::Read(&reader, &decoded_value));
177   // All values we test can be represented exactly.
178   EXPECT_EQ(value, decoded_value);
179   EXPECT_TRUE(reader.Close());
180   return true;
181 }
182
183 TEST(FieldsTest, F16CoderTest) {
184   for (float sign : {-1.0f, 1.0f}) {
185     // (anything less than 1E-3 are subnormals)
186     for (float mag : {0.0f, 0.5f, 1.0f, 2.0f, 2.5f, 16.015625f, 1.0f / 4096,
187                       1.0f / 16384, 65504.0f}) {
188       EXPECT_TRUE(TestF16Coder(sign * mag));
189     }
190   }
191
192   // Out of range
193   EXPECT_FALSE(TestF16Coder(65504.01f));
194   EXPECT_FALSE(TestF16Coder(-65505.0f));
195 }
196
197 // Ensures Read(Write()) returns the same fields.
198 TEST(FieldsTest, TestRoundtripSize) {
199   for (int i = 0; i < 8; i++) {
200     SizeHeader size;
201     ASSERT_TRUE(size.Set(123 + 77 * i, 7 + i));
202
203     size_t extension_bits = 999, total_bits = 999;  // Initialize as garbage.
204     ASSERT_TRUE(Bundle::CanEncode(size, &extension_bits, &total_bits));
205     EXPECT_EQ(0u, extension_bits);
206
207     BitWriter writer;
208     ASSERT_TRUE(WriteSizeHeader(size, &writer, 0, nullptr));
209     EXPECT_EQ(total_bits, writer.BitsWritten());
210     writer.ZeroPadToByte();
211
212     SizeHeader size2;
213     BitReader reader(writer.GetSpan());
214     ASSERT_TRUE(ReadSizeHeader(&reader, &size2));
215     EXPECT_EQ(total_bits, reader.TotalBitsConsumed());
216     EXPECT_TRUE(reader.Close());
217
218     EXPECT_EQ(size.xsize(), size2.xsize());
219     EXPECT_EQ(size.ysize(), size2.ysize());
220   }
221 }
222
223 // Ensure all values can be reached by the encoding.
224 TEST(FieldsTest, TestCropRect) {
225   CodecMetadata metadata;
226   for (int32_t i = -999; i < 19000; ++i) {
227     FrameHeader f(&metadata);
228     f.custom_size_or_origin = true;
229     f.frame_origin.x0 = i;
230     f.frame_origin.y0 = i;
231     f.frame_size.xsize = 1000 + i;
232     f.frame_size.ysize = 1000 + i;
233     size_t extension_bits = 0, total_bits = 0;
234     ASSERT_TRUE(Bundle::CanEncode(f, &extension_bits, &total_bits));
235     EXPECT_EQ(0u, extension_bits);
236     EXPECT_GE(total_bits, 9u);
237   }
238 }
239 TEST(FieldsTest, TestPreview) {
240   // (div8 cannot represent 4360, but !div8 can go a little higher)
241   for (uint32_t i = 1; i < 4360; ++i) {
242     PreviewHeader p;
243     ASSERT_TRUE(p.Set(i, i));
244     size_t extension_bits = 0, total_bits = 0;
245     ASSERT_TRUE(Bundle::CanEncode(p, &extension_bits, &total_bits));
246     EXPECT_EQ(0u, extension_bits);
247     EXPECT_GE(total_bits, 6u);
248   }
249 }
250
251 // Ensures Read(Write()) returns the same fields.
252 TEST(FieldsTest, TestRoundtripFrame) {
253   CodecMetadata metadata;
254   FrameHeader h(&metadata);
255   h.extensions = 0x800;
256
257   size_t extension_bits = 999, total_bits = 999;  // Initialize as garbage.
258   ASSERT_TRUE(Bundle::CanEncode(h, &extension_bits, &total_bits));
259   EXPECT_EQ(0u, extension_bits);
260   BitWriter writer;
261   ASSERT_TRUE(WriteFrameHeader(h, &writer, nullptr));
262   EXPECT_EQ(total_bits, writer.BitsWritten());
263   writer.ZeroPadToByte();
264
265   FrameHeader h2(&metadata);
266   BitReader reader(writer.GetSpan());
267   ASSERT_TRUE(ReadFrameHeader(&reader, &h2));
268   EXPECT_EQ(total_bits, reader.TotalBitsConsumed());
269   EXPECT_TRUE(reader.Close());
270
271   EXPECT_EQ(h.extensions, h2.extensions);
272   EXPECT_EQ(h.flags, h2.flags);
273 }
274
275 #ifndef JXL_CRASH_ON_ERROR
276 // Ensure out-of-bounds values cause an error.
277 TEST(FieldsTest, TestOutOfRange) {
278   SizeHeader h;
279   ASSERT_TRUE(h.Set(0xFFFFFFFFull, 0xFFFFFFFFull));
280   size_t extension_bits = 999, total_bits = 999;  // Initialize as garbage.
281   ASSERT_FALSE(Bundle::CanEncode(h, &extension_bits, &total_bits));
282 }
283 #endif
284
285 struct OldBundle : public Fields {
286   OldBundle() { Bundle::Init(this); }
287   JXL_FIELDS_NAME(OldBundle)
288
289   Status VisitFields(Visitor* JXL_RESTRICT visitor) override {
290     JXL_QUIET_RETURN_IF_ERROR(
291         visitor->U32(Val(1), Bits(2), Bits(3), Bits(4), 1, &old_small));
292     JXL_QUIET_RETURN_IF_ERROR(visitor->F16(1.125f, &old_f));
293     JXL_QUIET_RETURN_IF_ERROR(
294         visitor->U32(Bits(7), Bits(12), Bits(16), Bits(32), 0, &old_large));
295
296     JXL_QUIET_RETURN_IF_ERROR(visitor->BeginExtensions(&extensions));
297     return visitor->EndExtensions();
298   }
299
300   uint32_t old_small;
301   float old_f;
302   uint32_t old_large;
303   uint64_t extensions;
304 };
305
306 struct NewBundle : public Fields {
307   NewBundle() { Bundle::Init(this); }
308   JXL_FIELDS_NAME(NewBundle)
309
310   Status VisitFields(Visitor* JXL_RESTRICT visitor) override {
311     JXL_QUIET_RETURN_IF_ERROR(
312         visitor->U32(Val(1), Bits(2), Bits(3), Bits(4), 1, &old_small));
313     JXL_QUIET_RETURN_IF_ERROR(visitor->F16(1.125f, &old_f));
314     JXL_QUIET_RETURN_IF_ERROR(
315         visitor->U32(Bits(7), Bits(12), Bits(16), Bits(32), 0, &old_large));
316
317     JXL_QUIET_RETURN_IF_ERROR(visitor->BeginExtensions(&extensions));
318     if (visitor->Conditional(extensions & 1)) {
319       JXL_QUIET_RETURN_IF_ERROR(
320           visitor->U32(Val(2), Bits(2), Bits(3), Bits(4), 2, &new_small));
321       JXL_QUIET_RETURN_IF_ERROR(visitor->F16(-2.0f, &new_f));
322     }
323     if (visitor->Conditional(extensions & 2)) {
324       JXL_QUIET_RETURN_IF_ERROR(
325           visitor->U32(Bits(9), Bits(12), Bits(16), Bits(32), 0, &new_large));
326     }
327     return visitor->EndExtensions();
328   }
329
330   uint32_t old_small;
331   float old_f;
332   uint32_t old_large;
333   uint64_t extensions;
334
335   // If extensions & 1
336   uint32_t new_small = 2;
337   float new_f = -2.0f;
338   // If extensions & 2
339   uint32_t new_large = 0;
340 };
341
342 TEST(FieldsTest, TestNewDecoderOldData) {
343   OldBundle old_bundle;
344   old_bundle.old_large = 123;
345   old_bundle.old_f = 3.75f;
346   old_bundle.extensions = 0;
347
348   // Write to bit stream
349   const size_t kMaxOutBytes = 999;
350   BitWriter writer;
351   // Make sure values are initialized by code under test.
352   size_t extension_bits = 12345, total_bits = 12345;
353   ASSERT_TRUE(Bundle::CanEncode(old_bundle, &extension_bits, &total_bits));
354   ASSERT_LE(total_bits, kMaxOutBytes * kBitsPerByte);
355   EXPECT_EQ(0u, extension_bits);
356   AuxOut aux_out;
357   ASSERT_TRUE(Bundle::Write(old_bundle, &writer, kLayerHeader, &aux_out));
358
359   BitWriter::Allotment allotment(&writer,
360                                  kMaxOutBytes * kBitsPerByte - total_bits);
361   writer.Write(20, 0xA55A);  // sentinel
362   writer.ZeroPadToByte();
363   allotment.ReclaimAndCharge(&writer, kLayerHeader, nullptr);
364
365   ASSERT_LE(writer.GetSpan().size(), kMaxOutBytes);
366   BitReader reader(writer.GetSpan());
367   NewBundle new_bundle;
368   ASSERT_TRUE(Bundle::Read(&reader, &new_bundle));
369   EXPECT_EQ(reader.TotalBitsConsumed(),
370             aux_out.layers[kLayerHeader].total_bits);
371   EXPECT_EQ(reader.ReadBits(20), 0xA55Au);
372   EXPECT_TRUE(reader.Close());
373
374   // Old fields are the same in both
375   EXPECT_EQ(old_bundle.extensions, new_bundle.extensions);
376   EXPECT_EQ(old_bundle.old_small, new_bundle.old_small);
377   EXPECT_EQ(old_bundle.old_f, new_bundle.old_f);
378   EXPECT_EQ(old_bundle.old_large, new_bundle.old_large);
379   // New fields match their defaults
380   EXPECT_EQ(2u, new_bundle.new_small);
381   EXPECT_EQ(-2.0f, new_bundle.new_f);
382   EXPECT_EQ(0u, new_bundle.new_large);
383 }
384
385 TEST(FieldsTest, TestOldDecoderNewData) {
386   NewBundle new_bundle;
387   new_bundle.old_large = 123;
388   new_bundle.extensions = 3;
389   new_bundle.new_f = 999.0f;
390   new_bundle.new_large = 456;
391
392   // Write to bit stream
393   constexpr size_t kMaxOutBytes = 999;
394   BitWriter writer;
395   // Make sure values are initialized by code under test.
396   size_t extension_bits = 12345, total_bits = 12345;
397   ASSERT_TRUE(Bundle::CanEncode(new_bundle, &extension_bits, &total_bits));
398   EXPECT_NE(0u, extension_bits);
399   AuxOut aux_out;
400   ASSERT_TRUE(Bundle::Write(new_bundle, &writer, kLayerHeader, &aux_out));
401   ASSERT_LE(aux_out.layers[kLayerHeader].total_bits,
402             kMaxOutBytes * kBitsPerByte);
403
404   BitWriter::Allotment allotment(
405       &writer,
406       kMaxOutBytes * kBitsPerByte - aux_out.layers[kLayerHeader].total_bits);
407   // Ensure Read skips the additional fields
408   writer.Write(20, 0xA55A);  // sentinel
409   writer.ZeroPadToByte();
410   allotment.ReclaimAndCharge(&writer, kLayerHeader, nullptr);
411
412   BitReader reader(writer.GetSpan());
413   OldBundle old_bundle;
414   ASSERT_TRUE(Bundle::Read(&reader, &old_bundle));
415   EXPECT_EQ(reader.TotalBitsConsumed(),
416             aux_out.layers[kLayerHeader].total_bits);
417   EXPECT_EQ(reader.ReadBits(20), 0xA55Au);
418   EXPECT_TRUE(reader.Close());
419
420   // Old fields are the same in both
421   EXPECT_EQ(new_bundle.extensions, old_bundle.extensions);
422   EXPECT_EQ(new_bundle.old_small, old_bundle.old_small);
423   EXPECT_EQ(new_bundle.old_f, old_bundle.old_f);
424   EXPECT_EQ(new_bundle.old_large, old_bundle.old_large);
425   // (Can't check new fields because old decoder doesn't know about them)
426 }
427
428 }  // namespace
429 }  // namespace jxl