[313] | 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 | * |
---|
[595] | 6 | * Copyright (c) 2010-2014, ITU/ISO/IEC |
---|
[313] | 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 | |
---|
| 37 | //! \ingroup TLibCommon |
---|
| 38 | //! \{ |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Update md5 using n samples from plane, each sample is adjusted to |
---|
| 42 | * OUTBIT_BITDEPTH_DIV8. |
---|
| 43 | */ |
---|
| 44 | template<UInt OUTPUT_BITDEPTH_DIV8> |
---|
[916] | 45 | static Void md5_block(MD5& md5, const Pel* plane, UInt n) |
---|
[313] | 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 | |
---|
| 61 | /** |
---|
| 62 | * Update md5 with all samples in plane in raster order, each sample |
---|
| 63 | * is adjusted to OUTBIT_BITDEPTH_DIV8. |
---|
| 64 | */ |
---|
| 65 | template<UInt OUTPUT_BITDEPTH_DIV8> |
---|
[916] | 66 | static Void md5_plane(MD5& md5, const Pel* plane, UInt width, UInt height, UInt stride) |
---|
[313] | 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 | { |
---|
[916] | 76 | /* convert pels into unsigned chars in little endian byte order. |
---|
[313] | 77 | * NB, for 8bit data, data is truncated to 8bits. */ |
---|
| 78 | for (UInt x = 0; x < width_less_modN; x += N) |
---|
| 79 | md5_block<OUTPUT_BITDEPTH_DIV8>(md5, &plane[y*stride + x], N); |
---|
| 80 | |
---|
| 81 | /* mop up any of the remaining line */ |
---|
| 82 | md5_block<OUTPUT_BITDEPTH_DIV8>(md5, &plane[y*stride + width_less_modN], width_modN); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[916] | 86 | |
---|
| 87 | UInt compCRC(Int bitdepth, const Pel* plane, UInt width, UInt height, UInt stride, TComDigest &digest) |
---|
[313] | 88 | { |
---|
| 89 | UInt crcMsb; |
---|
| 90 | UInt bitVal; |
---|
| 91 | UInt crcVal = 0xffff; |
---|
| 92 | UInt bitIdx; |
---|
| 93 | for (UInt y = 0; y < height; y++) |
---|
| 94 | { |
---|
| 95 | for (UInt x = 0; x < width; x++) |
---|
| 96 | { |
---|
| 97 | // take CRC of first pictureData byte |
---|
| 98 | for(bitIdx=0; bitIdx<8; bitIdx++) |
---|
| 99 | { |
---|
| 100 | crcMsb = (crcVal >> 15) & 1; |
---|
| 101 | bitVal = (plane[y*stride+x] >> (7 - bitIdx)) & 1; |
---|
| 102 | crcVal = (((crcVal << 1) + bitVal) & 0xffff) ^ (crcMsb * 0x1021); |
---|
| 103 | } |
---|
| 104 | // take CRC of second pictureData byte if bit depth is greater than 8-bits |
---|
| 105 | if(bitdepth > 8) |
---|
| 106 | { |
---|
| 107 | for(bitIdx=0; bitIdx<8; bitIdx++) |
---|
| 108 | { |
---|
| 109 | crcMsb = (crcVal >> 15) & 1; |
---|
| 110 | bitVal = (plane[y*stride+x] >> (15 - bitIdx)) & 1; |
---|
| 111 | crcVal = (((crcVal << 1) + bitVal) & 0xffff) ^ (crcMsb * 0x1021); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | for(bitIdx=0; bitIdx<16; bitIdx++) |
---|
| 117 | { |
---|
| 118 | crcMsb = (crcVal >> 15) & 1; |
---|
| 119 | crcVal = ((crcVal << 1) & 0xffff) ^ (crcMsb * 0x1021); |
---|
| 120 | } |
---|
| 121 | |
---|
[916] | 122 | digest.hash.push_back((crcVal>>8) & 0xff); |
---|
| 123 | digest.hash.push_back( crcVal & 0xff); |
---|
| 124 | return 2; |
---|
[313] | 125 | } |
---|
| 126 | |
---|
[916] | 127 | UInt calcCRC(const TComPicYuv& pic, TComDigest &digest) |
---|
[313] | 128 | { |
---|
[916] | 129 | UInt digestLen=0; |
---|
| 130 | digest.hash.clear(); |
---|
| 131 | for(Int chan=0; chan<pic.getNumberValidComponents(); chan++) |
---|
| 132 | { |
---|
| 133 | const ComponentID compID=ComponentID(chan); |
---|
| 134 | digestLen=compCRC(g_bitDepth[toChannelType(compID)], pic.getAddr(compID), pic.getWidth(compID), pic.getHeight(compID), pic.getStride(compID), digest); |
---|
| 135 | } |
---|
| 136 | return digestLen; |
---|
[313] | 137 | } |
---|
| 138 | |
---|
[916] | 139 | UInt compChecksum(Int bitdepth, const Pel* plane, UInt width, UInt height, UInt stride, TComDigest &digest) |
---|
[313] | 140 | { |
---|
| 141 | UInt checksum = 0; |
---|
| 142 | UChar xor_mask; |
---|
| 143 | |
---|
| 144 | for (UInt y = 0; y < height; y++) |
---|
| 145 | { |
---|
| 146 | for (UInt x = 0; x < width; x++) |
---|
| 147 | { |
---|
| 148 | xor_mask = (x & 0xff) ^ (y & 0xff) ^ (x >> 8) ^ (y >> 8); |
---|
| 149 | checksum = (checksum + ((plane[y*stride+x] & 0xff) ^ xor_mask)) & 0xffffffff; |
---|
| 150 | |
---|
| 151 | if(bitdepth > 8) |
---|
| 152 | { |
---|
| 153 | checksum = (checksum + ((plane[y*stride+x]>>8) ^ xor_mask)) & 0xffffffff; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
[916] | 158 | digest.hash.push_back((checksum>>24) & 0xff); |
---|
| 159 | digest.hash.push_back((checksum>>16) & 0xff); |
---|
| 160 | digest.hash.push_back((checksum>>8) & 0xff); |
---|
| 161 | digest.hash.push_back( checksum & 0xff); |
---|
| 162 | return 4; |
---|
[313] | 163 | } |
---|
| 164 | |
---|
[916] | 165 | UInt calcChecksum(const TComPicYuv& pic, TComDigest &digest) |
---|
[313] | 166 | { |
---|
[916] | 167 | UInt digestLen=0; |
---|
| 168 | digest.hash.clear(); |
---|
| 169 | for(Int chan=0; chan<pic.getNumberValidComponents(); chan++) |
---|
| 170 | { |
---|
| 171 | const ComponentID compID=ComponentID(chan); |
---|
| 172 | digestLen=compChecksum(g_bitDepth[toChannelType(compID)], pic.getAddr(compID), pic.getWidth(compID), pic.getHeight(compID), pic.getStride(compID), digest); |
---|
| 173 | } |
---|
| 174 | return digestLen; |
---|
[313] | 175 | } |
---|
| 176 | /** |
---|
| 177 | * Calculate the MD5sum of pic, storing the result in digest. |
---|
| 178 | * MD5 calculation is performed on Y' then Cb, then Cr; each in raster order. |
---|
| 179 | * Pel data is inserted into the MD5 function in little-endian byte order, |
---|
| 180 | * using sufficient bytes to represent the picture bitdepth. Eg, 10bit data |
---|
| 181 | * uses little-endian two byte words; 8bit data uses single byte words. |
---|
| 182 | */ |
---|
[916] | 183 | UInt calcMD5(const TComPicYuv& pic, TComDigest &digest) |
---|
[313] | 184 | { |
---|
| 185 | /* choose an md5_plane packing function based on the system bitdepth */ |
---|
[916] | 186 | typedef Void (*MD5PlaneFunc)(MD5&, const Pel*, UInt, UInt, UInt); |
---|
[313] | 187 | MD5PlaneFunc md5_plane_func; |
---|
| 188 | |
---|
[916] | 189 | MD5 md5[MAX_NUM_COMPONENT]; |
---|
[313] | 190 | |
---|
[916] | 191 | digest.hash.clear(); |
---|
| 192 | for(Int chan=0; chan<pic.getNumberValidComponents(); chan++) |
---|
| 193 | { |
---|
| 194 | const ComponentID compID=ComponentID(chan); |
---|
| 195 | md5_plane_func = g_bitDepth[toChannelType(compID)] <= 8 ? (MD5PlaneFunc)md5_plane<1> : (MD5PlaneFunc)md5_plane<2>; |
---|
| 196 | UChar tmp_digest[MD5_DIGEST_STRING_LENGTH]; |
---|
| 197 | md5_plane_func(md5[compID], pic.getAddr(compID), pic.getWidth(compID), pic.getHeight(compID), pic.getStride(compID)); |
---|
| 198 | md5[compID].finalize(tmp_digest); |
---|
| 199 | for(UInt i=0; i<MD5_DIGEST_STRING_LENGTH; i++) |
---|
| 200 | { |
---|
| 201 | digest.hash.push_back(tmp_digest[i]); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | return 16; |
---|
| 205 | } |
---|
[313] | 206 | |
---|
[916] | 207 | std::string digestToString(const TComDigest &digest, Int numChar) |
---|
| 208 | { |
---|
| 209 | static const Char* hex = "0123456789abcdef"; |
---|
| 210 | std::string result; |
---|
[313] | 211 | |
---|
[916] | 212 | for(Int pos=0; pos<Int(digest.hash.size()); pos++) |
---|
| 213 | { |
---|
| 214 | if ((pos % numChar) == 0 && pos!=0 ) result += ','; |
---|
| 215 | result += hex[digest.hash[pos] >> 4]; |
---|
| 216 | result += hex[digest.hash[pos] & 0xf]; |
---|
| 217 | } |
---|
[313] | 218 | |
---|
[916] | 219 | return result; |
---|
[313] | 220 | } |
---|
[916] | 221 | |
---|
[313] | 222 | //! \} |
---|