xref: /aosp_15_r20/external/libaom/test/md5_helper.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_TEST_MD5_HELPER_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_TEST_MD5_HELPER_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_decoder.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "common/md5_utils.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker namespace libaom_test {
19*77c1e3ccSAndroid Build Coastguard Worker class MD5 {
20*77c1e3ccSAndroid Build Coastguard Worker  public:
MD5()21*77c1e3ccSAndroid Build Coastguard Worker   MD5() { MD5Init(&md5_); }
22*77c1e3ccSAndroid Build Coastguard Worker 
Add(const aom_image_t * img)23*77c1e3ccSAndroid Build Coastguard Worker   void Add(const aom_image_t *img) {
24*77c1e3ccSAndroid Build Coastguard Worker     for (int plane = 0; plane < 3; ++plane) {
25*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *buf = img->planes[plane];
26*77c1e3ccSAndroid Build Coastguard Worker       // Calculate the width and height to do the md5 check. For the chroma
27*77c1e3ccSAndroid Build Coastguard Worker       // plane, we never want to round down and thus skip a pixel so if
28*77c1e3ccSAndroid Build Coastguard Worker       // we are shifting by 1 (chroma_shift) we add 1 before doing the shift.
29*77c1e3ccSAndroid Build Coastguard Worker       // This works only for chroma_shift of 0 and 1.
30*77c1e3ccSAndroid Build Coastguard Worker       const int bytes_per_sample =
31*77c1e3ccSAndroid Build Coastguard Worker           (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
32*77c1e3ccSAndroid Build Coastguard Worker       const int h =
33*77c1e3ccSAndroid Build Coastguard Worker           plane ? (img->d_h + img->y_chroma_shift) >> img->y_chroma_shift
34*77c1e3ccSAndroid Build Coastguard Worker                 : img->d_h;
35*77c1e3ccSAndroid Build Coastguard Worker       const int w =
36*77c1e3ccSAndroid Build Coastguard Worker           (plane ? (img->d_w + img->x_chroma_shift) >> img->x_chroma_shift
37*77c1e3ccSAndroid Build Coastguard Worker                  : img->d_w) *
38*77c1e3ccSAndroid Build Coastguard Worker           bytes_per_sample;
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker       for (int y = 0; y < h; ++y) {
41*77c1e3ccSAndroid Build Coastguard Worker         MD5Update(&md5_, buf, w);
42*77c1e3ccSAndroid Build Coastguard Worker         buf += img->stride[plane];
43*77c1e3ccSAndroid Build Coastguard Worker       }
44*77c1e3ccSAndroid Build Coastguard Worker     }
45*77c1e3ccSAndroid Build Coastguard Worker   }
46*77c1e3ccSAndroid Build Coastguard Worker 
Add(const uint8_t * data,size_t size)47*77c1e3ccSAndroid Build Coastguard Worker   void Add(const uint8_t *data, size_t size) {
48*77c1e3ccSAndroid Build Coastguard Worker     MD5Update(&md5_, data, static_cast<uint32_t>(size));
49*77c1e3ccSAndroid Build Coastguard Worker   }
50*77c1e3ccSAndroid Build Coastguard Worker 
Get()51*77c1e3ccSAndroid Build Coastguard Worker   const char *Get() {
52*77c1e3ccSAndroid Build Coastguard Worker     static const char hex[16] = {
53*77c1e3ccSAndroid Build Coastguard Worker       '0', '1', '2', '3', '4', '5', '6', '7',
54*77c1e3ccSAndroid Build Coastguard Worker       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
55*77c1e3ccSAndroid Build Coastguard Worker     };
56*77c1e3ccSAndroid Build Coastguard Worker     uint8_t tmp[16];
57*77c1e3ccSAndroid Build Coastguard Worker     MD5Context ctx_tmp = md5_;
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker     MD5Final(tmp, &ctx_tmp);
60*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 16; i++) {
61*77c1e3ccSAndroid Build Coastguard Worker       res_[i * 2 + 0] = hex[tmp[i] >> 4];
62*77c1e3ccSAndroid Build Coastguard Worker       res_[i * 2 + 1] = hex[tmp[i] & 0xf];
63*77c1e3ccSAndroid Build Coastguard Worker     }
64*77c1e3ccSAndroid Build Coastguard Worker     res_[32] = 0;
65*77c1e3ccSAndroid Build Coastguard Worker 
66*77c1e3ccSAndroid Build Coastguard Worker     return res_;
67*77c1e3ccSAndroid Build Coastguard Worker   }
68*77c1e3ccSAndroid Build Coastguard Worker 
69*77c1e3ccSAndroid Build Coastguard Worker  protected:
70*77c1e3ccSAndroid Build Coastguard Worker   char res_[33];
71*77c1e3ccSAndroid Build Coastguard Worker   MD5Context md5_;
72*77c1e3ccSAndroid Build Coastguard Worker };
73*77c1e3ccSAndroid Build Coastguard Worker 
74*77c1e3ccSAndroid Build Coastguard Worker }  // namespace libaom_test
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_TEST_MD5_HELPER_H_
77