HEVC Test Model (HM)  HM-16.18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TComPicYuvMD5.cpp
Go to the documentation of this file.
1 /* The copyright in this software is being made available under the BSD
2  * License, included below. This software may be subject to other third party
3  * and contributor rights, including patent rights, and no such rights are
4  * granted under this license.
5  *
6  * Copyright (c) 2010-2017, ITU/ISO/IEC
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18  * be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "TComPicYuv.h"
35 #include "libmd5/MD5.h"
36 
39 
44 template<UInt OUTPUT_BITDEPTH_DIV8>
45 static Void md5_block(MD5& md5, const Pel* plane, UInt n)
46 {
47  /* create a 64 byte buffer for packing Pel's into */
48  UChar buf[64/OUTPUT_BITDEPTH_DIV8][OUTPUT_BITDEPTH_DIV8];
49  for (UInt i = 0; i < n; i++)
50  {
51  Pel pel = plane[i];
52  /* perform bitdepth and endian conversion */
53  for (UInt d = 0; d < OUTPUT_BITDEPTH_DIV8; d++)
54  {
55  buf[i][d] = pel >> (d*8);
56  }
57  }
58  md5.update((UChar*)buf, n * OUTPUT_BITDEPTH_DIV8);
59 }
60 
65 template<UInt OUTPUT_BITDEPTH_DIV8>
66 static Void md5_plane(MD5& md5, const Pel* plane, UInt width, UInt height, UInt stride)
67 {
68  /* N is the number of samples to process per md5 update.
69  * All N samples must fit in buf */
70  UInt N = 32;
71  UInt width_modN = width % N;
72  UInt width_less_modN = width - width_modN;
73 
74  for (UInt y = 0; y < height; y++)
75  {
76  /* convert pels into unsigned chars in little endian byte order.
77  * NB, for 8bit data, data is truncated to 8bits. */
78  for (UInt x = 0; x < width_less_modN; x += N)
79  {
80  md5_block<OUTPUT_BITDEPTH_DIV8>(md5, &plane[y*stride + x], N);
81  }
82 
83  /* mop up any of the remaining line */
84  md5_block<OUTPUT_BITDEPTH_DIV8>(md5, &plane[y*stride + width_less_modN], width_modN);
85  }
86 }
87 
88 
89 UInt compCRC(Int bitdepth, const Pel* plane, UInt width, UInt height, UInt stride, TComPictureHash &digest)
90 {
91  UInt crcMsb;
92  UInt bitVal;
93  UInt crcVal = 0xffff;
94  UInt bitIdx;
95  for (UInt y = 0; y < height; y++)
96  {
97  for (UInt x = 0; x < width; x++)
98  {
99  // take CRC of first pictureData byte
100  for(bitIdx=0; bitIdx<8; bitIdx++)
101  {
102  crcMsb = (crcVal >> 15) & 1;
103  bitVal = (plane[y*stride+x] >> (7 - bitIdx)) & 1;
104  crcVal = (((crcVal << 1) + bitVal) & 0xffff) ^ (crcMsb * 0x1021);
105  }
106  // take CRC of second pictureData byte if bit depth is greater than 8-bits
107  if(bitdepth > 8)
108  {
109  for(bitIdx=0; bitIdx<8; bitIdx++)
110  {
111  crcMsb = (crcVal >> 15) & 1;
112  bitVal = (plane[y*stride+x] >> (15 - bitIdx)) & 1;
113  crcVal = (((crcVal << 1) + bitVal) & 0xffff) ^ (crcMsb * 0x1021);
114  }
115  }
116  }
117  }
118  for(bitIdx=0; bitIdx<16; bitIdx++)
119  {
120  crcMsb = (crcVal >> 15) & 1;
121  crcVal = ((crcVal << 1) & 0xffff) ^ (crcMsb * 0x1021);
122  }
123 
124  digest.hash.push_back((crcVal>>8) & 0xff);
125  digest.hash.push_back( crcVal & 0xff);
126  return 2;
127 }
128 
129 UInt calcCRC(const TComPicYuv& pic, TComPictureHash &digest, const BitDepths &bitDepths)
130 {
131  UInt digestLen=0;
132  digest.hash.clear();
133  for(Int chan=0; chan<pic.getNumberValidComponents(); chan++)
134  {
135  const ComponentID compID=ComponentID(chan);
136  digestLen=compCRC(bitDepths.recon[toChannelType(compID)], pic.getAddr(compID), pic.getWidth(compID), pic.getHeight(compID), pic.getStride(compID), digest);
137  }
138  return digestLen;
139 }
140 
141 UInt compChecksum(Int bitdepth, const Pel* plane, UInt width, UInt height, UInt stride, TComPictureHash &digest, const BitDepths &/*bitDepths*/)
142 {
143  UInt checksum = 0;
144  UChar xor_mask;
145 
146  for (UInt y = 0; y < height; y++)
147  {
148  for (UInt x = 0; x < width; x++)
149  {
150  xor_mask = (x & 0xff) ^ (y & 0xff) ^ (x >> 8) ^ (y >> 8);
151  checksum = (checksum + ((plane[y*stride+x] & 0xff) ^ xor_mask)) & 0xffffffff;
152 
153  if(bitdepth > 8)
154  {
155  checksum = (checksum + ((plane[y*stride+x]>>8) ^ xor_mask)) & 0xffffffff;
156  }
157  }
158  }
159 
160  digest.hash.push_back((checksum>>24) & 0xff);
161  digest.hash.push_back((checksum>>16) & 0xff);
162  digest.hash.push_back((checksum>>8) & 0xff);
163  digest.hash.push_back( checksum & 0xff);
164  return 4;
165 }
166 
167 UInt calcChecksum(const TComPicYuv& pic, TComPictureHash &digest, const BitDepths &bitDepths)
168 {
169  UInt digestLen=0;
170  digest.hash.clear();
171  for(Int chan=0; chan<pic.getNumberValidComponents(); chan++)
172  {
173  const ComponentID compID=ComponentID(chan);
174  digestLen=compChecksum(bitDepths.recon[toChannelType(compID)], pic.getAddr(compID), pic.getWidth(compID), pic.getHeight(compID), pic.getStride(compID), digest, bitDepths);
175  }
176  return digestLen;
177 }
185 UInt calcMD5(const TComPicYuv& pic, TComPictureHash &digest, const BitDepths &bitDepths)
186 {
187  /* choose an md5_plane packing function based on the system bitdepth */
188  typedef Void (*MD5PlaneFunc)(MD5&, const Pel*, UInt, UInt, UInt);
189  MD5PlaneFunc md5_plane_func;
190 
191  MD5 md5[MAX_NUM_COMPONENT];
192 
193  digest.hash.clear();
194  for(Int chan=0; chan<pic.getNumberValidComponents(); chan++)
195  {
196  const ComponentID compID=ComponentID(chan);
197  md5_plane_func = bitDepths.recon[toChannelType(compID)] <= 8 ? (MD5PlaneFunc)md5_plane<1> : (MD5PlaneFunc)md5_plane<2>;
198  UChar tmp_digest[MD5_DIGEST_STRING_LENGTH];
199  md5_plane_func(md5[compID], pic.getAddr(compID), pic.getWidth(compID), pic.getHeight(compID), pic.getStride(compID));
200  md5[compID].finalize(tmp_digest);
201  for(UInt i=0; i<MD5_DIGEST_STRING_LENGTH; i++)
202  {
203  digest.hash.push_back(tmp_digest[i]);
204  }
205  }
206  return 16;
207 }
208 
209 std::string hashToString(const TComPictureHash &digest, Int numChar)
210 {
211  static const TChar* hex = "0123456789abcdef";
212  std::string result;
213 
214  for(Int pos=0; pos<Int(digest.hash.size()); pos++)
215  {
216  if ((pos % numChar) == 0 && pos!=0 )
217  {
218  result += ',';
219  }
220  result += hex[digest.hash[pos] >> 4];
221  result += hex[digest.hash[pos] & 0xf];
222  }
223 
224  return result;
225 }
226 
picture YUV buffer class
Definition: TComPicYuv.h:55
void Void
Definition: TypeDef.h:203
UInt calcChecksum(const TComPicYuv &pic, TComPictureHash &digest, const BitDepths &bitDepths)
Int getStride(const ComponentID id) const
Definition: TComPicYuv.h:121
Int getHeight(const ComponentID id) const
Definition: TComPicYuv.h:117
UInt compChecksum(Int bitdepth, const Pel *plane, UInt width, UInt height, UInt stride, TComPictureHash &digest, const BitDepths &)
UInt compCRC(Int bitdepth, const Pel *plane, UInt width, UInt height, UInt stride, TComPictureHash &digest)
unsigned int UInt
Definition: TypeDef.h:212
Definition: MD5.h:42
void update(unsigned char *buf, unsigned len)
Definition: MD5.h:57
Short Pel
pixel type
Definition: TypeDef.h:249
static Void md5_block(MD5 &md5, const Pel *plane, UInt n)
Int recon[MAX_NUM_CHANNEL_TYPE]
the bit depth as indicated in the SPS
Definition: TypeDef.h:793
static const UInt MD5_DIGEST_STRING_LENGTH
Definition: MD5.h:40
char TChar
Definition: TypeDef.h:206
UInt calcMD5(const TComPicYuv &pic, TComPictureHash &digest, const BitDepths &bitDepths)
Int getWidth(const ComponentID id) const
Definition: TComPicYuv.h:116
static ChannelType toChannelType(const ComponentID id)
unsigned char UChar
Definition: TypeDef.h:208
picture YUV buffer class (header)
UInt getNumberValidComponents() const
Definition: TComPicYuv.h:119
std::vector< UChar > hash
Definition: TypeDef.h:821
Pel * getAddr(const ComponentID ch)
Definition: TComPicYuv.h:139
static Void md5_plane(MD5 &md5, const Pel *plane, UInt width, UInt height, UInt stride)
int Int
Definition: TypeDef.h:211
ComponentID
Definition: TypeDef.h:308
void finalize(unsigned char digest[MD5_DIGEST_STRING_LENGTH])
Definition: MD5.h:65
std::string hashToString(const TComPictureHash &digest, Int numChar)
UInt calcCRC(const TComPicYuv &pic, TComPictureHash &digest, const BitDepths &bitDepths)