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(youzhou@microsoft.com): 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
av1_hash_table_clear_all(hash_table * p_hash_table)105 void av1_hash_table_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 av1_hash_table_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 av1_hash_table_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_has_exact_match(hash_table * p_hash_table,uint32_t hash_value1,uint32_t hash_value2)173 int32_t av1_has_exact_match(hash_table *p_hash_table, uint32_t hash_value1,
174 uint32_t hash_value2) {
175 if (p_hash_table->p_lookup_table[hash_value1] == NULL) {
176 return 0;
177 }
178 Iterator iterator =
179 aom_vector_begin(p_hash_table->p_lookup_table[hash_value1]);
180 Iterator last = aom_vector_end(p_hash_table->p_lookup_table[hash_value1]);
181 for (; !aom_iterator_equals(&iterator, &last);
182 aom_iterator_increment(&iterator)) {
183 if ((*(block_hash *)aom_iterator_get(&iterator)).hash_value2 ==
184 hash_value2) {
185 return 1;
186 }
187 }
188 return 0;
189 }
190
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])191 void av1_generate_block_2x2_hash_value(IntraBCHashInfo *intrabc_hash_info,
192 const YV12_BUFFER_CONFIG *picture,
193 uint32_t *pic_block_hash[2],
194 int8_t *pic_block_same_info[3]) {
195 const int width = 2;
196 const int height = 2;
197 const int x_end = picture->y_crop_width - width + 1;
198 const int y_end = picture->y_crop_height - height + 1;
199 CRC_CALCULATOR *calc_1 = &intrabc_hash_info->crc_calculator1;
200 CRC_CALCULATOR *calc_2 = &intrabc_hash_info->crc_calculator2;
201
202 const int length = width * 2;
203 if (picture->flags & YV12_FLAG_HIGHBITDEPTH) {
204 uint16_t p[4];
205 int pos = 0;
206 for (int y_pos = 0; y_pos < y_end; y_pos++) {
207 for (int x_pos = 0; x_pos < x_end; x_pos++) {
208 get_pixels_in_1D_short_array_by_block_2x2(
209 CONVERT_TO_SHORTPTR(picture->y_buffer) + y_pos * picture->y_stride +
210 x_pos,
211 picture->y_stride, p);
212 pic_block_same_info[0][pos] = is_block16_2x2_row_same_value(p);
213 pic_block_same_info[1][pos] = is_block16_2x2_col_same_value(p);
214
215 pic_block_hash[0][pos] =
216 av1_get_crc_value(calc_1, (uint8_t *)p, length * sizeof(p[0]));
217 pic_block_hash[1][pos] =
218 av1_get_crc_value(calc_2, (uint8_t *)p, length * sizeof(p[0]));
219 pos++;
220 }
221 pos += width - 1;
222 }
223 } else {
224 uint8_t p[4];
225 int pos = 0;
226 for (int y_pos = 0; y_pos < y_end; y_pos++) {
227 for (int x_pos = 0; x_pos < x_end; x_pos++) {
228 get_pixels_in_1D_char_array_by_block_2x2(
229 picture->y_buffer + y_pos * picture->y_stride + x_pos,
230 picture->y_stride, p);
231 pic_block_same_info[0][pos] = is_block_2x2_row_same_value(p);
232 pic_block_same_info[1][pos] = is_block_2x2_col_same_value(p);
233
234 pic_block_hash[0][pos] =
235 av1_get_crc_value(calc_1, p, length * sizeof(p[0]));
236 pic_block_hash[1][pos] =
237 av1_get_crc_value(calc_2, p, length * sizeof(p[0]));
238 pos++;
239 }
240 pos += width - 1;
241 }
242 }
243 }
244
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])245 void av1_generate_block_hash_value(IntraBCHashInfo *intrabc_hash_info,
246 const YV12_BUFFER_CONFIG *picture,
247 int block_size,
248 uint32_t *src_pic_block_hash[2],
249 uint32_t *dst_pic_block_hash[2],
250 int8_t *src_pic_block_same_info[3],
251 int8_t *dst_pic_block_same_info[3]) {
252 CRC_CALCULATOR *calc_1 = &intrabc_hash_info->crc_calculator1;
253 CRC_CALCULATOR *calc_2 = &intrabc_hash_info->crc_calculator2;
254
255 const int pic_width = picture->y_crop_width;
256 const int x_end = picture->y_crop_width - block_size + 1;
257 const int y_end = picture->y_crop_height - block_size + 1;
258
259 const int src_size = block_size >> 1;
260 const int quad_size = block_size >> 2;
261
262 uint32_t p[4];
263 const int length = sizeof(p);
264
265 int pos = 0;
266 for (int y_pos = 0; y_pos < y_end; y_pos++) {
267 for (int x_pos = 0; x_pos < x_end; x_pos++) {
268 p[0] = src_pic_block_hash[0][pos];
269 p[1] = src_pic_block_hash[0][pos + src_size];
270 p[2] = src_pic_block_hash[0][pos + src_size * pic_width];
271 p[3] = src_pic_block_hash[0][pos + src_size * pic_width + src_size];
272 dst_pic_block_hash[0][pos] =
273 av1_get_crc_value(calc_1, (uint8_t *)p, length);
274
275 p[0] = src_pic_block_hash[1][pos];
276 p[1] = src_pic_block_hash[1][pos + src_size];
277 p[2] = src_pic_block_hash[1][pos + src_size * pic_width];
278 p[3] = src_pic_block_hash[1][pos + src_size * pic_width + src_size];
279 dst_pic_block_hash[1][pos] =
280 av1_get_crc_value(calc_2, (uint8_t *)p, length);
281
282 dst_pic_block_same_info[0][pos] =
283 src_pic_block_same_info[0][pos] &&
284 src_pic_block_same_info[0][pos + quad_size] &&
285 src_pic_block_same_info[0][pos + src_size] &&
286 src_pic_block_same_info[0][pos + src_size * pic_width] &&
287 src_pic_block_same_info[0][pos + src_size * pic_width + quad_size] &&
288 src_pic_block_same_info[0][pos + src_size * pic_width + src_size];
289
290 dst_pic_block_same_info[1][pos] =
291 src_pic_block_same_info[1][pos] &&
292 src_pic_block_same_info[1][pos + src_size] &&
293 src_pic_block_same_info[1][pos + quad_size * pic_width] &&
294 src_pic_block_same_info[1][pos + quad_size * pic_width + src_size] &&
295 src_pic_block_same_info[1][pos + src_size * pic_width] &&
296 src_pic_block_same_info[1][pos + src_size * pic_width + src_size];
297 pos++;
298 }
299 pos += block_size - 1;
300 }
301
302 if (block_size >= 4) {
303 const int size_minus_1 = block_size - 1;
304 pos = 0;
305 for (int y_pos = 0; y_pos < y_end; y_pos++) {
306 for (int x_pos = 0; x_pos < x_end; x_pos++) {
307 dst_pic_block_same_info[2][pos] =
308 (!dst_pic_block_same_info[0][pos] &&
309 !dst_pic_block_same_info[1][pos]) ||
310 (((x_pos & size_minus_1) == 0) && ((y_pos & size_minus_1) == 0));
311 pos++;
312 }
313 pos += block_size - 1;
314 }
315 }
316 }
317
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)318 bool av1_add_to_hash_map_by_row_with_precal_data(hash_table *p_hash_table,
319 uint32_t *pic_hash[2],
320 int8_t *pic_is_same,
321 int pic_width, int pic_height,
322 int block_size) {
323 const int x_end = pic_width - block_size + 1;
324 const int y_end = pic_height - block_size + 1;
325
326 const int8_t *src_is_added = pic_is_same;
327 const uint32_t *src_hash[2] = { pic_hash[0], pic_hash[1] };
328
329 int add_value = hash_block_size_to_index(block_size);
330 assert(add_value >= 0);
331 add_value <<= kSrcBits;
332 const int crc_mask = (1 << kSrcBits) - 1;
333
334 for (int x_pos = 0; x_pos < x_end; x_pos++) {
335 for (int y_pos = 0; y_pos < y_end; y_pos++) {
336 const int pos = y_pos * pic_width + x_pos;
337 // valid data
338 if (src_is_added[pos]) {
339 block_hash curr_block_hash;
340 curr_block_hash.x = x_pos;
341 curr_block_hash.y = y_pos;
342
343 const uint32_t hash_value1 = (src_hash[0][pos] & crc_mask) + add_value;
344 curr_block_hash.hash_value2 = src_hash[1][pos];
345
346 if (!hash_table_add_to_table(p_hash_table, hash_value1,
347 &curr_block_hash)) {
348 return false;
349 }
350 }
351 }
352 }
353 return true;
354 }
355
av1_hash_is_horizontal_perfect(const YV12_BUFFER_CONFIG * picture,int block_size,int x_start,int y_start)356 int av1_hash_is_horizontal_perfect(const YV12_BUFFER_CONFIG *picture,
357 int block_size, int x_start, int y_start) {
358 const int stride = picture->y_stride;
359 const uint8_t *p = picture->y_buffer + y_start * stride + x_start;
360
361 if (picture->flags & YV12_FLAG_HIGHBITDEPTH) {
362 const uint16_t *p16 = CONVERT_TO_SHORTPTR(p);
363 for (int i = 0; i < block_size; i++) {
364 for (int j = 1; j < block_size; j++) {
365 if (p16[j] != p16[0]) {
366 return 0;
367 }
368 }
369 p16 += stride;
370 }
371 } else {
372 for (int i = 0; i < block_size; i++) {
373 for (int j = 1; j < block_size; j++) {
374 if (p[j] != p[0]) {
375 return 0;
376 }
377 }
378 p += stride;
379 }
380 }
381
382 return 1;
383 }
384
av1_hash_is_vertical_perfect(const YV12_BUFFER_CONFIG * picture,int block_size,int x_start,int y_start)385 int av1_hash_is_vertical_perfect(const YV12_BUFFER_CONFIG *picture,
386 int block_size, int x_start, int y_start) {
387 const int stride = picture->y_stride;
388 const uint8_t *p = picture->y_buffer + y_start * stride + x_start;
389
390 if (picture->flags & YV12_FLAG_HIGHBITDEPTH) {
391 const uint16_t *p16 = CONVERT_TO_SHORTPTR(p);
392 for (int i = 0; i < block_size; i++) {
393 for (int j = 1; j < block_size; j++) {
394 if (p16[j * stride + i] != p16[i]) {
395 return 0;
396 }
397 }
398 }
399 } else {
400 for (int i = 0; i < block_size; i++) {
401 for (int j = 1; j < block_size; j++) {
402 if (p[j * stride + i] != p[i]) {
403 return 0;
404 }
405 }
406 }
407 }
408 return 1;
409 }
410
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)411 void av1_get_block_hash_value(IntraBCHashInfo *intrabc_hash_info,
412 const uint8_t *y_src, int stride, int block_size,
413 uint32_t *hash_value1, uint32_t *hash_value2,
414 int use_highbitdepth) {
415 int add_value = hash_block_size_to_index(block_size);
416 assert(add_value >= 0);
417 add_value <<= kSrcBits;
418 const int crc_mask = (1 << kSrcBits) - 1;
419
420 CRC_CALCULATOR *calc_1 = &intrabc_hash_info->crc_calculator1;
421 CRC_CALCULATOR *calc_2 = &intrabc_hash_info->crc_calculator2;
422 uint32_t **buf_1 = intrabc_hash_info->hash_value_buffer[0];
423 uint32_t **buf_2 = intrabc_hash_info->hash_value_buffer[1];
424
425 // 2x2 subblock hash values in current CU
426 int sub_block_in_width = (block_size >> 1);
427 if (use_highbitdepth) {
428 uint16_t pixel_to_hash[4];
429 uint16_t *y16_src = CONVERT_TO_SHORTPTR(y_src);
430 for (int y_pos = 0; y_pos < block_size; y_pos += 2) {
431 for (int x_pos = 0; x_pos < block_size; x_pos += 2) {
432 int pos = (y_pos >> 1) * sub_block_in_width + (x_pos >> 1);
433 get_pixels_in_1D_short_array_by_block_2x2(
434 y16_src + y_pos * stride + x_pos, stride, pixel_to_hash);
435 assert(pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
436 buf_1[0][pos] = av1_get_crc_value(calc_1, (uint8_t *)pixel_to_hash,
437 sizeof(pixel_to_hash));
438 buf_2[0][pos] = av1_get_crc_value(calc_2, (uint8_t *)pixel_to_hash,
439 sizeof(pixel_to_hash));
440 }
441 }
442 } else {
443 uint8_t pixel_to_hash[4];
444 for (int y_pos = 0; y_pos < block_size; y_pos += 2) {
445 for (int x_pos = 0; x_pos < block_size; x_pos += 2) {
446 int pos = (y_pos >> 1) * sub_block_in_width + (x_pos >> 1);
447 get_pixels_in_1D_char_array_by_block_2x2(y_src + y_pos * stride + x_pos,
448 stride, pixel_to_hash);
449 assert(pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
450 buf_1[0][pos] =
451 av1_get_crc_value(calc_1, pixel_to_hash, sizeof(pixel_to_hash));
452 buf_2[0][pos] =
453 av1_get_crc_value(calc_2, pixel_to_hash, sizeof(pixel_to_hash));
454 }
455 }
456 }
457
458 int src_sub_block_in_width = sub_block_in_width;
459 sub_block_in_width >>= 1;
460
461 int src_idx = 1;
462 int dst_idx = 0;
463
464 // 4x4 subblock hash values to current block hash values
465 uint32_t to_hash[4];
466 for (int sub_width = 4; sub_width <= block_size; sub_width *= 2) {
467 src_idx = 1 - src_idx;
468 dst_idx = 1 - dst_idx;
469
470 int dst_pos = 0;
471 for (int y_pos = 0; y_pos < sub_block_in_width; y_pos++) {
472 for (int x_pos = 0; x_pos < sub_block_in_width; x_pos++) {
473 int srcPos = (y_pos << 1) * src_sub_block_in_width + (x_pos << 1);
474
475 assert(srcPos + 1 < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
476 assert(srcPos + src_sub_block_in_width + 1 <
477 AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
478 assert(dst_pos < AOM_BUFFER_SIZE_FOR_BLOCK_HASH);
479 to_hash[0] = buf_1[src_idx][srcPos];
480 to_hash[1] = buf_1[src_idx][srcPos + 1];
481 to_hash[2] = buf_1[src_idx][srcPos + src_sub_block_in_width];
482 to_hash[3] = buf_1[src_idx][srcPos + src_sub_block_in_width + 1];
483
484 buf_1[dst_idx][dst_pos] =
485 av1_get_crc_value(calc_1, (uint8_t *)to_hash, sizeof(to_hash));
486
487 to_hash[0] = buf_2[src_idx][srcPos];
488 to_hash[1] = buf_2[src_idx][srcPos + 1];
489 to_hash[2] = buf_2[src_idx][srcPos + src_sub_block_in_width];
490 to_hash[3] = buf_2[src_idx][srcPos + src_sub_block_in_width + 1];
491 buf_2[dst_idx][dst_pos] =
492 av1_get_crc_value(calc_2, (uint8_t *)to_hash, sizeof(to_hash));
493 dst_pos++;
494 }
495 }
496
497 src_sub_block_in_width = sub_block_in_width;
498 sub_block_in_width >>= 1;
499 }
500
501 *hash_value1 = (buf_1[dst_idx][0] & crc_mask) + add_value;
502 *hash_value2 = buf_2[dst_idx][0];
503 }
504