/src/aom/av1/encoder/hash.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016, Alliance for Open Media. All rights reserved. |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 2 Clause License and |
5 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
6 | | * was not distributed with this source code in the LICENSE file, you can |
7 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
8 | | * Media Patent License 1.0 was not distributed with this source code in the |
9 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
10 | | */ |
11 | | |
12 | | #include "av1/encoder/hash.h" |
13 | | |
14 | | #include <stddef.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include "config/av1_rtcd.h" |
18 | | |
19 | | /* CRC-32C (iSCSI) polynomial in reversed bit order. */ |
20 | 0 | #define POLY 0x82f63b78 |
21 | | |
22 | | /* Construct table for software CRC-32C calculation. */ |
23 | 0 | void av1_crc32c_calculator_init(CRC32C *p_crc32c) { |
24 | 0 | uint32_t crc; |
25 | |
|
26 | 0 | for (int n = 0; n < 256; n++) { |
27 | 0 | crc = n; |
28 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
29 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
30 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
31 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
32 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
33 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
34 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
35 | 0 | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
36 | 0 | p_crc32c->table[0][n] = crc; |
37 | 0 | } |
38 | 0 | for (int n = 0; n < 256; n++) { |
39 | 0 | crc = p_crc32c->table[0][n]; |
40 | 0 | for (int k = 1; k < 8; k++) { |
41 | 0 | crc = p_crc32c->table[0][crc & 0xff] ^ (crc >> 8); |
42 | 0 | p_crc32c->table[k][n] = crc; |
43 | 0 | } |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | /* Table-driven software version as a fall-back. This is about 15 times slower |
48 | | than using the hardware instructions. This assumes little-endian integers, |
49 | | as is the case on Intel processors that the assembler code here is for. */ |
50 | 0 | uint32_t av1_get_crc32c_value_c(void *c, const uint8_t *buf, size_t len) { |
51 | 0 | const uint8_t *next = buf; |
52 | 0 | uint64_t crc; |
53 | 0 | CRC32C *p = (CRC32C *)c; |
54 | 0 | crc = 0 ^ 0xffffffff; |
55 | 0 | while (len && ((uintptr_t)next & 7) != 0) { |
56 | 0 | crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); |
57 | 0 | len--; |
58 | 0 | } |
59 | 0 | while (len >= 8) { |
60 | 0 | crc ^= *(uint64_t *)next; |
61 | 0 | crc = p->table[7][crc & 0xff] ^ p->table[6][(crc >> 8) & 0xff] ^ |
62 | 0 | p->table[5][(crc >> 16) & 0xff] ^ p->table[4][(crc >> 24) & 0xff] ^ |
63 | 0 | p->table[3][(crc >> 32) & 0xff] ^ p->table[2][(crc >> 40) & 0xff] ^ |
64 | 0 | p->table[1][(crc >> 48) & 0xff] ^ p->table[0][crc >> 56]; |
65 | 0 | next += 8; |
66 | 0 | len -= 8; |
67 | 0 | } |
68 | 0 | while (len) { |
69 | 0 | crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); |
70 | 0 | len--; |
71 | 0 | } |
72 | 0 | return (uint32_t)crc ^ 0xffffffff; |
73 | 0 | } |
74 | | |
75 | | // For each of the width positions in a row, hashes the 4 sub-block hashes at |
76 | | // that position and sub_size to the right of it, in top and then in bottom. |
77 | | // The two rows are separate pointers so they can come from a ring buffer. |
78 | | void av1_combine_block_hashes_c(void *crc_calculator, const uint32_t *top, |
79 | | const uint32_t *bottom, uint32_t *dst, |
80 | 0 | int width, int sub_size) { |
81 | 0 | for (int x = 0; x < width; x++) { |
82 | 0 | const uint32_t v[4] = { top[x], top[x + sub_size], bottom[x], |
83 | 0 | bottom[x + sub_size] }; |
84 | | // Hash a byte copy so the static analyzer can follow the byte reads. |
85 | | // TODO: bug aomedia:433531610 - serialize input values in a way that's |
86 | | // independent of the computer architecture's endianness |
87 | 0 | uint8_t p[sizeof(v)]; |
88 | 0 | memcpy(p, v, sizeof(v)); |
89 | 0 | dst[x] = av1_get_crc32c_value_c(crc_calculator, p, sizeof(p)); |
90 | 0 | } |
91 | 0 | } |