xref: /aosp_15_r20/external/libaom/av1/encoder/hash_motion.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 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 <assert.h>
13 #include <stdbool.h>
14 
15 #include "config/av1_rtcd.h"
16 
17 #include "av1/encoder/block.h"
18 #include "av1/encoder/hash.h"
19 #include "av1/encoder/hash_motion.h"
20 
21 #define kSrcBits 16
22 #define kBlockSizeBits 3
23 #define kMaxAddr (1 << (kSrcBits + kBlockSizeBits))
24 
25 // TODO([email protected]): is higher than 8 bits screen content supported?
26 // If yes, fix this function
get_pixels_in_1D_char_array_by_block_2x2(const uint8_t * y_src,int stride,uint8_t * p_pixels_in1D)27 static void get_pixels_in_1D_char_array_by_block_2x2(const uint8_t *y_src,
28                                                      int stride,
29                                                      uint8_t *p_pixels_in1D) {
30   const uint8_t *p_pel = y_src;
31   int index = 0;
32   for (int i = 0; i < 2; i++) {
33     for (int j = 0; j < 2; j++) {
34       p_pixels_in1D[index++] = p_pel[j];
35     }
36     p_pel += stride;
37   }
38 }
39 
get_pixels_in_1D_short_array_by_block_2x2(const uint16_t * y_src,int stride,uint16_t * p_pixels_in1D)40 static void get_pixels_in_1D_short_array_by_block_2x2(const uint16_t *y_src,
41                                                       int stride,
42                                                       uint16_t *p_pixels_in1D) {
43   const uint16_t *p_pel = y_src;
44   int index = 0;
45   for (int i = 0; i < 2; i++) {
46     for (int j = 0; j < 2; j++) {
47       p_pixels_in1D[index++] = p_pel[j];
48     }
49     p_pel += stride;
50   }
51 }
52 
is_block_2x2_row_same_value(const uint8_t * p)53 static int is_block_2x2_row_same_value(const uint8_t *p) {
54   if (p[0] != p[1] || p[2] != p[3]) {
55     return 0;
56   }
57   return 1;
58 }
59 
is_block16_2x2_row_same_value(const uint16_t * p)60 static int is_block16_2x2_row_same_value(const uint16_t *p) {
61   if (p[0] != p[1] || p[2] != p[3]) {
62     return 0;
63   }
64   return 1;
65 }
66 
is_block_2x2_col_same_value(const uint8_t * p)67 static int is_block_2x2_col_same_value(const uint8_t *p) {
68   if ((p[0] != p[2]) || (p[1] != p[3])) {
69     return 0;
70   }
71   return 1;
72 }
73 
is_block16_2x2_col_same_value(const uint16_t * p)74 static int is_block16_2x2_col_same_value(const uint16_t *p) {
75   if ((p[0] != p[2]) || (p[1] != p[3])) {
76     return 0;
77   }
78   return 1;
79 }
80 
81 // the hash value (hash_value1 consists two parts, the first 3 bits relate to
82 // the block size and the remaining 16 bits are the crc values. This fuction
83 // is used to get the first 3 bits.
hash_block_size_to_index(int block_size)84 static int hash_block_size_to_index(int block_size) {
85   switch (block_size) {
86     case 4: return 0;
87     case 8: return 1;
88     case 16: return 2;
89     case 32: return 3;
90     case 64: return 4;
91     case 128: return 5;
92     default: return -1;
93   }
94 }
95 
av1_hash_table_init(IntraBCHashInfo * intrabc_hash_info)96 void av1_hash_table_init(IntraBCHashInfo *intrabc_hash_info) {
97   if (!intrabc_hash_info->g_crc_initialized) {
98     av1_crc_calculator_init(&intrabc_hash_info->crc_calculator1, 24, 0x5D6DCB);
99     av1_crc_calculator_init(&intrabc_hash_info->crc_calculator2, 24, 0x864CFB);
100     intrabc_hash_info->g_crc_initialized = 1;
101   }
102   intrabc_hash_info->intrabc_hash_table.p_lookup_table = NULL;
103 }
104 
clear_all(hash_table * p_hash_table)105 static void clear_all(hash_table *p_hash_table) {
106   if (p_hash_table->p_lookup_table == NULL) {
107     return;
108   }
109   for (int i = 0; i < kMaxAddr; i++) {
110     if (p_hash_table->p_lookup_table[i] != NULL) {
111       aom_vector_destroy(p_hash_table->p_lookup_table[i]);
112       aom_free(p_hash_table->p_lookup_table[i]);
113       p_hash_table->p_lookup_table[i] = NULL;
114     }
115   }
116 }
117 
av1_hash_table_destroy(hash_table * p_hash_table)118 void av1_hash_table_destroy(hash_table *p_hash_table) {
119   clear_all(p_hash_table);
120   aom_free(p_hash_table->p_lookup_table);
121   p_hash_table->p_lookup_table = NULL;
122 }
123 
av1_hash_table_create(hash_table * p_hash_table)124 bool av1_hash_table_create(hash_table *p_hash_table) {
125   if (p_hash_table->p_lookup_table != NULL) {
126     clear_all(p_hash_table);
127     return true;
128   }
129   p_hash_table->p_lookup_table =
130       (Vector **)aom_calloc(kMaxAddr, sizeof(p_hash_table->p_lookup_table[0]));
131   if (!p_hash_table->p_lookup_table) return false;
132   return true;
133 }
134 
hash_table_add_to_table(hash_table * p_hash_table,uint32_t hash_value,block_hash * curr_block_hash)135 static bool hash_table_add_to_table(hash_table *p_hash_table,
136                                     uint32_t hash_value,
137                                     block_hash *curr_block_hash) {
138   if (p_hash_table->p_lookup_table[hash_value] == NULL) {
139     p_hash_table->p_lookup_table[hash_value] =
140         aom_malloc(sizeof(p_hash_table->p_lookup_table[0][0]));
141     if (p_hash_table->p_lookup_table[hash_value] == NULL) {
142       return false;
143     }
144     if (aom_vector_setup(p_hash_table->p_lookup_table[hash_value], 10,
145                          sizeof(curr_block_hash[0])) == VECTOR_ERROR)
146       return false;
147     if (aom_vector_push_back(p_hash_table->p_lookup_table[hash_value],
148                              curr_block_hash) == VECTOR_ERROR)
149       return false;
150   } else {
151     if (aom_vector_push_back(p_hash_table->p_lookup_table[hash_value],
152                              curr_block_hash) == VECTOR_ERROR)
153       return false;
154   }
155   return true;
156 }
157 
av1_hash_table_count(const hash_table * p_hash_table,uint32_t hash_value)158 int32_t av1_hash_table_count(const hash_table *p_hash_table,
159                              uint32_t hash_value) {
160   if (p_hash_table->p_lookup_table[hash_value] == NULL) {
161     return 0;
162   } else {
163     return (int32_t)(p_hash_table->p_lookup_table[hash_value]->size);
164   }
165 }
166 
av1_hash_get_first_iterator(hash_table * p_hash_table,uint32_t hash_value)167 Iterator av1_hash_get_first_iterator(hash_table *p_hash_table,
168                                      uint32_t hash_value) {
169   assert(av1_hash_table_count(p_hash_table, hash_value) > 0);
170   return aom_vector_begin(p_hash_table->p_lookup_table[hash_value]);
171 }
172 
av1_generate_block_2x2_hash_value(IntraBCHashInfo * intrabc_hash_info,const YV12_BUFFER_CONFIG * picture,uint32_t * pic_block_hash[2],int8_t * pic_block_same_info[3])173 void av1_generate_block_2x2_hash_value(IntraBCHashInfo *intrabc_hash_info,
174                                        const YV12_BUFFER_CONFIG *picture,
175                                        uint32_t *pic_block_hash[2],
176                                        int8_t *pic_block_same_info[3]) {
177   const int width = 2;
178   const int height = 2;
179   const int x_end = picture->y_crop_width - width + 1;
180   const int y_end = picture->y_crop_height - height + 1;
181   CRC_CALCULATOR *calc_1 = &intrabc_hash_info->crc_calculator1;
182   CRC_CALCULATOR *calc_2 = &intrabc_hash_info->crc_calculator2;
183 
184   const int length = width * 2;
185   if (picture->flags & YV12_FLAG_HIGHBITDEPTH) {
186     uint16_t p[4];
187     int pos = 0;
188     for (int y_pos = 0; y_pos < y_end; y_pos++) {
189       for (int x_pos = 0; x_pos < x_end; x_pos++) {
190         get_pixels_in_1D_short_array_by_block_2x2(
191             CONVERT_TO_SHORTPTR(picture->y_buffer) + y_pos * picture->y_stride +
192                 x_pos,
193             picture->y_stride, p);
194         pic_block_same_info[0][pos] = is_block16_2x2_row_same_value(p);
195         pic_block_same_info[1][pos] = is_block16_2x2_col_same_value(p);
196 
197         pic_block_hash[0][pos] =
198             av1_get_crc_value(calc_1, (uint8_t *)p, length * sizeof(p[0]));
199         pic_block_hash[1][pos] =
200             av1_get_crc_value(calc_2, (uint8_t *)p, length * sizeof(p[0]));
201         pos++;
202       }
203       pos += width - 1;
204     }
205   } else {
206     uint8_t p[4];
207     int pos = 0;
208     for (int y_pos = 0; y_pos < y_end; y_pos++) {
209       for (int x_pos = 0; x_pos < x_end; x_pos++) {
210         get_pixels_in_1D_char_array_by_block_2x2(
211             picture->y_buffer + y_pos * picture->y_stride + x_pos,
212             picture->y_stride, p);
213         pic_block_same_info[0][pos] = is_block_2x2_row_same_value(p);
214         pic_block_same_info[1][pos] = is_block_2x2_col_same_value(p);
215 
216         pic_block_hash[0][pos] =
217             av1_get_crc_value(calc_1, p, length * sizeof(p[0]));
218         pic_block_hash[1][pos] =
219             av1_get_crc_value(calc_2, p, length * sizeof(p[0]));
220         pos++;
221       }
222       pos += width - 1;
223     }
224   }
225 }
226 
av1_generate_block_hash_value(IntraBCHashInfo * intrabc_hash_info,const YV12_BUFFER_CONFIG * picture,int block_size,uint32_t * src_pic_block_hash[2],uint32_t * dst_pic_block_hash[2],int8_t * src_pic_block_same_info[3],int8_t * dst_pic_block_same_info[3])227 void av1_generate_block_hash_value(IntraBCHashInfo *intrabc_hash_info,
228                                    const YV12_BUFFER_CONFIG *picture,
229                                    int block_size,
230                                    uint32_t *src_pic_block_hash[2],
231                                    uint32_t *dst_pic_block_hash[2],
232                                    int8_t *src_pic_block_same_info[3],
233                                    int8_t *dst_pic_block_same_info[3]) {
234   CRC_CALCULATOR *calc_1 = &intrabc_hash_info->crc_calculator1;
235   CRC_CALCULATOR *calc_2 = &intrabc_hash_info->crc_calculator2;
236 
237   const int pic_width = picture->y_crop_width;
238   const int x_end = picture->y_crop_width - block_size + 1;
239   const int y_end = picture->y_crop_height - block_size + 1;
240 
241   const int src_size = block_size >> 1;
242   const int quad_size = block_size >> 2;
243 
244   uint32_t p[4];
245   const int length = sizeof(p);
246 
247   int pos = 0;
248   for (int y_pos = 0; y_pos < y_end; y_pos++) {
249     for (int x_pos = 0; x_pos < x_end; x_pos++) {
250       p[0] = src_pic_block_hash[0][pos];
251       p[1] = src_pic_block_hash[0][pos + src_size];
252       p[2] = src_pic_block_hash[0][pos + src_size * pic_width];
253       p[3] = src_pic_block_hash[0][pos + src_size * pic_width + src_size];
254       dst_pic_block_hash[0][pos] =
255           av1_get_crc_value(calc_1, (uint8_t *)p, length);
256 
257       p[0] = src_pic_block_hash[1][pos];
258       p[1] = src_pic_block_hash[1][pos + src_size];
259       p[2] = src_pic_block_hash[1][pos + src_size * pic_width];
260       p[3] = src_pic_block_hash[1][pos + src_size * pic_width + src_size];
261       dst_pic_block_hash[1][pos] =
262           av1_get_crc_value(calc_2, (uint8_t *)p, length);
263 
264       dst_pic_block_same_info[0][pos] =
265           src_pic_block_same_info[0][pos] &&
266           src_pic_block_same_info[0][pos + quad_size] &&
267           src_pic_block_same_info[0][pos + src_size] &&
268           src_pic_block_same_info[0][pos + src_size * pic_width] &&
269           src_pic_block_same_info[0][pos + src_size * pic_width + quad_size] &&
270           src_pic_block_same_info[0][pos + src_size * pic_width + src_size];
271 
272       dst_pic_block_same_info[1][pos] =
273           src_pic_block_same_info[1][pos] &&
274           src_pic_block_same_info[1][pos + src_size] &&
275           src_pic_block_same_info[1][pos + quad_size * pic_width] &&
276           src_pic_block_same_info[1][pos + quad_size * pic_width + src_size] &&
277           src_pic_block_same_info[1][pos + src_size * pic_width] &&
278           src_pic_block_same_info[1][pos + src_size * pic_width + src_size];
279       pos++;
280     }
281     pos += block_size - 1;
282   }
283 
284   if (block_size >= 4) {
285     const int size_minus_1 = block_size - 1;
286     pos = 0;
287     for (int y_pos = 0; y_pos < y_end; y_pos++) {
288       for (int x_pos = 0; x_pos < x_end; x_pos++) {
289         dst_pic_block_same_info[2][pos] =
290             (!dst_pic_block_same_info[0][pos] &&
291              !dst_pic_block_same_info[1][pos]) ||
292             (((x_pos & size_minus_1) == 0) && ((y_pos & size_minus_1) == 0));
293         pos++;
294       }
295       pos += block_size - 1;
296     }
297   }
298 }
299 
av1_add_to_hash_map_by_row_with_precal_data(hash_table * p_hash_table,uint32_t * pic_hash[2],int8_t * pic_is_same,int pic_width,int pic_height,int block_size)300 bool av1_add_to_hash_map_by_row_with_precal_data(hash_table *p_hash_table,
301                                                  uint32_t *pic_hash[2],
302                                                  int8_t *pic_is_same,
303                                                  int pic_width, int pic_height,
304                                                  int block_size) {
305   const int x_end = pic_width - block_size + 1;
306   const int y_end = pic_height - block_size + 1;
307 
308   const int8_t *src_is_added = pic_is_same;
309   const uint32_t *src_hash[2] = { pic_hash[0], pic_hash[1] };
310 
311   int add_value = hash_block_size_to_index(block_size);
312   assert(add_value >= 0);
313   add_value <<= kSrcBits;
314   const int crc_mask = (1 << kSrcBits) - 1;
315 
316   for (int x_pos = 0; x_pos < x_end; x_pos++) {
317     for (int y_pos = 0; y_pos < y_end; y_pos++) {
318       const int pos = y_pos * pic_width + x_pos;
319       // valid data
320       if (src_is_added[pos]) {
321         block_hash curr_block_hash;
322         curr_block_hash.x = x_pos;
323         curr_block_hash.y = y_pos;
324 
325         const uint32_t hash_value1 = (src_hash[0][pos] & crc_mask) + add_value;
326         curr_block_hash.hash_value2 = src_hash[1][pos];
327 
328         if (!hash_table_add_to_table(p_hash_table, hash_value1,
329                                      &curr_block_hash)) {
330           return false;
331         }
332       }
333     }
334   }
335   return true;
336 }
337 
av1_hash_is_horizontal_perfect(const YV12_BUFFER_CONFIG * picture,int block_size,int x_start,int y_start)338 int av1_hash_is_horizontal_perfect(const YV12_BUFFER_CONFIG *picture,
339                                    int block_size, int x_start, int y_start) {
340   const int stride = picture->y_stride;
341   const uint8_t *p = picture->y_buffer + y_start * stride + x_start;
342 
343   if (picture->flags & YV12_FLAG_HIGHBITDEPTH) {
344     const uint16_t *p16 = CONVERT_TO_SHORTPTR(p);
345     for (int i = 0; i < block_size; i++) {
346       for (int j = 1; j < block_size; j++) {
347         if (p16[j] != p16[0]) {
348           return 0;
349         }
350       }
351       p16 += stride;
352     }
353   } else {
354     for (int i = 0; i < block_size; i++) {
355       for (int j = 1; j < block_size; j++) {
356         if (p[j] != p[0]) {
357           return 0;
358         }
359       }
360       p += stride;
361     }
362   }
363 
364   return 1;
365 }
366 
av1_hash_is_vertical_perfect(const YV12_BUFFER_CONFIG * picture,int block_size,int x_start,int y_start)367 int av1_hash_is_vertical_perfect(const YV12_BUFFER_CONFIG *picture,
368                                  int block_size, int x_start, int y_start) {
369   const int stride = picture->y_stride;
370   const uint8_t *p = picture->y_buffer + y_start * stride + x_start;
371 
372   if (picture->flags & YV12_FLAG_HIGHBITDEPTH) {
373     const uint16_t *p16 = CONVERT_TO_SHORTPTR(p);
374     for (int i = 0; i < block_size; i++) {
375       for (int j = 1; j < block_size; j++) {
376         if (p16[j * stride + i] != p16[i]) {
377           return 0;
378         }
379       }
380     }
381   } else {
382     for (int i = 0; i < block_size; i++) {
383       for (int j = 1; j < block_size; j++) {
384         if (p[j * stride + i] != p[i]) {
385           return 0;
386         }
387       }
388     }
389   }
390   return 1;
391 }
392 
av1_get_block_hash_value(IntraBCHashInfo * intrabc_hash_info,const uint8_t * y_src,int stride,int block_size,uint32_t * hash_value1,uint32_t * hash_value2,int use_highbitdepth)393 void av1_get_block_hash_value(IntraBCHashInfo *intrabc_hash_info,
394                               const uint8_t *y_src, int stride, int block_size,
395                               uint32_t *hash_value1, uint32_t *hash_value2,
396                               int use_highbitdepth) {
397   int add_value = hash_block_size_to_index(block_size);
398   assert(add_value >= 0);
399   add_value <<= kSrcBits;
400   const int crc_mask = (1 << kSrcBits) - 1;
401 
402   CRC_CALCULATOR *calc_1 = &intrabc_hash_info->crc_calculator1;
403   CRC_CALCULATOR *calc_2 = &intrabc_hash_info->crc_calculator2;
404   uint32_t **buf_1 = intrabc_hash_info->hash_value_buffer[0];
405   uint32_t **buf_2 = intrabc_hash_info->hash_value_buffer[1];
406 
407   // 2x2 subblock hash values in current CU
408   int sub_block_in_width = (block_size >> 1);
409   if (use_highbitdepth) {
410     uint16_t pixel_to_hash[4];
411     uint16_t *y16_src = CONVERT_TO_SHORTPTR(y_src);
412     for (int y_pos = 0; y_pos < block_size; y_pos += 2) {
413       for (int x_pos = 0; x_pos < block_size; x_pos += 2) {
414         int pos = (y_pos >> 1) * sub_block_in_width + (x_pos >> 1);
415         get_pixels_in_1D_short_array_by_block_2x2(
416             y16_src + y_pos * stride + x_pos, stride, pixel_to_hash);
417         assert(pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
418         buf_1[0][pos] = av1_get_crc_value(calc_1, (uint8_t *)pixel_to_hash,
419                                           sizeof(pixel_to_hash));
420         buf_2[0][pos] = av1_get_crc_value(calc_2, (uint8_t *)pixel_to_hash,
421                                           sizeof(pixel_to_hash));
422       }
423     }
424   } else {
425     uint8_t pixel_to_hash[4];
426     for (int y_pos = 0; y_pos < block_size; y_pos += 2) {
427       for (int x_pos = 0; x_pos < block_size; x_pos += 2) {
428         int pos = (y_pos >> 1) * sub_block_in_width + (x_pos >> 1);
429         get_pixels_in_1D_char_array_by_block_2x2(y_src + y_pos * stride + x_pos,
430                                                  stride, pixel_to_hash);
431         assert(pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
432         buf_1[0][pos] =
433             av1_get_crc_value(calc_1, pixel_to_hash, sizeof(pixel_to_hash));
434         buf_2[0][pos] =
435             av1_get_crc_value(calc_2, pixel_to_hash, sizeof(pixel_to_hash));
436       }
437     }
438   }
439 
440   int src_sub_block_in_width = sub_block_in_width;
441   sub_block_in_width >>= 1;
442 
443   int src_idx = 1;
444   int dst_idx = 0;
445 
446   // 4x4 subblock hash values to current block hash values
447   uint32_t to_hash[4];
448   for (int sub_width = 4; sub_width <= block_size; sub_width *= 2) {
449     src_idx = 1 - src_idx;
450     dst_idx = 1 - dst_idx;
451 
452     int dst_pos = 0;
453     for (int y_pos = 0; y_pos < sub_block_in_width; y_pos++) {
454       for (int x_pos = 0; x_pos < sub_block_in_width; x_pos++) {
455         int srcPos = (y_pos << 1) * src_sub_block_in_width + (x_pos << 1);
456 
457         assert(srcPos + 1 < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
458         assert(srcPos + src_sub_block_in_width + 1 <
459                AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
460         assert(dst_pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
461         to_hash[0] = buf_1[src_idx][srcPos];
462         to_hash[1] = buf_1[src_idx][srcPos + 1];
463         to_hash[2] = buf_1[src_idx][srcPos + src_sub_block_in_width];
464         to_hash[3] = buf_1[src_idx][srcPos + src_sub_block_in_width + 1];
465 
466         buf_1[dst_idx][dst_pos] =
467             av1_get_crc_value(calc_1, (uint8_t *)to_hash, sizeof(to_hash));
468 
469         to_hash[0] = buf_2[src_idx][srcPos];
470         to_hash[1] = buf_2[src_idx][srcPos + 1];
471         to_hash[2] = buf_2[src_idx][srcPos + src_sub_block_in_width];
472         to_hash[3] = buf_2[src_idx][srcPos + src_sub_block_in_width + 1];
473         buf_2[dst_idx][dst_pos] =
474             av1_get_crc_value(calc_2, (uint8_t *)to_hash, sizeof(to_hash));
475         dst_pos++;
476       }
477     }
478 
479     src_sub_block_in_width = sub_block_in_width;
480     sub_block_in_width >>= 1;
481   }
482 
483   *hash_value1 = (buf_1[dst_idx][0] & crc_mask) + add_value;
484   *hash_value2 = buf_2[dst_idx][0];
485 }
486