Coverage Report

Created: 2026-07-16 06:32

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