Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* The copyright in this software is being made available under the BSD
* License, included below. This software may be subject to other third party
* and contributor rights, including patent rights, and no such rights are
* granted under this license.
*
* Copyright (c) 2010-2019, ITU/ISO/IEC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/** \file TEncHash.cpp
\brief hash encoder class
*/
#include "CommonLib/dtrace_codingstruct.h"
#include "CommonLib/Picture.h"
#include "CommonLib/UnitTools.h"
#include "Hash.h"
// ====================================================================================================================
// Constructor / destructor / create / destroy
// ====================================================================================================================
int TComHash::m_blockSizeToIndex[65][65];
TCRCCalculatorLight TComHash::m_crcCalculator1(24, 0x5D6DCB);
TCRCCalculatorLight TComHash::m_crcCalculator2(24, 0x864CFB);
TCRCCalculatorLight::TCRCCalculatorLight(uint32_t bits, uint32_t truncPoly)
{
m_remainder = 0;
m_bits = bits;
m_truncPoly = truncPoly;
m_finalResultMask = (1 << bits) - 1;
xInitTable();
}
TCRCCalculatorLight::~TCRCCalculatorLight()
{
}
void TCRCCalculatorLight::xInitTable()
{
const uint32_t highBit = 1 << (m_bits - 1);
const uint32_t byteHighBit = 1 << (8 - 1);
for (uint32_t value = 0; value < 256; value++)
for (unsigned char mask = byteHighBit; mask != 0; mask >>= 1)
{
if (value & mask)
{
remainder ^= highBit;
}
if (remainder & highBit)
{
remainder <<= 1;
remainder ^= m_truncPoly;
}
else
{
remainder <<= 1;
}
}
m_table[value] = remainder;
}
}
void TCRCCalculatorLight::processData(unsigned char* curData, uint32_t dataLength)
{
unsigned char index = (m_remainder >> (m_bits - 8)) ^ curData[i];
m_remainder <<= 8;
m_remainder ^= m_table[index];
}
}
TComHash::TComHash()
{
m_lookupTable = NULL;
tableHasContent = false;
for (int i = 0; i < 5; i++)
{
hashPic[i] = NULL;
}
}
TComHash::~TComHash()
{
clearAll();
if (m_lookupTable != NULL)
{
delete[] m_lookupTable;
m_lookupTable = NULL;
}
}
void TComHash::create(int picWidth, int picHeight)
{
if (m_lookupTable)
{
clearAll();
}
if (!hashPic[0])
{
for (int k = 0; k < 5; k++)
{
hashPic[k] = new uint16_t[picWidth*picHeight];
}
}
if (m_lookupTable)
{
return;
}
int maxAddr = 1 << (m_CRCBits + m_blockSizeBits);
m_lookupTable = new std::vector<BlockHash>*[maxAddr];
memset(m_lookupTable, 0, sizeof(std::vector<BlockHash>*) * maxAddr);
tableHasContent = false;
}
void TComHash::clearAll()
{
if (hashPic[0])
{
for (int k = 0; k < 5; k++)
{
delete[] hashPic[k];
hashPic[k] = NULL;
}
}
tableHasContent = false;
if (m_lookupTable == NULL)
{
return;
}
int maxAddr = 1 << (m_CRCBits + m_blockSizeBits);
for (int i = 0; i < maxAddr; i++)
{
if (m_lookupTable[i] != NULL)
{
delete m_lookupTable[i];
m_lookupTable[i] = NULL;
}
}
}
void TComHash::addToTable(uint32_t hashValue, const BlockHash& blockHash)
{
if (m_lookupTable[hashValue] == NULL)
{
m_lookupTable[hashValue] = new std::vector<BlockHash>;
m_lookupTable[hashValue]->push_back(blockHash);
}
else
{
m_lookupTable[hashValue]->push_back(blockHash);
}
}
{
if (m_lookupTable[hashValue] == NULL)
{
return 0;
}
else
{
return static_cast<int>(m_lookupTable[hashValue]->size());
}
}
int TComHash::count(uint32_t hashValue) const
{
if (m_lookupTable[hashValue] == NULL)
{
return 0;
}
else
{
return static_cast<int>(m_lookupTable[hashValue]->size());
}
}
MapIterator TComHash::getFirstIterator(uint32_t hashValue)
{
return m_lookupTable[hashValue]->begin();
}
const MapIterator TComHash::getFirstIterator(uint32_t hashValue) const
{
return m_lookupTable[hashValue]->begin();
}
bool TComHash::hasExactMatch(uint32_t hashValue1, uint32_t hashValue2)
{
if (m_lookupTable[hashValue1] == NULL)
{
return false;
}
std::vector<BlockHash>::iterator it;
for (it = m_lookupTable[hashValue1]->begin(); it != m_lookupTable[hashValue1]->end(); it++)
{
if ((*it).hashValue2 == hashValue2)
{
return true;
}
}
return false;
}
void TComHash::generateBlock2x2HashValue(const PelUnitBuf &curPicBuf, int picWidth, int picHeight, const BitDepths bitDepths, uint32_t* picBlockHash[2], bool* picBlockSameInfo[3])
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
{
const int width = 2;
const int height = 2;
int xEnd = picWidth - width + 1;
int yEnd = picHeight - height + 1;
int length = width * 2;
bool includeChroma = false;
if ((curPicBuf).chromaFormat == CHROMA_444)
{
length *= 3;
includeChroma = true;
}
unsigned char* p = new unsigned char[length];
int pos = 0;
for (int yPos = 0; yPos < yEnd; yPos++)
{
for (int xPos = 0; xPos < xEnd; xPos++)
{
TComHash::getPixelsIn1DCharArrayByBlock2x2(curPicBuf, p, xPos, yPos, bitDepths, includeChroma);
picBlockSameInfo[0][pos] = isBlock2x2RowSameValue(p, includeChroma);
picBlockSameInfo[1][pos] = isBlock2x2ColSameValue(p, includeChroma);
picBlockHash[0][pos] = TComHash::getCRCValue1(p, length * sizeof(unsigned char));
picBlockHash[1][pos] = TComHash::getCRCValue2(p, length * sizeof(unsigned char));
pos++;
}
pos += width - 1;
}
delete[] p;
}
void TComHash::generateBlockHashValue(int picWidth, int picHeight, int width, int height, uint32_t* srcPicBlockHash[2], uint32_t* dstPicBlockHash[2], bool* srcPicBlockSameInfo[3], bool* dstPicBlockSameInfo[3])
{
int xEnd = picWidth - width + 1;
int yEnd = picHeight - height + 1;
int srcWidth = width >> 1;
int quadWidth = width >> 2;
int srcHeight = height >> 1;
int quadHeight = height >> 2;
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
int pos = 0;
for (int yPos = 0; yPos < yEnd; yPos++)
{
for (int xPos = 0; xPos < xEnd; xPos++)
{
p[0] = srcPicBlockHash[0][pos];
p[1] = srcPicBlockHash[0][pos + srcWidth];
p[2] = srcPicBlockHash[0][pos + srcHeight * picWidth];
p[3] = srcPicBlockHash[0][pos + srcHeight * picWidth + srcWidth];
dstPicBlockHash[0][pos] = TComHash::getCRCValue1((unsigned char*)p, length);
p[0] = srcPicBlockHash[1][pos];
p[1] = srcPicBlockHash[1][pos + srcWidth];
p[2] = srcPicBlockHash[1][pos + srcHeight * picWidth];
p[3] = srcPicBlockHash[1][pos + srcHeight * picWidth + srcWidth];
dstPicBlockHash[1][pos] = TComHash::getCRCValue2((unsigned char*)p, length);
dstPicBlockSameInfo[0][pos] = srcPicBlockSameInfo[0][pos] && srcPicBlockSameInfo[0][pos + quadWidth] && srcPicBlockSameInfo[0][pos + srcWidth]
&& srcPicBlockSameInfo[0][pos + srcHeight * picWidth] && srcPicBlockSameInfo[0][pos + srcHeight * picWidth + quadWidth] && srcPicBlockSameInfo[0][pos + srcHeight * picWidth + srcWidth];
dstPicBlockSameInfo[1][pos] = srcPicBlockSameInfo[1][pos] && srcPicBlockSameInfo[1][pos + srcWidth] && srcPicBlockSameInfo[1][pos + quadHeight * picWidth]
&& srcPicBlockSameInfo[1][pos + quadHeight * picWidth + srcWidth] && srcPicBlockSameInfo[1][pos + srcHeight * picWidth] && srcPicBlockSameInfo[1][pos + srcHeight * picWidth + srcWidth];
pos++;
}
pos += width - 1;
}
if (width >= 4)
{
pos = 0;
for (int yPos = 0; yPos < yEnd; yPos++)
{
for (int xPos = 0; xPos < xEnd; xPos++)
{
dstPicBlockSameInfo[2][pos] = (!dstPicBlockSameInfo[0][pos] && !dstPicBlockSameInfo[1][pos]);
pos++;
}
pos += width - 1;
}
}
}
void TComHash::addToHashMapByRowWithPrecalData(uint32_t* picHash[2], bool* picIsSame, int picWidth, int picHeight, int width, int height)
{
int xEnd = picWidth - width + 1;
int yEnd = picHeight - height + 1;
bool* srcIsAdded = picIsSame;
uint32_t* srcHash[2] = { picHash[0], picHash[1] };
int addValue = m_blockSizeToIndex[width][height];
CHECK(addValue < 0, "Wrong")
int crcMask = 1 << m_CRCBits;
crcMask -= 1;
int blockIdx = floorLog2(width) - 2;
for (int xPos = 0; xPos < xEnd; xPos++)
{
for (int yPos = 0; yPos < yEnd; yPos++)
{
int pos = yPos * picWidth + xPos;
hashPic[blockIdx][pos] = (uint16_t)(srcHash[1][pos] & crcMask);
//valid data
if (srcIsAdded[pos])
{
BlockHash blockHash;
blockHash.x = xPos;
blockHash.y = yPos;
uint32_t hashValue1 = (srcHash[0][pos] & crcMask) + addValue;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
blockHash.hashValue2 = srcHash[1][pos];
addToTable(hashValue1, blockHash);
}
}
}
}
void TComHash::getPixelsIn1DCharArrayByBlock2x2(const PelUnitBuf &curPicBuf, unsigned char* pixelsIn1D, int xStart, int yStart, const BitDepths& bitDepths, bool includeAllComponent)
{
ChromaFormat fmt = (curPicBuf).chromaFormat;
if (fmt != CHROMA_444)
{
includeAllComponent = false;
}
if (bitDepths.recon[CHANNEL_TYPE_LUMA] == 8 && bitDepths.recon[CHANNEL_TYPE_CHROMA] == 8)
{
Pel* curPel[3];
int stride[3];
for (int id = 0; id < 3; id++)
{
ComponentID compID = ComponentID(id);
stride[id] = (curPicBuf).get(compID).stride;
curPel[id] = (curPicBuf).get(compID).buf;
curPel[id] += (yStart >> getComponentScaleY(compID, fmt)) * stride[id] + (xStart >> getComponentScaleX(compID, fmt));
}
int index = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
pixelsIn1D[index++] = static_cast<unsigned char>(curPel[0][j]);
if (includeAllComponent)
{
pixelsIn1D[index++] = static_cast<unsigned char>(curPel[1][j]);
pixelsIn1D[index++] = static_cast<unsigned char>(curPel[2][j]);
}
}
curPel[0] += stride[0];
if (includeAllComponent)
{
curPel[1] += stride[1];
curPel[2] += stride[2];
}
}
}
else
{
int shift = bitDepths.recon[CHANNEL_TYPE_LUMA] - 8;
int shiftc = bitDepths.recon[CHANNEL_TYPE_CHROMA] - 8;
Pel* curPel[3];
int stride[3];
for (int id = 0; id < 3; id++)
{
ComponentID compID = ComponentID(id);
stride[id] = (curPicBuf).get(compID).stride;
curPel[id] = (curPicBuf).get(compID).buf;
curPel[id] += (yStart >> getComponentScaleY(compID, fmt)) * stride[id] + (xStart >> getComponentScaleX(compID, fmt));
}
int index = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
pixelsIn1D[index++] = static_cast<unsigned char>(curPel[0][j] >> shift);
if (includeAllComponent)
{
pixelsIn1D[index++] = static_cast<unsigned char>(curPel[1][j] >> shiftc);
pixelsIn1D[index++] = static_cast<unsigned char>(curPel[2][j] >> shiftc);
}
}
curPel[0] += stride[0];
if (includeAllComponent)
{
curPel[1] += stride[1];
curPel[2] += stride[2];
}
}
}
}
bool TComHash::isBlock2x2RowSameValue(unsigned char* p, bool includeAllComponent)
{
if (includeAllComponent)
{
if (p[0] != p[3] || p[6] != p[9])
{
return false;
}
if (p[1] != p[4] || p[7] != p[10])
{
return false;
}
if (p[2] != p[5] || p[8] != p[11])
{
return false;
}
}
else
{
if (p[0] != p[1] || p[2] != p[3])
{
return false;
}
}
return true;
}
bool TComHash::isBlock2x2ColSameValue(unsigned char* p, bool includeAllComponent)
{
if (includeAllComponent)
{
if (p[0] != p[6] || p[3] != p[9])
{
return false;
}
if (p[1] != p[7] || p[4] != p[10])
{
return false;
}
if (p[2] != p[8] || p[5] != p[11])
{
return false;
}
}
else
{
if ((p[0] != p[2]) || (p[1] != p[3]))
{
return false;
}
}
return true;
}
bool TComHash::isHorizontalPerfectLuma(const Pel* srcPel, int stride, int width, int height)
{
for (int i = 0; i < height; i++)
{
for (int j = 1; j < width; j++)
{
if (srcPel[j] != srcPel[0])
{
return false;
}
}
srcPel += stride;
}
return true;
}
bool TComHash::isVerticalPerfectLuma(const Pel* srcPel, int stride, int width, int height)
{
for (int i = 0; i < width; i++)
{
for (int j = 1; j < height; j++)
{
if (srcPel[j*stride + i] != srcPel[i])
{
return false;
}
}
}
return true;
}
bool TComHash::getBlockHashValue(const PelUnitBuf &curPicBuf, int width, int height, int xStart, int yStart, const BitDepths bitDepths, uint32_t& hashValue1, uint32_t& hashValue2)
{
int addValue = m_blockSizeToIndex[width][height];
CHECK(addValue < 0, "Wrong")
addValue <<= m_CRCBits;
int crcMask = 1 << m_CRCBits;
crcMask -= 1;
int length = 4;
bool includeChroma = false;
if ((curPicBuf).chromaFormat == CHROMA_444)
{
length *= 3;
includeChroma = true;
}
unsigned char* p = new unsigned char[length];
int block2x2Num = (width*height) >> 2;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
hashValueBuffer[i][j] = new uint32_t[block2x2Num];
}
}
//2x2 subblock hash values in current CU
int subBlockInWidth = (width >> 1);
int subBlockInHeight = (height >> 1);
for (int yPos = 0; yPos < height; yPos += 2)
{
for (int xPos = 0; xPos < width; xPos += 2)
{
int pos = (yPos >> 1)*subBlockInWidth + (xPos >> 1);
TComHash::getPixelsIn1DCharArrayByBlock2x2(curPicBuf, p, xStart + xPos, yStart + yPos, bitDepths, includeChroma);
hashValueBuffer[0][0][pos] = TComHash::getCRCValue1(p, length * sizeof(unsigned char));
hashValueBuffer[1][0][pos] = TComHash::getCRCValue2(p, length * sizeof(unsigned char));
}
}
int srcSubBlockInWidth = subBlockInWidth;
subBlockInWidth >>= 1;
subBlockInHeight >>= 1;
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
int srcIdx = 1;
int dstIdx = 0;
//4x4 subblock hash values to current block hash values
int minSize = std::min(height, width);
for (int subWidth = 4; subWidth <= minSize; subWidth *= 2)
{
srcIdx = 1 - srcIdx;
dstIdx = 1 - dstIdx;
int dstPos = 0;
for (int yPos = 0; yPos < subBlockInHeight; yPos++)
{
for (int xPos = 0; xPos < subBlockInWidth; xPos++)
{
int srcPos = (yPos << 1)*srcSubBlockInWidth + (xPos << 1);
toHash[0] = hashValueBuffer[0][srcIdx][srcPos];
toHash[1] = hashValueBuffer[0][srcIdx][srcPos + 1];
toHash[2] = hashValueBuffer[0][srcIdx][srcPos + srcSubBlockInWidth];
toHash[3] = hashValueBuffer[0][srcIdx][srcPos + srcSubBlockInWidth + 1];
hashValueBuffer[0][dstIdx][dstPos] = TComHash::getCRCValue1((unsigned char*)toHash, length);
toHash[0] = hashValueBuffer[1][srcIdx][srcPos];
toHash[1] = hashValueBuffer[1][srcIdx][srcPos + 1];
toHash[2] = hashValueBuffer[1][srcIdx][srcPos + srcSubBlockInWidth];
toHash[3] = hashValueBuffer[1][srcIdx][srcPos + srcSubBlockInWidth + 1];
hashValueBuffer[1][dstIdx][dstPos] = TComHash::getCRCValue2((unsigned char*)toHash, length);
dstPos++;
}
}
srcSubBlockInWidth = subBlockInWidth;
subBlockInWidth >>= 1;
subBlockInHeight >>= 1;
}
if (width != height)//currently support 1:2 or 2:1 block size
{
CHECK(width != (height << 1) && (width << 1) != height, "Wrong")
bool isHorizontal = width == (height << 1) ? true : false;
length = 2 * sizeof(uint32_t);
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
srcIdx = 1 - srcIdx;
dstIdx = 1 - dstIdx;
if (isHorizontal)
{
toHash[0] = hashValueBuffer[0][srcIdx][0];
toHash[1] = hashValueBuffer[0][srcIdx][1];
hashValueBuffer[0][dstIdx][0] = TComHash::getCRCValue1((unsigned char*)toHash, length);
toHash[0] = hashValueBuffer[1][srcIdx][0];
toHash[1] = hashValueBuffer[1][srcIdx][1];
hashValueBuffer[1][dstIdx][0] = TComHash::getCRCValue2((unsigned char*)toHash, length);
}
else
{
CHECK(srcSubBlockInWidth != 1, "Wrong")
toHash[0] = hashValueBuffer[0][srcIdx][0];
toHash[1] = hashValueBuffer[0][srcIdx][srcSubBlockInWidth];
hashValueBuffer[0][dstIdx][0] = TComHash::getCRCValue1((unsigned char*)toHash, length);
toHash[0] = hashValueBuffer[1][srcIdx][0];
toHash[1] = hashValueBuffer[1][srcIdx][srcSubBlockInWidth];
hashValueBuffer[1][dstIdx][0] = TComHash::getCRCValue2((unsigned char*)toHash, length);
}
}
hashValue1 = (hashValueBuffer[0][dstIdx][0] & crcMask) + addValue;
hashValue2 = hashValueBuffer[1][dstIdx][0];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
delete[] hashValueBuffer[i][j];
}
}
delete[] p;
return true;
}
void TComHash::initBlockSizeToIndex()
{
for (int i = 0; i < 65; i++)
{
for (int j = 0; j < 65; j++)
{
m_blockSizeToIndex[i][j] = -1;
}
}
m_blockSizeToIndex[8][8] = 0;
m_blockSizeToIndex[16][16] = 1;
m_blockSizeToIndex[32][32] = 2;
m_blockSizeToIndex[64][64] = 3;
m_blockSizeToIndex[4][4] = 4;
}
uint32_t TComHash::getCRCValue1(unsigned char* p, int length)
{
m_crcCalculator1.reset();
m_crcCalculator1.processData(p, length);
return m_crcCalculator1.getCRC();
}
uint32_t TComHash::getCRCValue2(unsigned char* p, int length)