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