Commit b25cd10360 for aom
commit b25cd10360be3c044098e173b3887c1f3c30bfcf
Author: Ihor Buhaiov <freedbrt@gmail.com>
Date: Mon Nov 2 18:22:29 2026 +0100
Make IntraBC hashes independent of byte order
The IntraBC block hashes combine the hashes of 4 smaller blocks with
CRC-32C. av1_get_block_hash_value() put the 4 uint32_t values in an
array and hashed its bytes, and the C version of
av1_combine_block_hashes() did the same. Those bytes depend on the byte
order of the CPU, and the C CRC also reads its input 8 bytes at a time
as a native integer. So big endian CPUs got other hashes than x86 and
Arm, picked other IntraBC candidates and wrote other streams. The
SSE4.2 and Arm CRC32 versions take the values from registers and were
not affected.
The C version of av1_combine_block_hashes() now feeds each value into a
table CRC directly, low byte first, the way the CRC32C instructions do.
av1_get_block_hash_value() calls av1_combine_block_hashes() for each
block instead of hashing a byte array, so the search uses the same code
as the frame passes. hash_test.cc is now built on every target, so its
C tests also run on CPUs without CRC instructions. Two new tests pin
the hashes of fixed input to the CRC-32C of their little endian bytes.
On little endian CPUs the hashes stay the same, so x86 and Arm output
doesn't change. Packet hashes match before and after on an Apple M3
Pro for RT speed 11 on 2 screen clips, 4:2:0 and 4:4:4, 1 and 4
threads, and for all intra speed 2 and 6. They also match on x86 with
gcc 13 for RT speed 11, both with SSE4.2 and with the generic C code.
On s390x, built with gcc 13 and run in qemu, the streams of the same
RT speed 11 runs differed from x86 before this change. Only 4 of 32
frames matched, and the new tests failed. Now every frame matches x86
in RT-only and default builds, and the tests pass.
Key frame CPU time on the M3 Pro at RT speed 11, 1 thread, 2 clips, 10
interleaved rounds of 10 frames, medians: 1.5% and 1.7% less for
4:2:0, 0.2% and 2.5% more for 4:4:4. Single rounds vary by up to 6%,
so this is noise. av1_get_block_hash_value() alone takes 14% to 21%
less time on 8x8 to 32x32 blocks.
Bug: aomedia:433531610
Change-Id: I855169d71562979461af06f0e73be99935a9d577
diff --git a/av1/encoder/hash.c b/av1/encoder/hash.c
index fe911dae68..049b1af355 100644
--- a/av1/encoder/hash.c
+++ b/av1/encoder/hash.c
@@ -12,7 +12,6 @@
#include "av1/encoder/hash.h"
#include <stddef.h>
-#include <string.h>
#include "config/av1_rtcd.h"
@@ -75,17 +74,21 @@ uint32_t av1_get_crc32c_value_c(void *c, const uint8_t *buf, size_t len) {
// For each of the width positions in a row, hashes the 4 sub-block hashes at
// that position and sub_size to the right of it, in top and then in bottom.
// The two rows are separate pointers so they can come from a ring buffer.
+// Each value goes into the CRC low byte first, as the CRC32C instructions take
+// it, so the hash does not depend on the byte order of the CPU.
void av1_combine_block_hashes_c(void *crc_calculator, const uint32_t *top,
const uint32_t *bottom, uint32_t *dst,
int width, int sub_size) {
+ const CRC32C *p = (const CRC32C *)crc_calculator;
for (int x = 0; x < width; x++) {
const uint32_t v[4] = { top[x], top[x + sub_size], bottom[x],
bottom[x + sub_size] };
- // Hash a byte copy so the static analyzer can follow the byte reads.
- // TODO: bug aomedia:433531610 - serialize input values in a way that's
- // independent of the computer architecture's endianness
- uint8_t p[sizeof(v)];
- memcpy(p, v, sizeof(v));
- dst[x] = av1_get_crc32c_value_c(crc_calculator, p, sizeof(p));
+ uint32_t crc = 0xffffffff;
+ for (int i = 0; i < 4; i++) {
+ crc ^= v[i];
+ crc = p->table[3][crc & 0xff] ^ p->table[2][(crc >> 8) & 0xff] ^
+ p->table[1][(crc >> 16) & 0xff] ^ p->table[0][crc >> 24];
+ }
+ dst[x] = crc ^ 0xffffffff;
}
}
diff --git a/av1/encoder/hash_motion.c b/av1/encoder/hash_motion.c
index f20c221305..d86466de28 100644
--- a/av1/encoder/hash_motion.c
+++ b/av1/encoder/hash_motion.c
@@ -572,7 +572,6 @@ void av1_get_block_hash_value(IntraBCHashInfo *intra_bc_hash_info,
int dst_idx = !src_idx;
// 4x4 subblock hash values to current block hash values
- uint32_t to_hash[4];
for (int sub_width = 4; sub_width <= block_size;
sub_width *= 2, src_idx = !src_idx) {
dst_idx = !src_idx;
@@ -587,15 +586,11 @@ void av1_get_block_hash_value(IntraBCHashInfo *intra_bc_hash_info,
AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
assert(dst_pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
- to_hash[0] = buf[src_idx][srcPos];
- to_hash[1] = buf[src_idx][srcPos + 1];
- to_hash[2] = buf[src_idx][srcPos + src_sub_block_in_width];
- to_hash[3] = buf[src_idx][srcPos + src_sub_block_in_width + 1];
-
- // TODO: bug aomedia:433531610 - serialize input values in a way that's
- // independent of the computer architecture's endianness
- buf[dst_idx][dst_pos] =
- av1_get_crc32c_value(calc, (uint8_t *)to_hash, sizeof(to_hash));
+ // Hashes the values rather than their bytes in memory, the same way
+ // as the frame passes, so the result does not depend on the CPU.
+ av1_combine_block_hashes(calc, &buf[src_idx][srcPos],
+ &buf[src_idx][srcPos + src_sub_block_in_width],
+ &buf[dst_idx][dst_pos], 1, 1);
dst_pos++;
}
}
diff --git a/test/hash_test.cc b/test/hash_test.cc
index 1f50f06afc..d728419d03 100644
--- a/test/hash_test.cc
+++ b/test/hash_test.cc
@@ -197,6 +197,22 @@ TEST_P(AV1CombineBlockHashesTest, CheckOutput) {
}
}
+// The hash of 4 values is the CRC-32C of their little endian bytes, the same
+// on every CPU, so the expected values are fixed.
+TEST_P(AV1CombineBlockHashesTest, KnownValues) {
+ const uint32_t top[] = { 0x00000000, 0x01234567, 0x89abcdef,
+ 0xfedcba98, 0x76543210, 0xffffffff };
+ const uint32_t bottom[] = { 0x00000001, 0x80000000, 0x0f0f0f0f,
+ 0xdeadbeef, 0x12345678, 0xa5a5a5a5 };
+ const uint32_t expected[] = { 0xff9c1f5a, 0x9316cf55, 0xca1073ad,
+ 0x6680eda7 };
+ uint32_t out[4];
+ GetParam()(&calc_, top, bottom, out, 4, 2);
+ for (int i = 0; i < 4; ++i) {
+ EXPECT_EQ(expected[i], out[i]) << "x " << i;
+ }
+}
+
TEST_P(AV1CombineBlockHashesTest, DISABLED_Speed) {
// One 4x4 pass over a 2560x1440 frame.
const int stride = 2560;
@@ -324,4 +340,39 @@ TEST(AV1BlockHashValueTest, MatchesSingleBlockHash) {
}
}
+// Pins the hashes of a few fixed blocks, so they can't depend on the CPU.
+TEST(AV1BlockHashValueTest, KnownValues) {
+ const int kSize = 16;
+ const int kBlockSizes[] = { 4, 8, 16 };
+ const uint32_t kExpected[2][3] = { { 0xcf80714a, 0x4609549a, 0x198c5543 },
+ { 0x178eba79, 0xe5a79f58, 0x2813775f } };
+
+ IntraBCHashInfo info = {};
+ av1_hash_table_init(&info);
+ std::vector<uint32_t> buf0(AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
+ std::vector<uint32_t> buf1(AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
+ info.hash_value_buffer[0] = buf0.data();
+ info.hash_value_buffer[1] = buf1.data();
+
+ uint8_t pixels8[kSize * kSize];
+ uint16_t pixels16[kSize * kSize];
+ for (int y = 0; y < kSize; ++y) {
+ for (int x = 0; x < kSize; ++x) {
+ pixels8[y * kSize + x] = (x * 29 + y * 71 + x * y) & 0xff;
+ pixels16[y * kSize + x] = (x * 29 + y * 71 + x * y * 3) & 0x3ff;
+ }
+ }
+ for (int hbd = 0; hbd <= 1; ++hbd) {
+ const uint8_t *src = hbd ? CONVERT_TO_BYTEPTR(pixels16) : pixels8;
+ for (int i = 0; i < 3; ++i) {
+ uint32_t hash_value1;
+ uint32_t hash_value2;
+ av1_get_block_hash_value(&info, src, kSize, kBlockSizes[i], &hash_value1,
+ &hash_value2, hbd);
+ EXPECT_EQ(kExpected[hbd][i], hash_value2)
+ << "hbd " << hbd << " size " << kBlockSizes[i];
+ }
+ }
+}
+
} // namespace
diff --git a/test/test.cmake b/test/test.cmake
index 6d4639c620..db48c730af 100644
--- a/test/test.cmake
+++ b/test/test.cmake
@@ -386,9 +386,7 @@ if(NOT BUILD_SHARED_LIBS)
"${AOM_ROOT}/test/intra_edge_test.cc")
endif()
- if(HAVE_SSE4_2 OR HAVE_ARM_CRC32)
- list(APPEND AOM_UNIT_TEST_ENCODER_SOURCES "${AOM_ROOT}/test/hash_test.cc")
- endif()
+ list(APPEND AOM_UNIT_TEST_ENCODER_SOURCES "${AOM_ROOT}/test/hash_test.cc")
if(CONFIG_REALTIME_ONLY)
list(REMOVE_ITEM AOM_UNIT_TEST_ENCODER_SOURCES