Coverage Report

Created: 2026-10-17 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Codec/hash_motion.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2018, 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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license.
10
 */
11
12
#include "aom_dsp_rtcd.h"
13
#include "hash.h"
14
#include "hash_motion.h"
15
#include "pcs.h"
16
17
static const int crc_bits        = 16;
18
static const int block_size_bits = 3;
19
20
0
static void hash_table_clear_all(HashTable* p_hash_table) {
21
0
    if (p_hash_table->bucket_count == NULL) {
22
0
        return;
23
0
    }
24
0
    memset(p_hash_table->bucket_count, 0, sizeof(*p_hash_table->bucket_count) << (crc_bits + block_size_bits));
25
0
    p_hash_table->num_entries = 0;
26
0
}
27
28
0
static void get_pixels_in_1d_char_array_by_block_2x2(uint8_t* y_src, int stride, uint8_t* p_pixels_in1D) {
29
0
    uint8_t* p_pel = y_src;
30
0
    int      index = 0;
31
0
    for (int i = 0; i < 2; i++) {
32
0
        for (int j = 0; j < 2; j++) {
33
0
            p_pixels_in1D[index++] = p_pel[j];
34
0
        }
35
0
        p_pel += stride;
36
0
    }
37
0
}
38
39
0
static void get_pixels_in_1d_short_array_by_block_2x2(uint16_t* y_src, int stride, uint16_t* p_pixels_in1D) {
40
0
    uint16_t* p_pel = y_src;
41
0
    int       index = 0;
42
0
    for (int i = 0; i < 2; i++) {
43
0
        for (int j = 0; j < 2; j++) {
44
0
            p_pixels_in1D[index++] = p_pel[j];
45
0
        }
46
0
        p_pel += stride;
47
0
    }
48
0
}
49
50
// the hash value (hash_value1 consists two parts, the first 3 bits relate to
51
// the block size and the remaining 16 bits are the crc values. This fuction
52
// is used to get the first 3 bits.
53
static int hash_block_size_to_index(int block_size) {
54
    switch (block_size) {
55
    case 4:
56
        return 0;
57
    case 8:
58
        return 1;
59
    case 16:
60
        return 2;
61
    case 32:
62
        return 3;
63
    case 64:
64
        return 4;
65
    case 128:
66
        return 5;
67
    default:
68
        return -1;
69
    }
70
}
71
72
0
static uint32_t get_identity_hash_value(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) {
73
    // The four input values add up to 32 bits, which is the size of the output.
74
    // Just pack those values as is.
75
0
    return ((uint32_t)a << 24) + ((uint32_t)b << 16) + ((uint32_t)c << 8) + ((uint32_t)d);
76
0
}
77
78
0
static uint32_t get_xor_hash_value_hbd(const uint16_t a, const uint16_t b, const uint16_t c, const uint16_t d) {
79
0
    uint32_t result;
80
    // Pack the lower 8 bits of each input value to the 32 bit output, then xor
81
    // with the upper 8 bits of each input value.
82
0
    result = ((uint32_t)(a & 0x00ff) << 24) + ((uint32_t)(b & 0x00ff) << 16) + ((uint32_t)(c & 0x00ff) << 8) +
83
0
        ((uint32_t)(d & 0x00ff));
84
0
    result ^= ((uint32_t)(a & 0xff00) << 16) + ((uint32_t)(b & 0xff00) << 8) + ((uint32_t)(c & 0xff00)) +
85
0
        ((uint32_t)(d & 0xff00) >> 8);
86
0
    return result;
87
0
}
88
89
492
void svt_av1_hash_table_destroy(HashTable* p_hash_table) {
90
492
    EB_FREE_ARRAY(p_hash_table->bucket_start);
91
492
    EB_FREE_ARRAY(p_hash_table->bucket_count);
92
492
    EB_FREE_ARRAY(p_hash_table->fill_cursor);
93
492
    EB_FREE_ARRAY(p_hash_table->entries);
94
492
    p_hash_table->num_entries      = 0;
95
492
    p_hash_table->entries_capacity = 0;
96
492
}
97
98
0
EbErrorType svt_aom_rtime_alloc_svt_av1_hash_table_create(HashTable* p_hash_table) {
99
0
    if (p_hash_table->bucket_count != NULL) {
100
0
        hash_table_clear_all(p_hash_table);
101
0
        return EB_ErrorNone;
102
0
    }
103
0
    const int max_addr = 1 << (crc_bits + block_size_bits);
104
0
    EB_MALLOC_ARRAY(p_hash_table->bucket_start, max_addr);
105
0
    EB_CALLOC_ARRAY(p_hash_table->bucket_count, max_addr);
106
0
    EB_MALLOC_ARRAY(p_hash_table->fill_cursor, 1 << crc_bits);
107
0
    p_hash_table->entries          = NULL;
108
0
    p_hash_table->num_entries      = 0;
109
0
    p_hash_table->entries_capacity = 0;
110
0
    return EB_ErrorNone;
111
0
}
112
113
0
int32_t svt_av1_hash_table_count(const HashTable* p_hash_table, uint32_t hash_value) {
114
0
    return p_hash_table->bucket_count[hash_value];
115
0
}
116
117
0
const BlockHash* svt_av1_hash_get_first(const HashTable* p_hash_table, uint32_t hash_value) {
118
0
    assert(svt_av1_hash_table_count(p_hash_table, hash_value) > 0);
119
0
    return &p_hash_table->entries[p_hash_table->bucket_start[hash_value]];
120
0
}
121
122
0
void svt_av1_generate_block_2x2_hash_value(const Yv12BufferConfig* picture, uint32_t* pic_block_hash) {
123
0
    const int width  = 2;
124
0
    const int height = 2;
125
0
    const int x_end  = picture->y_crop_width - width + 1;
126
0
    const int y_end  = picture->y_crop_height - height + 1;
127
0
    if (picture->flags & YV12_FLAG_HIGHBITDEPTH) {
128
0
        uint16_t p[4];
129
0
        int      pos = 0;
130
0
        for (int y_pos = 0; y_pos < y_end; y_pos++) {
131
0
            for (int x_pos = 0; x_pos < x_end; x_pos++) {
132
0
                get_pixels_in_1d_short_array_by_block_2x2(
133
0
                    CONVERT_TO_SHORTPTR(picture->y_buffer) + y_pos * picture->y_stride + x_pos, picture->y_stride, p);
134
                // For HBD, we either have 40 or 48 bits of input data that the xor hash
135
                // reduce to 32 bits. We intentionally don't want to "discard" bits to
136
                // avoid any kind of biasing.
137
0
                pic_block_hash[pos] = get_xor_hash_value_hbd(p[0], p[1], p[2], p[3]);
138
0
                pos++;
139
0
            }
140
0
            pos += width - 1;
141
0
        }
142
0
    } else {
143
0
        uint8_t p[4];
144
0
        int     pos = 0;
145
0
        for (int y_pos = 0; y_pos < y_end; y_pos++) {
146
0
            for (int x_pos = 0; x_pos < x_end; x_pos++) {
147
0
                get_pixels_in_1d_char_array_by_block_2x2(
148
0
                    picture->y_buffer + y_pos * picture->y_stride + x_pos, picture->y_stride, p);
149
                // This 2x2 hash isn't used directly as a "key" for the hash table, so
150
                // we can afford to just copy the 4 8-bit pixel values as a single
151
                // 32-bit value directly. (i.e. there are no concerns of a lack of
152
                // uniform distribution)
153
0
                pic_block_hash[pos] = get_identity_hash_value(p[0], p[1], p[2], p[3]);
154
0
                pos++;
155
0
            }
156
0
            pos += width - 1;
157
0
        }
158
0
    }
159
0
}
160
161
void svt_av1_generate_block_hash_value(const Yv12BufferConfig* picture, int block_size, uint32_t* src_pic_block_hash,
162
0
                                       uint32_t* dst_pic_block_hash) {
163
0
    const int pic_width = picture->y_crop_width;
164
0
    const int x_end     = picture->y_crop_width - block_size + 1;
165
0
    const int y_end     = picture->y_crop_height - block_size + 1;
166
167
0
    const int src_size = block_size >> 1;
168
169
0
    uint32_t  p[4];
170
0
    const int length = sizeof(p);
171
172
0
    int pos = 0;
173
0
    for (int y_pos = 0; y_pos < y_end; y_pos++) {
174
0
        for (int x_pos = 0; x_pos < x_end; x_pos++) {
175
0
            p[0]                    = src_pic_block_hash[pos];
176
0
            p[1]                    = src_pic_block_hash[pos + src_size];
177
0
            p[2]                    = src_pic_block_hash[pos + src_size * pic_width];
178
0
            p[3]                    = src_pic_block_hash[pos + src_size * pic_width + src_size];
179
0
            dst_pic_block_hash[pos] = svt_av1_get_crc32c_value((uint8_t*)p, length);
180
181
0
            pos++;
182
0
        }
183
0
        pos += block_size - 1;
184
0
    }
185
0
}
186
187
// Walk every candidate block of one block size in the hierarchical exploration order below. With
188
// fill == false, count the entries each bucket keeps (capped at max_cand_per_bucket); with fill ==
189
// true, write them. Both passes visit blocks in the same order, so each bucket keeps the same blocks
190
// in the same order as a single push-back pass would.
191
static void explore_blocks(HashTable* p_hash_table, const uint32_t* pic_hash, int pic_width, int x_end, int y_end,
192
0
                           int block_size, int add_value, uint16_t max_cand_per_bucket, bool fill) {
193
0
    const uint32_t  crc_mask = (1 << crc_bits) - 1;
194
0
    uint16_t*       count    = p_hash_table->bucket_count;
195
0
    const uint32_t* start    = p_hash_table->bucket_start;
196
0
    uint16_t*       cursor   = p_hash_table->fill_cursor;
197
0
    BlockHash*      entries  = p_hash_table->entries;
198
0
    int             step     = block_size;
199
0
    int             x_offset = 0;
200
0
    int             y_offset = 0;
201
202
    // Explore the entire frame hierarchically to add intrabc candidate blocks to
203
    // the hash table, by starting with coarser steps (the block size), towards
204
    // finer-grained steps until every candidate block has been considered.
205
    // The nested for loop goes through the pic_hash array column by column.
206
207
    // Doing a hierarchical block exploration helps maximize spatial dispersion
208
    // of the first and foremost candidate blocks while minimizing overlap between
209
    // them. This is helpful because we only keep up to 256 entries of the
210
    // same candidate block (located in different places), so we want those
211
    // entries to cover the biggest area of the image to encode to maximize coding
212
    // efficiency.
213
214
    // This is the coordinate exploration order example for an 8x8 region, with
215
    // block_size = 4. The top-left corner (x, y) coordinates of each candidate
216
    // block are shown below. There are 5 * 5 (25) candidate blocks.
217
    //    x  0  1  2  3  4  5  6  7
218
    //  y +------------------------
219
    //  0 |  1 10  5 13  3
220
    //  1 | 16 22 18 24 20
221
    //  2 |  7 11  9 14  8
222
    //  3 | 17 23 19 25 21
223
    //  4 |  2 12  6 15  4--------+
224
    //  5 |              | 4 x 4  |
225
    //  6 |              | block  |
226
    //  7 |              +--------+
227
228
    // Please note that due to the way block exploration works, the smallest step
229
    // used is 2 (i.e. no two adjacent blocks will be explored consecutively).
230
    // Also, the exploration is designed to visit each block candidate only once.
231
0
    while (step > 1) {
232
0
        for (int x_pos = x_offset; x_pos < x_end; x_pos += step) {
233
0
            for (int y_pos = y_offset; y_pos < y_end; y_pos += step) {
234
0
                const int      pos         = y_pos * pic_width + x_pos;
235
0
                const uint32_t bucket      = pic_hash[pos] & crc_mask;
236
0
                const uint32_t hash_value1 = bucket + add_value;
237
                // Keep only the first max_cand_per_bucket blocks of each bucket in exploration order.
238
0
                if (!fill) {
239
0
                    if (count[hash_value1] < max_cand_per_bucket) {
240
0
                        count[hash_value1]++;
241
0
                    }
242
0
                } else if (cursor[bucket] < count[hash_value1]) {
243
0
                    BlockHash* e   = &entries[start[hash_value1] + cursor[bucket]++];
244
0
                    e->x           = x_pos;
245
0
                    e->y           = y_pos;
246
0
                    e->hash_value2 = pic_hash[pos];
247
0
                }
248
0
            }
249
0
        }
250
251
        // Adjust offsets and step sizes with this state machine.
252
        // State 0 is needed because no blocks in pic_hash have been explored,
253
        // so exploration requires a way to account for blocks with both zero
254
        // x_offset and zero y_offset.
255
        // State 0 is always meant to be executed first, but the relative order of
256
        // states 1, 2 and 3 can be arbitrary, as long as no two adjacent blocks
257
        // are explored consecutively.
258
0
        if (x_offset == 0 && y_offset == 0) {
259
            // State 0 -> State 1: special case
260
            // This state transition will only execute when step == block_size
261
0
            x_offset = step / 2;
262
0
        } else if (x_offset == step / 2 && y_offset == 0) {
263
            // State 1 -> State 2
264
0
            x_offset = 0;
265
0
            y_offset = step / 2;
266
0
        } else if (x_offset == 0 && y_offset == step / 2) {
267
            // State 2 -> State 3
268
0
            x_offset = step / 2;
269
0
        } else {
270
0
            assert(x_offset == step / 2 && y_offset == step / 2);
271
            // State 3 -> State 1: We've fully explored all the coordinates for the
272
            // current step size, continue by halving the step size
273
0
            step /= 2;
274
0
            x_offset = step / 2;
275
0
            y_offset = 0;
276
0
        }
277
0
    }
278
0
}
279
280
bool svt_aom_rtime_alloc_svt_av1_add_to_hash_map_by_row_with_precal_data(HashTable* p_hash_table, uint32_t* pic_hash,
281
                                                                         int pic_width, int pic_height, int block_size,
282
0
                                                                         uint16_t max_cand_per_bucket) {
283
0
    const int x_end = pic_width - block_size + 1;
284
0
    const int y_end = pic_height - block_size + 1;
285
286
0
    int add_value = hash_block_size_to_index(block_size);
287
0
    assert(add_value >= 0);
288
0
    add_value <<= crc_bits;
289
290
    // Each block size owns its own range of 1 << crc_bits buckets, so it can be counted, laid out
291
    // and filled independently of the other sizes.
292
0
    explore_blocks(p_hash_table, pic_hash, pic_width, x_end, y_end, block_size, add_value, max_cand_per_bucket, false);
293
294
0
    uint32_t total = p_hash_table->num_entries;
295
0
    for (int b = 0; b < (1 << crc_bits); b++) {
296
0
        p_hash_table->bucket_start[add_value + b] = total;
297
0
        total += p_hash_table->bucket_count[add_value + b];
298
0
    }
299
0
    if (total > p_hash_table->entries_capacity) {
300
0
        EB_REALLOC_ARRAY_NO_CHECK(p_hash_table->entries, total);
301
0
        if (p_hash_table->entries == NULL) {
302
0
            p_hash_table->entries_capacity = 0;
303
0
            return false;
304
0
        }
305
0
        p_hash_table->entries_capacity = total;
306
0
    }
307
0
    p_hash_table->num_entries = total;
308
309
0
    memset(p_hash_table->fill_cursor, 0, sizeof(*p_hash_table->fill_cursor) << crc_bits);
310
0
    explore_blocks(p_hash_table, pic_hash, pic_width, x_end, y_end, block_size, add_value, max_cand_per_bucket, true);
311
0
    return true;
312
0
}
313
314
void svt_av1_get_block_hash_value(uint8_t* y_src, int stride, int block_size, uint32_t* hash_value1,
315
0
                                  uint32_t* hash_value2, int use_highbitdepth, IntraBcContext* x) {
316
0
    const int add_value = hash_block_size_to_index(block_size) << crc_bits;
317
0
    assert(add_value >= 0);
318
0
    const int crc_mask = (1 << crc_bits) - 1;
319
320
    // 2x2 subblock hash values in current CU
321
0
    int sub_block_in_width = (block_size >> 1);
322
0
    if (use_highbitdepth) {
323
0
        uint16_t  pixel_to_hash[4];
324
0
        uint16_t* y16_src = CONVERT_TO_SHORTPTR(y_src);
325
0
        for (int y_pos = 0; y_pos < block_size; y_pos += 2) {
326
0
            for (int x_pos = 0; x_pos < block_size; x_pos += 2) {
327
0
                int pos = (y_pos >> 1) * sub_block_in_width + (x_pos >> 1);
328
0
                get_pixels_in_1d_short_array_by_block_2x2(y16_src + y_pos * stride + x_pos, stride, pixel_to_hash);
329
0
                assert(pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
330
                // For HBD, we either have 40 or 48 bits of input data that the xor hash
331
                // reduce to 32 bits. We intentionally don't want to "discard" bits to
332
                // avoid any kind of biasing.
333
0
                x->hash_value_buffer[0][pos] = get_xor_hash_value_hbd(
334
0
                    pixel_to_hash[0], pixel_to_hash[1], pixel_to_hash[2], pixel_to_hash[3]);
335
0
            }
336
0
        }
337
0
    } else {
338
0
        uint8_t pixel_to_hash[4];
339
0
        for (int y_pos = 0; y_pos < block_size; y_pos += 2) {
340
0
            for (int x_pos = 0; x_pos < block_size; x_pos += 2) {
341
0
                int pos = (y_pos >> 1) * sub_block_in_width + (x_pos >> 1);
342
0
                get_pixels_in_1d_char_array_by_block_2x2(y_src + y_pos * stride + x_pos, stride, pixel_to_hash);
343
0
                assert(pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
344
                // This 2x2 hash isn't used directly as a "key" for the hash table, so
345
                // we can afford to just copy the 4 8-bit pixel values as a single
346
                // 32-bit value directly. (i.e. there are no concerns of a lack of
347
                // uniform distribution)
348
0
                x->hash_value_buffer[0][pos] = get_identity_hash_value(
349
0
                    pixel_to_hash[0], pixel_to_hash[1], pixel_to_hash[2], pixel_to_hash[3]);
350
0
            }
351
0
        }
352
0
    }
353
354
0
    int src_sub_block_in_width = sub_block_in_width;
355
0
    sub_block_in_width >>= 1;
356
357
0
    int src_idx = 0;
358
0
    int dst_idx = 1 - src_idx;
359
360
    // 4x4 subblock hash values to current block hash values
361
0
    uint32_t to_hash[4];
362
0
    for (int sub_width = 4; sub_width <= block_size; sub_width *= 2, src_idx = 1 - src_idx) {
363
0
        dst_idx = 1 - src_idx;
364
365
0
        int dst_pos = 0;
366
0
        for (int y_pos = 0; y_pos < sub_block_in_width; y_pos++) {
367
0
            for (int x_pos = 0; x_pos < sub_block_in_width; x_pos++) {
368
0
                int src_pos = (y_pos << 1) * src_sub_block_in_width + (x_pos << 1);
369
370
0
                assert(src_pos + 1 < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
371
0
                assert(src_pos + src_sub_block_in_width + 1 < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
372
0
                assert(dst_pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
373
374
0
                to_hash[0] = x->hash_value_buffer[src_idx][src_pos];
375
0
                to_hash[1] = x->hash_value_buffer[src_idx][src_pos + 1];
376
0
                to_hash[2] = x->hash_value_buffer[src_idx][src_pos + src_sub_block_in_width];
377
0
                to_hash[3] = x->hash_value_buffer[src_idx][src_pos + src_sub_block_in_width + 1];
378
379
0
                x->hash_value_buffer[dst_idx][dst_pos] = svt_av1_get_crc32c_value((uint8_t*)to_hash, sizeof(to_hash));
380
0
                dst_pos++;
381
0
            }
382
0
        }
383
384
0
        src_sub_block_in_width = sub_block_in_width;
385
0
        sub_block_in_width >>= 1;
386
0
    }
387
388
0
    *hash_value1 = (x->hash_value_buffer[dst_idx][0] & crc_mask) + add_value;
389
0
    *hash_value2 = x->hash_value_buffer[dst_idx][0];
390
0
}