source: SHVCSoftware/branches/SHM-4.1-dev/source/Lib/TLibCommon/TComPicYuvMD5.cpp @ 757

Last change on this file since 757 was 313, checked in by suehring, 11 years ago

set svn:eol-style=native property on all source files to do proper
automatic line break conversion on check-out and check-in

  • Property svn:eol-style set to native
File size: 7.1 KB
Line 
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-2013, 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
37//! \ingroup TLibCommon
38//! \{
39
40/**
41 * Update md5 using n samples from plane, each sample is adjusted to
42 * OUTBIT_BITDEPTH_DIV8.
43 */
44template<UInt OUTPUT_BITDEPTH_DIV8>
45static 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
61/**
62 * Update md5 with all samples in plane in raster order, each sample
63 * is adjusted to OUTBIT_BITDEPTH_DIV8.
64 */
65template<UInt OUTPUT_BITDEPTH_DIV8>
66static 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 pel's into UInt 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      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
86static void compCRC(Int bitdepth, const Pel* plane, UInt width, UInt height, UInt stride, UChar digest[16])
87{
88  UInt crcMsb;
89  UInt bitVal;
90  UInt crcVal = 0xffff;
91  UInt bitIdx;
92  for (UInt y = 0; y < height; y++)
93  {
94    for (UInt x = 0; x < width; x++)
95    {
96      // take CRC of first pictureData byte
97      for(bitIdx=0; bitIdx<8; bitIdx++)
98      {
99        crcMsb = (crcVal >> 15) & 1;
100        bitVal = (plane[y*stride+x] >> (7 - bitIdx)) & 1;
101        crcVal = (((crcVal << 1) + bitVal) & 0xffff) ^ (crcMsb * 0x1021);
102      }
103      // take CRC of second pictureData byte if bit depth is greater than 8-bits
104      if(bitdepth > 8)
105      {
106        for(bitIdx=0; bitIdx<8; bitIdx++)
107        {
108          crcMsb = (crcVal >> 15) & 1;
109          bitVal = (plane[y*stride+x] >> (15 - bitIdx)) & 1;
110          crcVal = (((crcVal << 1) + bitVal) & 0xffff) ^ (crcMsb * 0x1021);
111        }
112      }
113    }
114  }
115  for(bitIdx=0; bitIdx<16; bitIdx++)
116  {
117    crcMsb = (crcVal >> 15) & 1;
118    crcVal = ((crcVal << 1) & 0xffff) ^ (crcMsb * 0x1021);
119  }
120
121  digest[0] = (crcVal>>8)  & 0xff;
122  digest[1] =  crcVal      & 0xff;
123}
124
125void calcCRC(TComPicYuv& pic, UChar digest[3][16])
126{
127  UInt width = pic.getWidth();
128  UInt height = pic.getHeight();
129  UInt stride = pic.getStride();
130
131  compCRC(g_bitDepthY, pic.getLumaAddr(), width, height, stride, digest[0]);
132
133  width >>= 1;
134  height >>= 1;
135  stride >>= 1;
136
137  compCRC(g_bitDepthC, pic.getCbAddr(), width, height, stride, digest[1]);
138  compCRC(g_bitDepthC, pic.getCrAddr(), width, height, stride, digest[2]);
139}
140
141static void compChecksum(Int bitdepth, const Pel* plane, UInt width, UInt height, UInt stride, UChar digest[16])
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[0] = (checksum>>24) & 0xff;
161  digest[1] = (checksum>>16) & 0xff;
162  digest[2] = (checksum>>8)  & 0xff;
163  digest[3] =  checksum      & 0xff;
164}
165
166void calcChecksum(TComPicYuv& pic, UChar digest[3][16])
167{
168  UInt width = pic.getWidth();
169  UInt height = pic.getHeight();
170  UInt stride = pic.getStride();
171
172  compChecksum(g_bitDepthY, pic.getLumaAddr(), width, height, stride, digest[0]);
173
174  width >>= 1;
175  height >>= 1;
176  stride >>= 1;
177
178  compChecksum(g_bitDepthC, pic.getCbAddr(), width, height, stride, digest[1]);
179  compChecksum(g_bitDepthC, pic.getCrAddr(), width, height, stride, digest[2]);
180}
181/**
182 * Calculate the MD5sum of pic, storing the result in digest.
183 * MD5 calculation is performed on Y' then Cb, then Cr; each in raster order.
184 * Pel data is inserted into the MD5 function in little-endian byte order,
185 * using sufficient bytes to represent the picture bitdepth.  Eg, 10bit data
186 * uses little-endian two byte words; 8bit data uses single byte words.
187 */
188void calcMD5(TComPicYuv& pic, UChar digest[3][16])
189{
190  /* choose an md5_plane packing function based on the system bitdepth */
191  typedef void (*MD5PlaneFunc)(MD5&, const Pel*, UInt, UInt, UInt);
192  MD5PlaneFunc md5_plane_func;
193  md5_plane_func = g_bitDepthY <= 8 ? (MD5PlaneFunc)md5_plane<1> : (MD5PlaneFunc)md5_plane<2>;
194
195  MD5 md5Y, md5U, md5V;
196  UInt width = pic.getWidth();
197  UInt height = pic.getHeight();
198  UInt stride = pic.getStride();
199
200  md5_plane_func(md5Y, pic.getLumaAddr(), width, height, stride);
201  md5Y.finalize(digest[0]);
202
203  md5_plane_func = g_bitDepthC <= 8 ? (MD5PlaneFunc)md5_plane<1> : (MD5PlaneFunc)md5_plane<2>;
204  width >>= 1;
205  height >>= 1;
206  stride >>= 1;
207
208  md5_plane_func(md5U, pic.getCbAddr(), width, height, stride);
209  md5U.finalize(digest[1]);
210
211  md5_plane_func(md5V, pic.getCrAddr(), width, height, stride);
212  md5V.finalize(digest[2]);
213}
214//! \}
Note: See TracBrowser for help on using the repository browser.