1 /* 2 * Copyright (c) 2016, 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 #ifndef AOM_TEST_MD5_HELPER_H_ 13 #define AOM_TEST_MD5_HELPER_H_ 14 15 #include "aom/aom_decoder.h" 16 #include "common/md5_utils.h" 17 18 namespace libaom_test { 19 class MD5 { 20 public: MD5()21 MD5() { MD5Init(&md5_); } 22 Add(const aom_image_t * img)23 void Add(const aom_image_t *img) { 24 for (int plane = 0; plane < 3; ++plane) { 25 const uint8_t *buf = img->planes[plane]; 26 // Calculate the width and height to do the md5 check. For the chroma 27 // plane, we never want to round down and thus skip a pixel so if 28 // we are shifting by 1 (chroma_shift) we add 1 before doing the shift. 29 // This works only for chroma_shift of 0 and 1. 30 const int bytes_per_sample = 31 (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1; 32 const int h = 33 plane ? (img->d_h + img->y_chroma_shift) >> img->y_chroma_shift 34 : img->d_h; 35 const int w = 36 (plane ? (img->d_w + img->x_chroma_shift) >> img->x_chroma_shift 37 : img->d_w) * 38 bytes_per_sample; 39 40 for (int y = 0; y < h; ++y) { 41 MD5Update(&md5_, buf, w); 42 buf += img->stride[plane]; 43 } 44 } 45 } 46 Add(const uint8_t * data,size_t size)47 void Add(const uint8_t *data, size_t size) { 48 MD5Update(&md5_, data, static_cast<uint32_t>(size)); 49 } 50 Get()51 const char *Get() { 52 static const char hex[16] = { 53 '0', '1', '2', '3', '4', '5', '6', '7', 54 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 55 }; 56 uint8_t tmp[16]; 57 MD5Context ctx_tmp = md5_; 58 59 MD5Final(tmp, &ctx_tmp); 60 for (int i = 0; i < 16; i++) { 61 res_[i * 2 + 0] = hex[tmp[i] >> 4]; 62 res_[i * 2 + 1] = hex[tmp[i] & 0xf]; 63 } 64 res_[32] = 0; 65 66 return res_; 67 } 68 69 protected: 70 char res_[33]; 71 MD5Context md5_; 72 }; 73 74 } // namespace libaom_test 75 76 #endif // AOM_TEST_MD5_HELPER_H_ 77