1 /*
2 * Copyright (c) 2017, 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 // Utility functions used by encoder binaries.
13
14 #include "examples/encoder_util.h"
15
16 #include <assert.h>
17 #include <string.h>
18
19 #include "aom/aom_integer.h"
20
21 #define mmin(a, b) ((a) < (b) ? (a) : (b))
22
find_mismatch_plane(const aom_image_t * const img1,const aom_image_t * const img2,int plane,int use_highbitdepth,int loc[4])23 static void find_mismatch_plane(const aom_image_t *const img1,
24 const aom_image_t *const img2, int plane,
25 int use_highbitdepth, int loc[4]) {
26 const unsigned char *const p1 = img1->planes[plane];
27 const int p1_stride = img1->stride[plane] >> use_highbitdepth;
28 const unsigned char *const p2 = img2->planes[plane];
29 const int p2_stride = img2->stride[plane] >> use_highbitdepth;
30 const uint32_t bsize = 64;
31 const int is_y_plane = (plane == AOM_PLANE_Y);
32 const uint32_t bsizex = is_y_plane ? bsize : bsize >> img1->x_chroma_shift;
33 const uint32_t bsizey = is_y_plane ? bsize : bsize >> img1->y_chroma_shift;
34 const uint32_t c_w =
35 is_y_plane ? img1->d_w
36 : (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
37 const uint32_t c_h =
38 is_y_plane ? img1->d_h
39 : (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
40 assert(img1->d_w == img2->d_w && img1->d_h == img2->d_h);
41 assert(img1->x_chroma_shift == img2->x_chroma_shift &&
42 img1->y_chroma_shift == img2->y_chroma_shift);
43 loc[0] = loc[1] = loc[2] = loc[3] = -1;
44 if (img1->monochrome && img2->monochrome && plane) return;
45 int match = 1;
46 uint32_t i, j;
47 for (i = 0; match && i < c_h; i += bsizey) {
48 for (j = 0; match && j < c_w; j += bsizex) {
49 const int si =
50 is_y_plane ? mmin(i + bsizey, c_h) - i : mmin(i + bsizey, c_h - i);
51 const int sj =
52 is_y_plane ? mmin(j + bsizex, c_w) - j : mmin(j + bsizex, c_w - j);
53 int k, l;
54 for (k = 0; match && k < si; ++k) {
55 for (l = 0; match && l < sj; ++l) {
56 const int row = i + k;
57 const int col = j + l;
58 const int offset1 = row * p1_stride + col;
59 const int offset2 = row * p2_stride + col;
60 const int val1 = use_highbitdepth
61 ? p1[2 * offset1] | (p1[2 * offset1 + 1] << 8)
62 : p1[offset1];
63 const int val2 = use_highbitdepth
64 ? p2[2 * offset2] | (p2[2 * offset2 + 1] << 8)
65 : p2[offset2];
66 if (val1 != val2) {
67 loc[0] = row;
68 loc[1] = col;
69 loc[2] = val1;
70 loc[3] = val2;
71 match = 0;
72 break;
73 }
74 }
75 }
76 }
77 }
78 }
79
find_mismatch_helper(const aom_image_t * const img1,const aom_image_t * const img2,int use_highbitdepth,int yloc[4],int uloc[4],int vloc[4])80 static void find_mismatch_helper(const aom_image_t *const img1,
81 const aom_image_t *const img2,
82 int use_highbitdepth, int yloc[4], int uloc[4],
83 int vloc[4]) {
84 find_mismatch_plane(img1, img2, AOM_PLANE_Y, use_highbitdepth, yloc);
85 find_mismatch_plane(img1, img2, AOM_PLANE_U, use_highbitdepth, uloc);
86 find_mismatch_plane(img1, img2, AOM_PLANE_V, use_highbitdepth, vloc);
87 }
88
aom_find_mismatch_high(const aom_image_t * const img1,const aom_image_t * const img2,int yloc[4],int uloc[4],int vloc[4])89 void aom_find_mismatch_high(const aom_image_t *const img1,
90 const aom_image_t *const img2, int yloc[4],
91 int uloc[4], int vloc[4]) {
92 find_mismatch_helper(img1, img2, 1, yloc, uloc, vloc);
93 }
94
aom_find_mismatch(const aom_image_t * const img1,const aom_image_t * const img2,int yloc[4],int uloc[4],int vloc[4])95 void aom_find_mismatch(const aom_image_t *const img1,
96 const aom_image_t *const img2, int yloc[4], int uloc[4],
97 int vloc[4]) {
98 find_mismatch_helper(img1, img2, 0, yloc, uloc, vloc);
99 }
100
aom_compare_img(const aom_image_t * const img1,const aom_image_t * const img2)101 int aom_compare_img(const aom_image_t *const img1,
102 const aom_image_t *const img2) {
103 assert(img1->cp == img2->cp);
104 assert(img1->tc == img2->tc);
105 assert(img1->mc == img2->mc);
106 assert(img1->monochrome == img2->monochrome);
107
108 int num_planes = img1->monochrome ? 1 : 3;
109
110 uint32_t l_w = img1->d_w;
111 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
112 const uint32_t c_h =
113 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
114 int match = 1;
115
116 match &= (img1->fmt == img2->fmt);
117 match &= (img1->d_w == img2->d_w);
118 match &= (img1->d_h == img2->d_h);
119 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
120 l_w *= 2;
121 c_w *= 2;
122 }
123
124 for (int plane = 0; plane < num_planes; ++plane) {
125 uint32_t height = plane ? c_h : img1->d_h;
126 uint32_t width = plane ? c_w : l_w;
127
128 for (uint32_t i = 0; i < height; ++i) {
129 match &=
130 (memcmp(img1->planes[plane] + i * img1->stride[plane],
131 img2->planes[plane] + i * img2->stride[plane], width) == 0);
132 }
133 }
134
135 return match;
136 }
137